Implement the NULL cipher in the default provider
[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 #include <openssl/des.h>
11 #include "cipher_tdes_default.h"
12
13 /*
14  * Note the PROV_TDES_CTX has been used for the DESX cipher, just to reduce
15  * code size.
16  */
17 #define ks1 tks.ks[0]
18 #define ks2 tks.ks[1].ks[0].cblock
19 #define ks3 tks.ks[2].ks[0].cblock
20
21 static int cipher_hw_desx_cbc_initkey(PROV_CIPHER_CTX *ctx,
22                                       const unsigned char *key, size_t keylen)
23 {
24     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
25     DES_cblock *deskey = (DES_cblock *)key;
26
27     DES_set_key_unchecked(deskey, &tctx->ks1);
28     memcpy(&tctx->ks2, &key[8], 8);
29     memcpy(&tctx->ks3, &key[16], 8);
30
31     return 1;
32 }
33
34 static int cipher_hw_desx_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
35                               const unsigned char *in, size_t inl)
36 {
37     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
38
39     while (inl >= MAXCHUNK) {
40         DES_xcbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1,
41                          (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,
42                          ctx->enc);
43         inl -= MAXCHUNK;
44         in += MAXCHUNK;
45         out += MAXCHUNK;
46     }
47     if (inl > 0)
48         DES_xcbc_encrypt(in, out, (long)inl, &tctx->ks1,
49                          (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,
50                          ctx->enc);
51     return 1;
52 }
53
54 static const PROV_CIPHER_HW desx_cbc =
55 {
56     cipher_hw_desx_cbc_initkey,
57     cipher_hw_desx_cbc
58 };
59 const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_desx_cbc(void)
60 {
61     return &desx_cbc;
62 }