cmac.c 8.5 KB

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