1 /* 2 * crypto_hash/try.c version 20090118 3 * D. J. Bernstein 4 * Public domain. 5 */ 6 7 #include <stdlib.h> 8 #include "crypto_hash.h" 9 10 extern unsigned char *alignedcalloc(unsigned long long); 11 12 const char *primitiveimplementation = crypto_hash_IMPLEMENTATION; 13 14 #define MAXTEST_BYTES (10000 + crypto_hash_BYTES) 15 #define CHECKSUM_BYTES 4096 16 #define TUNE_BYTES 1536 17 18 static unsigned char *h; 19 static unsigned char *h2; 20 static unsigned char *m; 21 static unsigned char *m2; 22 preallocate(void)23void preallocate(void) 24 { 25 } 26 allocate(void)27void allocate(void) 28 { 29 h = alignedcalloc(crypto_hash_BYTES); 30 h2 = alignedcalloc(crypto_hash_BYTES); 31 m = alignedcalloc(MAXTEST_BYTES); 32 m2 = alignedcalloc(MAXTEST_BYTES); 33 } 34 predoit(void)35void predoit(void) 36 { 37 } 38 doit(void)39void doit(void) 40 { 41 crypto_hash(h,m,TUNE_BYTES); 42 } 43 44 char checksum[crypto_hash_BYTES * 2 + 1]; 45 checksum_compute(void)46const char *checksum_compute(void) 47 { 48 long long i; 49 long long j; 50 51 for (i = 0;i < CHECKSUM_BYTES;++i) { 52 long long hlen = crypto_hash_BYTES; 53 long long mlen = i; 54 for (j = -16;j < 0;++j) h[j] = random(); 55 for (j = hlen;j < hlen + 16;++j) h[j] = random(); 56 for (j = -16;j < hlen + 16;++j) h2[j] = h[j]; 57 for (j = -16;j < 0;++j) m[j] = random(); 58 for (j = mlen;j < mlen + 16;++j) m[j] = random(); 59 for (j = -16;j < mlen + 16;++j) m2[j] = m[j]; 60 if (crypto_hash(h,m,mlen) != 0) return "crypto_hash returns nonzero"; 61 for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_hash writes to input"; 62 for (j = -16;j < 0;++j) if (h2[j] != h[j]) return "crypto_hash writes before output"; 63 for (j = hlen;j < hlen + 16;++j) if (h2[j] != h[j]) return "crypto_hash writes after output"; 64 if (crypto_hash(m2,m2,mlen) != 0) return "crypto_hash returns nonzero"; 65 for (j = 0;j < hlen;++j) if (m2[j] != h[j]) return "crypto_hash does not handle overlap"; 66 for (j = 0;j < mlen;++j) m[j] ^= h[j % hlen]; 67 m[mlen] = h[0]; 68 } 69 if (crypto_hash(h,m,CHECKSUM_BYTES) != 0) return "crypto_hash returns nonzero"; 70 71 for (i = 0;i < crypto_hash_BYTES;++i) { 72 checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)]; 73 checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]]; 74 } 75 checksum[2 * i] = 0; 76 return 0; 77 } 78