@@ -111,140 +111,140 @@ int ccm_test(void)
111111},
112112
113113};
114- unsigned long taglen , x , y ;
115- unsigned char buf [64 ], buf2 [64 ], tag [16 ], tag2 [16 ], tag3 [16 ], zero [64 ];
116- int err , idx ;
117- symmetric_key skey ;
118- ccm_state ccm ;
114+ unsigned long taglen , x , y ;
115+ unsigned char buf [64 ], buf2 [64 ], tag [16 ], tag2 [16 ], tag3 [16 ], zero [64 ];
116+ int err , idx ;
117+ symmetric_key skey ;
118+ ccm_state ccm ;
119119
120- zeromem (zero , 64 );
120+ zeromem (zero , 64 );
121121
122- idx = find_cipher ("aes" );
123- if (idx == -1 ) {
124- idx = find_cipher ("rijndael" );
125- if (idx == -1 ) {
126- return CRYPT_NOP ;
127- }
128- }
122+ idx = find_cipher ("aes" );
123+ if (idx == -1 ) {
124+ idx = find_cipher ("rijndael" );
125+ if (idx == -1 ) {
126+ return CRYPT_NOP ;
127+ }
128+ }
129129
130- for (x = 0 ; x < (sizeof (tests )/sizeof (tests [0 ])); x ++ ) {
131- for (y = 0 ; y < 2 ; y ++ ) {
132- taglen = tests [x ].taglen ;
133- if (y == 0 ) {
134- if ((err = cipher_descriptor [idx ].setup (tests [x ].key , 16 , 0 , & skey )) != CRYPT_OK ) {
135- return err ;
136- }
130+ for (x = 0 ; x < (sizeof (tests )/sizeof (tests [0 ])); x ++ ) {
131+ for (y = 0 ; y < 2 ; y ++ ) {
132+ taglen = tests [x ].taglen ;
133+ if (y == 0 ) {
134+ if ((err = cipher_descriptor [idx ].setup (tests [x ].key , 16 , 0 , & skey )) != CRYPT_OK ) {
135+ return err ;
136+ }
137137
138- if ((err = ccm_memory (idx ,
139- tests [x ].key , 16 ,
140- & skey ,
141- tests [x ].nonce , tests [x ].noncelen ,
142- tests [x ].header , tests [x ].headerlen ,
143- (unsigned char * )tests [x ].pt , tests [x ].ptlen ,
144- buf ,
145- tag , & taglen , 0 )) != CRYPT_OK ) {
146- return err ;
147- }
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 ;
138+ if ((err = ccm_memory (idx ,
139+ tests [x ].key , 16 ,
140+ & skey ,
141+ tests [x ].nonce , tests [x ].noncelen ,
142+ tests [x ].header , tests [x ].headerlen ,
143+ (unsigned char * )tests [x ].pt , tests [x ].ptlen ,
144+ buf ,
145+ tag , & taglen , 0 )) != CRYPT_OK ) {
146+ return err ;
147+ }
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+ }
165175 }
166- if ((err = ccm_add_aad (& ccm , tests [x ].header , tests [x ].headerlen )) != CRYPT_OK ) {
167- return err ;
176+
177+ if (compare_testvector (buf , tests [x ].ptlen , tests [x ].ct , tests [x ].ptlen , "CCM encrypt data" , x )) {
178+ return CRYPT_FAIL_TESTVECTOR ;
168179 }
169- if (( err = ccm_process ( & ccm , ( unsigned char * ) tests [x ].pt , tests [x ].ptlen , buf , CCM_ENCRYPT )) != CRYPT_OK ) {
170- return err ;
180+ if (compare_testvector ( tag , taglen , tests [x ].tag , tests [x ].taglen , "CCM encrypt tag" , x ) ) {
181+ return CRYPT_FAIL_TESTVECTOR ;
171182 }
172- if ((err = ccm_done (& ccm , tag , & taglen )) != CRYPT_OK ) {
173- return err ;
183+
184+ if (y == 0 ) {
185+ XMEMCPY (tag3 , tests [x ].tag , tests [x ].taglen );
186+ taglen = tests [x ].taglen ;
187+ if ((err = ccm_memory (idx ,
188+ tests [x ].key , 16 ,
189+ NULL ,
190+ tests [x ].nonce , tests [x ].noncelen ,
191+ tests [x ].header , tests [x ].headerlen ,
192+ buf2 , tests [x ].ptlen ,
193+ buf ,
194+ tag3 , & taglen , 1 )) != CRYPT_OK ) {
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+ }
174213 }
175- }
176214
177- if (compare_testvector (buf , tests [x ].ptlen , tests [x ].ct , tests [x ].ptlen , "CCM encrypt data" , x )) {
178- return CRYPT_FAIL_TESTVECTOR ;
179- }
180- if (compare_testvector (tag , taglen , tests [x ].tag , tests [x ].taglen , "CCM encrypt tag" , x )) {
181- return CRYPT_FAIL_TESTVECTOR ;
182- }
183215
184- if (y == 0 ) {
185- XMEMCPY (tag3 , tests [x ].tag , tests [x ].taglen );
186- taglen = tests [x ].taglen ;
187- if ((err = ccm_memory (idx ,
188- tests [x ].key , 16 ,
189- NULL ,
190- tests [x ].nonce , tests [x ].noncelen ,
191- tests [x ].header , tests [x ].headerlen ,
192- buf2 , tests [x ].ptlen ,
193- buf ,
194- tag3 , & taglen , 1 )) != CRYPT_OK ) {
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 ;
216+ if (compare_testvector (buf2 , tests [x ].ptlen , tests [x ].pt , tests [x ].ptlen , "CCM decrypt data" , x )) {
217+ return CRYPT_FAIL_TESTVECTOR ;
206218 }
207- if ((err = ccm_process (& ccm , buf2 , tests [x ].ptlen , buf , CCM_DECRYPT )) != CRYPT_OK ) {
208- return err ;
219+ if (y == 0 ) {
220+ /* check if decryption with the wrong tag does not reveal the plaintext */
221+ XMEMCPY (tag3 , tests [x ].tag , tests [x ].taglen );
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+ }
209241 }
210- if ((err = ccm_done (& ccm , tag2 , & taglen )) != CRYPT_OK ) {
211- return err ;
212- }
213- }
214-
215242
216- if (compare_testvector (buf2 , tests [x ].ptlen , tests [x ].pt , tests [x ].ptlen , "CCM decrypt data" , x )) {
217- return CRYPT_FAIL_TESTVECTOR ;
218- }
219- if (y == 0 ) {
220- /* check if decryption with the wrong tag does not reveal the plaintext */
221- XMEMCPY (tag3 , tests [x ].tag , tests [x ].taglen );
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 );
243+ if (y == 0 ) {
244+ cipher_descriptor [idx ].done (& skey );
245+ }
245246 }
246- }
247- }
247+ }
248248
249249 /* wycheproof failing test - https://github.com/libtom/libtomcrypt/pull/452 */
250250 {
@@ -273,7 +273,7 @@ int ccm_test(void)
273273 }
274274 }
275275
276- return CRYPT_OK ;
276+ return CRYPT_OK ;
277277#endif
278278}
279279
0 commit comments