Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate implementations of some DSA/ECDSA functions #2269

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
31 changes: 1 addition & 30 deletions src/lib/crypto/dsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
77 changes: 77 additions & 0 deletions src/lib/crypto/dsa_common.cpp
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 46 in src/lib/crypto/dsa_common.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/crypto/dsa_common.cpp#L45-L46

Added lines #L45 - L46 were not covered by tests
/*(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;
}
}
32 changes: 32 additions & 0 deletions src/lib/crypto/dsa_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*-
* 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.
*/

#ifndef RNP_DSA_COMMON_H_
#define RNP_DSA_COMMON_H_

#define DSA_MAX_Q_BITLEN 256

#endif
31 changes: 1 addition & 30 deletions src/lib/crypto/dsa_ossl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@
#include <openssl/dh.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include "dsa_common.h"
#if defined(CRYPTO_BACKEND_OPENSSL3)
#include <openssl/core_names.h>
#include <openssl/param_build.h>
#endif

#define DSA_MAX_Q_BITLEN 256

static bool
dsa_decode_sig(const uint8_t *data, size_t len, pgp_dsa_signature_t &sig)
{
Expand Down Expand Up @@ -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;
}
19 changes: 0 additions & 19 deletions src/lib/crypto/ecdsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 0 additions & 19 deletions src/lib/crypto/ecdsa_ossl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,3 @@ ecdsa_verify(const pgp_ec_signature_t *sig,
EVP_PKEY_free(evpkey);
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;
}
}