1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/openssl/enc_provider/des.c
4 * Copyright (C) 2009 by the Massachusetts Institute of Technology.
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.
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.
28 * Copyright (C) 1998 by the FundsXpress, INC.
30 * All rights reserved.
32 * Export of this software from the United States of America may require
33 * a specific license from the United States Government. It is the
34 * responsibility of any person or organization contemplating export to
35 * obtain such a license before exporting.
37 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
38 * distribute this software and its documentation for any purpose and
39 * without fee is hereby granted, provided that the above copyright
40 * notice appear in all copies and that both that copyright notice and
41 * this permission notice appear in supporting documentation, and that
42 * the name of FundsXpress. not be used in advertising or publicity pertaining
43 * to distribution of the software without specific, written prior
44 * permission. FundsXpress makes no representations about the suitability of
45 * this software for any purpose. It is provided "as is" without express
46 * or implied warranty.
48 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
50 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
56 #include <openssl/evp.h>
59 #define DES_BLOCK_SIZE 8
60 #define DES_KEY_BYTES 7
62 static krb5_error_code
63 validate(krb5_key key, const krb5_data *ivec,
64 const krb5_crypto_iov *data, size_t num_data)
66 size_t i, input_length;
68 for (i = 0, input_length = 0; i < num_data; i++) {
69 const krb5_crypto_iov *iov = &data[i];
71 input_length += iov->data.length;
74 if (key->keyblock.length != KRB5_MIT_DES_KEYSIZE)
75 return(KRB5_BAD_KEYSIZE);
76 if ((input_length%DES_BLOCK_SIZE) != 0)
77 return(KRB5_BAD_MSIZE);
78 if (ivec && (ivec->length != 8))
79 return(KRB5_BAD_MSIZE);
84 static krb5_error_code
85 k5_des_encrypt(krb5_key key, const krb5_data *ivec, krb5_crypto_iov *data,
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;
93 IOV_BLOCK_STATE_INIT(&input_pos);
94 IOV_BLOCK_STATE_INIT(&output_pos);
97 ret = validate(key, ivec, data, num_data);
101 EVP_CIPHER_CTX_init(&ciph_ctx);
103 ret = EVP_EncryptInit_ex(&ciph_ctx, EVP_des_cbc(), NULL,
104 key->keyblock.contents, (ivec && ivec->data) ? (unsigned char*)ivec->data : NULL);
106 return KRB5_CRYPTO_INTERNAL;
108 EVP_CIPHER_CTX_set_padding(&ciph_ctx,0);
112 if (!krb5int_c_iov_get_block(iblock, MIT_DES_BLOCK_LENGTH, data,
113 num_data, &input_pos))
116 ret = EVP_EncryptUpdate(&ciph_ctx, oblock, &olen,
117 (unsigned char *)iblock, MIT_DES_BLOCK_LENGTH);
121 krb5int_c_iov_put_block(data, num_data, oblock, MIT_DES_BLOCK_LENGTH,
125 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
127 zap(iblock, sizeof(iblock));
128 zap(oblock, sizeof(oblock));
131 return KRB5_CRYPTO_INTERNAL;
135 static krb5_error_code
136 k5_des_decrypt(krb5_key key, const krb5_data *ivec, krb5_crypto_iov *data,
139 int ret, olen = MIT_DES_BLOCK_LENGTH;
140 unsigned char iblock[MIT_DES_BLOCK_LENGTH], oblock[MIT_DES_BLOCK_LENGTH];
141 struct iov_block_state input_pos, output_pos;
142 EVP_CIPHER_CTX ciph_ctx;
144 IOV_BLOCK_STATE_INIT(&input_pos);
145 IOV_BLOCK_STATE_INIT(&output_pos);
147 ret = validate(key, ivec, data, num_data);
151 EVP_CIPHER_CTX_init(&ciph_ctx);
153 ret = EVP_DecryptInit_ex(&ciph_ctx, EVP_des_cbc(), NULL,
154 key->keyblock.contents,
155 (ivec) ? (unsigned char*)ivec->data : NULL);
157 return KRB5_CRYPTO_INTERNAL;
159 EVP_CIPHER_CTX_set_padding(&ciph_ctx,0);
163 if (!krb5int_c_iov_get_block(iblock, MIT_DES_BLOCK_LENGTH,
164 data, num_data, &input_pos))
167 ret = EVP_DecryptUpdate(&ciph_ctx, oblock, &olen,
168 iblock, MIT_DES_BLOCK_LENGTH);
171 krb5int_c_iov_put_block(data, num_data, oblock,
172 MIT_DES_BLOCK_LENGTH, &output_pos);
175 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
177 zap(iblock, sizeof(iblock));
178 zap(oblock, sizeof(oblock));
181 return KRB5_CRYPTO_INTERNAL;
185 const struct krb5_enc_provider krb5int_enc_des = {
187 DES_KEY_BYTES, KRB5_MIT_DES_KEYSIZE,
191 krb5int_des_make_key,
192 krb5int_des_init_state,
193 krb5int_default_free_state