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