/src/openssl30/crypto/evp/e_des.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2021 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 <stdio.h> | 
| 17 |  | #include "internal/cryptlib.h" | 
| 18 |  | #ifndef OPENSSL_NO_DES | 
| 19 |  | # include <openssl/evp.h> | 
| 20 |  | # include <openssl/objects.h> | 
| 21 |  | # include "crypto/evp.h" | 
| 22 |  | # include <openssl/des.h> | 
| 23 |  | # include <openssl/rand.h> | 
| 24 |  | # include "evp_local.h" | 
| 25 |  |  | 
| 26 |  | typedef struct { | 
| 27 |  |     union { | 
| 28 |  |         OSSL_UNION_ALIGN; | 
| 29 |  |         DES_key_schedule ks; | 
| 30 |  |     } ks; | 
| 31 |  |     union { | 
| 32 |  |         void (*cbc) (const void *, void *, size_t, | 
| 33 |  |                      const DES_key_schedule *, unsigned char *); | 
| 34 |  |     } stream; | 
| 35 |  | } EVP_DES_KEY; | 
| 36 |  |  | 
| 37 |  | # if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__)) | 
| 38 |  | /* ----------^^^ this is not a typo, just a way to detect that | 
| 39 |  |  * assembler support was in general requested... */ | 
| 40 |  | #  include "crypto/sparc_arch.h" | 
| 41 |  |  | 
| 42 |  | #  define SPARC_DES_CAPABLE       (OPENSSL_sparcv9cap_P[1] & CFR_DES) | 
| 43 |  |  | 
| 44 |  | void des_t4_key_expand(const void *key, DES_key_schedule *ks); | 
| 45 |  | void des_t4_cbc_encrypt(const void *inp, void *out, size_t len, | 
| 46 |  |                         const DES_key_schedule *ks, unsigned char iv[8]); | 
| 47 |  | void des_t4_cbc_decrypt(const void *inp, void *out, size_t len, | 
| 48 |  |                         const DES_key_schedule *ks, unsigned char iv[8]); | 
| 49 |  | # endif | 
| 50 |  |  | 
| 51 |  | static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 
| 52 |  |                         const unsigned char *iv, int enc); | 
| 53 |  | static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); | 
| 54 |  |  | 
| 55 |  | /* | 
| 56 |  |  * Because of various casts and different names can't use | 
| 57 |  |  * IMPLEMENT_BLOCK_CIPHER | 
| 58 |  |  */ | 
| 59 |  |  | 
| 60 |  | static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 
| 61 |  |                           const unsigned char *in, size_t inl) | 
| 62 | 0 | { | 
| 63 | 0 |     BLOCK_CIPHER_ecb_loop() | 
| 64 | 0 |         DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i), | 
| 65 | 0 |                         EVP_CIPHER_CTX_get_cipher_data(ctx), | 
| 66 | 0 |                         EVP_CIPHER_CTX_is_encrypting(ctx)); | 
| 67 | 0 |     return 1; | 
| 68 | 0 | } | 
| 69 |  |  | 
| 70 |  | static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 
| 71 |  |                           const unsigned char *in, size_t inl) | 
| 72 | 0 | { | 
| 73 | 0 |     while (inl >= EVP_MAXCHUNK) { | 
| 74 | 0 |         int num = EVP_CIPHER_CTX_get_num(ctx); | 
| 75 | 0 |         DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, | 
| 76 | 0 |                           EVP_CIPHER_CTX_get_cipher_data(ctx), | 
| 77 | 0 |                           (DES_cblock *)ctx->iv, &num); | 
| 78 | 0 |         EVP_CIPHER_CTX_set_num(ctx, num); | 
| 79 | 0 |         inl -= EVP_MAXCHUNK; | 
| 80 | 0 |         in += EVP_MAXCHUNK; | 
| 81 | 0 |         out += EVP_MAXCHUNK; | 
| 82 | 0 |     } | 
| 83 | 0 |     if (inl) { | 
| 84 | 0 |         int num = EVP_CIPHER_CTX_get_num(ctx); | 
| 85 | 0 |         DES_ofb64_encrypt(in, out, (long)inl, | 
| 86 | 0 |                           EVP_CIPHER_CTX_get_cipher_data(ctx), | 
| 87 | 0 |                           (DES_cblock *)ctx->iv, &num); | 
| 88 | 0 |         EVP_CIPHER_CTX_set_num(ctx, num); | 
| 89 | 0 |     } | 
| 90 | 0 |     return 1; | 
| 91 | 0 | } | 
| 92 |  |  | 
| 93 |  | static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 
| 94 |  |                           const unsigned char *in, size_t inl) | 
| 95 | 0 | { | 
| 96 | 0 |     EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_get_cipher_data(ctx); | 
| 97 |  | 
 | 
| 98 | 0 |     if (dat->stream.cbc != NULL) { | 
| 99 | 0 |         (*dat->stream.cbc) (in, out, inl, &dat->ks.ks, ctx->iv); | 
| 100 | 0 |         return 1; | 
| 101 | 0 |     } | 
| 102 | 0 |     while (inl >= EVP_MAXCHUNK) { | 
| 103 | 0 |         DES_ncbc_encrypt(in, out, (long)EVP_MAXCHUNK, | 
| 104 | 0 |                          EVP_CIPHER_CTX_get_cipher_data(ctx), | 
| 105 | 0 |                          (DES_cblock *)ctx->iv, | 
| 106 | 0 |                          EVP_CIPHER_CTX_is_encrypting(ctx)); | 
| 107 | 0 |         inl -= EVP_MAXCHUNK; | 
| 108 | 0 |         in += EVP_MAXCHUNK; | 
| 109 | 0 |         out += EVP_MAXCHUNK; | 
| 110 | 0 |     } | 
| 111 | 0 |     if (inl) | 
| 112 | 0 |         DES_ncbc_encrypt(in, out, (long)inl, | 
| 113 | 0 |                          EVP_CIPHER_CTX_get_cipher_data(ctx), | 
| 114 | 0 |                          (DES_cblock *)ctx->iv, | 
| 115 | 0 |                          EVP_CIPHER_CTX_is_encrypting(ctx)); | 
| 116 | 0 |     return 1; | 
| 117 | 0 | } | 
| 118 |  |  | 
| 119 |  | static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 
| 120 |  |                             const unsigned char *in, size_t inl) | 
| 121 | 0 | { | 
| 122 | 0 |     while (inl >= EVP_MAXCHUNK) { | 
| 123 | 0 |         int num = EVP_CIPHER_CTX_get_num(ctx); | 
| 124 | 0 |         DES_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, | 
| 125 | 0 |                           EVP_CIPHER_CTX_get_cipher_data(ctx), | 
| 126 | 0 |                           (DES_cblock *)ctx->iv, &num, | 
| 127 | 0 |                           EVP_CIPHER_CTX_is_encrypting(ctx)); | 
| 128 | 0 |         EVP_CIPHER_CTX_set_num(ctx, num); | 
| 129 | 0 |         inl -= EVP_MAXCHUNK; | 
| 130 | 0 |         in += EVP_MAXCHUNK; | 
| 131 | 0 |         out += EVP_MAXCHUNK; | 
| 132 | 0 |     } | 
| 133 | 0 |     if (inl) { | 
| 134 | 0 |         int num = EVP_CIPHER_CTX_get_num(ctx); | 
| 135 | 0 |         DES_cfb64_encrypt(in, out, (long)inl, | 
| 136 | 0 |                           EVP_CIPHER_CTX_get_cipher_data(ctx), | 
| 137 | 0 |                           (DES_cblock *)ctx->iv, &num, | 
| 138 | 0 |                           EVP_CIPHER_CTX_is_encrypting(ctx)); | 
| 139 | 0 |         EVP_CIPHER_CTX_set_num(ctx, num); | 
| 140 | 0 |     } | 
| 141 | 0 |     return 1; | 
| 142 | 0 | } | 
| 143 |  |  | 
| 144 |  | /* | 
| 145 |  |  * Although we have a CFB-r implementation for DES, it doesn't pack the right | 
| 146 |  |  * way, so wrap it here | 
| 147 |  |  */ | 
| 148 |  | static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 
| 149 |  |                            const unsigned char *in, size_t inl) | 
| 150 | 0 | { | 
| 151 | 0 |     size_t n, chunk = EVP_MAXCHUNK / 8; | 
| 152 | 0 |     unsigned char c[1], d[1]; | 
| 153 |  | 
 | 
| 154 | 0 |     if (inl < chunk) | 
| 155 | 0 |         chunk = inl; | 
| 156 |  | 
 | 
| 157 | 0 |     while (inl && inl >= chunk) { | 
| 158 | 0 |         for (n = 0; n < chunk * 8; ++n) { | 
| 159 | 0 |             c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0; | 
| 160 | 0 |             DES_cfb_encrypt(c, d, 1, 1, EVP_CIPHER_CTX_get_cipher_data(ctx), | 
| 161 | 0 |                             (DES_cblock *)ctx->iv, | 
| 162 | 0 |                             EVP_CIPHER_CTX_is_encrypting(ctx)); | 
| 163 | 0 |             out[n / 8] = | 
| 164 | 0 |                 (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8))) | | 
| 165 | 0 |                 ((d[0] & 0x80) >> (unsigned int)(n % 8)); | 
| 166 | 0 |         } | 
| 167 | 0 |         inl -= chunk; | 
| 168 | 0 |         in += chunk; | 
| 169 | 0 |         out += chunk; | 
| 170 | 0 |         if (inl < chunk) | 
| 171 | 0 |             chunk = inl; | 
| 172 | 0 |     } | 
| 173 |  | 
 | 
