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 "cipher_tdes_default.h" |
11 | |
12 | #define ks1 tks.ks[0] |
13 | #define ks2 tks.ks[1] |
14 | #define ks3 tks.ks[2] |
15 | |
16 | static int cipher_hw_tdes_ede2_initkey(PROV_CIPHER_CTX *ctx, |
17 | const unsigned char *key, size_t keylen) |
18 | { |
19 | PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; |
20 | DES_cblock *deskey = (DES_cblock *)key; |
21 | |
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; |
31 | return 1; |
32 | } |
33 | } |
34 | # endif |
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)); |
38 | return 1; |
39 | } |
40 | |
41 | static int cipher_hw_tdes_ofb(PROV_CIPHER_CTX *ctx, unsigned char *out, |
42 | const unsigned char *in, size_t inl) |
43 | { |
44 | PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; |
45 | int num = ctx->num; |
46 | |
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); |
50 | inl -= MAXCHUNK; |
51 | in += MAXCHUNK; |
52 | out += MAXCHUNK; |
53 | } |
54 | if (inl > 0) { |
55 | DES_ede3_ofb64_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2, |
56 | &tctx->ks3, (DES_cblock *)ctx->iv, &num); |
57 | } |
58 | ctx->num = num; |
59 | return 1; |
60 | } |
61 | |
62 | static int cipher_hw_tdes_cfb(PROV_CIPHER_CTX *ctx, unsigned char *out, |
63 | const unsigned char *in, size_t inl) |
64 | { |
65 | PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; |
66 | int num = ctx->num; |
67 | |
68 | while (inl >= MAXCHUNK) { |
69 | |
70 | DES_ede3_cfb64_encrypt(in, out, (long)MAXCHUNK, |
71 | &tctx->ks1, &tctx->ks2, &tctx->ks3, |
72 | (DES_cblock *)ctx->iv, &num, ctx->enc); |
73 | inl -= MAXCHUNK; |
74 | in += MAXCHUNK; |
75 | out += MAXCHUNK; |
76 | } |
77 | if (inl > 0) { |
78 | DES_ede3_cfb64_encrypt(in, out, (long)inl, |
79 | &tctx->ks1, &tctx->ks2, &tctx->ks3, |
80 | (DES_cblock *)ctx->iv, &num, ctx->enc); |
81 | } |
82 | ctx->num = num; |
83 | return 1; |
84 | } |
85 | |
86 | /* |
87 | * Although we have a CFB-r implementation for 3-DES, it doesn't pack the |
88 | * right way, so wrap it here |
89 | */ |
90 | static int cipher_hw_tdes_cfb1(PROV_CIPHER_CTX *ctx, unsigned char *out, |
91 | const unsigned char *in, size_t inl) |
92 | { |
93 | PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; |
94 | size_t n; |
95 | unsigned char c[1], d[1]; |
96 | |
97 | if ((ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) == 0) |
98 | inl *= 8; |
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)); |
106 | } |
107 | |
108 | return 1; |
109 | } |
110 | |
111 | static int cipher_hw_tdes_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out, |
112 | const unsigned char *in, size_t inl) |
113 | { |
114 | PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; |
115 | |
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); |
120 | inl -= MAXCHUNK; |
121 | in += MAXCHUNK; |
122 | out += MAXCHUNK; |
123 | } |
124 | if (inl > 0) |
125 | DES_ede3_cfb_encrypt(in, out, 8, (long)inl, |
126 | &tctx->ks1, &tctx->ks2, &tctx->ks3, |
127 | (DES_cblock *)ctx->iv, ctx->enc); |
128 | return 1; |
129 | } |
130 | |
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) |
135 | |
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) |
140 | |
141 | |