-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaf_alg_hash.c
98 lines (76 loc) · 1.54 KB
/
af_alg_hash.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
95
96
97
98
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/if_alg.h>
#include <linux/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <openssl/sha.h>
#ifndef AF_ALG
#define AF_ALG 38
#endif
int main(int argc, char *argv[])
{
int rc = -1, tfmfd = -1, opfd = -1, len = 0;
char buf[20] = {};
char *msg = NULL;
struct sockaddr_alg sa = {
.salg_family = AF_ALG,
.salg_type = "hash",
.salg_name = "sha1"
};
if (argc != 2) {
perror("usage: cmd msg");
goto end;
}
tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
if (tfmfd < 0) {
perror("Unable to create socket");
goto end;
}
rc = bind(tfmfd, (struct sockaddr *)&sa, sizeof(sa));
if (rc < 0) {
perror("Unable to bind");
goto end;
}
opfd = accept(tfmfd, NULL, 0);
if (opfd < 0) {
perror("Unable to accept");
goto end;
}
msg = argv[1];
len = strlen(msg);
rc = write(opfd, msg, len);
if (rc < 0) {
perror("Unable to write");
goto end;
}
rc = read(opfd, buf, sizeof(buf));
if (rc < 0) {
perror("Unable to read");
goto end;
}
int i = 0;
for (i = 0; i < sizeof(buf); i++) {
printf("%02x", (unsigned char)buf[i]);
}
printf(" [af_alg]\n");
memset(buf, 0, sizeof(buf));
/* use openssl to get hash value */
if (SHA1((unsigned char *)msg, len, (unsigned char *)buf) == NULL) {
goto end;
}
for (i = 0; i < sizeof(buf); i++) {
printf("%02x", (unsigned char)buf[i]);
}
printf(" [openssl]\n");
rc = 0;
end:
if (tfmfd >= 0)
close(tfmfd);
if (opfd >= 0)
close(opfd);
return rc;
}