2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "cipher_tdes_default.h"
16 static int cipher_hw_tdes_ede2_initkey(PROV_CIPHER_CTX
*ctx
,
17 const unsigned char *key
, size_t keylen
)
19 PROV_TDES_CTX
*tctx
= (PROV_TDES_CTX
*)ctx
;
20 DES_cblock
*deskey
= (DES_cblock
*)key
;
22 tctx
->tstream
.cbc
= NULL
;
23 # if defined(SPARC_DES_CAPABLE)
24 if (SPARC_DES_CAPABLE
) {
25 if (ctx
->mode
== EVP_CIPH_CBC_MODE
) {
26 des_t4_key_expand(&deskey
[0], &tctx
->ks1
);
27 des_t4_key_expand(&deskey
[1], &tctx
->ks2
);
28 memcpy(&tctx
->ks3
, &tctx
->ks1
, sizeof(tctx
->ks1
));
29 tctx
->tstream
.cbc
= ctx
->enc
? des_t4_ede3_cbc_encrypt
:
30 des_t4_ede3_cbc_decrypt
;
35 DES_set_key_unchecked(&deskey
[0], &tctx
->ks1
);
36 DES_set_key_unchecked(&deskey
[1], &tctx
->ks2
);
37 memcpy(&tctx
->ks3
, &tctx
->ks1
, sizeof(tctx
->ks1
));
41 static int cipher_hw_tdes_ofb(PROV_CIPHER_CTX
*ctx
, unsigned char *out
,
42 const unsigned char *in
, size_t inl
)
44 PROV_TDES_CTX
*tctx
= (PROV_TDES_CTX
*)ctx
;
47 while (inl
>= MAXCHUNK
) {
48 DES_ede3_ofb64_encrypt(in
, out
, (long)MAXCHUNK
, &tctx
->ks1
, &tctx
->ks2
,
49 &tctx
->ks3
, (DES_cblock
*)ctx
->iv
, &num
);
55 DES_ede3_ofb64_encrypt(in
, out
, (long)inl
, &tctx
->ks1
, &tctx
->ks2
,
56 &tctx
->ks3
, (DES_cblock
*)ctx
->iv
, &num
);
62 static int cipher_hw_tdes_cfb(PROV_CIPHER_CTX
*ctx
, unsigned char *out
,
63 const unsigned char *in
, size_t inl
)
65 PROV_TDES_CTX
*tctx
= (PROV_TDES_CTX
*)ctx
;
68 while (inl
>= MAXCHUNK
) {
70 DES_ede3_cfb64_encrypt(in
, out
, (long)MAXCHUNK
,
71 &tctx
->ks1
, &tctx
->ks2
, &tctx
->ks3
,
72 (DES_cblock
*)ctx
->iv
, &num
, ctx
->enc
);
78 DES_ede3_cfb64_encrypt(in
, out
, (long)inl
,
79 &tctx
->ks1
, &tctx
->ks2
, &tctx
->ks3
,
80 (DES_cblock
*)ctx
->iv
, &num
, ctx
->enc
);
87 * Although we have a CFB-r implementation for 3-DES, it doesn't pack the
88 * right way, so wrap it here
90 static int cipher_hw_tdes_cfb1(PROV_CIPHER_CTX
*ctx
, unsigned char *out
,
91 const unsigned char *in
, size_t inl
)
93 PROV_TDES_CTX
*tctx
= (PROV_TDES_CTX
*)ctx
;
95 unsigned char c
[1], d
[1];
97 if ((ctx
->flags
& EVP_CIPH_FLAG_LENGTH_BITS
) == 0)
99 for (n
= 0; n
< inl
; ++n
) {
100 c
[0] = (in
[n
/ 8] & (1 << (7 - n
% 8))) ? 0x80 : 0;
101 DES_ede3_cfb_encrypt(c
, d
, 1, 1,
102 &tctx
->ks1
, &tctx
->ks2
, &tctx
->ks3
,
103 (DES_cblock
*)ctx
->iv
, ctx
->enc
);
104 out
[n
/ 8] = (out
[n
/ 8] & ~(0x80 >> (unsigned int)(n
% 8)))
105 | ((d
[0] & 0x80) >> (unsigned int)(n
% 8));
111 static int cipher_hw_tdes_cfb8(PROV_CIPHER_CTX
*ctx
, unsigned char *out
,
112 const unsigned char *in
, size_t inl
)
114 PROV_TDES_CTX
*tctx
= (PROV_TDES_CTX
*)ctx
;
116 while (inl
>= MAXCHUNK
) {
117 DES_ede3_cfb_encrypt(in
, out
, 8, (long)MAXCHUNK
,
118 &tctx
->ks1
, &tctx
->ks2
, &tctx
->ks3
,
119 (DES_cblock
*)ctx
->iv
, ctx
->enc
);
125 DES_ede3_cfb_encrypt(in
, out
, 8, (long)inl
,
126 &tctx
->ks1
, &tctx
->ks2
, &tctx
->ks3
,
127 (DES_cblock
*)ctx
->iv
, ctx
->enc
);
131 PROV_CIPHER_HW_tdes_mode(ede3
, ofb
)
132 PROV_CIPHER_HW_tdes_mode(ede3
, cfb
)
133 PROV_CIPHER_HW_tdes_mode(ede3
, cfb1
)
134 PROV_CIPHER_HW_tdes_mode(ede3
, cfb8
)
136 PROV_CIPHER_HW_tdes_mode(ede2
, ecb
)
137 PROV_CIPHER_HW_tdes_mode(ede2
, cbc
)
138 PROV_CIPHER_HW_tdes_mode(ede2
, ofb
)
139 PROV_CIPHER_HW_tdes_mode(ede2
, cfb
)