]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Add -in option to prime function to allow input from file for primality testing....
authorzriback <zacharythecdr@gmail.com>
Fri, 24 Jan 2025 06:03:35 +0000 (01:03 -0500)
committerViktor Dukhovni <openssl-users@dukhovni.org>
Tue, 29 Apr 2025 04:17:06 +0000 (14:17 +1000)
Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/26549)

apps/prime.c
doc/man1/openssl-prime.pod.in

index 23b7e462fc20d2b0d03212fa034f4cb7aad8a1ce..7ee1a71e26851c7dd49793e5b23ef65f66ed2fa5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004-2022 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2004-2025 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
 #include "progs.h"
 #include <openssl/bn.h>
 
+#define BUFSIZE 256
+
 typedef enum OPTION_choice {
     OPT_COMMON,
     OPT_HEX, OPT_GENERATE, OPT_BITS, OPT_SAFE, OPT_CHECKS,
-    OPT_PROV_ENUM
+    OPT_PROV_ENUM,
+    OPT_IN_FILE
 } OPTION_CHOICE;
 
 static int check_num(const char *s, const int is_hex)
@@ -43,9 +46,11 @@ const OPTIONS prime_options[] = {
     {"help", OPT_HELP, '-', "Display this summary"},
     {"bits", OPT_BITS, 'p', "Size of number in bits"},
     {"checks", OPT_CHECKS, 'p', "Number of checks"},
+    {"hex", OPT_HEX, '-',
+     "Enables hex format for output from prime generation or input to primality checking"},
+    {"in", OPT_IN_FILE, '-', "Provide file names containing numbers for primality checking"},
 
     OPT_SECTION("Output"),
-    {"hex", OPT_HEX, '-', "Hex output"},
     {"generate", OPT_GENERATE, '-', "Generate a prime"},
     {"safe", OPT_SAFE, '-',
      "When used with -generate, generate a safe prime"},
@@ -60,9 +65,11 @@ const OPTIONS prime_options[] = {
 int prime_main(int argc, char **argv)
 {
     BIGNUM *bn = NULL;
-    int hex = 0, generate = 0, bits = 0, safe = 0, ret = 1;
+    int hex = 0, generate = 0, bits = 0, safe = 0, ret = 1, in_file = 0;
     char *prog;
     OPTION_CHOICE o;
+    char *file_read_buf = NULL;
+    BIO *in = NULL;
 
     prog = opt_init(argc, argv, prime_options);
     while ((o = opt_next()) != OPT_EOF) {
@@ -96,6 +103,9 @@ opthelp:
             if (!opt_provider(o))
                 goto end;
             break;
+        case OPT_IN_FILE:
+            in_file = 1;
+            break;
         }
     }
 
@@ -134,13 +144,49 @@ opthelp:
         OPENSSL_free(s);
     } else {
         for ( ; *argv; argv++) {
-            int r = check_num(argv[0], hex);
+            char *check_val;
+            int total_read = 0;
+            int r;
+
+            if (!in_file) {
+                check_val = argv[0];
+            } else {
+                in = bio_open_default_quiet(argv[0], 'r', 0);
+                if (in == NULL) {
+                    BIO_printf(bio_err, "Error opening file %s\n", argv[0]);
+                    goto end;
+                }
+
+                int i;
+                file_read_buf = (char *)app_malloc(BUFSIZE, "File read buffer");
+                while (BIO_pending(in) || !BIO_eof(in)) {
+                    i = BIO_read(in, (char *)(file_read_buf + total_read), BUFSIZE);
+                    if (i < 0) {
+                        BIO_printf(bio_err, "Read error in %s\n", argv[0]);
+                        goto end;
+                    }
+                    if (i == 0)
+                        break;
+                    total_read += i;
+                    if (i == BUFSIZE)
+                        file_read_buf = (char *)realloc(file_read_buf, BUFSIZE + total_read);
+                }
+                
+                /* Trim trailing newline if present */
+                if (file_read_buf[total_read - 1] == '\n')
+                    file_read_buf[total_read - 1] = '\0';
+
+                check_val = file_read_buf;
+
+            }
+
+            r = check_num(check_val, hex);
 
             if (r)
-                r = hex ? BN_hex2bn(&bn, argv[0]) : BN_dec2bn(&bn, argv[0]);
+                r = hex ? BN_hex2bn(&bn, check_val) : BN_dec2bn(&bn, check_val);
 
             if (!r) {
-                BIO_printf(bio_err, "Failed to process value (%s)\n", argv[0]);
+                BIO_printf(bio_err, "Failed to process value (%s)\n", check_val);
                 goto end;
             }
 
@@ -151,13 +197,24 @@ opthelp:
                 goto end;
             }
             BIO_printf(bio_out, " (%s) %s prime\n",
-                       argv[0],
+                       check_val,
                        r == 1 ? "is" : "is not");
+
+            if (in_file) {
+                BIO_free(in);
+                OPENSSL_free(file_read_buf);
+                in = NULL;
+                file_read_buf = NULL;
+            }
         }
     }
 
     ret = 0;
  end:
     BN_free(bn);
+    if (in != NULL)
+        BIO_free(in);
+    if (file_read_buf != NULL)
+        OPENSSL_free(file_read_buf);
     return ret;
 }
index 7ed13bb4c3870e2823f8d88ca17079076cf36f0b..e128e4c0b0d2acb80aaedb14f5dea1cc18f31dd8 100644 (file)
@@ -10,6 +10,7 @@ openssl-prime - compute prime numbers
 B<openssl prime>
 [B<-help>]
 [B<-hex>]
+[B<-in>]
 [B<-generate>]
 [B<-bits> I<num>]
 [B<-safe>]
@@ -35,7 +36,11 @@ Display an option summary.
 
 =item B<-hex>
 
-Generate hex output.
+Enable hex format for output from prime generation or input to primality checking.
+
+=item B<-in>
+
+Provide file names containing numbers for primality checking.
 
 =item B<-generate>
 
@@ -60,7 +65,7 @@ This parameter is ignored.
 
 =head1 COPYRIGHT
 
-Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
+Copyright 2017-2025 The OpenSSL Project Authors. All Rights Reserved.
 
 Licensed under the Apache License 2.0 (the "License").  You may not use
 this file except in compliance with the License.  You can obtain a copy