From 3a50714bbd62a5bbf33b10b2e3b087633fbf1e17 Mon Sep 17 00:00:00 2001 From: tb Date: Tue, 25 Apr 2023 16:50:33 +0000 Subject: [PATCH] Remove X9.31 support ok jsing --- lib/libcrypto/Makefile | 4 +- lib/libcrypto/bn/bn.h | 16 +- lib/libcrypto/bn/bn_x931p.c | 292 ---------------------------- lib/libcrypto/evp/evp.h | 3 +- lib/libcrypto/man/EVP_PKEY_keygen.3 | 10 +- lib/libcrypto/rsa/rsa.h | 10 +- lib/libcrypto/rsa/rsa_x931.c | 164 ---------------- 7 files changed, 6 insertions(+), 493 deletions(-) delete mode 100644 lib/libcrypto/bn/bn_x931p.c delete mode 100644 lib/libcrypto/rsa/rsa_x931.c diff --git a/lib/libcrypto/Makefile b/lib/libcrypto/Makefile index 5710b9210ae..87b699bb2fa 100644 --- a/lib/libcrypto/Makefile +++ b/lib/libcrypto/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.109 2023/04/25 15:51:31 tb Exp $ +# $OpenBSD: Makefile,v 1.110 2023/04/25 16:50:33 tb Exp $ LIB= crypto LIBREBUILD=y @@ -204,7 +204,6 @@ SRCS+= bn_shift.c SRCS+= bn_small_primes.c SRCS+= bn_sqr.c SRCS+= bn_word.c -#SRCS+= bn_x931p.c # buffer/ SRCS+= buf_err.c @@ -617,7 +616,6 @@ SRCS+= rsa_prn.c SRCS+= rsa_pss.c SRCS+= rsa_saos.c SRCS+= rsa_sign.c -#SRCS+= rsa_x931.c # sha/ SRCS+= sha1.c diff --git a/lib/libcrypto/bn/bn.h b/lib/libcrypto/bn/bn.h index fae8df5d904..b443d82c960 100644 --- a/lib/libcrypto/bn/bn.h +++ b/lib/libcrypto/bn/bn.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn.h,v 1.63 2023/04/25 16:41:29 tb Exp $ */ +/* $OpenBSD: bn.h,v 1.64 2023/04/25 16:50:33 tb Exp $ */ /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -483,20 +483,6 @@ int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb); int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, int do_trial_division, BN_GENCB *cb); -/* Remove in next major bump. */ -#if !defined(LIBRESSL_NEXT_API) || defined(LIBRESSL_INTERNAL) -int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx); - -int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, - const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2, - const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb); -int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, - BIGNUM *Xp1, BIGNUM *Xp2, - const BIGNUM *Xp, - const BIGNUM *e, BN_CTX *ctx, - BN_GENCB *cb); -#endif - BN_MONT_CTX *BN_MONT_CTX_new(void ); /* Remove in next major bump. */ #if !defined(LIBRESSL_NEXT_API) || defined(LIBRESSL_INTERNAL) diff --git a/lib/libcrypto/bn/bn_x931p.c b/lib/libcrypto/bn/bn_x931p.c deleted file mode 100644 index 9105be11cdf..00000000000 --- a/lib/libcrypto/bn/bn_x931p.c +++ /dev/null @@ -1,292 +0,0 @@ -/* $OpenBSD: bn_x931p.c,v 1.16 2023/03/27 10:25:02 tb Exp $ */ -/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL - * project 2005. - */ -/* ==================================================================== - * Copyright (c) 2005 The OpenSSL Project. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. All advertising materials mentioning features or use of this - * software must display the following acknowledgment: - * "This product includes software developed by the OpenSSL Project - * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" - * - * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to - * endorse or promote products derived from this software without - * prior written permission. For written permission, please contact - * licensing@OpenSSL.org. - * - * 5. Products derived from this software may not be called "OpenSSL" - * nor may "OpenSSL" appear in their names without prior written - * permission of the OpenSSL Project. - * - * 6. Redistributions of any form whatsoever must retain the following - * acknowledgment: - * "This product includes software developed by the OpenSSL Project - * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" - * - * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY - * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * ==================================================================== - * - * This product includes cryptographic software written by Eric Young - * (eay@cryptsoft.com). This product includes software written by Tim - * Hudson (tjh@cryptsoft.com). - * - */ - -#include -#include - -#include "bn_local.h" - -/* X9.31 routines for prime derivation */ - -/* X9.31 prime derivation. This is used to generate the primes pi - * (p1, p2, q1, q2) from a parameter Xpi by checking successive odd - * integers. - */ - -static int -bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx, BN_GENCB *cb) -{ - int i = 0, is_prime; - - if (!bn_copy(pi, Xpi)) - return 0; - if (!BN_is_odd(pi) && !BN_add_word(pi, 1)) - return 0; - for (;;) { - i++; - BN_GENCB_call(cb, 0, i); - /* NB 27 MR is specified in X9.31 */ - is_prime = BN_is_prime_fasttest_ex(pi, 27, ctx, 1, cb); - if (is_prime < 0) - return 0; - if (is_prime == 1) - break; - if (!BN_add_word(pi, 2)) - return 0; - } - BN_GENCB_call(cb, 2, i); - return 1; -} - -/* This is the main X9.31 prime derivation function. From parameters - * Xp1, Xp2 and Xp derive the prime p. If the parameters p1 or p2 are - * not NULL they will be returned too: this is needed for testing. - */ - -int -BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, const BIGNUM *Xp, - const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx, - BN_GENCB *cb) -{ - int ret = 0; - - BIGNUM *t, *p1p2, *pm1; - - /* Only even e supported */ - if (!BN_is_odd(e)) - return 0; - - BN_CTX_start(ctx); - if (p1 == NULL) { - if ((p1 = BN_CTX_get(ctx)) == NULL) - goto err; - } - if (p2 == NULL) { - if ((p2 = BN_CTX_get(ctx)) == NULL) - goto err; - } - - if ((t = BN_CTX_get(ctx)) == NULL) - goto err; - if ((p1p2 = BN_CTX_get(ctx)) == NULL) - goto err; - if ((pm1 = BN_CTX_get(ctx)) == NULL) - goto err; - - if (!bn_x931_derive_pi(p1, Xp1, ctx, cb)) - goto err; - - if (!bn_x931_derive_pi(p2, Xp2, ctx, cb)) - goto err; - - if (!BN_mul(p1p2, p1, p2, ctx)) - goto err; - - /* First set p to value of Rp */ - - if (BN_mod_inverse_ct(p, p2, p1, ctx) == NULL) - goto err; - - if (!BN_mul(p, p, p2, ctx)) - goto err; - - if (BN_mod_inverse_ct(t, p1, p2, ctx) == NULL) - goto err; - - if (!BN_mul(t, t, p1, ctx)) - goto err; - - if (!BN_sub(p, p, t)) - goto err; - - if (BN_is_negative(p) && !BN_add(p, p, p1p2)) - goto err; - - /* p now equals Rp */ - - if (!BN_mod_sub(p, p, Xp, p1p2, ctx)) - goto err; - - if (!BN_add(p, p, Xp)) - goto err; - - /* p now equals Yp0 */ - - for (;;) { - int i = 1; - BN_GENCB_call(cb, 0, i++); - if (!bn_copy(pm1, p)) - goto err; - if (!BN_sub_word(pm1, 1)) - goto err; - if (!BN_gcd_ct(t, pm1, e, ctx)) - goto err; - if (BN_is_one(t)) { - int r; - - /* - * X9.31 specifies 8 MR and 1 Lucas test or any prime - * test offering similar or better guarantees 50 MR - * is considerably better. - */ - r = BN_is_prime_fasttest_ex(p, 50, ctx, 1, cb); - if (r < 0) - goto err; - if (r == 1) - break; - } - if (!BN_add(p, p, p1p2)) - goto err; - } - - BN_GENCB_call(cb, 3, 0); - - ret = 1; - -err: - - BN_CTX_end(ctx); - - return ret; -} - -/* Generate pair of parameters Xp, Xq for X9.31 prime generation. - * Note: nbits parameter is sum of number of bits in both. - */ - -int -BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx) -{ - BIGNUM *t; - int i; - int ret = 0; - - /* Number of bits for each prime is of the form - * 512+128s for s = 0, 1, ... - */ - if ((nbits < 1024) || (nbits & 0xff)) - return 0; - nbits >>= 1; - /* The random value Xp must be between sqrt(2) * 2^(nbits-1) and - * 2^nbits - 1. By setting the top two bits we ensure that the lower - * bound is exceeded. - */ - if (!BN_rand(Xp, nbits, 1, 0)) - return 0; - - BN_CTX_start(ctx); - if ((t = BN_CTX_get(ctx)) == NULL) - goto err; - - for (i = 0; i < 1000; i++) { - if (!BN_rand(Xq, nbits, 1, 0)) - goto err; - /* Check that |Xp - Xq| > 2^(nbits - 100) */ - if (!BN_sub(t, Xp, Xq)) - goto err; - if (BN_num_bits(t) > (nbits - 100)) - break; - } - - if (i < 1000) - ret = 1; - -err: - BN_CTX_end(ctx); - - return ret; -} - -/* Generate primes using X9.31 algorithm. Of the values p, p1, p2, Xp1 - * and Xp2 only 'p' needs to be non-NULL. If any of the others are not NULL - * the relevant parameter will be stored in it. - * - * Due to the fact that |Xp - Xq| > 2^(nbits - 100) must be satisfied Xp and Xq - * are generated using the previous function and supplied as input. - */ - -int -BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, BIGNUM *Xp1, - BIGNUM *Xp2, const BIGNUM *Xp, const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb) -{ - int ret = 0; - - BN_CTX_start(ctx); - if (Xp1 == NULL) { - if ((Xp1 = BN_CTX_get(ctx)) == NULL) - goto error; - } - if (Xp2 == NULL) { - if ((Xp2 = BN_CTX_get(ctx)) == NULL) - goto error; - } - - if (!BN_rand(Xp1, 101, 0, 0)) - goto error; - if (!BN_rand(Xp2, 101, 0, 0)) - goto error; - if (!BN_X931_derive_prime_ex(p, p1, p2, Xp, Xp1, Xp2, e, ctx, cb)) - goto error; - - ret = 1; - -error: - BN_CTX_end(ctx); - - return ret; -} diff --git a/lib/libcrypto/evp/evp.h b/lib/libcrypto/evp/evp.h index 830774a7400..548522eb12f 100644 --- a/lib/libcrypto/evp/evp.h +++ b/lib/libcrypto/evp/evp.h @@ -1,4 +1,4 @@ -/* $OpenBSD: evp.h,v 1.116 2023/04/16 17:06:19 jsing Exp $ */ +/* $OpenBSD: evp.h,v 1.117 2023/04/25 16:50:33 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -183,7 +183,6 @@ extern "C" { */ #define EVP_MD_CTX_FLAG_PAD_MASK 0xF0 /* RSA mode to use */ #define EVP_MD_CTX_FLAG_PAD_PKCS1 0x00 /* PKCS#1 v1.5 mode */ -#define EVP_MD_CTX_FLAG_PAD_X931 0x10 /* X9.31 mode */ #define EVP_MD_CTX_FLAG_PAD_PSS 0x20 /* PSS mode */ #define EVP_MD_CTX_FLAG_NO_INIT 0x0100 /* Don't initialize md_data */ diff --git a/lib/libcrypto/man/EVP_PKEY_keygen.3 b/lib/libcrypto/man/EVP_PKEY_keygen.3 index 8994496a91c..aae994541fb 100644 --- a/lib/libcrypto/man/EVP_PKEY_keygen.3 +++ b/lib/libcrypto/man/EVP_PKEY_keygen.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: EVP_PKEY_keygen.3,v 1.10 2022/11/16 15:34:41 schwarze Exp $ +.\" $OpenBSD: EVP_PKEY_keygen.3,v 1.11 2023/04/25 16:50:33 tb Exp $ .\" full merge up to: OpenSSL e9b77246 Jan 20 19:58:49 2017 +0100 .\" selective merge up to: OpenSSL 48e5119a Jan 19 10:49:22 2018 +0100 .\" @@ -50,7 +50,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED .\" OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd $Mdocdate: November 16 2022 $ +.Dd $Mdocdate: April 25 2023 $ .Dt EVP_PKEY_KEYGEN 3 .Os .Sh NAME @@ -64,12 +64,6 @@ .Nm EVP_PKEY_CTX_get_keygen_info , .Nm EVP_PKEY_CTX_set_app_data , .Nm EVP_PKEY_CTX_get_app_data -.\" The following functions are intentionally undocumented: -.\" .Nm BN_X931_derive_prime_ex , -.\" .Nm BN_X931_generate_prime_ex , -.\" .Nm BN_X931_generate_Xpq -.\" They are unused outside OpenSSL/LibreSSL and deprecated in OpenSSL 3.0, -.\" see https://github.com/openssl/openssl/issues/10111 .Nd key and parameter generation functions .Sh SYNOPSIS .In openssl/evp.h diff --git a/lib/libcrypto/rsa/rsa.h b/lib/libcrypto/rsa/rsa.h index cddd80bc856..b2a9a85ea8f 100644 --- a/lib/libcrypto/rsa/rsa.h +++ b/lib/libcrypto/rsa/rsa.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa.h,v 1.62 2023/04/18 08:47:28 tb Exp $ */ +/* $OpenBSD: rsa.h,v 1.63 2023/04/25 16:50:33 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -349,14 +349,6 @@ int RSA_padding_add_none(unsigned char *to, int tlen, const unsigned char *f, int fl); int RSA_padding_check_none(unsigned char *to, int tlen, const unsigned char *f, int fl, int rsa_len); -/* Remove in next major bump. */ -#if !defined(LIBRESSL_NEXT_API) || defined(LIBRESSL_INTERNAL) -int RSA_padding_add_X931(unsigned char *to, int tlen, - const unsigned char *f, int fl); -int RSA_padding_check_X931(unsigned char *to, int tlen, - const unsigned char *f, int fl, int rsa_len); -int RSA_X931_hash_id(int nid); -#endif int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash, const EVP_MD *Hash, const unsigned char *EM, int sLen); diff --git a/lib/libcrypto/rsa/rsa_x931.c b/lib/libcrypto/rsa/rsa_x931.c deleted file mode 100644 index 3579735ab24..00000000000 --- a/lib/libcrypto/rsa/rsa_x931.c +++ /dev/null @@ -1,164 +0,0 @@ -/* $OpenBSD: rsa_x931.c,v 1.10 2017/01/29 17:49:23 beck Exp $ */ -/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL - * project 2005. - */ -/* ==================================================================== - * Copyright (c) 2005 The OpenSSL Project. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. All advertising materials mentioning features or use of this - * software must display the following acknowledgment: - * "This product includes software developed by the OpenSSL Project - * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" - * - * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to - * endorse or promote products derived from this software without - * prior written permission. For written permission, please contact - * licensing@OpenSSL.org. - * - * 5. Products derived from this software may not be called "OpenSSL" - * nor may "OpenSSL" appear in their names without prior written - * permission of the OpenSSL Project. - * - * 6. Redistributions of any form whatsoever must retain the following - * acknowledgment: - * "This product includes software developed by the OpenSSL Project - * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" - * - * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY - * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * ==================================================================== - * - * This product includes cryptographic software written by Eric Young - * (eay@cryptsoft.com). This product includes software written by Tim - * Hudson (tjh@cryptsoft.com). - * - */ - -#include -#include - -#include -#include -#include -#include - -int -RSA_padding_add_X931(unsigned char *to, int tlen, const unsigned char *from, - int flen) -{ - int j; - unsigned char *p; - - /* - * Absolute minimum amount of padding is 1 header nibble, 1 padding - * nibble and 2 trailer bytes: but 1 hash if is already in 'from'. - */ - j = tlen - flen - 2; - - if (j < 0) { - RSAerror(RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); - return -1; - } - - p = (unsigned char *)to; - - /* If no padding start and end nibbles are in one byte */ - if (j == 0) - *p++ = 0x6A; - else { - *p++ = 0x6B; - if (j > 1) { - memset(p, 0xBB, j - 1); - p += j - 1; - } - *p++ = 0xBA; - } - memcpy(p, from, flen); - p += flen; - *p = 0xCC; - return 1; -} - -int -RSA_padding_check_X931(unsigned char *to, int tlen, const unsigned char *from, - int flen, int num) -{ - int i = 0, j; - const unsigned char *p = from; - - if (num != flen || (*p != 0x6A && *p != 0x6B)) { - RSAerror(RSA_R_INVALID_HEADER); - return -1; - } - - if (*p++ == 0x6B) { - j = flen - 3; - for (i = 0; i < j; i++) { - unsigned char c = *p++; - if (c == 0xBA) - break; - if (c != 0xBB) { - RSAerror(RSA_R_INVALID_PADDING); - return -1; - } - } - - if (i == 0) { - RSAerror(RSA_R_INVALID_PADDING); - return -1; - } - - j -= i; - } else - j = flen - 2; - - if (j < 0 || p[j] != 0xCC) { - RSAerror(RSA_R_INVALID_TRAILER); - return -1; - } - - memcpy(to, p, j); - - return j; -} - -/* Translate between X931 hash ids and NIDs */ - -int -RSA_X931_hash_id(int nid) -{ - switch (nid) { - case NID_sha1: - return 0x33; - case NID_sha256: - return 0x34; - case NID_sha384: - return 0x36; - case NID_sha512: - return 0x35; - } - - return -1; -} -- 2.20.1