Skip to content

Commit cf506f1

Browse files
davidbenBoringssl LUCI CQ
authored and
Boringssl LUCI CQ
committed
Make EVP_CIPHER opaque.
If we're to have any hope of fixing EVP_CIPHER_CTX's calling convention, we need to be able to change the shape of its method table. Looking back, it looks like we exported this in https://boringssl-review.googlesource.com/4330, for OpenSSH. I don't remember exactly what OpenSSH was doing, but I see in this commit, they removed a bunch of custom EVP_CIPHERs which would definitely have required an exported EVP_CIPHER struct: openssh/openssh-portable@cdccebd That's been gone for a while now, so hopefully we can hide it again. (If a project needs a cipher not implemented by OpenSSL, it's not strictly necessarily to make a custom EVP_CIPHER. It might be convenient to reuse the abstraction, but you can always just call your own APIs directly.) Update-Note: EVP_CIPHER is now opaque. Use accessors instead. Bug: 494 Change-Id: I9344690c3cfe7d19d6ca12fb66484ced57dbe869 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/52725 Reviewed-by: Bob Beck <[email protected]> Commit-Queue: Bob Beck <[email protected]>
1 parent 2d4f1b8 commit cf506f1

File tree

11 files changed

+53
-44
lines changed

11 files changed

+53
-44
lines changed

crypto/cipher_extra/derive_key.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
6969
unsigned count, uint8_t *key, uint8_t *iv) {
7070
EVP_MD_CTX c;
7171
uint8_t md_buf[EVP_MAX_MD_SIZE];
72-
unsigned niv, nkey, addmd = 0;
72+
unsigned addmd = 0;
7373
unsigned mds = 0, i;
7474
int rv = 0;
7575

76-
nkey = type->key_len;
77-
niv = type->iv_len;
76+
unsigned nkey = EVP_CIPHER_key_length(type);
77+
unsigned niv = EVP_CIPHER_iv_length(type);
7878

7979
assert(nkey <= EVP_MAX_KEY_LENGTH);
8080
assert(niv <= EVP_MAX_IV_LENGTH);
@@ -143,7 +143,7 @@ int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
143143
break;
144144
}
145145
}
146-
rv = type->key_len;
146+
rv = EVP_CIPHER_key_length(type);
147147

148148
err:
149149
EVP_MD_CTX_cleanup(&c);

crypto/cipher_extra/e_des.c

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <openssl/des.h>
5959
#include <openssl/nid.h>
6060

61+
#include "../fipsmodule/cipher/internal.h"
6162
#include "internal.h"
6263

6364

crypto/cipher_extra/e_null.c

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
#include <openssl/nid.h>
6262

63+
#include "../fipsmodule/cipher/internal.h"
6364
#include "../internal.h"
6465

6566

crypto/cipher_extra/e_rc2.c

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <openssl/cipher.h>
5858
#include <openssl/nid.h>
5959

60+
#include "../fipsmodule/cipher/internal.h"
6061
#include "../internal.h"
6162

6263

crypto/cipher_extra/e_rc4.c

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
#include <openssl/nid.h>
6262
#include <openssl/rc4.h>
6363

64+
#include "../fipsmodule/cipher/internal.h"
65+
6466

6567
static int rc4_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
6668
const uint8_t *iv, int enc) {

crypto/fipsmodule/cipher/internal.h

+39
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,45 @@ struct evp_aead_st {
112112
size_t extra_in_len);
113113
};
114114

