This is a port of cyaSSL 2.7.0.

Dependents:   CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet

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_CAVIUM
00047     #include <cyassl/ctaocrypt/logging.h>
00048     #include "cavium_common.h"
00049 #endif
00050 
00051 #ifdef __cplusplus
00052     extern "C" {
00053 #endif
00054 
00055 
00056 #define CYASSL_HMAC_CAVIUM_MAGIC 0xBEEF0005
00057 
00058 enum {
00059     IPAD    = 0x36,
00060     OPAD    = 0x5C,
00061 
00062 /* If any hash is not enabled, add the ID here. */
00063 #ifdef NO_MD5
00064     MD5     = 0,
00065 #endif
00066 #ifdef NO_SHA
00067     SHA     = 1,
00068 #endif
00069 #ifdef NO_SHA256
00070     SHA256  = 2,
00071 #endif
00072 #ifndef CYASSL_SHA512
00073     SHA512  = 4,
00074 #endif
00075 #ifndef CYASSL_SHA384
00076     SHA384  = 5,
00077 #endif
00078 
00079 /* Select the largest available hash for the buffer size. */
00080 #if defined(CYASSL_SHA512)
00081     INNER_HASH_SIZE = SHA512_DIGEST_SIZE,
00082     HMAC_BLOCK_SIZE = SHA512_BLOCK_SIZE
00083 #elif defined(CYASSL_SHA384)
00084     INNER_HASH_SIZE = SHA384_DIGEST_SIZE,
00085     HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE
00086 #elif !defined(NO_SHA256)
00087     INNER_HASH_SIZE = SHA256_DIGEST_SIZE,
00088     HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE
00089 #elif !defined(NO_SHA)
00090     INNER_HASH_SIZE = SHA_DIGEST_SIZE,
00091     HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE
00092 #elif !defined(NO_MD5)
00093     INNER_HASH_SIZE = MD5_DIGEST_SIZE,
00094     HMAC_BLOCK_SIZE = MD5_BLOCK_SIZE
00095 #else
00096     #error "You have to have some kind of hash if you want to use HMAC."
00097 #endif
00098 };
00099 
00100 
00101 /* hash union */
00102 typedef union {
00103     #ifndef NO_MD5
00104         Md5 md5;
00105     #endif
00106     #ifndef NO_SHA
00107         Sha sha;
00108     #endif
00109     #ifndef NO_SHA256
00110         Sha256 sha256;
00111     #endif
00112     #ifdef CYASSL_SHA384
00113         Sha384 sha384;
00114     #endif
00115     #ifdef CYASSL_SHA512
00116         Sha512 sha512;
00117     #endif
00118 } Hash;
00119 
00120 /* Hmac digest */
00121 typedef struct Hmac {
00122     Hash    hash;
00123     word32  ipad[HMAC_BLOCK_SIZE  / sizeof(word32)];  /* same block size all*/
00124     word32  opad[HMAC_BLOCK_SIZE  / sizeof(word32)];
00125     word32  innerHash[INNER_HASH_SIZE / sizeof(word32)]; /* max size */
00126     byte    macType;                                     /* md5 sha or sha256 */
00127     byte    innerHashKeyed;                              /* keyed flag */
00128 #ifdef HAVE_CAVIUM
00129     word16   keyLen;          /* hmac key length */
00130     word16   dataLen;
00131     HashType type;            /* hmac key type */
00132     int      devId;           /* nitrox device id */
00133     word32   magic;           /* using cavium magic */
00134     word64   contextHandle;   /* nitrox context memory handle */
00135     byte*    data;            /* buffered input data for one call */
00136 #endif
00137 } Hmac;
00138 
00139 
00140 /* does init */
00141 CYASSL_API void HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
00142 CYASSL_API void HmacUpdate(Hmac*, const byte*, word32);
00143 CYASSL_API void HmacFinal(Hmac*, byte*);
00144 
00145 #ifdef HAVE_CAVIUM
00146     CYASSL_API int  HmacInitCavium(Hmac*, int);
00147     CYASSL_API void HmacFreeCavium(Hmac*);
00148 #endif
00149 
00150 
00151 #ifdef __cplusplus
00152     } /* extern "C" */
00153 #endif
00154 
00155 #endif /* CTAO_CRYPT_HMAC_H */
00156 
00157 #endif /* NO_HMAC */
00158