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