-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha224-combine.c
95 lines (76 loc) · 1.58 KB
/
sha224-combine.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/if_alg.h>
#include <linux/socket.h>
#include "common.h"
#ifndef SOL_ALG
#define SOL_ALG 279
#endif
int sha224_afalg(char *input_buf, char *alafg_buf)
{
int i;
int opfd;
int tfmfd;
struct sockaddr_alg sa = {
.salg_family = AF_ALG,
.salg_type = "hash",
.salg_name = "sha224"
};
struct msghdr msg = {};
struct cmsghdr *cmsg;
char cbuf[CMSG_SPACE(4)] = {0};
struct iovec iov;
tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
bind(tfmfd, (struct sockaddr *)&sa, sizeof(sa));
opfd = accept(tfmfd, NULL, 0);
write(opfd, input_buf, 1024);
read(opfd, alafg_buf, 28);
#ifdef PRINT
printf("AF_ALG RESULT:\n");
for (i = 0; i < 28; i++) {
printf("%02x ", (unsigned char)alafg_buf[i]);
}
printf("\n");
#endif
close(opfd);
close(tfmfd);
return 0;
}
int sha224_openssl(char *input_buf, char *openssl_buf)
{
int i;
SHA224(input_buf, 1024, openssl_buf);
#ifdef PRINT
printf("OPENSSL RESULT:\n");
for (i = 0; i < 28; i++) {
printf("%02x ", (unsigned char)openssl_buf[i]);
}
printf("\n");
#endif
return 0;
}
int sha224_test()
{
int ret;
char alafg_buf[28];
char openssl_buf[28];
char input_buf[1024];
for (int i = 0; i < 100; ++i)
{
rand_array(input_buf, 1024);
sha224_afalg(input_buf, alafg_buf);
sha224_openssl(input_buf, openssl_buf);
ret = array_equal(alafg_buf, openssl_buf, 28);
if (ret < 0)
{
printf("SHA224 ERROR: AFALG AND OPENSSL RESULTS DIFFER\n");
return -1;
}
}
printf("SHA224 PASS\n");
return 0;
}