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