Imported Upstream version 1.10.2
[platform/upstream/krb5.git] / src / lib / krb5 / krb / enc_keyhelper.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/enc_keyhelper.c */
3 /*
4  * Copyright (C) 1998 by the FundsXpress, INC.
5  *
6  * All rights reserved.
7  *
8  * Export of this software from the United States of America may require
9  * a specific license from the United States Government.  It is the
10  * responsibility of any person or organization contemplating export to
11  * obtain such a license before exporting.
12  *
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of FundsXpress. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  FundsXpress makes no representations about the suitability of
21  * this software for any purpose.  It is provided "as is" without express
22  * or implied warranty.
23  *
24  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
26  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  *
28  * krb5_encrypt_keyhelper()
29  *
30  */
31
32 #include "k5-int.h"
33
34 krb5_error_code
35 krb5_encrypt_keyhelper(krb5_context context, krb5_key key, krb5_keyusage usage,
36                        const krb5_data *plain, krb5_enc_data *cipher)
37 {
38     krb5_enctype enctype;
39     krb5_error_code ret;
40     size_t enclen;
41
42     enctype = krb5_k_key_enctype(context, key);
43     ret = krb5_c_encrypt_length(context, enctype, plain->length, &enclen);
44     if (ret != 0)
45         return ret;
46
47     cipher->ciphertext.length = enclen;
48     cipher->ciphertext.data = malloc(enclen);
49     if (cipher->ciphertext.data == NULL)
50         return ENOMEM;
51     ret = krb5_k_encrypt(context, key, usage, 0, plain, cipher);
52     if (ret) {
53         free(cipher->ciphertext.data);
54         cipher->ciphertext.data = NULL;
55     }
56
57     return ret;
58 }