2013-03-20 21:14:06 +04:00
|
|
|
/* crypto.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2013 wolfSSL Inc.
|
|
|
|
*
|
|
|
|
* This file is part of CyaSSL.
|
|
|
|
*
|
|
|
|
* CyaSSL 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.
|
|
|
|
*
|
|
|
|
* CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Implements Microchip CRYPTO API layer */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "crypto.h"
|
|
|
|
|
|
|
|
#include <cyassl/ctaocrypt/md5.h>
|
|
|
|
#include <cyassl/ctaocrypt/sha.h>
|
|
|
|
#include <cyassl/ctaocrypt/sha256.h>
|
|
|
|
#include <cyassl/ctaocrypt/sha512.h>
|
2013-03-20 22:59:27 +04:00
|
|
|
#include <cyassl/ctaocrypt/hmac.h>
|
2013-03-21 01:37:05 +04:00
|
|
|
#include <cyassl/ctaocrypt/compress.h>
|
2013-03-20 21:14:06 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* Initialize MD5 */
|
|
|
|
int CRYPT_MD5_Initialize(CRYPT_MD5_CTX* md5)
|
|
|
|
{
|
|
|
|
typedef char md5_test[sizeof(CRYPT_MD5_CTX) >= sizeof(Md5) ? 1 : -1];
|
|
|
|
(void)sizeof(md5_test);
|
|
|
|
|
|
|
|
InitMd5((Md5*)md5);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add data to MD5 */
|
|
|
|
int CRYPT_MD5_DataAdd(CRYPT_MD5_CTX* md5, const unsigned char* input,
|
|
|
|
unsigned int sz)
|
|
|
|
{
|
|
|
|
Md5Update((Md5*)md5, input, sz);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Get MD5 Final into digest */
|
|
|
|
int CRYPT_MD5_Finalize(CRYPT_MD5_CTX* md5, unsigned char* digest)
|
|
|
|
{
|
|
|
|
Md5Final((Md5*)md5, digest);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize SHA */
|
|
|
|
int CRYPT_SHA_Initialize(CRYPT_SHA_CTX* sha)
|
|
|
|
{
|
|
|
|
typedef char sha_test[sizeof(CRYPT_SHA_CTX) >= sizeof(Sha) ? 1 : -1];
|
|
|
|
(void)sizeof(sha_test);
|
|
|
|
|
|
|
|
InitSha((Sha*)sha);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add data to SHA */
|
|
|
|
int CRYPT_SHA_DataAdd(CRYPT_SHA_CTX* sha, const unsigned char* input,
|
|
|
|
unsigned int sz)
|
|
|
|
{
|
|
|
|
ShaUpdate((Sha*)sha, input, sz);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Get SHA Final into digest */
|
|
|
|
int CRYPT_SHA_Finalize(CRYPT_SHA_CTX* sha, unsigned char* digest)
|
|
|
|
{
|
|
|
|
ShaFinal((Sha*)sha, digest);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize SHA-256 */
|
|
|
|
int CRYPT_SHA256_Initialize(CRYPT_SHA256_CTX* sha256)
|
|
|
|
{
|
|
|
|
typedef char sha_test[sizeof(CRYPT_SHA256_CTX) >= sizeof(Sha256) ? 1 : -1];
|
|
|
|
(void)sizeof(sha_test);
|
|
|
|
|
|
|
|
InitSha256((Sha256*)sha256);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add data to SHA-256 */
|
|
|
|
int CRYPT_SHA256_DataAdd(CRYPT_SHA256_CTX* sha256, const unsigned char* input,
|
|
|
|
unsigned int sz)
|
|
|
|
{
|
|
|
|
Sha256Update((Sha256*)sha256, input, sz);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Get SHA-256 Final into digest */
|
|
|
|
int CRYPT_SHA256_Finalize(CRYPT_SHA256_CTX* sha256, unsigned char* digest)
|
|
|
|
{
|
|
|
|
Sha256Final((Sha256*)sha256, digest);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize SHA-384 */
|
|
|
|
int CRYPT_SHA384_Initialize(CRYPT_SHA384_CTX* sha384)
|
|
|
|
{
|
|
|
|
typedef char sha_test[sizeof(CRYPT_SHA384_CTX) >= sizeof(Sha384) ? 1 : -1];
|
|
|
|
(void)sizeof(sha_test);
|
|
|
|
|
|
|
|
InitSha384((Sha384*)sha384);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add data to SHA-384 */
|
|
|
|
int CRYPT_SHA384_DataAdd(CRYPT_SHA384_CTX* sha384, const unsigned char* input,
|
|
|
|
unsigned int sz)
|
|
|
|
{
|
|
|
|
Sha384Update((Sha384*)sha384, input, sz);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Get SHA-384 Final into digest */
|
|
|
|
int CRYPT_SHA384_Finalize(CRYPT_SHA384_CTX* sha384, unsigned char* digest)
|
|
|
|
{
|
|
|
|
Sha384Final((Sha384*)sha384, digest);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize SHA-512 */
|
|
|
|
int CRYPT_SHA512_Initialize(CRYPT_SHA512_CTX* sha512)
|
|
|
|
{
|
|
|
|
typedef char sha_test[sizeof(CRYPT_SHA512_CTX) >= sizeof(Sha512) ? 1 : -1];
|
|
|
|
(void)sizeof(sha_test);
|
|
|
|
|
|
|
|
InitSha512((Sha512*)sha512);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add data to SHA-512 */
|
|
|
|
int CRYPT_SHA512_DataAdd(CRYPT_SHA512_CTX* sha512, const unsigned char* input,
|
|
|
|
unsigned int sz)
|
|
|
|
{
|
|
|
|
Sha512Update((Sha512*)sha512, input, sz);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Get SHA-512 Final into digest */
|
|
|
|
int CRYPT_SHA512_Finalize(CRYPT_SHA512_CTX* sha512, unsigned char* digest)
|
|
|
|
{
|
|
|
|
Sha512Final((Sha512*)sha512, digest);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-20 22:59:27 +04:00
|
|
|
/* Set HMAC key with type */
|
|
|
|
int CRYPT_HMAC_SetKey(CRYPT_HMAC_CTX* hmac, int type, const unsigned char* key,
|
|
|
|
unsigned int sz)
|
|
|
|
{
|
|
|
|
typedef char hmac_test[sizeof(CRYPT_HMAC_CTX) >= sizeof(Hmac) ? 1 : -1];
|
|
|
|
(void)sizeof(hmac_test);
|
|
|
|
|
|
|
|
if (type != CRYPT_HMAC_SHA && type != CRYPT_HMAC_SHA256 &&
|
|
|
|
type != CRYPT_HMAC_SHA384 && type != CRYPT_HMAC_SHA512) {
|
|
|
|
return -1; /* bad hmac type */
|
|
|
|
}
|
|
|
|
|
|
|
|
HmacSetKey((Hmac*)hmac, type, key, sz);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CRYPT_HMAC_DataAdd(CRYPT_HMAC_CTX* hmac, const unsigned char* input,
|
|
|
|
unsigned int sz)
|
|
|
|
{
|
|
|
|
HmacUpdate((Hmac*)hmac, input, sz);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Get HMAC Final into digest */
|
|
|
|
int CRYPT_HMAC_Finalize(CRYPT_HMAC_CTX* hmac, unsigned char* digest)
|
|
|
|
{
|
|
|
|
HmacFinal((Hmac*)hmac, digest);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-21 01:37:05 +04:00
|
|
|
/* Huffman Compression, set flag to do static, otherwise dynamic */
|
|
|
|
/* return compressed size, otherwise < 0 for error */
|
|
|
|
int CRYPT_HUFFMAN_Compress(unsigned char* out, unsigned int outSz,
|
|
|
|
const unsigned char* in, unsigned int inSz,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
return Compress(out, outSz, in, inSz, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Huffman DeCompression, self determines type */
|
|
|
|
/* return decompressed size, otherwise < 0 for error */
|
|
|
|
int CRYPT_HUFFMAN_DeCompress(unsigned char* out, unsigned int outSz,
|
|
|
|
const unsigned char* in, unsigned int inSz)
|
|
|
|
{
|
|
|
|
return DeCompress(out, outSz, in, inSz);
|
|
|
|
}
|
|
|
|
|
|
|
|
|