diff options
author | Joel Sing <jsing@cvs.openbsd.org> | 2022-09-04 13:17:19 +0000 |
---|---|---|
committer | Joel Sing <jsing@cvs.openbsd.org> | 2022-09-04 13:17:19 +0000 |
commit | df65d2305e8e45f3fb04cb5f2aa49a2240aad0e7 (patch) | |
tree | 8856ce542674df431d1d86be89282001d717a9fd /lib/libcrypto | |
parent | c800265462dcd78dc227d93e115383aec860c3a6 (diff) |
Rearrange some functions.
Pull the init_key and ctrl (if present) functions up to the top. This
improves readability and allows for the removal of function prototypes.
No functional change.
Diffstat (limited to 'lib/libcrypto')
-rw-r--r-- | lib/libcrypto/evp/e_bf.c | 22 | ||||
-rw-r--r-- | lib/libcrypto/evp/e_camellia.c | 38 | ||||
-rw-r--r-- | lib/libcrypto/evp/e_cast.c | 21 | ||||
-rw-r--r-- | lib/libcrypto/evp/e_des.c | 56 | ||||
-rw-r--r-- | lib/libcrypto/evp/e_des3.c | 103 | ||||
-rw-r--r-- | lib/libcrypto/evp/e_gost2814789.c | 22 | ||||
-rw-r--r-- | lib/libcrypto/evp/e_idea.c | 53 | ||||
-rw-r--r-- | lib/libcrypto/evp/e_sm4.c | 3 |
8 files changed, 141 insertions, 177 deletions
diff --git a/lib/libcrypto/evp/e_bf.c b/lib/libcrypto/evp/e_bf.c index f4e0c4c0934..745c4d32014 100644 --- a/lib/libcrypto/evp/e_bf.c +++ b/lib/libcrypto/evp/e_bf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: e_bf.c,v 1.9 2022/09/03 19:43:16 jsing Exp $ */ +/* $OpenBSD: e_bf.c,v 1.10 2022/09/04 13:17:18 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -68,9 +68,6 @@ #include "evp_locl.h" -static int bf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); - typedef struct { BF_KEY ks; } EVP_BF_KEY; @@ -78,6 +75,14 @@ typedef struct { #define data(ctx) ((EVP_BF_KEY *)(ctx)->cipher_data) static int +bf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + BF_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key); + return 1; +} + +static int bf_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) { while (inl >= EVP_MAXCHUNK) { @@ -237,13 +242,4 @@ EVP_bf_ecb(void) { return &bf_ecb; } - - -static int -bf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) -{ - BF_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key); - return 1; -} #endif diff --git a/lib/libcrypto/evp/e_camellia.c b/lib/libcrypto/evp/e_camellia.c index 42c036a7abc..2d7ab736271 100644 --- a/lib/libcrypto/evp/e_camellia.c +++ b/lib/libcrypto/evp/e_camellia.c @@ -1,4 +1,4 @@ -/* $OpenBSD: e_camellia.c,v 1.11 2022/09/04 07:54:59 jsing Exp $ */ +/* $OpenBSD: e_camellia.c,v 1.12 2022/09/04 13:17:18 jsing Exp $ */ /* ==================================================================== * Copyright (c) 2006 The OpenSSL Project. All rights reserved. * @@ -64,9 +64,6 @@ #include "evp_locl.h" -static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); - /* Camellia subkey Structure */ typedef struct { CAMELLIA_KEY ks; @@ -76,6 +73,22 @@ typedef struct { #define data(ctx) ((EVP_CAMELLIA_KEY *)(ctx)->cipher_data) static int +camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + int ret; + + ret = Camellia_set_key(key, ctx->key_len * 8, ctx->cipher_data); + + if (ret < 0) { + EVPerror(EVP_R_CAMELLIA_KEY_SETUP_FAILED); + return 0; + } + + return 1; +} + +static int camellia_128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) { while (inl >= EVP_MAXCHUNK) { @@ -828,21 +841,4 @@ EVP_camellia_256_cfb8(void) { return &camellia_256_cfb8; } - -/* The subkey for Camellia is generated. */ -static int -camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) -{ - int ret; - - ret = Camellia_set_key(key, ctx->key_len * 8, ctx->cipher_data); - - if (ret < 0) { - EVPerror(EVP_R_CAMELLIA_KEY_SETUP_FAILED); - return 0; - } - - return 1; -} #endif diff --git a/lib/libcrypto/evp/e_cast.c b/lib/libcrypto/evp/e_cast.c index b23dfeef1c5..32f86d86af7 100644 --- a/lib/libcrypto/evp/e_cast.c +++ b/lib/libcrypto/evp/e_cast.c @@ -1,4 +1,4 @@ -/* $OpenBSD: e_cast.c,v 1.8 2022/09/03 19:51:53 jsing Exp $ */ +/* $OpenBSD: e_cast.c,v 1.9 2022/09/04 13:17:18 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -68,9 +68,6 @@ #include "evp_locl.h" -static int cast_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); - typedef struct { CAST_KEY ks; } EVP_CAST_KEY; @@ -78,6 +75,14 @@ typedef struct { #define data(ctx) ((EVP_CAST_KEY *)(ctx)->cipher_data) static int +cast_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + CAST_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key); + return 1; +} + +static int cast5_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) { while (inl >= EVP_MAXCHUNK) { @@ -237,12 +242,4 @@ EVP_cast5_ecb(void) { return &cast5_ecb; } - -static int -cast_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) -{ - CAST_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key); - return 1; -} #endif diff --git a/lib/libcrypto/evp/e_des.c b/lib/libcrypto/evp/e_des.c index 1064c0b7cc9..bf037591be8 100644 --- a/lib/libcrypto/evp/e_des.c +++ b/lib/libcrypto/evp/e_des.c @@ -1,4 +1,4 @@ -/* $OpenBSD: e_des.c,v 1.16 2022/09/04 08:18:07 jsing Exp $ */ +/* $OpenBSD: e_des.c,v 1.17 2022/09/04 13:17:18 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -68,9 +68,29 @@ #include "evp_locl.h" -static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); -static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); +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; + + DES_set_key_unchecked(deskey, ctx->cipher_data); + return 1; +} + +static int +des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ + switch (type) { + case EVP_CTRL_RAND_KEY: + if (DES_random_key((DES_cblock *)ptr) == 0) + return 0; + return 1; + + default: + return -1; + } +} static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, @@ -278,7 +298,6 @@ EVP_des_ecb(void) return &des_ecb; } - static const EVP_CIPHER des_cfb1 = { .nid = NID_des_cfb1, .block_size = 1, @@ -301,7 +320,6 @@ EVP_des_cfb1(void) return &des_cfb1; } - static const EVP_CIPHER des_cfb8 = { .nid = NID_des_cfb8, .block_size = 1, @@ -323,30 +341,4 @@ EVP_des_cfb8(void) { return &des_cfb8; } - - -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; - - DES_set_key_unchecked(deskey, ctx->cipher_data); - return 1; -} - -static int -des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) -{ - switch (type) { - case EVP_CTRL_RAND_KEY: - if (DES_random_key((DES_cblock *)ptr) == 0) - return 0; - return 1; - - default: - return -1; - } -} - #endif diff --git a/lib/libcrypto/evp/e_des3.c b/lib/libcrypto/evp/e_des3.c index 1d843d9c84c..e9d7f56809f 100644 --- a/lib/libcrypto/evp/e_des3.c +++ b/lib/libcrypto/evp/e_des3.c @@ -1,4 +1,4 @@ -/* $OpenBSD: e_des3.c,v 1.22 2022/09/04 08:54:16 jsing Exp $ */ +/* $OpenBSD: e_des3.c,v 1.23 2022/09/04 13:17:18 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -69,14 +69,6 @@ #include "evp_locl.h" -static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); - -static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); - -static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); - typedef struct { DES_key_schedule ks1;/* key schedule */ DES_key_schedule ks2;/* key schedule (for ede) */ @@ -86,6 +78,52 @@ typedef struct { #define data(ctx) ((DES_EDE_KEY *)(ctx)->cipher_data) static int +des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + DES_cblock *deskey = (DES_cblock *)key; + + DES_set_key_unchecked(&deskey[0], &data(ctx)->ks1); + DES_set_key_unchecked(&deskey[1], &data(ctx)->ks2); + memcpy(&data(ctx)->ks3, &data(ctx)->ks1, + sizeof(data(ctx)->ks1)); + return 1; +} + +static int +des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + DES_cblock *deskey = (DES_cblock *)key; + + + DES_set_key_unchecked(&deskey[0], &data(ctx)->ks1); + DES_set_key_unchecked(&deskey[1], &data(ctx)->ks2); + DES_set_key_unchecked(&deskey[2], &data(ctx)->ks3); + return 1; +} + +static int +des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ + DES_cblock *deskey = ptr; + + switch (type) { + case EVP_CTRL_RAND_KEY: + if (DES_random_key(deskey) == 0) + return 0; + if (c->key_len >= 16 && DES_random_key(deskey + 1) == 0) + return 0; + if (c->key_len >= 24 && DES_random_key(deskey + 2) == 0) + return 0; + return 1; + + default: + return -1; + } +} + +static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) { @@ -432,53 +470,6 @@ EVP_des_ede3_cfb8(void) return &des_ede3_cfb8; } - -static int -des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) -{ - DES_cblock *deskey = (DES_cblock *)key; - - DES_set_key_unchecked(&deskey[0], &data(ctx)->ks1); - DES_set_key_unchecked(&deskey[1], &data(ctx)->ks2); - memcpy(&data(ctx)->ks3, &data(ctx)->ks1, - sizeof(data(ctx)->ks1)); - return 1; -} - -static int -des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) -{ - DES_cblock *deskey = (DES_cblock *)key; - - - DES_set_key_unchecked(&deskey[0], &data(ctx)->ks1); - DES_set_key_unchecked(&deskey[1], &data(ctx)->ks2); - DES_set_key_unchecked(&deskey[2], &data(ctx)->ks3); - return 1; -} - -static int -des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) -{ - DES_cblock *deskey = ptr; - - switch (type) { - case EVP_CTRL_RAND_KEY: - if (DES_random_key(deskey) == 0) - return 0; - if (c->key_len >= 16 && DES_random_key(deskey + 1) == 0) - return 0; - if (c->key_len >= 24 && DES_random_key(deskey + 2) == 0) - return 0; - return 1; - - default: - return -1; - } -} - const EVP_CIPHER * EVP_des_ede(void) { diff --git a/lib/libcrypto/evp/e_gost2814789.c b/lib/libcrypto/evp/e_gost2814789.c index 11379be5476..959610f16db 100644 --- a/lib/libcrypto/evp/e_gost2814789.c +++ b/lib/libcrypto/evp/e_gost2814789.c @@ -1,4 +1,4 @@ -/* $OpenBSD: e_gost2814789.c,v 1.6 2022/09/04 09:48:23 jsing Exp $ */ +/* $OpenBSD: e_gost2814789.c,v 1.7 2022/09/04 13:17:18 jsing Exp $ */ /* * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> * Copyright (c) 2005-2006 Cryptocom LTD @@ -66,6 +66,15 @@ typedef struct { } EVP_GOST2814789_CTX; static int +gost2814789_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + EVP_GOST2814789_CTX *c = ctx->cipher_data; + + return Gost2814789_set_key(&c->ks, key, ctx->key_len * 8); +} + +static int gost2814789_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) { EVP_GOST2814789_CTX *c = ctx->cipher_data; @@ -89,15 +98,6 @@ gost2814789_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) } } -static int -gost2814789_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) -{ - EVP_GOST2814789_CTX *c = ctx->cipher_data; - - return Gost2814789_set_key(&c->ks, key, ctx->key_len * 8); -} - int gost2814789_set_asn1_params(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params) { @@ -227,7 +227,6 @@ gost2814789_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned return 1; } - static int gost2814789_cnt_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) @@ -316,5 +315,4 @@ EVP_gost2814789_cnt(void) { return &gost2814789_cnt; } - #endif diff --git a/lib/libcrypto/evp/e_idea.c b/lib/libcrypto/evp/e_idea.c index d69f200423a..819f52ba23d 100644 --- a/lib/libcrypto/evp/e_idea.c +++ b/lib/libcrypto/evp/e_idea.c @@ -1,4 +1,4 @@ -/* $OpenBSD: e_idea.c,v 1.12 2022/09/04 08:57:32 jsing Exp $ */ +/* $OpenBSD: e_idea.c,v 1.13 2022/09/04 13:17:18 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -69,14 +69,34 @@ #include "evp_locl.h" -static int idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); - /* NB idea_ecb_encrypt doesn't take an 'encrypt' argument so we treat it as a special * case */ static int +idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + if (!enc) { + if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE) + enc = 1; + else if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE) + enc = 1; + } + if (enc) + idea_set_encrypt_key(key, ctx->cipher_data); + else { + IDEA_KEY_SCHEDULE tmp; + + idea_set_encrypt_key(key, &tmp); + idea_set_decrypt_key(&tmp, ctx->cipher_data); + explicit_bzero((unsigned char *)&tmp, + sizeof(IDEA_KEY_SCHEDULE)); + } + return 1; +} + +static int idea_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) { @@ -241,29 +261,4 @@ EVP_idea_ecb(void) { return &idea_ecb; } - - -static int -idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) -{ - if (!enc) { - if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE) - enc = 1; - else if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE) - enc = 1; - } - if (enc) - idea_set_encrypt_key(key, ctx->cipher_data); - else { - IDEA_KEY_SCHEDULE tmp; - - idea_set_encrypt_key(key, &tmp); - idea_set_decrypt_key(&tmp, ctx->cipher_data); - explicit_bzero((unsigned char *)&tmp, - sizeof(IDEA_KEY_SCHEDULE)); - } - return 1; -} - #endif diff --git a/lib/libcrypto/evp/e_sm4.c b/lib/libcrypto/evp/e_sm4.c index 9003abccc30..fd9a9d04f72 100644 --- a/lib/libcrypto/evp/e_sm4.c +++ b/lib/libcrypto/evp/e_sm4.c @@ -1,4 +1,4 @@ -/* $OpenBSD: e_sm4.c,v 1.2 2022/09/03 20:02:17 jsing Exp $ */ +/* $OpenBSD: e_sm4.c,v 1.3 2022/09/04 13:17:18 jsing Exp $ */ /* * Copyright (c) 2017, 2019 Ribose Inc * @@ -267,5 +267,4 @@ EVP_sm4_ctr(void) { return &sm4_ctr_mode; } - #endif |