1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * Copyright (C) 1998 by the FundsXpress, INC.
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.
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.
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.
32 find_cached_dkey(struct derived_key *list, const krb5_data *constant)
34 for (; list; list = list->next) {
35 if (data_eq(list->constant, *constant)) {
36 krb5_k_reference_key(NULL, list->dkey);
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)
49 struct derived_key *dkent = NULL;
52 /* Allocate fields for the new entry. */
53 dkent = malloc(sizeof(*dkent));
56 data = malloc(constant->length);
59 ret = krb5_k_create_key(NULL, dkeyblock, &dkey);
63 /* Add the new entry to the list. */
64 memcpy(data, constant->data, constant->length);
66 dkent->constant.data = data;
67 dkent->constant.length = constant->length;
68 dkent->next = key->derived;
71 /* Return a "copy" of the cached key. */
72 krb5_k_reference_key(NULL, dkey);
83 krb5int_derive_random(const struct krb5_enc_provider *enc,
84 krb5_key inkey, krb5_data *outrnd,
85 const krb5_data *in_constant)
87 size_t blocksize, keybytes, n;
91 blocksize = enc->block_size;
92 keybytes = enc->keybytes;
94 if (inkey->keyblock.length != enc->keylength || outrnd->length != keybytes)
95 return KRB5_CRYPTO_INTERNAL;
97 /* Allocate encryption data buffer. */
98 iov.flags = KRB5_CRYPTO_TYPE_DATA;
99 ret = alloc_data(&iov.data, blocksize);
103 /* Initialize the input block. */
104 if (in_constant->length == blocksize) {
105 memcpy(iov.data.data, in_constant->data, blocksize);
107 krb5int_nfold(in_constant->length * 8,
108 (unsigned char *) in_constant->data,
109 blocksize * 8, (unsigned char *) iov.data.data);
112 /* Loop encrypting the blocks until enough key bytes are generated. */
114 while (n < keybytes) {
115 ret = enc->encrypt(inkey, 0, &iov, 1);
119 if ((keybytes - n) <= blocksize) {
120 memcpy(outrnd->data + n, iov.data.data, (keybytes - n));
124 memcpy(outrnd->data + n, iov.data.data, blocksize);
129 zapfree(iov.data.data, blocksize);
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.
140 krb5int_derive_keyblock(const struct krb5_enc_provider *enc,
141 krb5_key inkey, krb5_keyblock *outkey,
142 const krb5_data *in_constant)
145 krb5_data rawkey = empty_data();
147 /* Allocate a buffer for the raw key bytes. */
148 ret = alloc_data(&rawkey, enc->keybytes);
152 /* Derive pseudo-random data for the key bytes. */
153 ret = krb5int_derive_random(enc, inkey, &rawkey, in_constant);
157 /* Postprocess the key. */
158 ret = enc->make_key(&rawkey, outkey);
161 zapfree(rawkey.data, enc->keybytes);
166 krb5int_derive_key(const struct krb5_enc_provider *enc,
167 krb5_key inkey, krb5_key *outkey,
168 const krb5_data *in_constant)
170 krb5_keyblock keyblock;
176 /* Check for a cached result. */
177 dkey = find_cached_dkey(inkey->derived, in_constant);
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)
192 ret = krb5int_derive_keyblock(enc, inkey, &keyblock, in_constant);
196 /* Cache the derived key. */
197 ret = add_cached_dkey(inkey, in_constant, &keyblock, &dkey);
204 zapfree(keyblock.contents, keyblock.length);