mirror of
https://gitlab.nic.cz/labs/bird.git
synced 2025-01-10 19:11:54 +00:00
180 lines
5.7 KiB
C
180 lines
5.7 KiB
C
/*
|
|
* BIRD Library -- HMAC-SHA256 Message Authentication Tests
|
|
*
|
|
* (c) 2015 CZ.NIC z.s.p.o.
|
|
*
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
*/
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "test/birdtest.h"
|
|
#include "sysdep/config.h"
|
|
#include "lib/sha256_hmac.h"
|
|
#include "lib/sha256_hmac.c" /* REMOVE ME */
|
|
|
|
#define HMAC_BUFFER_SIZE 160
|
|
struct hmac_data_in {
|
|
byte key[HMAC_BUFFER_SIZE];
|
|
uint key_len;
|
|
byte data[HMAC_BUFFER_SIZE];
|
|
uint data_len;
|
|
};
|
|
|
|
static void
|
|
get_sha256_hmac(const struct hmac_data_in in, char (*out_hash)[SHA256_HEX_SIZE])
|
|
{
|
|
sha256_hmac_context ctx;
|
|
sha256_hmac_init(&ctx, in.key, in.key_len);
|
|
sha256_hmac_update(&ctx, in.data, in.data_len);
|
|
byte *hash_byte = sha256_hmac_final(&ctx);
|
|
|
|
int i;
|
|
for (i = 0; i < SHA256_SIZE; i++)
|
|
sprintf(*out_hash + i*2, "%02x", hash_byte[i]);
|
|
}
|
|
|
|
static int
|
|
t_sha256_hmac(void)
|
|
{
|
|
struct in_out {
|
|
struct hmac_data_in in;
|
|
char out[SHA256_HEX_SIZE];
|
|
} in_out[] = {
|
|
{
|
|
.in = {
|
|
.key = {
|
|
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
|
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
|
},
|
|
.key_len = 20,
|
|
.data = "Hi There",
|
|
.data_len = 8,
|
|
},
|
|
.out = "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7",
|
|
},
|
|
{
|
|
.in = {
|
|
.key = "Jefe",
|
|
.key_len = 4,
|
|
.data = "what do ya want for nothing?",
|
|
.data_len = 28,
|
|
},
|
|
.out = "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
|
|
},
|
|
{
|
|
.in = {
|
|
.key = {
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
},
|
|
.key_len = 20,
|
|
.data = {
|
|
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
|
|
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
|
|
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
|
|
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
|
|
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
|
|
},
|
|
.data_len = 50,
|
|
},
|
|
.out = "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe",
|
|
},
|
|
{
|
|
.in = {
|
|
.key = {
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
|
|
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
|
|
0x15, 0x16, 0x17, 0x18, 0x19,
|
|
},
|
|
.key_len = 25,
|
|
.data = {
|
|
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
|
|
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
|
|
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
|
|
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
|
|
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
|
|
},
|
|
.data_len = 50,
|
|
},
|
|
.out = "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b",
|
|
},
|
|
{
|
|
.in = {
|
|
.key = {
|
|
0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
|
|
0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
|
|
},
|
|
.key_len = 20,
|
|
.data = "Test With Truncation",
|
|
.data_len = 20,
|
|
},
|
|
.out = "a3b6167473100ee06e0c796c2955552bfa6f7c0a6a8aef8b93f860aab0cd20c5",
|
|
},
|
|
{
|
|
.in = {
|
|
.key = {
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa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, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
0xaa,
|
|
},
|
|
.key_len = 131,
|
|
.data = "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.",
|
|
.data_len = 152,
|
|
},
|
|
.out = "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2",
|
|
},
|
|
};
|
|
|
|
bt_assert_fn_in_out(get_sha256_hmac, in_out, NULL, "'%s'");
|
|
|
|
return BT_SUCCESS;
|
|
}
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
bt_init(argc, argv);
|
|
|
|
bt_test_suite(t_sha256_hmac, "Test Suite by RFC 4231");
|
|
// bt_test_suite(t_sha224_hmac, "Test Suite by RFC 4231");
|
|
|
|
return bt_end();
|
|
}
|