Debian Codegraph @

test/cryptounittest/test-digest-sha1.c

raw
1 /**
2 * \file test-digest-sha1.c
3 *
4 * \brief The SHA-1 message digest unit-tests
5 *
6 * \warning SHA-1 is considered a weak message digest and its use constitutes a
7 * security risk! We recommend considering stronger message digests
8 * instead!
9 *
10 * \copyright 2022 Joseph Benden <[email protected]>
11 *
12 * \license GPL-2.0-OR-LATER
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <strings.h>
23 #include <stdlib.h>
24
25 #include "aircrack-ng/crypto/crypto.h"
26 #include "tests.h"
27
28 // clang-format off
29 /*
30 * FIPS-180-1 test vectors
31 */
32 static const unsigned char sha1_test_buf[3][57] =
33 {
34 { "abc" },
35 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
36 { "" }
37 };
38
39 static const size_t sha1_test_buflen[3] =
40 {
41 3, 56, 1000
42 };
43
44 static const unsigned char sha1_test_sum[3][20] =
45 {
46 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
47 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
48 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
49 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
50 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
51 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
52 };
53 // clang-format on
54
55 int main(int argc, char ** argv)
56 {
57 int i, error = 0;
58 uint8_t sha1sum[DIGEST_SHA1_MAC_LEN];
59
60 (void) argc;
61
62 for (i = 0; i < 2; i++)
63 {
64 error |= Digest_SHA1(sha1_test_buf[i], sha1_test_buflen[i], sha1sum);
65 error |= test(sha1sum, sha1_test_sum[i], DIGEST_SHA1_MAC_LEN, argv[0]);
66 }
67
68 for (i = 0; i < 2; i++)
69 {
70 error |= Digest_SHA1_Vector(
71 1,
72 (const uint8_t * []){(uint8_t *) &sha1_test_buf[i]},
73 &sha1_test_buflen[i],
74 sha1sum);
75 error |= test(sha1sum, sha1_test_sum[i], DIGEST_SHA1_MAC_LEN, argv[0]);
76 }
77
78 Digest_SHA1_CTX * ctx = Digest_SHA1_Create();
79 uint8_t buf[1000];
80 size_t buflen = sizeof(buf);
81
82 memset(buf, 'a', buflen);
83
84 error |= Digest_SHA1_Init(ctx);
85 for (i = 0; i < 1000; i++)
86 {
87 error |= Digest_SHA1_Update(ctx, buf, buflen);
88 }
89 Digest_SHA1_Finish(ctx, sha1sum);
90
91 error |= test(sha1sum, sha1_test_sum[2], DIGEST_SHA1_MAC_LEN, argv[0]);
92
93 Digest_SHA1_Destroy(ctx);
94
95 return error;
96 }