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