@@ -195,3 +195,60 @@ void fio_sha512_update(struct fio_sha512_ctx *sctx, const uint8_t *data,
195195 /* erase our data */
196196 memset (sctx -> W , 0 , sizeof (sctx -> W ));
197197}
198+
199+ void fio_sha512_final (struct fio_sha512_ctx * sctx )
200+ {
201+ uint8_t * hash = sctx -> buf ;
202+ static uint8_t padding [128 ] = { 0x80 , };
203+ unsigned int index , pad_len ;
204+ uint8_t bits [128 ];
205+ uint64_t t2 ;
206+ uint32_t t ;
207+ int i , j ;
208+
209+ index = pad_len = t = i = j = 0 ;
210+ t2 = 0 ;
211+
212+ /* Save number of bits */
213+ t = sctx -> count [0 ];
214+ bits [15 ] = t ; t >>=8 ;
215+ bits [14 ] = t ; t >>=8 ;
216+ bits [13 ] = t ; t >>=8 ;
217+ bits [12 ] = t ;
218+ t = sctx -> count [1 ];
219+ bits [11 ] = t ; t >>=8 ;
220+ bits [10 ] = t ; t >>=8 ;
221+ bits [9 ] = t ; t >>=8 ;
222+ bits [8 ] = t ;
223+ t = sctx -> count [2 ];
224+ bits [7 ] = t ; t >>=8 ;
225+ bits [6 ] = t ; t >>=8 ;
226+ bits [5 ] = t ; t >>=8 ;
227+ bits [4 ] = t ;
228+ t = sctx -> count [3 ];
229+ bits [3 ] = t ; t >>=8 ;
230+ bits [2 ] = t ; t >>=8 ;
231+ bits [1 ] = t ; t >>=8 ;
232+ bits [0 ] = t ;
233+
234+ /* Pad out to 112 mod 128. */
235+ index = (sctx -> count [0 ] >> 3 ) & 0x7f ;
236+ pad_len = (index < 112 ) ? (112 - index ) : ((128 + 112 ) - index );
237+ fio_sha512_update (sctx , padding , pad_len );
238+
239+ /* Append length (before padding) */
240+ fio_sha512_update (sctx , bits , 16 );
241+
242+ /* Store state in digest */
243+ for (i = j = 0 ; i < 8 ; i ++ , j += 8 ) {
244+ t2 = sctx -> state [i ];
245+ hash [j + 7 ] = (char )t2 & 0xff ; t2 >>=8 ;
246+ hash [j + 6 ] = (char )t2 & 0xff ; t2 >>=8 ;
247+ hash [j + 5 ] = (char )t2 & 0xff ; t2 >>=8 ;
248+ hash [j + 4 ] = (char )t2 & 0xff ; t2 >>=8 ;
249+ hash [j + 3 ] = (char )t2 & 0xff ; t2 >>=8 ;
250+ hash [j + 2 ] = (char )t2 & 0xff ; t2 >>=8 ;
251+ hash [j + 1 ] = (char )t2 & 0xff ; t2 >>=8 ;
252+ hash [j ] = (char )t2 & 0xff ;
253+ }
254+ }
0 commit comments