cd46d454a880c134adc21b4b7a2f5b3705598ba3
[krb5.git] / src / lib / crypto / krb / string_to_key.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 "crypto_int.h"
29
30 krb5_error_code KRB5_CALLCONV
31 krb5_c_string_to_key(krb5_context context, krb5_enctype enctype,
32                      const krb5_data *string, const krb5_data *salt,
33                      krb5_keyblock *key)
34 {
35     return krb5_c_string_to_key_with_params(context, enctype, string, salt,
36                                             NULL, key);
37 }
38
39 krb5_error_code KRB5_CALLCONV
40 krb5_c_string_to_key_with_params(krb5_context context, krb5_enctype enctype,
41                                  const krb5_data *string,
42                                  const krb5_data *salt,
43                                  const krb5_data *params, krb5_keyblock *key)
44 {
45     krb5_error_code ret;
46     const struct krb5_keytypes *ktp;
47     size_t keylength;
48
49     ktp = find_enctype(enctype);
50     if (ktp == NULL)
51         return KRB5_BAD_ENCTYPE;
52     keylength = ktp->enc->keylength;
53
54     /*
55      * xxx AFS string2key function is indicated by a special length  in
56      * the salt in much of the code.  However only the DES enctypes can
57      * deal with this.  Using s2kparams would be a much better solution.
58      */
59     if (salt && salt->length == SALT_TYPE_AFS_LENGTH) {
60         switch (enctype) {
61         case ENCTYPE_DES_CBC_CRC:
62         case ENCTYPE_DES_CBC_MD4:
63         case ENCTYPE_DES_CBC_MD5:
64             break;
65         default:
66             return KRB5_CRYPTO_INTERNAL;
67         }
68     }
69
70     key->contents = malloc(keylength);
71     if (key->contents == NULL)
72         return ENOMEM;
73
74     key->magic = KV5M_KEYBLOCK;
75     key->enctype = enctype;
76     key->length = keylength;
77
78     ret = (*ktp->str2key)(ktp, string, salt, params, key);
79     if (ret) {
80         zapfree(key->contents, keylength);
81         key->length = 0;
82         key->contents = NULL;
83     }
84
85     return ret;
86 }