/src/openssl/providers/implementations/ciphers/cipher_tdes_hw.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2020 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 "prov/ciphercommon.h"  | 
17  |  | #include "cipher_tdes.h"  | 
18  |  |  | 
19  | 0  | #define ks1 tks.ks[0]  | 
20  | 0  | #define ks2 tks.ks[1]  | 
21  | 0  | #define ks3 tks.ks[2]  | 
22  |  |  | 
23  |  | int ossl_cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx,  | 
24  |  |                                      const unsigned char *key, size_t keylen)  | 
25  | 0  | { | 
26  | 0  |     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;  | 
27  | 0  |     DES_cblock *deskey = (DES_cblock *)key;  | 
28  |  | 
  | 
29  | 0  |     tctx->tstream.cbc = NULL;  | 
30  |  | # if defined(SPARC_DES_CAPABLE)  | 
31  |  |     if (SPARC_DES_CAPABLE) { | 
32  |  |         if (ctx->mode == EVP_CIPH_CBC_MODE) { | 
33  |  |             des_t4_key_expand(&deskey[0], &tctx->ks1);  | 
34  |  |             des_t4_key_expand(&deskey[1], &tctx->ks2);  | 
35  |  |             des_t4_key_expand(&deskey[2], &tctx->ks3);  | 
36  |  |             tctx->tstream.cbc = ctx->enc ? des_t4_ede3_cbc_encrypt :  | 
37  |  |                                            des_t4_ede3_cbc_decrypt;  | 
38  |  |             return 1;  | 
39  |  |         }  | 
40  |  |     }  | 
41  |  | # endif  | 
42  | 0  |     DES_set_key_unchecked(&deskey[0], &tctx->ks1);  | 
43  | 0  |     DES_set_key_unchecked(&deskey[1], &tctx->ks2);  | 
44  | 0  |     DES_set_key_unchecked(&deskey[2], &tctx->ks3);  | 
45  | 0  |     return 1;  | 
46  | 0  | }  | 
47  |  |  | 
48  |  | void ossl_cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst,  | 
49  |  |                                  const PROV_CIPHER_CTX *src)  | 
50  | 0  | { | 
51  | 0  |     PROV_TDES_CTX *sctx = (PROV_TDES_CTX *)src;  | 
52  | 0  |     PROV_TDES_CTX *dctx = (PROV_TDES_CTX *)dst;  | 
53  |  | 
  | 
54  | 0  |     *dctx = *sctx;  | 
55  | 0  |     dst->ks = &dctx->tks.ks;  | 
56  | 0  | }  | 
57  |  |  | 
58  |  | int ossl_cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,  | 
59  |  |                             const unsigned char *in, size_t inl)  | 
60  | 0  | { | 
61  | 0  |     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;  | 
62  |  | 
  | 
63  | 0  |     if (tctx->tstream.cbc != NULL) { | 
64  | 0  |         (*tctx->tstream.cbc) (in, out, inl, tctx->tks.ks, ctx->iv);  | 
65  | 0  |         return 1;  | 
66  | 0  |     }  | 
67  |  |  | 
68  | 0  |     while (inl >= MAXCHUNK) { | 
69  | 0  |         DES_ede3_cbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1, &tctx->ks2,  | 
70  | 0  |                              &tctx->ks3, (DES_cblock *)ctx->iv, ctx->enc);  | 
71  | 0  |         inl -= MAXCHUNK;  | 
72  | 0  |         in += MAXCHUNK;  | 
73  | 0  |         out += MAXCHUNK;  | 
74  | 0  |     }  | 
75  | 0  |     if (inl > 0)  | 
76  | 0  |         DES_ede3_cbc_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2,  | 
77  | 0  |                              &tctx->ks3, (DES_cblock *)ctx->iv, ctx->enc);  | 
78  | 0  |     return 1;  | 
79  | 0  | }  | 
80  |  |  | 
81  |  | int ossl_cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,  | 
82  |  |                             const unsigned char *in, size_t len)  | 
83  | 0  | { | 
84  | 0  |     size_t i;  | 
85  | 0  |     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;  | 
86  |  | 
  | 
87  | 0  |     if (len < DES_BLOCK_SIZE)  | 
88  | 0  |         return 1;  | 
89  |  |  | 
90  | 0  |     for (i = 0, len -= DES_BLOCK_SIZE; i <= len; i += DES_BLOCK_SIZE) { | 
91  | 0  |         DES_ecb3_encrypt((const_DES_cblock *)(in + i), (DES_cblock *)(out + i),  | 
92  | 0  |                          &tctx->ks1, &tctx->ks2, &tctx->ks3, ctx->enc);  | 
93  | 0  |     }  | 
94  | 0  |     return 1;  | 
95  | 0  | }  | 
96  |  |  | 
97  |  | PROV_CIPHER_HW_tdes_mode(ede3, ecb)  | 
98  |  | PROV_CIPHER_HW_tdes_mode(ede3, cbc)  |