17c2fbfb3SApril Chin /***********************************************************************
27c2fbfb3SApril Chin *                                                                      *
37c2fbfb3SApril Chin *               This software is part of the ast package               *
4*b30d1939SAndy Fiddaman *          Copyright (c) 1996-2011 AT&T Intellectual Property          *
57c2fbfb3SApril Chin *                      and is licensed under the                       *
6*b30d1939SAndy Fiddaman *                 Eclipse Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
87c2fbfb3SApril Chin *                                                                      *
97c2fbfb3SApril Chin *                A copy of the License is available at                 *
10*b30d1939SAndy Fiddaman *          http://www.eclipse.org/org/documents/epl-v10.html           *
11*b30d1939SAndy Fiddaman *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
127c2fbfb3SApril Chin *                                                                      *
137c2fbfb3SApril Chin *              Information and Software Systems Research               *
147c2fbfb3SApril Chin *                            AT&T Research                             *
157c2fbfb3SApril Chin *                           Florham Park NJ                            *
167c2fbfb3SApril Chin *                                                                      *
177c2fbfb3SApril Chin *                 Glenn Fowler <gsf@research.att.com>                  *
187c2fbfb3SApril Chin *                                                                      *
197c2fbfb3SApril Chin ***********************************************************************/
207c2fbfb3SApril Chin #pragma prototyped
217c2fbfb3SApril Chin 
227c2fbfb3SApril Chin /*
237c2fbfb3SApril Chin  * sum(3) wrapper for solaris -lmd message digest library
247c2fbfb3SApril Chin  */
257c2fbfb3SApril Chin 
267c2fbfb3SApril Chin typedef void (*Lmd_init_f)(void*);
277c2fbfb3SApril Chin typedef void (*Lmd_update_f)(void*, const void*, size_t);
287c2fbfb3SApril Chin typedef void (*Lmd_final_f)(unsigned char*, void*);
297c2fbfb3SApril Chin 
307c2fbfb3SApril Chin #define	_SUM_LMD_	\
317c2fbfb3SApril Chin 	_SUM_PUBLIC_	\
327c2fbfb3SApril Chin 	_SUM_PRIVATE_	\
337c2fbfb3SApril Chin 	Lmd_init_f	initf; \
347c2fbfb3SApril Chin 	Lmd_update_f	updatef; \
357c2fbfb3SApril Chin 	Lmd_final_f	finalf; \
367c2fbfb3SApril Chin 	unsigned int	datasize; \
377c2fbfb3SApril Chin 	unsigned char	total[64]; \
387c2fbfb3SApril Chin 	unsigned char	data[64];
397c2fbfb3SApril Chin 
407c2fbfb3SApril Chin typedef struct Lmd_s
417c2fbfb3SApril Chin {
427c2fbfb3SApril Chin 	_SUM_LMD_
437c2fbfb3SApril Chin 	struct
447c2fbfb3SApril Chin 	{
457c2fbfb3SApril Chin 	uintmax_t	context;
467c2fbfb3SApril Chin 	}		context;
477c2fbfb3SApril Chin } Lmd_t;
487c2fbfb3SApril Chin 
497c2fbfb3SApril Chin static int
lmd_init(Sum_t * p)507c2fbfb3SApril Chin lmd_init(Sum_t* p)
517c2fbfb3SApril Chin {
527c2fbfb3SApril Chin 	Lmd_t*	lmd = (Lmd_t*)p;
537c2fbfb3SApril Chin 
547c2fbfb3SApril Chin 	(*lmd->initf)(&lmd->context);
557c2fbfb3SApril Chin 	return 0;
567c2fbfb3SApril Chin }
577c2fbfb3SApril Chin 
587c2fbfb3SApril Chin static int
lmd_block(Sum_t * p,const void * s,size_t n)597c2fbfb3SApril Chin lmd_block(Sum_t* p, const void* s, size_t n)
607c2fbfb3SApril Chin {
617c2fbfb3SApril Chin 	Lmd_t*	lmd = (Lmd_t*)p;
627c2fbfb3SApril Chin 
637c2fbfb3SApril Chin 	(*lmd->updatef)(&lmd->context, s, n);
647c2fbfb3SApril Chin 	return 0;
657c2fbfb3SApril Chin }
667c2fbfb3SApril Chin 
677c2fbfb3SApril Chin static int
lmd_done(Sum_t * p)687c2fbfb3SApril Chin lmd_done(Sum_t* p)
697c2fbfb3SApril Chin {
707c2fbfb3SApril Chin 	register Lmd_t*	lmd = (Lmd_t*)p;
717c2fbfb3SApril Chin 	register int	i;
727c2fbfb3SApril Chin 
737c2fbfb3SApril Chin 	(*lmd->finalf)(lmd->data, &lmd->context);
747c2fbfb3SApril Chin 	for (i = 0; i < lmd->datasize; i++)
757c2fbfb3SApril Chin 		lmd->total[i] ^= lmd->data[i];
767c2fbfb3SApril Chin 	return 0;
777c2fbfb3SApril Chin }
787c2fbfb3SApril Chin 
797c2fbfb3SApril Chin static int
lmd_print(Sum_t * p,Sfio_t * sp,register int flags,size_t scale)807c2fbfb3SApril Chin lmd_print(Sum_t* p, Sfio_t* sp, register int flags, size_t scale)
817c2fbfb3SApril Chin {
827c2fbfb3SApril Chin 	register Lmd_t*		lmd = (Lmd_t*)p;
837c2fbfb3SApril Chin 	register unsigned char*	d;
847c2fbfb3SApril Chin 	register int		i;
857c2fbfb3SApril Chin 
867c2fbfb3SApril Chin 	d = (flags & SUM_TOTAL) ? lmd->total : lmd->data;
877c2fbfb3SApril Chin 	for (i = 0; i < lmd->datasize; i++)
887c2fbfb3SApril Chin 		sfprintf(sp, "%02x", d[i]);
897c2fbfb3SApril Chin 	return 0;
907c2fbfb3SApril Chin }
917c2fbfb3SApril Chin 
927c2fbfb3SApril Chin static int
lmd_data(Sum_t * p,Sumdata_t * data)937c2fbfb3SApril Chin lmd_data(Sum_t* p, Sumdata_t* data)
947c2fbfb3SApril Chin {
957c2fbfb3SApril Chin 	Lmd_t*		lmd = (Lmd_t*)p;
967c2fbfb3SApril Chin 
977c2fbfb3SApril Chin 	data->size = lmd->datasize;
987c2fbfb3SApril Chin 	data->num = 0;
997c2fbfb3SApril Chin 	data->buf = lmd->data;
1007c2fbfb3SApril Chin 	return 0;
1017c2fbfb3SApril Chin }
1027c2fbfb3SApril Chin 
1037c2fbfb3SApril Chin #if _lib_MD4Init && _hdr_md4
1047c2fbfb3SApril Chin 
1057c2fbfb3SApril Chin #include <md4.h>
1067c2fbfb3SApril Chin 
1077c2fbfb3SApril Chin #define md4_description "RFC1320 MD4 message digest. Cryptographically weak. The block count is not printed."
1087c2fbfb3SApril Chin #define md4_options	"[+(version)?md4 (solaris -lmd) 2005-07-26]"
1097c2fbfb3SApril Chin #define md4_match	"md4|MD4"
1107c2fbfb3SApril Chin #define md4_scale	0
1117c2fbfb3SApril Chin #define md4_init	lmd_init
1127c2fbfb3SApril Chin #define md4_block	lmd_block
1137c2fbfb3SApril Chin #define md4_done	lmd_done
1147c2fbfb3SApril Chin #define md4_print	lmd_print
1157c2fbfb3SApril Chin #define md4_data	lmd_data
1167c2fbfb3SApril Chin 
1177c2fbfb3SApril Chin typedef struct Md4_s
1187c2fbfb3SApril Chin {
1197c2fbfb3SApril Chin 	_SUM_LMD_
1207c2fbfb3SApril Chin 	MD4_CTX		context;
1217c2fbfb3SApril Chin } Md4_t;
1227c2fbfb3SApril Chin 
1237c2fbfb3SApril Chin static Sum_t*
md4_open(const Method_t * method,const char * name)1247c2fbfb3SApril Chin md4_open(const Method_t* method, const char* name)
1257c2fbfb3SApril Chin {
1267c2fbfb3SApril Chin 	Md4_t*	lmd;
1277c2fbfb3SApril Chin 
1287c2fbfb3SApril Chin 	if (lmd = newof(0, Md4_t, 1, 0))
1297c2fbfb3SApril Chin 	{
1307c2fbfb3SApril Chin 		lmd->method = (Method_t*)method;
1317c2fbfb3SApril Chin 		lmd->name = name;
1327c2fbfb3SApril Chin 		lmd->datasize = 16;
1337c2fbfb3SApril Chin 		lmd->initf = (Lmd_init_f)MD4Init;
1347c2fbfb3SApril Chin 		lmd->updatef = (Lmd_update_f)MD4Update;
1357c2fbfb3SApril Chin 		lmd->finalf = (Lmd_final_f)MD4Final;
1367c2fbfb3SApril Chin 		md4_init((Sum_t*)lmd);
1377c2fbfb3SApril Chin 	}
1387c2fbfb3SApril Chin 	return (Sum_t*)lmd;
1397c2fbfb3SApril Chin }
1407c2fbfb3SApril Chin 
1417c2fbfb3SApril Chin #endif
1427c2fbfb3SApril Chin 
1437c2fbfb3SApril Chin #if _lib_MD5Init && _hdr_md5
1447c2fbfb3SApril Chin 
1457c2fbfb3SApril Chin #include <md5.h>
1467c2fbfb3SApril Chin 
1477c2fbfb3SApril Chin #define md5_description	"RFC1321 MD5 message digest. Cryptographically weak. The block count is not printed."
1487c2fbfb3SApril Chin #define md5_options	"[+(version)?md5 (solaris -lmd) 2005-07-26]"
1497c2fbfb3SApril Chin #define md5_match	"md5|MD5"
1507c2fbfb3SApril Chin #define md5_scale	0
1517c2fbfb3SApril Chin #define md5_init	lmd_init
1527c2fbfb3SApril Chin #define md5_block	lmd_block
1537c2fbfb3SApril Chin #define md5_done	lmd_done
1547c2fbfb3SApril Chin #define md5_print	lmd_print
1557c2fbfb3SApril Chin #define md5_data	lmd_data
1567c2fbfb3SApril Chin 
1577c2fbfb3SApril Chin typedef struct Md5_s
1587c2fbfb3SApril Chin {
1597c2fbfb3SApril Chin 	_SUM_LMD_
1607c2fbfb3SApril Chin 	MD5_CTX		context;
1617c2fbfb3SApril Chin } Md5_t;
1627c2fbfb3SApril Chin 
1637c2fbfb3SApril Chin static Sum_t*
md5_open(const Method_t * method,const char * name)1647c2fbfb3SApril Chin md5_open(const Method_t* method, const char* name)
1657c2fbfb3SApril Chin {
1667c2fbfb3SApril Chin 	Md5_t*	lmd;
1677c2fbfb3SApril Chin 
1687c2fbfb3SApril Chin 	if (lmd = newof(0, Md5_t, 1, 0))
1697c2fbfb3SApril Chin 	{
1707c2fbfb3SApril Chin 		lmd->method = (Method_t*)method;
1717c2fbfb3SApril Chin 		lmd->name = name;
1727c2fbfb3SApril Chin 		lmd->datasize = 16;
1737c2fbfb3SApril Chin 		lmd->initf = (Lmd_init_f)MD5Init;
1746dcdaa03SToomas Soome 		lmd->updatef = (Lmd_update_f)(uintptr_t)MD5Update;
1757c2fbfb3SApril Chin 		lmd->finalf = (Lmd_final_f)MD5Final;
1767c2fbfb3SApril Chin 		md5_init((Sum_t*)lmd);
1777c2fbfb3SApril Chin 	}
1787c2fbfb3SApril Chin 	return (Sum_t*)lmd;
1797c2fbfb3SApril Chin }
1807c2fbfb3SApril Chin 
1817c2fbfb3SApril Chin #endif
1827c2fbfb3SApril Chin 
1837c2fbfb3SApril Chin #if _lib_SHA1Init && _hdr_sha1
1847c2fbfb3SApril Chin 
1857c2fbfb3SApril Chin #include <sha1.h>
1867c2fbfb3SApril Chin 
1877c2fbfb3SApril Chin #define sha1_description "RFC3174 / FIPS 180-1 SHA-1 secure hash algorithm 1. Cryptographically weak. The block count is not printed."
1887c2fbfb3SApril Chin #define sha1_options	"[+(version)?sha1 (solaris -lmd) 2005-07-26]"
1897c2fbfb3SApril Chin #define sha1_match	"sha1|SHA1|sha-1|SHA-1"
1907c2fbfb3SApril Chin #define sha1_scale	0
1917c2fbfb3SApril Chin #define sha1_init	lmd_init
1927c2fbfb3SApril Chin #define sha1_block	lmd_block
1937c2fbfb3SApril Chin #define sha1_done	lmd_done
1947c2fbfb3SApril Chin #define sha1_print	lmd_print
1957c2fbfb3SApril Chin #define sha1_data	lmd_data
1967c2fbfb3SApril Chin 
1977c2fbfb3SApril Chin typedef struct Sha1_s
1987c2fbfb3SApril Chin {
1997c2fbfb3SApril Chin 	_SUM_LMD_
2007c2fbfb3SApril Chin 	SHA1_CTX	context;
2017c2fbfb3SApril Chin 	unsigned char	pad[1024];	/* XXX: who's bug is it? */
2027c2fbfb3SApril Chin } Sha1_t;
2037c2fbfb3SApril Chin 
2047c2fbfb3SApril Chin static Sum_t*
sha1_open(const Method_t * method,const char * name)2057c2fbfb3SApril Chin sha1_open(const Method_t* method, const char* name)
2067c2fbfb3SApril Chin {
2077c2fbfb3SApril Chin 	Sha1_t*	lmd;
2087c2fbfb3SApril Chin 
2097c2fbfb3SApril Chin 	if (lmd = newof(0, Sha1_t, 1, 0))
2107c2fbfb3SApril Chin 	{
2117c2fbfb3SApril Chin 		lmd->method = (Method_t*)method;
2127c2fbfb3SApril Chin 		lmd->name = name;
2137c2fbfb3SApril Chin 		lmd->datasize = 20;
2147c2fbfb3SApril Chin 		lmd->initf = (Lmd_init_f)SHA1Init;
2157c2fbfb3SApril Chin 		lmd->updatef = (Lmd_update_f)SHA1Update;
2167c2fbfb3SApril Chin 		lmd->finalf = (Lmd_final_f)SHA1Final;
2177c2fbfb3SApril Chin 		sha1_init((Sum_t*)lmd);
2187c2fbfb3SApril Chin 	}
2197c2fbfb3SApril Chin 	return (Sum_t*)lmd;
2207c2fbfb3SApril Chin }
2217c2fbfb3SApril Chin 
2227c2fbfb3SApril Chin #endif
2237c2fbfb3SApril Chin 
2247c2fbfb3SApril Chin #if _lib_SHA2Init && _hdr_sha2
2257c2fbfb3SApril Chin 
2267c2fbfb3SApril Chin #include <sha2.h>
2277c2fbfb3SApril Chin 
2287c2fbfb3SApril Chin #define sha256_description "FIPS 180-2 SHA256 secure hash algorithm.  The block count is not printed."
2297c2fbfb3SApril Chin #define sha256_options	"[+(version)?sha256 (solaris -lmd) 2005-07-26]"
2307c2fbfb3SApril Chin #define sha256_match	"sha256|sha-256|SHA256|SHA-256"
2317c2fbfb3SApril Chin #define sha256_scale	0
2327c2fbfb3SApril Chin #define sha256_init	lmd_init
2337c2fbfb3SApril Chin #define sha256_block	lmd_block
2347c2fbfb3SApril Chin #define sha256_done	lmd_done
2357c2fbfb3SApril Chin #define sha256_print	lmd_print
2367c2fbfb3SApril Chin #define sha256_data	lmd_data
2377c2fbfb3SApril Chin 
2387c2fbfb3SApril Chin typedef struct Sha256_s
2397c2fbfb3SApril Chin {
2407c2fbfb3SApril Chin 	_SUM_LMD_
2417c2fbfb3SApril Chin 	SHA256_CTX	context;
2427c2fbfb3SApril Chin } Sha256_t;
2437c2fbfb3SApril Chin 
2447c2fbfb3SApril Chin static Sum_t*
sha256_open(const Method_t * method,const char * name)2457c2fbfb3SApril Chin sha256_open(const Method_t* method, const char* name)
2467c2fbfb3SApril Chin {
2477c2fbfb3SApril Chin 	Sha256_t*	lmd;
2487c2fbfb3SApril Chin 
2497c2fbfb3SApril Chin 	if (lmd = newof(0, Sha256_t, 1, 0))
2507c2fbfb3SApril Chin 	{
2517c2fbfb3SApril Chin 		lmd->method = (Method_t*)method;
2527c2fbfb3SApril Chin 		lmd->name = name;
2537c2fbfb3SApril Chin 		lmd->datasize = 32;
2547c2fbfb3SApril Chin 		lmd->initf = (Lmd_init_f)SHA256Init;
2557c2fbfb3SApril Chin 		lmd->updatef = (Lmd_update_f)SHA256Update;
2567c2fbfb3SApril Chin 		lmd->finalf = (Lmd_final_f)SHA256Final;
2577c2fbfb3SApril Chin 		sha256_init((Sum_t*)lmd);
2587c2fbfb3SApril Chin 	}
2597c2fbfb3SApril Chin 	return (Sum_t*)lmd;
2607c2fbfb3SApril Chin }
2617c2fbfb3SApril Chin 
2627c2fbfb3SApril Chin #define sha384_description "FIPS 180-2 SHA384 secure hash algorithm.  The block count is not printed."
2637c2fbfb3SApril Chin #define sha384_options	"[+(version)?sha384 (solaris -lmd) 2005-07-26]"
2647c2fbfb3SApril Chin #define sha384_match	"sha384|sha-384|SHA384|SHA-384"
2657c2fbfb3SApril Chin #define sha384_scale	0
2667c2fbfb3SApril Chin #define sha384_init	lmd_init
2677c2fbfb3SApril Chin #define sha384_block	lmd_block
2687c2fbfb3SApril Chin #define sha384_done	lmd_done
2697c2fbfb3SApril Chin #define sha384_print	lmd_print
2707c2fbfb3SApril Chin #define sha384_data	lmd_data
2717c2fbfb3SApril Chin 
2727c2fbfb3SApril Chin typedef struct Sha384_s
2737c2fbfb3SApril Chin {
2747c2fbfb3SApril Chin 	_SUM_LMD_
2757c2fbfb3SApril Chin 	SHA384_CTX	context;
2767c2fbfb3SApril Chin } Sha384_t;
2777c2fbfb3SApril Chin 
2787c2fbfb3SApril Chin static Sum_t*
sha384_open(const Method_t * method,const char * name)2797c2fbfb3SApril Chin sha384_open(const Method_t* method, const char* name)
2807c2fbfb3SApril Chin {
2817c2fbfb3SApril Chin 	Sha384_t*	lmd;
2827c2fbfb3SApril Chin 
2837c2fbfb3SApril Chin 	if (lmd = newof(0, Sha384_t, 1, 0))
2847c2fbfb3SApril Chin 	{
2857c2fbfb3SApril Chin 		lmd->method = (Method_t*)method;
2867c2fbfb3SApril Chin 		lmd->name = name;
2877c2fbfb3SApril Chin 		lmd->datasize = 48;
2887c2fbfb3SApril Chin 		lmd->initf = (Lmd_init_f)SHA384Init;
2897c2fbfb3SApril Chin 		lmd->updatef = (Lmd_update_f)SHA384Update;
2907c2fbfb3SApril Chin 		lmd->finalf = (Lmd_final_f)SHA384Final;
2917c2fbfb3SApril Chin 		sha384_init((Sum_t*)lmd);
2927c2fbfb3SApril Chin 	}
2937c2fbfb3SApril Chin 	return (Sum_t*)lmd;
2947c2fbfb3SApril Chin }
2957c2fbfb3SApril Chin 
2967c2fbfb3SApril Chin #define sha512_description "FIPS 180-2 SHA512 secure hash algorithm.  The block count is not printed."
2977c2fbfb3SApril Chin #define sha512_options	"[+(version)?sha512 (solaris -lmd) 2005-07-26]"
2987c2fbfb3SApril Chin #define sha512_match	"sha512|sha-512|SHA512|SHA-512"
2997c2fbfb3SApril Chin #define sha512_scale	0
3007c2fbfb3SApril Chin #define sha512_init	lmd_init
3017c2fbfb3SApril Chin #define sha512_block	lmd_block
3027c2fbfb3SApril Chin #define sha512_done	lmd_done
3037c2fbfb3SApril Chin #define sha512_print	lmd_print
3047c2fbfb3SApril Chin #define sha512_data	lmd_data
3057c2fbfb3SApril Chin 
3067c2fbfb3SApril Chin typedef struct Sha512_s
3077c2fbfb3SApril Chin {
3087c2fbfb3SApril Chin 	_SUM_LMD_
3097c2fbfb3SApril Chin 	SHA512_CTX	context;
3107c2fbfb3SApril Chin } Sha512_t;
3117c2fbfb3SApril Chin 
3127c2fbfb3SApril Chin static Sum_t*
sha512_open(const Method_t * method,const char * name)3137c2fbfb3SApril Chin sha512_open(const Method_t* method, const char* name)
3147c2fbfb3SApril Chin {
3157c2fbfb3SApril Chin 	Sha512_t*	lmd;
3167c2fbfb3SApril Chin 
3177c2fbfb3SApril Chin 	if (lmd = newof(0, Sha512_t, 1, 0))
3187c2fbfb3SApril Chin 	{
3197c2fbfb3SApril Chin 		lmd->method = (Method_t*)method;
3207c2fbfb3SApril Chin 		lmd->name = name;
3217c2fbfb3SApril Chin 		lmd->datasize = 64;
3227c2fbfb3SApril Chin 		lmd->initf = (Lmd_init_f)SHA512Init;
3237c2fbfb3SApril Chin 		lmd->updatef = (Lmd_update_f)SHA512Update;
3247c2fbfb3SApril Chin 		lmd->finalf = (Lmd_final_f)SHA512Final;
3257c2fbfb3SApril Chin 		sha512_init((Sum_t*)lmd);
3267c2fbfb3SApril Chin 	}
3277c2fbfb3SApril Chin 	return (Sum_t*)lmd;
3287c2fbfb3SApril Chin }
3297c2fbfb3SApril Chin 
3307c2fbfb3SApril Chin #endif