5fd8876479982078caf79ca6a7e4f00f87f15067
[krb5.git] / src / lib / crypto / krb / dk / derive.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright (C) 1998 by the FundsXpress, INC.
4  *
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may require
8  * a specific license from the United States Government.  It is the
9  * responsibility of any person or organization contemplating export to
10  * 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 FundsXpress. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  FundsXpress makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
25  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26  */
27
28 #include "k5-int.h"
29 #include "dk.h"
30
31 static krb5_key
32 find_cached_dkey(struct derived_key *list, const krb5_data *constant)
33 {
34     for (; list; list = list->next) {
35         if (data_eq(list->constant, *constant)) {
36             krb5_k_reference_key(NULL, list->dkey);
37             return list->dkey;
38         }
39     }
40     return NULL;
41 }
42
43 static krb5_error_code
44 add_cached_dkey(krb5_key key, const krb5_data *constant,
45                 const krb5_keyblock *dkeyblock, krb5_key *cached_dkey)
46 {
47     krb5_key dkey;
48     krb5_error_code ret;
49     struct derived_key *dkent = NULL;
50     char *data = NULL;
51
52     /* Allocate fields for the new entry. */
53     dkent = malloc(sizeof(*dkent));
54     if (dkent == NULL)
55         goto cleanup;
56     data = malloc(constant->length);
57     if (data == NULL)
58         goto cleanup;
59     ret = krb5_k_create_key(NULL, dkeyblock, &dkey);
60     if (ret != 0)
61         goto cleanup;
62
63     /* Add the new entry to the list. */
64     memcpy(data, constant->data, constant->length);
65     dkent->dkey = dkey;
66     dkent->constant.data = data;
67     dkent->constant.length = constant->length;
68     dkent->next = key->derived;
69     key->derived = dkent;
70
71     /* Return a "copy" of the cached key. */
72     krb5_k_reference_key(NULL, dkey);
73     *cached_dkey = dkey;
74     return 0;
75
76 cleanup:
77     free(dkent);
78     free(data);
79     return ENOMEM;
80 }
81
82 krb5_error_code
83 krb5int_derive_random(const struct krb5_enc_provider *enc,
84                       krb5_key inkey, krb5_data *outrnd,
85                       const krb5_data *in_constant)
86 {
87     size_t blocksize, keybytes, n;
88     krb5_crypto_iov iov;
89     krb5_error_code ret;
90
91     blocksize = enc->block_size;
92     keybytes = enc->keybytes;
93
94     if (inkey->keyblock.length != enc->keylength || outrnd->length != keybytes)
95         return KRB5_CRYPTO_INTERNAL;
96
97     /* Allocate encryption data buffer. */
98     iov.flags = KRB5_CRYPTO_TYPE_DATA;
99     ret = alloc_data(&iov.data, blocksize);
100     if (ret)
101         return ret;
102
103     /* Initialize the input block. */
104     if (in_constant->length == blocksize) {
105         memcpy(iov.data.data, in_constant->data, blocksize);
106     } else {
107         krb5int_nfold(in_constant->length * 8,
108                       (unsigned char *) in_constant->data,
109                       blocksize * 8, (unsigned char *) iov.data.data);
110     }
111
112     /* Loop encrypting the blocks until enough key bytes are generated. */
113     n = 0;
114     while (n < keybytes) {
115         ret = enc->encrypt(inkey, 0, &iov, 1);
116         if (ret)
117             goto cleanup;
118
119         if ((keybytes - n) <= blocksize) {
120             memcpy(outrnd->data + n, iov.data.data, (keybytes - n));
121             break;
122         }
123
124         memcpy(outrnd->data + n, iov.data.data, blocksize);
125         n += blocksize;
126     }
127
128 cleanup:
129     zapfree(iov.data.data, blocksize);
130     return ret;
131 }
132
133 /*
134  * Compute a derived key into the keyblock outkey.  This variation on
135  * krb5int_derive_key does not cache the result, as it is only used
136  * directly in situations which are not expected to be repeated with
137  * the same inkey and constant.
138  */
139 krb5_error_code
140 krb5int_derive_keyblock(const struct krb5_enc_provider *enc,
141                         krb5_key inkey, krb5_keyblock *outkey,
142                         const krb5_data *in_constant)
143 {
144     krb5_error_code ret;
145     krb5_data rawkey = empty_data();
146
147     /* Allocate a buffer for the raw key bytes. */
148     ret = alloc_data(&rawkey, enc->keybytes);
149     if (ret)
150         goto cleanup;
151
152     /* Derive pseudo-random data for the key bytes. */
153     ret = krb5int_derive_random(enc, inkey, &rawkey, in_constant);
154     if (ret)
155         goto cleanup;
156
157     /* Postprocess the key. */
158     ret = enc->make_key(&rawkey, outkey);
159
160 cleanup:
161     zapfree(rawkey.data, enc->keybytes);
162     return ret;
163 }
164
165 krb5_error_code
166 krb5int_derive_key(const struct krb5_enc_provider *enc,
167                    krb5_key inkey, krb5_key *outkey,
168                    const krb5_data *in_constant)
169 {
170     krb5_keyblock keyblock;
171     krb5_error_code ret;
172     krb5_key dkey;
173
174     *outkey = NULL;
175
176     /* Check for a cached result. */
177     dkey = find_cached_dkey(inkey->derived, in_constant);
178     if (dkey != NULL) {
179         *outkey = dkey;
180         return 0;
181     }
182
183     /* Derive into a temporary keyblock. */
184     keyblock.length = enc->keylength;
185     keyblock.contents = malloc(keyblock.length);
186     /* Set the enctype as the krb5_k_free_key will iterate over list
187        or derived keys and invoke krb5_k_free_key which will lookup
188        the enctype for key_cleanup handler */
189     keyblock.enctype = inkey->keyblock.enctype;
190     if (keyblock.contents == NULL)
191         return ENOMEM;
192     ret = krb5int_derive_keyblock(enc, inkey, &keyblock, in_constant);
193     if (ret)
194         goto cleanup;
195
196     /* Cache the derived key. */
197     ret = add_cached_dkey(inkey, in_constant, &keyblock, &dkey);
198     if (ret != 0)
199         goto cleanup;
200
201     *outkey = dkey;
202
203 cleanup:
204     zapfree(keyblock.contents, keyblock.length);
205     return ret;
206 }