2 * Copyright 1995-2021 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 ossl_cipher_hw_tdes_ede2_initkey(PROV_CIPHER_CTX *ctx,
23 const unsigned char *key,
26 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
27 DES_cblock *deskey = (DES_cblock *)key;
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;
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));
48 static int ossl_cipher_hw_tdes_ofb(PROV_CIPHER_CTX *ctx, unsigned char *out,
49 const unsigned char *in, size_t inl)
51 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
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);
62 DES_ede3_ofb64_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2,
63 &tctx->ks3, (DES_cblock *)ctx->iv, &num);
69 static int ossl_cipher_hw_tdes_cfb(PROV_CIPHER_CTX *ctx, unsigned char *out,
70 const unsigned char *in, size_t inl)
72 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
75 while (inl >= MAXCHUNK) {
77 DES_ede3_cfb64_encrypt(in, out, (long)MAXCHUNK,
78 &tctx->ks1, &tctx->ks2, &tctx->ks3,
79 (DES_cblock *)ctx->iv, &num, ctx->enc);
85 DES_ede3_cfb64_encrypt(in, out, (long)inl,
86 &tctx->ks1, &tctx->ks2, &tctx->ks3,
87 (DES_cblock *)ctx->iv, &num, ctx->enc);
94 * Although we have a CFB-r implementation for 3-DES, it doesn't pack the
95 * right way, so wrap it here
97 static int ossl_cipher_hw_tdes_cfb1(PROV_CIPHER_CTX *ctx, unsigned char *out,
98 const unsigned char *in, size_t inl)
100 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
102 unsigned char c[1], d[1];
104 if (ctx->use_bits == 0)
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));
118 static int ossl_cipher_hw_tdes_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
119 const unsigned char *in, size_t inl)
121 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
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);
132 DES_ede3_cfb_encrypt(in, out, 8, (long)inl,
133 &tctx->ks1, &tctx->ks2, &tctx->ks3,
134 (DES_cblock *)ctx->iv, ctx->enc);
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)
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)