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 
29 #include "k5-int.h"
30 #include "int-proto.h"
31 
32 krb5_error_code
k5_encrypt_keyhelper(krb5_context context,krb5_key key,krb5_keyusage usage,const krb5_data * plain,krb5_enc_data * cipher)33 k5_encrypt_keyhelper(krb5_context context, krb5_key key, krb5_keyusage usage,
34                      const krb5_data *plain, krb5_enc_data *cipher)
35 {
36     krb5_enctype enctype;
37     krb5_error_code ret;
38     size_t enclen;
39 
40     enctype = krb5_k_key_enctype(context, key);
41     ret = krb5_c_encrypt_length(context, enctype, plain->length, &enclen);
42     if (ret != 0)
43         return ret;
44 
45     cipher->ciphertext.length = enclen;
46     cipher->ciphertext.data = malloc(enclen);
47     if (cipher->ciphertext.data == NULL)
48         return ENOMEM;
49     ret = krb5_k_encrypt(context, key, usage, 0, plain, cipher);
50     if (ret) {
51         free(cipher->ciphertext.data);
52         cipher->ciphertext.data = NULL;
53     }
54 
55     return ret;
56 }
57