/src/openssl/providers/implementations/ciphers/cipher_desx_hw.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2024 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  | 0  | #define ks1 tks.ks[0]  | 
24  | 0  | #define ks2 tks.ks[1].ks[0].cblock  | 
25  | 0  | #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  | 0  | { | 
30  | 0  |     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;  | 
31  | 0  |     DES_cblock *deskey = (DES_cblock *)key;  | 
32  |  | 
  | 
33  | 0  |     DES_set_key_unchecked(deskey, &tctx->ks1);  | 
34  | 0  |     memcpy(&tctx->ks2, &key[8], 8);  | 
35  | 0  |     memcpy(&tctx->ks3, &key[16], 8);  | 
36  |  | 
  | 
37  | 0  |     return 1;  | 
38  | 0  | }  | 
39  |  |  | 
40  |  | static void cipher_hw_desx_copyctx(PROV_CIPHER_CTX *dst,  | 
41  |  |                                    const PROV_CIPHER_CTX *src)  | 
42  | 0  | { | 
43  | 0  |     PROV_TDES_CTX *sctx = (PROV_TDES_CTX *)src;  | 
44  | 0  |     PROV_TDES_CTX *dctx = (PROV_TDES_CTX *)dst;  | 
45  |  | 
  | 
46  | 0  |     *dctx = *sctx;  | 
47  | 0  |     dst->ks = &dctx->tks.ks;  | 
48  | 0  | }  | 
49  |  |  | 
50  |  | static int cipher_hw_desx_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,  | 
51  |  |                               const unsigned char *in, size_t inl)  | 
52  | 0  | { | 
53  | 0  |     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;  | 
54  |  | 
  | 
55  | 0  |     while (inl >= MAXCHUNK) { | 
56  | 0  |         DES_xcbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1,  | 
57  | 0  |                          (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,  | 
58  | 0  |                          ctx->enc);  | 
59  | 0  |         inl -= MAXCHUNK;  | 
60  | 0  |         in += MAXCHUNK;  | 
61  | 0  |         out += MAXCHUNK;  | 
62  | 0  |     }  | 
63  | 0  |     if (inl > 0)  | 
64  | 0  |         DES_xcbc_encrypt(in, out, (long)inl, &tctx->ks1,  | 
65  | 0  |                          (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,  | 
66  | 0  |                          ctx->enc);  | 
67  | 0  |     return 1;  | 
68  | 0  | }  | 
69  |  |  | 
70  |  | static const PROV_CIPHER_HW desx_cbc = { | 
71  |  |     cipher_hw_desx_cbc_initkey,  | 
72  |  |     cipher_hw_desx_cbc,  | 
73  |  |     cipher_hw_desx_copyctx  | 
74  |  | };  | 
75  |  |  | 
76  |  | const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_desx_cbc(void)  | 
77  | 0  | { | 
78  | 0  |     return &desx_cbc;  | 
79  | 0  | }  |