xref: /freebsd/crypto/krb5/src/lib/crypto/krb/encrypt.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
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
krb5_k_encrypt(krb5_context context,krb5_key key,krb5_keyusage usage,const krb5_data * cipher_state,const krb5_data * input,krb5_enc_data * output)31 krb5_k_encrypt(krb5_context context, krb5_key key,
32                krb5_keyusage usage, const krb5_data *cipher_state,
33                const krb5_data *input, krb5_enc_data *output)
34 {
35     const struct krb5_keytypes *ktp;
36     krb5_crypto_iov iov[4];
37     krb5_error_code ret;
38     unsigned int header_len, padding_len, trailer_len, total_len;
39 
40     ktp = find_enctype(key->keyblock.enctype);
41     if (ktp == NULL)
42         return KRB5_BAD_ENCTYPE;
43 
44     output->magic = KV5M_ENC_DATA;
45     output->kvno = 0;
46     output->enctype = key->keyblock.enctype;
47 
48     /* Get the lengths of the token parts and compute the total. */
49     header_len = ktp->crypto_length(ktp, KRB5_CRYPTO_TYPE_HEADER);
50     padding_len = krb5int_c_padding_length(ktp, input->length);
51     trailer_len = ktp->crypto_length(ktp, KRB5_CRYPTO_TYPE_TRAILER);
52     total_len = header_len + input->length + padding_len + trailer_len;
53     if (output->ciphertext.length < total_len)
54         return KRB5_BAD_MSIZE;
55 
56     /* Set up the iov structures for the token parts. */
57     iov[0].flags = KRB5_CRYPTO_TYPE_HEADER;
58     iov[0].data = make_data(output->ciphertext.data, header_len);
59 
60     iov[1].flags = KRB5_CRYPTO_TYPE_DATA;
61     iov[1].data = make_data(output->ciphertext.data + header_len,
62                             input->length);
63     if (input->length > 0)
64         memcpy(iov[1].data.data, input->data, input->length);
65 
66     iov[2].flags = KRB5_CRYPTO_TYPE_PADDING;
67     iov[2].data = make_data(iov[1].data.data + input->length, padding_len);
68 
69     iov[3].flags = KRB5_CRYPTO_TYPE_TRAILER;
70     iov[3].data = make_data(iov[2].data.data + padding_len, trailer_len);
71 
72     ret = ktp->encrypt(ktp, key, usage, cipher_state, iov, 4);
73     if (ret != 0)
74         zap(iov[1].data.data, iov[1].data.length);
75     else
76         output->ciphertext.length = total_len;
77     return ret;
78 }
79 
80 krb5_error_code KRB5_CALLCONV
krb5_c_encrypt(krb5_context context,const krb5_keyblock * keyblock,krb5_keyusage usage,const krb5_data * cipher_state,const krb5_data * input,krb5_enc_data * output)81 krb5_c_encrypt(krb5_context context, const krb5_keyblock *keyblock,
82                krb5_keyusage usage, const krb5_data *cipher_state,
83                const krb5_data *input, krb5_enc_data *output)
84 {
85     krb5_key key;
86     krb5_error_code ret;
87 
88     ret = krb5_k_create_key(context, keyblock, &key);
89     if (ret != 0)
90         return ret;
91     ret = krb5_k_encrypt(context, key, usage, cipher_state, input, output);
92     krb5_k_free_key(context, key);
93     return ret;
94 }
95