Skip to content

Commit ee4fb9c

Browse files
committed
Add SM Cipher certificate test header: certs_test_sm.h
1 parent 2a1df11 commit ee4fb9c

File tree

3 files changed

+543
-8
lines changed

3 files changed

+543
-8
lines changed

gencertbuf.pl

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
# ---- SCRIPT SETTINGS -------------------------------------------------------
1414

1515
# output C header file to write cert/key buffers to
16-
my $outputFile = "./wolfssl/certs_test.h";
16+
my $outputFile = "./wolfssl/certs_test.h";
17+
my $outputFileSM = "./wolfssl/certs_test_sm.h";
1718

1819
# ecc keys and certs to be converted
1920
# Used with HAVE_ECC && USE_CERT_BUFFERS_256
@@ -109,6 +110,23 @@
109110
[ "./certs/dh4096.der", "dh_key_der_4096" ],
110111
);
111112

113+
# SM ciphers in certs/sm2
114+
my @fileList_sm2 = (
115+
[ "./certs/sm2/ca-sm2.der", "ca_sm2" ],
116+
[ "./certs/sm2/ca-sm2-key.der", "ca_sm2_key" ],
117+
[ "./certs/sm2/ca-sm2-priv.der", "ca_sm2_priv" ],
118+
[ "./certs/sm2/client-sm2.der", "client_sm2" ],
119+
[ "./certs/sm2/client-sm2-key.der", "client_sm2_key" ],
120+
[ "./certs/sm2/client-sm2-priv.der", "client_sm2_priv" ],
121+
[ "./certs/sm2/root-sm2.der", "root_sm2" ],
122+
[ "./certs/sm2/root-sm2-key.der", "root_sm2_key" ],
123+
[ "./certs/sm2/root-sm2-priv.der", "root_sm2_priv" ],
124+
[ "./certs/sm2/server-sm2.der", "server_sm2" ],
125+
[ "./certs/sm2/server-sm2-key.der", "server_sm2_key" ],
126+
[ "./certs/sm2/server-sm2-priv.der", "server_sm2_priv" ],
127+
);
128+
129+
112130
#Falcon Post-Quantum Keys
113131
#Used with HAVE_PQC
114132
my @fileList_falcon = (
@@ -137,6 +155,7 @@
137155
my $num_2048 = @fileList_2048;
138156
my $num_3072 = @fileList_3072;
139157
my $num_4096 = @fileList_4096;
158+
my $num_sm2 = @fileList_sm2;
140159
my $num_falcon = @fileList_falcon;
141160
my $num_sphincs = @fileList_sphincs;
142161

@@ -2202,9 +2221,43 @@
22022221
# close certs_test.h file
22032222
close OUT_FILE or die $!;
22042223

2224+
#---------------------------------------------------------------------------
2225+
# open our output file, "+>" creates and/or truncates
2226+
open OUT_FILE_SM, "+>", $outputFileSM or die $!;
2227+
2228+
print OUT_FILE_SM "/* certs_test_sm.h */\n";
2229+
print OUT_FILE_SM "/* This file was generated using: ./gencertbuf.pl */\n\n";
2230+
print OUT_FILE_SM "#ifndef WOLFSSL_CERTS_TEST_SM_H\n";
2231+
print OUT_FILE_SM "#define WOLFSSL_CERTS_TEST_SM_H\n\n";
2232+
print OUT_FILE_SM "#if defined(WOLFSSL_SM2) || defined(WOLFSSL_SM3) || defined(WOLFSSL_SM4)\n\n";
2233+
2234+
# convert and print SM2 certs/keys
2235+
for (my $i = 0; $i < $num_sm2; $i++) {
2236+
2237+
my $fname = $fileList_sm2[$i][0];
2238+
my $sname = $fileList_sm2[$i][1];
2239+
2240+
print OUT_FILE_SM "/* $fname */\n";
2241+
print OUT_FILE_SM "static const unsigned char $sname\[] =\n";
2242+
print OUT_FILE_SM "{\n";
2243+
file_to_hex($fname, \*OUT_FILE_SM);
2244+
print OUT_FILE_SM "};\n";
2245+
# In C89/C90 (which Watcom generally defaults to), sizeof must be a
2246+
# compile-time constant expression when used in a static initializer.
2247+
# So don't use `static const int sizeof_` here:
2248+
print OUT_FILE_SM "#define sizeof_$sname (sizeof($sname))\n\n";
2249+
}
2250+
2251+
print OUT_FILE_SM "#endif /* WOLFSSL_SM2 || WOLFSSL_SM3 || WOLFSSL_SM4 */\n";
2252+
print OUT_FILE_SM "#endif /* WOLFSSL_CERTS_TEST_SM_H */\n";
2253+
2254+
# close certs_test_sm.h file
2255+
close OUT_FILE_SM or die $!;
2256+
22052257
# print file as hex, comma-separated, as needed by C buffer
22062258
sub file_to_hex {
2207-
my $fileName = $_[0];
2259+
my ($fileName, $out_fh) = @_;
2260+
$out_fh //= \*OUT_FILE; # default handle
22082261

22092262
open my $fp, "<", $fileName or die $!;
22102263
binmode($fp);
@@ -2215,26 +2268,27 @@ sub file_to_hex {
22152268
for (my $i = 0, my $j = 1; $i < $fileLen; $i++, $j++)
22162269
{
22172270
if ($j == 1) {
2218-
print OUT_FILE " ";
2271+
print {$out_fh} " ";
22192272
}
22202273
if ($j != 1) {
2221-
print OUT_FILE " ";
2274+
print {$out_fh} " ";
22222275
}
22232276
read($fp, $byte, 1) or die "Error reading $fileName";
22242277
my $output = sprintf("0x%02X", ord($byte));
2225-
print OUT_FILE $output;
2278+
print {$out_fh} $output;
22262279

22272280
if ($i != ($fileLen - 1)) {
2228-
print OUT_FILE ",";
2281+
print {$out_fh} ",";
22292282
}
22302283

22312284
if ($j == 10) {
22322285
$j = 0;
2233-
print OUT_FILE "\n";
2286+
print {$out_fh} "\n";
22342287
}
22352288
}
22362289

2237-
print OUT_FILE "\n";
2290+
print {$out_fh} "\n";
22382291

22392292
close($fp);
22402293
}
2294+

0 commit comments

Comments
 (0)