1 | // RUN: %clangxx -O0 -g %s -DSHASIZE=224 -o %t && %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-224 |
2 | // RUN: %clangxx -O0 -g %s -DSHASIZE=256 -o %t && %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-256 |
3 | // RUN: %clangxx -O0 -g %s -DSHASIZE=384 -o %t && %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-384 |
4 | // RUN: %clangxx -O0 -g %s -DSHASIZE=512 -o %t && %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-512 |
5 | |
6 | #include <sys/param.h> |
7 | |
8 | #include <assert.h> |
9 | #include <endian.h> |
10 | #include <sha2.h> |
11 | #include <stdio.h> |
12 | #include <stdlib.h> |
13 | #include <string.h> |
14 | |
15 | #ifndef SHASIZE |
16 | #error SHASIZE must be defined |
17 | #endif |
18 | |
19 | #define _SHA_CTX(x) SHA##x##_CTX |
20 | #define SHA_CTX(x) _SHA_CTX(x) |
21 | |
22 | #define _SHA_DIGEST_LENGTH(x) SHA##x##_DIGEST_LENGTH |
23 | #define SHA_DIGEST_LENGTH(x) _SHA_DIGEST_LENGTH(x) |
24 | |
25 | #define _SHA_DIGEST_STRING_LENGTH(x) SHA##x##_DIGEST_STRING_LENGTH |
26 | #define SHA_DIGEST_STRING_LENGTH(x) _SHA_DIGEST_STRING_LENGTH(x) |
27 | |
28 | #define _SHA_Init(x) SHA##x##_Init |
29 | #define SHA_Init(x) _SHA_Init(x) |
30 | |
31 | #define _SHA_Update(x) SHA##x##_Update |
32 | #define SHA_Update(x) _SHA_Update(x) |
33 | |
34 | #define _SHA_Final(x) SHA##x##_Final |
35 | #define SHA_Final(x) _SHA_Final(x) |
36 | |
37 | #define _SHA_End(x) SHA##x##_End |
38 | #define SHA_End(x) _SHA_End(x) |
39 | |
40 | #define _SHA_File(x) SHA##x##_File |
41 | #define SHA_File(x) _SHA_File(x) |
42 | |
43 | #define _SHA_FileChunk(x) SHA##x##_FileChunk |
44 | #define SHA_FileChunk(x) _SHA_FileChunk(x) |
45 | |
46 | #define _SHA_Data(x) SHA##x##_Data |
47 | #define SHA_Data(x) _SHA_Data(x) |
48 | |
49 | void test1() { |
50 | SHA_CTX(SHASIZE) ctx; |
51 | uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
52 | uint8_t digest[SHA_DIGEST_LENGTH(SHASIZE)]; |
53 | |
54 | SHA_Init(SHASIZE)(&ctx); |
55 | SHA_Update(SHASIZE)(&ctx, entropy, __arraycount(entropy)); |
56 | SHA_Final(SHASIZE)(digest, &ctx); |
57 | |
58 | printf(format: "test1: '" ); |
59 | for (size_t i = 0; i < __arraycount(digest); i++) |
60 | printf("%02x" , digest[i]); |
61 | printf(format: "'\n" ); |
62 | } |
63 | |
64 | void test2() { |
65 | SHA_CTX(SHASIZE) ctx; |
66 | uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
67 | char digest[SHA_DIGEST_STRING_LENGTH(SHASIZE)]; |
68 | |
69 | SHA_Init(SHASIZE)(&ctx); |
70 | SHA_Update(SHASIZE)(&ctx, entropy, __arraycount(entropy)); |
71 | char *p = SHA_End(SHASIZE)(&ctx, digest); |
72 | assert(p == digest); |
73 | |
74 | printf("test2: '%s'\n" , digest); |
75 | } |
76 | |
77 | void test3() { |
78 | SHA_CTX(SHASIZE) ctx; |
79 | uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
80 | |
81 | SHA_Init(SHASIZE)(&ctx); |
82 | SHA_Update(SHASIZE)(&ctx, entropy, __arraycount(entropy)); |
83 | char *p = SHA_End(SHASIZE)(&ctx, NULL); |
84 | assert(strlen(p) == SHA_DIGEST_STRING_LENGTH(SHASIZE) - 1); |
85 | |
86 | printf(format: "test3: '%s'\n" , p); |
87 | |
88 | free(ptr: p); |
89 | } |
90 | |
91 | void test4() { |
92 | char digest[SHA_DIGEST_STRING_LENGTH(SHASIZE)]; |
93 | |
94 | char *p = SHA_File(SHASIZE)("/etc/fstab" , digest); |
95 | assert(p == digest); |
96 | |
97 | printf(format: "test4: '%s'\n" , p); |
98 | } |
99 | |
100 | void test5() { |
101 | char *p = SHA_File(SHASIZE)("/etc/fstab" , NULL); |
102 | assert(strlen(p) == SHA_DIGEST_STRING_LENGTH(SHASIZE) - 1); |
103 | |
104 | printf(format: "test5: '%s'\n" , p); |
105 | |
106 | free(ptr: p); |
107 | } |
108 | |
109 | void test6() { |
110 | char digest[SHA_DIGEST_STRING_LENGTH(SHASIZE)]; |
111 | |
112 | char *p = SHA_FileChunk(SHASIZE)("/etc/fstab" , digest, 10, 20); |
113 | assert(p == digest); |
114 | |
115 | printf(format: "test6: '%s'\n" , p); |
116 | } |
117 | |
118 | void test7() { |
119 | char *p = SHA_FileChunk(SHASIZE)("/etc/fstab" , NULL, 10, 20); |
120 | assert(strlen(p) == SHA_DIGEST_STRING_LENGTH(SHASIZE) - 1); |
121 | |
122 | printf(format: "test7: '%s'\n" , p); |
123 | |
124 | free(ptr: p); |
125 | } |
126 | |
127 | void test8() { |
128 | uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
129 | char digest[SHA_DIGEST_STRING_LENGTH(SHASIZE)]; |
130 | |
131 | char *p = SHA_Data(SHASIZE)(entropy, __arraycount(entropy), digest); |
132 | assert(p == digest); |
133 | |
134 | printf(format: "test8: '%s'\n" , p); |
135 | } |
136 | |
137 | void test9() { |
138 | uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
139 | |
140 | char *p = SHA_Data(SHASIZE)(entropy, __arraycount(entropy), NULL); |
141 | assert(strlen(p) == SHA_DIGEST_STRING_LENGTH(SHASIZE) - 1); |
142 | |
143 | printf(format: "test9: '%s'\n" , p); |
144 | |
145 | free(ptr: p); |
146 | } |
147 | |
148 | int main(void) { |
149 | printf("SHA" ___STRING(SHASIZE) "\n" ); |
150 | |
151 | test1(); |
152 | test2(); |
153 | test3(); |
154 | test4(); |
155 | test5(); |
156 | test6(); |
157 | test7(); |
158 | test8(); |
159 | test9(); |
160 | |
161 | // CHECK-224: SHA224 |
162 | // CHECK-224: test1: '760dfb93100a6bf5996c90f678e529dc945bb2f74a211eedcf0f3a48' |
163 | // CHECK-224: test2: '760dfb93100a6bf5996c90f678e529dc945bb2f74a211eedcf0f3a48' |
164 | // CHECK-224: test3: '760dfb93100a6bf5996c90f678e529dc945bb2f74a211eedcf0f3a48' |
165 | // CHECK-224: test4: '{{.*}}' |
166 | // CHECK-224: test5: '{{.*}}' |
167 | // CHECK-224: test6: '{{.*}}' |
168 | // CHECK-224: test7: '{{.*}}' |
169 | // CHECK-224: test8: '760dfb93100a6bf5996c90f678e529dc945bb2f74a211eedcf0f3a48' |
170 | // CHECK-224: test9: '760dfb93100a6bf5996c90f678e529dc945bb2f74a211eedcf0f3a48' |
171 | |
172 | // CHECK-256: SHA256 |
173 | // CHECK-256: test1: 'bb000ddd92a0a2a346f0b531f278af06e370f86932ccafccc892d68d350f80f8' |
174 | // CHECK-256: test2: 'bb000ddd92a0a2a346f0b531f278af06e370f86932ccafccc892d68d350f80f8' |
175 | // CHECK-256: test3: 'bb000ddd92a0a2a346f0b531f278af06e370f86932ccafccc892d68d350f80f8' |
176 | // CHECK-256: test4: '{{.*}}' |
177 | // CHECK-256: test5: '{{.*}}' |
178 | // CHECK-256: test6: '{{.*}}' |
179 | // CHECK-256: test7: '{{.*}}' |
180 | // CHECK-256: test8: 'bb000ddd92a0a2a346f0b531f278af06e370f86932ccafccc892d68d350f80f8' |
181 | // CHECK-256: test9: 'bb000ddd92a0a2a346f0b531f278af06e370f86932ccafccc892d68d350f80f8' |
182 | |
183 | // CHECK-384: SHA384 |
184 | // CHECK-384: test1: 'f450c023b168ebd56ff916ca9b1f1f0010b8c592d28205cc91fa3056f629eed108e8bac864f01ca37a3edee596739e12' |
185 | // CHECK-384: test2: 'f450c023b168ebd56ff916ca9b1f1f0010b8c592d28205cc91fa3056f629eed108e8bac864f01ca37a3edee596739e12' |
186 | // CHECK-384: test3: 'f450c023b168ebd56ff916ca9b1f1f0010b8c592d28205cc91fa3056f629eed108e8bac864f01ca37a3edee596739e12' |
187 | // CHECK-384: test4: '{{.*}}' |
188 | // CHECK-384: test5: '{{.*}}' |
189 | // CHECK-384: test6: '{{.*}}' |
190 | // CHECK-384: test7: '{{.*}}' |
191 | // CHECK-384: test8: 'f450c023b168ebd56ff916ca9b1f1f0010b8c592d28205cc91fa3056f629eed108e8bac864f01ca37a3edee596739e12' |
192 | // CHECK-384: test9: 'f450c023b168ebd56ff916ca9b1f1f0010b8c592d28205cc91fa3056f629eed108e8bac864f01ca37a3edee596739e12' |
193 | |
194 | // CHECK-512: SHA512 |
195 | // CHECK-512: test1: '0e3f68731c0e2a6a4eab5d713c9a80dc78086b5fa7d2b5ab127277958e68d1b1dee1882b083b0106cd4319de42c0c8f452871364f5baa8a6379690612c6b844e' |
196 | // CHECK-512: test2: '0e3f68731c0e2a6a4eab5d713c9a80dc78086b5fa7d2b5ab127277958e68d1b1dee1882b083b0106cd4319de42c0c8f452871364f5baa8a6379690612c6b844e' |
197 | // CHECK-512: test3: '0e3f68731c0e2a6a4eab5d713c9a80dc78086b5fa7d2b5ab127277958e68d1b1dee1882b083b0106cd4319de42c0c8f452871364f5baa8a6379690612c6b844e' |
198 | // CHECK-512: test4: '{{.*}}' |
199 | // CHECK-512: test5: '{{.*}}' |
200 | // CHECK-512: test6: '{{.*}}' |
201 | // CHECK-512: test7: '{{.*}}' |
202 | // CHECK-512: test8: '0e3f68731c0e2a6a4eab5d713c9a80dc78086b5fa7d2b5ab127277958e68d1b1dee1882b083b0106cd4319de42c0c8f452871364f5baa8a6379690612c6b844e' |
203 | // CHECK-512: test9: '0e3f68731c0e2a6a4eab5d713c9a80dc78086b5fa7d2b5ab127277958e68d1b1dee1882b083b0106cd4319de42c0c8f452871364f5baa8a6379690612c6b844e' |
204 | |
205 | return 0; |
206 | } |
207 | |