cmac.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2010-2024 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. * CMAC low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "internal/cryptlib.h"
  18. #include <openssl/cmac.h>
  19. #include <openssl/err.h>
  20. #define LOCAL_BUF_SIZE 2048
  21. struct CMAC_CTX_st {
  22. /* Cipher context to use */
  23. EVP_CIPHER_CTX *cctx;
  24. /* Keys k1 and k2 */
  25. unsigned char k1[EVP_MAX_BLOCK_LENGTH];
  26. unsigned char k2[EVP_MAX_BLOCK_LENGTH];
  27. /* Temporary block */
  28. unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
  29. /* Last (possibly partial) block */
  30. unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
  31. /* Number of bytes in last block: -1 means context not initialised */
  32. int nlast_block;
  33. };
  34. /* Make temporary keys K1 and K2 */
  35. static void make_kn(unsigned char *k1, const unsigned char *l, int bl)
  36. {
  37. int i;
  38. unsigned char c = l[0], carry = c >> 7, cnext;
  39. /* Shift block to left, including carry */
  40. for (i = 0; i < bl - 1; i++, c = cnext)
  41. k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7);
  42. /* If MSB set fixup with R */
  43. k1[i] = (c << 1) ^ ((0 - carry) & (bl == 16 ? 0x87 : 0x1b));
  44. }
  45. CMAC_CTX *CMAC_CTX_new(void)
  46. {
  47. CMAC_CTX *ctx;
  48. if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL)
  49. return NULL;
  50. ctx->cctx = EVP_CIPHER_CTX_new();
  51. if (ctx->cctx == NULL) {
  52. OPENSSL_free(ctx);
  53. return NULL;
  54. }
  55. ctx->nlast_block = -1;
  56. return ctx;
  57. }
  58. void CMAC_CTX_cleanup(CMAC_CTX *ctx)
  59. {
  60. EVP_CIPHER_CTX_reset(ctx->cctx);
  61. OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
  62. OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH);
  63. OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH);
  64. OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
  65. ctx->nlast_block = -1;
  66. }
  67. EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx)
  68. {
  69. return ctx->cctx;
  70. }
  71. void CMAC_CTX_free(CMAC_CTX *ctx)
  72. {
  73. if (!ctx)
  74. return;
  75. CMAC_CTX_cleanup(ctx);
  76. EVP_CIPHER_CTX_free(ctx->cctx);
  77. OPENSSL_free(ctx);
  78. }
  79. int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
  80. {
  81. int bl;
  82. if (in->nlast_block == -1)
  83. return 0;
  84. if ((bl = EVP_CIPHER_CTX_get_block_size(in->cctx)) == 0)
  85. return 0;
  86. if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
  87. return 0;
  88. memcpy(out->k1, in->k1, bl);
  89. memcpy(out->k2, in->k2, bl);
  90. memcpy(out->tbl, in->tbl, bl);
  91. memcpy(out->last_block, in->last_block, bl);
  92. out->nlast_block = in->nlast_block;
  93. return 1;
  94. }
  95. int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
  96. const EVP_CIPHER *cipher, ENGINE *impl)
  97. {
  98. static const unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH] = { 0 };
  99. int block_len;
  100. /* All zeros means restart */
  101. if (!key && !cipher && !impl && keylen == 0) {
  102. /* Not initialised */
  103. if (ctx->nlast_block == -1)
  104. return 0;
  105. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
  106. return 0;
  107. block_len = EVP_CIPHER_CTX_get_block_size(ctx->cctx);
  108. if (block_len == 0)
  109. return 0;
  110. memset(ctx->tbl, 0, block_len);
  111. ctx->nlast_block = 0;
  112. return 1;
  113. }
  114. /* Initialise context */
  115. if (cipher != NULL) {
  116. /* Ensure we can't use this ctx until we also have a key */
  117. ctx->nlast_block = -1;
  118. if (!EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
  119. return 0;
  120. }
  121. /* Non-NULL key means initialisation complete */
  122. if (key != NULL) {
  123. int bl;
  124. /* If anything fails then ensure we can't use this ctx */
  125. ctx->nlast_block = -1;
  126. if (EVP_CIPHER_CTX_get0_cipher(ctx->cctx) == NULL)
  127. return 0;
  128. if (EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen) <= 0)
  129. return 0;
  130. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, key, zero_iv))
  131. return 0;
  132. if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) < 0)
  133. return 0;
  134. if (EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl) <= 0)
  135. return 0;
  136. make_kn(ctx->k1, ctx->tbl, bl);
  137. make_kn(ctx->k2, ctx->k1, bl);
  138. OPENSSL_cleanse(ctx->tbl, bl);
  139. /* Reset context again ready for first data block */
  140. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
  141. return 0;
  142. /* Zero tbl so resume works */
  143. memset(ctx->tbl, 0, bl);
  144. ctx->nlast_block = 0;
  145. }
  146. return 1;
  147. }
  148. int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
  149. {
  150. const unsigned char *data = in;
  151. int bl;
  152. size_t max_burst_blocks, cipher_blocks;
  153. unsigned char buf[LOCAL_BUF_SIZE];
  154. if (ctx->nlast_block == -1)
  155. return 0;
  156. if (dlen == 0)
  157. return 1;
  158. if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) == 0)
  159. return 0;
  160. /* Copy into partial block if we need to */
  161. if (ctx->nlast_block > 0) {
  162. size_t nleft;
  163. nleft = bl - ctx->nlast_block;
  164. if (dlen < nleft)
  165. nleft = dlen;
  166. memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
  167. dlen -= nleft;
  168. ctx->nlast_block += nleft;
  169. /* If no more to process return */
  170. if (dlen == 0)
  171. return 1;
  172. data += nleft;
  173. /* Else not final block so encrypt it */
  174. if (EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl) <= 0)
  175. return 0;
  176. }
  177. /* Encrypt all but one of the complete blocks left */
  178. max_burst_blocks = LOCAL_BUF_SIZE / bl;
  179. cipher_blocks = (dlen - 1) / bl;
  180. if (max_burst_blocks == 0) {
  181. /*
  182. * When block length is greater than local buffer size,
  183. * use ctx->tbl as cipher output.
  184. */
  185. while (dlen > (size_t)bl) {
  186. if (EVP_Cipher(ctx->cctx, ctx->tbl, data, bl) <= 0)
  187. return 0;
  188. dlen -= bl;
  189. data += bl;
  190. }
  191. } else {
  192. while (cipher_blocks > max_burst_blocks) {
  193. if (EVP_Cipher(ctx->cctx, buf, data, max_burst_blocks * bl) <= 0)
  194. return 0;
  195. dlen -= max_burst_blocks * bl;
  196. data += max_burst_blocks * bl;
  197. cipher_blocks -= max_burst_blocks;
  198. }
  199. if (cipher_blocks > 0) {
  200. if (EVP_Cipher(ctx->cctx, buf, data, cipher_blocks * bl) <= 0)
  201. return 0;
  202. dlen -= cipher_blocks * bl;
  203. data += cipher_blocks * bl;
  204. memcpy(ctx->tbl, &buf[(cipher_blocks - 1) * bl], bl);
  205. }
  206. }
  207. /* Copy any data left to last block buffer */
  208. memcpy(ctx->last_block, data, dlen);
  209. ctx->nlast_block = dlen;
  210. return 1;
  211. }
  212. int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
  213. {
  214. int i, bl, lb;
  215. if (ctx->nlast_block == -1)
  216. return 0;
  217. if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) == 0)
  218. return 0;
  219. if (poutlen != NULL)
  220. *poutlen = (size_t)bl;
  221. if (!out)
  222. return 1;
  223. lb = ctx->nlast_block;
  224. /* Is last block complete? */
  225. if (lb == bl) {
  226. for (i = 0; i < bl; i++)
  227. out[i] = ctx->last_block[i] ^ ctx->k1[i];
  228. } else {
  229. ctx->last_block[lb] = 0x80;
  230. if (bl - lb > 1)
  231. memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
  232. for (i = 0; i < bl; i++)
  233. out[i] = ctx->last_block[i] ^ ctx->k2[i];
  234. }
  235. if (EVP_Cipher(ctx->cctx, out, out, bl) <= 0) {
  236. OPENSSL_cleanse(out, bl);
  237. return 0;
  238. }
  239. return 1;
  240. }
  241. int CMAC_resume(CMAC_CTX *ctx)
  242. {
  243. if (ctx->nlast_block == -1)
  244. return 0;
  245. /*
  246. * The buffer "tbl" contains the last fully encrypted block which is the
  247. * last IV (or all zeroes if no last encrypted block). The last block has
  248. * not been modified since CMAC_final(). So reinitialising using the last
  249. * decrypted block will allow CMAC to continue after calling
  250. * CMAC_Final().
  251. */
  252. return EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, ctx->tbl);
  253. }