#include "k5-int.h"
#include "etypes.h"
#include "dk.h"
static krb5_error_code dr
(const struct krb5_enc_provider *enc, const krb5_keyblock *inkey,
unsigned char *outdata, const krb5_data *in_constant);
static krb5_boolean enctype_ok (krb5_enctype e)
{
switch (e) {
case ENCTYPE_DES_CBC_CRC:
case ENCTYPE_DES_CBC_MD4:
case ENCTYPE_DES_CBC_MD5:
case ENCTYPE_DES3_CBC_SHA1:
return 1;
default:
return 0;
}
}
krb5_error_code krb5int_c_combine_keys
(krb5_context context, krb5_keyblock *key1, krb5_keyblock *key2, krb5_keyblock *outkey)
{
unsigned char *r1, *r2, *combined, *rnd, *output;
size_t keybytes, keylength;
const struct krb5_enc_provider *enc;
krb5_data input, randbits;
krb5_keyblock tkey;
krb5_error_code ret;
int i, myalloc = 0;
if (!(enctype_ok(key1->enctype)&&enctype_ok(key2->enctype)))
return (KRB5_CRYPTO_INTERNAL);
if (key1->length != key2->length || key1->enctype != key2->enctype)
return (KRB5_CRYPTO_INTERNAL);
for (i = 0; i < krb5_enctypes_length; i++) {
if (krb5_enctypes_list[i].etype == key1->enctype)
break;
}
if (i == krb5_enctypes_length)
return (KRB5_BAD_ENCTYPE);
enc = krb5_enctypes_list[i].enc;
keybytes = enc->keybytes;
keylength = enc->keylength;
if ((r1 = (unsigned char *) malloc(keybytes)) == NULL)
return (ENOMEM);
if ((r2 = (unsigned char *) malloc(keybytes)) == NULL) {
free(r1);
return (ENOMEM);
}
if ((rnd = (unsigned char *) malloc(keybytes)) == NULL) {
free(r1);
free(r2);
return (ENOMEM);
}
if ((combined = (unsigned char *) malloc(keybytes * 2)) == NULL) {
free(r1);
free(r2);
free(rnd);
return (ENOMEM);
}
if ((output = (unsigned char *) malloc(keylength)) == NULL) {
free(r1);
free(r2);
free(rnd);
free(combined);
return (ENOMEM);
}
input.length = key2->length;
input.data = (char *) key2->contents;
if ((ret = dr(enc, key1, r1, &input)))
goto cleanup;
#if 0
{
int i;
printf("R1 =");
for (i = 0; i < keybytes; i++)
printf(" %02x", (unsigned char) r1[i]);
printf("\n");
}
#endif
input.length = key1->length;
input.data = (char *) key1->contents;
if ((ret = dr(enc, key2, r2, &input)))
goto cleanup;
#if 0
{
int i;
printf("R2 =");
for (i = 0; i < keybytes; i++)
printf(" %02x", (unsigned char) r2[i]);
printf("\n");
}
#endif
memcpy(combined, r1, keybytes);
memcpy(combined + keybytes, r2, keybytes);
krb5_nfold((keybytes * 2) * 8, combined, keybytes * 8, rnd);
#if 0
{
int i;
printf("rnd =");
for (i = 0; i < keybytes; i++)
printf(" %02x", (unsigned char) rnd[i]);
printf("\n");
}
#endif
randbits.length = keybytes;
randbits.data = (char *) rnd;
tkey.length = keylength;
tkey.contents = output;
if ((ret = (*(enc->make_key))(&randbits, &tkey)))
goto cleanup;
#if 0
{
int i;
printf("tkey =");
for (i = 0; i < tkey.length; i++)
printf(" %02x", (unsigned char) tkey.contents[i]);
printf("\n");
}
#endif
input.length = 7;
input.data = "combine";
if (outkey->length == 0 || outkey->contents == NULL) {
outkey->contents = (krb5_octet *) malloc(keylength);
if (!outkey->contents) {
ret = ENOMEM;
goto cleanup;
}
outkey->length = keylength;
outkey->enctype = key1->enctype;
myalloc = 1;
}
if ((ret = krb5_derive_key(enc, &tkey, outkey, &input))) {
if (myalloc) {
free(outkey->contents);
outkey->contents = NULL;
}
goto cleanup;
}
#if 0
{
int i;
printf("output =");
for (i = 0; i < outkey->length; i++)
printf(" %02x", (unsigned char) outkey->contents[i]);
printf("\n");
}
#endif
ret = 0;
cleanup:
memset(r1, 0, keybytes);
memset(r2, 0, keybytes);
memset(rnd, 0, keybytes);
memset(combined, 0, keybytes * 2);
memset(output, 0, keylength);
free(r1);
free(r2);
free(rnd);
free(combined);
free(output);
return (ret);
}
static krb5_error_code dr
(const struct krb5_enc_provider *enc, const krb5_keyblock *inkey, unsigned char *out, const krb5_data *in_constant)
{
size_t blocksize, keybytes, keylength, n;
unsigned char *inblockdata, *outblockdata;
krb5_data inblock, outblock;
blocksize = enc->block_size;
keybytes = enc->keybytes;
keylength = enc->keylength;
if ((inblockdata = (unsigned char *) malloc(blocksize)) == NULL)
return(ENOMEM);
if ((outblockdata = (unsigned char *) malloc(blocksize)) == NULL) {
free(inblockdata);
return(ENOMEM);
}
inblock.data = (char *) inblockdata;
inblock.length = blocksize;
outblock.data = (char *) outblockdata;
outblock.length = blocksize;
if (in_constant->length == inblock.length) {
memcpy(inblock.data, in_constant->data, inblock.length);
} else {
krb5_nfold(in_constant->length*8, (unsigned char *) in_constant->data,
inblock.length*8, (unsigned char *) inblock.data);
}
n = 0;
while (n < keybytes) {
(*(enc->encrypt))(inkey, 0, &inblock, &outblock);
if ((keybytes - n) <= outblock.length) {
memcpy(out+n, outblock.data, (keybytes - n));
break;
}
memcpy(out+n, outblock.data, outblock.length);
memcpy(inblock.data, outblock.data, outblock.length);
n += outblock.length;
}
memset(inblockdata, 0, blocksize);
memset(outblockdata, 0, blocksize);
free(outblockdata);
free(inblockdata);
return(0);
}