pull up r23734 from trunk
[krb5.git] / src / lib / crypto / openssl / enc_provider / des3.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/openssl/enc_provider/des3.c
3  *
4  * Copyright (C) 2009 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to 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 M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 /*
27  * Copyright (C) 1998 by the FundsXpress, INC.
28  *
29  * All rights reserved.
30  *
31  * Export of this software from the United States of America may require
32  * a specific license from the United States Government.  It is the
33  * responsibility of any person or organization contemplating export to
34  * obtain such a license before exporting.
35  *
36  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
37  * distribute this software and its documentation for any purpose and
38  * without fee is hereby granted, provided that the above copyright
39  * notice appear in all copies and that both that copyright notice and
40  * this permission notice appear in supporting documentation, and that
41  * the name of FundsXpress. not be used in advertising or publicity pertaining
42  * to distribution of the software without specific, written prior
43  * permission.  FundsXpress makes no representations about the suitability of
44  * this software for any purpose.  It is provided "as is" without express
45  * or implied warranty.
46  *
47  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
50  */
51
52 #include "k5-int.h"
53 #include "des_int.h"
54 #include <aead.h>
55 #include <rand2key.h>
56 #include <openssl/evp.h>
57
58
59 #define DES_BLOCK_SIZE  8
60
61 static krb5_error_code
62 validate(krb5_key key, const krb5_data *ivec, const krb5_crypto_iov *data,
63          size_t num_data, krb5_boolean *empty)
64 {
65     size_t i, input_length;
66
67     for (i = 0, input_length = 0; i < num_data; i++) {
68         const krb5_crypto_iov *iov = &data[i];
69         if (ENCRYPT_IOV(iov))
70             input_length += iov->data.length;
71     }
72
73     if (key->keyblock.length != KRB5_MIT_DES3_KEYSIZE)
74         return(KRB5_BAD_KEYSIZE);
75     if ((input_length%DES_BLOCK_SIZE) != 0)
76         return(KRB5_BAD_MSIZE);
77     if (ivec && (ivec->length != 8))
78         return(KRB5_BAD_MSIZE);
79
80     *empty = (input_length == 0);
81     return 0;
82 }
83
84 static krb5_error_code
85 k5_des3_encrypt(krb5_key key, const krb5_data *ivec, krb5_crypto_iov *data,
86                 size_t num_data)
87 {
88     int ret, olen = MIT_DES_BLOCK_LENGTH;
89     unsigned char iblock[MIT_DES_BLOCK_LENGTH], oblock[MIT_DES_BLOCK_LENGTH];
90     struct iov_block_state input_pos, output_pos;
91     EVP_CIPHER_CTX ciph_ctx;
92     krb5_boolean empty;
93
94     ret = validate(key, ivec, data, num_data, &empty);
95     if (ret != 0 || empty)
96         return ret;
97
98     IOV_BLOCK_STATE_INIT(&input_pos);
99     IOV_BLOCK_STATE_INIT(&output_pos);
100
101     EVP_CIPHER_CTX_init(&ciph_ctx);
102
103     ret = EVP_EncryptInit_ex(&ciph_ctx, EVP_des_ede3_cbc(), NULL,
104                              key->keyblock.contents,
105                              (ivec) ? (unsigned char*)ivec->data : NULL);
106     if (!ret)
107         return KRB5_CRYPTO_INTERNAL;
108
109     EVP_CIPHER_CTX_set_padding(&ciph_ctx,0);
110
111     for (;;) {
112
113         if (!krb5int_c_iov_get_block(iblock, MIT_DES_BLOCK_LENGTH,
114                                      data, num_data, &input_pos))
115             break;
116
117         ret = EVP_EncryptUpdate(&ciph_ctx, oblock, &olen,
118                                 (unsigned char *)iblock, MIT_DES_BLOCK_LENGTH);
119         if (!ret)
120             break;
121
122         krb5int_c_iov_put_block(data, num_data,
123                                 oblock, MIT_DES_BLOCK_LENGTH, &output_pos);
124     }
125
126     if (ivec != NULL)
127         memcpy(ivec->data, oblock, MIT_DES_BLOCK_LENGTH);
128
129     EVP_CIPHER_CTX_cleanup(&ciph_ctx);
130
131     zap(iblock, sizeof(iblock));
132     zap(oblock, sizeof(oblock));
133
134     if (ret != 1)
135         return KRB5_CRYPTO_INTERNAL;
136     return 0;
137 }
138
139 static krb5_error_code
140 k5_des3_decrypt(krb5_key key, const krb5_data *ivec, krb5_crypto_iov *data,
141                 size_t num_data)
142 {
143     int ret, olen = MIT_DES_BLOCK_LENGTH;
144     unsigned char iblock[MIT_DES_BLOCK_LENGTH], oblock[MIT_DES_BLOCK_LENGTH];
145     struct iov_block_state input_pos, output_pos;
146     EVP_CIPHER_CTX ciph_ctx;
147     krb5_boolean empty;
148
149     ret = validate(key, ivec, data, num_data, &empty);
150     if (ret != 0 || empty)
151         return ret;
152
153     IOV_BLOCK_STATE_INIT(&input_pos);
154     IOV_BLOCK_STATE_INIT(&output_pos);
155
156     EVP_CIPHER_CTX_init(&ciph_ctx);
157
158     ret = EVP_DecryptInit_ex(&ciph_ctx, EVP_des_ede3_cbc(), NULL,
159                              key->keyblock.contents,
160                              (ivec) ? (unsigned char*)ivec->data : NULL);
161     if (!ret)
162         return KRB5_CRYPTO_INTERNAL;
163
164     EVP_CIPHER_CTX_set_padding(&ciph_ctx,0);
165
166     for (;;) {
167
168         if (!krb5int_c_iov_get_block(iblock, MIT_DES_BLOCK_LENGTH,
169                                      data, num_data, &input_pos))
170             break;
171
172         ret = EVP_DecryptUpdate(&ciph_ctx, oblock, &olen,
173                                 (unsigned char *)iblock, MIT_DES_BLOCK_LENGTH);
174         if (!ret)
175             break;
176
177         krb5int_c_iov_put_block(data, num_data, oblock, MIT_DES_BLOCK_LENGTH,
178                                 &output_pos);
179     }
180
181     if (ivec != NULL)
182         memcpy(ivec->data, iblock, MIT_DES_BLOCK_LENGTH);
183
184     EVP_CIPHER_CTX_cleanup(&ciph_ctx);
185
186     zap(iblock, sizeof(iblock));
187     zap(oblock, sizeof(oblock));
188
189     if (ret != 1)
190         return KRB5_CRYPTO_INTERNAL;
191     return 0;
192 }
193
194 const struct krb5_enc_provider krb5int_enc_des3 = {
195     DES_BLOCK_SIZE,
196     KRB5_MIT_DES3_KEY_BYTES, KRB5_MIT_DES3_KEYSIZE,
197     k5_des3_encrypt,
198     k5_des3_decrypt,
199     NULL,
200     krb5int_des3_make_key,
201     krb5int_des_init_state,
202     krb5int_default_free_state
203 };