~ubuntu-branches/ubuntu/karmic/openipmi/karmic

« back to all changes in this revision

Viewing changes to utils/md2.c

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2005-07-04 21:29:17 UTC
  • Revision ID: james.westby@ubuntu.com-20050704212917-igddk5jawjmhrlay
Tags: upstream-2.0.1
Import upstream version 2.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* md2.c - MD2 Message-Digest Algorithm
 
2
 * Copyright (C) 2002,2003 MontaVista Software.
 
3
 * Corey Minyard <cminyard@mvista.com>
 
4
 *
 
5
 * This file is part of the IPMI Interface (IPMIIF).
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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
 
20
 *
 
21
 * This is the MD2 algorithm, as defined by RFC1319.
 
22
 */
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdint.h>
 
26
#include <string.h>
 
27
#include <errno.h>
 
28
#include <OpenIPMI/internal/md2.h>
 
29
 
 
30
typedef uint32_t u32;
 
31
typedef uint8_t  byte;
 
32
 
 
33
typedef struct {
 
34
    byte buf[48];
 
35
    byte inbuf[16];
 
36
    byte checksum[16];
 
37
    int  count;
 
38
    byte l;
 
39
} MD2_CONTEXT;
 
40
 
 
41
static void
 
42
md2_init( MD2_CONTEXT *ctx )
 
43
{
 
44
    memset(ctx->buf, 0, 16);
 
45
    memset(ctx->checksum, 0, 16);
 
46
    ctx->count = 0;
 
47
    ctx->l = 0;
 
48
}
 
49
 
 
50
/* The "Bytes of PI" defined by the algorithm. */
 
51
static byte s[256] =
 
52
{
 
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
 
69
};
 
70
 
 
71
static void
 
72
checksum( MD2_CONTEXT *ctx )
 
73
{
 
74
    int j;
 
75
 
 
76
    for (j=0; j<16; j++) {
 
77
        ctx->checksum[j] ^= s[ctx->inbuf[j] ^ ctx->l];
 
78
        ctx->l = ctx->checksum[j];
 
79
    }
 
80
}
 
81
 
 
82
/****************
 
83
 * transform 16 bytes
 
84
 */
 
85
static void
 
86
transform( MD2_CONTEXT *ctx )
 
87
{
 
88
    int j, k;
 
89
    int t;
 
90
 
 
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];
 
94
    }
 
95
 
 
96
    t = 0;
 
97
    for (j=0; j<18; j++) {
 
98
        for (k=0; k<48; k++) {
 
99
            t = ctx->buf[k] ^ s[t];
 
100
            ctx->buf[k] = t;
 
101
        }
 
102
        t = (t + j) % 256;
 
103
    }
 
104
}
 
105
 
 
106
 
 
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.
 
110
 */
 
111
static void
 
112
md2_write( MD2_CONTEXT *ctx, byte *inbuf, size_t inlen )
 
113
{
 
114
    int cnt;
 
115
 
 
116
    if( !inbuf )
 
117
        return;
 
118
 
 
119
    if (ctx->count+inlen > 16)
 
120
        cnt = 16-ctx->count;
 
121
    else
 
122
        cnt = inlen;
 
123
 
 
124
    memcpy(ctx->inbuf+ctx->count, inbuf, cnt);
 
125
    inbuf += cnt;
 
126
    inlen -= cnt;
 
127
    ctx->count += cnt;
 
128
 
 
129
    while (ctx->count == 16) {
 
130
        checksum(ctx);
 
131
        transform(ctx);
 
132
 
 
133
        if (inlen > 16)
 
134
            cnt = 16;
 
135
        else
 
136
            cnt = inlen;
 
137
 
 
138
        memcpy(ctx->inbuf, inbuf, cnt);
 
139
        inbuf += cnt;
 
140
        inlen -= cnt;
 
141
        ctx->count = cnt;
 
142
    }
 
143
}
 
144
 
 
145
 
 
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.
 
150
 */
 
151
 
 
152
static void
 
153
md2_final( MD2_CONTEXT *ctx )
 
154
{
 
155
    int i, cnt;
 
156
 
 
157
    cnt = 16 - ctx->count;
 
158
    for (i=ctx->count; i<16; i++)
 
159
        ctx->inbuf[i] = cnt;
 
160
 
 
161
    checksum(ctx);
 
162
    transform(ctx);
 
163
 
 
164
    memcpy(ctx->inbuf, ctx->checksum, 16);
 
165
 
 
166
    transform(ctx);
 
167
}
 
168
 
 
169
static byte *
 
170
md2_read( MD2_CONTEXT *hd )
 
171
{
 
172
    return hd->buf;
 
173
}
 
174
 
 
175
struct ipmi_authdata_s
 
176
{
 
177
    void          *info;
 
178
    void          *(*mem_alloc)(void *info, int size);
 
179
    void          (*mem_free)(void *info, void *data);
 
180
    unsigned char data[16];
 
181
};
 
