summaryrefslogtreecommitdiff
path: root/src/crypto-types.adb
blob: ac9afc245020151bfa91723b857648ff166cc467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License as
-- published by the Free Software Foundation; either version 2 of the
-- License, or (at your option) any later version.

-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-- General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.

-- As a special exception, if other files instantiate generics from
-- this unit, or you link this unit with other files to produce an
-- executable, this unit does not by itself cause the resulting
-- executable to be covered by the GNU General Public License. This
-- exception does not however invalidate any other reasons why the
-- executable file might be covered by the GNU Public License.

with Ada.Unchecked_Conversion;

--pragma Elaborate_All(Generic_Mod_Aux);
  
package body Crypto.Types is
   
   package body Generic_Mod_Aux is
      function "xor"(Left, Right : T_A) return T_A is
	 Result : T_A(0..Left'Length-1);
   begin
      if Left'Length /= Right'Length then
         raise  Constraint_Error;
      end if;
      for I in 0..Left'Length-1 loop
         Result(I) := Left(Left'First+I) xor Right(Right'First+I);
      end loop;
      return Result;
   end "xor";
    
   ------------------------------------------------------------------------
   
   function "xor"(Left : T_A; Right : T) return T_A is
      Result : T_A := Left;
   begin
      Result(Result'Last) := Left(Result'Last) xor Right;
      return Result;
   end "xor";
   
   ------------------------------------------------------------------------

    function "xor"(Left : T; Right : T_A) return T_A is
   begin
      return Right xor Left;
   end "xor";
   
      
      ------------------------------------------------------------------------
      
   function "and"(Left, Right : T_A) return T_A is
	Result : T_A(0..Left'Length-1);
   begin

      if Left'Length /= Right'Length then
         raise  Constraint_Error;
      end if;
      for I in 0..Left'Length-1 loop
         Result(I) := Left(Left'First+I) and Right(Right'First+I);
      end loop;
      return Result;
   end "and";
      
   ------------------------------------------------------------------------
   function "+"(Left : T_A; Right : T) return T_A is
      Result: T_A(Left'Range) := Left;
   begin
      Result(Left'Last) := Left(Left'Last) + Right;

      -- overflow?
      if Result(Left'Last) < Left(Left'Last) then
         for I in reverse Left'First..Left'Last-1 loop
            Result(I):=Result(I)+1;
            if Result(I) /= 0 then
               return  Result;
            end if;
         end loop;
      end if;
      return Result;
   end "+";

   
   ------------------------------------------------------------------------
   function "+"(Left : T; Right : T_A) return T_A is
   begin
      return Right + Left;
   end "+";
   ------------------------------------------------------------------------
   
   function Is_Zero(Item : T_A) return Boolean is
   begin
      for I in  Item'Range loop
         if Item(I) /= 0 then return False;
         end if;
      end loop;
      return True;
   end Is_Zero;
   
------------------------------------------------------------------------
   
   function Left_Part(Block : in T_A) return T_A is
      Len  : constant Natural := ((Block'Length+1)/2)-1;
      Left : constant T_A(0..Len) := Block(Block'First..(Block'First+Len));
   begin
      return Left;
   end Left_Part;
   
   ------------------------------------------------------------------------
   
   function Right_Part(Block : in T_A) return T_A is
      Len : constant Natural := Block'Length/2; 
      Right : constant T_A(0..Len-1) := Block(Block'Last-Len+1..Block'Last);
   begin
      return Right;
   end Right_Part;
   
   ------------------------------------------------------------------------
   
   function Shift_Left(Value : T_A;  Amount : Natural) return T_A  is
      Result : T_A(Value'Range) := (others => 0);
      L : constant Natural := Amount mod T'Size;
      M : constant Natural := Value'First+(Amount/T'Size);
   begin
      if Amount >= Value'Size then
	 return Result;
      elsif Amount = 0 then
	 return Value;
      end if;      
      Result(Value'Last-M) := Shift_Left(Value(Value'Last),L);      
      
      for I in reverse Value'First..Value'Last-(M+1) loop
	 Result(I) := Shift_Left(Value(I),L)
	   xor Shift_Right(Value(I+1),T'Size-L);
      end loop;
      return Result;
      end Shift_Left; 
      -------------------------------------------------------------------------
         function Shift_Right(Value : T_A;  Amount : Natural) return T_A  is
      Result : T_A(Value'Range) := (others => 0);
      L : constant Natural := Amount mod T'Size;
      M : constant Natural := Value'First+(Amount/T'Size);
   begin
      if Amount >= Value'Size then
	 return Result;
      elsif Amount = 0 then
	 return Value;
      end if;      
      Result(Value'Last-M) := Shift_Right(Value(Value'Last),L);      
      
      for I in reverse Value'First..Value'Last-(M+1) loop
	 Result(I) := Shift_Right(Value(I),L)
	   xor Shift_Left(Value(I+1),T'Size-L);
      end loop;
      return Result;
   end Shift_Right;    

   end Generic_Mod_Aux;
   
   
   function Cast  is new Ada.Unchecked_Conversion (Byte_Word, Word);
   function Cast  is new Ada.Unchecked_Conversion (Word, Byte_Word);
   function DCast is new Ada.Unchecked_Conversion (Byte_DWord, DWord);
   function DCast is new Ada.Unchecked_Conversion (DWord, Byte_DWord);
   pragma Inline (Cast, DCast);
  
      
   package Aux_Byte is new Generic_Mod_Aux(Byte,Bytes);
   package Aux_Word is new Generic_Mod_Aux(Word,Words);
   package Aux_DWord is new Generic_Mod_Aux(Dword,DWords);
   
   ---------------------------------------------------------------------------

   function To_Word(A,B,C,D : Character) return Word is
   begin
      return Cast((Byte(Character'Pos(D)), Byte(Character'Pos(C)),
                   Byte(Character'Pos(B)), Byte(Character'Pos(A))));
   end To_Word;

   ---------------------------------------------------------------------------

   function To_Word(A,B,C,D : Byte) return Word is
   begin
      return Cast((D, C, B, A));
   end To_Word;

   ---------------------------------------------------------------------------

   function To_Word (X : Byte_Word) return Word is
   begin
      return Cast((X(3), X(2), X(1), X(0)));
   end To_Word;

   ---------------------------------------------------------------------------

   function R_To_Word (X : Byte_Word) return Word is
   begin
      return Cast(X);
   end R_To_Word;

   ---------------------------------------------------------------------------

   function To_Bytes (X : Word) return Byte_Word is
   begin
      return (Cast(X)(3), Cast(X)(2), Cast(X)(1), Cast(X)(0));
   end To_Bytes;

   ---------------------------------------------------------------------------

   function R_To_Bytes (X : Word) return Byte_Word is
   begin
      return Cast(X);
   end R_To_Bytes;

   ---------------------------------------------------------------------------

   function Byte0 (W : Word) return Byte is
   begin
      return Cast(W)(3);
   end Byte0;

   ---------------------------------------------------------------------------

   function Byte1 (W : Word) return Byte is
   begin
      return Cast(W)(2);
   end Byte1;

   ---------------------------------------------------------------------------

   function Byte2 (W : Word) return Byte is
   begin
      return Cast(W)(1);
   end Byte2;

   ---------------------------------------------------------------------------

   function Byte3 (W : Word) return Byte is
   begin
      return Cast(W)(0);
   end Byte3;

   ---------------------------------------------------------------------------

   function To_DWord (X : Byte_DWord) return DWord is
   begin
      return DCast((X(7), X(6), X(5), X(4), X(3), X(2), X(1), X(0)));
   end To_DWord;

   ---------------------------------------------------------------------------

   function R_To_DWord (X : Byte_DWord) return DWord is
   begin
      return DCast(X);
   end R_To_DWord;

   ---------------------------------------------------------------------------

   function To_Bytes (X : DWord) return Byte_DWord is
   begin
      return (DCast(X)(7), DCast(X)(6), DCast(X)(5), DCast(X)(4),
              DCast(X)(3), DCast(X)(2), DCast(X)(1), DCast(X)(0));

   end To_Bytes;

   ---------------------------------------------------------------------------

   function R_To_Bytes (X : DWord) return Byte_DWord is
   begin
      return DCast(X);
   end R_To_Bytes;

   ---------------------------------------------------------------------------

   function Byte0 (D : DWord) return Byte is
   begin
      return DCast(D)(7);
   end Byte0;

   ---------------------------------------------------------------------------
   function Byte1 (D : DWord) return Byte is
   begin
      return DCast(D)(6);
   end Byte1;

   ---------------------------------------------------------------------------

   function Byte2 (D : DWord) return Byte is
   begin
      return DCast(D)(5);
   end Byte2;

   ---------------------------------------------------------------------------

    function Byte3 (D : DWord) return Byte is
   begin
      return DCast(D)(4);
   end Byte3;

   ---------------------------------------------------------------------------

    function Byte4 (D : DWord) return Byte is
   begin
      return DCast(D)(3);
   end Byte4;

   ---------------------------------------------------------------------------

    function Byte5 (D : DWord) return Byte is
   begin
      return DCast(D)(2);
   end Byte5;

   ---------------------------------------------------------------------------

    function Byte6 (D : DWord) return Byte is
   begin
      return DCast(D)(1);
   end Byte6;

   ---------------------------------------------------------------------------

   function Byte7 (D : DWord) return Byte is
   begin
      return DCast(D)(0);
   end Byte7;

   ---------------------------------------------------------------------------


   function "xor"(Left, Right : Bytes) return Bytes is
   begin
      return Aux_Byte."xor"(Left,Right);
   end "xor";

   ---------------------------------------------------------------------------
   
   function "xor"(Left : Bytes; Right : Byte) return Bytes is
   begin
      return Aux_Byte."xor"(Left,Right);
   end "xor";
   
   ---------------------------------------------------------------------------
      
   function "+"(Left : Bytes; Right : Byte) return Bytes is
   begin
      return Aux_Byte."+"(Left,Right);
   end "+";

   ---------------------------------------------------------------------------

   function "+"(Left : Byte; Right : Bytes) return Bytes is
   begin
      return Right + Left;
   end "+";
   
   ---------------------------------------------------------------------------
   
   function "and"(Left, Right : Bytes) return Bytes is
   begin
      return Aux_Byte."and"(Left,Right);
   end "and";

   ---------------------------------------------------------------------------
   ---------------------------------------------------------------------------

   function "xor"(Left, Right : Words) return Words is
   begin
      return Aux_Word."xor"(Left,Right);
   end "xor";

   ---------------------------------------------------------------------------

   function "+"(Left : Words; Right : Word) return Words is

   begin
      return Aux_Word."+"(Left,Right);
   end "+";

   ---------------------------------------------------------------------------

   function "+"(Left : Word; Right : Words) return Words is
   begin
      return Right + Left;
   end "+";

   ---------------------------------------------------------------------------

   function "+"(Left : Words; Right : Byte) return Words is
    Result: Words(Left'Range) := Left;
   begin
      Result(Left'Last) := Left(Left'Last) + Word(Right);

      -- overflow?
      if Result(Left'Last) < Left(Left'Last) then
         for I in reverse Left'First..Left'Last-1 loop
            Result(I):=Result(I)+1;
            if Result(I) /= 0 then
               return  Result;
            end if;
         end loop;
      end if;
      return Result;
   end "+";

   ---------------------------------------------------------------------------
   ---------------------------------------------------------------------------

   function "xor"(Left, Right : DWords) return DWords is
   begin
      return Aux_DWord."xor"(Left,Right);
   end "xor";

   ---------------------------------------------------------------------------

   function "+"(Left : DWords; Right : DWord) return DWords is
   begin
      return Aux_DWord."+"(Left,Right);
   end "+";

   ---------------------------------------------------------------------------

   function "+"(Left : DWord; Right : DWords) return DWords is
   begin
      return Right + Left;
   end "+";

   ---------------------------------------------------------------------------

   function "+"(Left : DWords; Right : Byte) return DWords is
    Result: DWords(Left'Range) := Left;
   begin
      Result(Left'Last) := Left(Left'Last) + DWord(Right);

      -- overflow?
      if Result(Left'Last) < Left(Left'Last) then
         for I in reverse Left'First..Left'Last-1 loop
            Result(I):=Result(I)+1;
            if Result(I) /= 0 then
               return  Result;
            end if;
         end loop;
      end if;
      return Result;
   end "+";

   ---------------------------------------------------------------------------

   function To_Words(Byte_Array : Bytes) return Words is
      L : constant Natural :=
        Natural(Float'Ceiling(Float(Byte_Array'Length)/4.0))-1;
      W : Words(0..L) := (others => 0);
      N : Integer := Byte_Array'First;
      S : Natural :=24;
   begin

      for I in 0..(Byte_Array'Length/4)-1 loop
         W(I) := To_Word(Byte_Array(N..N+3));
         N := N+4;
      end loop;

      for I in  1..(Byte_Array'Length mod 4) loop
         W(L):= W(L) or Shift_Left(Word(Byte_Array(N)),S);
         N := N+1;
         S := S-8;
      end loop;

      return W;
   end To_Words;

   ---------------------------------------------------------------------------

   function To_Bytes(Word_Array : Words) return Bytes is
      B : Bytes(1..Word_Array'Length*4);
      C : Natural := 1;
   begin
      for I in  Word_Array'Range loop
         B(C..C+3) := To_Bytes(Word_Array(I));
         C:=C+4;
      end loop;
      return B;
   end To_Bytes;

   ---------------------------------------------------------------------------

   function To_DWords(Byte_Array : Bytes) return DWords is
      L : constant Natural := 
	Natural(Float'Ceiling(Float(Byte_Array'Length)/8.0))-1;
      W : DWords(0..L):=(others => 0);
      N : Natural := Byte_Array'First;
      S : Natural := 56;
   begin

      for I in 0..(Byte_Array'Length/8)-1 loop
         W(I) := To_DWord(Byte_Array(N..N+7));
         N := N+8;
      end loop;

      for I in  1..(Byte_Array'Length mod 8) loop
         W(L):= W(L) or Shift_Left(DWord(Byte_Array(N)),S);
         N := N+1;
         S := S-8;
      end loop;

      return W;
   end To_DWords;

   ---------------------------------------------------------------------------


    function To_Bytes(DWord_Array : DWords) return Bytes is
      B : Bytes(1..DWord_Array'Length*8);
      C : Natural := 1;
   begin
      for I in  DWord_Array'Range loop
         B(C..C+7) := To_Bytes(DWord_Array(I));
         C:=C+8;
      end loop;
      return B;
   end To_Bytes;
   
   ---------------------------------------------------------------------------

   function To_Hex(B : Byte) return Hex_Byte is
      S : constant String := "0123456789ABCDEF";
      H : Hex_Byte;
   begin
      H(2) := S(Natural(B and 15)+1);
      H(1) := S(Natural(Shift_Right(B,4)+1));
      return H;
   end To_Hex;

   ---------------------------------------------------------------------------

   function To_Hex(W : Word) return Hex_Word is
      S : constant String := "0123456789ABCDEF";
      H : Hex_Word;
      T : Word := W;
   begin
      for I in reverse  H'Range loop
        H(I) :=  S(Natural(T and 15)+1);
        T := Shift_Right(T,4);
      end loop;
      return H;
   end To_Hex;

   ---------------------------------------------------------------------------


   function To_Hex(D : DWord) return Hex_DWord is
      S : constant String := "0123456789ABCDEF";
      H : Hex_DWord;
      T : DWord := D;
   begin
      for I in reverse H'Range loop
         H(I) :=  S(Natural(T and 15)+1);
         T := Shift_Right(T,4);
      end loop;

      return H;
   end To_Hex;


   function Hex_To_Bytes(Hex : String) return Bytes is
    Return_Bytes : Bytes(0..Hex'Length/2 -1);
   begin
   for I in Return_Bytes'Range loop
      case Hex(Hex'First + 2*i) is
         when '0'  => Return_Bytes(i):=16#00#;
         when '1'  => Return_Bytes(i):=16#01#;
         when '2' => Return_Bytes(i):=16#02#;
         when '3' => Return_Bytes(i):=16#03#;
         when '4' => Return_Bytes(i):=16#04#;
         when '5' => Return_Bytes(i):=16#05#;
         when '6' => Return_Bytes(i):=16#06#;
         when '7' => Return_Bytes(i):=16#07#;
         when '8' => Return_Bytes(i):=16#08#;
         when '9' => Return_Bytes(i):=16#09#;
         when 'A' => Return_Bytes(i):=16#0A#;
         when 'B' => Return_Bytes(i):=16#0B#;
         when 'C' => Return_Bytes(i):=16#0C#;
         when 'D' => Return_Bytes(i):=16#0D#;
         when 'E' => Return_Bytes(i):=16#0E#;
         when 'F' => Return_Bytes(i):=16#0F#;
         when others => null;
      end case;
      
      case Hex(Hex'First + 2*i + 1) is
         when '0' => Return_Bytes(i):= Return_Bytes(i) + 16#00#;
         when '1' => Return_Bytes(i):= Return_Bytes(i) + 16#10#;
         when '2' => Return_Bytes(i):= Return_Bytes(i) + 16#20#;
         when '3' => Return_Bytes(i):= Return_Bytes(i) + 16#30#;
         when '4' => Return_Bytes(i):= Return_Bytes(i) + 16#40#;
         when '5' => Return_Bytes(i):= Return_Bytes(i) + 16#50#;
         when '6' => Return_Bytes(i):= Return_Bytes(i) + 16#60#;
         when '7' => Return_Bytes(i):= Return_Bytes(i) + 16#70#;
         when '8' => Return_Bytes(i):= Return_Bytes(i) + 16#80#;
         when '9' => Return_Bytes(i):= Return_Bytes(i) + 16#90#;
         when 'A' => Return_Bytes(i):= Return_Bytes(i) + 16#A0#;
         when 'B' => Return_Bytes(i):= Return_Bytes(i) + 16#B0#;
         when 'C' => Return_Bytes(i):= Return_Bytes(i) + 16#C0#;
         when 'D' => Return_Bytes(i):= Return_Bytes(i) + 16#D0#;
         when 'E' => Return_Bytes(i):= Return_Bytes(i) + 16#E0#;
         when 'F' => Return_Bytes(i):= Return_Bytes(i) + 16#F0#;
         when others => null;
      end case;
      
   end loop;
      return Return_Bytes;
   end Hex_To_Bytes;

   ---------------------------------------------------------------------------

   function Is_Zero(Byte_Array : Bytes) return Boolean is
   begin
      return Aux_Byte.Is_Zero(Byte_Array);
   end Is_Zero;

   ---------------------------------------------------------------------------


   function Is_Zero(Word_Array : Words) return Boolean is
   begin
      return Aux_Word.Is_Zero(Word_Array);
   end Is_Zero;

   ---------------------------------------------------------------------------

   function Is_Zero(DWord_Array : Dwords) return Boolean is
   begin
      return Aux_DWord.Is_Zero(DWord_Array);
   end Is_Zero;

   ---------------------------------------------------------------------------
   ---------------------------------------------------------------------------
   ---------------------------------------------------------------------------

   function To_Bytes(Message : String) return Bytes is
     B : Bytes(Message'Range);
   begin
      for I in Message'Range loop
         B(I) := Character'Pos(Message(I));
      end loop;
      return B;
   end To_Bytes;

   ---------------------------------------------------------------------------

   function To_String(ASCII : Bytes) return String is
      S : String(1..ASCII'Length);
      J : Integer:=1;
   begin
      for I in ASCII'Range loop
         S(J) := Character'Val(ASCII(I));
         J:=J+1;
      end loop;
      return S;
   end To_String;
   
   ---------------------------------------------------------------------------
   
   function Left_Part(Block : in Bytes) return Bytes is
   begin
      return Aux_Byte.Left_Part(Block);
   end Left_Part;
   
   ---------------------------------------------------------------------------
   
   function Right_Part(Block : in Bytes) return Bytes is
   begin
      return Aux_Byte.Right_Part(Block);
   end Right_Part;
   
   ---------------------------------------------------------------------------
   
    function To_Bytes(B : B_Block64) return Bytes is
   begin
      return Bytes(B);
   end To_Bytes;
    
   ---------------------------------------------------------------------------
   
   function To_Bytes(B : B_Block128) return Bytes is
   begin
      return Bytes(B);
   end To_Bytes;
   
   ---------------------------------------------------------------------------
   
   function To_Bytes(B : B_Block192) return Bytes is
      
   begin
      return Bytes(B);
   end To_Bytes;
   
    ---------------------------------------------------------------------------
   
   function To_Bytes(B : B_Block256) return Bytes is
   begin
      return Bytes(B);
   end To_Bytes;
   
   ---------------------------------------------------------------------------
   
   function To_Bytes(W : W_Block160) return Bytes is
   begin
      return To_Bytes(Words(W));
   end To_Bytes;
   
   ---------------------------------------------------------------------------
   
    function To_Bytes(W : W_Block256) return Bytes is
   begin
      return To_Bytes(Words(W));
   end To_Bytes;
   
   ---------------------------------------------------------------------------
      
    function To_Bytes(W : W_Block512) return Bytes is
   begin
      return To_Bytes(Words(W));
   end To_Bytes;
   
   ---------------------------------------------------------------------------
   
   function To_Bytes(D : DW_Block256) return Bytes is
   begin
      return To_Bytes(DWords(D));
   end To_Bytes;
   
   ---------------------------------------------------------------------------
   
   function To_Bytes(D : DW_Block384) return Bytes is
   begin
      return To_Bytes(DWords(D));
   end To_Bytes;
   
   ---------------------------------------------------------------------------
   
   function To_Bytes(D : DW_Block512) return Bytes is
   begin
      return To_Bytes(DWords(D));
   end To_Bytes;

   ---------------------------------------------------------------------------
   
   function To_Bytes(D : DW_Block1024) return Bytes is
   begin
      return To_Bytes(DWords(D));
   end To_Bytes;

   ---------------------------------------------------------------------------
   
   function To_Bytes(D : DW_Block8192) return Bytes is
   begin
      return To_Bytes(DWords(D));
   end To_Bytes;
   
   ---------------------------------------------------------------------------  
   
   function To_B_Block64(B : Bytes) return B_Block64 is
   begin
      return B_Block64(B);
   end To_B_Block64;
   
   ---------------------------------------------------------------------------
   
   function To_B_Block128(B : Bytes) return B_Block128 is
   begin
      return B_Block128(B);
   end To_B_Block128;
      
   ---------------------------------------------------------------------------
      
   function To_B_Block192(B : Bytes) return B_Block192 is
   begin
      return B_Block192(B);
   end To_B_Block192;
   
      ---------------------------------------------------------------------------
      
   function To_B_Block256(B : Bytes) return B_Block256 is
   begin
      return B_Block256(B);
   end To_B_Block256;
   
   ---------------------------------------------------------------------------
   
   function To_W_Block160(B : Bytes) return W_Block160 is
   begin
     return W_Block160(To_Words(B));
   end To_W_Block160;
   
    ---------------------------------------------------------------------------
   
   function To_W_Block256(B : Bytes) return W_Block256 is
   begin
     return W_Block256(To_Words(B));
   end To_W_Block256;
   
    ---------------------------------------------------------------------------
   
   function To_W_Block512(B : Bytes) return W_Block512 is
   begin
     return W_Block512(To_Words(B));
   end To_W_Block512;
   
   ---------------------------------------------------------------------------
      
   function To_DW_Block256(B : Bytes) return DW_Block256 is
   begin
     return DW_Block256(To_DWords(B));
   end To_DW_Block256;
   
   ---------------------------------------------------------------------------
   
   function To_DW_Block384(B : Bytes) return DW_Block384 is
   begin
     return DW_Block384(To_DWords(B));
   end To_DW_Block384;
   
   ---------------------------------------------------------------------------
     
   function To_DW_Block512(B : Bytes) return DW_Block512 is
   begin
     return DW_Block512(To_DWords(B));
   end To_DW_Block512;
      
   ---------------------------------------------------------------------------
     
   function To_DW_Block1024(B : Bytes) return DW_Block1024 is
   begin
     return DW_Block1024(To_DWords(B));
   end To_DW_Block1024;

   ---------------------------------------------------------------------------
     
   function To_DW_Block8192(B : Bytes) return DW_Block8192 is
   begin
     return DW_Block8192(To_DWords(B));
   end To_DW_Block8192;
   
   ---------------------------------------------------------------------------
   
   function "xor"(Left, Right : W_Block160) return  W_Block160 is
   begin
      return W_Block160(Words(Left) xor Words(Right));
   end "xor";
   ---------------------------------------------------------------------------
   
   function "xor"(Left, Right : W_Block256) return  W_Block256 is
   begin
      return W_Block256(Words(Left) xor Words(Right));
   end "xor";
   ---------------------------------------------------------------------------
   
   function "xor"(Left, Right : W_Block512) return  W_Block512 is
   begin
      return W_Block512(Words(Left) xor Words(Right));
   end "xor";
   
   ---------------------------------------------------------------------------

   function "xor"(Left, Right : DW_Block512) return  DW_Block512 is
   begin
      return DW_Block512(DWords(Left) xor DWords(Right));
   end "xor";
   
   ---------------------------------------------------------------------------
     
   function "xor"(Left, Right : DW_Block1024) return  DW_Block1024 is
   begin
      return DW_Block1024(DWords(Left) xor DWords(Right));
   end "xor"; 

   ---------------------------------------------------------------------------
     
   function "xor"(Left, Right : DW_Block8192) return  DW_Block8192 is
   begin
      return DW_Block8192(DWords(Left) xor DWords(Right));
   end "xor"; 
   
   ---------------------------------------------------------------------------
   
   function "xor"(Left, Right : B_Block128) return  B_Block128 is
   begin
      return B_Block128(Bytes(Left) xor Bytes(Right));
   end "xor"; 
     
   ---------------------------------------------------------------------------
   
   function "xor"(Left, Right : B_Block64) return  B_Block64 is
   begin
      return B_Block64(Bytes(Left) xor Bytes(Right));
   end "xor"; 
   
   ---------------------------------------------------------------------------
   
   function "+"(Left : B_Block128; Right : Byte) return B_Block128 is
   begin
      return B_Block128(Bytes(Left) + Right);
   end "+";
   
   ---------------------------------------------------------------------------
   
   function Shift_Left(Value : Bytes; Amount : Natural) return Bytes is
   begin
      return Aux_Byte.Shift_Left(Value,Amount);
   end Shift_Left;
   
   ---------------------------------------------------------------------------
   
   function Shift_Left(Value : B_Block128; Amount :Natural) return B_Block128 is
   begin
      return  B_Block128(Aux_Byte.Shift_Left(Bytes(Value),Amount));
   end Shift_Left;
   
   ----------------------------------------------------------------------------
   
   function Shift_Right(Value : Bytes; Amount : Natural) return Bytes is
   begin
      return Aux_Byte.Shift_Right(Value,Amount);
   end Shift_Right;
   
   ---------------------------------------------------------------------------
   
   function Shift_Right(Value : B_Block128; Amount :Natural)return B_Block128 is
   begin
      return  B_Block128(Aux_Byte.Shift_Right(Bytes(Value),Amount));
   end Shift_Right;
   
 
   
   
  end Crypto.Types;