2 * Written in 2013 by Gregor Pintar <grpintar@gmail.com>
4 * To the extent possible under law, the author(s) have dedicated
5 * all copyright and related and neighboring rights to this software
6 * to the public domain worldwide.
8 * This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication.
11 * If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
16 #include <kripto/stream.h>
18 #include <kripto/stream/rc4.h>
19 #include <kripto/stream/rc4i.h>
21 #if defined(_TEST) || defined(_PERF)
30 #define ITERATIONS 10000000
33 #ifndef KSA_ITERATIONS
34 #define KSA_ITERATIONS 100000
48 static const uint8_t test
[512] = {
49 0xc3, 0xf9, 0x68, 0xc2, 0x32, 0x70, 0x29, 0x4a,
50 0x1a, 0x55, 0xdb, 0x22, 0xf9, 0xb8, 0x6, 0xeb,
51 0x4e, 0x7e, 0x8a, 0x45, 0xa2, 0xf1, 0xd6, 0xc7,
52 0xae, 0x42, 0x9d, 0x17, 0x80, 0xb7, 0x7, 0x63,
53 0x20, 0x16, 0x90, 0xb, 0xf4, 0xb1, 0x5a, 0xa7,
54 0x3, 0xec, 0x3a, 0xee, 0x2d, 0x68, 0x65, 0x7d,
55 0x16, 0x68, 0x4a, 0x9d, 0xee, 0xc3, 0xef, 0xb8,
56 0xce, 0xa4, 0x1b, 0x41, 0xe8, 0x56, 0x7b, 0x6,
57 0x7e, 0x7e, 0xaf, 0x9c, 0x4b, 0xe2, 0x9e, 0xfd,
58 0x5e, 0xab, 0x81, 0xfe, 0xe3, 0xf1, 0xa5, 0xb2,
59 0xa9, 0xf2, 0x3, 0xd4, 0x48, 0x83, 0x7c, 0x49,
60 0xfc, 0x8d, 0x58, 0x32, 0x28, 0x28, 0x6f, 0x3d,
61 0xb2, 0x7b, 0x83, 0x9, 0x92, 0x14, 0x86, 0x4a,
62 0x88, 0xa1, 0xfb, 0xce, 0x54, 0x38, 0x61, 0x16,
63 0xf0, 0xdf, 0x23, 0xde, 0xca, 0x13, 0xef, 0x69,
64 0xaa, 0xc0, 0xcb,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, 0xb3, 0xc6, 0x7a, 0x9c,
87 0xbe, 0x8, 0xb0, 0xeb, 0x86, 0xc8, 0xd4, 0x53,
88 0x0, 0x50, 0x64, 0x94, 0xbe, 0x5c, 0x2d, 0x83,
89 0xde, 0x34, 0xb, 0x1c, 0x73, 0x63, 0x98, 0xe8,
90 0x52, 0xd9, 0x95, 0xec, 0xea, 0x10, 0xda, 0x81,
91 0xb3, 0xc5, 0xa6, 0x89, 0x36, 0xd, 0x2a, 0x1d,
92 0xdf, 0x1f, 0xd9, 0x45, 0xdc, 0x95, 0x71, 0x1f,
93 0xe4, 0x49, 0x51, 0x8f, 0x9c, 0xad, 0x3d, 0x4d,
94 0x53, 0x74, 0x8a, 0xce, 0xa4, 0x65, 0x86, 0x56,
95 0xb8, 0x1a, 0x25, 0x3d, 0xe5, 0x7e, 0xda, 0xa3,
96 0xc2, 0xbc, 0x6b, 0x3c, 0xb6, 0xff, 0x62, 0x81,
97 0xc0, 0x32, 0x66, 0x49, 0x31, 0x4f, 0x59, 0x35,
98 0x18, 0xff, 0x75, 0x86, 0xe3, 0xc, 0x6c, 0x1b,
99 0x75, 0x84, 0xd0, 0x84, 0x8b, 0x1e, 0x1b, 0x51,
100 0xdb, 0x15, 0xb7, 0x8b, 0x7f, 0xff, 0x6c, 0xdd,
101 0xac, 0x6, 0xd8, 0xfa, 0xc3, 0x75, 0xbe, 0x1a,
102 0x72, 0xe1, 0xb5, 0x6b, 0xf8, 0xbc, 0xa5, 0xd5,
103 0xc0, 0xf8, 0xd3, 0xc, 0x4e, 0x52, 0x91, 0xe5,
104 0xff, 0x87, 0xd6, 0x60, 0xcd, 0x8b, 0xcc, 0x21,
105 0xf8, 0xc2, 0x7f, 0xe6, 0x97, 0xd8, 0xdc, 0x70,
106 0x43, 0x20, 0x4b, 0x19, 0xd9, 0x26, 0xb0, 0x1c,
107 0xc8, 0x97, 0x49, 0xd6, 0x8c, 0x77, 0x5b, 0xe9,
108 0xad, 0x11, 0x3a, 0xa3, 0x42, 0x65, 0xc2, 0xaa,
109 0x95, 0x7a, 0x49, 0x49, 0x0, 0x1f, 0x25, 0x94,
110 0x9f, 0xb1, 0xcd, 0xe2, 0xfb, 0xb3, 0x6f, 0xe0,
111 0xf3, 0x62, 0xca, 0xa8, 0xa9, 0x7d, 0x2a, 0xb3,
112 0x18, 0xd3, 0xef, 0x2f, 0xaa, 0x35, 0xb3, 0x32
123 kripto_stream_rc4i_setup(&s
, "RC4i stream cipher", 18, "iv", 2);
124 for(i
= 0; i
< 512; i
++) if(kripto_stream_rc4i(&s
) != test
[i
])
129 if( i
== 512) puts("OK");
134 for(i
= 0; i
< KSA_ITERATIONS
; i
++) kripto_stream_rc4i_setup(&s
, &i
, sizeof(i
), 0, 0);
137 printf("KSA without IV: %.1f cycles/byte\n",
138 (float)c
/ (float)KSA_ITERATIONS
* _CPU
);
141 for(i
= 0; i
< KSA_ITERATIONS
; i
++) kripto_stream_rc4i_setup(&s
, &i
, sizeof(i
), &i
, sizeof(i
));
144 printf("KSA with IV: %.1f cycles/byte\n",
145 (float)c
/ (float)KSA_ITERATIONS
* _CPU
);
148 for(i
= 0; i
< ITERATIONS
; i
++) b
= kripto_stream_rc4i(&s
);
151 printf("Crypt: %.1f cycles/byte, %.1f MB/s\n",
152 (float)c
/ (float)ITERATIONS
* _CPU
,
153 (float)ITERATIONS
/ ((float)c
/ (float)CLOCKS_PER_SEC
) / 1000000.0);