1 2 #define TEST_NAME "verify1" 3 #include "cmptest.h" 4 5 int 6 main(void) 7 { 8 unsigned char *v16, *v16x; 9 unsigned char *v32, *v32x; 10 unsigned char *v64, *v64x; 11 uint32_t r; 12 uint8_t o; 13 int i; 14 15 v16 = (unsigned char *) sodium_malloc(16); 16 v16x = (unsigned char *) sodium_malloc(16); 17 v32 = (unsigned char *) sodium_malloc(32); 18 v32x = (unsigned char *) sodium_malloc(32); 19 v64 = (unsigned char *) sodium_malloc(64); 20 v64x = (unsigned char *) sodium_malloc(64); 21 for (i = 0; i < 10000; i++) { 22 randombytes_buf(v16, 16); 23 randombytes_buf(v32, 32); 24 randombytes_buf(v64, 64); 25 26 memcpy(v16x, v16, 16); 27 memcpy(v32x, v32, 32); 28 memcpy(v64x, v64, 64); 29 30 if (crypto_verify_16(v16, v16x) != 0 || 31 crypto_verify_32(v32, v32x) != 0 || 32 crypto_verify_64(v64, v64x) != 0 || 33 sodium_memcmp(v16, v16x, 16) != 0 || 34 sodium_memcmp(v32, v32x, 32) != 0 || 35 sodium_memcmp(v64, v64x, 64) != 0) { 36 printf("Failed\n"); 37 } 38 } 39 printf("OK\n"); 40 41 for (i = 0; i < 100000; i++) { 42 r = randombytes_random(); 43 o = (uint8_t) randombytes_random(); 44 if (o == 0) { 45 continue; 46 } 47 v16x[r & 15U] ^= o; 48 v32x[r & 31U] ^= o; 49 v64x[r & 63U] ^= o; 50 if (crypto_verify_16(v16, v16x) != -1 || 51 crypto_verify_32(v32, v32x) != -1 || 52 crypto_verify_64(v64, v64x) != -1 || 53 sodium_memcmp(v16, v16x, 16) != -1 || 54 sodium_memcmp(v32, v32x, 32) != -1 || 55 sodium_memcmp(v64, v64x, 64) != -1) { 56 printf("Failed\n"); 57 } 58 v16x[r & 15U] ^= o; 59 v32x[r & 31U] ^= o; 60 v64x[r & 63U] ^= o; 61 } 62 printf("OK\n"); 63 64 assert(crypto_verify_16_bytes() == 16U); 65 assert(crypto_verify_32_bytes() == 32U); 66 assert(crypto_verify_64_bytes() == 64U); 67 68 sodium_free(v16); 69 sodium_free(v16x); 70 sodium_free(v32); 71 sodium_free(v32x); 72 sodium_free(v64); 73 sodium_free(v64x); 74 75 return 0; 76 } 77