Natools

Check-in [29a0283a2f]
Login
Overview
Comment:hmac_tests: fully-covering test suite for HMAC
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 29a0283a2fa79288498138f5acf419d0e5112aae
User & Date: nat on 2014-04-06 17:01:42
Other Links: manifest | tags
Context
2014-04-07
19:04
tools: new directory for command-line tools using directly Natools library, seeded with HMAC tools check-in: 4d8481daa6 user: nat tags: trunk
2014-04-06
17:01
hmac_tests: fully-covering test suite for HMAC check-in: 29a0283a2f user: nat tags: trunk
2014-04-05
17:08
natools-gnat_hmac-*: new packages instances of Natools.HMAC with NAT-provided hash functions check-in: 20773f86ed user: nat tags: trunk
Changes

Added tests/natools-hmac_tests.adb version [6636abbe1d].



































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
------------------------------------------------------------------------------
-- Copyright (c) 2014, Natacha Porté                                        --
--                                                                          --
-- Permission to use, copy, modify, and distribute this software for any    --
-- purpose with or without fee is hereby granted, provided that the above   --
-- copyright notice and this permission notice appear in all copies.        --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         --
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  --
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   --
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    --
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

with Natools.GNAT_HMAC.MD5;
with Natools.GNAT_HMAC.SHA1;
with Natools.GNAT_HMAC.SHA256;
with Natools.S_Expressions.Encodings;
with Natools.S_Expressions.Test_Tools;

