xref: /titanic_50/usr/src/lib/libsum/common/sum-lmd.c (revision 45179f4335d029f1129eb7283f8087740f0395f1)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1996-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                                                                      *
19 ***********************************************************************/
20 #pragma prototyped
21 
22 /*
23  * sum(3) wrapper for solaris -lmd message digest library
24  */
25 
26 typedef void (*Lmd_init_f)(void*);
27 typedef void (*Lmd_update_f)(void*, const void*, size_t);
28 typedef void (*Lmd_final_f)(unsigned char*, void*);
29 
30 #define	_SUM_LMD_	\
31 	_SUM_PUBLIC_	\
32 	_SUM_PRIVATE_	\
33 	Lmd_init_f	initf; \
34 	Lmd_update_f	updatef; \
35 	Lmd_final_f	finalf; \
36 	unsigned int	datasize; \
37 	unsigned char	total[64]; \
38 	unsigned char	data[64];
39 
40 typedef struct Lmd_s
41 {
42 	_SUM_LMD_
43 	struct
44 	{
45 	uintmax_t	context;
46 	}		context;
47 } Lmd_t;
48 
49 static int
50 lmd_init(Sum_t* p)
51 {
52 	Lmd_t*	lmd = (Lmd_t*)p;
53 
54 	(*lmd->initf)(&lmd->context);
55 	return 0;
56 }
57 
58 static int
59 lmd_block(Sum_t* p, const void* s, size_t n)
60 {
61 	Lmd_t*	lmd = (Lmd_t*)p;
62 
63 	(*lmd->updatef)(&lmd->context, s, n);
64 	return 0;
65 }
66 
67 static int
68 lmd_done(Sum_t* p)
69 {
70 	register Lmd_t*	lmd = (Lmd_t*)p;
71 	register int	i;
72 
73 	(*lmd->finalf)(lmd->data, &lmd->context);
74 	for (i = 0; i < lmd->datasize; i++)
75 		lmd->total[i] ^= lmd->data[i];
76 	return 0;
77 }
78 
79 static int
80 lmd_print(Sum_t* p, Sfio_t* sp, register int flags, size_t scale)
81 {
82 	register Lmd_t*		lmd = (Lmd_t*)p;
83 	register unsigned char*	d;
84 	register int		i;
85 
86 	d = (flags & SUM_TOTAL) ? lmd->total : lmd->data;
87 	for (i = 0; i < lmd->datasize; i++)
88 		sfprintf(sp, "%02x", d[i]);
89 	return 0;
90 }
91 
92 static int
93 lmd_data(Sum_t* p, Sumdata_t* data)
94 {
95 	Lmd_t*		lmd = (Lmd_t*)p;
96 
97 	data->size = lmd->datasize;
98 	data->num = 0;
99 	data->buf = lmd->data;
100 	return 0;
101 }
102 
103 #if _lib_MD4Init && _hdr_md4
104 
105 #include <md4.h>
106 
107 #define md4_description "RFC1320 MD4 message digest. Cryptographically weak. The block count is not printed."
108 #define md4_options	"[+(version)?md4 (solaris -lmd) 2005-07-26]"
109 #define md4_match	"md4|MD4"
110 #define md4_scale	0
111 #define md4_init	lmd_init
112 #define md4_block	lmd_block
113 #define md4_done	lmd_done
114 #define md4_print	lmd_print
115 #define md4_data	lmd_data
116 
117 typedef struct Md4_s
118 {
119 	_SUM_LMD_
120 	MD4_CTX		context;
121 } Md4_t;
122 
123 static Sum_t*
124 md4_open(const Method_t* method, const char* name)
125 {
126 	Md4_t*	lmd;
127 
128 	if (lmd = newof(0, Md4_t, 1, 0))
129 	{
130 		lmd->method = (Method_t*)method;
131 		lmd->name = name;
132 		lmd->datasize = 16;
133 		lmd->initf = (Lmd_init_f)MD4Init;
134 		lmd->updatef = (Lmd_update_f)MD4Update;
135 		lmd->finalf = (Lmd_final_f)MD4Final;
136 		md4_init((Sum_t*)lmd);
137 	}
138 	return (Sum_t*)lmd;
139 }
140 
141 #endif
142 
143 #if _lib_MD5Init && _hdr_md5
144 
145 #include <md5.h>
146 
147 #define md5_description	"RFC1321 MD5 message digest. Cryptographically weak. The block count is not printed."
148 #define md5_options	"[+(version)?md5 (solaris -lmd) 2005-07-26]"
149 #define md5_match	"md5|MD5"
150 #define md5_scale	0
151 #define md5_init	lmd_init
152 #define md5_block	lmd_block
153 #define md5_done	lmd_done
154 #define md5_print	lmd_print
155 #define md5_data	lmd_data
156 
157 typedef struct Md5_s
158 {
159 	_SUM_LMD_
160 	MD5_CTX		context;
161 } Md5_t;
162 
163 static Sum_t*
164 md5_open(const Method_t* method, const char* name)
165 {
166 	Md5_t*	lmd;
167 
168 	if (lmd = newof(0, Md5_t, 1, 0))
169 	{
170 		lmd->method = (Method_t*)method;
171 		lmd->name = name;
172 		lmd->datasize = 16;
173 		lmd->initf = (Lmd_init_f)MD5Init;
174 		lmd->updatef = (Lmd_update_f)MD5Update;
175 		lmd->finalf = (Lmd_final_f)MD5Final;
176 		md5_init((Sum_t*)lmd);
177 	}
178 	return (Sum_t*)lmd;
179 }
180 
181 #endif
182 
183 #if _lib_SHA1Init && _hdr_sha1
184 
185 #include <sha1.h>
186 
187 #define sha1_description "RFC3174 / FIPS 180-1 SHA-1 secure hash algorithm 1. Cryptographically weak. The block count is not printed."
188 #define sha1_options	"[+(version)?sha1 (solaris -lmd) 2005-07-26]"
189 #define sha1_match	"sha1|SHA1|sha-1|SHA-1"
190 #define sha1_scale	0
191 #define sha1_init	lmd_init
192 #define sha1_block	lmd_block
193 #define sha1_done	lmd_done
194 #define sha1_print	lmd_print
195 #define sha1_data	lmd_data
196 
197 typedef struct Sha1_s
198 {
199 	_SUM_LMD_
200 	SHA1_CTX	context;
201 	unsigned char	pad[1024];	/* XXX: who's bug is it? */
202 } Sha1_t;
203 
204 static Sum_t*
205 sha1_open(const Method_t* method, const char* name)
206 {
207 	Sha1_t*	lmd;
208 
209 	if (lmd = newof(0, Sha1_t, 1, 0))
210 	{
211 		lmd->method = (Method_t*)method;
212 		lmd->name = name;
213 		lmd->datasize = 20;
214 		lmd->initf = (Lmd_init_f)SHA1Init;
215 		lmd->updatef = (Lmd_update_f)SHA1Update;
216 		lmd->finalf = (Lmd_final_f)SHA1Final;
217 		sha1_init((Sum_t*)lmd);
218 	}
219 	return (Sum_t*)lmd;
220 }
221 
222 #endif
223 
224 #if _lib_SHA2Init && _hdr_sha2
225 
226 #include <sha2.h>
227 
228 #define sha256_description "FIPS 180-2 SHA256 secure hash algorithm.  The block count is not printed."
229 #define sha256_options	"[+(version)?sha256 (solaris -lmd) 2005-07-26]"
230 #define sha256_match	"sha256|sha-256|SHA256|SHA-256"
231 #define sha256_scale	0
232 #define sha256_init	lmd_init
233 #define sha256_block	lmd_block
234 #define sha256_done	lmd_done
235 #define sha256_print	lmd_print
236 #define sha256_data	lmd_data
237 
238 typedef struct Sha256_s
239 {
240 	_SUM_LMD_
241 	SHA256_CTX	context;
242 } Sha256_t;
243 
244 static Sum_t*
245 sha256_open(const Method_t* method, const char* name)
246 {
247 	Sha256_t*	lmd;
248 
249 	if (lmd = newof(0, Sha256_t, 1, 0))
250 	{
251 		lmd->method = (Method_t*)method;
252 		lmd->name = name;
253 		lmd->datasize = 32;
254 		lmd->initf = (Lmd_init_f)SHA256Init;
255 		lmd->updatef = (Lmd_update_f)SHA256Update;
256 		lmd->finalf = (Lmd_final_f)SHA256Final;
257 		sha256_init((Sum_t*)lmd);
258 	}
259 	return (Sum_t*)lmd;
260 }
261 
262 #define sha384_description "FIPS 180-2 SHA384 secure hash algorithm.  The block count is not printed."
263 #define sha384_options	"[+(version)?sha384 (solaris -lmd) 2005-07-26]"
264 #define sha384_match	"sha384|sha-384|SHA384|SHA-384"
265 #define sha384_scale	0
266 #define sha384_init	lmd_init
267 #define sha384_block	lmd_block
268 #define sha384_done	lmd_done
269 #define sha384_print	lmd_print
270 #define sha384_data	lmd_data
271 
272 typedef struct Sha384_s
273 {
274 	_SUM_LMD_
275 	SHA384_CTX	context;
276 } Sha384_t;
277 
278 static Sum_t*
279 sha384_open(const Method_t* method, const char* name)
280 {
281 	Sha384_t*	lmd;
282 
283 	if (lmd = newof(0, Sha384_t, 1, 0))
284 	{
285 		lmd->method = (Method_t*)method;
286 		lmd->name = name;
287 		lmd->datasize = 48;
288 		lmd->initf = (Lmd_init_f)SHA384Init;
289 		lmd->updatef = (Lmd_update_f)SHA384Update;
290 		lmd->finalf = (Lmd_final_f)SHA384Final;
291 		sha384_init((Sum_t*)lmd);
292 	}
293 	return (Sum_t*)lmd;
294 }
295 
296 #define sha512_description "FIPS 180-2 SHA512 secure hash algorithm.  The block count is not printed."
297 #define sha512_options	"[+(version)?sha512 (solaris -lmd) 2005-07-26]"
298 #define sha512_match	"sha512|sha-512|SHA512|SHA-512"
299 #define sha512_scale	0
300 #define sha512_init	lmd_init
301 #define sha512_block	lmd_block
302 #define sha512_done	lmd_done
303 #define sha512_print	lmd_print
304 #define sha512_data	lmd_data
305 
306 typedef struct Sha512_s
307 {
308 	_SUM_LMD_
309 	SHA512_CTX	context;
310 } Sha512_t;
311 
312 static Sum_t*
313 sha512_open(const Method_t* method, const char* name)
314 {
315 	Sha512_t*	lmd;
316 
317 	if (lmd = newof(0, Sha512_t, 1, 0))
318 	{
319 		lmd->method = (Method_t*)method;
320 		lmd->name = name;
321 		lmd->datasize = 64;
322 		lmd->initf = (Lmd_init_f)SHA512Init;
323 		lmd->updatef = (Lmd_update_f)SHA512Update;
324 		lmd->finalf = (Lmd_final_f)SHA512Final;
325 		sha512_init((Sum_t*)lmd);
326 	}
327 	return (Sum_t*)lmd;
328 }
329 
330 #endif
331