cmac.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* cmac.h
  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. #ifndef WOLF_CRYPT_CMAC_H
  22. #define WOLF_CRYPT_CMAC_H
  23. #include <wolfssl/wolfcrypt/types.h>
  24. #include <wolfssl/wolfcrypt/aes.h>
  25. #if !defined(NO_AES) && defined(WOLFSSL_CMAC)
  26. #if defined(HAVE_FIPS) && \
  27. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  28. #include <wolfssl/wolfcrypt/fips.h>
  29. #endif /* HAVE_FIPS_VERSION >= 2 */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* avoid redefinition of structs */
  34. #if !defined(HAVE_FIPS) || \
  35. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  36. #ifndef WC_CMAC_TYPE_DEFINED
  37. typedef struct Cmac Cmac;
  38. #define WC_CMAC_TYPE_DEFINED
  39. #endif
  40. struct Cmac {
  41. Aes aes;
  42. byte buffer[AES_BLOCK_SIZE]; /* partially stored block */
  43. byte digest[AES_BLOCK_SIZE]; /* running digest */
  44. byte k1[AES_BLOCK_SIZE];
  45. byte k2[AES_BLOCK_SIZE];
  46. word32 bufferSz;
  47. word32 totalSz;
  48. #ifdef WOLF_CRYPTO_CB
  49. int devId;
  50. void* devCtx;
  51. #ifdef WOLFSSL_CAAM
  52. byte ctx[32]; /* hold state for save and return */
  53. word32 blackKey;
  54. word32 keylen;
  55. byte initialized;
  56. #endif
  57. #endif
  58. #if defined(WOLFSSL_HASH_KEEP)
  59. byte* msg;
  60. word32 used;
  61. word32 len;
  62. #endif
  63. #ifdef WOLFSSL_SE050
  64. byte useSWCrypt; /* Use SW crypt instead of SE050, before SCP03 auth */
  65. #endif
  66. };
  67. typedef enum CmacType {
  68. WC_CMAC_AES = 1
  69. } CmacType;
  70. #define WC_CMAC_TAG_MAX_SZ AES_BLOCK_SIZE
  71. #define WC_CMAC_TAG_MIN_SZ (AES_BLOCK_SIZE/4)
  72. #endif /* HAVE_FIPS */
  73. WOLFSSL_API
  74. int wc_InitCmac(Cmac* cmac,
  75. const byte* key, word32 keySz,
  76. int type, void* unused);
  77. WOLFSSL_API
  78. int wc_InitCmac_ex(Cmac* cmac,
  79. const byte* key, word32 keySz,
  80. int type, void* unused, void* heap, int devId);
  81. WOLFSSL_API
  82. int wc_CmacUpdate(Cmac* cmac,
  83. const byte* in, word32 inSz);
  84. WOLFSSL_API
  85. int wc_CmacFinalNoFree(Cmac* cmac,
  86. byte* out, word32* outSz);
  87. WOLFSSL_API
  88. int wc_CmacFinal(Cmac* cmac,
  89. byte* out, word32* outSz);
  90. WOLFSSL_API
  91. int wc_CmacFree(Cmac* cmac);
  92. WOLFSSL_API
  93. int wc_AesCmacGenerate(byte* out, word32* outSz,
  94. const byte* in, word32 inSz,
  95. const byte* key, word32 keySz);
  96. WOLFSSL_API
  97. int wc_AesCmacVerify(const byte* check, word32 checkSz,
  98. const byte* in, word32 inSz,
  99. const byte* key, word32 keySz);
  100. WOLFSSL_LOCAL
  101. void ShiftAndXorRb(byte* out, byte* in);
  102. #ifdef WOLFSSL_HASH_KEEP
  103. WOLFSSL_API
  104. int wc_CMAC_Grow(Cmac* cmac, const byte* in, int inSz);
  105. #endif
  106. #ifdef __cplusplus
  107. } /* extern "C" */
  108. #endif
  109. #endif /* NO_AES && WOLFSSL_CMAC */
  110. #endif /* WOLF_CRYPT_CMAC_H */