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/>.
17 #include <kripto/block.h>
18 #include <kripto/block/rc6.h>
20 #if defined(_TEST) || defined(_PERF)
29 #define ITERATIONS 10000000
40 kripto_block_rc6_t
*s
;
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
46 const uint8_t k
[32] = {
47 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
48 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
49 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0,
50 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe
54 const uint8_t pt
[16] = {
55 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79,
56 0x8A, 0x9B, 0xAC, 0xBD, 0xCE, 0xDF, 0xE0, 0xF1
58 const uint8_t ct
[32][16] = {
59 {0x1b, 0x1d, 0xda, 0xca, 0xbb, 0x34, 0x3d, 0x14,
60 0xc5, 0x6c, 0xd1, 0x18, 0x79, 0x2e, 0xd2, 0x7e},
61 {0x6e, 0x9e, 0xd9, 0x32, 0x36, 0xd4, 0xd5, 0x4d,
62 0xf5, 0x45, 0xcd, 0x71, 0xf6, 0x81, 0xb4, 0x6c},
63 {0xa9, 0x71, 0x2b, 0xae, 0xba, 0xea, 0xf7, 0x70,
64 0x57, 0x9c, 0x51, 0x15, 0x94, 0x08, 0xe5, 0x48},
65 {0x25, 0x84, 0x59, 0xb1, 0x64, 0xfd, 0x3f, 0x6f,
66 0xb9, 0x0a, 0x14, 0x6d, 0x8a, 0xce, 0x0c, 0x81},
67 {0x02, 0xef, 0x17, 0x92, 0x38, 0x6a, 0xc7, 0xaf,
68 0xd6, 0x13, 0xbc, 0x81, 0x59, 0xc1, 0x42, 0x30},
69 {0x1d, 0x44, 0xab, 0xc7, 0x40, 0x8a, 0x58, 0x7a,
70 0x8e, 0x50, 0x5f, 0x4c, 0xb3, 0xb2, 0xa9, 0x21},
71 {0xa0, 0xda, 0xa6, 0xfc, 0x23, 0x13, 0x44, 0x48,
72 0x75, 0xb9, 0x57, 0x12, 0xd6, 0xc9, 0x02, 0x7a},
73 {0xd0, 0x59, 0x7d, 0x0e, 0xf9, 0xcd, 0xcf, 0xc8,
74 0x62, 0xb3, 0xc0, 0xfd, 0xe1, 0x84, 0x7b, 0x9e},
75 {0xf2, 0x31, 0x7e, 0xc4, 0x52, 0xae, 0x5e, 0x97,
76 0x1b, 0x86, 0x39, 0x2a, 0xd2, 0x7a, 0x88, 0x1f},
77 {0x8e, 0xd4, 0x8b, 0xb7, 0x23, 0xb9, 0x00, 0x55,
78 0x29, 0x30, 0xb0, 0x4c, 0x1e, 0x52, 0x08, 0x26},
79 {0xc3, 0x23, 0x51, 0x25, 0xc1, 0xca, 0x49, 0x93,
80 0xbe, 0x60, 0xa4, 0xea, 0xf3, 0x0b, 0x0a, 0x4e},
81 {0xde, 0x47, 0x7a, 0x88, 0x9b, 0x02, 0x27, 0xaa,
82 0x93, 0x4a, 0xe8, 0xaf, 0x03, 0x31, 0x02, 0xdd},
83 {0x0b, 0xd9, 0x10, 0x0c, 0xae, 0xd8, 0x62, 0x30,
84 0xa9, 0x48, 0x52, 0xf0, 0x71, 0x47, 0xb8, 0x79},
85 {0xfd, 0xf7, 0x4d, 0x44, 0x66, 0x26, 0x81, 0x5c,
86 0x4a, 0xa4, 0x94, 0x3d, 0xd6, 0x68, 0x55, 0xf0},
87 {0x2b, 0xcd, 0xd3, 0x5f, 0xe5, 0xf2, 0xee, 0x5f,
88 0x14, 0x21, 0x11, 0xd7, 0x80, 0x95, 0xc1, 0x91},
89 {0x52, 0x4e, 0x19, 0x2f, 0x47, 0x15, 0xc6, 0x23,
90 0x1f, 0x51, 0xf6, 0x36, 0x7e, 0xa4, 0x3f, 0x18},
91 {0x8e, 0x52, 0x28, 0xa7, 0x84, 0x0b, 0x58, 0x7b,
92 0xd6, 0x2e, 0xd2, 0xef, 0xbf, 0xf0, 0x21, 0x06},
93 {0xd2, 0xb4, 0x31, 0x43, 0xac, 0xff, 0xba, 0xce,
94 0xc4, 0xd0, 0xeb, 0xd4, 0xfe, 0xe6, 0xac, 0x36},
95 {0x2d, 0xb2, 0x20, 0xcd, 0x40, 0xc1, 0xa6, 0x8c,
96 0xe6, 0xa9, 0xcf, 0x81, 0x0c, 0xb6, 0x68, 0xea},
97 {0x7c, 0xc6, 0x66, 0x6c, 0xd7, 0xd2, 0xdb, 0x0a,
98 0x1a, 0x20, 0x81, 0x66, 0xdc, 0x20, 0x40, 0x9d},
99 {0x02, 0x8b, 0xb2, 0xf8, 0x5a, 0xf8, 0x33, 0x41,
100 0x13, 0xa2, 0x63, 0x79, 0x01, 0x92, 0x0b, 0x0c},
101 {0x93, 0x03, 0x3a, 0x34, 0xfd, 0xe0, 0xee, 0x6c,
102 0x7f, 0x14, 0x65, 0xb2, 0x57, 0x04, 0xa1, 0x04},
103 {0x09, 0xfb, 0xbf, 0xed, 0x95, 0xd2, 0xae, 0x7f,
104 0x4c, 0x4d, 0x42, 0x79, 0xae, 0x2a, 0x9e, 0x9f},
105 {0x68, 0x83, 0x29, 0xd0, 0x19, 0xe5, 0x05, 0x04,
106 0x1e, 0x52, 0xe9, 0x2a, 0xf9, 0x52, 0x91, 0xd4},
107 {0x9e, 0xba, 0xe1, 0x18, 0xe6, 0xe2, 0x10, 0x96,
108 0x55, 0x8c, 0xd1, 0x20, 0x17, 0xae, 0xe7, 0x63},
109 {0x45, 0x9d, 0x36, 0x60, 0xe8, 0x6e, 0x31, 0x5a,
110 0x80, 0xa1, 0x6f, 0xb0, 0x80, 0x1b, 0x5b, 0xdc},
111 {0x1b, 0x2f, 0x03, 0x83, 0x3a, 0x90, 0x0b, 0x3c,
112 0xbe, 0xdb, 0x3d, 0x0a, 0x53, 0x27, 0x13, 0x30},
113 {0xbe, 0x7c, 0x57, 0xa0, 0xf6, 0xd8, 0xa7, 0xb4,
114 0x77, 0xd3, 0xf9, 0x2a, 0x76, 0xa8, 0x46, 0xde},
115 {0xa7, 0x2c, 0xd3, 0xf0, 0x19, 0xc8, 0x42, 0x03,
116 0xf7, 0x30, 0x87, 0x86, 0x1d, 0x6e, 0x65, 0x8b},
117 {0x8c, 0xf6, 0x37, 0x66, 0xfb, 0x8d, 0xad, 0x6f,
118 0xa9, 0x75, 0xaf, 0x27, 0xfe, 0xc0, 0x1b, 0x91},
119 {0x88, 0x2f, 0x78, 0x86, 0xdc, 0xad, 0xd7, 0xda,
120 0xc0, 0x44, 0x7f, 0x4b, 0xd5, 0xcc, 0x82, 0x22},
121 {0xc8, 0x24, 0x18, 0x16, 0xf0, 0xd7, 0xe4, 0x89,
122 0x20, 0xad, 0x16, 0xa1, 0x67, 0x4e, 0x5d, 0x48}
132 for(n
= 1; n
<= 32; n
++)
134 s
= kripto_block_rc6_new(k
, n
, RC6_DEFAULT_ROUNDS
);
136 kripto_block_rc6_encrypt(s
, pt
, t
);
137 for(i
= 0; i
< 16; i
++) if(t
[i
] != ct
[n
- 1][i
])
139 printf("%u-bit key encrypt: FAIL\n", n
* 8);
142 if(i
== 16) printf("%u-bit key encrypt: OK\n", n
* 8);
143 kripto_block_rc6_decrypt(s
, ct
[n
- 1], t
);
144 for(i
= 0; i
< 16; i
++) if(t
[i
] != pt
[i
])
146 printf("%u-bit key decrypt: FAIL\n", n
* 8);
149 if(i
== 16) printf("%u-bit key decrypt: OK\n", n
* 8);
151 kripto_block_rc6_delete(s
);
156 s
= kripto_block_rc6_new(k
, 16, RC6_DEFAULT_ROUNDS
);
159 for(i
= 0; i
< ITERATIONS
; i
++) kripto_block_rc6_encrypt(s
, t
, t
);
162 printf("encrypt: %.1f cycles/byte, %.1f MB/s\n",
163 (float)c
/ (float)(ITERATIONS
* 16) * _CPU
,
164 (float)(ITERATIONS
* 16) / ((float)c
/ (float)CLOCKS_PER_SEC
) / 1000000.0);
167 for(i
= 0; i
< ITERATIONS
; i
++) kripto_block_rc6_decrypt(s
, t
, t
);
170 printf("decrypt: %.1f cycles/byte, %.1f MB/s\n",
171 (float)c
/ (float)(ITERATIONS
* 16) * _CPU
,
172 (float)(ITERATIONS
* 16) / ((float)c
/ (float)CLOCKS_PER_SEC
) / 1000000.0);
174 kripto_block_rc6_delete(s
);