wolfSSL 3.11.1 for TLS1.3 beta
Fork of wolfSSL by
wolfssl/wolfcrypt/hmac.h@4:1b0d80432c79, 2016-04-28 (annotated)
- Committer:
- wolfSSL
- Date:
- Thu Apr 28 00:57:21 2016 +0000
- Revision:
- 4:1b0d80432c79
wolfSSL 3.9.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 4:1b0d80432c79 | 1 | /* hmac.h |
wolfSSL | 4:1b0d80432c79 | 2 | * |
wolfSSL | 4:1b0d80432c79 | 3 | * Copyright (C) 2006-2016 wolfSSL Inc. |
wolfSSL | 4:1b0d80432c79 | 4 | * |
wolfSSL | 4:1b0d80432c79 | 5 | * This file is part of wolfSSL. |
wolfSSL | 4:1b0d80432c79 | 6 | * |
wolfSSL | 4:1b0d80432c79 | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 4:1b0d80432c79 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 4:1b0d80432c79 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 4:1b0d80432c79 | 10 | * (at your option) any later version. |
wolfSSL | 4:1b0d80432c79 | 11 | * |
wolfSSL | 4:1b0d80432c79 | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 4:1b0d80432c79 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 4:1b0d80432c79 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 4:1b0d80432c79 | 15 | * GNU General Public License for more details. |
wolfSSL | 4:1b0d80432c79 | 16 | * |
wolfSSL | 4:1b0d80432c79 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 4:1b0d80432c79 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 4:1b0d80432c79 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 4:1b0d80432c79 | 20 | */ |
wolfSSL | 4:1b0d80432c79 | 21 | |
wolfSSL | 4:1b0d80432c79 | 22 | |
wolfSSL | 4:1b0d80432c79 | 23 | |
wolfSSL | 4:1b0d80432c79 | 24 | #ifndef NO_HMAC |
wolfSSL | 4:1b0d80432c79 | 25 | |
wolfSSL | 4:1b0d80432c79 | 26 | #ifndef WOLF_CRYPT_HMAC_H |
wolfSSL | 4:1b0d80432c79 | 27 | #define WOLF_CRYPT_HMAC_H |
wolfSSL | 4:1b0d80432c79 | 28 | |
wolfSSL | 4:1b0d80432c79 | 29 | #include <wolfssl/wolfcrypt/types.h> |
wolfSSL | 4:1b0d80432c79 | 30 | |
wolfSSL | 4:1b0d80432c79 | 31 | #ifndef NO_MD5 |
wolfSSL | 4:1b0d80432c79 | 32 | #include <wolfssl/wolfcrypt/md5.h> |
wolfSSL | 4:1b0d80432c79 | 33 | #endif |
wolfSSL | 4:1b0d80432c79 | 34 | |
wolfSSL | 4:1b0d80432c79 | 35 | #ifndef NO_SHA |
wolfSSL | 4:1b0d80432c79 | 36 | #include <wolfssl/wolfcrypt/sha.h> |
wolfSSL | 4:1b0d80432c79 | 37 | #endif |
wolfSSL | 4:1b0d80432c79 | 38 | |
wolfSSL | 4:1b0d80432c79 | 39 | #ifndef NO_SHA256 |
wolfSSL | 4:1b0d80432c79 | 40 | #include <wolfssl/wolfcrypt/sha256.h> |
wolfSSL | 4:1b0d80432c79 | 41 | #endif |
wolfSSL | 4:1b0d80432c79 | 42 | |
wolfSSL | 4:1b0d80432c79 | 43 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 4:1b0d80432c79 | 44 | #include <wolfssl/wolfcrypt/sha512.h> |
wolfSSL | 4:1b0d80432c79 | 45 | #endif |
wolfSSL | 4:1b0d80432c79 | 46 | |
wolfSSL | 4:1b0d80432c79 | 47 | #ifdef HAVE_BLAKE2 |
wolfSSL | 4:1b0d80432c79 | 48 | #include <wolfssl/wolfcrypt/blake2.h> |
wolfSSL | 4:1b0d80432c79 | 49 | #endif |
wolfSSL | 4:1b0d80432c79 | 50 | |
wolfSSL | 4:1b0d80432c79 | 51 | #ifdef HAVE_FIPS |
wolfSSL | 4:1b0d80432c79 | 52 | /* for fips */ |
wolfSSL | 4:1b0d80432c79 | 53 | #include <cyassl/ctaocrypt/hmac.h> |
wolfSSL | 4:1b0d80432c79 | 54 | #endif |
wolfSSL | 4:1b0d80432c79 | 55 | |
wolfSSL | 4:1b0d80432c79 | 56 | #ifdef HAVE_CAVIUM |
wolfSSL | 4:1b0d80432c79 | 57 | #include <wolfssl/wolfcrypt/logging.h> |
wolfSSL | 4:1b0d80432c79 | 58 | #include "cavium_common.h" |
wolfSSL | 4:1b0d80432c79 | 59 | #endif |
wolfSSL | 4:1b0d80432c79 | 60 | |
wolfSSL | 4:1b0d80432c79 | 61 | |
wolfSSL | 4:1b0d80432c79 | 62 | #ifdef __cplusplus |
wolfSSL | 4:1b0d80432c79 | 63 | extern "C" { |
wolfSSL | 4:1b0d80432c79 | 64 | #endif |
wolfSSL | 4:1b0d80432c79 | 65 | #ifndef HAVE_FIPS |
wolfSSL | 4:1b0d80432c79 | 66 | #define WOLFSSL_HMAC_CAVIUM_MAGIC 0xBEEF0005 |
wolfSSL | 4:1b0d80432c79 | 67 | |
wolfSSL | 4:1b0d80432c79 | 68 | enum { |
wolfSSL | 4:1b0d80432c79 | 69 | HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */ |
wolfSSL | 4:1b0d80432c79 | 70 | |
wolfSSL | 4:1b0d80432c79 | 71 | IPAD = 0x36, |
wolfSSL | 4:1b0d80432c79 | 72 | OPAD = 0x5C, |
wolfSSL | 4:1b0d80432c79 | 73 | |
wolfSSL | 4:1b0d80432c79 | 74 | /* If any hash is not enabled, add the ID here. */ |
wolfSSL | 4:1b0d80432c79 | 75 | #ifdef NO_MD5 |
wolfSSL | 4:1b0d80432c79 | 76 | MD5 = 0, |
wolfSSL | 4:1b0d80432c79 | 77 | #endif |
wolfSSL | 4:1b0d80432c79 | 78 | #ifdef NO_SHA |
wolfSSL | 4:1b0d80432c79 | 79 | SHA = 1, |
wolfSSL | 4:1b0d80432c79 | 80 | #endif |
wolfSSL | 4:1b0d80432c79 | 81 | #ifdef NO_SHA256 |
wolfSSL | 4:1b0d80432c79 | 82 | SHA256 = 2, |
wolfSSL | 4:1b0d80432c79 | 83 | #endif |
wolfSSL | 4:1b0d80432c79 | 84 | #ifndef WOLFSSL_SHA512 |
wolfSSL | 4:1b0d80432c79 | 85 | SHA512 = 4, |
wolfSSL | 4:1b0d80432c79 | 86 | #endif |
wolfSSL | 4:1b0d80432c79 | 87 | #ifndef WOLFSSL_SHA384 |
wolfSSL | 4:1b0d80432c79 | 88 | SHA384 = 5, |
wolfSSL | 4:1b0d80432c79 | 89 | #endif |
wolfSSL | 4:1b0d80432c79 | 90 | #ifndef HAVE_BLAKE2 |
wolfSSL | 4:1b0d80432c79 | 91 | BLAKE2B_ID = 7, |
wolfSSL | 4:1b0d80432c79 | 92 | #endif |
wolfSSL | 4:1b0d80432c79 | 93 | |
wolfSSL | 4:1b0d80432c79 | 94 | /* Select the largest available hash for the buffer size. */ |
wolfSSL | 4:1b0d80432c79 | 95 | #if defined(WOLFSSL_SHA512) |
wolfSSL | 4:1b0d80432c79 | 96 | MAX_DIGEST_SIZE = SHA512_DIGEST_SIZE, |
wolfSSL | 4:1b0d80432c79 | 97 | HMAC_BLOCK_SIZE = SHA512_BLOCK_SIZE |
wolfSSL | 4:1b0d80432c79 | 98 | #elif defined(HAVE_BLAKE2) |
wolfSSL | 4:1b0d80432c79 | 99 | MAX_DIGEST_SIZE = BLAKE2B_OUTBYTES, |
wolfSSL | 4:1b0d80432c79 | 100 | HMAC_BLOCK_SIZE = BLAKE2B_BLOCKBYTES, |
wolfSSL | 4:1b0d80432c79 | 101 | #elif defined(WOLFSSL_SHA384) |
wolfSSL | 4:1b0d80432c79 | 102 | MAX_DIGEST_SIZE = SHA384_DIGEST_SIZE, |
wolfSSL | 4:1b0d80432c79 | 103 | HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE |
wolfSSL | 4:1b0d80432c79 | 104 | #elif !defined(NO_SHA256) |
wolfSSL | 4:1b0d80432c79 | 105 | MAX_DIGEST_SIZE = SHA256_DIGEST_SIZE, |
wolfSSL | 4:1b0d80432c79 | 106 | HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE |
wolfSSL | 4:1b0d80432c79 | 107 | #elif !defined(NO_SHA) |
wolfSSL | 4:1b0d80432c79 | 108 | MAX_DIGEST_SIZE = SHA_DIGEST_SIZE, |
wolfSSL | 4:1b0d80432c79 | 109 | HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE |
wolfSSL | 4:1b0d80432c79 | 110 | #elif !defined(NO_MD5) |
wolfSSL | 4:1b0d80432c79 | 111 | MAX_DIGEST_SIZE = MD5_DIGEST_SIZE, |
wolfSSL | 4:1b0d80432c79 | 112 | HMAC_BLOCK_SIZE = MD5_BLOCK_SIZE |
wolfSSL | 4:1b0d80432c79 | 113 | #else |
wolfSSL | 4:1b0d80432c79 | 114 | #error "You have to have some kind of hash if you want to use HMAC." |
wolfSSL | 4:1b0d80432c79 | 115 | #endif |
wolfSSL | 4:1b0d80432c79 | 116 | }; |
wolfSSL | 4:1b0d80432c79 | 117 | |
wolfSSL | 4:1b0d80432c79 | 118 | |
wolfSSL | 4:1b0d80432c79 | 119 | /* hash union */ |
wolfSSL | 4:1b0d80432c79 | 120 | typedef union { |
wolfSSL | 4:1b0d80432c79 | 121 | #ifndef NO_MD5 |
wolfSSL | 4:1b0d80432c79 | 122 | Md5 md5; |
wolfSSL | 4:1b0d80432c79 | 123 | #endif |
wolfSSL | 4:1b0d80432c79 | 124 | #ifndef NO_SHA |
wolfSSL | 4:1b0d80432c79 | 125 | Sha sha; |
wolfSSL | 4:1b0d80432c79 | 126 | #endif |
wolfSSL | 4:1b0d80432c79 | 127 | #ifndef NO_SHA256 |
wolfSSL | 4:1b0d80432c79 | 128 | Sha256 sha256; |
wolfSSL | 4:1b0d80432c79 | 129 | #endif |
wolfSSL | 4:1b0d80432c79 | 130 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 4:1b0d80432c79 | 131 | Sha384 sha384; |
wolfSSL | 4:1b0d80432c79 | 132 | #endif |
wolfSSL | 4:1b0d80432c79 | 133 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 4:1b0d80432c79 | 134 | Sha512 sha512; |
wolfSSL | 4:1b0d80432c79 | 135 | #endif |
wolfSSL | 4:1b0d80432c79 | 136 | #ifdef HAVE_BLAKE2 |
wolfSSL | 4:1b0d80432c79 | 137 | Blake2b blake2b; |
wolfSSL | 4:1b0d80432c79 | 138 | #endif |
wolfSSL | 4:1b0d80432c79 | 139 | } Hash; |
wolfSSL | 4:1b0d80432c79 | 140 | |
wolfSSL | 4:1b0d80432c79 | 141 | /* Hmac digest */ |
wolfSSL | 4:1b0d80432c79 | 142 | typedef struct Hmac { |
wolfSSL | 4:1b0d80432c79 | 143 | Hash hash; |
wolfSSL | 4:1b0d80432c79 | 144 | word32 ipad[HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/ |
wolfSSL | 4:1b0d80432c79 | 145 | word32 opad[HMAC_BLOCK_SIZE / sizeof(word32)]; |
wolfSSL | 4:1b0d80432c79 | 146 | word32 innerHash[MAX_DIGEST_SIZE / sizeof(word32)]; |
wolfSSL | 4:1b0d80432c79 | 147 | byte macType; /* md5 sha or sha256 */ |
wolfSSL | 4:1b0d80432c79 | 148 | byte innerHashKeyed; /* keyed flag */ |
wolfSSL | 4:1b0d80432c79 | 149 | #ifdef HAVE_CAVIUM |
wolfSSL | 4:1b0d80432c79 | 150 | word16 keyLen; /* hmac key length */ |
wolfSSL | 4:1b0d80432c79 | 151 | word16 dataLen; |
wolfSSL | 4:1b0d80432c79 | 152 | HashType type; /* hmac key type */ |
wolfSSL | 4:1b0d80432c79 | 153 | int devId; /* nitrox device id */ |
wolfSSL | 4:1b0d80432c79 | 154 | word32 magic; /* using cavium magic */ |
wolfSSL | 4:1b0d80432c79 | 155 | word64 contextHandle; /* nitrox context memory handle */ |
wolfSSL | 4:1b0d80432c79 | 156 | byte* data; /* buffered input data for one call */ |
wolfSSL | 4:1b0d80432c79 | 157 | #endif |
wolfSSL | 4:1b0d80432c79 | 158 | } Hmac; |
wolfSSL | 4:1b0d80432c79 | 159 | |
wolfSSL | 4:1b0d80432c79 | 160 | #endif /* HAVE_FIPS */ |
wolfSSL | 4:1b0d80432c79 | 161 | |
wolfSSL | 4:1b0d80432c79 | 162 | /* does init */ |
wolfSSL | 4:1b0d80432c79 | 163 | WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz); |
wolfSSL | 4:1b0d80432c79 | 164 | WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32); |
wolfSSL | 4:1b0d80432c79 | 165 | WOLFSSL_API int wc_HmacFinal(Hmac*, byte*); |
wolfSSL | 4:1b0d80432c79 | 166 | |
wolfSSL | 4:1b0d80432c79 | 167 | #ifdef HAVE_CAVIUM |
wolfSSL | 4:1b0d80432c79 | 168 | WOLFSSL_API int wc_HmacInitCavium(Hmac*, int); |
wolfSSL | 4:1b0d80432c79 | 169 | WOLFSSL_API void wc_HmacFreeCavium(Hmac*); |
wolfSSL | 4:1b0d80432c79 | 170 | #endif |
wolfSSL | 4:1b0d80432c79 | 171 | |
wolfSSL | 4:1b0d80432c79 | 172 | WOLFSSL_API int wolfSSL_GetHmacMaxSize(void); |
wolfSSL | 4:1b0d80432c79 | 173 | |
wolfSSL | 4:1b0d80432c79 | 174 | |
wolfSSL | 4:1b0d80432c79 | 175 | #ifdef HAVE_HKDF |
wolfSSL | 4:1b0d80432c79 | 176 | |
wolfSSL | 4:1b0d80432c79 | 177 | WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz, |
wolfSSL | 4:1b0d80432c79 | 178 | const byte* salt, word32 saltSz, |
wolfSSL | 4:1b0d80432c79 | 179 | const byte* info, word32 infoSz, |
wolfSSL | 4:1b0d80432c79 | 180 | byte* out, word32 outSz); |
wolfSSL | 4:1b0d80432c79 | 181 | |
wolfSSL | 4:1b0d80432c79 | 182 | #endif /* HAVE_HKDF */ |
wolfSSL | 4:1b0d80432c79 | 183 | |
wolfSSL | 4:1b0d80432c79 | 184 | #ifdef __cplusplus |
wolfSSL | 4:1b0d80432c79 | 185 | } /* extern "C" */ |
wolfSSL | 4:1b0d80432c79 | 186 | #endif |
wolfSSL | 4:1b0d80432c79 | 187 | |
wolfSSL | 4:1b0d80432c79 | 188 | #endif /* WOLF_CRYPT_HMAC_H */ |
wolfSSL | 4:1b0d80432c79 | 189 | |
wolfSSL | 4:1b0d80432c79 | 190 | #endif /* NO_HMAC */ |
wolfSSL | 4:1b0d80432c79 | 191 | |
wolfSSL | 4:1b0d80432c79 | 192 |