sparknacl_4.0.0_3bb462a1/tests/src/ada/box8.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.Cryptobox; use SPARKNaCl.Cryptobox;
with SPARKNaCl.Stream;
with Random;              use Random;

with Ada.Numerics.Discrete_Random;
with Ada.Text_IO;      use Ada.Text_IO;
with Interfaces;       use Interfaces;

procedure Box8
is
   Raw_SK : Bytes_32;
   AliceSK, BobSK : Secret_Key;
   AlicePK, BobPK : Public_Key;
   N : Stream.HSalsa20_Nonce;
   S, S2 : Boolean;
begin
--   for MLen in N32 range 0 .. 999 loop
   for MLen in N32 range 0 .. 99 loop
      Random.Random_Bytes (Raw_SK);
      Keypair (Raw_SK, AlicePK, AliceSK);
      Random.Random_Bytes (Raw_SK);
      Keypair (Raw_SK, BobPK, BobSK);
      Random.Random_Bytes (Bytes_24 (N));
      Put ("Box8 - iteration" & MLen'Img);
      declare
         subtype Index is
           N32 range 0 .. Plaintext_Zero_Bytes + MLen - 1;
         subtype CIndex is
           N32 range Ciphertext_Zero_Bytes .. Index'Last;
         subtype Text is
           Byte_Seq (Index);
         package RI is new Ada.Numerics.Discrete_Random (CIndex);
         G : RI.Generator;
         M, C, M2 : Text := (others => 0);
      begin
         RI.Reset (G);
         Random.Random_Bytes (M (Plaintext_Zero_Bytes .. M'Last));
         Create (C, S, M, N, BobPK, AliceSK);
         if S then
            C (RI.Random (G)) := Random_Byte;
            Open (M2, S2, C, N, AlicePK, BobSK);
            if S2 then
               if not Equal (M, M2) then
                  Put_Line (" forgery!");
                  exit;
               else
                  Put_Line (" OK");
               end if;
            else
               Put_Line (" OK"); --  data corruption spotted OK
            end if;
         else
            Put_Line ("bad encryption");
         end if;
      end;
   end loop;
end Box8;