cts128.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* ====================================================================
  2. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  3. *
  4. * Rights for redistribution and usage in source and binary
  5. * forms are granted according to the OpenSSL license.
  6. */
  7. #include <openssl/crypto.h>
  8. #include "modes_lcl.h"
  9. #include <string.h>
  10. #ifndef MODES_DEBUG
  11. # ifndef NDEBUG
  12. # define NDEBUG
  13. # endif
  14. #endif
  15. #include <assert.h>
  16. /*
  17. * Trouble with Ciphertext Stealing, CTS, mode is that there is no
  18. * common official specification, but couple of cipher/application
  19. * specific ones: RFC2040 and RFC3962. Then there is 'Proposal to
  20. * Extend CBC Mode By "Ciphertext Stealing"' at NIST site, which
  21. * deviates from mentioned RFCs. Most notably it allows input to be
  22. * of block length and it doesn't flip the order of the last two
  23. * blocks. CTS is being discussed even in ECB context, but it's not
  24. * adopted for any known application. This implementation provides
  25. * two interfaces: one compliant with above mentioned RFCs and one
  26. * compliant with the NIST proposal, both extending CBC mode.
  27. */
  28. size_t CRYPTO_cts128_encrypt_block(const unsigned char *in,
  29. unsigned char *out, size_t len,
  30. const void *key, unsigned char ivec[16],
  31. block128_f block)
  32. {
  33. size_t residue, n;
  34. assert(in && out && key && ivec);
  35. if (len <= 16)
  36. return 0;
  37. if ((residue = len % 16) == 0)
  38. residue = 16;
  39. len -= residue;
  40. CRYPTO_cbc128_encrypt(in, out, len, key, ivec, block);
  41. in += len;
  42. out += len;
  43. for (n = 0; n < residue; ++n)
  44. ivec[n] ^= in[n];
  45. (*block) (ivec, ivec, key);
  46. memcpy(out, out - 16, residue);
  47. memcpy(out - 16, ivec, 16);
  48. return len + residue;
  49. }
  50. size_t CRYPTO_nistcts128_encrypt_block(const unsigned char *in,
  51. unsigned char *out, size_t len,
  52. const void *key,
  53. unsigned char ivec[16],
  54. block128_f block)
  55. {
  56. size_t residue, n;
  57. assert(in && out && key && ivec);
  58. if (len < 16)
  59. return 0;
  60. residue = len % 16;
  61. len -= residue;
  62. CRYPTO_cbc128_encrypt(in, out, len, key, ivec, block);
  63. if (residue == 0)
  64. return len;
  65. in += len;
  66. out += len;
  67. for (n = 0; n < residue; ++n)
  68. ivec[n] ^= in[n];
  69. (*block) (ivec, ivec, key);
  70. memcpy(out - 16 + residue, ivec, 16);
  71. return len + residue;
  72. }
  73. size_t CRYPTO_cts128_encrypt(const unsigned char *in, unsigned char *out,
  74. size_t len, const void *key,
  75. unsigned char ivec[16], cbc128_f cbc)
  76. {
  77. size_t residue;
  78. union {
  79. size_t align;
  80. unsigned char c[16];
  81. } tmp;
  82. assert(in && out && key && ivec);
  83. if (len <= 16)
  84. return 0;
  85. if ((residue = len % 16) == 0)
  86. residue = 16;
  87. len -= residue;
  88. (*cbc) (in, out, len, key, ivec, 1);
  89. in += len;
  90. out += len;
  91. #if defined(CBC_HANDLES_TRUNCATED_IO)
  92. memcpy(tmp.c, out - 16, 16);
  93. (*cbc) (in, out - 16, residue, key, ivec, 1);
  94. memcpy(out, tmp.c, residue);
  95. #else
  96. memset(tmp.c, 0, sizeof(tmp));
  97. memcpy(tmp.c, in, residue);
  98. memcpy(out, out - 16, residue);
  99. (*cbc) (tmp.c, out - 16, 16, key, ivec, 1);
  100. #endif
  101. return len + residue;
  102. }
  103. size_t CRYPTO_nistcts128_encrypt(const unsigned char *in, unsigned char *out,
  104. size_t len, const void *key,
  105. unsigned char ivec[16], cbc128_f cbc)
  106. {
  107. size_t residue;
  108. union {
  109. size_t align;
  110. unsigned char c[16];
  111. } tmp;
  112. assert(in && out && key && ivec);
  113. if (len < 16)
  114. return 0;
  115. residue = len % 16;
  116. len -= residue;
  117. (*cbc) (in, out, len, key, ivec, 1);
  118. if (residue == 0)
  119. return len;
  120. in += len;
  121. out += len;
  122. #if defined(CBC_HANDLES_TRUNCATED_IO)
  123. (*cbc) (in, out - 16 + residue, residue, key, ivec, 1);
  124. #else
  125. memset(tmp.c, 0, sizeof(tmp));
  126. memcpy(tmp.c, in, residue);
  127. (*cbc) (tmp.c, out - 16 + residue, 16, key, ivec, 1);
  128. #endif
  129. return len + residue;
  130. }
  131. size_t CRYPTO_cts128_decrypt_block(const unsigned char *in,
  132. unsigned char *out, size_t len,
  133. const void *key, unsigned char ivec[16],
  134. block128_f block)
  135. {
  136. size_t residue, n;
  137. union {
  138. size_t align;
  139. unsigned char c[32];
  140. } tmp;
  141. assert(in && out && key && ivec);
  142. if (len <= 16)
  143. return 0;
  144. if ((residue = len % 16) == 0)
  145. residue = 16;
  146. len -= 16 + residue;
  147. if (len) {
  148. CRYPTO_cbc128_decrypt(in, out, len, key, ivec, block);
  149. in += len;
  150. out += len;
  151. }
  152. (*block) (in, tmp.c + 16, key);
  153. memcpy(tmp.c, tmp.c + 16, 16);
  154. memcpy(tmp.c, in + 16, residue);
  155. (*block) (tmp.c, tmp.c, key);
  156. for (n = 0; n < 16; ++n) {
  157. unsigned char c = in[n];
  158. out[n] = tmp.c[n] ^ ivec[n];
  159. ivec[n] = c;
  160. }
  161. for (residue += 16; n < residue; ++n)
  162. out[n] = tmp.c[n] ^ in[n];
  163. return 16 + len + residue;
  164. }
  165. size_t CRYPTO_nistcts128_decrypt_block(const unsigned char *in,
  166. unsigned char *out, size_t len,
  167. const void *key,
  168. unsigned char ivec[16],
  169. block128_f block)
  170. {
  171. size_t residue, n;
  172. union {
  173. size_t align;
  174. unsigned char c[32];
  175. } tmp;
  176. assert(in && out && key && ivec);
  177. if (len < 16)
  178. return 0;
  179. residue = len % 16;
  180. if (residue == 0) {
  181. CRYPTO_cbc128_decrypt(in, out, len, key, ivec, block);
  182. return len;
  183. }
  184. len -= 16 + residue;
  185. if (len) {
  186. CRYPTO_cbc128_decrypt(in, out, len, key, ivec, block);
  187. in += len;
  188. out += len;
  189. }
  190. (*block) (in + residue, tmp.c + 16, key);
  191. memcpy(tmp.c, tmp.c + 16, 16);
  192. memcpy(tmp.c, in, residue);
  193. (*block) (tmp.c, tmp.c, key);
  194. for (n = 0; n < 16; ++n) {
  195. unsigned char c = in[n];
  196. out[n] = tmp.c[n] ^ ivec[n];
  197. ivec[n] = in[n + residue];
  198. tmp.c[n] = c;
  199. }
  200. for (residue += 16; n < residue; ++n)
  201. out[n] = tmp.c[n] ^ tmp.c[n - 16];
  202. return 16 + len + residue;
  203. }
  204. size_t CRYPTO_cts128_decrypt(const unsigned char *in, unsigned char *out,
  205. size_t len, const void *key,
  206. unsigned char ivec[16], cbc128_f cbc)
  207. {
  208. size_t residue;
  209. union {
  210. size_t align;
  211. unsigned char c[32];
  212. } tmp;
  213. assert(in && out && key && ivec);
  214. if (len <= 16)
  215. return 0;
  216. if ((residue = len % 16) == 0)
  217. residue = 16;
  218. len -= 16 + residue;
  219. if (len) {
  220. (*cbc) (in, out, len, key, ivec, 0);
  221. in += len;
  222. out += len;
  223. }
  224. memset(tmp.c, 0, sizeof(tmp));
  225. /*
  226. * this places in[16] at &tmp.c[16] and decrypted block at &tmp.c[0]
  227. */
  228. (*cbc) (in, tmp.c, 16, key, tmp.c + 16, 0);
  229. memcpy(tmp.c, in + 16, residue);
  230. #if defined(CBC_HANDLES_TRUNCATED_IO)
  231. (*cbc) (tmp.c, out, 16 + residue, key, ivec, 0);
  232. #else
  233. (*cbc) (tmp.c, tmp.c, 32, key, ivec, 0);
  234. memcpy(out, tmp.c, 16 + residue);
  235. #endif
  236. return 16 + len + residue;
  237. }
  238. size_t CRYPTO_nistcts128_decrypt(const unsigned char *in, unsigned char *out,
  239. size_t len, const void *key,
  240. unsigned char ivec[16], cbc128_f cbc)
  241. {
  242. size_t residue;
  243. union {
  244. size_t align;
  245. unsigned char c[32];
  246. } tmp;
  247. assert(in && out && key && ivec);
  248. if (len < 16)
  249. return 0;
  250. residue = len % 16;
  251. if (residue == 0) {
  252. (*cbc) (in, out, len, key, ivec, 0);
  253. return len;
  254. }
  255. len -= 16 + residue;
  256. if (len) {
  257. (*cbc) (in, out, len, key, ivec, 0);
  258. in += len;
  259. out += len;
  260. }
  261. memset(tmp.c, 0, sizeof(tmp));
  262. /*
  263. * this places in[16] at &tmp.c[16] and decrypted block at &tmp.c[0]
  264. */
  265. (*cbc) (in + residue, tmp.c, 16, key, tmp.c + 16, 0);
  266. memcpy(tmp.c, in, residue);
  267. #if defined(CBC_HANDLES_TRUNCATED_IO)
  268. (*cbc) (tmp.c, out, 16 + residue, key, ivec, 0);
  269. #else
  270. (*cbc) (tmp.c, tmp.c, 32, key, ivec, 0);
  271. memcpy(out, tmp.c, 16 + residue);
  272. #endif
  273. return 16 + len + residue;
  274. }
  275. #if defined(SELFTEST)
  276. # include <stdio.h>
  277. # include <openssl/aes.h>
  278. /* test vectors from RFC 3962 */
  279. static const unsigned char test_key[16] = "chicken teriyaki";
  280. static const unsigned char test_input[64] =
  281. "I would like the" " General Gau's C"
  282. "hicken, please, " "and wonton soup.";
  283. static const unsigned char test_iv[16] =
  284. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  285. static const unsigned char vector_17[17] = {
  286. 0xc6, 0x35, 0x35, 0x68, 0xf2, 0xbf, 0x8c, 0xb4,
  287. 0xd8, 0xa5, 0x80, 0x36, 0x2d, 0xa7, 0xff, 0x7f,
  288. 0x97
  289. };
  290. static const unsigned char vector_31[31] = {
  291. 0xfc, 0x00, 0x78, 0x3e, 0x0e, 0xfd, 0xb2, 0xc1,
  292. 0xd4, 0x45, 0xd4, 0xc8, 0xef, 0xf7, 0xed, 0x22,
  293. 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
  294. 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5
  295. };
  296. static const unsigned char vector_32[32] = {
  297. 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5,
  298. 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5, 0xa8,
  299. 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
  300. 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84
  301. };
  302. static const unsigned char vector_47[47] = {
  303. 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
  304. 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84,
  305. 0xb3, 0xff, 0xfd, 0x94, 0x0c, 0x16, 0xa1, 0x8c,
  306. 0x1b, 0x55, 0x49, 0xd2, 0xf8, 0x38, 0x02, 0x9e,
  307. 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5,
  308. 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5
  309. };
  310. static const unsigned char vector_48[48] = {
  311. 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
  312. 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84,
  313. 0x9d, 0xad, 0x8b, 0xbb, 0x96, 0xc4, 0xcd, 0xc0,
  314. 0x3b, 0xc1, 0x03, 0xe1, 0xa1, 0x94, 0xbb, 0xd8,
  315. 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5,
  316. 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5, 0xa8
  317. };
  318. static const unsigned char vector_64[64] = {
  319. 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
  320. 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84,
  321. 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5,
  322. 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5, 0xa8,
  323. 0x48, 0x07, 0xef, 0xe8, 0x36, 0xee, 0x89, 0xa5,
  324. 0x26, 0x73, 0x0d, 0xbc, 0x2f, 0x7b, 0xc8, 0x40,
  325. 0x9d, 0xad, 0x8b, 0xbb, 0x96, 0xc4, 0xcd, 0xc0,
  326. 0x3b, 0xc1, 0x03, 0xe1, 0xa1, 0x94, 0xbb, 0xd8
  327. };
  328. static AES_KEY encks, decks;
  329. void test_vector(const unsigned char *vector, size_t len)
  330. {
  331. unsigned char iv[sizeof(test_iv)];
  332. unsigned char cleartext[64], ciphertext[64];
  333. size_t tail;
  334. printf("vector_%d\n", len);
  335. fflush(stdout);
  336. if ((tail = len % 16) == 0)
  337. tail = 16;
  338. tail += 16;
  339. /* test block-based encryption */
  340. memcpy(iv, test_iv, sizeof(test_iv));
  341. CRYPTO_cts128_encrypt_block(test_input, ciphertext, len, &encks, iv,
  342. (block128_f) AES_encrypt);
  343. if (memcmp(ciphertext, vector, len))
  344. fprintf(stderr, "output_%d mismatch\n", len), exit(1);
  345. if (memcmp(iv, vector + len - tail, sizeof(iv)))
  346. fprintf(stderr, "iv_%d mismatch\n", len), exit(1);
  347. /* test block-based decryption */
  348. memcpy(iv, test_iv, sizeof(test_iv));
  349. CRYPTO_cts128_decrypt_block(ciphertext, cleartext, len, &decks, iv,
  350. (block128_f) AES_decrypt);
  351. if (memcmp(cleartext, test_input, len))
  352. fprintf(stderr, "input_%d mismatch\n", len), exit(2);
  353. if (memcmp(iv, vector + len - tail, sizeof(iv)))
  354. fprintf(stderr, "iv_%d mismatch\n", len), exit(2);
  355. /* test streamed encryption */
  356. memcpy(iv, test_iv, sizeof(test_iv));
  357. CRYPTO_cts128_encrypt(test_input, ciphertext, len, &encks, iv,
  358. (cbc128_f) AES_cbc_encrypt);
  359. if (memcmp(ciphertext, vector, len))
  360. fprintf(stderr, "output_%d mismatch\n", len), exit(3);
  361. if (memcmp(iv, vector + len - tail, sizeof(iv)))
  362. fprintf(stderr, "iv_%d mismatch\n", len), exit(3);
  363. /* test streamed decryption */
  364. memcpy(iv, test_iv, sizeof(test_iv));
  365. CRYPTO_cts128_decrypt(ciphertext, cleartext, len, &decks, iv,
  366. (cbc128_f) AES_cbc_encrypt);
  367. if (memcmp(cleartext, test_input, len))
  368. fprintf(stderr, "input_%d mismatch\n", len), exit(4);
  369. if (memcmp(iv, vector + len - tail, sizeof(iv)))
  370. fprintf(stderr, "iv_%d mismatch\n", len), exit(4);
  371. }
  372. void test_nistvector(const unsigned char *vector, size_t len)
  373. {
  374. unsigned char iv[sizeof(test_iv)];
  375. unsigned char cleartext[64], ciphertext[64], nistvector[64];
  376. size_t tail;
  377. printf("nistvector_%d\n", len);
  378. fflush(stdout);
  379. if ((tail = len % 16) == 0)
  380. tail = 16;
  381. len -= 16 + tail;
  382. memcpy(nistvector, vector, len);
  383. /* flip two last blocks */
  384. memcpy(nistvector + len, vector + len + 16, tail);
  385. memcpy(nistvector + len + tail, vector + len, 16);
  386. len += 16 + tail;
  387. tail = 16;
  388. /* test block-based encryption */
  389. memcpy(iv, test_iv, sizeof(test_iv));
  390. CRYPTO_nistcts128_encrypt_block(test_input, ciphertext, len, &encks, iv,
  391. (block128_f) AES_encrypt);
  392. if (memcmp(ciphertext, nistvector, len))
  393. fprintf(stderr, "output_%d mismatch\n", len), exit(1);
  394. if (memcmp(iv, nistvector + len - tail, sizeof(iv)))
  395. fprintf(stderr, "iv_%d mismatch\n", len), exit(1);
  396. /* test block-based decryption */
  397. memcpy(iv, test_iv, sizeof(test_iv));
  398. CRYPTO_nistcts128_decrypt_block(ciphertext, cleartext, len, &decks, iv,
  399. (block128_f) AES_decrypt);
  400. if (memcmp(cleartext, test_input, len))
  401. fprintf(stderr, "input_%d mismatch\n", len), exit(2);
  402. if (memcmp(iv, nistvector + len - tail, sizeof(iv)))
  403. fprintf(stderr, "iv_%d mismatch\n", len), exit(2);
  404. /* test streamed encryption */
  405. memcpy(iv, test_iv, sizeof(test_iv));
  406. CRYPTO_nistcts128_encrypt(test_input, ciphertext, len, &encks, iv,
  407. (cbc128_f) AES_cbc_encrypt);
  408. if (memcmp(ciphertext, nistvector, len))
  409. fprintf(stderr, "output_%d mismatch\n", len), exit(3);
  410. if (memcmp(iv, nistvector + len - tail, sizeof(iv)))
  411. fprintf(stderr, "iv_%d mismatch\n", len), exit(3);
  412. /* test streamed decryption */
  413. memcpy(iv, test_iv, sizeof(test_iv));
  414. CRYPTO_nistcts128_decrypt(ciphertext, cleartext, len, &decks, iv,
  415. (cbc128_f) AES_cbc_encrypt);
  416. if (memcmp(cleartext, test_input, len))
  417. fprintf(stderr, "input_%d mismatch\n", len), exit(4);
  418. if (memcmp(iv, nistvector + len - tail, sizeof(iv)))
  419. fprintf(stderr, "iv_%d mismatch\n", len), exit(4);
  420. }
  421. int main()
  422. {
  423. AES_set_encrypt_key(test_key, 128, &encks);
  424. AES_set_decrypt_key(test_key, 128, &decks);
  425. test_vector(vector_17, sizeof(vector_17));
  426. test_vector(vector_31, sizeof(vector_31));
  427. test_vector(vector_32, sizeof(vector_32));
  428. test_vector(vector_47, sizeof(vector_47));
  429. test_vector(vector_48, sizeof(vector_48));
  430. test_vector(vector_64, sizeof(vector_64));
  431. test_nistvector(vector_17, sizeof(vector_17));
  432. test_nistvector(vector_31, sizeof(vector_31));
  433. test_nistvector(vector_32, sizeof(vector_32));
  434. test_nistvector(vector_47, sizeof(vector_47));
  435. test_nistvector(vector_48, sizeof(vector_48));
  436. test_nistvector(vector_64, sizeof(vector_64));
  437. return 0;
  438. }
  439. #endif