cryb-to/t/t_rc4.c
2015-10-03 14:43:33 +00:00

287 lines
7.9 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*-
* Copyright (c) 2015 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "cryb/impl.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <cryb/rc4.h>
#include "t.h"
struct t_case {
const char *desc;
unsigned int keylen;
const uint8_t key[32];
const uint8_t stream[32];
};
/***************************************************************************
* Test cases
*/
static struct t_case t_cases[] = {
/* test vectors from RFC 6229 */
{
.desc ="RFC 6229 test vector 1",
.keylen = 5,
.key = {
0x01, 0x02, 0x03, 0x04, 0x05,
},
.stream = {
0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27,
0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8,
0x69, 0x82, 0x94, 0x4f, 0x18, 0xfc, 0x82, 0xd5,
0x89, 0xc4, 0x03, 0xa4, 0x7a, 0x0d, 0x09, 0x19,
},
},
{
.desc ="RFC 6229 test vector 2",
.keylen = 7,
.key = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
},
.stream = {
0x29, 0x3f, 0x02, 0xd4, 0x7f, 0x37, 0xc9, 0xb6,
0x33, 0xf2, 0xaf, 0x52, 0x85, 0xfe, 0xb4, 0x6b,
0xe6, 0x20, 0xf1, 0x39, 0x0d, 0x19, 0xbd, 0x84,
0xe2, 0xe0, 0xfd, 0x75, 0x20, 0x31, 0xaf, 0xc1,
},
},
{
.desc ="RFC 6229 test vector 3",
.keylen = 8,
.key = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
},
.stream = {
0x97, 0xab, 0x8a, 0x1b, 0xf0, 0xaf, 0xb9, 0x61,
0x32, 0xf2, 0xf6, 0x72, 0x58, 0xda, 0x15, 0xa8,
0x82, 0x63, 0xef, 0xdb, 0x45, 0xc4, 0xa1, 0x86,
0x84, 0xef, 0x87, 0xe6, 0xb1, 0x9e, 0x5b, 0x09,
},
},
{
.desc ="RFC 6229 test vector 4",
.keylen = 10,
.key = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a,
},
.stream = {
0xed, 0xe3, 0xb0, 0x46, 0x43, 0xe5, 0x86, 0xcc,
0x90, 0x7d, 0xc2, 0x18, 0x51, 0x70, 0x99, 0x02,
0x03, 0x51, 0x6b, 0xa7, 0x8f, 0x41, 0x3b, 0xeb,
0x22, 0x3a, 0xa5, 0xd4, 0xd2, 0xdf, 0x67, 0x11,
},
},
{
.desc ="RFC 6229 test vector 5",
.keylen = 16,
.key = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
},
.stream = {
0x9a, 0xc7, 0xcc, 0x9a, 0x60, 0x9d, 0x1e, 0xf7,
0xb2, 0x93, 0x28, 0x99, 0xcd, 0xe4, 0x1b, 0x97,
0x52, 0x48, 0xc4, 0x95, 0x90, 0x14, 0x12, 0x6a,
0x6e, 0x8a, 0x84, 0xf1, 0x1d, 0x1a, 0x9e, 0x1c,
},
},
{
.desc ="RFC 6229 test vector 6",
.keylen = 24,
.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,
},
.stream = {
0x05, 0x95, 0xe5, 0x7f, 0xe5, 0xf0, 0xbb, 0x3c,
0x70, 0x6e, 0xda, 0xc8, 0xa4, 0xb2, 0xdb, 0x11,
0xdf, 0xde, 0x31, 0x34, 0x4a, 0x1a, 0xf7, 0x69,
0xc7, 0x4f, 0x07, 0x0a, 0xee, 0x9e, 0x23, 0x26,
},
},
{
.desc ="RFC 6229 test vector 7",
.keylen = 32,
.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, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
},
.stream = {
0xea, 0xa6, 0xbd, 0x25, 0x88, 0x0b, 0xf9, 0x3d,
0x3f, 0x5d, 0x1e, 0x4c, 0xa2, 0x61, 0x1d, 0x91,
0xcf, 0xa4, 0x5c, 0x9f, 0x7e, 0x71, 0x4b, 0x54,
0xbd, 0xfa, 0x80, 0x02, 0x7c, 0xb1, 0x43, 0x80,
},
},
{
.desc ="RFC 6229 test vector 8",
.keylen = 5,
.key = {
0x83, 0x32, 0x22, 0x77, 0x2a,
},
.stream = {
0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a,
0x2e, 0x87, 0x9e, 0x92, 0xa4, 0x97, 0xef, 0xda,
0x20, 0xf0, 0x60, 0xc2, 0xf2, 0xe5, 0x12, 0x65,
0x01, 0xd3, 0xd4, 0xfe, 0xa1, 0x0d, 0x5f, 0xc0,
},
},
{
.desc ="RFC 6229 test vector 9",
.keylen = 7,
.key = {
0x19, 0x10, 0x83, 0x32, 0x22, 0x77, 0x2a,
},
.stream = {
0xbc, 0x92, 0x22, 0xdb, 0xd3, 0x27, 0x4d, 0x8f,
0xc6, 0x6d, 0x14, 0xcc, 0xbd, 0xa6, 0x69, 0x0b,
0x7a, 0xe6, 0x27, 0x41, 0x0c, 0x9a, 0x2b, 0xe6,
0x93, 0xdf, 0x5b, 0xb7, 0x48, 0x5a, 0x63, 0xe3,
},
},
{
.desc ="RFC 6229 test vector 10",
.keylen = 8,
.key = {
0x64, 0x19, 0x10, 0x83, 0x32, 0x22, 0x77, 0x2a,
},
.stream = {
0xbb, 0xf6, 0x09, 0xde, 0x94, 0x13, 0x17, 0x2d,
0x07, 0x66, 0x0c, 0xb6, 0x80, 0x71, 0x69, 0x26,
0x46, 0x10, 0x1a, 0x6d, 0xab, 0x43, 0x11, 0x5d,
0x6c, 0x52, 0x2b, 0x4f, 0xe9, 0x36, 0x04, 0xa9,
},
},
{
.desc ="RFC 6229 test vector 11",
.keylen = 10,
.key = {
0x8b, 0x37, 0x64, 0x19, 0x10, 0x83, 0x32, 0x22,
0x77, 0x2a,
},
.stream = {
0xab, 0x65, 0xc2, 0x6e, 0xdd, 0xb2, 0x87, 0x60,
0x0d, 0xb2, 0xfd, 0xa1, 0x0d, 0x1e, 0x60, 0x5c,
0xbb, 0x75, 0x90, 0x10, 0xc2, 0x96, 0x58, 0xf2,
0xc7, 0x2d, 0x93, 0xa2, 0xd1, 0x6d, 0x29, 0x30,
},
},
{
.desc ="RFC 6229 test vector 12",
.keylen = 16,
.key = {
0xeb, 0xb4, 0x62, 0x27, 0xc6, 0xcc, 0x8b, 0x37,
0x64, 0x19, 0x10, 0x83, 0x32, 0x22, 0x77, 0x2a,
},
.stream = {
0x72, 0x0c, 0x94, 0xb6, 0x3e, 0xdf, 0x44, 0xe1,
0x31, 0xd9, 0x50, 0xca, 0x21, 0x1a, 0x5a, 0x30,
0xc3, 0x66, 0xfd, 0xea, 0xcf, 0x9c, 0xa8, 0x04,
0x36, 0xbe, 0x7c, 0x35, 0x84, 0x24, 0xd2, 0x0b,
},
},
{
.desc ="RFC 6229 test vector 13",
.keylen = 24,
.key = {
0xc1, 0x09, 0x16, 0x39, 0x08, 0xeb, 0xe5, 0x1d,
0xeb, 0xb4, 0x62, 0x27, 0xc6, 0xcc, 0x8b, 0x37,
0x64, 0x19, 0x10, 0x83, 0x32, 0x22, 0x77, 0x2a,
},
.stream = {
0x54, 0xb6, 0x4e, 0x6b, 0x5a, 0x20, 0xb5, 0xe2,
0xec, 0x84, 0x59, 0x3d, 0xc7, 0x98, 0x9d, 0xa7,
0xc1, 0x35, 0xee, 0xe2, 0x37, 0xa8, 0x54, 0x65,
0xff, 0x97, 0xdc, 0x03, 0x92, 0x4f, 0x45, 0xce,
},
},
{
.desc ="RFC 6229 test vector 14",
.keylen = 32,
.key = {
0x1a, 0xda, 0x31, 0xd5, 0xcf, 0x68, 0x82, 0x21,
0xc1, 0x09, 0x16, 0x39, 0x08, 0xeb, 0xe5, 0x1d,
0xeb, 0xb4, 0x62, 0x27, 0xc6, 0xcc, 0x8b, 0x37,
0x64, 0x19, 0x10, 0x83, 0x32, 0x22, 0x77, 0x2a,
},
.stream = {
0xdd, 0x5b, 0xcb, 0x00, 0x18, 0xe9, 0x22, 0xd4,
0x94, 0x75, 0x9d, 0x7c, 0x39, 0x5d, 0x02, 0xd3,
0xc8, 0x44, 0x6f, 0x8f, 0x77, 0xab, 0xf7, 0x37,
0x68, 0x53, 0x53, 0xeb, 0x89, 0xa1, 0xc9, 0xeb,
},
},
};
/***************************************************************************
* Test functions
*/
static int
t_rc4(char **desc CRYB_UNUSED, void *arg)
{
struct t_case *t = arg;
uint8_t out[32];
rc4_ctx ctx;
rc4_init(&ctx, CIPHER_MODE_ENCRYPT, t->key, t->keylen);
rc4_update(&ctx, t_zero, 32, out);
rc4_finish(&ctx);
return (t_compare_mem(t->stream, out, 32));
}
/***************************************************************************
* Boilerplate
*/
int
t_prepare(int argc, char *argv[])
{
unsigned int i, n;
(void)argc;
(void)argv;
n = sizeof t_cases / sizeof t_cases[0];
for (i = 0; i < n; ++i)
t_add_test(t_rc4, &t_cases[i], t_cases[i].desc);
return (0);
}
void
t_cleanup(void)
{
}