libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

libutil/src/test/test_rc2.c (173 lines, 4.2 KiB) -rw-r--r-- blame download

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
// test_crypt.c
// Crypt unit test for libutil
// Copyright (C) 2021, Jakob Wakeling
// MIT Licence

#include "../rc2.h"
#include "unit.h"

#include <stdint.h>
#include <stdio.h>
#include <string.h>

int main(void) {
	{
		uint8_t k[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		uint8_t c[] = { 0xEB, 0xB7, 0x73, 0xF9, 0x93, 0x27, 0x8E, 0xFF };
		struct rc2 ctx; uint8_t r[8] = { 0 };
		
		rc2expand(&ctx, k, 8, 63);
		rc2encrypt(&ctx, p, r);
		
		ASSERT("T000 TEST VECTOR 0 ENCRYPT",
				memcmp(c, r, 8) == 0);
		
		rc2decrypt(&ctx, c, r);
		
		ASSERT("T001 TEST VECTOR 0 DECRYPT",
				memcmp(p, r, 8) == 0);
	}
	
	{
		uint8_t k[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
		uint8_t p[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
		uint8_t c[] = { 0x27, 0x8B, 0x27, 0xE4, 0x2E, 0x2F, 0x0D, 0x49 };
		struct rc2 ctx; uint8_t r[8] = { 0 };
		
		rc2expand(&ctx, k, 8, 64);
		rc2encrypt(&ctx, p, r);
		
		ASSERT("T002 TEST VECTOR 1 ENCRYPT",
				memcmp(c, r, 8) == 0);
		
		rc2decrypt(&ctx, c, r);
		
		ASSERT("T003 TEST VECTOR 1 DECRYPT",
				memcmp(p, r, 8) == 0);
	}
	
	{
		uint8_t k[] = { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		uint8_t p[] = { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
		uint8_t c[] = { 0x30, 0x64, 0x9E, 0xDF, 0x9B, 0xE7, 0xD2, 0xC2 };
		struct rc2 ctx; uint8_t r[8] = { 0 };
		
		rc2expand(&ctx, k, 8, 64);
		rc2encrypt(&ctx, p, r);
		
		ASSERT("T004 TEST VECTOR 2 ENCRYPT",
				memcmp(c, r, 8) == 0);
		
		rc2decrypt(&ctx, c, r);
		
		ASSERT("T005 TEST VECTOR 2 DECRYPT",
				memcmp(p, r, 8) == 0);
	}
	
	{
		uint8_t k[] = { 0x88 };
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		uint8_t c[] = { 0x61, 0xA8, 0xA2, 0x44, 0xAD, 0xAC, 0xCC, 0xF0 };
		struct rc2 ctx; uint8_t r[8] = { 0 };
		
		rc2expand(&ctx, k, 1, 64);
		rc2encrypt(&ctx, p, r);
		
		ASSERT("T006 TEST VECTOR 3 ENCRYPT",
				memcmp(c, r, 8) == 0);
		
		rc2decrypt(&ctx, c, r);
		
		ASSERT("T007 TEST VECTOR 3 DECRYPT",
				memcmp(p, r, 8) == 0);
	}
	
	{
		uint8_t k[] = { 0x88, 0xBC, 0xA9, 0x0E, 0x90, 0x87, 0x5A };
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		uint8_t c[] = { 0x6C, 0xCF, 0x43, 0x08, 0x97, 0x4C, 0x26, 0x7F };
		struct rc2 ctx; uint8_t r[8] = { 0 };
		
		rc2expand(&ctx, k, 7, 64);
		rc2encrypt(&ctx, p, r);
		
		ASSERT("T008 TEST VECTOR 4 ENCRYPT",
				memcmp(c, r, 8) == 0);
		
		rc2decrypt(&ctx, c, r);
		
		ASSERT("T009 TEST VECTOR 4 DECRYPT",
				memcmp(p, r, 8) == 0);
	}
	
	{
		uint8_t k[] = {
			0x88, 0xBC, 0xA9, 0x0E, 0x90, 0x87, 0x5A, 0x7F,
			0x0F, 0x79, 0xC3, 0x84, 0x62, 0x7B, 0xAF, 0xB2
		};
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		uint8_t c[] = { 0x1A, 0x80, 0x7D, 0x27, 0x2B, 0xBE, 0x5D, 0xB1 };
		struct rc2 ctx; uint8_t r[8] = { 0 };
		
		rc2expand(&ctx, k, 16, 64);
		rc2encrypt(&ctx, p, r);
		
		ASSERT("T010 TEST VECTOR 5 ENCRYPT",
				memcmp(c, r, 8) == 0);
		
		rc2decrypt(&ctx, c, r);
		
		ASSERT("T011 TEST VECTOR 5 DECRYPT",
				memcmp(p, r, 8) == 0);
	}
	
	{
		uint8_t k[] = {
			0x88, 0xBC, 0xA9, 0x0E, 0x90, 0x87, 0x5A, 0x7F,
			0x0F, 0x79, 0xC3, 0x84, 0x62, 0x7B, 0xAF, 0xB2
		};
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		uint8_t c[] = { 0x22, 0x69, 0x55, 0x2A, 0xB0, 0xF8, 0x5C, 0xA6 };
		struct rc2 ctx; uint8_t r[8] = { 0 };
		
		rc2expand(&ctx, k, 16, 128);
		rc2encrypt(&ctx, p, r);
		
		ASSERT("T012 TEST VECTOR 6 ENCRYPT",
				memcmp(c, r, 8) == 0);
		
		rc2decrypt(&ctx, c, r);
		
		ASSERT("T013 TEST VECTOR 6 DECRYPT",
				memcmp(p, r, 8) == 0);
	}
	
	{
		uint8_t k[] = {
			0x88, 0xBC, 0xA9, 0x0E, 0x90, 0x87, 0x5A, 0x7F,
			0x0F, 0x79, 0xC3, 0x84, 0x62, 0x7B, 0xAF, 0xB2,
			0x16, 0xF8, 0x0A, 0x6F, 0x85, 0x92, 0x05, 0x84,
			0xC4, 0x2F, 0xCE, 0xB0, 0xBE, 0x25, 0x5D, 0xAF,
			0x1E
		};
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		uint8_t c[] = { 0x5B, 0x78, 0xD3, 0xA4, 0x3D, 0xFF, 0xF1, 0xF1 };
		struct rc2 ctx; uint8_t r[8] = { 0 };
		
		rc2expand(&ctx, k, 33, 129);
		rc2encrypt(&ctx, p, r);
		
		ASSERT("T014 TEST VECTOR 7 ENCRYPT",
				memcmp(c, r, 8) == 0);
		
		rc2decrypt(&ctx, c, r);
		
		ASSERT("T015 TEST VECTOR 7 DECRYPT",
				memcmp(p, r, 8) == 0);
	}
	
	printf("%d of %d tests passed\n", testspassed, testsrun);
	return testsfailed;
}