cya_u

Fork of CyaSSL-forEncrypt by Mobius IoT

Committer:
vbahl2
Date:
Wed May 10 18:20:47 2017 +0000
Revision:
2:d0516dc143b1
Parent:
0:5045d2638c29
updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
toddouska 0:5045d2638c29 1 /* hmac.c
toddouska 0:5045d2638c29 2 *
toddouska 0:5045d2638c29 3 * Copyright (C) 2006-2009 Sawtooth Consulting Ltd.
toddouska 0:5045d2638c29 4 *
toddouska 0:5045d2638c29 5 * This file is part of CyaSSL.
toddouska 0:5045d2638c29 6 *
toddouska 0:5045d2638c29 7 * CyaSSL is free software; you can redistribute it and/or modify
toddouska 0:5045d2638c29 8 * it under the terms of the GNU General Public License as published by
toddouska 0:5045d2638c29 9 * the Free Software Foundation; either version 2 of the License, or
toddouska 0:5045d2638c29 10 * (at your option) any later version.
toddouska 0:5045d2638c29 11 *
toddouska 0:5045d2638c29 12 * CyaSSL is distributed in the hope that it will be useful,
toddouska 0:5045d2638c29 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
toddouska 0:5045d2638c29 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
toddouska 0:5045d2638c29 15 * GNU General Public License for more details.
toddouska 0:5045d2638c29 16 *
toddouska 0:5045d2638c29 17 * You should have received a copy of the GNU General Public License
toddouska 0:5045d2638c29 18 * along with this program; if not, write to the Free Software
toddouska 0:5045d2638c29 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
toddouska 0:5045d2638c29 20 */
toddouska 0:5045d2638c29 21
toddouska 0:5045d2638c29 22
toddouska 0:5045d2638c29 23 #ifndef NO_HMAC
toddouska 0:5045d2638c29 24
toddouska 0:5045d2638c29 25 #include "ctc_hmac.h"
toddouska 0:5045d2638c29 26
toddouska 0:5045d2638c29 27
toddouska 0:5045d2638c29 28
toddouska 0:5045d2638c29 29 static int InitHmac(Hmac* hmac, int type)
toddouska 0:5045d2638c29 30 {
toddouska 0:5045d2638c29 31 hmac->innerHashKeyed = 0;
toddouska 0:5045d2638c29 32 hmac->macType = type;
toddouska 0:5045d2638c29 33
toddouska 0:5045d2638c29 34 if (!(type == MD5 || type == SHA || type == SHA256))
toddouska 0:5045d2638c29 35 return -1;
toddouska 0:5045d2638c29 36
toddouska 0:5045d2638c29 37 if (type == MD5)
toddouska 0:5045d2638c29 38 InitMd5(&hmac->hash.md5);
toddouska 0:5045d2638c29 39 else if (type == SHA)
toddouska 0:5045d2638c29 40 InitSha(&hmac->hash.sha);
toddouska 0:5045d2638c29 41 #ifndef NO_SHA256
toddouska 0:5045d2638c29 42 else if (type == SHA256)
toddouska 0:5045d2638c29 43 InitSha256(&hmac->hash.sha256);
toddouska 0:5045d2638c29 44 #endif
toddouska 0:5045d2638c29 45
toddouska 0:5045d2638c29 46 return 0;
toddouska 0:5045d2638c29 47 }
toddouska 0:5045d2638c29 48
toddouska 0:5045d2638c29 49
toddouska 0:5045d2638c29 50 void HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
toddouska 0:5045d2638c29 51 {
toddouska 0:5045d2638c29 52 byte* ip = (byte*) hmac->ipad;
toddouska 0:5045d2638c29 53 byte* op = (byte*) hmac->opad;
toddouska 0:5045d2638c29 54 word32 i;
toddouska 0:5045d2638c29 55
toddouska 0:5045d2638c29 56 InitHmac(hmac, type);
toddouska 0:5045d2638c29 57
toddouska 0:5045d2638c29 58 if (length <= HMAC_BLOCK_SIZE)
toddouska 0:5045d2638c29 59 XMEMCPY(ip, key, length);
toddouska 0:5045d2638c29 60 else {
toddouska 0:5045d2638c29 61 if (hmac->macType == MD5) {
toddouska 0:5045d2638c29 62 Md5Update(&hmac->hash.md5, key, length);
toddouska 0:5045d2638c29 63 Md5Final(&hmac->hash.md5, ip);
toddouska 0:5045d2638c29 64 length = MD5_DIGEST_SIZE;
toddouska 0:5045d2638c29 65 }
toddouska 0:5045d2638c29 66 else if (hmac->macType == SHA) {
toddouska 0:5045d2638c29 67 ShaUpdate(&hmac->hash.sha, key, length);
toddouska 0:5045d2638c29 68 ShaFinal(&hmac->hash.sha, ip);
toddouska 0:5045d2638c29 69 length = SHA_DIGEST_SIZE;
toddouska 0:5045d2638c29 70 }
toddouska 0:5045d2638c29 71 #ifndef NO_SHA256
toddouska 0:5045d2638c29 72 else if (hmac->macType == SHA256) {
toddouska 0:5045d2638c29 73 Sha256Update(&hmac->hash.sha256, key, length);
toddouska 0:5045d2638c29 74 Sha256Final(&hmac->hash.sha256, ip);
toddouska 0:5045d2638c29 75 length = SHA256_DIGEST_SIZE;
toddouska 0:5045d2638c29 76 }
toddouska 0:5045d2638c29 77 #endif
toddouska 0:5045d2638c29 78 }
toddouska 0:5045d2638c29 79 XMEMSET(ip + length, 0, HMAC_BLOCK_SIZE - length);
toddouska 0:5045d2638c29 80
toddouska 0:5045d2638c29 81 for(i = 0; i < HMAC_BLOCK_SIZE; i++) {
toddouska 0:5045d2638c29 82 op[i] = ip[i] ^ OPAD;
toddouska 0:5045d2638c29 83 ip[i] ^= IPAD;
toddouska 0:5045d2638c29 84 }
toddouska 0:5045d2638c29 85 }
toddouska 0:5045d2638c29 86
toddouska 0:5045d2638c29 87
toddouska 0:5045d2638c29 88 static void HmacKeyInnerHash(Hmac* hmac)
toddouska 0:5045d2638c29 89 {
toddouska 0:5045d2638c29 90 if (hmac->macType == MD5)
toddouska 0:5045d2638c29 91 Md5Update(&hmac->hash.md5, (byte*) hmac->ipad, HMAC_BLOCK_SIZE);
toddouska 0:5045d2638c29 92 else if (hmac->macType == SHA)
toddouska 0:5045d2638c29 93 ShaUpdate(&hmac->hash.sha, (byte*) hmac->ipad, HMAC_BLOCK_SIZE);
toddouska 0:5045d2638c29 94 #ifndef NO_SHA256
toddouska 0:5045d2638c29 95 else if (hmac->macType == SHA256)
toddouska 0:5045d2638c29 96 Sha256Update(&hmac->hash.sha256, (byte*) hmac->ipad, HMAC_BLOCK_SIZE);
toddouska 0:5045d2638c29 97 #endif
toddouska 0:5045d2638c29 98
toddouska 0:5045d2638c29 99 hmac->innerHashKeyed = 1;
toddouska 0:5045d2638c29 100 }
toddouska 0:5045d2638c29 101
toddouska 0:5045d2638c29 102
toddouska 0:5045d2638c29 103 void HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
toddouska 0:5045d2638c29 104 {
toddouska 0:5045d2638c29 105 if (!hmac->innerHashKeyed)
toddouska 0:5045d2638c29 106 HmacKeyInnerHash(hmac);
toddouska 0:5045d2638c29 107
toddouska 0:5045d2638c29 108 if (hmac->macType == MD5)
toddouska 0:5045d2638c29 109 Md5Update(&hmac->hash.md5, msg, length);
toddouska 0:5045d2638c29 110 else if (hmac->macType == SHA)
toddouska 0:5045d2638c29 111 ShaUpdate(&hmac->hash.sha, msg, length);
toddouska 0:5045d2638c29 112 #ifndef NO_SHA256
toddouska 0:5045d2638c29 113 else if (hmac->macType == SHA256)
toddouska 0:5045d2638c29 114 Sha256Update(&hmac->hash.sha256, msg, length);
toddouska 0:5045d2638c29 115 #endif
toddouska 0:5045d2638c29 116
toddouska 0:5045d2638c29 117 }
toddouska 0:5045d2638c29 118
toddouska 0:5045d2638c29 119
toddouska 0:5045d2638c29 120 void HmacFinal(Hmac* hmac, byte* hash)
toddouska 0:5045d2638c29 121 {
toddouska 0:5045d2638c29 122 if (!hmac->innerHashKeyed)
toddouska 0:5045d2638c29 123 HmacKeyInnerHash(hmac);
toddouska 0:5045d2638c29 124
toddouska 0:5045d2638c29 125 if (hmac->macType == MD5) {
toddouska 0:5045d2638c29 126 Md5Final(&hmac->hash.md5, (byte*) hmac->innerHash);
toddouska 0:5045d2638c29 127
toddouska 0:5045d2638c29 128 Md5Update(&hmac->hash.md5, (byte*) hmac->opad, HMAC_BLOCK_SIZE);
toddouska 0:5045d2638c29 129 Md5Update(&hmac->hash.md5, (byte*) hmac->innerHash, MD5_DIGEST_SIZE);
toddouska 0:5045d2638c29 130
toddouska 0:5045d2638c29 131 Md5Final(&hmac->hash.md5, hash);
toddouska 0:5045d2638c29 132 }
toddouska 0:5045d2638c29 133 else if (hmac->macType ==SHA) {
toddouska 0:5045d2638c29 134 ShaFinal(&hmac->hash.sha, (byte*) hmac->innerHash);
toddouska 0:5045d2638c29 135
toddouska 0:5045d2638c29 136 ShaUpdate(&hmac->hash.sha, (byte*) hmac->opad, HMAC_BLOCK_SIZE);
toddouska 0:5045d2638c29 137 ShaUpdate(&hmac->hash.sha, (byte*) hmac->innerHash, SHA_DIGEST_SIZE);
toddouska 0:5045d2638c29 138
toddouska 0:5045d2638c29 139 ShaFinal(&hmac->hash.sha, hash);
toddouska 0:5045d2638c29 140 }
toddouska 0:5045d2638c29 141 #ifndef NO_SHA256
toddouska 0:5045d2638c29 142 else if (hmac->macType ==SHA256) {
toddouska 0:5045d2638c29 143 Sha256Final(&hmac->hash.sha256, (byte*) hmac->innerHash);
toddouska 0:5045d2638c29 144
toddouska 0:5045d2638c29 145 Sha256Update(&hmac->hash.sha256, (byte*) hmac->opad, HMAC_BLOCK_SIZE);
toddouska 0:5045d2638c29 146 Sha256Update(&hmac->hash.sha256, (byte*) hmac->innerHash,
toddouska 0:5045d2638c29 147 SHA256_DIGEST_SIZE);
toddouska 0:5045d2638c29 148
toddouska 0:5045d2638c29 149 Sha256Final(&hmac->hash.sha256, hash);
toddouska 0:5045d2638c29 150 }
toddouska 0:5045d2638c29 151 #endif
toddouska 0:5045d2638c29 152
toddouska 0:5045d2638c29 153 hmac->innerHashKeyed = 0;
toddouska 0:5045d2638c29 154 }
toddouska 0:5045d2638c29 155
toddouska 0:5045d2638c29 156
toddouska 0:5045d2638c29 157 #endif /* NO_HMAC */
toddouska 0:5045d2638c29 158