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

source code of compiler-rt/test/sanitizer_common/TestCases/FreeBSD/sha2.cpp