1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
6e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
874664626SKris Kennaway */
974664626SKris Kennaway
10*b077aed3SPierre Pronchery /*
11*b077aed3SPierre Pronchery * DES low level APIs are deprecated for public use, but still ok for internal
12*b077aed3SPierre Pronchery * use.
13*b077aed3SPierre Pronchery */
14*b077aed3SPierre Pronchery #include "internal/deprecated.h"
15*b077aed3SPierre Pronchery
1674664626SKris Kennaway #include <stdio.h>
17e71b7053SJung-uk Kim #include "internal/cryptlib.h"
183b4e3dcbSSimon L. B. Nielsen
193b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_DES
203b4e3dcbSSimon L. B. Nielsen
2174664626SKris Kennaway # include <openssl/evp.h>
2274664626SKris Kennaway # include <openssl/objects.h>
2317f01e99SJung-uk Kim # include "crypto/evp.h"
245c87c606SMark Murray # include <openssl/des.h>
25*b077aed3SPierre Pronchery # include "evp_local.h"
2674664626SKris Kennaway
27ddd58736SKris Kennaway static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
28ddd58736SKris Kennaway const unsigned char *iv, int enc);
29ddd58736SKris Kennaway static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
301f13597dSJung-uk Kim const unsigned char *in, size_t inl);
315c87c606SMark Murray
326f9291ceSJung-uk Kim typedef struct {
335c87c606SMark Murray DES_key_schedule ks; /* key schedule */
345c87c606SMark Murray DES_cblock inw;
355c87c606SMark Murray DES_cblock outw;
365c87c606SMark Murray } DESX_CBC_KEY;
375c87c606SMark Murray
38e71b7053SJung-uk Kim # define data(ctx) EVP_C_DATA(DESX_CBC_KEY,ctx)
395c87c606SMark Murray
406f9291ceSJung-uk Kim static const EVP_CIPHER d_xcbc_cipher = {
4174664626SKris Kennaway NID_desx_cbc,
4274664626SKris Kennaway 8, 24, 8,
43ddd58736SKris Kennaway EVP_CIPH_CBC_MODE,
44*b077aed3SPierre Pronchery EVP_ORIG_GLOBAL,
4574664626SKris Kennaway desx_cbc_init_key,
4674664626SKris Kennaway desx_cbc_cipher,
4774664626SKris Kennaway NULL,
485c87c606SMark Murray sizeof(DESX_CBC_KEY),
4974664626SKris Kennaway EVP_CIPHER_set_asn1_iv,
5074664626SKris Kennaway EVP_CIPHER_get_asn1_iv,
513b4e3dcbSSimon L. B. Nielsen NULL,
52ddd58736SKris Kennaway NULL
5374664626SKris Kennaway };
5474664626SKris Kennaway
EVP_desx_cbc(void)555c87c606SMark Murray const EVP_CIPHER *EVP_desx_cbc(void)
5674664626SKris Kennaway {
57e71b7053SJung-uk Kim return &d_xcbc_cipher;
5874664626SKris Kennaway }
5974664626SKris Kennaway
desx_cbc_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)60ddd58736SKris Kennaway static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
61ddd58736SKris Kennaway const unsigned char *iv, int enc)
6274664626SKris Kennaway {
635c87c606SMark Murray DES_cblock *deskey = (DES_cblock *)key;
6474664626SKris Kennaway
655c87c606SMark Murray DES_set_key_unchecked(deskey, &data(ctx)->ks);
665c87c606SMark Murray memcpy(&data(ctx)->inw[0], &key[8], 8);
675c87c606SMark Murray memcpy(&data(ctx)->outw[0], &key[16], 8);
68ddd58736SKris Kennaway
69ddd58736SKris Kennaway return 1;
7074664626SKris Kennaway }
7174664626SKris Kennaway
desx_cbc_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)72ddd58736SKris Kennaway static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
731f13597dSJung-uk Kim const unsigned char *in, size_t inl)
7474664626SKris Kennaway {
756f9291ceSJung-uk Kim while (inl >= EVP_MAXCHUNK) {
761f13597dSJung-uk Kim DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks,
77*b077aed3SPierre Pronchery (DES_cblock *)ctx->iv,
78e71b7053SJung-uk Kim &data(ctx)->inw, &data(ctx)->outw,
79*b077aed3SPierre Pronchery EVP_CIPHER_CTX_is_encrypting(ctx));
801f13597dSJung-uk Kim inl -= EVP_MAXCHUNK;
811f13597dSJung-uk Kim in += EVP_MAXCHUNK;
821f13597dSJung-uk Kim out += EVP_MAXCHUNK;
831f13597dSJung-uk Kim }
841f13597dSJung-uk Kim if (inl)
851f13597dSJung-uk Kim DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks,
86*b077aed3SPierre Pronchery (DES_cblock *)ctx->iv,
87e71b7053SJung-uk Kim &data(ctx)->inw, &data(ctx)->outw,
88*b077aed3SPierre Pronchery EVP_CIPHER_CTX_is_encrypting(ctx));
89ddd58736SKris Kennaway return 1;
9074664626SKris Kennaway }
9174664626SKris Kennaway #endif
92