Added Md5GetHash for BuildCertHashes

This commit is contained in:
Takashi Kojo
2015-05-21 13:42:02 +09:00
parent 1a315fd89e
commit b5654092ec
9 changed files with 3045 additions and 2860 deletions

View File

@@ -191,6 +191,8 @@ if BUILD_PKCS7
src_libwolfssl_la_SOURCES += wolfcrypt/src/pkcs7.c src_libwolfssl_la_SOURCES += wolfcrypt/src/pkcs7.c
endif endif
src_libwolfssl_la_SOURCES += wolfcrypt/src/port/ti/ti-hash.c
# ssl files # ssl files
src_libwolfssl_la_SOURCES += \ src_libwolfssl_la_SOURCES += \
src/internal.c \ src/internal.c \

View File

@@ -6925,14 +6925,14 @@ static int BuildCertHashes(WOLFSSL* ssl, Hashes* hashes)
if (ssl->options.tls) { if (ssl->options.tls) {
#if ! defined( NO_OLD_TLS ) #if ! defined( NO_OLD_TLS )
wc_Md5Final(&ssl->hsHashes->hashMd5, hashes->md5); wc_Md5GetHash(&ssl->hsHashes->hashMd5, hashes->md5);
wc_ShaFinal(&ssl->hsHashes->hashSha, hashes->sha); wc_ShaGetHash(&ssl->hsHashes->hashSha, hashes->sha);
#endif #endif
if (IsAtLeastTLSv1_2(ssl)) { if (IsAtLeastTLSv1_2(ssl)) {
int ret; int ret;
#ifndef NO_SHA256 #ifndef NO_SHA256
ret = wc_Sha256Final(&ssl->hsHashes->hashSha256,hashes->sha256); ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256,hashes->sha256);
if (ret != 0) if (ret != 0)
return ret; return ret;
#endif #endif

View File

@@ -1,392 +1,426 @@
/* md5.c /* md5.c
* *
* Copyright (C) 2006-2015 wolfSSL Inc. * Copyright (C) 2006-2015 wolfSSL Inc.
* *
* This file is part of wolfSSL. (formerly known as CyaSSL) * This file is part of wolfSSL. (formerly known as CyaSSL)
* *
* wolfSSL is free software; you can redistribute it and/or modify * wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* wolfSSL is distributed in the hope that it will be useful, * wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include <config.h> #include <config.h>
#endif #endif
#include <wolfssl/wolfcrypt/settings.h> #include <wolfssl/wolfcrypt/settings.h>
#if !defined(NO_MD5) #if !defined(NO_MD5) && !defined(WOLFSSL_TI_HASH)
#ifdef WOLFSSL_PIC32MZ_HASH #ifdef WOLFSSL_PIC32MZ_HASH
#define wc_InitMd5 wc_InitMd5_sw #define wc_InitMd5 wc_InitMd5_sw
#define wc_Md5Update wc_Md5Update_sw #define wc_Md5Update wc_Md5Update_sw
#define wc_Md5Final wc_Md5Final_sw #define wc_Md5Final wc_Md5Final_sw
#endif #endif
#include <wolfssl/wolfcrypt/md5.h> #include <wolfssl/wolfcrypt/md5.h>
#include <wolfssl/wolfcrypt/error-crypt.h> #include <wolfssl/wolfcrypt/error-crypt.h>
#ifdef NO_INLINE #ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h> #include <wolfssl/wolfcrypt/misc.h>
#else #else
#include <wolfcrypt/src/misc.c> #include <wolfcrypt/src/misc.c>
#endif #endif
#ifdef FREESCALE_MMCAU #ifdef FREESCALE_MMCAU
#include "cau_api.h" #include "cau_api.h"
#define XTRANSFORM(S,B) cau_md5_hash_n((B), 1, (unsigned char*)(S)->digest) #define XTRANSFORM(S,B) cau_md5_hash_n((B), 1, (unsigned char*)(S)->digest)
#else #else
#define XTRANSFORM(S,B) Transform((S)) #define XTRANSFORM(S,B) Transform((S))
#endif #endif
#ifdef STM32F2_HASH #ifdef STM32F2_HASH
/* /*
* STM32F2 hardware MD5 support through the STM32F2 standard peripheral * STM32F2 hardware MD5 support through the STM32F2 standard peripheral
* library. Documentation located in STM32F2xx Standard Peripheral Library * library. Documentation located in STM32F2xx Standard Peripheral Library
* document (See note in README). * document (See note in README).
*/ */
#include "stm32f2xx.h" #include "stm32f2xx.h"
void wc_InitMd5(Md5* md5) void wc_InitMd5(Md5* md5)
{ {
/* STM32F2 struct notes: /* STM32F2 struct notes:
* md5->buffer = first 4 bytes used to hold partial block if needed * md5->buffer = first 4 bytes used to hold partial block if needed
* md5->buffLen = num bytes currently stored in md5->buffer * md5->buffLen = num bytes currently stored in md5->buffer
* md5->loLen = num bytes that have been written to STM32 FIFO * md5->loLen = num bytes that have been written to STM32 FIFO
*/ */
XMEMSET(md5->buffer, 0, MD5_REG_SIZE); XMEMSET(md5->buffer, 0, MD5_REG_SIZE);
md5->buffLen = 0; md5->buffLen = 0;
md5->loLen = 0; md5->loLen = 0;
/* initialize HASH peripheral */ /* initialize HASH peripheral */
HASH_DeInit(); HASH_DeInit();
/* configure algo used, algo mode, datatype */ /* configure algo used, algo mode, datatype */
HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE); HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
HASH->CR |= (HASH_AlgoSelection_MD5 | HASH_AlgoMode_HASH HASH->CR |= (HASH_AlgoSelection_MD5 | HASH_AlgoMode_HASH
| HASH_DataType_8b); | HASH_DataType_8b);
/* reset HASH processor */ /* reset HASH processor */
HASH->CR |= HASH_CR_INIT; HASH->CR |= HASH_CR_INIT;
} }
void wc_Md5Update(Md5* md5, const byte* data, word32 len) void wc_Md5Update(Md5* md5, const byte* data, word32 len)
{ {
word32 i = 0; word32 i = 0;
word32 fill = 0; word32 fill = 0;
word32 diff = 0; word32 diff = 0;
/* if saved partial block is available */ /* if saved partial block is available */
if (md5->buffLen > 0) { if (md5->buffLen > 0) {
fill = 4 - md5->buffLen; fill = 4 - md5->buffLen;
/* if enough data to fill, fill and push to FIFO */ /* if enough data to fill, fill and push to FIFO */
if (fill <= len) { if (fill <= len) {
XMEMCPY((byte*)md5->buffer + md5->buffLen, data, fill); XMEMCPY((byte*)md5->buffer + md5->buffLen, data, fill);
HASH_DataIn(*(uint32_t*)md5->buffer); HASH_DataIn(*(uint32_t*)md5->buffer);
data += fill; data += fill;
len -= fill; len -= fill;
md5->loLen += 4; md5->loLen += 4;
md5->buffLen = 0; md5->buffLen = 0;
} else { } else {
/* append partial to existing stored block */ /* append partial to existing stored block */
XMEMCPY((byte*)md5->buffer + md5->buffLen, data, len); XMEMCPY((byte*)md5->buffer + md5->buffLen, data, len);
md5->buffLen += len; md5->buffLen += len;
return; return;
} }
} }
/* write input block in the IN FIFO */ /* write input block in the IN FIFO */
for (i = 0; i < len; i += 4) for (i = 0; i < len; i += 4)
{ {
diff = len - i; diff = len - i;
if (diff < 4) { if (diff < 4) {
/* store incomplete last block, not yet in FIFO */ /* store incomplete last block, not yet in FIFO */
XMEMSET(md5->buffer, 0, MD5_REG_SIZE); XMEMSET(md5->buffer, 0, MD5_REG_SIZE);
XMEMCPY((byte*)md5->buffer, data, diff); XMEMCPY((byte*)md5->buffer, data, diff);
md5->buffLen = diff; md5->buffLen = diff;
} else { } else {
HASH_DataIn(*(uint32_t*)data); HASH_DataIn(*(uint32_t*)data);
data+=4; data+=4;
} }
} }
/* keep track of total data length thus far */ /* keep track of total data length thus far */
md5->loLen += (len - md5->buffLen); md5->loLen += (len - md5->buffLen);
} }
void wc_Md5Final(Md5* md5, byte* hash) void wc_Md5Final(Md5* md5, byte* hash)
{ {
__IO uint16_t nbvalidbitsdata = 0; __IO uint16_t nbvalidbitsdata = 0;
/* finish reading any trailing bytes into FIFO */ /* finish reading any trailing bytes into FIFO */
if (md5->buffLen > 0) { if (md5->buffLen > 0) {
HASH_DataIn(*(uint32_t*)md5->buffer); HASH_DataIn(*(uint32_t*)md5->buffer);
md5->loLen += md5->buffLen; md5->loLen += md5->buffLen;
} }
/* calculate number of valid bits in last word of input data */ /* calculate number of valid bits in last word of input data */
nbvalidbitsdata = 8 * (md5->loLen % MD5_REG_SIZE); nbvalidbitsdata = 8 * (md5->loLen % MD5_REG_SIZE);
/* configure number of valid bits in last word of the data */ /* configure number of valid bits in last word of the data */
HASH_SetLastWordValidBitsNbr(nbvalidbitsdata); HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
/* start HASH processor */ /* start HASH processor */
HASH_StartDigest(); HASH_StartDigest();
/* wait until Busy flag == RESET */ /* wait until Busy flag == RESET */
while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {} while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {}
/* read message digest */ /* read message digest */
md5->digest[0] = HASH->HR[0]; md5->digest[0] = HASH->HR[0];
md5->digest[1] = HASH->HR[1]; md5->digest[1] = HASH->HR[1];
md5->digest[2] = HASH->HR[2]; md5->digest[2] = HASH->HR[2];
md5->digest[3] = HASH->HR[3]; md5->digest[3] = HASH->HR[3];
ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE); ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE);
XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE); XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE);
wc_InitMd5(md5); /* reset state */ wc_InitMd5(md5); /* reset state */
} }
#else /* CTaoCrypt software implementation */ #elif defined(WOLFSSL_IT_HASH)
#ifndef WOLFSSL_HAVE_MIN /* defined in port/ti_md5.c */
#define WOLFSSL_HAVE_MIN
#else /* CTaoCrypt software implementation */
static INLINE word32 min(word32 a, word32 b)
{ #ifndef WOLFSSL_HAVE_MIN
return a > b ? b : a; #define WOLFSSL_HAVE_MIN
}
static INLINE word32 min(word32 a, word32 b)
#endif /* WOLFSSL_HAVE_MIN */ {
return a > b ? b : a;
}
void wc_InitMd5(Md5* md5)
{ #endif /* WOLFSSL_HAVE_MIN */
md5->digest[0] = 0x67452301L;
md5->digest[1] = 0xefcdab89L; #ifdef TI_HASH_TEST
md5->digest[2] = 0x98badcfeL; void wc_InitMd5_ti(Md5* md5) ;
md5->digest[3] = 0x10325476L; void wc_Md5Update_ti(Md5* md5, const byte* data, word32 len) ;
void wc_Md5Final_ti(Md5* md5, byte* hash) ;
md5->buffLen = 0; #endif
md5->loLen = 0;
md5->hiLen = 0; void wc_InitMd5(Md5* md5)
} {
md5->digest[0] = 0x67452301L;
#ifndef FREESCALE_MMCAU md5->digest[1] = 0xefcdab89L;
md5->digest[2] = 0x98badcfeL;
static void Transform(Md5* md5) md5->digest[3] = 0x10325476L;
{
#define F1(x, y, z) (z ^ (x & (y ^ z))) md5->buffLen = 0;
#define F2(x, y, z) F1(z, x, y) md5->loLen = 0;
#define F3(x, y, z) (x ^ y ^ z) md5->hiLen = 0;
#define F4(x, y, z) (y ^ (x | ~z))
#ifdef TI_HASH_TEST
#define MD5STEP(f, w, x, y, z, data, s) \ wc_InitMd5_ti(md5) ;
w = rotlFixed(w + f(x, y, z) + data, s) + x #endif
}
/* Copy context->state[] to working vars */
word32 a = md5->digest[0]; #ifndef FREESCALE_MMCAU
word32 b = md5->digest[1];
word32 c = md5->digest[2]; static void Transform(Md5* md5)
word32 d = md5->digest[3]; {
#define F1(x, y, z) (z ^ (x & (y ^ z)))
MD5STEP(F1, a, b, c, d, md5->buffer[0] + 0xd76aa478, 7); #define F2(x, y, z) F1(z, x, y)
MD5STEP(F1, d, a, b, c, md5->buffer[1] + 0xe8c7b756, 12); #define F3(x, y, z) (x ^ y ^ z)
MD5STEP(F1, c, d, a, b, md5->buffer[2] + 0x242070db, 17); #define F4(x, y, z) (y ^ (x | ~z))
MD5STEP(F1, b, c, d, a, md5->buffer[3] + 0xc1bdceee, 22);
MD5STEP(F1, a, b, c, d, md5->buffer[4] + 0xf57c0faf, 7); #define MD5STEP(f, w, x, y, z, data, s) \
MD5STEP(F1, d, a, b, c, md5->buffer[5] + 0x4787c62a, 12); w = rotlFixed(w + f(x, y, z) + data, s) + x
MD5STEP(F1, c, d, a, b, md5->buffer[6] + 0xa8304613, 17);
MD5STEP(F1, b, c, d, a, md5->buffer[7] + 0xfd469501, 22); /* Copy context->state[] to working vars */
MD5STEP(F1, a, b, c, d, md5->buffer[8] + 0x698098d8, 7); word32 a = md5->digest[0];
MD5STEP(F1, d, a, b, c, md5->buffer[9] + 0x8b44f7af, 12); word32 b = md5->digest[1];
MD5STEP(F1, c, d, a, b, md5->buffer[10] + 0xffff5bb1, 17); word32 c = md5->digest[2];
MD5STEP(F1, b, c, d, a, md5->buffer[11] + 0x895cd7be, 22); word32 d = md5->digest[3];
MD5STEP(F1, a, b, c, d, md5->buffer[12] + 0x6b901122, 7);
MD5STEP(F1, d, a, b, c, md5->buffer[13] + 0xfd987193, 12); MD5STEP(F1, a, b, c, d, md5->buffer[0] + 0xd76aa478, 7);
MD5STEP(F1, c, d, a, b, md5->buffer[14] + 0xa679438e, 17); MD5STEP(F1, d, a, b, c, md5->buffer[1] + 0xe8c7b756, 12);
MD5STEP(F1, b, c, d, a, md5->buffer[15] + 0x49b40821, 22); MD5STEP(F1, c, d, a, b, md5->buffer[2] + 0x242070db, 17);
MD5STEP(F1, b, c, d, a, md5->buffer[3] + 0xc1bdceee, 22);
MD5STEP(F2, a, b, c, d, md5->buffer[1] + 0xf61e2562, 5); MD5STEP(F1, a, b, c, d, md5->buffer[4] + 0xf57c0faf, 7);
MD5STEP(F2, d, a, b, c, md5->buffer[6] + 0xc040b340, 9); MD5STEP(F1, d, a, b, c, md5->buffer[5] + 0x4787c62a, 12);
MD5STEP(F2, c, d, a, b, md5->buffer[11] + 0x265e5a51, 14); MD5STEP(F1, c, d, a, b, md5->buffer[6] + 0xa8304613, 17);
MD5STEP(F2, b, c, d, a, md5->buffer[0] + 0xe9b6c7aa, 20); MD5STEP(F1, b, c, d, a, md5->buffer[7] + 0xfd469501, 22);
MD5STEP(F2, a, b, c, d, md5->buffer[5] + 0xd62f105d, 5); MD5STEP(F1, a, b, c, d, md5->buffer[8] + 0x698098d8, 7);
MD5STEP(F2, d, a, b, c, md5->buffer[10] + 0x02441453, 9); MD5STEP(F1, d, a, b, c, md5->buffer[9] + 0x8b44f7af, 12);
MD5STEP(F2, c, d, a, b, md5->buffer[15] + 0xd8a1e681, 14); MD5STEP(F1, c, d, a, b, md5->buffer[10] + 0xffff5bb1, 17);
MD5STEP(F2, b, c, d, a, md5->buffer[4] + 0xe7d3fbc8, 20); MD5STEP(F1, b, c, d, a, md5->buffer[11] + 0x895cd7be, 22);
MD5STEP(F2, a, b, c, d, md5->buffer[9] + 0x21e1cde6, 5); MD5STEP(F1, a, b, c, d, md5->buffer[12] + 0x6b901122, 7);
MD5STEP(F2, d, a, b, c, md5->buffer[14] + 0xc33707d6, 9); MD5STEP(F1, d, a, b, c, md5->buffer[13] + 0xfd987193, 12);
MD5STEP(F2, c, d, a, b, md5->buffer[3] + 0xf4d50d87, 14); MD5STEP(F1, c, d, a, b, md5->buffer[14] + 0xa679438e, 17);
MD5STEP(F2, b, c, d, a, md5->buffer[8] + 0x455a14ed, 20); MD5STEP(F1, b, c, d, a, md5->buffer[15] + 0x49b40821, 22);
MD5STEP(F2, a, b, c, d, md5->buffer[13] + 0xa9e3e905, 5);
MD5STEP(F2, d, a, b, c, md5->buffer[2] + 0xfcefa3f8, 9); MD5STEP(F2, a, b, c, d, md5->buffer[1] + 0xf61e2562, 5);
MD5STEP(F2, c, d, a, b, md5->buffer[7] + 0x676f02d9, 14); MD5STEP(F2, d, a, b, c, md5->buffer[6] + 0xc040b340, 9);
MD5STEP(F2, b, c, d, a, md5->buffer[12] + 0x8d2a4c8a, 20); MD5STEP(F2, c, d, a, b, md5->buffer[11] + 0x265e5a51, 14);
MD5STEP(F2, b, c, d, a, md5->buffer[0] + 0xe9b6c7aa, 20);
MD5STEP(F3, a, b, c, d, md5->buffer[5] + 0xfffa3942, 4); MD5STEP(F2, a, b, c, d, md5->buffer[5] + 0xd62f105d, 5);
MD5STEP(F3, d, a, b, c, md5->buffer[8] + 0x8771f681, 11); MD5STEP(F2, d, a, b, c, md5->buffer[10] + 0x02441453, 9);
MD5STEP(F3, c, d, a, b, md5->buffer[11] + 0x6d9d6122, 16); MD5STEP(F2, c, d, a, b, md5->buffer[15] + 0xd8a1e681, 14);
MD5STEP(F3, b, c, d, a, md5->buffer[14] + 0xfde5380c, 23); MD5STEP(F2, b, c, d, a, md5->buffer[4] + 0xe7d3fbc8, 20);
MD5STEP(F3, a, b, c, d, md5->buffer[1] + 0xa4beea44, 4); MD5STEP(F2, a, b, c, d, md5->buffer[9] + 0x21e1cde6, 5);
MD5STEP(F3, d, a, b, c, md5->buffer[4] + 0x4bdecfa9, 11); MD5STEP(F2, d, a, b, c, md5->buffer[14] + 0xc33707d6, 9);
MD5STEP(F3, c, d, a, b, md5->buffer[7] + 0xf6bb4b60, 16); MD5STEP(F2, c, d, a, b, md5->buffer[3] + 0xf4d50d87, 14);
MD5STEP(F3, b, c, d, a, md5->buffer[10] + 0xbebfbc70, 23); MD5STEP(F2, b, c, d, a, md5->buffer[8] + 0x455a14ed, 20);
MD5STEP(F3, a, b, c, d, md5->buffer[13] + 0x289b7ec6, 4); MD5STEP(F2, a, b, c, d, md5->buffer[13] + 0xa9e3e905, 5);
MD5STEP(F3, d, a, b, c, md5->buffer[0] + 0xeaa127fa, 11); MD5STEP(F2, d, a, b, c, md5->buffer[2] + 0xfcefa3f8, 9);
MD5STEP(F3, c, d, a, b, md5->buffer[3] + 0xd4ef3085, 16); MD5STEP(F2, c, d, a, b, md5->buffer[7] + 0x676f02d9, 14);
MD5STEP(F3, b, c, d, a, md5->buffer[6] + 0x04881d05, 23); MD5STEP(F2, b, c, d, a, md5->buffer[12] + 0x8d2a4c8a, 20);
MD5STEP(F3, a, b, c, d, md5->buffer[9] + 0xd9d4d039, 4);
MD5STEP(F3, d, a, b, c, md5->buffer[12] + 0xe6db99e5, 11); MD5STEP(F3, a, b, c, d, md5->buffer[5] + 0xfffa3942, 4);
MD5STEP(F3, c, d, a, b, md5->buffer[15] + 0x1fa27cf8, 16); MD5STEP(F3, d, a, b, c, md5->buffer[8] + 0x8771f681, 11);
MD5STEP(F3, b, c, d, a, md5->buffer[2] + 0xc4ac5665, 23); MD5STEP(F3, c, d, a, b, md5->buffer[11] + 0x6d9d6122, 16);
MD5STEP(F3, b, c, d, a, md5->buffer[14] + 0xfde5380c, 23);
MD5STEP(F4, a, b, c, d, md5->buffer[0] + 0xf4292244, 6); MD5STEP(F3, a, b, c, d, md5->buffer[1] + 0xa4beea44, 4);
MD5STEP(F4, d, a, b, c, md5->buffer[7] + 0x432aff97, 10); MD5STEP(F3, d, a, b, c, md5->buffer[4] + 0x4bdecfa9, 11);
MD5STEP(F4, c, d, a, b, md5->buffer[14] + 0xab9423a7, 15); MD5STEP(F3, c, d, a, b, md5->buffer[7] + 0xf6bb4b60, 16);
MD5STEP(F4, b, c, d, a, md5->buffer[5] + 0xfc93a039, 21); MD5STEP(F3, b, c, d, a, md5->buffer[10] + 0xbebfbc70, 23);
MD5STEP(F4, a, b, c, d, md5->buffer[12] + 0x655b59c3, 6); MD5STEP(F3, a, b, c, d, md5->buffer[13] + 0x289b7ec6, 4);
MD5STEP(F4, d, a, b, c, md5->buffer[3] + 0x8f0ccc92, 10); MD5STEP(F3, d, a, b, c, md5->buffer[0] + 0xeaa127fa, 11);
MD5STEP(F4, c, d, a, b, md5->buffer[10] + 0xffeff47d, 15); MD5STEP(F3, c, d, a, b, md5->buffer[3] + 0xd4ef3085, 16);
MD5STEP(F4, b, c, d, a, md5->buffer[1] + 0x85845dd1, 21); MD5STEP(F3, b, c, d, a, md5->buffer[6] + 0x04881d05, 23);
MD5STEP(F4, a, b, c, d, md5->buffer[8] + 0x6fa87e4f, 6); MD5STEP(F3, a, b, c, d, md5->buffer[9] + 0xd9d4d039, 4);
MD5STEP(F4, d, a, b, c, md5->buffer[15] + 0xfe2ce6e0, 10); MD5STEP(F3, d, a, b, c, md5->buffer[12] + 0xe6db99e5, 11);
MD5STEP(F4, c, d, a, b, md5->buffer[6] + 0xa3014314, 15); MD5STEP(F3, c, d, a, b, md5->buffer[15] + 0x1fa27cf8, 16);
MD5STEP(F4, b, c, d, a, md5->buffer[13] + 0x4e0811a1, 21); MD5STEP(F3, b, c, d, a, md5->buffer[2] + 0xc4ac5665, 23);
MD5STEP(F4, a, b, c, d, md5->buffer[4] + 0xf7537e82, 6);
MD5STEP(F4, d, a, b, c, md5->buffer[11] + 0xbd3af235, 10); MD5STEP(F4, a, b, c, d, md5->buffer[0] + 0xf4292244, 6);
MD5STEP(F4, c, d, a, b, md5->buffer[2] + 0x2ad7d2bb, 15); MD5STEP(F4, d, a, b, c, md5->buffer[7] + 0x432aff97, 10);
MD5STEP(F4, b, c, d, a, md5->buffer[9] + 0xeb86d391, 21); MD5STEP(F4, c, d, a, b, md5->buffer[14] + 0xab9423a7, 15);
MD5STEP(F4, b, c, d, a, md5->buffer[5] + 0xfc93a039, 21);
/* Add the working vars back into digest state[] */ MD5STEP(F4, a, b, c, d, md5->buffer[12] + 0x655b59c3, 6);
md5->digest[0] += a; MD5STEP(F4, d, a, b, c, md5->buffer[3] + 0x8f0ccc92, 10);
md5->digest[1] += b; MD5STEP(F4, c, d, a, b, md5->buffer[10] + 0xffeff47d, 15);
md5->digest[2] += c; MD5STEP(F4, b, c, d, a, md5->buffer[1] + 0x85845dd1, 21);
md5->digest[3] += d; MD5STEP(F4, a, b, c, d, md5->buffer[8] + 0x6fa87e4f, 6);
} MD5STEP(F4, d, a, b, c, md5->buffer[15] + 0xfe2ce6e0, 10);
MD5STEP(F4, c, d, a, b, md5->buffer[6] + 0xa3014314, 15);
#endif /* FREESCALE_MMCAU */ MD5STEP(F4, b, c, d, a, md5->buffer[13] + 0x4e0811a1, 21);
MD5STEP(F4, a, b, c, d, md5->buffer[4] + 0xf7537e82, 6);
MD5STEP(F4, d, a, b, c, md5->buffer[11] + 0xbd3af235, 10);
static INLINE void AddLength(Md5* md5, word32 len) MD5STEP(F4, c, d, a, b, md5->buffer[2] + 0x2ad7d2bb, 15);
{ MD5STEP(F4, b, c, d, a, md5->buffer[9] + 0xeb86d391, 21);
word32 tmp = md5->loLen;
if ( (md5->loLen += len) < tmp) /* Add the working vars back into digest state[] */
md5->hiLen++; /* carry low to high */ md5->digest[0] += a;
} md5->digest[1] += b;
md5->digest[2] += c;
md5->digest[3] += d;
void wc_Md5Update(Md5* md5, const byte* data, word32 len) }
{
/* do block size increments */ #endif /* FREESCALE_MMCAU */
byte* local = (byte*)md5->buffer;
while (len) { static INLINE void AddLength(Md5* md5, word32 len)
word32 add = min(len, MD5_BLOCK_SIZE - md5->buffLen); {
XMEMCPY(&local[md5->buffLen], data, add); word32 tmp = md5->loLen;
if ( (md5->loLen += len) < tmp)
md5->buffLen += add; md5->hiLen++; /* carry low to high */
data += add; }
len -= add;
if (md5->buffLen == MD5_BLOCK_SIZE) { void wc_Md5Update(Md5* md5, const byte* data, word32 len)
#if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) {
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); /* do block size increments */
#endif byte* local = (byte*)md5->buffer;
XTRANSFORM(md5, local);
AddLength(md5, MD5_BLOCK_SIZE); while (len) {
md5->buffLen = 0; word32 add = min(len, MD5_BLOCK_SIZE - md5->buffLen);
} XMEMCPY(&local[md5->buffLen], data, add);
}
} md5->buffLen += add;
data += add;
len -= add;
void wc_Md5Final(Md5* md5, byte* hash)
{ if (md5->buffLen == MD5_BLOCK_SIZE) {
byte* local = (byte*)md5->buffer; #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
AddLength(md5, md5->buffLen); /* before adding pads */ #endif
XTRANSFORM(md5, local);
local[md5->buffLen++] = 0x80; /* add 1 */ AddLength(md5, MD5_BLOCK_SIZE);
md5->buffLen = 0;
/* pad with zeros */ }
if (md5->buffLen > MD5_PAD_SIZE) { }
XMEMSET(&local[md5->buffLen], 0, MD5_BLOCK_SIZE - md5->buffLen); #ifdef TI_HASH_TEST
md5->buffLen += MD5_BLOCK_SIZE - md5->buffLen; wc_Md5Update_ti(md5, data, len) ;
#endif
#if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); }
#endif
XTRANSFORM(md5, local);
md5->buffLen = 0; void wc_Md5Final(Md5* md5, byte* hash)
} {
XMEMSET(&local[md5->buffLen], 0, MD5_PAD_SIZE - md5->buffLen); byte* local = (byte*)md5->buffer;
/* put lengths in bits */ AddLength(md5, md5->buffLen); /* before adding pads */
md5->hiLen = (md5->loLen >> (8*sizeof(md5->loLen) - 3)) +
(md5->hiLen << 3); local[md5->buffLen++] = 0x80; /* add 1 */
md5->loLen = md5->loLen << 3;
/* pad with zeros */
/* store lengths */ if (md5->buffLen > MD5_PAD_SIZE) {
#if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) XMEMSET(&local[md5->buffLen], 0, MD5_BLOCK_SIZE - md5->buffLen);
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); md5->buffLen += MD5_BLOCK_SIZE - md5->buffLen;
#endif
/* ! length ordering dependent on digest endian type ! */ #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
XMEMCPY(&local[MD5_PAD_SIZE], &md5->loLen, sizeof(word32)); ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
XMEMCPY(&local[MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32)); #endif
XTRANSFORM(md5, local);
XTRANSFORM(md5, local); md5->buffLen = 0;
#ifdef BIG_ENDIAN_ORDER }
ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE); XMEMSET(&local[md5->buffLen], 0, MD5_PAD_SIZE - md5->buffLen);
#endif
XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE); /* put lengths in bits */
md5->hiLen = (md5->loLen >> (8*sizeof(md5->loLen) - 3)) +
wc_InitMd5(md5); /* reset state */ (md5->hiLen << 3);
} md5->loLen = md5->loLen << 3;
#endif /* STM32F2_HASH */ /* store lengths */
#if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
int wc_Md5Hash(const byte* data, word32 len, byte* hash) #endif
{ /* ! length ordering dependent on digest endian type ! */
#ifdef WOLFSSL_SMALL_STACK XMEMCPY(&local[MD5_PAD_SIZE], &md5->loLen, sizeof(word32));
Md5* md5; XMEMCPY(&local[MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32));
#else
Md5 md5[1]; XTRANSFORM(md5, local);
#endif #ifdef BIG_ENDIAN_ORDER
ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE);
#ifdef WOLFSSL_SMALL_STACK #endif
md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER); XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE);
if (md5 == NULL)
return MEMORY_E; wc_InitMd5(md5); /* reset state */
#endif
#ifdef TI_HASH_TEST
wc_InitMd5(md5); wc_Md5Final_ti(md5, hash) ;
wc_Md5Update(md5, data, len); #endif
wc_Md5Final(md5, hash); }
#ifdef WOLFSSL_SMALL_STACK #endif /* STM32F2_HASH */
XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
int wc_Md5Hash(const byte* data, word32 len, byte* hash)
return 0; {
} #ifdef WOLFSSL_SMALL_STACK
Md5* md5;
#endif /* NO_MD5 */ #else
Md5 md5[1];
#endif
#ifdef WOLFSSL_SMALL_STACK
md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (md5 == NULL)
return MEMORY_E;
#endif
wc_InitMd5(md5);
wc_Md5Update(md5, data, len);
wc_Md5Final(md5, hash);
#ifdef WOLFSSL_SMALL_STACK
XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return 0;
}
#if defined(WOLFSSL_TI_HASH)||defined(TI_HASH_TEST)
#include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
#endif
void wc_Md5GetHash(Md5* md5, byte* hash)
{
#if defined(WOLFSSL_TI_HASH) || defined(TI_HASH_TEST)
wc_Md5GetHash_ti(md5, hash) ;
#else
Md5 save = *md5 ;
wc_Md5Final(md5, hash) ;
*md5 = save ;
#endif
}
#endif /* NO_MD5 */

