~ubuntu-branches/ubuntu/maverick/krb5/maverick

« back to all changes in this revision

Viewing changes to src/lib/des425/verify.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman
  • Date: 2009-05-07 16:16:34 UTC
  • mfrom: (13.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20090507161634-xqyk0s9na0le4flj
Tags: 1.7dfsg~beta1-4
When  decrypting the TGS response fails with the subkey, try with the
session key to work around Heimdal bug, Closes: #527353 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * lib/des425/verify.c
3
 
 *
4
 
 * Copyright 1988,1990 by the Massachusetts Institute of Technology.
5
 
 * All Rights Reserved.
6
 
 *
7
 
 * Export of this software from the United States of America may
8
 
 *   require a specific license from the United States Government.
9
 
 *   It is the responsibility of any person or organization contemplating
10
 
 *   export to obtain such a license before exporting.
11
 
 * 
12
 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13
 
 * distribute this software and its documentation for any purpose and
14
 
 * without fee is hereby granted, provided that the above copyright
15
 
 * notice appear in all copies and that both that copyright notice and
16
 
 * this permission notice appear in supporting documentation, and that
17
 
 * the name of M.I.T. not be used in advertising or publicity pertaining
18
 
 * to distribution of the software without specific, written prior
19
 
 * permission.  Furthermore if you modify this software you must label
20
 
 * your software as modified software and not distribute it in such a
21
 
 * fashion that it might be confused with the original M.I.T. software.
22
 
 * M.I.T. makes no representations about the suitability of
23
 
 * this software for any purpose.  It is provided "as is" without express
24
 
 * or implied warranty.
25
 
 * 
26
 
 *
27
 
 * Program to test the correctness of the DES library
28
 
 * implementation.
29
 
 *
30
 
 * exit returns  0 ==> success
31
 
 *              -1 ==> error
32
 
 */
33
 
 
34
 
 
35
 
#include <stdio.h>
36
 
#include <errno.h>
37
 
#include "des_int.h"
38
 
#include "des.h"
39
 
 
40
 
char *progname;
41
 
int nflag = 2;
42
 
int vflag;
43
 
int mflag;
44
 
int zflag;
45
 
int pid;
46
 
int des_debug;
47
 
des_key_schedule KS;
48
 
unsigned char cipher_text[64];
49
 
unsigned char clear_text[64] = "Now is the time for all " ;
50
 
unsigned char clear_text2[64] = "7654321 Now is the time for ";
51
 
unsigned char clear_text3[64] = {2,0,0,0, 1,0,0,0};
52
 
unsigned char output[64];
53
 
unsigned char zero_text[8] = {0x0,0,0,0,0,0,0,0};
54
 
unsigned char msb_text[8] = {0x0,0,0,0, 0,0,0,0x40}; /* to ANSI MSB */
55
 
unsigned char *input;
56
 
 
57
 
/* 0x0123456789abcdef */
58
 
unsigned char default_key[8] = {
59
 
    0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
60
 
};
61
 
unsigned char key2[8] = { 0x08,0x19,0x2a,0x3b,0x4c,0x5d,0x6e,0x7f };
62
 
unsigned char key3[8] = { 0x80,1,1,1,1,1,1,1 };
63
 
des_cblock s_key;
64
 
unsigned char default_ivec[8] = {
65
 
    0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
66
 
};
67
 
unsigned char *ivec;
68
 
unsigned char zero_key[8] = {1,1,1,1,1,1,1,1}; /* just parity bits */
69
 
int i,j;
70
 
 
71
 
unsigned char cipher1[8] = {
72
 
    0x25,0xdd,0xac,0x3e,0x96,0x17,0x64,0x67
73
 
};
74
 
unsigned char cipher2[8] = {
75
 
    0x3f,0xa4,0x0e,0x8a,0x98,0x4d,0x48,0x15
76
 
};
77
 
unsigned char cipher3[64] = {
78
 
    0xe5,0xc7,0xcd,0xde,0x87,0x2b,0xf2,0x7c,
79
 
    0x43,0xe9,0x34,0x00,0x8c,0x38,0x9c,0x0f,
80
 
    0x68,0x37,0x88,0x49,0x9a,0x7c,0x05,0xf6
81
 
};
82
 
unsigned char checksum[8] = {
83
 
    0x58,0xd2,0xe7,0x7e,0x86,0x06,0x27,0x33
84
 
};
85
 
 
86
 
unsigned char zresult[8] = {
87
 
    0x8c, 0xa6, 0x4d, 0xe9, 0xc1, 0xb1, 0x23, 0xa7
88
 
};
89
 
 
90
 
unsigned char mresult[8] = {
91
 
    0xa3, 0x80, 0xe0, 0x2a, 0x6b, 0xe5, 0x46, 0x96
92
 
};
93
 
 
94
 
   
95
 
/*
96
 
 * Can also add :
97
 
 * plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
98
 
 */
99
 
 
100
 
void do_encrypt (unsigned char *, unsigned char *);
101
 
void do_decrypt (unsigned char *, unsigned char *);
102
 
 
103
 
int
104
 
main(argc,argv)
105
 
    int argc;
106
 
    char *argv[];
107
 
{
108
 
    /* Local Declarations */
109
 
    unsigned long in_length;
110
 
 
111
 
    progname=argv[0];           /* salt away invoking program */
112
 
 
113
 
    while (--argc > 0 && (*++argv)[0] == '-')
114
 
        for (i=1; argv[0][i] != '\0'; i++) {
115
 
            switch (argv[0][i]) {
116
 
 
117
 
                /* debug flag */
118
 
            case 'd':
119
 
                des_debug=3;
120
 
                continue;
121
 
 
122
 
            case 'z':
123
 
                zflag = 1;
124
 
                continue;
125
 
 
126
 
            case 'm':
127
 
                mflag = 1;
128
 
                continue;
129
 
 
130
 
            default:
131
 
                printf("%s: illegal flag \"%c\" ",
132
 
                       progname,argv[0][i]);
133
 
                exit(1);
134
 
            }
135
 
        };
136
 
 
137
 
    if (argc) {
138
 
        fprintf(stderr, "Usage: %s [-dmz]\n", progname);
139
 
        exit(1);
140
 
    }
141
 
 
142
 
    /* use known input and key */
143
 
 
144
 
    /* ECB zero text zero key */
145
 
    if (zflag) {
146
 
        input = zero_text;
147
 
        des_key_sched(zero_key,KS);
148
 
        printf("plaintext = key = 0, cipher = 0x8ca64de9c1b123a7\n");
149
 
        do_encrypt(input,cipher_text);
150
 
        printf("\tcipher  = (low to high bytes)\n\t\t");
151
 
        for (j = 0; j<=7; j++)
152
 
            printf("%02x ",cipher_text[j]);
153
 
        printf("\n");
154
 
        do_decrypt(output,cipher_text);
155
 
        if ( memcmp((char *)cipher_text, (char *)zresult, 8) ) {
156
 
            printf("verify: error in zero key test\n");
157
 
            exit(-1);
158
 
        }
159
 
        exit(0);
160
 
    }
161
 
 
162
 
    if (mflag) {
163
 
        input = msb_text;
164
 
        des_key_sched(key3,KS);
165
 
        printf("plaintext = 0x00 00 00 00 00 00 00 40, ");
166
 
        printf("key = 0, cipher = 0x??\n");
167
 
        do_encrypt(input,cipher_text);
168
 
        printf("\tcipher  = (low to high bytes)\n\t\t");
169
 
        for (j = 0; j<=7; j++) {
170
 
            printf("%02x ",cipher_text[j]);
171
 
        }
172
 
        printf("\n");
173
 
        do_decrypt(output,cipher_text);
174
 
        if ( memcmp((char *)cipher_text, (char *)mresult, 8) ) {
175
 
            printf("verify: error in msb test\n");
176
 
            exit(-1);
177
 
        }
178
 
        exit(0);
179
 
    }
180
 
 
181
 
    /* ECB mode Davies and Price */
182
 
    {
183
 
        input = zero_text;
184
 
        des_key_sched(key2,KS);
185
 
        printf("Examples per FIPS publication 81, keys ivs and cipher\n");
186
 
        printf("in hex.  These are the correct answers, see below for\n");
187
 
        printf("the actual answers.\n\n");
188
 
        printf("Examples per Davies and Price.\n\n");
189
 
        printf("EXAMPLE ECB\tkey = 08192a3b4c5d6e7f\n");
190
 
        printf("\tclear = 0\n");
191
 
        printf("\tcipher = 25 dd ac 3e 96 17 64 67\n");
192
 
        printf("ACTUAL ECB\n");
193
 
        printf("\tclear \"%s\"\n", input);
194
 
        do_encrypt(input,cipher_text);
195
 
        printf("\tcipher  = (low to high bytes)\n\t\t");
196
 
        for (j = 0; j<=7; j++)
197
 
            printf("%02x ",cipher_text[j]);
198
 
        printf("\n\n");
199
 
        do_decrypt(output,cipher_text);
200
 
        if ( memcmp((char *)cipher_text, (char *)cipher1, 8) ) {
201
 
            printf("verify: error in ECB encryption\n");
202
 
            exit(-1);
203
 
        }
204
 
        else 
205
 
            printf("verify: ECB encription is correct\n\n");
206
 
    }
207
 
 
208
 
    /* ECB mode */
209
 
    {
210
 
        des_key_sched(default_key,KS);
211
 
        input = clear_text;
212
 
        ivec = default_ivec;
213
 
        printf("EXAMPLE ECB\tkey = 0123456789abcdef\n");
214
 
        printf("\tclear = \"Now is the time for all \"\n");
215
 
        printf("\tcipher = 3f a4 0e 8a 98 4d 48 15 ...\n");
216
 
        printf("ACTUAL ECB\n\tclear \"%s\"",input);
217
 
        do_encrypt(input,cipher_text);
218
 
        printf("\n\tcipher      = (low to high bytes)\n\t\t");
219
 
        for (j = 0; j<=7; j++) {
220
 
            printf("%02x ",cipher_text[j]);
221
 
        }
222
 
        printf("\n\n");
223
 
        do_decrypt(output,cipher_text);
224
 
        if ( memcmp((char *)cipher_text, (char *)cipher2, 8) ) {
225
 
            printf("verify: error in ECB encryption\n");
226
 
            exit(-1);
227
 
        }
228
 
        else 
229
 
            printf("verify: ECB encription is correct\n\n");
230
 
    }
231
 
 
232
 
    /* CBC mode */
233
 
    printf("EXAMPLE CBC\tkey = 0123456789abcdef");
234
 
    printf("\tiv = 1234567890abcdef\n");
235
 
    printf("\tclear = \"Now is the time for all \"\n");
236
 
    printf("\tcipher =\te5 c7 cd de 87 2b f2 7c\n");
237
 
    printf("\t\t\t43 e9 34 00 8c 38 9c 0f\n");
238
 
    printf("\t\t\t68 37 88 49 9a 7c 05 f6\n");
239
 
 
240
 
    printf("ACTUAL CBC\n\tclear \"%s\"\n",input);
241
 
    in_length = strlen((char *) input);
242
 
    des_cbc_encrypt(input,cipher_text, in_length,KS,ivec,1);
243
 
    printf("\tciphertext = (low to high bytes)\n");
244
 
    for (i = 0; i <= 7; i++) {
245
 
        printf("\t\t");
246
 
        for (j = 0; j <= 7; j++) {
247
 
            printf("%02x ",cipher_text[i*8+j]);
248
 
        }
249
 
        printf("\n");
250
 
    }
251
 
    des_cbc_encrypt(cipher_text,clear_text,in_length,KS,ivec,0);
252
 
    printf("\tdecrypted clear_text = \"%s\"\n",clear_text);
253
 
 
254
 
    if ( memcmp(cipher_text, cipher3, (size_t) in_length) ) {
255
 
        printf("verify: error in CBC encryption\n");
256
 
        exit(-1);
257
 
    }
258
 
    else 
259
 
        printf("verify: CBC encription is correct\n\n");
260
 
 
261
 
    printf("EXAMPLE CBC checksum");
262
 
    printf("\tkey =  0123456789abcdef\tiv =  1234567890abcdef\n");
263
 
    printf("\tclear =\t\t\"7654321 Now is the time for \"\n");
264
 
    printf("\tchecksum\t58 d2 e7 7e 86 06 27 33, ");
265
 
    printf("or some part thereof\n");
266
 
    input = clear_text2;
267
 
    des_cbc_cksum(input,cipher_text,(long) strlen((char *) input),KS,ivec);
268
 
    printf("ACTUAL CBC checksum\n");
269
 
    printf("\t\tencrypted cksum = (low to high bytes)\n\t\t");
270
 
    for (j = 0; j<=7; j++)
271
 
        printf("%02x ",cipher_text[j]);
272
 
    printf("\n\n");
273
 
    if ( memcmp((char *)cipher_text, (char *)checksum, 8) ) {
274
 
        printf("verify: error in CBC cheksum\n");
275
 
        exit(-1);
276
 
    }
277
 
    else 
278
 
        printf("verify: CBC checksum is correct\n\n");
279
 
    exit(0);
280
 
}
281
 
 
282
 
void
283
 
do_encrypt(in,out)
284
 
    unsigned char *in;
285
 
    unsigned char *out;
286
 
{
287
 
    for (i =1; i<=nflag; i++) {
288
 
        des_ecb_encrypt((unsigned long *) in, (unsigned long *)out, KS, 1);
289
 
        if (des_debug) {
290
 
            printf("\nclear %s\n",in);
291
 
            for (j = 0; j<=7; j++)
292
 
                printf("%02X ",in[j] & 0xff);
293
 
            printf("\tcipher ");
294
 
            for (j = 0; j<=7; j++)
295
 
                printf("%02X ",out[j] & 0xff);
296
 
        }
297
 
    }
298
 
}
299
 
 
300
 
void
301
 
do_decrypt(in,out)
302
 
    unsigned char *out;
303
 
    unsigned char *in;
304
 
    /* try to invert it */
305
 
{
306
 
    for (i =1; i<=nflag; i++) {
307
 
        des_ecb_encrypt((unsigned long *) out, (unsigned long *)in,KS,0);
308
 
        if (des_debug) {
309
 
            printf("clear %s\n",in);
310
 
            for (j = 0; j<=7; j++)
311
 
                printf("%02X ",in[j] & 0xff);
312
 
            printf("\tcipher ");
313
 
            for (j = 0; j<=7; j++)
314
 
                printf("%02X ",out[j] & 0xff);
315
 
        }
316
 
    }
317
 
}