182
 
 
183
/* External functions for the IPMI authcode algorithms. */
 
184
int
 
185
ipmi_md2_authcode_init(unsigned char   *password,
 
186
                       ipmi_authdata_t *handle,
 
187
                       void            *info,
 
188
                       void            *(*mem_alloc)(void *info, int size),
 
189
                       void            (*mem_free)(void *info, void *data))
 
190
{
 
191
    struct ipmi_authdata_s *data;
 
192
 
 
193
    data = mem_alloc(info, sizeof(*data));
 
194
    if (!data)
 
195
        return ENOMEM;
 
196
 
 
197
    data->info = info;
 
198
    data->mem_alloc = mem_alloc;
 
199
    data->mem_free = mem_free;
 
200
 
 
201
    memcpy(data->data, password, 16);
 
202
    *handle = data;
 
203
    return 0;
 
204
}
 
205
 
 
206
int
 
207
ipmi_md2_authcode_gen(ipmi_authdata_t handle,
 
208
                      ipmi_auth_sg_t  data[],
 
209
                      void            *output)
 
210
{
 
211
    MD2_CONTEXT ctx;
 
212
    int         i;
 
213
 
 
214
    md2_init(&ctx);
 
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);
 
218
    }
 
219
    md2_write(&ctx, handle->data, 16);
 
220
    md2_final(&ctx);
 
221
    memcpy(output, md2_read(&ctx), 16);
 
222
    return 0;
 
223
}
 
224
 
 
225
int
 
226
ipmi_md2_authcode_check(ipmi_authdata_t handle,
 
227
                        ipmi_auth_sg_t  data[],
 
228
                        void            *code)
 
229
{
 
230
    MD2_CONTEXT ctx;
 
231
    int         i;
 
232
 
 
233
    md2_init(&ctx);
 
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);
 
237
    }
 
238
    md2_write(&ctx, handle->data, 16);
 
239
    md2_final(&ctx);
 
240
    if (memcmp(code, md2_read(&ctx), 16) != 0)
 
241
        return EINVAL;
 
242
    return 0;
 
243
}
 
244
 
 
245
void
 
246
ipmi_md2_authcode_cleanup(ipmi_authdata_t handle)
 
247
{
 
248
    memset(handle->data, 0, sizeof(handle->data));
 
249
    handle->mem_free(handle->info, handle);
 
250
    handle = NULL;
 
251
}
 
252
 
 
253
/* The stuff below is libgcrypt-specific, and does not apply to IPMI.  The
 
254
   stuff above is generic.  Nice separation, thank you :-).
 
255
   -Corey Minyard
 
256
*/
 
257
#if 0
 
258
/****************
 
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.
 
263
 */
 
264
static const char *
 
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 )
 
271
             )
 
272
{
 
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 };
 
276
 
 
277
    if( algo != 1 )
 
278
        return NULL;
 
279
 
 
280
    *contextsize = sizeof(MD2_CONTEXT);
 
281
    *r_asnoid = asn;
 
282
    *r_asnlen = DIM(asn);
 
283
    *r_mdlen = 16;
 
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;
 
288
 
 
289
    return "MD2";
 
290
}
 
291
 
 
292
 
 
293
#ifndef IS_MODULE
 
294
static
 
295
#endif
 
296
const char * const gnupgext_version = "MD2 ($Revision: 1.3 $)";
 
297
 
 
298
static struct {
 
299
    int class;
 
300
    int version;
 
301
    int  value;
 
302
    void (*func)(void);
 
303
} func_table[] = {
 
304
    { 10, 1, 0, (void(*)(void))md2_get_info },
 
305
    { 11, 1, 1 },
 
306
};
 
307
 
 
308
 
 
309
#ifndef IS_MODULE
 
310
static
 
311
#endif
 
312
void *
 
313
gnupgext_enum_func( int what, int *sequence, int *class, int *vers )
 
314
{
 
315
    void *ret;
 
316
    int i = *sequence;
 
317
 
 
318
    do {
 
319
        if( i >= DIM(func_table) || i < 0 )
 
320
            return NULL;
 
321
        *class = func_table[i].class;
 
322
        *vers  = func_table[i].version;
 
323
        switch( *class ) {
 
324
          case 11: case 21: case 31: ret = &func_table[i].value; break;
 
325
          default:                   ret = func_table[i].func; break;
 
326
        }
 
327
        i++;
 
328
    } while( what && what != *class );
 
329
 
 
330
    *sequence = i;
 
331
    return ret;
 
332
}
 
333
 
 
334
 
 
335
 
 
336
 
 
337
#ifndef IS_MODULE
 
338
void
 
339
_gcry_md2_constructor(void)
 
340
{
 
341
    _gcry_register_internal_cipher_extension( gnupgext_version, gnupgext_enum_func );
 
342
}
 
343
#endif
 
344
#endif
 
345
/* end of file */