View File

@@ -1,4 +1,4 @@
/* port/ti/ti-hash.h /* port/ti/ti-hash.c
* *
* Copyright (C) 2006-2015 wolfSSL Inc. * Copyright (C) 2006-2015 wolfSSL Inc.
* *
@@ -20,12 +20,9 @@
*/ */
#ifndef WOLFSSL_TI_HASH_H
#define WOLFSSL_TI_HASH_H
#include <wolfssl/wolfcrypt/types.h> #include <wolfssl/wolfcrypt/types.h>
#if defined(WOLFSSL_TI_HASH) #if defined(WOLFSSL_TI_HASH)||defined(TI_HASH_TEST)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -41,7 +38,8 @@
#include <wolfssl/wolfcrypt/port/ti/ti-hash.h> #include <wolfssl/wolfcrypt/port/ti/ti-hash.h>
#include <wolfssl/wolfcrypt/port/ti/ti-ccm.h> #include <wolfssl/wolfcrypt/port/ti/ti-ccm.h>
#include <wolfssl/wolfcrypt/logging.h> #include <wolfssl/wolfcrypt/logging.h>
#if !defined(TI_HASH_TEST)
#include "inc/hw_memmap.h" #include "inc/hw_memmap.h"
#include "inc/hw_shamd5.h" #include "inc/hw_shamd5.h"
#include "inc/hw_ints.h" #include "inc/hw_ints.h"
@@ -49,6 +47,7 @@
#include "driverlib/sysctl.h" #include "driverlib/sysctl.h"
#include "driverlib/rom_map.h" #include "driverlib/rom_map.h"
#include "driverlib/rom.h" #include "driverlib/rom.h"
#endif
static int hashInit(wolfssl_TI_Hash *hash) { static int hashInit(wolfssl_TI_Hash *hash) {
hash->used = 0 ; hash->used = 0 ;
@@ -60,6 +59,7 @@ static int hashInit(wolfssl_TI_Hash *hash) {
static int hashUpdate(wolfssl_TI_Hash *hash, const byte* data, word32 len) static int hashUpdate(wolfssl_TI_Hash *hash, const byte* data, word32 len)
{ {
void *p ; void *p ;
if((hash== NULL) || (data == NULL))return BAD_FUNC_ARG; if((hash== NULL) || (data == NULL))return BAD_FUNC_ARG;
if(hash->len < hash->used+len) { if(hash->len < hash->used+len) {
if(hash->msg == NULL) { if(hash->msg == NULL) {
@@ -77,8 +77,10 @@ static int hashUpdate(wolfssl_TI_Hash *hash, const byte* data, word32 len)
} }
static int hashFinal(wolfssl_TI_Hash *hash, byte* result, word32 algo, word32 hsize) static int hashFinal(wolfssl_TI_Hash *hash, byte* result, word32 algo, word32 hsize)
{ {
#if !defined(TI_HASH_TEST)
uint32_t h[16] ; uint32_t h[16] ;
wolfSSL_TI_lockCCM() ; wolfSSL_TI_lockCCM() ;
ROM_SHAMD5Reset(SHAMD5_BASE); ROM_SHAMD5Reset(SHAMD5_BASE);
ROM_SHAMD5ConfigSet(SHAMD5_BASE, algo); ROM_SHAMD5ConfigSet(SHAMD5_BASE, algo);
@@ -86,11 +88,38 @@ static int hashFinal(wolfssl_TI_Hash *hash, byte* result, word32 algo, word32 hs
(uint32_t *)hash->msg, hash->used, h); (uint32_t *)hash->msg, hash->used, h);
XMEMCPY(result, h, hsize) ; XMEMCPY(result, h, hsize) ;
wolfSSL_TI_unlockCCM() ; wolfSSL_TI_unlockCCM() ;
#else
(void) result ;
(void) algo ;
(void) hsize ;
#endif
XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER);
hashInit(hash) ; hashInit(hash) ;
return 0 ; return 0 ;
} }
static int hashGetHash(wolfssl_TI_Hash *hash, byte* result, word32 algo, word32 hsize)
{
#if !defined(TI_HASH_TEST)
uint32_t h[16] ;
wolfSSL_TI_lockCCM() ;
ROM_SHAMD5Reset(SHAMD5_BASE);
ROM_SHAMD5ConfigSet(SHAMD5_BASE, algo);
ROM_SHAMD5DataProcess(SHAMD5_BASE,
(uint32_t *)hash->msg, hash->used, h);
XMEMCPY(result, h, hsize) ;
wolfSSL_TI_unlockCCM() ;
#else
(void) hash ;
(void) result ;
(void) algo ;
(void) hsize ;
#endif
return 0 ;
}
#ifndef TI_HASH_TEST
static int hashHash(const byte* data, word32 len, byte* hash, word32 algo, word32 hsize) static int hashHash(const byte* data, word32 len, byte* hash, word32 algo, word32 hsize)
{ {
int ret = 0; int ret = 0;
@@ -120,34 +149,80 @@ static int hashHash(const byte* data, word32 len, byte* hash, word32 algo, word3
return ret; return ret;
} }
#endif
#if !defined(NO_MD5) #if !defined(NO_MD5)
#ifdef TI_HASH_TEST
#define SHAMD5_ALGO_MD5 1
void wc_InitMd5_ti(Md5* md5) ;
void wc_Md5Update_ti(Md5* md5, const byte* data, word32 len);
void wc_Md5Final_ti(Md5* md5, byte* hash);
bool wolfSSL_TI_CCMInit(void) ;
bool wolfSSL_TI_CCMInit(void) { return true ; }
#endif
#ifdef TI_HASH_TEST
void wc_InitMd5_ti(Md5* md5)
#else
void wc_InitMd5(Md5* md5) void wc_InitMd5(Md5* md5)
#endif
{ {
if (md5 == NULL) if (md5 == NULL)
return ; return ;
if(!wolfSSL_TI_CCMInit())return ; if(!wolfSSL_TI_CCMInit())return ;
#ifdef TI_HASH_TEST
hashInit(&(md5->ti)) ;
#else
hashInit((wolfssl_TI_Hash *)md5) ; hashInit((wolfssl_TI_Hash *)md5) ;
#endif
} }
#ifdef TI_HASH_TEST
void wc_Md5Update_ti(Md5* md5, const byte* data, word32 len)
#else
void wc_Md5Update(Md5* md5, const byte* data, word32 len) void wc_Md5Update(Md5* md5, const byte* data, word32 len)
#endif
{ {
#ifdef TI_HASH_TEST
hashUpdate(&(md5->ti), data, len) ;
#else
hashUpdate((wolfssl_TI_Hash *)md5, data, len) ; hashUpdate((wolfssl_TI_Hash *)md5, data, len) ;
#endif
} }
#ifdef TI_HASH_TEST
void wc_Md5Final_ti(Md5* md5, byte* hash)
#else
void wc_Md5Final(Md5* md5, byte* hash) void wc_Md5Final(Md5* md5, byte* hash)
#endif
{ {
hashFinal((wolfssl_TI_Hash *)md5, hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ; #ifdef TI_HASH_TEST
hashFinal(&(md5->ti), hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ;
#else
hashFinal((wolfssl_TI_Hash *)md5, hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ;
#endif
} }
void wc_Md5GetHash_ti(Md5* md5, byte* hash)
{
hashGetHash(&(md5->ti), hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ;
#ifdef TI_HASH_TEST
wc_Md5Final(md5, hash) ;
#endif
}
#ifndef TI_HASH_TEST
WOLFSSL_API int wc_Md5Hash(const byte*data, word32 len, byte*hash) WOLFSSL_API int wc_Md5Hash(const byte*data, word32 len, byte*hash)
{ {
return hashHash(data, len, hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ; return hashHash(data, len, hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ;
} }
#endif
#endif /* NO_MD5 */ #endif /* NO_MD5 */
#ifndef TI_HASH_TEST
#if !defined(NO_SHA) #if !defined(NO_SHA)
WOLFSSL_API int wc_InitSha(Sha* sha) WOLFSSL_API int wc_InitSha(Sha* sha)
@@ -223,9 +298,7 @@ WOLFSSL_API int wc_Sha256Hash(const byte* data, word32 len, byte*hash)
{ {
return hashHash(data, len, hash, SHAMD5_ALGO_SHA256, SHA256_DIGEST_SIZE) ; return hashHash(data, len, hash, SHAMD5_ALGO_SHA256, SHA256_DIGEST_SIZE) ;
} }
#endif
#endif /* TI_HASH_TEST */
#endif /* NO_SHA256 */ #endif
#endif /* WOLFSSL_TI_HASH */
#endif /* WOLFSSL_TI_HASH_H */

View File

@@ -1,452 +1,473 @@
/* sha.c /* sha.c
* *
* Copyright (C) 2006-2015 wolfSSL Inc. * Copyright (C) 2006-2015 wolfSSL Inc.
* *
* This file is part of wolfSSL. (formerly known as CyaSSL) * This file is part of wolfSSL. (formerly known as CyaSSL)
* *
* wolfSSL is free software; you can redistribute it and/or modify * wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* wolfSSL is distributed in the hope that it will be useful, * wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include <config.h> #include <config.h>
#endif #endif
#include <wolfssl/wolfcrypt/settings.h> #include <wolfssl/wolfcrypt/settings.h>
#if !defined(NO_SHA) #if !defined(NO_SHA) && !defined(WOLFSSL_TI_HASH)
#include <wolfssl/wolfcrypt/sha.h> #include <wolfssl/wolfcrypt/sha.h>
#include <wolfssl/wolfcrypt/logging.h> #include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/error-crypt.h> #include <wolfssl/wolfcrypt/error-crypt.h>
#ifdef NO_INLINE #ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h> #include <wolfssl/wolfcrypt/misc.h>
#else #else
#include <wolfcrypt/src/misc.c> #include <wolfcrypt/src/misc.c>
#endif #endif
/* fips wrapper calls, user can call direct */ /* fips wrapper calls, user can call direct */
#ifdef HAVE_FIPS #ifdef HAVE_FIPS
int wc_InitSha(Sha* sha) int wc_InitSha(Sha* sha)
{ {
return InitSha_fips(sha); return InitSha_fips(sha);
} }
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len) int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
{ {
return ShaUpdate_fips(sha, data, len); return ShaUpdate_fips(sha, data, len);
} }
int wc_ShaFinal(Sha* sha, byte* out) int wc_ShaFinal(Sha* sha, byte* out)
{ {
return ShaFinal_fips(sha,out); return ShaFinal_fips(sha,out);
} }
int wc_ShaHash(const byte* data, word32 sz, byte* out) int wc_ShaHash(const byte* data, word32 sz, byte* out)
{ {
return ShaHash(data, sz, out); return ShaHash(data, sz, out);
} }
#else /* else build without fips */ #else /* else build without fips */
#ifdef WOLFSSL_PIC32MZ_HASH #ifdef WOLFSSL_PIC32MZ_HASH
#define wc_InitSha wc_InitSha_sw #define wc_InitSha wc_InitSha_sw
#define wc_ShaUpdate wc_ShaUpdate_sw #define wc_ShaUpdate wc_ShaUpdate_sw
#define wc_ShaFinal wc_ShaFinal_sw #define wc_ShaFinal wc_ShaFinal_sw
#endif #endif
#ifdef FREESCALE_MMCAU #ifdef FREESCALE_MMCAU
#include "cau_api.h" #include "cau_api.h"
#define XTRANSFORM(S,B) cau_sha1_hash_n((B), 1, ((S))->digest) #define XTRANSFORM(S,B) cau_sha1_hash_n((B), 1, ((S))->digest)
#else #else
#define XTRANSFORM(S,B) Transform((S)) #define XTRANSFORM(S,B) Transform((S))
#endif #endif
#ifdef STM32F2_HASH #ifdef STM32F2_HASH
/* /*
* STM32F2 hardware SHA1 support through the STM32F2 standard peripheral * STM32F2 hardware SHA1 support through the STM32F2 standard peripheral
* library. Documentation located in STM32F2xx Standard Peripheral Library * library. Documentation located in STM32F2xx Standard Peripheral Library
* document (See note in README). * document (See note in README).
*/ */
#include "stm32f2xx.h" #include "stm32f2xx.h"
#include "stm32f2xx_hash.h" #include "stm32f2xx_hash.h"
int wc_InitSha(Sha* sha) int wc_InitSha(Sha* sha)
{ {
/* STM32F2 struct notes: /* STM32F2 struct notes:
* sha->buffer = first 4 bytes used to hold partial block if needed * sha->buffer = first 4 bytes used to hold partial block if needed
* sha->buffLen = num bytes currently stored in sha->buffer * sha->buffLen = num bytes currently stored in sha->buffer
* sha->loLen = num bytes that have been written to STM32 FIFO * sha->loLen = num bytes that have been written to STM32 FIFO
*/ */
XMEMSET(sha->buffer, 0, SHA_REG_SIZE); XMEMSET(sha->buffer, 0, SHA_REG_SIZE);
sha->buffLen = 0; sha->buffLen = 0;
sha->loLen = 0; sha->loLen = 0;
/* initialize HASH peripheral */ /* initialize HASH peripheral */
HASH_DeInit(); HASH_DeInit();
/* configure algo used, algo mode, datatype */ /* configure algo used, algo mode, datatype */
HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE); HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
HASH->CR |= (HASH_AlgoSelection_SHA1 | HASH_AlgoMode_HASH HASH->CR |= (HASH_AlgoSelection_SHA1 | HASH_AlgoMode_HASH
| HASH_DataType_8b); | HASH_DataType_8b);
/* reset HASH processor */ /* reset HASH processor */
HASH->CR |= HASH_CR_INIT; HASH->CR |= HASH_CR_INIT;
return 0; return 0;
} }
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len) int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
{ {
word32 i = 0; word32 i = 0;
word32 fill = 0; word32 fill = 0;
word32 diff = 0; word32 diff = 0;
/* if saved partial block is available */ /* if saved partial block is available */
if (sha->buffLen) { if (sha->buffLen) {
fill = 4 - sha->buffLen; fill = 4 - sha->buffLen;
/* if enough data to fill, fill and push to FIFO */ /* if enough data to fill, fill and push to FIFO */
if (fill <= len) { if (fill <= len) {
XMEMCPY((byte*)sha->buffer + sha->buffLen, data, fill); XMEMCPY((byte*)sha->buffer + sha->buffLen, data, fill);
HASH_DataIn(*(uint32_t*)sha->buffer); HASH_DataIn(*(uint32_t*)sha->buffer);
data += fill; data += fill;
len -= fill; len -= fill;
sha->loLen += 4; sha->loLen += 4;
sha->buffLen = 0; sha->buffLen = 0;
} else { } else {
/* append partial to existing stored block */ /* append partial to existing stored block */
XMEMCPY((byte*)sha->buffer + sha->buffLen, data, len); XMEMCPY((byte*)sha->buffer + sha->buffLen, data, len);
sha->buffLen += len; sha->buffLen += len;
return; return;
} }
} }
/* write input block in the IN FIFO */ /* write input block in the IN FIFO */
for(i = 0; i < len; i += 4) for(i = 0; i < len; i += 4)
{ {
diff = len - i; diff = len - i;
if ( diff < 4) { if ( diff < 4) {
/* store incomplete last block, not yet in FIFO */ /* store incomplete last block, not yet in FIFO */
XMEMSET(sha->buffer, 0, SHA_REG_SIZE); XMEMSET(sha->buffer, 0, SHA_REG_SIZE);
XMEMCPY((byte*)sha->buffer, data, diff); XMEMCPY((byte*)sha->buffer, data, diff);
sha->buffLen = diff; sha->buffLen = diff;
} else { } else {
HASH_DataIn(*(uint32_t*)data); HASH_DataIn(*(uint32_t*)data);
data+=4; data+=4;
} }
} }
/* keep track of total data length thus far */ /* keep track of total data length thus far */
sha->loLen += (len - sha->buffLen); sha->loLen += (len - sha->buffLen);
return 0; return 0;
} }
int wc_ShaFinal(Sha* sha, byte* hash) int wc_ShaFinal(Sha* sha, byte* hash)
{ {
__IO uint16_t nbvalidbitsdata = 0; __IO uint16_t nbvalidbitsdata = 0;
/* finish reading any trailing bytes into FIFO */ /* finish reading any trailing bytes into FIFO */
if (sha->buffLen) { if (sha->buffLen) {
HASH_DataIn(*(uint32_t*)sha->buffer); HASH_DataIn(*(uint32_t*)sha->buffer);
sha->loLen += sha->buffLen; sha->loLen += sha->buffLen;
} }
/* calculate number of valid bits in last word of input data */ /* calculate number of valid bits in last word of input data */
nbvalidbitsdata = 8 * (sha->loLen % SHA_REG_SIZE); nbvalidbitsdata = 8 * (sha->loLen % SHA_REG_SIZE);
/* configure number of valid bits in last word of the data */ /* configure number of valid bits in last word of the data */
HASH_SetLastWordValidBitsNbr(nbvalidbitsdata); HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
/* start HASH processor */ /* start HASH processor */
HASH_StartDigest(); HASH_StartDigest();
/* wait until Busy flag == RESET */ /* wait until Busy flag == RESET */
while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {} while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {}
/* read message digest */ /* read message digest */
sha->digest[0] = HASH->HR[0]; sha->digest[0] = HASH->HR[0];
sha->digest[1] = HASH->HR[1]; sha->digest[1] = HASH->HR[1];
sha->digest[2] = HASH->HR[2]; sha->digest[2] = HASH->HR[2];
sha->digest[3] = HASH->HR[3]; sha->digest[3] = HASH->HR[3];
sha->digest[4] = HASH->HR[4]; sha->digest[4] = HASH->HR[4];
ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE); ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE); XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
return wc_InitSha(sha); /* reset state */ return wc_InitSha(sha); /* reset state */
} }
#else /* wc_ software implementation */ #elif defined(WOLFSSL_TI_HASH)
#ifndef WOLFSSL_HAVE_MIN /* defined in port/ti/ti_sha.c */
#define WOLFSSL_HAVE_MIN
#else /* wc_ software implementation */
static INLINE word32 min(word32 a, word32 b)
{ #ifndef WOLFSSL_HAVE_MIN
return a > b ? b : a; #define WOLFSSL_HAVE_MIN
}
static INLINE word32 min(word32 a, word32 b)
#endif /* WOLFSSL_HAVE_MIN */ {
return a > b ? b : a;
}
int wc_InitSha(Sha* sha)
{ #endif /* WOLFSSL_HAVE_MIN */
#ifdef FREESCALE_MMCAU
cau_sha1_initialize_output(sha->digest);
#else int wc_InitSha(Sha* sha)
sha->digest[0] = 0x67452301L; {
sha->digest[1] = 0xEFCDAB89L; #ifdef FREESCALE_MMCAU
sha->digest[2] = 0x98BADCFEL; cau_sha1_initialize_output(sha->digest);
sha->digest[3] = 0x10325476L; #else
sha->digest[4] = 0xC3D2E1F0L; sha->digest[0] = 0x67452301L;
#endif sha->digest[1] = 0xEFCDAB89L;
sha->digest[2] = 0x98BADCFEL;
sha->buffLen = 0; sha->digest[3] = 0x10325476L;
sha->loLen = 0; sha->digest[4] = 0xC3D2E1F0L;
sha->hiLen = 0; #endif
return 0; sha->buffLen = 0;
} sha->loLen = 0;
sha->hiLen = 0;
#ifndef FREESCALE_MMCAU
return 0;
#define blk0(i) (W[i] = sha->buffer[i]) }
#define blk1(i) (W[(i)&15] = \
rotlFixed(W[((i)+13)&15]^W[((i)+8)&15]^W[((i)+2)&15]^W[(i)&15],1)) #ifndef FREESCALE_MMCAU
#define f1(x,y,z) ((z)^((x) &((y)^(z)))) #define blk0(i) (W[i] = sha->buffer[i])
#define f2(x,y,z) ((x)^(y)^(z)) #define blk1(i) (W[(i)&15] = \
#define f3(x,y,z) (((x)&(y))|((z)&((x)|(y)))) rotlFixed(W[((i)+13)&15]^W[((i)+8)&15]^W[((i)+2)&15]^W[(i)&15],1))
#define f4(x,y,z) ((x)^(y)^(z))
#define f1(x,y,z) ((z)^((x) &((y)^(z))))
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ #define f2(x,y,z) ((x)^(y)^(z))
#define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \ #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y))))
rotlFixed((v),5); (w) = rotlFixed((w),30); #define f4(x,y,z) ((x)^(y)^(z))
#define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \
rotlFixed((v),5); (w) = rotlFixed((w),30); /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
#define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \ #define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \
rotlFixed((v),5); (w) = rotlFixed((w),30); rotlFixed((v),5); (w) = rotlFixed((w),30);
#define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \ #define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \
rotlFixed((v),5); (w) = rotlFixed((w),30); rotlFixed((v),5); (w) = rotlFixed((w),30);
#define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \ #define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \
rotlFixed((v),5); (w) = rotlFixed((w),30); rotlFixed((v),5); (w) = rotlFixed((w),30);
#define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \
static void Transform(Sha* sha) rotlFixed((v),5); (w) = rotlFixed((w),30);
{ #define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \
word32 W[SHA_BLOCK_SIZE / sizeof(word32)]; rotlFixed((v),5); (w) = rotlFixed((w),30);
/* Copy context->state[] to working vars */ static void Transform(Sha* sha)
word32 a = sha->digest[0]; {
word32 b = sha->digest[1]; word32 W[SHA_BLOCK_SIZE / sizeof(word32)];
word32 c = sha->digest[2];
word32 d = sha->digest[3]; /* Copy context->state[] to working vars */
word32 e = sha->digest[4]; word32 a = sha->digest[0];
word32 b = sha->digest[1];
#ifdef USE_SLOW_SHA word32 c = sha->digest[2];
word32 t, i; word32 d = sha->digest[3];
word32 e = sha->digest[4];
for (i = 0; i < 16; i++) {
R0(a, b, c, d, e, i); #ifdef USE_SLOW_SHA
t = e; e = d; d = c; c = b; b = a; a = t; word32 t, i;
}
for (i = 0; i < 16; i++) {
for (; i < 20; i++) { R0(a, b, c, d, e, i);
R1(a, b, c, d, e, i); t = e; e = d; d = c; c = b; b = a; a = t;
t = e; e = d; d = c; c = b; b = a; a = t; }
}
for (; i < 20; i++) {
for (; i < 40; i++) { R1(a, b, c, d, e, i);
R2(a, b, c, d, e, i); t = e; e = d; d = c; c = b; b = a; a = t;
t = e; e = d; d = c; c = b; b = a; a = t; }
}
for (; i < 40; i++) {
for (; i < 60; i++) { R2(a, b, c, d, e, i);
R3(a, b, c, d, e, i); t = e; e = d; d = c; c = b; b = a; a = t;
t = e; e = d; d = c; c = b; b = a; a = t; }
}
for (; i < 60; i++) {
for (; i < 80; i++) { R3(a, b, c, d, e, i);
R4(a, b, c, d, e, i); t = e; e = d; d = c; c = b; b = a; a = t;
t = e; e = d; d = c; c = b; b = a; a = t; }
}
#else for (; i < 80; i++) {
/* nearly 1 K bigger in code size but 25% faster */ R4(a, b, c, d, e, i);
/* 4 rounds of 20 operations each. Loop unrolled. */ t = e; e = d; d = c; c = b; b = a; a = t;
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); }
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); #else
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); /* nearly 1 K bigger in code size but 25% faster */
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); /* 4 rounds of 20 operations each. Loop unrolled. */
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
#endif R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
/* Add the working vars back into digest state[] */ R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
sha->digest[0] += a; R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
sha->digest[1] += b; #endif
sha->digest[2] += c;
sha->digest[3] += d; /* Add the working vars back into digest state[] */
sha->digest[4] += e; sha->digest[0] += a;
} sha->digest[1] += b;
sha->digest[2] += c;
#endif /* FREESCALE_MMCAU */ sha->digest[3] += d;
sha->digest[4] += e;
}
static INLINE void AddLength(Sha* sha, word32 len)
{ #endif /* FREESCALE_MMCAU */
word32 tmp = sha->loLen;
if ( (sha->loLen += len) < tmp)
sha->hiLen++; /* carry low to high */ static INLINE void AddLength(Sha* sha, word32 len)
} {
word32 tmp = sha->loLen;
if ( (sha->loLen += len) < tmp)
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len) sha->hiLen++; /* carry low to high */
{ }
/* do block size increments */
byte* local = (byte*)sha->buffer;
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
while (len) { {
word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen); /* do block size increments */
XMEMCPY(&local[sha->buffLen], data, add); byte* local = (byte*)sha->buffer;
sha->buffLen += add; while (len) {
data += add; word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen);
len -= add; XMEMCPY(&local[sha->buffLen], data, add);
if (sha->buffLen == SHA_BLOCK_SIZE) { sha->buffLen += add;
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) data += add;
ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE); len -= add;
#endif
XTRANSFORM(sha, local); if (sha->buffLen == SHA_BLOCK_SIZE) {
AddLength(sha, SHA_BLOCK_SIZE); #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
sha->buffLen = 0; ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
} #endif
} XTRANSFORM(sha, local);
AddLength(sha, SHA_BLOCK_SIZE);
return 0; sha->buffLen = 0;
} }
}
int wc_ShaFinal(Sha* sha, byte* hash) return 0;
{ }
byte* local = (byte*)sha->buffer;
AddLength(sha, sha->buffLen); /* before adding pads */ int wc_ShaFinal(Sha* sha, byte* hash)
{
local[sha->buffLen++] = 0x80; /* add 1 */ byte* local = (byte*)sha->buffer;
/* pad with zeros */ AddLength(sha, sha->buffLen); /* before adding pads */
if (sha->buffLen > SHA_PAD_SIZE) {
XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen); local[sha->buffLen++] = 0x80; /* add 1 */
sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen;
/* pad with zeros */
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) if (sha->buffLen > SHA_PAD_SIZE) {
ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE); XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen);
#endif sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen;
XTRANSFORM(sha, local);
sha->buffLen = 0; #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
} ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen); #endif
XTRANSFORM(sha, local);
/* put lengths in bits */ sha->buffLen = 0;
sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) + }
(sha->hiLen << 3); XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen);
sha->loLen = sha->loLen << 3;
/* put lengths in bits */
/* store lengths */ sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) +
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) (sha->hiLen << 3);
ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE); sha->loLen = sha->loLen << 3;
#endif
/* ! length ordering dependent on digest endian type ! */ /* store lengths */
XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32)); #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32)); ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
#endif
#ifdef FREESCALE_MMCAU /* ! length ordering dependent on digest endian type ! */
/* Kinetis requires only these bytes reversed */ XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
ByteReverseWords(&sha->buffer[SHA_PAD_SIZE/sizeof(word32)], XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
&sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
2 * sizeof(word32)); #ifdef FREESCALE_MMCAU
#endif /* Kinetis requires only these bytes reversed */
ByteReverseWords(&sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
XTRANSFORM(sha, local); &sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
#ifdef LITTLE_ENDIAN_ORDER 2 * sizeof(word32));
ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE); #endif
#endif
XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE); XTRANSFORM(sha, local);
#ifdef LITTLE_ENDIAN_ORDER
return wc_InitSha(sha); /* reset state */ ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
} #endif
XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
#endif /* STM32F2_HASH */
return wc_InitSha(sha); /* reset state */
}
int wc_ShaHash(const byte* data, word32 len, byte* hash)
{ #endif /* STM32F2_HASH */
int ret = 0;
#ifdef WOLFSSL_SMALL_STACK
Sha* sha; int wc_ShaHash(const byte* data, word32 len, byte* hash)
#else {
Sha sha[1]; int ret = 0;
#endif #ifdef WOLFSSL_SMALL_STACK
Sha* sha;
#ifdef WOLFSSL_SMALL_STACK #else
sha = (Sha*)XMALLOC(sizeof(Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER); Sha sha[1];
if (sha == NULL) #endif
return MEMORY_E;
#endif #ifdef WOLFSSL_SMALL_STACK
sha = (Sha*)XMALLOC(sizeof(Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if ((ret = wc_InitSha(sha)) != 0) { if (sha == NULL)
WOLFSSL_MSG("wc_InitSha failed"); return MEMORY_E;
} #endif
else {
wc_ShaUpdate(sha, data, len); if ((ret = wc_InitSha(sha)) != 0) {
wc_ShaFinal(sha, hash); WOLFSSL_MSG("wc_InitSha failed");
} }
else {
#ifdef WOLFSSL_SMALL_STACK wc_ShaUpdate(sha, data, len);
XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER); wc_ShaFinal(sha, hash);
#endif }
return ret; #ifdef WOLFSSL_SMALL_STACK
XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
} #endif
#endif /* HAVE_FIPS */
#endif /* NO_SHA */ return ret;
}
#ifdef WOLFSSL_TI_HASH
#include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
#endif
int wc_ShaGetHash(Sha* sha, byte* hash)
{
#if defined(WOLFSS_TI_HASH)
wc_ShaGetHash_TI(sha, hash) ;
#else
int ret ;
Sha save = *sha ;
ret = wc_ShaFinal(sha, hash) ;
*sha = save ;
return ret ;
#endif
}
#endif /* HAVE_FIPS */
#endif /* NO_SHA */

3540
wolfcrypt/src/sha256.c Executable file → Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,81 +1,100 @@
/* md5.h /* md5.h
* *
* Copyright (C) 2006-2015 wolfSSL Inc. * Copyright (C) 2006-2015 wolfSSL Inc.
* *
* This file is part of wolfSSL. (formerly known as CyaSSL) * This file is part of wolfSSL. (formerly known as CyaSSL)
* *
* wolfSSL is free software; you can redistribute it and/or modify * wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* wolfSSL is distributed in the hope that it will be useful, * wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
#ifndef WOLF_CRYPT_MD5_H #ifndef WOLF_CRYPT_MD5_H
#define WOLF_CRYPT_MD5_H #define WOLF_CRYPT_MD5_H
#include <wolfssl/wolfcrypt/types.h> #include <wolfssl/wolfcrypt/types.h>
#ifndef NO_MD5 #ifndef NO_MD5
#ifdef HAVE_FIPS #ifdef HAVE_FIPS
#define wc_InitMd5 InitMd5 #define wc_InitMd5 InitMd5
#define wc_Md5Update Md5Update #define wc_Md5Update Md5Update
#define wc_Md5Final Md5Final #define wc_Md5Final Md5Final
#define wc_Md5Hash Md5Hash #define wc_Md5Hash Md5Hash
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* in bytes */
/* in bytes */ enum {
enum { #ifdef STM32F2_HASH
#ifdef STM32F2_HASH MD5_REG_SIZE = 4, /* STM32 register size, bytes */
MD5_REG_SIZE = 4, /* STM32 register size, bytes */ #endif
#endif MD5 = 0, /* hash type unique */
MD5 = 0, /* hash type unique */ MD5_BLOCK_SIZE = 64,
MD5_BLOCK_SIZE = 64, MD5_DIGEST_SIZE = 16,
MD5_DIGEST_SIZE = 16, MD5_PAD_SIZE = 56
MD5_PAD_SIZE = 56 };
};
#if defined(WOLFSSL_PIC32MZ_HASH)
#if defined(WOLFSSL_PIC32MZ_HASH) #include "port/pic32/pic32mz-crypt.h"
#include "port/pic32/pic32mz-crypt.h" #endif
#endif
#ifdef TI_HASH_TEST
/* MD5 digest */ #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
typedef struct Md5 { #endif
word32 buffLen; /* in bytes */
word32 loLen; /* length in bytes */
word32 hiLen; /* length in bytes */ #ifndef WOLFSSL_TI_HASH
word32 buffer[MD5_BLOCK_SIZE / sizeof(word32)];
#if !defined(WOLFSSL_PIC32MZ_HASH) /* MD5 digest */
word32 digest[MD5_DIGEST_SIZE / sizeof(word32)]; typedef struct Md5 {
#else word32 buffLen; /* in bytes */
word32 digest[PIC32_HASH_SIZE / sizeof(word32)]; word32 loLen; /* length in bytes */
pic32mz_desc desc ; /* Crypt Engine descripter */ word32 hiLen; /* length in bytes */
#endif word32 buffer[MD5_BLOCK_SIZE / sizeof(word32)];
} Md5; #if !defined(WOLFSSL_PIC32MZ_HASH)
word32 digest[MD5_DIGEST_SIZE / sizeof(word32)];
WOLFSSL_API void wc_InitMd5(Md5*); #else
WOLFSSL_API void wc_Md5Update(Md5*, const byte*, word32); word32 digest[PIC32_HASH_SIZE / sizeof(word32)];
WOLFSSL_API void wc_Md5Final(Md5*, byte*); pic32mz_desc desc ; /* Crypt Engine descripter */
WOLFSSL_API int wc_Md5Hash(const byte*, word32, byte*); #endif
#ifdef TI_HASH_TEST
#ifdef __cplusplus wolfssl_TI_Hash ti ;
} /* extern "C" */ #endif
#endif
} Md5;
#endif /* NO_MD5 */
#endif /* WOLF_CRYPT_MD5_H */ #if defined(TI_HASH_TEST)
void wc_Md5GetHash_ti(Md5* md5, byte* hash) ;
#endif
#else /* WOLFSSL_TI_HASH */
#include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
#endif
WOLFSSL_API void wc_InitMd5(Md5*);
WOLFSSL_API void wc_Md5Update(Md5*, const byte*, word32);
WOLFSSL_API void wc_Md5Final(Md5*, byte*);
WOLFSSL_API int wc_Md5Hash(const byte*, word32, byte*);
WOLFSSL_API void wc_Md5GetHash(Md5*, byte*);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* NO_MD5 */
#endif /* WOLF_CRYPT_MD5_H */

View File

@@ -1,80 +1,88 @@
/* sha.h /* sha.h
* *
* Copyright (C) 2006-2015 wolfSSL Inc. * Copyright (C) 2006-2015 wolfSSL Inc.
* *
* This file is part of wolfSSL. (formerly known as CyaSSL) * This file is part of wolfSSL. (formerly known as CyaSSL)
* *
* wolfSSL is free software; you can redistribute it and/or modify * wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* wolfSSL is distributed in the hope that it will be useful, * wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
#ifndef WOLF_CRYPT_SHA_H #ifndef WOLF_CRYPT_SHA_H
#define WOLF_CRYPT_SHA_H #define WOLF_CRYPT_SHA_H
#include <wolfssl/wolfcrypt/types.h> #include <wolfssl/wolfcrypt/types.h>
#ifndef NO_SHA #ifndef NO_SHA
#ifdef HAVE_FIPS #ifdef HAVE_FIPS
/* for fips @wc_fips */ /* for fips @wc_fips */
#include <cyassl/ctaocrypt/sha.h> #include <cyassl/ctaocrypt/sha.h>
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifndef HAVE_FIPS /* avoid redefining structs */ #ifndef HAVE_FIPS /* avoid redefining structs */
/* in bytes */ /* in bytes */
enum { enum {
#ifdef STM32F2_HASH #ifdef STM32F2_HASH
SHA_REG_SIZE = 4, /* STM32 register size, bytes */ SHA_REG_SIZE = 4, /* STM32 register size, bytes */
#endif #endif
SHA = 1, /* hash type unique */ SHA = 1, /* hash type unique */
SHA_BLOCK_SIZE = 64, SHA_BLOCK_SIZE = 64,
SHA_DIGEST_SIZE = 20, SHA_DIGEST_SIZE = 20,
SHA_PAD_SIZE = 56 SHA_PAD_SIZE = 56
}; };
#ifdef WOLFSSL_PIC32MZ_HASH #ifdef WOLFSSL_PIC32MZ_HASH
#include "port/pic32/pic32mz-crypt.h" #include "port/pic32/pic32mz-crypt.h"
#endif #endif
/* Sha digest */ #ifndef WOLFSSL_TI_HASH
typedef struct Sha {
word32 buffLen; /* in bytes */ /* Sha digest */
word32 loLen; /* length in bytes */ typedef struct Sha {
word32 hiLen; /* length in bytes */ word32 buffLen; /* in bytes */
word32 buffer[SHA_BLOCK_SIZE / sizeof(word32)]; word32 loLen; /* length in bytes */
#ifndef WOLFSSL_PIC32MZ_HASH word32 hiLen; /* length in bytes */
word32 digest[SHA_DIGEST_SIZE / sizeof(word32)]; word32 buffer[SHA_BLOCK_SIZE / sizeof(word32)];
#else #ifndef WOLFSSL_PIC32MZ_HASH
word32 digest[PIC32_HASH_SIZE / sizeof(word32)]; word32 digest[SHA_DIGEST_SIZE / sizeof(word32)];
pic32mz_desc desc; /* Crypt Engine descripter */ #else
#endif word32 digest[PIC32_HASH_SIZE / sizeof(word32)];
} Sha; pic32mz_desc desc; /* Crypt Engine descripter */
#endif /* HAVE_FIPS */ #endif
} Sha;
WOLFSSL_API int wc_InitSha(Sha*);
WOLFSSL_API int wc_ShaUpdate(Sha*, const byte*, word32); #else /* WOLFSSL_TI_HASH */
WOLFSSL_API int wc_ShaFinal(Sha*, byte*); #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
WOLFSSL_API int wc_ShaHash(const byte*, word32, byte*); #endif
#ifdef __cplusplus #endif /* HAVE_FIPS */
} /* extern "C" */
#endif WOLFSSL_API int wc_InitSha(Sha*);
WOLFSSL_API int wc_ShaUpdate(Sha*, const byte*, word32);
#endif /* NO_SHA */ WOLFSSL_API int wc_ShaFinal(Sha*, byte*);
#endif /* WOLF_CRYPT_SHA_H */ WOLFSSL_API int wc_ShaHash(const byte*, word32, byte*);
WOLFSSL_API int wc_ShaGetHash(Sha*, byte*);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* NO_SHA */
#endif /* WOLF_CRYPT_SHA_H */

