aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/preauth/pkinit/pkinit_kdf_test.c
blob: 710e1ff393374974a5de7ba1467008e0df638ac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* plugins/preauth/pkinit/pkinit_kdf_test.c */
/*
 * Copyright (C) 2011 by the Massachusetts Institute of Technology.
 * All rights reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

/*
 * pkinit_kdf_test.c -- Test to verify the correctness of the function
 * pkinit_alg_agility_kdf() in pkinit_crypto_openssl, which implements
 * the Key Derivation Function from the PKInit Algorithm Agility
 * document, currently draft-ietf-krb-wg-pkinit-alg-agility-04.txt.
 */

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <unistd.h>
#include <dirent.h>

#include "k5-platform.h"

#include "pkinit_crypto_openssl.h"

/**
 * Initialize a krb5_data from @a s, a constant string. Note @a s is evaluated
 * multiple times; this is acceptable for constants.
 */
#define DATA_FROM_STRING(s)                     \
    {0, sizeof(s)-1, (char *) s}


/* values from test vectors in the pkinit-alg-agility draft */
int secret_len = 256;
char twenty_as[10];
char eighteen_bs[9];
char party_u_name[] = "lha@SU.SE";
char party_v_name[] = "krbtgt/SU.SE@SU.SE";
int enctype_aes = ENCTYPE_AES256_CTS_HMAC_SHA1_96;
int enctype_des3 = ENCTYPE_DES3_CBC_SHA1;
const krb5_data lha_data = DATA_FROM_STRING("lha");

krb5_octet key1_hex[] =
{0xe6, 0xAB, 0x38, 0xC9, 0x41, 0x3E, 0x03, 0x5B,
 0xB0, 0x79, 0x20, 0x1E, 0xD0, 0xB6, 0xB7, 0x3D,
 0x8D, 0x49, 0xA8, 0x14, 0xA7, 0x37, 0xC0, 0x4E,
 0xE6, 0x64, 0x96, 0x14, 0x20, 0x6F, 0x73, 0xAD};

krb5_octet key2_hex[] =
{0x77, 0xEF, 0x4E, 0x48, 0xC4, 0x20, 0xAE, 0x3F,
 0xEC, 0x75, 0x10, 0x9D, 0x79, 0x81, 0x69, 0x7E,
 0xED, 0x5D, 0x29, 0x5C, 0x90, 0xc6, 0x25, 0x64,
 0xF7, 0xBF, 0xD1, 0x01, 0xFA, 0x9b, 0xC1, 0xD5};

krb5_octet key3_hex[] =
{0xD3, 0xC7, 0x8A, 0x79, 0xD6, 0x52, 0x13, 0xEF,
 0xE9, 0xA8, 0x26, 0xF7, 0x5D, 0xFB, 0x01, 0xF7,
 0x23, 0x62, 0xFB, 0x16, 0xFB, 0x01, 0xDA, 0xD6};

