0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-24 10:41:54 +00:00
bird/lib/md5_test.c
2015-05-19 10:06:28 +02:00

223 lines
5.8 KiB
C

/*
* BIRD Library -- MD5 and HMAC-MD5 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/md5.h"
#include "lib/md5.c" /* REMOVE ME */
static void
get_md5(const char *str, char (*out_hash)[MD5_HEX_SIZE])
{
struct md5_context ctxt;
md5_init(&ctxt);
md5_update(&ctxt, str, strlen(str));
byte *hash = md5_final(&ctxt);
int i;
for(i = 0; i < MD5_SIZE; i++)
sprintf(*out_hash + i*2, "%02x", hash[i]);
}
static int
t_md5(void)
{
struct in_out {
char *in;
char out[MD5_HEX_SIZE];
} in_out[] = {
{
.in = "",
.out = "d41d8cd98f00b204e9800998ecf8427e",
},
{
.in = "a",
.out = "0cc175b9c0f1b6a831c399e269772661",
},
{
.in = "abc",
.out = "900150983cd24fb0d6963f7d28e17f72",
},
{
.in = "message digest",
.out = "f96b697d7cb7938d525a2f31aaf161d0",
},
{
.in = "abcdefghijklmnopqrstuvwxyz",
.out = "c3fcd3d76192e4007dfb496cca67e13b",
},
{
.in = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
.out = "d174ab98d277d9f5a5611c2c9f419d9f",
},
{
.in = "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
.out = "57edf4a22be3c955ac49da2e2107b67a",
},
};
bt_assert_fn_in_out(get_md5, in_out, "'%s'", "'%s'");
return BT_SUCCESS;
}
#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_md5_hmac(const struct hmac_data_in in, char (*out_hash)[MD5_HEX_SIZE])
{
struct md5_hmac_context ctx;
md5_hmac_init(&ctx, in.key, in.key_len);
md5_hmac_update(&ctx, in.data, in.data_len);
byte *hash_byte = md5_hmac_final(&ctx);
int i;
for (i = 0; i < MD5_SIZE; i++)
sprintf(*out_hash + i*2, "%02x", hash_byte[i]);
}
static int
t_md5_hmac(void)
{
struct in_out {
struct hmac_data_in in;
char out[MD5_HEX_SIZE];
} in_out[] = {
{
.in = {
.key = {
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
},
.key_len = 16,
.data = "Hi There",
.data_len = 8,
},
.out = "9294727a3638bb1c13f48ef8158bfc9d",
},
{
.in = {
.key = "Jefe",
.key_len = 4,
.data = "what do ya want for nothing?",
.data_len = 28,
},
.out = "750c783e6ab0b503eaa86e310a5db738",
},
{
.in = {
.key = {
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
},
.key_len = 16,
.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 = "56be34521d144c88dbb8c733f0e8b3f6",
},
{
.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 = "697eaf0aca3a3aea3a75164746ffaa79",
},
{
.in = {
.key = {
0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
},
.key_len = 16,
.data = "Test With Truncation",
.data_len = 20,
},
.out = "56461ef2342edc00f9bab995690efd4c",
},
{
.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 = "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd",
},
{
.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 = "6f630fad67cda0ee1fb1f562db3aa53e",
},
};
bt_assert_fn_in_out(get_md5_hmac, in_out, NULL, "'%s'");
return BT_SUCCESS;
}
int
main(int argc, char *argv[])
{
bt_init(argc, argv);
bt_test_suite(t_md5, "Test Suite by RFC 1321");
bt_test_suite(t_md5_hmac, "Test Suite by RFC 2202");
return bt_end();
}