comparison libtomcrypt/src/encauth/ccm/ccm_test.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 /** 11 /**
14 @file ccm_test.c 12 @file ccm_test.c
30 int headerlen; 28 int headerlen;
31 unsigned char pt[64]; 29 unsigned char pt[64];
32 int ptlen; 30 int ptlen;
33 unsigned char ct[64]; 31 unsigned char ct[64];
34 unsigned char tag[16]; 32 unsigned char tag[16];
35 int taglen; 33 unsigned long taglen;
36 } tests[] = { 34 } tests[] = {
37 35
38 /* 13 byte nonce, 8 byte auth, 23 byte pt */ 36 /* 13 byte nonce, 8 byte auth, 23 byte pt */
39 { 37 {
40 { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 38 { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
41 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF }, 39 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF },
42 { 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xA0, 40 { 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xA0,
43 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 }, 41 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 },
44 13, 42 13,
45 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, 43 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 },
46 8, 44 8,
47 { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 45 { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
55 8 53 8
56 }, 54 },
57 55
58 /* 13 byte nonce, 12 byte header, 19 byte pt */ 56 /* 13 byte nonce, 12 byte header, 19 byte pt */
59 { 57 {
60 { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 58 { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
61 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF }, 59 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF },
62 { 0x00, 0x00, 0x00, 0x06, 0x05, 0x04, 0x03, 0xA0, 60 { 0x00, 0x00, 0x00, 0x06, 0x05, 0x04, 0x03, 0xA0,
63 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 }, 61 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 },
64 13, 62 13,
65 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 63 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
66 0x08, 0x09, 0x0A, 0x0B }, 64 0x08, 0x09, 0x0A, 0x0B },
67 12, 65 12,
68 { 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 66 { 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
69 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 67 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
70 0x1C, 0x1D, 0x1E }, 68 0x1C, 0x1D, 0x1E },
71 19, 69 19,
72 { 0xA2, 0x8C, 0x68, 0x65, 0x93, 0x9A, 0x9A, 0x79, 70 { 0xA2, 0x8C, 0x68, 0x65, 0x93, 0x9A, 0x9A, 0x79,
73 0xFA, 0xAA, 0x5C, 0x4C, 0x2A, 0x9D, 0x4A, 0x91, 71 0xFA, 0xAA, 0x5C, 0x4C, 0x2A, 0x9D, 0x4A, 0x91,
74 0xCD, 0xAC, 0x8C }, 72 0xCD, 0xAC, 0x8C },
75 { 0x96, 0xC8, 0x61, 0xB9, 0xC9, 0xE6, 0x1E, 0xF1 }, 73 { 0x96, 0xC8, 0x61, 0xB9, 0xC9, 0xE6, 0x1E, 0xF1 },
76 8 74 8
77 }, 75 },
78 76
79 /* supplied by Brian Gladman */ 77 /* supplied by Brian Gladman */
80 { 78 {
81 { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 79 { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
82 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }, 80 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f },
83 { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }, 81 { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 },
84 7, 82 7,
85 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, 83 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 },
86 8, 84 8,
90 { 0x4d, 0xac, 0x25, 0x5d }, 88 { 0x4d, 0xac, 0x25, 0x5d },
91 4 89 4
92 }, 90 },
93 91
94 { 92 {
95 { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85, 93 { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
96 0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f }, 94 0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f },
97 { 0x00, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0xb5, 95 { 0x00, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0xb5,
98 0x03, 0x97, 0x76, 0xe7, 0x0c }, 96 0x03, 0x97, 0x76, 0xe7, 0x0c },
99 13, 97 13,
100 { 0x08, 0x40, 0x0f, 0xd2, 0xe1, 0x28, 0xa5, 0x7c, 98 { 0x08, 0x40, 0x0f, 0xd2, 0xe1, 0x28, 0xa5, 0x7c,
101 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0xab, 0xae, 99 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0xab, 0xae,
102 0xa5, 0xb8, 0xfc, 0xba, 0x00, 0x00 }, 100 0xa5, 0xb8, 0xfc, 0xba, 0x00, 0x00 },
103 22, 101 22,
104 { 0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae, 102 { 0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
105 0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb, 103 0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
106 0x7e, 0x78, 0xa0, 0x50 }, 104 0x7e, 0x78, 0xa0, 0x50 },
107 20, 105 20,
108 { 0xf3, 0xd0, 0xa2, 0xfe, 0x9a, 0x3d, 0xbf, 0x23, 106 { 0xf3, 0xd0, 0xa2, 0xfe, 0x9a, 0x3d, 0xbf, 0x23,
109 0x42, 0xa6, 0x43, 0xe4, 0x32, 0x46, 0xe8, 0x0c, 107 0x42, 0xa6, 0x43, 0xe4, 0x32, 0x46, 0xe8, 0x0c,
110 0x3c, 0x04, 0xd0, 0x19 }, 108 0x3c, 0x04, 0xd0, 0x19 },
111 { 0x78, 0x45, 0xce, 0x0b, 0x16, 0xf9, 0x76, 0x23 }, 109 { 0x78, 0x45, 0xce, 0x0b, 0x16, 0xf9, 0x76, 0x23 },
112 8 110 8
113 }, 111 },
114 112
115 }; 113 };
116 unsigned long taglen, x; 114 unsigned long taglen, x, y;
117 unsigned char buf[64], buf2[64], tag2[16], tag[16]; 115 unsigned char buf[64], buf2[64], tag[16], tag2[16], tag3[16], zero[64];
118 int err, idx; 116 int err, idx;
119 symmetric_key skey; 117 symmetric_key skey;
118 ccm_state ccm;
119
120 zeromem(zero, 64);
120 121
121 idx = find_cipher("aes"); 122 idx = find_cipher("aes");
122 if (idx == -1) { 123 if (idx == -1) {
123 idx = find_cipher("rijndael"); 124 idx = find_cipher("rijndael");
124 if (idx == -1) { 125 if (idx == -1) {
125 return CRYPT_NOP; 126 return CRYPT_NOP;
126 } 127 }
127 } 128 }
128 129
129 for (x = 0; x < (sizeof(tests)/sizeof(tests[0])); x++) { 130 for (x = 0; x < (sizeof(tests)/sizeof(tests[0])); x++) {
131 for (y = 0; y < 2; y++) {
130 taglen = tests[x].taglen; 132 taglen = tests[x].taglen;
131 if ((err = cipher_descriptor[idx].setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) { 133 if (y == 0) {
132 return err; 134 if ((err = cipher_descriptor[idx].setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) {
133 } 135 return err;
134 136 }
135 if ((err = ccm_memory(idx, 137
136 tests[x].key, 16, 138 if ((err = ccm_memory(idx,
137 &skey, 139 tests[x].key, 16,
138 tests[x].nonce, tests[x].noncelen, 140 &skey,
139 tests[x].header, tests[x].headerlen, 141 tests[x].nonce, tests[x].noncelen,
140 (unsigned char*)tests[x].pt, tests[x].ptlen, 142 tests[x].header, tests[x].headerlen,
141 buf, 143 (unsigned char*)tests[x].pt, tests[x].ptlen,
142 tag, &taglen, 0)) != CRYPT_OK) { 144 buf,
143 return err; 145 tag, &taglen, 0)) != CRYPT_OK) {
144 } 146 return err;
145 147 }
146 if (XMEMCMP(buf, tests[x].ct, tests[x].ptlen)) { 148 /* run a second time to make sure skey is not touched */
149 if ((err = ccm_memory(idx,
150 tests[x].key, 16,
151 &skey,
152 tests[x].nonce, tests[x].noncelen,
153 tests[x].header, tests[x].headerlen,
154 (unsigned char*)tests[x].pt, tests[x].ptlen,
155 buf,
156 tag, &taglen, 0)) != CRYPT_OK) {
157 return err;
158 }
159 } else {
160 if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) {
161 return err;
162 }
163 if ((err = ccm_add_nonce(&ccm, tests[x].nonce, tests[x].noncelen)) != CRYPT_OK) {
164 return err;
165 }
166 if ((err = ccm_add_aad(&ccm, tests[x].header, tests[x].headerlen)) != CRYPT_OK) {
167 return err;
168 }
169 if ((err = ccm_process(&ccm, (unsigned char*)tests[x].pt, tests[x].ptlen, buf, CCM_ENCRYPT)) != CRYPT_OK) {
170 return err;
171 }
172 if ((err = ccm_done(&ccm, tag, &taglen)) != CRYPT_OK) {
173 return err;
174 }
175 }
176
177 if (compare_testvector(buf, tests[x].ptlen, tests[x].ct, tests[x].ptlen, "CCM encrypt data", x)) {
147 return CRYPT_FAIL_TESTVECTOR; 178 return CRYPT_FAIL_TESTVECTOR;
148 } 179 }
149 if (XMEMCMP(tag, tests[x].tag, tests[x].taglen)) { 180 if (compare_testvector(tag, taglen, tests[x].tag, tests[x].taglen, "CCM encrypt tag", x)) {
150 return CRYPT_FAIL_TESTVECTOR; 181 return CRYPT_FAIL_TESTVECTOR;
151 } 182 }
152 183
153 if ((err = ccm_memory(idx, 184 if (y == 0) {
154 tests[x].key, 16, 185 XMEMCPY(tag3, tests[x].tag, tests[x].taglen);
155 NULL, 186 taglen = tests[x].taglen;
156 tests[x].nonce, tests[x].noncelen, 187 if ((err = ccm_memory(idx,
157 tests[x].header, tests[x].headerlen, 188 tests[x].key, 16,
158 buf2, tests[x].ptlen, 189 NULL,
159 buf, 190 tests[x].nonce, tests[x].noncelen,
160 tag2, &taglen, 1 )) != CRYPT_OK) { 191 tests[x].header, tests[x].headerlen,
161 return err; 192 buf2, tests[x].ptlen,
162 } 193 buf,
163 194 tag3, &taglen, 1 )) != CRYPT_OK) {
164 if (XMEMCMP(buf2, tests[x].pt, tests[x].ptlen)) { 195 return err;
196 }
197 } else {
198 if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) {
199 return err;
200 }
201 if ((err = ccm_add_nonce(&ccm, tests[x].nonce, tests[x].noncelen)) != CRYPT_OK) {
202 return err;
203 }
204 if ((err = ccm_add_aad(&ccm, tests[x].header, tests[x].headerlen)) != CRYPT_OK) {
205 return err;
206 }
207 if ((err = ccm_process(&ccm, buf2, tests[x].ptlen, buf, CCM_DECRYPT)) != CRYPT_OK) {
208 return err;
209 }
210 if ((err = ccm_done(&ccm, tag2, &taglen)) != CRYPT_OK) {
211 return err;
212 }
213 }
214
215
216 if (compare_testvector(buf2, tests[x].ptlen, tests[x].pt, tests[x].ptlen, "CCM decrypt data", x)) {
165 return CRYPT_FAIL_TESTVECTOR; 217 return CRYPT_FAIL_TESTVECTOR;
166 } 218 }
167 if (XMEMCMP(tag2, tests[x].tag, tests[x].taglen)) { 219 if (y == 0) {
168 return CRYPT_FAIL_TESTVECTOR; 220 /* check if decryption with the wrong tag does not reveal the plaintext */
169 } 221 XMEMCPY(tag3, tests[x].tag, tests[x].taglen);
170 cipher_descriptor[idx].done(&skey); 222 tag3[0] ^= 0xff; /* set the tag to the wrong value */
223 taglen = tests[x].taglen;
224 if ((err = ccm_memory(idx,
225 tests[x].key, 16,
226 NULL,
227 tests[x].nonce, tests[x].noncelen,
228 tests[x].header, tests[x].headerlen,
229 buf2, tests[x].ptlen,
230 buf,
231 tag3, &taglen, 1 )) != CRYPT_ERROR) {
232 return CRYPT_FAIL_TESTVECTOR;
233 }
234 if (compare_testvector(buf2, tests[x].ptlen, zero, tests[x].ptlen, "CCM decrypt wrong tag", x)) {
235 return CRYPT_FAIL_TESTVECTOR;
236 }
237 } else {
238 if (compare_testvector(tag2, taglen, tests[x].tag, tests[x].taglen, "CCM decrypt tag", x)) {
239 return CRYPT_FAIL_TESTVECTOR;
240 }
241 }
242
243 if (y == 0) {
244 cipher_descriptor[idx].done(&skey);
245 }
246 }
171 } 247 }
248
172 return CRYPT_OK; 249 return CRYPT_OK;
173 #endif 250 #endif
174 } 251 }
175 252
176 #endif 253 #endif
177 254
178 /* $Source$ */ 255 /* ref: $Format:%D$ */
179 /* $Revision$ */ 256 /* git commit: $Format:%H$ */
180 /* $Date$ */ 257 /* commit time: $Format:%ai$ */