From 5af61821c00d0ab863a5f933b9ce9a5936bc4248 Mon Sep 17 00:00:00 2001 From: Denys Date: Sun, 8 Sep 2024 18:01:55 +0300 Subject: [PATCH] Remove duplicate implementations of some DSA/ECDSA functions --- src/lib/CMakeLists.txt | 2 + src/lib/crypto/dsa.cpp | 31 +------------- src/lib/crypto/dsa_common.cpp | 77 +++++++++++++++++++++++++++++++++++ src/lib/crypto/dsa_common.h | 36 ++++++++++++++++ src/lib/crypto/dsa_ossl.cpp | 31 +------------- src/lib/crypto/ecdsa.cpp | 19 --------- src/lib/crypto/ecdsa_ossl.cpp | 18 -------- 7 files changed, 117 insertions(+), 97 deletions(-) create mode 100644 src/lib/crypto/dsa_common.cpp create mode 100644 src/lib/crypto/dsa_common.h diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 1e4443c757..d0c10fcb2f 100755 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -242,6 +242,7 @@ configure_file(config.h.in config.h) if(CRYPTO_BACKEND_OPENSSL) set(CRYPTO_SOURCES crypto/bn_ossl.cpp + crypto/dsa_common.cpp crypto/dsa_ossl.cpp crypto/ec_curves.cpp crypto/ec_ossl.cpp @@ -272,6 +273,7 @@ if(CRYPTO_BACKEND_OPENSSL) elseif(CRYPTO_BACKEND_BOTAN) set(CRYPTO_SOURCES crypto/bn.cpp + crypto/dsa_common.cpp crypto/dsa.cpp crypto/ec_curves.cpp crypto/ec.cpp diff --git a/src/lib/crypto/dsa.cpp b/src/lib/crypto/dsa.cpp index 575c1c9f99..1d6cbb9562 100644 --- a/src/lib/crypto/dsa.cpp +++ b/src/lib/crypto/dsa.cpp @@ -82,8 +82,7 @@ #include "dsa.h" #include "bn.h" #include "utils.h" - -#define DSA_MAX_Q_BITLEN 256 +#include "dsa_common.h" rnp_result_t dsa_validate_key(rnp::RNG *rng, const pgp_dsa_key_t *key, bool secret) @@ -347,31 +346,3 @@ dsa_generate(rnp::RNG *rng, pgp_dsa_key_t *key, size_t keylen, size_t qbits) botan_pubkey_destroy(key_pub); return ret; } - -pgp_hash_alg_t -dsa_get_min_hash(size_t qsize) -{ - /* - * I'm using _broken_ SHA1 here only because - * some old implementations may not understand keys created - * with other hashes. If you're sure we don't have to support - * such implementations, please be my guest and remove it. - */ - return (qsize < 160) ? PGP_HASH_UNKNOWN : - (qsize == 160) ? PGP_HASH_SHA1 : - (qsize <= 224) ? PGP_HASH_SHA224 : - (qsize <= 256) ? PGP_HASH_SHA256 : - (qsize <= 384) ? PGP_HASH_SHA384 : - (qsize <= 512) ? PGP_HASH_SHA512 - /*(qsize>512)*/ : - PGP_HASH_UNKNOWN; -} - -size_t -dsa_choose_qsize_by_psize(size_t psize) -{ - return (psize == 1024) ? 160 : - (psize <= 2047) ? 224 : - (psize <= 3072) ? DSA_MAX_Q_BITLEN : - 0; -} diff --git a/src/lib/crypto/dsa_common.cpp b/src/lib/crypto/dsa_common.cpp new file mode 100644 index 0000000000..e32a206feb --- /dev/null +++ b/src/lib/crypto/dsa_common.cpp @@ -0,0 +1,77 @@ +/*- + * Copyright (c) 2021-2024 Ribose Inc. + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS 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 COPYRIGHT HOLDERS OR 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. + */ + +#include "crypto.h" +#include "config.h" +#include "defaults.h" +#include "dsa_common.h" + +pgp_hash_alg_t +dsa_get_min_hash(size_t qsize) +{ + /* + * I'm using _broken_ SHA1 here only because + * some old implementations may not understand keys created + * with other hashes. If you're sure we don't have to support + * such implementations, please be my guest and remove it. + */ + return (qsize < 160) ? PGP_HASH_UNKNOWN : + (qsize == 160) ? PGP_HASH_SHA1 : + (qsize <= 224) ? PGP_HASH_SHA224 : + (qsize <= 256) ? PGP_HASH_SHA256 : + (qsize <= 384) ? PGP_HASH_SHA384 : + (qsize <= 512) ? PGP_HASH_SHA512 + /*(qsize>512)*/ : + PGP_HASH_UNKNOWN; +} + +size_t +dsa_choose_qsize_by_psize(size_t psize) +{ + return (psize == 1024) ? 160 : + (psize <= 2047) ? 224 : + (psize <= 3072) ? DSA_MAX_Q_BITLEN : + 0; +} + +pgp_hash_alg_t +ecdsa_get_min_hash(pgp_curve_t curve) +{ + switch (curve) { + case PGP_CURVE_NIST_P_256: + case PGP_CURVE_BP256: + case PGP_CURVE_P256K1: + return PGP_HASH_SHA256; + case PGP_CURVE_NIST_P_384: + case PGP_CURVE_BP384: + return PGP_HASH_SHA384; + case PGP_CURVE_NIST_P_521: + case PGP_CURVE_BP512: + return PGP_HASH_SHA512; + default: + return PGP_HASH_UNKNOWN; + } +} diff --git a/src/lib/crypto/dsa_common.h b/src/lib/crypto/dsa_common.h new file mode 100644 index 0000000000..a7d4a64cf5 --- /dev/null +++ b/src/lib/crypto/dsa_common.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2017-2018, [Ribose Inc](https://www.ribose.com). + * All rights reserved. + * + * This code is originally derived from software contributed to + * The NetBSD Foundation by Alistair Crooks (agc@netbsd.org), and + * carried further by Ribose Inc (https://www.ribose.com). + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS 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 COPYRIGHT HOLDERS OR 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. + */ + +#ifndef RNP_DSA_COMMON_H_ +#define RNP_DSA_COMMON_H_ + +#define DSA_MAX_Q_BITLEN 256 + +#endif diff --git a/src/lib/crypto/dsa_ossl.cpp b/src/lib/crypto/dsa_ossl.cpp index c0b124f6b8..7c87511d08 100644 --- a/src/lib/crypto/dsa_ossl.cpp +++ b/src/lib/crypto/dsa_ossl.cpp @@ -35,13 +35,12 @@ #include #include #include +#include "dsa_common.h" #if defined(CRYPTO_BACKEND_OPENSSL3) #include #include #endif -#define DSA_MAX_Q_BITLEN 256 - static bool dsa_decode_sig(const uint8_t *data, size_t len, pgp_dsa_signature_t &sig) { @@ -403,31 +402,3 @@ dsa_generate(rnp::RNG *rng, pgp_dsa_key_t *key, size_t keylen, size_t qbits) EVP_PKEY_free(pkey); return ret; } - -pgp_hash_alg_t -dsa_get_min_hash(size_t qsize) -{ - /* - * I'm using _broken_ SHA1 here only because - * some old implementations may not understand keys created - * with other hashes. If you're sure we don't have to support - * such implementations, please be my guest and remove it. - */ - return (qsize < 160) ? PGP_HASH_UNKNOWN : - (qsize == 160) ? PGP_HASH_SHA1 : - (qsize <= 224) ? PGP_HASH_SHA224 : - (qsize <= 256) ? PGP_HASH_SHA256 : - (qsize <= 384) ? PGP_HASH_SHA384 : - (qsize <= 512) ? PGP_HASH_SHA512 - /*(qsize>512)*/ : - PGP_HASH_UNKNOWN; -} - -size_t -dsa_choose_qsize_by_psize(size_t psize) -{ - return (psize == 1024) ? 160 : - (psize <= 2047) ? 224 : - (psize <= 3072) ? DSA_MAX_Q_BITLEN : - 0; -} diff --git a/src/lib/crypto/ecdsa.cpp b/src/lib/crypto/ecdsa.cpp index 8fed5cd9c5..d73a47a9ce 100644 --- a/src/lib/crypto/ecdsa.cpp +++ b/src/lib/crypto/ecdsa.cpp @@ -245,22 +245,3 @@ ecdsa_verify(const pgp_ec_signature_t *sig, botan_pk_op_verify_destroy(verifier); return ret; } - -pgp_hash_alg_t -ecdsa_get_min_hash(pgp_curve_t curve) -{ - switch (curve) { - case PGP_CURVE_NIST_P_256: - case PGP_CURVE_BP256: - case PGP_CURVE_P256K1: - return PGP_HASH_SHA256; - case PGP_CURVE_NIST_P_384: - case PGP_CURVE_BP384: - return PGP_HASH_SHA384; - case PGP_CURVE_NIST_P_521: - case PGP_CURVE_BP512: - return PGP_HASH_SHA512; - default: - return PGP_HASH_UNKNOWN; - } -} diff --git a/src/lib/crypto/ecdsa_ossl.cpp b/src/lib/crypto/ecdsa_ossl.cpp index d0e1f8e1bb..35738d5f6b 100644 --- a/src/lib/crypto/ecdsa_ossl.cpp +++ b/src/lib/crypto/ecdsa_ossl.cpp @@ -170,21 +170,3 @@ ecdsa_verify(const pgp_ec_signature_t *sig, return ret; } -pgp_hash_alg_t -ecdsa_get_min_hash(pgp_curve_t curve) -{ - switch (curve) { - case PGP_CURVE_NIST_P_256: - case PGP_CURVE_BP256: - case PGP_CURVE_P256K1: - return PGP_HASH_SHA256; - case PGP_CURVE_NIST_P_384: - case PGP_CURVE_BP384: - return PGP_HASH_SHA384; - case PGP_CURVE_NIST_P_521: - case PGP_CURVE_BP512: - return PGP_HASH_SHA512; - default: - return PGP_HASH_UNKNOWN; - } -}