SSL/TLS Library
CyaSSL is SSL/TLS library for embedded systems.
cyassl/ctaocrypt/hmac.h@0:9d17e4342598, 2014-04-20 (annotated)
- Committer:
- wolfSSL
- Date:
- Sun Apr 20 12:40:57 2014 +0000
- Revision:
- 0:9d17e4342598
CyaSSL SSL/TLS Library 2.9.4;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 0:9d17e4342598 | 1 | /* hmac.h |
wolfSSL | 0:9d17e4342598 | 2 | * |
wolfSSL | 0:9d17e4342598 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
wolfSSL | 0:9d17e4342598 | 4 | * |
wolfSSL | 0:9d17e4342598 | 5 | * This file is part of CyaSSL. |
wolfSSL | 0:9d17e4342598 | 6 | * |
wolfSSL | 0:9d17e4342598 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
wolfSSL | 0:9d17e4342598 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 0:9d17e4342598 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 0:9d17e4342598 | 10 | * (at your option) any later version. |
wolfSSL | 0:9d17e4342598 | 11 | * |
wolfSSL | 0:9d17e4342598 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
wolfSSL | 0:9d17e4342598 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 0:9d17e4342598 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 0:9d17e4342598 | 15 | * GNU General Public License for more details. |
wolfSSL | 0:9d17e4342598 | 16 | * |
wolfSSL | 0:9d17e4342598 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 0:9d17e4342598 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 0:9d17e4342598 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
wolfSSL | 0:9d17e4342598 | 20 | */ |
wolfSSL | 0:9d17e4342598 | 21 | |
wolfSSL | 0:9d17e4342598 | 22 | |
wolfSSL | 0:9d17e4342598 | 23 | #ifndef NO_HMAC |
wolfSSL | 0:9d17e4342598 | 24 | |
wolfSSL | 0:9d17e4342598 | 25 | #ifndef CTAO_CRYPT_HMAC_H |
wolfSSL | 0:9d17e4342598 | 26 | #define CTAO_CRYPT_HMAC_H |
wolfSSL | 0:9d17e4342598 | 27 | |
wolfSSL | 0:9d17e4342598 | 28 | #include <cyassl/ctaocrypt/types.h> |
wolfSSL | 0:9d17e4342598 | 29 | |
wolfSSL | 0:9d17e4342598 | 30 | #ifndef NO_MD5 |
wolfSSL | 0:9d17e4342598 | 31 | #include <cyassl/ctaocrypt/md5.h> |
wolfSSL | 0:9d17e4342598 | 32 | #endif |
wolfSSL | 0:9d17e4342598 | 33 | |
wolfSSL | 0:9d17e4342598 | 34 | #ifndef NO_SHA |
wolfSSL | 0:9d17e4342598 | 35 | #include <cyassl/ctaocrypt/sha.h> |
wolfSSL | 0:9d17e4342598 | 36 | #endif |
wolfSSL | 0:9d17e4342598 | 37 | |
wolfSSL | 0:9d17e4342598 | 38 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 39 | #include <cyassl/ctaocrypt/sha256.h> |
wolfSSL | 0:9d17e4342598 | 40 | #endif |
wolfSSL | 0:9d17e4342598 | 41 | |
wolfSSL | 0:9d17e4342598 | 42 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 43 | #include <cyassl/ctaocrypt/sha512.h> |
wolfSSL | 0:9d17e4342598 | 44 | #endif |
wolfSSL | 0:9d17e4342598 | 45 | |
wolfSSL | 0:9d17e4342598 | 46 | #ifdef HAVE_BLAKE2 |
wolfSSL | 0:9d17e4342598 | 47 | #include <cyassl/ctaocrypt/blake2.h> |
wolfSSL | 0:9d17e4342598 | 48 | #endif |
wolfSSL | 0:9d17e4342598 | 49 | |
wolfSSL | 0:9d17e4342598 | 50 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 51 | #include <cyassl/ctaocrypt/logging.h> |
wolfSSL | 0:9d17e4342598 | 52 | #include "cavium_common.h" |
wolfSSL | 0:9d17e4342598 | 53 | #endif |
wolfSSL | 0:9d17e4342598 | 54 | |
wolfSSL | 0:9d17e4342598 | 55 | #ifdef __cplusplus |
wolfSSL | 0:9d17e4342598 | 56 | extern "C" { |
wolfSSL | 0:9d17e4342598 | 57 | #endif |
wolfSSL | 0:9d17e4342598 | 58 | |
wolfSSL | 0:9d17e4342598 | 59 | |
wolfSSL | 0:9d17e4342598 | 60 | #define CYASSL_HMAC_CAVIUM_MAGIC 0xBEEF0005 |
wolfSSL | 0:9d17e4342598 | 61 | |
wolfSSL | 0:9d17e4342598 | 62 | enum { |
wolfSSL | 0:9d17e4342598 | 63 | IPAD = 0x36, |
wolfSSL | 0:9d17e4342598 | 64 | OPAD = 0x5C, |
wolfSSL | 0:9d17e4342598 | 65 | |
wolfSSL | 0:9d17e4342598 | 66 | /* If any hash is not enabled, add the ID here. */ |
wolfSSL | 0:9d17e4342598 | 67 | #ifdef NO_MD5 |
wolfSSL | 0:9d17e4342598 | 68 | MD5 = 0, |
wolfSSL | 0:9d17e4342598 | 69 | #endif |
wolfSSL | 0:9d17e4342598 | 70 | #ifdef NO_SHA |
wolfSSL | 0:9d17e4342598 | 71 | SHA = 1, |
wolfSSL | 0:9d17e4342598 | 72 | #endif |
wolfSSL | 0:9d17e4342598 | 73 | #ifdef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 74 | SHA256 = 2, |
wolfSSL | 0:9d17e4342598 | 75 | #endif |
wolfSSL | 0:9d17e4342598 | 76 | #ifndef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 77 | SHA512 = 4, |
wolfSSL | 0:9d17e4342598 | 78 | #endif |
wolfSSL | 0:9d17e4342598 | 79 | #ifndef CYASSL_SHA384 |
wolfSSL | 0:9d17e4342598 | 80 | SHA384 = 5, |
wolfSSL | 0:9d17e4342598 | 81 | #endif |
wolfSSL | 0:9d17e4342598 | 82 | #ifndef HAVE_BLAKE2 |
wolfSSL | 0:9d17e4342598 | 83 | BLAKE2B_ID = 7, |
wolfSSL | 0:9d17e4342598 | 84 | #endif |
wolfSSL | 0:9d17e4342598 | 85 | |
wolfSSL | 0:9d17e4342598 | 86 | /* Select the largest available hash for the buffer size. */ |
wolfSSL | 0:9d17e4342598 | 87 | #if defined(CYASSL_SHA512) |
wolfSSL | 0:9d17e4342598 | 88 | MAX_DIGEST_SIZE = SHA512_DIGEST_SIZE, |
wolfSSL | 0:9d17e4342598 | 89 | HMAC_BLOCK_SIZE = SHA512_BLOCK_SIZE |
wolfSSL | 0:9d17e4342598 | 90 | #elif defined(HAVE_BLAKE2) |
wolfSSL | 0:9d17e4342598 | 91 | MAX_DIGEST_SIZE = BLAKE2B_OUTBYTES, |
wolfSSL | 0:9d17e4342598 | 92 | HMAC_BLOCK_SIZE = BLAKE2B_BLOCKBYTES, |
wolfSSL | 0:9d17e4342598 | 93 | #elif defined(CYASSL_SHA384) |
wolfSSL | 0:9d17e4342598 | 94 | MAX_DIGEST_SIZE = SHA384_DIGEST_SIZE, |
wolfSSL | 0:9d17e4342598 | 95 | HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE |
wolfSSL | 0:9d17e4342598 | 96 | #elif !defined(NO_SHA256) |
wolfSSL | 0:9d17e4342598 | 97 | MAX_DIGEST_SIZE = SHA256_DIGEST_SIZE, |
wolfSSL | 0:9d17e4342598 | 98 | HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE |
wolfSSL | 0:9d17e4342598 | 99 | #elif !defined(NO_SHA) |
wolfSSL | 0:9d17e4342598 | 100 | MAX_DIGEST_SIZE = SHA_DIGEST_SIZE, |
wolfSSL | 0:9d17e4342598 | 101 | HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE |
wolfSSL | 0:9d17e4342598 | 102 | #elif !defined(NO_MD5) |
wolfSSL | 0:9d17e4342598 | 103 | MAX_DIGEST_SIZE = MD5_DIGEST_SIZE, |
wolfSSL | 0:9d17e4342598 | 104 | HMAC_BLOCK_SIZE = MD5_BLOCK_SIZE |
wolfSSL | 0:9d17e4342598 | 105 | #else |
wolfSSL | 0:9d17e4342598 | 106 | #error "You have to have some kind of hash if you want to use HMAC." |
wolfSSL | 0:9d17e4342598 | 107 | #endif |
wolfSSL | 0:9d17e4342598 | 108 | }; |
wolfSSL | 0:9d17e4342598 | 109 | |
wolfSSL | 0:9d17e4342598 | 110 | |
wolfSSL | 0:9d17e4342598 | 111 | /* hash union */ |
wolfSSL | 0:9d17e4342598 | 112 | typedef union { |
wolfSSL | 0:9d17e4342598 | 113 | #ifndef NO_MD5 |
wolfSSL | 0:9d17e4342598 | 114 | Md5 md5; |
wolfSSL | 0:9d17e4342598 | 115 | #endif |
wolfSSL | 0:9d17e4342598 | 116 | #ifndef NO_SHA |
wolfSSL | 0:9d17e4342598 | 117 | Sha sha; |
wolfSSL | 0:9d17e4342598 | 118 | #endif |
wolfSSL | 0:9d17e4342598 | 119 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 120 | Sha256 sha256; |
wolfSSL | 0:9d17e4342598 | 121 | #endif |
wolfSSL | 0:9d17e4342598 | 122 | #ifdef CYASSL_SHA384 |
wolfSSL | 0:9d17e4342598 | 123 | Sha384 sha384; |
wolfSSL | 0:9d17e4342598 | 124 | #endif |
wolfSSL | 0:9d17e4342598 | 125 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 126 | Sha512 sha512; |
wolfSSL | 0:9d17e4342598 | 127 | #endif |
wolfSSL | 0:9d17e4342598 | 128 | #ifdef HAVE_BLAKE2 |
wolfSSL | 0:9d17e4342598 | 129 | Blake2b blake2b; |
wolfSSL | 0:9d17e4342598 | 130 | #endif |
wolfSSL | 0:9d17e4342598 | 131 | } Hash; |
wolfSSL | 0:9d17e4342598 | 132 | |
wolfSSL | 0:9d17e4342598 | 133 | /* Hmac digest */ |
wolfSSL | 0:9d17e4342598 | 134 | typedef struct Hmac { |
wolfSSL | 0:9d17e4342598 | 135 | Hash hash; |
wolfSSL | 0:9d17e4342598 | 136 | word32 ipad[HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/ |
wolfSSL | 0:9d17e4342598 | 137 | word32 opad[HMAC_BLOCK_SIZE / sizeof(word32)]; |
wolfSSL | 0:9d17e4342598 | 138 | word32 innerHash[MAX_DIGEST_SIZE / sizeof(word32)]; |
wolfSSL | 0:9d17e4342598 | 139 | byte macType; /* md5 sha or sha256 */ |
wolfSSL | 0:9d17e4342598 | 140 | byte innerHashKeyed; /* keyed flag */ |
wolfSSL | 0:9d17e4342598 | 141 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 142 | word16 keyLen; /* hmac key length */ |
wolfSSL | 0:9d17e4342598 | 143 | word16 dataLen; |
wolfSSL | 0:9d17e4342598 | 144 | HashType type; /* hmac key type */ |
wolfSSL | 0:9d17e4342598 | 145 | int devId; /* nitrox device id */ |
wolfSSL | 0:9d17e4342598 | 146 | word32 magic; /* using cavium magic */ |
wolfSSL | 0:9d17e4342598 | 147 | word64 contextHandle; /* nitrox context memory handle */ |
wolfSSL | 0:9d17e4342598 | 148 | byte* data; /* buffered input data for one call */ |
wolfSSL | 0:9d17e4342598 | 149 | #endif |
wolfSSL | 0:9d17e4342598 | 150 | } Hmac; |
wolfSSL | 0:9d17e4342598 | 151 | |
wolfSSL | 0:9d17e4342598 | 152 | |
wolfSSL | 0:9d17e4342598 | 153 | /* does init */ |
wolfSSL | 0:9d17e4342598 | 154 | CYASSL_API int HmacSetKey(Hmac*, int type, const byte* key, word32 keySz); |
wolfSSL | 0:9d17e4342598 | 155 | CYASSL_API int HmacUpdate(Hmac*, const byte*, word32); |
wolfSSL | 0:9d17e4342598 | 156 | CYASSL_API int HmacFinal(Hmac*, byte*); |
wolfSSL | 0:9d17e4342598 | 157 | |
wolfSSL | 0:9d17e4342598 | 158 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 159 | CYASSL_API int HmacInitCavium(Hmac*, int); |
wolfSSL | 0:9d17e4342598 | 160 | CYASSL_API void HmacFreeCavium(Hmac*); |
wolfSSL | 0:9d17e4342598 | 161 | #endif |
wolfSSL | 0:9d17e4342598 | 162 | |
wolfSSL | 0:9d17e4342598 | 163 | CYASSL_API int CyaSSL_GetHmacMaxSize(void); |
wolfSSL | 0:9d17e4342598 | 164 | |
wolfSSL | 0:9d17e4342598 | 165 | |
wolfSSL | 0:9d17e4342598 | 166 | #ifdef HAVE_HKDF |
wolfSSL | 0:9d17e4342598 | 167 | |
wolfSSL | 0:9d17e4342598 | 168 | CYASSL_API int HKDF(int type, const byte* inKey, word32 inKeySz, |
wolfSSL | 0:9d17e4342598 | 169 | const byte* salt, word32 saltSz, |
wolfSSL | 0:9d17e4342598 | 170 | const byte* info, word32 infoSz, |
wolfSSL | 0:9d17e4342598 | 171 | byte* out, word32 outSz); |
wolfSSL | 0:9d17e4342598 | 172 | |
wolfSSL | 0:9d17e4342598 | 173 | #endif /* HAVE_HKDF */ |
wolfSSL | 0:9d17e4342598 | 174 | |
wolfSSL | 0:9d17e4342598 | 175 | |
wolfSSL | 0:9d17e4342598 | 176 | #ifdef HAVE_FIPS |
wolfSSL | 0:9d17e4342598 | 177 | /* fips wrapper calls, user can call direct */ |
wolfSSL | 0:9d17e4342598 | 178 | CYASSL_API int HmacSetKey_fips(Hmac*, int type, const byte* key, |
wolfSSL | 0:9d17e4342598 | 179 | word32 keySz); |
wolfSSL | 0:9d17e4342598 | 180 | CYASSL_API int HmacUpdate_fips(Hmac*, const byte*, word32); |
wolfSSL | 0:9d17e4342598 | 181 | CYASSL_API int HmacFinal_fips(Hmac*, byte*); |
wolfSSL | 0:9d17e4342598 | 182 | #ifndef FIPS_NO_WRAPPERS |
wolfSSL | 0:9d17e4342598 | 183 | /* if not impl or fips.c impl wrapper force fips calls if fips build */ |
wolfSSL | 0:9d17e4342598 | 184 | #define HmacSetKey HmacSetKey_fips |
wolfSSL | 0:9d17e4342598 | 185 | #define HmacUpdate HmacUpdate_fips |
wolfSSL | 0:9d17e4342598 | 186 | #define HmacFinal HmacFinal_fips |
wolfSSL | 0:9d17e4342598 | 187 | #endif /* FIPS_NO_WRAPPERS */ |
wolfSSL | 0:9d17e4342598 | 188 | |
wolfSSL | 0:9d17e4342598 | 189 | #endif /* HAVE_FIPS */ |
wolfSSL | 0:9d17e4342598 | 190 | |
wolfSSL | 0:9d17e4342598 | 191 | |
wolfSSL | 0:9d17e4342598 | 192 | #ifdef __cplusplus |
wolfSSL | 0:9d17e4342598 | 193 | } /* extern "C" */ |
wolfSSL | 0:9d17e4342598 | 194 | #endif |
wolfSSL | 0:9d17e4342598 | 195 | |
wolfSSL | 0:9d17e4342598 | 196 | #endif /* CTAO_CRYPT_HMAC_H */ |
wolfSSL | 0:9d17e4342598 | 197 | |
wolfSSL | 0:9d17e4342598 | 198 | #endif /* NO_HMAC */ |
wolfSSL | 0:9d17e4342598 | 199 |