1 /*
2  * Copyright 1995-2021 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 "cipher_tdes_default.h"
17 
18 #define ks1 tks.ks[0]
19 #define ks2 tks.ks[1]
20 #define ks3 tks.ks[2]
21 
ossl_cipher_hw_tdes_ede2_initkey(PROV_CIPHER_CTX * ctx,const unsigned char * key,size_t keylen)22 static int ossl_cipher_hw_tdes_ede2_initkey(PROV_CIPHER_CTX *ctx,
23                                             const unsigned char *key,
24                                             size_t keylen)
25 {
26     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
27     DES_cblock *deskey = (DES_cblock *)key;
28 
29     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             memcpy(&tctx->ks3, &tctx->ks1, sizeof(tctx->ks1));
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     DES_set_key_unchecked(&deskey[0], &tctx->ks1);
43     DES_set_key_unchecked(&deskey[1], &tctx->ks2);
44     memcpy(&tctx->ks3, &tctx->ks1, sizeof(tctx->ks1));
45     return 1;
46 }
47 
ossl_cipher_hw_tdes_ofb(PROV_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)48 static int ossl_cipher_hw_tdes_ofb(PROV_CIPHER_CTX *ctx, unsigned char *out,
49                                    const unsigned char *in, size_t inl)
50 {
51     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
52     int num = ctx->num;
53 
54     while (inl >= MAXCHUNK) {
55         DES_ede3_ofb64_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1, &tctx->ks2,
56                                &tctx->ks3, (DES_cblock *)ctx->iv, &num);
57         inl -= MAXCHUNK;
58         in += MAXCHUNK;
59         out += MAXCHUNK;
60     }
61     if (inl > 0) {
62         DES_ede3_ofb64_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2,
63                                &tctx->ks3, (DES_cblock *)ctx->iv, &num);
64     }
65     ctx->num = num;
66     return 1;
67 }
68 
ossl_cipher_hw_tdes_cfb(PROV_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)69 static int ossl_cipher_hw_tdes_cfb(PROV_CIPHER_CTX *ctx, unsigned char *out,
70                                    const unsigned char *in, size_t inl)
71 {
72     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
73     int num = ctx->num;
74 
75     while (inl >= MAXCHUNK) {
76 
77         DES_ede3_cfb64_encrypt(in, out, (long)MAXCHUNK,
78                                &tctx->ks1, &tctx->ks2, &tctx->ks3,
79                                (DES_cblock *)ctx->iv, &num, ctx->enc);
80         inl -= MAXCHUNK;
81         in += MAXCHUNK;
82         out += MAXCHUNK;
83     }
84     if (inl > 0) {
85         DES_ede3_cfb64_encrypt(in, out, (long)inl,
86                                &tctx->ks1, &tctx->ks2, &tctx->ks3,
87                                (DES_cblock *)ctx->iv, &num, ctx->enc);
88     }
89     ctx->num = num;
90     return 1;
91 }
92 
93 /*
94  * Although we have a CFB-r implementation for 3-DES, it doesn't pack the
95  * right way, so wrap it here
96  */
ossl_cipher_hw_tdes_cfb1(PROV_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)97 static int ossl_cipher_hw_tdes_cfb1(PROV_CIPHER_CTX *ctx, unsigned char *out,
98                                     const unsigned char *in, size_t inl)
99 {
100     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
101     size_t n;
102     unsigned char c[1];
103     unsigned char d[1] = { 0 };
104 
105     if (ctx->use_bits == 0)
106         inl *= 8;
107     for (n = 0; n < inl; ++n) {
108         c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
109         DES_ede3_cfb_encrypt(c, d, 1, 1,
110                              &tctx->ks1, &tctx->ks2, &tctx->ks3,
111                              (DES_cblock *)ctx->iv, ctx->enc);
112         out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8)))
113             | ((d[0] & 0x80) >> (unsigned int)(n % 8));
114     }
115 
116     return 1;
117 }
118 
ossl_cipher_hw_tdes_cfb8(PROV_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)119 static int ossl_cipher_hw_tdes_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
120                                     const unsigned char *in, size_t inl)
121 {
122     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
123 
124     while (inl >= MAXCHUNK) {
125         DES_ede3_cfb_encrypt(in, out, 8, (long)MAXCHUNK,
126                              &tctx->ks1, &tctx->ks2, &tctx->ks3,
127                              (DES_cblock *)ctx->iv, ctx->enc);
128         inl -= MAXCHUNK;
129         in += MAXCHUNK;
130         out += MAXCHUNK;
131     }
132     if (inl > 0)
133         DES_ede3_cfb_encrypt(in, out, 8, (long)inl,
134                              &tctx->ks1, &tctx->ks2, &tctx->ks3,
135                              (DES_cblock *)ctx->iv, ctx->enc);
136     return 1;
137 }
138 
139 PROV_CIPHER_HW_tdes_mode(ede3, ofb)
140 PROV_CIPHER_HW_tdes_mode(ede3, cfb)
141 PROV_CIPHER_HW_tdes_mode(ede3, cfb1)
142 PROV_CIPHER_HW_tdes_mode(ede3, cfb8)
143 
144 PROV_CIPHER_HW_tdes_mode(ede2, ecb)
145 PROV_CIPHER_HW_tdes_mode(ede2, cbc)
146 PROV_CIPHER_HW_tdes_mode(ede2, ofb)
147 PROV_CIPHER_HW_tdes_mode(ede2, cfb)
148 
149