int
main(int argc, char **argv)
{
    /* arguments for calls to pkinit_alg_agility_kdf() */
    krb5_context context = 0;
    krb5_data secret;
    krb5_algorithm_identifier alg_id;
    krb5_data as_req;
    krb5_data pk_as_rep;
    krb5_keyblock key_block;

    /* other local variables */
    int retval = 0;
    int max_keylen = 2048;
    krb5_enctype enctype = 0;
    krb5_principal u_principal = NULL;
    krb5_principal v_principal = NULL;
    krb5_keyblock *key_block_ptr = &key_block;

    /* initialize variables that get malloc'ed, so cleanup is safe */
    krb5_init_context (&context);
    memset(&alg_id, 0, sizeof(alg_id));
    memset(&as_req, 0, sizeof(as_req));
    memset(&pk_as_rep, 0, sizeof(pk_as_rep));
    memset(&key_block, 0, sizeof(key_block));

    /* set up a 256-byte, ALL-ZEROS secret */
    if (NULL == (secret.data = malloc(secret_len))) {
        printf("ERROR in pkinit_kdf_test: Memory allocation failed.");
        retval = ENOMEM;
        goto cleanup;
    }
    secret.length = secret_len;
    memset(secret.data, 0, secret_len);

    /* set-up the partyUInfo and partyVInfo principals */
    if ((0 != (retval = krb5_parse_name(context, party_u_name,
                                        &u_principal))) ||
        (0 != (retval = krb5_parse_name(context, party_v_name,
                                        &v_principal)))) {
        printf("ERROR in pkinit_kdf_test: Error parsing names, retval = %d",
               retval);
        goto cleanup;
    }

    /* set-up the as_req and and pk_as_rep data */
    memset(twenty_as, 0xaa, sizeof(twenty_as));
    memset(eighteen_bs, 0xbb, sizeof(eighteen_bs));
    as_req.length = sizeof(twenty_as);
    as_req.data = twenty_as;

    pk_as_rep.length = sizeof(eighteen_bs);
    pk_as_rep.data = eighteen_bs;

    /* TEST 1:  SHA-1/AES */
    /* set up algorithm id */
    alg_id.algorithm.data = (char *)krb5_pkinit_sha1_oid;
    alg_id.algorithm.length = krb5_pkinit_sha1_oid_len;

    enctype = enctype_aes;

    /* set-up the key_block */
    if (0 != (retval = krb5_init_keyblock(context, enctype, max_keylen,
                                          &key_block_ptr))) {
        printf("ERROR in pkinit_kdf_test: can't init keyblock, retval = %d",
               retval);
        goto cleanup;

    }

    /* call pkinit_alg_agility_kdf() with test vector values*/
    if (0 != (retval = pkinit_alg_agility_kdf(context, &secret,
                                              &alg_id.algorithm,
                                              u_principal, v_principal,
                                              enctype, &as_req, &pk_as_rep,
                                              &key_block))) {
        printf("ERROR in pkinit_kdf_test: kdf call failed, retval = %d",
               retval);
        goto cleanup;
    }

    /* compare key to expected key value */

    if ((key_block.length == sizeof(key1_hex)) &&
        (0 == memcmp(key_block.contents, key1_hex, key_block.length))) {
        printf("SUCCESS: TEST 1 (SHA-1/AES), Correct key value generated.\n");
        retval = 0;
        /* free the keyblock contents, so we can use it for the next test */
        krb5_free_keyblock_contents(context, &key_block);
    } else {
        printf("FAILURE: TEST 1 (SHA-1/AES), Incorrect key value generated!\n");
        retval = 1;
        goto cleanup;
    }

    /* TEST 2: SHA-256/AES */
    /* set up algorithm id */
    alg_id.algorithm.data = (char *)krb5_pkinit_sha256_oid;
    alg_id.algorithm.length = krb5_pkinit_sha256_oid_len;

    enctype = enctype_aes;

    /* set-up the key_block */
    if (0 != (retval = krb5_init_keyblock(context, enctype, max_keylen,
                                          &key_block_ptr))) {
        printf("ERROR in pkinit_kdf_test: can't init keyblock, retval = %d",
               retval);
        goto cleanup;

    }

    /* call pkinit_alg_agility_kdf() with test vector values*/
    if (0 != (retval = pkinit_alg_agility_kdf(context, &secret,
                                              &alg_id.algorithm,
                                              u_principal, v_principal,
                                              enctype, &as_req, &pk_as_rep,
                                              &key_block))) {
        printf("ERROR in pkinit_kdf_test: kdf call failed, retval = %d",
               retval);
        goto cleanup;
    }

    /* compare key to expected key value */

    if ((key_block.length == sizeof(key2_hex)) &&
        (0 == memcmp(key_block.contents, key2_hex, key_block.length))) {
        printf("SUCCESS: TEST 2 (SHA-256/AES), Correct key value generated.\n");
        retval = 0;
        /* free the keyblock contents, so we can use it for the next test */
        krb5_free_keyblock_contents(context, &key_block);
    } else {
        printf("FAILURE: TEST 2 (SHA-256/AES), Incorrect key value generated!\n");
        retval = 1;
        goto cleanup;
    }

    /* TEST 3: SHA-512/DES3 */
    /* set up algorithm id */
    alg_id.algorithm.data = (char *)krb5_pkinit_sha512_oid;
    alg_id.algorithm.length = krb5_pkinit_sha512_oid_len;

    enctype = enctype_des3;

    /* set-up the key_block */
    if (0 != (retval = krb5_init_keyblock(context, enctype, max_keylen,
                                          &key_block_ptr))) {
        printf("ERROR in pkinit_kdf_test: can't init keyblock, retval = %d",
               retval);
        goto cleanup;

    }

    /* call pkinit_alg_agility_kdf() with test vector values*/
    if (0 != (retval = pkinit_alg_agility_kdf(context, &secret,
                                              &alg_id.algorithm,
                                              u_principal, v_principal,
                                              enctype, &as_req, &pk_as_rep,
                                              &key_block))) {
        printf("ERROR in pkinit_kdf_test: kdf call failed, retval = %d",
               retval);
        goto cleanup;
    }

    /* compare key to expected key value */

    if ((key_block.length == sizeof(key3_hex)) &&
        (0 == memcmp(key_block.contents, key3_hex, key_block.length))) {
        printf("SUCCESS: TEST 3 (SHA-512/DES3), Correct key value generated.\n");
        retval = 0;
    } else {
        printf("FAILURE: TEST 2 (SHA-512/DES3), Incorrect key value generated!\n");
        retval = 1;
        goto cleanup;
    }

cleanup:
    /* release all allocated resources, whether good or bad return */
    free(secret.data);
    free(u_principal);
    free(v_principal);
    krb5_free_keyblock_contents(context, &key_block);
    exit(retval);
}