package body Natools.HMAC_Tests is

   package Test_Tools renames Natools.S_Expressions.Test_Tools;

   function Hex_Atom (Hexadecimal : String) return S_Expressions.Atom;


   ------------------------------
   -- Local Helper Subprograms --
   ------------------------------

   function Hex_Atom (Hexadecimal : String) return S_Expressions.Atom is
   begin
      return S_Expressions.Encodings.Decode_Hex
        (S_Expressions.To_Atom (Hexadecimal));
   end Hex_Atom;



   -------------------------
   -- Complete Test Suite --
   -------------------------

   procedure All_Tests (Report : in out NT.Reporter'Class) is
   begin
      RFC_2104 (Report);
      RFC_4231 (Report);
      Wikipedia (Report);
   end All_Tests;


   -----------------------
   -- Inidividual Tests --
   -----------------------

   procedure RFC_2104 (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("HMAC-MD5 test vectors from RFC-2104");
   begin
      Test_Tools.Test_Atom
        (Test,
         Hex_Atom ("9294727a3638bb1c13f48ef8158bfc9d"),
         GNAT_HMAC.MD5.Digest
           (S_Expressions.Atom'(1 .. 16 => 16#0b#),
            S_Expressions.To_Atom ("Hi There")));

      Test_Tools.Test_Atom
        (Test,
         Hex_Atom ("750c783e6ab0b503eaa86e310a5db738"),
         GNAT_HMAC.MD5.Digest
           ("Jefe",
            S_Expressions.To_Atom ("what do ya want for nothing?")));

      Test_Tools.Test_Atom
        (Test,
         Hex_Atom ("56be34521d144c88dbb8c733f0e8b3f6"),
         GNAT_HMAC.MD5.Digest
           (S_Expressions.Atom'(1 .. 16 => 16#aa#),
            (1 .. 50 => 16#dd#)));
   exception
      when Error : others => Test.Report_Exception (Error);
   end RFC_2104;


   procedure RFC_4231 (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("HMAC-SHA256 test vectors from RFC-4231");
   begin
      declare
         Context : GNAT_HMAC.SHA256.Context;
      begin
         GNAT_HMAC.SHA256.Setup (Context,
            S_Expressions.Atom'(1 .. 20 => 16#0b#));
         GNAT_HMAC.SHA256.Update (Context, "Hi There");
         Test_Tools.Test_Atom
           (Test,
            Hex_Atom ("b0344c61d8db38535ca8afceaf0bf12b"
                    & "881dc200c9833da726e9376c2e32cff7"),
            GNAT_HMAC.SHA256.Digest (Context));

         GNAT_HMAC.SHA256.Setup (Context, "Jefe");
         GNAT_HMAC.SHA256.Update (Context, "what do ya want for nothing?");
         Test_Tools.Test_Atom
           (Test,
            Hex_Atom ("5bdcc146bf60754e6a042426089575c7"
                    & "5a003f089d2739839dec58b964ec3843"),
            GNAT_HMAC.SHA256.Digest (Context));

         GNAT_HMAC.SHA256.Setup (Context,
            S_Expressions.Atom'(1 .. 20 => 16#aa#));
         GNAT_HMAC.SHA256.Update (Context,
            S_Expressions.Atom'(1 .. 50 => 16#dd#));
         Test_Tools.Test_Atom
           (Test,
            Hex_Atom ("773ea91e36800e46854db8ebd09181a7"
                    & "2959098b3ef8c122d9635514ced565fe"),
            GNAT_HMAC.SHA256.Digest (Context));

         GNAT_HMAC.SHA256.Setup (Context,
            Hex_Atom ("0102030405060708090a0b0c0d0e0f10111213141516171819"));
         GNAT_HMAC.SHA256.Update (Context,
            S_Expressions.Atom'(1 .. 50 => 16#CD#));
         Test_Tools.Test_Atom
           (Test,
            Hex_Atom ("82558a389a443c0ea4cc819899f2083a"
                    & "85f0faa3e578f8077a2e3ff46729665b"),
            GNAT_HMAC.SHA256.Digest (Context));

         GNAT_HMAC.SHA256.Setup (Context,
            S_Expressions.Atom'(1 .. 131 => 16#AA#));
         GNAT_HMAC.SHA256.Update (Context,
            "Test Using Larger Than Block-Size Key - Hash Key First");
         Test_Tools.Test_Atom
           (Test,
            Hex_Atom ("60e431591ee0b67f0d8a26aacbf5b77f"
                    & "8e0bc6213728c5140546040f0ee37f54"),
            GNAT_HMAC.SHA256.Digest (Context));

         GNAT_HMAC.SHA256.Setup (Context,
            S_Expressions.Atom'(1 .. 131 => 16#aa#));
         GNAT_HMAC.SHA256.Update (Context,
            "This is a test using a larger than block-size key and a larger"
            & " than block-size data. The key needs to be hashed before"
            & " being used by the HMAC algorithm.");
         Test_Tools.Test_Atom
           (Test,
            Hex_Atom ("9b09ffa71b942fcb27635fbcd5b0e944"
                    & "bfdc63644f0713938a7f51535c3a35e2"),
            GNAT_HMAC.SHA256.Digest (Context));
      end;
   exception
      when Error : others => Test.Report_Exception (Error);
   end RFC_4231;


   procedure Wikipedia (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("Test vectors from Wikipedia");
   begin
      Test_Tools.Test_Atom
        (Test,
         Hex_Atom ("74e6f7298a9c2d168935f58c001bad88"),
         GNAT_HMAC.MD5.Digest
           (S_Expressions.Null_Atom,
            S_Expressions.Null_Atom));

      Test_Tools.Test_Atom
        (Test,
         Hex_Atom ("80070713463e7749b90c2dc24911e275"),
         GNAT_HMAC.MD5.Digest
           ("key",
            S_Expressions.To_Atom
              ("The quick brown fox jumps over the lazy dog")));

      Test_Tools.Test_Atom
        (Test,
         Hex_Atom ("fbdb1d1b18aa6c08324b7d64b71fb76370690e1d"),
         GNAT_HMAC.SHA1.Digest
           (S_Expressions.Null_Atom,
            S_Expressions.Null_Atom));

      Test_Tools.Test_Atom
        (Test,
         Hex_Atom ("de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"),
         GNAT_HMAC.SHA1.Digest
           ("key",
            S_Expressions.To_Atom
              ("The quick brown fox jumps over the lazy dog")));

      Test_Tools.Test_Atom
        (Test,
         Hex_Atom ("b613679a0814d9ec772f95d778c35fc5"
                 & "ff1697c493715653c6c712144292c5ad"),
         GNAT_HMAC.SHA256.Digest
           (S_Expressions.Null_Atom,
            S_Expressions.Null_Atom));

      Test_Tools.Test_Atom
        (Test,
         Hex_Atom ("f7bc83f430538424b13298e6aa6fb143"
                 & "ef4d59a14946175997479dbc2d1a3cd8"),
         GNAT_HMAC.SHA256.Digest
           ("key",
            S_Expressions.To_Atom
              ("The quick brown fox jumps over the lazy dog")));
   exception
      when Error : others => Test.Report_Exception (Error);
   end Wikipedia;

end Natools.HMAC_Tests;

Added tests/natools-hmac_tests.ads version [11c3433bb3].





































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
------------------------------------------------------------------------------
-- Copyright (c) 2014, Natacha Porté                                        --
--                                                                          --
-- Permission to use, copy, modify, and distribute this software for any    --
-- purpose with or without fee is hereby granted, provided that the above   --
-- copyright notice and this permission notice appear in all copies.        --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         --
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  --
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   --
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    --
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

------------------------------------------------------------------------------
-- Natools.HMAC_Tests provides tests for Natools.HMAC hash-based message    --
-- authentication codes, using test vectors against provided instances.     --
------------------------------------------------------------------------------

with Natools.Tests;

package Natools.HMAC_Tests is

   package NT renames Natools.Tests;

   procedure All_Tests (Report : in out NT.Reporter'Class);

   procedure RFC_2104 (Report : in out NT.Reporter'Class);
   procedure RFC_4231 (Report : in out NT.Reporter'Class);
   procedure Wikipedia (Report : in out NT.Reporter'Class);

end Natools.HMAC_Tests;

Modified tests/test_all.adb from [a5c3ef0e6c] to [cf090ebe77].

18
19
20
21
22
23
24

25
26
27
28
29
30
31
-- Test_All is a binary gathering all tests from Natools components. --
-----------------------------------------------------------------------

with Ada.Command_Line;
with Ada.Text_IO;
with Natools.Chunked_Strings.Tests;
with Natools.Getopt_Long_Tests;

with Natools.Reference_Tests;
with Natools.S_Expressions.Atom_Buffers.Tests;
with Natools.S_Expressions.Cache_Tests;
with Natools.S_Expressions.Encodings.Tests;
with Natools.S_Expressions.Interpreter_Tests;
with Natools.S_Expressions.Lockable.Tests;
with Natools.S_Expressions.Parsers.Tests;







>







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- Test_All is a binary gathering all tests from Natools components. --
-----------------------------------------------------------------------

with Ada.Command_Line;
with Ada.Text_IO;
with Natools.Chunked_Strings.Tests;
with Natools.Getopt_Long_Tests;
with Natools.HMAC_Tests;
with Natools.Reference_Tests;
with Natools.S_Expressions.Atom_Buffers.Tests;
with Natools.S_Expressions.Cache_Tests;
with Natools.S_Expressions.Encodings.Tests;
with Natools.S_Expressions.Interpreter_Tests;
with Natools.S_Expressions.Lockable.Tests;
with Natools.S_Expressions.Parsers.Tests;
68
69
70
71
72
73
74




75
76
77
78
79
80
81
   Report.Section ("Chunked_String with single allocation unit");
   Single_Chunked_Strings_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("Getopt_Long");
   Natools.Getopt_Long_Tests.All_Tests (Report);
   Report.End_Section;





   Report.Section ("References");
   Natools.Reference_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("S_Expressions.Atom_Buffers");
   Natools.S_Expressions.Atom_Buffers.Tests.All_Tests (Report);







>
>
>
>







69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
   Report.Section ("Chunked_String with single allocation unit");
   Single_Chunked_Strings_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("Getopt_Long");
   Natools.Getopt_Long_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("HMAC and GNAT_HMAC");
   Natools.HMAC_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("References");
   Natools.Reference_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("S_Expressions.Atom_Buffers");
   Natools.S_Expressions.Atom_Buffers.Tests.All_Tests (Report);