xref: /freebsd-src/crypto/openssl/test/recipes/15-test_gensm2.t (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 2023-2024 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubertuse strict;
11*e0c4386eSCy Schubertuse warnings;
12*e0c4386eSCy Schubert
13*e0c4386eSCy Schubertuse File::Spec;
14*e0c4386eSCy Schubertuse OpenSSL::Test qw(:DEFAULT pipe);
15*e0c4386eSCy Schubertuse OpenSSL::Test::Utils;
16*e0c4386eSCy Schubert
17*e0c4386eSCy Schubert# These are special key generation tests for SM2 keys specifically,
18*e0c4386eSCy Schubert# as they could be said to be a bit special in their encoding.
19*e0c4386eSCy Schubert# This is an auxilliary test to 15-test_genec.t
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubertsetup("test_gensm2");
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubertplan skip_all => "This test is unsupported in a no-sm2 build"
24*e0c4386eSCy Schubert    if disabled("sm2");
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubertplan tests => 2;
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubert# According to the example in  GM/T 0015-2012, appendix D.2,
29*e0c4386eSCy Schubert# generating an EC key with the named SM2 curve or generating
30*e0c4386eSCy Schubert# an SM2 key should end up with the same encoding (apart from
31*e0c4386eSCy Schubert# key private key field itself).  This regular expressions
32*e0c4386eSCy Schubert# shows us what 'openssl asn1parse' should display.
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubertmy $sm2_re = qr|
35*e0c4386eSCy Schubert   ^
36*e0c4386eSCy Schubert   .*?\Qcons: SEQUENCE\E\s+?\R
37*e0c4386eSCy Schubert   .*?\Qprim:  INTEGER           :00\E\R
38*e0c4386eSCy Schubert   .*?\Qcons:  SEQUENCE\E\s+?\R
39*e0c4386eSCy Schubert   .*?\Qprim:   OBJECT            :id-ecPublicKey\E\R
40*e0c4386eSCy Schubert   .*?\Qprim:   OBJECT            :sm2\E\R
41*e0c4386eSCy Schubert   .*?\Qprim:  OCTET STRING      [HEX DUMP]:\E
42*e0c4386eSCy Schubert   |mx;
43*e0c4386eSCy Schubert
44*e0c4386eSCy Schubertmy $cmd_genec = app([ 'openssl', 'genpkey',
45*e0c4386eSCy Schubert                      '-algorithm', 'EC',
46*e0c4386eSCy Schubert                      '-pkeyopt', 'ec_paramgen_curve:SM2',
47*e0c4386eSCy Schubert                      '-pkeyopt', 'ec_param_enc:named_curve' ]);
48*e0c4386eSCy Schubertmy $cmd_gensm2 = app([ 'openssl', 'genpkey', '-algorithm', 'SM2' ]);
49*e0c4386eSCy Schubertmy $cmd_asn1parse = app([ 'openssl', 'asn1parse', '-i' ]);
50*e0c4386eSCy Schubert
51*e0c4386eSCy Schubertmy $result_ec = join("", run(pipe($cmd_genec, $cmd_asn1parse),
52*e0c4386eSCy Schubert                             capture => 1));
53*e0c4386eSCy Schubert
54*e0c4386eSCy Schubertlike($result_ec, $sm2_re,
55*e0c4386eSCy Schubert     "Check that 'genpkey -algorithm EC' resulted in a correctly encoded SM2 key");
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubertmy $result_sm2 = join("", run(pipe($cmd_gensm2, $cmd_asn1parse),
58*e0c4386eSCy Schubert                              capture => 1));
59*e0c4386eSCy Schubert
60*e0c4386eSCy Schubertlike($result_sm2, $sm2_re,
61*e0c4386eSCy Schubert     "Check that 'genpkey -algorithm SM2' resulted in a correctly encoded SM2 key");
62