Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
wolfSSL
Date:
Tue May 02 08:44:26 2017 +0000
Revision:
6:fa3bd0ca5896
wolfSSL3.10.2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 6:fa3bd0ca5896 1 /* cmac.c
wolfSSL 6:fa3bd0ca5896 2 *
wolfSSL 6:fa3bd0ca5896 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 6:fa3bd0ca5896 4 *
wolfSSL 6:fa3bd0ca5896 5 * This file is part of wolfSSL.
wolfSSL 6:fa3bd0ca5896 6 *
wolfSSL 6:fa3bd0ca5896 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 6:fa3bd0ca5896 8 * it under the terms of the GNU General Public License as published by
wolfSSL 6:fa3bd0ca5896 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 6:fa3bd0ca5896 10 * (at your option) any later version.
wolfSSL 6:fa3bd0ca5896 11 *
wolfSSL 6:fa3bd0ca5896 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 6:fa3bd0ca5896 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 6:fa3bd0ca5896 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 6:fa3bd0ca5896 15 * GNU General Public License for more details.
wolfSSL 6:fa3bd0ca5896 16 *
wolfSSL 6:fa3bd0ca5896 17 * You should have received a copy of the GNU General Public License
wolfSSL 6:fa3bd0ca5896 18 * along with this program; if not, write to the Free Software
wolfSSL 6:fa3bd0ca5896 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 6:fa3bd0ca5896 20 */
wolfSSL 6:fa3bd0ca5896 21
wolfSSL 6:fa3bd0ca5896 22
wolfSSL 6:fa3bd0ca5896 23 #ifdef HAVE_CONFIG_H
wolfSSL 6:fa3bd0ca5896 24 #include <config.h>
wolfSSL 6:fa3bd0ca5896 25 #endif
wolfSSL 6:fa3bd0ca5896 26
wolfSSL 6:fa3bd0ca5896 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 6:fa3bd0ca5896 28
wolfSSL 6:fa3bd0ca5896 29 #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
wolfSSL 6:fa3bd0ca5896 30
wolfSSL 6:fa3bd0ca5896 31 #ifdef NO_INLINE
wolfSSL 6:fa3bd0ca5896 32 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 6:fa3bd0ca5896 33 #else
wolfSSL 6:fa3bd0ca5896 34 #define WOLFSSL_MISC_INCLUDED
wolfSSL 6:fa3bd0ca5896 35 #include <wolfcrypt/src/misc.c>
wolfSSL 6:fa3bd0ca5896 36 #endif
wolfSSL 6:fa3bd0ca5896 37
wolfSSL 6:fa3bd0ca5896 38 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 6:fa3bd0ca5896 39 #include <wolfssl/wolfcrypt/aes.h>
wolfSSL 6:fa3bd0ca5896 40 #include <wolfssl/wolfcrypt/cmac.h>
wolfSSL 6:fa3bd0ca5896 41
wolfSSL 6:fa3bd0ca5896 42
wolfSSL 6:fa3bd0ca5896 43 static void ShiftAndXorRb(byte* out, byte* in)
wolfSSL 6:fa3bd0ca5896 44 {
wolfSSL 6:fa3bd0ca5896 45 int i, j, xorRb;
wolfSSL 6:fa3bd0ca5896 46 int mask = 0, last = 0;
wolfSSL 6:fa3bd0ca5896 47 byte Rb = 0x87;
wolfSSL 6:fa3bd0ca5896 48
wolfSSL 6:fa3bd0ca5896 49 xorRb = (in[0] & 0x80) != 0;
wolfSSL 6:fa3bd0ca5896 50
wolfSSL 6:fa3bd0ca5896 51 for (i = 1, j = AES_BLOCK_SIZE - 1; i <= AES_BLOCK_SIZE; i++, j--) {
wolfSSL 6:fa3bd0ca5896 52 last = (in[j] & 0x80) ? 1 : 0;
wolfSSL 6:fa3bd0ca5896 53 out[j] = (in[j] << 1) | mask;
wolfSSL 6:fa3bd0ca5896 54 mask = last;
wolfSSL 6:fa3bd0ca5896 55 if (xorRb) {
wolfSSL 6:fa3bd0ca5896 56 out[j] ^= Rb;
wolfSSL 6:fa3bd0ca5896 57 Rb = 0;
wolfSSL 6:fa3bd0ca5896 58 }
wolfSSL 6:fa3bd0ca5896 59 }
wolfSSL 6:fa3bd0ca5896 60 }
wolfSSL 6:fa3bd0ca5896 61
wolfSSL 6:fa3bd0ca5896 62
wolfSSL 6:fa3bd0ca5896 63 int wc_InitCmac(Cmac* cmac, const byte* key, word32 keySz,
wolfSSL 6:fa3bd0ca5896 64 int type, void* unused)
wolfSSL 6:fa3bd0ca5896 65 {
wolfSSL 6:fa3bd0ca5896 66 int ret;
wolfSSL 6:fa3bd0ca5896 67
wolfSSL 6:fa3bd0ca5896 68 (void)unused;
wolfSSL 6:fa3bd0ca5896 69
wolfSSL 6:fa3bd0ca5896 70 if (cmac == NULL || key == NULL || keySz == 0 || type != WC_CMAC_AES)
wolfSSL 6:fa3bd0ca5896 71 return BAD_FUNC_ARG;
wolfSSL 6:fa3bd0ca5896 72
wolfSSL 6:fa3bd0ca5896 73 XMEMSET(cmac, 0, sizeof(Cmac));
wolfSSL 6:fa3bd0ca5896 74 ret = wc_AesSetKey(&cmac->aes, key, keySz, NULL, AES_ENCRYPTION);
wolfSSL 6:fa3bd0ca5896 75 if (ret == 0) {
wolfSSL 6:fa3bd0ca5896 76 byte l[AES_BLOCK_SIZE];
wolfSSL 6:fa3bd0ca5896 77
wolfSSL 6:fa3bd0ca5896 78 XMEMSET(l, 0, AES_BLOCK_SIZE);
wolfSSL 6:fa3bd0ca5896 79 wc_AesEncryptDirect(&cmac->aes, l, l);
wolfSSL 6:fa3bd0ca5896 80 ShiftAndXorRb(cmac->k1, l);
wolfSSL 6:fa3bd0ca5896 81 ShiftAndXorRb(cmac->k2, cmac->k1);
wolfSSL 6:fa3bd0ca5896 82 ForceZero(l, AES_BLOCK_SIZE);
wolfSSL 6:fa3bd0ca5896 83 }
wolfSSL 6:fa3bd0ca5896 84 return ret;
wolfSSL 6:fa3bd0ca5896 85 }
wolfSSL 6:fa3bd0ca5896 86
wolfSSL 6:fa3bd0ca5896 87
wolfSSL 6:fa3bd0ca5896 88 int wc_CmacUpdate(Cmac* cmac, const byte* in, word32 inSz)
wolfSSL 6:fa3bd0ca5896 89 {
wolfSSL 6:fa3bd0ca5896 90 if ((cmac == NULL) || (in == NULL && inSz != 0))
wolfSSL 6:fa3bd0ca5896 91 return BAD_FUNC_ARG;
wolfSSL 6:fa3bd0ca5896 92
wolfSSL 6:fa3bd0ca5896 93 while (inSz != 0) {
wolfSSL 6:fa3bd0ca5896 94 word32 add = min(inSz, AES_BLOCK_SIZE - cmac->bufferSz);
wolfSSL 6:fa3bd0ca5896 95 XMEMCPY(&cmac->buffer[cmac->bufferSz], in, add);
wolfSSL 6:fa3bd0ca5896 96
wolfSSL 6:fa3bd0ca5896 97 cmac->bufferSz += add;
wolfSSL 6:fa3bd0ca5896 98 in += add;
wolfSSL 6:fa3bd0ca5896 99 inSz -= add;
wolfSSL 6:fa3bd0ca5896 100
wolfSSL 6:fa3bd0ca5896 101 if (cmac->bufferSz == AES_BLOCK_SIZE && inSz != 0) {
wolfSSL 6:fa3bd0ca5896 102 if (cmac->totalSz != 0)
wolfSSL 6:fa3bd0ca5896 103 xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
wolfSSL 6:fa3bd0ca5896 104 wc_AesEncryptDirect(&cmac->aes,
wolfSSL 6:fa3bd0ca5896 105 cmac->digest,
wolfSSL 6:fa3bd0ca5896 106 cmac->buffer);
wolfSSL 6:fa3bd0ca5896 107 cmac->totalSz += AES_BLOCK_SIZE;
wolfSSL 6:fa3bd0ca5896 108 cmac->bufferSz = 0;
wolfSSL 6:fa3bd0ca5896 109 }
wolfSSL 6:fa3bd0ca5896 110 }
wolfSSL 6:fa3bd0ca5896 111
wolfSSL 6:fa3bd0ca5896 112 return 0;
wolfSSL 6:fa3bd0ca5896 113 }
wolfSSL 6:fa3bd0ca5896 114
wolfSSL 6:fa3bd0ca5896 115
wolfSSL 6:fa3bd0ca5896 116 int wc_CmacFinal(Cmac* cmac, byte* out, word32* outSz)
wolfSSL 6:fa3bd0ca5896 117 {
wolfSSL 6:fa3bd0ca5896 118 const byte* subKey;
wolfSSL 6:fa3bd0ca5896 119
wolfSSL 6:fa3bd0ca5896 120 if (cmac == NULL || out == NULL)
wolfSSL 6:fa3bd0ca5896 121 return BAD_FUNC_ARG;
wolfSSL 6:fa3bd0ca5896 122
wolfSSL 6:fa3bd0ca5896 123 if (outSz != NULL && *outSz < AES_BLOCK_SIZE)
wolfSSL 6:fa3bd0ca5896 124 return BUFFER_E;
wolfSSL 6:fa3bd0ca5896 125
wolfSSL 6:fa3bd0ca5896 126 if (cmac->bufferSz == AES_BLOCK_SIZE) {
wolfSSL 6:fa3bd0ca5896 127 subKey = cmac->k1;
wolfSSL 6:fa3bd0ca5896 128 }
wolfSSL 6:fa3bd0ca5896 129 else {
wolfSSL 6:fa3bd0ca5896 130 word32 remainder = AES_BLOCK_SIZE - cmac->bufferSz;
wolfSSL 6:fa3bd0ca5896 131
wolfSSL 6:fa3bd0ca5896 132 if (remainder == 0)
wolfSSL 6:fa3bd0ca5896 133 remainder = AES_BLOCK_SIZE;
wolfSSL 6:fa3bd0ca5896 134
wolfSSL 6:fa3bd0ca5896 135 if (remainder > 1)
wolfSSL 6:fa3bd0ca5896 136 XMEMSET(cmac->buffer + AES_BLOCK_SIZE - remainder, 0, remainder);
wolfSSL 6:fa3bd0ca5896 137 cmac->buffer[AES_BLOCK_SIZE - remainder] = 0x80;
wolfSSL 6:fa3bd0ca5896 138 subKey = cmac->k2;
wolfSSL 6:fa3bd0ca5896 139 }
wolfSSL 6:fa3bd0ca5896 140 xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
wolfSSL 6:fa3bd0ca5896 141 xorbuf(cmac->buffer, subKey, AES_BLOCK_SIZE);
wolfSSL 6:fa3bd0ca5896 142 wc_AesEncryptDirect(&cmac->aes, out, cmac->buffer);
wolfSSL 6:fa3bd0ca5896 143
wolfSSL 6:fa3bd0ca5896 144 if (outSz != NULL)
wolfSSL 6:fa3bd0ca5896 145 *outSz = AES_BLOCK_SIZE;
wolfSSL 6:fa3bd0ca5896 146 ForceZero(cmac, sizeof(Cmac));
wolfSSL 6:fa3bd0ca5896 147
wolfSSL 6:fa3bd0ca5896 148 return 0;
wolfSSL 6:fa3bd0ca5896 149 }
wolfSSL 6:fa3bd0ca5896 150
wolfSSL 6:fa3bd0ca5896 151
wolfSSL 6:fa3bd0ca5896 152 int wc_AesCmacGenerate(byte* out, word32* outSz,
wolfSSL 6:fa3bd0ca5896 153 const byte* in, word32 inSz,
wolfSSL 6:fa3bd0ca5896 154 const byte* key, word32 keySz)
wolfSSL 6:fa3bd0ca5896 155 {
wolfSSL 6:fa3bd0ca5896 156 Cmac cmac;
wolfSSL 6:fa3bd0ca5896 157 int ret;
wolfSSL 6:fa3bd0ca5896 158
wolfSSL 6:fa3bd0ca5896 159 if (out == NULL || (in == NULL && inSz > 0) || key == NULL || keySz == 0)
wolfSSL 6:fa3bd0ca5896 160 return BAD_FUNC_ARG;
wolfSSL 6:fa3bd0ca5896 161
wolfSSL 6:fa3bd0ca5896 162 ret = wc_InitCmac(&cmac, key, keySz, WC_CMAC_AES, NULL);
wolfSSL 6:fa3bd0ca5896 163 if (ret != 0)
wolfSSL 6:fa3bd0ca5896 164 return ret;
wolfSSL 6:fa3bd0ca5896 165
wolfSSL 6:fa3bd0ca5896 166 ret = wc_CmacUpdate(&cmac, in, inSz);
wolfSSL 6:fa3bd0ca5896 167 if (ret != 0)
wolfSSL 6:fa3bd0ca5896 168 return ret;
wolfSSL 6:fa3bd0ca5896 169
wolfSSL 6:fa3bd0ca5896 170 ret = wc_CmacFinal(&cmac, out, outSz);
wolfSSL 6:fa3bd0ca5896 171 if (ret != 0)
wolfSSL 6:fa3bd0ca5896 172 return ret;
wolfSSL 6:fa3bd0ca5896 173
wolfSSL 6:fa3bd0ca5896 174 return 0;
wolfSSL 6:fa3bd0ca5896 175 }
wolfSSL 6:fa3bd0ca5896 176
wolfSSL 6:fa3bd0ca5896 177
wolfSSL 6:fa3bd0ca5896 178 int wc_AesCmacVerify(const byte* check, word32 checkSz,
wolfSSL 6:fa3bd0ca5896 179 const byte* in, word32 inSz,
wolfSSL 6:fa3bd0ca5896 180 const byte* key, word32 keySz)
wolfSSL 6:fa3bd0ca5896 181 {
wolfSSL 6:fa3bd0ca5896 182 byte a[AES_BLOCK_SIZE];
wolfSSL 6:fa3bd0ca5896 183 word32 aSz = sizeof(a);
wolfSSL 6:fa3bd0ca5896 184 int result;
wolfSSL 6:fa3bd0ca5896 185 int compareRet;
wolfSSL 6:fa3bd0ca5896 186
wolfSSL 6:fa3bd0ca5896 187 if (check == NULL || checkSz == 0 || (in == NULL && inSz != 0) ||
wolfSSL 6:fa3bd0ca5896 188 key == NULL || keySz == 0)
wolfSSL 6:fa3bd0ca5896 189
wolfSSL 6:fa3bd0ca5896 190 return BAD_FUNC_ARG;
wolfSSL 6:fa3bd0ca5896 191
wolfSSL 6:fa3bd0ca5896 192 XMEMSET(a, 0, aSz);
wolfSSL 6:fa3bd0ca5896 193 result = wc_AesCmacGenerate(a, &aSz, in, inSz, key, keySz);
wolfSSL 6:fa3bd0ca5896 194 compareRet = ConstantCompare(check, a, min(checkSz, aSz));
wolfSSL 6:fa3bd0ca5896 195
wolfSSL 6:fa3bd0ca5896 196 if (result == 0)
wolfSSL 6:fa3bd0ca5896 197 result = compareRet ? 1 : 0;
wolfSSL 6:fa3bd0ca5896 198
wolfSSL 6:fa3bd0ca5896 199 return result;
wolfSSL 6:fa3bd0ca5896 200 }
wolfSSL 6:fa3bd0ca5896 201
wolfSSL 6:fa3bd0ca5896 202
wolfSSL 6:fa3bd0ca5896 203 #endif /* WOLFSSL_CMAC && NO_AES && WOLFSSL_AES_DIRECT */
wolfSSL 6:fa3bd0ca5896 204