1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- G N A T . S E C U R E _ H A S H E S . S H A 2 _ 3 2 --
9 -- Copyright (C) 2009-2023, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This package provides support for the 32-bit FIPS PUB 180-3 functions
33 -- SHA-224 and SHA-256.
35 -- This is an internal unit and should not be used directly in applications.
36 -- Use GNAT.SHA224 and GNAT.SHA256 instead.
39 with GNAT
.Byte_Swapping
;
40 with GNAT
.Secure_Hashes
.SHA2_Common
;
42 package GNAT
.Secure_Hashes
.SHA2_32
is
44 subtype Word
is Interfaces
.Unsigned_32
;
46 package Hash_State
is new Hash_Function_State
48 Swap
=> GNAT
.Byte_Swapping
.Swap4
,
49 Hash_Bit_Order
=> System
.High_Order_First
);
50 -- SHA-224 and SHA-256 operate on 32-bit big endian words
52 K
: constant Hash_State
.State
(0 .. 63) :=
53 [16#
428a2f98#
, 16#
71374491#
, 16#b5c0fbcf#
, 16#e9b5dba5#
,
54 16#
3956c25b#
, 16#
59f111f1#
, 16#
923f82a4#
, 16#ab1c5ed5#
,
55 16#d807aa98#
, 16#
12835b01#
, 16#
243185be#
, 16#
550c7dc3#
,
56 16#
72be5d74#
, 16#
80deb1fe#
, 16#
9bdc06a7#
, 16#c19bf174#
,
57 16#e49b69c1#
, 16#efbe4786#
, 16#
0fc19dc6#
, 16#
240ca1cc#
,
58 16#
2de92c6f#
, 16#
4a7484aa#
, 16#
5cb0a9dc#
, 16#
76f988da#
,
59 16#
983e5152#
, 16#a831c66d#
, 16#b00327c8#
, 16#bf597fc7#
,
60 16#c6e00bf3#
, 16#d5a79147#
, 16#
06ca6351#
, 16#
14292967#
,
61 16#
27b70a85#
, 16#
2e1b2138#
, 16#
4d2c6dfc#
, 16#
53380d13#
,
62 16#
650a7354#
, 16#
766a0abb#
, 16#
81c2c92e#
, 16#
92722c85#
,
63 16#a2bfe8a1#
, 16#a81a664b#
, 16#c24b8b70#
, 16#c76c51a3#
,
64 16#d192e819#
, 16#d6990624#
, 16#f40e3585#
, 16#
106aa070#
,
65 16#
19a4c116#
, 16#
1e376c08#
, 16#
2748774c#
, 16#
34b0bcb5#
,
66 16#
391c0cb3#
, 16#
4ed8aa4a#
, 16#
5b9cca4f#
, 16#
682e6ff3#
,
67 16#
748f82ee#
, 16#
78a5636f#
, 16#
84c87814#
, 16#
8cc70208#
,
68 16#
90befffa#
, 16#a4506ceb#
, 16#bef9a3f7#
, 16#c67178f2#
];
69 -- Constants from FIPS PUB 180-3
71 function Sigma0
(X
: Word
) return Word
;
72 function Sigma1
(X
: Word
) return Word
;
73 function S0
(X
: Word
) return Word
;
74 function S1
(X
: Word
) return Word
;
75 pragma Inline
(Sigma0
, Sigma1
, S0
, S1
);
76 -- Elementary functions Sigma^256_0, Sigma^256_1, sigma^256_0, sigma^256_1
77 -- from FIPS PUB 180-3.
79 procedure Transform
is new SHA2_Common
.Transform
80 (Hash_State
=> Hash_State
,
88 SHA224_Init_State
: constant Hash_State
.State
(0 .. 7) :=
97 SHA256_Init_State
: constant Hash_State
.State
(0 .. 7) :=
106 -- Initialization vectors from FIPS PUB 180-3
108 end GNAT
.Secure_Hashes
.SHA2_32
;