summaryrefslogtreecommitdiff
path: root/auto_tests/crypto_test.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-16 13:11:09 -0400
committerirungentoo <irungentoo@gmail.com>2013-08-16 13:11:09 -0400
commit88ff81d9def5efe69cbaf91aa41906177ba7dde9 (patch)
treecb9f149e438bcd1f18d8c1eb5d8be6b0a22f58a4 /auto_tests/crypto_test.c
parentc5af8f44a9d040a0bbe0442ec074d9fc8562dd32 (diff)
Passed everything through astyle.
Diffstat (limited to 'auto_tests/crypto_test.c')
-rw-r--r--auto_tests/crypto_test.c157
1 files changed, 80 insertions, 77 deletions
diff --git a/auto_tests/crypto_test.c b/auto_tests/crypto_test.c
index 8b5397e4..6b9f6113 100644
--- a/auto_tests/crypto_test.c
+++ b/auto_tests/crypto_test.c
@@ -10,8 +10,8 @@
10void rand_bytes(uint8_t *b, size_t blen) 10void rand_bytes(uint8_t *b, size_t blen)
11{ 11{
12 size_t i; 12 size_t i;
13 for (i = 0; i < blen; i++) 13
14 { 14 for (i = 0; i < blen; i++) {
15 b[i] = rand(); 15 b[i] = rand();
16 } 16 }
17} 17}
@@ -19,65 +19,65 @@ void rand_bytes(uint8_t *b, size_t blen)
19// These test vectors are from libsodium's test suite 19// These test vectors are from libsodium's test suite
20 20
21unsigned char alicesk[32] = { 21unsigned char alicesk[32] = {
22 0x77,0x07,0x6d,0x0a,0x73,0x18,0xa5,0x7d, 22 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d,
23 0x3c,0x16,0xc1,0x72,0x51,0xb2,0x66,0x45, 23 0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45,
24 0xdf,0x4c,0x2f,0x87,0xeb,0xc0,0x99,0x2a, 24 0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a,
25 0xb1,0x77,0xfb,0xa5,0x1d,0xb9,0x2c,0x2a 25 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a
26}; 26};
27 27
28unsigned char bobpk[32] = { 28unsigned char bobpk[32] = {
29 0xde,0x9e,0xdb,0x7d,0x7b,0x7d,0xc1,0xb4, 29 0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4,
30 0xd3,0x5b,0x61,0xc2,0xec,0xe4,0x35,0x37, 30 0xd3, 0x5b, 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37,
31 0x3f,0x83,0x43,0xc8,0x5b,0x78,0x67,0x4d, 31 0x3f, 0x83, 0x43, 0xc8, 0x5b, 0x78, 0x67, 0x4d,
32 0xad,0xfc,0x7e,0x14,0x6f,0x88,0x2b,0x4f 32 0xad, 0xfc, 0x7e, 0x14, 0x6f, 0x88, 0x2b, 0x4f
33}; 33};
34 34
35unsigned char nonce[24] = { 35unsigned char nonce[24] = {
36 0x69,0x69,0x6e,0xe9,0x55,0xb6,0x2b,0x73, 36 0x69, 0x69, 0x6e, 0xe9, 0x55, 0xb6, 0x2b, 0x73,
37 0xcd,0x62,0xbd,0xa8,0x75,0xfc,0x73,0xd6, 37 0xcd, 0x62, 0xbd, 0xa8, 0x75, 0xfc, 0x73, 0xd6,
38 0x82,0x19,0xe0,0x03,0x6b,0x7a,0x0b,0x37 38 0x82, 0x19, 0xe0, 0x03, 0x6b, 0x7a, 0x0b, 0x37
39}; 39};
40 40
41unsigned char test_m[131] = { 41unsigned char test_m[131] = {
42 0xbe,0x07,0x5f,0xc5,0x3c,0x81,0xf2,0xd5, 42 0xbe, 0x07, 0x5f, 0xc5, 0x3c, 0x81, 0xf2, 0xd5,
43 0xcf,0x14,0x13,0x16,0xeb,0xeb,0x0c,0x7b, 43 0xcf, 0x14, 0x13, 0x16, 0xeb, 0xeb, 0x0c, 0x7b,
44 0x52,0x28,0xc5,0x2a,0x4c,0x62,0xcb,0xd4, 44 0x52, 0x28, 0xc5, 0x2a, 0x4c, 0x62, 0xcb, 0xd4,
45 0x4b,0x66,0x84,0x9b,0x64,0x24,0x4f,0xfc, 45 0x4b, 0x66, 0x84, 0x9b, 0x64, 0x24, 0x4f, 0xfc,
46 0xe5,0xec,0xba,0xaf,0x33,0xbd,0x75,0x1a, 46 0xe5, 0xec, 0xba, 0xaf, 0x33, 0xbd, 0x75, 0x1a,
47 0x1a,0xc7,0x28,0xd4,0x5e,0x6c,0x61,0x29, 47 0x1a, 0xc7, 0x28, 0xd4, 0x5e, 0x6c, 0x61, 0x29,
48 0x6c,0xdc,0x3c,0x01,0x23,0x35,0x61,0xf4, 48 0x6c, 0xdc, 0x3c, 0x01, 0x23, 0x35, 0x61, 0xf4,
49 0x1d,0xb6,0x6c,0xce,0x31,0x4a,0xdb,0x31, 49 0x1d, 0xb6, 0x6c, 0xce, 0x31, 0x4a, 0xdb, 0x31,
50 0x0e,0x3b,0xe8,0x25,0x0c,0x46,0xf0,0x6d, 50 0x0e, 0x3b, 0xe8, 0x25, 0x0c, 0x46, 0xf0, 0x6d,
51 0xce,0xea,0x3a,0x7f,0xa1,0x34,0x80,0x57, 51 0xce, 0xea, 0x3a, 0x7f, 0xa1, 0x34, 0x80, 0x57,
52 0xe2,0xf6,0x55,0x6a,0xd6,0xb1,0x31,0x8a, 52 0xe2, 0xf6, 0x55, 0x6a, 0xd6, 0xb1, 0x31, 0x8a,
53 0x02,0x4a,0x83,0x8f,0x21,0xaf,0x1f,0xde, 53 0x02, 0x4a, 0x83, 0x8f, 0x21, 0xaf, 0x1f, 0xde,
54 0x04,0x89,0x77,0xeb,0x48,0xf5,0x9f,0xfd, 54 0x04, 0x89, 0x77, 0xeb, 0x48, 0xf5, 0x9f, 0xfd,
55 0x49,0x24,0xca,0x1c,0x60,0x90,0x2e,0x52, 55 0x49, 0x24, 0xca, 0x1c, 0x60, 0x90, 0x2e, 0x52,
56 0xf0,0xa0,0x89,0xbc,0x76,0x89,0x70,0x40, 56 0xf0, 0xa0, 0x89, 0xbc, 0x76, 0x89, 0x70, 0x40,
57 0xe0,0x82,0xf9,0x37,0x76,0x38,0x48,0x64, 57 0xe0, 0x82, 0xf9, 0x37, 0x76, 0x38, 0x48, 0x64,
58 0x5e,0x07,0x05 58 0x5e, 0x07, 0x05
59}; 59};
60 60
61unsigned char test_c[147] = { 61unsigned char test_c[147] = {
62 0xf3,0xff,0xc7,0x70,0x3f,0x94,0x00,0xe5, 62 0xf3, 0xff, 0xc7, 0x70, 0x3f, 0x94, 0x00, 0xe5,
63 0x2a,0x7d,0xfb,0x4b,0x3d,0x33,0x05,0xd9, 63 0x2a, 0x7d, 0xfb, 0x4b, 0x3d, 0x33, 0x05, 0xd9,
64 0x8e,0x99,0x3b,0x9f,0x48,0x68,0x12,0x73, 64 0x8e, 0x99, 0x3b, 0x9f, 0x48, 0x68, 0x12, 0x73,
65 0xc2,0x96,0x50,0xba,0x32,0xfc,0x76,0xce, 65 0xc2, 0x96, 0x50, 0xba, 0x32, 0xfc, 0x76, 0xce,
66 0x48,0x33,0x2e,0xa7,0x16,0x4d,0x96,0xa4, 66 0x48, 0x33, 0x2e, 0xa7, 0x16, 0x4d, 0x96, 0xa4,
67 0x47,0x6f,0xb8,0xc5,0x31,0xa1,0x18,0x6a, 67 0x47, 0x6f, 0xb8, 0xc5, 0x31, 0xa1, 0x18, 0x6a,
68 0xc0,0xdf,0xc1,0x7c,0x98,0xdc,0xe8,0x7b, 68 0xc0, 0xdf, 0xc1, 0x7c, 0x98, 0xdc, 0xe8, 0x7b,
69 0x4d,0xa7,0xf0,0x11,0xec,0x48,0xc9,0x72, 69 0x4d, 0xa7, 0xf0, 0x11, 0xec, 0x48, 0xc9, 0x72,
70 0x71,0xd2,0xc2,0x0f,0x9b,0x92,0x8f,0xe2, 70 0x71, 0xd2, 0xc2, 0x0f, 0x9b, 0x92, 0x8f, 0xe2,
71 0x27,0x0d,0x6f,0xb8,0x63,0xd5,0x17,0x38, 71 0x27, 0x0d, 0x6f, 0xb8, 0x63, 0xd5, 0x17, 0x38,
72 0xb4,0x8e,0xee,0xe3,0x14,0xa7,0xcc,0x8a, 72 0xb4, 0x8e, 0xee, 0xe3, 0x14, 0xa7, 0xcc, 0x8a,
73 0xb9,0x32,0x16,0x45,0x48,0xe5,0x26,0xae, 73 0xb9, 0x32, 0x16, 0x45, 0x48, 0xe5, 0x26, 0xae,
74 0x90,0x22,0x43,0x68,0x51,0x7a,0xcf,0xea, 74 0x90, 0x22, 0x43, 0x68, 0x51, 0x7a, 0xcf, 0xea,
75 0xbd,0x6b,0xb3,0x73,0x2b,0xc0,0xe9,0xda, 75 0xbd, 0x6b, 0xb3, 0x73, 0x2b, 0xc0, 0xe9, 0xda,
76 0x99,0x83,0x2b,0x61,0xca,0x01,0xb6,0xde, 76 0x99, 0x83, 0x2b, 0x61, 0xca, 0x01, 0xb6, 0xde,
77 0x56,0x24,0x4a,0x9e,0x88,0xd5,0xf9,0xb3, 77 0x56, 0x24, 0x4a, 0x9e, 0x88, 0xd5, 0xf9, 0xb3,
78 0x79,0x73,0xf6,0x22,0xa4,0x3d,0x14,0xa6, 78 0x79, 0x73, 0xf6, 0x22, 0xa4, 0x3d, 0x14, 0xa6,
79 0x59,0x9b,0x1f,0x65,0x4c,0xb4,0x5a,0x74, 79 0x59, 0x9b, 0x1f, 0x65, 0x4c, 0xb4, 0x5a, 0x74,
80 0xe3,0x55,0xa5 80 0xe3, 0x55, 0xa5
81}; 81};
82 82
83START_TEST(test_known) 83START_TEST(test_known)
@@ -86,19 +86,20 @@ START_TEST(test_known)
86 unsigned char m[131]; 86 unsigned char m[131];
87 int clen, mlen; 87 int clen, mlen;
88 88
89 ck_assert_msg(sizeof(c) == sizeof(m) + ENCRYPTION_PADDING * sizeof(unsigned char), "cyphertext should be ENCRYPTION_PADDING bytes longer than plaintext"); 89 ck_assert_msg(sizeof(c) == sizeof(m) + ENCRYPTION_PADDING * sizeof(unsigned char),
90 "cyphertext should be ENCRYPTION_PADDING bytes longer than plaintext");
90 ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed"); 91 ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed");
91 ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed"); 92 ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed");
92 93
93 clen = encrypt_data(bobpk, alicesk, nonce, test_m, sizeof(test_m)/sizeof(unsigned char), c); 94 clen = encrypt_data(bobpk, alicesk, nonce, test_m, sizeof(test_m) / sizeof(unsigned char), c);
94 95
95 ck_assert_msg(memcmp(test_c, c, sizeof(c)) == 0, "cyphertext doesn't match test vector"); 96 ck_assert_msg(memcmp(test_c, c, sizeof(c)) == 0, "cyphertext doesn't match test vector");
96 ck_assert_msg(clen == sizeof(c)/sizeof(unsigned char), "wrong ciphertext length"); 97 ck_assert_msg(clen == sizeof(c) / sizeof(unsigned char), "wrong ciphertext length");
97 98
98 mlen = decrypt_data(bobpk, alicesk, nonce, test_c, sizeof(test_c)/sizeof(unsigned char), m); 99 mlen = decrypt_data(bobpk, alicesk, nonce, test_c, sizeof(test_c) / sizeof(unsigned char), m);
99 100
100 ck_assert_msg(memcmp(test_m, m, sizeof(m)) == 0, "decrypted text doesn't match test vector"); 101 ck_assert_msg(memcmp(test_m, m, sizeof(m)) == 0, "decrypted text doesn't match test vector");
101 ck_assert_msg(mlen == sizeof(m)/sizeof(unsigned char), "wrong plaintext length"); 102 ck_assert_msg(mlen == sizeof(m) / sizeof(unsigned char), "wrong plaintext length");
102} 103}
103END_TEST 104END_TEST
104 105
@@ -111,20 +112,21 @@ START_TEST(test_fast_known)
111 112
112 encrypt_precompute(bobpk, alicesk, k); 113 encrypt_precompute(bobpk, alicesk, k);
113 114
114 ck_assert_msg(sizeof(c) == sizeof(m) + ENCRYPTION_PADDING * sizeof(unsigned char), "cyphertext should be ENCRYPTION_PADDING bytes longer than plaintext"); 115 ck_assert_msg(sizeof(c) == sizeof(m) + ENCRYPTION_PADDING * sizeof(unsigned char),
116 "cyphertext should be ENCRYPTION_PADDING bytes longer than plaintext");
115 ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed"); 117 ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed");
116 ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed"); 118 ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed");
117 119
118 clen = encrypt_data_fast(k, nonce, test_m, sizeof(test_m)/sizeof(unsigned char), c); 120 clen = encrypt_data_fast(k, nonce, test_m, sizeof(test_m) / sizeof(unsigned char), c);
119 121
120 ck_assert_msg(memcmp(test_c, c, sizeof(c)) == 0, "cyphertext doesn't match test vector"); 122 ck_assert_msg(memcmp(test_c, c, sizeof(c)) == 0, "cyphertext doesn't match test vector");
121 ck_assert_msg(clen == sizeof(c)/sizeof(unsigned char), "wrong ciphertext length"); 123 ck_assert_msg(clen == sizeof(c) / sizeof(unsigned char), "wrong ciphertext length");
122 124
123 mlen = decrypt_data_fast(k, nonce, test_c, sizeof(test_c)/sizeof(unsigned char), m); 125 mlen = decrypt_data_fast(k, nonce, test_c, sizeof(test_c) / sizeof(unsigned char), m);
124 126
125 ck_assert_msg(memcmp(test_m, m, sizeof(m)) == 0, "decrypted text doesn't match test vector"); 127 ck_assert_msg(memcmp(test_m, m, sizeof(m)) == 0, "decrypted text doesn't match test vector");
126 ck_assert_msg(mlen == sizeof(m)/sizeof(unsigned char), "wrong plaintext length"); 128 ck_assert_msg(mlen == sizeof(m) / sizeof(unsigned char), "wrong plaintext length");
127 129
128} 130}
129END_TEST 131END_TEST
130 132
@@ -156,23 +158,22 @@ START_TEST(test_endtoend)
156 int testno; 158 int testno;
157 159
158 // Test 100 random messages and keypairs 160 // Test 100 random messages and keypairs
159 for (testno = 0; testno < 100; testno++) 161 for (testno = 0; testno < 100; testno++) {
160 {
161 //Generate random message (random length from 100 to 500) 162 //Generate random message (random length from 100 to 500)
162 mlen = (rand() % 400) + 100; 163 mlen = (rand() % 400) + 100;
163 rand_bytes(m, mlen); 164 rand_bytes(m, mlen);
164 rand_bytes(n, crypto_box_NONCEBYTES); 165 rand_bytes(n, crypto_box_NONCEBYTES);
165 166
166 //Generate keypairs 167 //Generate keypairs
167 crypto_box_keypair(pk1,sk1); 168 crypto_box_keypair(pk1, sk1);
168 crypto_box_keypair(pk2,sk2); 169 crypto_box_keypair(pk2, sk2);
169 170
170 //Precompute shared keys 171 //Precompute shared keys
171 encrypt_precompute(pk2, sk1, k1); 172 encrypt_precompute(pk2, sk1, k1);
172 encrypt_precompute(pk1, sk2, k2); 173 encrypt_precompute(pk1, sk2, k2);
173 174
174 ck_assert_msg(memcmp(k1, k2, crypto_box_BEFORENMBYTES) == 0, "encrypt_precompute: bad"); 175 ck_assert_msg(memcmp(k1, k2, crypto_box_BEFORENMBYTES) == 0, "encrypt_precompute: bad");
175 176
176 //Encrypt all four ways 177 //Encrypt all four ways
177 c1len = encrypt_data(pk2, sk1, n, m, mlen, c1); 178 c1len = encrypt_data(pk2, sk1, n, m, mlen, c1);
178 c2len = encrypt_data(pk1, sk2, n, m, mlen, c2); 179 c2len = encrypt_data(pk1, sk2, n, m, mlen, c2);
@@ -181,7 +182,8 @@ START_TEST(test_endtoend)
181 182
182 ck_assert_msg(c1len == c2len && c1len == c3len && c1len == c4len, "cyphertext lengths differ"); 183 ck_assert_msg(c1len == c2len && c1len == c3len && c1len == c4len, "cyphertext lengths differ");
183 ck_assert_msg(c1len == mlen + ENCRYPTION_PADDING, "wrong cyphertext length"); 184 ck_assert_msg(c1len == mlen + ENCRYPTION_PADDING, "wrong cyphertext length");
184 ck_assert_msg(memcmp(c1, c2, c1len) == 0 && memcmp(c1, c3, c1len) == 0 && memcmp(c1, c4, c1len) == 0, "crypertexts differ"); 185 ck_assert_msg(memcmp(c1, c2, c1len) == 0 && memcmp(c1, c3, c1len) == 0
186 && memcmp(c1, c4, c1len) == 0, "crypertexts differ");
185 187
186 //Decrypt all four ways 188 //Decrypt all four ways
187 m1len = decrypt_data(pk2, sk1, n, c1, c1len, m1); 189 m1len = decrypt_data(pk2, sk1, n, c1, c1len, m1);
@@ -191,7 +193,8 @@ START_TEST(test_endtoend)
191 193
192 ck_assert_msg(m1len == m2len && m1len == m3len && m1len == m4len, "decrypted text lengths differ"); 194 ck_assert_msg(m1len == m2len && m1len == m3len && m1len == m4len, "decrypted text lengths differ");
193 ck_assert_msg(m1len == mlen, "wrong decrypted text length"); 195 ck_assert_msg(m1len == mlen, "wrong decrypted text length");
194 ck_assert_msg(memcmp(m1, m2, mlen) == 0 && memcmp(m1, m3, mlen) == 0 && memcmp(m1, m4, mlen) == 0, "decrypted texts differ"); 196 ck_assert_msg(memcmp(m1, m2, mlen) == 0 && memcmp(m1, m3, mlen) == 0
197 && memcmp(m1, m4, mlen) == 0, "decrypted texts differ");
195 ck_assert_msg(memcmp(m1, m, mlen) == 0, "wrong decrypted text"); 198 ck_assert_msg(memcmp(m1, m, mlen) == 0, "wrong decrypted text");
196 } 199 }
197} 200}
@@ -217,18 +220,18 @@ START_TEST(test_large_data)
217 rand_bytes(m1, sizeof(m1)); 220 rand_bytes(m1, sizeof(m1));
218 rand_bytes(m2, sizeof(m2)); 221 rand_bytes(m2, sizeof(m2));
219 rand_bytes(n, crypto_box_NONCEBYTES); 222 rand_bytes(n, crypto_box_NONCEBYTES);
220 223
221 //Generate key 224 //Generate key
222 rand_bytes(k, crypto_box_BEFORENMBYTES); 225 rand_bytes(k, crypto_box_BEFORENMBYTES);
223 226
224 c1len = encrypt_data_fast(k, n, m1, sizeof(m1), c1); 227 c1len = encrypt_data_fast(k, n, m1, sizeof(m1), c1);
225 c2len = encrypt_data_fast(k, n, m2, sizeof(m2), c2); 228 c2len = encrypt_data_fast(k, n, m2, sizeof(m2), c2);
226 229
227 ck_assert_msg(c1len == sizeof(m1) + ENCRYPTION_PADDING, "Could not encrypt max size"); 230 ck_assert_msg(c1len == sizeof(m1) + ENCRYPTION_PADDING, "Could not encrypt max size");
228 ck_assert_msg(c2len == -1, "incorrectly succeeded encrypting massive size"); 231 ck_assert_msg(c2len == -1, "incorrectly succeeded encrypting massive size");
229 232
230 m1plen = decrypt_data_fast(k, n, c1, c1len, m1prime); 233 m1plen = decrypt_data_fast(k, n, c1, c1len, m1prime);
231 234
232 ck_assert_msg(m1plen == sizeof(m1), "decrypted text lengths differ"); 235 ck_assert_msg(m1plen == sizeof(m1), "decrypted text lengths differ");
233 ck_assert_msg(memcmp(m1prime, m1, sizeof(m1)) == 0, "decrypted texts differ"); 236 ck_assert_msg(memcmp(m1prime, m1, sizeof(m1)) == 0, "decrypted texts differ");
234} 237}
@@ -239,7 +242,7 @@ END_TEST
239 tcase_add_test(NAME, test_##NAME); \ 242 tcase_add_test(NAME, test_##NAME); \
240 suite_add_tcase(s, NAME); 243 suite_add_tcase(s, NAME);
241 244
242Suite* crypto_suite(void) 245Suite *crypto_suite(void)
243{ 246{
244 Suite *s = suite_create("Crypto"); 247 Suite *s = suite_create("Crypto");
245 248
@@ -251,7 +254,7 @@ Suite* crypto_suite(void)
251 return s; 254 return s;
252} 255}
253 256
254int main(int argc, char* argv[]) 257int main(int argc, char *argv[])
255{ 258{
256 srand((unsigned int) time(NULL)); 259 srand((unsigned int) time(NULL));
257 260