mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-06 11:53:39 +08:00
Update EDSA
This commit is contained in:
@@ -18,6 +18,12 @@
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/oid.h>
|
||||
#include <gmssl/asn1.h>
|
||||
#ifdef ENABLE_SECP256R1
|
||||
#include <gmssl/secp256r1_key.h>
|
||||
#endif
|
||||
#ifdef ENABLE_SECP384R1
|
||||
#include <gmssl/secp384r1_key.h>
|
||||
#endif
|
||||
#include <gmssl/x509_key.h>
|
||||
|
||||
|
||||
@@ -39,11 +45,25 @@ int ec_named_curve_from_name(const char *name);
|
||||
int ec_named_curve_to_der(int curve, uint8_t **out, size_t *outlen);
|
||||
int ec_named_curve_from_der(int *curve, const uint8_t **in, size_t *inlen);
|
||||
|
||||
typedef struct {
|
||||
int oid;
|
||||
union {
|
||||
#ifdef ENABLE_SECP256R1
|
||||
SECP256R1_KEY secp256r1_key;
|
||||
#endif
|
||||
#ifdef ENABLE_SECP384R1
|
||||
SECP384R1_KEY secp384r1_key;
|
||||
#endif
|
||||
int unused;
|
||||
} u;
|
||||
} EC_KEY;
|
||||
|
||||
/*
|
||||
ECPoint ::= OCTET STRING -- uncompressed point
|
||||
*/
|
||||
int ec_point_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
|
||||
|
||||
|
||||
/*
|
||||
ECPrivateKey ::= SEQUENCE {
|
||||
version INTEGER, -- value MUST be (1)
|
||||
|
||||
@@ -7,16 +7,13 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
#ifndef GMSSL_ECDSA_H
|
||||
#define GMSSL_ECDSA_H
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/digest.h>
|
||||
#include <gmssl/secp256r1_key.h>
|
||||
#include <gmssl/ec.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -24,43 +21,12 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
secp256r1_t r;
|
||||
secp256r1_t s;
|
||||
} ECDSA_SIGNATURE;
|
||||
|
||||
#define ECDSA_SIGNATURE_COMPACT_SIZE 70
|
||||
#define ECDSA_SIGNATURE_TYPICAL_SIZE 71
|
||||
#define ECDSA_SIGNATURE_MAX_SIZE 72
|
||||
|
||||
int ecdsa_signature_to_der(const ECDSA_SIGNATURE *sig, uint8_t **out, size_t *outlen);
|
||||
int ecdsa_signature_from_der(ECDSA_SIGNATURE *sig, const uint8_t **in, size_t *inlen);
|
||||
int ecdsa_signature_print_ex(FILE *fp, int fmt, int ind, const char *label, const ECDSA_SIGNATURE *sig);
|
||||
int ecdsa_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *sig, size_t siglen);
|
||||
|
||||
int ecdsa_do_sign_ex(const SECP256R1_KEY *key, const secp256r1_t k, const uint8_t dgst[32], ECDSA_SIGNATURE *sig);
|
||||
int ecdsa_do_sign(const SECP256R1_KEY *key, const uint8_t dgst[32], ECDSA_SIGNATURE *sig);
|
||||
int ecdsa_do_verify(const SECP256R1_KEY *key, const uint8_t dgst[32], const ECDSA_SIGNATURE *sig);
|
||||
int ecdsa_sign(const SECP256R1_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen);
|
||||
int ecdsa_sign_fixlen(const SECP256R1_KEY *key, const uint8_t dgst[32], size_t siglen, uint8_t *sig);
|
||||
int ecdsa_verify(const SECP256R1_KEY *key, const uint8_t dgst[32], const uint8_t *sig, size_t siglen);
|
||||
|
||||
|
||||
typedef struct {
|
||||
DIGEST_CTX digest_ctx;
|
||||
SECP256R1_KEY key;
|
||||
ECDSA_SIGNATURE sig;
|
||||
} ECDSA_SIGN_CTX;
|
||||
|
||||
int ecdsa_sign_init(ECDSA_SIGN_CTX *ctx, const SECP256R1_KEY *key, const DIGEST *digest);
|
||||
int ecdsa_sign_update(ECDSA_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int ecdsa_sign_finish(ECDSA_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen);
|
||||
int ecdsa_sign_finish_fixlen(ECDSA_SIGN_CTX *ctx, size_t siglen, uint8_t *sig);
|
||||
int ecdsa_verify_init(ECDSA_SIGN_CTX *ctx, const SECP256R1_KEY *key, const DIGEST *digest,
|
||||
int ecdsa_sign(const EC_KEY *key, const uint8_t *dgst, size_t dgstlen,
|
||||
uint8_t *sig, size_t *siglen);
|
||||
int ecdsa_sign_fixed_len(const EC_KEY *key, const uint8_t *dgst, size_t dgstlen,
|
||||
size_t siglen, uint8_t *sig);
|
||||
int ecdsa_verify(const EC_KEY *key, const uint8_t *dgst, size_t dgstlen,
|
||||
const uint8_t *sig, size_t siglen);
|
||||
int ecdsa_verify_update(ECDSA_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int ecdsa_verify_finish(ECDSA_SIGN_CTX *ctx);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
79
include/gmssl/secp256r1_ecdsa.h
Normal file
79
include/gmssl/secp256r1_ecdsa.h
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
#ifndef GMSSL_SECP256R1_ECDSA_H
|
||||
#define GMSSL_SECP256R1_ECDSA_H
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/digest.h>
|
||||
#include <gmssl/secp256r1_key.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// 不应该在保留一个独立的SECP256R1_ECDSA_SIGNATURE类型,应该直接输出紧凑的二进制,实际上一般来说总是输出DER
|
||||
typedef struct {
|
||||
secp256r1_t r;
|
||||
secp256r1_t s;
|
||||
} SECP256R1_ECDSA_SIGNATURE;
|
||||
|
||||
#define SECP256R1_ECDSA_SIGNATURE_COMPACT_SIZE 70
|
||||
#define SECP256R1_ECDSA_SIGNATURE_TYPICAL_SIZE 71
|
||||
#define SECP256R1_ECDSA_SIGNATURE_MAX_SIZE 72
|
||||
|
||||
// 这几个函数应都去掉,不再开放底层的类型了
|
||||
int secp256r1_ecdsa_signature_to_der(const SECP256R1_ECDSA_SIGNATURE *sig, uint8_t **out, size_t *outlen);
|
||||
int secp256r1_ecdsa_signature_from_der(SECP256R1_ECDSA_SIGNATURE *sig, const uint8_t **in, size_t *inlen);
|
||||
int secp256r1_ecdsa_signature_print_ex(FILE *fp, int fmt, int ind, const char *label, const SECP256R1_ECDSA_SIGNATURE *sig);
|
||||
|
||||
|
||||
|
||||
int secp256r1_ecdsa_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *sig, size_t siglen);
|
||||
|
||||
|
||||
int secp256r1_ecdsa_do_sign_ex(const SECP256R1_KEY *key, const secp256r1_t k, const uint8_t dgst[32], SECP256R1_ECDSA_SIGNATURE *sig);
|
||||
int secp256r1_ecdsa_do_sign(const SECP256R1_KEY *key, const uint8_t dgst[32], SECP256R1_ECDSA_SIGNATURE *sig);
|
||||
int secp256r1_ecdsa_do_verify(const SECP256R1_KEY *key, const uint8_t dgst[32], const SECP256R1_ECDSA_SIGNATURE *sig);
|
||||
|
||||
|
||||
// 这个函数应该改为将key的类型编程通用支持P256, P384的,摘要可以支持不同长度的
|
||||
int secp256r1_ecdsa_sign(const SECP256R1_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen);
|
||||
int secp256r1_ecdsa_sign_fixlen(const SECP256R1_KEY *key, const uint8_t dgst[32], size_t siglen, uint8_t *sig);
|
||||
int secp256r1_ecdsa_verify(const SECP256R1_KEY *key, const uint8_t dgst[32], const uint8_t *sig, size_t siglen);
|
||||
|
||||
|
||||
// 后面的CTX就没有意义了
|
||||
typedef struct {
|
||||
DIGEST_CTX digest_ctx;
|
||||
SECP256R1_KEY key;
|
||||
SECP256R1_ECDSA_SIGNATURE sig;
|
||||
} SECP256R1_ECDSA_SIGN_CTX;
|
||||
|
||||
int secp256r1_ecdsa_sign_init(SECP256R1_ECDSA_SIGN_CTX *ctx, const SECP256R1_KEY *key, const DIGEST *digest);
|
||||
int secp256r1_ecdsa_sign_update(SECP256R1_ECDSA_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int secp256r1_ecdsa_sign_finish(SECP256R1_ECDSA_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen);
|
||||
int secp256r1_ecdsa_sign_finish_fixlen(SECP256R1_ECDSA_SIGN_CTX *ctx, size_t siglen, uint8_t *sig);
|
||||
int secp256r1_ecdsa_verify_init(SECP256R1_ECDSA_SIGN_CTX *ctx, const SECP256R1_KEY *key, const DIGEST *digest,
|
||||
const uint8_t *sig, size_t siglen);
|
||||
int secp256r1_ecdsa_verify_update(SECP256R1_ECDSA_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int secp256r1_ecdsa_verify_finish(SECP256R1_ECDSA_SIGN_CTX *ctx);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
79
include/gmssl/secp384r1_ecdsa.h
Normal file
79
include/gmssl/secp384r1_ecdsa.h
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
#ifndef GMSSL_SECP384R1_ECDSA_H
|
||||
#define GMSSL_SECP384R1_ECDSA_H
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/digest.h>
|
||||
#include <gmssl/secp384r1_key.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// 不应该在保留一个独立的SECP384R1_ECDSA_SIGNATURE类型,应该直接输出紧凑的二进制,实际上一般来说总是输出DER
|
||||
typedef struct {
|
||||
secp384r1_t r;
|
||||
secp384r1_t s;
|
||||
} SECP384R1_ECDSA_SIGNATURE;
|
||||
|
||||
#define SECP384R1_ECDSA_SIGNATURE_COMPACT_SIZE 102
|
||||
#define SECP384R1_ECDSA_SIGNATURE_TYPICAL_SIZE 103
|
||||
#define SECP384R1_ECDSA_SIGNATURE_MAX_SIZE 104
|
||||
|
||||
// 这几个函数应都去掉,不再开放底层的类型了
|
||||
int secp384r1_ecdsa_signature_to_der(const SECP384R1_ECDSA_SIGNATURE *sig, uint8_t **out, size_t *outlen);
|
||||
int secp384r1_ecdsa_signature_from_der(SECP384R1_ECDSA_SIGNATURE *sig, const uint8_t **in, size_t *inlen);
|
||||
int secp384r1_ecdsa_signature_print_ex(FILE *fp, int fmt, int ind, const char *label, const SECP384R1_ECDSA_SIGNATURE *sig);
|
||||
|
||||
|
||||
|
||||
int secp384r1_ecdsa_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *sig, size_t siglen);
|
||||
|
||||
|
||||
int secp384r1_ecdsa_do_sign_ex(const SECP384R1_KEY *key, const secp384r1_t k, const uint8_t dgst[48], SECP384R1_ECDSA_SIGNATURE *sig);
|
||||
int secp384r1_ecdsa_do_sign(const SECP384R1_KEY *key, const uint8_t dgst[48], SECP384R1_ECDSA_SIGNATURE *sig);
|
||||
int secp384r1_ecdsa_do_verify(const SECP384R1_KEY *key, const uint8_t dgst[48], const SECP384R1_ECDSA_SIGNATURE *sig);
|
||||
|
||||
|
||||
// 这个函数应该改为将key的类型编程通用支持P256, P384的,摘要可以支持不同长度的
|
||||
int secp384r1_ecdsa_sign(const SECP384R1_KEY *key, const uint8_t dgst[48], uint8_t *sig, size_t *siglen);
|
||||
int secp384r1_ecdsa_sign_fixlen(const SECP384R1_KEY *key, const uint8_t dgst[48], size_t siglen, uint8_t *sig);
|
||||
int secp384r1_ecdsa_verify(const SECP384R1_KEY *key, const uint8_t dgst[48], const uint8_t *sig, size_t siglen);
|
||||
|
||||
|
||||
// 后面的CTX就没有意义了
|
||||
typedef struct {
|
||||
DIGEST_CTX digest_ctx;
|
||||
SECP384R1_KEY key;
|
||||
SECP384R1_ECDSA_SIGNATURE sig;
|
||||
} SECP384R1_ECDSA_SIGN_CTX;
|
||||
|
||||
int secp384r1_ecdsa_sign_init(SECP384R1_ECDSA_SIGN_CTX *ctx, const SECP384R1_KEY *key, const DIGEST *digest);
|
||||
int secp384r1_ecdsa_sign_update(SECP384R1_ECDSA_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int secp384r1_ecdsa_sign_finish(SECP384R1_ECDSA_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen);
|
||||
int secp384r1_ecdsa_sign_finish_fixlen(SECP384R1_ECDSA_SIGN_CTX *ctx, size_t siglen, uint8_t *sig);
|
||||
int secp384r1_ecdsa_verify_init(SECP384R1_ECDSA_SIGN_CTX *ctx, const SECP384R1_KEY *key, const DIGEST *digest,
|
||||
const uint8_t *sig, size_t siglen);
|
||||
int secp384r1_ecdsa_verify_update(SECP384R1_ECDSA_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int secp384r1_ecdsa_verify_finish(SECP384R1_ECDSA_SIGN_CTX *ctx);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
63
include/gmssl/secp384r1_key.h
Normal file
63
include/gmssl/secp384r1_key.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
#ifndef GMSSL_SECP384R1_KEY_H
|
||||
#define GMSSL_SECP384R1_KEY_H
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/secp384r1.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
SECP384R1_POINT public_key;
|
||||
secp384r1_t private_key;
|
||||
} SECP384R1_KEY;
|
||||
|
||||
int secp384r1_key_generate(SECP384R1_KEY *key);
|
||||
int secp384r1_key_set_private_key(SECP384R1_KEY *key, const secp384r1_t private_key);
|
||||
int secp384r1_public_key_equ(const SECP384R1_KEY *key, const SECP384R1_KEY *pub);
|
||||
|
||||
int secp384r1_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SECP384R1_KEY *key);
|
||||
int secp384r1_private_key_print(FILE *fp, int fmt, int ind, const char *label, const SECP384R1_KEY *key);
|
||||
|
||||
int secp384r1_public_key_to_bytes(const SECP384R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp384r1_public_key_from_bytes(SECP384R1_KEY *key, const uint8_t **in, size_t *inlen);
|
||||
int secp384r1_public_key_to_der(const SECP384R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp384r1_public_key_from_der(SECP384R1_KEY *key, const uint8_t **in, size_t *inlen);
|
||||
int secp384r1_private_key_to_der(const SECP384R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp384r1_private_key_from_der(SECP384R1_KEY *key, const uint8_t **in, size_t *inlen);
|
||||
int secp384r1_private_key_info_to_der(const SECP384R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp384r1_private_key_info_from_der(SECP384R1_KEY *key, const uint8_t **attrs, size_t *attrslen,
|
||||
const uint8_t **in, size_t *inlen);
|
||||
int secp384r1_private_key_info_encrypt_to_der(const SECP384R1_KEY *ec_key, const char *pass,
|
||||
uint8_t **out, size_t *outlen);
|
||||
int secp384r1_private_key_info_decrypt_from_der(SECP384R1_KEY *ec_key,
|
||||
const uint8_t **attrs, size_t *attrs_len,
|
||||
const char *pass, const uint8_t **in, size_t *inlen);
|
||||
|
||||
int secp384r1_private_key_to_pem(const SECP384R1_KEY *key, FILE *fp);
|
||||
int secp384r1_private_key_from_pem(SECP384R1_KEY *key, FILE *fp);
|
||||
int secp384r1_private_key_info_encrypt_to_pem(const SECP384R1_KEY *key, const char *pass, FILE *fp);
|
||||
int secp384r1_private_key_info_decrypt_from_pem(SECP384R1_KEY *key, const char *pass, FILE *fp);
|
||||
|
||||
int secp384r1_do_ecdh(const SECP384R1_KEY *key, const SECP384R1_KEY *pub, uint8_t out[48]);
|
||||
int secp384r1_ecdh(const SECP384R1_KEY *key, const uint8_t uncompressed_point[97], uint8_t out[48]);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -18,7 +18,7 @@ extern "C" {
|
||||
|
||||
|
||||
#define GMSSL_VERSION_NUM 30300
|
||||
#define GMSSL_VERSION_STR "GmSSL 3.3.0-dev.1170"
|
||||
#define GMSSL_VERSION_STR "GmSSL 3.3.0-dev.1171"
|
||||
|
||||
int gmssl_version_num(void);
|
||||
const char *gmssl_version_str(void);
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
#endif
|
||||
#ifdef ENABLE_SECP256R1
|
||||
#include <gmssl/secp256r1_key.h>
|
||||
#include <gmssl/ecdsa.h>
|
||||
#include <gmssl/secp256r1_ecdsa.h>
|
||||
#endif
|
||||
#ifdef ENABLE_LMS
|
||||
#include <gmssl/lms.h>
|
||||
@@ -187,7 +187,7 @@ int x509_private_keys_from_file(X509_KEY *keys, size_t *keys_cnt, size_t max_cnt
|
||||
// XMSS_SIGNATURE_MAX_SIZE = 2820
|
||||
// XMSSMT_SIGNATURE_MAX_SIZE = 8356?
|
||||
// SPHINCS_SIGNATURE_SIZE = 7856?
|
||||
// ECDSA_SIGNATURE_MAX_SIZE = 72
|
||||
// SECP256R1_ECDSA_SIGNATURE_MAX_SIZE = 72
|
||||
|
||||
typedef union {
|
||||
uint8_t sm2_sig[SM2_MAX_SIGNATURE_SIZE];
|
||||
@@ -215,7 +215,7 @@ typedef struct {
|
||||
SM2_SIGN_CTX sm2_sign_ctx;
|
||||
SM2_VERIFY_CTX sm2_verify_ctx;
|
||||
#ifdef ENABLE_SECP256R1
|
||||
ECDSA_SIGN_CTX ecdsa_sign_ctx;
|
||||
SECP256R1_ECDSA_SIGN_CTX secp256r1_ecdsa_sign_ctx;
|
||||
#endif
|
||||
#ifdef ENABLE_SM9
|
||||
SM9_SIGN_CTX sm9_sign_ctx;
|
||||
|
||||
Reference in New Issue
Block a user