MagickCore 7.0.10
cipher.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% CCCC IIIII PPPP H H EEEEE RRRR %
6% C I P P H H E R R %
7% C I PPPP HHHHH EEE RRRR %
8% C I P H H E R R %
9% CCCC IIIII P H H EEEEE R R %
10% %
11% %
12% Wizard's Toolkit Secure Cipher Algorithm Methods %
13% %
14% Software Design %
15% Cristy %
16% March 2003 %
17% %
18% %
19% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
20% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% https://imagemagick.org/script/license.php %
26% %
27% Unless required by applicable law or agreed to in writing, software %
28% distributed under the License is distributed on an "AS IS" BASIS, %
29% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30% See the License for the specific language governing permissions and %
31% limitations under the License. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35% See http://csrc.nist.gov/groups/ST/toolkit/index.html.
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "wizard/studio.h"
43#include "wizard/aes.h"
44#include "wizard/chacha.h"
45#include "wizard/cipher.h"
46#include "wizard/exception.h"
48#include "wizard/memory_.h"
49#include "wizard/random_.h"
50#include "wizard/serpent.h"
51#include "wizard/twofish.h"
52
53/*
54 Define declarations.
55*/
56#define CipherRandomHash SHA2256Hash
57
58/*
59 Typedef declarations.
60*/
61typedef void
62 (*DecipherBlock)(void *,const unsigned char *,const unsigned char *),
63 (*EncipherBlock)(void *,const unsigned char *,const unsigned char *);
64
66{
67 void
69
72
75
76 size_t
78
81
84
87
90
91 time_t
93
94 size_t
96};
97
98/*
99 Forward declaration.
100*/
101static StringInfo
108
109/*
110%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111% %
112% %
113% %
114% A c q u i r e C i p h e r I n f o %
115% %
116% %
117% %
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119%
120% AcquireCipherInfo() allocates the CipherInfo structure.
121%
122% The format of the AcquireCipherInfo method is:
123%
124% CipherInfo *AcquireCipherInfo(const CipherType cipher,
125% const CipherMode mode)
126%
127% A description of each parameter follows:
128%
129% o cipher: The cipher type.
130%
131% o mode: The cipher mode.
132%
133*/
135 const CipherMode mode)
136{
138 *cipher_info;
139
140 cipher_info=(CipherInfo *) AcquireWizardMemory(sizeof(*cipher_info));
141 if (cipher_info == (CipherInfo *) NULL)
143 (void) memset(cipher_info,0,sizeof(*cipher_info));
144 cipher_info->cipher=cipher;
145 switch (cipher_info->cipher)
146 {
147 case AESCipher:
148 {
149 AESInfo
150 *aes_info;
151
152 aes_info=AcquireAESInfo();
153 cipher_info->handle=(CipherInfo *) aes_info;
154 cipher_info->blocksize=GetAESBlocksize(aes_info);
157 break;
158 }
159 case ChachaCipher:
160 {
162 *chacha_info;
163
164 chacha_info=AcquireChachaInfo();
165 cipher_info->handle=(CipherInfo *) chacha_info;
166 cipher_info->blocksize=GetChachaBlocksize(chacha_info);
169 break;
170 }
171 case SerpentCipher:
172 {
174 *serpent_info;
175
176 serpent_info=AcquireSerpentInfo();
177 cipher_info->handle=(CipherInfo *) serpent_info;
178 cipher_info->blocksize=GetSerpentBlocksize(serpent_info);
181 break;
182 }
183 case TwofishCipher:
184 {
186 *twofish_info;
187
188 twofish_info=AcquireTwofishInfo();
189 cipher_info->handle=(CipherInfo *) twofish_info;
190 cipher_info->blocksize=GetTwofishBlocksize(twofish_info);
193 break;
194 }
195 default:
197 }
198 cipher_info->mode=mode;
199 if (cipher_info->nonce != (StringInfo *) NULL)
200 cipher_info->nonce=DestroyStringInfo(cipher_info->nonce);
202 cipher_info->timestamp=time((time_t *) NULL);
203 cipher_info->signature=WizardSignature;
204 cipher_info->nonce=GenerateCipherNonce(cipher_info);
205 return(cipher_info);
206}
207
208/*
209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210% %
211% %
212% %
213+ D e c i p h e r C B C M o d e %
214% %
215% %
216% %
217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218%
219% DecipherCBCMode() deciphers with the cipher in Cipher Block Chaining mode.
220% This mode is a confidentiality mode whose enciphering process features the
221% combining ("chaining") of the plaintext blocks with the previous ciphertext
222% blocks. The CBC mode requires an IV to combine with the first plaintext
223% block. The IV need not be secret, but it must be unpredictable.
224%
225% The format of the DecipherCBCMode method is:
226%
227% StringInfo *DecipherCBCMode(CipherInfo *cipher_info,
228% StringInfo *ciphertext)
229%
230% A description of each parameter follows:
231%
232% o cipher_info: The cipher context.
233%
234% o ciphertext: The cipher text.
235%
236*/
238 StringInfo *ciphertext)
239{
240 size_t
241 i;
242
243 unsigned char
244 *p,
245 *q;
246
247 size_t
248 blocksize;
249
251 *plaintext;
252
253 unsigned char
254 input_block[MaxCipherBlocksize],
255 output_block[MaxCipherBlocksize];
256
257 /*
258 Decipher in CBC mode.
259 */
261 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
263 blocksize=cipher_info->blocksize;
265 WizardAssert(CipherDomain,ciphertext != (StringInfo *) NULL);
266 plaintext=ciphertext;
267 p=GetStringInfoDatum(cipher_info->nonce);
268 for (i=0; i < blocksize; i++)
269 input_block[i]=p[i];
270 q=GetStringInfoDatum(ciphertext)+GetStringInfoLength(ciphertext);
271 for (p=GetStringInfoDatum(ciphertext); p < q; p+=blocksize)
272 {
273 for (i=0; i < blocksize; i++)
274 output_block[i]=p[i];
275 cipher_info->decipher_block(cipher_info->handle,p,p);
276 for (i=0; i < blocksize; i++)
277 p[i]^=input_block[i];
278 for (i=0; i < blocksize; i++)
279 input_block[i]=output_block[i];
280 }
281 /*
282 Reset registers.
283 */
284 (void) ResetWizardMemory(input_block,0,sizeof(input_block));
285 (void) ResetWizardMemory(output_block,0,sizeof(output_block));
286 return(plaintext);
287}
288
289/*
290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291% %
292% %
293% %
294+ D e c i p h e r C F B M o d e %
295% %
296% %
297% %
298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299%
300% DecipherCFBMode() deciphers with the cipher in Cipher Feedback mode. This
301% mode is a confidentiality mode that features the feedback of successive
302% ciphertext segments into the input blocks of the forward cipher to generate
303% output blocks that are exclusive-ORed with the plaintext to produce the
304% ciphertext, and vice versa. The CFB mode requires an IV as the initial
305% input block. The IV need not be secret, but it must be unpredictable.
306%
307% The format of the DecipherCFBMode method is:
308%
309% StringInfo *DecipherCFBMode(CipherInfo *cipher_info,
310% StringInfo *ciphertext)
311%
312% A description of each parameter follows:
313%
314% o cipher_info: The cipher context.
315%
316% o ciphertext: The cipher text.
317%
318*/
320 StringInfo *ciphertext)
321{
322 size_t
323 i;
324
325 unsigned char
326 *p,
327 *q;
328
329 size_t
330 blocksize;
331
333 *plaintext;
334
335 unsigned char
336 input_block[MaxCipherBlocksize],
337 output_block[MaxCipherBlocksize];
338
339 /*
340 Decipher in CFB mode.
341 */
343 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
345 blocksize=cipher_info->blocksize;
346 WizardAssert(CipherDomain,blocksize != 0);
348 WizardAssert(CipherDomain,ciphertext != (StringInfo *) NULL);
349 plaintext=ciphertext;
350 p=GetStringInfoDatum(cipher_info->nonce);
351 for (i=0; i < blocksize; i++)
352 input_block[i]=p[i];
353 q=GetStringInfoDatum(ciphertext)+GetStringInfoLength(ciphertext);
354 for (p=GetStringInfoDatum(ciphertext); p < q; p++)
355 {
356 for (i=0; i < blocksize; i++)
357 output_block[i]=input_block[i];
358 cipher_info->encipher_block(cipher_info->handle,output_block,output_block);
359 for (i=0; i < (blocksize-1); i++)
360 input_block[i]=input_block[i+1];
361 input_block[blocksize-1]=(*p);
362 *p^=(*output_block);
363 }
364 /*
365 Reset registers.
366 */
367 (void) ResetWizardMemory(input_block,0,sizeof(input_block));
368 (void) ResetWizardMemory(output_block,0,sizeof(output_block));
369 return(plaintext);
370}
371
372/*
373%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374% %
375% %
376% %
377% D e c i p h e r C i p h e r %
378% %
379% %
380% %
381%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
382%
383% DecipherCipher() deciphers ciphertext and returns plaintext. The deciphering
384% is performed in-place and DecipherCipher() returns a pointer to the
385% ciphertext string.
386%
387% The format of the DecipherCipher method is:
388%
389% StringInfo *DecipherCipher(CipherInfo *cipher_info,StringInfo *ciphertext)
390%
391% A description of each parameter follows:
392%
393% o cipher_info: The cipher context.
394%
395% o ciphertext: The cipher text.
396%
397*/
398
400 StringInfo *plaintext)
401{
402 return(DecipherCipher(cipher_info,plaintext));
403}
404
406 StringInfo *ciphertext)
407{
409 *plaintext;
410
412 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
414 WizardAssert(CipherDomain,ciphertext != (StringInfo *) NULL);
415 plaintext=(StringInfo *) NULL;
416 switch (cipher_info->mode)
417 {
418 case CBCMode:
419 {
420 plaintext=DecipherCBCMode(cipher_info,ciphertext);
421 break;
422 }
423 case CFBMode:
424 {
425 plaintext=DecipherCFBMode(cipher_info,ciphertext);
426 break;
427 }
428 case CTRMode:
429 {
430 plaintext=DecipherCTRMode(cipher_info,ciphertext);
431 break;
432 }
433 case ECBMode:
434 {
435 plaintext=DecipherECBMode(cipher_info,ciphertext);
436 break;
437 }
438 case OFBMode:
439 {
440 plaintext=DecipherOFBMode(cipher_info,ciphertext);
441 break;
442 }
443 default:
445 }
446 return(plaintext);
447}
448
449/*
450%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451% %
452% %
453% %
454+ D e c i p h e r C T R M o d e %
455% %
456% %
457% %
458%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459%
460% DecipherCTRMode() deciphers with the cipher in Counter mode. This mode is a
461% confidentiality mode that features the application of the forward cipher to
462% a set of input blocks, called counters, to produce a sequence of output
463% blocks that are exclusive-ORed with the plaintext to produce the ciphertext,
464% and vice versa. The sequence of counters must have the property that each
465% block in the sequence is different from every other block. This condition is
466% not restricted to a single message: across all of the messages that are
467% enciphered under the given key, all of the counters must be distinct.
468%
469% The format of the DecipherCTRMode method is:
470%
471% StringInfo *DecipherCTRMode(CipherInfo *cipher_info,
472% StringInfo *ciphertext)
473%
474% A description of each parameter follows:
475%
476% o cipher_info: The cipher context.
477%
478% o ciphertext: The cipher text.
479%
480*/
481
482static inline void IncrementCipherNonce(const size_t length,
483 unsigned char *nonce)
484{
485 ssize_t
486 i;
487
488 for (i=(ssize_t) (length-1); i >= 0; i--)
489 {
490 nonce[i]++;
491 if (nonce[i] != 0)
492 return;
493 }
494 ThrowFatalException(CipherFatalError,"Sequence wrap error `%s'");
495}
496
498 StringInfo *ciphertext)
499{
500 size_t
501 i;
502
503 unsigned char
504 *p,
505 *q;
506
507 size_t
508 blocksize;
509
511 *plaintext;
512
513 unsigned char
514 input_block[MaxCipherBlocksize],
515 output_block[MaxCipherBlocksize];
516
517 /*
518 Decipher in CTR mode.
519 */
521 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
523 blocksize=cipher_info->blocksize;
525 WizardAssert(CipherDomain,ciphertext != (StringInfo *) NULL);
526 plaintext=ciphertext;
527 p=GetStringInfoDatum(cipher_info->nonce);
528 for (i=0; i < blocksize; i++)
529 input_block[i]=p[i];
530 q=GetStringInfoDatum(ciphertext)+GetStringInfoLength(ciphertext);
531 for (p=GetStringInfoDatum(ciphertext); p < q; p+=blocksize)
532 {
533 for (i=0; i < blocksize; i++)
534 output_block[i]=input_block[i];
535 cipher_info->encipher_block(cipher_info->handle,output_block,output_block);
536 for (i=0; i < blocksize; i++)
537 p[i]^=output_block[i];
538 IncrementCipherNonce(blocksize,input_block);
539 }
540 /*
541 Reset registers.
542 */
543 (void) ResetWizardMemory(input_block,0,sizeof(input_block));
544 (void) ResetWizardMemory(output_block,0,sizeof(output_block));
545 return(plaintext);
546}
547
548/*
549%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550% %
551% %
552% %
553+ D e c i p h e r E C B M o d e %
554% %
555% %
556% %
557%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558%
559% DecipherECBMode() deciphers with the cipher in Electronic Codebook mode.
560% This mode is a confidentiality mode that features, for a given key, the
561% assignment of a fixed ciphertext block to each plaintext block, analogous to
562% the assignment of code words in a codebook.
563%
564% The format of the DecipherECBMode method is:
565%
566% StringInfo *DecipherECBMode(CipherInfo *cipher_info,
567% StringInfo *ciphertext)
568%
569% A description of each parameter follows:
570%
571% o cipher_info: The cipher context.
572%
573% o ciphertext: The cipher text.
574%
575*/
577 StringInfo *ciphertext)
578{
579 unsigned char
580 *p,
581 *q;
582
583 size_t
584 blocksize;
585
587 *plaintext;
588
589 /*
590 Decipher in ECB mode.
591 */
593 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
595 blocksize=cipher_info->blocksize;
597 WizardAssert(CipherDomain,ciphertext != (StringInfo *) NULL);
598 plaintext=ciphertext;
599 q=GetStringInfoDatum(ciphertext)+GetStringInfoLength(ciphertext);
600 for (p=GetStringInfoDatum(ciphertext); p < q; p+=blocksize)
601 cipher_info->decipher_block(cipher_info->handle,p,p);
602 return(plaintext);
603}
604
605/*
606%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607% %
608% %
609% %
610+ D e c i p h e r O F B M o d e %
611% %
612% %
613% %
614%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
615%
616% DecipherOFBMode() deciphers with the cipher in Output Feedback mode. This
617% mode is a confidentiality mode that features the iteration of the forward
618% cipher on a nonce to generate a sequence of output blocks that are
619% exclusive-ORed with the plaintext to produce the ciphertext, and vice versa.
620% The OFB mode requires that the IV is a nonce, i.e., the IV must be unique
621% for each execution of the mode under the given key.
622%
623% The format of the DecipherOFBMode method is:
624%
625% StringInfo *DecipherOFBMode(CipherInfo *cipher_info,
626% StringInfo *ciphertext)
627%
628% A description of each parameter follows:
629%
630% o cipher_info: The cipher context.
631%
632% o ciphertext: The cipher text.
633%
634*/
636 StringInfo *ciphertext)
637{
638 size_t
639 i;
640
641 unsigned char
642 *p,
643 *q;
644
645 size_t
646 blocksize;
647
649 *plaintext;
650
651 unsigned char
652 input_block[MaxCipherBlocksize];
653
654 /*
655 Decipher in OFB mode.
656 */
658 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
660 blocksize=cipher_info->blocksize;
662 WizardAssert(CipherDomain,ciphertext != (StringInfo *) NULL);
663 plaintext=ciphertext;
664 p=GetStringInfoDatum(cipher_info->nonce);
665 for (i=0; i < blocksize; i++)
666 input_block[i]=p[i];
667 q=GetStringInfoDatum(ciphertext)+GetStringInfoLength(ciphertext);
668 for (p=GetStringInfoDatum(ciphertext); p < q; p+=blocksize)
669 {
670 cipher_info->encipher_block(cipher_info->handle,input_block,input_block);
671 for (i=0; i < blocksize; i++)
672 p[i]^=input_block[i];
673 }
674 /*
675 Reset registers.
676 */
677 (void) ResetWizardMemory(input_block,0,sizeof(input_block));
678 return(plaintext);
679}
680
681/*
682%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683% %
684% %
685% %
686% D e s t r o y C i p h e r I n f o %
687% %
688% %
689% %
690%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
691%
692% DestroyCipherInfo() zeros memory associated with the CipherInfo structure.
693%
694% The format of the DestroyCipherInfo method is:
695%
696% CipherInfo *DestroyCipherInfo(CipherInfo *cipher_info)
697%
698% A description of each parameter follows:
699%
700% o cipher_info: The cipher info.
701%
702*/
704{
706 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
708 if (cipher_info->handle != (CipherInfo *) NULL)
709 switch (cipher_info->cipher)
710 {
711 case AESCipher:
712 {
713 cipher_info->handle=DestroyAESInfo((AESInfo *) cipher_info->handle);
714 break;
715 }
716 case ChachaCipher:
717 {
718 cipher_info->handle=DestroyChachaInfo((ChachaInfo *)
719 cipher_info->handle);
720 break;
721 }
722 case SerpentCipher:
723 {
724 cipher_info->handle=DestroySerpentInfo((SerpentInfo *)
725 cipher_info->handle);
726 break;
727 }
728 case TwofishCipher:
729 {
730 cipher_info->handle=DestroyTwofishInfo((TwofishInfo *)
731 cipher_info->handle);
732 break;
733 }
734 default:
736 }
737 if (cipher_info->nonce != (StringInfo *) NULL)
738 cipher_info->nonce=DestroyStringInfo(cipher_info->nonce);
739 if (cipher_info->random_info != (RandomInfo *) NULL)
740 cipher_info->random_info=DestroyRandomInfo(cipher_info->random_info);
741 cipher_info->signature=(~WizardSignature);
742 cipher_info=(CipherInfo *) RelinquishWizardMemory(cipher_info);
743 return(cipher_info);
744}
745
746/*
747%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
748% %
749% %
750% %
751+ E n c i p h e r C B C M o d e %
752% %
753% %
754% %
755%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756%
757% EncipherCBCMode() enciphers with the cipher in Cipher Block Chaining mode.
758% This mode is a confidentiality mode whose enciphering process features the
759% combining ("chaining") of the plaintext blocks with the previous ciphertext
760% blocks. The CBC mode requires an IV to combine with the first plaintext
761% block. The IV need not be secret, but it must be unpredictable.
762%
763% The format of the EncipherCBCMode method is:
764%
765% StringInfo *EncipherCBCMode(CipherInfo *cipher_info,
766% StringInfo *plaintext)
767%
768% A description of each parameter follows:
769%
770% o cipher_info: The cipher context.
771%
772% o plaintext: The plain text.
773%
774*/
776 StringInfo *plaintext)
777{
778 unsigned char
779 *p,
780 *q;
781
782 size_t
783 i;
784
785 size_t
786 blocksize,
787 pad;
788
790 *ciphertext;
791
792 unsigned char
793 input_block[MaxCipherBlocksize];
794
795 /*
796 Encipher in CBC mode.
797 */
799 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
801 blocksize=cipher_info->blocksize;
802 WizardAssert(CipherDomain,blocksize != 0);
804 WizardAssert(CipherDomain,plaintext != (StringInfo *) NULL);
805 ciphertext=plaintext;
806 p=GetStringInfoDatum(cipher_info->nonce);
807 for (i=0; i < blocksize; i++)
808 input_block[i]=p[i];
809 q=GetStringInfoDatum(plaintext)+GetStringInfoLength(plaintext);
810 pad=blocksize-GetStringInfoLength(plaintext) % blocksize;
811 SetRandomKey(cipher_info->random_info,pad-1,q);
812 q[pad-1]=(unsigned char) (pad-1);
813 if (pad == blocksize)
814 q+=blocksize;
815 for (p=GetStringInfoDatum(plaintext); p < q; p+=blocksize)
816 {
817 for (i=0; i < blocksize; i++)
818 p[i]^=input_block[i];
819 cipher_info->encipher_block(cipher_info->handle,p,p);
820 for (i=0; i < blocksize; i++)
821 input_block[i]=p[i];
822 }
823 /*
824 Reset registers.
825 */
826 (void) ResetWizardMemory(input_block,0,sizeof(input_block));
827 return(ciphertext);
828}
829
830/*
831%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
832% %
833% %
834% %
835+ E n c i p h e r C F B M o d e %
836% %
837% %
838% %
839%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
840%
841% EncipherCFBMode() enciphers with the cipher in Cipher Feedback mode. This
842% mode is a confidentiality mode that features the feedback of successive
843% ciphertext segments into the input blocks of the forward cipher to generate
844% output blocks that are exclusive-ORed with the plaintext to produce the
845% ciphertext, and vice versa. The CFB mode requires a nonce as the initial
846% input block. The nonce need not be secret, but it must be unpredictable.
847%
848% The format of the EncipherCFBMode method is:
849%
850% StringInfo *EncipherCFBMode(CipherInfo *cipher_info,
851% StringInfo *plaintext)
852%
853% A description of each parameter follows:
854%
855% o cipher_info: The cipher context.
856%
857% o plaintext: The plain text.
858%
859*/
861 StringInfo *plaintext)
862{
863 size_t
864 i;
865
866 unsigned char
867 *p,
868 *q;
869
870 size_t
871 blocksize;
872
874 *ciphertext;
875
876 unsigned char
877 input_block[MaxCipherBlocksize],
878 output_block[MaxCipherBlocksize];
879
880 /*
881 Encipher in CFB mode.
882 */
884 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
886 blocksize=cipher_info->blocksize;
887 WizardAssert(CipherDomain,blocksize != 0);
889 WizardAssert(CipherDomain,plaintext != (StringInfo *) NULL);
890 ciphertext=plaintext;
891 p=GetStringInfoDatum(cipher_info->nonce);
892 for (i=0; i < blocksize; i++)
893 input_block[i]=p[i];
894 q=GetStringInfoDatum(plaintext)+GetStringInfoLength(plaintext);
895 for (p=GetStringInfoDatum(plaintext); p < q; p++)
896 {
897 for (i=0; i < blocksize; i++)
898 output_block[i]=input_block[i];
899 cipher_info->encipher_block(cipher_info->handle,output_block,output_block);
900 *p^=(*output_block);
901 for (i=0; i < (blocksize-1); i++)
902 input_block[i]=input_block[i+1];
903 input_block[blocksize-1]=(*p);
904 }
905 /*
906 Reset registers.
907 */
908 (void) ResetWizardMemory(input_block,0,sizeof(input_block));
909 (void) ResetWizardMemory(output_block,0,sizeof(output_block));
910 return(ciphertext);
911}
912
913/*
914%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915% %
916% %
917% %
918% E n c i p h e r C i p h e r %
919% %
920% %
921% %
922%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
923%
924% EncipherCipher() enciphers plaintext and returns ciphertext. The
925% enciphering is performed in-place and EncipherCipher() returns a pointer to
926% the plaintext string.
927%
928% The format of the EncipherCipher method is:
929%
930% StringInfo *EncipherCipher(CipherInfo *cipher_info,StringInfo *plaintext)
931%
932% A description of each parameter follows:
933%
934% o cipher_info: The cipher context.
935%
936% o plaintext: The plain text.
937%
938*/
939
941 StringInfo *plaintext)
942{
943 return(EncipherCipher(cipher_info,plaintext));
944}
945
947 StringInfo *plaintext)
948{
950 *ciphertext;
951
953 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
955 WizardAssert(CipherDomain,plaintext != (StringInfo *) NULL);
956 ciphertext=(StringInfo *) NULL;
957 switch (cipher_info->mode)
958 {
959 case CBCMode:
960 {
961 ciphertext=EncipherCBCMode(cipher_info,plaintext);
962 break;
963 }
964 case CFBMode:
965 {
966 ciphertext=EncipherCFBMode(cipher_info,plaintext);
967 break;
968 }
969 case CTRMode:
970 {
971 ciphertext=EncipherCTRMode(cipher_info,plaintext);
972 break;
973 }
974 case ECBMode:
975 {
976 ciphertext=EncipherECBMode(cipher_info,plaintext);
977 break;
978 }
979 case OFBMode:
980 {
981 ciphertext=EncipherOFBMode(cipher_info,plaintext);
982 break;
983 }
984 default:
986 }
987 return(ciphertext);
988}
989
990/*
991%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992% %
993% %
994% %
995+ E n c i p h e r C T R M o d e %
996% %
997% %
998% %
999%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000%
1001% EncipherCTRMode() enciphers with the cipher in Counter mode. This mode is a
1002% confidentiality mode that features the application of the forward cipher to
1003% a set of input blocks, called counters, to produce a sequence of output
1004% blocks that are exclusive-ORed with the plaintext to produce the ciphertext,
1005% and vice versa. The sequence of counters must have the property that each
1006% block in the sequence is different from every other block. This condition is
1007% not restricted to a single message: across all of the messages that are
1008% enciphered under the given key, all of the counters must be distinct.
1009%
1010% The format of the EncipherCTRMode method is:
1011%
1012% StringInfo *EncipherCTRMode(CipherInfo *cipher_info,
1013% StringInfo *plaintext)
1014%
1015% A description of each parameter follows:
1016%
1017% o cipher_info: The cipher context.
1018%
1019% o plaintext: The plain text.
1020%
1021*/
1023 StringInfo *plaintext)
1024{
1025 size_t
1026 i;
1027
1028 unsigned char
1029 *p,
1030 *q;
1031
1032 size_t
1033 blocksize,
1034 pad;
1035
1037 *ciphertext;
1038
1039 unsigned char
1040 input_block[MaxCipherBlocksize],
1041 output_block[MaxCipherBlocksize];
1042
1043 /*
1044 Encipher in CTR mode.
1045 */
1047 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
1049 blocksize=cipher_info->blocksize;
1050 WizardAssert(CipherDomain,blocksize != 0);
1052 WizardAssert(CipherDomain,plaintext != (StringInfo *) NULL);
1053 ciphertext=plaintext;
1054 p=GetStringInfoDatum(cipher_info->nonce);
1055 for (i=0; i < blocksize; i++)
1056 input_block[i]=p[i];
1057 q=GetStringInfoDatum(plaintext)+GetStringInfoLength(plaintext);
1058 pad=blocksize-GetStringInfoLength(plaintext) % blocksize;
1059 SetRandomKey(cipher_info->random_info,pad-1,q);
1060 q[pad-1]=(unsigned char) (pad-1);
1061 if (pad == blocksize)
1062 q+=blocksize;
1063 for (p=GetStringInfoDatum(plaintext); p < q; p+=blocksize)
1064 {
1065 for (i=0; i < blocksize; i++)
1066 output_block[i]=input_block[i];
1067 cipher_info->encipher_block(cipher_info->handle,output_block,output_block);
1068 for (i=0; i < blocksize; i++)
1069 p[i]^=output_block[i];
1070 IncrementCipherNonce(blocksize,input_block);
1071 }
1072 /*
1073 Reset registers.
1074 */
1075 (void) ResetWizardMemory(input_block,0,sizeof(input_block));
1076 (void) ResetWizardMemory(output_block,0,sizeof(output_block));
1077 return(ciphertext);
1078}
1079
1080/*
1081%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082% %
1083% %
1084% %
1085+ E n c i p h e r E C B M o d e %
1086% %
1087% %
1088% %
1089%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1090%
1091% EncipherECBMode() enciphers with the cipher in Electronic Codebook mode.
1092% This mode is a confidentiality mode that features, for a given key, the
1093% assignment of a fixed ciphertext block to each plaintext block, analogous to
1094% the assignment of code words in a codebook.
1095%
1096% The format of the EncipherECBMode method is:
1097%
1098% StringInfo *EncipherECBMode(CipherInfo *cipher_info,
1099% StringInfo *plaintext)
1100%
1101% A description of each parameter follows:
1102%
1103% o cipher_info: The cipher context.
1104%
1105% o plaintext: The plain text.
1106%
1107*/
1109 StringInfo *plaintext)
1110{
1111 unsigned char
1112 *p,
1113 *q;
1114
1115 size_t
1116 blocksize,
1117 pad;
1118
1120 *ciphertext;
1121
1122 /*
1123 Encipher in ECB mode.
1124 */
1126 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
1128 blocksize=cipher_info->blocksize;
1130 WizardAssert(CipherDomain,plaintext != (StringInfo *) NULL);
1131 ciphertext=plaintext;
1132 q=GetStringInfoDatum(plaintext)+GetStringInfoLength(plaintext);
1133 pad=blocksize-GetStringInfoLength(plaintext) % blocksize;
1134 SetRandomKey(cipher_info->random_info,pad-1,q);
1135 q[pad-1]=(unsigned char) (pad-1);
1136 if (pad == blocksize)
1137 q+=blocksize;
1138 for (p=GetStringInfoDatum(plaintext); p < q; p+=blocksize)
1139 cipher_info->encipher_block(cipher_info->handle,p,p);
1140 return(ciphertext);
1141}
1142
1143/*
1144%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1145% %
1146% %
1147% %
1148+ E n c i p h e r O F B M o d e %
1149% %
1150% %
1151% %
1152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1153%
1154% EncipherOFBMode() enciphers with the cipher in Output Feedback mode. This
1155% mode is a confidentiality mode that features the iteration of the forward
1156% cipher on a nonce to generate a sequence of output blocks that are
1157% exclusive-ORed with the plaintext to produce the ciphertext, and vice versa.
1158% The OFB mode requires that the IV is a nonce, i.e., the IV must be unique
1159% for each execution of the mode under the given key.
1160%
1161% The format of the EncipherOFBMode method is:
1162%
1163% StringInfo *EncipherOFBMode(CipherInfo *cipher_info,
1164% StringInfo *plaintext)
1165%
1166% A description of each parameter follows:
1167%
1168% o cipher_info: The cipher context.
1169%
1170% o plaintext: The plain text.
1171%
1172*/
1174 StringInfo *plaintext)
1175{
1176 size_t
1177 i;
1178
1179 unsigned char
1180 *p,
1181 *q;
1182
1183 size_t
1184 blocksize,
1185 pad;
1186
1188 *ciphertext;
1189
1190 unsigned char
1191 input_block[MaxCipherBlocksize];
1192
1193 /*
1194 Encipher in OBC mode.
1195 */
1197 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
1199 blocksize=cipher_info->blocksize;
1200 WizardAssert(CipherDomain,blocksize != 0);
1202 WizardAssert(CipherDomain,plaintext != (StringInfo *) NULL);
1203 ciphertext=plaintext;
1204 p=GetStringInfoDatum(cipher_info->nonce);
1205 for (i=0; i < blocksize; i++)
1206 input_block[i]=p[i];
1207 q=GetStringInfoDatum(plaintext)+GetStringInfoLength(plaintext);
1208 pad=blocksize-GetStringInfoLength(plaintext) % blocksize;
1209 SetRandomKey(cipher_info->random_info,pad-1,q);
1210 q[pad-1]=(unsigned char) (pad-1);
1211 if (pad == blocksize)
1212 q+=blocksize;
1213 for (p=GetStringInfoDatum(plaintext); p < q; p+=blocksize)
1214 {
1215 cipher_info->encipher_block(cipher_info->handle,input_block,input_block);
1216 for (i=0; i < blocksize; i++)
1217 p[i]^=input_block[i];
1218 }
1219 /*
1220 Reset registers.
1221 */
1222 (void) ResetWizardMemory(input_block,0,sizeof(input_block));
1223 return(ciphertext);
1224}
1225
1226/*
1227%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1228% %
1229% %
1230% %
1231% G e t C i p h e r B l o c k s i z e %
1232% %
1233% %
1234% %
1235%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1236%
1237% GetCipherBlocksize() returns the cipher blocksize.
1238%
1239% The format of the GetCipherBlocksize method is:
1240%
1241% size_t GetCipherBlocksize(const CipherInfo *cipher_info)
1242%
1243% A description of each parameter follows:
1244%
1245% o cipher_info: The cipher info.
1246%
1247*/
1249{
1251 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
1253 return(cipher_info->blocksize);
1254}
1255
1256/*
1257%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1258% %
1259% %
1260% %
1261% G e n e r a t e C i p h e r N o n c e %
1262% %
1263% %
1264% %
1265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1266%
1267% GenerateCipherNonce() generate a nonce for the given cipher.
1268%
1269% The format of the GenerateCipherNonce method is:
1270%
1271% StringInfo *GenerateCipherNonce(CipherInfo *cipher_info)
1272%
1273% A description of each parameter follows:
1274%
1275% o cipher_info: The cipher context.
1276%
1277*/
1279{
1281 *nonce;
1282
1284 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
1286 switch (cipher_info->mode)
1287 {
1288 case CBCMode:
1289 case CFBMode:
1290 case ECBMode:
1291 case OFBMode:
1292 {
1293 nonce=GetRandomKey(cipher_info->random_info,cipher_info->blocksize);
1294 break;
1295 }
1296 case CTRMode:
1297 {
1298 nonce=AcquireStringInfo(cipher_info->blocksize);
1299 ResetStringInfo(nonce);
1300 SetRandomKey(cipher_info->random_info,(cipher_info->blocksize+1)/2,
1301 GetStringInfoDatum(nonce));
1302 break;
1303 }
1304 default:
1306 }
1307 return(nonce);
1308}
1309
1310/*
1311%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1312% %
1313% %
1314% %
1315% G e t C i p h e r N o n c e %
1316% %
1317% %
1318% %
1319%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1320%
1321% GetCipherNonce() returns a nonce of the cipher.
1322%
1323% The format of the GetCipherNonce method is:
1324%
1325% const StringInfo *GetCipherNonce(CipherInfo *cipher_info)
1326%
1327% A description of each parameter follows:
1328%
1329% o cipher_info: The cipher context.
1330%
1331*/
1333{
1335 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
1337 return(cipher_info->nonce);
1338}
1339
1340/*
1341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1342% %
1343% %
1344% %
1345% R e s e t C i p h e r N o n c e %
1346% %
1347% %
1348% %
1349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1350%
1351% ResetCipherNonce() resets the initialization vector for the cipher.
1352%
1353% The format of the ResetCipherNonce method is:
1354%
1355% ResetCipherNonce(CipherInfo *cipher_info)
1356%
1357% A description of each parameter follows:
1358%
1359% o cipher_info: The cipher context.
1360%
1361*/
1363{
1365 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
1367 ResetStringInfo(cipher_info->nonce);
1368}
1369
1370/*
1371%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1372% %
1373% %
1374% %
1375% S e t C i p h e r N o n c e %
1376% %
1377% %
1378% %
1379%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1380%
1381% SetCipherNonce() sets the initialization vector for the cipher.
1382%
1383% The format of the SetCipherNonce method is:
1384%
1385% SetCipherNonce(CipherInfo *cipher_info,const StringInfo *nonce)
1386%
1387% A description of each parameter follows:
1388%
1389% o cipher_info: The cipher context.
1390%
1391% o nonce: The initialization vector.
1392%
1393*/
1395 const StringInfo *nonce)
1396{
1398 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
1400 WizardAssert(CipherDomain,nonce != (StringInfo *) NULL);
1401 SetStringInfo(cipher_info->nonce,nonce);
1402}
1403
1404/*
1405%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1406% %
1407% %
1408% %
1409% S e t C i p h e r K e y %
1410% %
1411% %
1412% %
1413%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1414%
1415% SetCipherKey() sets the key for the cipher. The key length is specified
1416% in bits. Valid values are 128, 192, or 256.
1417%
1418% The format of the SetCipherKey method is:
1419%
1420% SetCipherKey(CipherInfo *cipher_info,const StringInfo *key)
1421%
1422% A description of each parameter follows:
1423%
1424% o cipher_info: The cipher context.
1425%
1426% o key: The key.
1427%
1428*/
1429WizardExport void SetCipherKey(CipherInfo *cipher_info,const StringInfo *key)
1430{
1432 WizardAssert(CipherDomain,cipher_info != (CipherInfo *) NULL);
1434 WizardAssert(CipherDomain,key != (StringInfo *) NULL);
1435 switch (cipher_info->cipher)
1436 {
1437 case AESCipher:
1438 {
1439 SetAESKey((AESInfo *) cipher_info->handle,key);
1440 break;
1441 }
1442 case ChachaCipher:
1443 {
1444 SetChachaKey((ChachaInfo *) cipher_info->handle,key);
1445 break;
1446 }
1447 case SerpentCipher:
1448 {
1449 SetSerpentKey((SerpentInfo *) cipher_info->handle,key);
1450 break;
1451 }
1452 case TwofishCipher:
1453 {
1454 SetTwofishKey((TwofishInfo *) cipher_info->handle,key);
1455 break;
1456 }
1457 default:
1459 }
1460}
WizardExport void EncipherAESBlock(AESInfo *aes_info, const unsigned char *plaintext, unsigned char *ciphertext)
Definition aes.c:521
WizardExport void SetAESKey(AESInfo *aes_info, const StringInfo *key)
Definition aes.c:722
WizardExport AESInfo * DestroyAESInfo(AESInfo *aes_info)
Definition aes.c:475
WizardExport unsigned int GetAESBlocksize(const AESInfo *aes_info)
Definition aes.c:653
WizardExport AESInfo * AcquireAESInfo(void)
Definition aes.c:165
WizardExport void DecipherAESBlock(AESInfo *aes_info, const unsigned char *ciphertext, unsigned char *plaintext)
Definition aes.c:319
WizardExport void EncipherChachaBlock(ChachaInfo *chacha_info, const unsigned char *plaintext, unsigned char *ciphertext)
Definition chacha.c:203
WizardExport ChachaInfo * DestroyChachaInfo(ChachaInfo *chacha_info)
Definition chacha.c:166
WizardExport void DecipherChachaBlock(ChachaInfo *chacha_info, const unsigned char *ciphertext, unsigned char *plaintext)
Definition chacha.c:138
WizardExport void SetChachaKey(ChachaInfo *chacha_info, const StringInfo *key)
Definition chacha.c:386
WizardExport unsigned int GetChachaBlocksize(const ChachaInfo *chacha_info)
Definition chacha.c:352
WizardExport ChachaInfo * AcquireChachaInfo(void)
Definition chacha.c:95
static StringInfo * EncipherOFBMode(CipherInfo *, StringInfo *)
Definition cipher.c:1173
static StringInfo * DecipherCBCMode(CipherInfo *cipher_info, StringInfo *ciphertext)
Definition cipher.c:237
WizardExport CipherInfo * DestroyCipherInfo(CipherInfo *cipher_info)
Definition cipher.c:703
WizardExport void SetCipherNonce(CipherInfo *cipher_info, const StringInfo *nonce)
Definition cipher.c:1394
static StringInfo * EncipherCFBMode(CipherInfo *cipher_info, StringInfo *plaintext)
Definition cipher.c:860
static StringInfo * EncipherECBMode(CipherInfo *, StringInfo *)
Definition cipher.c:1108
WizardExport const StringInfo * GetCipherNonce(CipherInfo *cipher_info)
Definition cipher.c:1332
WizardExport void SetCipherKey(CipherInfo *cipher_info, const StringInfo *key)
Definition cipher.c:1429
WizardExport StringInfo * DecipherCipher(CipherInfo *cipher_info, StringInfo *ciphertext)
Definition cipher.c:405
WizardExport void ResetCipherNonce(CipherInfo *cipher_info)
Definition cipher.c:1362
static StringInfo * EncipherCBCMode(CipherInfo *cipher_info, StringInfo *plaintext)
Definition cipher.c:775
static void IncrementCipherNonce(const size_t length, unsigned char *nonce)
Definition cipher.c:482
WizardExport StringInfo * EncryptCipher(CipherInfo *cipher_info, StringInfo *plaintext)
Definition cipher.c:940
WizardExport StringInfo * GenerateCipherNonce(CipherInfo *cipher_info)
Definition cipher.c:1278
void(* DecipherBlock)(void *, const unsigned char *, const unsigned char *)
Definition cipher.c:62
WizardExport CipherInfo * AcquireCipherInfo(const CipherType cipher, const CipherMode mode)
Definition cipher.c:134
static StringInfo * DecipherCTRMode(CipherInfo *, StringInfo *)
Definition cipher.c:497
static StringInfo * DecipherOFBMode(CipherInfo *, StringInfo *)
Definition cipher.c:635
static StringInfo * DecipherECBMode(CipherInfo *, StringInfo *)
Definition cipher.c:576
WizardExport StringInfo * DecryptCipher(CipherInfo *cipher_info, StringInfo *plaintext)
Definition cipher.c:399
WizardExport StringInfo * EncipherCipher(CipherInfo *cipher_info, StringInfo *plaintext)
Definition cipher.c:946
static StringInfo * EncipherCTRMode(CipherInfo *, StringInfo *)
Definition cipher.c:1022
void(*)(* EncipherBlock)(void *, const unsigned char *, const unsigned char *)
Definition cipher.c:63
#define CipherRandomHash
Definition cipher.c:56
static StringInfo * DecipherCFBMode(CipherInfo *cipher_info, StringInfo *ciphertext)
Definition cipher.c:319
WizardExport size_t GetCipherBlocksize(const CipherInfo *cipher_info)
Definition cipher.c:1248
CipherType
Definition cipher.h:40
@ SerpentCipher
Definition cipher.h:45
@ ChachaCipher
Definition cipher.h:44
@ TwofishCipher
Definition cipher.h:46
@ AESCipher
Definition cipher.h:43
CipherMode
Definition cipher.h:30
@ CTRMode
Definition cipher.h:34
@ CFBMode
Definition cipher.h:33
@ CBCMode
Definition cipher.h:32
@ OFBMode
Definition cipher.h:36
@ ECBMode
Definition cipher.h:35
#define MaxCipherBlocksize
Definition cipher.h:27
#define WizardAssert(domain, predicate)
#define ThrowWizardFatalError(domain, error)
#define ThrowFatalException(severity, tag)
@ CipherDomain
Definition exception.h:34
@ CipherFatalError
Definition exception.h:109
@ EnumerateError
Definition exception.h:50
@ MemoryError
Definition exception.h:49
WizardBooleanType LogWizardEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition log.c:1390
@ TraceEvent
Definition log.h:39
#define GetWizardModule()
Definition log.h:30
WizardExport void * AcquireWizardMemory(const size_t size)
Definition memory.c:586
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
WizardExport void * ResetWizardMemory(void *memory, int c, const size_t size)
Definition memory.c:1114
#define WizardExport
#define WizardSignature
WizardExport RandomInfo * AcquireRandomInfo(const HashType hash)
Definition random.c:165
WizardExport void SetRandomKey(RandomInfo *random_info, const size_t length, unsigned char *key)
Definition random.c:1068
WizardExport StringInfo * GetRandomKey(RandomInfo *random_info, const size_t length)
Definition random.c:820
WizardExport RandomInfo * DestroyRandomInfo(RandomInfo *random_info)
Definition random.c:289
WizardExport void DecipherSerpentBlock(SerpentInfo *serpent_info, const unsigned char *ciphertext, unsigned char *plaintext)
Definition serpent.c:154
WizardExport void EncipherSerpentBlock(SerpentInfo *serpent_info, const unsigned char *plaintext, unsigned char *ciphertext)
Definition serpent.c:403
WizardExport SerpentInfo * AcquireSerpentInfo(void)
Definition serpent.c:100
WizardExport void SetSerpentKey(SerpentInfo *serpent_info, const StringInfo *key)
Definition serpent.c:637
WizardExport unsigned int GetSerpentBlocksize(const SerpentInfo *serpent_info)
Definition serpent.c:603
WizardExport SerpentInfo * DestroySerpentInfo(SerpentInfo *serpent_info)
Definition serpent.c:359
WizardExport void SetStringInfo(StringInfo *string_info, const StringInfo *source)
Definition string.c:1792
WizardExport size_t GetStringInfoLength(const StringInfo *string_info)
Definition string.c:1280
WizardExport StringInfo * AcquireStringInfo(const size_t length)
Definition string.c:179
WizardExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
Definition string.c:1251
WizardExport void ResetStringInfo(StringInfo *string_info)
Definition string.c:1761
WizardExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition string.c:857
Definition aes.c:52
size_t signature
Definition cipher.c:95
time_t timestamp
Definition cipher.c:92
DecipherBlock encipher_block
Definition cipher.c:83
StringInfo * nonce
Definition cipher.c:86
void * handle
Definition cipher.c:68
DecipherBlock decipher_block
Definition cipher.c:80
CipherMode mode
Definition cipher.c:74
CipherType cipher
Definition cipher.c:71
size_t blocksize
Definition cipher.c:77
RandomInfo * random_info
Definition cipher.c:89
WizardExport void EncipherTwofishBlock(TwofishInfo *twofish_info, const unsigned char *plaintext, unsigned char *ciphertext)
Definition twofish.c:283
WizardExport TwofishInfo * DestroyTwofishInfo(TwofishInfo *twofish_info)
Definition twofish.c:245
WizardExport void DecipherTwofishBlock(TwofishInfo *twofish_info, const unsigned char *ciphertext, unsigned char *plaintext)
Definition twofish.c:155
WizardExport unsigned int GetTwofishBlocksize(const TwofishInfo *twofish_info)
Definition twofish.c:374
WizardExport TwofishInfo * AcquireTwofishInfo(void)
Definition twofish.c:112
WizardExport void SetTwofishKey(TwofishInfo *twofish_info, const StringInfo *key)
Definition twofish.c:408