add mcapi rng with test

This commit is contained in:
toddouska 2013-03-20 15:02:03 -07:00
parent ae9265fa44
commit dc55de007d
3 changed files with 99 additions and 1 deletions

View File

@ -32,6 +32,7 @@
#include <cyassl/ctaocrypt/sha512.h>
#include <cyassl/ctaocrypt/hmac.h>
#include <cyassl/ctaocrypt/compress.h>
#include <cyassl/ctaocrypt/random.h>
/* Initialize MD5 */
@ -244,3 +245,35 @@ int CRYPT_HUFFMAN_DeCompress(unsigned char* out, unsigned int outSz,
}
/* RNG Initialize, < 0 on error */
int CRYPT_RNG_Initialize(CRYPT_RNG_CTX* rng)
{
typedef char rng_test[sizeof(CRYPT_RNG_CTX) >= sizeof(RNG) ? 1 : -1];
(void)sizeof(rng_test);
return InitRng((RNG*)rng);
}
/* RNG Get single bytes, < 0 on error */
int CRYPT_RNG_Get(CRYPT_RNG_CTX* rng, unsigned char* b)
{
*b = RNG_GenerateByte((RNG*)rng);
return 0;
}
/* RNG Block Generation of sz bytes, < 0 on error */
int CRYPT_RNG_BlockGenerate(CRYPT_RNG_CTX* rng, unsigned char* b,
unsigned int sz)
{
RNG_GenerateBlock((RNG*)rng, b, sz);
return 0;
}

View File

@ -130,6 +130,16 @@ enum {
};
/* RNG */
typedef struct CRYPT_RNG_CTX {
int holder[66]; /* big enough to hold internal, but check on init */
} CRYPT_RNG_CTX;
int CRYPT_RNG_Initialize(CRYPT_RNG_CTX*);
int CRYPT_RNG_Get(CRYPT_RNG_CTX*, unsigned char*);
int CRYPT_RNG_BlockGenerate(CRYPT_RNG_CTX*, unsigned char*, unsigned int);
#ifdef __cplusplus
} /* extern "C" */

View File

@ -34,6 +34,7 @@
#include <cyassl/ctaocrypt/sha512.h>
#include <cyassl/ctaocrypt/hmac.h>
#include <cyassl/ctaocrypt/compress.h>
#include <cyassl/ctaocrypt/random.h>
/* c stdlib headers */
#include <stdio.h>
@ -50,6 +51,7 @@ static int check_sha384(void);
static int check_sha512(void);
static int check_hmac(void);
static int check_compress(void);
static int check_rng(void);
int main(int argc, char** argv)
@ -108,7 +110,13 @@ int main(int argc, char** argv)
ret = check_compress();
if (ret != 0) {
printf("mcapi check_comopress failed\n");
printf("mcapi check_compress failed\n");
return -1;
}
ret = check_rng();
if (ret != 0) {
printf("mcapi check_rng failed\n");
return -1;
}
@ -433,4 +441,51 @@ static int check_compress(void)
}
#define RANDOM_BYTE_SZ 32
/* check mcapi rng */
static int check_rng(void)
{
CRYPT_RNG_CTX rng;
int ret;
int i;
byte in[RANDOM_BYTE_SZ];
byte out[RANDOM_BYTE_SZ];
for (i = 0; i < RANDOM_BYTE_SZ; i++)
in[i] = (byte)i;
for (i = 0; i < RANDOM_BYTE_SZ; i++)
out[i] = (byte)i;
ret = CRYPT_RNG_Initialize(&rng);
if (ret != 0) {
printf("mcap rng init failed\n");
return -1;
}
ret = CRYPT_RNG_Get(&rng, &out[0]);
if (ret != 0) {
printf("mcap rng get failed\n");
return -1;
}
ret = CRYPT_RNG_BlockGenerate(&rng, out, RANDOM_BYTE_SZ);
if (ret != 0) {
printf("mcap rng block gen failed\n");
return -1;
}
if (memcmp(in, out, RANDOM_BYTE_SZ) == 0) {
printf("mcap rng block gen output failed\n");
return -1;
}
printf("rng mcapi test passed\n");
return 0;
}