wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 30 01:44:10 2017 +0000
Revision:
11:cee25a834751
wolfSSL 3.11.0

Who changed what in which revision?

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