wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 30 06:16:19 2017 +0000
Revision:
13:80fb167dafdf
wolfSSL 3.11.1: TLS1.3 Beta

Who changed what in which revision?

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