Skip to content

Commit 9c211b6

Browse files
committed
bn: use BN_check_prime() in OpenSSL::BN#prime{,_fasttest}?
In OpenSSL 3.0, BN_is_prime_ex() and BN_is_prime_fasttest_ex() are deprecated in favor of BN_check_prime().
1 parent 4e1d9fb commit 9c211b6

File tree

2 files changed

+22
-49
lines changed

2 files changed

+22
-49
lines changed

ext/openssl/extconf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def find_openssl_library
175175
have_func("ERR_get_error_all")
176176
have_func("TS_VERIFY_CTX_set_certs(NULL, NULL)", "openssl/ts.h")
177177
have_func("SSL_CTX_load_verify_file")
178+
have_func("BN_check_prime")
178179

179180
Logging::message "=== Checking done. ===\n"
180181

ext/openssl/ossl_bn.c

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,34 +1118,29 @@ ossl_bn_hash(VALUE self)
11181118
* bn.prime? => true | false
11191119
* bn.prime?(checks) => true | false
11201120
*
1121-
* Performs a Miller-Rabin probabilistic primality test with _checks_
1122-
* iterations. If _checks_ is not specified, a number of iterations is used
1123-
* that yields a false positive rate of at most 2^-80 for random input.
1121+
* Performs a Miller-Rabin probabilistic primality test for +bn+.
11241122
*
1125-
* === Parameters
1126-
* * _checks_ - integer
1123+
* <b>+checks+ parameter is deprecated in version 3.0.</b> It has no effect.
11271124
*/
11281125
static VALUE
11291126
ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
11301127
{
11311128
BIGNUM *bn;
1132-
VALUE vchecks;
1133-
int checks = BN_prime_checks;
1129+
int ret;
11341130

1135-
if (rb_scan_args(argc, argv, "01", &vchecks) == 1) {
1136-
checks = NUM2INT(vchecks);
1137-
}
1131+
rb_check_arity(argc, 0, 1);
11381132
GetBN(self, bn);
1139-
switch (BN_is_prime_ex(bn, checks, ossl_bn_ctx, NULL)) {
1140-
case 1:
1141-
return Qtrue;
1142-
case 0:
1143-
return Qfalse;
1144-
default:
1145-
ossl_raise(eBNError, NULL);
1146-
}
1147-
/* not reachable */
1148-
return Qnil;
1133+
1134+
#ifdef HAVE_BN_CHECK_PRIME
1135+
ret = BN_check_prime(bn, ossl_bn_ctx, NULL);
1136+
if (ret < 0)
1137+
ossl_raise(eBNError, "BN_check_prime");
1138+
#else
1139+
ret = BN_is_prime_fasttest_ex(bn, BN_prime_checks, ossl_bn_ctx, 1, NULL);
1140+
if (ret < 0)
1141+
ossl_raise(eBNError, "BN_is_prime_fasttest_ex");
1142+
#endif
1143+
return ret ? Qtrue : Qfalse;
11491144
}
11501145

11511146
/*
@@ -1154,40 +1149,17 @@ ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
11541149
* bn.prime_fasttest?(checks) => true | false
11551150
* bn.prime_fasttest?(checks, trial_div) => true | false
11561151
*
1157-
* Performs a Miller-Rabin primality test. This is same as #prime? except this
1158-
* first attempts trial divisions with some small primes.
1152+
* Performs a Miller-Rabin probabilistic primality test for +bn+.
11591153
*
1160-
* === Parameters
1161-
* * _checks_ - integer
1162-
* * _trial_div_ - boolean
1154+
* <b>Deprecated in version 3.0.</b> Use #prime? instead.
1155+
*
1156+
* +checks+ and +trial_div+ parameters no longer have any effect.
11631157
*/
11641158
static VALUE
11651159
ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
11661160
{
1167-
BIGNUM *bn;
1168-
VALUE vchecks, vtrivdiv;
1169-
int checks = BN_prime_checks, do_trial_division = 1;
1170-
1171-
rb_scan_args(argc, argv, "02", &vchecks, &vtrivdiv);
1172-
1173-
if (!NIL_P(vchecks)) {
1174-
checks = NUM2INT(vchecks);
1175-
}
1176-
GetBN(self, bn);
1177-
/* handle true/false */
1178-
if (vtrivdiv == Qfalse) {
1179-
do_trial_division = 0;
1180-
}
1181-
switch (BN_is_prime_fasttest_ex(bn, checks, ossl_bn_ctx, do_trial_division, NULL)) {
1182-
case 1:
1183-
return Qtrue;
1184-
case 0:
1185-
return Qfalse;
1186-
default:
1187-
ossl_raise(eBNError, NULL);
1188-
}
1189-
/* not reachable */
1190-
return Qnil;
1161+
rb_check_arity(argc, 0, 2);
1162+
return ossl_bn_is_prime(0, argv, self);
11911163
}
11921164

11931165
/*

0 commit comments

Comments
 (0)