-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsha_512_pkg.vhdl
135 lines (114 loc) · 5.81 KB
/
sha_512_pkg.vhdl
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
--MIT License
--
--Copyright (c) 2017 Danny Savory
--
--Permission is hereby granted, free of charge, to any person obtaining a copy
--of this software and associated documentation files (the "Software"), to deal
--in the Software without restriction, including without limitation the rights
--to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
--copies of the Software, and to permit persons to whom the Software is
--furnished to do so, subject to the following conditions:
--
--The above copyright notice and this permission notice shall be included in all
--copies or substantial portions of the Software.
--
--THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
--AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
--OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
--SOFTWARE.
-- ############################################################################
-- The official specifications of the SHA-256 algorithm can be found here:
-- http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
package sha_512_pkg is
constant WORD_SIZE : natural := 64; --SHA-512 uses 64-bit words
--array types for SHA-512
type K_DATA is array (0 to 79) of std_logic_vector(WORD_SIZE-1 downto 0);
type M_DATA is array (0 to 15) of std_logic_vector(WORD_SIZE-1 downto 0);
type H_DATA is array (0 to 7) of std_logic_vector(WORD_SIZE-1 downto 0);
--Message blocks, the padded message should be a multiple of 512 bits,
signal M : M_DATA;
--function definitions
function ROTR (a : std_logic_vector(WORD_SIZE-1 downto 0); n : natural)
return std_logic_vector;
function ROTL (a : std_logic_vector(WORD_SIZE-1 downto 0); n : natural)
return std_logic_vector;
function SHR (a : std_logic_vector(WORD_SIZE-1 downto 0); n : natural)
return std_logic_vector;
function CH (x : std_logic_vector(WORD_SIZE-1 downto 0);
y : std_logic_vector(WORD_SIZE-1 downto 0);
z : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector;
function MAJ (x : std_logic_vector(WORD_SIZE-1 downto 0);
y : std_logic_vector(WORD_SIZE-1 downto 0);
z : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector;
function SIGMA_UCASE_0 (x : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector;
function SIGMA_UCASE_1 (x : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector;
function SIGMA_LCASE_0 (x : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector;
function SIGMA_LCASE_1 (x : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector;
end package;
package body sha_512_pkg is
function ROTR (a : std_logic_vector(WORD_SIZE-1 downto 0); n : natural)
return std_logic_vector is
--result : std_logic_vector(WORD_SIZE-1 downto 0);
begin
--signal result : std_logic_vector(WORD_SIZE-1 downto 0);
return (std_logic_vector(shift_right(unsigned(a), n))) or std_logic_vector((shift_left(unsigned(a), (WORD_SIZE-n))));
end function;
function ROTL (a : std_logic_vector(WORD_SIZE-1 downto 0); n : natural)
return std_logic_vector is
--result : std_logic_vector(WORD_SIZE-1 downto 0);
begin
--signal result : std_logic_vector(WORD_SIZE-1 downto 0);
return (std_logic_vector(shift_left(unsigned(a), n))) or std_logic_vector((shift_right(unsigned(a), (WORD_SIZE-n))));
end function;
function SHR (a : std_logic_vector(WORD_SIZE-1 downto 0); n : natural)
return std_logic_vector is
begin
return std_logic_vector(shift_right(unsigned(a), n));
end function;
function CH (x : std_logic_vector(WORD_SIZE-1 downto 0);
y : std_logic_vector(WORD_SIZE-1 downto 0);
z : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector is
begin
return (x and y) xor (not(x) and z);
end function;
function MAJ (x : std_logic_vector(WORD_SIZE-1 downto 0);
y : std_logic_vector(WORD_SIZE-1 downto 0);
z : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector is
begin
return (x and y) xor (x and z) xor (y and z);
end function;
function SIGMA_UCASE_0 (x : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector is
begin
return ROTR(x, 28) xor ROTR(x, 34) xor ROTR(x, 39);
end function;
function SIGMA_UCASE_1 (x : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector is
begin
return ROTR(x, 14) xor ROTR(x, 18) xor ROTR(x, 41);
end function;
function SIGMA_LCASE_0 (x : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector is
begin
return ROTR(x, 1) xor ROTR(x, 8) xor SHR(x, 7);
end function;
function SIGMA_LCASE_1 (x : std_logic_vector(WORD_SIZE-1 downto 0))
return std_logic_vector is
begin
return ROTR(x, 19) xor ROTR(x, 61) xor SHR(x, 6);
end function;
end package body;