SSL/TLS Library
CyaSSL is SSL/TLS library for embedded systems.
ctaocrypt/src/hmac.c@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.c |
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 | #ifdef HAVE_CONFIG_H |
wolfSSL | 0:9d17e4342598 | 23 | #include <config.h> |
wolfSSL | 0:9d17e4342598 | 24 | #endif |
wolfSSL | 0:9d17e4342598 | 25 | |
wolfSSL | 0:9d17e4342598 | 26 | #include <cyassl/ctaocrypt/settings.h> |
wolfSSL | 0:9d17e4342598 | 27 | |
wolfSSL | 0:9d17e4342598 | 28 | #ifndef NO_HMAC |
wolfSSL | 0:9d17e4342598 | 29 | |
wolfSSL | 0:9d17e4342598 | 30 | #ifdef CYASSL_PIC32MZ_HASH |
wolfSSL | 0:9d17e4342598 | 31 | |
wolfSSL | 0:9d17e4342598 | 32 | #define InitMd5 InitMd5_sw |
wolfSSL | 0:9d17e4342598 | 33 | #define Md5Update Md5Update_sw |
wolfSSL | 0:9d17e4342598 | 34 | #define Md5Final Md5Final_sw |
wolfSSL | 0:9d17e4342598 | 35 | |
wolfSSL | 0:9d17e4342598 | 36 | #define InitSha InitSha_sw |
wolfSSL | 0:9d17e4342598 | 37 | #define ShaUpdate ShaUpdate_sw |
wolfSSL | 0:9d17e4342598 | 38 | #define ShaFinal ShaFinal_sw |
wolfSSL | 0:9d17e4342598 | 39 | |
wolfSSL | 0:9d17e4342598 | 40 | #define InitSha256 InitSha256_sw |
wolfSSL | 0:9d17e4342598 | 41 | #define Sha256Update Sha256Update_sw |
wolfSSL | 0:9d17e4342598 | 42 | #define Sha256Final Sha256Final_sw |
wolfSSL | 0:9d17e4342598 | 43 | |
wolfSSL | 0:9d17e4342598 | 44 | #endif |
wolfSSL | 0:9d17e4342598 | 45 | |
wolfSSL | 0:9d17e4342598 | 46 | #ifdef HAVE_FIPS |
wolfSSL | 0:9d17e4342598 | 47 | /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */ |
wolfSSL | 0:9d17e4342598 | 48 | #define FIPS_NO_WRAPPERS |
wolfSSL | 0:9d17e4342598 | 49 | #endif |
wolfSSL | 0:9d17e4342598 | 50 | |
wolfSSL | 0:9d17e4342598 | 51 | #include <cyassl/ctaocrypt/hmac.h> |
wolfSSL | 0:9d17e4342598 | 52 | #include <cyassl/ctaocrypt/error-crypt.h> |
wolfSSL | 0:9d17e4342598 | 53 | |
wolfSSL | 0:9d17e4342598 | 54 | |
wolfSSL | 0:9d17e4342598 | 55 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 56 | static void HmacCaviumFinal(Hmac* hmac, byte* hash); |
wolfSSL | 0:9d17e4342598 | 57 | static void HmacCaviumUpdate(Hmac* hmac, const byte* msg, word32 length); |
wolfSSL | 0:9d17e4342598 | 58 | static void HmacCaviumSetKey(Hmac* hmac, int type, const byte* key, |
wolfSSL | 0:9d17e4342598 | 59 | word32 length); |
wolfSSL | 0:9d17e4342598 | 60 | #endif |
wolfSSL | 0:9d17e4342598 | 61 | |
wolfSSL | 0:9d17e4342598 | 62 | static int InitHmac(Hmac* hmac, int type) |
wolfSSL | 0:9d17e4342598 | 63 | { |
wolfSSL | 0:9d17e4342598 | 64 | int ret = 0; |
wolfSSL | 0:9d17e4342598 | 65 | |
wolfSSL | 0:9d17e4342598 | 66 | hmac->innerHashKeyed = 0; |
wolfSSL | 0:9d17e4342598 | 67 | hmac->macType = (byte)type; |
wolfSSL | 0:9d17e4342598 | 68 | |
wolfSSL | 0:9d17e4342598 | 69 | if (!(type == MD5 || type == SHA || type == SHA256 || type == SHA384 |
wolfSSL | 0:9d17e4342598 | 70 | || type == SHA512 || type == BLAKE2B_ID)) |
wolfSSL | 0:9d17e4342598 | 71 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 72 | |
wolfSSL | 0:9d17e4342598 | 73 | switch (type) { |
wolfSSL | 0:9d17e4342598 | 74 | #ifndef NO_MD5 |
wolfSSL | 0:9d17e4342598 | 75 | case MD5: |
wolfSSL | 0:9d17e4342598 | 76 | InitMd5(&hmac->hash.md5); |
wolfSSL | 0:9d17e4342598 | 77 | break; |
wolfSSL | 0:9d17e4342598 | 78 | #endif |
wolfSSL | 0:9d17e4342598 | 79 | |
wolfSSL | 0:9d17e4342598 | 80 | #ifndef NO_SHA |
wolfSSL | 0:9d17e4342598 | 81 | case SHA: |
wolfSSL | 0:9d17e4342598 | 82 | ret = InitSha(&hmac->hash.sha); |
wolfSSL | 0:9d17e4342598 | 83 | break; |
wolfSSL | 0:9d17e4342598 | 84 | #endif |
wolfSSL | 0:9d17e4342598 | 85 | |
wolfSSL | 0:9d17e4342598 | 86 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 87 | case SHA256: |
wolfSSL | 0:9d17e4342598 | 88 | ret = InitSha256(&hmac->hash.sha256); |
wolfSSL | 0:9d17e4342598 | 89 | break; |
wolfSSL | 0:9d17e4342598 | 90 | #endif |
wolfSSL | 0:9d17e4342598 | 91 | |
wolfSSL | 0:9d17e4342598 | 92 | #ifdef CYASSL_SHA384 |
wolfSSL | 0:9d17e4342598 | 93 | case SHA384: |
wolfSSL | 0:9d17e4342598 | 94 | ret = InitSha384(&hmac->hash.sha384); |
wolfSSL | 0:9d17e4342598 | 95 | break; |
wolfSSL | 0:9d17e4342598 | 96 | #endif |
wolfSSL | 0:9d17e4342598 | 97 | |
wolfSSL | 0:9d17e4342598 | 98 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 99 | case SHA512: |
wolfSSL | 0:9d17e4342598 | 100 | ret = InitSha512(&hmac->hash.sha512); |
wolfSSL | 0:9d17e4342598 | 101 | break; |
wolfSSL | 0:9d17e4342598 | 102 | #endif |
wolfSSL | 0:9d17e4342598 | 103 | |
wolfSSL | 0:9d17e4342598 | 104 | #ifdef HAVE_BLAKE2 |
wolfSSL | 0:9d17e4342598 | 105 | case BLAKE2B_ID: |
wolfSSL | 0:9d17e4342598 | 106 | InitBlake2b(&hmac->hash.blake2b, BLAKE2B_256); |
wolfSSL | 0:9d17e4342598 | 107 | break; |
wolfSSL | 0:9d17e4342598 | 108 | #endif |
wolfSSL | 0:9d17e4342598 | 109 | |
wolfSSL | 0:9d17e4342598 | 110 | default: |
wolfSSL | 0:9d17e4342598 | 111 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 112 | } |
wolfSSL | 0:9d17e4342598 | 113 | |
wolfSSL | 0:9d17e4342598 | 114 | return ret; |
wolfSSL | 0:9d17e4342598 | 115 | } |
wolfSSL | 0:9d17e4342598 | 116 | |
wolfSSL | 0:9d17e4342598 | 117 | |
wolfSSL | 0:9d17e4342598 | 118 | int HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length) |
wolfSSL | 0:9d17e4342598 | 119 | { |
wolfSSL | 0:9d17e4342598 | 120 | byte* ip = (byte*) hmac->ipad; |
wolfSSL | 0:9d17e4342598 | 121 | byte* op = (byte*) hmac->opad; |
wolfSSL | 0:9d17e4342598 | 122 | word32 i, hmac_block_size = 0; |
wolfSSL | 0:9d17e4342598 | 123 | int ret; |
wolfSSL | 0:9d17e4342598 | 124 | |
wolfSSL | 0:9d17e4342598 | 125 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 126 | if (hmac->magic == CYASSL_HMAC_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 127 | return HmacCaviumSetKey(hmac, type, key, length); |
wolfSSL | 0:9d17e4342598 | 128 | #endif |
wolfSSL | 0:9d17e4342598 | 129 | |
wolfSSL | 0:9d17e4342598 | 130 | ret = InitHmac(hmac, type); |
wolfSSL | 0:9d17e4342598 | 131 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 132 | return ret; |
wolfSSL | 0:9d17e4342598 | 133 | |
wolfSSL | 0:9d17e4342598 | 134 | switch (hmac->macType) { |
wolfSSL | 0:9d17e4342598 | 135 | #ifndef NO_MD5 |
wolfSSL | 0:9d17e4342598 | 136 | case MD5: |
wolfSSL | 0:9d17e4342598 | 137 | { |
wolfSSL | 0:9d17e4342598 | 138 | hmac_block_size = MD5_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 139 | if (length <= MD5_BLOCK_SIZE) { |
wolfSSL | 0:9d17e4342598 | 140 | XMEMCPY(ip, key, length); |
wolfSSL | 0:9d17e4342598 | 141 | } |
wolfSSL | 0:9d17e4342598 | 142 | else { |
wolfSSL | 0:9d17e4342598 | 143 | Md5Update(&hmac->hash.md5, key, length); |
wolfSSL | 0:9d17e4342598 | 144 | Md5Final(&hmac->hash.md5, ip); |
wolfSSL | 0:9d17e4342598 | 145 | length = MD5_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 146 | } |
wolfSSL | 0:9d17e4342598 | 147 | } |
wolfSSL | 0:9d17e4342598 | 148 | break; |
wolfSSL | 0:9d17e4342598 | 149 | #endif |
wolfSSL | 0:9d17e4342598 | 150 | |
wolfSSL | 0:9d17e4342598 | 151 | #ifndef NO_SHA |
wolfSSL | 0:9d17e4342598 | 152 | case SHA: |
wolfSSL | 0:9d17e4342598 | 153 | { |
wolfSSL | 0:9d17e4342598 | 154 | hmac_block_size = SHA_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 155 | if (length <= SHA_BLOCK_SIZE) { |
wolfSSL | 0:9d17e4342598 | 156 | XMEMCPY(ip, key, length); |
wolfSSL | 0:9d17e4342598 | 157 | } |
wolfSSL | 0:9d17e4342598 | 158 | else { |
wolfSSL | 0:9d17e4342598 | 159 | ShaUpdate(&hmac->hash.sha, key, length); |
wolfSSL | 0:9d17e4342598 | 160 | ShaFinal(&hmac->hash.sha, ip); |
wolfSSL | 0:9d17e4342598 | 161 | length = SHA_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 162 | } |
wolfSSL | 0:9d17e4342598 | 163 | } |
wolfSSL | 0:9d17e4342598 | 164 | break; |
wolfSSL | 0:9d17e4342598 | 165 | #endif |
wolfSSL | 0:9d17e4342598 | 166 | |
wolfSSL | 0:9d17e4342598 | 167 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 168 | case SHA256: |
wolfSSL | 0:9d17e4342598 | 169 | { |
wolfSSL | 0:9d17e4342598 | 170 | hmac_block_size = SHA256_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 171 | if (length <= SHA256_BLOCK_SIZE) { |
wolfSSL | 0:9d17e4342598 | 172 | XMEMCPY(ip, key, length); |
wolfSSL | 0:9d17e4342598 | 173 | } |
wolfSSL | 0:9d17e4342598 | 174 | else { |
wolfSSL | 0:9d17e4342598 | 175 | Sha256Update(&hmac->hash.sha256, key, length); |
wolfSSL | 0:9d17e4342598 | 176 | Sha256Final(&hmac->hash.sha256, ip); |
wolfSSL | 0:9d17e4342598 | 177 | length = SHA256_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 178 | } |
wolfSSL | 0:9d17e4342598 | 179 | } |
wolfSSL | 0:9d17e4342598 | 180 | break; |
wolfSSL | 0:9d17e4342598 | 181 | #endif |
wolfSSL | 0:9d17e4342598 | 182 | |
wolfSSL | 0:9d17e4342598 | 183 | #ifdef CYASSL_SHA384 |
wolfSSL | 0:9d17e4342598 | 184 | case SHA384: |
wolfSSL | 0:9d17e4342598 | 185 | { |
wolfSSL | 0:9d17e4342598 | 186 | hmac_block_size = SHA384_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 187 | if (length <= SHA384_BLOCK_SIZE) { |
wolfSSL | 0:9d17e4342598 | 188 | XMEMCPY(ip, key, length); |
wolfSSL | 0:9d17e4342598 | 189 | } |
wolfSSL | 0:9d17e4342598 | 190 | else { |
wolfSSL | 0:9d17e4342598 | 191 | Sha384Update(&hmac->hash.sha384, key, length); |
wolfSSL | 0:9d17e4342598 | 192 | Sha384Final(&hmac->hash.sha384, ip); |
wolfSSL | 0:9d17e4342598 | 193 | length = SHA384_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 194 | } |
wolfSSL | 0:9d17e4342598 | 195 | } |
wolfSSL | 0:9d17e4342598 | 196 | break; |
wolfSSL | 0:9d17e4342598 | 197 | #endif |
wolfSSL | 0:9d17e4342598 | 198 | |
wolfSSL | 0:9d17e4342598 | 199 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 200 | case SHA512: |
wolfSSL | 0:9d17e4342598 | 201 | { |
wolfSSL | 0:9d17e4342598 | 202 | hmac_block_size = SHA512_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 203 | if (length <= SHA512_BLOCK_SIZE) { |
wolfSSL | 0:9d17e4342598 | 204 | XMEMCPY(ip, key, length); |
wolfSSL | 0:9d17e4342598 | 205 | } |
wolfSSL | 0:9d17e4342598 | 206 | else { |
wolfSSL | 0:9d17e4342598 | 207 | Sha512Update(&hmac->hash.sha512, key, length); |
wolfSSL | 0:9d17e4342598 | 208 | Sha512Final(&hmac->hash.sha512, ip); |
wolfSSL | 0:9d17e4342598 | 209 | length = SHA512_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 210 | } |
wolfSSL | 0:9d17e4342598 | 211 | } |
wolfSSL | 0:9d17e4342598 | 212 | break; |
wolfSSL | 0:9d17e4342598 | 213 | #endif |
wolfSSL | 0:9d17e4342598 | 214 | |
wolfSSL | 0:9d17e4342598 | 215 | #ifdef HAVE_BLAKE2 |
wolfSSL | 0:9d17e4342598 | 216 | case BLAKE2B_ID: |
wolfSSL | 0:9d17e4342598 | 217 | { |
wolfSSL | 0:9d17e4342598 | 218 | hmac_block_size = BLAKE2B_BLOCKBYTES; |
wolfSSL | 0:9d17e4342598 | 219 | if (length <= BLAKE2B_BLOCKBYTES) { |
wolfSSL | 0:9d17e4342598 | 220 | XMEMCPY(ip, key, length); |
wolfSSL | 0:9d17e4342598 | 221 | } |
wolfSSL | 0:9d17e4342598 | 222 | else { |
wolfSSL | 0:9d17e4342598 | 223 | Blake2bUpdate(&hmac->hash.blake2b, key, length); |
wolfSSL | 0:9d17e4342598 | 224 | Blake2bFinal(&hmac->hash.blake2b, ip, BLAKE2B_256); |
wolfSSL | 0:9d17e4342598 | 225 | length = BLAKE2B_256; |
wolfSSL | 0:9d17e4342598 | 226 | } |
wolfSSL | 0:9d17e4342598 | 227 | } |
wolfSSL | 0:9d17e4342598 | 228 | break; |
wolfSSL | 0:9d17e4342598 | 229 | #endif |
wolfSSL | 0:9d17e4342598 | 230 | |
wolfSSL | 0:9d17e4342598 | 231 | default: |
wolfSSL | 0:9d17e4342598 | 232 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 233 | } |
wolfSSL | 0:9d17e4342598 | 234 | if (length < hmac_block_size) |
wolfSSL | 0:9d17e4342598 | 235 | XMEMSET(ip + length, 0, hmac_block_size - length); |
wolfSSL | 0:9d17e4342598 | 236 | |
wolfSSL | 0:9d17e4342598 | 237 | for(i = 0; i < hmac_block_size; i++) { |
wolfSSL | 0:9d17e4342598 | 238 | op[i] = ip[i] ^ OPAD; |
wolfSSL | 0:9d17e4342598 | 239 | ip[i] ^= IPAD; |
wolfSSL | 0:9d17e4342598 | 240 | } |
wolfSSL | 0:9d17e4342598 | 241 | return 0; |
wolfSSL | 0:9d17e4342598 | 242 | } |
wolfSSL | 0:9d17e4342598 | 243 | |
wolfSSL | 0:9d17e4342598 | 244 | |
wolfSSL | 0:9d17e4342598 | 245 | static void HmacKeyInnerHash(Hmac* hmac) |
wolfSSL | 0:9d17e4342598 | 246 | { |
wolfSSL | 0:9d17e4342598 | 247 | switch (hmac->macType) { |
wolfSSL | 0:9d17e4342598 | 248 | #ifndef NO_MD5 |
wolfSSL | 0:9d17e4342598 | 249 | case MD5: |
wolfSSL | 0:9d17e4342598 | 250 | Md5Update(&hmac->hash.md5, (byte*) hmac->ipad, MD5_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 251 | break; |
wolfSSL | 0:9d17e4342598 | 252 | #endif |
wolfSSL | 0:9d17e4342598 | 253 | |
wolfSSL | 0:9d17e4342598 | 254 | #ifndef NO_SHA |
wolfSSL | 0:9d17e4342598 | 255 | case SHA: |
wolfSSL | 0:9d17e4342598 | 256 | ShaUpdate(&hmac->hash.sha, (byte*) hmac->ipad, SHA_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 257 | break; |
wolfSSL | 0:9d17e4342598 | 258 | #endif |
wolfSSL | 0:9d17e4342598 | 259 | |
wolfSSL | 0:9d17e4342598 | 260 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 261 | case SHA256: |
wolfSSL | 0:9d17e4342598 | 262 | Sha256Update(&hmac->hash.sha256, |
wolfSSL | 0:9d17e4342598 | 263 | (byte*) hmac->ipad, SHA256_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 264 | break; |
wolfSSL | 0:9d17e4342598 | 265 | #endif |
wolfSSL | 0:9d17e4342598 | 266 | |
wolfSSL | 0:9d17e4342598 | 267 | #ifdef CYASSL_SHA384 |
wolfSSL | 0:9d17e4342598 | 268 | case SHA384: |
wolfSSL | 0:9d17e4342598 | 269 | Sha384Update(&hmac->hash.sha384, |
wolfSSL | 0:9d17e4342598 | 270 | (byte*) hmac->ipad, SHA384_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 271 | break; |
wolfSSL | 0:9d17e4342598 | 272 | #endif |
wolfSSL | 0:9d17e4342598 | 273 | |
wolfSSL | 0:9d17e4342598 | 274 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 275 | case SHA512: |
wolfSSL | 0:9d17e4342598 | 276 | Sha512Update(&hmac->hash.sha512, |
wolfSSL | 0:9d17e4342598 | 277 | (byte*) hmac->ipad, SHA512_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 278 | break; |
wolfSSL | 0:9d17e4342598 | 279 | #endif |
wolfSSL | 0:9d17e4342598 | 280 | |
wolfSSL | 0:9d17e4342598 | 281 | #ifdef HAVE_BLAKE2 |
wolfSSL | 0:9d17e4342598 | 282 | case BLAKE2B_ID: |
wolfSSL | 0:9d17e4342598 | 283 | Blake2bUpdate(&hmac->hash.blake2b, |
wolfSSL | 0:9d17e4342598 | 284 | (byte*) hmac->ipad,BLAKE2B_BLOCKBYTES); |
wolfSSL | 0:9d17e4342598 | 285 | break; |
wolfSSL | 0:9d17e4342598 | 286 | #endif |
wolfSSL | 0:9d17e4342598 | 287 | |
wolfSSL | 0:9d17e4342598 | 288 | default: |
wolfSSL | 0:9d17e4342598 | 289 | break; |
wolfSSL | 0:9d17e4342598 | 290 | } |
wolfSSL | 0:9d17e4342598 | 291 | |
wolfSSL | 0:9d17e4342598 | 292 | hmac->innerHashKeyed = 1; |
wolfSSL | 0:9d17e4342598 | 293 | } |
wolfSSL | 0:9d17e4342598 | 294 | |
wolfSSL | 0:9d17e4342598 | 295 | |
wolfSSL | 0:9d17e4342598 | 296 | int HmacUpdate(Hmac* hmac, const byte* msg, word32 length) |
wolfSSL | 0:9d17e4342598 | 297 | { |
wolfSSL | 0:9d17e4342598 | 298 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 299 | if (hmac->magic == CYASSL_HMAC_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 300 | return HmacCaviumUpdate(hmac, msg, length); |
wolfSSL | 0:9d17e4342598 | 301 | #endif |
wolfSSL | 0:9d17e4342598 | 302 | |
wolfSSL | 0:9d17e4342598 | 303 | if (!hmac->innerHashKeyed) |
wolfSSL | 0:9d17e4342598 | 304 | HmacKeyInnerHash(hmac); |
wolfSSL | 0:9d17e4342598 | 305 | |
wolfSSL | 0:9d17e4342598 | 306 | switch (hmac->macType) { |
wolfSSL | 0:9d17e4342598 | 307 | #ifndef NO_MD5 |
wolfSSL | 0:9d17e4342598 | 308 | case MD5: |
wolfSSL | 0:9d17e4342598 | 309 | Md5Update(&hmac->hash.md5, msg, length); |
wolfSSL | 0:9d17e4342598 | 310 | break; |
wolfSSL | 0:9d17e4342598 | 311 | #endif |
wolfSSL | 0:9d17e4342598 | 312 | |
wolfSSL | 0:9d17e4342598 | 313 | #ifndef NO_SHA |
wolfSSL | 0:9d17e4342598 | 314 | case SHA: |
wolfSSL | 0:9d17e4342598 | 315 | ShaUpdate(&hmac->hash.sha, msg, length); |
wolfSSL | 0:9d17e4342598 | 316 | break; |
wolfSSL | 0:9d17e4342598 | 317 | #endif |
wolfSSL | 0:9d17e4342598 | 318 | |
wolfSSL | 0:9d17e4342598 | 319 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 320 | case SHA256: |
wolfSSL | 0:9d17e4342598 | 321 | Sha256Update(&hmac->hash.sha256, msg, length); |
wolfSSL | 0:9d17e4342598 | 322 | break; |
wolfSSL | 0:9d17e4342598 | 323 | #endif |
wolfSSL | 0:9d17e4342598 | 324 | |
wolfSSL | 0:9d17e4342598 | 325 | #ifdef CYASSL_SHA384 |
wolfSSL | 0:9d17e4342598 | 326 | case SHA384: |
wolfSSL | 0:9d17e4342598 | 327 | Sha384Update(&hmac->hash.sha384, msg, length); |
wolfSSL | 0:9d17e4342598 | 328 | break; |
wolfSSL | 0:9d17e4342598 | 329 | #endif |
wolfSSL | 0:9d17e4342598 | 330 | |
wolfSSL | 0:9d17e4342598 | 331 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 332 | case SHA512: |
wolfSSL | 0:9d17e4342598 | 333 | Sha512Update(&hmac->hash.sha512, msg, length); |
wolfSSL | 0:9d17e4342598 | 334 | break; |
wolfSSL | 0:9d17e4342598 | 335 | #endif |
wolfSSL | 0:9d17e4342598 | 336 | |
wolfSSL | 0:9d17e4342598 | 337 | #ifdef HAVE_BLAKE2 |
wolfSSL | 0:9d17e4342598 | 338 | case BLAKE2B_ID: |
wolfSSL | 0:9d17e4342598 | 339 | Blake2bUpdate(&hmac->hash.blake2b, msg, length); |
wolfSSL | 0:9d17e4342598 | 340 | break; |
wolfSSL | 0:9d17e4342598 | 341 | #endif |
wolfSSL | 0:9d17e4342598 | 342 | |
wolfSSL | 0:9d17e4342598 | 343 | default: |
wolfSSL | 0:9d17e4342598 | 344 | break; |
wolfSSL | 0:9d17e4342598 | 345 | } |
wolfSSL | 0:9d17e4342598 | 346 | |
wolfSSL | 0:9d17e4342598 | 347 | return 0; |
wolfSSL | 0:9d17e4342598 | 348 | } |
wolfSSL | 0:9d17e4342598 | 349 | |
wolfSSL | 0:9d17e4342598 | 350 | |
wolfSSL | 0:9d17e4342598 | 351 | int HmacFinal(Hmac* hmac, byte* hash) |
wolfSSL | 0:9d17e4342598 | 352 | { |
wolfSSL | 0:9d17e4342598 | 353 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 354 | if (hmac->magic == CYASSL_HMAC_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 355 | return HmacCaviumFinal(hmac, hash); |
wolfSSL | 0:9d17e4342598 | 356 | #endif |
wolfSSL | 0:9d17e4342598 | 357 | |
wolfSSL | 0:9d17e4342598 | 358 | if (!hmac->innerHashKeyed) |
wolfSSL | 0:9d17e4342598 | 359 | HmacKeyInnerHash(hmac); |
wolfSSL | 0:9d17e4342598 | 360 | |
wolfSSL | 0:9d17e4342598 | 361 | switch (hmac->macType) { |
wolfSSL | 0:9d17e4342598 | 362 | #ifndef NO_MD5 |
wolfSSL | 0:9d17e4342598 | 363 | case MD5: |
wolfSSL | 0:9d17e4342598 | 364 | { |
wolfSSL | 0:9d17e4342598 | 365 | Md5Final(&hmac->hash.md5, (byte*) hmac->innerHash); |
wolfSSL | 0:9d17e4342598 | 366 | |
wolfSSL | 0:9d17e4342598 | 367 | Md5Update(&hmac->hash.md5, (byte*) hmac->opad, MD5_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 368 | Md5Update(&hmac->hash.md5, |
wolfSSL | 0:9d17e4342598 | 369 | (byte*) hmac->innerHash, MD5_DIGEST_SIZE); |
wolfSSL | 0:9d17e4342598 | 370 | |
wolfSSL | 0:9d17e4342598 | 371 | Md5Final(&hmac->hash.md5, hash); |
wolfSSL | 0:9d17e4342598 | 372 | } |
wolfSSL | 0:9d17e4342598 | 373 | break; |
wolfSSL | 0:9d17e4342598 | 374 | #endif |
wolfSSL | 0:9d17e4342598 | 375 | |
wolfSSL | 0:9d17e4342598 | 376 | #ifndef NO_SHA |
wolfSSL | 0:9d17e4342598 | 377 | case SHA: |
wolfSSL | 0:9d17e4342598 | 378 | { |
wolfSSL | 0:9d17e4342598 | 379 | ShaFinal(&hmac->hash.sha, (byte*) hmac->innerHash); |
wolfSSL | 0:9d17e4342598 | 380 | |
wolfSSL | 0:9d17e4342598 | 381 | ShaUpdate(&hmac->hash.sha, (byte*) hmac->opad, SHA_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 382 | ShaUpdate(&hmac->hash.sha, |
wolfSSL | 0:9d17e4342598 | 383 | (byte*) hmac->innerHash, SHA_DIGEST_SIZE); |
wolfSSL | 0:9d17e4342598 | 384 | |
wolfSSL | 0:9d17e4342598 | 385 | ShaFinal(&hmac->hash.sha, hash); |
wolfSSL | 0:9d17e4342598 | 386 | } |
wolfSSL | 0:9d17e4342598 | 387 | break; |
wolfSSL | 0:9d17e4342598 | 388 | #endif |
wolfSSL | 0:9d17e4342598 | 389 | |
wolfSSL | 0:9d17e4342598 | 390 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 391 | case SHA256: |
wolfSSL | 0:9d17e4342598 | 392 | { |
wolfSSL | 0:9d17e4342598 | 393 | Sha256Final(&hmac->hash.sha256, (byte*) hmac->innerHash); |
wolfSSL | 0:9d17e4342598 | 394 | |
wolfSSL | 0:9d17e4342598 | 395 | Sha256Update(&hmac->hash.sha256, |
wolfSSL | 0:9d17e4342598 | 396 | (byte*) hmac->opad, SHA256_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 397 | Sha256Update(&hmac->hash.sha256, |
wolfSSL | 0:9d17e4342598 | 398 | (byte*) hmac->innerHash, SHA256_DIGEST_SIZE); |
wolfSSL | 0:9d17e4342598 | 399 | |
wolfSSL | 0:9d17e4342598 | 400 | Sha256Final(&hmac->hash.sha256, hash); |
wolfSSL | 0:9d17e4342598 | 401 | } |
wolfSSL | 0:9d17e4342598 | 402 | break; |
wolfSSL | 0:9d17e4342598 | 403 | #endif |
wolfSSL | 0:9d17e4342598 | 404 | |
wolfSSL | 0:9d17e4342598 | 405 | #ifdef CYASSL_SHA384 |
wolfSSL | 0:9d17e4342598 | 406 | case SHA384: |
wolfSSL | 0:9d17e4342598 | 407 | { |
wolfSSL | 0:9d17e4342598 | 408 | Sha384Final(&hmac->hash.sha384, (byte*) hmac->innerHash); |
wolfSSL | 0:9d17e4342598 | 409 | |
wolfSSL | 0:9d17e4342598 | 410 | Sha384Update(&hmac->hash.sha384, |
wolfSSL | 0:9d17e4342598 | 411 | (byte*) hmac->opad, SHA384_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 412 | Sha384Update(&hmac->hash.sha384, |
wolfSSL | 0:9d17e4342598 | 413 | (byte*) hmac->innerHash, SHA384_DIGEST_SIZE); |
wolfSSL | 0:9d17e4342598 | 414 | |
wolfSSL | 0:9d17e4342598 | 415 | Sha384Final(&hmac->hash.sha384, hash); |
wolfSSL | 0:9d17e4342598 | 416 | } |
wolfSSL | 0:9d17e4342598 | 417 | break; |
wolfSSL | 0:9d17e4342598 | 418 | #endif |
wolfSSL | 0:9d17e4342598 | 419 | |
wolfSSL | 0:9d17e4342598 | 420 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 421 | case SHA512: |
wolfSSL | 0:9d17e4342598 | 422 | { |
wolfSSL | 0:9d17e4342598 | 423 | Sha512Final(&hmac->hash.sha512, (byte*) hmac->innerHash); |
wolfSSL | 0:9d17e4342598 | 424 | |
wolfSSL | 0:9d17e4342598 | 425 | Sha512Update(&hmac->hash.sha512, |
wolfSSL | 0:9d17e4342598 | 426 | (byte*) hmac->opad, SHA512_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 427 | Sha512Update(&hmac->hash.sha512, |
wolfSSL | 0:9d17e4342598 | 428 | (byte*) hmac->innerHash, SHA512_DIGEST_SIZE); |
wolfSSL | 0:9d17e4342598 | 429 | |
wolfSSL | 0:9d17e4342598 | 430 | Sha512Final(&hmac->hash.sha512, hash); |
wolfSSL | 0:9d17e4342598 | 431 | } |
wolfSSL | 0:9d17e4342598 | 432 | break; |
wolfSSL | 0:9d17e4342598 | 433 | #endif |
wolfSSL | 0:9d17e4342598 | 434 | |
wolfSSL | 0:9d17e4342598 | 435 | #ifdef HAVE_BLAKE2 |
wolfSSL | 0:9d17e4342598 | 436 | case BLAKE2B_ID: |
wolfSSL | 0:9d17e4342598 | 437 | { |
wolfSSL | 0:9d17e4342598 | 438 | Blake2bFinal(&hmac->hash.blake2b, (byte*) hmac->innerHash, |
wolfSSL | 0:9d17e4342598 | 439 | BLAKE2B_256); |
wolfSSL | 0:9d17e4342598 | 440 | Blake2bUpdate(&hmac->hash.blake2b, |
wolfSSL | 0:9d17e4342598 | 441 | (byte*) hmac->opad, BLAKE2B_BLOCKBYTES); |
wolfSSL | 0:9d17e4342598 | 442 | Blake2bUpdate(&hmac->hash.blake2b, |
wolfSSL | 0:9d17e4342598 | 443 | (byte*) hmac->innerHash, BLAKE2B_256); |
wolfSSL | 0:9d17e4342598 | 444 | Blake2bFinal(&hmac->hash.blake2b, hash, BLAKE2B_256); |
wolfSSL | 0:9d17e4342598 | 445 | } |
wolfSSL | 0:9d17e4342598 | 446 | break; |
wolfSSL | 0:9d17e4342598 | 447 | #endif |
wolfSSL | 0:9d17e4342598 | 448 | |
wolfSSL | 0:9d17e4342598 | 449 | default: |
wolfSSL | 0:9d17e4342598 | 450 | break; |
wolfSSL | 0:9d17e4342598 | 451 | } |
wolfSSL | 0:9d17e4342598 | 452 | |
wolfSSL | 0:9d17e4342598 | 453 | hmac->innerHashKeyed = 0; |
wolfSSL | 0:9d17e4342598 | 454 | |
wolfSSL | 0:9d17e4342598 | 455 | return 0; |
wolfSSL | 0:9d17e4342598 | 456 | } |
wolfSSL | 0:9d17e4342598 | 457 | |
wolfSSL | 0:9d17e4342598 | 458 | |
wolfSSL | 0:9d17e4342598 | 459 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 460 | |
wolfSSL | 0:9d17e4342598 | 461 | /* Initiliaze Hmac for use with Nitrox device */ |
wolfSSL | 0:9d17e4342598 | 462 | int HmacInitCavium(Hmac* hmac, int devId) |
wolfSSL | 0:9d17e4342598 | 463 | { |
wolfSSL | 0:9d17e4342598 | 464 | if (hmac == NULL) |
wolfSSL | 0:9d17e4342598 | 465 | return -1; |
wolfSSL | 0:9d17e4342598 | 466 | |
wolfSSL | 0:9d17e4342598 | 467 | if (CspAllocContext(CONTEXT_SSL, &hmac->contextHandle, devId) != 0) |
wolfSSL | 0:9d17e4342598 | 468 | return -1; |
wolfSSL | 0:9d17e4342598 | 469 | |
wolfSSL | 0:9d17e4342598 | 470 | hmac->keyLen = 0; |
wolfSSL | 0:9d17e4342598 | 471 | hmac->dataLen = 0; |
wolfSSL | 0:9d17e4342598 | 472 | hmac->type = 0; |
wolfSSL | 0:9d17e4342598 | 473 | hmac->devId = devId; |
wolfSSL | 0:9d17e4342598 | 474 | hmac->magic = CYASSL_HMAC_CAVIUM_MAGIC; |
wolfSSL | 0:9d17e4342598 | 475 | hmac->data = NULL; /* buffered input data */ |
wolfSSL | 0:9d17e4342598 | 476 | |
wolfSSL | 0:9d17e4342598 | 477 | hmac->innerHashKeyed = 0; |
wolfSSL | 0:9d17e4342598 | 478 | |
wolfSSL | 0:9d17e4342598 | 479 | return 0; |
wolfSSL | 0:9d17e4342598 | 480 | } |
wolfSSL | 0:9d17e4342598 | 481 | |
wolfSSL | 0:9d17e4342598 | 482 | |
wolfSSL | 0:9d17e4342598 | 483 | /* Free Hmac from use with Nitrox device */ |
wolfSSL | 0:9d17e4342598 | 484 | void HmacFreeCavium(Hmac* hmac) |
wolfSSL | 0:9d17e4342598 | 485 | { |
wolfSSL | 0:9d17e4342598 | 486 | if (hmac == NULL) |
wolfSSL | 0:9d17e4342598 | 487 | return; |
wolfSSL | 0:9d17e4342598 | 488 | |
wolfSSL | 0:9d17e4342598 | 489 | CspFreeContext(CONTEXT_SSL, hmac->contextHandle, hmac->devId); |
wolfSSL | 0:9d17e4342598 | 490 | hmac->magic = 0; |
wolfSSL | 0:9d17e4342598 | 491 | XFREE(hmac->data, NULL, DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 492 | hmac->data = NULL; |
wolfSSL | 0:9d17e4342598 | 493 | } |
wolfSSL | 0:9d17e4342598 | 494 | |
wolfSSL | 0:9d17e4342598 | 495 | |
wolfSSL | 0:9d17e4342598 | 496 | static void HmacCaviumFinal(Hmac* hmac, byte* hash) |
wolfSSL | 0:9d17e4342598 | 497 | { |
wolfSSL | 0:9d17e4342598 | 498 | word32 requestId; |
wolfSSL | 0:9d17e4342598 | 499 | |
wolfSSL | 0:9d17e4342598 | 500 | if (CspHmac(CAVIUM_BLOCKING, hmac->type, NULL, hmac->keyLen, |
wolfSSL | 0:9d17e4342598 | 501 | (byte*)hmac->ipad, hmac->dataLen, hmac->data, hash, &requestId, |
wolfSSL | 0:9d17e4342598 | 502 | hmac->devId) != 0) { |
wolfSSL | 0:9d17e4342598 | 503 | CYASSL_MSG("Cavium Hmac failed"); |
wolfSSL | 0:9d17e4342598 | 504 | } |
wolfSSL | 0:9d17e4342598 | 505 | hmac->innerHashKeyed = 0; /* tell update to start over if used again */ |
wolfSSL | 0:9d17e4342598 | 506 | } |
wolfSSL | 0:9d17e4342598 | 507 | |
wolfSSL | 0:9d17e4342598 | 508 | |
wolfSSL | 0:9d17e4342598 | 509 | static void HmacCaviumUpdate(Hmac* hmac, const byte* msg, word32 length) |
wolfSSL | 0:9d17e4342598 | 510 | { |
wolfSSL | 0:9d17e4342598 | 511 | word16 add = (word16)length; |
wolfSSL | 0:9d17e4342598 | 512 | word32 total; |
wolfSSL | 0:9d17e4342598 | 513 | byte* tmp; |
wolfSSL | 0:9d17e4342598 | 514 | |
wolfSSL | 0:9d17e4342598 | 515 | if (length > CYASSL_MAX_16BIT) { |
wolfSSL | 0:9d17e4342598 | 516 | CYASSL_MSG("Too big msg for cavium hmac"); |
wolfSSL | 0:9d17e4342598 | 517 | return; |
wolfSSL | 0:9d17e4342598 | 518 | } |
wolfSSL | 0:9d17e4342598 | 519 | |
wolfSSL | 0:9d17e4342598 | 520 | if (hmac->innerHashKeyed == 0) { /* starting new */ |
wolfSSL | 0:9d17e4342598 | 521 | hmac->dataLen = 0; |
wolfSSL | 0:9d17e4342598 | 522 | hmac->innerHashKeyed = 1; |
wolfSSL | 0:9d17e4342598 | 523 | } |
wolfSSL | 0:9d17e4342598 | 524 | |
wolfSSL | 0:9d17e4342598 | 525 | total = add + hmac->dataLen; |
wolfSSL | 0:9d17e4342598 | 526 | if (total > CYASSL_MAX_16BIT) { |
wolfSSL | 0:9d17e4342598 | 527 | CYASSL_MSG("Too big msg for cavium hmac"); |
wolfSSL | 0:9d17e4342598 | 528 | return; |
wolfSSL | 0:9d17e4342598 | 529 | } |
wolfSSL | 0:9d17e4342598 | 530 | |
wolfSSL | 0:9d17e4342598 | 531 | tmp = XMALLOC(hmac->dataLen + add, NULL,DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 532 | if (tmp == NULL) { |
wolfSSL | 0:9d17e4342598 | 533 | CYASSL_MSG("Out of memory for cavium update"); |
wolfSSL | 0:9d17e4342598 | 534 | return; |
wolfSSL | 0:9d17e4342598 | 535 | } |
wolfSSL | 0:9d17e4342598 | 536 | if (hmac->dataLen) |
wolfSSL | 0:9d17e4342598 | 537 | XMEMCPY(tmp, hmac->data, hmac->dataLen); |
wolfSSL | 0:9d17e4342598 | 538 | XMEMCPY(tmp + hmac->dataLen, msg, add); |
wolfSSL | 0:9d17e4342598 | 539 | |
wolfSSL | 0:9d17e4342598 | 540 | hmac->dataLen += add; |
wolfSSL | 0:9d17e4342598 | 541 | XFREE(hmac->data, NULL, DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 542 | hmac->data = tmp; |
wolfSSL | 0:9d17e4342598 | 543 | } |
wolfSSL | 0:9d17e4342598 | 544 | |
wolfSSL | 0:9d17e4342598 | 545 | |
wolfSSL | 0:9d17e4342598 | 546 | static void HmacCaviumSetKey(Hmac* hmac, int type, const byte* key, |
wolfSSL | 0:9d17e4342598 | 547 | word32 length) |
wolfSSL | 0:9d17e4342598 | 548 | { |
wolfSSL | 0:9d17e4342598 | 549 | hmac->macType = (byte)type; |
wolfSSL | 0:9d17e4342598 | 550 | if (type == MD5) |
wolfSSL | 0:9d17e4342598 | 551 | hmac->type = MD5_TYPE; |
wolfSSL | 0:9d17e4342598 | 552 | else if (type == SHA) |
wolfSSL | 0:9d17e4342598 | 553 | hmac->type = SHA1_TYPE; |
wolfSSL | 0:9d17e4342598 | 554 | else if (type == SHA256) |
wolfSSL | 0:9d17e4342598 | 555 | hmac->type = SHA256_TYPE; |
wolfSSL | 0:9d17e4342598 | 556 | else { |
wolfSSL | 0:9d17e4342598 | 557 | CYASSL_MSG("unsupported cavium hmac type"); |
wolfSSL | 0:9d17e4342598 | 558 | } |
wolfSSL | 0:9d17e4342598 | 559 | |
wolfSSL | 0:9d17e4342598 | 560 | hmac->innerHashKeyed = 0; /* should we key Startup flag */ |
wolfSSL | 0:9d17e4342598 | 561 | |
wolfSSL | 0:9d17e4342598 | 562 | hmac->keyLen = (word16)length; |
wolfSSL | 0:9d17e4342598 | 563 | /* store key in ipad */ |
wolfSSL | 0:9d17e4342598 | 564 | XMEMCPY(hmac->ipad, key, length); |
wolfSSL | 0:9d17e4342598 | 565 | } |
wolfSSL | 0:9d17e4342598 | 566 | |
wolfSSL | 0:9d17e4342598 | 567 | #endif /* HAVE_CAVIUM */ |
wolfSSL | 0:9d17e4342598 | 568 | |
wolfSSL | 0:9d17e4342598 | 569 | int CyaSSL_GetHmacMaxSize(void) |
wolfSSL | 0:9d17e4342598 | 570 | { |
wolfSSL | 0:9d17e4342598 | 571 | return MAX_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 572 | } |
wolfSSL | 0:9d17e4342598 | 573 | |
wolfSSL | 0:9d17e4342598 | 574 | #ifdef HAVE_HKDF |
wolfSSL | 0:9d17e4342598 | 575 | |
wolfSSL | 0:9d17e4342598 | 576 | #ifndef min |
wolfSSL | 0:9d17e4342598 | 577 | |
wolfSSL | 0:9d17e4342598 | 578 | static INLINE word32 min(word32 a, word32 b) |
wolfSSL | 0:9d17e4342598 | 579 | { |
wolfSSL | 0:9d17e4342598 | 580 | return a > b ? b : a; |
wolfSSL | 0:9d17e4342598 | 581 | } |
wolfSSL | 0:9d17e4342598 | 582 | |
wolfSSL | 0:9d17e4342598 | 583 | #endif /* min */ |
wolfSSL | 0:9d17e4342598 | 584 | |
wolfSSL | 0:9d17e4342598 | 585 | |
wolfSSL | 0:9d17e4342598 | 586 | static INLINE int GetHashSizeByType(int type) |
wolfSSL | 0:9d17e4342598 | 587 | { |
wolfSSL | 0:9d17e4342598 | 588 | if (!(type == MD5 || type == SHA || type == SHA256 || type == SHA384 |
wolfSSL | 0:9d17e4342598 | 589 | || type == SHA512 || type == BLAKE2B_ID)) |
wolfSSL | 0:9d17e4342598 | 590 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 591 | |
wolfSSL | 0:9d17e4342598 | 592 | switch (type) { |
wolfSSL | 0:9d17e4342598 | 593 | #ifndef NO_MD5 |
wolfSSL | 0:9d17e4342598 | 594 | case MD5: |
wolfSSL | 0:9d17e4342598 | 595 | return MD5_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 596 | break; |
wolfSSL | 0:9d17e4342598 | 597 | #endif |
wolfSSL | 0:9d17e4342598 | 598 | |
wolfSSL | 0:9d17e4342598 | 599 | #ifndef NO_SHA |
wolfSSL | 0:9d17e4342598 | 600 | case SHA: |
wolfSSL | 0:9d17e4342598 | 601 | return SHA_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 602 | break; |
wolfSSL | 0:9d17e4342598 | 603 | #endif |
wolfSSL | 0:9d17e4342598 | 604 | |
wolfSSL | 0:9d17e4342598 | 605 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 606 | case SHA256: |
wolfSSL | 0:9d17e4342598 | 607 | return SHA256_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 608 | break; |
wolfSSL | 0:9d17e4342598 | 609 | #endif |
wolfSSL | 0:9d17e4342598 | 610 | |
wolfSSL | 0:9d17e4342598 | 611 | #ifdef CYASSL_SHA384 |
wolfSSL | 0:9d17e4342598 | 612 | case SHA384: |
wolfSSL | 0:9d17e4342598 | 613 | return SHA384_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 614 | break; |
wolfSSL | 0:9d17e4342598 | 615 | #endif |
wolfSSL | 0:9d17e4342598 | 616 | |
wolfSSL | 0:9d17e4342598 | 617 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 618 | case SHA512: |
wolfSSL | 0:9d17e4342598 | 619 | return SHA512_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 620 | break; |
wolfSSL | 0:9d17e4342598 | 621 | #endif |
wolfSSL | 0:9d17e4342598 | 622 | |
wolfSSL | 0:9d17e4342598 | 623 | #ifdef HAVE_BLAKE2 |
wolfSSL | 0:9d17e4342598 | 624 | case BLAKE2B_ID: |
wolfSSL | 0:9d17e4342598 | 625 | return BLAKE2B_OUTBYTES; |
wolfSSL | 0:9d17e4342598 | 626 | break; |
wolfSSL | 0:9d17e4342598 | 627 | #endif |
wolfSSL | 0:9d17e4342598 | 628 | |
wolfSSL | 0:9d17e4342598 | 629 | default: |
wolfSSL | 0:9d17e4342598 | 630 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 631 | break; |
wolfSSL | 0:9d17e4342598 | 632 | } |
wolfSSL | 0:9d17e4342598 | 633 | } |
wolfSSL | 0:9d17e4342598 | 634 | |
wolfSSL | 0:9d17e4342598 | 635 | |
wolfSSL | 0:9d17e4342598 | 636 | /* HMAC-KDF with hash type, optional salt and info, return 0 on success */ |
wolfSSL | 0:9d17e4342598 | 637 | int HKDF(int type, const byte* inKey, word32 inKeySz, |
wolfSSL | 0:9d17e4342598 | 638 | const byte* salt, word32 saltSz, |
wolfSSL | 0:9d17e4342598 | 639 | const byte* info, word32 infoSz, |
wolfSSL | 0:9d17e4342598 | 640 | byte* out, word32 outSz) |
wolfSSL | 0:9d17e4342598 | 641 | { |
wolfSSL | 0:9d17e4342598 | 642 | Hmac myHmac; |
wolfSSL | 0:9d17e4342598 | 643 | byte tmp[MAX_DIGEST_SIZE]; /* localSalt helper and T */ |
wolfSSL | 0:9d17e4342598 | 644 | byte prk[MAX_DIGEST_SIZE]; |
wolfSSL | 0:9d17e4342598 | 645 | const byte* localSalt; /* either points to user input or tmp */ |
wolfSSL | 0:9d17e4342598 | 646 | int hashSz = GetHashSizeByType(type); |
wolfSSL | 0:9d17e4342598 | 647 | word32 outIdx = 0; |
wolfSSL | 0:9d17e4342598 | 648 | byte n = 0x1; |
wolfSSL | 0:9d17e4342598 | 649 | |
wolfSSL | 0:9d17e4342598 | 650 | if (hashSz < 0) |
wolfSSL | 0:9d17e4342598 | 651 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 652 | |
wolfSSL | 0:9d17e4342598 | 653 | localSalt = salt; |
wolfSSL | 0:9d17e4342598 | 654 | if (localSalt == NULL) { |
wolfSSL | 0:9d17e4342598 | 655 | XMEMSET(tmp, 0, hashSz); |
wolfSSL | 0:9d17e4342598 | 656 | localSalt = tmp; |
wolfSSL | 0:9d17e4342598 | 657 | saltSz = hashSz; |
wolfSSL | 0:9d17e4342598 | 658 | } |
wolfSSL | 0:9d17e4342598 | 659 | |
wolfSSL | 0:9d17e4342598 | 660 | if (HmacSetKey(&myHmac, type, localSalt, saltSz) != 0) |
wolfSSL | 0:9d17e4342598 | 661 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 662 | |
wolfSSL | 0:9d17e4342598 | 663 | HmacUpdate(&myHmac, inKey, inKeySz); |
wolfSSL | 0:9d17e4342598 | 664 | HmacFinal(&myHmac, prk); |
wolfSSL | 0:9d17e4342598 | 665 | |
wolfSSL | 0:9d17e4342598 | 666 | while (outIdx < outSz) { |
wolfSSL | 0:9d17e4342598 | 667 | int tmpSz = (n == 1) ? 0 : hashSz; |
wolfSSL | 0:9d17e4342598 | 668 | word32 left = outSz - outIdx; |
wolfSSL | 0:9d17e4342598 | 669 | |
wolfSSL | 0:9d17e4342598 | 670 | if (HmacSetKey(&myHmac, type, prk, hashSz) != 0) |
wolfSSL | 0:9d17e4342598 | 671 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 672 | |
wolfSSL | 0:9d17e4342598 | 673 | HmacUpdate(&myHmac, tmp, tmpSz); |
wolfSSL | 0:9d17e4342598 | 674 | HmacUpdate(&myHmac, info, infoSz); |
wolfSSL | 0:9d17e4342598 | 675 | HmacUpdate(&myHmac, &n, 1); |
wolfSSL | 0:9d17e4342598 | 676 | HmacFinal(&myHmac, tmp); |
wolfSSL | 0:9d17e4342598 | 677 | |
wolfSSL | 0:9d17e4342598 | 678 | left = min(left, (word32)hashSz); |
wolfSSL | 0:9d17e4342598 | 679 | XMEMCPY(out+outIdx, tmp, left); |
wolfSSL | 0:9d17e4342598 | 680 | |
wolfSSL | 0:9d17e4342598 | 681 | outIdx += hashSz; |
wolfSSL | 0:9d17e4342598 | 682 | n++; |
wolfSSL | 0:9d17e4342598 | 683 | } |
wolfSSL | 0:9d17e4342598 | 684 | |
wolfSSL | 0:9d17e4342598 | 685 | return 0; |
wolfSSL | 0:9d17e4342598 | 686 | } |
wolfSSL | 0:9d17e4342598 | 687 | |
wolfSSL | 0:9d17e4342598 | 688 | #endif /* HAVE_HKDF */ |
wolfSSL | 0:9d17e4342598 | 689 | |
wolfSSL | 0:9d17e4342598 | 690 | #endif /* NO_HMAC */ |
wolfSSL | 0:9d17e4342598 | 691 |