2015-03-27 13:09:27 +00:00
|
|
|
/*
|
2015-05-19 07:57:10 +00:00
|
|
|
* BIRD Library -- MD5 and HMAC-MD5 Tests
|
2015-03-27 13:09:27 +00:00
|
|
|
*
|
|
|
|
* (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"
|
|
|
|
|
|
|
|
static void
|
2015-04-22 09:02:46 +00:00
|
|
|
get_md5(const char *str, char (*out_hash)[MD5_HEX_SIZE])
|
2015-03-27 13:09:27 +00:00
|
|
|
{
|
2015-05-19 07:57:10 +00:00
|
|
|
struct md5_context ctxt;
|
2015-03-27 13:09:27 +00:00
|
|
|
|
2015-05-13 07:32:00 +00:00
|
|
|
md5_init(&ctxt);
|
|
|
|
md5_update(&ctxt, str, strlen(str));
|
2015-05-13 08:55:02 +00:00
|
|
|
byte *hash = md5_final(&ctxt);
|
2015-03-27 13:09:27 +00:00
|
|
|
|
|
|
|
int i;
|
2015-04-22 09:02:46 +00:00
|
|
|
for(i = 0; i < MD5_SIZE; i++)
|
2015-04-13 08:57:47 +00:00
|
|
|
sprintf(*out_hash + i*2, "%02x", hash[i]);
|
2015-03-27 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
t_md5(void)
|
|
|
|
{
|
2015-04-15 10:11:50 +00:00
|
|
|
struct in_out {
|
2015-04-13 08:57:47 +00:00
|
|
|
char *in;
|
2015-04-22 09:02:46 +00:00
|
|
|
char out[MD5_HEX_SIZE];
|
2015-04-15 10:11:50 +00:00
|
|
|
} in_out[] = {
|
2015-04-13 08:57:47 +00:00
|
|
|
{
|
|
|
|
.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",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-04-22 09:02:46 +00:00
|
|
|
bt_assert_fn_in_out(get_md5, in_out, "'%s'", "'%s'");
|
2015-03-27 13:09:27 +00:00
|
|
|
|
|
|
|
return BT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-05-13 09:14:11 +00:00
|
|
|
#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])
|
|
|
|
{
|
2015-05-19 07:57:10 +00:00
|
|
|
struct md5_hmac_context ctx;
|
2015-05-13 09:14:11 +00:00
|
|
|
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, 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c, 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;
|
|
|
|
}
|
|
|
|
|
2015-03-27 13:09:27 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
bt_init(argc, argv);
|
|
|
|
|
2015-05-13 09:14:11 +00:00
|
|
|
bt_test_suite(t_md5, "Test Suite by RFC 1321");
|
|
|
|
bt_test_suite(t_md5_hmac, "Test Suite by RFC 2202");
|
2015-03-27 13:09:27 +00:00
|
|
|
|
2015-04-13 08:52:21 +00:00
|
|
|
return bt_end();
|
2015-03-27 13:09:27 +00:00
|
|
|
}
|