cmac.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* crypto/cmac/cmac.c */
  2. /* Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * [email protected].
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include "cryptlib.h"
  57. #include <openssl/cmac.h>
  58. #ifdef OPENSSL_FIPS
  59. #include <openssl/fips.h>
  60. #endif
  61. struct CMAC_CTX_st
  62. {
  63. /* Cipher context to use */
  64. EVP_CIPHER_CTX cctx;
  65. /* Keys k1 and k2 */
  66. unsigned char k1[EVP_MAX_BLOCK_LENGTH];
  67. unsigned char k2[EVP_MAX_BLOCK_LENGTH];
  68. /* Temporary block */
  69. unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
  70. /* Last (possibly partial) block */
  71. unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
  72. /* Number of bytes in last block: -1 means context not initialised */
  73. int nlast_block;
  74. };
  75. /* Make temporary keys K1 and K2 */
  76. static void make_kn(unsigned char *k1, unsigned char *l, int bl)
  77. {
  78. int i;
  79. /* Shift block to left, including carry */
  80. for (i = 0; i < bl; i++)
  81. {
  82. k1[i] = l[i] << 1;
  83. if (i < bl - 1 && l[i + 1] & 0x80)
  84. k1[i] |= 1;
  85. }
  86. /* If MSB set fixup with R */
  87. if (l[0] & 0x80)
  88. k1[bl - 1] ^= bl == 16 ? 0x87 : 0x1b;
  89. }
  90. CMAC_CTX *CMAC_CTX_new(void)
  91. {
  92. CMAC_CTX *ctx;
  93. ctx = OPENSSL_malloc(sizeof(CMAC_CTX));
  94. if (!ctx)
  95. return NULL;
  96. EVP_CIPHER_CTX_init(&ctx->cctx);
  97. ctx->nlast_block = -1;
  98. return ctx;
  99. }
  100. void CMAC_CTX_cleanup(CMAC_CTX *ctx)
  101. {
  102. #ifdef OPENSSL_FIPS
  103. if (FIPS_mode() && !ctx->cctx.engine)
  104. {
  105. FIPS_cmac_ctx_cleanup(ctx);
  106. return;
  107. }
  108. #endif
  109. EVP_CIPHER_CTX_cleanup(&ctx->cctx);
  110. OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
  111. OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH);
  112. OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH);
  113. OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
  114. ctx->nlast_block = -1;
  115. }
  116. EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx)
  117. {
  118. return &ctx->cctx;
  119. }
  120. void CMAC_CTX_free(CMAC_CTX *ctx)
  121. {
  122. CMAC_CTX_cleanup(ctx);
  123. OPENSSL_free(ctx);
  124. }
  125. int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
  126. {
  127. int bl;
  128. if (in->nlast_block == -1)
  129. return 0;
  130. if (!EVP_CIPHER_CTX_copy(&out->cctx, &in->cctx))
  131. return 0;
  132. bl = EVP_CIPHER_CTX_block_size(&in->cctx);
  133. memcpy(out->k1, in->k1, bl);
  134. memcpy(out->k2, in->k2, bl);
  135. memcpy(out->tbl, in->tbl, bl);
  136. memcpy(out->last_block, in->last_block, bl);
  137. out->nlast_block = in->nlast_block;
  138. return 1;
  139. }
  140. int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
  141. const EVP_CIPHER *cipher, ENGINE *impl)
  142. {
  143. static unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH];
  144. #ifdef OPENSSL_FIPS
  145. if (FIPS_mode())
  146. {
  147. /* If we have an ENGINE need to allow non FIPS */
  148. if ((impl || ctx->cctx.engine)
  149. && !(ctx->cctx.flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW))
  150. {
  151. EVPerr(EVP_F_CMAC_INIT, EVP_R_DISABLED_FOR_FIPS);
  152. return 0;
  153. }
  154. /* Other algorithm blocking will be done in FIPS_cmac_init,
  155. * via FIPS_cipherinit().
  156. */
  157. if (!impl && !ctx->cctx.engine)
  158. return FIPS_cmac_init(ctx, key, keylen, cipher, NULL);
  159. }
  160. #endif
  161. /* All zeros means restart */
  162. if (!key && !cipher && !impl && keylen == 0)
  163. {
  164. /* Not initialised */
  165. if (ctx->nlast_block == -1)
  166. return 0;
  167. if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
  168. return 0;
  169. memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(&ctx->cctx));
  170. ctx->nlast_block = 0;
  171. return 1;
  172. }
  173. /* Initialiase context */
  174. if (cipher && !EVP_EncryptInit_ex(&ctx->cctx, cipher, impl, NULL, NULL))
  175. return 0;
  176. /* Non-NULL key means initialisation complete */
  177. if (key)
  178. {
  179. int bl;
  180. if (!EVP_CIPHER_CTX_cipher(&ctx->cctx))
  181. return 0;
  182. if (!EVP_CIPHER_CTX_set_key_length(&ctx->cctx, keylen))
  183. return 0;
  184. if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, key, zero_iv))
  185. return 0;
  186. bl = EVP_CIPHER_CTX_block_size(&ctx->cctx);
  187. if (!EVP_Cipher(&ctx->cctx, ctx->tbl, zero_iv, bl))
  188. return 0;
  189. make_kn(ctx->k1, ctx->tbl, bl);
  190. make_kn(ctx->k2, ctx->k1, bl);
  191. OPENSSL_cleanse(ctx->tbl, bl);
  192. /* Reset context again ready for first data block */
  193. if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
  194. return 0;
  195. /* Zero tbl so resume works */
  196. memset(ctx->tbl, 0, bl);
  197. ctx->nlast_block = 0;
  198. }
  199. return 1;
  200. }
  201. int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
  202. {
  203. const unsigned char *data = in;
  204. size_t bl;
  205. #ifdef OPENSSL_FIPS
  206. if (FIPS_mode() && !ctx->cctx.engine)
  207. return FIPS_cmac_update(ctx, in, dlen);
  208. #endif
  209. if (ctx->nlast_block == -1)
  210. return 0;
  211. if (dlen == 0)
  212. return 1;
  213. bl = EVP_CIPHER_CTX_block_size(&ctx->cctx);
  214. /* Copy into partial block if we need to */
  215. if (ctx->nlast_block > 0)
  216. {
  217. size_t nleft;
  218. nleft = bl - ctx->nlast_block;
  219. if (dlen < nleft)
  220. nleft = dlen;
  221. memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
  222. dlen -= nleft;
  223. ctx->nlast_block += nleft;
  224. /* If no more to process return */
  225. if (dlen == 0)
  226. return 1;
  227. data += nleft;
  228. /* Else not final block so encrypt it */
  229. if (!EVP_Cipher(&ctx->cctx, ctx->tbl, ctx->last_block,bl))
  230. return 0;
  231. }
  232. /* Encrypt all but one of the complete blocks left */
  233. while(dlen > bl)
  234. {
  235. if (!EVP_Cipher(&ctx->cctx, ctx->tbl, data, bl))
  236. return 0;
  237. dlen -= bl;
  238. data += bl;
  239. }
  240. /* Copy any data left to last block buffer */
  241. memcpy(ctx->last_block, data, dlen);
  242. ctx->nlast_block = dlen;
  243. return 1;
  244. }
  245. int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
  246. {
  247. int i, bl, lb;
  248. #ifdef OPENSSL_FIPS
  249. if (FIPS_mode() && !ctx->cctx.engine)
  250. return FIPS_cmac_final(ctx, out, poutlen);
  251. #endif
  252. if (ctx->nlast_block == -1)
  253. return 0;
  254. bl = EVP_CIPHER_CTX_block_size(&ctx->cctx);
  255. *poutlen = (size_t)bl;
  256. if (!out)
  257. return 1;
  258. lb = ctx->nlast_block;
  259. /* Is last block complete? */
  260. if (lb == bl)
  261. {
  262. for (i = 0; i < bl; i++)
  263. out[i] = ctx->last_block[i] ^ ctx->k1[i];
  264. }
  265. else
  266. {
  267. ctx->last_block[lb] = 0x80;
  268. if (bl - lb > 1)
  269. memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
  270. for (i = 0; i < bl; i++)
  271. out[i] = ctx->last_block[i] ^ ctx->k2[i];
  272. }
  273. if (!EVP_Cipher(&ctx->cctx, out, out, bl))
  274. {
  275. OPENSSL_cleanse(out, bl);
  276. return 0;
  277. }
  278. return 1;
  279. }
  280. int CMAC_resume(CMAC_CTX *ctx)
  281. {
  282. if (ctx->nlast_block == -1)
  283. return 0;
  284. /* The buffer "tbl" containes the last fully encrypted block
  285. * which is the last IV (or all zeroes if no last encrypted block).
  286. * The last block has not been modified since CMAC_final().
  287. * So reinitliasing using the last decrypted block will allow
  288. * CMAC to continue after calling CMAC_Final().
  289. */
  290. return EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, ctx->tbl);
  291. }