This is a port of cyaSSL 2.7.0.

Dependents:   CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet

Committer:
ashleymills
Date:
Thu Sep 05 15:55:50 2013 +0000
Revision:
1:c0ce1562443a
Parent:
0:714293de3836
Nothing;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:714293de3836 1 /* hmac.c
ashleymills 0:714293de3836 2 *
ashleymills 0:714293de3836 3 * Copyright (C) 2006-2013 wolfSSL Inc.
ashleymills 0:714293de3836 4 *
ashleymills 0:714293de3836 5 * This file is part of CyaSSL.
ashleymills 0:714293de3836 6 *
ashleymills 0:714293de3836 7 * CyaSSL is free software; you can redistribute it and/or modify
ashleymills 0:714293de3836 8 * it under the terms of the GNU General Public License as published by
ashleymills 0:714293de3836 9 * the Free Software Foundation; either version 2 of the License, or
ashleymills 0:714293de3836 10 * (at your option) any later version.
ashleymills 0:714293de3836 11 *
ashleymills 0:714293de3836 12 * CyaSSL is distributed in the hope that it will be useful,
ashleymills 0:714293de3836 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ashleymills 0:714293de3836 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ashleymills 0:714293de3836 15 * GNU General Public License for more details.
ashleymills 0:714293de3836 16 *
ashleymills 0:714293de3836 17 * You should have received a copy of the GNU General Public License
ashleymills 0:714293de3836 18 * along with this program; if not, write to the Free Software
ashleymills 0:714293de3836 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
ashleymills 0:714293de3836 20 */
ashleymills 0:714293de3836 21
ashleymills 0:714293de3836 22 #ifdef HAVE_CONFIG_H
ashleymills 0:714293de3836 23 #include <config.h>
ashleymills 0:714293de3836 24 #endif
ashleymills 0:714293de3836 25
ashleymills 0:714293de3836 26 #include <cyassl/ctaocrypt/settings.h>
ashleymills 0:714293de3836 27
ashleymills 0:714293de3836 28 #ifndef NO_HMAC
ashleymills 0:714293de3836 29
ashleymills 0:714293de3836 30 #include <cyassl/ctaocrypt/hmac.h>
ashleymills 0:714293de3836 31 #include <cyassl/ctaocrypt/ctaoerror2.h>
ashleymills 0:714293de3836 32
ashleymills 0:714293de3836 33
ashleymills 0:714293de3836 34 #ifdef HAVE_CAVIUM
ashleymills 0:714293de3836 35 static void HmacCaviumFinal(Hmac* hmac, byte* hash);
ashleymills 0:714293de3836 36 static void HmacCaviumUpdate(Hmac* hmac, const byte* msg, word32 length);
ashleymills 0:714293de3836 37 static void HmacCaviumSetKey(Hmac* hmac, int type, const byte* key,
ashleymills 0:714293de3836 38 word32 length);
ashleymills 0:714293de3836 39 #endif
ashleymills 0:714293de3836 40
ashleymills 0:714293de3836 41 static int InitHmac(Hmac* hmac, int type)
ashleymills 0:714293de3836 42 {
ashleymills 0:714293de3836 43 hmac->innerHashKeyed = 0;
ashleymills 0:714293de3836 44 hmac->macType = (byte)type;
ashleymills 0:714293de3836 45
ashleymills 0:714293de3836 46 if (!(type == MD5 || type == SHA || type == SHA256 || type == SHA384
ashleymills 0:714293de3836 47 || type == SHA512))
ashleymills 0:714293de3836 48 return BAD_FUNC_ARG;
ashleymills 0:714293de3836 49
ashleymills 0:714293de3836 50 switch (type) {
ashleymills 0:714293de3836 51 #ifndef NO_MD5
ashleymills 0:714293de3836 52 case MD5:
ashleymills 0:714293de3836 53 InitMd5(&hmac->hash.md5);
ashleymills 0:714293de3836 54 break;
ashleymills 0:714293de3836 55 #endif
ashleymills 0:714293de3836 56
ashleymills 0:714293de3836 57 #ifndef NO_SHA
ashleymills 0:714293de3836 58 case SHA:
ashleymills 0:714293de3836 59 InitSha(&hmac->hash.sha);
ashleymills 0:714293de3836 60 break;
ashleymills 0:714293de3836 61 #endif
ashleymills 0:714293de3836 62
ashleymills 0:714293de3836 63 #ifndef NO_SHA256
ashleymills 0:714293de3836 64 case SHA256:
ashleymills 0:714293de3836 65 InitSha256(&hmac->hash.sha256);
ashleymills 0:714293de3836 66 break;
ashleymills 0:714293de3836 67 #endif
ashleymills 0:714293de3836 68
ashleymills 0:714293de3836 69 #ifdef CYASSL_SHA384
ashleymills 0:714293de3836 70 case SHA384:
ashleymills 0:714293de3836 71 InitSha384(&hmac->hash.sha384);
ashleymills 0:714293de3836 72 break;
ashleymills 0:714293de3836 73 #endif
ashleymills 0:714293de3836 74
ashleymills 0:714293de3836 75 #ifdef CYASSL_SHA512
ashleymills 0:714293de3836 76 case SHA512:
ashleymills 0:714293de3836 77 InitSha512(&hmac->hash.sha512);
ashleymills 0:714293de3836 78 break;
ashleymills 0:714293de3836 79 #endif
ashleymills 0:714293de3836 80
ashleymills 0:714293de3836 81 default:
ashleymills 0:714293de3836 82 break;
ashleymills 0:714293de3836 83 }
ashleymills 0:714293de3836 84
ashleymills 0:714293de3836 85 return 0;
ashleymills 0:714293de3836 86 }
ashleymills 0:714293de3836 87
ashleymills 0:714293de3836 88
ashleymills 0:714293de3836 89 void HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
ashleymills 0:714293de3836 90 {
ashleymills 0:714293de3836 91 byte* ip = (byte*) hmac->ipad;
ashleymills 0:714293de3836 92 byte* op = (byte*) hmac->opad;
ashleymills 0:714293de3836 93 word32 i, hmac_block_size = 0;
ashleymills 0:714293de3836 94
ashleymills 0:714293de3836 95 #ifdef HAVE_CAVIUM
ashleymills 0:714293de3836 96 if (hmac->magic == CYASSL_HMAC_CAVIUM_MAGIC)
ashleymills 0:714293de3836 97 return HmacCaviumSetKey(hmac, type, key, length);
ashleymills 0:714293de3836 98 #endif
ashleymills 0:714293de3836 99
ashleymills 0:714293de3836 100 InitHmac(hmac, type);
ashleymills 0:714293de3836 101
ashleymills 0:714293de3836 102 switch (hmac->macType) {
ashleymills 0:714293de3836 103 #ifndef NO_MD5
ashleymills 0:714293de3836 104 case MD5:
ashleymills 0:714293de3836 105 {
ashleymills 0:714293de3836 106 hmac_block_size = MD5_BLOCK_SIZE;
ashleymills 0:714293de3836 107 if (length <= MD5_BLOCK_SIZE) {
ashleymills 0:714293de3836 108 XMEMCPY(ip, key, length);
ashleymills 0:714293de3836 109 }
ashleymills 0:714293de3836 110 else {
ashleymills 0:714293de3836 111 Md5Update(&hmac->hash.md5, key, length);
ashleymills 0:714293de3836 112 Md5Final(&hmac->hash.md5, ip);
ashleymills 0:714293de3836 113 length = MD5_DIGEST_SIZE;
ashleymills 0:714293de3836 114 }
ashleymills 0:714293de3836 115 }
ashleymills 0:714293de3836 116 break;
ashleymills 0:714293de3836 117 #endif
ashleymills 0:714293de3836 118
ashleymills 0:714293de3836 119 #ifndef NO_SHA
ashleymills 0:714293de3836 120 case SHA:
ashleymills 0:714293de3836 121 {
ashleymills 0:714293de3836 122 hmac_block_size = SHA_BLOCK_SIZE;
ashleymills 0:714293de3836 123 if (length <= SHA_BLOCK_SIZE) {
ashleymills 0:714293de3836 124 XMEMCPY(ip, key, length);
ashleymills 0:714293de3836 125 }
ashleymills 0:714293de3836 126 else {
ashleymills 0:714293de3836 127 ShaUpdate(&hmac->hash.sha, key, length);
ashleymills 0:714293de3836 128 ShaFinal(&hmac->hash.sha, ip);
ashleymills 0:714293de3836 129 length = SHA_DIGEST_SIZE;
ashleymills 0:714293de3836 130 }
ashleymills 0:714293de3836 131 }
ashleymills 0:714293de3836 132 break;
ashleymills 0:714293de3836 133 #endif
ashleymills 0:714293de3836 134
ashleymills 0:714293de3836 135 #ifndef NO_SHA256
ashleymills 0:714293de3836 136 case SHA256:
ashleymills 0:714293de3836 137 {
ashleymills 0:714293de3836 138 hmac_block_size = SHA256_BLOCK_SIZE;
ashleymills 0:714293de3836 139 if (length <= SHA256_BLOCK_SIZE) {
ashleymills 0:714293de3836 140 XMEMCPY(ip, key, length);
ashleymills 0:714293de3836 141 }
ashleymills 0:714293de3836 142 else {
ashleymills 0:714293de3836 143 Sha256Update(&hmac->hash.sha256, key, length);
ashleymills 0:714293de3836 144 Sha256Final(&hmac->hash.sha256, ip);
ashleymills 0:714293de3836 145 length = SHA256_DIGEST_SIZE;
ashleymills 0:714293de3836 146 }
ashleymills 0:714293de3836 147 }
ashleymills 0:714293de3836 148 break;
ashleymills 0:714293de3836 149 #endif
ashleymills 0:714293de3836 150
ashleymills 0:714293de3836 151 #ifdef CYASSL_SHA384
ashleymills 0:714293de3836 152 case SHA384:
ashleymills 0:714293de3836 153 {
ashleymills 0:714293de3836 154 hmac_block_size = SHA384_BLOCK_SIZE;
ashleymills 0:714293de3836 155 if (length <= SHA384_BLOCK_SIZE) {
ashleymills 0:714293de3836 156 XMEMCPY(ip, key, length);
ashleymills 0:714293de3836 157 }
ashleymills 0:714293de3836 158 else {
ashleymills 0:714293de3836 159 Sha384Update(&hmac->hash.sha384, key, length);
ashleymills 0:714293de3836 160 Sha384Final(&hmac->hash.sha384, ip);
ashleymills 0:714293de3836 161 length = SHA384_DIGEST_SIZE;
ashleymills 0:714293de3836 162 }
ashleymills 0:714293de3836 163 }
ashleymills 0:714293de3836 164 break;
ashleymills 0:714293de3836 165 #endif
ashleymills 0:714293de3836 166
ashleymills 0:714293de3836 167 #ifdef CYASSL_SHA512
ashleymills 0:714293de3836 168 case SHA512:
ashleymills 0:714293de3836 169 {
ashleymills 0:714293de3836 170 hmac_block_size = SHA512_BLOCK_SIZE;
ashleymills 0:714293de3836 171 if (length <= SHA512_BLOCK_SIZE) {
ashleymills 0:714293de3836 172 XMEMCPY(ip, key, length);
ashleymills 0:714293de3836 173 }
ashleymills 0:714293de3836 174 else {
ashleymills 0:714293de3836 175 Sha512Update(&hmac->hash.sha512, key, length);
ashleymills 0:714293de3836 176 Sha512Final(&hmac->hash.sha512, ip);
ashleymills 0:714293de3836 177 length = SHA512_DIGEST_SIZE;
ashleymills 0:714293de3836 178 }
ashleymills 0:714293de3836 179 }
ashleymills 0:714293de3836 180 break;
ashleymills 0:714293de3836 181 #endif
ashleymills 0:714293de3836 182
ashleymills 0:714293de3836 183 default:
ashleymills 0:714293de3836 184 break;
ashleymills 0:714293de3836 185 }
ashleymills 0:714293de3836 186 if (length < hmac_block_size)
ashleymills 0:714293de3836 187 XMEMSET(ip + length, 0, hmac_block_size - length);
ashleymills 0:714293de3836 188
ashleymills 0:714293de3836 189 for(i = 0; i < hmac_block_size; i++) {
ashleymills 0:714293de3836 190 op[i] = ip[i] ^ OPAD;
ashleymills 0:714293de3836 191 ip[i] ^= IPAD;
ashleymills 0:714293de3836 192 }
ashleymills 0:714293de3836 193 }
ashleymills 0:714293de3836 194
ashleymills 0:714293de3836 195
ashleymills 0:714293de3836 196 static void HmacKeyInnerHash(Hmac* hmac)
ashleymills 0:714293de3836 197 {
ashleymills 0:714293de3836 198 switch (hmac->macType) {
ashleymills 0:714293de3836 199 #ifndef NO_MD5
ashleymills 0:714293de3836 200 case MD5:
ashleymills 0:714293de3836 201 Md5Update(&hmac->hash.md5, (byte*) hmac->ipad, MD5_BLOCK_SIZE);
ashleymills 0:714293de3836 202 break;
ashleymills 0:714293de3836 203 #endif
ashleymills 0:714293de3836 204
ashleymills 0:714293de3836 205 #ifndef NO_SHA
ashleymills 0:714293de3836 206 case SHA:
ashleymills 0:714293de3836 207 ShaUpdate(&hmac->hash.sha, (byte*) hmac->ipad, SHA_BLOCK_SIZE);
ashleymills 0:714293de3836 208 break;
ashleymills 0:714293de3836 209 #endif
ashleymills 0:714293de3836 210
ashleymills 0:714293de3836 211 #ifndef NO_SHA256
ashleymills 0:714293de3836 212 case SHA256:
ashleymills 0:714293de3836 213 Sha256Update(&hmac->hash.sha256,
ashleymills 0:714293de3836 214 (byte*) hmac->ipad, SHA256_BLOCK_SIZE);
ashleymills 0:714293de3836 215 break;
ashleymills 0:714293de3836 216 #endif
ashleymills 0:714293de3836 217
ashleymills 0:714293de3836 218 #ifdef CYASSL_SHA384
ashleymills 0:714293de3836 219 case SHA384:
ashleymills 0:714293de3836 220 Sha384Update(&hmac->hash.sha384,
ashleymills 0:714293de3836 221 (byte*) hmac->ipad, SHA384_BLOCK_SIZE);
ashleymills 0:714293de3836 222 break;
ashleymills 0:714293de3836 223 #endif
ashleymills 0:714293de3836 224
ashleymills 0:714293de3836 225 #ifdef CYASSL_SHA512
ashleymills 0:714293de3836 226 case SHA512:
ashleymills 0:714293de3836 227 Sha512Update(&hmac->hash.sha512,
ashleymills 0:714293de3836 228 (byte*) hmac->ipad, SHA512_BLOCK_SIZE);
ashleymills 0:714293de3836 229 break;
ashleymills 0:714293de3836 230 #endif
ashleymills 0:714293de3836 231
ashleymills 0:714293de3836 232 default:
ashleymills 0:714293de3836 233 break;
ashleymills 0:714293de3836 234 }
ashleymills 0:714293de3836 235
ashleymills 0:714293de3836 236 hmac->innerHashKeyed = 1;
ashleymills 0:714293de3836 237 }
ashleymills 0:714293de3836 238
ashleymills 0:714293de3836 239
ashleymills 0:714293de3836 240 void HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
ashleymills 0:714293de3836 241 {
ashleymills 0:714293de3836 242 #ifdef HAVE_CAVIUM
ashleymills 0:714293de3836 243 if (hmac->magic == CYASSL_HMAC_CAVIUM_MAGIC)
ashleymills 0:714293de3836 244 return HmacCaviumUpdate(hmac, msg, length);
ashleymills 0:714293de3836 245 #endif
ashleymills 0:714293de3836 246
ashleymills 0:714293de3836 247 if (!hmac->innerHashKeyed)
ashleymills 0:714293de3836 248 HmacKeyInnerHash(hmac);
ashleymills 0:714293de3836 249
ashleymills 0:714293de3836 250 switch (hmac->macType) {
ashleymills 0:714293de3836 251 #ifndef NO_MD5
ashleymills 0:714293de3836 252 case MD5:
ashleymills 0:714293de3836 253 Md5Update(&hmac->hash.md5, msg, length);
ashleymills 0:714293de3836 254 break;
ashleymills 0:714293de3836 255 #endif
ashleymills 0:714293de3836 256
ashleymills 0:714293de3836 257 #ifndef NO_SHA
ashleymills 0:714293de3836 258 case SHA:
ashleymills 0:714293de3836 259 ShaUpdate(&hmac->hash.sha, msg, length);
ashleymills 0:714293de3836 260 break;
ashleymills 0:714293de3836 261 #endif
ashleymills 0:714293de3836 262
ashleymills 0:714293de3836 263 #ifndef NO_SHA256
ashleymills 0:714293de3836 264 case SHA256:
ashleymills 0:714293de3836 265 Sha256Update(&hmac->hash.sha256, msg, length);
ashleymills 0:714293de3836 266 break;
ashleymills 0:714293de3836 267 #endif
ashleymills 0:714293de3836 268
ashleymills 0:714293de3836 269 #ifdef CYASSL_SHA384
ashleymills 0:714293de3836 270 case SHA384:
ashleymills 0:714293de3836 271 Sha384Update(&hmac->hash.sha384, msg, length);
ashleymills 0:714293de3836 272 break;
ashleymills 0:714293de3836 273 #endif
ashleymills 0:714293de3836 274
ashleymills 0:714293de3836 275 #ifdef CYASSL_SHA512
ashleymills 0:714293de3836 276 case SHA512:
ashleymills 0:714293de3836 277 Sha512Update(&hmac->hash.sha512, msg, length);
ashleymills 0:714293de3836 278 break;
ashleymills 0:714293de3836 279 #endif
ashleymills 0:714293de3836 280
ashleymills 0:714293de3836 281 default:
ashleymills 0:714293de3836 282 break;
ashleymills 0:714293de3836 283 }
ashleymills 0:714293de3836 284
ashleymills 0:714293de3836 285 }
ashleymills 0:714293de3836 286
ashleymills 0:714293de3836 287
ashleymills 0:714293de3836 288 void HmacFinal(Hmac* hmac, byte* hash)
ashleymills 0:714293de3836 289 {
ashleymills 0:714293de3836 290 #ifdef HAVE_CAVIUM
ashleymills 0:714293de3836 291 if (hmac->magic == CYASSL_HMAC_CAVIUM_MAGIC)
ashleymills 0:714293de3836 292 return HmacCaviumFinal(hmac, hash);
ashleymills 0:714293de3836 293 #endif
ashleymills 0:714293de3836 294
ashleymills 0:714293de3836 295 if (!hmac->innerHashKeyed)
ashleymills 0:714293de3836 296 HmacKeyInnerHash(hmac);
ashleymills 0:714293de3836 297
ashleymills 0:714293de3836 298 switch (hmac->macType) {
ashleymills 0:714293de3836 299 #ifndef NO_MD5
ashleymills 0:714293de3836 300 case MD5:
ashleymills 0:714293de3836 301 {
ashleymills 0:714293de3836 302 Md5Final(&hmac->hash.md5, (byte*) hmac->innerHash);
ashleymills 0:714293de3836 303
ashleymills 0:714293de3836 304 Md5Update(&hmac->hash.md5, (byte*) hmac->opad, MD5_BLOCK_SIZE);
ashleymills 0:714293de3836 305 Md5Update(&hmac->hash.md5,
ashleymills 0:714293de3836 306 (byte*) hmac->innerHash, MD5_DIGEST_SIZE);
ashleymills 0:714293de3836 307
ashleymills 0:714293de3836 308 Md5Final(&hmac->hash.md5, hash);
ashleymills 0:714293de3836 309 }
ashleymills 0:714293de3836 310 break;
ashleymills 0:714293de3836 311 #endif
ashleymills 0:714293de3836 312
ashleymills 0:714293de3836 313 #ifndef NO_SHA
ashleymills 0:714293de3836 314 case SHA:
ashleymills 0:714293de3836 315 {
ashleymills 0:714293de3836 316 ShaFinal(&hmac->hash.sha, (byte*) hmac->innerHash);
ashleymills 0:714293de3836 317
ashleymills 0:714293de3836 318 ShaUpdate(&hmac->hash.sha, (byte*) hmac->opad, SHA_BLOCK_SIZE);
ashleymills 0:714293de3836 319 ShaUpdate(&hmac->hash.sha,
ashleymills 0:714293de3836 320 (byte*) hmac->innerHash, SHA_DIGEST_SIZE);
ashleymills 0:714293de3836 321
ashleymills 0:714293de3836 322 ShaFinal(&hmac->hash.sha, hash);
ashleymills 0:714293de3836 323 }
ashleymills 0:714293de3836 324 break;
ashleymills 0:714293de3836 325 #endif
ashleymills 0:714293de3836 326
ashleymills 0:714293de3836 327 #ifndef NO_SHA256
ashleymills 0:714293de3836 328 case SHA256:
ashleymills 0:714293de3836 329 {
ashleymills 0:714293de3836 330 Sha256Final(&hmac->hash.sha256, (byte*) hmac->innerHash);
ashleymills 0:714293de3836 331
ashleymills 0:714293de3836 332 Sha256Update(&hmac->hash.sha256,
ashleymills 0:714293de3836 333 (byte*) hmac->opad, SHA256_BLOCK_SIZE);
ashleymills 0:714293de3836 334 Sha256Update(&hmac->hash.sha256,
ashleymills 0:714293de3836 335 (byte*) hmac->innerHash, SHA256_DIGEST_SIZE);
ashleymills 0:714293de3836 336
ashleymills 0:714293de3836 337 Sha256Final(&hmac->hash.sha256, hash);
ashleymills 0:714293de3836 338 }
ashleymills 0:714293de3836 339 break;
ashleymills 0:714293de3836 340 #endif
ashleymills 0:714293de3836 341
ashleymills 0:714293de3836 342 #ifdef CYASSL_SHA384
ashleymills 0:714293de3836 343 case SHA384:
ashleymills 0:714293de3836 344 {
ashleymills 0:714293de3836 345 Sha384Final(&hmac->hash.sha384, (byte*) hmac->innerHash);
ashleymills 0:714293de3836 346
ashleymills 0:714293de3836 347 Sha384Update(&hmac->hash.sha384,
ashleymills 0:714293de3836 348 (byte*) hmac->opad, SHA384_BLOCK_SIZE);
ashleymills 0:714293de3836 349 Sha384Update(&hmac->hash.sha384,
ashleymills 0:714293de3836 350 (byte*) hmac->innerHash, SHA384_DIGEST_SIZE);
ashleymills 0:714293de3836 351
ashleymills 0:714293de3836 352 Sha384Final(&hmac->hash.sha384, hash);
ashleymills 0:714293de3836 353 }
ashleymills 0:714293de3836 354 break;
ashleymills 0:714293de3836 355 #endif
ashleymills 0:714293de3836 356
ashleymills 0:714293de3836 357 #ifdef CYASSL_SHA512
ashleymills 0:714293de3836 358 case SHA512:
ashleymills 0:714293de3836 359 {
ashleymills 0:714293de3836 360 Sha512Final(&hmac->hash.sha512, (byte*) hmac->innerHash);
ashleymills 0:714293de3836 361
ashleymills 0:714293de3836 362 Sha512Update(&hmac->hash.sha512,
ashleymills 0:714293de3836 363 (byte*) hmac->opad, SHA512_BLOCK_SIZE);
ashleymills 0:714293de3836 364 Sha512Update(&hmac->hash.sha512,
ashleymills 0:714293de3836 365 (byte*) hmac->innerHash, SHA512_DIGEST_SIZE);
ashleymills 0:714293de3836 366
ashleymills 0:714293de3836 367 Sha512Final(&hmac->hash.sha512, hash);
ashleymills 0:714293de3836 368 }
ashleymills 0:714293de3836 369 break;
ashleymills 0:714293de3836 370 #endif
ashleymills 0:714293de3836 371
ashleymills 0:714293de3836 372 default:
ashleymills 0:714293de3836 373 break;
ashleymills 0:714293de3836 374 }
ashleymills 0:714293de3836 375
ashleymills 0:714293de3836 376 hmac->innerHashKeyed = 0;
ashleymills 0:714293de3836 377 }
ashleymills 0:714293de3836 378
ashleymills 0:714293de3836 379
ashleymills 0:714293de3836 380 #ifdef HAVE_CAVIUM
ashleymills 0:714293de3836 381
ashleymills 0:714293de3836 382 /* Initiliaze Hmac for use with Nitrox device */
ashleymills 0:714293de3836 383 int HmacInitCavium(Hmac* hmac, int devId)
ashleymills 0:714293de3836 384 {
ashleymills 0:714293de3836 385 if (hmac == NULL)
ashleymills 0:714293de3836 386 return -1;
ashleymills 0:714293de3836 387
ashleymills 0:714293de3836 388 if (CspAllocContext(CONTEXT_SSL, &hmac->contextHandle, devId) != 0)
ashleymills 0:714293de3836 389 return -1;
ashleymills 0:714293de3836 390
ashleymills 0:714293de3836 391 hmac->keyLen = 0;
ashleymills 0:714293de3836 392 hmac->dataLen = 0;
ashleymills 0:714293de3836 393 hmac->type = 0;
ashleymills 0:714293de3836 394 hmac->devId = devId;
ashleymills 0:714293de3836 395 hmac->magic = CYASSL_HMAC_CAVIUM_MAGIC;
ashleymills 0:714293de3836 396 hmac->data = NULL; /* buffered input data */
ashleymills 0:714293de3836 397
ashleymills 0:714293de3836 398 hmac->innerHashKeyed = 0;
ashleymills 0:714293de3836 399
ashleymills 0:714293de3836 400 return 0;
ashleymills 0:714293de3836 401 }
ashleymills 0:714293de3836 402
ashleymills 0:714293de3836 403
ashleymills 0:714293de3836 404 /* Free Hmac from use with Nitrox device */
ashleymills 0:714293de3836 405 void HmacFreeCavium(Hmac* hmac)
ashleymills 0:714293de3836 406 {
ashleymills 0:714293de3836 407 if (hmac == NULL)
ashleymills 0:714293de3836 408 return;
ashleymills 0:714293de3836 409
ashleymills 0:714293de3836 410 CspFreeContext(CONTEXT_SSL, hmac->contextHandle, hmac->devId);
ashleymills 0:714293de3836 411 hmac->magic = 0;
ashleymills 0:714293de3836 412 XFREE(hmac->data, NULL, DYNAMIC_TYPE_CAVIUM_TMP);
ashleymills 0:714293de3836 413 hmac->data = NULL;
ashleymills 0:714293de3836 414 }
ashleymills 0:714293de3836 415
ashleymills 0:714293de3836 416
ashleymills 0:714293de3836 417 static void HmacCaviumFinal(Hmac* hmac, byte* hash)
ashleymills 0:714293de3836 418 {
ashleymills 0:714293de3836 419 word32 requestId;
ashleymills 0:714293de3836 420
ashleymills 0:714293de3836 421 if (CspHmac(CAVIUM_BLOCKING, hmac->type, NULL, hmac->keyLen,
ashleymills 0:714293de3836 422 (byte*)hmac->ipad, hmac->dataLen, hmac->data, hash, &requestId,
ashleymills 0:714293de3836 423 hmac->devId) != 0) {
ashleymills 0:714293de3836 424 CYASSL_MSG("Cavium Hmac failed");
ashleymills 0:714293de3836 425 }
ashleymills 0:714293de3836 426 hmac->innerHashKeyed = 0; /* tell update to start over if used again */
ashleymills 0:714293de3836 427 }
ashleymills 0:714293de3836 428
ashleymills 0:714293de3836 429
ashleymills 0:714293de3836 430 static void HmacCaviumUpdate(Hmac* hmac, const byte* msg, word32 length)
ashleymills 0:714293de3836 431 {
ashleymills 0:714293de3836 432 word16 add = (word16)length;
ashleymills 0:714293de3836 433 word32 total;
ashleymills 0:714293de3836 434 byte* tmp;
ashleymills 0:714293de3836 435
ashleymills 0:714293de3836 436 if (length > CYASSL_MAX_16BIT) {
ashleymills 0:714293de3836 437 CYASSL_MSG("Too big msg for cavium hmac");
ashleymills 0:714293de3836 438 return;
ashleymills 0:714293de3836 439 }
ashleymills 0:714293de3836 440
ashleymills 0:714293de3836 441 if (hmac->innerHashKeyed == 0) { /* starting new */
ashleymills 0:714293de3836 442 hmac->dataLen = 0;
ashleymills 0:714293de3836 443 hmac->innerHashKeyed = 1;
ashleymills 0:714293de3836 444 }
ashleymills 0:714293de3836 445
ashleymills 0:714293de3836 446 total = add + hmac->dataLen;
ashleymills 0:714293de3836 447 if (total > CYASSL_MAX_16BIT) {
ashleymills 0:714293de3836 448 CYASSL_MSG("Too big msg for cavium hmac");
ashleymills 0:714293de3836 449 return;
ashleymills 0:714293de3836 450 }
ashleymills 0:714293de3836 451
ashleymills 0:714293de3836 452 tmp = XMALLOC(hmac->dataLen + add, NULL,DYNAMIC_TYPE_CAVIUM_TMP);
ashleymills 0:714293de3836 453 if (tmp == NULL) {
ashleymills 0:714293de3836 454 CYASSL_MSG("Out of memory for cavium update");
ashleymills 0:714293de3836 455 return;
ashleymills 0:714293de3836 456 }
ashleymills 0:714293de3836 457 if (hmac->dataLen)
ashleymills 0:714293de3836 458 XMEMCPY(tmp, hmac->data, hmac->dataLen);
ashleymills 0:714293de3836 459 XMEMCPY(tmp + hmac->dataLen, msg, add);
ashleymills 0:714293de3836 460
ashleymills 0:714293de3836 461 hmac->dataLen += add;
ashleymills 0:714293de3836 462 XFREE(hmac->data, NULL, DYNAMIC_TYPE_CAVIUM_TMP);
ashleymills 0:714293de3836 463 hmac->data = tmp;
ashleymills 0:714293de3836 464 }
ashleymills 0:714293de3836 465
ashleymills 0:714293de3836 466
ashleymills 0:714293de3836 467 static void HmacCaviumSetKey(Hmac* hmac, int type, const byte* key,
ashleymills 0:714293de3836 468 word32 length)
ashleymills 0:714293de3836 469 {
ashleymills 0:714293de3836 470 hmac->macType = (byte)type;
ashleymills 0:714293de3836 471 if (type == MD5)
ashleymills 0:714293de3836 472 hmac->type = MD5_TYPE;
ashleymills 0:714293de3836 473 else if (type == SHA)
ashleymills 0:714293de3836 474 hmac->type = SHA1_TYPE;
ashleymills 0:714293de3836 475 else if (type == SHA256)
ashleymills 0:714293de3836 476 hmac->type = SHA256_TYPE;
ashleymills 0:714293de3836 477 else {
ashleymills 0:714293de3836 478 CYASSL_MSG("unsupported cavium hmac type");
ashleymills 0:714293de3836 479 }
ashleymills 0:714293de3836 480
ashleymills 0:714293de3836 481 hmac->innerHashKeyed = 0; /* should we key Startup flag */
ashleymills 0:714293de3836 482
ashleymills 0:714293de3836 483 hmac->keyLen = (word16)length;
ashleymills 0:714293de3836 484 /* store key in ipad */
ashleymills 0:714293de3836 485 XMEMCPY(hmac->ipad, key, length);
ashleymills 0:714293de3836 486 }
ashleymills 0:714293de3836 487
ashleymills 0:714293de3836 488 #endif /* HAVE_CAVIUM */
ashleymills 0:714293de3836 489
ashleymills 0:714293de3836 490 #endif /* NO_HMAC */
ashleymills 0:714293de3836 491