forked from qca/open-plc-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHA256Fetch.c
61 lines (52 loc) · 1.68 KB
/
SHA256Fetch.c
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
/*====================================================================*
*
* void SHA256Fetch (struct sha256 * sha256, uint8_t digest []);
*
* SHA256.h
*
* read the SHA256 digest; this function works like function read()
* but the function returns no value and the digest buffer is fixed
* length;
*
* to start a digest, use function SHA256Reset(); to write data to
* the digest use function SHA256Write();
*
* Read standard FIPS180-2 sec 5.3.2 for an explanation;
*
* Motley Tools by Charles Maier;
* Copyright (c) 2001-2006 by Charles Maier Associates;
* Licensed under the Internet Software Consortium License;
*
*--------------------------------------------------------------------*/
#ifndef SHA256FETCH_SOURCE
#define SHA256FETCH_SOURCE
#include "../key/SHA256.h"
static void encode (uint8_t memory [], uint32_t number)
{
*memory++ = (uint8_t)(number >> 24);
*memory++ = (uint8_t)(number >> 16);
*memory++ = (uint8_t)(number >> 8);
*memory++ = (uint8_t)(number >> 0);
return;
}
void SHA256Fetch (struct sha256 * sha256, uint8_t digest [])
{
unsigned word;
uint8_t bits [8];
uint32_t upper = (sha256->count [0] >> 29) | (sha256->count [1] << 3);
uint32_t lower = (sha256->count [0] << 3);
uint32_t final = (sha256->count [0] & 0x3F);
uint32_t extra = (final < 56)? (56 - final): (120 - final);
encode (&bits [0], upper);
encode (&bits [4], lower);
SHA256Write (sha256, sha256->extra, extra);
SHA256Write (sha256, bits, sizeof (bits));
for (word = 0; word < sizeof (sha256->state) / sizeof (uint32_t); word++)
{
encode (digest, sha256->state [word]);
digest += sizeof (uint32_t);
}
memset (sha256, 0, sizeof (struct sha256));
return;
}
#endif