sparknacl_4.0.0_3bb462a1/tests/src/ada/onetimeauth7.adb

 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
with SPARKNaCl;       use SPARKNaCl;
with SPARKNaCl.Debug; use SPARKNaCl.Debug;
with SPARKNaCl.MAC;   use SPARKNaCl.MAC;

with Random;
with Interfaces;      use Interfaces;
with Ada.Numerics.Discrete_Random;
procedure Onetimeauth7
is
   package RB is new Ada.Numerics.Discrete_Random (Byte);
   package RBI16 is new Ada.Numerics.Discrete_Random (Index_16);
   RBG    : RB.Generator;
   RBI16G : RBI16.Generator;
   Raw_K  : Bytes_32;
   K      : Poly_1305_Key;
   A      : Bytes_16;
begin
   RB.Reset (RBG);
   RBI16.Reset (RBI16G);
--   for I in N32 range 0 .. 9999 loop
   for I in N32 range 0 .. 99 loop
      declare
         subtype C_Index is N32 range 0 .. I;
         subtype CT is Byte_Seq (C_Index);
         package RCI is new Ada.Numerics.Discrete_Random (C_Index);
         RCIG : RCI.Generator;
         C : CT;
         R1 : C_Index;
         R2 : Byte;
         R3 : Index_16;
      begin
         RCI.Reset (RCIG);
         Random.Random_Bytes (C);
         Random.Random_Bytes (Raw_K);
         Construct (K, Raw_K);
         Onetimeauth (A, C, K);
         if not Onetimeauth_Verify (A, C, K) then
            DH ("Fail ", I64 (I));
            return;
         end if;
         R1 := RCI.Random (RCIG);
         R2 := RB.Random (RBG) mod 255;
         C (R1) := C (R1) + 1 + R2;
         if Onetimeauth_Verify (A, C, K) then
            DH ("Forgery", I64 (I));
            return;
         end if;

         R2 := RB.Random (RBG) mod 255;
         R3 := RBI16.Random (RBI16G);
         A (R3) := A (R3) + 1 + R2;
         if Onetimeauth_Verify (A, C, K) then
            DH ("Forgery", I64 (I));
            return;
         end if;
      end;
   end loop;
end Onetimeauth7;