Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
sPymbed
Date:
Tue Nov 19 14:32:16 2019 +0000
Revision:
16:048e5e270a58
Parent:
15:117db924cf7c
working ssl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* hmac.h
wolfSSL 15:117db924cf7c 2 *
wolfSSL 15:117db924cf7c 3 * Copyright (C) 2006-2017 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22 /*!
wolfSSL 15:117db924cf7c 23 \file wolfssl/wolfcrypt/hmac.h
wolfSSL 15:117db924cf7c 24 */
wolfSSL 15:117db924cf7c 25
wolfSSL 15:117db924cf7c 26 #ifndef NO_HMAC
wolfSSL 15:117db924cf7c 27
wolfSSL 15:117db924cf7c 28 #ifndef WOLF_CRYPT_HMAC_H
wolfSSL 15:117db924cf7c 29 #define WOLF_CRYPT_HMAC_H
wolfSSL 15:117db924cf7c 30
wolfSSL 15:117db924cf7c 31 #include <wolfssl/wolfcrypt/hash.h>
wolfSSL 15:117db924cf7c 32
wolfSSL 15:117db924cf7c 33 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 34 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
wolfSSL 15:117db924cf7c 35 /* for fips @wc_fips */
wolfSSL 15:117db924cf7c 36 #include <cyassl/ctaocrypt/hmac.h>
wolfSSL 15:117db924cf7c 37 #define WC_HMAC_BLOCK_SIZE HMAC_BLOCK_SIZE
wolfSSL 15:117db924cf7c 38 #endif
wolfSSL 15:117db924cf7c 39
wolfSSL 15:117db924cf7c 40
wolfSSL 15:117db924cf7c 41 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 42 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
wolfSSL 15:117db924cf7c 43 #include <wolfssl/wolfcrypt/fips.h>
wolfSSL 15:117db924cf7c 44 #endif
wolfSSL 15:117db924cf7c 45
wolfSSL 15:117db924cf7c 46 #ifdef __cplusplus
wolfSSL 15:117db924cf7c 47 extern "C" {
wolfSSL 15:117db924cf7c 48 #endif
wolfSSL 15:117db924cf7c 49
wolfSSL 15:117db924cf7c 50 /* avoid redefinition of structs */
wolfSSL 15:117db924cf7c 51 #if !defined(HAVE_FIPS) || \
wolfSSL 15:117db924cf7c 52 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
wolfSSL 15:117db924cf7c 53
wolfSSL 15:117db924cf7c 54 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 55 #include <wolfssl/wolfcrypt/async.h>
wolfSSL 15:117db924cf7c 56 #endif
wolfSSL 15:117db924cf7c 57
wolfSSL 15:117db924cf7c 58 #ifndef NO_OLD_WC_NAMES
wolfSSL 15:117db924cf7c 59 #define HMAC_BLOCK_SIZE WC_HMAC_BLOCK_SIZE
wolfSSL 15:117db924cf7c 60 #endif
wolfSSL 15:117db924cf7c 61
wolfSSL 15:117db924cf7c 62 enum {
wolfSSL 15:117db924cf7c 63 HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */
wolfSSL 15:117db924cf7c 64
wolfSSL 15:117db924cf7c 65 IPAD = 0x36,
wolfSSL 15:117db924cf7c 66 OPAD = 0x5C,
wolfSSL 15:117db924cf7c 67
wolfSSL 15:117db924cf7c 68 /* If any hash is not enabled, add the ID here. */
wolfSSL 15:117db924cf7c 69 #ifdef NO_MD5
wolfSSL 15:117db924cf7c 70 WC_MD5 = WC_HASH_TYPE_MD5,
wolfSSL 15:117db924cf7c 71 #endif
wolfSSL 15:117db924cf7c 72 #ifdef NO_SHA
wolfSSL 15:117db924cf7c 73 WC_SHA = WC_HASH_TYPE_SHA,
wolfSSL 15:117db924cf7c 74 #endif
wolfSSL 15:117db924cf7c 75 #ifdef NO_SHA256
wolfSSL 15:117db924cf7c 76 WC_SHA256 = WC_HASH_TYPE_SHA256,
wolfSSL 15:117db924cf7c 77 #endif
wolfSSL 15:117db924cf7c 78 #ifndef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 79 WC_SHA512 = WC_HASH_TYPE_SHA512,
wolfSSL 15:117db924cf7c 80 #endif
wolfSSL 15:117db924cf7c 81 #ifndef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 82 WC_SHA384 = WC_HASH_TYPE_SHA384,
wolfSSL 15:117db924cf7c 83 #endif
wolfSSL 15:117db924cf7c 84 #ifndef HAVE_BLAKE2
wolfSSL 15:117db924cf7c 85 BLAKE2B_ID = WC_HASH_TYPE_BLAKE2B,
wolfSSL 15:117db924cf7c 86 #endif
wolfSSL 15:117db924cf7c 87 #ifndef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 88 WC_SHA224 = WC_HASH_TYPE_SHA224,
wolfSSL 15:117db924cf7c 89 #endif
wolfSSL 15:117db924cf7c 90 #ifndef WOLFSSL_SHA3
wolfSSL 15:117db924cf7c 91 WC_SHA3_224 = WC_HASH_TYPE_SHA3_224,
wolfSSL 15:117db924cf7c 92 WC_SHA3_256 = WC_HASH_TYPE_SHA3_256,
wolfSSL 15:117db924cf7c 93 WC_SHA3_384 = WC_HASH_TYPE_SHA3_384,
wolfSSL 15:117db924cf7c 94 WC_SHA3_512 = WC_HASH_TYPE_SHA3_512,
wolfSSL 15:117db924cf7c 95 #endif
wolfSSL 15:117db924cf7c 96 };
wolfSSL 15:117db924cf7c 97
wolfSSL 15:117db924cf7c 98 /* Select the largest available hash for the buffer size. */
wolfSSL 15:117db924cf7c 99 #define WC_HMAC_BLOCK_SIZE WC_MAX_BLOCK_SIZE
wolfSSL 15:117db924cf7c 100
wolfSSL 15:117db924cf7c 101 #if !defined(WOLFSSL_SHA3) && !defined(WOLFSSL_SHA512) && !defined(HAVE_BLAKE2) && \
wolfSSL 15:117db924cf7c 102 !defined(WOLFSSL_SHA384) && defined(NO_SHA256) && defined(WOLFSSL_SHA224) && \
wolfSSL 15:117db924cf7c 103 defined(NO_SHA) && defined(NO_MD5)
wolfSSL 15:117db924cf7c 104 #error "You have to have some kind of hash if you want to use HMAC."
wolfSSL 15:117db924cf7c 105 #endif
wolfSSL 15:117db924cf7c 106
wolfSSL 15:117db924cf7c 107
wolfSSL 15:117db924cf7c 108 /* hash union */
wolfSSL 15:117db924cf7c 109 typedef union {
wolfSSL 15:117db924cf7c 110 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 111 wc_Md5 md5;
wolfSSL 15:117db924cf7c 112 #endif
wolfSSL 15:117db924cf7c 113 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 114 wc_Sha sha;
wolfSSL 15:117db924cf7c 115 #endif
wolfSSL 15:117db924cf7c 116 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 117 wc_Sha224 sha224;
wolfSSL 15:117db924cf7c 118 #endif
wolfSSL 15:117db924cf7c 119 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 120 wc_Sha256 sha256;
wolfSSL 15:117db924cf7c 121 #endif
wolfSSL 15:117db924cf7c 122 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 123 wc_Sha384 sha384;
wolfSSL 15:117db924cf7c 124 #endif
wolfSSL 15:117db924cf7c 125 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 126 wc_Sha512 sha512;
wolfSSL 15:117db924cf7c 127 #endif
wolfSSL 15:117db924cf7c 128 #ifdef HAVE_BLAKE2
wolfSSL 15:117db924cf7c 129 Blake2b blake2b;
wolfSSL 15:117db924cf7c 130 #endif
wolfSSL 15:117db924cf7c 131 #ifdef WOLFSSL_SHA3
wolfSSL 15:117db924cf7c 132 wc_Sha3 sha3;
wolfSSL 15:117db924cf7c 133 #endif
wolfSSL 15:117db924cf7c 134 } Hash;
wolfSSL 15:117db924cf7c 135
wolfSSL 15:117db924cf7c 136 /* Hmac digest */
wolfSSL 15:117db924cf7c 137 typedef struct Hmac {
wolfSSL 15:117db924cf7c 138 Hash hash;
wolfSSL 15:117db924cf7c 139 word32 ipad[WC_HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/
wolfSSL 15:117db924cf7c 140 word32 opad[WC_HMAC_BLOCK_SIZE / sizeof(word32)];
wolfSSL 15:117db924cf7c 141 word32 innerHash[WC_MAX_DIGEST_SIZE / sizeof(word32)];
wolfSSL 15:117db924cf7c 142 void* heap; /* heap hint */
wolfSSL 15:117db924cf7c 143 byte macType; /* md5 sha or sha256 */
wolfSSL 15:117db924cf7c 144 byte innerHashKeyed; /* keyed flag */
wolfSSL 15:117db924cf7c 145
wolfSSL 15:117db924cf7c 146 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 147 WC_ASYNC_DEV asyncDev;
wolfSSL 15:117db924cf7c 148 word16 keyLen; /* hmac key length (key in ipad) */
wolfSSL 15:117db924cf7c 149 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 150 } Hmac;
wolfSSL 15:117db924cf7c 151
wolfSSL 15:117db924cf7c 152 #endif /* HAVE_FIPS */
wolfSSL 15:117db924cf7c 153
wolfSSL 15:117db924cf7c 154 /* does init */
wolfSSL 15:117db924cf7c 155 WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
wolfSSL 15:117db924cf7c 156 WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32);
wolfSSL 15:117db924cf7c 157 WOLFSSL_API int wc_HmacFinal(Hmac*, byte*);
wolfSSL 15:117db924cf7c 158 WOLFSSL_API int wc_HmacSizeByType(int type);
wolfSSL 15:117db924cf7c 159
wolfSSL 15:117db924cf7c 160 WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId);
wolfSSL 15:117db924cf7c 161 WOLFSSL_API void wc_HmacFree(Hmac*);
wolfSSL 15:117db924cf7c 162
wolfSSL 15:117db924cf7c 163 WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
wolfSSL 15:117db924cf7c 164
wolfSSL 15:117db924cf7c 165 WOLFSSL_LOCAL int _InitHmac(Hmac* hmac, int type, void* heap);
wolfSSL 15:117db924cf7c 166
wolfSSL 15:117db924cf7c 167 #ifdef HAVE_HKDF
wolfSSL 15:117db924cf7c 168
wolfSSL 15:117db924cf7c 169 WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
wolfSSL 15:117db924cf7c 170 const byte* inKey, word32 inKeySz, byte* out);
wolfSSL 15:117db924cf7c 171 WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
wolfSSL 15:117db924cf7c 172 const byte* info, word32 infoSz,
wolfSSL 15:117db924cf7c 173 byte* out, word32 outSz);
wolfSSL 15:117db924cf7c 174
wolfSSL 15:117db924cf7c 175 WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
wolfSSL 15:117db924cf7c 176 const byte* salt, word32 saltSz,
wolfSSL 15:117db924cf7c 177 const byte* info, word32 infoSz,
wolfSSL 15:117db924cf7c 178 byte* out, word32 outSz);
wolfSSL 15:117db924cf7c 179
wolfSSL 15:117db924cf7c 180 #endif /* HAVE_HKDF */
wolfSSL 15:117db924cf7c 181
wolfSSL 15:117db924cf7c 182 #ifdef __cplusplus
wolfSSL 15:117db924cf7c 183 } /* extern "C" */
wolfSSL 15:117db924cf7c 184 #endif
wolfSSL 15:117db924cf7c 185
wolfSSL 15:117db924cf7c 186 #endif /* WOLF_CRYPT_HMAC_H */
wolfSSL 15:117db924cf7c 187
wolfSSL 15:117db924cf7c 188 #endif /* NO_HMAC */
wolfSSL 15:117db924cf7c 189
wolfSSL 15:117db924cf7c 190