Skip to content

Commit 47fee37

Browse files
desvxxmaxirmx
authored andcommitted
Remove duplicate implementations of some DSA/ECDSA functions
1 parent 112fcf4 commit 47fee37

File tree

7 files changed

+113
-98
lines changed

7 files changed

+113
-98
lines changed

src/lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ configure_file(config.h.in config.h)
242242
if(CRYPTO_BACKEND_OPENSSL)
243243
set(CRYPTO_SOURCES
244244
crypto/bn_ossl.cpp
245+
crypto/dsa_common.cpp
245246
crypto/dsa_ossl.cpp
246247
crypto/ec_curves.cpp
247248
crypto/ec_ossl.cpp
@@ -272,6 +273,7 @@ if(CRYPTO_BACKEND_OPENSSL)
272273
elseif(CRYPTO_BACKEND_BOTAN)
273274
set(CRYPTO_SOURCES
274275
crypto/bn.cpp
276+
crypto/dsa_common.cpp
275277
crypto/dsa.cpp
276278
crypto/ec_curves.cpp
277279
crypto/ec.cpp

src/lib/crypto/dsa.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@
8282
#include "dsa.h"
8383
#include "bn.h"
8484
#include "utils.h"
85-
86-
#define DSA_MAX_Q_BITLEN 256
85+
#include "dsa_common.h"
8786

8887
rnp_result_t
8988
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)
347346
botan_pubkey_destroy(key_pub);
348347
return ret;
349348
}
350-
351-
pgp_hash_alg_t
352-
dsa_get_min_hash(size_t qsize)
353-
{
354-
/*
355-
* I'm using _broken_ SHA1 here only because
356-
* some old implementations may not understand keys created
357-
* with other hashes. If you're sure we don't have to support
358-
* such implementations, please be my guest and remove it.
359-
*/
360-
return (qsize < 160) ? PGP_HASH_UNKNOWN :
361-
(qsize == 160) ? PGP_HASH_SHA1 :
362-
(qsize <= 224) ? PGP_HASH_SHA224 :
363-
(qsize <= 256) ? PGP_HASH_SHA256 :
364-
(qsize <= 384) ? PGP_HASH_SHA384 :
365-
(qsize <= 512) ? PGP_HASH_SHA512
366-
/*(qsize>512)*/ :
367-
PGP_HASH_UNKNOWN;
368-
}
369-
370-
size_t
371-
dsa_choose_qsize_by_psize(size_t psize)
372-
{
373-
return (psize == 1024) ? 160 :
374-
(psize <= 2047) ? 224 :
375-
(psize <= 3072) ? DSA_MAX_Q_BITLEN :
376-
0;
377-
}

src/lib/crypto/dsa_common.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*-
2+
* Copyright (c) 2021-2024 Ribose Inc.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
18+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#include "crypto.h"
28+
#include "config.h"
29+
#include "defaults.h"
30+
#include "dsa_common.h"
31+
32+
pgp_hash_alg_t
33+
dsa_get_min_hash(size_t qsize)
34+
{
35+
/*
36+
* I'm using _broken_ SHA1 here only because
37+
* some old implementations may not understand keys created
38+
* with other hashes. If you're sure we don't have to support
39+
* such implementations, please be my guest and remove it.
40+
*/
41+
return (qsize < 160) ? PGP_HASH_UNKNOWN :
42+
(qsize == 160) ? PGP_HASH_SHA1 :
43+
(qsize <= 224) ? PGP_HASH_SHA224 :
44+
(qsize <= 256) ? PGP_HASH_SHA256 :
45+
(qsize <= 384) ? PGP_HASH_SHA384 :
46+
(qsize <= 512) ? PGP_HASH_SHA512
47+
/*(qsize>512)*/ :
48+
PGP_HASH_UNKNOWN;
49+
}
50+
51+
size_t
52+
dsa_choose_qsize_by_psize(size_t psize)
53+
{
54+
return (psize == 1024) ? 160 :
55+
(psize <= 2047) ? 224 :
56+
(psize <= 3072) ? DSA_MAX_Q_BITLEN :
57+
0;
58+
}
59+
60+
pgp_hash_alg_t
61+
ecdsa_get_min_hash(pgp_curve_t curve)
62+
{
63+
switch (curve) {
64+
case PGP_CURVE_NIST_P_256:
65+
case PGP_CURVE_BP256:
66+
case PGP_CURVE_P256K1:
67+
return PGP_HASH_SHA256;
68+
case PGP_CURVE_NIST_P_384:
69+
case PGP_CURVE_BP384:
70+
return PGP_HASH_SHA384;
71+
case PGP_CURVE_NIST_P_521:
72+
case PGP_CURVE_BP512:
73+
return PGP_HASH_SHA512;
74+
default:
75+
return PGP_HASH_UNKNOWN;
76+
}
77+
}

