1226031Sstas/*
2226031Sstas * Copyright (c) 1997 - 2008 Kungliga Tekniska H��gskolan
3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4226031Sstas * All rights reserved.
5226031Sstas *
6226031Sstas * Redistribution and use in source and binary forms, with or without
7226031Sstas * modification, are permitted provided that the following conditions
8226031Sstas * are met:
9226031Sstas *
10226031Sstas * 1. Redistributions of source code must retain the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer.
12226031Sstas *
13226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
14226031Sstas *    notice, this list of conditions and the following disclaimer in the
15226031Sstas *    documentation and/or other materials provided with the distribution.
16226031Sstas *
17226031Sstas * 3. Neither the name of the Institute nor the names of its contributors
18226031Sstas *    may be used to endorse or promote products derived from this software
19226031Sstas *    without specific prior written permission.
20226031Sstas *
21226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31226031Sstas * SUCH DAMAGE.
32226031Sstas */
33226031Sstas
34226031Sstas#include "krb5_locl.h"
35226031Sstas
36226031Sstasint _krb5_AES_string_to_default_iterator = 4096;
37226031Sstas
38226031Sstasstatic krb5_error_code
39226031SstasAES_string_to_key(krb5_context context,
40226031Sstas		  krb5_enctype enctype,
41226031Sstas		  krb5_data password,
42226031Sstas		  krb5_salt salt,
43226031Sstas		  krb5_data opaque,
44226031Sstas		  krb5_keyblock *key)
45226031Sstas{
46226031Sstas    krb5_error_code ret;
47226031Sstas    uint32_t iter;
48226031Sstas    struct _krb5_encryption_type *et;
49226031Sstas    struct _krb5_key_data kd;
50226031Sstas
51226031Sstas    if (opaque.length == 0)
52226031Sstas	iter = _krb5_AES_string_to_default_iterator;
53226031Sstas    else if (opaque.length == 4) {
54226031Sstas	unsigned long v;
55226031Sstas	_krb5_get_int(opaque.data, &v, 4);
56226031Sstas	iter = ((uint32_t)v);
57226031Sstas    } else
58226031Sstas	return KRB5_PROG_KEYTYPE_NOSUPP; /* XXX */
59226031Sstas
60226031Sstas    et = _krb5_find_enctype(enctype);
61226031Sstas    if (et == NULL)
62226031Sstas	return KRB5_PROG_KEYTYPE_NOSUPP;
63226031Sstas
64226031Sstas    kd.schedule = NULL;
65226031Sstas    ALLOC(kd.key, 1);
66226031Sstas    if(kd.key == NULL) {
67226031Sstas	krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
68226031Sstas	return ENOMEM;
69226031Sstas    }
70226031Sstas    kd.key->keytype = enctype;
71226031Sstas    ret = krb5_data_alloc(&kd.key->keyvalue, et->keytype->size);
72226031Sstas    if (ret) {
73226031Sstas	krb5_set_error_message (context, ret, N_("malloc: out of memory", ""));
74226031Sstas	return ret;
75226031Sstas    }
76226031Sstas
77226031Sstas    ret = PKCS5_PBKDF2_HMAC_SHA1(password.data, password.length,
78226031Sstas				 salt.saltvalue.data, salt.saltvalue.length,
79226031Sstas				 iter,
80226031Sstas				 et->keytype->size, kd.key->keyvalue.data);
81226031Sstas    if (ret != 1) {
82226031Sstas	_krb5_free_key_data(context, &kd, et);
83226031Sstas	krb5_set_error_message(context, KRB5_PROG_KEYTYPE_NOSUPP,
84226031Sstas			       "Error calculating s2k");
85226031Sstas	return KRB5_PROG_KEYTYPE_NOSUPP;
86226031Sstas    }
87226031Sstas
88226031Sstas    ret = _krb5_derive_key(context, et, &kd, "kerberos", strlen("kerberos"));
89226031Sstas    if (ret == 0)
90226031Sstas	ret = krb5_copy_keyblock_contents(context, kd.key, key);
91226031Sstas    _krb5_free_key_data(context, &kd, et);
92226031Sstas
93226031Sstas    return ret;
94226031Sstas}
95226031Sstas
96226031Sstasstruct salt_type _krb5_AES_salt[] = {
97226031Sstas    {
98226031Sstas	KRB5_PW_SALT,
99226031Sstas	"pw-salt",
100226031Sstas	AES_string_to_key
101226031Sstas    },
102226031Sstas    { 0 }
103226031Sstas};
104