1e71b7053SJung-uk Kim /* 2e71b7053SJung-uk Kim * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 374664626SKris Kennaway * 4e71b7053SJung-uk Kim * Licensed under the OpenSSL license (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 1074664626SKris Kennaway #include <stdio.h> 11e71b7053SJung-uk Kim #include "internal/cryptlib.h" 123b4e3dcbSSimon L. B. Nielsen 133b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_DES 143b4e3dcbSSimon L. B. Nielsen 1574664626SKris Kennaway # include <openssl/evp.h> 1674664626SKris Kennaway # include <openssl/objects.h> 17*17f01e99SJung-uk Kim # include "crypto/evp.h" 185c87c606SMark Murray # include <openssl/des.h> 1974664626SKris Kennaway 20ddd58736SKris Kennaway static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 21ddd58736SKris Kennaway const unsigned char *iv, int enc); 22ddd58736SKris Kennaway static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 231f13597dSJung-uk Kim const unsigned char *in, size_t inl); 245c87c606SMark Murray 256f9291ceSJung-uk Kim typedef struct { 265c87c606SMark Murray DES_key_schedule ks; /* key schedule */ 275c87c606SMark Murray DES_cblock inw; 285c87c606SMark Murray DES_cblock outw; 295c87c606SMark Murray } DESX_CBC_KEY; 305c87c606SMark Murray 31e71b7053SJung-uk Kim # define data(ctx) EVP_C_DATA(DESX_CBC_KEY,ctx) 325c87c606SMark Murray 336f9291ceSJung-uk Kim static const EVP_CIPHER d_xcbc_cipher = { 3474664626SKris Kennaway NID_desx_cbc, 3574664626SKris Kennaway 8, 24, 8, 36ddd58736SKris Kennaway EVP_CIPH_CBC_MODE, 3774664626SKris Kennaway desx_cbc_init_key, 3874664626SKris Kennaway desx_cbc_cipher, 3974664626SKris Kennaway NULL, 405c87c606SMark Murray sizeof(DESX_CBC_KEY), 4174664626SKris Kennaway EVP_CIPHER_set_asn1_iv, 4274664626SKris Kennaway EVP_CIPHER_get_asn1_iv, 433b4e3dcbSSimon L. B. Nielsen NULL, 44ddd58736SKris Kennaway NULL 4574664626SKris Kennaway }; 4674664626SKris Kennaway 475c87c606SMark Murray const EVP_CIPHER *EVP_desx_cbc(void) 4874664626SKris Kennaway { 49e71b7053SJung-uk Kim return &d_xcbc_cipher; 5074664626SKris Kennaway } 5174664626SKris Kennaway 52ddd58736SKris Kennaway static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 53ddd58736SKris Kennaway const unsigned char *iv, int enc) 5474664626SKris Kennaway { 555c87c606SMark Murray DES_cblock *deskey = (DES_cblock *)key; 5674664626SKris Kennaway 575c87c606SMark Murray DES_set_key_unchecked(deskey, &data(ctx)->ks); 585c87c606SMark Murray memcpy(&data(ctx)->inw[0], &key[8], 8); 595c87c606SMark Murray memcpy(&data(ctx)->outw[0], &key[16], 8); 60ddd58736SKris Kennaway 61ddd58736SKris Kennaway return 1; 6274664626SKris Kennaway } 6374664626SKris Kennaway 64ddd58736SKris Kennaway static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 651f13597dSJung-uk Kim const unsigned char *in, size_t inl) 6674664626SKris Kennaway { 676f9291ceSJung-uk Kim while (inl >= EVP_MAXCHUNK) { 681f13597dSJung-uk Kim DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks, 69e71b7053SJung-uk Kim (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), 70e71b7053SJung-uk Kim &data(ctx)->inw, &data(ctx)->outw, 71e71b7053SJung-uk Kim EVP_CIPHER_CTX_encrypting(ctx)); 721f13597dSJung-uk Kim inl -= EVP_MAXCHUNK; 731f13597dSJung-uk Kim in += EVP_MAXCHUNK; 741f13597dSJung-uk Kim out += EVP_MAXCHUNK; 751f13597dSJung-uk Kim } 761f13597dSJung-uk Kim if (inl) 771f13597dSJung-uk Kim DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks, 78e71b7053SJung-uk Kim (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), 79e71b7053SJung-uk Kim &data(ctx)->inw, &data(ctx)->outw, 80e71b7053SJung-uk Kim EVP_CIPHER_CTX_encrypting(ctx)); 81ddd58736SKris Kennaway return 1; 8274664626SKris Kennaway } 8374664626SKris Kennaway #endif 84