xref: /freebsd/crypto/krb5/src/lib/crypto/krb/decrypt.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_decrypt(krb5_context context,krb5_key key,krb5_keyusage usage,const krb5_data * cipher_state,const krb5_enc_data * input,krb5_data * output)31 krb5_k_decrypt(krb5_context context, krb5_key key,
32                krb5_keyusage usage, const krb5_data *cipher_state,
33                const krb5_enc_data *input, krb5_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, trailer_len, plain_len;
39     char *scratch = NULL;
40 
41     ktp = find_enctype(key->keyblock.enctype);
42     if (ktp == NULL)
43         return KRB5_BAD_ENCTYPE;
44 
45     if (input->enctype != ENCTYPE_UNKNOWN && ktp->etype != input->enctype)
46         return KRB5_BAD_ENCTYPE;
47 
48     /* Verify the input and output lengths. */
49     header_len = ktp->crypto_length(ktp, KRB5_CRYPTO_TYPE_HEADER);
50     trailer_len = ktp->crypto_length(ktp, KRB5_CRYPTO_TYPE_TRAILER);
51     if (input->ciphertext.length < header_len + trailer_len)
52         return KRB5_BAD_MSIZE;
53     plain_len = input->ciphertext.length - header_len - trailer_len;
54     if (output->length < plain_len)
55         return KRB5_BAD_MSIZE;
56 
57     scratch = k5alloc(header_len + trailer_len, &ret);
58     if (scratch == NULL)
59         return ret;
60 
61     iov[0].flags = KRB5_CRYPTO_TYPE_HEADER;
62     iov[0].data = make_data(scratch, header_len);
63     memcpy(iov[0].data.data, input->ciphertext.data, header_len);
64 
65     iov[1].flags = KRB5_CRYPTO_TYPE_DATA;
66     iov[1].data = make_data(output->data, plain_len);
67     memcpy(iov[1].data.data, input->ciphertext.data + header_len, plain_len);
68 
69     /* Use empty padding since tokens don't indicate the padding length. */
70     iov[2].flags = KRB5_CRYPTO_TYPE_PADDING;
71     iov[2].data = empty_data();
72 
73     iov[3].flags = KRB5_CRYPTO_TYPE_TRAILER;
74     iov[3].data = make_data(scratch + header_len, trailer_len);
75     memcpy(iov[3].data.data, input->ciphertext.data + header_len + plain_len,
76            trailer_len);
77 
78     ret = ktp->decrypt(ktp, key, usage, cipher_state, iov, 4);
79     if (ret != 0)
80         zap(output->data, plain_len);
81     else
82         output->length = plain_len;
83     zapfree(scratch, header_len + trailer_len);
84     return ret;
85 }
86 
87 krb5_error_code KRB5_CALLCONV
krb5_c_decrypt(krb5_context context,const krb5_keyblock * keyblock,krb5_keyusage usage,const krb5_data * cipher_state,const krb5_enc_data * input,krb5_data * output)88 krb5_c_decrypt(krb5_context context, const krb5_keyblock *keyblock,
89                krb5_keyusage usage, const krb5_data *cipher_state,
90                const krb5_enc_data *input, krb5_data *output)
91 {
92     krb5_key key;
93     krb5_error_code ret;
94 
95     ret = krb5_k_create_key(context, keyblock, &key);
96     if (ret != 0)
97         return ret;
98     ret = krb5_k_decrypt(context, key, usage, cipher_state, input, output);
99     krb5_k_free_key(context, key);
100     return ret;
101 }
102