forked from wolfSSL/wolfssl
corrected NL code
This commit is contained in:
@ -1,426 +1,426 @@
|
||||
/* md5.c
|
||||
*
|
||||
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#if !defined(NO_MD5) && !defined(WOLFSSL_TI_HASH)
|
||||
|
||||
#ifdef WOLFSSL_PIC32MZ_HASH
|
||||
#define wc_InitMd5 wc_InitMd5_sw
|
||||
#define wc_Md5Update wc_Md5Update_sw
|
||||
#define wc_Md5Final wc_Md5Final_sw
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/md5.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#ifdef FREESCALE_MMCAU
|
||||
#include "cau_api.h"
|
||||
#define XTRANSFORM(S,B) cau_md5_hash_n((B), 1, (unsigned char*)(S)->digest)
|
||||
#else
|
||||
#define XTRANSFORM(S,B) Transform((S))
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef STM32F2_HASH
|
||||
/*
|
||||
* STM32F2 hardware MD5 support through the STM32F2 standard peripheral
|
||||
* library. Documentation located in STM32F2xx Standard Peripheral Library
|
||||
* document (See note in README).
|
||||
*/
|
||||
#include "stm32f2xx.h"
|
||||
|
||||
void wc_InitMd5(Md5* md5)
|
||||
{
|
||||
/* STM32F2 struct notes:
|
||||
* md5->buffer = first 4 bytes used to hold partial block if needed
|
||||
* md5->buffLen = num bytes currently stored in md5->buffer
|
||||
* md5->loLen = num bytes that have been written to STM32 FIFO
|
||||
*/
|
||||
XMEMSET(md5->buffer, 0, MD5_REG_SIZE);
|
||||
|
||||
md5->buffLen = 0;
|
||||
md5->loLen = 0;
|
||||
|
||||
/* initialize HASH peripheral */
|
||||
HASH_DeInit();
|
||||
|
||||
/* configure algo used, algo mode, datatype */
|
||||
HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
|
||||
HASH->CR |= (HASH_AlgoSelection_MD5 | HASH_AlgoMode_HASH
|
||||
| HASH_DataType_8b);
|
||||
|
||||
/* reset HASH processor */
|
||||
HASH->CR |= HASH_CR_INIT;
|
||||
}
|
||||
|
||||
void wc_Md5Update(Md5* md5, const byte* data, word32 len)
|
||||
{
|
||||
word32 i = 0;
|
||||
word32 fill = 0;
|
||||
word32 diff = 0;
|
||||
|
||||
/* if saved partial block is available */
|
||||
if (md5->buffLen > 0) {
|
||||
fill = 4 - md5->buffLen;
|
||||
|
||||
/* if enough data to fill, fill and push to FIFO */
|
||||
if (fill <= len) {
|
||||
XMEMCPY((byte*)md5->buffer + md5->buffLen, data, fill);
|
||||
HASH_DataIn(*(uint32_t*)md5->buffer);
|
||||
|
||||
data += fill;
|
||||
len -= fill;
|
||||
md5->loLen += 4;
|
||||
md5->buffLen = 0;
|
||||
} else {
|
||||
/* append partial to existing stored block */
|
||||
XMEMCPY((byte*)md5->buffer + md5->buffLen, data, len);
|
||||
md5->buffLen += len;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* write input block in the IN FIFO */
|
||||
for (i = 0; i < len; i += 4)
|
||||
{
|
||||
diff = len - i;
|
||||
if (diff < 4) {
|
||||
/* store incomplete last block, not yet in FIFO */
|
||||
XMEMSET(md5->buffer, 0, MD5_REG_SIZE);
|
||||
XMEMCPY((byte*)md5->buffer, data, diff);
|
||||
md5->buffLen = diff;
|
||||
} else {
|
||||
HASH_DataIn(*(uint32_t*)data);
|
||||
data+=4;
|
||||
}
|
||||
}
|
||||
|
||||
/* keep track of total data length thus far */
|
||||
md5->loLen += (len - md5->buffLen);
|
||||
}
|
||||
|
||||
void wc_Md5Final(Md5* md5, byte* hash)
|
||||
{
|
||||
__IO uint16_t nbvalidbitsdata = 0;
|
||||
|
||||
/* finish reading any trailing bytes into FIFO */
|
||||
if (md5->buffLen > 0) {
|
||||
HASH_DataIn(*(uint32_t*)md5->buffer);
|
||||
md5->loLen += md5->buffLen;
|
||||
}
|
||||
|
||||
/* calculate number of valid bits in last word of input data */
|
||||
nbvalidbitsdata = 8 * (md5->loLen % MD5_REG_SIZE);
|
||||
|
||||
/* configure number of valid bits in last word of the data */
|
||||
HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
|
||||
|
||||
/* start HASH processor */
|
||||
HASH_StartDigest();
|
||||
|
||||
/* wait until Busy flag == RESET */
|
||||
while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {}
|
||||
|
||||
/* read message digest */
|
||||
md5->digest[0] = HASH->HR[0];
|
||||
md5->digest[1] = HASH->HR[1];
|
||||
md5->digest[2] = HASH->HR[2];
|
||||
md5->digest[3] = HASH->HR[3];
|
||||
|
||||
ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE);
|
||||
|
||||
XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE);
|
||||
|
||||
wc_InitMd5(md5); /* reset state */
|
||||
}
|
||||
|
||||
#elif defined(WOLFSSL_IT_HASH)
|
||||
|
||||
/* defined in port/ti_md5.c */
|
||||
|
||||
#else /* CTaoCrypt software implementation */
|
||||
|
||||
#ifndef WOLFSSL_HAVE_MIN
|
||||
#define WOLFSSL_HAVE_MIN
|
||||
|
||||
static INLINE word32 min(word32 a, word32 b)
|
||||
{
|
||||
return a > b ? b : a;
|
||||
}
|
||||
|
||||
#endif /* WOLFSSL_HAVE_MIN */
|
||||
|
||||
#ifdef TI_HASH_TEST
|
||||
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) ;
|
||||
#endif
|
||||
|
||||
void wc_InitMd5(Md5* md5)
|
||||
{
|
||||
md5->digest[0] = 0x67452301L;
|
||||
md5->digest[1] = 0xefcdab89L;
|
||||
md5->digest[2] = 0x98badcfeL;
|
||||
md5->digest[3] = 0x10325476L;
|
||||
|
||||
md5->buffLen = 0;
|
||||
md5->loLen = 0;
|
||||
md5->hiLen = 0;
|
||||
|
||||
#ifdef TI_HASH_TEST
|
||||
wc_InitMd5_ti(md5) ;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef FREESCALE_MMCAU
|
||||
|
||||
static void Transform(Md5* md5)
|
||||
{
|
||||
#define F1(x, y, z) (z ^ (x & (y ^ z)))
|
||||
#define F2(x, y, z) F1(z, x, y)
|
||||
#define F3(x, y, z) (x ^ y ^ z)
|
||||
#define F4(x, y, z) (y ^ (x | ~z))
|
||||
|
||||
#define MD5STEP(f, w, x, y, z, data, s) \
|
||||
w = rotlFixed(w + f(x, y, z) + data, s) + x
|
||||
|
||||
/* Copy context->state[] to working vars */
|
||||
word32 a = md5->digest[0];
|
||||
word32 b = md5->digest[1];
|
||||
word32 c = md5->digest[2];
|
||||
word32 d = md5->digest[3];
|
||||
|
||||
MD5STEP(F1, a, b, c, d, md5->buffer[0] + 0xd76aa478, 7);
|
||||
MD5STEP(F1, d, a, b, c, md5->buffer[1] + 0xe8c7b756, 12);
|
||||
MD5STEP(F1, c, d, a, b, md5->buffer[2] + 0x242070db, 17);
|
||||
MD5STEP(F1, b, c, d, a, md5->buffer[3] + 0xc1bdceee, 22);
|
||||
MD5STEP(F1, a, b, c, d, md5->buffer[4] + 0xf57c0faf, 7);
|
||||
MD5STEP(F1, d, a, b, c, md5->buffer[5] + 0x4787c62a, 12);
|
||||
MD5STEP(F1, c, d, a, b, md5->buffer[6] + 0xa8304613, 17);
|
||||
MD5STEP(F1, b, c, d, a, md5->buffer[7] + 0xfd469501, 22);
|
||||
MD5STEP(F1, a, b, c, d, md5->buffer[8] + 0x698098d8, 7);
|
||||
MD5STEP(F1, d, a, b, c, md5->buffer[9] + 0x8b44f7af, 12);
|
||||
MD5STEP(F1, c, d, a, b, md5->buffer[10] + 0xffff5bb1, 17);
|
||||
MD5STEP(F1, b, c, d, a, md5->buffer[11] + 0x895cd7be, 22);
|
||||
MD5STEP(F1, a, b, c, d, md5->buffer[12] + 0x6b901122, 7);
|
||||
MD5STEP(F1, d, a, b, c, md5->buffer[13] + 0xfd987193, 12);
|
||||
MD5STEP(F1, c, d, a, b, md5->buffer[14] + 0xa679438e, 17);
|
||||
MD5STEP(F1, b, c, d, a, md5->buffer[15] + 0x49b40821, 22);
|
||||
|
||||
MD5STEP(F2, a, b, c, d, md5->buffer[1] + 0xf61e2562, 5);
|
||||
MD5STEP(F2, d, a, b, c, md5->buffer[6] + 0xc040b340, 9);
|
||||
MD5STEP(F2, c, d, a, b, md5->buffer[11] + 0x265e5a51, 14);
|
||||
MD5STEP(F2, b, c, d, a, md5->buffer[0] + 0xe9b6c7aa, 20);
|
||||
MD5STEP(F2, a, b, c, d, md5->buffer[5] + 0xd62f105d, 5);
|
||||
MD5STEP(F2, d, a, b, c, md5->buffer[10] + 0x02441453, 9);
|
||||
MD5STEP(F2, c, d, a, b, md5->buffer[15] + 0xd8a1e681, 14);
|
||||
MD5STEP(F2, b, c, d, a, md5->buffer[4] + 0xe7d3fbc8, 20);
|
||||
MD5STEP(F2, a, b, c, d, md5->buffer[9] + 0x21e1cde6, 5);
|
||||
MD5STEP(F2, d, a, b, c, md5->buffer[14] + 0xc33707d6, 9);
|
||||
MD5STEP(F2, c, d, a, b, md5->buffer[3] + 0xf4d50d87, 14);
|
||||
MD5STEP(F2, b, c, d, a, md5->buffer[8] + 0x455a14ed, 20);
|
||||
MD5STEP(F2, a, b, c, d, md5->buffer[13] + 0xa9e3e905, 5);
|
||||
MD5STEP(F2, d, a, b, c, md5->buffer[2] + 0xfcefa3f8, 9);
|
||||
MD5STEP(F2, c, d, a, b, md5->buffer[7] + 0x676f02d9, 14);
|
||||
MD5STEP(F2, b, c, d, a, md5->buffer[12] + 0x8d2a4c8a, 20);
|
||||
|
||||
MD5STEP(F3, a, b, c, d, md5->buffer[5] + 0xfffa3942, 4);
|
||||
MD5STEP(F3, d, a, b, c, md5->buffer[8] + 0x8771f681, 11);
|
||||
MD5STEP(F3, c, d, a, b, md5->buffer[11] + 0x6d9d6122, 16);
|
||||
MD5STEP(F3, b, c, d, a, md5->buffer[14] + 0xfde5380c, 23);
|
||||
MD5STEP(F3, a, b, c, d, md5->buffer[1] + 0xa4beea44, 4);
|
||||
MD5STEP(F3, d, a, b, c, md5->buffer[4] + 0x4bdecfa9, 11);
|
||||
MD5STEP(F3, c, d, a, b, md5->buffer[7] + 0xf6bb4b60, 16);
|
||||
MD5STEP(F3, b, c, d, a, md5->buffer[10] + 0xbebfbc70, 23);
|
||||
MD5STEP(F3, a, b, c, d, md5->buffer[13] + 0x289b7ec6, 4);
|
||||
MD5STEP(F3, d, a, b, c, md5->buffer[0] + 0xeaa127fa, 11);
|
||||
MD5STEP(F3, c, d, a, b, md5->buffer[3] + 0xd4ef3085, 16);
|
||||
MD5STEP(F3, b, c, d, a, md5->buffer[6] + 0x04881d05, 23);
|
||||
MD5STEP(F3, a, b, c, d, md5->buffer[9] + 0xd9d4d039, 4);
|
||||
MD5STEP(F3, d, a, b, c, md5->buffer[12] + 0xe6db99e5, 11);
|
||||
MD5STEP(F3, c, d, a, b, md5->buffer[15] + 0x1fa27cf8, 16);
|
||||
MD5STEP(F3, b, c, d, a, md5->buffer[2] + 0xc4ac5665, 23);
|
||||
|
||||
MD5STEP(F4, a, b, c, d, md5->buffer[0] + 0xf4292244, 6);
|
||||
MD5STEP(F4, d, a, b, c, md5->buffer[7] + 0x432aff97, 10);
|
||||
MD5STEP(F4, c, d, a, b, md5->buffer[14] + 0xab9423a7, 15);
|
||||
MD5STEP(F4, b, c, d, a, md5->buffer[5] + 0xfc93a039, 21);
|
||||
MD5STEP(F4, a, b, c, d, md5->buffer[12] + 0x655b59c3, 6);
|
||||
MD5STEP(F4, d, a, b, c, md5->buffer[3] + 0x8f0ccc92, 10);
|
||||
MD5STEP(F4, c, d, a, b, md5->buffer[10] + 0xffeff47d, 15);
|
||||
MD5STEP(F4, b, c, d, a, md5->buffer[1] + 0x85845dd1, 21);
|
||||
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);
|
||||
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);
|
||||
MD5STEP(F4, c, d, a, b, md5->buffer[2] + 0x2ad7d2bb, 15);
|
||||
MD5STEP(F4, b, c, d, a, md5->buffer[9] + 0xeb86d391, 21);
|
||||
|
||||
/* Add the working vars back into digest state[] */
|
||||
md5->digest[0] += a;
|
||||
md5->digest[1] += b;
|
||||
md5->digest[2] += c;
|
||||
md5->digest[3] += d;
|
||||
}
|
||||
|
||||
#endif /* FREESCALE_MMCAU */
|
||||
|
||||
|
||||
static INLINE void AddLength(Md5* md5, word32 len)
|
||||
{
|
||||
word32 tmp = md5->loLen;
|
||||
if ( (md5->loLen += len) < tmp)
|
||||
md5->hiLen++; /* carry low to high */
|
||||
}
|
||||
|
||||
|
||||
void wc_Md5Update(Md5* md5, const byte* data, word32 len)
|
||||
{
|
||||
/* do block size increments */
|
||||
byte* local = (byte*)md5->buffer;
|
||||
|
||||
while (len) {
|
||||
word32 add = min(len, MD5_BLOCK_SIZE - md5->buffLen);
|
||||
XMEMCPY(&local[md5->buffLen], data, add);
|
||||
|
||||
md5->buffLen += add;
|
||||
data += add;
|
||||
len -= add;
|
||||
|
||||
if (md5->buffLen == MD5_BLOCK_SIZE) {
|
||||
#if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
|
||||
#endif
|
||||
XTRANSFORM(md5, local);
|
||||
AddLength(md5, MD5_BLOCK_SIZE);
|
||||
md5->buffLen = 0;
|
||||
}
|
||||
}
|
||||
#ifdef TI_HASH_TEST
|
||||
wc_Md5Update_ti(md5, data, len) ;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
void wc_Md5Final(Md5* md5, byte* hash)
|
||||
{
|
||||
byte* local = (byte*)md5->buffer;
|
||||
|
||||
AddLength(md5, md5->buffLen); /* before adding pads */
|
||||
|
||||
local[md5->buffLen++] = 0x80; /* add 1 */
|
||||
|
||||
/* pad with zeros */
|
||||
if (md5->buffLen > MD5_PAD_SIZE) {
|
||||
XMEMSET(&local[md5->buffLen], 0, MD5_BLOCK_SIZE - md5->buffLen);
|
||||
md5->buffLen += MD5_BLOCK_SIZE - md5->buffLen;
|
||||
|
||||
#if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
|
||||
#endif
|
||||
XTRANSFORM(md5, local);
|
||||
md5->buffLen = 0;
|
||||
}
|
||||
XMEMSET(&local[md5->buffLen], 0, MD5_PAD_SIZE - md5->buffLen);
|
||||
|
||||
/* put lengths in bits */
|
||||
md5->hiLen = (md5->loLen >> (8*sizeof(md5->loLen) - 3)) +
|
||||
(md5->hiLen << 3);
|
||||
md5->loLen = md5->loLen << 3;
|
||||
|
||||
/* store lengths */
|
||||
#if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
|
||||
#endif
|
||||
/* ! length ordering dependent on digest endian type ! */
|
||||
XMEMCPY(&local[MD5_PAD_SIZE], &md5->loLen, sizeof(word32));
|
||||
XMEMCPY(&local[MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32));
|
||||
|
||||
XTRANSFORM(md5, local);
|
||||
#ifdef BIG_ENDIAN_ORDER
|
||||
ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE);
|
||||
#endif
|
||||
XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE);
|
||||
|
||||
wc_InitMd5(md5); /* reset state */
|
||||
|
||||
#ifdef TI_HASH_TEST
|
||||
wc_Md5Final_ti(md5, hash) ;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* STM32F2_HASH */
|
||||
|
||||
|
||||
int wc_Md5Hash(const byte* data, word32 len, byte* hash)
|
||||
{
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
Md5* 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 */
|
||||
/* md5.c
|
||||
*
|
||||
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#if !defined(NO_MD5) && !defined(WOLFSSL_TI_HASH)
|
||||
|
||||
#ifdef WOLFSSL_PIC32MZ_HASH
|
||||
#define wc_InitMd5 wc_InitMd5_sw
|
||||
#define wc_Md5Update wc_Md5Update_sw
|
||||
#define wc_Md5Final wc_Md5Final_sw
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/md5.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
#ifdef FREESCALE_MMCAU
|
||||
#include "cau_api.h"
|
||||
#define XTRANSFORM(S,B) cau_md5_hash_n((B), 1, (unsigned char*)(S)->digest)
|
||||
#else
|
||||
#define XTRANSFORM(S,B) Transform((S))
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef STM32F2_HASH
|
||||
/*
|
||||
* STM32F2 hardware MD5 support through the STM32F2 standard peripheral
|
||||
* library. Documentation located in STM32F2xx Standard Peripheral Library
|
||||
* document (See note in README).
|
||||
*/
|
||||
#include "stm32f2xx.h"
|
||||
|
||||
void wc_InitMd5(Md5* md5)
|
||||
{
|
||||
/* STM32F2 struct notes:
|
||||
* md5->buffer = first 4 bytes used to hold partial block if needed
|
||||
* md5->buffLen = num bytes currently stored in md5->buffer
|
||||
* md5->loLen = num bytes that have been written to STM32 FIFO
|
||||
*/
|
||||
XMEMSET(md5->buffer, 0, MD5_REG_SIZE);
|
||||
|
||||
md5->buffLen = 0;
|
||||
md5->loLen = 0;
|
||||
|
||||
/* initialize HASH peripheral */
|
||||
HASH_DeInit();
|
||||
|
||||
/* configure algo used, algo mode, datatype */
|
||||
HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
|
||||
HASH->CR |= (HASH_AlgoSelection_MD5 | HASH_AlgoMode_HASH
|
||||
| HASH_DataType_8b);
|
||||
|
||||
/* reset HASH processor */
|
||||
HASH->CR |= HASH_CR_INIT;
|
||||
}
|
||||
|
||||
void wc_Md5Update(Md5* md5, const byte* data, word32 len)
|
||||
{
|
||||
word32 i = 0;
|
||||
word32 fill = 0;
|
||||
word32 diff = 0;
|
||||
|
||||
/* if saved partial block is available */
|
||||
if (md5->buffLen > 0) {
|
||||
fill = 4 - md5->buffLen;
|
||||
|
||||
/* if enough data to fill, fill and push to FIFO */
|
||||
if (fill <= len) {
|
||||
XMEMCPY((byte*)md5->buffer + md5->buffLen, data, fill);
|
||||
HASH_DataIn(*(uint32_t*)md5->buffer);
|
||||
|
||||
data += fill;
|
||||
len -= fill;
|
||||
md5->loLen += 4;
|
||||
md5->buffLen = 0;
|
||||
} else {
|
||||
/* append partial to existing stored block */
|
||||
XMEMCPY((byte*)md5->buffer + md5->buffLen, data, len);
|
||||
md5->buffLen += len;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* write input block in the IN FIFO */
|
||||
for (i = 0; i < len; i += 4)
|
||||
{
|
||||
diff = len - i;
|
||||
if (diff < 4) {
|
||||
/* store incomplete last block, not yet in FIFO */
|
||||
XMEMSET(md5->buffer, 0, MD5_REG_SIZE);
|
||||
XMEMCPY((byte*)md5->buffer, data, diff);
|
||||
md5->buffLen = diff;
|
||||
} else {
|
||||
HASH_DataIn(*(uint32_t*)data);
|
||||
data+=4;
|
||||
}
|
||||
}
|
||||
|
||||
/* keep track of total data length thus far */
|
||||
md5->loLen += (len - md5->buffLen);
|
||||
}
|
||||
|
||||
void wc_Md5Final(Md5* md5, byte* hash)
|
||||
{
|
||||
__IO uint16_t nbvalidbitsdata = 0;
|
||||
|
||||
/* finish reading any trailing bytes into FIFO */
|
||||
if (md5->buffLen > 0) {
|
||||
HASH_DataIn(*(uint32_t*)md5->buffer);
|
||||
md5->loLen += md5->buffLen;
|
||||
}
|
||||
|
||||
/* calculate number of valid bits in last word of input data */
|
||||
nbvalidbitsdata = 8 * (md5->loLen % MD5_REG_SIZE);
|
||||
|
||||
/* configure number of valid bits in last word of the data */
|
||||
HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
|
||||
|
||||
/* start HASH processor */
|
||||
HASH_StartDigest();
|
||||
|
||||
/* wait until Busy flag == RESET */
|
||||
while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {}
|
||||
|
||||
/* read message digest */
|
||||
md5->digest[0] = HASH->HR[0];
|
||||
md5->digest[1] = HASH->HR[1];
|
||||
md5->digest[2] = HASH->HR[2];
|
||||
md5->digest[3] = HASH->HR[3];
|
||||
|
||||
ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE);
|
||||
|
||||
XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE);
|
||||
|
||||
wc_InitMd5(md5); /* reset state */
|
||||
}
|
||||
|
||||
#elif defined(WOLFSSL_IT_HASH)
|
||||
|
||||
/* defined in port/ti_md5.c */
|
||||
|
||||
#else /* CTaoCrypt software implementation */
|
||||
|
||||
#ifndef WOLFSSL_HAVE_MIN
|
||||
#define WOLFSSL_HAVE_MIN
|
||||
|
||||
static INLINE word32 min(word32 a, word32 b)
|
||||
{
|
||||
return a > b ? b : a;
|
||||
}
|
||||
|
||||
#endif /* WOLFSSL_HAVE_MIN */
|
||||
|
||||
#ifdef TI_HASH_TEST
|
||||
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) ;
|
||||
#endif
|
||||
|
||||
void wc_InitMd5(Md5* md5)
|
||||
{
|
||||
md5->digest[0] = 0x67452301L;
|
||||
md5->digest[1] = 0xefcdab89L;
|
||||
md5->digest[2] = 0x98badcfeL;
|
||||
md5->digest[3] = 0x10325476L;
|
||||
|
||||
md5->buffLen = 0;
|
||||
md5->loLen = 0;
|
||||
md5->hiLen = 0;
|
||||
|
||||
#ifdef TI_HASH_TEST
|
||||
wc_InitMd5_ti(md5) ;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef FREESCALE_MMCAU
|
||||
|
||||
static void Transform(Md5* md5)
|
||||
{
|
||||
#define F1(x, y, z) (z ^ (x & (y ^ z)))
|
||||
#define F2(x, y, z) F1(z, x, y)
|
||||
#define F3(x, y, z) (x ^ y ^ z)
|
||||
#define F4(x, y, z) (y ^ (x | ~z))
|
||||
|
||||
#define MD5STEP(f, w, x, y, z, data, s) \
|
||||
w = rotlFixed(w + f(x, y, z) + data, s) + x
|
||||
|
||||
/* Copy context->state[] to working vars */
|
||||
word32 a = md5->digest[0];
|
||||
word32 b = md5->digest[1];
|
||||
word32 c = md5->digest[2];
|
||||
word32 d = md5->digest[3];
|
||||
|
||||
MD5STEP(F1, a, b, c, d, md5->buffer[0] + 0xd76aa478, 7);
|
||||
MD5STEP(F1, d, a, b, c, md5->buffer[1] + 0xe8c7b756, 12);
|
||||
MD5STEP(F1, c, d, a, b, md5->buffer[2] + 0x242070db, 17);
|
||||
MD5STEP(F1, b, c, d, a, md5->buffer[3] + 0xc1bdceee, 22);
|
||||
MD5STEP(F1, a, b, c, d, md5->buffer[4] + 0xf57c0faf, 7);
|
||||
MD5STEP(F1, d, a, b, c, md5->buffer[5] + 0x4787c62a, 12);
|
||||
MD5STEP(F1, c, d, a, b, md5->buffer[6] + 0xa8304613, 17);
|
||||
MD5STEP(F1, b, c, d, a, md5->buffer[7] + 0xfd469501, 22);
|
||||
MD5STEP(F1, a, b, c, d, md5->buffer[8] + 0x698098d8, 7);
|
||||
MD5STEP(F1, d, a, b, c, md5->buffer[9] + 0x8b44f7af, 12);
|
||||
MD5STEP(F1, c, d, a, b, md5->buffer[10] + 0xffff5bb1, 17);
|
||||
MD5STEP(F1, b, c, d, a, md5->buffer[11] + 0x895cd7be, 22);
|
||||
MD5STEP(F1, a, b, c, d, md5->buffer[12] + 0x6b901122, 7);
|
||||
MD5STEP(F1, d, a, b, c, md5->buffer[13] + 0xfd987193, 12);
|
||||
MD5STEP(F1, c, d, a, b, md5->buffer[14] + 0xa679438e, 17);
|
||||
MD5STEP(F1, b, c, d, a, md5->buffer[15] + 0x49b40821, 22);
|
||||
|
||||
MD5STEP(F2, a, b, c, d, md5->buffer[1] + 0xf61e2562, 5);
|
||||
MD5STEP(F2, d, a, b, c, md5->buffer[6] + 0xc040b340, 9);
|
||||
MD5STEP(F2, c, d, a, b, md5->buffer[11] + 0x265e5a51, 14);
|
||||
MD5STEP(F2, b, c, d, a, md5->buffer[0] + 0xe9b6c7aa, 20);
|
||||
MD5STEP(F2, a, b, c, d, md5->buffer[5] + 0xd62f105d, 5);
|
||||
MD5STEP(F2, d, a, b, c, md5->buffer[10] + 0x02441453, 9);
|
||||
MD5STEP(F2, c, d, a, b, md5->buffer[15] + 0xd8a1e681, 14);
|
||||
MD5STEP(F2, b, c, d, a, md5->buffer[4] + 0xe7d3fbc8, 20);
|
||||
MD5STEP(F2, a, b, c, d, md5->buffer[9] + 0x21e1cde6, 5);
|
||||
MD5STEP(F2, d, a, b, c, md5->buffer[14] + 0xc33707d6, 9);
|
||||
MD5STEP(F2, c, d, a, b, md5->buffer[3] + 0xf4d50d87, 14);
|
||||
MD5STEP(F2, b, c, d, a, md5->buffer[8] + 0x455a14ed, 20);
|
||||
MD5STEP(F2, a, b, c, d, md5->buffer[13] + 0xa9e3e905, 5);
|
||||
MD5STEP(F2, d, a, b, c, md5->buffer[2] + 0xfcefa3f8, 9);
|
||||
MD5STEP(F2, c, d, a, b, md5->buffer[7] + 0x676f02d9, 14);
|
||||
MD5STEP(F2, b, c, d, a, md5->buffer[12] + 0x8d2a4c8a, 20);
|
||||
|
||||
MD5STEP(F3, a, b, c, d, md5->buffer[5] + 0xfffa3942, 4);
|
||||
MD5STEP(F3, d, a, b, c, md5->buffer[8] + 0x8771f681, 11);
|
||||
MD5STEP(F3, c, d, a, b, md5->buffer[11] + 0x6d9d6122, 16);
|
||||
MD5STEP(F3, b, c, d, a, md5->buffer[14] + 0xfde5380c, 23);
|
||||
MD5STEP(F3, a, b, c, d, md5->buffer[1] + 0xa4beea44, 4);
|
||||
MD5STEP(F3, d, a, b, c, md5->buffer[4] + 0x4bdecfa9, 11);
|
||||
MD5STEP(F3, c, d, a, b, md5->buffer[7] + 0xf6bb4b60, 16);
|
||||
MD5STEP(F3, b, c, d, a, md5->buffer[10] + 0xbebfbc70, 23);
|
||||
MD5STEP(F3, a, b, c, d, md5->buffer[13] + 0x289b7ec6, 4);
|
||||
MD5STEP(F3, d, a, b, c, md5->buffer[0] + 0xeaa127fa, 11);
|
||||
MD5STEP(F3, c, d, a, b, md5->buffer[3] + 0xd4ef3085, 16);
|
||||
MD5STEP(F3, b, c, d, a, md5->buffer[6] + 0x04881d05, 23);
|
||||
MD5STEP(F3, a, b, c, d, md5->buffer[9] + 0xd9d4d039, 4);
|
||||
MD5STEP(F3, d, a, b, c, md5->buffer[12] + 0xe6db99e5, 11);
|
||||
MD5STEP(F3, c, d, a, b, md5->buffer[15] + 0x1fa27cf8, 16);
|
||||
MD5STEP(F3, b, c, d, a, md5->buffer[2] + 0xc4ac5665, 23);
|
||||
|
||||
MD5STEP(F4, a, b, c, d, md5->buffer[0] + 0xf4292244, 6);
|
||||
MD5STEP(F4, d, a, b, c, md5->buffer[7] + 0x432aff97, 10);
|
||||
MD5STEP(F4, c, d, a, b, md5->buffer[14] + 0xab9423a7, 15);
|
||||
MD5STEP(F4, b, c, d, a, md5->buffer[5] + 0xfc93a039, 21);
|
||||
MD5STEP(F4, a, b, c, d, md5->buffer[12] + 0x655b59c3, 6);
|
||||
MD5STEP(F4, d, a, b, c, md5->buffer[3] + 0x8f0ccc92, 10);
|
||||
MD5STEP(F4, c, d, a, b, md5->buffer[10] + 0xffeff47d, 15);
|
||||
MD5STEP(F4, b, c, d, a, md5->buffer[1] + 0x85845dd1, 21);
|
||||
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);
|
||||
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);
|
||||
MD5STEP(F4, c, d, a, b, md5->buffer[2] + 0x2ad7d2bb, 15);
|
||||
MD5STEP(F4, b, c, d, a, md5->buffer[9] + 0xeb86d391, 21);
|
||||
|
||||
/* Add the working vars back into digest state[] */
|
||||
md5->digest[0] += a;
|
||||
md5->digest[1] += b;
|
||||
md5->digest[2] += c;
|
||||
md5->digest[3] += d;
|
||||
}
|
||||
|
||||
#endif /* FREESCALE_MMCAU */
|
||||
|
||||
|
||||
static INLINE void AddLength(Md5* md5, word32 len)
|
||||
{
|
||||
word32 tmp = md5->loLen;
|
||||
if ( (md5->loLen += len) < tmp)
|
||||
md5->hiLen++; /* carry low to high */
|
||||
}
|
||||
|
||||
|
||||
void wc_Md5Update(Md5* md5, const byte* data, word32 len)
|
||||
{
|
||||
/* do block size increments */
|
||||
byte* local = (byte*)md5->buffer;
|
||||
|
||||
while (len) {
|
||||
word32 add = min(len, MD5_BLOCK_SIZE - md5->buffLen);
|
||||
XMEMCPY(&local[md5->buffLen], data, add);
|
||||
|
||||
md5->buffLen += add;
|
||||
data += add;
|
||||
len -= add;
|
||||
|
||||
if (md5->buffLen == MD5_BLOCK_SIZE) {
|
||||
#if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
|
||||
#endif
|
||||
XTRANSFORM(md5, local);
|
||||
AddLength(md5, MD5_BLOCK_SIZE);
|
||||
md5->buffLen = 0;
|
||||
}
|
||||
}
|
||||
#ifdef TI_HASH_TEST
|
||||
wc_Md5Update_ti(md5, data, len) ;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
void wc_Md5Final(Md5* md5, byte* hash)
|
||||
{
|
||||
byte* local = (byte*)md5->buffer;
|
||||
|
||||
AddLength(md5, md5->buffLen); /* before adding pads */
|
||||
|
||||
local[md5->buffLen++] = 0x80; /* add 1 */
|
||||
|
||||
/* pad with zeros */
|
||||
if (md5->buffLen > MD5_PAD_SIZE) {
|
||||
XMEMSET(&local[md5->buffLen], 0, MD5_BLOCK_SIZE - md5->buffLen);
|
||||
md5->buffLen += MD5_BLOCK_SIZE - md5->buffLen;
|
||||
|
||||
#if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
|
||||
#endif
|
||||
XTRANSFORM(md5, local);
|
||||
md5->buffLen = 0;
|
||||
}
|
||||
XMEMSET(&local[md5->buffLen], 0, MD5_PAD_SIZE - md5->buffLen);
|
||||
|
||||
/* put lengths in bits */
|
||||
md5->hiLen = (md5->loLen >> (8*sizeof(md5->loLen) - 3)) +
|
||||
(md5->hiLen << 3);
|
||||
md5->loLen = md5->loLen << 3;
|
||||
|
||||
/* store lengths */
|
||||
#if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
|
||||
#endif
|
||||
/* ! length ordering dependent on digest endian type ! */
|
||||
XMEMCPY(&local[MD5_PAD_SIZE], &md5->loLen, sizeof(word32));
|
||||
XMEMCPY(&local[MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32));
|
||||
|
||||
XTRANSFORM(md5, local);
|
||||
#ifdef BIG_ENDIAN_ORDER
|
||||
ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE);
|
||||
#endif
|
||||
XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE);
|
||||
|
||||
wc_InitMd5(md5); /* reset state */
|
||||
|
||||
#ifdef TI_HASH_TEST
|
||||
wc_Md5Final_ti(md5, hash) ;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* STM32F2_HASH */
|
||||
|
||||
|
||||
int wc_Md5Hash(const byte* data, word32 len, byte* hash)
|
||||
{
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
Md5* 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 */
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,473 +1,473 @@
|
||||
/* sha.c
|
||||
*
|
||||
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#if !defined(NO_SHA) && !defined(WOLFSSL_TI_HASH)
|
||||
|
||||
#include <wolfssl/wolfcrypt/sha.h>
|
||||
#include <wolfssl/wolfcrypt/logging.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
/* fips wrapper calls, user can call direct */
|
||||
#ifdef HAVE_FIPS
|
||||
int wc_InitSha(Sha* sha)
|
||||
{
|
||||
return InitSha_fips(sha);
|
||||
}
|
||||
|
||||
|
||||
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
|
||||
{
|
||||
return ShaUpdate_fips(sha, data, len);
|
||||
}
|
||||
|
||||
|
||||
int wc_ShaFinal(Sha* sha, byte* out)
|
||||
{
|
||||
return ShaFinal_fips(sha,out);
|
||||
}
|
||||
|
||||
int wc_ShaHash(const byte* data, word32 sz, byte* out)
|
||||
{
|
||||
return ShaHash(data, sz, out);
|
||||
}
|
||||
|
||||
#else /* else build without fips */
|
||||
|
||||
#ifdef WOLFSSL_PIC32MZ_HASH
|
||||
#define wc_InitSha wc_InitSha_sw
|
||||
#define wc_ShaUpdate wc_ShaUpdate_sw
|
||||
#define wc_ShaFinal wc_ShaFinal_sw
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef FREESCALE_MMCAU
|
||||
#include "cau_api.h"
|
||||
#define XTRANSFORM(S,B) cau_sha1_hash_n((B), 1, ((S))->digest)
|
||||
#else
|
||||
#define XTRANSFORM(S,B) Transform((S))
|
||||
#endif
|
||||
|
||||
#ifdef STM32F2_HASH
|
||||
/*
|
||||
* STM32F2 hardware SHA1 support through the STM32F2 standard peripheral
|
||||
* library. Documentation located in STM32F2xx Standard Peripheral Library
|
||||
* document (See note in README).
|
||||
*/
|
||||
#include "stm32f2xx.h"
|
||||
#include "stm32f2xx_hash.h"
|
||||
|
||||
int wc_InitSha(Sha* sha)
|
||||
{
|
||||
/* STM32F2 struct notes:
|
||||
* sha->buffer = first 4 bytes used to hold partial block if needed
|
||||
* sha->buffLen = num bytes currently stored in sha->buffer
|
||||
* sha->loLen = num bytes that have been written to STM32 FIFO
|
||||
*/
|
||||
XMEMSET(sha->buffer, 0, SHA_REG_SIZE);
|
||||
sha->buffLen = 0;
|
||||
sha->loLen = 0;
|
||||
|
||||
/* initialize HASH peripheral */
|
||||
HASH_DeInit();
|
||||
|
||||
/* configure algo used, algo mode, datatype */
|
||||
HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
|
||||
HASH->CR |= (HASH_AlgoSelection_SHA1 | HASH_AlgoMode_HASH
|
||||
| HASH_DataType_8b);
|
||||
|
||||
/* reset HASH processor */
|
||||
HASH->CR |= HASH_CR_INIT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
|
||||
{
|
||||
word32 i = 0;
|
||||
word32 fill = 0;
|
||||
word32 diff = 0;
|
||||
|
||||
/* if saved partial block is available */
|
||||
if (sha->buffLen) {
|
||||
fill = 4 - sha->buffLen;
|
||||
|
||||
/* if enough data to fill, fill and push to FIFO */
|
||||
if (fill <= len) {
|
||||
XMEMCPY((byte*)sha->buffer + sha->buffLen, data, fill);
|
||||
HASH_DataIn(*(uint32_t*)sha->buffer);
|
||||
|
||||
data += fill;
|
||||
len -= fill;
|
||||
sha->loLen += 4;
|
||||
sha->buffLen = 0;
|
||||
} else {
|
||||
/* append partial to existing stored block */
|
||||
XMEMCPY((byte*)sha->buffer + sha->buffLen, data, len);
|
||||
sha->buffLen += len;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* write input block in the IN FIFO */
|
||||
for(i = 0; i < len; i += 4)
|
||||
{
|
||||
diff = len - i;
|
||||
if ( diff < 4) {
|
||||
/* store incomplete last block, not yet in FIFO */
|
||||
XMEMSET(sha->buffer, 0, SHA_REG_SIZE);
|
||||
XMEMCPY((byte*)sha->buffer, data, diff);
|
||||
sha->buffLen = diff;
|
||||
} else {
|
||||
HASH_DataIn(*(uint32_t*)data);
|
||||
data+=4;
|
||||
}
|
||||
}
|
||||
|
||||
/* keep track of total data length thus far */
|
||||
sha->loLen += (len - sha->buffLen);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wc_ShaFinal(Sha* sha, byte* hash)
|
||||
{
|
||||
__IO uint16_t nbvalidbitsdata = 0;
|
||||
|
||||
/* finish reading any trailing bytes into FIFO */
|
||||
if (sha->buffLen) {
|
||||
HASH_DataIn(*(uint32_t*)sha->buffer);
|
||||
sha->loLen += sha->buffLen;
|
||||
}
|
||||
|
||||
/* calculate number of valid bits in last word of input data */
|
||||
nbvalidbitsdata = 8 * (sha->loLen % SHA_REG_SIZE);
|
||||
|
||||
/* configure number of valid bits in last word of the data */
|
||||
HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
|
||||
|
||||
/* start HASH processor */
|
||||
HASH_StartDigest();
|
||||
|
||||
/* wait until Busy flag == RESET */
|
||||
while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {}
|
||||
|
||||
/* read message digest */
|
||||
sha->digest[0] = HASH->HR[0];
|
||||
sha->digest[1] = HASH->HR[1];
|
||||
sha->digest[2] = HASH->HR[2];
|
||||
sha->digest[3] = HASH->HR[3];
|
||||
sha->digest[4] = HASH->HR[4];
|
||||
|
||||
ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
|
||||
|
||||
XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
|
||||
|
||||
return wc_InitSha(sha); /* reset state */
|
||||
}
|
||||
|
||||
#elif defined(WOLFSSL_TI_HASH)
|
||||
|
||||
/* defined in port/ti/ti_sha.c */
|
||||
|
||||
#else /* wc_ software implementation */
|
||||
|
||||
#ifndef WOLFSSL_HAVE_MIN
|
||||
#define WOLFSSL_HAVE_MIN
|
||||
|
||||
static INLINE word32 min(word32 a, word32 b)
|
||||
{
|
||||
return a > b ? b : a;
|
||||
}
|
||||
|
||||
#endif /* WOLFSSL_HAVE_MIN */
|
||||
|
||||
|
||||
int wc_InitSha(Sha* sha)
|
||||
{
|
||||
#ifdef FREESCALE_MMCAU
|
||||
cau_sha1_initialize_output(sha->digest);
|
||||
#else
|
||||
sha->digest[0] = 0x67452301L;
|
||||
sha->digest[1] = 0xEFCDAB89L;
|
||||
sha->digest[2] = 0x98BADCFEL;
|
||||
sha->digest[3] = 0x10325476L;
|
||||
sha->digest[4] = 0xC3D2E1F0L;
|
||||
#endif
|
||||
|
||||
sha->buffLen = 0;
|
||||
sha->loLen = 0;
|
||||
sha->hiLen = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef FREESCALE_MMCAU
|
||||
|
||||
#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))
|
||||
|
||||
#define f1(x,y,z) ((z)^((x) &((y)^(z))))
|
||||
#define f2(x,y,z) ((x)^(y)^(z))
|
||||
#define f3(x,y,z) (((x)&(y))|((z)&((x)|(y))))
|
||||
#define f4(x,y,z) ((x)^(y)^(z))
|
||||
|
||||
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
|
||||
#define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \
|
||||
rotlFixed((v),5); (w) = rotlFixed((w),30);
|
||||
#define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \
|
||||
rotlFixed((v),5); (w) = rotlFixed((w),30);
|
||||
#define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \
|
||||
rotlFixed((v),5); (w) = rotlFixed((w),30);
|
||||
#define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \
|
||||
rotlFixed((v),5); (w) = rotlFixed((w),30);
|
||||
#define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \
|
||||
rotlFixed((v),5); (w) = rotlFixed((w),30);
|
||||
|
||||
static void Transform(Sha* sha)
|
||||
{
|
||||
word32 W[SHA_BLOCK_SIZE / sizeof(word32)];
|
||||
|
||||
/* Copy context->state[] to working vars */
|
||||
word32 a = sha->digest[0];
|
||||
word32 b = sha->digest[1];
|
||||
word32 c = sha->digest[2];
|
||||
word32 d = sha->digest[3];
|
||||
word32 e = sha->digest[4];
|
||||
|
||||
#ifdef USE_SLOW_SHA
|
||||
word32 t, i;
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
R0(a, b, c, d, e, i);
|
||||
t = e; e = d; d = c; c = b; b = a; a = t;
|
||||
}
|
||||
|
||||
for (; i < 20; i++) {
|
||||
R1(a, b, c, d, e, i);
|
||||
t = e; e = d; d = c; c = b; b = a; a = t;
|
||||
}
|
||||
|
||||
for (; i < 40; i++) {
|
||||
R2(a, b, c, d, e, i);
|
||||
t = e; e = d; d = c; c = b; b = a; a = t;
|
||||
}
|
||||
|
||||
for (; i < 60; i++) {
|
||||
R3(a, b, c, d, e, i);
|
||||
t = e; e = d; d = c; c = b; b = a; a = t;
|
||||
}
|
||||
|
||||
for (; i < 80; i++) {
|
||||
R4(a, b, c, d, e, i);
|
||||
t = e; e = d; d = c; c = b; b = a; a = t;
|
||||
}
|
||||
#else
|
||||
/* nearly 1 K bigger in code size but 25% faster */
|
||||
/* 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);
|
||||
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);
|
||||
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);
|
||||
|
||||
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(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);
|
||||
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);
|
||||
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);
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
#endif
|
||||
|
||||
/* Add the working vars back into digest state[] */
|
||||
sha->digest[0] += a;
|
||||
sha->digest[1] += b;
|
||||
sha->digest[2] += c;
|
||||
sha->digest[3] += d;
|
||||
sha->digest[4] += e;
|
||||
}
|
||||
|
||||
#endif /* FREESCALE_MMCAU */
|
||||
|
||||
|
||||
static INLINE void AddLength(Sha* sha, word32 len)
|
||||
{
|
||||
word32 tmp = sha->loLen;
|
||||
if ( (sha->loLen += len) < tmp)
|
||||
sha->hiLen++; /* carry low to high */
|
||||
}
|
||||
|
||||
|
||||
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
|
||||
{
|
||||
/* do block size increments */
|
||||
byte* local = (byte*)sha->buffer;
|
||||
|
||||
while (len) {
|
||||
word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen);
|
||||
XMEMCPY(&local[sha->buffLen], data, add);
|
||||
|
||||
sha->buffLen += add;
|
||||
data += add;
|
||||
len -= add;
|
||||
|
||||
if (sha->buffLen == SHA_BLOCK_SIZE) {
|
||||
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
|
||||
#endif
|
||||
XTRANSFORM(sha, local);
|
||||
AddLength(sha, SHA_BLOCK_SIZE);
|
||||
sha->buffLen = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int wc_ShaFinal(Sha* sha, byte* hash)
|
||||
{
|
||||
byte* local = (byte*)sha->buffer;
|
||||
|
||||
AddLength(sha, sha->buffLen); /* before adding pads */
|
||||
|
||||
local[sha->buffLen++] = 0x80; /* add 1 */
|
||||
|
||||
/* pad with zeros */
|
||||
if (sha->buffLen > SHA_PAD_SIZE) {
|
||||
XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen);
|
||||
sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen;
|
||||
|
||||
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
|
||||
#endif
|
||||
XTRANSFORM(sha, local);
|
||||
sha->buffLen = 0;
|
||||
}
|
||||
XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen);
|
||||
|
||||
/* put lengths in bits */
|
||||
sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) +
|
||||
(sha->hiLen << 3);
|
||||
sha->loLen = sha->loLen << 3;
|
||||
|
||||
/* store lengths */
|
||||
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
|
||||
#endif
|
||||
/* ! length ordering dependent on digest endian type ! */
|
||||
XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
|
||||
XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
|
||||
|
||||
#ifdef FREESCALE_MMCAU
|
||||
/* Kinetis requires only these bytes reversed */
|
||||
ByteReverseWords(&sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
|
||||
&sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
|
||||
2 * sizeof(word32));
|
||||
#endif
|
||||
|
||||
XTRANSFORM(sha, local);
|
||||
#ifdef LITTLE_ENDIAN_ORDER
|
||||
ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
|
||||
#endif
|
||||
XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
|
||||
|
||||
return wc_InitSha(sha); /* reset state */
|
||||
}
|
||||
|
||||
#endif /* STM32F2_HASH */
|
||||
|
||||
|
||||
int wc_ShaHash(const byte* data, word32 len, byte* hash)
|
||||
{
|
||||
int ret = 0;
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
Sha* sha;
|
||||
#else
|
||||
Sha sha[1];
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
sha = (Sha*)XMALLOC(sizeof(Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
if (sha == NULL)
|
||||
return MEMORY_E;
|
||||
#endif
|
||||
|
||||
if ((ret = wc_InitSha(sha)) != 0) {
|
||||
WOLFSSL_MSG("wc_InitSha failed");
|
||||
}
|
||||
else {
|
||||
wc_ShaUpdate(sha, data, len);
|
||||
wc_ShaFinal(sha, hash);
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
|
||||
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 */
|
||||
|
||||
/* sha.c
|
||||
*
|
||||
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#if !defined(NO_SHA) && !defined(WOLFSSL_TI_HASH)
|
||||
|
||||
#include <wolfssl/wolfcrypt/sha.h>
|
||||
#include <wolfssl/wolfcrypt/logging.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
/* fips wrapper calls, user can call direct */
|
||||
#ifdef HAVE_FIPS
|
||||
int wc_InitSha(Sha* sha)
|
||||
{
|
||||
return InitSha_fips(sha);
|
||||
}
|
||||
|
||||
|
||||
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
|
||||
{
|
||||
return ShaUpdate_fips(sha, data, len);
|
||||
}
|
||||
|
||||
|
||||
int wc_ShaFinal(Sha* sha, byte* out)
|
||||
{
|
||||
return ShaFinal_fips(sha,out);
|
||||
}
|
||||
|
||||
int wc_ShaHash(const byte* data, word32 sz, byte* out)
|
||||
{
|
||||
return ShaHash(data, sz, out);
|
||||
}
|
||||
|
||||
#else /* else build without fips */
|
||||
|
||||
#ifdef WOLFSSL_PIC32MZ_HASH
|
||||
#define wc_InitSha wc_InitSha_sw
|
||||
#define wc_ShaUpdate wc_ShaUpdate_sw
|
||||
#define wc_ShaFinal wc_ShaFinal_sw
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef FREESCALE_MMCAU
|
||||
#include "cau_api.h"
|
||||
#define XTRANSFORM(S,B) cau_sha1_hash_n((B), 1, ((S))->digest)
|
||||
#else
|
||||
#define XTRANSFORM(S,B) Transform((S))
|
||||
#endif
|
||||
|
||||
#ifdef STM32F2_HASH
|
||||
/*
|
||||
* STM32F2 hardware SHA1 support through the STM32F2 standard peripheral
|
||||
* library. Documentation located in STM32F2xx Standard Peripheral Library
|
||||
* document (See note in README).
|
||||
*/
|
||||
#include "stm32f2xx.h"
|
||||
#include "stm32f2xx_hash.h"
|
||||
|
||||
int wc_InitSha(Sha* sha)
|
||||
{
|
||||
/* STM32F2 struct notes:
|
||||
* sha->buffer = first 4 bytes used to hold partial block if needed
|
||||
* sha->buffLen = num bytes currently stored in sha->buffer
|
||||
* sha->loLen = num bytes that have been written to STM32 FIFO
|
||||
*/
|
||||
XMEMSET(sha->buffer, 0, SHA_REG_SIZE);
|
||||
sha->buffLen = 0;
|
||||
sha->loLen = 0;
|
||||
|
||||
/* initialize HASH peripheral */
|
||||
HASH_DeInit();
|
||||
|
||||
/* configure algo used, algo mode, datatype */
|
||||
HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
|
||||
HASH->CR |= (HASH_AlgoSelection_SHA1 | HASH_AlgoMode_HASH
|
||||
| HASH_DataType_8b);
|
||||
|
||||
/* reset HASH processor */
|
||||
HASH->CR |= HASH_CR_INIT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
|
||||
{
|
||||
word32 i = 0;
|
||||
word32 fill = 0;
|
||||
word32 diff = 0;
|
||||
|
||||
/* if saved partial block is available */
|
||||
if (sha->buffLen) {
|
||||
fill = 4 - sha->buffLen;
|
||||
|
||||
/* if enough data to fill, fill and push to FIFO */
|
||||
if (fill <= len) {
|
||||
XMEMCPY((byte*)sha->buffer + sha->buffLen, data, fill);
|
||||
HASH_DataIn(*(uint32_t*)sha->buffer);
|
||||
|
||||
data += fill;
|
||||
len -= fill;
|
||||
sha->loLen += 4;
|
||||
sha->buffLen = 0;
|
||||
} else {
|
||||
/* append partial to existing stored block */
|
||||
XMEMCPY((byte*)sha->buffer + sha->buffLen, data, len);
|
||||
sha->buffLen += len;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* write input block in the IN FIFO */
|
||||
for(i = 0; i < len; i += 4)
|
||||
{
|
||||
diff = len - i;
|
||||
if ( diff < 4) {
|
||||
/* store incomplete last block, not yet in FIFO */
|
||||
XMEMSET(sha->buffer, 0, SHA_REG_SIZE);
|
||||
XMEMCPY((byte*)sha->buffer, data, diff);
|
||||
sha->buffLen = diff;
|
||||
} else {
|
||||
HASH_DataIn(*(uint32_t*)data);
|
||||
data+=4;
|
||||
}
|
||||
}
|
||||
|
||||
/* keep track of total data length thus far */
|
||||
sha->loLen += (len - sha->buffLen);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wc_ShaFinal(Sha* sha, byte* hash)
|
||||
{
|
||||
__IO uint16_t nbvalidbitsdata = 0;
|
||||
|
||||
/* finish reading any trailing bytes into FIFO */
|
||||
if (sha->buffLen) {
|
||||
HASH_DataIn(*(uint32_t*)sha->buffer);
|
||||
sha->loLen += sha->buffLen;
|
||||
}
|
||||
|
||||
/* calculate number of valid bits in last word of input data */
|
||||
nbvalidbitsdata = 8 * (sha->loLen % SHA_REG_SIZE);
|
||||
|
||||
/* configure number of valid bits in last word of the data */
|
||||
HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
|
||||
|
||||
/* start HASH processor */
|
||||
HASH_StartDigest();
|
||||
|
||||
/* wait until Busy flag == RESET */
|
||||
while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {}
|
||||
|
||||
/* read message digest */
|
||||
sha->digest[0] = HASH->HR[0];
|
||||
sha->digest[1] = HASH->HR[1];
|
||||
sha->digest[2] = HASH->HR[2];
|
||||
sha->digest[3] = HASH->HR[3];
|
||||
sha->digest[4] = HASH->HR[4];
|
||||
|
||||
ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
|
||||
|
||||
XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
|
||||
|
||||
return wc_InitSha(sha); /* reset state */
|
||||
}
|
||||
|
||||
#elif defined(WOLFSSL_TI_HASH)
|
||||
|
||||
/* defined in port/ti/ti_sha.c */
|
||||
|
||||
#else /* wc_ software implementation */
|
||||
|
||||
#ifndef WOLFSSL_HAVE_MIN
|
||||
#define WOLFSSL_HAVE_MIN
|
||||
|
||||
static INLINE word32 min(word32 a, word32 b)
|
||||
{
|
||||
return a > b ? b : a;
|
||||
}
|
||||
|
||||
#endif /* WOLFSSL_HAVE_MIN */
|
||||
|
||||
|
||||
int wc_InitSha(Sha* sha)
|
||||
{
|
||||
#ifdef FREESCALE_MMCAU
|
||||
cau_sha1_initialize_output(sha->digest);
|
||||
#else
|
||||
sha->digest[0] = 0x67452301L;
|
||||
sha->digest[1] = 0xEFCDAB89L;
|
||||
sha->digest[2] = 0x98BADCFEL;
|
||||
sha->digest[3] = 0x10325476L;
|
||||
sha->digest[4] = 0xC3D2E1F0L;
|
||||
#endif
|
||||
|
||||
sha->buffLen = 0;
|
||||
sha->loLen = 0;
|
||||
sha->hiLen = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef FREESCALE_MMCAU
|
||||
|
||||
#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))
|
||||
|
||||
#define f1(x,y,z) ((z)^((x) &((y)^(z))))
|
||||
#define f2(x,y,z) ((x)^(y)^(z))
|
||||
#define f3(x,y,z) (((x)&(y))|((z)&((x)|(y))))
|
||||
#define f4(x,y,z) ((x)^(y)^(z))
|
||||
|
||||
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
|
||||
#define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \
|
||||
rotlFixed((v),5); (w) = rotlFixed((w),30);
|
||||
#define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \
|
||||
rotlFixed((v),5); (w) = rotlFixed((w),30);
|
||||
#define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \
|
||||
rotlFixed((v),5); (w) = rotlFixed((w),30);
|
||||
#define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \
|
||||
rotlFixed((v),5); (w) = rotlFixed((w),30);
|
||||
#define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \
|
||||
rotlFixed((v),5); (w) = rotlFixed((w),30);
|
||||
|
||||
static void Transform(Sha* sha)
|
||||
{
|
||||
word32 W[SHA_BLOCK_SIZE / sizeof(word32)];
|
||||
|
||||
/* Copy context->state[] to working vars */
|
||||
word32 a = sha->digest[0];
|
||||
word32 b = sha->digest[1];
|
||||
word32 c = sha->digest[2];
|
||||
word32 d = sha->digest[3];
|
||||
word32 e = sha->digest[4];
|
||||
|
||||
#ifdef USE_SLOW_SHA
|
||||
word32 t, i;
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
R0(a, b, c, d, e, i);
|
||||
t = e; e = d; d = c; c = b; b = a; a = t;
|
||||
}
|
||||
|
||||
for (; i < 20; i++) {
|
||||
R1(a, b, c, d, e, i);
|
||||
t = e; e = d; d = c; c = b; b = a; a = t;
|
||||
}
|
||||
|
||||
for (; i < 40; i++) {
|
||||
R2(a, b, c, d, e, i);
|
||||
t = e; e = d; d = c; c = b; b = a; a = t;
|
||||
}
|
||||
|
||||
for (; i < 60; i++) {
|
||||
R3(a, b, c, d, e, i);
|
||||
t = e; e = d; d = c; c = b; b = a; a = t;
|
||||
}
|
||||
|
||||
for (; i < 80; i++) {
|
||||
R4(a, b, c, d, e, i);
|
||||
t = e; e = d; d = c; c = b; b = a; a = t;
|
||||
}
|
||||
#else
|
||||
/* nearly 1 K bigger in code size but 25% faster */
|
||||
/* 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);
|
||||
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);
|
||||
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);
|
||||
|
||||
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(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);
|
||||
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);
|
||||
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);
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
#endif
|
||||
|
||||
/* Add the working vars back into digest state[] */
|
||||
sha->digest[0] += a;
|
||||
sha->digest[1] += b;
|
||||
sha->digest[2] += c;
|
||||
sha->digest[3] += d;
|
||||
sha->digest[4] += e;
|
||||
}
|
||||
|
||||
#endif /* FREESCALE_MMCAU */
|
||||
|
||||
|
||||
static INLINE void AddLength(Sha* sha, word32 len)
|
||||
{
|
||||
word32 tmp = sha->loLen;
|
||||
if ( (sha->loLen += len) < tmp)
|
||||
sha->hiLen++; /* carry low to high */
|
||||
}
|
||||
|
||||
|
||||
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
|
||||
{
|
||||
/* do block size increments */
|
||||
byte* local = (byte*)sha->buffer;
|
||||
|
||||
while (len) {
|
||||
word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen);
|
||||
XMEMCPY(&local[sha->buffLen], data, add);
|
||||
|
||||
sha->buffLen += add;
|
||||
data += add;
|
||||
len -= add;
|
||||
|
||||
if (sha->buffLen == SHA_BLOCK_SIZE) {
|
||||
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
|
||||
#endif
|
||||
XTRANSFORM(sha, local);
|
||||
AddLength(sha, SHA_BLOCK_SIZE);
|
||||
sha->buffLen = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int wc_ShaFinal(Sha* sha, byte* hash)
|
||||
{
|
||||
byte* local = (byte*)sha->buffer;
|
||||
|
||||
AddLength(sha, sha->buffLen); /* before adding pads */
|
||||
|
||||
local[sha->buffLen++] = 0x80; /* add 1 */
|
||||
|
||||
/* pad with zeros */
|
||||
if (sha->buffLen > SHA_PAD_SIZE) {
|
||||
XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen);
|
||||
sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen;
|
||||
|
||||
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
|
||||
#endif
|
||||
XTRANSFORM(sha, local);
|
||||
sha->buffLen = 0;
|
||||
}
|
||||
XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen);
|
||||
|
||||
/* put lengths in bits */
|
||||
sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) +
|
||||
(sha->hiLen << 3);
|
||||
sha->loLen = sha->loLen << 3;
|
||||
|
||||
/* store lengths */
|
||||
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
|
||||
ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
|
||||
#endif
|
||||
/* ! length ordering dependent on digest endian type ! */
|
||||
XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
|
||||
XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
|
||||
|
||||
#ifdef FREESCALE_MMCAU
|
||||
/* Kinetis requires only these bytes reversed */
|
||||
ByteReverseWords(&sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
|
||||
&sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
|
||||
2 * sizeof(word32));
|
||||
#endif
|
||||
|
||||
XTRANSFORM(sha, local);
|
||||
#ifdef LITTLE_ENDIAN_ORDER
|
||||
ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
|
||||
#endif
|
||||
XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
|
||||
|
||||
return wc_InitSha(sha); /* reset state */
|
||||
}
|
||||
|
||||
#endif /* STM32F2_HASH */
|
||||
|
||||
|
||||
int wc_ShaHash(const byte* data, word32 len, byte* hash)
|
||||
{
|
||||
int ret = 0;
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
Sha* sha;
|
||||
#else
|
||||
Sha sha[1];
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
sha = (Sha*)XMALLOC(sizeof(Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
if (sha == NULL)
|
||||
return MEMORY_E;
|
||||
#endif
|
||||
|
||||
if ((ret = wc_InitSha(sha)) != 0) {
|
||||
WOLFSSL_MSG("wc_InitSha failed");
|
||||
}
|
||||
else {
|
||||
wc_ShaUpdate(sha, data, len);
|
||||
wc_ShaFinal(sha, hash);
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
|
||||
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 */
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,100 +1,100 @@
|
||||
/* md5.h
|
||||
*
|
||||
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
#ifndef WOLF_CRYPT_MD5_H
|
||||
#define WOLF_CRYPT_MD5_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
#ifndef NO_MD5
|
||||
|
||||
#ifdef HAVE_FIPS
|
||||
#define wc_InitMd5 InitMd5
|
||||
#define wc_Md5Update Md5Update
|
||||
#define wc_Md5Final Md5Final
|
||||
#define wc_Md5Hash Md5Hash
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* in bytes */
|
||||
enum {
|
||||
#ifdef STM32F2_HASH
|
||||
MD5_REG_SIZE = 4, /* STM32 register size, bytes */
|
||||
#endif
|
||||
MD5 = 0, /* hash type unique */
|
||||
MD5_BLOCK_SIZE = 64,
|
||||
MD5_DIGEST_SIZE = 16,
|
||||
MD5_PAD_SIZE = 56
|
||||
};
|
||||
|
||||
#if defined(WOLFSSL_PIC32MZ_HASH)
|
||||
#include "port/pic32/pic32mz-crypt.h"
|
||||
#endif
|
||||
|
||||
#ifdef TI_HASH_TEST
|
||||
#include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef WOLFSSL_TI_HASH
|
||||
|
||||
/* MD5 digest */
|
||||
typedef struct Md5 {
|
||||
word32 buffLen; /* in bytes */
|
||||
word32 loLen; /* length in bytes */
|
||||
word32 hiLen; /* length in bytes */
|
||||
word32 buffer[MD5_BLOCK_SIZE / sizeof(word32)];
|
||||
#if !defined(WOLFSSL_PIC32MZ_HASH)
|
||||
word32 digest[MD5_DIGEST_SIZE / sizeof(word32)];
|
||||
#else
|
||||
word32 digest[PIC32_HASH_SIZE / sizeof(word32)];
|
||||
pic32mz_desc desc ; /* Crypt Engine descripter */
|
||||
#endif
|
||||
|
||||
#ifdef TI_HASH_TEST
|
||||
wolfssl_TI_Hash ti ;
|
||||
#endif
|
||||
|
||||
} Md5;
|
||||
|
||||
#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 */
|
||||
/* md5.h
|
||||
*
|
||||
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
#ifndef WOLF_CRYPT_MD5_H
|
||||
#define WOLF_CRYPT_MD5_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
#ifndef NO_MD5
|
||||
|
||||
#ifdef HAVE_FIPS
|
||||
#define wc_InitMd5 InitMd5
|
||||
#define wc_Md5Update Md5Update
|
||||
#define wc_Md5Final Md5Final
|
||||
#define wc_Md5Hash Md5Hash
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* in bytes */
|
||||
enum {
|
||||
#ifdef STM32F2_HASH
|
||||
MD5_REG_SIZE = 4, /* STM32 register size, bytes */
|
||||
#endif
|
||||
MD5 = 0, /* hash type unique */
|
||||
MD5_BLOCK_SIZE = 64,
|
||||
MD5_DIGEST_SIZE = 16,
|
||||
MD5_PAD_SIZE = 56
|
||||
};
|
||||
|
||||
#if defined(WOLFSSL_PIC32MZ_HASH)
|
||||
#include "port/pic32/pic32mz-crypt.h"
|
||||
#endif
|
||||
|
||||
#ifdef TI_HASH_TEST
|
||||
#include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef WOLFSSL_TI_HASH
|
||||
|
||||
/* MD5 digest */
|
||||
typedef struct Md5 {
|
||||
word32 buffLen; /* in bytes */
|
||||
word32 loLen; /* length in bytes */
|
||||
word32 hiLen; /* length in bytes */
|
||||
word32 buffer[MD5_BLOCK_SIZE / sizeof(word32)];
|
||||
#if !defined(WOLFSSL_PIC32MZ_HASH)
|
||||
word32 digest[MD5_DIGEST_SIZE / sizeof(word32)];
|
||||
#else
|
||||
word32 digest[PIC32_HASH_SIZE / sizeof(word32)];
|
||||
pic32mz_desc desc ; /* Crypt Engine descripter */
|
||||
#endif
|
||||
|
||||
#ifdef TI_HASH_TEST
|
||||
wolfssl_TI_Hash ti ;
|
||||
#endif
|
||||
|
||||
} Md5;
|
||||
|
||||
#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 */
|
||||
|
@ -1,88 +1,88 @@
|
||||
/* sha.h
|
||||
*
|
||||
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
#ifndef WOLF_CRYPT_SHA_H
|
||||
#define WOLF_CRYPT_SHA_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
#ifndef NO_SHA
|
||||
|
||||
#ifdef HAVE_FIPS
|
||||
/* for fips @wc_fips */
|
||||
#include <cyassl/ctaocrypt/sha.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_FIPS /* avoid redefining structs */
|
||||
/* in bytes */
|
||||
enum {
|
||||
#ifdef STM32F2_HASH
|
||||
SHA_REG_SIZE = 4, /* STM32 register size, bytes */
|
||||
#endif
|
||||
SHA = 1, /* hash type unique */
|
||||
SHA_BLOCK_SIZE = 64,
|
||||
SHA_DIGEST_SIZE = 20,
|
||||
SHA_PAD_SIZE = 56
|
||||
};
|
||||
|
||||
#ifdef WOLFSSL_PIC32MZ_HASH
|
||||
#include "port/pic32/pic32mz-crypt.h"
|
||||
#endif
|
||||
|
||||
#ifndef WOLFSSL_TI_HASH
|
||||
|
||||
/* Sha digest */
|
||||
typedef struct Sha {
|
||||
word32 buffLen; /* in bytes */
|
||||
word32 loLen; /* length in bytes */
|
||||
word32 hiLen; /* length in bytes */
|
||||
word32 buffer[SHA_BLOCK_SIZE / sizeof(word32)];
|
||||
#ifndef WOLFSSL_PIC32MZ_HASH
|
||||
word32 digest[SHA_DIGEST_SIZE / sizeof(word32)];
|
||||
#else
|
||||
word32 digest[PIC32_HASH_SIZE / sizeof(word32)];
|
||||
pic32mz_desc desc; /* Crypt Engine descripter */
|
||||
#endif
|
||||
} Sha;
|
||||
|
||||
#else /* WOLFSSL_TI_HASH */
|
||||
#include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
|
||||
#endif
|
||||
|
||||
#endif /* HAVE_FIPS */
|
||||
|
||||
WOLFSSL_API int wc_InitSha(Sha*);
|
||||
WOLFSSL_API int wc_ShaUpdate(Sha*, const byte*, word32);
|
||||
WOLFSSL_API int wc_ShaFinal(Sha*, byte*);
|
||||
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 */
|
||||
|
||||
/* sha.h
|
||||
*
|
||||
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
#ifndef WOLF_CRYPT_SHA_H
|
||||
#define WOLF_CRYPT_SHA_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
#ifndef NO_SHA
|
||||
|
||||
#ifdef HAVE_FIPS
|
||||
/* for fips @wc_fips */
|
||||
#include <cyassl/ctaocrypt/sha.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_FIPS /* avoid redefining structs */
|
||||
/* in bytes */
|
||||
enum {
|
||||
#ifdef STM32F2_HASH
|
||||
SHA_REG_SIZE = 4, /* STM32 register size, bytes */
|
||||
#endif
|
||||
SHA = 1, /* hash type unique */
|
||||
SHA_BLOCK_SIZE = 64,
|
||||
SHA_DIGEST_SIZE = 20,
|
||||
SHA_PAD_SIZE = 56
|
||||
};
|
||||
|
||||
#ifdef WOLFSSL_PIC32MZ_HASH
|
||||
#include "port/pic32/pic32mz-crypt.h"
|
||||
#endif
|
||||
|
||||
#ifndef WOLFSSL_TI_HASH
|
||||
|
||||
/* Sha digest */
|
||||
typedef struct Sha {
|
||||
word32 buffLen; /* in bytes */
|
||||
word32 loLen; /* length in bytes */
|
||||
word32 hiLen; /* length in bytes */
|
||||
word32 buffer[SHA_BLOCK_SIZE / sizeof(word32)];
|
||||
#ifndef WOLFSSL_PIC32MZ_HASH
|
||||
word32 digest[SHA_DIGEST_SIZE / sizeof(word32)];
|
||||
#else
|
||||
word32 digest[PIC32_HASH_SIZE / sizeof(word32)];
|
||||
pic32mz_desc desc; /* Crypt Engine descripter */
|
||||
#endif
|
||||
} Sha;
|
||||
|
||||
#else /* WOLFSSL_TI_HASH */
|
||||
#include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
|
||||
#endif
|
||||
|
||||
#endif /* HAVE_FIPS */
|
||||
|
||||
WOLFSSL_API int wc_InitSha(Sha*);
|
||||
WOLFSSL_API int wc_ShaUpdate(Sha*, const byte*, word32);
|
||||
WOLFSSL_API int wc_ShaFinal(Sha*, byte*);
|
||||
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 */
|
||||
|
||||
|
@ -1,86 +1,86 @@
|
||||
/* sha256.h
|
||||
*
|
||||
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
/* code submitted by raphael.huck@efixo.com */
|
||||
|
||||
#ifndef WOLF_CRYPT_SHA256_H
|
||||
#define WOLF_CRYPT_SHA256_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
#ifndef NO_SHA256
|
||||
|
||||
#ifdef HAVE_FIPS
|
||||
/* for fips @wc_fips */
|
||||
#include <cyassl/ctaocrypt/sha256.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_FIPS /* avoid redefinition of structs */
|
||||
#ifdef WOLFSSL_PIC32MZ_HASH
|
||||
#include "port/pic32/pic32mz-crypt.h"
|
||||
#endif
|
||||
|
||||
/* in bytes */
|
||||
enum {
|
||||
SHA256 = 2, /* hash type unique */
|
||||
SHA256_BLOCK_SIZE = 64,
|
||||
SHA256_DIGEST_SIZE = 32,
|
||||
SHA256_PAD_SIZE = 56
|
||||
};
|
||||
|
||||
#ifndef WOLFSSL_TI_HASH
|
||||
|
||||
/* Sha256 digest */
|
||||
typedef struct Sha256 {
|
||||
word32 buffLen; /* in bytes */
|
||||
word32 loLen; /* length in bytes */
|
||||
word32 hiLen; /* length in bytes */
|
||||
word32 digest[SHA256_DIGEST_SIZE / sizeof(word32)];
|
||||
word32 buffer[SHA256_BLOCK_SIZE / sizeof(word32)];
|
||||
#ifdef WOLFSSL_PIC32MZ_HASH
|
||||
pic32mz_desc desc ; /* Crypt Engine descripter */
|
||||
#endif
|
||||
} Sha256;
|
||||
|
||||
#else /* WOLFSSL_TI_HASH */
|
||||
#include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
|
||||
#endif
|
||||
|
||||
#endif /* HAVE_FIPS */
|
||||
|
||||
WOLFSSL_API int wc_InitSha256(Sha256*);
|
||||
WOLFSSL_API int wc_Sha256Update(Sha256*, const byte*, word32);
|
||||
WOLFSSL_API int wc_Sha256Final(Sha256*, byte*);
|
||||
WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*);
|
||||
WOLFSSL_API int wc_Sha256GetHash(Sha256*, byte*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* NO_SHA256 */
|
||||
#endif /* WOLF_CRYPT_SHA256_H */
|
||||
|
||||
/* sha256.h
|
||||
*
|
||||
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
/* code submitted by raphael.huck@efixo.com */
|
||||
|
||||
#ifndef WOLF_CRYPT_SHA256_H
|
||||
#define WOLF_CRYPT_SHA256_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
#ifndef NO_SHA256
|
||||
|
||||
#ifdef HAVE_FIPS
|
||||
/* for fips @wc_fips */
|
||||
#include <cyassl/ctaocrypt/sha256.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_FIPS /* avoid redefinition of structs */
|
||||
#ifdef WOLFSSL_PIC32MZ_HASH
|
||||
#include "port/pic32/pic32mz-crypt.h"
|
||||
#endif
|
||||
|
||||
/* in bytes */
|
||||
enum {
|
||||
SHA256 = 2, /* hash type unique */
|
||||
SHA256_BLOCK_SIZE = 64,
|
||||
SHA256_DIGEST_SIZE = 32,
|
||||
SHA256_PAD_SIZE = 56
|
||||
};
|
||||
|
||||
#ifndef WOLFSSL_TI_HASH
|
||||
|
||||
/* Sha256 digest */
|
||||
typedef struct Sha256 {
|
||||
word32 buffLen; /* in bytes */
|
||||
word32 loLen; /* length in bytes */
|
||||
word32 hiLen; /* length in bytes */
|
||||
word32 digest[SHA256_DIGEST_SIZE / sizeof(word32)];
|
||||
word32 buffer[SHA256_BLOCK_SIZE / sizeof(word32)];
|
||||
#ifdef WOLFSSL_PIC32MZ_HASH
|
||||
pic32mz_desc desc ; /* Crypt Engine descripter */
|
||||
#endif
|
||||
} Sha256;
|
||||
|
||||
#else /* WOLFSSL_TI_HASH */
|
||||
#include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
|
||||
#endif
|
||||
|
||||
#endif /* HAVE_FIPS */
|
||||
|
||||
WOLFSSL_API int wc_InitSha256(Sha256*);
|
||||
WOLFSSL_API int wc_Sha256Update(Sha256*, const byte*, word32);
|
||||
WOLFSSL_API int wc_Sha256Final(Sha256*, byte*);
|
||||
WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*);
|
||||
WOLFSSL_API int wc_Sha256GetHash(Sha256*, byte*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* NO_SHA256 */
|
||||
#endif /* WOLF_CRYPT_SHA256_H */
|
||||
|
||||
|
Reference in New Issue
Block a user