1/*
2 * Copyright 1995-2019 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#include "prov/ciphercommon.h"
11#include "cipher_tdes.h"
12
13#define ks1 tks.ks[0]
14#define ks2 tks.ks[1]
15#define ks3 tks.ks[2]
16
17int cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx, const unsigned char *key,
18 size_t keylen)
19{
20 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
21 DES_cblock *deskey = (DES_cblock *)key;
22
23 tctx->tstream.cbc = NULL;
24# if defined(SPARC_DES_CAPABLE)
25 if (SPARC_DES_CAPABLE) {
26 if (ctx->mode == EVP_CIPH_CBC_MODE) {
27 des_t4_key_expand(&deskey[0], &tctx->ks1);
28 des_t4_key_expand(&deskey[1], &tctx->ks2);
29 des_t4_key_expand(&deskey[2], &tctx->ks3);
30 tctx->tstream.cbc = ctx->enc ? des_t4_ede3_cbc_encrypt :
31 des_t4_ede3_cbc_decrypt;
32 return 1;
33 }
34 }
35# endif
36 DES_set_key_unchecked(&deskey[0], &tctx->ks1);
37 DES_set_key_unchecked(&deskey[1], &tctx->ks2);
38 DES_set_key_unchecked(&deskey[2], &tctx->ks3);
39 return 1;
40}
41
42int cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
43 const unsigned char *in, size_t inl)
44{
45 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
46
47 if (tctx->tstream.cbc != NULL) {
48 (*tctx->tstream.cbc) (in, out, inl, tctx->tks.ks, ctx->iv);
49 return 1;
50 }
51
52 while (inl >= MAXCHUNK) {
53 DES_ede3_cbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1, &tctx->ks2,
54 &tctx->ks3, (DES_cblock *)ctx->iv, ctx->enc);
55 inl -= MAXCHUNK;
56 in += MAXCHUNK;
57 out += MAXCHUNK;
58 }
59 if (inl > 0)
60 DES_ede3_cbc_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2,
61 &tctx->ks3, (DES_cblock *)ctx->iv, ctx->enc);
62 return 1;
63}
64
65int cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
66 const unsigned char *in, size_t len)
67{
68 size_t i;
69 PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
70
71 if (len < DES_BLOCK_SIZE)
72 return 1;
73
74 for (i = 0, len -= DES_BLOCK_SIZE; i <= len; i += DES_BLOCK_SIZE) {
75 DES_ecb3_encrypt((const_DES_cblock *)(in + i), (DES_cblock *)(out + i),
76 &tctx->ks1, &tctx->ks2, &tctx->ks3, ctx->enc);
77 }
78 return 1;
79}
80
81PROV_CIPHER_HW_tdes_mode(ede3, ecb)
82PROV_CIPHER_HW_tdes_mode(ede3, cbc)
83