Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/ttssh2/ttxssh/cipher-ctr.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10648 - (show annotations) (download) (as text)
Tue Mar 28 14:12:32 2023 UTC (12 months, 2 weeks ago) by nmaya
File MIME type: text/x-csrc
File size: 17194 byte(s)
EVP_CIPHER 構造体を用いる場合と、EVP_CIPHER_meth_new() 関数を用いる場合の条件分岐を整理

LibreSSL 3.5.0-3.7.0 は EVP_CIPHER 構造体も EVP_CIPHER_meth_new() 関数も使えない

ticket #45656, #43469, wiki:暗号ライブラリ
1 /*
2 * Import from OpenSSH (TeraTerm Project 2008.11.18)
3 */
4 /* $OpenBSD: cipher-ctr.c,v 1.10 2006/08/03 03:34:42 deraadt Exp $ */
5 /*
6 * Copyright (c) 2003 Markus Friedl <markus@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20 #include <sys/types.h>
21 #include <malloc.h>
22 #include <string.h>
23 #include <windows.h>
24
25 #include "config.h"
26
27 #include <openssl/evp.h>
28 #include <openssl/aes.h>
29 #include <openssl/des.h>
30 #include <openssl/blowfish.h>
31 #include <openssl/cast.h>
32 #include <openssl/camellia.h>
33
34 #include "cipher-ctr.h"
35
36 struct ssh_aes_ctr_ctx
37 {
38 AES_KEY aes_ctx;
39 unsigned char aes_counter[AES_BLOCK_SIZE];
40 };
41
42 #define DES_BLOCK_SIZE sizeof(DES_cblock)
43 struct ssh_des3_ctr_ctx
44 {
45 DES_key_schedule des3_ctx[3];
46 unsigned char des3_counter[DES_BLOCK_SIZE];
47 };
48
49 struct ssh_blowfish_ctr_ctx
50 {
51 BF_KEY blowfish_ctx;
52 unsigned char blowfish_counter[BF_BLOCK];
53 };
54
55 struct ssh_cast5_ctr_ctx
56 {
57 CAST_KEY cast5_ctx;
58 unsigned char cast5_counter[CAST_BLOCK];
59 };
60
61 struct ssh_camellia_ctr_ctx
62 {
63 CAMELLIA_KEY camellia_ctx;
64 unsigned char camellia_counter[CAMELLIA_BLOCK_SIZE];
65 };
66
67 static void
68 ssh_ctr_inc(unsigned char *ctr, unsigned int len)
69 {
70 int i;
71
72 for ( i = len - 1; i>= 0; i--)
73 if (++ctr[i])
74 return;
75 }
76
77 //============================================================================
78 // AES
79 //============================================================================
80 static int
81 ssh_aes_ctr(EVP_CIPHER_CTX *ctx, unsigned char *dest, const unsigned char *src, size_t len)
82 {
83 struct ssh_aes_ctr_ctx *c;
84 unsigned int n = 0;
85 unsigned char buf[AES_BLOCK_SIZE];
86
87 if (len == 0)
88 return (1);
89 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
90 return (0);
91
92 while ((len--) > 0) {
93 if (n == 0) {
94 AES_encrypt(c->aes_counter, buf, &c->aes_ctx);
95 ssh_ctr_inc(c->aes_counter, AES_BLOCK_SIZE);
96 }
97 *(dest++) = *(src++) ^ buf[n];
98 n = (n + 1) % AES_BLOCK_SIZE;
99 }
100 return (1);
101 }
102
103 static int
104 ssh_aes_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc)
105 {
106 struct ssh_aes_ctr_ctx *c;
107
108 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
109 c = malloc(sizeof(*c));
110 EVP_CIPHER_CTX_set_app_data(ctx, c);
111 }
112 if (key != NULL)
113 AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, &c->aes_ctx);
114 if (iv != NULL)
115 memcpy(c->aes_counter, iv, AES_BLOCK_SIZE);
116 return (1);
117 }
118
119 static int
120 ssh_aes_ctr_cleanup(EVP_CIPHER_CTX *ctx)
121 {
122 struct ssh_aes_ctr_ctx *c;
123
124 if((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
125 SecureZeroMemory(c, sizeof(*c));
126 free(c);
127 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
128 }
129 return (1);
130 }
131
132 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER <= 0x3040300fL || LIBRESSL_VERSION_NUMBER >= 0x3070100fL
133 const EVP_CIPHER *
134 evp_aes_128_ctr(void)
135 {
136 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x3070100fL
137 static EVP_CIPHER *p = NULL;
138
139 if (p == NULL) {
140 p = EVP_CIPHER_meth_new(NID_undef, /*block_size*/AES_BLOCK_SIZE, /*key_len*/16);
141 /*** TODO: OPENSSL1.1.1 ERROR CHECK(ticket#39335�����u�\��) ***/
142 }
143 if (p) {
144 EVP_CIPHER_meth_set_iv_length(p, AES_BLOCK_SIZE);
145 EVP_CIPHER_meth_set_init(p, ssh_aes_ctr_init);
146 EVP_CIPHER_meth_set_cleanup(p, ssh_aes_ctr_cleanup);
147 EVP_CIPHER_meth_set_do_cipher(p, ssh_aes_ctr);
148 EVP_CIPHER_meth_set_flags(p, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV);
149 }
150 return (p);
151 #else
152 static EVP_CIPHER aes_ctr;
153
154 memset(&aes_ctr, 0, sizeof(EVP_CIPHER));
155 aes_ctr.nid = NID_undef;
156 aes_ctr.block_size = AES_BLOCK_SIZE;
157 aes_ctr.iv_len = AES_BLOCK_SIZE;
158 aes_ctr.key_len = 16;
159 aes_ctr.init = ssh_aes_ctr_init;
160 aes_ctr.cleanup = ssh_aes_ctr_cleanup;
161 aes_ctr.do_cipher = ssh_aes_ctr;
162 #ifndef SSH_OLD_EVP
163 aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
164 #endif
165 return (&aes_ctr);
166 #endif
167 }
168 #endif
169
170 //============================================================================
171 // Triple-DES
172 //============================================================================
173 static int
174 ssh_des3_ctr(EVP_CIPHER_CTX *ctx, unsigned char *dest, const unsigned char *src, size_t len)
175 {
176 struct ssh_des3_ctr_ctx *c;
177 unsigned int n = 0;
178 unsigned char buf[DES_BLOCK_SIZE];
179
180 if (len == 0)
181 return (1);
182 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
183 return (0);
184
185 while ((len--) > 0) {
186 if (n == 0) {
187 memcpy(buf, (unsigned char *)(c->des3_counter), DES_BLOCK_SIZE);
188 DES_encrypt3((DES_LONG *)buf, &c->des3_ctx[0], &c->des3_ctx[1], &c->des3_ctx[2]);
189 ssh_ctr_inc(c->des3_counter, DES_BLOCK_SIZE);
190 }
191 *(dest++) = *(src++) ^ buf[n];
192 n = (n + 1) % DES_BLOCK_SIZE;
193 }
194 return (1);
195 }
196
197 static int
198 ssh_des3_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc)
199 {
200 struct ssh_des3_ctr_ctx *c;
201
202 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
203 c = malloc(sizeof(*c));
204 EVP_CIPHER_CTX_set_app_data(ctx, c);
205 }
206 if (key != NULL) {
207 DES_set_key((const_DES_cblock *)key, &c->des3_ctx[0]);
208 DES_set_key((const_DES_cblock *)(key + 8), &c->des3_ctx[1]);
209 DES_set_key((const_DES_cblock *)(key + 16), &c->des3_ctx[2]);
210 }
211
212 if (iv != NULL)
213 memcpy(c->des3_counter, iv, DES_BLOCK_SIZE);
214 return (1);
215 }
216
217 static int
218 ssh_des3_ctr_cleanup(EVP_CIPHER_CTX *ctx)
219 {
220 struct ssh_des3_ctr_ctx *c;
221
222 if((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
223 SecureZeroMemory(c, sizeof(*c));
224 free(c);
225 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
226 }
227 return (1);
228 }
229
230 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER <= 0x3040300fL || LIBRESSL_VERSION_NUMBER >= 0x3070100fL
231 const EVP_CIPHER *
232 evp_des3_ctr(void)
233 {
234 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x3070100fL
235 static EVP_CIPHER *p = NULL;
236
237 if (p == NULL) {
238 p = EVP_CIPHER_meth_new(NID_undef, /*block_size*/DES_BLOCK_SIZE, /*key_len*/24);
239 /*** TODO: OPENSSL1.1.1 ERROR CHECK(ticket#39335�����u�\��) ***/
240 }
241 if (p) {
242 EVP_CIPHER_meth_set_iv_length(p, DES_BLOCK_SIZE);
243 EVP_CIPHER_meth_set_init(p, ssh_des3_ctr_init);
244 EVP_CIPHER_meth_set_cleanup(p, ssh_des3_ctr_cleanup);
245 EVP_CIPHER_meth_set_do_cipher(p, ssh_des3_ctr);
246 EVP_CIPHER_meth_set_flags(p, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV);
247 }
248 return (p);
249 #else
250 static EVP_CIPHER des3_ctr;
251
252 memset(&des3_ctr, 0, sizeof(EVP_CIPHER));
253 des3_ctr.nid = NID_undef;
254 des3_ctr.block_size = DES_BLOCK_SIZE;
255 des3_ctr.iv_len = DES_BLOCK_SIZE;
256 des3_ctr.key_len = 24;
257 des3_ctr.init = ssh_des3_ctr_init;
258 des3_ctr.cleanup = ssh_des3_ctr_cleanup;
259 des3_ctr.do_cipher = ssh_des3_ctr;
260 #ifndef SSH_OLD_EVP
261 des3_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
262 #endif
263 return (&des3_ctr);
264 #endif
265 }
266 #endif
267
268 //============================================================================
269 // Blowfish
270 //============================================================================
271 static int
272 ssh_bf_ctr(EVP_CIPHER_CTX *ctx, unsigned char *dest, const unsigned char *src, size_t len)
273 {
274 struct ssh_blowfish_ctr_ctx *c;
275 unsigned int n = 0;
276 unsigned char buf[BF_BLOCK];
277 int i, j;
278 BF_LONG tmp[(BF_BLOCK + 3) / 4];
279 unsigned char *p;
280
281 if (len == 0)
282 return (1);
283 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
284 return (0);
285
286 while ((len--) > 0) {
287 if (n == 0) {
288 for (i = j = 0, p = c->blowfish_counter; i < BF_BLOCK; i += 4, j++) {
289 tmp[j] = ((BF_LONG)*p++) << 24;
290 tmp[j] |= ((BF_LONG)*p++) << 16;
291 tmp[j] |= ((BF_LONG)*p++) << 8;
292 tmp[j] |= ((BF_LONG)*p++);
293 }
294
295 BF_encrypt(tmp, &c->blowfish_ctx);
296
297 for (i = j = 0, p = buf; i < BF_BLOCK; i += 4, j++) {
298 *p++ = (unsigned char)(tmp[j] >> 24);
299 *p++ = (unsigned char)(tmp[j] >> 16);
300 *p++ = (unsigned char)(tmp[j] >> 8);
301 *p++ = (unsigned char)tmp[j];
302 }
303
304 ssh_ctr_inc(c->blowfish_counter, BF_BLOCK);
305 }
306 *(dest++) = *(src++) ^ buf[n];
307 n = (n + 1) % BF_BLOCK;
308 }
309 return (1);
310 }
311
312 static int
313 ssh_bf_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc)
314 {
315 struct ssh_blowfish_ctr_ctx *c;
316
317 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
318 c = malloc(sizeof(*c));
319 EVP_CIPHER_CTX_set_app_data(ctx, c);
320 }
321 if (key != NULL) {
322 BF_set_key(&c->blowfish_ctx, EVP_CIPHER_CTX_key_length(ctx), key);
323 }
324
325 if (iv != NULL)
326 memcpy(c->blowfish_counter, iv, BF_BLOCK);
327 return (1);
328 }
329
330 static int
331 ssh_bf_ctr_cleanup(EVP_CIPHER_CTX *ctx)
332 {
333 struct ssh_blowfish_ctr_ctx *c;
334
335 if((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
336 SecureZeroMemory(c, sizeof(*c));
337 free(c);
338 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
339 }
340 return (1);
341 }
342
343 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER <= 0x3040300fL || LIBRESSL_VERSION_NUMBER >= 0x3070100fL
344 const EVP_CIPHER *
345 evp_bf_ctr(void)
346 {
347 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x3070100fL
348 static EVP_CIPHER *p = NULL;
349
350 if (p == NULL) {
351 p = EVP_CIPHER_meth_new(NID_undef, /*block_size*/BF_BLOCK, /*key_len*/16);
352 /*** TODO: OPENSSL1.1.1 ERROR CHECK(ticket#39335�����u�\��) ***/
353 }
354 if (p) {
355 EVP_CIPHER_meth_set_iv_length(p, BF_BLOCK);
356 EVP_CIPHER_meth_set_init(p, ssh_bf_ctr_init);
357 EVP_CIPHER_meth_set_cleanup(p, ssh_bf_ctr_cleanup);
358 EVP_CIPHER_meth_set_do_cipher(p, ssh_bf_ctr);
359 EVP_CIPHER_meth_set_flags(p, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV);
360 }
361 return (p);
362 #else
363 static EVP_CIPHER blowfish_ctr;
364
365 memset(&blowfish_ctr, 0, sizeof(EVP_CIPHER));
366 blowfish_ctr.nid = NID_undef;
367 blowfish_ctr.block_size = BF_BLOCK;
368 blowfish_ctr.iv_len = BF_BLOCK;
369 blowfish_ctr.key_len = 16;
370 blowfish_ctr.init = ssh_bf_ctr_init;
371 blowfish_ctr.cleanup = ssh_bf_ctr_cleanup;
372 blowfish_ctr.do_cipher = ssh_bf_ctr;
373 #ifndef SSH_OLD_EVP
374 blowfish_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
375 #endif
376 return (&blowfish_ctr);
377 #endif
378 }
379 #endif
380
381 //============================================================================
382 // CAST-128
383 //============================================================================
384 static int
385 ssh_cast5_ctr(EVP_CIPHER_CTX *ctx, unsigned char *dest, const unsigned char *src, size_t len)
386 {
387 struct ssh_cast5_ctr_ctx *c;
388 unsigned int n = 0;
389 unsigned char buf[CAST_BLOCK];
390 int i, j;
391 CAST_LONG tmp[(CAST_BLOCK + 3) / 4];
392 unsigned char *p;
393
394 if (len == 0)
395 return (1);
396 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
397 return (0);
398
399 while ((len--) > 0) {
400 if (n == 0) {
401 for (i = j = 0, p = c->cast5_counter; i < CAST_BLOCK; i += 4, j++) {
402 tmp[j] = ((CAST_LONG)*p++) << 24;
403 tmp[j] |= ((CAST_LONG)*p++) << 16;
404 tmp[j] |= ((CAST_LONG)*p++) << 8;
405 tmp[j] |= ((CAST_LONG)*p++);
406 }
407
408 CAST_encrypt(tmp, &c->cast5_ctx);
409
410 for (i = j = 0, p = buf; i < CAST_BLOCK; i += 4, j++) {
411 *p++ = (unsigned char)(tmp[j] >> 24);
412 *p++ = (unsigned char)(tmp[j] >> 16);
413 *p++ = (unsigned char)(tmp[j] >> 8);
414 *p++ = (unsigned char)tmp[j];
415 }
416
417 ssh_ctr_inc(c->cast5_counter, CAST_BLOCK);
418 }
419 *(dest++) = *(src++) ^ buf[n];
420 n = (n + 1) % CAST_BLOCK;
421 }
422 return (1);
423 }
424
425 static int
426 ssh_cast5_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc)
427 {
428 struct ssh_cast5_ctr_ctx *c;
429
430 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
431 c = malloc(sizeof(*c));
432 EVP_CIPHER_CTX_set_app_data(ctx, c);
433 }
434 if (key != NULL) {
435 CAST_set_key(&c->cast5_ctx, EVP_CIPHER_CTX_key_length(ctx), key);
436 }
437
438 if (iv != NULL)
439 memcpy(c->cast5_counter, iv, CAST_BLOCK);
440 return (1);
441 }
442
443 static int
444 ssh_cast5_ctr_cleanup(EVP_CIPHER_CTX *ctx)
445 {
446 struct ssh_cast5_ctr_ctx *c;
447
448 if((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
449 SecureZeroMemory(c, sizeof(*c));
450 free(c);
451 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
452 }
453 return (1);
454 }
455
456 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER <= 0x3040300fL || LIBRESSL_VERSION_NUMBER >= 0x3070100fL
457 const EVP_CIPHER *
458 evp_cast5_ctr(void)
459 {
460 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x3070100fL
461 static EVP_CIPHER *p = NULL;
462
463 if (p == NULL) {
464 p = EVP_CIPHER_meth_new(NID_undef, /*block_size*/CAST_BLOCK, /*key_len*/16);
465 /*** TODO: OPENSSL1.1.1 ERROR CHECK(ticket#39335�����u�\��) ***/
466 }
467 if (p) {
468 EVP_CIPHER_meth_set_iv_length(p, CAST_BLOCK);
469 EVP_CIPHER_meth_set_init(p, ssh_cast5_ctr_init);
470 EVP_CIPHER_meth_set_cleanup(p, ssh_cast5_ctr_cleanup);
471 EVP_CIPHER_meth_set_do_cipher(p, ssh_cast5_ctr);
472 EVP_CIPHER_meth_set_flags(p, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV);
473 }
474 return (p);
475 #else
476 static EVP_CIPHER cast5_ctr;
477
478 memset(&cast5_ctr, 0, sizeof(EVP_CIPHER));
479 cast5_ctr.nid = NID_undef;
480 cast5_ctr.block_size = CAST_BLOCK;
481 cast5_ctr.iv_len = CAST_BLOCK;
482 cast5_ctr.key_len = 16;
483 cast5_ctr.init = ssh_cast5_ctr_init;
484 cast5_ctr.cleanup = ssh_cast5_ctr_cleanup;
485 cast5_ctr.do_cipher = ssh_cast5_ctr;
486 #ifndef SSH_OLD_EVP
487 cast5_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
488 #endif
489 return (&cast5_ctr);
490 #endif
491 }
492 #endif
493
494 //============================================================================
495 // Camellia
496 //============================================================================
497 static int
498 ssh_camellia_ctr(EVP_CIPHER_CTX *ctx, unsigned char *dest, const unsigned char *src, size_t len)
499 {
500 struct ssh_camellia_ctr_ctx *c;
501 unsigned int n = 0;
502 unsigned char buf[CAMELLIA_BLOCK_SIZE];
503
504 if (len == 0)
505 return (1);
506 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
507 return (0);
508
509 while ((len--) > 0) {
510 if (n == 0) {
511 Camellia_encrypt(c->camellia_counter, buf, &c->camellia_ctx);
512 ssh_ctr_inc(c->camellia_counter, CAMELLIA_BLOCK_SIZE);
513 }
514 *(dest++) = *(src++) ^ buf[n];
515 n = (n + 1) % CAMELLIA_BLOCK_SIZE;
516 }
517 return (1);
518 }
519
520 static int
521 ssh_camellia_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc)
522 {
523 struct ssh_camellia_ctr_ctx *c;
524
525 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
526 c = malloc(sizeof(*c));
527 EVP_CIPHER_CTX_set_app_data(ctx, c);
528 }
529 if (key != NULL)
530 Camellia_set_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, &c->camellia_ctx);
531 if (iv != NULL)
532 memcpy(c->camellia_counter, iv, CAMELLIA_BLOCK_SIZE);
533 return (1);
534 }
535
536 static int
537 ssh_camellia_ctr_cleanup(EVP_CIPHER_CTX *ctx)
538 {
539 struct ssh_camellia_ctr_ctx *c;
540
541 if((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
542 SecureZeroMemory(c, sizeof(*c));
543 free(c);
544 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
545 }
546 return (1);
547 }
548
549 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER <= 0x3040300fL || LIBRESSL_VERSION_NUMBER >= 0x3070100fL
550 const EVP_CIPHER *
551 evp_camellia_128_ctr(void)
552 {
553 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x3070100fL
554 static EVP_CIPHER *p = NULL;
555
556 if (p == NULL) {
557 p = EVP_CIPHER_meth_new(NID_undef, /*block_size*/CAMELLIA_BLOCK_SIZE, /*key_len*/16);
558 /*** TODO: OPENSSL1.1.1 ERROR CHECK(ticket#39335�����u�\��) ***/
559 }
560 if (p) {
561 EVP_CIPHER_meth_set_iv_length(p, CAMELLIA_BLOCK_SIZE);
562 EVP_CIPHER_meth_set_init(p, ssh_camellia_ctr_init);
563 EVP_CIPHER_meth_set_cleanup(p, ssh_camellia_ctr_cleanup);
564 EVP_CIPHER_meth_set_do_cipher(p, ssh_camellia_ctr);
565 EVP_CIPHER_meth_set_flags(p, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV);
566 }
567 return (p);
568 #else
569 static EVP_CIPHER camellia_ctr;
570
571 memset(&camellia_ctr, 0, sizeof(EVP_CIPHER));
572 camellia_ctr.nid = NID_undef;
573 camellia_ctr.block_size = CAMELLIA_BLOCK_SIZE;
574 camellia_ctr.iv_len = CAMELLIA_BLOCK_SIZE;
575 camellia_ctr.key_len = 16;
576 camellia_ctr.init = ssh_camellia_ctr_init;
577 camellia_ctr.cleanup = ssh_camellia_ctr_cleanup;
578 camellia_ctr.do_cipher = ssh_camellia_ctr;
579 #ifndef SSH_OLD_EVP
580 camellia_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
581 #endif
582 return (&camellia_ctr);
583 #endif
584 }
585 #endif

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26