Skip to content

Commit 0de58e2

Browse files
Add SHA512/224 and SHA512/256
Signed-off-by: Waqas Kiffal <[email protected]>
1 parent 917f8e3 commit 0de58e2

File tree

1 file changed

+137
-27
lines changed

1 file changed

+137
-27
lines changed

shs/sha.c

Lines changed: 137 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,54 @@
88
#include <openssl/evp.h>
99
#include <openssl/err.h>
1010

11+
#define SHA512_224_DIGEST_LENGTH 28
12+
#define SHA512_256_DIGEST_LENGTH 32
13+
1114
const size_t DIGEST_LENGTH = 40;
1215

13-
void print_hex(FILE *out, const char *s) {
14-
while(*s)
15-
fprintf(out, "%02x", (unsigned char) *s++);
16+
static const EVP_MD sha512_224_md;
17+
static const EVP_MD sha512_256_md;
18+
19+
static int init512_224(EVP_MD_CTX *ctx);
20+
static int init512_256(EVP_MD_CTX *ctx);
21+
static int update512(EVP_MD_CTX *ctx, const void *data, size_t count);
22+
static int final512(EVP_MD_CTX *ctx, unsigned char *md);
23+
24+
const EVP_MD *FIPS_evp_sha512_224(void);
25+
const EVP_MD *FIPS_evp_sha512_256(void);
26+
27+
void print_hex(FILE *out, const char *s, int size)
28+
{
29+
int i;
30+
for (i = 0; i < size; i++)
31+
fprintf(out, "%02x", (unsigned char)*s++);
1632
fprintf(out, "\n");
1733
}
1834

19-
unsigned int calc_hash(EVP_MD *md, const char* in, size_t size, unsigned char* out) {
35+
unsigned int calc_hash(EVP_MD *md, const char *in, size_t size, unsigned char *out)
36+
{
2037
unsigned int md_len = -1;
21-
if (NULL != md) {
38+
if (NULL != md)
39+
{
2240
EVP_MD_CTX mdctx;
23-
FIPS_md_ctx_init(&mdctx);
24-
FIPS_digestinit(&mdctx, md);
25-
FIPS_digestupdate(&mdctx, in, size);
26-
FIPS_digestfinal(&mdctx, out, &md_len);
27-
FIPS_md_ctx_cleanup(&mdctx);
41+
EVP_MD_CTX_init(&mdctx);
42+
EVP_DigestInit_ex(&mdctx, md, NULL);
43+
EVP_DigestUpdate(&mdctx, in, size);
44+
EVP_DigestFinal_ex(&mdctx, out, &md_len);
45+
EVP_MD_CTX_cleanup(&mdctx);
2846
}
2947
return md_len;
3048
}
3149
typedef enum { false, true } bool;
3250

33-
int main(int argc, char *argv[]) {
34-
if (FIPS_init(1) != 1) {
51+
int main(int argc, char *argv[])
52+
{
53+
if (FIPS_init(1) != 1)
54+
{
3555
unsigned long err_code = ERR_get_error();
3656

3757
const size_t ERR_BUFFER_SIZE = 120;
38-
char *err_buf = (char*)malloc(sizeof(char) * ERR_BUFFER_SIZE);
58+
char *err_buf = (char *)malloc(sizeof(char) * ERR_BUFFER_SIZE);
3959
ERR_error_string(err_code, err_buf);
4060

4161
printf("error while initializing FIPS mode: %s", err_buf);
@@ -45,52 +65,142 @@ int main(int argc, char *argv[]) {
4565
EVP_MD *md = NULL;
4666
bool use_rand = false;
4767
int i = 0;
48-
49-
for (i; i < argc; i++) {
50-
if (strcmp(argv[i], "-sha1") == 0) {
68+
for (i; i < argc; i++)
69+
{
70+
if (strcmp(argv[i], "-sha1") == 0)
71+
{
5172
md = FIPS_evp_sha1();
5273
continue;
5374
}
54-
if (strcmp(argv[i], "-sha224") == 0) {
75+
if (strcmp(argv[i], "-sha224") == 0)
76+
{
5577
md = FIPS_evp_sha224();
5678
continue;
5779
}
58-
if (strcmp(argv[i], "-sha256") == 0) {
80+
if (strcmp(argv[i], "-sha256") == 0)
81+
{
5982
md = FIPS_evp_sha256();
6083
continue;
6184
}
62-
if (strcmp(argv[i], "-sha384") == 0) {
85+
if (strcmp(argv[i], "-sha384") == 0)
86+
{
6387
md = FIPS_evp_sha384();
6488
continue;
6589
}
66-
if (strcmp(argv[i], "-sha512") == 0) {
90+
if (strcmp(argv[i], "-sha512") == 0)
91+
{
6792
md = FIPS_evp_sha512();
6893
continue;
6994
}
95+
if (strcmp(argv[i], "-sha512-224") == 0)
96+
{
97+
md = FIPS_evp_sha512_224();
98+
continue;
99+
}
100+
if (strcmp(argv[i], "-sha512-256") == 0)
101+
{
102+
md = FIPS_evp_sha512_256();
103+
continue;
104+
}
70105

71-
if (strcmp(argv[i], "-use-rand") == 0) {
106+
if (strcmp(argv[i], "-use-rand") == 0)
107+
{
72108
use_rand = true;
73109
continue;
74110
}
75111
}
76112

77113
char *in;
78-
if (use_rand == true) {
114+
if (use_rand == true)
115+
{
79116
const size_t BUFFER_SIZE = 40;
80117
srand(time(NULL) * BUFFER_SIZE);
81-
char* buffer = (char*) malloc(sizeof(char) * BUFFER_SIZE);
118+
char *buffer = (char *)malloc(sizeof(char) * BUFFER_SIZE);
82119
rand_str(buffer, BUFFER_SIZE);
83120

84121
in = buffer;
85122
} else {
86-
in = (char *)argv[argc-1];
123+
in = (char *)argv[argc - 1];
87124
}
88125

89126
OpenSSL_add_all_algorithms();
90127
OpenSSL_add_all_digests();
91128
OpenSSL_add_all_ciphers();
92129

93-
unsigned char* hash = (unsigned char*) malloc(sizeof(unsigned char) * DIGEST_LENGTH);
94-
calc_hash(md, in, strlen(in), hash);
95-
print_hex(stdout, hash);
130+
unsigned char *hash = (unsigned char *)malloc(sizeof(unsigned char) * DIGEST_LENGTH);
131+
int len = calc_hash(md, in, strlen(in), hash);
132+
print_hex(stdout, hash, len);
96133
}
134+
135+
static const EVP_MD sha512_224_md = {
136+
922,
137+
920,
138+
SHA512_224_DIGEST_LENGTH,
139+
EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT | EVP_MD_FLAG_FIPS,
140+
init512_224,
141+
update512,
142+
final512,
143+
NULL,
144+
NULL,
145+
EVP_PKEY_NULL_method,
146+
SHA512_CBLOCK,
147+
sizeof(EVP_MD *) + sizeof(SHA512_CTX),
148+
};
149+
150+
static const EVP_MD sha512_256_md = {
151+
923,
152+
921,
153+
SHA512_256_DIGEST_LENGTH,
154+
EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT | EVP_MD_FLAG_FIPS,
155+
init512_256,
156+
update512,
157+
final512,
158+
NULL,
159+
NULL,
160+
EVP_PKEY_NULL_method,
161+
SHA512_CBLOCK,
162+
sizeof(EVP_MD *) + sizeof(SHA512_CTX),
163+
};
164+
165+
int SHA512_224_Init(SHA512_CTX *c)
166+
{
167+
c->h[0] = U64(0x8c3d37c819544da2);
168+
c->h[1] = U64(0x73e1996689dcd4d6);
169+
c->h[2] = U64(0x1dfab7ae32ff9c82);
170+
c->h[3] = U64(0x679dd514582f9fcf);
171+
c->h[4] = U64(0x0f6d2b697bd44da8);
172+
c->h[5] = U64(0x77e36f7304c48942);
173+
c->h[6] = U64(0x3f9d85a86a1d36c8);
174+
c->h[7] = U64(0x1112e6ad91d692a1);
175+
176+
c->Nl = 0;
177+
c->Nh = 0;
178+
c->num = 0;
179+
c->md_len = SHA512_DIGEST_LENGTH;
180+
return 1;
181+
}
182+
183+
int SHA512_256_Init(SHA512_CTX *c)
184+
{
185+
c->h[0] = U64(0x22312194fc2bf72c);
186+
c->h[1] = U64(0x9f555fa3c84c64c2);
187+
c->h[2] = U64(0x2393b86b6f53b151);
188+
c->h[3] = U64(0x963877195940eabd);
189+
c->h[4] = U64(0x96283ee2a88effe3);
190+
c->h[5] = U64(0xbe5e1e2553863992);
191+
c->h[6] = U64(0x2b0199fc2c85b8aa);
192+
c->h[7] = U64(0x0eb72ddc81c52ca2);
193+
194+
c->Nl = 0;
195+
c->Nh = 0;
196+
c->num = 0;
197+
c->md_len = SHA512_DIGEST_LENGTH;
198+
return 1;
199+
}
200+
201+
static int init512_224(EVP_MD_CTX *ctx) { return SHA512_224_Init(ctx->md_data); }
202+
static int init512_256(EVP_MD_CTX *ctx) { return SHA512_256_Init(ctx->md_data); }
203+
static int update512(EVP_MD_CTX *ctx, const void *data, size_t count) { return SHA512_Update(ctx->md_data, data, count); }
204+
static int final512(EVP_MD_CTX *ctx, unsigned char *md) { return SHA512_Final(md, ctx->md_data); }
205+
const EVP_MD *FIPS_evp_sha512_224(void) { return (&sha512_224_md); }
206+
const EVP_MD *FIPS_evp_sha512_256(void) { return (&sha512_256_md); }

0 commit comments

Comments
 (0)