wolf SSL / CyaSSL-2.9.4

Dependents:  

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-2013 wolfSSL Inc.
00004  *
00005  * This file is part of CyaSSL.
00006  *
00007  * CyaSSL 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  * CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00020  */
00021 
00022 
00023 #ifndef NO_HMAC
00024 
00025 #ifndef CTAO_CRYPT_HMAC_H
00026 #define CTAO_CRYPT_HMAC_H
00027 
00028 #include <cyassl/ctaocrypt/types.h>
00029 
00030 #ifndef NO_MD5
00031     #include <cyassl/ctaocrypt/md5.h>
00032 #endif
00033 
00034 #ifndef NO_SHA
00035     #include <cyassl/ctaocrypt/sha.h>
00036 #endif
00037 
00038 #ifndef NO_SHA256
00039     #include <cyassl/ctaocrypt/sha256.h>
00040 #endif
00041 
00042 #ifdef CYASSL_SHA512
00043     #include <cyassl/ctaocrypt/sha512.h>
00044 #endif
00045 
00046 #ifdef HAVE_BLAKE2 
00047     #include <cyassl/ctaocrypt/blake2.h>
00048 #endif
00049 
00050 #ifdef HAVE_CAVIUM
00051     #include <cyassl/ctaocrypt/logging.h>
00052     #include "cavium_common.h"
00053 #endif
00054 
00055 #ifdef __cplusplus
00056     extern "C" {
00057 #endif
00058 
00059 
00060 #define CYASSL_HMAC_CAVIUM_MAGIC 0xBEEF0005
00061 
00062 enum {
00063     IPAD    = 0x36,
00064     OPAD    = 0x5C,
00065 
00066 /* If any hash is not enabled, add the ID here. */
00067 #ifdef NO_MD5
00068     MD5     = 0,
00069 #endif
00070 #ifdef NO_SHA
00071     SHA     = 1,
00072 #endif
00073 #ifdef NO_SHA256
00074     SHA256  = 2,
00075 #endif
00076 #ifndef CYASSL_SHA512
00077     SHA512  = 4,
00078 #endif
00079 #ifndef CYASSL_SHA384
00080     SHA384  = 5,
00081 #endif
00082 #ifndef HAVE_BLAKE2 
00083     BLAKE2B_ID = 7,
00084 #endif
00085 
00086 /* Select the largest available hash for the buffer size. */
00087 #if defined(CYASSL_SHA512)
00088     MAX_DIGEST_SIZE = SHA512_DIGEST_SIZE,
00089     HMAC_BLOCK_SIZE = SHA512_BLOCK_SIZE
00090 #elif defined(HAVE_BLAKE2)
00091     MAX_DIGEST_SIZE = BLAKE2B_OUTBYTES,
00092     HMAC_BLOCK_SIZE = BLAKE2B_BLOCKBYTES,
00093 #elif defined(CYASSL_SHA384)
00094     MAX_DIGEST_SIZE = SHA384_DIGEST_SIZE,
00095     HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE
00096 #elif !defined(NO_SHA256)
00097     MAX_DIGEST_SIZE = SHA256_DIGEST_SIZE,
00098     HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE
00099 #elif !defined(NO_SHA)
00100     MAX_DIGEST_SIZE = SHA_DIGEST_SIZE,
00101     HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE
00102 #elif !defined(NO_MD5)
00103     MAX_DIGEST_SIZE = MD5_DIGEST_SIZE,
00104     HMAC_BLOCK_SIZE = MD5_BLOCK_SIZE
00105 #else
00106     #error "You have to have some kind of hash if you want to use HMAC."
00107 #endif
00108 };
00109 
00110 
00111 /* hash union */
00112 typedef union {
00113     #ifndef NO_MD5
00114         Md5 md5;
00115     #endif
00116     #ifndef NO_SHA
00117         Sha sha;
00118     #endif
00119     #ifndef NO_SHA256
00120         Sha256 sha256;
00121     #endif
00122     #ifdef CYASSL_SHA384
00123         Sha384 sha384;
00124     #endif
00125     #ifdef CYASSL_SHA512
00126         Sha512 sha512;
00127     #endif
00128     #ifdef HAVE_BLAKE2 
00129         Blake2b blake2b;
00130     #endif
00131 } Hash;
00132 
00133 /* Hmac digest */
00134 typedef struct Hmac {
00135     Hash    hash;
00136     word32  ipad[HMAC_BLOCK_SIZE  / sizeof(word32)];  /* same block size all*/
00137     word32  opad[HMAC_BLOCK_SIZE  / sizeof(word32)];
00138     word32  innerHash[MAX_DIGEST_SIZE / sizeof(word32)];
00139     byte    macType;                                     /* md5 sha or sha256 */
00140     byte    innerHashKeyed;                              /* keyed flag */
00141 #ifdef HAVE_CAVIUM
00142     word16   keyLen;          /* hmac key length */
00143     word16   dataLen;
00144     HashType type;            /* hmac key type */
00145     int      devId;           /* nitrox device id */
00146     word32   magic;           /* using cavium magic */
00147     word64   contextHandle;   /* nitrox context memory handle */
00148     byte*    data;            /* buffered input data for one call */
00149 #endif
00150 } Hmac;
00151 
00152 
00153 /* does init */
00154 CYASSL_API int HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
00155 CYASSL_API int HmacUpdate(Hmac*, const byte*, word32);
00156 CYASSL_API int HmacFinal(Hmac*, byte*);
00157 
00158 #ifdef HAVE_CAVIUM
00159     CYASSL_API int  HmacInitCavium(Hmac*, int);
00160     CYASSL_API void HmacFreeCavium(Hmac*);
00161 #endif
00162 
00163 CYASSL_API int CyaSSL_GetHmacMaxSize(void);
00164 
00165 
00166 #ifdef HAVE_HKDF
00167 
00168 CYASSL_API int HKDF(int type, const byte* inKey, word32 inKeySz,
00169                     const byte* salt, word32 saltSz,
00170                     const byte* info, word32 infoSz,
00171                     byte* out, word32 outSz);
00172 
00173 #endif /* HAVE_HKDF */
00174 
00175 
00176 #ifdef HAVE_FIPS
00177     /* fips wrapper calls, user can call direct */
00178     CYASSL_API int HmacSetKey_fips(Hmac*, int type, const byte* key,
00179                                    word32 keySz);
00180     CYASSL_API int HmacUpdate_fips(Hmac*, const byte*, word32);
00181     CYASSL_API int HmacFinal_fips(Hmac*, byte*);
00182     #ifndef FIPS_NO_WRAPPERS
00183         /* if not impl or fips.c impl wrapper force fips calls if fips build */
00184         #define HmacSetKey HmacSetKey_fips
00185         #define HmacUpdate HmacUpdate_fips
00186         #define HmacFinal  HmacFinal_fips
00187     #endif /* FIPS_NO_WRAPPERS */
00188 
00189 #endif /* HAVE_FIPS */
00190 
00191 
00192 #ifdef __cplusplus
00193     } /* extern "C" */
00194 #endif
00195 
00196 #endif /* CTAO_CRYPT_HMAC_H */
00197 
00198 #endif /* NO_HMAC */
00199