SyoSil ApS UVM Scoreboard  1.0.3.0
cl_syoscb_hash_md5.svh
1 /// MD5 hash algorithm implementation. The class implements the hash API as defined
2 /// by the hash base class.
3 class cl_syoscb_hash_md5 extends cl_syoscb_hash_base#(pk_syoscb::MD5_HASH_DIGEST_WIDTH);
4  //Used in the internal hashing functions
5  typedef bit [31:0] M;
6  //-------------------------------------
7  // Non randomizable variables
8  //-------------------------------------
9 
10  //-------------------------------------
11  // UVM Macros
12  //-------------------------------------
13  `uvm_object_utils_begin(cl_syoscb_hash_md5)
14  `uvm_object_utils_end
15 
16  //-------------------------------------
17  // Constructor
18  //-------------------------------------
19  extern function new(string name = "cl_syoscb_hash_md5");
20 
21  //-------------------------------------
22  // Hash API
23  //-------------------------------------
24  // Basic hash functions
25  extern protected virtual function tp_hash_digest do_hash(bit ser []);
26 
27  // Support functions
28  extern virtual local function bit [511:0] get_next_512_word(int unsigned word_idx,bit ser[]);
29  extern virtual local function M [15:0] get_next_32_word(bit [511:0]word);
30 endclass: cl_syoscb_hash_md5
31 
32 function cl_syoscb_hash_md5::new(string name = "cl_syoscb_hash_md5");
34  super.new(name);
35 
36  p = new;
37  this.packer = p;
38 endfunction: new
39 
40 /// <b>Hash API:</b> See cl_syoscb_hash_base#do_hash for more details
41 /// Expects a bitstream with a length which is a multiple of 512, which
42 /// follows the below format, specified in RFC 1321:
43 /// <table>
44 /// <tr>
45 /// <th>Bits</th>
46 /// <th>Information</th>
47 /// </tr>
48 /// <tr>
49 /// <td> 0 - length of the serialized item-1</td>
50 /// <td> Serialized item</td>
51 /// </tr>
52 /// <tr>
53 /// <td> length of the serialized item - lentgh of the serialized item+6</td>
54 /// <td> Zeros</td>
55 /// </tr>
56 /// <tr>
57 /// <td>length of the serialized item+7</td>
58 /// <td> One </td>
59 /// </tr>
60 /// <tr>
61 /// <td>length of the serialized item+8 - (512*x-64)</td>
62 /// <td> Zeros</td>
63 /// </tr>
64 /// <tr>
65 /// <td> last 64 bits</td>
66 /// <td> length of the item modulo 2^64</td>
67 /// </tr>
68 /// </table>
69 /// A bitstream of this kind can be generated by packing with cl_syoscb_md5_packer,
70 /// which automatically appends the required metadata to the end of the bitstream
72  // implement md5 algorithm
73  int unsigned s [64];
74  int unsigned K [64];
75  int i;
76  int unsigned a0;
77  int unsigned b0;
78  int unsigned c0;
79  int unsigned d0;
80  bit [31:0] a0d;
81  bit [31:0] b0d;
82  bit [31:0] c0d;
83  bit [31:0] d0d;
84  int p;
85  int q;
86  int unsigned A;
87  int unsigned B;
88  int unsigned C;
89  int unsigned D;
90  bit [63:0]length;
91  int lmsg;
92  int j;
93  int unsigned F;
94  int g;
95  int l;
96  bit [511:0] word;
97  M [15:0] mWords;
98 
99  //s specifies the per-round shift amounts
100  s[ 0:15] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22 };
101  s[16:31] = { 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20 };
102  s[32:47] = { 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23 };
103  s[48:63] = { 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 };
104 
105  K[ 0: 3] = { 'hd76aa478, 'he8c7b756, 'h242070db, 'hc1bdceee };
106  K[ 4: 7] = { 'hf57c0faf, 'h4787c62a, 'ha8304613, 'hfd469501 };
107  K[ 8:11] = { 'h698098d8, 'h8b44f7af, 'hffff5bb1, 'h895cd7be };
108  K[12:15] = { 'h6b901122, 'hfd987193, 'ha679438e, 'h49b40821 };
109  K[16:19] = { 'hf61e2562, 'hc040b340, 'h265e5a51, 'he9b6c7aa };
110  K[20:23] = { 'hd62f105d, 'h02441453, 'hd8a1e681, 'he7d3fbc8 };
111  K[24:27] = { 'h21e1cde6, 'hc33707d6, 'hf4d50d87, 'h455a14ed };
112  K[28:31] = { 'ha9e3e905, 'hfcefa3f8, 'h676f02d9, 'h8d2a4c8a };
113  K[32:35] = { 'hfffa3942, 'h8771f681, 'h6d9d6122, 'hfde5380c };
114  K[36:39] = { 'ha4beea44, 'h4bdecfa9, 'hf6bb4b60, 'hbebfbc70 };
115  K[40:43] = { 'h289b7ec6, 'heaa127fa, 'hd4ef3085, 'h04881d05 };
116  K[44:47] = { 'hd9d4d039, 'he6db99e5, 'h1fa27cf8, 'hc4ac5665 };
117  K[48:51] = { 'hf4292244, 'h432aff97, 'hab9423a7, 'hfc93a039 };
118  K[52:55] = { 'h655b59c3, 'h8f0ccc92, 'hffeff47d, 'h85845dd1 };
119  K[56:59] = { 'h6fa87e4f, 'hfe2ce6e0, 'ha3014314, 'h4e0811a1 };
120  K[60:63] = { 'hf7537e82, 'hbd3af235, 'h2ad7d2bb, 'heb86d391 };
121 
122  //Initialize variables:
123  a0 = 'h67452301;
124  b0 = 'hefcdab89;
125  c0 = 'h98badcfe;
126  d0 = 'h10325476;
127 
128  lmsg=ser.size();
129  j=lmsg/512;
130 
131  for (i=0;i<j;i=i+1) begin
132  word=this.get_next_512_word(i,ser);
133  mWords=this.get_next_32_word(word);
134 
135  A=a0;
136  B=b0;
137  C=c0;
138  D=d0;
139 
140  //Main loop
141  for (l=0;l<64;l=l+1)begin
142  if(0 <= l && l <= 15) begin
143  F=D^(B&(C^D));
144  g=l;
145 
146  end
147  else if(16 <= l && l <= 31) begin
148  F=C^(D&(B^C));
149  g=(5*l+1)% (16);
150 
151  end
152  else if(32 <= l && l<= 47) begin
153  F = ((B^C)^D);
154  g=(3*l+5)%(16);
155 
156  end
157  else if(48 <= l && l<= 63) begin
158  F = (C^ (B | ~D));
159  g=(7*l)%(16);
160 
161  end
162 
163  F=F+A+K[l] + mWords[g];
164  A=D;
165  D=C;
166  C=B;
167  B=B+((F<<<s[l])|(F>>>(32-s[l])));
168 
169  end // for (l=0;l<64;l=l+1)
170 
171  a0=a0+A;
172  b0=b0+B;
173  c0=c0+C;
174  d0=d0+D;
175 
176  end // for (i=0;i<j;i=i+1)
177  for (p=0;p< 4;p=p+1) begin
178 
179  for (q=0;q<8;q=q+1)begin
180  a0d[31-p*8-q]=a0[7+(8*p)-q];
181  b0d[31-p*8-q]=b0[7+(8*p)-q];
182  c0d[31-p*8-q]=c0[7+(8*p)-q];
183  d0d[31-p*8-q]=d0[7+(8*p)-q];
184 
185  end
186 
187  end
188 
189  return {a0d,b0d,c0d,d0d};
190 endfunction : do_hash
191 
192 // Returns the next 512 bits acording to word_idx of the serialized bits ser.
193 function bit [511:0] cl_syoscb_hash_md5::get_next_512_word(int unsigned word_idx,bit ser[]);
194  int k;
195  bit [511:0] out;
196 
197  for (k=0;k<512;k=k+1)begin
198  out[k]=ser[k+512*word_idx];
199  end
200 
201  return out;
202 endfunction : get_next_512_word
203 
204 // Returns the 16 words of 32 bits in which the 512 bits array word is divided
205 function cl_syoscb_hash_md5::M [15:0] cl_syoscb_hash_md5::get_next_32_word(bit [511:0]word);
206 
207  int k;
208  int l;
209  bit [31:0] out;
210  M [15:0] mWords;
211 
212  for (k=0;k<16;k=k+1) begin
213  for (l=0;l<32;l=l+1)begin
214  out[l]=word[k*32+l];
215  end
216 
217  mWords[k]=out;
218  end
219 
220  return mWords;
221 endfunction : get_next_32_word
virtual tp_hash_digest do_hash(bit ser [])
Hash API: See cl_syoscb_hash_base::do_hash for more details Expects a bitstream with a length which i...
bit< HASH_DIGEST_WIDTH-1:0 > tp_hash_digest
Typedef for a bitstream of HASH_DIGEST_WIDTH bits.
MD5 hash algorithm implementation.
cl_syoscb_hash_packer packer
Handle to a packer suited for this hash algorithm.
Class which defines the base concept of a hash algorithm.
An implementation of a uvm_packer which returns bitstreams that are ready for md5 packing...

Project: SyoSil ApS UVM Scoreboard, Revision: 1.0.3.0

Copyright 2014-2022 SyoSil ApS
All Rights Reserved Worldwide

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
doxygen
Doxygen Version: 1.8.14
Generated with IDV SV Filter Version: 2.6.3
Fri Sep 2 2022 14:39:18
Find a documentation bug? Report bugs to: scoreboard@syosil.com