| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT LIBRARY COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G N A T . S E C U R E _ H A S H E S . S H A 2 _ 3 2 -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2009-2025, 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 |
-- This package provides support for the 32-bit FIPS PUB 180-3 functions |
| 33 |
-- SHA-224 and SHA-256. |
| 34 |
|
| 35 |
-- This is an internal unit and should not be used directly in applications. |
| 36 |
-- Use GNAT.SHA224 and GNAT.SHA256 instead. |
| 37 |
|
| 38 |
with Interfaces; |
| 39 |
with GNAT.Byte_Swapping; |
| 40 |
with GNAT.Secure_Hashes.SHA2_Common; |
| 41 |
|
| 42 |
package GNAT.Secure_Hashes.SHA2_32 is |
| 43 |
|
| 44 |
subtype Word is Interfaces.Unsigned_32; |
| 45 |
|
| 46 |
package Hash_State is new Hash_Function_State |
| 47 |
(Word => Word, |
| 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 |
| 51 |
|
| 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 |
| 70 |
|
| 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. |
| 78 |
|
| 79 |
procedure Transform is new SHA2_Common.Transform |
| 80 |
(Hash_State => Hash_State, |
| 81 |
K => K, |
| 82 |
Rounds => 64, |
| 83 |
Sigma0 => Sigma0, |
| 84 |
Sigma1 => Sigma1, |
| 85 |
S0 => S0, |
| 86 |
S1 => S1); |
| 87 |
|
| 88 |
SHA224_Init_State : constant Hash_State.State (0 .. 7) := |
| 89 |
[0 => 16#c1059ed8#, |
| 90 |
1 => 16#367cd507#, |
| 91 |
2 => 16#3070dd17#, |
| 92 |
3 => 16#f70e5939#, |
| 93 |
4 => 16#ffc00b31#, |
| 94 |
5 => 16#68581511#, |
| 95 |
6 => 16#64f98fa7#, |
| 96 |
7 => 16#befa4fa4#]; |
| 97 |
SHA256_Init_State : constant Hash_State.State (0 .. 7) := |
| 98 |
[0 => 16#6a09e667#, |
| 99 |
1 => 16#bb67ae85#, |
| 100 |
2 => 16#3c6ef372#, |
| 101 |
3 => 16#a54ff53a#, |
| 102 |
4 => 16#510e527f#, |
| 103 |
5 => 16#9b05688c#, |
| 104 |
6 => 16#1f83d9ab#, |
| 105 |
7 => 16#5be0cd19#]; |
| 106 |
-- Initialization vectors from FIPS PUB 180-3 |
| 107 |
|
| 108 |
end GNAT.Secure_Hashes.SHA2_32; |