aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/libgnat/g-shshco.adb
blob: e6207e0839bada321f9dae12d295e68c5b6a2452 (plain)
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
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT LIBRARY COMPONENTS                          --
--                                                                          --
--       G N A T . S E C U R E _ H A S H E S . S H A 2 _ C O M M O N        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--           Copyright (C) 2009-2024, Free Software Foundation, Inc.        --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

package body GNAT.Secure_Hashes.SHA2_Common is

   ---------------
   -- Transform --
   ---------------

   procedure Transform
     (H_St : in out Hash_State.State;
      M_St : in out Message_State)
   is
      use System;

      subtype Word is Hash_State.Word;
      use type Hash_State.Word;

      function Ch (X, Y, Z : Word) return Word;
      function Maj (X, Y, Z : Word) return Word;
      pragma Inline (Ch, Maj);
      --  Elementary functions from FIPS PUB 180-3

      --------
      -- Ch --
      --------

      function Ch (X, Y, Z : Word) return Word is
      begin
         return (X and Y) xor ((not X) and Z);
      end Ch;

      ---------
      -- Maj --
      ---------

      function Maj (X, Y, Z : Word) return Word is
      begin
         return (X and Y) xor (X and Z) xor (Y and Z);
      end Maj;

      type Words is array (Natural range <>) of Word;

      X : Words (0 .. 15);
      for X'Address use M_St.Buffer'Address;
      pragma Import (Ada, X);

      W : Words (0 .. Rounds - 1);

      A, B, C, D, E, F, G, H, T1, T2 : Word;

   --  Start of processing for Transform

   begin
      if Default_Bit_Order /= High_Order_First then
         for J in X'Range loop
            Hash_State.Swap (X (J)'Address);
         end loop;
      end if;

      --  1. Prepare message schedule

      W (0 .. 15) := X;

      for T in 16 .. Rounds - 1 loop
         W (T) := S1 (W (T - 2)) + W (T - 7) + S0 (W (T - 15)) + W (T - 16);
      end loop;

      --  2. Initialize working variables

      A := H_St (0);
      B := H_St (1);
      C := H_St (2);
      D := H_St (3);
      E := H_St (4);
      F := H_St (5);
      G := H_St (6);
      H := H_St (7);

      --  3. Perform transformation rounds

      for T in 0 .. Rounds - 1 loop
         T1 := H + Sigma1 (E) + Ch (E, F, G)
             + K (Stream_Element_Offset (T)) + W (T);
         T2 := Sigma0 (A) + Maj (A, B, C);
         H := G;
         G := F;
         F := E;
         E := D + T1;
         D := C;
         C := B;
         B := A;
         A := T1 + T2;
      end loop;

      --  4. Update hash state

      H_St (0) := A + H_St (0);
      H_St (1) := B + H_St (1);
      H_St (2) := C + H_St (2);
      H_St (3) := D + H_St (3);
      H_St (4) := E + H_St (4);
      H_St (5) := F + H_St (5);
      H_St (6) := G + H_St (6);
      H_St (7) := H + H_St (7);
   end Transform;

end GNAT.Secure_Hashes.SHA2_Common;