115+
struct evp_cipher_st {
116+
// type contains a NID identifying the cipher. (e.g. NID_aes_128_gcm.)
117+
int nid;
118+
119+
// block_size contains the block size, in bytes, of the cipher, or 1 for a
120+
// stream cipher.
121+
unsigned block_size;
122+
123+
// key_len contains the key size, in bytes, for the cipher. If the cipher
124+
// takes a variable key size then this contains the default size.
125+
unsigned key_len;
126+
127+
// iv_len contains the IV size, in bytes, or zero if inapplicable.
128+
unsigned iv_len;
129+
130+
// ctx_size contains the size, in bytes, of the per-key context for this
131+
// cipher.
132+
unsigned ctx_size;
133+
134+
// flags contains the OR of a number of flags. See |EVP_CIPH_*|.
135+
uint32_t flags;
136+
137+
// app_data is a pointer to opaque, user data.
138+
void *app_data;
139+
140+
int (*init)(EVP_CIPHER_CTX *ctx, const uint8_t *key, const uint8_t *iv,
141+
int enc);
142+
143+
int (*cipher)(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
144+
size_t inl);
145+
146+
// cleanup, if non-NULL, releases memory associated with the context. It is
147+
// called if |EVP_CTRL_INIT| succeeds. Note that |init| may not have been
148+
// called at this point.
149+
void (*cleanup)(EVP_CIPHER_CTX *);
150+
151+
int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
152+
};
153+
115154
// aes_ctr_set_key initialises |*aes_key| using |key_bytes| bytes from |key|,
116155
// where |key_bytes| must either be 16, 24 or 32. If not NULL, |*out_block| is
117156
// set to a function that encrypts single blocks. If not NULL, |*gcm_key| is

decrepit/blowfish/blowfish.c

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include <assert.h>
6262
#include <string.h>
6363

64+
#include "../../crypto/fipsmodule/cipher/internal.h"
6465
#include "../../crypto/internal.h"
6566
#include "../macros.h"
6667

decrepit/cast/cast.c

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ OPENSSL_MSVC_PRAGMA(warning(push, 3))
6464
OPENSSL_MSVC_PRAGMA(warning(pop))
6565
#endif
6666

67+
#include "../../crypto/fipsmodule/cipher/internal.h"
6768
#include "../../crypto/internal.h"
6869
#include "internal.h"
6970
#include "../macros.h"

decrepit/cfb/cfb.c

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <openssl/aes.h>
2020
#include <openssl/obj.h>
2121

22+
#include "../../crypto/fipsmodule/cipher/internal.h"
2223
#include "../../crypto/internal.h"
2324

2425
typedef struct {

decrepit/xts/xts.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
#include <openssl/aes.h>
5454
#include <openssl/cipher.h>
5555

56-
#include "../crypto/fipsmodule/modes/internal.h"
56+
#include "../../crypto/fipsmodule/cipher/internal.h"
57+
#include "../../crypto/fipsmodule/modes/internal.h"
5758

5859

5960
typedef struct xts128_context {

include/openssl/cipher.h

-39
Original file line numberDiff line numberDiff line change
@@ -582,45 +582,6 @@ typedef struct evp_cipher_info_st {
582582
unsigned char iv[EVP_MAX_IV_LENGTH];
583583
} EVP_CIPHER_INFO;
584584

585-
struct evp_cipher_st {
586-
// type contains a NID identifing the cipher. (e.g. NID_aes_128_gcm.)
587-
int nid;
588-
589-
// block_size contains the block size, in bytes, of the cipher, or 1 for a
590-
// stream cipher.
591-
unsigned block_size;
592-
593-
// key_len contains the key size, in bytes, for the cipher. If the cipher
594-
// takes a variable key size then this contains the default size.
595-
unsigned key_len;
596-
597-
// iv_len contains the IV size, in bytes, or zero if inapplicable.
598-
unsigned iv_len;
599-
600-
// ctx_size contains the size, in bytes, of the per-key context for this
601-
// cipher.
602-
unsigned ctx_size;
603-
604-
// flags contains the OR of a number of flags. See |EVP_CIPH_*|.
605-
uint32_t flags;
606-
607-
// app_data is a pointer to opaque, user data.
608-
void *app_data;
609-
610-
int (*init)(EVP_CIPHER_CTX *ctx, const uint8_t *key, const uint8_t *iv,
611-
int enc);
612-
613-
int (*cipher)(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
614-
size_t inl);
615-
616-
// cleanup, if non-NULL, releases memory associated with the context. It is
617-
// called if |EVP_CTRL_INIT| succeeds. Note that |init| may not have been
618-
// called at this point.
619-
void (*cleanup)(EVP_CIPHER_CTX *);
620-
621-
int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
622-
};
623-
624585

625586
#if defined(__cplusplus)
626587
} // extern C

0 commit comments

Comments
 (0)