{"id":3886,"date":"2022-12-20T17:28:20","date_gmt":"2022-12-20T20:28:20","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/sm2-man7\/"},"modified":"2022-12-20T17:28:20","modified_gmt":"2022-12-20T20:28:20","slug":"sm2-man7","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/sm2-man7\/","title":{"rendered":"SM2 (man7)"},"content":{"rendered":"<h1 align=\"center\">SM2<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">SM2 \u2212 Chinese SM2 signature and encryption algorithm support<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b><small>SM2<\/small><\/b> algorithm was first defined by the Chinese national standard <small>GM\/T 0003\u22122012<\/small> and was later standardized by <small>ISO<\/small> as <small>ISO\/IEC 14888. <b>SM2<\/b><\/small> is actually an elliptic curve based algorithm. The current implementation in OpenSSL supports both signature and encryption schemes via the <small>EVP<\/small> interface.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When doing the <b><small>SM2<\/small><\/b> signature algorithm, it requires a distinguishing identifier to form the message prefix which is hashed before the real message is hashed.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b><small>SM2<\/small><\/b> signatures can be generated by using the \u2019DigestSign\u2019 series of APIs, for instance, <b>EVP_DigestSignInit()<\/b>, <b>EVP_DigestSignUpdate()<\/b> and <b>EVP_DigestSignFinal()<\/b>. Ditto for the verification process by calling the \u2019DigestVerify\u2019 series of APIs.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">There are several special steps that need to be done before computing an <b><small>SM2<\/small><\/b> signature.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b><small>EVP_PKEY<\/small><\/b> structure will default to using <small>ECDSA<\/small> for signatures when it is created. It should be set to <b><small>EVP_PKEY_SM2<\/small><\/b> by calling:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Then an <small>ID<\/small> should be set by calling:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">EVP_PKEY_CTX_set1_id(pctx, id, id_len);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When calling the <b>EVP_DigestSignInit()<\/b> or <b>EVP_DigestVerifyInit()<\/b> functions, a preallocated <b><small>EVP_PKEY_CTX<\/small><\/b> should be assigned to the <b><small>EVP_MD_CTX<\/small><\/b> . This is done by calling:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">EVP_MD_CTX_set_pkey_ctx(mctx, pctx);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">And normally there is no need to pass a <b>pctx<\/b> parameter to <b>EVP_DigestSignInit()<\/b> or <b>EVP_DigestVerifyInit()<\/b> in such a scenario.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This example demonstrates the calling sequence for using an <b><small>EVP_PKEY<\/small><\/b> to verify a message with the <small>SM2<\/small> signature algorithm and the <small>SM3<\/small> hash algorithm:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <openssl\/evp.h> <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var obtain an EVP_PKEY using whatever methods&#8230; bodies\/ usr\/ <br \/> EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2); <br \/> mctx = EVP_MD_CTX_new(); <br \/> pctx = EVP_PKEY_CTX_new(pkey, NULL); <br \/> EVP_PKEY_CTX_set1_id(pctx, id, id_len); <br \/> EVP_MD_CTX_set_pkey_ctx(mctx, pctx);; <br \/> EVP_DigestVerifyInit(mctx, NULL, EVP_sm3(), NULL, pkey); <br \/> EVP_DigestVerifyUpdate(mctx, msg, msg_len); <br \/> EVP_DigestVerifyFinal(mctx, sig, sig_len)<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>EVP_PKEY_CTX_new<\/b>(3), <b>EVP_PKEY_set_alias_type<\/b>(3), <b>EVP_DigestSignInit<\/b>(3), <b>EVP_DigestVerifyInit<\/b>(3), <b>EVP_PKEY_CTX_set1_id<\/b>(3), <b>EVP_MD_CTX_set_pkey_ctx<\/b>(3)<\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Copyright 2018\u22122020 The OpenSSL Project Authors. All Rights Reserved.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Licensed under the OpenSSL license (the &#8220;License&#8221;). You may not use this file except in compliance with the License. You can obtain a copy in the file <small>LICENSE<\/small> in the source distribution or at <https:\/\/www.openssl.org\/source\/license.html>.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  SM2 \u2212 Chinese SM2 signature and encryption algorithm support <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[971],"tags":[973,972,981],"class_list":["post-3886","post","type-post","status-publish","format-standard","hentry","category-7-miscelanea","tag-973","tag-man7","tag-sm2"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=3886"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3886\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=3886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=3886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=3886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}