gencertbuf.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #!/usr/bin/perl
  2. # gencertbuf.pl
  3. # version 1.1
  4. # Updated 07/01/2014
  5. #
  6. # Copyright (C) 2006-2015 wolfSSL Inc.
  7. #
  8. use strict;
  9. use warnings;
  10. # ---- SCRIPT SETTINGS -------------------------------------------------------
  11. # output C header file to write cert/key buffers to
  12. my $outputFile = "./wolfssl/certs_test.h";
  13. # ecc keys and certs to be converted
  14. # Used with HAVE_ECC && USE_CERT_BUFFERS_256
  15. my @fileList_ecc = (
  16. [ "./certs/ecc-client-key.der", "ecc_clikey_der_256" ],
  17. [ "./certs/ecc-client-keyPub.der", "ecc_clikeypub_der_256" ],
  18. [ "./certs/client-ecc-cert.der", "cliecc_cert_der_256" ],
  19. [ "./certs/ecc-key.der", "ecc_key_der_256" ],
  20. [ "./certs/ecc-keyPub.der", "ecc_key_pub_der_256" ],
  21. [ "./certs/server-ecc-comp.der", "serv_ecc_comp_der_256" ],
  22. [ "./certs/server-ecc-rsa.der", "serv_ecc_rsa_der_256" ],
  23. [ "./certs/server-ecc.der", "serv_ecc_der_256" ],
  24. [ "./certs/ca-ecc-key.der", "ca_ecc_key_der_256" ],
  25. [ "./certs/ca-ecc-cert.der", "ca_ecc_cert_der_256" ],
  26. [ "./certs/ca-ecc384-key.der", "ca_ecc_key_der_384" ],
  27. [ "./certs/ca-ecc384-cert.der", "ca_ecc_cert_der_384" ]
  28. );
  29. # ed25519 keys and certs
  30. # Used with HAVE_ED25519 define.
  31. my @fileList_ed = (
  32. [ "./certs/ed25519/server-ed25519.der", "server_ed25519_cert" ],
  33. [ "./certs/ed25519/server-ed25519-key.der", "server_ed25519_key" ],
  34. [ "./certs/ed25519/ca-ed25519.der", "ca_ed25519_cert" ],
  35. [ "./certs/ed25519/client-ed25519.der", "client_ed25519_cert" ],
  36. [ "./certs/ed25519/client-ed25519-key.der", "client_ed25519_key" ]
  37. );
  38. # 1024-bit certs/keys to be converted
  39. # Used with USE_CERT_BUFFERS_1024 define.
  40. my @fileList_1024 = (
  41. [ "./certs/1024/client-key.der", "client_key_der_1024" ],
  42. [ "./certs/1024/client-keyPub.der", "client_keypub_der_1024" ],
  43. [ "./certs/1024/client-cert.der", "client_cert_der_1024" ],
  44. [ "./certs/1024/dh1024.der", "dh_key_der_1024" ],
  45. [ "./certs/1024/dsa1024.der", "dsa_key_der_1024" ],
  46. [ "./certs/1024/rsa1024.der", "rsa_key_der_1024" ],
  47. [ "./certs/1024/ca-key.der", "ca_key_der_1024"],
  48. [ "./certs/1024/ca-cert.der", "ca_cert_der_1024" ],
  49. [ "./certs/1024/server-key.der", "server_key_der_1024" ],
  50. [ "./certs/1024/server-cert.der", "server_cert_der_1024" ]
  51. );
  52. # 2048-bit certs/keys to be converted
  53. # Used with USE_CERT_BUFFERS_2048 define.
  54. my @fileList_2048 = (
  55. [ "./certs/client-key.der", "client_key_der_2048" ],
  56. [ "./certs/client-keyPub.der", "client_keypub_der_2048" ],
  57. [ "./certs/client-cert.der", "client_cert_der_2048" ],
  58. [ "./certs/dh2048.der", "dh_key_der_2048" ],
  59. [ "./certs/dh-pubkey-2048.der", "dh_pub_key_der_2048" ],
  60. [ "./certs/dsa-pubkey-2048.der", "dsa_pub_key_der_2048" ],
  61. [ "./certs/dsa2048.der", "dsa_key_der_2048" ],
  62. [ "./certs/rsa2048.der", "rsa_key_der_2048" ],
  63. [ "./certs/ca-key.der", "ca_key_der_2048" ],
  64. [ "./certs/ca-cert.der", "ca_cert_der_2048" ],
  65. [ "./certs/ca-cert-chain.der", "ca_cert_chain_der" ],
  66. [ "./certs/server-key.der", "server_key_der_2048" ],
  67. [ "./certs/server-cert.der", "server_cert_der_2048" ]
  68. );
  69. # 3072-bit certs/keys to be converted
  70. # Used with USE_CERT_BUFFERS_3072 define.
  71. my @fileList_3072 = (
  72. [ "./certs/dh3072.der", "dh_key_der_3072" ],
  73. [ "./certs/dsa3072.der", "dsa_key_der_3072" ],
  74. [ "./certs/rsa3072.der", "rsa_key_der_3072" ],
  75. [ "./certs/3072/client-key.der", "client_key_der_3072" ],
  76. [ "./certs/3072/client-keyPub.der", "client_keypub_der_3072" ],
  77. [ "./certs/3072/client-cert.der", "client_cert_der_3072" ],
  78. );
  79. # 4096-bit certs/keys to be converted
  80. # Used with USE_CERT_BUFFERS_4096 define.
  81. my @fileList_4096 = (
  82. [ "./certs/4096/client-key.der", "client_key_der_4096" ],
  83. [ "./certs/4096/client-keyPub.der", "client_keypub_der_4096" ],
  84. [ "./certs/4096/client-cert.der", "client_cert_der_4096" ],
  85. [ "./certs/dh4096.der", "dh_key_der_4096" ],
  86. );
  87. #Falcon Post-Quantum Keys
  88. #Used with HAVE_PQC
  89. my @fileList_falcon = (
  90. ["certs/falcon/bench_falcon_level1_key.der", "bench_falcon_level1_key" ],
  91. ["certs/falcon/bench_falcon_level5_key.der", "bench_falcon_level5_key" ],
  92. );
  93. #Dilithium Post-Quantum Keys
  94. #Used with HAVE_PQC
  95. my @fileList_dilithium = (
  96. ["certs/dilithium/bench_dilithium_level2_key.der", "bench_dilithium_level2_key" ],
  97. ["certs/dilithium/bench_dilithium_level3_key.der", "bench_dilithium_level3_key" ],
  98. ["certs/dilithium/bench_dilithium_level5_key.der", "bench_dilithium_level5_key" ],
  99. ["certs/dilithium/bench_dilithium_aes_level2_key.der", "bench_dilithium_aes_level2_key" ],
  100. ["certs/dilithium/bench_dilithium_aes_level3_key.der", "bench_dilithium_aes_level3_key" ],
  101. ["certs/dilithium/bench_dilithium_aes_level5_key.der", "bench_dilithium_aes_level5_key" ],
  102. );
  103. #Sphincs+ Post-Quantum Keys
  104. #Used with HAVE_PQC
  105. my @fileList_sphincs = (
  106. ["certs/sphincs/bench_sphincs_fast_level1_key.der", "bench_sphincs_fast_level1_key" ],
  107. ["certs/sphincs/bench_sphincs_fast_level3_key.der", "bench_sphincs_fast_level3_key" ],
  108. ["certs/sphincs/bench_sphincs_fast_level5_key.der", "bench_sphincs_fast_level5_key" ],
  109. ["certs/sphincs/bench_sphincs_small_level1_key.der", "bench_sphincs_small_level1_key" ],
  110. ["certs/sphincs/bench_sphincs_small_level3_key.der", "bench_sphincs_small_level3_key" ],
  111. ["certs/sphincs/bench_sphincs_small_level5_key.der", "bench_sphincs_small_level5_key" ],
  112. );
  113. # ----------------------------------------------------------------------------
  114. my $num_ecc = @fileList_ecc;
  115. my $num_ed = @fileList_ed;
  116. my $num_1024 = @fileList_1024;
  117. my $num_2048 = @fileList_2048;
  118. my $num_3072 = @fileList_3072;
  119. my $num_4096 = @fileList_4096;
  120. my $num_falcon = @fileList_falcon;
  121. my $num_dilithium = @fileList_dilithium;
  122. my $num_sphincs = @fileList_sphincs;
  123. # open our output file, "+>" creates and/or truncates
  124. open OUT_FILE, "+>", $outputFile or die $!;
  125. print OUT_FILE "/* certs_test.h */\n\n";
  126. print OUT_FILE "#ifndef WOLFSSL_CERTS_TEST_H\n";
  127. print OUT_FILE "#define WOLFSSL_CERTS_TEST_H\n\n";
  128. # convert and print 1024-bit cert/keys
  129. print OUT_FILE "#ifdef USE_CERT_BUFFERS_1024\n\n";
  130. for (my $i = 0; $i < $num_1024; $i++) {
  131. my $fname = $fileList_1024[$i][0];
  132. my $sname = $fileList_1024[$i][1];
  133. print OUT_FILE "/* $fname, 1024-bit */\n";
  134. print OUT_FILE "static const unsigned char $sname\[] =\n";
  135. print OUT_FILE "{\n";
  136. file_to_hex($fname);
  137. print OUT_FILE "};\n";
  138. print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
  139. }
  140. print OUT_FILE "#endif /* USE_CERT_BUFFERS_1024 */\n\n";
  141. # convert and print 2048-bit certs/keys
  142. print OUT_FILE "#ifdef USE_CERT_BUFFERS_2048\n\n";
  143. for (my $i = 0; $i < $num_2048; $i++) {
  144. my $fname = $fileList_2048[$i][0];
  145. my $sname = $fileList_2048[$i][1];
  146. print OUT_FILE "/* $fname, 2048-bit */\n";
  147. print OUT_FILE "static const unsigned char $sname\[] =\n";
  148. print OUT_FILE "{\n";
  149. file_to_hex($fname);
  150. print OUT_FILE "};\n";
  151. print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
  152. }
  153. print OUT_FILE "#endif /* USE_CERT_BUFFERS_2048 */\n\n";
  154. # convert and print 3072-bit certs/keys
  155. print OUT_FILE "#ifdef USE_CERT_BUFFERS_3072\n\n";
  156. for (my $i = 0; $i < $num_3072; $i++) {
  157. my $fname = $fileList_3072[$i][0];
  158. my $sname = $fileList_3072[$i][1];
  159. print OUT_FILE "/* $fname, 3072-bit */\n";
  160. print OUT_FILE "static const unsigned char $sname\[] =\n";
  161. print OUT_FILE "{\n";
  162. file_to_hex($fname);
  163. print OUT_FILE "};\n";
  164. print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
  165. }
  166. print OUT_FILE "#endif /* USE_CERT_BUFFERS_3072 */\n\n";
  167. # convert and print 4096-bit certs/keys
  168. print OUT_FILE "#ifdef USE_CERT_BUFFERS_4096\n\n";
  169. for (my $i = 0; $i < $num_4096; $i++) {
  170. my $fname = $fileList_4096[$i][0];
  171. my $sname = $fileList_4096[$i][1];
  172. print OUT_FILE "/* $fname, 4096-bit */\n";
  173. print OUT_FILE "static const unsigned char $sname\[] =\n";
  174. print OUT_FILE "{\n";
  175. file_to_hex($fname);
  176. print OUT_FILE "};\n";
  177. print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
  178. }
  179. print OUT_FILE "#endif /* USE_CERT_BUFFERS_4096 */\n\n";
  180. # convert and print falcon keys
  181. print OUT_FILE "#if defined(HAVE_PQC) && defined(HAVE_FALCON)\n\n";
  182. for (my $i = 0; $i < $num_falcon; $i++) {
  183. my $fname = $fileList_falcon[$i][0];
  184. my $sname = $fileList_falcon[$i][1];
  185. print OUT_FILE "/* $fname */\n";
  186. print OUT_FILE "static const unsigned char $sname\[] =\n";
  187. print OUT_FILE "{\n";
  188. file_to_hex($fname);
  189. print OUT_FILE "};\n";
  190. print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
  191. }
  192. print OUT_FILE "#endif /* HAVE_PQC && HAVE_FALCON */\n\n";
  193. # convert and print dilithium keys
  194. print OUT_FILE "#if defined (HAVE_PQC) && defined(HAVE_DILITHIUM)\n\n";
  195. for (my $i = 0; $i < $num_dilithium; $i++) {
  196. my $fname = $fileList_dilithium[$i][0];
  197. my $sname = $fileList_dilithium[$i][1];
  198. print OUT_FILE "/* $fname */\n";
  199. print OUT_FILE "static const unsigned char $sname\[] =\n";
  200. print OUT_FILE "{\n";
  201. file_to_hex($fname);
  202. print OUT_FILE "};\n";
  203. print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
  204. }
  205. print OUT_FILE "#endif /* HAVE_PQC && HAVE_DILITHIUM */\n\n";
  206. # convert and print sphincs keys
  207. print OUT_FILE "#if defined(HAVE_PQC) && defined(HAVE_SPHINCS)\n\n";
  208. for (my $i = 0; $i < $num_sphincs; $i++) {
  209. my $fname = $fileList_sphincs[$i][0];
  210. my $sname = $fileList_sphincs[$i][1];
  211. print OUT_FILE "/* $fname */\n";
  212. print OUT_FILE "static const unsigned char $sname\[] =\n";
  213. print OUT_FILE "{\n";
  214. file_to_hex($fname);
  215. print OUT_FILE "};\n";
  216. print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
  217. }
  218. print OUT_FILE "#endif /* HAVE_PQC && HAVE_SPHINCS */\n\n";
  219. # convert and print 256-bit cert/keys
  220. print OUT_FILE "#if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)\n\n";
  221. for (my $i = 0; $i < $num_ecc; $i++) {
  222. my $fname = $fileList_ecc[$i][0];
  223. my $sname = $fileList_ecc[$i][1];
  224. print OUT_FILE "/* $fname, ECC */\n";
  225. print OUT_FILE "static const unsigned char $sname\[] =\n";
  226. print OUT_FILE "{\n";
  227. file_to_hex($fname);
  228. print OUT_FILE "};\n";
  229. print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
  230. }
  231. print OUT_FILE "#endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */\n\n";
  232. print OUT_FILE "/* dh1024 p */
  233. static const unsigned char dh_p[] =
  234. {
  235. 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
  236. 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
  237. 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
  238. 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
  239. 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
  240. 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
  241. 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
  242. 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
  243. 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
  244. 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
  245. 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
  246. };
  247. /* dh1024 g */
  248. static const unsigned char dh_g[] =
  249. {
  250. 0x02,
  251. };\n\n";
  252. # convert and print ed25519 cert/keys
  253. print OUT_FILE "#if defined(HAVE_ED25519)\n\n";
  254. for (my $i = 0; $i < $num_ed; $i++) {
  255. my $fname = $fileList_ed[$i][0];
  256. my $sname = $fileList_ed[$i][1];
  257. print OUT_FILE "/* $fname, ED25519 */\n";
  258. print OUT_FILE "static const unsigned char $sname\[] =\n";
  259. print OUT_FILE "{\n";
  260. file_to_hex($fname);
  261. print OUT_FILE "};\n";
  262. print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
  263. }
  264. print OUT_FILE "#endif /* HAVE_ED25519 */\n\n";
  265. print OUT_FILE "#endif /* WOLFSSL_CERTS_TEST_H */\n\n";
  266. # close certs_test.h file
  267. close OUT_FILE or die $!;
  268. # print file as hex, comma-separated, as needed by C buffer
  269. sub file_to_hex {
  270. my $fileName = $_[0];
  271. open my $fp, "<", $fileName or die $!;
  272. binmode($fp);
  273. my $fileLen = -s $fileName;
  274. my $byte;
  275. for (my $i = 0, my $j = 1; $i < $fileLen; $i++, $j++)
  276. {
  277. if ($j == 1) {
  278. print OUT_FILE " ";
  279. }
  280. if ($j != 1) {
  281. print OUT_FILE " ";
  282. }
  283. read($fp, $byte, 1) or die "Error reading $fileName";
  284. my $output = sprintf("0x%02X", ord($byte));
  285. print OUT_FILE $output;
  286. if ($i != ($fileLen - 1)) {
  287. print OUT_FILE ",";
  288. }
  289. if ($j == 10) {
  290. $j = 0;
  291. print OUT_FILE "\n";
  292. }
  293. }
  294. print OUT_FILE "\n";
  295. close($fp);
  296. }