MultiTech / CyaSSL

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-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-2014 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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     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     MD5     = 0,
00071 #endif
00072 #ifdef NO_SHA
00073     SHA     = 1,
00074 #endif
00075 #ifdef NO_SHA256
00076     SHA256  = 2,
00077 #endif
00078 #ifndef CYASSL_SHA512
00079     SHA512  = 4,
00080 #endif
00081 #ifndef CYASSL_SHA384
00082     SHA384  = 5,
00083 #endif
00084 #ifndef HAVE_BLAKE2 
00085     BLAKE2B_ID = 7,
00086 #endif
00087 
00088 /* Select the largest available hash for the buffer size. */
00089 #if defined(CYASSL_SHA512)
00090     MAX_DIGEST_SIZE = SHA512_DIGEST_SIZE,
00091     HMAC_BLOCK_SIZE = SHA512_BLOCK_SIZE
00092 #elif defined(HAVE_BLAKE2)
00093     MAX_DIGEST_SIZE = BLAKE2B_OUTBYTES,
00094     HMAC_BLOCK_SIZE = BLAKE2B_BLOCKBYTES,
00095 #elif defined(CYASSL_SHA384)
00096     MAX_DIGEST_SIZE = SHA384_DIGEST_SIZE,
00097     HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE
00098 #elif !defined(NO_SHA256)
00099     MAX_DIGEST_SIZE = SHA256_DIGEST_SIZE,
00100     HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE
00101 #elif !defined(NO_SHA)
00102     MAX_DIGEST_SIZE = SHA_DIGEST_SIZE,
00103     HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE
00104 #elif !defined(NO_MD5)
00105     MAX_DIGEST_SIZE = MD5_DIGEST_SIZE,
00106     HMAC_BLOCK_SIZE = MD5_BLOCK_SIZE
00107 #else
00108     #error "You have to have some kind of hash if you want to use HMAC."
00109 #endif
00110 };
00111 
00112 
00113 /* hash union */
00114 typedef union {
00115     #ifndef NO_MD5
00116         Md5 md5;
00117     #endif
00118     #ifndef NO_SHA
00119         Sha sha;
00120     #endif
00121     #ifndef NO_SHA256
00122         Sha256 sha256;
00123     #endif
00124     #ifdef CYASSL_SHA384
00125         Sha384 sha384;
00126     #endif
00127     #ifdef CYASSL_SHA512
00128         Sha512 sha512;
00129     #endif
00130     #ifdef HAVE_BLAKE2 
00131         Blake2b blake2b;
00132     #endif
00133 } Hash;
00134 
00135 /* Hmac digest */
00136 typedef struct Hmac {
00137     Hash    hash;
00138     word32  ipad[HMAC_BLOCK_SIZE  / sizeof(word32)];  /* same block size all*/
00139     word32  opad[HMAC_BLOCK_SIZE  / sizeof(word32)];
00140     word32  innerHash[MAX_DIGEST_SIZE / sizeof(word32)];
00141     byte    macType;                                     /* md5 sha or sha256 */
00142     byte    innerHashKeyed;                              /* keyed flag */
00143 #ifdef HAVE_CAVIUM
00144     word16   keyLen;          /* hmac key length */
00145     word16   dataLen;
00146     HashType type;            /* hmac key type */
00147     int      devId;           /* nitrox device id */
00148     word32   magic;           /* using cavium magic */
00149     word64   contextHandle;   /* nitrox context memory handle */
00150     byte*    data;            /* buffered input data for one call */
00151 #endif
00152 } Hmac;
00153 
00154 
00155 /* does init */
00156 CYASSL_API int HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
00157 CYASSL_API int HmacUpdate(Hmac*, const byte*, word32);
00158 CYASSL_API int HmacFinal(Hmac*, byte*);
00159 
00160 #ifdef HAVE_CAVIUM
00161     CYASSL_API int  HmacInitCavium(Hmac*, int);
00162     CYASSL_API void HmacFreeCavium(Hmac*);
00163 #endif
00164 
00165 CYASSL_API int CyaSSL_GetHmacMaxSize(void);
00166 
00167 
00168 #ifdef HAVE_HKDF
00169 
00170 CYASSL_API int HKDF(int type, const byte* inKey, word32 inKeySz,
00171                     const byte* salt, word32 saltSz,
00172                     const byte* info, word32 infoSz,
00173                     byte* out, word32 outSz);
00174 
00175 #endif /* HAVE_HKDF */
00176 
00177 
00178 #ifdef HAVE_FIPS
00179     /* fips wrapper calls, user can call direct */
00180     CYASSL_API int HmacSetKey_fips(Hmac*, int type, const byte* key,
00181                                    word32 keySz);
00182     CYASSL_API int HmacUpdate_fips(Hmac*, const byte*, word32);
00183     CYASSL_API int HmacFinal_fips(Hmac*, byte*);
00184     #ifndef FIPS_NO_WRAPPERS
00185         /* if not impl or fips.c impl wrapper force fips calls if fips build */
00186         #define HmacSetKey HmacSetKey_fips
00187         #define HmacUpdate HmacUpdate_fips
00188         #define HmacFinal  HmacFinal_fips
00189     #endif /* FIPS_NO_WRAPPERS */
00190 
00191 #endif /* HAVE_FIPS */
00192 
00193 
00194 #ifdef __cplusplus
00195     } /* extern "C" */
00196 #endif
00197 
00198 #endif /* CTAO_CRYPT_HMAC_H */
00199 
00200 #endif /* NO_HMAC */
00201