xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hcrypto/md2.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: md2.c,v 1.2 2014/03/27 16:10:45 apb Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2006 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "config.h"
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #include "hash.h"
39ebfedea0SLionel Sambuc #include "md2.h"
40ebfedea0SLionel Sambuc 
41ebfedea0SLionel Sambuc static const unsigned char subst[256] = {
42ebfedea0SLionel Sambuc   41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
43ebfedea0SLionel Sambuc   19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
44ebfedea0SLionel Sambuc   76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
45ebfedea0SLionel Sambuc   138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
46ebfedea0SLionel Sambuc   245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
47ebfedea0SLionel Sambuc   148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
48ebfedea0SLionel Sambuc   39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
49ebfedea0SLionel Sambuc   181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
50ebfedea0SLionel Sambuc   150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
51ebfedea0SLionel Sambuc   112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
52ebfedea0SLionel Sambuc   96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
53ebfedea0SLionel Sambuc   85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
54ebfedea0SLionel Sambuc   234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
55ebfedea0SLionel Sambuc   129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
56ebfedea0SLionel Sambuc   8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
57ebfedea0SLionel Sambuc   203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
58ebfedea0SLionel Sambuc   166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
59ebfedea0SLionel Sambuc   31, 26, 219, 153, 141, 51, 159, 17, 131, 20
60ebfedea0SLionel Sambuc };
61ebfedea0SLionel Sambuc 
62ebfedea0SLionel Sambuc void
MD2_Init(struct md2 * m)63ebfedea0SLionel Sambuc MD2_Init (struct md2 *m)
64ebfedea0SLionel Sambuc {
65ebfedea0SLionel Sambuc     memset(m, 0, sizeof(*m));
66ebfedea0SLionel Sambuc }
67ebfedea0SLionel Sambuc 
68ebfedea0SLionel Sambuc static void
calc(struct md2 * m,const void * v)69ebfedea0SLionel Sambuc calc(struct md2 *m, const void *v)
70ebfedea0SLionel Sambuc {
71ebfedea0SLionel Sambuc     unsigned char x[48], L;
72ebfedea0SLionel Sambuc     const unsigned char *p = v;
73ebfedea0SLionel Sambuc     int i, j, t;
74ebfedea0SLionel Sambuc 
75ebfedea0SLionel Sambuc     L = m->checksum[15];
76ebfedea0SLionel Sambuc     for (i = 0; i < 16; i++)
77ebfedea0SLionel Sambuc 	L = m->checksum[i] ^= subst[p[i] ^ L];
78ebfedea0SLionel Sambuc 
79ebfedea0SLionel Sambuc     for (i = 0; i < 16; i++) {
80ebfedea0SLionel Sambuc 	x[i]      = m->state[i];
81ebfedea0SLionel Sambuc 	x[i + 16] = p[i];
82ebfedea0SLionel Sambuc 	x[i + 32] = x[i] ^ p[i];
83ebfedea0SLionel Sambuc     }
84ebfedea0SLionel Sambuc 
85ebfedea0SLionel Sambuc     t = 0;
86ebfedea0SLionel Sambuc     for (i = 0; i < 18; i++) {
87ebfedea0SLionel Sambuc 	for (j = 0; j < 48; j++)
88ebfedea0SLionel Sambuc 	    t = x[j] ^= subst[t];
89ebfedea0SLionel Sambuc 	t = (t + i) & 0xff;
90ebfedea0SLionel Sambuc     }
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc     memcpy(m->state, x, 16);
93ebfedea0SLionel Sambuc     memset(x, 0, sizeof(x));
94ebfedea0SLionel Sambuc }
95ebfedea0SLionel Sambuc 
96ebfedea0SLionel Sambuc void
MD2_Update(struct md2 * m,const void * v,size_t len)97ebfedea0SLionel Sambuc MD2_Update (struct md2 *m, const void *v, size_t len)
98ebfedea0SLionel Sambuc {
99ebfedea0SLionel Sambuc     size_t idx = m->len & 0xf;
100ebfedea0SLionel Sambuc     const unsigned char *p = v;
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc     m->len += len;
103ebfedea0SLionel Sambuc     if (len + idx >= 16) {
104ebfedea0SLionel Sambuc 	if (idx) {
105ebfedea0SLionel Sambuc 	    memcpy(m->data + idx, p, 16 - idx);
106ebfedea0SLionel Sambuc 	    calc(m, m->data);
107ebfedea0SLionel Sambuc 	    p += 16;
108ebfedea0SLionel Sambuc 	    len -= 16 - idx;
109ebfedea0SLionel Sambuc 	}
110ebfedea0SLionel Sambuc 	while (len >= 16) {
111ebfedea0SLionel Sambuc 	    calc(m, p);
112ebfedea0SLionel Sambuc 	    p += 16;
113ebfedea0SLionel Sambuc 	    len -= 16;
114ebfedea0SLionel Sambuc 	}
115ebfedea0SLionel Sambuc 	idx = 0;
116ebfedea0SLionel Sambuc     }
117ebfedea0SLionel Sambuc 
118ebfedea0SLionel Sambuc     memcpy(m->data + idx, p, len);
119ebfedea0SLionel Sambuc }
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc void
MD2_Final(void * res,struct md2 * m)122ebfedea0SLionel Sambuc MD2_Final (void *res, struct md2 *m)
123ebfedea0SLionel Sambuc {
124ebfedea0SLionel Sambuc     unsigned char pad[16];
125ebfedea0SLionel Sambuc     size_t padlen;
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc     padlen = 16 - (m->len % 16);
128ebfedea0SLionel Sambuc     memset(pad, padlen, padlen);
129ebfedea0SLionel Sambuc 
130ebfedea0SLionel Sambuc     MD2_Update(m, pad, padlen);
131ebfedea0SLionel Sambuc     memcpy(pad, m->checksum, 16);
132ebfedea0SLionel Sambuc     MD2_Update(m, pad, 16);
133ebfedea0SLionel Sambuc 
134ebfedea0SLionel Sambuc     memcpy(res, m->state, MD2_DIGEST_LENGTH);
135*0a6a1f1dSLionel Sambuc     memset(m, 0, sizeof(*m));
136ebfedea0SLionel Sambuc }
137