Coverage report: /home/ellis/comp/ext/ironclad/src/ciphers/camellia.lisp
Kind | Covered | All | % |
expression | 0 | 782 | 0.0 |
branch | 0 | 8 | 0.0 |
Key
Not instrumented
Conditionalized out
Executed
Not executed
Both branches taken
One branch taken
Neither branch taken
1
;;;; camellia.lisp - implementation of the Camellia block cipher
4
(defconst +camellia-sbox1+
6
:element-type '(unsigned-byte 32)
7
:initial-contents '(#x70707000 #x82828200 #x2c2c2c00 #xececec00
8
#xb3b3b300 #x27272700 #xc0c0c000 #xe5e5e500
9
#xe4e4e400 #x85858500 #x57575700 #x35353500
10
#xeaeaea00 #x0c0c0c00 #xaeaeae00 #x41414100
11
#x23232300 #xefefef00 #x6b6b6b00 #x93939300
12
#x45454500 #x19191900 #xa5a5a500 #x21212100
13
#xededed00 #x0e0e0e00 #x4f4f4f00 #x4e4e4e00
14
#x1d1d1d00 #x65656500 #x92929200 #xbdbdbd00
15
#x86868600 #xb8b8b800 #xafafaf00 #x8f8f8f00
16
#x7c7c7c00 #xebebeb00 #x1f1f1f00 #xcecece00
17
#x3e3e3e00 #x30303000 #xdcdcdc00 #x5f5f5f00
18
#x5e5e5e00 #xc5c5c500 #x0b0b0b00 #x1a1a1a00
19
#xa6a6a600 #xe1e1e100 #x39393900 #xcacaca00
20
#xd5d5d500 #x47474700 #x5d5d5d00 #x3d3d3d00
21
#xd9d9d900 #x01010100 #x5a5a5a00 #xd6d6d600
22
#x51515100 #x56565600 #x6c6c6c00 #x4d4d4d00
23
#x8b8b8b00 #x0d0d0d00 #x9a9a9a00 #x66666600
24
#xfbfbfb00 #xcccccc00 #xb0b0b000 #x2d2d2d00
25
#x74747400 #x12121200 #x2b2b2b00 #x20202000
26
#xf0f0f000 #xb1b1b100 #x84848400 #x99999900
27
#xdfdfdf00 #x4c4c4c00 #xcbcbcb00 #xc2c2c200
28
#x34343400 #x7e7e7e00 #x76767600 #x05050500
29
#x6d6d6d00 #xb7b7b700 #xa9a9a900 #x31313100
30
#xd1d1d100 #x17171700 #x04040400 #xd7d7d700
31
#x14141400 #x58585800 #x3a3a3a00 #x61616100
32
#xdedede00 #x1b1b1b00 #x11111100 #x1c1c1c00
33
#x32323200 #x0f0f0f00 #x9c9c9c00 #x16161600
34
#x53535300 #x18181800 #xf2f2f200 #x22222200
35
#xfefefe00 #x44444400 #xcfcfcf00 #xb2b2b200
36
#xc3c3c300 #xb5b5b500 #x7a7a7a00 #x91919100
37
#x24242400 #x08080800 #xe8e8e800 #xa8a8a800
38
#x60606000 #xfcfcfc00 #x69696900 #x50505000
39
#xaaaaaa00 #xd0d0d000 #xa0a0a000 #x7d7d7d00
40
#xa1a1a100 #x89898900 #x62626200 #x97979700
41
#x54545400 #x5b5b5b00 #x1e1e1e00 #x95959500
42
#xe0e0e000 #xffffff00 #x64646400 #xd2d2d200
43
#x10101000 #xc4c4c400 #x00000000 #x48484800
44
#xa3a3a300 #xf7f7f700 #x75757500 #xdbdbdb00
45
#x8a8a8a00 #x03030300 #xe6e6e600 #xdadada00
46
#x09090900 #x3f3f3f00 #xdddddd00 #x94949400
47
#x87878700 #x5c5c5c00 #x83838300 #x02020200
48
#xcdcdcd00 #x4a4a4a00 #x90909000 #x33333300
49
#x73737300 #x67676700 #xf6f6f600 #xf3f3f300
50
#x9d9d9d00 #x7f7f7f00 #xbfbfbf00 #xe2e2e200
51
#x52525200 #x9b9b9b00 #xd8d8d800 #x26262600
52
#xc8c8c800 #x37373700 #xc6c6c600 #x3b3b3b00
53
#x81818100 #x96969600 #x6f6f6f00 #x4b4b4b00
54
#x13131300 #xbebebe00 #x63636300 #x2e2e2e00
55
#xe9e9e900 #x79797900 #xa7a7a700 #x8c8c8c00
56
#x9f9f9f00 #x6e6e6e00 #xbcbcbc00 #x8e8e8e00
57
#x29292900 #xf5f5f500 #xf9f9f900 #xb6b6b600
58
#x2f2f2f00 #xfdfdfd00 #xb4b4b400 #x59595900
59
#x78787800 #x98989800 #x06060600 #x6a6a6a00
60
#xe7e7e700 #x46464600 #x71717100 #xbababa00
61
#xd4d4d400 #x25252500 #xababab00 #x42424200
62
#x88888800 #xa2a2a200 #x8d8d8d00 #xfafafa00
63
#x72727200 #x07070700 #xb9b9b900 #x55555500
64
#xf8f8f800 #xeeeeee00 #xacacac00 #x0a0a0a00
65
#x36363600 #x49494900 #x2a2a2a00 #x68686800
66
#x3c3c3c00 #x38383800 #xf1f1f100 #xa4a4a400
67
#x40404000 #x28282800 #xd3d3d300 #x7b7b7b00
68
#xbbbbbb00 #xc9c9c900 #x43434300 #xc1c1c100
69
#x15151500 #xe3e3e300 #xadadad00 #xf4f4f400
70
#x77777700 #xc7c7c700 #x80808000 #x9e9e9e00)))
72
(defconst +camellia-sbox2+
74
:element-type '(unsigned-byte 32)
75
:initial-contents '(#x00e0e0e0 #x00050505 #x00585858 #x00d9d9d9
76
#x00676767 #x004e4e4e #x00818181 #x00cbcbcb
77
#x00c9c9c9 #x000b0b0b #x00aeaeae #x006a6a6a
78
#x00d5d5d5 #x00181818 #x005d5d5d #x00828282
79
#x00464646 #x00dfdfdf #x00d6d6d6 #x00272727
80
#x008a8a8a #x00323232 #x004b4b4b #x00424242
81
#x00dbdbdb #x001c1c1c #x009e9e9e #x009c9c9c
82
#x003a3a3a #x00cacaca #x00252525 #x007b7b7b
83
#x000d0d0d #x00717171 #x005f5f5f #x001f1f1f
84
#x00f8f8f8 #x00d7d7d7 #x003e3e3e #x009d9d9d
85
#x007c7c7c #x00606060 #x00b9b9b9 #x00bebebe
86
#x00bcbcbc #x008b8b8b #x00161616 #x00343434
87
#x004d4d4d #x00c3c3c3 #x00727272 #x00959595
88
#x00ababab #x008e8e8e #x00bababa #x007a7a7a
89
#x00b3b3b3 #x00020202 #x00b4b4b4 #x00adadad
90
#x00a2a2a2 #x00acacac #x00d8d8d8 #x009a9a9a
91
#x00171717 #x001a1a1a #x00353535 #x00cccccc
92
#x00f7f7f7 #x00999999 #x00616161 #x005a5a5a
93
#x00e8e8e8 #x00242424 #x00565656 #x00404040
94
#x00e1e1e1 #x00636363 #x00090909 #x00333333
95
#x00bfbfbf #x00989898 #x00979797 #x00858585
96
#x00686868 #x00fcfcfc #x00ececec #x000a0a0a
97
#x00dadada #x006f6f6f #x00535353 #x00626262
98
#x00a3a3a3 #x002e2e2e #x00080808 #x00afafaf
99
#x00282828 #x00b0b0b0 #x00747474 #x00c2c2c2
100
#x00bdbdbd #x00363636 #x00222222 #x00383838
101
#x00646464 #x001e1e1e #x00393939 #x002c2c2c
102
#x00a6a6a6 #x00303030 #x00e5e5e5 #x00444444
103
#x00fdfdfd #x00888888 #x009f9f9f #x00656565
104
#x00878787 #x006b6b6b #x00f4f4f4 #x00232323
105
#x00484848 #x00101010 #x00d1d1d1 #x00515151
106
#x00c0c0c0 #x00f9f9f9 #x00d2d2d2 #x00a0a0a0
107
#x00555555 #x00a1a1a1 #x00414141 #x00fafafa
108
#x00434343 #x00131313 #x00c4c4c4 #x002f2f2f
109
#x00a8a8a8 #x00b6b6b6 #x003c3c3c #x002b2b2b
110
#x00c1c1c1 #x00ffffff #x00c8c8c8 #x00a5a5a5
111
#x00202020 #x00898989 #x00000000 #x00909090
112
#x00474747 #x00efefef #x00eaeaea #x00b7b7b7
113
#x00151515 #x00060606 #x00cdcdcd #x00b5b5b5
114
#x00121212 #x007e7e7e #x00bbbbbb #x00292929
115
#x000f0f0f #x00b8b8b8 #x00070707 #x00040404
116
#x009b9b9b #x00949494 #x00212121 #x00666666
117
#x00e6e6e6 #x00cecece #x00ededed #x00e7e7e7
118
#x003b3b3b #x00fefefe #x007f7f7f #x00c5c5c5
119
#x00a4a4a4 #x00373737 #x00b1b1b1 #x004c4c4c
120
#x00919191 #x006e6e6e #x008d8d8d #x00767676
121
#x00030303 #x002d2d2d #x00dedede #x00969696
122
#x00262626 #x007d7d7d #x00c6c6c6 #x005c5c5c
123
#x00d3d3d3 #x00f2f2f2 #x004f4f4f #x00191919
124
#x003f3f3f #x00dcdcdc #x00797979 #x001d1d1d
125
#x00525252 #x00ebebeb #x00f3f3f3 #x006d6d6d
126
#x005e5e5e #x00fbfbfb #x00696969 #x00b2b2b2
127
#x00f0f0f0 #x00313131 #x000c0c0c #x00d4d4d4
128
#x00cfcfcf #x008c8c8c #x00e2e2e2 #x00757575
129
#x00a9a9a9 #x004a4a4a #x00575757 #x00848484
130
#x00111111 #x00454545 #x001b1b1b #x00f5f5f5
131
#x00e4e4e4 #x000e0e0e #x00737373 #x00aaaaaa
132
#x00f1f1f1 #x00dddddd #x00595959 #x00141414
133
#x006c6c6c #x00929292 #x00545454 #x00d0d0d0
134
#x00787878 #x00707070 #x00e3e3e3 #x00494949
135
#x00808080 #x00505050 #x00a7a7a7 #x00f6f6f6
136
#x00777777 #x00939393 #x00868686 #x00838383
137
#x002a2a2a #x00c7c7c7 #x005b5b5b #x00e9e9e9
138
#x00eeeeee #x008f8f8f #x00010101 #x003d3d3d)))
140
(defconst +camellia-sbox3+
142
:element-type '(unsigned-byte 32)
143
:initial-contents '(#x38003838 #x41004141 #x16001616 #x76007676
144
#xd900d9d9 #x93009393 #x60006060 #xf200f2f2
145
#x72007272 #xc200c2c2 #xab00abab #x9a009a9a
146
#x75007575 #x06000606 #x57005757 #xa000a0a0
147
#x91009191 #xf700f7f7 #xb500b5b5 #xc900c9c9
148
#xa200a2a2 #x8c008c8c #xd200d2d2 #x90009090
149
#xf600f6f6 #x07000707 #xa700a7a7 #x27002727
150
#x8e008e8e #xb200b2b2 #x49004949 #xde00dede
151
#x43004343 #x5c005c5c #xd700d7d7 #xc700c7c7
152
#x3e003e3e #xf500f5f5 #x8f008f8f #x67006767
153
#x1f001f1f #x18001818 #x6e006e6e #xaf00afaf
154
#x2f002f2f #xe200e2e2 #x85008585 #x0d000d0d
155
#x53005353 #xf000f0f0 #x9c009c9c #x65006565
156
#xea00eaea #xa300a3a3 #xae00aeae #x9e009e9e
157
#xec00ecec #x80008080 #x2d002d2d #x6b006b6b
158
#xa800a8a8 #x2b002b2b #x36003636 #xa600a6a6
159
#xc500c5c5 #x86008686 #x4d004d4d #x33003333
160
#xfd00fdfd #x66006666 #x58005858 #x96009696
161
#x3a003a3a #x09000909 #x95009595 #x10001010
162
#x78007878 #xd800d8d8 #x42004242 #xcc00cccc
163
#xef00efef #x26002626 #xe500e5e5 #x61006161
164
#x1a001a1a #x3f003f3f #x3b003b3b #x82008282
165
#xb600b6b6 #xdb00dbdb #xd400d4d4 #x98009898
166
#xe800e8e8 #x8b008b8b #x02000202 #xeb00ebeb
167
#x0a000a0a #x2c002c2c #x1d001d1d #xb000b0b0
168
#x6f006f6f #x8d008d8d #x88008888 #x0e000e0e
169
#x19001919 #x87008787 #x4e004e4e #x0b000b0b
170
#xa900a9a9 #x0c000c0c #x79007979 #x11001111
171
#x7f007f7f #x22002222 #xe700e7e7 #x59005959
172
#xe100e1e1 #xda00dada #x3d003d3d #xc800c8c8
173
#x12001212 #x04000404 #x74007474 #x54005454
174
#x30003030 #x7e007e7e #xb400b4b4 #x28002828
175
#x55005555 #x68006868 #x50005050 #xbe00bebe
176
#xd000d0d0 #xc400c4c4 #x31003131 #xcb00cbcb
177
#x2a002a2a #xad00adad #x0f000f0f #xca00caca
178
#x70007070 #xff00ffff #x32003232 #x69006969
179
#x08000808 #x62006262 #x00000000 #x24002424
180
#xd100d1d1 #xfb00fbfb #xba00baba #xed00eded
181
#x45004545 #x81008181 #x73007373 #x6d006d6d
182
#x84008484 #x9f009f9f #xee00eeee #x4a004a4a
183
#xc300c3c3 #x2e002e2e #xc100c1c1 #x01000101
184
#xe600e6e6 #x25002525 #x48004848 #x99009999
185
#xb900b9b9 #xb300b3b3 #x7b007b7b #xf900f9f9
186
#xce00cece #xbf00bfbf #xdf00dfdf #x71007171
187
#x29002929 #xcd00cdcd #x6c006c6c #x13001313
188
#x64006464 #x9b009b9b #x63006363 #x9d009d9d
189
#xc000c0c0 #x4b004b4b #xb700b7b7 #xa500a5a5
190
#x89008989 #x5f005f5f #xb100b1b1 #x17001717
191
#xf400f4f4 #xbc00bcbc #xd300d3d3 #x46004646
192
#xcf00cfcf #x37003737 #x5e005e5e #x47004747
193
#x94009494 #xfa00fafa #xfc00fcfc #x5b005b5b
194
#x97009797 #xfe00fefe #x5a005a5a #xac00acac
195
#x3c003c3c #x4c004c4c #x03000303 #x35003535
196
#xf300f3f3 #x23002323 #xb800b8b8 #x5d005d5d
197
#x6a006a6a #x92009292 #xd500d5d5 #x21002121
198
#x44004444 #x51005151 #xc600c6c6 #x7d007d7d
199
#x39003939 #x83008383 #xdc00dcdc #xaa00aaaa
200
#x7c007c7c #x77007777 #x56005656 #x05000505
201
#x1b001b1b #xa400a4a4 #x15001515 #x34003434
202
#x1e001e1e #x1c001c1c #xf800f8f8 #x52005252
203
#x20002020 #x14001414 #xe900e9e9 #xbd00bdbd
204
#xdd00dddd #xe400e4e4 #xa100a1a1 #xe000e0e0
205
#x8a008a8a #xf100f1f1 #xd600d6d6 #x7a007a7a
206
#xbb00bbbb #xe300e3e3 #x40004040 #x4f004f4f)))
208
(defconst +camellia-sbox4+
210
:element-type '(unsigned-byte 32)
211
:initial-contents '(#x70700070 #x2c2c002c #xb3b300b3 #xc0c000c0
212
#xe4e400e4 #x57570057 #xeaea00ea #xaeae00ae
213
#x23230023 #x6b6b006b #x45450045 #xa5a500a5
214
#xeded00ed #x4f4f004f #x1d1d001d #x92920092
215
#x86860086 #xafaf00af #x7c7c007c #x1f1f001f
216
#x3e3e003e #xdcdc00dc #x5e5e005e #x0b0b000b
217
#xa6a600a6 #x39390039 #xd5d500d5 #x5d5d005d
218
#xd9d900d9 #x5a5a005a #x51510051 #x6c6c006c
219
#x8b8b008b #x9a9a009a #xfbfb00fb #xb0b000b0
220
#x74740074 #x2b2b002b #xf0f000f0 #x84840084
221
#xdfdf00df #xcbcb00cb #x34340034 #x76760076
222
#x6d6d006d #xa9a900a9 #xd1d100d1 #x04040004
223
#x14140014 #x3a3a003a #xdede00de #x11110011
224
#x32320032 #x9c9c009c #x53530053 #xf2f200f2
225
#xfefe00fe #xcfcf00cf #xc3c300c3 #x7a7a007a
226
#x24240024 #xe8e800e8 #x60600060 #x69690069
227
#xaaaa00aa #xa0a000a0 #xa1a100a1 #x62620062
228
#x54540054 #x1e1e001e #xe0e000e0 #x64640064
229
#x10100010 #x00000000 #xa3a300a3 #x75750075
230
#x8a8a008a #xe6e600e6 #x09090009 #xdddd00dd
231
#x87870087 #x83830083 #xcdcd00cd #x90900090
232
#x73730073 #xf6f600f6 #x9d9d009d #xbfbf00bf
233
#x52520052 #xd8d800d8 #xc8c800c8 #xc6c600c6
234
#x81810081 #x6f6f006f #x13130013 #x63630063
235
#xe9e900e9 #xa7a700a7 #x9f9f009f #xbcbc00bc
236
#x29290029 #xf9f900f9 #x2f2f002f #xb4b400b4
237
#x78780078 #x06060006 #xe7e700e7 #x71710071
238
#xd4d400d4 #xabab00ab #x88880088 #x8d8d008d
239
#x72720072 #xb9b900b9 #xf8f800f8 #xacac00ac
240
#x36360036 #x2a2a002a #x3c3c003c #xf1f100f1
241
#x40400040 #xd3d300d3 #xbbbb00bb #x43430043
242
#x15150015 #xadad00ad #x77770077 #x80800080
243
#x82820082 #xecec00ec #x27270027 #xe5e500e5
244
#x85850085 #x35350035 #x0c0c000c #x41410041
245
#xefef00ef #x93930093 #x19190019 #x21210021
246
#x0e0e000e #x4e4e004e #x65650065 #xbdbd00bd
247
#xb8b800b8 #x8f8f008f #xebeb00eb #xcece00ce
248
#x30300030 #x5f5f005f #xc5c500c5 #x1a1a001a
249
#xe1e100e1 #xcaca00ca #x47470047 #x3d3d003d
250
#x01010001 #xd6d600d6 #x56560056 #x4d4d004d
251
#x0d0d000d #x66660066 #xcccc00cc #x2d2d002d
252
#x12120012 #x20200020 #xb1b100b1 #x99990099
253
#x4c4c004c #xc2c200c2 #x7e7e007e #x05050005
254
#xb7b700b7 #x31310031 #x17170017 #xd7d700d7
255
#x58580058 #x61610061 #x1b1b001b #x1c1c001c
256
#x0f0f000f #x16160016 #x18180018 #x22220022
257
#x44440044 #xb2b200b2 #xb5b500b5 #x91910091
258
#x08080008 #xa8a800a8 #xfcfc00fc #x50500050
259
#xd0d000d0 #x7d7d007d #x89890089 #x97970097
260
#x5b5b005b #x95950095 #xffff00ff #xd2d200d2
261
#xc4c400c4 #x48480048 #xf7f700f7 #xdbdb00db
262
#x03030003 #xdada00da #x3f3f003f #x94940094
263
#x5c5c005c #x02020002 #x4a4a004a #x33330033
264
#x67670067 #xf3f300f3 #x7f7f007f #xe2e200e2
265
#x9b9b009b #x26260026 #x37370037 #x3b3b003b
266
#x96960096 #x4b4b004b #xbebe00be #x2e2e002e
267
#x79790079 #x8c8c008c #x6e6e006e #x8e8e008e
268
#xf5f500f5 #xb6b600b6 #xfdfd00fd #x59590059
269
#x98980098 #x6a6a006a #x46460046 #xbaba00ba
270
#x25250025 #x42420042 #xa2a200a2 #xfafa00fa
271
#x07070007 #x55550055 #xeeee00ee #x0a0a000a
272
#x49490049 #x68680068 #x38380038 #xa4a400a4
273
#x28280028 #x7b7b007b #xc9c900c9 #xc1c100c1
274
#xe3e300e3 #xf4f400f4 #xc7c700c7 #x9e9e009e)))
276
(defconst +camellia-sigma+
278
:element-type '(unsigned-byte 32)
279
:initial-contents '(#xa09e667f #x3bcc908b #xb67ae858 #x4caa73b2
280
#xc6ef372f #xe94f82be #x54ff53a5 #xf1d36f1c
281
#x10e527fa #xde682d1d #xb05688c2 #xb3e6c1fd)))
283
(defconst +camellia-ksft1+
285
:element-type '(unsigned-byte 7)
286
:initial-contents '(0 64 0 64 15 79 15 79 30 94 45 109 45
287
124 60 124 77 13 94 30 94 30 111 47 111 47)))
289
(defconst +camellia-kidx1+
291
:element-type '(unsigned-byte 4)
292
:initial-contents '(0 0 8 8 0 0 8 8 8 8 0 0 8
293
0 8 8 0 0 0 0 8 8 0 0 8 8)))
295
(defconst +camellia-ksft2+
297
:element-type '(unsigned-byte 7)
298
:initial-contents '(0 64 0 64 15 79 15 79 30 94 30 94 45 109 45 109 60
299
124 60 124 60 124 77 13 77 13 94 30 94 30 111 47 111 47)))
301
(defconst +camellia-kidx2+
303
:element-type '(unsigned-byte 4)
304
:initial-contents '(0 0 12 12 4 4 8 8 4 4 12 12 0 0 8 8 0
305
0 4 4 12 12 0 0 8 8 4 4 8 8 0 0 12 12)))
307
(declaim (ftype (function ((simple-array (unsigned-byte 32) (*))
309
(simple-array (unsigned-byte 32) (*))
313
(inline camellia-feistel))
314
(defun camellia-feistel (data data-start keys keys-start key-offset)
315
(declare (type (simple-array (unsigned-byte 32) (*)) data keys)
316
(type fixnum data-start keys-start key-offset)
317
(optimize (speed 3) (space 0) (safety 0) (debug 0)))
318
(macrolet ((sbox1 (n)
319
`(aref +camellia-sbox1+ (logand ,n 255)))
321
`(aref +camellia-sbox2+ (logand ,n 255)))
323
`(aref +camellia-sbox3+ (logand ,n 255)))
325
`(aref +camellia-sbox4+ (logand ,n 255))))
330
(declare (type (unsigned-byte 32) d u s1 s2))
331
(setf s1 (logxor (aref data data-start)
332
(aref keys keys-start))
334
(sbox3 (mod32ash s1 -8))
335
(sbox2 (mod32ash s1 -16))
336
(sbox1 (mod32ash s1 -24)))
337
s2 (logxor (aref data (+ data-start 1))
338
(aref keys (+ keys-start 1)))
340
(sbox4 (mod32ash s2 -8))
341
(sbox3 (mod32ash s2 -16))
342
(sbox2 (mod32ash s2 -24))))
343
(setf (aref data (+ data-start 2)) (logxor (aref data (+ data-start 2)) d u)
344
(aref data (+ data-start 3)) (logxor (aref data (+ data-start 3)) d u (ror32 u 8)))
345
(setf s1 (logxor (aref data (+ data-start 2))
346
(aref keys (+ keys-start key-offset)))
348
(sbox3 (mod32ash s1 -8))
349
(sbox2 (mod32ash s1 -16))
350
(sbox1 (mod32ash s1 -24)))
351
s2 (logxor (aref data (+ data-start 3))
352
(aref keys (+ keys-start key-offset 1)))
354
(sbox4 (mod32ash s2 -8))
355
(sbox3 (mod32ash s2 -16))
356
(sbox2 (mod32ash s2 -24))))
357
(setf (aref data data-start) (logxor (aref data data-start) d u)
358
(aref data (+ data-start 1)) (logxor (aref data (+ data-start 1)) d u (ror32 u 8)))
361
(defclass camellia (cipher 16-byte-block-mixin)
362
((round-keys :accessor round-keys
363
:type (simple-array (unsigned-byte 32) (68)))
364
(grand-rounds :accessor grand-rounds
365
:type (integer 3 4))))
367
(defmethod schedule-key ((cipher camellia) key)
368
(declare (type (simple-array (unsigned-byte 8) (*)) key))
369
(let ((round-keys (make-array 68 :element-type '(unsigned-byte 32)))
370
(data (make-array 16 :element-type '(unsigned-byte 32))))
371
(declare (type (simple-array (unsigned-byte 32) (*)) round-keys data)
372
(dynamic-extent data))
376
(setf (aref data i) (ub32ref/be key (* 4 i))))
377
(fill data 0 :start 4 :end 8))
381
(setf (aref data i) (ub32ref/be key (* 4 i))))
382
(setf (aref data 6) (mod32lognot (aref data 4))
383
(aref data 7) (mod32lognot (aref data 5))))
387
(setf (aref data i) (ub32ref/be key (* 4 i))))))
390
(setf (aref data (+ i 8)) (logxor (aref data i) (aref data (+ i 4)))))
391
(camellia-feistel data 8 +camellia-sigma+ 0 2)
393
(setf (aref data (+ i 8)) (logxor (aref data (+ i 8)) (aref data i))))
394
(camellia-feistel data 8 +camellia-sigma+ 4 2)
396
(flet ((rotblock (in in-start out out-start n)
397
(let* ((r (logand n 31))
399
(idx1 (logand (1+ idx) 3))
400
(idx2 (logand (1+ idx1) 3)))
401
(setf (aref out out-start) (logior (mod32ash (aref in (+ in-start idx))
403
(mod32ash (aref in (+ in-start idx1))
405
(aref out (+ out-start 1)) (logior (mod32ash (aref in (+ in-start idx1))
407
(mod32ash (aref in (+ in-start idx2))
410
(if (= 16 (length key))
412
(setf (grand-rounds cipher) 3)
413
(replace round-keys data :end2 4)
414
(replace round-keys data :start1 4 :start2 8 :end2 12)
415
(loop for i from 4 below 26 by 2 do
416
(rotblock data (aref +camellia-kidx1+ i)
418
(aref +camellia-ksft1+ i))
419
(rotblock data (aref +camellia-kidx1+ (1+ i))
420
round-keys (+ (* 2 i) 2)
421
(aref +camellia-ksft1+ (1+ i)))))
423
(setf (grand-rounds cipher) 4)
425
(setf (aref data (+ i 12)) (logxor (aref data (+ i 8)) (aref data (+ i 4)))))
426
(camellia-feistel data 12 +camellia-sigma+ 8 2)
427
(replace round-keys data :end2 4)
428
(replace round-keys data :start1 4 :start2 12 :end2 16)
429
(loop for i from 4 below 34 by 2 do
430
(rotblock data (aref +camellia-kidx2+ i)
432
(aref +camellia-ksft2+ i))
433
(rotblock data (aref +camellia-kidx2+ (1+ i))
434
round-keys (+ (* 2 i) 2)
435
(aref +camellia-ksft2+ (1+ i)))))))
436
(setf (round-keys cipher) round-keys)
439
(define-block-encryptor camellia 16
440
(let ((round-keys (round-keys context))
442
(grand-rounds (grand-rounds context))
443
(data (make-array 4 :element-type '(unsigned-byte 32))))
444
(declare (type (simple-array (unsigned-byte 32) (*)) round-keys data)
445
(type fixnum keys-start grand-rounds)
446
(dynamic-extent data))
447
(setf (aref data 0) (logxor (ub32ref/be plaintext plaintext-start)
449
(aref data 1) (logxor (ub32ref/be plaintext (+ plaintext-start 4))
451
(aref data 2) (logxor (ub32ref/be plaintext (+ plaintext-start 8))
453
(aref data 3) (logxor (ub32ref/be plaintext (+ plaintext-start 12))
454
(aref round-keys 3)))
456
(dotimes (i grand-rounds)
458
(camellia-feistel data 0 round-keys keys-start 2)
460
(when (< i (1- grand-rounds))
461
(setf (aref data 1) (logxor (aref data 1)
462
(rol32 (logand (aref data 0)
463
(aref round-keys keys-start))
465
(aref data 0) (logxor (aref data 0)
466
(logior (aref data 1) (aref round-keys (+ keys-start 1))))
467
(aref data 2) (logxor (aref data 2)
468
(logior (aref data 3) (aref round-keys (+ keys-start 3))))
469
(aref data 3) (logxor (aref data 3)
470
(rol32 (logand (aref data 2)
471
(aref round-keys (+ keys-start 2)))
473
(incf keys-start 4)))
475
(rotatef (aref data 0) (aref data 2))
476
(rotatef (aref data 1) (aref data 3))
477
(setf (ub32ref/be ciphertext ciphertext-start)
478
(logxor (aref data 0) (aref round-keys keys-start)))
479
(setf (ub32ref/be ciphertext (+ ciphertext-start 4))
480
(logxor (aref data 1) (aref round-keys (+ keys-start 1))))
481
(setf (ub32ref/be ciphertext (+ ciphertext-start 8))
482
(logxor (aref data 2) (aref round-keys (+ keys-start 2))))
483
(setf (ub32ref/be ciphertext (+ ciphertext-start 12))
484
(logxor (aref data 3) (aref round-keys (+ keys-start 3))))))
486
(define-block-decryptor camellia 16
487
(let* ((round-keys (round-keys context))
488
(grand-rounds (grand-rounds context))
489
(keys-start (if (= 3 grand-rounds) 48 64))
490
(data (make-array 4 :element-type '(unsigned-byte 32))))
491
(declare (type (simple-array (unsigned-byte 32) (*)) round-keys data)
492
(type fixnum keys-start grand-rounds)
493
(dynamic-extent data))
494
(setf (aref data 0) (logxor (ub32ref/be ciphertext ciphertext-start)
495
(aref round-keys keys-start))
496
(aref data 1) (logxor (ub32ref/be ciphertext (+ ciphertext-start 4))
497
(aref round-keys (+ keys-start 1)))
498
(aref data 2) (logxor (ub32ref/be ciphertext (+ ciphertext-start 8))
499
(aref round-keys (+ keys-start 2)))
500
(aref data 3) (logxor (ub32ref/be ciphertext (+ ciphertext-start 12))
501
(aref round-keys (+ keys-start 3))))
504
(dotimes (i grand-rounds)
506
(camellia-feistel data 0 round-keys keys-start -2)
508
(when (< i (1- grand-rounds))
509
(setf (aref data 1) (logxor (aref data 1)
510
(rol32 (logand (aref data 0)
511
(aref round-keys keys-start))
513
(aref data 0) (logxor (aref data 0)
514
(logior (aref data 1) (aref round-keys (+ keys-start 1))))
515
(aref data 2) (logxor (aref data 2)
516
(logior (aref data 3) (aref round-keys (- keys-start 1))))
517
(aref data 3) (logxor (aref data 3)
518
(rol32 (logand (aref data 2)
519
(aref round-keys (- keys-start 2)))
521
(decf keys-start 4)))
524
(rotatef (aref data 0) (aref data 2))
525
(rotatef (aref data 1) (aref data 3))
526
(setf (ub32ref/be plaintext plaintext-start)
527
(logxor (aref data 0) (aref round-keys keys-start)))
528
(setf (ub32ref/be plaintext (+ plaintext-start 4))
529
(logxor (aref data 1) (aref round-keys (+ keys-start 1))))
530
(setf (ub32ref/be plaintext (+ plaintext-start 8))
531
(logxor (aref data 2) (aref round-keys (+ keys-start 2))))
532
(setf (ub32ref/be plaintext (+ plaintext-start 12))
533
(logxor (aref data 3) (aref round-keys (+ keys-start 3))))))
536
(:encrypt-function camellia-encrypt-block)
537
(:decrypt-function camellia-decrypt-block)
539
(:key-length (:fixed 16 24 32)))