Coverage Report

Created: 2026-06-15 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/cipher/e_des.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
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 <stddef.h>
16
17
#include <openssl/cipher.h>
18
#include <openssl/des.h>
19
#include <openssl/nid.h>
20
21
#include "../des/internal.h"
22
#include "../fipsmodule/cipher/internal.h"
23
#include "internal.h"
24
25
26
using namespace bssl;
27
28
typedef struct {
29
  union {
30
    double align;
31
    DES_key_schedule ks;
32
  } ks;
33
} EVP_DES_KEY;
34
35
static int des_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
36
0
                        const uint8_t *iv, int enc) {
37
0
  EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data;
38
0
  DES_set_key_ex(key, &dat->ks.ks);
39
0
  return 1;
40
0
}
41
42
static int des_cbc_cipher_update(EVP_CIPHER_CTX *ctx, uint8_t *out,
43
0
                                 const uint8_t *in, size_t len) {
44
0
  EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data;
45
0
  DES_ncbc_encrypt_ex(in, out, len, &dat->ks.ks, ctx->iv, ctx->encrypt);
46
0
  return 1;
47
0
}
48
49
static const EVP_CIPHER evp_des_cbc = {
50
    /*nid=*/NID_des_cbc,
51
    /*block_size=*/8,
52
    /*key_len=*/8,
53
    /*iv_len=*/8,
54
    /*ctx_size=*/sizeof(EVP_DES_KEY),
55
    /*flags=*/EVP_CIPH_CBC_MODE,
56
    /*init=*/des_init_key,
57
    /*cipher_update=*/des_cbc_cipher_update,
58
    /*cipher_final=*/nullptr,
59
    /*update_aad=*/nullptr,
60
    /*cleanup=*/nullptr,
61
    /*ctrl=*/nullptr,
62
};
63
64
0
const EVP_CIPHER *EVP_des_cbc() { return &evp_des_cbc; }
65
66
static int des_ecb_cipher_update(EVP_CIPHER_CTX *ctx, uint8_t *out,
67
0
                                 const uint8_t *in, size_t len) {
68
0
  if (len < ctx->cipher->block_size) {
69
0
    return 1;
70
0
  }
71
0
  len -= ctx->cipher->block_size;
72
73
0
  EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data;
74
0
  for (size_t i = 0; i <= len; i += ctx->cipher->block_size) {
75
0
    DES_ecb_encrypt_ex(in + i, out + i, &dat->ks.ks, ctx->encrypt);
76
0
  }
77
0
  return 1;
78
0
}
79
80
static const EVP_CIPHER evp_des_ecb = {
81
    /*nid=*/NID_des_ecb,
82
    /*block_size=*/8,
83
    /*key_len=*/8,
84
    /*iv_len=*/0,
85
    /*ctx_size=*/sizeof(EVP_DES_KEY),
86
    /*flags=*/EVP_CIPH_ECB_MODE,
87
    /*init=*/des_init_key,
88
    /*cipher_update=*/des_ecb_cipher_update,
89
    /*cipher_final=*/nullptr,
90
    /*update_aad=*/nullptr,
91
    /*cleanup=*/nullptr,
92
    /*ctrl=*/nullptr,
93
};
94
95
0
const EVP_CIPHER *EVP_des_ecb() { return &evp_des_ecb; }
96
97
typedef struct {
98
  union {
99
    double align;
100
    DES_key_schedule ks[3];
101
  } ks;
102
} DES_EDE_KEY;
103
104
static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
105
3.77k
                             const uint8_t *iv, int enc) {
106
3.77k
  DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data;
107
3.77k
  DES_set_key_ex(key, &dat->ks.ks[0]);
108
3.77k
  DES_set_key_ex(key + 8, &dat->ks.ks[1]);
109
3.77k
  DES_set_key_ex(key + 16, &dat->ks.ks[2]);
110
3.77k
  return 1;
111
3.77k
}
112
113
static int des_ede3_cbc_cipher_update(EVP_CIPHER_CTX *ctx, uint8_t *out,
114
2.36k
                                      const uint8_t *in, size_t len) {
115
2.36k
  DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data;
116
2.36k
  DES_ede3_cbc_encrypt_ex(in, out, len, &dat->ks.ks[0], &dat->ks.ks[1],
117
2.36k
                          &dat->ks.ks[2], ctx->iv, ctx->encrypt);
118
2.36k
  return 1;
119
2.36k
}
120
121
static const EVP_CIPHER evp_des_ede3_cbc = {
122
    /*nid=*/NID_des_ede3_cbc,
123
    /*block_size=*/8,
124
    /*key_len=*/24,
125
    /*iv_len=*/8,
126
    /*ctx_size=*/sizeof(DES_EDE_KEY),
127
    /*flags=*/EVP_CIPH_CBC_MODE,
128
    /*init=*/des_ede3_init_key,
129
    /*cipher_update=*/des_ede3_cbc_cipher_update,
130
    /*cipher_final=*/nullptr,
131
    /*update_aad=*/nullptr,
132
    /*cleanup=*/nullptr,
133
    /*ctrl=*/nullptr,
134
};
135
136
3.77k
const EVP_CIPHER *EVP_des_ede3_cbc() { return &evp_des_ede3_cbc; }
137
138
static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
139
0
                            const uint8_t *iv, int enc) {
140
0
  DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data;
141
  // 2-DES is 3-DES with the first key used twice.
142
0
  DES_set_key_ex(key, &dat->ks.ks[0]);
143
0
  DES_set_key_ex(key + 8, &dat->ks.ks[1]);
144
0
  DES_set_key_ex(key, &dat->ks.ks[2]);
145
0
  return 1;
146
0
}
147
148
static const EVP_CIPHER evp_des_ede_cbc = {
149
    /*nid=*/NID_des_ede_cbc,
150
    /*block_size=*/8,
151
    /*key_len=*/16,
152
    /*iv_len=*/8,
153
    /*ctx_size=*/sizeof(DES_EDE_KEY),
154
    /*flags=*/EVP_CIPH_CBC_MODE,
155
    /*init=*/des_ede_init_key,
156
    /*cipher_update=*/des_ede3_cbc_cipher_update,
157
    /*cipher_final=*/nullptr,
158
    /*update_aad=*/nullptr,
159
    /*cleanup=*/nullptr,
160
    /*ctrl=*/nullptr,
161
};
162
163
0
const EVP_CIPHER *EVP_des_ede_cbc() { return &evp_des_ede_cbc; }
164
165
static int des_ede_ecb_cipher_update(EVP_CIPHER_CTX *ctx, uint8_t *out,
166
0
                                     const uint8_t *in, size_t len) {
167
0
  if (len < ctx->cipher->block_size) {
168
0
    return 1;
169
0
  }
170
0
  len -= ctx->cipher->block_size;
171
172
0
  DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data;
173
0
  for (size_t i = 0; i <= len; i += ctx->cipher->block_size) {
174
0
    DES_ecb3_encrypt_ex(in + i, out + i, &dat->ks.ks[0], &dat->ks.ks[1],
175
0
                        &dat->ks.ks[2], ctx->encrypt);
176
0
  }
177
0
  return 1;
178
0
}
179
180
static const EVP_CIPHER evp_des_ede = {
181
    /*nid=*/NID_des_ede_ecb,
182
    /*block_size=*/8,
183
    /*key_len=*/16,
184
    /*iv_len=*/0,
185
    /*ctx_size=*/sizeof(DES_EDE_KEY),
186
    /*flags=*/EVP_CIPH_ECB_MODE,
187
    /*init=*/des_ede_init_key,
188
    /*cipher_update=*/des_ede_ecb_cipher_update,
189
    /*cipher_final=*/nullptr,
190
    /*update_aad=*/nullptr,
191
    /*cleanup=*/nullptr,
192
    /*ctrl=*/nullptr,
193
};
194
195
0
const EVP_CIPHER *EVP_des_ede() { return &evp_des_ede; }
196
197
static const EVP_CIPHER evp_des_ede3 = {
198
    /*nid=*/NID_des_ede3_ecb,
199
    /*block_size=*/8,
200
    /*key_len=*/24,
201
    /*iv_len=*/0,
202
    /*ctx_size=*/sizeof(DES_EDE_KEY),
203
    /*flags=*/EVP_CIPH_ECB_MODE,
204
    /*init=*/des_ede3_init_key,
205
    /*cipher_update=*/des_ede_ecb_cipher_update,
206
    /*cipher_final=*/nullptr,
207
    /*update_aad=*/nullptr,
208
    /*cleanup=*/nullptr,
209
    /*ctrl=*/nullptr,
210
};
211
212
0
const EVP_CIPHER *EVP_des_ede3() { return &evp_des_ede3; }
213
214
0
const EVP_CIPHER *EVP_des_ede3_ecb() { return EVP_des_ede3(); }