summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2024-01-04 12:42:05 +0100
committerTomas Mraz <tomas@openssl.org>2024-01-12 11:08:37 +0100
commit29463f17f2f7978e67b74e3f76bad1c126d34bed (patch)
treeaf90bb56f9a2ff2024a9fb38adaeb01eaf1b9608
parent7c93d2eb8c29f0c82e79ad1a147507f4d58de816 (diff)
Add test/recipes/15-test_gensm2.t, to test SM2 key generation results
Reviewed-by: Shane Lontis <shane.lontis@oracle.com> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from https://github.com/openssl/openssl/pull/22529) (cherry picked from commit d4d9b57530b2ecdca6b4263b5841b42c820e5275)
-rw-r--r--test/recipes/15-test_gensm2.t61
1 files changed, 61 insertions, 0 deletions
diff --git a/test/recipes/15-test_gensm2.t b/test/recipes/15-test_gensm2.t
new file mode 100644
index 0000000000..1c4c01a248
--- /dev/null
+++ b/test/recipes/15-test_gensm2.t
@@ -0,0 +1,61 @@
+#! /usr/bin/env perl
+# Copyright 2023 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
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+
+use strict;
+use warnings;
+
+use File::Spec;
+use OpenSSL::Test qw(:DEFAULT pipe);
+use OpenSSL::Test::Utils;
+
+# These are special key generation tests for SM2 keys specifically,
+# as they could be said to be a bit special in their encoding.
+# This is an auxilliary test to 15-test_genec.t
+
+setup("test_gensm2");
+
+plan skip_all => "This test is unsupported in a no-sm2 build"
+ if disabled("sm2");
+
+plan tests => 2;
+
+# According to the example in GM/T 0015-2012, appendix D.2,
+# generating an EC key with the named SM2 curve or generating
+# an SM2 key should end up with the same encoding (apart from
+# key private key field itself). This regular expressions
+# shows us what 'openssl asn1parse' should display.
+
+my $sm2_re = qr|
+ ^
+ .*?\Qcons: SEQUENCE\E\s+?\R
+ .*?\Qprim: INTEGER :00\E\R
+ .*?\Qcons: SEQUENCE\E\s+?\R
+ .*?\Qprim: OBJECT :id-ecPublicKey\E\R
+ .*?\Qprim: OBJECT :sm2\E\R
+ .*?\Qprim: OCTET STRING [HEX DUMP]:\E
+ |mx;
+
+my $cmd_genec = app([ 'openssl', 'genpkey',
+ '-algorithm', 'EC',
+ '-pkeyopt', 'ec_paramgen_curve:SM2',
+ '-pkeyopt', 'ec_param_enc:named_curve' ]);
+my $cmd_gensm2 = app([ 'openssl', 'genpkey', '-algorithm', 'SM2' ]);
+my $cmd_asn1parse = app([ 'openssl', 'asn1parse', '-i' ]);
+
+my $result_ec = join("", run(pipe($cmd_genec, $cmd_asn1parse),
+ capture => 1));
+
+like($result_ec, $sm2_re,
+ "Check that 'genpkey -algorithm EC' resulted in a correctly encoded SM2 key");
+
+my $result_sm2 = join("", run(pipe($cmd_gensm2, $cmd_asn1parse),
+ capture => 1));
+
+like($result_sm2, $sm2_re,
+ "Check that 'genpkey -algorithm SM2' resulted in a correctly encoded SM2 key");