Xuyi Wang / wolfcrypt

Dependents:   OS

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-2017 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     \file wolfssl/wolfcrypt/hmac.h
00024 */
00025 
00026 #ifndef NO_HMAC
00027 
00028 #ifndef WOLF_CRYPT_HMAC_H
00029 #define WOLF_CRYPT_HMAC_H
00030 
00031 #include <wolfcrypt/hash.h>
00032 
00033 #if defined(HAVE_FIPS) && \
00034     (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
00035 /* for fips @wc_fips */
00036     #include <cyassl/ctaocrypt/hmac.h>
00037     #define WC_HMAC_BLOCK_SIZE HMAC_BLOCK_SIZE
00038 #endif
00039 
00040 
00041 #if defined(HAVE_FIPS) && \
00042     defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
00043     #include <wolfcrypt/fips.h>
00044 #endif
00045 
00046 #ifdef __cplusplus
00047     extern "C" {
00048 #endif
00049 
00050 /* avoid redefinition of structs */
00051 #if !defined(HAVE_FIPS) || \
00052     (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
00053 
00054 #ifdef WOLFSSL_ASYNC_CRYPT
00055     #include <wolfcrypt/async.h>
00056 #endif
00057 
00058 #ifndef NO_OLD_WC_NAMES
00059     #define HMAC_BLOCK_SIZE WC_HMAC_BLOCK_SIZE
00060 #endif
00061 
00062 enum {
00063     HMAC_FIPS_MIN_KEY = 14,   /* 112 bit key length minimum */
00064 
00065     IPAD    = 0x36,
00066     OPAD    = 0x5C,
00067 
00068 /* If any hash is not enabled, add the ID here. */
00069 #ifdef NO_MD5
00070     WC_MD5     = WC_HASH_TYPE_MD5,
00071 #endif
00072 #ifdef NO_SHA
00073     WC_SHA     = WC_HASH_TYPE_SHA,
00074 #endif
00075 #ifdef NO_SHA256
00076     WC_SHA256  = WC_HASH_TYPE_SHA256,
00077 #endif
00078 #ifndef WOLFSSL_SHA512
00079     WC_SHA512  = WC_HASH_TYPE_SHA512,
00080 #endif
00081 #ifndef WOLFSSL_SHA384
00082     WC_SHA384  = WC_HASH_TYPE_SHA384,
00083 #endif
00084 #ifndef HAVE_BLAKE2
00085     BLAKE2B_ID = WC_HASH_TYPE_BLAKE2B,
00086 #endif
00087 #ifndef WOLFSSL_SHA224
00088     WC_SHA224  = WC_HASH_TYPE_SHA224,
00089 #endif
00090 #ifndef WOLFSSL_SHA3
00091     WC_SHA3_224 = WC_HASH_TYPE_SHA3_224,
00092     WC_SHA3_256 = WC_HASH_TYPE_SHA3_256,
00093     WC_SHA3_384 = WC_HASH_TYPE_SHA3_384,
00094     WC_SHA3_512 = WC_HASH_TYPE_SHA3_512,
00095 #endif
00096 };
00097 
00098 /* Select the largest available hash for the buffer size. */
00099 #define WC_HMAC_BLOCK_SIZE WC_MAX_BLOCK_SIZE
00100 
00101 #if !defined(WOLFSSL_SHA3) && !defined(WOLFSSL_SHA512) && !defined(HAVE_BLAKE2) && \
00102     !defined(WOLFSSL_SHA384) && defined(NO_SHA256) && defined(WOLFSSL_SHA224) && \
00103      defined(NO_SHA) && defined(NO_MD5)
00104     #error "You have to have some kind of hash if you want to use HMAC."
00105 #endif
00106 
00107 
00108 /* hash union */
00109 typedef union {
00110 #ifndef NO_MD5
00111     wc_Md5 md5;
00112 #endif
00113 #ifndef NO_SHA
00114     wc_Sha sha;
00115 #endif
00116 #ifdef WOLFSSL_SHA224
00117     wc_Sha224 sha224;
00118 #endif
00119 #ifndef NO_SHA256
00120     wc_Sha256 sha256;
00121 #endif
00122 #ifdef WOLFSSL_SHA384
00123     wc_Sha384 sha384;
00124 #endif
00125 #ifdef WOLFSSL_SHA512
00126     wc_Sha512 sha512;
00127 #endif
00128 #ifdef HAVE_BLAKE2
00129     Blake2b blake2b;
00130 #endif
00131 #ifdef WOLFSSL_SHA3
00132     wc_Sha3 sha3;
00133 #endif
00134 } Hash;
00135 
00136 /* Hmac digest */
00137 typedef struct Hmac {
00138     Hash    hash;
00139     word32  ipad[WC_HMAC_BLOCK_SIZE  / sizeof(word32)];  /* same block size all*/
00140     word32  opad[WC_HMAC_BLOCK_SIZE  / sizeof(word32)];
00141     word32  innerHash[WC_MAX_DIGEST_SIZE / sizeof(word32)];
00142     void*   heap;                 /* heap hint */
00143     byte    macType;              /* md5 sha or sha256 */
00144     byte    innerHashKeyed;       /* keyed flag */
00145 
00146 #ifdef WOLFSSL_ASYNC_CRYPT
00147     WC_ASYNC_DEV asyncDev;
00148     word16       keyLen;          /* hmac key length (key in ipad) */
00149 #endif /* WOLFSSL_ASYNC_CRYPT */
00150 } Hmac;
00151 
00152 #endif /* HAVE_FIPS */
00153 
00154 /* does init */
00155 WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
00156 WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32);
00157 WOLFSSL_API int wc_HmacFinal(Hmac*, byte*);
00158 WOLFSSL_API int wc_HmacSizeByType(int type);
00159 
00160 WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId);
00161 WOLFSSL_API void wc_HmacFree(Hmac*);
00162 
00163 WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
00164 
00165 WOLFSSL_LOCAL int _InitHmac(Hmac* hmac, int type, void* heap);
00166 
00167 #ifdef HAVE_HKDF
00168 
00169 WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
00170                                 const byte* inKey, word32 inKeySz, byte* out);
00171 WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
00172                                const byte* info, word32 infoSz,
00173                                byte* out,        word32 outSz);
00174 
00175 WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
00176                     const byte* salt, word32 saltSz,
00177                     const byte* info, word32 infoSz,
00178                     byte* out, word32 outSz);
00179 
00180 #endif /* HAVE_HKDF */
00181 
00182 #ifdef __cplusplus
00183     } /* extern "C" */
00184 #endif
00185 
00186 #endif /* WOLF_CRYPT_HMAC_H */
00187 
00188 #endif /* NO_HMAC */
00189 
00190