1 /* $OpenBSD: e_camellia.c,v 1.20 2024/04/09 13:52:41 beck Exp $ */
2 /* ====================================================================
3 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56 #include <string.h>
57
58 #include <openssl/opensslconf.h>
59
60 #ifndef OPENSSL_NO_CAMELLIA
61 #include <openssl/evp.h>
62 #include <openssl/err.h>
63 #include <openssl/camellia.h>
64
65 #include "evp_local.h"
66
67 /* Camellia subkey Structure */
68 typedef struct {
69 CAMELLIA_KEY ks;
70 } EVP_CAMELLIA_KEY;
71
72 /* Attribute operation for Camellia */
73 #define data(ctx) ((EVP_CAMELLIA_KEY *)(ctx)->cipher_data)
74
75 static int
camellia_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)76 camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
77 const unsigned char *iv, int enc)
78 {
79 int ret;
80
81 ret = Camellia_set_key(key, ctx->key_len * 8, ctx->cipher_data);
82
83 if (ret < 0) {
84 EVPerror(EVP_R_CAMELLIA_KEY_SETUP_FAILED);
85 return 0;
86 }
87
88 return 1;
89 }
90
91 static int
camellia_128_cbc_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)92 camellia_128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
93 {
94 while (inl >= EVP_MAXCHUNK) {
95 Camellia_cbc_encrypt(in, out, EVP_MAXCHUNK, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
96 inl -= EVP_MAXCHUNK;
97 in += EVP_MAXCHUNK;
98 out += EVP_MAXCHUNK;
99 }
100
101 if (inl)
102 Camellia_cbc_encrypt(in, out, inl, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
103
104 return 1;
105 }
106
107 static int
camellia_128_cfb128_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)108 camellia_128_cfb128_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
109 {
110 size_t chunk = EVP_MAXCHUNK;
111
112 if (inl < chunk)
113 chunk = inl;
114
115 while (inl && inl >= chunk) {
116 Camellia_cfb128_encrypt(in, out, chunk, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
117 inl -= chunk;
118 in += chunk;
119 out += chunk;
120 if (inl < chunk)
121 chunk = inl;
122 }
123
124 return 1;
125 }
126
127 static int
camellia_128_ecb_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)128 camellia_128_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
129 {
130 size_t i, bl;
131
132 bl = ctx->cipher->block_size;
133
134 if (inl < bl)
135 return 1;
136
137 inl -= bl;
138
139 for (i = 0; i <= inl; i += bl)
140 Camellia_ecb_encrypt(in + i, out + i, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->encrypt);
141
142 return 1;
143 }
144
145 static int
camellia_128_ofb_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)146 camellia_128_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
147 {
148 while (inl >= EVP_MAXCHUNK) {
149 Camellia_ofb128_encrypt(in, out, EVP_MAXCHUNK, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
150 inl -= EVP_MAXCHUNK;
151 in += EVP_MAXCHUNK;
152 out += EVP_MAXCHUNK;
153 }
154
155 if (inl)
156 Camellia_ofb128_encrypt(in, out, inl, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
157
158 return 1;
159 }
160
161 static const EVP_CIPHER camellia_128_cbc = {
162 .nid = NID_camellia_128_cbc,
163 .block_size = 16,
164 .key_len = 16,
165 .iv_len = 16,
166 .flags = 0 | EVP_CIPH_CBC_MODE,
167 .init = camellia_init_key,
168 .do_cipher = camellia_128_cbc_cipher,
169 .cleanup = NULL,
170 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
171 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
172 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
173 .ctrl = NULL,
174 };
175
176 const EVP_CIPHER *
EVP_camellia_128_cbc(void)177 EVP_camellia_128_cbc(void)
178 {
179 return &camellia_128_cbc;
180 }
181 LCRYPTO_ALIAS(EVP_camellia_128_cbc);
182
183 static const EVP_CIPHER camellia_128_cfb128 = {
184 .nid = NID_camellia_128_cfb128,
185 .block_size = 1,
186 .key_len = 16,
187 .iv_len = 16,
188 .flags = 0 | EVP_CIPH_CFB_MODE,
189 .init = camellia_init_key,
190 .do_cipher = camellia_128_cfb128_cipher,
191 .cleanup = NULL,
192 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
193 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
194 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
195 .ctrl = NULL,
196 };
197
198 const EVP_CIPHER *
EVP_camellia_128_cfb128(void)199 EVP_camellia_128_cfb128(void)
200 {
201 return &camellia_128_cfb128;
202 }
203 LCRYPTO_ALIAS(EVP_camellia_128_cfb128);
204
205 static const EVP_CIPHER camellia_128_ofb = {
206 .nid = NID_camellia_128_ofb128,
207 .block_size = 1,
208 .key_len = 16,
209 .iv_len = 16,
210 .flags = 0 | EVP_CIPH_OFB_MODE,
211 .init = camellia_init_key,
212 .do_cipher = camellia_128_ofb_cipher,
213 .cleanup = NULL,
214 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
215 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
216 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
217 .ctrl = NULL,
218 };
219
220 const EVP_CIPHER *
EVP_camellia_128_ofb(void)221 EVP_camellia_128_ofb(void)
222 {
223 return &camellia_128_ofb;
224 }
225 LCRYPTO_ALIAS(EVP_camellia_128_ofb);
226
227 static const EVP_CIPHER camellia_128_ecb = {
228 .nid = NID_camellia_128_ecb,
229 .block_size = 16,
230 .key_len = 16,
231 .iv_len = 0,
232 .flags = 0 | EVP_CIPH_ECB_MODE,
233 .init = camellia_init_key,
234 .do_cipher = camellia_128_ecb_cipher,
235 .cleanup = NULL,
236 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
237 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
238 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
239 .ctrl = NULL,
240 };
241
242 const EVP_CIPHER *
EVP_camellia_128_ecb(void)243 EVP_camellia_128_ecb(void)
244 {
245 return &camellia_128_ecb;
246 }
247 LCRYPTO_ALIAS(EVP_camellia_128_ecb);
248
249 static int
camellia_192_cbc_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)250 camellia_192_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
251 {
252 while (inl >= EVP_MAXCHUNK) {
253 Camellia_cbc_encrypt(in, out, EVP_MAXCHUNK, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
254 inl -= EVP_MAXCHUNK;
255 in += EVP_MAXCHUNK;
256 out += EVP_MAXCHUNK;
257 }
258
259 if (inl)
260 Camellia_cbc_encrypt(in, out, inl, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
261
262 return 1;
263 }
264
265 static int
camellia_192_cfb128_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)266 camellia_192_cfb128_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
267 {
268 size_t chunk = EVP_MAXCHUNK;
269
270 if (inl < chunk)
271 chunk = inl;
272
273 while (inl && inl >= chunk) {
274 Camellia_cfb128_encrypt(in, out, chunk, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
275 inl -= chunk;
276 in += chunk;
277 out += chunk;
278 if (inl < chunk)
279 chunk = inl;
280 }
281
282 return 1;
283 }
284
285 static int
camellia_192_ecb_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)286 camellia_192_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
287 {
288 size_t i, bl;
289
290 bl = ctx->cipher->block_size;
291
292 if (inl < bl)
293 return 1;
294
295 inl -= bl;
296
297 for (i = 0; i <= inl; i += bl)
298 Camellia_ecb_encrypt(in + i, out + i, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->encrypt);
299
300 return 1;
301 }
302
303 static int
camellia_192_ofb_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)304 camellia_192_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
305 {
306 while (inl >= EVP_MAXCHUNK) {
307 Camellia_ofb128_encrypt(in, out, EVP_MAXCHUNK, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
308 inl -= EVP_MAXCHUNK;
309 in += EVP_MAXCHUNK;
310 out += EVP_MAXCHUNK;
311 }
312
313 if (inl)
314 Camellia_ofb128_encrypt(in, out, inl, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
315
316 return 1;
317 }
318
319 static const EVP_CIPHER camellia_192_cbc = {
320 .nid = NID_camellia_192_cbc,
321 .block_size = 16,
322 .key_len = 24,
323 .iv_len = 16,
324 .flags = 0 | EVP_CIPH_CBC_MODE,
325 .init = camellia_init_key,
326 .do_cipher = camellia_192_cbc_cipher,
327 .cleanup = NULL,
328 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
329 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
330 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
331 .ctrl = NULL,
332 };
333
334 const EVP_CIPHER *
EVP_camellia_192_cbc(void)335 EVP_camellia_192_cbc(void)
336 {
337 return &camellia_192_cbc;
338 }
339 LCRYPTO_ALIAS(EVP_camellia_192_cbc);
340
341 static const EVP_CIPHER camellia_192_cfb128 = {
342 .nid = NID_camellia_192_cfb128,
343 .block_size = 1,
344 .key_len = 24,
345 .iv_len = 16,
346 .flags = 0 | EVP_CIPH_CFB_MODE,
347 .init = camellia_init_key,
348 .do_cipher = camellia_192_cfb128_cipher,
349 .cleanup = NULL,
350 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
351 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
352 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
353 .ctrl = NULL,
354 };
355
356 const EVP_CIPHER *
EVP_camellia_192_cfb128(void)357 EVP_camellia_192_cfb128(void)
358 {
359 return &camellia_192_cfb128;
360 }
361 LCRYPTO_ALIAS(EVP_camellia_192_cfb128);
362
363 static const EVP_CIPHER camellia_192_ofb = {
364 .nid = NID_camellia_192_ofb128,
365 .block_size = 1,
366 .key_len = 24,
367 .iv_len = 16,
368 .flags = 0 | EVP_CIPH_OFB_MODE,
369 .init = camellia_init_key,
370 .do_cipher = camellia_192_ofb_cipher,
371 .cleanup = NULL,
372 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
373 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
374 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
375 .ctrl = NULL,
376 };
377
378 const EVP_CIPHER *
EVP_camellia_192_ofb(void)379 EVP_camellia_192_ofb(void)
380 {
381 return &camellia_192_ofb;
382 }
383 LCRYPTO_ALIAS(EVP_camellia_192_ofb);
384
385 static const EVP_CIPHER camellia_192_ecb = {
386 .nid = NID_camellia_192_ecb,
387 .block_size = 16,
388 .key_len = 24,
389 .iv_len = 0,
390 .flags = 0 | EVP_CIPH_ECB_MODE,
391 .init = camellia_init_key,
392 .do_cipher = camellia_192_ecb_cipher,
393 .cleanup = NULL,
394 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
395 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
396 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
397 .ctrl = NULL,
398 };
399
400 const EVP_CIPHER *
EVP_camellia_192_ecb(void)401 EVP_camellia_192_ecb(void)
402 {
403 return &camellia_192_ecb;
404 }
405 LCRYPTO_ALIAS(EVP_camellia_192_ecb);
406
407 static int
camellia_256_cbc_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)408 camellia_256_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
409 {
410 while (inl >= EVP_MAXCHUNK) {
411 Camellia_cbc_encrypt(in, out, EVP_MAXCHUNK, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
412 inl -= EVP_MAXCHUNK;
413 in += EVP_MAXCHUNK;
414 out += EVP_MAXCHUNK;
415 }
416
417 if (inl)
418 Camellia_cbc_encrypt(in, out, inl, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
419
420 return 1;
421 }
422
423 static int
camellia_256_cfb128_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)424 camellia_256_cfb128_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
425 {
426 size_t chunk = EVP_MAXCHUNK;
427
428 if (inl < chunk)
429 chunk = inl;
430
431 while (inl && inl >= chunk) {
432 Camellia_cfb128_encrypt(in, out, chunk, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
433 inl -= chunk;
434 in += chunk;
435 out += chunk;
436 if (inl < chunk)
437 chunk = inl;
438 }
439
440 return 1;
441 }
442
443 static int
camellia_256_ecb_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)444 camellia_256_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
445 {
446 size_t i, bl;
447
448 bl = ctx->cipher->block_size;
449
450 if (inl < bl)
451 return 1;
452
453 inl -= bl;
454
455 for (i = 0; i <= inl; i += bl)
456 Camellia_ecb_encrypt(in + i, out + i, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->encrypt);
457
458 return 1;
459 }
460
461 static int
camellia_256_ofb_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)462 camellia_256_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
463 {
464 while (inl >= EVP_MAXCHUNK) {
465 Camellia_ofb128_encrypt(in, out, EVP_MAXCHUNK, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
466 inl -= EVP_MAXCHUNK;
467 in += EVP_MAXCHUNK;
468 out += EVP_MAXCHUNK;
469 }
470
471 if (inl)
472 Camellia_ofb128_encrypt(in, out, inl, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
473
474 return 1;
475 }
476
477 static const EVP_CIPHER camellia_256_cbc = {
478 .nid = NID_camellia_256_cbc,
479 .block_size = 16,
480 .key_len = 32,
481 .iv_len = 16,
482 .flags = 0 | EVP_CIPH_CBC_MODE,
483 .init = camellia_init_key,
484 .do_cipher = camellia_256_cbc_cipher,
485 .cleanup = NULL,
486 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
487 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
488 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
489 .ctrl = NULL,
490 };
491
492 const EVP_CIPHER *
EVP_camellia_256_cbc(void)493 EVP_camellia_256_cbc(void)
494 {
495 return &camellia_256_cbc;
496 }
497 LCRYPTO_ALIAS(EVP_camellia_256_cbc);
498
499 static const EVP_CIPHER camellia_256_cfb128 = {
500 .nid = NID_camellia_256_cfb128,
501 .block_size = 1,
502 .key_len = 32,
503 .iv_len = 16,
504 .flags = 0 | EVP_CIPH_CFB_MODE,
505 .init = camellia_init_key,
506 .do_cipher = camellia_256_cfb128_cipher,
507 .cleanup = NULL,
508 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
509 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
510 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
511 .ctrl = NULL,
512 };
513
514 const EVP_CIPHER *
EVP_camellia_256_cfb128(void)515 EVP_camellia_256_cfb128(void)
516 {
517 return &camellia_256_cfb128;
518 }
519 LCRYPTO_ALIAS(EVP_camellia_256_cfb128);
520
521 static const EVP_CIPHER camellia_256_ofb = {
522 .nid = NID_camellia_256_ofb128,
523 .block_size = 1,
524 .key_len = 32,
525 .iv_len = 16,
526 .flags = 0 | EVP_CIPH_OFB_MODE,
527 .init = camellia_init_key,
528 .do_cipher = camellia_256_ofb_cipher,
529 .cleanup = NULL,
530 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
531 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
532 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
533 .ctrl = NULL,
534 };
535
536 const EVP_CIPHER *
EVP_camellia_256_ofb(void)537 EVP_camellia_256_ofb(void)
538 {
539 return &camellia_256_ofb;
540 }
541 LCRYPTO_ALIAS(EVP_camellia_256_ofb);
542
543 static const EVP_CIPHER camellia_256_ecb = {
544 .nid = NID_camellia_256_ecb,
545 .block_size = 16,
546 .key_len = 32,
547 .iv_len = 0,
548 .flags = 0 | EVP_CIPH_ECB_MODE,
549 .init = camellia_init_key,
550 .do_cipher = camellia_256_ecb_cipher,
551 .cleanup = NULL,
552 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
553 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
554 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
555 .ctrl = NULL,
556 };
557
558 const EVP_CIPHER *
EVP_camellia_256_ecb(void)559 EVP_camellia_256_ecb(void)
560 {
561 return &camellia_256_ecb;
562 }
563 LCRYPTO_ALIAS(EVP_camellia_256_ecb);
564
565 static int
camellia_128_cfb1_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)566 camellia_128_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
567 {
568 size_t chunk = EVP_MAXCHUNK;
569
570 chunk >>= 3;
571
572 if (inl < chunk)
573 chunk = inl;
574
575 while (inl && inl >= chunk) {
576 Camellia_cfb1_encrypt(in, out, ((1 == 1) && !(ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) ? chunk * 8 : chunk), &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
577 inl -= chunk;
578 in += chunk;
579 out += chunk;
580 if (inl < chunk)
581 chunk = inl;
582 }
583
584 return 1;
585 }
586
587 static const EVP_CIPHER camellia_128_cfb1 = {
588 .nid = NID_camellia_128_cfb1,
589 .block_size = 1,
590 .key_len = 128/8,
591 .iv_len = 16,
592 .flags = 0 | EVP_CIPH_CFB_MODE,
593 .init = camellia_init_key,
594 .do_cipher = camellia_128_cfb1_cipher,
595 .cleanup = NULL,
596 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
597 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
598 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
599 .ctrl = NULL,
600 };
601
602 const EVP_CIPHER *
EVP_camellia_128_cfb1(void)603 EVP_camellia_128_cfb1(void)
604 {
605 return &camellia_128_cfb1;
606 }
607 LCRYPTO_ALIAS(EVP_camellia_128_cfb1);
608
609 static int
camellia_192_cfb1_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)610 camellia_192_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
611 {
612 size_t chunk = EVP_MAXCHUNK;
613
614 chunk >>= 3;
615
616 if (inl < chunk)
617 chunk = inl;
618
619 while (inl && inl >= chunk) {
620 Camellia_cfb1_encrypt(in, out, ((1 == 1) && !(ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) ? chunk * 8 : chunk), &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
621 inl -= chunk;
622 in += chunk;
623 out += chunk;
624 if (inl < chunk)
625 chunk = inl;
626 }
627
628 return 1;
629 }
630
631 static const EVP_CIPHER camellia_192_cfb1 = {
632 .nid = NID_camellia_192_cfb1,
633 .block_size = 1,
634 .key_len = 192/8,
635 .iv_len = 16,
636 .flags = 0 | EVP_CIPH_CFB_MODE,
637 .init = camellia_init_key,
638 .do_cipher = camellia_192_cfb1_cipher,
639 .cleanup = NULL,
640 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
641 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
642 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
643 .ctrl = NULL,
644 };
645
646 const EVP_CIPHER *
EVP_camellia_192_cfb1(void)647 EVP_camellia_192_cfb1(void)
648 {
649 return &camellia_192_cfb1;
650 }
651 LCRYPTO_ALIAS(EVP_camellia_192_cfb1);
652
653 static int
camellia_256_cfb1_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)654 camellia_256_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
655 {
656 size_t chunk = EVP_MAXCHUNK;
657
658 chunk >>= 3;
659
660 if (inl < chunk)
661 chunk = inl;
662
663 while (inl && inl >= chunk) {
664 Camellia_cfb1_encrypt(in, out, ((1 == 1) && !(ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) ? chunk * 8 : chunk), &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
665 inl -= chunk;
666 in += chunk;
667 out += chunk;
668 if (inl < chunk)
669 chunk = inl;
670 }
671
672 return 1;
673 }
674
675 static const EVP_CIPHER camellia_256_cfb1 = {
676 .nid = NID_camellia_256_cfb1,
677 .block_size = 1,
678 .key_len = 256/8,
679 .iv_len = 16,
680 .flags = 0 | EVP_CIPH_CFB_MODE,
681 .init = camellia_init_key,
682 .do_cipher = camellia_256_cfb1_cipher,
683 .cleanup = NULL,
684 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
685 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
686 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
687 .ctrl = NULL,
688 };
689
690 const EVP_CIPHER *
EVP_camellia_256_cfb1(void)691 EVP_camellia_256_cfb1(void)
692 {
693 return &camellia_256_cfb1;
694 }
695 LCRYPTO_ALIAS(EVP_camellia_256_cfb1);
696
697
698 static int
camellia_128_cfb8_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)699 camellia_128_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
700 {
701 size_t chunk = EVP_MAXCHUNK;
702
703 if (inl < chunk)
704 chunk = inl;
705
706 while (inl && inl >= chunk) {
707 Camellia_cfb8_encrypt(in, out, chunk, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
708 inl -= chunk;
709 in += chunk;
710 out += chunk;
711 if (inl < chunk)
712 chunk = inl;
713 }
714
715 return 1;
716 }
717
718 static const EVP_CIPHER camellia_128_cfb8 = {
719 .nid = NID_camellia_128_cfb8,
720 .block_size = 1,
721 .key_len = 128/8,
722 .iv_len = 16,
723 .flags = 0 | EVP_CIPH_CFB_MODE,
724 .init = camellia_init_key,
725 .do_cipher = camellia_128_cfb8_cipher,
726 .cleanup = NULL,
727 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
728 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
729 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
730 .ctrl = NULL,
731 };
732
733 const EVP_CIPHER *
EVP_camellia_128_cfb8(void)734 EVP_camellia_128_cfb8(void)
735 {
736 return &camellia_128_cfb8;
737 }
738 LCRYPTO_ALIAS(EVP_camellia_128_cfb8);
739
740 static int
camellia_192_cfb8_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)741 camellia_192_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
742 {
743 size_t chunk = EVP_MAXCHUNK;
744
745 if (inl < chunk)
746 chunk = inl;
747
748 while (inl && inl >= chunk) {
749 Camellia_cfb8_encrypt(in, out, chunk, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
750 inl -= chunk;
751 in += chunk;
752 out += chunk;
753 if (inl < chunk)
754 chunk = inl;
755 }
756
757 return 1;
758 }
759
760 static const EVP_CIPHER camellia_192_cfb8 = {
761 .nid = NID_camellia_192_cfb8,
762 .block_size = 1,
763 .key_len = 192/8,
764 .iv_len = 16,
765 .flags = 0 | EVP_CIPH_CFB_MODE,
766 .init = camellia_init_key,
767 .do_cipher = camellia_192_cfb8_cipher,
768 .cleanup = NULL,
769 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
770 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
771 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
772 .ctrl = NULL,
773 };
774
775 const EVP_CIPHER *
EVP_camellia_192_cfb8(void)776 EVP_camellia_192_cfb8(void)
777 {
778 return &camellia_192_cfb8;
779 }
780 LCRYPTO_ALIAS(EVP_camellia_192_cfb8);
781
782 static int
camellia_256_cfb8_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)783 camellia_256_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
784 {
785 size_t chunk = EVP_MAXCHUNK;
786
787 if (inl < chunk)
788 chunk = inl;
789
790 while (inl && inl >= chunk) {
791 Camellia_cfb8_encrypt(in, out, chunk, &((EVP_CAMELLIA_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
792 inl -= chunk;
793 in += chunk;
794 out += chunk;
795 if (inl < chunk)
796 chunk = inl;
797 }
798
799 return 1;
800 }
801
802 static const EVP_CIPHER camellia_256_cfb8 = {
803 .nid = NID_camellia_256_cfb8,
804 .block_size = 1,
805 .key_len = 256/8,
806 .iv_len = 16,
807 .flags = 0 | EVP_CIPH_CFB_MODE,
808 .init = camellia_init_key,
809 .do_cipher = camellia_256_cfb8_cipher,
810 .cleanup = NULL,
811 .ctx_size = sizeof(EVP_CAMELLIA_KEY),
812 .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
813 .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
814 .ctrl = NULL,
815 };
816
817 const EVP_CIPHER *
EVP_camellia_256_cfb8(void)818 EVP_camellia_256_cfb8(void)
819 {
820 return &camellia_256_cfb8;
821 }
822 LCRYPTO_ALIAS(EVP_camellia_256_cfb8);
823 #endif
824