xref: /freebsd/crypto/openssl/providers/implementations/ciphers/cipher_tdes_default_hw.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
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], d[1];
103 
104     if (ctx->use_bits == 0)
105         inl *= 8;
106     for (n = 0; n < inl; ++n) {
107         c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
108         DES_ede3_cfb_encrypt(c, d, 1, 1,
109                              &tctx->ks1, &tctx->ks2, &tctx->ks3,
110                              (DES_cblock *)ctx->iv, ctx->enc);
111         out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8)))
112             | ((d[0] & 0x80) >> (unsigned int)(n % 8));
113     }
114 
115     return 1;
116 }
117 
ossl_cipher_hw_tdes_cfb8(PROV_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)118 static int ossl_cipher_hw_tdes_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
119                                     const unsigned char *in, size_t inl)
120 {
121     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
122 
123     while (inl >= MAXCHUNK) {
124         DES_ede3_cfb_encrypt(in, out, 8, (long)MAXCHUNK,
125                              &tctx->ks1, &tctx->ks2, &tctx->ks3,
126                              (DES_cblock *)ctx->iv, ctx->enc);
127         inl -= MAXCHUNK;
128         in += MAXCHUNK;
129         out += MAXCHUNK;
130     }
131     if (inl > 0)
132         DES_ede3_cfb_encrypt(in, out, 8, (long)inl,
133                              &tctx->ks1, &tctx->ks2, &tctx->ks3,
134                              (DES_cblock *)ctx->iv, ctx->enc);
135     return 1;
136 }
137 
138 PROV_CIPHER_HW_tdes_mode(ede3, ofb)
139 PROV_CIPHER_HW_tdes_mode(ede3, cfb)
140 PROV_CIPHER_HW_tdes_mode(ede3, cfb1)
141 PROV_CIPHER_HW_tdes_mode(ede3, cfb8)
142 
143 PROV_CIPHER_HW_tdes_mode(ede2, ecb)
144 PROV_CIPHER_HW_tdes_mode(ede2, cbc)
145 PROV_CIPHER_HW_tdes_mode(ede2, ofb)
146 PROV_CIPHER_HW_tdes_mode(ede2, cfb)
147 
148