2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A
2N/A/*
2N/A * lib/crypto/aes/aes_s2k.c
2N/A *
2N/A * Copyright 2003 by the Massachusetts Institute of Technology.
2N/A * All Rights Reserved.
2N/A *
2N/A * Export of this software from the United States of America may
2N/A * require a specific license from the United States Government.
2N/A * It is the responsibility of any person or organization contemplating
2N/A * export to obtain such a license before exporting.
2N/A *
2N/A * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
2N/A * distribute this software and its documentation for any purpose and
2N/A * without fee is hereby granted, provided that the above copyright
2N/A * notice appear in all copies and that both that copyright notice and
2N/A * this permission notice appear in supporting documentation, and that
2N/A * the name of M.I.T. not be used in advertising or publicity pertaining
2N/A * to distribution of the software without specific, written prior
2N/A * permission. Furthermore if you modify this software you must label
2N/A * your software as modified software and not distribute it in such a
2N/A * fashion that it might be confused with the original M.I.T. software.
2N/A * M.I.T. makes no representations about the suitability of
2N/A * this software for any purpose. It is provided "as is" without express
2N/A * or implied warranty.
2N/A *
2N/A *
2N/A * krb5int_aes_string_to_key
2N/A */
2N/A
2N/A#include "k5-int.h"
2N/A#include "dk.h"
2N/A#include "aes_s2k.h"
2N/A
2N/A#define DEFAULT_ITERATION_COUNT 4096 /* was 0xb000L in earlier drafts */
2N/A#define MAX_ITERATION_COUNT 0x1000000L
2N/A
2N/Akrb5_error_code
2N/Akrb5int_aes_string_to_key(krb5_context context,
2N/A const struct krb5_enc_provider *enc,
2N/A const krb5_data *string,
2N/A const krb5_data *salt,
2N/A const krb5_data *params,
2N/A krb5_keyblock *key)
2N/A{
2N/A unsigned long iter_count;
2N/A krb5_data out;
2N/A static const krb5_data usage = { KV5M_DATA, 8, "kerberos" };
2N/A krb5_error_code err;
2N/A /* Solaris Kerberos */
2N/A krb5_keyblock *inkey = NULL;
2N/A
2N/A if (params) {
2N/A unsigned char *p = (unsigned char *) params->data;
2N/A if (params->length != 4)
2N/A return KRB5_ERR_BAD_S2K_PARAMS;
2N/A /* The first two need casts in case 'int' is 16 bits. */
2N/A iter_count = (((unsigned long)p[0] << 24)
2N/A | ((unsigned long)p[1] << 16)
2N/A | (p[2] << 8)
2N/A | (p[3]));
2N/A if (iter_count == 0) {
2N/A /*
2N/A iter_count = (1L << 16) << 16;
2N/A if (((iter_count >> 16) >> 16) != 1)
2N/A return KRB5_ERR_BAD_S2K_PARAMS;
2N/A */
2N/A iter_count = DEFAULT_ITERATION_COUNT;
2N/A }
2N/A } else
2N/A iter_count = DEFAULT_ITERATION_COUNT;
2N/A
2N/A /* This is not a protocol specification constraint; this is an
2N/A implementation limit, which should eventually be controlled by
2N/A a config file. */
2N/A if (iter_count >= MAX_ITERATION_COUNT)
2N/A return KRB5_ERR_BAD_S2K_PARAMS;
2N/A
2N/A /*
2N/A * Dense key space, no parity bits or anything, so take a shortcut
2N/A * and use the key contents buffer for the generated bytes.
2N/A */
2N/A /* Solaris Kerberos */
2N/A if (key->length != 16 && key->length != 32)
2N/A return KRB5_CRYPTO_INTERNAL;
2N/A out.data = (char *) key->contents;
2N/A out.length = key->length;
2N/A /* Solaris Kerberos */
2N/A err = krb5int_pbkdf2_hmac_sha1 (context, &out, iter_count, key->enctype,
2N/A string, salt);
2N/A if (err) {
2N/A memset(out.data, 0, out.length);
2N/A return err;
2N/A }
2N/A
2N/A /*
2N/A * Solaris Kerberos:
2N/A * The derive key operation below will not work correctly
2N/A * if the input and output key pointers are to the same
2N/A * data. This is because the key object handle (PKCS#11)
2N/A * gets out-of-sync with the original key when the contents
2N/A * are modified. We copy the original key here for use
2N/A * below in the derive_key step, then we free this key
2N/A * before exiting.
2N/A */
2N/A err = krb5_copy_keyblock(context, key, &inkey);
2N/A if (err) {
2N/A memset(out.data, 0, out.length);
2N/A return err;
2N/A }
2N/A
2N/A err = krb5_derive_key (context, enc, inkey, key, &usage);
2N/A if (err) {
2N/A memset(out.data, 0, out.length);
2N/A }
2N/A krb5_free_keyblock(context, inkey);
2N/A
2N/A return (err);
2N/A}