xref: /netbsd-src/lib/libc/hash/md2/md2.c (revision b756f940a7442974149b02b90540105cb8e8b155)
1 /*	$NetBSD: md2.c,v 1.9 2024/01/26 21:34:01 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Brown.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: md2.c,v 1.9 2024/01/26 21:34:01 christos Exp $");
35 #endif /* LIBC_SCCS and not lint */
36 
37 #include "namespace.h"
38 
39 #include <sys/types.h>
40 
41 #include <assert.h>
42 #include <md2.h>
43 #include <string.h>
44 
45 #if HAVE_NBTOOL_CONFIG_H
46 #include "nbtool_config.h"
47 #endif
48 
49 #if !HAVE_MD2_H
50 
51 /* cut-n-pasted from rfc1319 */
52 static unsigned char S[256] = {
53 	41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
54 	19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
55 	76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
56 	138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
57 	245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
58 	148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
59 	39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
60 	181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
61 	150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
62 	112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
63 	96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
64 	85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
65 	234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
66 	129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
67 	8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
68 	203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
69 	166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
70 	31, 26, 219, 153, 141, 51, 159, 17, 131, 20
71 };
72 
73 /* cut-n-pasted from rfc1319 */
74 static const unsigned char *pad[] = {
75 	(const unsigned char *)"",
76 	(const unsigned char *)"\001",
77 	(const unsigned char *)"\002\002",
78 	(const unsigned char *)"\003\003\003",
79 	(const unsigned char *)"\004\004\004\004",
80 	(const unsigned char *)"\005\005\005\005\005",
81 	(const unsigned char *)"\006\006\006\006\006\006",
82 	(const unsigned char *)"\007\007\007\007\007\007\007",
83 	(const unsigned char *)"\010\010\010\010\010\010\010\010",
84 	(const unsigned char *)"\011\011\011\011\011\011\011\011\011",
85 	(const unsigned char *)"\012\012\012\012\012\012\012\012\012\012",
86 	(const unsigned char *)"\013\013\013\013\013\013\013\013\013\013\013",
87 	(const unsigned char *)
88 	"\014\014\014\014\014\014\014\014\014\014\014\014",
89 	(const unsigned char *)
90 	"\015\015\015\015\015\015\015\015\015\015\015\015\015",
91 	(const unsigned char *)
92 	"\016\016\016\016\016\016\016\016\016\016\016\016\016\016",
93 	(const unsigned char *)
94 	"\017\017\017\017\017\017\017\017\017\017\017\017\017\017\017",
95 	(const unsigned char *)
96 	"\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020"
97 };
98 
99 #ifdef __weak_alias
__weak_alias(MD2Init,_MD2Init)100 __weak_alias(MD2Init,_MD2Init)
101 __weak_alias(MD2Update,_MD2Update)
102 __weak_alias(MD2Final,_MD2Final)
103 __weak_alias(MD2Transform,_MD2Transform)
104 #endif
105 
106 /*
107  * XXX This should not be visible, but due to an accident, it is
108  * XXX so it must remain so.
109  */
110 /*static*/ void
111 MD2Transform(MD2_CTX *context)
112 {
113 	uint32_t l, j, k, t;
114 
115 	/* set block "3" and update "checksum" */
116 	for (l = context->C[15], j = 0; j < 16; j++) {
117 		context->X[32 + j] = context->X[j] ^ context->X[16 + j];
118 		l = context->C[j] ^= S[context->X[16 + j] ^ l];
119 	}
120 
121 	/* mangle input block */
122 	for (t = j = 0; j < 18; t = (t + j) % 256, j++)
123 		for (k = 0; k < 48; k++)
124 			t = context->X[k] = (context->X[k] ^ S[t]);
125 
126 	/* reset input pointer */
127 	context->i = 16;
128 }
129 
130 void
MD2Init(MD2_CTX * context)131 MD2Init(MD2_CTX *context)
132 {
133 	_DIAGASSERT(context != 0);
134 
135 	context->i = 16;
136 	memset(&context->C[0], 0, sizeof(context->C));
137 	memset(&context->X[0], 0, sizeof(context->X));
138 }
139 
140 void
MD2Update(MD2_CTX * context,const unsigned char * input,unsigned int inputLen)141 MD2Update(MD2_CTX *context, const unsigned char *input, unsigned int inputLen)
142 {
143 	unsigned int idx, piece;
144 
145 	_DIAGASSERT(context != 0);
146 	_DIAGASSERT(input != 0);
147 
148 	for (idx = 0; idx < inputLen; idx += piece) {
149 		piece = 32 - context->i;
150 		if ((inputLen - idx) < piece)
151 			piece = inputLen - idx;
152 		memcpy(&context->X[context->i], &input[idx], (size_t)piece);
153 		if ((context->i += piece) == 32)
154 			MD2Transform(context); /* resets i */
155 	}
156 }
157 
158 void
MD2Final(unsigned char digest[16],MD2_CTX * context)159 MD2Final(unsigned char digest[16], MD2_CTX *context)
160 {
161 	unsigned int padlen;
162 
163 	_DIAGASSERT(digest != 0);
164 	_DIAGASSERT(context != 0);
165 
166 	/* padlen should be 1..16 */
167 	padlen = 32 - context->i;
168 
169 	/* add padding */
170 	MD2Update(context, pad[padlen], padlen);
171 
172 	/* add checksum */
173 	MD2Update(context, &context->C[0], (unsigned int) sizeof(context->C));
174 
175 	/* copy out final digest */
176 	memcpy(digest, &context->X[0], (size_t)16);
177 
178 	/* reset the context */
179 	MD2Init(context);
180 }
181 
182 
183 #endif /* !HAVE_MD2_H */
184