Deprecate the low level DES functions.
[openssl.git] / providers / implementations / ciphers / cipher_desx_hw.c
1 /*
2  * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * DES low level APIs are deprecated for public use, but still ok for internal
12  * use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/des.h>
17 #include "cipher_tdes_default.h"
18
19 /*
20  * Note the PROV_TDES_CTX has been used for the DESX cipher, just to reduce
21  * code size.
22  */
23 #define ks1 tks.ks[0]
24 #define ks2 tks.ks[1].ks[0].cblock
25 #define ks3 tks.ks[2].ks[0].cblock
26
27 static int cipher_hw_desx_cbc_initkey(PROV_CIPHER_CTX *ctx,
28                                       const unsigned char *key, size_t keylen)
29 {
30     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
31     DES_cblock *deskey = (DES_cblock *)key;
32
33     DES_set_key_unchecked(deskey, &tctx->ks1);
34     memcpy(&tctx->ks2, &key[8], 8);
35     memcpy(&tctx->ks3, &key[16], 8);
36
37     return 1;
38 }
39
40 static int cipher_hw_desx_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
41                               const unsigned char *in, size_t inl)
42 {
43     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
44
45     while (inl >= MAXCHUNK) {
46         DES_xcbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1,
47                          (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,
48                          ctx->enc);
49         inl -= MAXCHUNK;
50         in += MAXCHUNK;
51         out += MAXCHUNK;
52     }
53     if (inl > 0)
54         DES_xcbc_encrypt(in, out, (long)inl, &tctx->ks1,
55                          (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,
56                          ctx->enc);
57     return 1;
58 }
59
60 static const PROV_CIPHER_HW desx_cbc =
61 {
62     cipher_hw_desx_cbc_initkey,
63     cipher_hw_desx_cbc
64 };
65 const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_desx_cbc(void)
66 {
67     return &desx_cbc;
68 }