| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/cipher.h> |
| 16 | #include <openssl/des.h> |
| 17 | #include <openssl/nid.h> |
| 18 |
| 19 | #include "../des/internal.h" |
| 20 | #include "../fipsmodule/cipher/internal.h" |
| 21 | #include "internal.h" |
| 22 |
| 23 |
| 24 | typedef struct { |
| 25 | union { |
| 26 | double align; |
| 27 | DES_key_schedule ks; |
| 28 | } ks; |
| 29 | } EVP_DES_KEY; |
| 30 |
| 31 | static int des_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 32 | const uint8_t *iv, int enc) { |
| 33 | EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data; |
| 34 | DES_set_key_ex(key, &dat->ks.ks); |
| 35 | return 1; |
| 36 | } |
| 37 |
| 38 | static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 39 | size_t in_len) { |
| 40 | EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data; |
| 41 | DES_ncbc_encrypt_ex(in, out, in_len, &dat->ks.ks, ctx->iv, ctx->encrypt); |
| 42 | return 1; |
| 43 | } |
| 44 |
| 45 | static const EVP_CIPHER evp_des_cbc = { |
| 46 | /*nid=*/NID_des_cbc, |
| 47 | /*block_size=*/8, |
| 48 | /*key_len=*/8, |
| 49 | /*iv_len=*/8, |
| 50 | /*ctx_size=*/sizeof(EVP_DES_KEY), |
| 51 | /*flags=*/EVP_CIPH_CBC_MODE, |
| 52 | /*init=*/des_init_key, |
| 53 | /*cipher=*/des_cbc_cipher, |
| 54 | /*cleanup=*/nullptr, |
| 55 | /*ctrl=*/nullptr, |
| 56 | }; |
| 57 |
| 58 | const EVP_CIPHER *EVP_des_cbc(void) { return &evp_des_cbc; } |
| 59 |
| 60 | static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 61 | size_t in_len) { |
| 62 | if (in_len < ctx->cipher->block_size) { |
| 63 | return 1; |
| 64 | } |
| 65 | in_len -= ctx->cipher->block_size; |
| 66 |
| 67 | EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data; |
| 68 | for (size_t i = 0; i <= in_len; i += ctx->cipher->block_size) { |
| 69 | DES_ecb_encrypt_ex(in + i, out + i, &dat->ks.ks, ctx->encrypt); |
| 70 | } |
| 71 | return 1; |
| 72 | } |
| 73 |
| 74 | static const EVP_CIPHER evp_des_ecb = { |
| 75 | /*nid=*/NID_des_ecb, |
| 76 | /*block_size=*/8, |
| 77 | /*key_len=*/8, |
| 78 | /*iv_len=*/0, |
| 79 | /*ctx_size=*/sizeof(EVP_DES_KEY), |
| 80 | /*flags=*/EVP_CIPH_ECB_MODE, |
| 81 | /*init=*/des_init_key, |
| 82 | /*cipher=*/des_ecb_cipher, |
| 83 | /*cleanup=*/nullptr, |
| 84 | /*ctrl=*/nullptr, |
| 85 | }; |
| 86 |
| 87 | const EVP_CIPHER *EVP_des_ecb(void) { return &evp_des_ecb; } |
| 88 |
| 89 | typedef struct { |
| 90 | union { |
| 91 | double align; |
| 92 | DES_key_schedule ks[3]; |
| 93 | } ks; |
| 94 | } DES_EDE_KEY; |
| 95 |
| 96 | static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 97 | const uint8_t *iv, int enc) { |
| 98 | DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data; |
| 99 | DES_set_key_ex(key, &dat->ks.ks[0]); |
| 100 | DES_set_key_ex(key + 8, &dat->ks.ks[1]); |
| 101 | DES_set_key_ex(key + 16, &dat->ks.ks[2]); |
| 102 | return 1; |
| 103 | } |
| 104 |
| 105 | static int des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, |
| 106 | const uint8_t *in, size_t in_len) { |
| 107 | DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data; |
| 108 | DES_ede3_cbc_encrypt_ex(in, out, in_len, &dat->ks.ks[0], &dat->ks.ks[1], |
| 109 | &dat->ks.ks[2], ctx->iv, ctx->encrypt); |
| 110 | return 1; |
| 111 | } |
| 112 |
| 113 | static const EVP_CIPHER evp_des_ede3_cbc = { |
| 114 | /*nid=*/NID_des_ede3_cbc, |
| 115 | /*block_size=*/8, |
| 116 | /*key_len=*/24, |
| 117 | /*iv_len=*/8, |
| 118 | /*ctx_size=*/sizeof(DES_EDE_KEY), |
| 119 | /*flags=*/EVP_CIPH_CBC_MODE, |
| 120 | /*init=*/des_ede3_init_key, |
| 121 | /*cipher=*/des_ede3_cbc_cipher, |
| 122 | /*cleanup=*/nullptr, |
| 123 | /*ctrl=*/nullptr, |
| 124 | }; |
| 125 |
| 126 | const EVP_CIPHER *EVP_des_ede3_cbc(void) { return &evp_des_ede3_cbc; } |
| 127 |
| 128 | static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 129 | const uint8_t *iv, int enc) { |
| 130 | DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data; |
| 131 | // 2-DES is 3-DES with the first key used twice. |
| 132 | DES_set_key_ex(key, &dat->ks.ks[0]); |
| 133 | DES_set_key_ex(key + 8, &dat->ks.ks[1]); |
| 134 | DES_set_key_ex(key, &dat->ks.ks[2]); |
| 135 | return 1; |
| 136 | } |
| 137 |
| 138 | static const EVP_CIPHER evp_des_ede_cbc = { |
| 139 | /*nid=*/NID_des_ede_cbc, |
| 140 | /*block_size=*/8, |
| 141 | /*key_len=*/16, |
| 142 | /*iv_len=*/8, |
| 143 | /*ctx_size=*/sizeof(DES_EDE_KEY), |
| 144 | /*flags=*/EVP_CIPH_CBC_MODE, |
| 145 | /*init=*/des_ede_init_key, |
| 146 | /*cipher=*/des_ede3_cbc_cipher, |
| 147 | /*cleanup=*/nullptr, |
| 148 | /*ctrl=*/nullptr, |
| 149 | }; |
| 150 |
| 151 | const EVP_CIPHER *EVP_des_ede_cbc(void) { return &evp_des_ede_cbc; } |
| 152 |
| 153 | static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, |
| 154 | const uint8_t *in, size_t in_len) { |
| 155 | if (in_len < ctx->cipher->block_size) { |
| 156 | return 1; |
| 157 | } |
| 158 | in_len -= ctx->cipher->block_size; |
| 159 |
| 160 | DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data; |
| 161 | for (size_t i = 0; i <= in_len; i += ctx->cipher->block_size) { |
| 162 | DES_ecb3_encrypt_ex(in + i, out + i, &dat->ks.ks[0], &dat->ks.ks[1], |
| 163 | &dat->ks.ks[2], ctx->encrypt); |
| 164 | } |
| 165 | return 1; |
| 166 | } |
| 167 |
| 168 | static const EVP_CIPHER evp_des_ede = { |
| 169 | /*nid=*/NID_des_ede_ecb, |
| 170 | /*block_size=*/8, |
| 171 | /*key_len=*/16, |
| 172 | /*iv_len=*/0, |
| 173 | /*ctx_size=*/sizeof(DES_EDE_KEY), |
| 174 | /*flags=*/EVP_CIPH_ECB_MODE, |
| 175 | /*init=*/des_ede_init_key, |
| 176 | /*cipher=*/des_ede_ecb_cipher, |
| 177 | /*cleanup=*/nullptr, |
| 178 | /*ctrl=*/nullptr, |
| 179 | }; |
| 180 |
| 181 | const EVP_CIPHER *EVP_des_ede(void) { return &evp_des_ede; } |
| 182 |
| 183 | static const EVP_CIPHER evp_des_ede3 = { |
| 184 | /*nid=*/NID_des_ede3_ecb, |
| 185 | /*block_size=*/8, |
| 186 | /*key_len=*/24, |
| 187 | /*iv_len=*/0, |
| 188 | /*ctx_size=*/sizeof(DES_EDE_KEY), |
| 189 | /*flags=*/EVP_CIPH_ECB_MODE, |
| 190 | /*init=*/des_ede3_init_key, |
| 191 | /*cipher=*/des_ede_ecb_cipher, |
| 192 | /*cleanup=*/nullptr, |
| 193 | /*ctrl=*/nullptr, |
| 194 | }; |
| 195 |
| 196 | const EVP_CIPHER *EVP_des_ede3(void) { return &evp_des_ede3; } |
| 197 |
| 198 | const EVP_CIPHER *EVP_des_ede3_ecb(void) { return EVP_des_ede3(); } |
| 199 |