xref: /minix3/lib/libc/hash/md2/md2.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: md2.c,v 1.7 2012/06/25 22:32:44 abs Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 2001 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras  * by Andrew Brown.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*f14fb602SLionel Sambuc __RCSID("$NetBSD: md2.c,v 1.7 2012/06/25 22:32:44 abs Exp $");
352fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras #include "namespace.h"
382fe8fb19SBen Gras 
392fe8fb19SBen Gras #include <sys/types.h>
402fe8fb19SBen Gras 
412fe8fb19SBen Gras #include <assert.h>
422fe8fb19SBen Gras #include <md2.h>
432fe8fb19SBen Gras #include <string.h>
442fe8fb19SBen Gras 
452fe8fb19SBen Gras #if HAVE_NBTOOL_CONFIG_H
462fe8fb19SBen Gras #include "nbtool_config.h"
472fe8fb19SBen Gras #endif
482fe8fb19SBen Gras 
492fe8fb19SBen Gras #if !HAVE_MD2_H
502fe8fb19SBen Gras 
512fe8fb19SBen Gras /* cut-n-pasted from rfc1319 */
522fe8fb19SBen Gras static unsigned char S[256] = {
532fe8fb19SBen Gras 	41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
542fe8fb19SBen Gras 	19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
552fe8fb19SBen Gras 	76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
562fe8fb19SBen Gras 	138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
572fe8fb19SBen Gras 	245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
582fe8fb19SBen Gras 	148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
592fe8fb19SBen Gras 	39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
602fe8fb19SBen Gras 	181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
612fe8fb19SBen Gras 	150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
622fe8fb19SBen Gras 	112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
632fe8fb19SBen Gras 	96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
642fe8fb19SBen Gras 	85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
652fe8fb19SBen Gras 	234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
662fe8fb19SBen Gras 	129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
672fe8fb19SBen Gras 	8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
682fe8fb19SBen Gras 	203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
692fe8fb19SBen Gras 	166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
702fe8fb19SBen Gras 	31, 26, 219, 153, 141, 51, 159, 17, 131, 20
712fe8fb19SBen Gras };
722fe8fb19SBen Gras 
732fe8fb19SBen Gras /* cut-n-pasted from rfc1319 */
742fe8fb19SBen Gras static const unsigned char *pad[] = {
752fe8fb19SBen Gras 	(const unsigned char *)"",
762fe8fb19SBen Gras 	(const unsigned char *)"\001",
772fe8fb19SBen Gras 	(const unsigned char *)"\002\002",
782fe8fb19SBen Gras 	(const unsigned char *)"\003\003\003",
792fe8fb19SBen Gras 	(const unsigned char *)"\004\004\004\004",
802fe8fb19SBen Gras 	(const unsigned char *)"\005\005\005\005\005",
812fe8fb19SBen Gras 	(const unsigned char *)"\006\006\006\006\006\006",
822fe8fb19SBen Gras 	(const unsigned char *)"\007\007\007\007\007\007\007",
832fe8fb19SBen Gras 	(const unsigned char *)"\010\010\010\010\010\010\010\010",
842fe8fb19SBen Gras 	(const unsigned char *)"\011\011\011\011\011\011\011\011\011",
852fe8fb19SBen Gras 	(const unsigned char *)"\012\012\012\012\012\012\012\012\012\012",
862fe8fb19SBen Gras 	(const unsigned char *)"\013\013\013\013\013\013\013\013\013\013\013",
872fe8fb19SBen Gras 	(const unsigned char *)
882fe8fb19SBen Gras 	"\014\014\014\014\014\014\014\014\014\014\014\014",
892fe8fb19SBen Gras 	(const unsigned char *)
902fe8fb19SBen Gras 	"\015\015\015\015\015\015\015\015\015\015\015\015\015",
912fe8fb19SBen Gras 	(const unsigned char *)
922fe8fb19SBen Gras 	"\016\016\016\016\016\016\016\016\016\016\016\016\016\016",
932fe8fb19SBen Gras 	(const unsigned char *)
942fe8fb19SBen Gras 	"\017\017\017\017\017\017\017\017\017\017\017\017\017\017\017",
952fe8fb19SBen Gras 	(const unsigned char *)
962fe8fb19SBen Gras 	"\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020"
972fe8fb19SBen Gras };
982fe8fb19SBen Gras 
992fe8fb19SBen Gras /*
1002fe8fb19SBen Gras  * XXX This should not be visible, but due to an accident, it is
1012fe8fb19SBen Gras  * XXX so it must remain so.
1022fe8fb19SBen Gras  */
1032fe8fb19SBen Gras /*static*/ void MD2Transform __P((MD2_CTX *));
1042fe8fb19SBen Gras 
1052fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(MD2Init,_MD2Init)1062fe8fb19SBen Gras __weak_alias(MD2Init,_MD2Init)
1072fe8fb19SBen Gras __weak_alias(MD2Update,_MD2Update)
1082fe8fb19SBen Gras __weak_alias(MD2Final,_MD2Final)
1092fe8fb19SBen Gras __weak_alias(MD2Transform,_MD2Transform)
1102fe8fb19SBen Gras #endif
1112fe8fb19SBen Gras 
1122fe8fb19SBen Gras void
113*f14fb602SLionel Sambuc MD2Init(MD2_CTX *context)
1142fe8fb19SBen Gras {
1152fe8fb19SBen Gras 	_DIAGASSERT(context != 0);
1162fe8fb19SBen Gras 
1172fe8fb19SBen Gras 	context->i = 16;
1182fe8fb19SBen Gras 	memset(&context->C[0], 0, sizeof(context->C));
1192fe8fb19SBen Gras 	memset(&context->X[0], 0, sizeof(context->X));
1202fe8fb19SBen Gras }
1212fe8fb19SBen Gras 
1222fe8fb19SBen Gras void
MD2Update(MD2_CTX * context,const unsigned char * input,unsigned int inputLen)123*f14fb602SLionel Sambuc MD2Update(MD2_CTX *context, const unsigned char *input, unsigned int inputLen)
1242fe8fb19SBen Gras {
1252fe8fb19SBen Gras 	unsigned int idx, piece;
1262fe8fb19SBen Gras 
1272fe8fb19SBen Gras 	_DIAGASSERT(context != 0);
1282fe8fb19SBen Gras 	_DIAGASSERT(input != 0);
1292fe8fb19SBen Gras 
1302fe8fb19SBen Gras 	for (idx = 0; idx < inputLen; idx += piece) {
1312fe8fb19SBen Gras 		piece = 32 - context->i;
1322fe8fb19SBen Gras 		if ((inputLen - idx) < piece)
1332fe8fb19SBen Gras 			piece = inputLen - idx;
1342fe8fb19SBen Gras 		memcpy(&context->X[context->i], &input[idx], (size_t)piece);
1352fe8fb19SBen Gras 		if ((context->i += piece) == 32)
1362fe8fb19SBen Gras 			MD2Transform(context); /* resets i */
1372fe8fb19SBen Gras 	}
1382fe8fb19SBen Gras }
1392fe8fb19SBen Gras 
1402fe8fb19SBen Gras void
MD2Final(unsigned char digest[16],MD2_CTX * context)141*f14fb602SLionel Sambuc MD2Final(unsigned char digest[16], MD2_CTX *context)
1422fe8fb19SBen Gras {
1432fe8fb19SBen Gras 	unsigned int padlen;
1442fe8fb19SBen Gras 
1452fe8fb19SBen Gras 	_DIAGASSERT(digest != 0);
1462fe8fb19SBen Gras 	_DIAGASSERT(context != 0);
1472fe8fb19SBen Gras 
1482fe8fb19SBen Gras 	/* padlen should be 1..16 */
1492fe8fb19SBen Gras 	padlen = 32 - context->i;
1502fe8fb19SBen Gras 
1512fe8fb19SBen Gras 	/* add padding */
1522fe8fb19SBen Gras 	MD2Update(context, pad[padlen], padlen);
1532fe8fb19SBen Gras 
1542fe8fb19SBen Gras 	/* add checksum */
1552fe8fb19SBen Gras 	MD2Update(context, &context->C[0], (unsigned int) sizeof(context->C));
1562fe8fb19SBen Gras 
1572fe8fb19SBen Gras 	/* copy out final digest */
1582fe8fb19SBen Gras 	memcpy(digest, &context->X[0], (size_t)16);
1592fe8fb19SBen Gras 
1602fe8fb19SBen Gras 	/* reset the context */
1612fe8fb19SBen Gras 	MD2Init(context);
1622fe8fb19SBen Gras }
1632fe8fb19SBen Gras 
1642fe8fb19SBen Gras /*static*/ void
MD2Transform(MD2_CTX * context)165*f14fb602SLionel Sambuc MD2Transform(MD2_CTX *context)
1662fe8fb19SBen Gras {
167*f14fb602SLionel Sambuc 	uint32_t l, j, k, t;
1682fe8fb19SBen Gras 
1692fe8fb19SBen Gras 	/* set block "3" and update "checksum" */
1702fe8fb19SBen Gras 	for (l = context->C[15], j = 0; j < 16; j++) {
1712fe8fb19SBen Gras 		context->X[32 + j] = context->X[j] ^ context->X[16 + j];
1722fe8fb19SBen Gras 		l = context->C[j] ^= S[context->X[16 + j] ^ l];
1732fe8fb19SBen Gras 	}
1742fe8fb19SBen Gras 
1752fe8fb19SBen Gras 	/* mangle input block */
1762fe8fb19SBen Gras 	for (t = j = 0; j < 18; t = (t + j) % 256, j++)
1772fe8fb19SBen Gras 		for (k = 0; k < 48; k++)
1782fe8fb19SBen Gras 			t = context->X[k] = (context->X[k] ^ S[t]);
1792fe8fb19SBen Gras 
1802fe8fb19SBen Gras 	/* reset input pointer */
1812fe8fb19SBen Gras 	context->i = 16;
1822fe8fb19SBen Gras }
1832fe8fb19SBen Gras 
1842fe8fb19SBen Gras #endif /* !HAVE_MD2_H */
185