| 174 | 0 |     return 1; | 
| 175 | 0 | } | 
| 176 |  |  | 
| 177 |  | static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 
| 178 |  |                            const unsigned char *in, size_t inl) | 
| 179 | 0 | { | 
| 180 | 0 |     while (inl >= EVP_MAXCHUNK) { | 
| 181 | 0 |         DES_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK, | 
| 182 | 0 |                         EVP_CIPHER_CTX_get_cipher_data(ctx), | 
| 183 | 0 |                         (DES_cblock *)ctx->iv, | 
| 184 | 0 |                         EVP_CIPHER_CTX_is_encrypting(ctx)); | 
| 185 | 0 |         inl -= EVP_MAXCHUNK; | 
| 186 | 0 |         in += EVP_MAXCHUNK; | 
| 187 | 0 |         out += EVP_MAXCHUNK; | 
| 188 | 0 |     } | 
| 189 | 0 |     if (inl) | 
| 190 | 0 |         DES_cfb_encrypt(in, out, 8, (long)inl, | 
| 191 | 0 |                         EVP_CIPHER_CTX_get_cipher_data(ctx), | 
| 192 | 0 |                         (DES_cblock *)ctx->iv, | 
| 193 | 0 |                         EVP_CIPHER_CTX_is_encrypting(ctx)); | 
| 194 | 0 |     return 1; | 
| 195 | 0 | } | 
| 196 |  |  | 
| 197 |  | BLOCK_CIPHER_defs(des, EVP_DES_KEY, NID_des, 8, 8, 8, 64, | 
| 198 |  |                   EVP_CIPH_RAND_KEY, des_init_key, NULL, | 
| 199 |  |                   EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, des_ctrl) | 
| 200 |  |  | 
| 201 |  |     BLOCK_CIPHER_def_cfb(des, EVP_DES_KEY, NID_des, 8, 8, 1, | 
| 202 |  |                      EVP_CIPH_RAND_KEY, des_init_key, NULL, | 
| 203 |  |                      EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, des_ctrl) | 
| 204 |  |  | 
| 205 |  |     BLOCK_CIPHER_def_cfb(des, EVP_DES_KEY, NID_des, 8, 8, 8, | 
| 206 |  |                      EVP_CIPH_RAND_KEY, des_init_key, NULL, | 
| 207 |  |                      EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, des_ctrl) | 
| 208 |  |  | 
| 209 |  | static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 
| 210 |  |                         const unsigned char *iv, int enc) | 
| 211 | 0 | { | 
| 212 | 0 |     DES_cblock *deskey = (DES_cblock *)key; | 
| 213 | 0 |     EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_get_cipher_data(ctx); | 
| 214 |  | 
 | 
| 215 | 0 |     dat->stream.cbc = NULL; | 
| 216 |  | # if defined(SPARC_DES_CAPABLE) | 
| 217 |  |     if (SPARC_DES_CAPABLE) { | 
| 218 |  |         int mode = EVP_CIPHER_CTX_get_mode(ctx); | 
| 219 |  |  | 
| 220 |  |         if (mode == EVP_CIPH_CBC_MODE) { | 
| 221 |  |             des_t4_key_expand(key, &dat->ks.ks); | 
| 222 |  |             dat->stream.cbc = enc ? des_t4_cbc_encrypt : des_t4_cbc_decrypt; | 
| 223 |  |             return 1; | 
| 224 |  |         } | 
| 225 |  |     } | 
| 226 |  | # endif | 
| 227 | 0 |     DES_set_key_unchecked(deskey, EVP_CIPHER_CTX_get_cipher_data(ctx)); | 
| 228 | 0 |     return 1; | 
| 229 | 0 | } | 
| 230 |  |  | 
| 231 |  | static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | 
| 232 | 0 | { | 
| 233 |  | 
 | 
| 234 | 0 |     switch (type) { | 
| 235 | 0 |     case EVP_CTRL_RAND_KEY: | 
| 236 | 0 |         if (RAND_priv_bytes(ptr, 8) <= 0) | 
| 237 | 0 |             return 0; | 
| 238 | 0 |         DES_set_odd_parity((DES_cblock *)ptr); | 
| 239 | 0 |         return 1; | 
| 240 |  |  | 
| 241 | 0 |     default: | 
| 242 | 0 |         return -1; | 
| 243 | 0 |     } | 
| 244 | 0 | } | 
| 245 |  |  | 
| 246 |  | #endif |