5 SM2 - Chinese SM2 signature and encryption algorithm support
9 B<SM2> algorithm is first defined by the Chinese national standard GM/T 0003-2012
10 and is standardized by ISO as ISO/IEC 14888. B<SM2> is actually an elliptic curve
11 based algorithm. Currnet implementation in OpenSSL supports both signature and
12 encryption schemes via EVP interface.
14 When doing the B<SM2> signature algorithm, it requires a distinguishing identifier
15 to form the message prefix which is hashed before the real message is hashed.
19 B<SM2> signature can be generated by using the 'DigestSign' series APIs, for instance,
20 EVP_DigestSignInit(), EVP_DigestSignUpdate() and EVP_DigestSignFinal(). Ditto
21 for the verification process by calling the 'DigestVerify' series APIs.
23 There are several special steps need to be done before computing an B<SM2> signature.
25 The B<EVP_PKEY> structure should be set to B<EVP_PKEY_SM2> by calling:
27 EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
29 Then an ID should be set by calling:
31 EVP_PKEY_CTX_set1_id(pctx, id, id_len);
33 When calling the EVP_DeigestSignInit() or EVP_DigestVerifyInit() function, a
34 pre-allocated B<EVP_PKEY_CTX> should be assigned to the B<EVP_MD_CTX>. This is
37 EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
39 And normally there is no need to pass a B<pctx> parameter to EVP_DeigestSignInit()
40 or EVP_DigestVerifyInit() in such scenario.
44 This example demonstrates the calling sequence on how to use an B<EVP_PKEY> to
45 sign a message with SM2 signature algorithm and SM3 hash algorithm:
47 #include <openssl/evp.h>
49 /* obtain an EVP_PKEY from whatever methods... */
50 EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
51 mctx = EVP_MD_CTX_new();
52 pctx = EVP_PKEY_CTX_new(pkey, NULL);
53 EVP_PKEY_CTX_set1_id(pctx, id, id_len);
54 EVP_MD_CTX_set_pkey_ctx(mctx, pctx);;
55 EVP_DigestVerifyInit(mctx, NULL, EVP_sm3(), NULL, pkey);
56 EVP_DigestVerifyUpdate(mctx, msg, msg_len);
57 EVP_DigestVerifyFinal(mctx, sig, sig_len)
61 L<EVP_PKEY_CTX_new(3)>,
62 L<EVP_PKEY_set_alias_type(3)>,
63 L<EVP_DigestSignInit(3)>,
64 L<EVP_DigestVerifyInit(3)>,
65 L<EVP_PKEY_CTX_set1_id(3)>,
66 L<EVP_MD_CTX_set_pkey_ctx(3)>
70 Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
72 Licensed under the OpenSSL license (the "License"). You may not use
73 this file except in compliance with the License. You can obtain a copy
74 in the file LICENSE in the source distribution or at
75 L<https://www.openssl.org/source/license.html>.