2016-05-17 18:24:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:24:46 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2003-03-20 23:29:38 +00:00
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_DES
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
# include <openssl/evp.h>
|
|
|
|
# include <openssl/objects.h>
|
2015-12-18 16:01:28 +00:00
|
|
|
# include "internal/evp_int.h"
|
2015-01-22 03:40:55 +00:00
|
|
|
# include <openssl/des.h>
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-06-03 14:13:58 +00:00
|
|
|
static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *iv, int enc);
|
2000-05-27 12:38:43 +00:00
|
|
|
static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *in, size_t inl);
|
2001-07-30 23:57:25 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
typedef struct {
|
|
|
|
DES_key_schedule ks; /* key schedule */
|
2001-10-24 21:21:12 +00:00
|
|
|
DES_cblock inw;
|
|
|
|
DES_cblock outw;
|
2015-01-22 03:40:55 +00:00
|
|
|
} DESX_CBC_KEY;
|
2001-07-30 23:57:25 +00:00
|
|
|
|
2015-12-18 16:01:28 +00:00
|
|
|
# define data(ctx) EVP_C_DATA(DESX_CBC_KEY,ctx)
|
2001-07-30 23:57:25 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static const EVP_CIPHER d_xcbc_cipher = {
|
|
|
|
NID_desx_cbc,
|
|
|
|
8, 24, 8,
|
|
|
|
EVP_CIPH_CBC_MODE,
|
|
|
|
desx_cbc_init_key,
|
|
|
|
desx_cbc_cipher,
|
|
|
|
NULL,
|
|
|
|
sizeof(DESX_CBC_KEY),
|
|
|
|
EVP_CIPHER_set_asn1_iv,
|
|
|
|
EVP_CIPHER_get_asn1_iv,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2001-03-09 02:51:02 +00:00
|
|
|
const EVP_CIPHER *EVP_desx_cbc(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return (&d_xcbc_cipher);
|
|
|
|
}
|
|
|
|
|
2000-06-03 14:13:58 +00:00
|
|
|
static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *iv, int enc)
|
|
|
|
{
|
|
|
|
DES_cblock *deskey = (DES_cblock *)key;
|
1999-05-16 12:26:16 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
DES_set_key_unchecked(deskey, &data(ctx)->ks);
|
|
|
|
memcpy(&data(ctx)->inw[0], &key[8], 8);
|
|
|
|
memcpy(&data(ctx)->outw[0], &key[16], 8);
|
2000-05-28 12:44:46 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-05-27 12:38:43 +00:00
|
|
|
static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *in, size_t inl)
|
|
|
|
{
|
|
|
|
while (inl >= EVP_MAXCHUNK) {
|
|
|
|
DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks,
|
2015-12-18 16:01:28 +00:00
|
|
|
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
|
|
|
&data(ctx)->inw, &data(ctx)->outw,
|
|
|
|
EVP_CIPHER_CTX_encrypting(ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
inl -= EVP_MAXCHUNK;
|
|
|
|
in += EVP_MAXCHUNK;
|
|
|
|
out += EVP_MAXCHUNK;
|
|
|
|
}
|
|
|
|
if (inl)
|
|
|
|
DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks,
|
2015-12-18 16:01:28 +00:00
|
|
|
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
|
|
|
&data(ctx)->inw, &data(ctx)->outw,
|
|
|
|
EVP_CIPHER_CTX_encrypting(ctx));
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1999-04-27 04:18:53 +00:00
|
|
|
#endif
|