2 * Copyright 1995-2020 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
11 * DES low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
16 #include "cipher_tdes_default.h"
22 static int cipher_hw_tdes_ede2_initkey(PROV_CIPHER_CTX *ctx,
23 const unsigned char *key, size_t keylen)
25 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
26 DES_cblock *deskey = (DES_cblock *)key;
28 tctx->tstream.cbc = NULL;
29 # if defined(SPARC_DES_CAPABLE)
30 if (SPARC_DES_CAPABLE) {
31 if (ctx->mode == EVP_CIPH_CBC_MODE) {
32 des_t4_key_expand(&deskey[0], &tctx->ks1);
33 des_t4_key_expand(&deskey[1], &tctx->ks2);
34 memcpy(&tctx->ks3, &tctx->ks1, sizeof(tctx->ks1));
35 tctx->tstream.cbc = ctx->enc ? des_t4_ede3_cbc_encrypt :
36 des_t4_ede3_cbc_decrypt;
41 DES_set_key_unchecked(&deskey[0], &tctx->ks1);
42 DES_set_key_unchecked(&deskey[1], &tctx->ks2);
43 memcpy(&tctx->ks3, &tctx->ks1, sizeof(tctx->ks1));
47 static int cipher_hw_tdes_ofb(PROV_CIPHER_CTX *ctx, unsigned char *out,
48 const unsigned char *in, size_t inl)
50 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
53 while (inl >= MAXCHUNK) {
54 DES_ede3_ofb64_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1, &tctx->ks2,
55 &tctx->ks3, (DES_cblock *)ctx->iv, &num);
61 DES_ede3_ofb64_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2,
62 &tctx->ks3, (DES_cblock *)ctx->iv, &num);
68 static int cipher_hw_tdes_cfb(PROV_CIPHER_CTX *ctx, unsigned char *out,
69 const unsigned char *in, size_t inl)
71 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
74 while (inl >= MAXCHUNK) {
76 DES_ede3_cfb64_encrypt(in, out, (long)MAXCHUNK,
77 &tctx->ks1, &tctx->ks2, &tctx->ks3,
78 (DES_cblock *)ctx->iv, &num, ctx->enc);
84 DES_ede3_cfb64_encrypt(in, out, (long)inl,
85 &tctx->ks1, &tctx->ks2, &tctx->ks3,
86 (DES_cblock *)ctx->iv, &num, ctx->enc);
93 * Although we have a CFB-r implementation for 3-DES, it doesn't pack the
94 * right way, so wrap it here
96 static int cipher_hw_tdes_cfb1(PROV_CIPHER_CTX *ctx, unsigned char *out,
97 const unsigned char *in, size_t inl)
99 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
101 unsigned char c[1], d[1];
103 if ((ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) == 0)
105 for (n = 0; n < inl; ++n) {
106 c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
107 DES_ede3_cfb_encrypt(c, d, 1, 1,
108 &tctx->ks1, &tctx->ks2, &tctx->ks3,
109 (DES_cblock *)ctx->iv, ctx->enc);
110 out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8)))
111 | ((d[0] & 0x80) >> (unsigned int)(n % 8));
117 static int cipher_hw_tdes_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
118 const unsigned char *in, size_t inl)
120 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
122 while (inl >= MAXCHUNK) {
123 DES_ede3_cfb_encrypt(in, out, 8, (long)MAXCHUNK,
124 &tctx->ks1, &tctx->ks2, &tctx->ks3,
125 (DES_cblock *)ctx->iv, ctx->enc);
131 DES_ede3_cfb_encrypt(in, out, 8, (long)inl,
132 &tctx->ks1, &tctx->ks2, &tctx->ks3,
133 (DES_cblock *)ctx->iv, ctx->enc);
137 PROV_CIPHER_HW_tdes_mode(ede3, ofb)
138 PROV_CIPHER_HW_tdes_mode(ede3, cfb)
139 PROV_CIPHER_HW_tdes_mode(ede3, cfb1)
140 PROV_CIPHER_HW_tdes_mode(ede3, cfb8)
142 PROV_CIPHER_HW_tdes_mode(ede2, ecb)
143 PROV_CIPHER_HW_tdes_mode(ede2, cbc)
144 PROV_CIPHER_HW_tdes_mode(ede2, ofb)
145 PROV_CIPHER_HW_tdes_mode(ede2, cfb)