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