summaryrefslogtreecommitdiff
path: root/usr/src/lib/libsum/common/sum-lmd.c
blob: ca104c394b28f374d7972e53364684f0623972e4 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1996-2010 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                                                                      *
***********************************************************************/
#pragma prototyped

/*
 * sum(3) wrapper for solaris -lmd message digest library
 */

typedef void (*Lmd_init_f)(void*);
typedef void (*Lmd_update_f)(void*, const void*, size_t);
typedef void (*Lmd_final_f)(unsigned char*, void*);

#define	_SUM_LMD_	\
	_SUM_PUBLIC_	\
	_SUM_PRIVATE_	\
	Lmd_init_f	initf; \
	Lmd_update_f	updatef; \
	Lmd_final_f	finalf; \
	unsigned int	datasize; \
	unsigned char	total[64]; \
	unsigned char	data[64];

typedef struct Lmd_s
{
	_SUM_LMD_
	struct
	{
	uintmax_t	context;
	}		context;
} Lmd_t;

static int
lmd_init(Sum_t* p)
{
	Lmd_t*	lmd = (Lmd_t*)p;

	(*lmd->initf)(&lmd->context);
	return 0;
}

static int
lmd_block(Sum_t* p, const void* s, size_t n)
{
	Lmd_t*	lmd = (Lmd_t*)p;

	(*lmd->updatef)(&lmd->context, s, n);
	return 0;
}

static int
lmd_done(Sum_t* p)
{
	register Lmd_t*	lmd = (Lmd_t*)p;
	register int	i;

	(*lmd->finalf)(lmd->data, &lmd->context);
	for (i = 0; i < lmd->datasize; i++)
		lmd->total[i] ^= lmd->data[i];
	return 0;
}

static int
lmd_print(Sum_t* p, Sfio_t* sp, register int flags, size_t scale)
{
	register Lmd_t*		lmd = (Lmd_t*)p;
	register unsigned char*	d;
	register int		i;

	d = (flags & SUM_TOTAL) ? lmd->total : lmd->data;
	for (i = 0; i < lmd->datasize; i++)
		sfprintf(sp, "%02x", d[i]);
	return 0;
}

static int
lmd_data(Sum_t* p, Sumdata_t* data)
{
	Lmd_t*		lmd = (Lmd_t*)p;

	data->size = lmd->datasize;
	data->num = 0;
	data->buf = lmd->data;
	return 0;
}

#if _lib_MD4Init && _hdr_md4

#include <md4.h>

#define md4_description "RFC1320 MD4 message digest. Cryptographically weak. The block count is not printed."
#define md4_options	"[+(version)?md4 (solaris -lmd) 2005-07-26]"
#define md4_match	"md4|MD4"
#define md4_scale	0
#define md4_init	lmd_init
#define md4_block	lmd_block
#define md4_done	lmd_done
#define md4_print	lmd_print
#define md4_data	lmd_data

typedef struct Md4_s
{
	_SUM_LMD_
	MD4_CTX		context;
} Md4_t;

static Sum_t*
md4_open(const Method_t* method, const char* name)
{
	Md4_t*	lmd;

	if (lmd = newof(0, Md4_t, 1, 0))
	{
		lmd->method = (Method_t*)method;
		lmd->name = name;
		lmd->datasize = 16;
		lmd->initf = (Lmd_init_f)MD4Init;
		lmd->updatef = (Lmd_update_f)MD4Update;
		lmd->finalf = (Lmd_final_f)MD4Final;
		md4_init((Sum_t*)lmd);
	}
	return (Sum_t*)lmd;
}

#endif

#if _lib_MD5Init && _hdr_md5

#include <md5.h>

#define md5_description	"RFC1321 MD5 message digest. Cryptographically weak. The block count is not printed."
#define md5_options	"[+(version)?md5 (solaris -lmd) 2005-07-26]"
#define md5_match	"md5|MD5"
#define md5_scale	0
#define md5_init	lmd_init
#define md5_block	lmd_block
#define md5_done	lmd_done
#define md5_print	lmd_print
#define md5_data	lmd_data

typedef struct Md5_s
{
	_SUM_LMD_
	MD5_CTX		context;
} Md5_t;

static Sum_t*
md5_open(const Method_t* method, const char* name)
{
	Md5_t*	lmd;

	if (lmd = newof(0, Md5_t, 1, 0))
	{
		lmd->method = (Method_t*)method;
		lmd->name = name;
		lmd->datasize = 16;
		lmd->initf = (Lmd_init_f)MD5Init;
		lmd->updatef = (Lmd_update_f)(uintptr_t)MD5Update;
		lmd->finalf = (Lmd_final_f)MD5Final;
		md5_init((Sum_t*)lmd);
	}
	return (Sum_t*)lmd;
}

#endif

#if _lib_SHA1Init && _hdr_sha1

#include <sha1.h>

