Adapt builtin cipher implementations to opaque EVP_CIPHER
They all stop including evp_locl.h, so we also take care of their adaptation to opaque EVP_CIPHER_CTX, as was promised in an earlier commit. Reviewed-by: Rich Salz <rsalz@openssl.org>
This commit is contained in:
parent
135727abe0
commit
6435f0f6c6
File diff suppressed because it is too large
Load Diff
@ -60,6 +60,7 @@
|
||||
# include <openssl/sha.h>
|
||||
# include <openssl/rand.h>
|
||||
# include "modes_lcl.h"
|
||||
# include "internal/evp_int.h"
|
||||
|
||||
# ifndef EVP_CIPH_FLAG_AEAD_CIPHER
|
||||
# define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
|
||||
|
@ -60,6 +60,7 @@
|
||||
# include <openssl/sha.h>
|
||||
# include <openssl/rand.h>
|
||||
# include "modes_lcl.h"
|
||||
# include "internal/evp_int.h"
|
||||
|
||||
# ifndef EVP_CIPH_FLAG_AEAD_CIPHER
|
||||
# define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
|
||||
|
@ -60,7 +60,7 @@
|
||||
#include "internal/cryptlib.h"
|
||||
#ifndef OPENSSL_NO_BF
|
||||
# include <openssl/evp.h>
|
||||
# include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
# include <openssl/objects.h>
|
||||
# include <openssl/blowfish.h>
|
||||
|
||||
|
@ -60,7 +60,7 @@
|
||||
# include <string.h>
|
||||
# include <assert.h>
|
||||
# include <openssl/camellia.h>
|
||||
# include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
# include "modes_lcl.h"
|
||||
|
||||
static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
@ -256,15 +256,15 @@ static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
int ret, mode;
|
||||
EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
|
||||
EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
|
||||
|
||||
ret = Camellia_set_key(key, ctx->key_len * 8, &dat->ks);
|
||||
ret = Camellia_set_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, &dat->ks);
|
||||
if (ret < 0) {
|
||||
EVPerr(EVP_F_CAMELLIA_INIT_KEY, EVP_R_CAMELLIA_KEY_SETUP_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
mode = ctx->cipher->flags & EVP_CIPH_MODE;
|
||||
mode = EVP_CIPHER_CTX_mode(ctx);
|
||||
if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
|
||||
&& !enc) {
|
||||
dat->block = (block128_f) Camellia_decrypt;
|
||||
@ -282,14 +282,18 @@ static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
static int camellia_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
|
||||
EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
|
||||
|
||||
if (dat->stream.cbc)
|
||||
(*dat->stream.cbc) (in, out, len, &dat->ks, ctx->iv, ctx->encrypt);
|
||||
else if (ctx->encrypt)
|
||||
CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
|
||||
(*dat->stream.cbc) (in, out, len, &dat->ks,
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
EVP_CIPHER_CTX_encrypting(ctx));
|
||||
else if (EVP_CIPHER_CTX_encrypting(ctx))
|
||||
CRYPTO_cbc128_encrypt(in, out, len, &dat->ks,
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx), dat->block);
|
||||
else
|
||||
CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
|
||||
CRYPTO_cbc128_decrypt(in, out, len, &dat->ks,
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx), dat->block);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -297,9 +301,9 @@ static int camellia_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
static int camellia_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
size_t bl = ctx->cipher->block_size;
|
||||
size_t bl = EVP_CIPHER_CTX_block_size(ctx);
|
||||
size_t i;
|
||||
EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
|
||||
EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
|
||||
|
||||
if (len < bl)
|
||||
return 1;
|
||||
@ -313,52 +317,65 @@ static int camellia_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
static int camellia_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
|
||||
EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
|
||||
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
CRYPTO_ofb128_encrypt(in, out, len, &dat->ks,
|
||||
ctx->iv, &ctx->num, dat->block);
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx), &num, dat->block);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int camellia_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
|
||||
EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
|
||||
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
CRYPTO_cfb128_encrypt(in, out, len, &dat->ks,
|
||||
ctx->iv, &ctx->num, ctx->encrypt, dat->block);
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx), &num, EVP_CIPHER_CTX_encrypting(ctx), dat->block);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int camellia_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
|
||||
EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
|
||||
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks,
|
||||
ctx->iv, &ctx->num, ctx->encrypt, dat->block);
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx), &num, EVP_CIPHER_CTX_encrypting(ctx), dat->block);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int camellia_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
|
||||
EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
|
||||
|
||||
if (ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) {
|
||||
if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) {
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks,
|
||||
ctx->iv, &ctx->num, ctx->encrypt, dat->block);
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx), &num, EVP_CIPHER_CTX_encrypting(ctx), dat->block);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (len >= MAXBITCHUNK) {
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, &dat->ks,
|
||||
ctx->iv, &ctx->num, ctx->encrypt, dat->block);
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx), &num, EVP_CIPHER_CTX_encrypting(ctx), dat->block);
|
||||
len -= MAXBITCHUNK;
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
}
|
||||
if (len)
|
||||
if (len) {
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
CRYPTO_cfb128_1_encrypt(in, out, len * 8, &dat->ks,
|
||||
ctx->iv, &ctx->num, ctx->encrypt, dat->block);
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx), &num, EVP_CIPHER_CTX_encrypting(ctx), dat->block);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -366,16 +383,20 @@ static int camellia_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
static int camellia_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
unsigned int num = ctx->num;
|
||||
EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
|
||||
unsigned int num = EVP_CIPHER_CTX_num(ctx);
|
||||
EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
|
||||
|
||||
if (dat->stream.ctr)
|
||||
CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks,
|
||||
ctx->iv, ctx->buf, &num, dat->stream.ctr);
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
EVP_CIPHER_CTX_buf_noconst(ctx), &num,
|
||||
dat->stream.ctr);
|
||||
else
|
||||
CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
|
||||
ctx->iv, ctx->buf, &num, dat->block);
|
||||
ctx->num = (size_t)num;
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
EVP_CIPHER_CTX_buf_noconst(ctx), &num,
|
||||
dat->block);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -62,7 +62,7 @@
|
||||
#ifndef OPENSSL_NO_CAST
|
||||
# include <openssl/evp.h>
|
||||
# include <openssl/objects.h>
|
||||
# include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
# include <openssl/cast.h>
|
||||
|
||||
static int cast_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
|
@ -61,7 +61,7 @@
|
||||
#ifndef OPENSSL_NO_DES
|
||||
# include <openssl/evp.h>
|
||||
# include <openssl/objects.h>
|
||||
# include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
# include <openssl/des.h>
|
||||
# include <openssl/rand.h>
|
||||
|
||||
@ -105,7 +105,8 @@ static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
{
|
||||
BLOCK_CIPHER_ecb_loop()
|
||||
DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i),
|
||||
ctx->cipher_data, ctx->encrypt);
|
||||
EVP_CIPHER_CTX_cipher_data(ctx),
|
||||
EVP_CIPHER_CTX_encrypting(ctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -113,37 +114,43 @@ static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
while (inl >= EVP_MAXCHUNK) {
|
||||
DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data,
|
||||
(DES_cblock *)ctx->iv, &ctx->num);
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK,
|
||||
EVP_CIPHER_CTX_cipher_data(ctx),
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), &num);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
inl -= EVP_MAXCHUNK;
|
||||
in += EVP_MAXCHUNK;
|
||||
out += EVP_MAXCHUNK;
|
||||
}
|
||||
if (inl)
|
||||
DES_ofb64_encrypt(in, out, (long)inl, ctx->cipher_data,
|
||||
(DES_cblock *)ctx->iv, &ctx->num);
|
||||
if (inl) {
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
DES_ofb64_encrypt(in, out, (long)inl, EVP_CIPHER_CTX_cipher_data(ctx),
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), &num);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
EVP_DES_KEY *dat = (EVP_DES_KEY *) ctx->cipher_data;
|
||||
EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_cipher_data(ctx);
|
||||
|
||||
if (dat->stream.cbc) {
|
||||
(*dat->stream.cbc) (in, out, inl, &dat->ks.ks, ctx->iv);
|
||||
(*dat->stream.cbc) (in, out, inl, &dat->ks.ks, EVP_CIPHER_CTX_iv_noconst(ctx));
|
||||
return 1;
|
||||
}
|
||||
while (inl >= EVP_MAXCHUNK) {
|
||||
DES_ncbc_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data,
|
||||
(DES_cblock *)ctx->iv, ctx->encrypt);
|
||||
DES_ncbc_encrypt(in, out, (long)EVP_MAXCHUNK, EVP_CIPHER_CTX_cipher_data(ctx),
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));
|
||||
inl -= EVP_MAXCHUNK;
|
||||
in += EVP_MAXCHUNK;
|
||||
out += EVP_MAXCHUNK;
|
||||
}
|
||||
if (inl)
|
||||
DES_ncbc_encrypt(in, out, (long)inl, ctx->cipher_data,
|
||||
(DES_cblock *)ctx->iv, ctx->encrypt);
|
||||
DES_ncbc_encrypt(in, out, (long)inl, EVP_CIPHER_CTX_cipher_data(ctx),
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -151,15 +158,23 @@ static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
while (inl >= EVP_MAXCHUNK) {
|
||||
DES_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data,
|
||||
(DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt);
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
DES_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK,
|
||||
EVP_CIPHER_CTX_cipher_data(ctx),
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), &num,
|
||||
EVP_CIPHER_CTX_encrypting(ctx));
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
inl -= EVP_MAXCHUNK;
|
||||
in += EVP_MAXCHUNK;
|
||||
out += EVP_MAXCHUNK;
|
||||
}
|
||||
if (inl)
|
||||
DES_cfb64_encrypt(in, out, (long)inl, ctx->cipher_data,
|
||||
(DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt);
|
||||
if (inl) {
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
DES_cfb64_encrypt(in, out, (long)inl, EVP_CIPHER_CTX_cipher_data(ctx),
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), &num,
|
||||
EVP_CIPHER_CTX_encrypting(ctx));
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -179,8 +194,8 @@ static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
while (inl && inl >= chunk) {
|
||||
for (n = 0; n < chunk * 8; ++n) {
|
||||
c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
|
||||
DES_cfb_encrypt(c, d, 1, 1, ctx->cipher_data,
|
||||
(DES_cblock *)ctx->iv, ctx->encrypt);
|
||||
DES_cfb_encrypt(c, d, 1, 1, EVP_CIPHER_CTX_cipher_data(ctx),
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));
|
||||
out[n / 8] =
|
||||
(out[n / 8] & ~(0x80 >> (unsigned int)(n % 8))) |
|
||||
((d[0] & 0x80) >> (unsigned int)(n % 8));
|
||||
@ -199,15 +214,15 @@ static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
while (inl >= EVP_MAXCHUNK) {
|
||||
DES_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK, ctx->cipher_data,
|
||||
(DES_cblock *)ctx->iv, ctx->encrypt);
|
||||
DES_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK, EVP_CIPHER_CTX_cipher_data(ctx),
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));
|
||||
inl -= EVP_MAXCHUNK;
|
||||
in += EVP_MAXCHUNK;
|
||||
out += EVP_MAXCHUNK;
|
||||
}
|
||||
if (inl)
|
||||
DES_cfb_encrypt(in, out, 8, (long)inl, ctx->cipher_data,
|
||||
(DES_cblock *)ctx->iv, ctx->encrypt);
|
||||
DES_cfb_encrypt(in, out, 8, (long)inl, EVP_CIPHER_CTX_cipher_data(ctx),
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -227,7 +242,7 @@ static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
DES_cblock *deskey = (DES_cblock *)key;
|
||||
EVP_DES_KEY *dat = (EVP_DES_KEY *) ctx->cipher_data;
|
||||
EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_cipher_data(ctx);
|
||||
|
||||
dat->stream.cbc = NULL;
|
||||
# if defined(SPARC_DES_CAPABLE)
|
||||
@ -241,7 +256,7 @@ static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
}
|
||||
}
|
||||
# endif
|
||||
DES_set_key_unchecked(deskey, ctx->cipher_data);
|
||||
DES_set_key_unchecked(deskey, EVP_CIPHER_CTX_cipher_data(ctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@
|
||||
#ifndef OPENSSL_NO_DES
|
||||
# include <openssl/evp.h>
|
||||
# include <openssl/objects.h>
|
||||
# include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
# include <openssl/des.h>
|
||||
# include <openssl/rand.h>
|
||||
|
||||
@ -102,7 +102,7 @@ static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
|
||||
static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
|
||||
|
||||
# define data(ctx) ((DES_EDE_KEY *)(ctx)->cipher_data)
|
||||
# define data(ctx) EVP_C_DATA(DES_EDE_KEY,ctx)
|
||||
|
||||
/*
|
||||
* Because of various casts and different args can't use
|
||||
@ -116,7 +116,7 @@ static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
DES_ecb3_encrypt((const_DES_cblock *)(in + i),
|
||||
(DES_cblock *)(out + i),
|
||||
&data(ctx)->ks1, &data(ctx)->ks2,
|
||||
&data(ctx)->ks3, ctx->encrypt);
|
||||
&data(ctx)->ks3, EVP_CIPHER_CTX_encrypting(ctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -124,20 +124,26 @@ static int des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
while (inl >= EVP_MAXCHUNK) {
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
DES_ede3_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK,
|
||||
&data(ctx)->ks1, &data(ctx)->ks2,
|
||||
&data(ctx)->ks3, (DES_cblock *)ctx->iv,
|
||||
&ctx->num);
|
||||
&data(ctx)->ks3,
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
&num);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
inl -= EVP_MAXCHUNK;
|
||||
in += EVP_MAXCHUNK;
|
||||
out += EVP_MAXCHUNK;
|
||||
}
|
||||
if (inl)
|
||||
if (inl) {
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
DES_ede3_ofb64_encrypt(in, out, (long)inl,
|
||||
&data(ctx)->ks1, &data(ctx)->ks2,
|
||||
&data(ctx)->ks3, (DES_cblock *)ctx->iv,
|
||||
&ctx->num);
|
||||
|
||||
&data(ctx)->ks3,
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
&num);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -147,14 +153,14 @@ static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
DES_EDE_KEY *dat = data(ctx);
|
||||
|
||||
if (dat->stream.cbc) {
|
||||
(*dat->stream.cbc) (in, out, inl, &dat->ks, ctx->iv);
|
||||
(*dat->stream.cbc) (in, out, inl, &dat->ks, EVP_CIPHER_CTX_iv_noconst(ctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (inl >= EVP_MAXCHUNK) {
|
||||
DES_ede3_cbc_encrypt(in, out, (long)EVP_MAXCHUNK,
|
||||
&dat->ks1, &dat->ks2, &dat->ks3,
|
||||
(DES_cblock *)ctx->iv, ctx->encrypt);
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));
|
||||
inl -= EVP_MAXCHUNK;
|
||||
in += EVP_MAXCHUNK;
|
||||
out += EVP_MAXCHUNK;
|
||||
@ -162,7 +168,7 @@ static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
if (inl)
|
||||
DES_ede3_cbc_encrypt(in, out, (long)inl,
|
||||
&dat->ks1, &dat->ks2, &dat->ks3,
|
||||
(DES_cblock *)ctx->iv, ctx->encrypt);
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -170,19 +176,26 @@ static int des_ede_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
while (inl >= EVP_MAXCHUNK) {
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
DES_ede3_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK,
|
||||
&data(ctx)->ks1, &data(ctx)->ks2,
|
||||
&data(ctx)->ks3, (DES_cblock *)ctx->iv,
|
||||
&ctx->num, ctx->encrypt);
|
||||
&data(ctx)->ks3,
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
&num, EVP_CIPHER_CTX_encrypting(ctx));
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
inl -= EVP_MAXCHUNK;
|
||||
in += EVP_MAXCHUNK;
|
||||
out += EVP_MAXCHUNK;
|
||||
}
|
||||
if (inl)
|
||||
if (inl) {
|
||||
int num = EVP_CIPHER_CTX_num(ctx);
|
||||
DES_ede3_cfb64_encrypt(in, out, (long)inl,
|
||||
&data(ctx)->ks1, &data(ctx)->ks2,
|
||||
&data(ctx)->ks3, (DES_cblock *)ctx->iv,
|
||||
&ctx->num, ctx->encrypt);
|
||||
&data(ctx)->ks3,
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
&num, EVP_CIPHER_CTX_encrypting(ctx));
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -200,8 +213,8 @@ static int des_ede3_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
|
||||
DES_ede3_cfb_encrypt(c, d, 1, 1,
|
||||
&data(ctx)->ks1, &data(ctx)->ks2,
|
||||
&data(ctx)->ks3, (DES_cblock *)ctx->iv,
|
||||
ctx->encrypt);
|
||||
&data(ctx)->ks3, (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
EVP_CIPHER_CTX_encrypting(ctx));
|
||||
out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8)))
|
||||
| ((d[0] & 0x80) >> (unsigned int)(n % 8));
|
||||
}
|
||||
@ -215,8 +228,8 @@ static int des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
while (inl >= EVP_MAXCHUNK) {
|
||||
DES_ede3_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK,
|
||||
&data(ctx)->ks1, &data(ctx)->ks2,
|
||||
&data(ctx)->ks3, (DES_cblock *)ctx->iv,
|
||||
ctx->encrypt);
|
||||
&data(ctx)->ks3, (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
EVP_CIPHER_CTX_encrypting(ctx));
|
||||
inl -= EVP_MAXCHUNK;
|
||||
in += EVP_MAXCHUNK;
|
||||
out += EVP_MAXCHUNK;
|
||||
@ -224,8 +237,8 @@ static int des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
if (inl)
|
||||
DES_ede3_cfb_encrypt(in, out, 8, (long)inl,
|
||||
&data(ctx)->ks1, &data(ctx)->ks2,
|
||||
&data(ctx)->ks3, (DES_cblock *)ctx->iv,
|
||||
ctx->encrypt);
|
||||
&data(ctx)->ks3, (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
EVP_CIPHER_CTX_encrypting(ctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -302,19 +315,19 @@ static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
|
||||
static int des3_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
|
||||
{
|
||||
|
||||
DES_cblock *deskey = ptr;
|
||||
|
||||
switch (type) {
|
||||
case EVP_CTRL_RAND_KEY:
|
||||
if (RAND_bytes(ptr, c->key_len) <= 0)
|
||||
if (RAND_bytes(ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0)
|
||||
return 0;
|
||||
DES_set_odd_parity(deskey);
|
||||
if (c->key_len >= 16)
|
||||
if (EVP_CIPHER_CTX_key_length(ctx) >= 16)
|
||||
DES_set_odd_parity(deskey + 1);
|
||||
if (c->key_len >= 24)
|
||||
if (EVP_CIPHER_CTX_key_length(ctx) >= 24)
|
||||
DES_set_odd_parity(deskey + 2);
|
||||
return 1;
|
||||
|
||||
@ -348,7 +361,7 @@ static int des_ede3_unwrap(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
return -1;
|
||||
if (!out)
|
||||
return inl - 16;
|
||||
memcpy(ctx->iv, wrap_iv, 8);
|
||||
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), wrap_iv, 8);
|
||||
/* Decrypt first block which will end up as icv */
|
||||
des_ede_cbc_cipher(ctx, icv, in, 8);
|
||||
/* Decrypt central blocks */
|
||||
@ -366,7 +379,7 @@ static int des_ede3_unwrap(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
/* Reverse order of everything */
|
||||
BUF_reverse(icv, NULL, 8);
|
||||
BUF_reverse(out, NULL, inl - 16);
|
||||
BUF_reverse(ctx->iv, iv, 8);
|
||||
BUF_reverse(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 8);
|
||||
/* Decrypt again using new IV */
|
||||
des_ede_cbc_cipher(ctx, out, out, inl - 16);
|
||||
des_ede_cbc_cipher(ctx, icv, icv, 8);
|
||||
@ -378,7 +391,7 @@ static int des_ede3_unwrap(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
OPENSSL_cleanse(icv, 8);
|
||||
OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
|
||||
OPENSSL_cleanse(iv, 8);
|
||||
OPENSSL_cleanse(ctx->iv, 8);
|
||||
OPENSSL_cleanse(EVP_CIPHER_CTX_iv_noconst(ctx), 8);
|
||||
if (rv == -1)
|
||||
OPENSSL_cleanse(out, inl - 16);
|
||||
|
||||
@ -398,13 +411,13 @@ static int des_ede3_wrap(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
memcpy(out + inl + 8, sha1tmp, 8);
|
||||
OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
|
||||
/* Generate random IV */
|
||||
if (RAND_bytes(ctx->iv, 8) <= 0)
|
||||
if (RAND_bytes(EVP_CIPHER_CTX_iv_noconst(ctx), 8) <= 0)
|
||||
return -1;
|
||||
memcpy(out, ctx->iv, 8);
|
||||
memcpy(out, EVP_CIPHER_CTX_iv_noconst(ctx), 8);
|
||||
/* Encrypt everything after IV in place */
|
||||
des_ede_cbc_cipher(ctx, out + 8, out + 8, inl + 8);
|
||||
BUF_reverse(out, NULL, inl + 16);
|
||||
memcpy(ctx->iv, wrap_iv, 8);
|
||||
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), wrap_iv, 8);
|
||||
des_ede_cbc_cipher(ctx, out, out, inl + 16);
|
||||
return inl + 16;
|
||||
}
|
||||
@ -419,7 +432,7 @@ static int des_ede3_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
*/
|
||||
if (inl >= EVP_MAXCHUNK || inl % 8)
|
||||
return -1;
|
||||
if (ctx->encrypt)
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx))
|
||||
return des_ede3_wrap(ctx, out, in, inl);
|
||||
else
|
||||
return des_ede3_unwrap(ctx, out, in, inl);
|
||||
|
@ -62,9 +62,15 @@
|
||||
#ifndef OPENSSL_NO_IDEA
|
||||
# include <openssl/evp.h>
|
||||
# include <openssl/objects.h>
|
||||
# include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
# include <openssl/idea.h>
|
||||
|
||||
/* Can't use IMPLEMENT_BLOCK_CIPHER because idea_ecb_encrypt is different */
|
||||
|
||||
typedef struct {
|
||||
IDEA_KEY_SCHEDULE ks;
|
||||
} EVP_IDEA_KEY;
|
||||
|
||||
static int idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc);
|
||||
|
||||
@ -77,16 +83,10 @@ static int idea_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
BLOCK_CIPHER_ecb_loop()
|
||||
idea_ecb_encrypt(in + i, out + i, ctx->cipher_data);
|
||||
idea_ecb_encrypt(in + i, out + i, &EVP_C_DATA(EVP_IDEA_KEY,ctx)->ks);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Can't use IMPLEMENT_BLOCK_CIPHER because idea_ecb_encrypt is different */
|
||||
|
||||
typedef struct {
|
||||
IDEA_KEY_SCHEDULE ks;
|
||||
} EVP_IDEA_KEY;
|
||||
|
||||
BLOCK_CIPHER_func_cbc(idea, idea, EVP_IDEA_KEY, ks)
|
||||
BLOCK_CIPHER_func_ofb(idea, idea, 64, EVP_IDEA_KEY, ks)
|
||||
BLOCK_CIPHER_func_cfb(idea, idea, 64, EVP_IDEA_KEY, ks)
|
||||
@ -105,12 +105,12 @@ static int idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
enc = 1;
|
||||
}
|
||||
if (enc)
|
||||
idea_set_encrypt_key(key, ctx->cipher_data);
|
||||
idea_set_encrypt_key(key, &EVP_C_DATA(EVP_IDEA_KEY,ctx)->ks);
|
||||
else {
|
||||
IDEA_KEY_SCHEDULE tmp;
|
||||
|
||||
idea_set_encrypt_key(key, &tmp);
|
||||
idea_set_decrypt_key(&tmp, ctx->cipher_data);
|
||||
idea_set_decrypt_key(&tmp, &EVP_C_DATA(EVP_IDEA_KEY,ctx)->ks);
|
||||
OPENSSL_cleanse((unsigned char *)&tmp, sizeof(IDEA_KEY_SCHEDULE));
|
||||
}
|
||||
return 1;
|
||||
|
@ -60,6 +60,7 @@
|
||||
#include "internal/cryptlib.h"
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "internal/evp_int.h"
|
||||
|
||||
static int null_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc);
|
||||
|
@ -63,7 +63,7 @@
|
||||
|
||||
# include <openssl/evp.h>
|
||||
# include <openssl/objects.h>
|
||||
# include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
# include <openssl/rc2.h>
|
||||
|
||||
static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
@ -79,7 +79,7 @@ typedef struct {
|
||||
RC2_KEY ks; /* key schedule */
|
||||
} EVP_RC2_KEY;
|
||||
|
||||
# define data(ctx) ((EVP_RC2_KEY *)(ctx)->cipher_data)
|
||||
# define data(ctx) EVP_C_DATA(EVP_RC2_KEY,ctx)
|
||||
|
||||
IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
|
||||
8,
|
||||
|
@ -65,6 +65,8 @@
|
||||
# include <openssl/objects.h>
|
||||
# include <openssl/rc4.h>
|
||||
|
||||
# include "internal/evp_int.h"
|
||||
|
||||
/* FIXME: surely this is available elsewhere? */
|
||||
# define EVP_RC4_KEY_SIZE 16
|
||||
|
||||
|
@ -59,6 +59,7 @@
|
||||
# include <openssl/objects.h>
|
||||
# include <openssl/rc4.h>
|
||||
# include <openssl/md5.h>
|
||||
# include "internal/evp_int.h"
|
||||
|
||||
# ifndef EVP_CIPH_FLAG_AEAD_CIPHER
|
||||
# define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
|
||||
|
@ -60,7 +60,7 @@
|
||||
# include <string.h>
|
||||
# include <assert.h>
|
||||
# include <openssl/seed.h>
|
||||
# include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
|
||||
static int seed_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc);
|
||||
@ -75,7 +75,7 @@ IMPLEMENT_BLOCK_CIPHER(seed, ks, SEED, EVP_SEED_KEY, NID_seed,
|
||||
static int seed_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
SEED_set_key(key, ctx->cipher_data);
|
||||
SEED_set_key(key, &EVP_C_DATA(EVP_SEED_KEY,ctx)->ks);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@
|
||||
|
||||
# include <openssl/evp.h>
|
||||
# include <openssl/objects.h>
|
||||
# include "evp_locl.h"
|
||||
# include "internal/evp_int.h"
|
||||
# include <openssl/des.h>
|
||||
|
||||
static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
@ -77,7 +77,7 @@ typedef struct {
|
||||
DES_cblock outw;
|
||||
} DESX_CBC_KEY;
|
||||
|
||||
# define data(ctx) ((DESX_CBC_KEY *)(ctx)->cipher_data)
|
||||
# define data(ctx) EVP_C_DATA(DESX_CBC_KEY,ctx)
|
||||
|
||||
static const EVP_CIPHER d_xcbc_cipher = {
|
||||
NID_desx_cbc,
|
||||
@ -115,16 +115,18 @@ static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
{
|
||||
while (inl >= EVP_MAXCHUNK) {
|
||||
DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks,
|
||||
(DES_cblock *)&(ctx->iv[0]),
|
||||
&data(ctx)->inw, &data(ctx)->outw, ctx->encrypt);
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
&data(ctx)->inw, &data(ctx)->outw,
|
||||
EVP_CIPHER_CTX_encrypting(ctx));
|
||||
inl -= EVP_MAXCHUNK;
|
||||
in += EVP_MAXCHUNK;
|
||||
out += EVP_MAXCHUNK;
|
||||
}
|
||||
if (inl)
|
||||
DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks,
|
||||
(DES_cblock *)&(ctx->iv[0]),
|
||||
&data(ctx)->inw, &data(ctx)->outw, ctx->encrypt);
|
||||
(DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
&data(ctx)->inw, &data(ctx)->outw,
|
||||
EVP_CIPHER_CTX_encrypting(ctx));
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user