cipher_aes_cts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Helper functions for AES CBC CTS ciphers.
  11. *
  12. * The function dispatch tables are embedded into cipher_aes.c
  13. * using cipher_aes_cts.inc
  14. */
  15. /*
  16. * Refer to SP800-38A-Addendum
  17. *
  18. * Ciphertext stealing encrypts plaintext using a block cipher, without padding
  19. * the message to a multiple of the block size, so the ciphertext is the same
  20. * size as the plaintext.
  21. * It does this by altering processing of the last two blocks of the message.
  22. * The processing of all but the last two blocks is unchanged, but a portion of
  23. * the second-last block's ciphertext is "stolen" to pad the last plaintext
  24. * block. The padded final block is then encrypted as usual.
  25. * The final ciphertext for the last two blocks, consists of the partial block
  26. * (with the "stolen" portion omitted) plus the full final block,
  27. * which are the same size as the original plaintext.
  28. * Decryption requires decrypting the final block first, then restoring the
  29. * stolen ciphertext to the partial block, which can then be decrypted as usual.
  30. * AES_CBC_CTS has 3 variants:
  31. * (1) CS1 The NIST variant.
  32. * If the length is a multiple of the blocksize it is the same as CBC mode.
  33. * otherwise it produces C1||C2||(C(n-1))*||Cn.
  34. * Where C(n-1)* is a partial block.
  35. * (2) CS2
  36. * If the length is a multiple of the blocksize it is the same as CBC mode.
  37. * otherwise it produces C1||C2||Cn||(C(n-1))*.
  38. * Where C(n-1)* is a partial block.
  39. * (3) CS3 The Kerberos5 variant.
  40. * Produces C1||C2||Cn||(C(n-1))* regardless of the length.
  41. * If the length is a multiple of the blocksize it looks similar to CBC mode
  42. * with the last 2 blocks swapped.
  43. * Otherwise it is the same as CS2.
  44. */
  45. #include "e_os.h" /* strcasecmp */
  46. #include <openssl/core_names.h>
  47. #include <openssl/aes.h>
  48. #include "prov/ciphercommon.h"
  49. #include "internal/nelem.h"
  50. #include "cipher_aes_cts.h"
  51. /* The value assigned to 0 is the default */
  52. #define CTS_CS1 0
  53. #define CTS_CS2 1
  54. #define CTS_CS3 2
  55. typedef union {
  56. size_t align;
  57. unsigned char c[AES_BLOCK_SIZE];
  58. } aligned_16bytes;
  59. typedef struct cts_mode_name2id_st {
  60. unsigned int id;
  61. const char *name;
  62. } CTS_MODE_NAME2ID;
  63. static CTS_MODE_NAME2ID cts_modes[] =
  64. {
  65. { CTS_CS1, OSSL_CIPHER_CTS_MODE_CS1 },
  66. { CTS_CS2, OSSL_CIPHER_CTS_MODE_CS2 },
  67. { CTS_CS3, OSSL_CIPHER_CTS_MODE_CS3 },
  68. };
  69. const char *ossl_aes_cbc_cts_mode_id2name(unsigned int id)
  70. {
  71. size_t i;
  72. for (i = 0; i < OSSL_NELEM(cts_modes); ++i) {
  73. if (cts_modes[i].id == id)
  74. return cts_modes[i].name;
  75. }
  76. return NULL;
  77. }
  78. int ossl_aes_cbc_cts_mode_name2id(const char *name)
  79. {
  80. size_t i;
  81. for (i = 0; i < OSSL_NELEM(cts_modes); ++i) {
  82. if (strcasecmp(name, cts_modes[i].name) == 0)
  83. return (int)cts_modes[i].id;
  84. }
  85. return -1;
  86. }
  87. static size_t cts128_cs1_encrypt(PROV_CIPHER_CTX *ctx, const unsigned char *in,
  88. unsigned char *out, size_t len)
  89. {
  90. aligned_16bytes tmp_in;
  91. size_t residue;
  92. residue = len % AES_BLOCK_SIZE;
  93. len -= residue;
  94. if (!ctx->hw->cipher(ctx, out, in, len))
  95. return 0;
  96. if (residue == 0)
  97. return len;
  98. in += len;
  99. out += len;
  100. memset(tmp_in.c, 0, sizeof(tmp_in));
  101. memcpy(tmp_in.c, in, residue);
  102. if (!ctx->hw->cipher(ctx, out - AES_BLOCK_SIZE + residue, tmp_in.c,
  103. AES_BLOCK_SIZE))
  104. return 0;
  105. return len + residue;
  106. }
  107. static void do_xor(const unsigned char *in1, const unsigned char *in2,
  108. size_t len, unsigned char *out)
  109. {
  110. size_t i;
  111. for (i = 0; i < len; ++i)
  112. out[i] = in1[i] ^ in2[i];
  113. }
  114. static size_t cts128_cs1_decrypt(PROV_CIPHER_CTX *ctx, const unsigned char *in,
  115. unsigned char *out, size_t len)
  116. {
  117. aligned_16bytes mid_iv, ct_mid, pt_last;
  118. size_t residue;
  119. residue = len % AES_BLOCK_SIZE;
  120. if (residue == 0) {
  121. /* If there are no partial blocks then it is the same as CBC mode */
  122. if (!ctx->hw->cipher(ctx, out, in, len))
  123. return 0;
  124. return len;
  125. }
  126. /* Process blocks at the start - but leave the last 2 blocks */
  127. len -= AES_BLOCK_SIZE + residue;
  128. if (len > 0) {
  129. if (!ctx->hw->cipher(ctx, out, in, len))
  130. return 0;
  131. in += len;
  132. out += len;
  133. }
  134. /* Save the iv that will be used by the second last block */
  135. memcpy(mid_iv.c, ctx->iv, AES_BLOCK_SIZE);
  136. /* Decrypt the last block first using an iv of zero */
  137. memset(ctx->iv, 0, AES_BLOCK_SIZE);
  138. if (!ctx->hw->cipher(ctx, pt_last.c, in + residue, AES_BLOCK_SIZE))
  139. return 0;
  140. /*
  141. * Rebuild the ciphertext of the second last block as a combination of
  142. * the decrypted last block + replace the start with the ciphertext bytes
  143. * of the partial second last block.
  144. */
  145. memcpy(ct_mid.c, in, residue);
  146. memcpy(ct_mid.c + residue, pt_last.c + residue, AES_BLOCK_SIZE - residue);
  147. /*
  148. * Restore the last partial ciphertext block.
  149. * Now that we have the cipher text of the second last block, apply
  150. * that to the partial plaintext end block. We have already decrypted the
  151. * block using an IV of zero. For decryption the IV is just XORed after
  152. * doing an AES block - so just XOR in the cipher text.
  153. */
  154. do_xor(ct_mid.c, pt_last.c, residue, out + AES_BLOCK_SIZE);
  155. /* Restore the iv needed by the second last block */
  156. memcpy(ctx->iv, mid_iv.c, AES_BLOCK_SIZE);
  157. /*
  158. * Decrypt the second last plaintext block now that we have rebuilt the
  159. * ciphertext.
  160. */
  161. if (!ctx->hw->cipher(ctx, out, ct_mid.c, AES_BLOCK_SIZE))
  162. return 0;
  163. return len + AES_BLOCK_SIZE + residue;
  164. }
  165. static size_t cts128_cs3_encrypt(PROV_CIPHER_CTX *ctx, const unsigned char *in,
  166. unsigned char *out, size_t len)
  167. {
  168. aligned_16bytes tmp_in;
  169. size_t residue;
  170. if (len <= AES_BLOCK_SIZE) /* CS3 requires 2 blocks */
  171. return 0;
  172. residue = len % AES_BLOCK_SIZE;
  173. if (residue == 0)
  174. residue = AES_BLOCK_SIZE;
  175. len -= residue;
  176. if (!ctx->hw->cipher(ctx, out, in, len))
  177. return 0;
  178. in += len;
  179. out += len;
  180. memset(tmp_in.c, 0, sizeof(tmp_in));
  181. memcpy(tmp_in.c, in, residue);
  182. memcpy(out, out - AES_BLOCK_SIZE, residue);
  183. if (!ctx->hw->cipher(ctx, out - AES_BLOCK_SIZE, tmp_in.c, AES_BLOCK_SIZE))
  184. return 0;
  185. return len + residue;
  186. }
  187. /*
  188. * Note:
  189. * The cipher text (in) is of the form C(0), C(1), ., C(n), C(n-1)* where
  190. * C(n) is a full block and C(n-1)* can be a partial block
  191. * (but could be a full block).
  192. * This means that the output plaintext (out) needs to swap the plaintext of
  193. * the last two decoded ciphertext blocks.
  194. */
  195. static size_t cts128_cs3_decrypt(PROV_CIPHER_CTX *ctx, const unsigned char *in,
  196. unsigned char *out, size_t len)
  197. {
  198. aligned_16bytes mid_iv, ct_mid, pt_last;
  199. size_t residue;
  200. if (len <= AES_BLOCK_SIZE) /* CS3 requires 2 blocks */
  201. return 0;
  202. /* Process blocks at the start - but leave the last 2 blocks */
  203. residue = len % AES_BLOCK_SIZE;
  204. if (residue == 0)
  205. residue = AES_BLOCK_SIZE;
  206. len -= AES_BLOCK_SIZE + residue;
  207. if (len > 0) {
  208. if (!ctx->hw->cipher(ctx, out, in, len))
  209. return 0;
  210. in += len;
  211. out += len;
  212. }
  213. /* Save the iv that will be used by the second last block */
  214. memcpy(mid_iv.c, ctx->iv, AES_BLOCK_SIZE);
  215. /* Decrypt the Cn block first using an iv of zero */
  216. memset(ctx->iv, 0, AES_BLOCK_SIZE);
  217. if (!ctx->hw->cipher(ctx, pt_last.c, in, AES_BLOCK_SIZE))
  218. return 0;
  219. /*
  220. * Rebuild the ciphertext of C(n-1) as a combination of
  221. * the decrypted C(n) block + replace the start with the ciphertext bytes
  222. * of the partial last block.
  223. */
  224. memcpy(ct_mid.c, in + AES_BLOCK_SIZE, residue);
  225. if (residue != AES_BLOCK_SIZE)
  226. memcpy(ct_mid.c + residue, pt_last.c + residue, AES_BLOCK_SIZE - residue);
  227. /*
  228. * Restore the last partial ciphertext block.
  229. * Now that we have the cipher text of the second last block, apply
  230. * that to the partial plaintext end block. We have already decrypted the
  231. * block using an IV of zero. For decryption the IV is just XORed after
  232. * doing an AES block - so just XOR in the ciphertext.
  233. */
  234. do_xor(ct_mid.c, pt_last.c, residue, out + AES_BLOCK_SIZE);
  235. /* Restore the iv needed by the second last block */
  236. memcpy(ctx->iv, mid_iv.c, AES_BLOCK_SIZE);
  237. /*
  238. * Decrypt the second last plaintext block now that we have rebuilt the
  239. * ciphertext.
  240. */
  241. if (!ctx->hw->cipher(ctx, out, ct_mid.c, AES_BLOCK_SIZE))
  242. return 0;
  243. return len + AES_BLOCK_SIZE + residue;
  244. }
  245. static size_t cts128_cs2_encrypt(PROV_CIPHER_CTX *ctx, const unsigned char *in,
  246. unsigned char *out, size_t len)
  247. {
  248. if (len % AES_BLOCK_SIZE == 0) {
  249. /* If there are no partial blocks then it is the same as CBC mode */
  250. if (!ctx->hw->cipher(ctx, out, in, len))
  251. return 0;
  252. return len;
  253. }
  254. /* For partial blocks CS2 is equivalent to CS3 */
  255. return cts128_cs3_encrypt(ctx, in, out, len);
  256. }
  257. static size_t cts128_cs2_decrypt(PROV_CIPHER_CTX *ctx, const unsigned char *in,
  258. unsigned char *out, size_t len)
  259. {
  260. if (len % AES_BLOCK_SIZE == 0) {
  261. /* If there are no partial blocks then it is the same as CBC mode */
  262. if (!ctx->hw->cipher(ctx, out, in, len))
  263. return 0;
  264. return len;
  265. }
  266. /* For partial blocks CS2 is equivalent to CS3 */
  267. return cts128_cs3_decrypt(ctx, in, out, len);
  268. }
  269. int ossl_aes_cbc_cts_block_update(void *vctx, unsigned char *out, size_t *outl,
  270. size_t outsize, const unsigned char *in,
  271. size_t inl)
  272. {
  273. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  274. size_t sz = 0;
  275. if (inl < AES_BLOCK_SIZE) /* There must be at least one block for CTS mode */
  276. return 0;
  277. if (outsize < inl)
  278. return 0;
  279. if (out == NULL) {
  280. *outl = inl;
  281. return 1;
  282. }
  283. /*
  284. * Return an error if the update is called multiple times, only one shot
  285. * is supported.
  286. */
  287. if (ctx->updated == 1)
  288. return 0;
  289. if (ctx->enc) {
  290. if (ctx->cts_mode == CTS_CS1)
  291. sz = cts128_cs1_encrypt(ctx, in, out, inl);
  292. else if (ctx->cts_mode == CTS_CS2)
  293. sz = cts128_cs2_encrypt(ctx, in, out, inl);
  294. else if (ctx->cts_mode == CTS_CS3)
  295. sz = cts128_cs3_encrypt(ctx, in, out, inl);
  296. } else {
  297. if (ctx->cts_mode == CTS_CS1)
  298. sz = cts128_cs1_decrypt(ctx, in, out, inl);
  299. else if (ctx->cts_mode == CTS_CS2)
  300. sz = cts128_cs2_decrypt(ctx, in, out, inl);
  301. else if (ctx->cts_mode == CTS_CS3)
  302. sz = cts128_cs3_decrypt(ctx, in, out, inl);
  303. }
  304. if (sz == 0)
  305. return 0;
  306. ctx->updated = 1; /* Stop multiple updates being allowed */
  307. *outl = sz;
  308. return 1;
  309. }
  310. int ossl_aes_cbc_cts_block_final(void *vctx, unsigned char *out, size_t *outl,
  311. size_t outsize)
  312. {
  313. *outl = 0;
  314. return 1;
  315. }