#define sha1_description "RFC3174 / FIPS 180-1 SHA-1 secure hash algorithm 1. Cryptographically weak. The block count is not printed."
#define sha1_options	"[+(version)?sha1 (solaris -lmd) 2005-07-26]"
#define sha1_match	"sha1|SHA1|sha-1|SHA-1"
#define sha1_scale	0
#define sha1_init	lmd_init
#define sha1_block	lmd_block
#define sha1_done	lmd_done
#define sha1_print	lmd_print
#define sha1_data	lmd_data

typedef struct Sha1_s
{
	_SUM_LMD_
	SHA1_CTX	context;
	unsigned char	pad[1024];	/* XXX: who's bug is it? */
} Sha1_t;

static Sum_t*
sha1_open(const Method_t* method, const char* name)
{
	Sha1_t*	lmd;

	if (lmd = newof(0, Sha1_t, 1, 0))
	{
		lmd->method = (Method_t*)method;
		lmd->name = name;
		lmd->datasize = 20;
		lmd->initf = (Lmd_init_f)SHA1Init;
		lmd->updatef = (Lmd_update_f)SHA1Update;
		lmd->finalf = (Lmd_final_f)SHA1Final;
		sha1_init((Sum_t*)lmd);
	}
	return (Sum_t*)lmd;
}

#endif

#if _lib_SHA2Init && _hdr_sha2

#include <sha2.h>

#define sha256_description "FIPS 180-2 SHA256 secure hash algorithm.  The block count is not printed."
#define sha256_options	"[+(version)?sha256 (solaris -lmd) 2005-07-26]"
#define sha256_match	"sha256|sha-256|SHA256|SHA-256"
#define sha256_scale	0
#define sha256_init	lmd_init
#define sha256_block	lmd_block
#define sha256_done	lmd_done
#define sha256_print	lmd_print
#define sha256_data	lmd_data

typedef struct Sha256_s
{
	_SUM_LMD_
	SHA256_CTX	context;
} Sha256_t;

static Sum_t*
sha256_open(const Method_t* method, const char* name)
{
	Sha256_t*	lmd;

	if (lmd = newof(0, Sha256_t, 1, 0))
	{
		lmd->method = (Method_t*)method;
		lmd->name = name;
		lmd->datasize = 32;
		lmd->initf = (Lmd_init_f)SHA256Init;
		lmd->updatef = (Lmd_update_f)SHA256Update;
		lmd->finalf = (Lmd_final_f)SHA256Final;
		sha256_init((Sum_t*)lmd);
	}
	return (Sum_t*)lmd;
}

#define sha384_description "FIPS 180-2 SHA384 secure hash algorithm.  The block count is not printed."
#define sha384_options	"[+(version)?sha384 (solaris -lmd) 2005-07-26]"
#define sha384_match	"sha384|sha-384|SHA384|SHA-384"
#define sha384_scale	0
#define sha384_init	lmd_init
#define sha384_block	lmd_block
#define sha384_done	lmd_done
#define sha384_print	lmd_print
#define sha384_data	lmd_data

typedef struct Sha384_s
{
	_SUM_LMD_
	SHA384_CTX	context;
} Sha384_t;

static Sum_t*
sha384_open(const Method_t* method, const char* name)
{
	Sha384_t*	lmd;

	if (lmd = newof(0, Sha384_t, 1, 0))
	{
		lmd->method = (Method_t*)method;
		lmd->name = name;
		lmd->datasize = 48;
		lmd->initf = (Lmd_init_f)SHA384Init;
		lmd->updatef = (Lmd_update_f)SHA384Update;
		lmd->finalf = (Lmd_final_f)SHA384Final;
		sha384_init((Sum_t*)lmd);
	}
	return (Sum_t*)lmd;
}

#define sha512_description "FIPS 180-2 SHA512 secure hash algorithm.  The block count is not printed."
#define sha512_options	"[+(version)?sha512 (solaris -lmd) 2005-07-26]"
#define sha512_match	"sha512|sha-512|SHA512|SHA-512"
#define sha512_scale	0
#define sha512_init	lmd_init
#define sha512_block	lmd_block
#define sha512_done	lmd_done
#define sha512_print	lmd_print
#define sha512_data	lmd_data

typedef struct Sha512_s
{
	_SUM_LMD_
	SHA512_CTX	context;
} Sha512_t;

static Sum_t*
sha512_open(const Method_t* method, const char* name)
{
	Sha512_t*	lmd;

	if (lmd = newof(0, Sha512_t, 1, 0))
	{
		lmd->method = (Method_t*)method;
		lmd->name = name;
		lmd->datasize = 64;
		lmd->initf = (Lmd_init_f)SHA512Init;
		lmd->updatef = (Lmd_update_f)SHA512Update;
		lmd->finalf = (Lmd_final_f)SHA512Final;
		sha512_init((Sum_t*)lmd);
	}
	return (Sum_t*)lmd;
}

#endif