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-- file download

3b606bc Jamozed 2021-09-04 00:05:02
0
// test_crypt.c
98e5939 Jamozed 2021-11-27 17:53:46
1
// Crypt unit test for libutil
3b606bc Jamozed 2021-09-04 00:05:02
2
// Copyright (C) 2021, Jakob Wakeling
7f427d9 Jamozed 2022-03-06 12:55:13
3
// MIT Licence
3b606bc Jamozed 2021-09-04 00:05:02
4
3b606bc Jamozed 2021-09-04 00:05:02
5
#include "../rc2.h"
3b606bc Jamozed 2021-09-04 00:05:02
6
#include "unit.h"
3b606bc Jamozed 2021-09-04 00:05:02
7
3b606bc Jamozed 2021-09-04 00:05:02
8
#include <stdint.h>
3b606bc Jamozed 2021-09-04 00:05:02
9
#include <stdio.h>
3b606bc Jamozed 2021-09-04 00:05:02
10
#include <string.h>
3b606bc Jamozed 2021-09-04 00:05:02
11
3b606bc Jamozed 2021-09-04 00:05:02
12
int main(void) {
3b606bc Jamozed 2021-09-04 00:05:02
13
	{
3b606bc Jamozed 2021-09-04 00:05:02
14
		uint8_t k[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3b606bc Jamozed 2021-09-04 00:05:02
15
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3b606bc Jamozed 2021-09-04 00:05:02
16
		uint8_t c[] = { 0xEB, 0xB7, 0x73, 0xF9, 0x93, 0x27, 0x8E, 0xFF };
3b606bc Jamozed 2021-09-04 00:05:02
17
		struct rc2 ctx; uint8_t r[8] = { 0 };
3b606bc Jamozed 2021-09-04 00:05:02
18
		
3b606bc Jamozed 2021-09-04 00:05:02
19
		rc2expand(&ctx, k, 8, 63);
3b606bc Jamozed 2021-09-04 00:05:02
20
		rc2encrypt(&ctx, p, r);
3b606bc Jamozed 2021-09-04 00:05:02
21
		
3b606bc Jamozed 2021-09-04 00:05:02
22
		ASSERT("T000 TEST VECTOR 0 ENCRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
23
				memcmp(c, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
24
		
3b606bc Jamozed 2021-09-04 00:05:02
25
		rc2decrypt(&ctx, c, r);
3b606bc Jamozed 2021-09-04 00:05:02
26
		
3b606bc Jamozed 2021-09-04 00:05:02
27
		ASSERT("T001 TEST VECTOR 0 DECRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
28
				memcmp(p, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
29
	}
3b606bc Jamozed 2021-09-04 00:05:02
30
	
3b606bc Jamozed 2021-09-04 00:05:02
31
	{
3b606bc Jamozed 2021-09-04 00:05:02
32
		uint8_t k[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3b606bc Jamozed 2021-09-04 00:05:02
33
		uint8_t p[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3b606bc Jamozed 2021-09-04 00:05:02
34
		uint8_t c[] = { 0x27, 0x8B, 0x27, 0xE4, 0x2E, 0x2F, 0x0D, 0x49 };
3b606bc Jamozed 2021-09-04 00:05:02
35
		struct rc2 ctx; uint8_t r[8] = { 0 };
3b606bc Jamozed 2021-09-04 00:05:02
36
		
3b606bc Jamozed 2021-09-04 00:05:02
37
		rc2expand(&ctx, k, 8, 64);
3b606bc Jamozed 2021-09-04 00:05:02
38
		rc2encrypt(&ctx, p, r);
3b606bc Jamozed 2021-09-04 00:05:02
39
		
3b606bc Jamozed 2021-09-04 00:05:02
40
		ASSERT("T002 TEST VECTOR 1 ENCRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
41
				memcmp(c, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
42
		
3b606bc Jamozed 2021-09-04 00:05:02
43
		rc2decrypt(&ctx, c, r);
3b606bc Jamozed 2021-09-04 00:05:02
44
		
3b606bc Jamozed 2021-09-04 00:05:02
45
		ASSERT("T003 TEST VECTOR 1 DECRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
46
				memcmp(p, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
47
	}
3b606bc Jamozed 2021-09-04 00:05:02
48
	
3b606bc Jamozed 2021-09-04 00:05:02
49
	{
3b606bc Jamozed 2021-09-04 00:05:02
50
		uint8_t k[] = { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3b606bc Jamozed 2021-09-04 00:05:02
51
		uint8_t p[] = { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
3b606bc Jamozed 2021-09-04 00:05:02
52
		uint8_t c[] = { 0x30, 0x64, 0x9E, 0xDF, 0x9B, 0xE7, 0xD2, 0xC2 };
3b606bc Jamozed 2021-09-04 00:05:02
53
		struct rc2 ctx; uint8_t r[8] = { 0 };
3b606bc Jamozed 2021-09-04 00:05:02
54
		
3b606bc Jamozed 2021-09-04 00:05:02
55
		rc2expand(&ctx, k, 8, 64);
3b606bc Jamozed 2021-09-04 00:05:02
56
		rc2encrypt(&ctx, p, r);
3b606bc Jamozed 2021-09-04 00:05:02
57
		
3b606bc Jamozed 2021-09-04 00:05:02
58
		ASSERT("T004 TEST VECTOR 2 ENCRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
59
				memcmp(c, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
60
		
3b606bc Jamozed 2021-09-04 00:05:02
61
		rc2decrypt(&ctx, c, r);
3b606bc Jamozed 2021-09-04 00:05:02
62
		
3b606bc Jamozed 2021-09-04 00:05:02
63
		ASSERT("T005 TEST VECTOR 2 DECRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
64
				memcmp(p, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
65
	}
3b606bc Jamozed 2021-09-04 00:05:02
66
	
3b606bc Jamozed 2021-09-04 00:05:02
67
	{
3b606bc Jamozed 2021-09-04 00:05:02
68
		uint8_t k[] = { 0x88 };
3b606bc Jamozed 2021-09-04 00:05:02
69
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3b606bc Jamozed 2021-09-04 00:05:02
70
		uint8_t c[] = { 0x61, 0xA8, 0xA2, 0x44, 0xAD, 0xAC, 0xCC, 0xF0 };
3b606bc Jamozed 2021-09-04 00:05:02
71
		struct rc2 ctx; uint8_t r[8] = { 0 };
3b606bc Jamozed 2021-09-04 00:05:02
72
		
3b606bc Jamozed 2021-09-04 00:05:02
73
		rc2expand(&ctx, k, 1, 64);
3b606bc Jamozed 2021-09-04 00:05:02
74
		rc2encrypt(&ctx, p, r);
3b606bc Jamozed 2021-09-04 00:05:02
75
		
3b606bc Jamozed 2021-09-04 00:05:02
76
		ASSERT("T006 TEST VECTOR 3 ENCRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
77
				memcmp(c, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
78
		
3b606bc Jamozed 2021-09-04 00:05:02
79
		rc2decrypt(&ctx, c, r);
3b606bc Jamozed 2021-09-04 00:05:02
80
		
3b606bc Jamozed 2021-09-04 00:05:02
81
		ASSERT("T007 TEST VECTOR 3 DECRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
82
				memcmp(p, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
83
	}
3b606bc Jamozed 2021-09-04 00:05:02
84
	
3b606bc Jamozed 2021-09-04 00:05:02
85
	{
3b606bc Jamozed 2021-09-04 00:05:02
86
		uint8_t k[] = { 0x88, 0xBC, 0xA9, 0x0E, 0x90, 0x87, 0x5A };
3b606bc Jamozed 2021-09-04 00:05:02
87
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3b606bc Jamozed 2021-09-04 00:05:02
88
		uint8_t c[] = { 0x6C, 0xCF, 0x43, 0x08, 0x97, 0x4C, 0x26, 0x7F };
3b606bc Jamozed 2021-09-04 00:05:02
89
		struct rc2 ctx; uint8_t r[8] = { 0 };
3b606bc Jamozed 2021-09-04 00:05:02
90
		
3b606bc Jamozed 2021-09-04 00:05:02
91
		rc2expand(&ctx, k, 7, 64);
3b606bc Jamozed 2021-09-04 00:05:02
92
		rc2encrypt(&ctx, p, r);
3b606bc Jamozed 2021-09-04 00:05:02
93
		
3b606bc Jamozed 2021-09-04 00:05:02
94
		ASSERT("T008 TEST VECTOR 4 ENCRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
95
				memcmp(c, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
96
		
3b606bc Jamozed 2021-09-04 00:05:02
97
		rc2decrypt(&ctx, c, r);
3b606bc Jamozed 2021-09-04 00:05:02
98
		
3b606bc Jamozed 2021-09-04 00:05:02
99
		ASSERT("T009 TEST VECTOR 4 DECRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
100
				memcmp(p, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
101
	}
3b606bc Jamozed 2021-09-04 00:05:02
102
	
3b606bc Jamozed 2021-09-04 00:05:02
103
	{
3b606bc Jamozed 2021-09-04 00:05:02
104
		uint8_t k[] = {
3b606bc Jamozed 2021-09-04 00:05:02
105
			0x88, 0xBC, 0xA9, 0x0E, 0x90, 0x87, 0x5A, 0x7F,
3b606bc Jamozed 2021-09-04 00:05:02
106
			0x0F, 0x79, 0xC3, 0x84, 0x62, 0x7B, 0xAF, 0xB2
3b606bc Jamozed 2021-09-04 00:05:02
107
		};
3b606bc Jamozed 2021-09-04 00:05:02
108
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3b606bc Jamozed 2021-09-04 00:05:02
109
		uint8_t c[] = { 0x1A, 0x80, 0x7D, 0x27, 0x2B, 0xBE, 0x5D, 0xB1 };
3b606bc Jamozed 2021-09-04 00:05:02
110
		struct rc2 ctx; uint8_t r[8] = { 0 };
3b606bc Jamozed 2021-09-04 00:05:02
111
		
3b606bc Jamozed 2021-09-04 00:05:02
112
		rc2expand(&ctx, k, 16, 64);
3b606bc Jamozed 2021-09-04 00:05:02
113
		rc2encrypt(&ctx, p, r);
3b606bc Jamozed 2021-09-04 00:05:02
114
		
3b606bc Jamozed 2021-09-04 00:05:02
115
		ASSERT("T010 TEST VECTOR 5 ENCRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
116
				memcmp(c, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
117
		
3b606bc Jamozed 2021-09-04 00:05:02
118
		rc2decrypt(&ctx, c, r);
3b606bc Jamozed 2021-09-04 00:05:02
119
		
3b606bc Jamozed 2021-09-04 00:05:02
120
		ASSERT("T011 TEST VECTOR 5 DECRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
121
				memcmp(p, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
122
	}
3b606bc Jamozed 2021-09-04 00:05:02
123
	
3b606bc Jamozed 2021-09-04 00:05:02
124
	{
3b606bc Jamozed 2021-09-04 00:05:02
125
		uint8_t k[] = {
3b606bc Jamozed 2021-09-04 00:05:02
126
			0x88, 0xBC, 0xA9, 0x0E, 0x90, 0x87, 0x5A, 0x7F,
3b606bc Jamozed 2021-09-04 00:05:02
127
			0x0F, 0x79, 0xC3, 0x84, 0x62, 0x7B, 0xAF, 0xB2
3b606bc Jamozed 2021-09-04 00:05:02
128
		};
3b606bc Jamozed 2021-09-04 00:05:02
129
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3b606bc Jamozed 2021-09-04 00:05:02
130
		uint8_t c[] = { 0x22, 0x69, 0x55, 0x2A, 0xB0, 0xF8, 0x5C, 0xA6 };
3b606bc Jamozed 2021-09-04 00:05:02
131
		struct rc2 ctx; uint8_t r[8] = { 0 };
3b606bc Jamozed 2021-09-04 00:05:02
132
		
3b606bc Jamozed 2021-09-04 00:05:02
133
		rc2expand(&ctx, k, 16, 128);
3b606bc Jamozed 2021-09-04 00:05:02
134
		rc2encrypt(&ctx, p, r);
3b606bc Jamozed 2021-09-04 00:05:02
135
		
3b606bc Jamozed 2021-09-04 00:05:02
136
		ASSERT("T012 TEST VECTOR 6 ENCRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
137
				memcmp(c, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
138
		
3b606bc Jamozed 2021-09-04 00:05:02
139
		rc2decrypt(&ctx, c, r);
3b606bc Jamozed 2021-09-04 00:05:02
140
		
3b606bc Jamozed 2021-09-04 00:05:02
141
		ASSERT("T013 TEST VECTOR 6 DECRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
142
				memcmp(p, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
143
	}
3b606bc Jamozed 2021-09-04 00:05:02
144
	
3b606bc Jamozed 2021-09-04 00:05:02
145
	{
3b606bc Jamozed 2021-09-04 00:05:02
146
		uint8_t k[] = {
3b606bc Jamozed 2021-09-04 00:05:02
147
			0x88, 0xBC, 0xA9, 0x0E, 0x90, 0x87, 0x5A, 0x7F,
3b606bc Jamozed 2021-09-04 00:05:02
148
			0x0F, 0x79, 0xC3, 0x84, 0x62, 0x7B, 0xAF, 0xB2,
3b606bc Jamozed 2021-09-04 00:05:02
149
			0x16, 0xF8, 0x0A, 0x6F, 0x85, 0x92, 0x05, 0x84,
3b606bc Jamozed 2021-09-04 00:05:02
150
			0xC4, 0x2F, 0xCE, 0xB0, 0xBE, 0x25, 0x5D, 0xAF,
3b606bc Jamozed 2021-09-04 00:05:02
151
			0x1E
3b606bc Jamozed 2021-09-04 00:05:02
152
		};
3b606bc Jamozed 2021-09-04 00:05:02
153
		uint8_t p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3b606bc Jamozed 2021-09-04 00:05:02
154
		uint8_t c[] = { 0x5B, 0x78, 0xD3, 0xA4, 0x3D, 0xFF, 0xF1, 0xF1 };
3b606bc Jamozed 2021-09-04 00:05:02
155
		struct rc2 ctx; uint8_t r[8] = { 0 };
3b606bc Jamozed 2021-09-04 00:05:02
156
		
3b606bc Jamozed 2021-09-04 00:05:02
157
		rc2expand(&ctx, k, 33, 129);
3b606bc Jamozed 2021-09-04 00:05:02
158
		rc2encrypt(&ctx, p, r);
3b606bc Jamozed 2021-09-04 00:05:02
159
		
3b606bc Jamozed 2021-09-04 00:05:02
160
		ASSERT("T014 TEST VECTOR 7 ENCRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
161
				memcmp(c, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
162
		
3b606bc Jamozed 2021-09-04 00:05:02
163
		rc2decrypt(&ctx, c, r);
3b606bc Jamozed 2021-09-04 00:05:02
164
		
3b606bc Jamozed 2021-09-04 00:05:02
165
		ASSERT("T015 TEST VECTOR 7 DECRYPT",
3b606bc Jamozed 2021-09-04 00:05:02
166
				memcmp(p, r, 8) == 0);
3b606bc Jamozed 2021-09-04 00:05:02
167
	}
3b606bc Jamozed 2021-09-04 00:05:02
168
	
3b606bc Jamozed 2021-09-04 00:05:02
169
	printf("%d of %d tests passed\n", testspassed, testsrun);
3b606bc Jamozed 2021-09-04 00:05:02
170
	return testsfailed;
3b606bc Jamozed 2021-09-04 00:05:02
171
}
172