Commit c34dc732 authored by Davide Galassi's avatar Davide Galassi

SHA-256 NIST CAVS test vectors

parent 85c77e0f
......@@ -24,13 +24,47 @@ typedef struct cry_sha256_ctx cry_sha256_ctx;
extern "C" {
#endif
/**
* Initialize SHA-256 context.
*
* @param ctx SHA-256 context to be initialized
*/
void cry_sha256_init(struct cry_sha256_ctx *ctx);
/**
* Clear SHA-256 context.
*
* @param ctx SHA-256 context to be cleared
*/
void cry_sha256_clear(cry_sha256_ctx *ctx);
/**
* SHA-256 process buffer.
*
* @param ctx SHA-256 context.
* @param data Buffer holding the data.
* @param len Length of the input data.
*/
void cry_sha256_update(struct cry_sha256_ctx *ctx, const unsigned char *data,
size_t size);
/**
* Final digest.
*
* @param ctx SHA-256 context
* @param out SHA-256 result
*/
void cry_sha256_digest(struct cry_sha256_ctx *ctx, unsigned char *digest);
/**
* SHA-256 computation of a given input.
*
* @out SHA-256 result buffer (shall be at least 32 bytes).
* @data Input data buffer.
* @len Input data length.
*/
void cry_sha256(unsigned char *out, const unsigned char *data, size_t len);
#ifdef __cplusplus
}
#endif
......
#include <cry/sha256.h>
#include <string.h>
#include "../misc.h"
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
......@@ -11,8 +12,7 @@
#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
static const uint32_t k[64] =
{
static const uint32_t k[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
......@@ -31,7 +31,7 @@ static const uint32_t k[64] =
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
static void cry_sha256_transform(struct cry_sha256_ctx *ctx,
static void cry_sha256_transform(cry_sha256_ctx *ctx,
const unsigned char *data)
{
uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
......@@ -76,10 +76,10 @@ static void cry_sha256_transform(struct cry_sha256_ctx *ctx,
ctx->state[7] += h;
}
void cry_sha256_init(struct cry_sha256_ctx *ctx)
void cry_sha256_init(cry_sha256_ctx *ctx)
{
ctx->datalen = 0;
ctx->bitlen = 0;
memset(ctx, 0, sizeof(*ctx));
/* Load magic initialization constants. */
ctx->state[0] = 0x6a09e667;
ctx->state[1] = 0xbb67ae85;
ctx->state[2] = 0x3c6ef372;
......@@ -90,7 +90,12 @@ void cry_sha256_init(struct cry_sha256_ctx *ctx)
ctx->state[7] = 0x5be0cd19;
}
void cry_sha256_update(struct cry_sha256_ctx *ctx, const unsigned char *data,
void cry_sha256_clear(cry_sha256_ctx *ctx)
{
cry_memset(ctx, 0, sizeof(*ctx));
}
void cry_sha256_update(cry_sha256_ctx *ctx, const unsigned char *data,
size_t size)
{
size_t i;
......@@ -106,7 +111,7 @@ void cry_sha256_update(struct cry_sha256_ctx *ctx, const unsigned char *data,
}
}
void cry_sha256_digest(struct cry_sha256_ctx *ctx, unsigned char *digest)
void cry_sha256_digest(cry_sha256_ctx *ctx, unsigned char *digest)
{
size_t i = ctx->datalen;
......@@ -154,3 +159,13 @@ void cry_sha256_digest(struct cry_sha256_ctx *ctx, unsigned char *digest)
digest[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0xff;
}
}
void cry_sha256(unsigned char *out, const unsigned char *data, size_t len)
{
cry_sha256_ctx ctx;
cry_sha256_init(&ctx);
cry_sha256_update(&ctx, data, len);
cry_sha256_digest(&ctx, out);
cry_sha256_clear(&ctx);
}
......@@ -23,8 +23,8 @@ objs := \
cmac_test.o \
rsa_test.o \
sha1_test.o \
# md5_test.o \
sha256_test.o \
# md5_test.o \
rand_test.o \
dh_test.o \
dsa_test.o \
......
#include "test.h"
#include <cry/sha256.h>
static char *input[] = {
"This file is part of CRY software.",
"CRY is free software",
struct sha256_param {
size_t len;
unsigned char *data;
unsigned char hash[CRY_SHA256_DIGEST_SIZE];
};
static const char sha256_hash[] = {
0x0f,0x02,0x31,0x2e,0x48,0xa2,0xdd,0x86,
0x97,0xf2,0xa1,0xd5,0xd4,0x29,0xb6,0x22,
0x20,0x00,0x67,0xaf,0xc1,0x31,0x82,0x4d,
0x57,0x30,0x75,0xda,0xa5,0x82,0x54,0x99
};
static void param_init(struct sha256_param *par, int argc, char *argv[])
{
memset(par, 0, sizeof(*par));
par->len = strlen(argv[0]) >> 1;
par->data = malloc(par->len);
par->len = raw_init(par->data, par->len, argv[0]);
raw_init(par->hash, CRY_SHA256_DIGEST_SIZE, argv[1]);
}
static void sha256_digest(int argc, char *argv[])
{
struct sha256_param par;
unsigned char out[CRY_SHA256_DIGEST_SIZE];
param_init(&par, argc, argv);
cry_sha256(out, par.data, par.len);
ASSERT_EQ_BUF(out, par.hash, CRY_SHA256_DIGEST_SIZE);
free(par.data);
}
static void dispatch(int argc, char *argv[])
{
char *test = *argv;
argv++;
argc--;
ASSERT(argc == 2);
if (strcmp(test, "sha256_digest") == 0)
sha256_digest(argc, argv);
else
printf("Test '%s' not defined\n", test);
}
void sha256_test(void)
{
struct cry_sha256_ctx sha256;
size_t len, i;
cry_sha256_init(&sha256);
for (i = 0; i < ARLEN(input); i++) {
len = strlen(input[i]);
PRINT_ASC("input", input[i], len);
cry_sha256_update(&sha256, input[i], len);
}
cry_sha256_digest(&sha256, buf);
PRINT_HEX("sha256", buf, 32);
ASSERT_EQ_BUF(buf, sha256_hash, 32);
printf("* SHA256 NIST CAVS Vectors\n");
func_test("sha256_test.data", dispatch);
printf("\n");
}
################################################################################
#
# NIST CAVS Test Vectors
#
################################################################################
################################################################################
# SHA-256 vectors
# p0 data
# p1 sha1
################################################################################
SHA-256 Test Vector NIST CAVS #1
sha256_digest
NULL
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
SHA-256 Test Vector NIST CAVS #2
sha256_digest
bd
68325720aabd7c82f30f554b313d0570c95accbb7dc4b5aae11204c08ffe732b
SHA-256 Test Vector NIST CAVS #3
sha256_digest
5fd4
7c4fbf484498d21b487b9d61de8914b2eadaf2698712936d47c3ada2558f6788
SHA-256 Test Vector NIST CAVS #4
sha256_digest
b0bd69
4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803
SHA-256 Test Vector NIST CAVS #5
sha256_digest
c98c8e55
7abc22c0ae5af26ce93dbb94433a0e0b2e119d014f8e7f65bd56c61ccccd9504
SHA-256 Test Vector NIST CAVS #6
sha256_digest
81a723d966
7516fb8bb11350df2bf386bc3c33bd0f52cb4c67c6e4745e0488e62c2aea2605
SHA-256 Test Vector NIST CAVS #7
sha256_digest
8390cf0be07661cc7669aac54ce09a37733a629d45f5d983ef201f9b2d13800e555d9b1097fec3b783d7a50dcb5e2b644b96a1e9463f177cf34906bf388f366db5c2deee04a30e283f764a97c3b377a034fefc22c259214faa99babaff160ab0aaa7e2ccb0ce09c6b32fe08cbc474694375aba703fadbfa31cf685b30a11c57f3cf4edd321e57d3ae6ebb1133c8260e75b9224fa47a2bb205249add2e2e62f817491482ae152322be0900355cdcc8d42a98f82e961a0dc6f537b7b410eff105f59673bfb787bf042aa071f7af68d944d27371c64160fe9382772372516c230c1f45c0d6b6cca7f274b394da9402d3eafdf733994ec58ab22d71829a98399574d4b5908a447a5a681cb0dd50a31145311d92c22a16de1ead66a5499f2dceb4cae694772ce90762ef8336afec653aa9b1a1c4820b221136dfce80dce2ba920d88a530c9410d0a4e0358a3a11052e58dd73b0b179ef8f56fe3b5a2d117a73a0c38a1392b6938e9782e0d86456ee4884e3c39d4d75813f13633bc79baa07c0d2d555afbf207f52b7dca126d015aa2b9873b3eb065e90b9b065a5373fe1fb1b20d594327d19fba56cb81e7b6696605ffa56eba3c27a438697cc21b201fd7e09f18deea1b3ea2f0d1edc02df0e20396a145412cd6b13c32d2e605641c948b714aec30c0649dc44143511f35ab0fd5dd64c34d06fe86f3836dfe9edeb7f08cfc3bd40956826356242191f99f53473f32b0cc0cf9321d6c92a112e8db90b86ee9e87cc32d0343db01e32ce9eb782cb24efbbbeb440fe929e8f2bf8dfb1550a3a2e742e8b455a3e5730e9e6a7a9824d17acc0f72a7f67eae0f0970f8bde46dcdefaed3047cf807e7f00a42e5fd11d40f5e98533d7574425b7d2bc3b3845c443008b58980e768e464e17cc6f6b3939eee52f713963d07d8c4abf02448ef0b889c9671e2f8a436ddeeffcca7176e9bf9d1005ecd377f2fa67c23ed1f137e60bf46018a8bd613d038e883704fc26e798969df35ec7bbc6a4fe46d8910bd82fa3cded265d0a3b6d399e4251e4d8233daa21b5812fded6536198ff13aa5a1cd46a5b9a17a4ddc1d9f85544d1d1cc16f3df858038c8e071a11a7e157a85a6a8dc47e88d75e7009a8b26fdb73f33a2a70f1e0c259f8f9533b9b8f9af9288b7274f21baeec78d396f8bacdcc22471207d9b4efccd3fedc5c5a2214ff5e51c553f35e21ae696fe51e8df733a8e06f50f419e599e9f9e4b37ce643fc810faaa47989771509d69a110ac916261427026369a21263ac4460fb4f708f8ae28599856db7cb6a43ac8e03d64a9609807e76c5f312b9d1863bfa304e8953647648b4f4ab0ed995e
4109cdbec3240ad74cc6c37f39300f70fede16e21efc77f7865998714aad0b5e
......@@ -14,6 +14,7 @@ void hmac_test(void);
void cmac_test(void);
void rsa_test(void);
void sha1_test(void);
void sha256_test(void);
static int g_runs;
int g_fails;
......@@ -43,12 +44,11 @@ struct sub_test g_tests[] = {
SUB_TEST(cmac),
SUB_TEST(rsa),
SUB_TEST(sha1),
SUB_TEST(sha256),
#if 0
TEST_ELEM(rand),
TEST_ELEM(dh),
TEST_ELEM(dsa),
TEST_ELEM(md5),
TEST_ELEM(sha256),
TEST_ELEM(ecp),
TEST_ELEM(ecdh),
TEST_ELEM(ecdsa),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment