cmac.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* cmac.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifdef WOLFSSL_QNX_CAAM
  26. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  27. #endif
  28. #if defined(WOLFSSL_HASH_KEEP)
  29. #include <wolfssl/wolfcrypt/hash.h>
  30. #endif
  31. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  32. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  33. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  34. #define FIPS_NO_WRAPPERS
  35. #ifdef USE_WINDOWS_API
  36. #pragma code_seg(".fipsA$n")
  37. #pragma const_seg(".fipsB$n")
  38. #endif
  39. #endif
  40. #ifdef NO_INLINE
  41. #include <wolfssl/wolfcrypt/misc.h>
  42. #else
  43. #define WOLFSSL_MISC_INCLUDED
  44. #include <wolfcrypt/src/misc.c>
  45. #endif
  46. #include <wolfssl/wolfcrypt/error-crypt.h>
  47. #include <wolfssl/wolfcrypt/aes.h>
  48. #include <wolfssl/wolfcrypt/cmac.h>
  49. #ifdef WOLF_CRYPTO_CB
  50. #include <wolfssl/wolfcrypt/cryptocb.h>
  51. #endif
  52. #ifdef WOLFSSL_HASH_KEEP
  53. /* Some hardware have issues with update, this function stores the data to be
  54. * hashed into an array. Once ready, the Final operation is called on all of the
  55. * data to be hashed at once.
  56. * returns 0 on success
  57. */
  58. int wc_CMAC_Grow(Cmac* cmac, const byte* in, int inSz)
  59. {
  60. return _wc_Hash_Grow(&cmac->msg, &cmac->used, &cmac->len, in, inSz, NULL);
  61. }
  62. #endif /* WOLFSSL_HASH_KEEP */
  63. /* Used by AES-SIV. See aes.c. */
  64. void ShiftAndXorRb(byte* out, byte* in)
  65. {
  66. int i, j, xorRb;
  67. int mask = 0, last = 0;
  68. byte Rb = 0x87;
  69. xorRb = (in[0] & 0x80) != 0;
  70. for (i = 1, j = AES_BLOCK_SIZE - 1; i <= AES_BLOCK_SIZE; i++, j--) {
  71. last = (in[j] & 0x80) ? 1 : 0;
  72. out[j] = (byte)((in[j] << 1) | mask);
  73. mask = last;
  74. if (xorRb) {
  75. out[j] ^= Rb;
  76. Rb = 0;
  77. }
  78. }
  79. }
  80. /* returns 0 on success */
  81. int wc_InitCmac_ex(Cmac* cmac, const byte* key, word32 keySz,
  82. int type, void* unused, void* heap, int devId)
  83. {
  84. int ret;
  85. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
  86. byte useSW = 0;
  87. #endif
  88. (void)unused;
  89. (void)heap;
  90. if (cmac == NULL || type != WC_CMAC_AES) {
  91. return BAD_FUNC_ARG;
  92. }
  93. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
  94. /* save if we should use SW crypt, restore after memset */
  95. useSW = cmac->useSWCrypt;
  96. #endif
  97. XMEMSET(cmac, 0, sizeof(Cmac));
  98. #ifdef WOLF_CRYPTO_CB
  99. /* Set devId regardless of value (invalid or not) */
  100. cmac->devId = devId;
  101. #ifndef WOLF_CRYPTO_CB_FIND
  102. if (devId != INVALID_DEVID)
  103. #endif
  104. {
  105. cmac->devCtx = NULL;
  106. ret = wc_CryptoCb_Cmac(cmac, key, keySz, NULL, 0, NULL, NULL,
  107. type, unused);
  108. if (ret != CRYPTOCB_UNAVAILABLE)
  109. return ret;
  110. /* fall-through when unavailable */
  111. }
  112. #else
  113. (void)devId;
  114. #endif
  115. if (key == NULL || keySz == 0) {
  116. return BAD_FUNC_ARG;
  117. }
  118. ret = wc_AesInit(&cmac->aes, heap, devId);
  119. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
  120. cmac->useSWCrypt = useSW;
  121. if (cmac->useSWCrypt == 1) {
  122. cmac->aes.useSWCrypt = 1;
  123. }
  124. #endif
  125. if (ret == 0) {
  126. ret = wc_AesSetKey(&cmac->aes, key, keySz, NULL, AES_ENCRYPTION);
  127. }
  128. if (ret == 0) {
  129. byte l[AES_BLOCK_SIZE];
  130. XMEMSET(l, 0, AES_BLOCK_SIZE);
  131. ret = wc_AesEncryptDirect(&cmac->aes, l, l);
  132. if (ret == 0) {
  133. ShiftAndXorRb(cmac->k1, l);
  134. ShiftAndXorRb(cmac->k2, cmac->k1);
  135. ForceZero(l, AES_BLOCK_SIZE);
  136. }
  137. }
  138. return ret;
  139. }
  140. int wc_InitCmac(Cmac* cmac, const byte* key, word32 keySz,
  141. int type, void* unused)
  142. {
  143. #ifdef WOLFSSL_QNX_CAAM
  144. int devId = WOLFSSL_CAAM_DEVID;
  145. #else
  146. int devId = INVALID_DEVID;
  147. #endif
  148. return wc_InitCmac_ex(cmac, key, keySz, type, unused, NULL, devId);
  149. }
  150. int wc_CmacUpdate(Cmac* cmac, const byte* in, word32 inSz)
  151. {
  152. int ret = 0;
  153. if ((cmac == NULL) || (in == NULL && inSz != 0)) {
  154. return BAD_FUNC_ARG;
  155. }
  156. #ifdef WOLF_CRYPTO_CB
  157. #ifndef WOLF_CRYPTO_CB_FIND
  158. if (cmac->devId != INVALID_DEVID)
  159. #endif
  160. {
  161. ret = wc_CryptoCb_Cmac(cmac, NULL, 0, in, inSz,
  162. NULL, NULL, 0, NULL);
  163. if (ret != CRYPTOCB_UNAVAILABLE)
  164. return ret;
  165. /* fall-through when unavailable */
  166. ret = 0; /* reset error code */
  167. }
  168. #endif
  169. while (inSz != 0) {
  170. word32 add = min(inSz, AES_BLOCK_SIZE - cmac->bufferSz);
  171. XMEMCPY(&cmac->buffer[cmac->bufferSz], in, add);
  172. cmac->bufferSz += add;
  173. in += add;
  174. inSz -= add;
  175. if (cmac->bufferSz == AES_BLOCK_SIZE && inSz != 0) {
  176. if (cmac->totalSz != 0) {
  177. xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
  178. }
  179. ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer);
  180. if (ret == 0) {
  181. cmac->totalSz += AES_BLOCK_SIZE;
  182. cmac->bufferSz = 0;
  183. }
  184. }
  185. }
  186. return ret;
  187. }
  188. int wc_CmacFree(Cmac* cmac)
  189. {
  190. if (cmac == NULL)
  191. return BAD_FUNC_ARG;
  192. #if defined(WOLFSSL_HASH_KEEP)
  193. /* TODO: msg is leaked if wc_CmacFinal() is not called
  194. * e.g. when multiple calls to wc_CmacUpdate() and one fails but
  195. * wc_CmacFinal() not called. */
  196. if (cmac->msg != NULL) {
  197. XFREE(cmac->msg, cmac->heap, DYNAMIC_TYPE_TMP_BUFFER);
  198. }
  199. #endif
  200. wc_AesFree(&cmac->aes);
  201. ForceZero(cmac, sizeof(Cmac));
  202. return 0;
  203. }
  204. int wc_CmacFinalNoFree(Cmac* cmac, byte* out, word32* outSz)
  205. {
  206. int ret;
  207. const byte* subKey;
  208. word32 remainder;
  209. if (cmac == NULL || out == NULL || outSz == NULL) {
  210. return BAD_FUNC_ARG;
  211. }
  212. if (*outSz < WC_CMAC_TAG_MIN_SZ || *outSz > WC_CMAC_TAG_MAX_SZ) {
  213. return BUFFER_E;
  214. }
  215. #ifdef WOLF_CRYPTO_CB
  216. #ifndef WOLF_CRYPTO_CB_FIND
  217. if (cmac->devId != INVALID_DEVID)
  218. #endif
  219. {
  220. ret = wc_CryptoCb_Cmac(cmac, NULL, 0, NULL, 0, out, outSz, 0, NULL);
  221. if (ret != CRYPTOCB_UNAVAILABLE)
  222. return ret;
  223. /* fall-through when unavailable */
  224. }
  225. #endif
  226. if (cmac->bufferSz == AES_BLOCK_SIZE) {
  227. subKey = cmac->k1;
  228. }
  229. else {
  230. /* ensure we will have a valid remainder value */
  231. if (cmac->bufferSz > AES_BLOCK_SIZE) {
  232. return BAD_STATE_E;
  233. }
  234. remainder = AES_BLOCK_SIZE - cmac->bufferSz;
  235. if (remainder == 0) {
  236. remainder = AES_BLOCK_SIZE;
  237. }
  238. if (remainder > 1) {
  239. XMEMSET(cmac->buffer + AES_BLOCK_SIZE - remainder, 0, remainder);
  240. }
  241. cmac->buffer[AES_BLOCK_SIZE - remainder] = 0x80;
  242. subKey = cmac->k2;
  243. }
  244. xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
  245. xorbuf(cmac->buffer, subKey, AES_BLOCK_SIZE);
  246. ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer);
  247. if (ret == 0) {
  248. XMEMCPY(out, cmac->digest, *outSz);
  249. }
  250. return 0;
  251. }
  252. int wc_CmacFinal(Cmac* cmac, byte* out, word32* outSz) {
  253. int ret;
  254. if (cmac == NULL)
  255. return BAD_FUNC_ARG;
  256. ret = wc_CmacFinalNoFree(cmac, out, outSz);
  257. (void)wc_CmacFree(cmac);
  258. return ret;
  259. }
  260. int wc_AesCmacGenerate(byte* out, word32* outSz,
  261. const byte* in, word32 inSz,
  262. const byte* key, word32 keySz)
  263. {
  264. int ret;
  265. #ifdef WOLFSSL_SMALL_STACK
  266. Cmac *cmac;
  267. #else
  268. Cmac cmac[1];
  269. #endif
  270. if (out == NULL || (in == NULL && inSz > 0) || key == NULL || keySz == 0) {
  271. return BAD_FUNC_ARG;
  272. }
  273. #ifdef WOLFSSL_SMALL_STACK
  274. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, NULL,
  275. DYNAMIC_TYPE_CMAC)) == NULL) {
  276. return MEMORY_E;
  277. }
  278. #endif
  279. #ifdef WOLFSSL_CHECK_MEM_ZERO
  280. XMEMSET(((unsigned char *)cmac) + sizeof(Aes), 0xff,
  281. sizeof(Cmac) - sizeof(Aes));
  282. /* Aes part is checked by wc_AesFree. */
  283. wc_MemZero_Add("wc_AesCmacGenerate cmac",
  284. ((unsigned char *)cmac) + sizeof(Aes), sizeof(Cmac) - sizeof(Aes));
  285. #endif
  286. ret = wc_InitCmac(cmac, key, keySz, WC_CMAC_AES, NULL);
  287. if (ret == 0) {
  288. ret = wc_CmacUpdate(cmac, in, inSz);
  289. }
  290. if (ret == 0) {
  291. ret = wc_CmacFinal(cmac, out, outSz);
  292. }
  293. #ifdef WOLFSSL_SMALL_STACK
  294. if (cmac) {
  295. XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
  296. }
  297. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  298. wc_MemZero_Check(cmac, sizeof(Cmac));
  299. #endif
  300. return ret;
  301. }
  302. int wc_AesCmacVerify(const byte* check, word32 checkSz,
  303. const byte* in, word32 inSz,
  304. const byte* key, word32 keySz)
  305. {
  306. int ret;
  307. byte a[AES_BLOCK_SIZE];
  308. word32 aSz = sizeof(a);
  309. int compareRet;
  310. if (check == NULL || checkSz == 0 || (in == NULL && inSz != 0) ||
  311. key == NULL || keySz == 0) {
  312. return BAD_FUNC_ARG;
  313. }
  314. XMEMSET(a, 0, aSz);
  315. ret = wc_AesCmacGenerate(a, &aSz, in, inSz, key, keySz);
  316. compareRet = ConstantCompare(check, a, (int)min(checkSz, aSz));
  317. if (ret == 0)
  318. ret = compareRet ? 1 : 0;
  319. return ret;
  320. }
  321. #endif /* WOLFSSL_CMAC && NO_AES && WOLFSSL_AES_DIRECT */