wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

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