OSDN Git Service

2009-11-30 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-sechas.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --                 S Y S T E M . S E C U R E _ H A S H E S                  --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --           Copyright (C) 2009, Free Software Foundation, Inc.             --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- GNAT was originally developed  by the GNAT team at  New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 with System;     use System;
33 with Interfaces; use Interfaces;
34
35 package body System.Secure_Hashes is
36
37    use Ada.Streams;
38
39    Hex_Digit : constant array (Stream_Element range 0 .. 15) of Character :=
40                  ('0', '1', '2', '3', '4', '5', '6', '7',
41                   '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
42
43    type Fill_Buffer_Access is
44      access procedure
45        (M     : in out Message_State;
46         S     : String;
47         First : Natural;
48         Last  : out Natural);
49    --  A procedure to transfer data from S into M's block buffer until either
50    --  the block buffer is full or all data from S has been consumed.
51
52    procedure Fill_Buffer_Copy
53      (M     : in out Message_State;
54       S     : String;
55       First : Natural;
56       Last  : out Natural);
57    --  Transfer procedure which just copies data from S to M
58
59    procedure Fill_Buffer_Swap
60      (M     : in out Message_State;
61       S     : String;
62       First : Natural;
63       Last  : out Natural);
64    --  Transfer procedure which swaps bytes from S when copying into M
65
66    procedure To_String (SEA : Stream_Element_Array; S : out String);
67    --  Return the hexadecimal representation of SEA
68
69    ----------------------
70    -- Fill_Buffer_Copy --
71    ----------------------
72
73    procedure Fill_Buffer_Copy
74      (M     : in out Message_State;
75       S     : String;
76       First : Natural;
77       Last  : out Natural)
78    is
79       Buf_String : String (M.Buffer'Range);
80       for Buf_String'Address use M.Buffer'Address;
81       pragma Import (Ada, Buf_String);
82
83       Length : constant Natural :=
84                  Natural'Min (M.Block_Length - M.Last, S'Last - First + 1);
85
86    begin
87       pragma Assert (Length > 0);
88
89       Buf_String (M.Last + 1 .. M.Last + Length) :=
90         S (First .. First + Length - 1);
91       M.Last := M.Last + Length;
92       Last := First + Length - 1;
93    end Fill_Buffer_Copy;
94
95    ----------------------
96    -- Fill_Buffer_Swap --
97    ----------------------
98
99    procedure Fill_Buffer_Swap
100      (M     : in out Message_State;
101       S     : String;
102       First : Natural;
103       Last  : out Natural)
104    is
105       Length : constant Natural :=
106                   Natural'Min (M.Block_Length - M.Last, S'Last - First + 1);
107    begin
108       Last := First;
109       while Last - First < Length loop
110          M.Buffer (M.Last + 1 + Last - First) :=
111             (if (Last - First) mod 2 = 0 then S (Last + 1) else S (Last - 1));
112          Last := Last + 1;
113       end loop;
114       M.Last := M.Last + Length;
115       Last := First + Length - 1;
116    end Fill_Buffer_Swap;
117
118    ---------------
119    -- To_String --
120    ---------------
121
122    procedure To_String (SEA : Stream_Element_Array; S : out String) is
123       pragma Assert (S'Length = 2 * SEA'Length);
124    begin
125       for J in SEA'Range loop
126          declare
127             S_J : constant Natural := 1 + Natural (J - SEA'First) * 2;
128          begin
129             S (S_J)     := Hex_Digit (SEA (J) / 16);
130             S (S_J + 1) := Hex_Digit (SEA (J) mod 16);
131          end;
132       end loop;
133    end To_String;
134
135    -------
136    -- H --
137    -------
138
139    package body H is
140
141       procedure Update
142         (C           : in out Context;
143          S           : String;
144          Fill_Buffer : Fill_Buffer_Access);
145       --  Internal common routine for all Update procedures
146
147       procedure Final
148         (C         : Context;
149          Hash_Bits : out Ada.Streams.Stream_Element_Array);
150       --  Perform final hashing operations (data padding) and extract the
151       --  (possibly truncated) state of C into Hash_Bits.
152
153       ------------
154       -- Digest --
155       ------------
156
157       function Digest (C : Context) return Message_Digest is
158          Hash_Bits : Stream_Element_Array
159                        (1 .. Stream_Element_Offset (Hash_Length));
160       begin
161          Final (C, Hash_Bits);
162          return MD : Message_Digest do
163             To_String (Hash_Bits, MD);
164          end return;
165       end Digest;
166
167       function Digest (S : String) return Message_Digest is
168          C : Context;
169       begin
170          Update (C, S);
171          return Digest (C);
172       end Digest;
173
174       function Digest (A : Stream_Element_Array) return Message_Digest is
175          C : Context;
176       begin
177          Update (C, A);
178          return Digest (C);
179       end Digest;
180
181       -----------
182       -- Final --
183       -----------
184
185       --  Once a complete message has been processed, it is padded with one
186       --  1 bit followed by enough 0 bits so that the last block is
187       --  2 * Word'Size bits short of being completed. The last 2 * Word'Size
188       --  bits are set to the message size in bits (excluding padding).
189
190       procedure Final
191         (C          : Context;
192          Hash_Bits  : out Stream_Element_Array)
193       is
194          FC : Context := C;
195
196          Zeroes : Natural;
197          --  Number of 0 bytes in padding
198
199          Message_Length : Unsigned_64 := FC.M_State.Length;
200          --  Message length in bytes
201
202          Size_Length : constant Natural :=
203                          2 * Hash_State.Word'Size / 8;
204          --  Length in bytes of the size representation
205
206       begin
207          Zeroes := (Block_Length - 1 - Size_Length - FC.M_State.Last)
208                      mod FC.M_State.Block_Length;
209          declare
210             Pad : String (1 .. 1 + Zeroes + Size_Length) :=
211                     (1 => Character'Val (128), others => ASCII.NUL);
212
213             Index       : Natural;
214             First_Index : Natural;
215
216          begin
217             First_Index := (if Hash_Bit_Order = Low_Order_First
218                             then Pad'Last - Size_Length + 1
219                             else Pad'Last);
220
221             Index := First_Index;
222             while Message_Length > 0 loop
223                if Index = First_Index then
224
225                   --  Message_Length is in bytes, but we need to store it as
226                   --  a bit count).
227
228                   Pad (Index) := Character'Val
229                                    (Shift_Left (Message_Length and 16#1f#, 3));
230                   Message_Length := Shift_Right (Message_Length, 5);
231
232                else
233                   Pad (Index) := Character'Val (Message_Length and 16#ff#);
234                   Message_Length := Shift_Right (Message_Length, 8);
235                end if;
236
237                Index := Index +
238                           (if Hash_Bit_Order = Low_Order_First then 1 else -1);
239             end loop;
240
241             Update (FC, Pad);
242          end;
243
244          pragma Assert (FC.M_State.Last = 0);
245
246          Hash_State.To_Hash (FC.H_State, Hash_Bits);
247       end Final;
248
249       ------------
250       -- Update --
251       ------------
252
253       procedure Update
254         (C           : in out Context;
255          S           : String;
256          Fill_Buffer : Fill_Buffer_Access)
257       is
258          Last : Natural := S'First - 1;
259
260       begin
261          C.M_State.Length := C.M_State.Length + S'Length;
262
263          while Last < S'Last loop
264             Fill_Buffer (C.M_State, S, Last + 1, Last);
265
266             if C.M_State.Last = Block_Length then
267                Transform (C.H_State, C.M_State);
268                C.M_State.Last := 0;
269             end if;
270          end loop;
271
272       end Update;
273
274       ------------
275       -- Update --
276       ------------
277
278       procedure Update (C : in out Context; Input : String) is
279       begin
280          Update (C, Input, Fill_Buffer_Copy'Access);
281       end Update;
282
283       ------------
284       -- Update --
285       ------------
286
287       procedure Update (C : in out Context; Input : Stream_Element_Array) is
288          S : String (1 .. Input'Length);
289          for S'Address use Input'Address;
290          pragma Import (Ada, S);
291       begin
292          Update (C, S, Fill_Buffer_Copy'Access);
293       end Update;
294
295       -----------------
296       -- Wide_Update --
297       -----------------
298
299       procedure Wide_Update (C : in out Context; Input : Wide_String) is
300          S : String (1 .. 2 * Input'Length);
301          for S'Address use Input'Address;
302          pragma Import (Ada, S);
303       begin
304          Update
305            (C, S,
306             (if System.Default_Bit_Order /= Low_Order_First
307              then Fill_Buffer_Swap'Access
308              else Fill_Buffer_Copy'Access));
309       end Wide_Update;
310
311       -----------------
312       -- Wide_Digest --
313       -----------------
314
315       function Wide_Digest (W : Wide_String) return Message_Digest is
316          C : Context;
317       begin
318          Wide_Update (C, W);
319          return Digest (C);
320       end Wide_Digest;
321
322    end H;
323
324    -------------------------
325    -- Hash_Function_State --
326    -------------------------
327
328    package body Hash_Function_State is
329
330       -------------
331       -- To_Hash --
332       -------------
333
334       procedure To_Hash (H : State; H_Bits : out Stream_Element_Array) is
335          Hash_Words : constant Natural := H'Size / Word'Size;
336          Result     : State (1 .. Hash_Words) :=
337                         H (H'Last - Hash_Words + 1 .. H'Last);
338
339          R_SEA : Stream_Element_Array (1 .. Result'Size / 8);
340          for R_SEA'Address use Result'Address;
341          pragma Import (Ada, R_SEA);
342
343       begin
344          if System.Default_Bit_Order /= Hash_Bit_Order then
345             for J in Result'Range loop
346                Swap (Result (J)'Address);
347             end loop;
348          end if;
349
350          --  Return truncated hash
351
352          pragma Assert (H_Bits'Length <= R_SEA'Length);
353          H_Bits := R_SEA (R_SEA'First .. R_SEA'First + H_Bits'Length - 1);
354       end To_Hash;
355
356    end Hash_Function_State;
357
358 end System.Secure_Hashes;