Skip to content

Commit 646627c

Browse files
karel-msjaeckel
authored andcommitted
SM2
1 parent a68fa19 commit 646627c

23 files changed

Lines changed: 1394 additions & 1 deletion

doc/crypt.tex

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5461,6 +5461,7 @@ \chapter{Elliptic Curve Cryptography - $GF(p)$}
54615461
\hline \texttt{secp224k1} & & 1.3.132.0.32 \\
54625462
\hline \texttt{secp256r1} & nistp256, prime256v1, ECC-256, P-256 & 1.2.840.10045.3.1.7 \\
54635463
\hline \texttt{secp256k1} & & 1.3.132.0.10 \\
5464+
\hline \texttt{sm2p256v1} & sm2 & 1.2.156.10197.1.301 \\
54645465
\hline \texttt{secp384r1} & nistp384, ECC-384, P-384 & 1.3.132.0.34 \\
54655466
\hline \texttt{secp521r1} & nistp521, ECC-521, P-521 & 1.3.132.0.35 \\
54665467
\hline \texttt{prime239v1} & & 1.2.840.10045.3.1.4 \\
@@ -6092,6 +6093,47 @@ \subsection{Signature Formats}
60926093
the option to use \code{LTC\_ECCSIG\_ANSIX962}. Also it is possible to disable \code{LTC\_SSH} which will disable
60936094
the option to use \code{LTC\_ECCSIG\_RFC5656}.
60946095

6096+
\mysection{Signatures (SM2)}
6097+
The library also provides helpers for the \textit{SM2} signature scheme. In contrast to the hash-level \textit{ECDSA} API,
6098+
these functions operate on the original message and the signer identifier (application-defined user ID bound into ZA). Internally they compute the SM2 message digest
6099+
\textit{Hash(ZA || M)}, where \textit{ZA} is the SM2 digest of the signer identifier, curve parameters, and public key, and produce or verify a DER-encoded \textit{(r, s)} signature. Standard deployments typically use
6100+
the built-in curve \texttt{sm2p256v1} together with the \textit{SM3} hash. These SM2 functions accept only keys on the built-in
6101+
\texttt{sm2p256v1} curve.
6102+
6103+
\textbf{NOTE:} These functions require \code{LTC\_DER}.
6104+
6105+
\subsection{Signature Generation}
6106+
\index{ecc\_sign\_sm2()}
6107+
\begin{verbatim}
6108+
int ecc_sign_sm2(const unsigned char *id, unsigned long idlen,
6109+
const unsigned char *msg, unsigned long msglen,
6110+
unsigned char *out, unsigned long *outlen,
6111+
prng_state *prng, int wprng, int hash_idx,
6112+
const ecc_key *key);
6113+
\end{verbatim}
6114+
6115+
This function signs the message in \code{msg} of length \code{msglen} octets using the signer identifier (application-defined user ID bound into ZA) \code{id} of
6116+
length \code{idlen} octets. The resulting DER-encoded signature is stored in \code{out}. The \code{hash\_idx} parameter
6117+
selects the hash used for both \code{ZA} and the message digest. If \code{hash\_idx} is \code{-1}, the default \textit{SM3}
6118+
hash is used. Other hashes are supported for compatibility and testing, but should only rarely be used in practice. The
6119+
\code{key} must be a private ECC key on the built-in \texttt{sm2p256v1} curve.
6120+
6121+
\subsection{Signature Verification}
6122+
\index{ecc\_verify\_sm2()}
6123+
\begin{verbatim}
6124+
int ecc_verify_sm2(const unsigned char *id, unsigned long idlen,
6125+
const unsigned char *msg, unsigned long msglen,
6126+
const unsigned char *sig, unsigned long siglen,
6127+
int hash_idx, int *stat, const ecc_key *key);
6128+
\end{verbatim}
6129+
6130+
This function verifies the DER-encoded signature in \code{sig} against the message in \code{msg} and the signer identifier
6131+
(application-defined user ID bound into ZA) \code{id}. The same identifier and hash must be used as during signature generation. The result is stored in \code{stat},
6132+
which is set to a non-zero value if the signature is valid. If \code{hash\_idx} is \code{-1}, the default \textit{SM3}
6133+
hash is used. Other hashes are supported for compatibility and testing, but should only rarely be used in practice. The
6134+
\code{key} must contain the corresponding public key (or the private key matching that public key) on the built-in
6135+
\texttt{sm2p256v1} curve.
6136+
60956137
\mysection{Shared Secret (ECDH)}
60966138
To construct a Diffie-Hellman shared secret with a private and public ECC key, use the following function:
60976139
\index{ecc\_shared\_secret()}
@@ -6157,6 +6199,39 @@ \subsection{Encryption Format}
61576199
}
61586200
\end{verbatim}
61596201

6202+
\mysection{Encrypt and Decrypt (SM2)}
6203+
The library also provides \textit{SM2} public-key encryption. The interface uses the raw SM2 ciphertext layout
6204+
\code{C1 || C3 || C2}, not the ASN.1 wrapper used by \code{ecc\_encrypt\_key()}. These SM2 functions accept only keys on the
6205+
built-in \texttt{sm2p256v1} curve.
6206+
6207+
\subsection{Encryption}
6208+
\index{ecc\_encrypt\_key\_sm2()}
6209+
\begin{verbatim}
6210+
int ecc_encrypt_key_sm2(const unsigned char *in, unsigned long inlen,
6211+
unsigned char *out, unsigned long *outlen,
6212+
prng_state *prng, int wprng, int hash_idx,
6213+
const ecc_key *key);
6214+
\end{verbatim}
6215+
6216+
This function encrypts the plaintext in \code{in} using the recipient public key in \code{key}. The \code{hash\_idx}
6217+
parameter selects the hash used by the SM2 KDF and for computing \code{C3}. If \code{hash\_idx} is \code{-1}, the default
6218+
\textit{SM3} hash is used. Other hashes are supported for compatibility and testing, but should only rarely be used in
6219+
practice. The ciphertext is written to \code{out} in \code{C1 || C3 || C2} format, where \code{C1} is the ephemeral public
6220+
point, \code{C3} is the authentication hash, and \code{C2} is the masked plaintext.
6221+
6222+
\subsection{Decryption}
6223+
\index{ecc\_decrypt\_key\_sm2()}
6224+
\begin{verbatim}
6225+
int ecc_decrypt_key_sm2(const unsigned char *in, unsigned long inlen,
6226+
unsigned char *out, unsigned long *outlen,
6227+
int hash_idx, const ecc_key *key);
6228+
\end{verbatim}
6229+
6230+
This function decrypts an SM2 ciphertext in \code{C1 || C3 || C2} format using the recipient private key in \code{key}.
6231+
The \code{hash\_idx} parameter must match the hash used during encryption. If \code{hash\_idx} is \code{-1}, the default
6232+
\textit{SM3} hash is used. Other hashes are supported for compatibility and testing, but should only rarely be used in
6233+
practice. The function verifies \code{C3} before returning the recovered plaintext in \code{out}.
6234+
61606235
\chapter{Elliptic Curve Cryptography - $Montgomery/Twisted Edwards$}
61616236
\mysection{Introduction}
61626237

0 commit comments

Comments
 (0)