[352] | 1 | /* hmac.h
|
---|
| 2 | *
|
---|
| 3 | * Copyright (C) 2006-2017 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 |
|
---|
[372] | 22 | /*!
|
---|
| 23 | \file wolfssl/wolfcrypt/hmac.h
|
---|
| 24 | */
|
---|
[352] | 25 |
|
---|
| 26 | #ifndef NO_HMAC
|
---|
| 27 |
|
---|
| 28 | #ifndef WOLF_CRYPT_HMAC_H
|
---|
| 29 | #define WOLF_CRYPT_HMAC_H
|
---|
| 30 |
|
---|
[372] | 31 | #include <wolfssl/wolfcrypt/hash.h>
|
---|
[352] | 32 |
|
---|
[372] | 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
|
---|
[352] | 38 | #endif
|
---|
| 39 |
|
---|
| 40 |
|
---|
[372] | 41 | #if defined(HAVE_FIPS) && \
|
---|
| 42 | defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
|
---|
| 43 | #include <wolfssl/wolfcrypt/fips.h>
|
---|
[352] | 44 | #endif
|
---|
| 45 |
|
---|
| 46 | #ifdef __cplusplus
|
---|
| 47 | extern "C" {
|
---|
| 48 | #endif
|
---|
| 49 |
|
---|
[372] | 50 | /* avoid redefinition of structs */
|
---|
| 51 | #if !defined(HAVE_FIPS) || \
|
---|
| 52 | (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
|
---|
| 53 |
|
---|
[352] | 54 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
| 55 | #include <wolfssl/wolfcrypt/async.h>
|
---|
| 56 | #endif
|
---|
| 57 |
|
---|
[372] | 58 | #ifndef NO_OLD_WC_NAMES
|
---|
| 59 | #define HMAC_BLOCK_SIZE WC_HMAC_BLOCK_SIZE
|
---|
| 60 | #endif
|
---|
| 61 |
|
---|
[352] | 62 | enum {
|
---|
| 63 | HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */
|
---|
| 64 |
|
---|
| 65 | IPAD = 0x36,
|
---|
| 66 | OPAD = 0x5C,
|
---|
| 67 |
|
---|
| 68 | /* If any hash is not enabled, add the ID here. */
|
---|
| 69 | #ifdef NO_MD5
|
---|
[372] | 70 | WC_MD5 = WC_HASH_TYPE_MD5,
|
---|
[352] | 71 | #endif
|
---|
| 72 | #ifdef NO_SHA
|
---|
[372] | 73 | WC_SHA = WC_HASH_TYPE_SHA,
|
---|
[352] | 74 | #endif
|
---|
| 75 | #ifdef NO_SHA256
|
---|
[372] | 76 | WC_SHA256 = WC_HASH_TYPE_SHA256,
|
---|
[352] | 77 | #endif
|
---|
| 78 | #ifndef WOLFSSL_SHA512
|
---|
[372] | 79 | WC_SHA512 = WC_HASH_TYPE_SHA512,
|
---|
[352] | 80 | #endif
|
---|
| 81 | #ifndef WOLFSSL_SHA384
|
---|
[372] | 82 | WC_SHA384 = WC_HASH_TYPE_SHA384,
|
---|
[352] | 83 | #endif
|
---|
| 84 | #ifndef HAVE_BLAKE2
|
---|
[372] | 85 | BLAKE2B_ID = WC_HASH_TYPE_BLAKE2B,
|
---|
[352] | 86 | #endif
|
---|
| 87 | #ifndef WOLFSSL_SHA224
|
---|
[372] | 88 | WC_SHA224 = WC_HASH_TYPE_SHA224,
|
---|
[352] | 89 | #endif
|
---|
[372] | 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 | };
|
---|
[352] | 97 |
|
---|
[372] | 98 | /* Select the largest available hash for the buffer size. */
|
---|
| 99 | #define WC_HMAC_BLOCK_SIZE WC_MAX_BLOCK_SIZE
|
---|
[352] | 100 |
|
---|
[372] | 101 | #if !defined(WOLFSSL_SHA3) && !defined(WOLFSSL_SHA512) && !defined(HAVE_BLAKE2) && \
|
---|
| 102 | !defined(WOLFSSL_SHA384) && defined(NO_SHA256) && defined(WOLFSSL_SHA224) && \
|
---|
| 103 | defined(NO_SHA) && defined(NO_MD5)
|
---|
[352] | 104 | #error "You have to have some kind of hash if you want to use HMAC."
|
---|
| 105 | #endif
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | /* hash union */
|
---|
| 109 | typedef union {
|
---|
| 110 | #ifndef NO_MD5
|
---|
| 111 | wc_Md5 md5;
|
---|
| 112 | #endif
|
---|
| 113 | #ifndef NO_SHA
|
---|
| 114 | wc_Sha sha;
|
---|
| 115 | #endif
|
---|
| 116 | #ifdef WOLFSSL_SHA224
|
---|
| 117 | wc_Sha224 sha224;
|
---|
| 118 | #endif
|
---|
| 119 | #ifndef NO_SHA256
|
---|
| 120 | wc_Sha256 sha256;
|
---|
| 121 | #endif
|
---|
| 122 | #ifdef WOLFSSL_SHA384
|
---|
| 123 | wc_Sha384 sha384;
|
---|
| 124 | #endif
|
---|
[372] | 125 | #ifdef WOLFSSL_SHA512
|
---|
[352] | 126 | wc_Sha512 sha512;
|
---|
| 127 | #endif
|
---|
| 128 | #ifdef HAVE_BLAKE2
|
---|
| 129 | Blake2b blake2b;
|
---|
| 130 | #endif
|
---|
[372] | 131 | #ifdef WOLFSSL_SHA3
|
---|
| 132 | wc_Sha3 sha3;
|
---|
| 133 | #endif
|
---|
[352] | 134 | } Hash;
|
---|
| 135 |
|
---|
| 136 | /* Hmac digest */
|
---|
| 137 | typedef struct Hmac {
|
---|
| 138 | Hash hash;
|
---|
[372] | 139 | word32 ipad[WC_HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/
|
---|
| 140 | word32 opad[WC_HMAC_BLOCK_SIZE / sizeof(word32)];
|
---|
| 141 | word32 innerHash[WC_MAX_DIGEST_SIZE / sizeof(word32)];
|
---|
[352] | 142 | void* heap; /* heap hint */
|
---|
| 143 | byte macType; /* md5 sha or sha256 */
|
---|
| 144 | byte innerHashKeyed; /* keyed flag */
|
---|
| 145 |
|
---|
| 146 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
| 147 | WC_ASYNC_DEV asyncDev;
|
---|
| 148 | word16 keyLen; /* hmac key length (key in ipad) */
|
---|
| 149 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
| 150 | } Hmac;
|
---|
| 151 |
|
---|
| 152 | #endif /* HAVE_FIPS */
|
---|
| 153 |
|
---|
| 154 | /* does init */
|
---|
| 155 | WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
|
---|
| 156 | WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32);
|
---|
| 157 | WOLFSSL_API int wc_HmacFinal(Hmac*, byte*);
|
---|
| 158 | WOLFSSL_API int wc_HmacSizeByType(int type);
|
---|
| 159 |
|
---|
| 160 | WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId);
|
---|
| 161 | WOLFSSL_API void wc_HmacFree(Hmac*);
|
---|
| 162 |
|
---|
| 163 | WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
|
---|
| 164 |
|
---|
[372] | 165 | WOLFSSL_LOCAL int _InitHmac(Hmac* hmac, int type, void* heap);
|
---|
| 166 |
|
---|
[352] | 167 | #ifdef HAVE_HKDF
|
---|
| 168 |
|
---|
| 169 | WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
|
---|
| 170 | const byte* inKey, word32 inKeySz, byte* out);
|
---|
| 171 | WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
|
---|
| 172 | const byte* info, word32 infoSz,
|
---|
| 173 | byte* out, word32 outSz);
|
---|
| 174 |
|
---|
| 175 | WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
|
---|
| 176 | const byte* salt, word32 saltSz,
|
---|
| 177 | const byte* info, word32 infoSz,
|
---|
| 178 | byte* out, word32 outSz);
|
---|
| 179 |
|
---|
| 180 | #endif /* HAVE_HKDF */
|
---|
| 181 |
|
---|
| 182 | #ifdef __cplusplus
|
---|
| 183 | } /* extern "C" */
|
---|
| 184 | #endif
|
---|
| 185 |
|
---|
| 186 | #endif /* WOLF_CRYPT_HMAC_H */
|
---|
| 187 |
|
---|
| 188 | #endif /* NO_HMAC */
|
---|
| 189 |
|
---|