mirror of
https://gitlab.nic.cz/labs/bird.git
synced 2024-12-23 18:21:54 +00:00
203 lines
6.2 KiB
C
203 lines
6.2 KiB
C
|
/*
|
||
|
* BIRD Library -- HMAC-SHA1 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/sha1.h"
|
||
|
#include "lib/sha1.c" /* REMOVE ME */
|
||
|
#include "lib/sha1_hmac.c" /* REMOVE ME */
|
||
|
|
||
|
#define HMAC_BUFFER_SIZE 80
|
||
|
struct hmac_data_in {
|
||
|
byte key[HMAC_BUFFER_SIZE];
|
||
|
uint key_len;
|
||
|
byte data[HMAC_BUFFER_SIZE];
|
||
|
uint data_len;
|
||
|
};
|
||
|
|
||
|
static void
|
||
|
get_sha1_hmac(const struct hmac_data_in in, char (*out_hash)[SHA1_HEX_SIZE])
|
||
|
{
|
||
|
sha1_hmac_context ctx;
|
||
|
sha1_hmac_init(&ctx, in.key, in.key_len);
|
||
|
sha1_hmac_update(&ctx, in.data, in.data_len);
|
||
|
byte *hash_byte = sha1_hmac_final(&ctx);
|
||
|
|
||
|
int i;
|
||
|
for (i = 0; i < SHA1_SIZE; i++)
|
||
|
sprintf(*out_hash + i*2, "%02x", hash_byte[i]);
|
||
|
}
|
||
|
|
||
|
static int
|
||
|
t_sha1(void)
|
||
|
{
|
||
|
struct in_out {
|
||
|
struct hmac_data_in in;
|
||
|
char out[SHA1_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 = "b617318655057264e28bc0b6fb378c8ef146be00",
|
||
|
},
|
||
|
{
|
||
|
.in = {
|
||
|
.key = "Jefe",
|
||
|
.key_len = 4,
|
||
|
.data = "what do ya want for nothing?",
|
||
|
.data_len = 28,
|
||
|
},
|
||
|
.out = "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
|
||
|
},
|
||
|
{
|
||
|
.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 = "125d7342b9ac11cd91a39af48aa17b4f63f175d3",
|
||
|
},
|
||
|
{
|
||
|
.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 = "4c9007f4026250c6bc8414f9bf50c86c2d7235da",
|
||
|
},
|
||
|
{
|
||
|
.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 = "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
|
||
|
},
|
||
|
{
|
||
|
.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, 0xaa,
|
||
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||
|
},
|
||
|
.key_len = 80,
|
||
|
.data = "Test Using Larger Than Block-Size Key - Hash Key First",
|
||
|
.data_len = 54,
|
||
|
},
|
||
|
.out = "aa4ae5e15272d00e95705637ce8a3b55ed402112",
|
||
|
},
|
||
|
{
|
||
|
.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, 0xaa,
|
||
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||
|
},
|
||
|
.key_len = 80,
|
||
|
.data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
|
||
|
.data_len = 73,
|
||
|
},
|
||
|
.out = "e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
|
||
|
},
|
||
|
{
|
||
|
.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, 0xaa,
|
||
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||
|
},
|
||
|
.key_len = 80,
|
||
|
.data = "Test Using Larger Than Block-Size Key - Hash Key First",
|
||
|
.data_len = 54,
|
||
|
},
|
||
|
.out = "aa4ae5e15272d00e95705637ce8a3b55ed402112",
|
||
|
},
|
||
|
{
|
||
|
.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, 0xaa,
|
||
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||
|
},
|
||
|
.key_len = 80,
|
||
|
.data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
|
||
|
.data_len = 73,
|
||
|
},
|
||
|
.out = "e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
|
||
|
},
|
||
|
};
|
||
|
|
||
|
bt_assert_fn_in_out(get_sha1_hmac, in_out, NULL, "'%s'");
|
||
|
|
||
|
return BT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
int
|
||
|
main(int argc, char *argv[])
|
||
|
{
|
||
|
bt_init(argc, argv);
|
||
|
|
||
|
bt_test_suite(t_sha1, "Test Suite by RFC 2202");
|
||
|
|
||
|
return bt_end();
|
||
|
}
|