1 /* ====================================================================
2 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
4 * Rights for redistribution and usage in source and binary
5 * forms are granted according to the OpenSSL license.
19 * Trouble with Ciphertext Stealing, CTS, mode is that there is no
20 * common official specification, but couple of cipher/application
21 * specific ones: RFC2040 and RFC3962. Then there is 'Proposal to
22 * Extend CBC Mode By "Ciphertext Stealing"' at NIST site, which
23 * deviates from mentioned RFCs. Most notably it allows input to be
24 * of block length and it doesn't flip the order of the last two
25 * blocks. CTS is being discussed even in ECB context, but it's not
26 * adopted for any known application. This implementation provides
27 * two interfaces: one compliant with above mentioned RFCs and one
28 * compliant with the NIST proposal, both extending CBC mode.
31 size_t CRYPTO_cts128_encrypt_block(const unsigned char *in, unsigned char *out,
32 size_t len, const void *key,
33 unsigned char ivec[16], block128_f block)
36 assert (in && out && key && ivec);
38 if (len <= 16) return 0;
40 if ((residue=len%16) == 0) residue = 16;
44 CRYPTO_cbc128_encrypt(in,out,len,key,ivec,block);
49 for (n=0; n<residue; ++n)
51 (*block)(ivec,ivec,key);
52 memcpy(out,out-16,residue);
53 memcpy(out-16,ivec,16);
58 size_t CRYPTO_nistcts128_encrypt_block(const unsigned char *in, unsigned char *out,
59 size_t len, const void *key,
60 unsigned char ivec[16], block128_f block)
63 assert (in && out && key && ivec);
65 if (len < 16) return 0;
71 CRYPTO_cbc128_encrypt(in,out,len,key,ivec,block);
73 if (residue==0) return len;
78 for (n=0; n<residue; ++n)
80 (*block)(ivec,ivec,key);
81 memcpy(out-16+residue,ivec,16);
86 size_t CRYPTO_cts128_encrypt(const unsigned char *in, unsigned char *out,
87 size_t len, const void *key,
88 unsigned char ivec[16], cbc128_f cbc)
90 union { size_t align; unsigned char c[16]; } tmp;
92 assert (in && out && key && ivec);
94 if (len <= 16) return 0;
96 if ((residue=len%16) == 0) residue = 16;
100 (*cbc)(in,out,len,key,ivec,1);
105 #if defined(CBC_HANDLES_TRUNCATED_IO)
106 memcpy(tmp.c,out-16,16);
107 (*cbc)(in,out-16,residue,key,ivec,1);
108 memcpy(out,tmp.c,residue);
112 for (n=0; n<16; n+=sizeof(size_t))
113 *(size_t *)(tmp.c+n) = 0;
114 memcpy(tmp.c,in,residue);
116 memcpy(out,out-16,residue);
117 (*cbc)(tmp.c,out-16,16,key,ivec,1);
122 size_t CRYPTO_nistcts128_encrypt(const unsigned char *in, unsigned char *out,
123 size_t len, const void *key,
124 unsigned char ivec[16], cbc128_f cbc)
126 union { size_t align; unsigned char c[16]; } tmp;
128 assert (in && out && key && ivec);
130 if (len < 16) return 0;
136 (*cbc)(in,out,len,key,ivec,1);
138 if (residue==0) return len;
143 #if defined(CBC_HANDLES_TRUNCATED_IO)
144 (*cbc)(in,out-16+residue,residue,key,ivec,1);
148 for (n=0; n<16; n+=sizeof(size_t))
149 *(size_t *)(tmp.c+n) = 0;
150 memcpy(tmp.c,in,residue);
152 (*cbc)(tmp.c,out-16+residue,16,key,ivec,1);
157 size_t CRYPTO_cts128_decrypt_block(const unsigned char *in, unsigned char *out,
158 size_t len, const void *key,
159 unsigned char ivec[16], block128_f block)
161 union { size_t align; unsigned char c[32]; } tmp;
163 assert (in && out && key && ivec);
165 if (len<=16) return 0;
167 if ((residue=len%16) == 0) residue = 16;
172 CRYPTO_cbc128_decrypt(in,out,len,key,ivec,block);
177 (*block)(in,tmp.c+16,key);
179 for (n=0; n<16; n+=sizeof(size_t))
180 *(size_t *)(tmp.c+n) = *(size_t *)(tmp.c+16+n);
181 memcpy(tmp.c,in+16,residue);
182 (*block)(tmp.c,tmp.c,key);
184 for(n=0; n<16; ++n) {
185 unsigned char c = in[n];
186 out[n] = tmp.c[n] ^ ivec[n];
189 for(residue+=16; n<residue; ++n)
190 out[n] = tmp.c[n] ^ in[n];
192 return 16+len+residue;
195 size_t CRYPTO_nistcts128_decrypt_block(const unsigned char *in, unsigned char *out,
196 size_t len, const void *key,
197 unsigned char ivec[16], block128_f block)
199 union { size_t align; unsigned char c[32]; } tmp;
201 assert (in && out && key && ivec);
203 if (len<16) return 0;
208 CRYPTO_cbc128_decrypt(in,out,len,key,ivec,block);
215 CRYPTO_cbc128_decrypt(in,out,len,key,ivec,block);
220 (*block)(in+residue,tmp.c+16,key);
222 for (n=0; n<16; n+=sizeof(size_t))
223 *(size_t *)(tmp.c+n) = *(size_t *)(tmp.c+16+n);
224 memcpy(tmp.c,in,residue);
225 (*block)(tmp.c,tmp.c,key);
227 for(n=0; n<16; ++n) {
228 unsigned char c = in[n];
229 out[n] = tmp.c[n] ^ ivec[n];
230 ivec[n] = in[n+residue];
233 for(residue+=16; n<residue; ++n)
234 out[n] = tmp.c[n] ^ tmp.c[n-16];
236 return 16+len+residue;
239 size_t CRYPTO_cts128_decrypt(const unsigned char *in, unsigned char *out,
240 size_t len, const void *key,
241 unsigned char ivec[16], cbc128_f cbc)
243 union { size_t align; unsigned char c[32]; } tmp;
245 assert (in && out && key && ivec);
247 if (len<=16) return 0;
249 if ((residue=len%16) == 0) residue = 16;
254 (*cbc)(in,out,len,key,ivec,0);
259 for (n=16; n<32; n+=sizeof(size_t))
260 *(size_t *)(tmp.c+n) = 0;
261 /* this places in[16] at &tmp.c[16] and decrypted block at &tmp.c[0] */
262 (*cbc)(in,tmp.c,16,key,tmp.c+16,0);
264 memcpy(tmp.c,in+16,residue);
265 #if defined(CBC_HANDLES_TRUNCATED_IO)
266 (*cbc)(tmp.c,out,16+residue,key,ivec,0);
268 (*cbc)(tmp.c,tmp.c,32,key,ivec,0);
269 memcpy(out,tmp.c,16+residue);
271 return 16+len+residue;
274 size_t CRYPTO_nistcts128_decrypt(const unsigned char *in, unsigned char *out,
275 size_t len, const void *key,
276 unsigned char ivec[16], cbc128_f cbc)
278 union { size_t align; unsigned char c[32]; } tmp;
280 assert (in && out && key && ivec);
282 if (len<16) return 0;
287 (*cbc)(in,out,len,key,ivec,0);
294 (*cbc)(in,out,len,key,ivec,0);
299 for (n=16; n<32; n+=sizeof(size_t))
300 *(size_t *)(tmp.c+n) = 0;
301 /* this places in[16] at &tmp.c[16] and decrypted block at &tmp.c[0] */
302 (*cbc)(in+residue,tmp.c,16,key,tmp.c+16,0);
304 memcpy(tmp.c,in,residue);
305 #if defined(CBC_HANDLES_TRUNCATED_IO)
306 (*cbc)(tmp.c,out,16+residue,key,ivec,0);
308 (*cbc)(tmp.c,tmp.c,32,key,ivec,0);
309 memcpy(out,tmp.c,16+residue);
311 return 16+len+residue;
314 #if defined(SELFTEST)
316 #include <openssl/aes.h>
318 /* test vectors from RFC 3962 */
319 static const unsigned char test_key[16] = "chicken teriyaki";
320 static const unsigned char test_input[64] =
321 "I would like the" " General Gau's C"
322 "hicken, please, " "and wonton soup.";
323 static const unsigned char test_iv[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
325 static const unsigned char vector_17[17] =
326 {0xc6,0x35,0x35,0x68,0xf2,0xbf,0x8c,0xb4, 0xd8,0xa5,0x80,0x36,0x2d,0xa7,0xff,0x7f,
328 static const unsigned char vector_31[31] =
329 {0xfc,0x00,0x78,0x3e,0x0e,0xfd,0xb2,0xc1, 0xd4,0x45,0xd4,0xc8,0xef,0xf7,0xed,0x22,
330 0x97,0x68,0x72,0x68,0xd6,0xec,0xcc,0xc0, 0xc0,0x7b,0x25,0xe2,0x5e,0xcf,0xe5};
331 static const unsigned char vector_32[32] =
332 {0x39,0x31,0x25,0x23,0xa7,0x86,0x62,0xd5, 0xbe,0x7f,0xcb,0xcc,0x98,0xeb,0xf5,0xa8,
333 0x97,0x68,0x72,0x68,0xd6,0xec,0xcc,0xc0, 0xc0,0x7b,0x25,0xe2,0x5e,0xcf,0xe5,0x84};
334 static const unsigned char vector_47[47] =
335 {0x97,0x68,0x72,0x68,0xd6,0xec,0xcc,0xc0, 0xc0,0x7b,0x25,0xe2,0x5e,0xcf,0xe5,0x84,
336 0xb3,0xff,0xfd,0x94,0x0c,0x16,0xa1,0x8c, 0x1b,0x55,0x49,0xd2,0xf8,0x38,0x02,0x9e,
337 0x39,0x31,0x25,0x23,0xa7,0x86,0x62,0xd5, 0xbe,0x7f,0xcb,0xcc,0x98,0xeb,0xf5};
338 static const unsigned char vector_48[48] =
339 {0x97,0x68,0x72,0x68,0xd6,0xec,0xcc,0xc0, 0xc0,0x7b,0x25,0xe2,0x5e,0xcf,0xe5,0x84,
340 0x9d,0xad,0x8b,0xbb,0x96,0xc4,0xcd,0xc0, 0x3b,0xc1,0x03,0xe1,0xa1,0x94,0xbb,0xd8,
341 0x39,0x31,0x25,0x23,0xa7,0x86,0x62,0xd5, 0xbe,0x7f,0xcb,0xcc,0x98,0xeb,0xf5,0xa8};
342 static const unsigned char vector_64[64] =
343 {0x97,0x68,0x72,0x68,0xd6,0xec,0xcc,0xc0, 0xc0,0x7b,0x25,0xe2,0x5e,0xcf,0xe5,0x84,
344 0x39,0x31,0x25,0x23,0xa7,0x86,0x62,0xd5, 0xbe,0x7f,0xcb,0xcc,0x98,0xeb,0xf5,0xa8,
345 0x48,0x07,0xef,0xe8,0x36,0xee,0x89,0xa5, 0x26,0x73,0x0d,0xbc,0x2f,0x7b,0xc8,0x40,
346 0x9d,0xad,0x8b,0xbb,0x96,0xc4,0xcd,0xc0, 0x3b,0xc1,0x03,0xe1,0xa1,0x94,0xbb,0xd8};
348 static AES_KEY encks, decks;
350 void test_vector(const unsigned char *vector,size_t len)
351 { unsigned char iv[sizeof(test_iv)];
352 unsigned char cleartext[64],ciphertext[64];
355 printf("vector_%d\n",len); fflush(stdout);
357 if ((tail=len%16) == 0) tail = 16;
360 /* test block-based encryption */
361 memcpy(iv,test_iv,sizeof(test_iv));
362 CRYPTO_cts128_encrypt_block(test_input,ciphertext,len,&encks,iv,(block128_f)AES_encrypt);
363 if (memcmp(ciphertext,vector,len))
364 fprintf(stderr,"output_%d mismatch\n",len), exit(1);
365 if (memcmp(iv,vector+len-tail,sizeof(iv)))
366 fprintf(stderr,"iv_%d mismatch\n",len), exit(1);
368 /* test block-based decryption */
369 memcpy(iv,test_iv,sizeof(test_iv));
370 CRYPTO_cts128_decrypt_block(ciphertext,cleartext,len,&decks,iv,(block128_f)AES_decrypt);
371 if (memcmp(cleartext,test_input,len))
372 fprintf(stderr,"input_%d mismatch\n",len), exit(2);
373 if (memcmp(iv,vector+len-tail,sizeof(iv)))
374 fprintf(stderr,"iv_%d mismatch\n",len), exit(2);
376 /* test streamed encryption */
377 memcpy(iv,test_iv,sizeof(test_iv));
378 CRYPTO_cts128_encrypt(test_input,ciphertext,len,&encks,iv,(cbc128_f)AES_cbc_encrypt);
379 if (memcmp(ciphertext,vector,len))
380 fprintf(stderr,"output_%d mismatch\n",len), exit(3);
381 if (memcmp(iv,vector+len-tail,sizeof(iv)))
382 fprintf(stderr,"iv_%d mismatch\n",len), exit(3);
384 /* test streamed decryption */
385 memcpy(iv,test_iv,sizeof(test_iv));
386 CRYPTO_cts128_decrypt(ciphertext,cleartext,len,&decks,iv,(cbc128_f)AES_cbc_encrypt);
387 if (memcmp(cleartext,test_input,len))
388 fprintf(stderr,"input_%d mismatch\n",len), exit(4);
389 if (memcmp(iv,vector+len-tail,sizeof(iv)))
390 fprintf(stderr,"iv_%d mismatch\n",len), exit(4);
393 void test_nistvector(const unsigned char *vector,size_t len)
394 { unsigned char iv[sizeof(test_iv)];
395 unsigned char cleartext[64],ciphertext[64],nistvector[64];
398 printf("nistvector_%d\n",len); fflush(stdout);
400 if ((tail=len%16) == 0) tail = 16;
403 memcpy(nistvector,vector,len);
404 /* flip two last blocks */
405 memcpy(nistvector+len,vector+len+16,tail);
406 memcpy(nistvector+len+tail,vector+len,16);
410 /* test block-based encryption */
411 memcpy(iv,test_iv,sizeof(test_iv));
412 CRYPTO_nistcts128_encrypt_block(test_input,ciphertext,len,&encks,iv,(block128_f)AES_encrypt);
413 if (memcmp(ciphertext,nistvector,len))
414 fprintf(stderr,"output_%d mismatch\n",len), exit(1);
415 if (memcmp(iv,nistvector+len-tail,sizeof(iv)))
416 fprintf(stderr,"iv_%d mismatch\n",len), exit(1);
418 /* test block-based decryption */
419 memcpy(iv,test_iv,sizeof(test_iv));
420 CRYPTO_nistcts128_decrypt_block(ciphertext,cleartext,len,&decks,iv,(block128_f)AES_decrypt);
421 if (memcmp(cleartext,test_input,len))
422 fprintf(stderr,"input_%d mismatch\n",len), exit(2);
423 if (memcmp(iv,nistvector+len-tail,sizeof(iv)))
424 fprintf(stderr,"iv_%d mismatch\n",len), exit(2);
426 /* test streamed encryption */
427 memcpy(iv,test_iv,sizeof(test_iv));
428 CRYPTO_nistcts128_encrypt(test_input,ciphertext,len,&encks,iv,(cbc128_f)AES_cbc_encrypt);
429 if (memcmp(ciphertext,nistvector,len))
430 fprintf(stderr,"output_%d mismatch\n",len), exit(3);
431 if (memcmp(iv,nistvector+len-tail,sizeof(iv)))
432 fprintf(stderr,"iv_%d mismatch\n",len), exit(3);
434 /* test streamed decryption */
435 memcpy(iv,test_iv,sizeof(test_iv));
436 CRYPTO_nistcts128_decrypt(ciphertext,cleartext,len,&decks,iv,(cbc128_f)AES_cbc_encrypt);
437 if (memcmp(cleartext,test_input,len))
438 fprintf(stderr,"input_%d mismatch\n",len), exit(4);
439 if (memcmp(iv,nistvector+len-tail,sizeof(iv)))
440 fprintf(stderr,"iv_%d mismatch\n",len), exit(4);
445 AES_set_encrypt_key(test_key,128,&encks);
446 AES_set_decrypt_key(test_key,128,&decks);
448 test_vector(vector_17,sizeof(vector_17));
449 test_vector(vector_31,sizeof(vector_31));
450 test_vector(vector_32,sizeof(vector_32));
451 test_vector(vector_47,sizeof(vector_47));
452 test_vector(vector_48,sizeof(vector_48));
453 test_vector(vector_64,sizeof(vector_64));
455 test_nistvector(vector_17,sizeof(vector_17));
456 test_nistvector(vector_31,sizeof(vector_31));
457 test_nistvector(vector_32,sizeof(vector_32));
458 test_nistvector(vector_47,sizeof(vector_47));
459 test_nistvector(vector_48,sizeof(vector_48));
460 test_nistvector(vector_64,sizeof(vector_64));