libkeccak_3.0.0_f33be1c8/tests/kat/src/test_vectors.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
 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
-------------------------------------------------------------------------------
--  Copyright (c) 2019, Daniel King
--  All rights reserved.
--
--  Redistribution and use in source and binary forms, with or without
--  modification, are permitted provided that the following conditions are met:
--      * Redistributions of source code must retain the above copyright
--        notice, this list of conditions and the following disclaimer.
--      * Redistributions in binary form must reproduce the above copyright
--        notice, this list of conditions and the following disclaimer in the
--        documentation and/or other materials provided with the distribution.
--      * The name of the copyright holder may not be used to endorse or promote
--        Products derived from this software without specific prior written
--        permission.
--
--  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
--  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
--  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
--  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
--  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
--  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
--  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
--  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
--  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
--  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-------------------------------------------------------------------------------
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with Ada.Strings.Maps;       use Ada.Strings.Maps;
with Ada.Strings.Fixed;      use Ada.Strings.Fixed;
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
with Interfaces; use Interfaces;

with GNAT.Regpat;

package body Test_Vectors is

   use type GNAT.Regpat.Match_Location;

   function Create_List (Value : in Value_Choice) return Value_Choice_Lists.List;

   ----------------
   -- Initialize --
   ----------------

   overriding
   procedure Initialize (Object : in out Value_Choice) is
   begin
      case Object.VType is
         when String_Type =>
            Object.Str := Null_Unbounded_String;

         when Integer_Type =>
            Object.Int := 0;

         when Hex_Array_Type =>
            Object.Hex := null;
      end case;
   end Initialize;

   ------------
   -- Adjust --
   ------------

   overriding
   procedure Adjust (Object : in out Value_Choice) is
   begin
      if Object.VType = Hex_Array_Type and then Object.Hex /= null then
         Object.Hex := new Keccak.Types.Byte_Array'(Object.Hex.all);
      end if;
   end Adjust;

   --------------
   -- Finalize --
   --------------

   overriding
   procedure Finalize (Object : in out Value_Choice) is
      procedure Free is new Ada.Unchecked_Deallocation (Keccak.Types.Byte_Array,
                                                        Byte_Array_Access);

   begin
      if Object.VType = Hex_Array_Type and then Object.Hex /= null then
         Free (Object.Hex);
      end if;
   end Finalize;

   ------------------------------
   -- Hex_String_To_Byte_Array --
   ------------------------------

   function Hex_String_To_Byte_Array (Str : in String) return Byte_Array_Access
   is
      function Char_To_Byte (C : in Character) return Keccak.Types.Byte;

      function Char_To_Byte (C : in Character) return Keccak.Types.Byte
      is
         Byte : Keccak.Types.Byte;
      begin
         if C >= '0' and C <= '9' then
            Byte := Keccak.Types.Byte (Character'Pos (C) - Character'Pos ('0'));

         elsif C >= 'a' and C <= 'f' then
            Byte := Keccak.Types.Byte (16#A# + (Character'Pos (C) - Character'Pos ('a')));

         elsif C >= 'A' and C <= 'F' then
            Byte := Keccak.Types.Byte (16#A# + (Character'Pos (C) - Character'Pos ('A')));

         else
            raise Constraint_Error;

         end if;

         return Byte;
      end Char_To_Byte;

      Byte_Array : Byte_Array_Access;
      I          : Natural := 0;
   begin
      if (Str'Length mod 2 /= 0) then
         raise Constraint_Error;
      end if;

      Byte_Array := new Keccak.Types.Byte_Array (0 .. Str'Length / 2 - 1);

      while I < Str'Length loop
         Byte_Array.all (I / 2) := Shift_Left (Char_To_Byte (Str (Str'First + I)), 4)
                                   or Char_To_Byte (Str (Str'First + I + 1));

         I := I + 2;
      end loop;

      return Byte_Array;

   end Hex_String_To_Byte_Array;

   --------------------------
   -- String_To_Byte_Array --
   --------------------------

   function String_To_Byte_Array (Str : in String) return Byte_Array_Access
   is
      Byte_Array : Byte_Array_Access;

   begin
      Byte_Array := new Keccak.Types.Byte_Array (1 .. Str'Length);

      for I in Natural range 0 .. Str'Length - 1 loop
         Byte_Array (Byte_Array'First + I) :=
           Keccak.Types.Byte (Character'Pos (Str (Str'First + I)));
      end loop;

      return Byte_Array;

   end String_To_Byte_Array;

   --------------------------
   -- Byte_Array_To_String --
   --------------------------

   function Byte_Array_To_String (Data : in Keccak.Types.Byte_Array) return String
   is

      Hex_Characters : constant array (Keccak.Types.Byte range 0 .. 15) of Character :=
         ('0', '1', '2', '3', '4', '5', '6', '7',
          '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');

      Str : String (1 .. Data'Length * 2);
      I   : Natural := 0;
   begin

      while I < Data'Length loop
         Str (I * 2 + 1)     := Hex_Characters (Shift_Right (Data (Data'First + I), 4));
         Str (I * 2 + 2) := Hex_Characters (Data (Data'First + I) mod 16);

         I := I + 1;
      end loop;

      return Str;

   end Byte_Array_To_String;

   -----------------
   -- Create_List --
   -----------------

   function Create_List (Value : in Value_Choice) return Value_Choice_Lists.List is
   begin
      return L : Value_Choice_Lists.List do
         L := Value_Choice_Lists.Empty_List;
         L.Append (Value);
      end return;
   end Create_List;

   ----------
   -- Load --
   ----------

   Pair_Regex       : constant GNAT.Regpat.Pattern_Matcher :=
     GNAT.Regpat.Compile ("^(\w+)\s*=\s*""?([^""]*)""?$");

   Empty_Line_Regex : constant GNAT.Regpat.Pattern_Matcher :=
     GNAT.Regpat.Compile ("^\s*$");

   procedure Load (File_Name    : in     String;
                   Schema       : in     Schema_Maps.Map;
                   Vectors_List :    out Lists.List) is

      File : Ada.Text_IO.File_Type;

      Test_Vector : Test_Vector_Maps.Map := Test_Vector_Maps.Empty_Map;

   begin

      Vectors_List := Lists.Empty_List;

      Ada.Text_IO.Open (File => File,
                        Mode => Ada.Text_IO.In_File,
                        Name => File_Name);

      while not Ada.Text_IO.End_Of_File (File) loop
         declare
            --  Read a line and trim whitespace.
            Line : constant String := Trim (Source => Ada.Text_IO.Get_Line (File),
                                            Left   => To_Set (" " & CR),
                                            Right  => To_Set (" " & CR));
         begin
            Parse_Line (Test_Vector, Vectors_List, Schema, Line);
         end;
      end loop;

      Ada.Text_IO.Close (File);

      Append_Test_Vector (Test_Vector, Vectors_List, Schema);
   end Load;

   ----------------
   -- Parse_Line --
   ----------------

   procedure Parse_Line (Test_Vector  : in out Test_Vector_Maps.Map;
                         Vectors_List : in out Lists.List;
                         Schema       : in     Schema_Maps.Map;
                         Line         : in     String)
   is
      Pair_Match       : GNAT.Regpat.Match_Array (0 .. 2) := (others => GNAT.Regpat.No_Match);
      Empty_Line_Match : GNAT.Regpat.Match_Array (0 .. 0) := (others => GNAT.Regpat.No_Match);

      Key_First : Natural;
      Key_Last  : Natural;
      Key       : Unbounded_String;

      Value_First : Natural;
      Value_Last  : Natural;
      Value       : Unbounded_String;
   begin

      GNAT.Regpat.Match (Pair_Regex,       Line, Pair_Match);
      GNAT.Regpat.Match (Empty_Line_Regex, Line, Empty_Line_Match);

      if Empty_Line_Match (0) /= GNAT.Regpat.No_Match then
         --  Found an empty line. This signals the end of the current test vector entry.

         Append_Test_Vector (Test_Vector, Vectors_List, Schema);

         Test_Vector_Maps.Clear (Test_Vector);

      elsif Pair_Match (0) /= GNAT.Regpat.No_Match then
         Key_First := Pair_Match (1).First;
         Key_Last  := Pair_Match (1).Last;
         Key := To_Unbounded_String (Line (Key_First .. Key_Last));

         if Pair_Match (2) = GNAT.Regpat.No_Match then
            Value_First := Line'First;
            Value_Last  := Line'First - 1;
         else
            Value_First := Pair_Match (2).First;
            Value_Last  := Pair_Match (2).Last;
         end if;
         Value := To_Unbounded_String (Line (Value_First .. Value_Last));

         Add_Test_Vector_Key_Value_Pair (Test_Vector, Vectors_List, Schema, Key, Value);

      end if;
   end Parse_Line;

   ------------------------------------
   -- Add_Test_Vector_Key_Value_Pair --
   ------------------------------------

   procedure Add_Test_Vector_Key_Value_Pair (Test_Vector  : in out Test_Vector_Maps.Map;
                                             Vectors_List : in out Lists.List;
                                             Schema       : in     Schema_Maps.Map;
                                             Key          : in     Unbounded_String;
                                             Value        : in     Unbounded_String)
   is
      procedure Update_List (Key  : in     Unbounded_String;
                             List : in out Value_Choice_Lists.List);

      procedure Update_List (Key    : in     Unbounded_String;
                             List   : in out Value_Choice_Lists.List) is
      begin
         case Schema.Element (Key).VType is
            when String_Type =>
               List.Append
                 (Value_Choice'(Controlled with
                  VType => String_Type,
                  Str   => Value));

            when Integer_Type =>
               List.Append
                 (Value_Choice'(Controlled with
                  VType => Integer_Type,
                  Int   => Integer'Value (To_String (Value))));

            when Hex_Array_Type =>
               List.Append
                 (Value_Choice'(Controlled with
                  VType => Hex_Array_Type,
                  Hex   => Hex_String_To_Byte_Array (To_String (Value))));

         end case;
      end Update_List;

   begin
      if Schema.Contains (Key) then

         --  Some test vector files do not have blank lines separating
         --  each test vector, so if we find a key that we have already
         --  put into the current test vector then we have found the
         --  start of the next test vector.
         if Test_Vector.Contains (Key) and not Schema.Element (Key).Is_List then
            Append_Test_Vector (Test_Vector, Vectors_List, Schema);
         end if;

         if Test_Vector.Contains (Key) then

            Test_Vector.Update_Element (Position => Test_Vector.Find (Key),
                                        Process  => Update_List'Access);

         else
            case Schema.Element (Key).VType is
               when String_Type =>
                  Test_Vector.Insert
                    (Key      => Key,
                     New_Item => Create_List (Value_Choice'(Controlled with
                       VType => String_Type,
                       Str   => Value)));

               when Integer_Type =>
                  Test_Vector.Insert
                    (Key      => Key,
                     New_Item => Create_List (Value_Choice'(Controlled with
                       VType => Integer_Type,
                       Int   => Integer'Value (To_String (Value)))));

               when Hex_Array_Type =>
                  Test_Vector.Insert
                    (Key      => Key,
                     New_Item => Create_List (Value_Choice'(Controlled with
                       VType => Hex_Array_Type,
                       Hex   => Hex_String_To_Byte_Array (To_String (Value)))));

            end case;
         end if;
      else
         raise Schema_Error with "Unknown key";
      end if;
   end Add_Test_Vector_Key_Value_Pair;

   ------------------------
   -- Append_Test_Vector --
   ------------------------

   procedure Append_Test_Vector (Test_Vector  : in out Test_Vector_Maps.Map;
                                 Vectors_List : in out Lists.List;
                                 Schema       : in     Schema_Maps.Map)
   is
   begin
      if not Test_Vector_Maps.Is_Empty (Test_Vector) then

         --  Check if there are any missing required schema values
         for C in Schema.Iterate loop
            if Schema_Maps.Element (C).Required and
              not Test_Vector.Contains (Schema_Maps.Key (C))
            then
               raise Schema_Error with "Missing required key";
            end if;
         end loop;

         Lists.Append (Vectors_List, Test_Vector);
      end if;
   end Append_Test_Vector;

end Test_Vectors;