File: test_sha1.c

package info (click to toggle)
libstrophe 0.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,356 kB
  • sloc: ansic: 16,695; makefile: 301; sh: 61
file content (93 lines) | stat: -rw-r--r-- 2,970 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Tests for Steve Reid's public domain SHA-1 implementation */
/* This file is in the public domain */

/* gcc -o test_sha1 -I./src tests/test_sha1.c src/sha1.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sha1.h"
#include "test.h"

/* Test Vectors (from FIPS PUB 180-1) */
static char *test_data[] = {
    "abc", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
    "A million repetitions of 'a'",
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"};
static char *test_results[] = {"A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
                               "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1",
                               "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F",
                               "AD5B3FDB CB526778 C2839D2F 151EA753 995E26A0"};

static void digest_to_hex(const uint8_t *digest, char *output)
{
    int i, j;
    char *c = output;

    for (i = 0; i < SHA1_DIGEST_SIZE / 4; i++) {
        for (j = 0; j < 4; j++) {
            sprintf(c, "%02X", digest[i * 4 + j]);
            c += 2;
        }
        sprintf(c, " ");
        c += 1;
    }
    *(c - 1) = '\0';
}

int main()
{
    size_t k;
    SHA1_CTX context;
    uint8_t digest[20];
    char output[80];
    char *copy;

    fprintf(stdout, "verifying SHA-1 implementation... ");

    for (k = 0; k < ARRAY_SIZE(test_data); k++) {
        if (k == 2) {
            /* this case will be checked below */
            continue;
        }
        copy = strdup(test_data[k]);
        crypto_SHA1_Init(&context);
        crypto_SHA1_Update(&context, (uint8_t *)test_data[k],
                           strlen(test_data[k]));
        crypto_SHA1_Final(&context, digest);
        digest_to_hex(digest, output);

        if (strcmp(output, test_results[k])) {
            fprintf(stdout, "FAIL\n");
            fprintf(stderr, "* hash of \"%s\" incorrect:\n", test_data[k]);
            fprintf(stderr, "\t%s returned\n", output);
            fprintf(stderr, "\t%s is correct\n", test_results[k]);
            return (1);
        }
        if (strcmp(copy, test_data[k])) {
            fprintf(stdout, "FAIL\n");
            fprintf(stdout, "* original string was changed by SHA1\n");
            return (1);
        }
        free(copy);
    }
    /* million 'a' vector we feed separately */
    crypto_SHA1_Init(&context);
    for (k = 0; k < 1000000; k++)
        crypto_SHA1_Update(&context, (uint8_t *)"a", 1);
    crypto_SHA1_Final(&context, digest);
    digest_to_hex(digest, output);
    if (strcmp(output, test_results[2])) {
        fprintf(stdout, "FAIL\n");
        fprintf(stderr, "* hash of \"%s\" incorrect:\n", test_data[2]);
        fprintf(stderr, "\t%s returned\n", output);
        fprintf(stderr, "\t%s is correct\n", test_results[2]);
        return (1);
    }

    /* success */
    fprintf(stdout, "ok\n");
    return (0);
}