1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery */
9*b077aed3SPierre Pronchery
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
16*b077aed3SPierre Pronchery #include <openssl/des.h>
17*b077aed3SPierre Pronchery #include "cipher_tdes_default.h"
18*b077aed3SPierre Pronchery
19*b077aed3SPierre Pronchery /*
20*b077aed3SPierre Pronchery * Note the PROV_TDES_CTX has been used for the DESX cipher, just to reduce
21*b077aed3SPierre Pronchery * code size.
22*b077aed3SPierre Pronchery */
23*b077aed3SPierre Pronchery #define ks1 tks.ks[0]
24*b077aed3SPierre Pronchery #define ks2 tks.ks[1].ks[0].cblock
25*b077aed3SPierre Pronchery #define ks3 tks.ks[2].ks[0].cblock
26*b077aed3SPierre Pronchery
cipher_hw_desx_cbc_initkey(PROV_CIPHER_CTX * ctx,const unsigned char * key,size_t keylen)27*b077aed3SPierre Pronchery static int cipher_hw_desx_cbc_initkey(PROV_CIPHER_CTX *ctx,
28*b077aed3SPierre Pronchery const unsigned char *key, size_t keylen)
29*b077aed3SPierre Pronchery {
30*b077aed3SPierre Pronchery PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
31*b077aed3SPierre Pronchery DES_cblock *deskey = (DES_cblock *)key;
32*b077aed3SPierre Pronchery
33*b077aed3SPierre Pronchery DES_set_key_unchecked(deskey, &tctx->ks1);
34*b077aed3SPierre Pronchery memcpy(&tctx->ks2, &key[8], 8);
35*b077aed3SPierre Pronchery memcpy(&tctx->ks3, &key[16], 8);
36*b077aed3SPierre Pronchery
37*b077aed3SPierre Pronchery return 1;
38*b077aed3SPierre Pronchery }
39*b077aed3SPierre Pronchery
cipher_hw_desx_copyctx(PROV_CIPHER_CTX * dst,const PROV_CIPHER_CTX * src)40*b077aed3SPierre Pronchery static void cipher_hw_desx_copyctx(PROV_CIPHER_CTX *dst,
41*b077aed3SPierre Pronchery const PROV_CIPHER_CTX *src)
42*b077aed3SPierre Pronchery {
43*b077aed3SPierre Pronchery PROV_TDES_CTX *sctx = (PROV_TDES_CTX *)src;
44*b077aed3SPierre Pronchery PROV_TDES_CTX *dctx = (PROV_TDES_CTX *)dst;
45*b077aed3SPierre Pronchery
46*b077aed3SPierre Pronchery *dctx = *sctx;
47*b077aed3SPierre Pronchery dst->ks = &dctx->tks.ks;
48*b077aed3SPierre Pronchery }
49*b077aed3SPierre Pronchery
cipher_hw_desx_cbc(PROV_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)50*b077aed3SPierre Pronchery static int cipher_hw_desx_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
51*b077aed3SPierre Pronchery const unsigned char *in, size_t inl)
52*b077aed3SPierre Pronchery {
53*b077aed3SPierre Pronchery PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
54*b077aed3SPierre Pronchery
55*b077aed3SPierre Pronchery while (inl >= MAXCHUNK) {
56*b077aed3SPierre Pronchery DES_xcbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1,
57*b077aed3SPierre Pronchery (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,
58*b077aed3SPierre Pronchery ctx->enc);
59*b077aed3SPierre Pronchery inl -= MAXCHUNK;
60*b077aed3SPierre Pronchery in += MAXCHUNK;
61*b077aed3SPierre Pronchery out += MAXCHUNK;
62*b077aed3SPierre Pronchery }
63*b077aed3SPierre Pronchery if (inl > 0)
64*b077aed3SPierre Pronchery DES_xcbc_encrypt(in, out, (long)inl, &tctx->ks1,
65*b077aed3SPierre Pronchery (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,
66*b077aed3SPierre Pronchery ctx->enc);
67*b077aed3SPierre Pronchery return 1;
68*b077aed3SPierre Pronchery }
69*b077aed3SPierre Pronchery
70*b077aed3SPierre Pronchery static const PROV_CIPHER_HW desx_cbc =
71*b077aed3SPierre Pronchery {
72*b077aed3SPierre Pronchery cipher_hw_desx_cbc_initkey,
73*b077aed3SPierre Pronchery cipher_hw_desx_cbc,
74*b077aed3SPierre Pronchery cipher_hw_desx_copyctx
75*b077aed3SPierre Pronchery };
76*b077aed3SPierre Pronchery
ossl_prov_cipher_hw_tdes_desx_cbc(void)77*b077aed3SPierre Pronchery const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_desx_cbc(void)
78*b077aed3SPierre Pronchery {
79*b077aed3SPierre Pronchery return &desx_cbc;
80*b077aed3SPierre Pronchery }
81