sm2.h
Go to the documentation of this file.
1 /**
2  * @file sm2.h
3  * @brief SM2 signature algorithm
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
29  **/
30 
31 #ifndef _SM2_H
32 #define _SM2_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "ecc/ecdsa.h"
37 #include "hash/hash_algorithms.h"
38 
39 //SM2 identifiers
40 #define SM2_DEFAULT_ID "1234567812345678"
41 #define SM2_TLS13_ID "TLSv1.3+GM+Cipher+Suite"
42 
43 //C++ guard
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 
49 /**
50  * @brief Working state (SM2 signature generation)
51  **/
52 
53 typedef struct
54 {
56  uint8_t buffer[32];
57  uint32_t e[8];
58  uint32_t k[8];
59  uint32_t r[8];
60  uint32_t t[8];
64 
65 
66 /**
67  * @brief Working state (SM2 signature verification)
68  **/
69 
70 typedef struct
71 {
73  uint8_t buffer[32];
74  uint32_t e[8];
75  uint32_t r[8];
76  uint32_t t[8];
80 
81 
82 //SM2 related constants
83 extern const uint8_t SM2_WITH_SM3_OID[8];
84 
85 //SM2 related functions
86 error_t sm2GenerateSignature(const PrngAlgo *prngAlgo, void *prngContext,
87  const EcPrivateKey *privateKey, const HashAlgo *hashAlgo, const char_t *id,
88  size_t idLen, const void *message, size_t messageLen,
89  EcdsaSignature *signature);
90 
91 error_t sm2VerifySignature(const EcPublicKey *publicKey,
92  const HashAlgo *hashAlgo, const char_t *id, size_t idLen,
93  const void *message, size_t messageLen, const EcdsaSignature *signature);
94 
95 error_t sm2ComputeZa(const HashAlgo *hashAlgo, HashContext *hashContext,
96  const EcCurve *curve, const EcPoint3 *pa, const char_t *ida, size_t idaLen,
97  uint8_t *za);
98 
99 //C++ guard
100 #ifdef __cplusplus
101 }
102 #endif
103 
104 #endif
ECDSA signature.
Definition: ecdsa.h:63
Generic hash algorithm context.
#define PrngAlgo
Definition: crypto.h:973
ECDSA (Elliptic Curve Digital Signature Algorithm)
uint8_t message[]
Definition: chap.h:154
error_t sm2GenerateSignature(const PrngAlgo *prngAlgo, void *prngContext, const EcPrivateKey *privateKey, const HashAlgo *hashAlgo, const char_t *id, size_t idLen, const void *message, size_t messageLen, EcdsaSignature *signature)
SM2 signature generation.
Definition: sm2.c:62
uint8_t t
Definition: lldp_ext_med.h:212
const uint8_t SM2_WITH_SM3_OID[8]
Definition: sm2.c:45
HashContext hashContext
Definition: sm2.h:72
error_t sm2ComputeZa(const HashAlgo *hashAlgo, HashContext *hashContext, const EcCurve *curve, const EcPoint3 *pa, const char_t *ida, size_t idaLen, uint8_t *za)
Calculate ZA.
Definition: sm2.c:451
uint8_t r
Definition: ndp.h:346
error_t
Error codes.
Definition: error.h:43
General definitions for cryptographic algorithms.
Working state (SM2 signature verification)
Definition: sm2.h:71
EC private key.
Definition: ec.h:432
Collection of hash algorithms.
EC public key.
Definition: ec.h:421
char char_t
Definition: compiler_port.h:55
EC point (projective coordinates)
Definition: ec.h:409
Common interface for hash algorithms.
Definition: crypto.h:1082
#define EcCurve
Definition: ec.h:346
HashContext hashContext
Definition: sm2.h:55
Working state (SM2 signature generation)
Definition: sm2.h:54
error_t sm2VerifySignature(const EcPublicKey *publicKey, const HashAlgo *hashAlgo, const char_t *id, size_t idLen, const void *message, size_t messageLen, const EcdsaSignature *signature)
SM2 signature verification.
Definition: sm2.c:274