1 #define TEST_NAME "sodium_utils" 2 #include "cmptest.h" 3 4 int 5 main(void) 6 { 7 unsigned char buf_add[1000]; 8 unsigned char buf1[1000]; 9 unsigned char buf2[1000]; 10 unsigned char buf1_rev[1000]; 11 unsigned char buf2_rev[1000]; 12 unsigned char nonce[24]; 13 char nonce_hex[49]; 14 unsigned char *bin_padded; 15 size_t bin_len, bin_len2; 16 size_t bin_padded_len; 17 size_t bin_padded_maxlen; 18 size_t blocksize; 19 unsigned int i; 20 unsigned int j; 21 22 randombytes_buf(buf1, sizeof buf1); 23 memcpy(buf2, buf1, sizeof buf2); 24 printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1)); 25 sodium_memzero(buf1, 0U); 26 printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1)); 27 sodium_memzero(buf1, sizeof buf1 / 2); 28 printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1)); 29 printf("%d\n", sodium_memcmp(buf1, buf2, 0U)); 30 sodium_memzero(buf2, sizeof buf2 / 2); 31 printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1)); 32 33 memset(nonce, 0, sizeof nonce); 34 sodium_increment(nonce, sizeof nonce); 35 printf("%s\n", 36 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 37 memset(nonce, 255, sizeof nonce); 38 sodium_increment(nonce, sizeof nonce); 39 printf("%s\n", 40 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 41 nonce[1] = 1U; 42 sodium_increment(nonce, sizeof nonce); 43 printf("%s\n", 44 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 45 nonce[1] = 0U; 46 sodium_increment(nonce, sizeof nonce); 47 printf("%s\n", 48 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 49 nonce[0] = 255U; 50 nonce[2] = 255U; 51 sodium_increment(nonce, sizeof nonce); 52 printf("%s\n", 53 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 54 for (i = 0U; i < 1000U; i++) { 55 bin_len = (size_t) randombytes_uniform(sizeof buf1); 56 randombytes_buf(buf1, bin_len); 57 randombytes_buf(buf2, bin_len); 58 for (j = 0U; j < bin_len; j++) { 59 buf1_rev[bin_len - 1 - j] = buf1[j]; 60 buf2_rev[bin_len - 1 - j] = buf2[j]; 61 } 62 if (memcmp(buf1_rev, buf2_rev, bin_len) * 63 sodium_compare(buf1, buf2, bin_len) < 64 0) { 65 printf("sodium_compare() failure with length=%u\n", 66 (unsigned int) bin_len); 67 } 68 memcpy(buf1, buf2, bin_len); 69 if (sodium_compare(buf1, buf2, bin_len)) { 70 printf("sodium_compare() equality failure with length=%u\n", 71 (unsigned int) bin_len); 72 } 73 } 74 memset(buf1, 0, sizeof buf1); 75 if (sodium_is_zero(buf1, sizeof buf1) != 1) { 76 printf("sodium_is_zero() failed\n"); 77 } 78 for (i = 0U; i < sizeof buf1; i++) { 79 buf1[i]++; 80 if (sodium_is_zero(buf1, sizeof buf1) != 0) { 81 printf("sodium_is_zero() failed\n"); 82 } 83 buf1[i]--; 84 } 85 bin_len = randombytes_uniform(sizeof buf1); 86 randombytes_buf(buf1, bin_len); 87 memcpy(buf2, buf1, bin_len); 88 memset(buf_add, 0, bin_len); 89 j = randombytes_uniform(10000); 90 for (i = 0U; i < j; i++) { 91 sodium_increment(buf1, bin_len); 92 sodium_increment(buf_add, bin_len); 93 } 94 sodium_add(buf2, buf_add, bin_len); 95 if (sodium_compare(buf1, buf2, bin_len) != 0) { 96 printf("sodium_add() failed\n"); 97 } 98 bin_len = randombytes_uniform(sizeof buf1); 99 randombytes_buf(buf1, bin_len); 100 memcpy(buf2, buf1, bin_len); 101 memset(buf_add, 0xff, bin_len); 102 sodium_increment(buf2, bin_len); 103 sodium_increment(buf2, 0U); 104 sodium_add(buf2, buf_add, bin_len); 105 sodium_add(buf2, buf_add, 0U); 106 if (sodium_compare(buf1, buf2, bin_len) != 0) { 107 printf("sodium_add() failed\n"); 108 } 109 110 assert(sizeof nonce >= 24U); 111 memset(nonce, 0xfe, 24U); 112 memset(nonce, 0xff, 6U); 113 sodium_increment(nonce, 8U); 114 printf("%s\n", 115 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 116 memset(nonce, 0xfe, 24U); 117 memset(nonce, 0xff, 10U); 118 sodium_increment(nonce, 12U); 119 printf("%s\n", 120 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 121 memset(nonce, 0xff, 22U); 122 sodium_increment(nonce, 24U); 123 printf("%s\n", 124 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 125 126 assert(sizeof nonce >= 24U); 127 memset(nonce, 0xfe, 24U); 128 memset(nonce, 0xff, 6U); 129 sodium_add(nonce, nonce, 7U); 130 sodium_add(nonce, nonce, 8U); 131 printf("%s\n", 132 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 133 memset(nonce, 0xfe, 24U); 134 memset(nonce, 0xff, 10U); 135 sodium_add(nonce, nonce, 11U); 136 sodium_add(nonce, nonce, 12U); 137 printf("%s\n", 138 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 139 memset(nonce, 0xff, 22U); 140 sodium_add(nonce, nonce, 23U); 141 sodium_add(nonce, nonce, 24U); 142 printf("%s\n", 143 sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce)); 144 145 for (i = 0; i < 2000U; i++) { 146 bin_len = randombytes_uniform(200U); 147 blocksize = 1U + randombytes_uniform(100U); 148 bin_padded_maxlen = bin_len + (blocksize - bin_len % blocksize); 149 bin_padded = (unsigned char *) sodium_malloc(bin_padded_maxlen); 150 randombytes_buf(bin_padded, bin_padded_maxlen); 151 152 assert(sodium_pad(&bin_padded_len, bin_padded, bin_len, 153 blocksize, bin_padded_maxlen - 1U) == -1); 154 assert(sodium_pad(NULL, bin_padded, bin_len, 155 blocksize, bin_padded_maxlen + 1U) == 0); 156 assert(sodium_pad(&bin_padded_len, bin_padded, bin_len, 157 blocksize, bin_padded_maxlen + 1U) == 0); 158 assert(sodium_pad(&bin_padded_len, bin_padded, bin_len, 159 0U, bin_padded_maxlen) == -1); 160 assert(sodium_pad(&bin_padded_len, bin_padded, bin_len, 161 blocksize, bin_padded_maxlen) == 0); 162 assert(bin_padded_len == bin_padded_maxlen); 163 164 assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len, 165 bin_padded_len + 1U) == -1); 166 assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len, 167 0U) == -1); 168 assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len, 169 blocksize) == 0); 170 assert(bin_len2 == bin_len); 171 172 sodium_free(bin_padded); 173 } 174 175 sodium_stackzero(512); 176 177 return 0; 178 } 179