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