View File

@@ -1,80 +1,86 @@
/* sha256.h /* sha256.h
* *
* Copyright (C) 2006-2015 wolfSSL Inc. * Copyright (C) 2006-2015 wolfSSL Inc.
* *
* This file is part of wolfSSL. (formerly known as CyaSSL) * This file is part of wolfSSL. (formerly known as CyaSSL)
* *
* wolfSSL is free software; you can redistribute it and/or modify * wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* wolfSSL is distributed in the hope that it will be useful, * wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
/* code submitted by raphael.huck@efixo.com */ /* code submitted by raphael.huck@efixo.com */
#ifndef WOLF_CRYPT_SHA256_H #ifndef WOLF_CRYPT_SHA256_H
#define WOLF_CRYPT_SHA256_H #define WOLF_CRYPT_SHA256_H
#include <wolfssl/wolfcrypt/types.h> #include <wolfssl/wolfcrypt/types.h>
#ifndef NO_SHA256 #ifndef NO_SHA256
#ifdef HAVE_FIPS #ifdef HAVE_FIPS
/* for fips @wc_fips */ /* for fips @wc_fips */
#include <cyassl/ctaocrypt/sha256.h> #include <cyassl/ctaocrypt/sha256.h>
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifndef HAVE_FIPS /* avoid redefinition of structs */ #ifndef HAVE_FIPS /* avoid redefinition of structs */
#ifdef WOLFSSL_PIC32MZ_HASH #ifdef WOLFSSL_PIC32MZ_HASH
#include "port/pic32/pic32mz-crypt.h" #include "port/pic32/pic32mz-crypt.h"
#endif #endif
/* in bytes */ /* in bytes */
enum { enum {
SHA256 = 2, /* hash type unique */ SHA256 = 2, /* hash type unique */
SHA256_BLOCK_SIZE = 64, SHA256_BLOCK_SIZE = 64,
SHA256_DIGEST_SIZE = 32, SHA256_DIGEST_SIZE = 32,
SHA256_PAD_SIZE = 56 SHA256_PAD_SIZE = 56
}; };
#ifndef WOLFSSL_TI_HASH
/* Sha256 digest */
typedef struct Sha256 { /* Sha256 digest */
word32 buffLen; /* in bytes */ typedef struct Sha256 {
word32 loLen; /* length in bytes */ word32 buffLen; /* in bytes */
word32 hiLen; /* length in bytes */ word32 loLen; /* length in bytes */
word32 digest[SHA256_DIGEST_SIZE / sizeof(word32)]; word32 hiLen; /* length in bytes */
word32 buffer[SHA256_BLOCK_SIZE / sizeof(word32)]; word32 digest[SHA256_DIGEST_SIZE / sizeof(word32)];
#ifdef WOLFSSL_PIC32MZ_HASH word32 buffer[SHA256_BLOCK_SIZE / sizeof(word32)];
pic32mz_desc desc ; /* Crypt Engine descripter */ #ifdef WOLFSSL_PIC32MZ_HASH
#endif pic32mz_desc desc ; /* Crypt Engine descripter */
} Sha256; #endif
} Sha256;
#endif /* HAVE_FIPS */
#else /* WOLFSSL_TI_HASH */
WOLFSSL_API int wc_InitSha256(Sha256*); #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
WOLFSSL_API int wc_Sha256Update(Sha256*, const byte*, word32); #endif
WOLFSSL_API int wc_Sha256Final(Sha256*, byte*);
WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*); #endif /* HAVE_FIPS */
#ifdef __cplusplus WOLFSSL_API int wc_InitSha256(Sha256*);
} /* extern "C" */ WOLFSSL_API int wc_Sha256Update(Sha256*, const byte*, word32);
#endif WOLFSSL_API int wc_Sha256Final(Sha256*, byte*);
WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*);
#endif /* NO_SHA256 */ WOLFSSL_API int wc_Sha256GetHash(Sha256*, byte*);
#endif /* WOLF_CRYPT_SHA256_H */
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* NO_SHA256 */
#endif /* WOLF_CRYPT_SHA256_H */