/[mcrypt]/libmcrypt-nm/lib/mcrypt.c
ViewVC logotype

Contents of /libmcrypt-nm/lib/mcrypt.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Mon May 22 13:09:08 2000 UTC (23 years, 11 months ago) by nmav
Branch: MAIN, mcrypt
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
File MIME type: text/plain

1 /*
2 * Copyright (C) 1998,1999 Nikos Mavroyanopoulos
3 *
4 * This library is free
5 * software; you can redistribute it and/or modify it under the terms of the
6 * GNU Library General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any
8 * later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21 /* $Id: mcrypt.c,v 1.8 1999/10/20 09:13:41 nmav Exp $ */
22
23 #ifndef LIBDEFS_H
24 #define LIBDEFS_H
25 #include <libdefs.h>
26 #endif
27 #include <bzero.h>
28 #include <swap.h>
29
30 #define MCRYPT_STRUCT_DEFINED
31 #include <mcrypt_int.h>
32 #include <blowfish.h>
33 #include <3-way.h>
34 #include <tripledes.h>
35 #include <gost.h>
36 #include <des.h>
37 #include <safer.h>
38 #include <cast-128.h>
39 #include <cast-256.h>
40 #include <tean.h>
41 #include <twofish.h>
42 #include <xmemory.h>
43 #include <rc2.h>
44 #include <enigma.h>
45 #include <saferplus.h>
46 #include <loki97.h>
47 #include <rijndael.h>
48 #include <serpent.h>
49 #include <arcfour.h>
50
51 MCRYPT init_mcrypt(const int mode, const int algorithm, void *key,
52 int lenofkey)
53 {
54 MCRYPT thread;
55
56 if (lenofkey == 0)
57 return MCRYPT_FAILED; /* error */
58
59 thread = malloc(sizeof(struct MCRYPT_INT));
60
61 if (mcrypt_get_key_size(algorithm) < lenofkey)
62 lenofkey = mcrypt_get_key_size(algorithm);
63
64 thread->algorithm_given = algorithm;
65 thread->mode_given = mode;
66 thread->keyword_given =
67 mxcalloc(1, mcrypt_get_key_size(algorithm));
68
69 memmove(thread->keyword_given, key, lenofkey);
70
71
72 switch (algorithm) {
73
74 case MCRYPT_DES:
75 thread->akey = mxcalloc(1, sizeof(DES_KEY));
76 _mcrypt_des_set_key((void *) thread->akey,
77 (void *) thread->keyword_given, lenofkey);
78 return thread;
79
80 case MCRYPT_ENIGMA:
81 thread->akey = mxcalloc(1, sizeof(CRYPT_KEY));
82 _mcrypt_enigma_set_key((void *) thread->akey,
83 (void *) thread->keyword_given, lenofkey);
84 return thread;
85
86 case MCRYPT_SAFER_SK64:
87 thread->akey =
88 mxcalloc(1, 1 +
89 SAFER_BLOCK_LEN * (1 +
90 2 * SAFER_MAX_NOF_ROUNDS));
91
92 _mcrypt_Safer_Init_Module();
93 _mcrypt_Safer_Expand_Userkey((void *)
94 thread->keyword_given,
95 (void *)
96 thread->keyword_given,
97 SAFER_SK64_DEFAULT_NOF_ROUNDS,
98 1, thread->akey);
99 /* 8 rounds and secure key schedule */
100 return thread;
101
102 case MCRYPT_CAST_128:
103 thread->akey = mxcalloc(1, sizeof(struct cast_key));
104 _mcrypt_cast_setkey((void *) thread->akey,
105 (void *) thread->keyword_given,
106 lenofkey);
107 return thread;
108
109 case MCRYPT_LOKI97:
110 thread->akey = mxcalloc(1, 96 * sizeof(word32));
111 _mcrypt_loki97_set_key((void *) thread->akey,
112 (void *) thread->keyword_given,
113 lenofkey);
114 return thread;
115
116 case MCRYPT_CAST_256:
117 thread->akey = mxcalloc(1, 96 * sizeof(word32));
118 _mcrypt_cast256_set_key((void *) thread->akey,
119 (void *) thread->keyword_given,
120 lenofkey);
121 return thread;
122
123
124 case MCRYPT_RIJNDAEL_128:
125 thread->akey = mxcalloc(1, sizeof(RI));
126 _mcrypt_rijndael_gentables();
127 _mcrypt_rijndael_set_key((void *) thread->akey, 4,
128 lenofkey / 4,
129 (void *) thread->keyword_given);
130 return thread;
131
132 case MCRYPT_SERPENT:
133 thread->akey = mxcalloc(1, sizeof(SERPENT_KEY));
134 _mcrypt_serpent_set_key((void *) thread->akey,
135 (void *) thread->keyword_given,
136 lenofkey);
137 return thread;
138
139 case MCRYPT_RC2:
140 thread->akey = mxcalloc(1, 64 * sizeof(word16));
141 _mcrypt_rc2_keyschedule((void *) thread->akey,
142 (void *) thread->keyword_given,
143 lenofkey);
144 return thread;
145
146 case MCRYPT_ARCFOUR:
147 thread->akey = mxcalloc(1, sizeof(arcfour_key));
148 _mcrypt_arcfour_prepare_key((void *) thread->keyword_given,
149 lenofkey,
150 (void *) thread->akey);
151 return thread;
152
153 case MCRYPT_SAFER_SK128:
154 _mcrypt_Safer_Init_Module();
155 thread->akey =
156 mxcalloc(1, 1 +
157 SAFER_BLOCK_LEN * (1 +
158 2 * SAFER_MAX_NOF_ROUNDS));
159
160 _mcrypt_Safer_Expand_Userkey((void *)
161 thread->keyword_given,
162 (void *)
163 &(thread->keyword_given)[8],
164 SAFER_SK128_DEFAULT_NOF_ROUNDS,
165 1, thread->akey);
166
167 /* 8 rounds and secure key schedule */
168
169 return thread;
170
171 case MCRYPT_TRIPLEDES:
172 thread->akey = mxcalloc(1, sizeof(TRIPLEDES_KEY));
173 _mcrypt_3des_set_key((void *) thread->akey,
174 (void *) thread->keyword_given, mcrypt_get_key_size(algorithm));
175
176 return thread;
177
178 case MCRYPT_SAFERPLUS:
179 thread->akey = mxcalloc(1, sizeof(SPI));
180 _mcrypt_saferplus_set_key((void *) thread->akey,
181 (void *) thread->keyword_given,
182 lenofkey);
183 return thread;
184
185 case MCRYPT_BLOWFISH:
186 thread->akey = mxcalloc(1, sizeof(blf_ctx));
187 if (lenofkey < 2) {
188 _mcrypt_blf_key((void *) thread->akey,
189 (void *) thread->keyword_given, 2);
190 } else {
191 _mcrypt_blf_key((void *) thread->akey,
192 (void *) thread->keyword_given,
193 lenofkey);
194 }
195 return thread;
196
197 case MCRYPT_TWOFISH:
198 thread->akey = mxcalloc(1, sizeof(TWI));
199 _mcrypt_twofish_set_key((void *) thread->akey,
200 (void *) thread->keyword_given,
201 lenofkey);
202 return thread;
203
204 case MCRYPT_GOST:
205 thread->akey = mxcalloc(1, 8*sizeof(word32));
206 _mcrypt_gost_set_key((void *) thread->akey, (void *) thread->keyword_given, lenofkey);
207
208 return thread;
209
210 case MCRYPT_THREEWAY:
211 case MCRYPT_XTEA:
212 return thread;
213
214 default:
215 return MCRYPT_FAILED;
216 }
217
218 }
219
220
221 /* plaintext should be in block's size */
222 int mcrypt(MCRYPT thread, void *plaintext)
223 {
224
225 switch (thread->algorithm_given) {
226
227 case MCRYPT_SAFER_SK64:
228 case MCRYPT_SAFER_SK128:
229 _mcrypt_Safer_Encrypt_Block(plaintext, thread->akey);
230 return 0;
231
232 case MCRYPT_SAFERPLUS:
233 _mcrypt_saferplus_encrypt((void *) thread->akey,
234 plaintext);
235 return 0;
236
237 case MCRYPT_CAST_128:
238 _mcrypt_cast_encrypt((void *) thread->akey,
239 (void *) plaintext);
240 return 0;
241
242 case MCRYPT_LOKI97:
243 _mcrypt_loki97_encrypt((void *) thread->akey,
244 (void *) plaintext);
245 return 0;
246
247 case MCRYPT_CAST_256:
248 _mcrypt_cast256_encrypt((void *) thread->akey,
249 (void *) plaintext);
250 return 0;
251
252 case MCRYPT_RC2:
253 _mcrypt_rc2_encrypt((void *) thread->akey,
254 (void *) plaintext);
255 return 0;
256
257 case MCRYPT_RIJNDAEL_128:
258 _mcrypt_rijndael_encrypt((void *) thread->akey,
259 (void *) plaintext);
260 return 0;
261
262 case MCRYPT_SERPENT:
263 _mcrypt_serpent_encrypt((void *) thread->akey,
264 (void *) plaintext);
265 return 0;
266
267 case MCRYPT_DES:
268 _mcrypt_des_encrypt((void *) thread->akey, plaintext);
269 return 0;
270
271 case MCRYPT_TRIPLEDES:
272 _mcrypt_3des_encrypt((void *) thread->akey, plaintext);
273 return 0;
274
275 case MCRYPT_BLOWFISH:
276 _mcrypt_enblf((void *) thread->akey, plaintext);
277 return 0;
278
279 case MCRYPT_TWOFISH:
280 _mcrypt_twofish_encrypt((void *) thread->akey, plaintext);
281 return 0;
282
283 case MCRYPT_GOST:
284 _mcrypt_gost_encrypt((void *) thread->akey, plaintext);
285 return 0;
286
287 case MCRYPT_XTEA:
288 _mcrypt_cl_enc_block((void *) thread->keyword_given,
289 plaintext);
290 return 0;
291
292 case MCRYPT_THREEWAY:
293 _mcrypt_en3way(plaintext, (void *) thread->keyword_given);
294 return 0;
295
296 default:
297 return 0;
298 }
299
300 }
301
302
303 /* plaintext should be in block's size */
304 int mdecrypt(MCRYPT thread, void *plaintext)
305 {
306 switch (thread->algorithm_given) {
307
308 case MCRYPT_SAFER_SK64:
309 case MCRYPT_SAFER_SK128:
310 _mcrypt_Safer_Decrypt_Block(plaintext,
311 (void *) thread->akey);
312 return 0;
313
314 case MCRYPT_SAFERPLUS:
315 _mcrypt_saferplus_decrypt((void *) thread->akey,
316 plaintext);
317 return 0;
318
319 case MCRYPT_CAST_128:
320 _mcrypt_cast_decrypt((void *) thread->akey,
321 (void *) plaintext);
322 return 0;
323
324 case MCRYPT_LOKI97:
325 _mcrypt_loki97_decrypt((void *) thread->akey,
326 (void *) plaintext);
327 return 0;
328
329 case MCRYPT_CAST_256:
330 _mcrypt_cast256_decrypt((void *) thread->akey,
331 (void *) plaintext);
332 return 0;
333
334 case MCRYPT_RC2:
335 _mcrypt_rc2_decrypt((void *) thread->akey,
336 (void *) plaintext);
337 return 0;
338
339 case MCRYPT_RIJNDAEL_128:
340 _mcrypt_rijndael_decrypt((void *) thread->akey,
341 (void *) plaintext);
342 return 0;
343
344 case MCRYPT_SERPENT:
345 _mcrypt_serpent_decrypt((void *) thread->akey,
346 (void *) plaintext);
347 return 0;
348
349 case MCRYPT_DES:
350 _mcrypt_des_decrypt( (void*) thread->akey, plaintext);
351 return 0;
352
353 case MCRYPT_XTEA:
354 _mcrypt_cl_dec_block((void *) thread->keyword_given,
355 plaintext);
356 return 0;
357
358 case MCRYPT_TRIPLEDES:
359 _mcrypt_3des_decrypt((void *) thread->akey, plaintext);
360 return 0;
361
362 case MCRYPT_TWOFISH:
363 _mcrypt_twofish_decrypt((void *) thread->akey, plaintext);
364 return 0;
365
366 case MCRYPT_BLOWFISH:
367 _mcrypt_deblf((void *) thread->akey, plaintext);
368 return 0;
369
370 case MCRYPT_GOST:
371 _mcrypt_gost_decrypt( (void *) thread->akey, plaintext);
372 return 0;
373
374 case MCRYPT_THREEWAY:
375 _mcrypt_de3way(plaintext, (void *) thread->keyword_given);
376 return 0;
377
378 default:
379 return 0;
380 }
381 }
382
383
384 int end_mcrypt(MCRYPT td)
385 {
386
387 if (td==MCRYPT_FAILED) return -1;
388
389 mxfree(td->keyword_given,
390 mcrypt_get_key_size(td->algorithm_given));
391
392 switch (td->algorithm_given) {
393
394 case MCRYPT_RIJNDAEL_128:
395 mxfree(td->akey, sizeof(RI));
396 break;
397
398 case MCRYPT_SERPENT:
399 mxfree(td->akey, sizeof(SERPENT_KEY));
400 break;
401
402
403 case MCRYPT_DES:
404 mxfree(td->akey, sizeof(DES_KEY));
405 break;
406
407 case MCRYPT_TRIPLEDES:
408 mxfree(td->akey, sizeof(TRIPLEDES_KEY));
409 break;
410
411 case MCRYPT_RC2:
412 mxfree(td->akey, 64 * sizeof(word16));
413 break;
414
415 case MCRYPT_ARCFOUR:
416 mxfree(td->akey, sizeof(arcfour_key));
417 break;
418
419 case MCRYPT_ENIGMA:
420 mxfree(td->akey, sizeof(CRYPT_KEY));
421 break;
422
423 case MCRYPT_TWOFISH:
424 mxfree(td->akey, sizeof(TWI));
425 break;
426
427 case MCRYPT_BLOWFISH:
428 mxfree(td->akey, sizeof(blf_ctx));
429 break;
430
431 case MCRYPT_GOST:
432 mxfree(td->akey, 8*sizeof(word32));
433 break;
434
435 case MCRYPT_XTEA:
436 case MCRYPT_THREEWAY:
437 break;
438
439 case MCRYPT_CAST_128:
440 mxfree(td->akey, sizeof(struct cast_key));
441 break;
442
443 case MCRYPT_LOKI97:
444 mxfree(td->akey, 96 * sizeof(word32));
445 break;
446
447 case MCRYPT_CAST_256:
448 mxfree(td->akey, 96 * sizeof(word32));
449 break;
450
451 case MCRYPT_SAFER_SK64:
452 case MCRYPT_SAFER_SK128:
453 mxfree(td->akey,
454 1 + SAFER_BLOCK_LEN * (1 +
455 2 * SAFER_MAX_NOF_ROUNDS));
456 break;
457
458 default:
459 return 0;
460 }
461
462 free(td);
463 return 0;
464 }
465
466 /* ECB MODE */
467
468 MCRYPT init_mcrypt_ecb(const int algorithm, void *key, int lenofkey)
469 {
470 MCRYPT x;
471
472 x = init_mcrypt(MCRYPT_ECB, algorithm, key, lenofkey);
473
474 return x;
475
476 }
477
478
479
480
481 int mcrypt_ecb(MCRYPT td, void *plaintext, int len)
482 {
483 int j, blocksize;
484 char *plain = plaintext;
485
486 blocksize = mcrypt_get_block_size(td->algorithm_given);
487 for (j = 0; j < len / blocksize; j++) {
488
489 mcrypt(td, &plain[j * blocksize]);
490
491 }
492 return 0;
493 }
494
495
496
497 int mdecrypt_ecb(MCRYPT td, void *ciphertext, int len)
498 {
499 int j, blocksize = mcrypt_get_block_size(td->algorithm_given);
500 char *cipher = ciphertext;
501
502 for (j = 0; j < len / blocksize; j++) {
503
504 mdecrypt(td, &cipher[j * blocksize]);
505
506 }
507
508 return 0;
509 }
510
511
512
513 /* STREAM MODE */
514 MCRYPT init_mcrypt_stream(const int algorithm, void *key, int lenofkey)
515 {
516 MCRYPT x;
517
518 x = init_mcrypt(MCRYPT_STREAM, algorithm, key, lenofkey);
519
520 return x;
521
522 }
523
524
525
526
527 int mcrypt_stream(MCRYPT td, void *plaintext, int len)
528 {
529 switch (td->algorithm_given) {
530
531
532 case MCRYPT_ENIGMA:
533 _mcrypt_enigma_encrypt((void *) td->akey, plaintext, len);
534 return 0;
535
536 case MCRYPT_ARCFOUR:
537 _mcrypt_arcfour((void *) plaintext, len,
538 (void *) td->akey);
539 return 0;
540 default:
541 return -1;
542 }
543
544 }
545
546
547
548 int mdecrypt_stream(MCRYPT td, void *ciphertext, int len)
549 {
550 switch (td->algorithm_given) {
551
552
553 case MCRYPT_ENIGMA:
554 _mcrypt_enigma_decrypt((void *) td->akey, ciphertext, len);
555 return 0;
556
557 case MCRYPT_ARCFOUR:
558 _mcrypt_arcfour((void *) ciphertext, len,
559 (void *) td->akey);
560 return 0;
561 default:
562 return -1;
563 }
564 }
565
566
567
568
569 /* CBC MODE */
570
571 int end_mcrypt_cbc(MCRYPT td)
572 {
573
574 mxfree(td->previous_ciphertext,
575 mcrypt_get_block_size(td->algorithm_given));
576 mxfree(td->previous_cipher,
577 mcrypt_get_block_size(td->algorithm_given));
578 mxfree(td->previous_plaintext,
579 mcrypt_get_block_size(td->algorithm_given));
580 return end_mcrypt(td);
581
582 }
583
584
585 MCRYPT init_mcrypt_cbc(const int algorithm, void *key, int lenofkey)
586 {
587
588 MCRYPT x;
589
590 x = init_mcrypt(MCRYPT_CBC, algorithm, key, lenofkey);
591
592 /* For cbc */
593 if (x != MCRYPT_FAILED) {
594 x->previous_ciphertext =
595 mxcalloc(1, mcrypt_get_block_size(algorithm));
596 x->previous_cipher =
597 mxmalloc(mcrypt_get_block_size(algorithm));
598 x->previous_plaintext =
599 mxcalloc(1, mcrypt_get_block_size(algorithm));
600 }
601 /* End cbc */
602
603 return x;
604
605 }
606
607 MCRYPT init_mcrypt_cbc_iv(const int algorithm, void *key, int lenofkey,
608 void *IV)
609 {
610
611 MCRYPT x;
612
613 x = init_mcrypt(MCRYPT_CBC, algorithm, key, lenofkey);
614
615 if (x != MCRYPT_FAILED) {
616 x->previous_ciphertext =
617 mxcalloc(1, mcrypt_get_block_size(algorithm));
618 x->previous_cipher =
619 mxmalloc(mcrypt_get_block_size(algorithm));
620 x->previous_plaintext =
621 mxcalloc(1, mcrypt_get_block_size(algorithm));
622
623 memmove(x->previous_ciphertext, IV,
624 mcrypt_get_block_size(algorithm));
625 memmove(x->previous_plaintext, IV,
626 mcrypt_get_block_size(algorithm));
627 /* End cbc */
628 }
629
630 return x;
631
632 }
633
634
635 int mcrypt_cbc(MCRYPT td, void *plaintext, int len)
636 {
637 word32 *fplain = plaintext;
638 word32 *plain;
639 int i, j, blocksize = mcrypt_get_block_size(td->algorithm_given);
640
641 for (j = 0; j < len / blocksize; j++) {
642
643 plain = &fplain[j * blocksize / sizeof(word32)];
644
645 for (i = 0; i < blocksize / sizeof(word32); i++) {
646 plain[i] ^= td->previous_ciphertext[i];
647 }
648
649 mcrypt(td, plain);
650
651
652 /* Copy the ciphertext to prev_ciphertext */
653 memmove(td->previous_ciphertext, plain, blocksize);
654 }
655
656 return 0;
657 }
658
659
660
661 int mdecrypt_cbc(MCRYPT td, void *ciphertext, int len)
662 {
663 word32 *cipher;
664 word32 *fcipher = ciphertext;
665 int i, j, blocksize = mcrypt_get_block_size(td->algorithm_given);
666
667 for (j = 0; j < len / blocksize; j++) {
668
669 cipher = &fcipher[j * blocksize / sizeof(word32)];
670 memmove(td->previous_cipher, cipher, blocksize);
671
672 mdecrypt(td, cipher);
673 for (i = 0; i < blocksize / sizeof(word32); i++) {
674 cipher[i] ^= td->previous_plaintext[i];
675 }
676
677 /* Copy the ciphertext to prev_cipher */
678 memmove(td->previous_plaintext, td->previous_cipher,
679 blocksize);
680
681 }
682
683 return 0;
684 }
685
686
687
688 /* CFB MODE */
689
690 int end_mcrypt_cfb(MCRYPT td)
691 {
692
693 mxfree(td->s_register, mcrypt_get_block_size(td->algorithm_given));
694 mxfree(td->enc_s_register,
695 mcrypt_get_block_size(td->algorithm_given));
696 mxfree(td->enc_sd_register,
697 mcrypt_get_block_size(td->algorithm_given));
698 mxfree(td->sd_register,
699 mcrypt_get_block_size(td->algorithm_given));
700
701 return end_mcrypt(td);
702
703 }
704
705
706 MCRYPT init_mcrypt_cfb(const int algorithm, void *key, int lenofkey,
707 void *IV)
708 {
709 MCRYPT x;
710
711 x = init_mcrypt(MCRYPT_CFB, algorithm, key, lenofkey);
712
713 if (x != MCRYPT_FAILED) {
714 /* For cfb */
715 x->s_register = mxmalloc(mcrypt_get_block_size(algorithm));
716 x->sd_register =
717 mxmalloc(mcrypt_get_block_size(algorithm));
718 x->enc_s_register =
719 mxmalloc(mcrypt_get_block_size(algorithm));
720 x->enc_sd_register =
721 mxmalloc(mcrypt_get_block_size(algorithm));
722 memmove(x->sd_register, IV,
723 mcrypt_get_block_size(algorithm));
724 memmove(x->s_register, IV,
725 mcrypt_get_block_size(algorithm));
726
727 /* End cfb */
728 }
729
730 return x;
731 }
732
733 int mcrypt_cfb(MCRYPT td, void *plaintext, int len)
734 { /* plaintext is 1 byte (8bit cfb) */
735 char *plain = plaintext;
736 int i, j, blocksize = mcrypt_get_block_size(td->algorithm_given);;
737
738 for (j = 0; j < len; j++) {
739
740 memmove(td->enc_s_register, td->s_register, blocksize);
741
742 mcrypt(td, td->enc_s_register);
743
744 plain[j] ^= td->enc_s_register[0];
745
746 /* Shift the register */
747 for (i = 0; i < (blocksize - 1); i++)
748 td->s_register[i] = td->s_register[i + 1];
749
750 td->s_register[blocksize - 1] = plain[j];
751
752
753 }
754
755 return 0;
756
757 }
758
759
760 int mdecrypt_cfb(MCRYPT td, void *plaintext, int len)
761 { /* plaintext is 1 byte (8bit cfb) */
762 char *plain = plaintext;
763 int i, j, blocksize = mcrypt_get_block_size(td->algorithm_given);;
764
765 for (j = 0; j < len; j++) {
766
767 memmove(td->enc_sd_register, td->sd_register, blocksize);
768
769 mcrypt(td, td->enc_sd_register);
770
771 /* Shift the register */
772 for (i = 0; i < (blocksize - 1); i++)
773 td->sd_register[i] = td->sd_register[i + 1];
774
775 td->sd_register[blocksize - 1] = plain[j];
776
777 plain[j] ^= td->enc_sd_register[0];
778
779 }
780
781 return 0;
782 }
783
784
785 /* OFB MODE */
786
787 int end_mcrypt_ofb(MCRYPT td)
788 {
789
790 mxfree(td->s_register, mcrypt_get_block_size(td->algorithm_given));
791 mxfree(td->enc_s_register,
792 mcrypt_get_block_size(td->algorithm_given));
793 mxfree(td->enc_sd_register,
794 mcrypt_get_block_size(td->algorithm_given));
795 mxfree(td->sd_register,
796 mcrypt_get_block_size(td->algorithm_given));
797
798 return end_mcrypt(td);
799
800 }
801
802
803 MCRYPT init_mcrypt_ofb(const int algorithm, void *key, int lenofkey,
804 void *IV)
805 {
806 MCRYPT x;
807
808 x = init_mcrypt(MCRYPT_OFB, algorithm, key, lenofkey);
809
810
811 if (x != MCRYPT_FAILED) {
812 /* For ofb */
813 x->s_register = mxmalloc(mcrypt_get_block_size(algorithm));
814 x->sd_register =
815 mxmalloc(mcrypt_get_block_size(algorithm));
816 x->enc_s_register =
817 mxmalloc(mcrypt_get_block_size(algorithm));
818 x->enc_sd_register =
819 mxmalloc(mcrypt_get_block_size(algorithm));
820 memmove(x->sd_register, IV,
821 mcrypt_get_block_size(algorithm));
822 memmove(x->s_register, IV,
823 mcrypt_get_block_size(algorithm));
824 /* End ofb */
825 }
826
827
828 return x;
829 }
830
831 int mcrypt_ofb(MCRYPT td, void *plaintext, int len)
832 { /* plaintext is 1 byte (8bit cfb) */
833 char *plain = plaintext;
834 int i, j, blocksize = mcrypt_get_block_size(td->algorithm_given);;
835
836 for (j = 0; j < len; j++) {
837
838 memmove(td->enc_s_register, td->s_register, blocksize);
839
840 mcrypt(td, td->enc_s_register);
841
842 plain[j] ^= td->enc_s_register[0];
843
844 /* Shift the register */
845 for (i = 0; i < (blocksize - 1); i++)
846 td->s_register[i] = td->s_register[i + 1];
847
848 td->s_register[blocksize - 1] = td->enc_s_register[0];
849
850
851 }
852
853 return 0;
854
855 }
856
857
858 int mdecrypt_ofb(MCRYPT td, void *plaintext, int len)
859 { /* plaintext is 1 byte (8bit ofb) */
860 char *plain = plaintext;
861 int i, j, blocksize = mcrypt_get_block_size(td->algorithm_given);;
862
863 for (j = 0; j < len; j++) {
864
865 memmove(td->enc_sd_register, td->sd_register, blocksize);
866
867 mcrypt(td, td->enc_sd_register);
868
869 /* Shift the register */
870 for (i = 0; i < (blocksize - 1); i++)
871 td->sd_register[i] = td->sd_register[i + 1];
872
873 td->sd_register[blocksize - 1] = td->enc_sd_register[0];
874
875 plain[j] ^= td->enc_sd_register[0];
876
877 }
878
879 return 0;
880 }
881
882
883
884 /* n-bit ofb */
885
886 int end_mcrypt_nofb(MCRYPT td)
887 {
888
889 mxfree(td->s_register, mcrypt_get_block_size(td->algorithm_given));
890 mxfree(td->enc_s_register,
891 mcrypt_get_block_size(td->algorithm_given));
892 mxfree(td->enc_sd_register,
893 mcrypt_get_block_size(td->algorithm_given));
894 mxfree(td->sd_register,
895 mcrypt_get_block_size(td->algorithm_given));
896
897 return end_mcrypt(td);
898
899 }
900
901
902 MCRYPT init_mcrypt_nofb(const int algorithm, void *key, int lenofkey,
903 void *IV)
904 {
905 MCRYPT x;
906
907 x = init_mcrypt(MCRYPT_nOFB, algorithm, key, lenofkey);
908
909 if (x != MCRYPT_FAILED) {
910 /* For nofb */
911 x->s_register = mxmalloc(mcrypt_get_block_size(algorithm));
912 x->sd_register =
913 mxmalloc(mcrypt_get_block_size(algorithm));
914 x->enc_s_register =
915 mxmalloc(mcrypt_get_block_size(algorithm));
916 x->enc_sd_register =
917 mxmalloc(mcrypt_get_block_size(algorithm));
918 memmove(x->sd_register, IV,
919 mcrypt_get_block_size(algorithm));
920 memmove(x->s_register, IV,
921 mcrypt_get_block_size(algorithm));
922 /* End nofb */
923 }
924
925 return x;
926 }
927
928 int mcrypt_nofb(MCRYPT td, void *plaintext, int len)
929 { /* plaintext is n*blocksize bytes (nbit ofb) */
930 word32 *plain, *fplain = plaintext;
931 int i, j, blocksize = mcrypt_get_block_size(td->algorithm_given);
932
933 for (j = 0; j < len / blocksize; j++) {
934 plain = &fplain[j * blocksize / sizeof(word32)];
935 memmove(td->enc_s_register, td->s_register, blocksize);
936 mcrypt(td, td->enc_s_register);
937
938 for (i = 0; i < blocksize / sizeof(word32); i++) {
939 plain[i] ^=
940 ((word32 *) &
941 td->enc_s_register[0 +
942 i * sizeof(word32)])[0];
943 }
944 /* Put the new register */
945 memmove(td->s_register, td->enc_s_register, blocksize);
946 }
947 return 0;
948 }
949
950
951 int mdecrypt_nofb(MCRYPT td, void *plaintext, int len)
952 { /* plaintext is n bytes (nbit ofb) */
953 word32 *plain, *fplain = plaintext;
954 int i, j, blocksize = mcrypt_get_block_size(td->algorithm_given);
955
956 for (j = 0; j < len / blocksize; j++) {
957 plain = &fplain[j * blocksize / sizeof(word32)];
958 memmove(td->enc_sd_register, td->sd_register, blocksize);
959 mcrypt(td, td->enc_sd_register);
960
961 /* Shift the register */
962 memmove(td->sd_register, td->enc_sd_register, blocksize);
963
964 for (i = 0; i < blocksize / sizeof(word32); i++) {
965 plain[i] ^=
966 ((word32 *) &
967 td->enc_sd_register[0 +
968 i * sizeof(word32)])[0];
969 }
970 }
971
972 return 0;
973 }
974
975
976
977 /* Generic -High level functions */
978
979 MCRYPT mcrypt_generic_init(const int mode, const int algorithm, void *key,
980 int lenofkey, void *IV)
981 {
982 if (mcrypt_is_block_algorithm(algorithm) != 0) {
983 switch (mode) {
984 case MCRYPT_CBC:
985 if (IV == NULL) {
986 return init_mcrypt_cbc(algorithm, key,
987 lenofkey);
988 } else {
989 return init_mcrypt_cbc_iv(algorithm, key,
990 lenofkey, IV);
991 }
992
993 case MCRYPT_ECB:
994 return init_mcrypt_ecb(algorithm, key, lenofkey);
995 case MCRYPT_CFB:
996 return init_mcrypt_cfb(algorithm, key, lenofkey,
997 IV);
998 case MCRYPT_OFB:
999 return init_mcrypt_ofb(algorithm, key, lenofkey,
1000 IV);
1001 case MCRYPT_nOFB:
1002 return init_mcrypt_nofb(algorithm, key, lenofkey,
1003 IV);
1004 default:
1005 return MCRYPT_FAILED;
1006 }
1007 } else {
1008 if (mode == MCRYPT_STREAM) {
1009 return init_mcrypt_stream(algorithm, key,
1010 lenofkey);
1011 } else {
1012 return MCRYPT_FAILED;
1013 }
1014 }
1015
1016
1017 }
1018
1019 int mcrypt_generic(MCRYPT td, void *plaintext, int len)
1020 {
1021
1022 switch (td->mode_given) {
1023 case MCRYPT_CBC:
1024 return mcrypt_cbc(td, plaintext, len);
1025 case MCRYPT_ECB:
1026 return mcrypt_ecb(td, plaintext, len);
1027 case MCRYPT_CFB:
1028 return mcrypt_cfb(td, plaintext, len);
1029 case MCRYPT_OFB:
1030 return mcrypt_ofb(td, plaintext, len);
1031 case MCRYPT_nOFB:
1032 return mcrypt_nofb(td, plaintext, len);
1033 case MCRYPT_STREAM:
1034 return mcrypt_stream(td, plaintext, len);
1035
1036 default:
1037 return -1;
1038 }
1039
1040 }
1041
1042 int mdecrypt_generic(MCRYPT td, void *ciphertext, int len)
1043 {
1044
1045 switch (td->mode_given) {
1046 case MCRYPT_CBC:
1047 return mdecrypt_cbc(td, ciphertext, len);
1048 case MCRYPT_ECB:
1049 return mdecrypt_ecb(td, ciphertext, len);
1050 case MCRYPT_CFB:
1051 return mdecrypt_cfb(td, ciphertext, len);
1052 case MCRYPT_OFB:
1053 return mdecrypt_ofb(td, ciphertext, len);
1054 case MCRYPT_nOFB:
1055 return mdecrypt_nofb(td, ciphertext, len);
1056 case MCRYPT_STREAM:
1057 return mdecrypt_stream(td, ciphertext, len);
1058
1059 default:
1060 return -1;
1061 }
1062
1063 }
1064
1065 int mcrypt_generic_end(const MCRYPT td)
1066 {
1067
1068 switch (td->mode_given) {
1069 case MCRYPT_CBC:
1070 return end_mcrypt_cbc(td);
1071 case MCRYPT_ECB:
1072 return end_mcrypt_ecb(td);
1073 case MCRYPT_CFB:
1074 return end_mcrypt_cfb(td);
1075 case MCRYPT_OFB:
1076 return end_mcrypt_ofb(td);
1077 case MCRYPT_nOFB:
1078 return end_mcrypt_nofb(td);
1079 case MCRYPT_STREAM:
1080 return end_mcrypt_stream(td);
1081 default:
1082 return -1;
1083 }
1084 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26