xref: /plan9-contrib/sys/src/libsec/port/chachatest.c (revision 59f1ab2eed2731678066f939e3f8a9647023435d)
1*59f1ab2eSDavid du Colombier #include <u.h>
2*59f1ab2eSDavid du Colombier #include <libc.h>
3*59f1ab2eSDavid du Colombier #include <libsec.h>
4*59f1ab2eSDavid du Colombier 
5*59f1ab2eSDavid du Colombier static void
printblock(uchar * b,usize n)6*59f1ab2eSDavid du Colombier printblock(uchar *b, usize n)
7*59f1ab2eSDavid du Colombier {
8*59f1ab2eSDavid du Colombier 	int i;
9*59f1ab2eSDavid du Colombier 
10*59f1ab2eSDavid du Colombier 	for(i=0; i+8<=n; i+=8){
11*59f1ab2eSDavid du Colombier 		print("%#.2ux %#.2ux %#.2ux %#.2ux %#.2ux %#.2ux %#.2ux %#.2ux\n",
12*59f1ab2eSDavid du Colombier 			b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
13*59f1ab2eSDavid du Colombier 		b += 8;
14*59f1ab2eSDavid du Colombier 	}
15*59f1ab2eSDavid du Colombier 	if(i < n){
16*59f1ab2eSDavid du Colombier 		print("%#.2ux", *b++);
17*59f1ab2eSDavid du Colombier 		while(++i < n)
18*59f1ab2eSDavid du Colombier 			print(" %#.2ux", *b++);
19*59f1ab2eSDavid du Colombier 		print("\n");
20*59f1ab2eSDavid du Colombier 	}
21*59f1ab2eSDavid du Colombier }
22*59f1ab2eSDavid du Colombier 
23*59f1ab2eSDavid du Colombier /* test vector from RFC7539 */
24*59f1ab2eSDavid du Colombier uchar	rfckey[] = {
25*59f1ab2eSDavid du Colombier 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
26*59f1ab2eSDavid du Colombier 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
27*59f1ab2eSDavid du Colombier 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
28*59f1ab2eSDavid du Colombier 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};
29*59f1ab2eSDavid du Colombier uchar	rfcnonce[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00};
30*59f1ab2eSDavid du Colombier u32int	rfccount = 1;
31*59f1ab2eSDavid du Colombier char	rfctext[] = "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, "
32*59f1ab2eSDavid du Colombier 	"sunscreen would be it.";
33*59f1ab2eSDavid du Colombier uchar	rfcout[3*ChachaBsize];
34*59f1ab2eSDavid du Colombier uchar	rfcref[3*ChachaBsize] = {
35*59f1ab2eSDavid du Colombier 	0x6e, 0x2e, 0x35, 0x9a, 0x25, 0x68, 0xf9, 0x80, 0x41, 0xba, 0x07, 0x28, 0xdd, 0x0d, 0x69, 0x81,
36*59f1ab2eSDavid du Colombier 	0xe9, 0x7e, 0x7a, 0xec, 0x1d, 0x43, 0x60, 0xc2, 0x0a, 0x27, 0xaf, 0xcc, 0xfd, 0x9f, 0xae, 0x0b,
37*59f1ab2eSDavid du Colombier 	0xf9, 0x1b, 0x65, 0xc5, 0x52, 0x47, 0x33, 0xab, 0x8f, 0x59, 0x3d, 0xab, 0xcd, 0x62, 0xb3, 0x57,
38*59f1ab2eSDavid du Colombier 	0x16, 0x39, 0xd6, 0x24, 0xe6, 0x51, 0x52, 0xab, 0x8f, 0x53, 0x0c, 0x35, 0x9f, 0x08, 0x61, 0xd8,
39*59f1ab2eSDavid du Colombier 	0x07, 0xca, 0x0d, 0xbf, 0x50, 0x0d, 0x6a, 0x61, 0x56, 0xa3, 0x8e, 0x08, 0x8a, 0x22, 0xb6, 0x5e,
40*59f1ab2eSDavid du Colombier 	0x52, 0xbc, 0x51, 0x4d, 0x16, 0xcc, 0xf8, 0x06, 0x81, 0x8c, 0xe9, 0x1a, 0xb7, 0x79, 0x37, 0x36,
41*59f1ab2eSDavid du Colombier 	0x5a, 0xf9, 0x0b, 0xbf, 0x74, 0xa3, 0x5b, 0xe6, 0xb4, 0x0b, 0x8e, 0xed, 0xf2, 0x78, 0x5e, 0x42,
42*59f1ab2eSDavid du Colombier 	0x87, 0x4d
43*59f1ab2eSDavid du Colombier };
44*59f1ab2eSDavid du Colombier 
45*59f1ab2eSDavid du Colombier void
main(int argc,char ** argv)46*59f1ab2eSDavid du Colombier main(int argc, char **argv)
47*59f1ab2eSDavid du Colombier {
48*59f1ab2eSDavid du Colombier 	Chachastate s;
49*59f1ab2eSDavid du Colombier 	int n;
50*59f1ab2eSDavid du Colombier 
51*59f1ab2eSDavid du Colombier 	ARGBEGIN{
52*59f1ab2eSDavid du Colombier 	}ARGEND
53*59f1ab2eSDavid du Colombier 	print("rfc7539:\n");
54*59f1ab2eSDavid du Colombier 	print("key:\n");
55*59f1ab2eSDavid du Colombier 	printblock(rfckey, sizeof(rfckey));
56*59f1ab2eSDavid du Colombier 	n = strlen(rfctext);
57*59f1ab2eSDavid du Colombier 	setupChachastate(&s, rfckey, sizeof(rfckey), rfcnonce, 0);
58*59f1ab2eSDavid du Colombier 	chacha_setblock(&s, rfccount);
59*59f1ab2eSDavid du Colombier 	print("rfc in:\n");
60*59f1ab2eSDavid du Colombier 	printblock((uchar*)rfctext, n);
61*59f1ab2eSDavid du Colombier 	chacha_encrypt2((uchar*)rfctext, rfcout, n, &s);
62*59f1ab2eSDavid du Colombier 	print("rfc out:\n");
63*59f1ab2eSDavid du Colombier 	printblock(rfcout, n);
64*59f1ab2eSDavid du Colombier 	if(memcmp(rfcout, rfcref, sizeof(rfcout)) != 0){
65*59f1ab2eSDavid du Colombier 		print("failure of vision\n");
66*59f1ab2eSDavid du Colombier 		exits("wrong");
67*59f1ab2eSDavid du Colombier 	}
68*59f1ab2eSDavid du Colombier 	print("passed\n");
69*59f1ab2eSDavid du Colombier 	exits(nil);
70*59f1ab2eSDavid du Colombier }
71