]>
Commit | Line | Data |
---|---|---|
4a42e264 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
4a42e264 SL |
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 | ||
c6fec81b P |
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 | ||
4a42e264 SL |
16 | #include "cipher_tdes_default.h" |
17 | ||
18 | #define ks1 tks.ks[0] | |
19 | #define ks2 tks.ks[1] | |
20 | #define ks3 tks.ks[2] | |
21 | ||
22 | static int cipher_hw_tdes_ede2_initkey(PROV_CIPHER_CTX *ctx, | |
23 | const unsigned char *key, size_t keylen) | |
24 | { | |
25 | PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; | |
26 | DES_cblock *deskey = (DES_cblock *)key; | |
27 | ||
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; | |
37 | return 1; | |
38 | } | |
39 | } | |
40 | # endif | |
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)); | |
44 | return 1; | |
45 | } | |
46 | ||
47 | static int cipher_hw_tdes_ofb(PROV_CIPHER_CTX *ctx, unsigned char *out, | |
48 | const unsigned char *in, size_t inl) | |
49 | { | |
50 | PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; | |
51 | int num = ctx->num; | |
52 | ||
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); | |
56 | inl -= MAXCHUNK; | |
57 | in += MAXCHUNK; | |
58 | out += MAXCHUNK; | |
59 | } | |
60 | if (inl > 0) { | |
61 | DES_ede3_ofb64_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2, | |
62 | &tctx->ks3, (DES_cblock *)ctx->iv, &num); | |
63 | } | |
64 | ctx->num = num; | |
65 | return 1; | |
66 | } | |
67 | ||
68 | static int cipher_hw_tdes_cfb(PROV_CIPHER_CTX *ctx, unsigned char *out, | |
69 | const unsigned char *in, size_t inl) | |
70 | { | |
71 | PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; | |
72 | int num = ctx->num; | |
73 | ||
74 | while (inl >= MAXCHUNK) { | |
75 | ||
76 | DES_ede3_cfb64_encrypt(in, out, (long)MAXCHUNK, | |
77 | &tctx->ks1, &tctx->ks2, &tctx->ks3, | |
78 | (DES_cblock *)ctx->iv, &num, ctx->enc); | |
79 | inl -= MAXCHUNK; | |
80 | in += MAXCHUNK; | |
81 | out += MAXCHUNK; | |
82 | } | |
83 | if (inl > 0) { | |
84 | DES_ede3_cfb64_encrypt(in, out, (long)inl, | |
85 | &tctx->ks1, &tctx->ks2, &tctx->ks3, | |
86 | (DES_cblock *)ctx->iv, &num, ctx->enc); | |
87 | } | |
88 | ctx->num = num; | |
89 | return 1; | |
90 | } | |
91 | ||
92 | /* | |
93 | * Although we have a CFB-r implementation for 3-DES, it doesn't pack the | |
94 | * right way, so wrap it here | |
95 | */ | |
96 | static int cipher_hw_tdes_cfb1(PROV_CIPHER_CTX *ctx, unsigned char *out, | |
97 | const unsigned char *in, size_t inl) | |
98 | { | |
99 | PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; | |
100 | size_t n; | |
101 | unsigned char c[1], d[1]; | |
102 | ||
103 | if ((ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) == 0) | |
104 | inl *= 8; | |
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)); | |
112 | } | |
113 | ||
114 | return 1; | |
115 | } | |
116 | ||
117 | static int cipher_hw_tdes_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out, | |
118 | const unsigned char *in, size_t inl) | |
119 | { | |
120 | PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx; | |
121 | ||
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); | |
126 | inl -= MAXCHUNK; | |
127 | in += MAXCHUNK; | |
128 | out += MAXCHUNK; | |
129 | } | |
130 | if (inl > 0) | |
131 | DES_ede3_cfb_encrypt(in, out, 8, (long)inl, | |
132 | &tctx->ks1, &tctx->ks2, &tctx->ks3, | |
133 | (DES_cblock *)ctx->iv, ctx->enc); | |
134 | return 1; | |
135 | } | |
136 | ||
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) | |
141 | ||
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) | |
146 |