Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / FreeBSD / sha2.cpp
blob3012aca7d720721d0018548e3db58ea44d3168a6
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
6 #include <sys/param.h>
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>
17 #ifndef SHASIZE
18 #error SHASIZE must be defined
19 #endif
21 #define _SHA_CTX(x) SHA##x##_CTX
22 #define SHA_CTX(x) _SHA_CTX(x)
24 #define _SHA_DIGEST_LENGTH(x) SHA##x##_DIGEST_LENGTH
25 #define SHA_DIGEST_LENGTH(x) _SHA_DIGEST_LENGTH(x)
27 #define _SHA_DIGEST_STRING_LENGTH(x) SHA##x##_DIGEST_STRING_LENGTH
28 #define SHA_DIGEST_STRING_LENGTH(x) _SHA_DIGEST_STRING_LENGTH(x)
30 #define _SHA_Init(x) SHA##x##_Init
31 #define SHA_Init(x) _SHA_Init(x)
33 #define _SHA_Update(x) SHA##x##_Update
34 #define SHA_Update(x) _SHA_Update(x)
36 #define _SHA_Final(x) SHA##x##_Final
37 #define SHA_Final(x) _SHA_Final(x)
39 #define _SHA_End(x) SHA##x##_End
40 #define SHA_End(x) _SHA_End(x)
42 #define _SHA_File(x) SHA##x##_File
43 #define SHA_File(x) _SHA_File(x)
45 #define _SHA_FileChunk(x) SHA##x##_FileChunk
46 #define SHA_FileChunk(x) _SHA_FileChunk(x)
48 #define _SHA_Data(x) SHA##x##_Data
49 #define SHA_Data(x) _SHA_Data(x)
51 void 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);
58 SHA_Init(SHASIZE)(&ctx);
59 SHA_Update(SHASIZE)(&ctx, entropy, entropysz);
60 SHA_Final(SHASIZE)(digest, &ctx);
62 printf("test1: '");
63 for (size_t i = 0; i < digestsz; i++)
64 printf("%02x", digest[i]);
65 printf("'\n");
68 void 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);
74 SHA_Init(SHASIZE)(&ctx);
75 SHA_Update(SHASIZE)(&ctx, entropy, entropysz);
76 char *p = SHA_End(SHASIZE)(&ctx, digest);
77 assert(p == digest);
79 printf("test2: '%s'\n", digest);
82 void test3() {
83 SHA_CTX(SHASIZE) ctx;
84 uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
85 size_t entropysz = sizeof(entropy);
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);
92 printf("test3: '%s'\n", p);
94 free(p);
97 void test4() {
98 char digest[SHA_DIGEST_STRING_LENGTH(SHASIZE)];
100 char *p = SHA_File(SHASIZE)("/etc/fstab", digest);
101 assert(p == digest);
103 printf("test4: '%s'\n", p);
106 void test5() {
107 char *p = SHA_File(SHASIZE)("/etc/fstab", NULL);
108 assert(strlen(p) == SHA_DIGEST_STRING_LENGTH(SHASIZE) - 1);
110 printf("test5: '%s'\n", p);
112 free(p);
115 void test6() {
116 char digest[SHA_DIGEST_STRING_LENGTH(SHASIZE)];
118 char *p = SHA_FileChunk(SHASIZE)("/etc/fstab", digest, 10, 20);
119 assert(p == digest);
121 printf("test6: '%s'\n", p);
124 void test7() {
125 char *p = SHA_FileChunk(SHASIZE)("/etc/fstab", NULL, 10, 20);
126 assert(strlen(p) == SHA_DIGEST_STRING_LENGTH(SHASIZE) - 1);
128 printf("test7: '%s'\n", p);
130 free(p);
133 void 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);
138 char *p = SHA_Data(SHASIZE)(entropy, entropysz, digest);
139 assert(p == digest);
141 printf("test8: '%s'\n", p);
144 void test9() {
145 uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
146 size_t entropysz = sizeof(entropy);
148 char *p = SHA_Data(SHASIZE)(entropy, entropysz, NULL);
149 assert(strlen(p) == SHA_DIGEST_STRING_LENGTH(SHASIZE) - 1);
151 printf("test9: '%s'\n", p);
153 free(p);
156 int main(void) {
157 printf("SHA%d\n", SHASIZE);
159 test1();
160 test2();
161 test3();
162 test4();
163 test5();
164 test6();
165 test7();
166 test8();
167 test9();
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'
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'
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'
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'
213 return 0;