src/lib/crypto/dsa_common.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*-
2+
* Copyright (c) 2021-2024 Ribose Inc.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
18+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#ifndef RNP_DSA_COMMON_H_
28+
#define RNP_DSA_COMMON_H_
29+
30+
#define DSA_MAX_Q_BITLEN 256
31+
32+
#endif

src/lib/crypto/dsa_ossl.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@
3535
#include <openssl/dh.h>
3636
#include <openssl/err.h>
3737
#include <openssl/evp.h>
38+
#include "dsa_common.h"
3839
#if defined(CRYPTO_BACKEND_OPENSSL3)
3940
#include <openssl/core_names.h>
4041
#include <openssl/param_build.h>
4142
#endif
4243

43-
#define DSA_MAX_Q_BITLEN 256
44-
4544
static bool
4645
dsa_decode_sig(const uint8_t *data, size_t len, pgp_dsa_signature_t &sig)
4746
{
@@ -403,31 +402,3 @@ dsa_generate(rnp::RNG *rng, pgp_dsa_key_t *key, size_t keylen, size_t qbits)
403402
EVP_PKEY_free(pkey);
404403
return ret;
405404
}
406-
407-
pgp_hash_alg_t
408-
dsa_get_min_hash(size_t qsize)
409-
{
410-
/*
411-
* I'm using _broken_ SHA1 here only because
412-
* some old implementations may not understand keys created
413-
* with other hashes. If you're sure we don't have to support
414-
* such implementations, please be my guest and remove it.
415-
*/
416-
return (qsize < 160) ? PGP_HASH_UNKNOWN :
417-
(qsize == 160) ? PGP_HASH_SHA1 :
418-
(qsize <= 224) ? PGP_HASH_SHA224 :
419-
(qsize <= 256) ? PGP_HASH_SHA256 :
420-
(qsize <= 384) ? PGP_HASH_SHA384 :
421-
(qsize <= 512) ? PGP_HASH_SHA512
422-
/*(qsize>512)*/ :
423-
PGP_HASH_UNKNOWN;
424-
}
425-
426-
size_t
427-
dsa_choose_qsize_by_psize(size_t psize)
428-
{
429-
return (psize == 1024) ? 160 :
430-
(psize <= 2047) ? 224 :
431-
(psize <= 3072) ? DSA_MAX_Q_BITLEN :
432-
0;
433-
}

src/lib/crypto/ecdsa.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,3 @@ ecdsa_verify(const pgp_ec_signature_t *sig,
245245
botan_pk_op_verify_destroy(verifier);
246246
return ret;
247247
}
248-
249-
pgp_hash_alg_t
250-
ecdsa_get_min_hash(pgp_curve_t curve)
251-
{
252-
switch (curve) {
253-
case PGP_CURVE_NIST_P_256:
254-
case PGP_CURVE_BP256:
255-
case PGP_CURVE_P256K1:
256-
return PGP_HASH_SHA256;
257-
case PGP_CURVE_NIST_P_384:
258-
case PGP_CURVE_BP384:
259-
return PGP_HASH_SHA384;
260-
case PGP_CURVE_NIST_P_521:
261-
case PGP_CURVE_BP512:
262-
return PGP_HASH_SHA512;
263-
default:
264-
return PGP_HASH_UNKNOWN;
265-
}
266-
}

src/lib/crypto/ecdsa_ossl.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,3 @@ ecdsa_verify(const pgp_ec_signature_t *sig,
169169
EVP_PKEY_free(evpkey);
170170
return ret;
171171
}
172-
173-
pgp_hash_alg_t
174-
ecdsa_get_min_hash(pgp_curve_t curve)
175-
{
176-
switch (curve) {
177-
case PGP_CURVE_NIST_P_256:
178-
case PGP_CURVE_BP256:
179-
case PGP_CURVE_P256K1:
180-
return PGP_HASH_SHA256;
181-
case PGP_CURVE_NIST_P_384:
182-
case PGP_CURVE_BP384:
183-
return PGP_HASH_SHA384;
184-
case PGP_CURVE_NIST_P_521:
185-
case PGP_CURVE_BP512:
186-
return PGP_HASH_SHA512;
187-
default:
188-
return PGP_HASH_UNKNOWN;
189-
}
190-
}

0 commit comments

Comments
 (0)