xref: /freebsd/crypto/openssl/providers/implementations/ciphers/cipher_tdes_hw.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*
2  * Copyright 1995-2020 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 "prov/ciphercommon.h"
17 #include "cipher_tdes.h"
18 
19 #define ks1 tks.ks[0]
20 #define ks2 tks.ks[1]
21 #define ks3 tks.ks[2]
22 
23 int ossl_cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx,
24                                      const unsigned char *key, 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             des_t4_key_expand(&deskey[2], &tctx->ks3);
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     DES_set_key_unchecked(&deskey[2], &tctx->ks3);
45     return 1;
46 }
47 
48 void ossl_cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst,
49                                  const PROV_CIPHER_CTX *src)
50 {
51     PROV_TDES_CTX *sctx = (PROV_TDES_CTX *)src;
52     PROV_TDES_CTX *dctx = (PROV_TDES_CTX *)dst;
53 
54     *dctx = *sctx;
55     dst->ks = &dctx->tks.ks;
56 }
57 
58 int ossl_cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
59                             const unsigned char *in, size_t inl)
60 {
61     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
62 
63     if (tctx->tstream.cbc != NULL) {
64         (*tctx->tstream.cbc) (in, out, inl, tctx->tks.ks, ctx->iv);
65         return 1;
66     }
67 
68     while (inl >= MAXCHUNK) {
69         DES_ede3_cbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1, &tctx->ks2,
70                              &tctx->ks3, (DES_cblock *)ctx->iv, ctx->enc);
71         inl -= MAXCHUNK;
72         in += MAXCHUNK;
73         out += MAXCHUNK;
74     }
75     if (inl > 0)
76         DES_ede3_cbc_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2,
77                              &tctx->ks3, (DES_cblock *)ctx->iv, ctx->enc);
78     return 1;
79 }
80 
81 int ossl_cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
82                             const unsigned char *in, size_t len)
83 {
84     size_t i;
85     PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
86 
87     if (len < DES_BLOCK_SIZE)
88         return 1;
89 
90     for (i = 0, len -= DES_BLOCK_SIZE; i <= len; i += DES_BLOCK_SIZE) {
91         DES_ecb3_encrypt((const_DES_cblock *)(in + i), (DES_cblock *)(out + i),
92                          &tctx->ks1, &tctx->ks2, &tctx->ks3, ctx->enc);
93     }
94     return 1;
95 }
96 
97 PROV_CIPHER_HW_tdes_mode(ede3, ecb)
98 PROV_CIPHER_HW_tdes_mode(ede3, cbc)
99