yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* hmac.h |
| 2 | * |
| 3 | * Copyright (C) 2006-2021 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 | |
| 22 | /*! |
| 23 | \file wolfssl/wolfcrypt/hmac.h |
| 24 | */ |
| 25 | |
| 26 | #ifndef NO_HMAC |
| 27 | |
| 28 | #ifndef WOLF_CRYPT_HMAC_H |
| 29 | #define WOLF_CRYPT_HMAC_H |
| 30 | |
| 31 | #include <wolfssl/wolfcrypt/hash.h> |
| 32 | |
| 33 | #if defined(HAVE_FIPS) && \ |
| 34 | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)) |
| 35 | /* for fips @wc_fips */ |
| 36 | #include <cyassl/ctaocrypt/hmac.h> |
| 37 | #define WC_HMAC_BLOCK_SIZE HMAC_BLOCK_SIZE |
| 38 | #endif |
| 39 | |
| 40 | |
| 41 | #if defined(HAVE_FIPS) && \ |
| 42 | defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) |
| 43 | #include <wolfssl/wolfcrypt/fips.h> |
| 44 | #endif |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
| 50 | /* avoid redefinition of structs */ |
| 51 | #if !defined(HAVE_FIPS) || \ |
| 52 | (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) |
| 53 | |
| 54 | #ifdef WOLFSSL_ASYNC_CRYPT |
| 55 | #include <wolfssl/wolfcrypt/async.h> |
| 56 | #endif |
| 57 | |
| 58 | #ifndef NO_OLD_WC_NAMES |
| 59 | #define HMAC_BLOCK_SIZE WC_HMAC_BLOCK_SIZE |
| 60 | #endif |
| 61 | |
| 62 | #define WC_HMAC_INNER_HASH_KEYED_SW 1 |
| 63 | #define WC_HMAC_INNER_HASH_KEYED_DEV 2 |
| 64 | |
| 65 | enum { |
| 66 | HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */ |
| 67 | |
| 68 | IPAD = 0x36, |
| 69 | OPAD = 0x5C, |
| 70 | |
| 71 | /* If any hash is not enabled, add the ID here. */ |
| 72 | #ifdef NO_MD5 |
| 73 | WC_MD5 = WC_HASH_TYPE_MD5, |
| 74 | #endif |
| 75 | #ifdef NO_SHA |
| 76 | WC_SHA = WC_HASH_TYPE_SHA, |
| 77 | #endif |
| 78 | #ifdef NO_SHA256 |
| 79 | WC_SHA256 = WC_HASH_TYPE_SHA256, |
| 80 | #endif |
| 81 | #ifndef WOLFSSL_SHA512 |
| 82 | WC_SHA512 = WC_HASH_TYPE_SHA512, |
| 83 | #endif |
| 84 | #ifndef WOLFSSL_SHA384 |
| 85 | WC_SHA384 = WC_HASH_TYPE_SHA384, |
| 86 | #endif |
| 87 | #ifndef WOLFSSL_SHA224 |
| 88 | WC_SHA224 = WC_HASH_TYPE_SHA224, |
| 89 | #endif |
| 90 | #ifndef WOLFSSL_SHA3 |
| 91 | WC_SHA3_224 = WC_HASH_TYPE_SHA3_224, |
| 92 | WC_SHA3_256 = WC_HASH_TYPE_SHA3_256, |
| 93 | WC_SHA3_384 = WC_HASH_TYPE_SHA3_384, |
| 94 | WC_SHA3_512 = WC_HASH_TYPE_SHA3_512, |
| 95 | #endif |
| 96 | #ifdef HAVE_PKCS11 |
| 97 | HMAC_MAX_ID_LEN = 32, |
| 98 | HMAC_MAX_LABEL_LEN = 32, |
| 99 | #endif |
| 100 | }; |
| 101 | |
| 102 | /* Select the largest available hash for the buffer size. */ |
| 103 | #define WC_HMAC_BLOCK_SIZE WC_MAX_BLOCK_SIZE |
| 104 | |
| 105 | #if !defined(WOLFSSL_SHA3) && !defined(WOLFSSL_SHA512) && \ |
| 106 | !defined(WOLFSSL_SHA384) && defined(NO_SHA256) && \ |
| 107 | defined(WOLFSSL_SHA224) && defined(NO_SHA) && defined(NO_MD5) |
| 108 | #error "You have to have some kind of hash if you want to use HMAC." |
| 109 | #endif |
| 110 | |
| 111 | |
| 112 | /* hash union */ |
| 113 | typedef union { |
| 114 | #ifndef NO_MD5 |
| 115 | wc_Md5 md5; |
| 116 | #endif |
| 117 | #ifndef NO_SHA |
| 118 | wc_Sha sha; |
| 119 | #endif |
| 120 | #ifdef WOLFSSL_SHA224 |
| 121 | wc_Sha224 sha224; |
| 122 | #endif |
| 123 | #ifndef NO_SHA256 |
| 124 | wc_Sha256 sha256; |
| 125 | #endif |
| 126 | #ifdef WOLFSSL_SHA384 |
| 127 | wc_Sha384 sha384; |
| 128 | #endif |
| 129 | #ifdef WOLFSSL_SHA512 |
| 130 | wc_Sha512 sha512; |
| 131 | #endif |
| 132 | #ifdef WOLFSSL_SHA3 |
| 133 | wc_Sha3 sha3; |
| 134 | #endif |
| 135 | } wc_Hmac_Hash; |
| 136 | |
| 137 | /* Hmac digest */ |
| 138 | struct Hmac { |
| 139 | wc_Hmac_Hash hash; |
| 140 | word32 ipad[WC_HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/ |
| 141 | word32 opad[WC_HMAC_BLOCK_SIZE / sizeof(word32)]; |
| 142 | word32 innerHash[WC_MAX_DIGEST_SIZE / sizeof(word32)]; |
| 143 | void* heap; /* heap hint */ |
| 144 | byte macType; /* md5 sha or sha256 */ |
| 145 | byte innerHashKeyed; /* keyed flag */ |
| 146 | #ifdef WOLFSSL_ASYNC_CRYPT |
| 147 | WC_ASYNC_DEV asyncDev; |
| 148 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
| 149 | #ifdef WOLF_CRYPTO_CB |
| 150 | int devId; |
| 151 | void* devCtx; |
| 152 | const byte* keyRaw; |
| 153 | #endif |
| 154 | #ifdef HAVE_PKCS11 |
| 155 | byte id[HMAC_MAX_ID_LEN]; |
| 156 | int idLen; |
| 157 | char label[HMAC_MAX_LABEL_LEN]; |
| 158 | int labelLen; |
| 159 | #endif |
| 160 | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB) |
| 161 | word16 keyLen; /* hmac key length (key in ipad) */ |
| 162 | #endif |
| 163 | }; |
| 164 | |
| 165 | #ifndef WC_HMAC_TYPE_DEFINED |
| 166 | typedef struct Hmac Hmac; |
| 167 | #define WC_HMAC_TYPE_DEFINED |
| 168 | #endif |
| 169 | |
| 170 | |
| 171 | #endif /* HAVE_FIPS */ |
| 172 | |
| 173 | /* does init */ |
| 174 | WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz); |
| 175 | WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32); |
| 176 | WOLFSSL_API int wc_HmacFinal(Hmac*, byte*); |
| 177 | WOLFSSL_API int wc_HmacSizeByType(int type); |
| 178 | |
| 179 | WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId); |
| 180 | #ifdef HAVE_PKCS11 |
| 181 | WOLFSSL_API int wc_HmacInit_Id(Hmac* hmac, byte* id, int len, void* heap, |
| 182 | int devId); |
| 183 | WOLFSSL_API int wc_HmacInit_Label(Hmac* hmac, const char* label, void* heap, |
| 184 | int devId); |
| 185 | #endif |
| 186 | WOLFSSL_API void wc_HmacFree(Hmac*); |
| 187 | |
| 188 | WOLFSSL_API int wolfSSL_GetHmacMaxSize(void); |
| 189 | |
| 190 | WOLFSSL_LOCAL int _InitHmac(Hmac* hmac, int type, void* heap); |
| 191 | |
| 192 | #ifdef HAVE_HKDF |
| 193 | |
| 194 | WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz, |
| 195 | const byte* inKey, word32 inKeySz, byte* out); |
| 196 | WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz, |
| 197 | const byte* info, word32 infoSz, |
| 198 | byte* out, word32 outSz); |
| 199 | |
| 200 | WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz, |
| 201 | const byte* salt, word32 saltSz, |
| 202 | const byte* info, word32 infoSz, |
| 203 | byte* out, word32 outSz); |
| 204 | |
| 205 | #endif /* HAVE_HKDF */ |
| 206 | |
| 207 | #ifdef __cplusplus |
| 208 | } /* extern "C" */ |
| 209 | #endif |
| 210 | |
| 211 | #endif /* WOLF_CRYPT_HMAC_H */ |
| 212 | |
| 213 | #endif /* NO_HMAC */ |
| 214 | |