forked from iputils/iputils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiputils_md5dig.h
142 lines (121 loc) · 3.21 KB
/
iputils_md5dig.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef IPUTILS_MD5DIG_H
#define IPUTILS_MD5DIG_H
#if defined(USE_GCRYPT)
# include <stdlib.h>
# include <gcrypt.h>
# define IPUTILS_MD5DIG_LEN 16
#elif defined(USE_NETTLE)
# include <nettle/md5.h>
#elif defined(USE_OPENSSL)
# include <openssl/md5.h>
#elif defined(USE_KERNEL_CRYPTO_API)
# define IPUTILS_MD5DIG_LEN 16
# include <errno.h>
# include <linux/if_alg.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <unistd.h>
# include "iputils_common.h"
#endif
#if defined(USE_GCRYPT)
typedef struct {
gcry_md_hd_t dig;
} iputils_md5dig_ctx;
static void iputils_md5dig_init(iputils_md5dig_ctx *ctx)
{
if (gcry_md_open(&ctx->dig, GCRY_MD_MD5, 0) != GPG_ERR_NO_ERROR)
abort();
return;
}
static void iputils_md5dig_update(iputils_md5dig_ctx *ctx,
const void *buf, int len)
{
gcry_md_write(ctx->dig, buf, len);
return;
}
static void iputils_md5dig_final(unsigned char *digest,
iputils_md5dig_ctx *ctx)
{
const void *p;
size_t dlen;
p = gcry_md_read(ctx->dig, GCRY_MD_MD5);
dlen = gcry_md_get_algo_dlen(GCRY_MD_MD5);
if (dlen != IPUTILS_MD5DIG_LEN)
abort();
memcpy(digest, p, dlen);
gcry_md_close(ctx->dig);
}
# define MD5_DIGEST_LENGTH IPUTILS_MD5DIG_LEN
# define MD5_CTX iputils_md5dig_ctx
# define MD5_Init iputils_md5dig_init
# define MD5_Update iputils_md5dig_update
# define MD5_Final iputils_md5dig_final
#elif defined(USE_NETTLE)
typedef struct md5_ctx iputils_md5dig_ctx;
static void iputils_md5dig_init(iputils_md5dig_ctx *ctx)
{
md5_init(ctx);
return;
}
static void iputils_md5dig_update(iputils_md5dig_ctx *ctx,
const void *buf, int len)
{
md5_update(ctx, len, buf);
return;
}
static void iputils_md5dig_final(unsigned char *digest,
iputils_md5dig_ctx *ctx)
{
md5_digest(ctx, MD5_DIGEST_SIZE, digest);
}
# define MD5_DIGEST_LENGTH MD5_DIGEST_SIZE
# define MD5_CTX iputils_md5dig_ctx
# define MD5_Init iputils_md5dig_init
# define MD5_Update iputils_md5dig_update
# define MD5_Final iputils_md5dig_final
#elif defined(USE_KERNEL_CRYPTO_API)
typedef struct {
int bind_sock;
int comm_sock;
} iputils_md5dig_ctx;
static void iputils_md5dig_init(iputils_md5dig_ctx *const ctx)
{
const struct sockaddr_alg sa = {
.salg_family = AF_ALG,
.salg_type = "hash",
.salg_name = "md5"
};
ctx->comm_sock = -1;
if ((ctx->bind_sock = socket(AF_ALG, SOCK_SEQPACKET, 0)) < 0)
return;
if (bind(ctx->bind_sock, (struct sockaddr *)&sa, sizeof(sa)) < 0)
return;
ctx->comm_sock = accept(ctx->bind_sock, NULL, 0);
return;
}
static void iputils_md5dig_update(iputils_md5dig_ctx *ctx,
void const *const buf, const int len)
{
if (ctx->comm_sock < 0)
return;
if (write(ctx->comm_sock, buf, len) != len)
error(0, errno, "write to AF_ALG socket failed");
return;
}
static void iputils_md5dig_final(unsigned char *digest,
iputils_md5dig_ctx const *const ctx)
{
if (ctx->comm_sock < 0)
return;
if (read(ctx->comm_sock, digest, IPUTILS_MD5DIG_LEN) != IPUTILS_MD5DIG_LEN)
error(0, errno, "read from AF_ALG socket failed");
close(ctx->comm_sock);
close(ctx->bind_sock);
}
# define MD5_DIGEST_LENGTH IPUTILS_MD5DIG_LEN
# define MD5_CTX iputils_md5dig_ctx
# define MD5_Init iputils_md5dig_init
# define MD5_Update iputils_md5dig_update
# define MD5_Final iputils_md5dig_final
#endif
#endif