wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

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