1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * DES low level APIs are deprecated for public use, but still ok for internal
12e1051a39Sopenharmony_ci * use.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#include "internal/deprecated.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <stdio.h>
17e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DES
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci# include <openssl/evp.h>
22e1051a39Sopenharmony_ci# include <openssl/objects.h>
23e1051a39Sopenharmony_ci# include "crypto/evp.h"
24e1051a39Sopenharmony_ci# include <openssl/des.h>
25e1051a39Sopenharmony_ci# include "evp_local.h"
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_cistatic int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
28e1051a39Sopenharmony_ci                             const unsigned char *iv, int enc);
29e1051a39Sopenharmony_cistatic int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
30e1051a39Sopenharmony_ci                           const unsigned char *in, size_t inl);
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_citypedef struct {
33e1051a39Sopenharmony_ci    DES_key_schedule ks;        /* key schedule */
34e1051a39Sopenharmony_ci    DES_cblock inw;
35e1051a39Sopenharmony_ci    DES_cblock outw;
36e1051a39Sopenharmony_ci} DESX_CBC_KEY;
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_ci# define data(ctx) EVP_C_DATA(DESX_CBC_KEY,ctx)
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_cistatic const EVP_CIPHER d_xcbc_cipher = {
41e1051a39Sopenharmony_ci    NID_desx_cbc,
42e1051a39Sopenharmony_ci    8, 24, 8,
43e1051a39Sopenharmony_ci    EVP_CIPH_CBC_MODE,
44e1051a39Sopenharmony_ci    EVP_ORIG_GLOBAL,
45e1051a39Sopenharmony_ci    desx_cbc_init_key,
46e1051a39Sopenharmony_ci    desx_cbc_cipher,
47e1051a39Sopenharmony_ci    NULL,
48e1051a39Sopenharmony_ci    sizeof(DESX_CBC_KEY),
49e1051a39Sopenharmony_ci    EVP_CIPHER_set_asn1_iv,
50e1051a39Sopenharmony_ci    EVP_CIPHER_get_asn1_iv,
51e1051a39Sopenharmony_ci    NULL,
52e1051a39Sopenharmony_ci    NULL
53e1051a39Sopenharmony_ci};
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ciconst EVP_CIPHER *EVP_desx_cbc(void)
56e1051a39Sopenharmony_ci{
57e1051a39Sopenharmony_ci    return &d_xcbc_cipher;
58e1051a39Sopenharmony_ci}
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_cistatic int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
61e1051a39Sopenharmony_ci                             const unsigned char *iv, int enc)
62e1051a39Sopenharmony_ci{
63e1051a39Sopenharmony_ci    DES_cblock *deskey = (DES_cblock *)key;
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci    DES_set_key_unchecked(deskey, &data(ctx)->ks);
66e1051a39Sopenharmony_ci    memcpy(&data(ctx)->inw[0], &key[8], 8);
67e1051a39Sopenharmony_ci    memcpy(&data(ctx)->outw[0], &key[16], 8);
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci    return 1;
70e1051a39Sopenharmony_ci}
71e1051a39Sopenharmony_ci
72e1051a39Sopenharmony_cistatic int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
73e1051a39Sopenharmony_ci                           const unsigned char *in, size_t inl)
74e1051a39Sopenharmony_ci{
75e1051a39Sopenharmony_ci    while (inl >= EVP_MAXCHUNK) {
76e1051a39Sopenharmony_ci        DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks,
77e1051a39Sopenharmony_ci                         (DES_cblock *)ctx->iv,
78e1051a39Sopenharmony_ci                         &data(ctx)->inw, &data(ctx)->outw,
79e1051a39Sopenharmony_ci                         EVP_CIPHER_CTX_is_encrypting(ctx));
80e1051a39Sopenharmony_ci        inl -= EVP_MAXCHUNK;
81e1051a39Sopenharmony_ci        in += EVP_MAXCHUNK;
82e1051a39Sopenharmony_ci        out += EVP_MAXCHUNK;
83e1051a39Sopenharmony_ci    }
84e1051a39Sopenharmony_ci    if (inl)
85e1051a39Sopenharmony_ci        DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks,
86e1051a39Sopenharmony_ci                         (DES_cblock *)ctx->iv,
87e1051a39Sopenharmony_ci                         &data(ctx)->inw, &data(ctx)->outw,
88e1051a39Sopenharmony_ci                         EVP_CIPHER_CTX_is_encrypting(ctx));
89e1051a39Sopenharmony_ci    return 1;
90e1051a39Sopenharmony_ci}
91e1051a39Sopenharmony_ci#endif
92