1
/* md2.c - MD2 Message-Digest Algorithm
2
* Copyright (C) 2002,2003 MontaVista Software.
3
* Corey Minyard <cminyard@mvista.com>
5
* This file is part of the IPMI Interface (IPMIIF).
7
* IPMIIF is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* IPMIIF is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21
* This is the MD2 algorithm, as defined by RFC1319.
28
#include <OpenIPMI/internal/md2.h>
42
md2_init( MD2_CONTEXT *ctx )
44
memset(ctx->buf, 0, 16);
45
memset(ctx->checksum, 0, 16);
50
/* The "Bytes of PI" defined by the algorithm. */
53
41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, 19,
54
98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76, 130, 202,
55
30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138, 23, 229, 18,
56
190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142, 187, 47, 238, 122,
57
169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16, 137, 11, 34, 95, 33,
58
128, 127, 93, 154, 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3,
59
255, 25, 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198,
60
79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241,
61
69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2,
62
27, 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
63
85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38,
64
44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82,
65
106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74,
66
120, 136, 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57,
67
242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10,
68
49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
72
checksum( MD2_CONTEXT *ctx )
76
for (j=0; j<16; j++) {
77
ctx->checksum[j] ^= s[ctx->inbuf[j] ^ ctx->l];
78
ctx->l = ctx->checksum[j];
86
transform( MD2_CONTEXT *ctx )
91
for (j=0; j<16; j++) {
92
ctx->buf[j+16] = ctx->inbuf[j];
93
ctx->buf[j+32] = ctx->inbuf[j] ^ ctx->buf[j];
97
for (j=0; j<18; j++) {
98
for (k=0; k<48; k++) {
99
t = ctx->buf[k] ^ s[t];
107
/* The routine updates the message-digest context to
108
* account for the presence of each of the characters inBuf[0..inLen-1]
109
* in the message whose digest is being computed.
112
md2_write( MD2_CONTEXT *ctx, byte *inbuf, size_t inlen )
119
if (ctx->count+inlen > 16)
124
memcpy(ctx->inbuf+ctx->count, inbuf, cnt);
129
while (ctx->count == 16) {
138
memcpy(ctx->inbuf, inbuf, cnt);
146
/* The routine final terminates the message-digest computation and
147
* ends with the desired message digest in mdContext->digest[0...15].
148
* The handle is prepared for a new MD2 cycle.
149
* Returns 16 bytes representing the digest.
153
md2_final( MD2_CONTEXT *ctx )
157
cnt = 16 - ctx->count;
158
for (i=ctx->count; i<16; i++)
164
memcpy(ctx->inbuf, ctx->checksum, 16);
170
md2_read( MD2_CONTEXT *hd )
175
struct ipmi_authdata_s
178
void *(*mem_alloc)(void *info, int size);
179
void (*mem_free)(void *info, void *data);
180
unsigned char data[16];
183
/* External functions for the IPMI authcode algorithms. */
185
ipmi_md2_authcode_init(unsigned char *password,
186
ipmi_authdata_t *handle,
188
void *(*mem_alloc)(void *info, int size),
189
void (*mem_free)(void *info, void *data))
191
struct ipmi_authdata_s *data;
193
data = mem_alloc(info, sizeof(*data));
198
data->mem_alloc = mem_alloc;
199
data->mem_free = mem_free;
201
memcpy(data->data, password, 16);
207
ipmi_md2_authcode_gen(ipmi_authdata_t handle,
208
ipmi_auth_sg_t data[],
215
md2_write(&ctx, handle->data, 16);
216
for (i=0; data[i].data != NULL; i++) {
217
md2_write(&ctx, data[i].data, data[i].len);
219
md2_write(&ctx, handle->data, 16);
221
memcpy(output, md2_read(&ctx), 16);
226
ipmi_md2_authcode_check(ipmi_authdata_t handle,
227
ipmi_auth_sg_t data[],
234
md2_write(&ctx, handle->data, 16);
235
for (i=0; data[i].data != NULL; i++) {
236
md2_write(&ctx, data[i].data, data[i].len);
238
md2_write(&ctx, handle->data, 16);
240
if (memcmp(code, md2_read(&ctx), 16) != 0)
246
ipmi_md2_authcode_cleanup(ipmi_authdata_t handle)
248
memset(handle->data, 0, sizeof(handle->data));
249
handle->mem_free(handle->info, handle);
253
/* The stuff below is libgcrypt-specific, and does not apply to IPMI. The
254
stuff above is generic. Nice separation, thank you :-).
259
* Return some information about the algorithm. We need algo here to
260
* distinguish different flavors of the algorithm.
261
* Returns: A pointer to string describing the algorithm or NULL if
262
* the ALGO is invalid.
265
md2_get_info( int algo, size_t *contextsize,
266
byte **r_asnoid, int *r_asnlen, int *r_mdlen,
267
void (**r_init)( void *c ),
268
void (**r_write)( void *c, byte *buf, size_t nbytes ),
269
void (**r_final)( void *c ),
270
byte *(**r_read)( void *c )
273
static byte asn[18] = /* Object ID is 1.2.840.113549.2.5 */
274
{ 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,0x48,
275
0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
280
*contextsize = sizeof(MD2_CONTEXT);
282
*r_asnlen = DIM(asn);
284
*(void (**)(MD2_CONTEXT *))r_init = md2_init;
285
*(void (**)(MD2_CONTEXT *, byte*, size_t))r_write = md2_write;
286
*(void (**)(MD2_CONTEXT *))r_final = md2_final;
287
*(byte *(**)(MD2_CONTEXT *))r_read = md2_read;
296
const char * const gnupgext_version = "MD2 ($Revision: 1.3 $)";
304
{ 10, 1, 0, (void(*)(void))md2_get_info },
313
gnupgext_enum_func( int what, int *sequence, int *class, int *vers )
319
if( i >= DIM(func_table) || i < 0 )
321
*class = func_table[i].class;
322
*vers = func_table[i].version;
324
case 11: case 21: case 31: ret = &func_table[i].value; break;
325
default: ret = func_table[i].func; break;
328
} while( what && what != *class );
339
_gcry_md2_constructor(void)
341
_gcry_register_internal_cipher_extension( gnupgext_version, gnupgext_enum_func );