wolf SSL / wolfSSL-TLS13-Beta

Fork of wolfSSL by wolf SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hmac.h Source File

hmac.h

00001 /* hmac.h
00002  *
00003  * Copyright (C) 2006-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 
00024 #ifndef NO_HMAC
00025 
00026 #ifndef WOLF_CRYPT_HMAC_H
00027 #define WOLF_CRYPT_HMAC_H
00028 
00029 #include <wolfssl/wolfcrypt/types.h>
00030 
00031 #ifndef NO_MD5
00032     #include <wolfssl/wolfcrypt/md5.h>
00033 #endif
00034 
00035 #ifndef NO_SHA
00036     #include <wolfssl/wolfcrypt/sha.h>
00037 #endif
00038 
00039 #if !defined(NO_SHA256) || defined(WOLFSSL_SHA224)
00040     #include <wolfssl/wolfcrypt/sha256.h>
00041 #endif
00042 
00043 #ifdef WOLFSSL_SHA512
00044     #include <wolfssl/wolfcrypt/sha512.h>
00045 #endif
00046 
00047 #ifdef HAVE_BLAKE2
00048     #include <wolfssl/wolfcrypt/blake2.h>
00049 #endif
00050 
00051 #ifdef HAVE_FIPS
00052 /* for fips */
00053     #include <cyassl/ctaocrypt/hmac.h>
00054 #endif
00055 
00056 
00057 #ifdef __cplusplus
00058     extern "C" {
00059 #endif
00060 #ifndef HAVE_FIPS
00061 
00062 #ifdef WOLFSSL_ASYNC_CRYPT
00063     #include <wolfssl/wolfcrypt/async.h>
00064 #endif
00065 
00066 enum {
00067     HMAC_FIPS_MIN_KEY = 14,   /* 112 bit key length minimum */
00068 
00069     IPAD    = 0x36,
00070     OPAD    = 0x5C,
00071 
00072 /* If any hash is not enabled, add the ID here. */
00073 #ifdef NO_MD5
00074     MD5     = 0,
00075 #endif
00076 #ifdef NO_SHA
00077     SHA     = 1,
00078 #endif
00079 #ifdef NO_SHA256
00080     SHA256  = 2,
00081 #endif
00082 #ifndef WOLFSSL_SHA512
00083     SHA512  = 4,
00084 #endif
00085 #ifndef WOLFSSL_SHA384
00086     SHA384  = 5,
00087 #endif
00088 #ifndef HAVE_BLAKE2
00089     BLAKE2B_ID = 7,
00090 #endif
00091 #ifndef WOLFSSL_SHA224
00092     SHA224  = 8,
00093 #endif
00094 
00095 /* Select the largest available hash for the buffer size. */
00096 #if defined(WOLFSSL_SHA512)
00097     MAX_DIGEST_SIZE = SHA512_DIGEST_SIZE,
00098     HMAC_BLOCK_SIZE = SHA512_BLOCK_SIZE,
00099 #elif defined(HAVE_BLAKE2)
00100     MAX_DIGEST_SIZE = BLAKE2B_OUTBYTES,
00101     HMAC_BLOCK_SIZE = BLAKE2B_BLOCKBYTES,
00102 #elif defined(WOLFSSL_SHA384)
00103     MAX_DIGEST_SIZE = SHA384_DIGEST_SIZE,
00104     HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE
00105 #elif !defined(NO_SHA256)
00106     MAX_DIGEST_SIZE = SHA256_DIGEST_SIZE,
00107     HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE
00108 #elif defined(WOLFSSL_SHA224)
00109     MAX_DIGEST_SIZE = SHA224_DIGEST_SIZE,
00110     HMAC_BLOCK_SIZE = SHA224_BLOCK_SIZE
00111 #elif !defined(NO_SHA)
00112     MAX_DIGEST_SIZE = SHA_DIGEST_SIZE,
00113     HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE,
00114 #elif !defined(NO_MD5)
00115     MAX_DIGEST_SIZE = MD5_DIGEST_SIZE,
00116     HMAC_BLOCK_SIZE = MD5_BLOCK_SIZE,
00117 #else
00118     #error "You have to have some kind of hash if you want to use HMAC."
00119 #endif
00120 };
00121 
00122 
00123 /* hash union */
00124 typedef union {
00125 #ifndef NO_MD5
00126     Md5 md5;
00127 #endif
00128 #ifndef NO_SHA
00129     Sha sha;
00130 #endif
00131 #ifdef WOLFSSL_SHA224
00132     Sha224 sha224;
00133 #endif
00134 #ifndef NO_SHA256
00135     Sha256 sha256;
00136 #endif
00137 #ifdef WOLFSSL_SHA512
00138 #ifdef WOLFSSL_SHA384
00139     Sha384 sha384;
00140 #endif
00141     Sha512 sha512;
00142 #endif
00143 #ifdef HAVE_BLAKE2
00144     Blake2b blake2b;
00145 #endif
00146 } Hash;
00147 
00148 /* Hmac digest */
00149 typedef struct Hmac {
00150     Hash    hash;
00151     word32  ipad[HMAC_BLOCK_SIZE  / sizeof(word32)];  /* same block size all*/
00152     word32  opad[HMAC_BLOCK_SIZE  / sizeof(word32)];
00153     word32  innerHash[MAX_DIGEST_SIZE / sizeof(word32)];
00154     void*   heap;                 /* heap hint */
00155     byte    macType;              /* md5 sha or sha256 */
00156     byte    innerHashKeyed;       /* keyed flag */
00157 
00158 #ifdef WOLFSSL_ASYNC_CRYPT
00159     WC_ASYNC_DEV asyncDev;
00160     byte         keyRaw[HMAC_BLOCK_SIZE];
00161     word16       keyLen;          /* hmac key length */
00162     #ifdef HAVE_CAVIUM
00163         byte*    data;            /* buffered input data for one call */
00164         word16   dataLen;
00165     #endif /* HAVE_CAVIUM */
00166 #endif /* WOLFSSL_ASYNC_CRYPT */
00167 } Hmac;
00168 
00169 #endif /* HAVE_FIPS */
00170 
00171 /* does init */
00172 WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
00173 WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32);
00174 WOLFSSL_API int wc_HmacFinal(Hmac*, byte*);
00175 WOLFSSL_API int wc_HmacSizeByType(int type);
00176 
00177 WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId);
00178 WOLFSSL_API void wc_HmacFree(Hmac*);
00179 
00180 WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
00181 
00182 #ifdef HAVE_HKDF
00183 
00184 WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
00185                                 const byte* inKey, word32 inKeySz, byte* out);
00186 WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
00187                                const byte* info, word32 infoSz,
00188                                byte* out,        word32 outSz);
00189 
00190 WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
00191                     const byte* salt, word32 saltSz,
00192                     const byte* info, word32 infoSz,
00193                     byte* out, word32 outSz);
00194 
00195 #endif /* HAVE_HKDF */
00196 
00197 #ifdef __cplusplus
00198     } /* extern "C" */
00199 #endif
00200 
00201 #endif /* WOLF_CRYPT_HMAC_H */
00202 
00203 #endif /* NO_HMAC */
00204 
00205