fork of cyassl-lib
Dependents: TLS_cyassl TLS_cyassl
cyassl/ctaocrypt/hmac.h@4:f377303c41be, 2013-09-16 (annotated)
- Committer:
- feb11
- Date:
- Mon Sep 16 09:53:35 2013 +0000
- Revision:
- 4:f377303c41be
- Parent:
- 0:714293de3836
changed settings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:714293de3836 | 1 | /* hmac.h |
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 | |
ashleymills | 0:714293de3836 | 23 | #ifndef NO_HMAC |
ashleymills | 0:714293de3836 | 24 | |
ashleymills | 0:714293de3836 | 25 | #ifndef CTAO_CRYPT_HMAC_H |
ashleymills | 0:714293de3836 | 26 | #define CTAO_CRYPT_HMAC_H |
ashleymills | 0:714293de3836 | 27 | |
ashleymills | 0:714293de3836 | 28 | #include <cyassl/ctaocrypt/types.h> |
ashleymills | 0:714293de3836 | 29 | |
ashleymills | 0:714293de3836 | 30 | #ifndef NO_MD5 |
ashleymills | 0:714293de3836 | 31 | #include <cyassl/ctaocrypt/md5.h> |
ashleymills | 0:714293de3836 | 32 | #endif |
ashleymills | 0:714293de3836 | 33 | |
ashleymills | 0:714293de3836 | 34 | #ifndef NO_SHA |
ashleymills | 0:714293de3836 | 35 | #include <cyassl/ctaocrypt/sha.h> |
ashleymills | 0:714293de3836 | 36 | #endif |
ashleymills | 0:714293de3836 | 37 | |
ashleymills | 0:714293de3836 | 38 | #ifndef NO_SHA256 |
ashleymills | 0:714293de3836 | 39 | #include <cyassl/ctaocrypt/sha256.h> |
ashleymills | 0:714293de3836 | 40 | #endif |
ashleymills | 0:714293de3836 | 41 | |
ashleymills | 0:714293de3836 | 42 | #ifdef CYASSL_SHA512 |
ashleymills | 0:714293de3836 | 43 | #include <cyassl/ctaocrypt/sha512.h> |
ashleymills | 0:714293de3836 | 44 | #endif |
ashleymills | 0:714293de3836 | 45 | |
ashleymills | 0:714293de3836 | 46 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 47 | #include <cyassl/ctaocrypt/logging.h> |
ashleymills | 0:714293de3836 | 48 | #include "cavium_common.h" |
ashleymills | 0:714293de3836 | 49 | #endif |
ashleymills | 0:714293de3836 | 50 | |
ashleymills | 0:714293de3836 | 51 | #ifdef __cplusplus |
ashleymills | 0:714293de3836 | 52 | extern "C" { |
ashleymills | 0:714293de3836 | 53 | #endif |
ashleymills | 0:714293de3836 | 54 | |
ashleymills | 0:714293de3836 | 55 | |
ashleymills | 0:714293de3836 | 56 | #define CYASSL_HMAC_CAVIUM_MAGIC 0xBEEF0005 |
ashleymills | 0:714293de3836 | 57 | |
ashleymills | 0:714293de3836 | 58 | enum { |
ashleymills | 0:714293de3836 | 59 | IPAD = 0x36, |
ashleymills | 0:714293de3836 | 60 | OPAD = 0x5C, |
ashleymills | 0:714293de3836 | 61 | |
ashleymills | 0:714293de3836 | 62 | /* If any hash is not enabled, add the ID here. */ |
ashleymills | 0:714293de3836 | 63 | #ifdef NO_MD5 |
ashleymills | 0:714293de3836 | 64 | MD5 = 0, |
ashleymills | 0:714293de3836 | 65 | #endif |
ashleymills | 0:714293de3836 | 66 | #ifdef NO_SHA |
ashleymills | 0:714293de3836 | 67 | SHA = 1, |
ashleymills | 0:714293de3836 | 68 | #endif |
ashleymills | 0:714293de3836 | 69 | #ifdef NO_SHA256 |
ashleymills | 0:714293de3836 | 70 | SHA256 = 2, |
ashleymills | 0:714293de3836 | 71 | #endif |
ashleymills | 0:714293de3836 | 72 | #ifndef CYASSL_SHA512 |
ashleymills | 0:714293de3836 | 73 | SHA512 = 4, |
ashleymills | 0:714293de3836 | 74 | #endif |
ashleymills | 0:714293de3836 | 75 | #ifndef CYASSL_SHA384 |
ashleymills | 0:714293de3836 | 76 | SHA384 = 5, |
ashleymills | 0:714293de3836 | 77 | #endif |
ashleymills | 0:714293de3836 | 78 | |
ashleymills | 0:714293de3836 | 79 | /* Select the largest available hash for the buffer size. */ |
ashleymills | 0:714293de3836 | 80 | #if defined(CYASSL_SHA512) |
ashleymills | 0:714293de3836 | 81 | INNER_HASH_SIZE = SHA512_DIGEST_SIZE, |
ashleymills | 0:714293de3836 | 82 | HMAC_BLOCK_SIZE = SHA512_BLOCK_SIZE |
ashleymills | 0:714293de3836 | 83 | #elif defined(CYASSL_SHA384) |
ashleymills | 0:714293de3836 | 84 | INNER_HASH_SIZE = SHA384_DIGEST_SIZE, |
ashleymills | 0:714293de3836 | 85 | HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE |
ashleymills | 0:714293de3836 | 86 | #elif !defined(NO_SHA256) |
ashleymills | 0:714293de3836 | 87 | INNER_HASH_SIZE = SHA256_DIGEST_SIZE, |
ashleymills | 0:714293de3836 | 88 | HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE |
ashleymills | 0:714293de3836 | 89 | #elif !defined(NO_SHA) |
ashleymills | 0:714293de3836 | 90 | INNER_HASH_SIZE = SHA_DIGEST_SIZE, |
ashleymills | 0:714293de3836 | 91 | HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE |
ashleymills | 0:714293de3836 | 92 | #elif !defined(NO_MD5) |
ashleymills | 0:714293de3836 | 93 | INNER_HASH_SIZE = MD5_DIGEST_SIZE, |
ashleymills | 0:714293de3836 | 94 | HMAC_BLOCK_SIZE = MD5_BLOCK_SIZE |
ashleymills | 0:714293de3836 | 95 | #else |
ashleymills | 0:714293de3836 | 96 | #error "You have to have some kind of hash if you want to use HMAC." |
ashleymills | 0:714293de3836 | 97 | #endif |
ashleymills | 0:714293de3836 | 98 | }; |
ashleymills | 0:714293de3836 | 99 | |
ashleymills | 0:714293de3836 | 100 | |
ashleymills | 0:714293de3836 | 101 | /* hash union */ |
ashleymills | 0:714293de3836 | 102 | typedef union { |
ashleymills | 0:714293de3836 | 103 | #ifndef NO_MD5 |
ashleymills | 0:714293de3836 | 104 | Md5 md5; |
ashleymills | 0:714293de3836 | 105 | #endif |
ashleymills | 0:714293de3836 | 106 | #ifndef NO_SHA |
ashleymills | 0:714293de3836 | 107 | Sha sha; |
ashleymills | 0:714293de3836 | 108 | #endif |
ashleymills | 0:714293de3836 | 109 | #ifndef NO_SHA256 |
ashleymills | 0:714293de3836 | 110 | Sha256 sha256; |
ashleymills | 0:714293de3836 | 111 | #endif |
ashleymills | 0:714293de3836 | 112 | #ifdef CYASSL_SHA384 |
ashleymills | 0:714293de3836 | 113 | Sha384 sha384; |
ashleymills | 0:714293de3836 | 114 | #endif |
ashleymills | 0:714293de3836 | 115 | #ifdef CYASSL_SHA512 |
ashleymills | 0:714293de3836 | 116 | Sha512 sha512; |
ashleymills | 0:714293de3836 | 117 | #endif |
ashleymills | 0:714293de3836 | 118 | } Hash; |
ashleymills | 0:714293de3836 | 119 | |
ashleymills | 0:714293de3836 | 120 | /* Hmac digest */ |
ashleymills | 0:714293de3836 | 121 | typedef struct Hmac { |
ashleymills | 0:714293de3836 | 122 | Hash hash; |
ashleymills | 0:714293de3836 | 123 | word32 ipad[HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/ |
ashleymills | 0:714293de3836 | 124 | word32 opad[HMAC_BLOCK_SIZE / sizeof(word32)]; |
ashleymills | 0:714293de3836 | 125 | word32 innerHash[INNER_HASH_SIZE / sizeof(word32)]; /* max size */ |
ashleymills | 0:714293de3836 | 126 | byte macType; /* md5 sha or sha256 */ |
ashleymills | 0:714293de3836 | 127 | byte innerHashKeyed; /* keyed flag */ |
ashleymills | 0:714293de3836 | 128 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 129 | word16 keyLen; /* hmac key length */ |
ashleymills | 0:714293de3836 | 130 | word16 dataLen; |
ashleymills | 0:714293de3836 | 131 | HashType type; /* hmac key type */ |
ashleymills | 0:714293de3836 | 132 | int devId; /* nitrox device id */ |
ashleymills | 0:714293de3836 | 133 | word32 magic; /* using cavium magic */ |
ashleymills | 0:714293de3836 | 134 | word64 contextHandle; /* nitrox context memory handle */ |
ashleymills | 0:714293de3836 | 135 | byte* data; /* buffered input data for one call */ |
ashleymills | 0:714293de3836 | 136 | #endif |
ashleymills | 0:714293de3836 | 137 | } Hmac; |
ashleymills | 0:714293de3836 | 138 | |
ashleymills | 0:714293de3836 | 139 | |
ashleymills | 0:714293de3836 | 140 | /* does init */ |
ashleymills | 0:714293de3836 | 141 | CYASSL_API void HmacSetKey(Hmac*, int type, const byte* key, word32 keySz); |
ashleymills | 0:714293de3836 | 142 | CYASSL_API void HmacUpdate(Hmac*, const byte*, word32); |
ashleymills | 0:714293de3836 | 143 | CYASSL_API void HmacFinal(Hmac*, byte*); |
ashleymills | 0:714293de3836 | 144 | |
ashleymills | 0:714293de3836 | 145 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 146 | CYASSL_API int HmacInitCavium(Hmac*, int); |
ashleymills | 0:714293de3836 | 147 | CYASSL_API void HmacFreeCavium(Hmac*); |
ashleymills | 0:714293de3836 | 148 | #endif |
ashleymills | 0:714293de3836 | 149 | |
ashleymills | 0:714293de3836 | 150 | |
ashleymills | 0:714293de3836 | 151 | #ifdef __cplusplus |
ashleymills | 0:714293de3836 | 152 | } /* extern "C" */ |
ashleymills | 0:714293de3836 | 153 | #endif |
ashleymills | 0:714293de3836 | 154 | |
ashleymills | 0:714293de3836 | 155 | #endif /* CTAO_CRYPT_HMAC_H */ |
ashleymills | 0:714293de3836 | 156 | |
ashleymills | 0:714293de3836 | 157 | #endif /* NO_HMAC */ |
ashleymills | 0:714293de3836 | 158 |