Renesas / SecureDweet
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 #ifndef NO_SHA256
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 #ifdef HAVE_CAVIUM
00057     #include <wolfssl/wolfcrypt/logging.h>
00058     #include "cavium_common.h"
00059 #endif
00060 
00061 
00062 #ifdef __cplusplus
00063     extern "C" {
00064 #endif
00065 #ifndef HAVE_FIPS
00066 #define WOLFSSL_HMAC_CAVIUM_MAGIC 0xBEEF0005
00067 
00068 enum {
00069     HMAC_FIPS_MIN_KEY = 14,   /* 112 bit key length minimum */
00070 
00071     IPAD    = 0x36,
00072     OPAD    = 0x5C,
00073 
00074 /* If any hash is not enabled, add the ID here. */
00075 #ifdef NO_MD5
00076     MD5     = 0,
00077 #endif
00078 #ifdef NO_SHA
00079     SHA     = 1,
00080 #endif
00081 #ifdef NO_SHA256
00082     SHA256  = 2,
00083 #endif
00084 #ifndef WOLFSSL_SHA512
00085     SHA512  = 4,
00086 #endif
00087 #ifndef WOLFSSL_SHA384
00088     SHA384  = 5,
00089 #endif
00090 #ifndef HAVE_BLAKE2
00091     BLAKE2B_ID = 7,
00092 #endif
00093 
00094 /* Select the largest available hash for the buffer size. */
00095 #if defined(WOLFSSL_SHA512)
00096     MAX_DIGEST_SIZE = SHA512_DIGEST_SIZE,
00097     HMAC_BLOCK_SIZE = SHA512_BLOCK_SIZE
00098 #elif defined(HAVE_BLAKE2)
00099     MAX_DIGEST_SIZE = BLAKE2B_OUTBYTES,
00100     HMAC_BLOCK_SIZE = BLAKE2B_BLOCKBYTES,
00101 #elif defined(WOLFSSL_SHA384)
00102     MAX_DIGEST_SIZE = SHA384_DIGEST_SIZE,
00103     HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE
00104 #elif !defined(NO_SHA256)
00105     MAX_DIGEST_SIZE = SHA256_DIGEST_SIZE,
00106     HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE
00107 #elif !defined(NO_SHA)
00108     MAX_DIGEST_SIZE = SHA_DIGEST_SIZE,
00109     HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE
00110 #elif !defined(NO_MD5)
00111     MAX_DIGEST_SIZE = MD5_DIGEST_SIZE,
00112     HMAC_BLOCK_SIZE = MD5_BLOCK_SIZE
00113 #else
00114     #error "You have to have some kind of hash if you want to use HMAC."
00115 #endif
00116 };
00117 
00118 
00119 /* hash union */
00120 typedef union {
00121     #ifndef NO_MD5
00122         Md5 md5;
00123     #endif
00124     #ifndef NO_SHA
00125         Sha sha;
00126     #endif
00127     #ifndef NO_SHA256
00128         Sha256 sha256;
00129     #endif
00130     #ifdef WOLFSSL_SHA384
00131         Sha384 sha384;
00132     #endif
00133     #ifdef WOLFSSL_SHA512
00134         Sha512 sha512;
00135     #endif
00136     #ifdef HAVE_BLAKE2
00137         Blake2b blake2b;
00138     #endif
00139 } Hash;
00140 
00141 /* Hmac digest */
00142 typedef struct Hmac {
00143     Hash    hash;
00144     word32  ipad[HMAC_BLOCK_SIZE  / sizeof(word32)];  /* same block size all*/
00145     word32  opad[HMAC_BLOCK_SIZE  / sizeof(word32)];
00146     word32  innerHash[MAX_DIGEST_SIZE / sizeof(word32)];
00147     byte    macType;                                     /* md5 sha or sha256 */
00148     byte    innerHashKeyed;                              /* keyed flag */
00149 #ifdef HAVE_CAVIUM
00150     word16   keyLen;          /* hmac key length */
00151     word16   dataLen;
00152     HashType type;            /* hmac key type */
00153     int      devId;           /* nitrox device id */
00154     word32   magic;           /* using cavium magic */
00155     word64   contextHandle;   /* nitrox context memory handle */
00156     byte*    data;            /* buffered input data for one call */
00157 #endif
00158 } Hmac;
00159 
00160 #endif /* HAVE_FIPS */
00161 
00162 /* does init */
00163 WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
00164 WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32);
00165 WOLFSSL_API int wc_HmacFinal(Hmac*, byte*);
00166 
00167 #ifdef HAVE_CAVIUM
00168     WOLFSSL_API int  wc_HmacInitCavium(Hmac*, int);
00169     WOLFSSL_API void wc_HmacFreeCavium(Hmac*);
00170 #endif
00171 
00172 WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
00173 
00174 
00175 #ifdef HAVE_HKDF
00176 
00177 WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
00178                     const byte* salt, word32 saltSz,
00179                     const byte* info, word32 infoSz,
00180                     byte* out, word32 outSz);
00181 
00182 #endif /* HAVE_HKDF */
00183 
00184 #ifdef __cplusplus
00185     } /* extern "C" */
00186 #endif
00187 
00188 #endif /* WOLF_CRYPT_HMAC_H */
00189 
00190 #endif /* NO_HMAC */
00191 
00192