mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-07 12:33:39 +08:00
Refactor X509_KEY
This commit is contained in:
@@ -11,20 +11,13 @@
|
||||
#ifndef GMSSL_EC_H
|
||||
#define GMSSL_EC_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#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>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -45,46 +38,6 @@ 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)
|
||||
privateKey OCTET STRING, -- big endian encoding of integer
|
||||
parameters [0] EXPLICIT OBJECT IDENTIFIER OPTIONAL, -- namedCurve
|
||||
publicKey [1] EXPLICIT BIT STRING OPTIONAL -- ECPoint
|
||||
}
|
||||
*/
|
||||
// ECPrivateKey when key->algor == OID_ec_public_key
|
||||
int ec_private_key_to_der(const X509_KEY *key, int encode_params, int encode_pubkey, uint8_t **out, size_t *outlen);
|
||||
int ec_private_key_from_der(X509_KEY *key, int opt_curve, const uint8_t **in, size_t *inlen);
|
||||
int ec_private_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen); // from <gmssl/ec.h>
|
||||
// X509_KEY lost some information of ECPrivateKey. so no ec_private_key_print_ex(X509_KEY)
|
||||
|
||||
|
||||
enum {
|
||||
EC_private_key_version = 1,
|
||||
};
|
||||
|
||||
int ec_private_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* 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_ECDSA_H
|
||||
#define GMSSL_ECDSA_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/ec.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/digest.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -103,6 +104,88 @@ int secp256r1_point_to_uncompressed_octets(const SECP256R1_POINT *P, uint8_t oc
|
||||
int secp256r1_point_from_uncompressed_octets(SECP256R1_POINT *P, const uint8_t octets[65]);
|
||||
|
||||
|
||||
typedef struct {
|
||||
SECP256R1_POINT public_key;
|
||||
secp256r1_t private_key;
|
||||
} SECP256R1_KEY;
|
||||
|
||||
int secp256r1_key_generate(SECP256R1_KEY *key);
|
||||
int secp256r1_key_set_private_key(SECP256R1_KEY *key, const secp256r1_t private_key);
|
||||
int secp256r1_public_key_equ(const SECP256R1_KEY *key, const SECP256R1_KEY *pub);
|
||||
|
||||
int secp256r1_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SECP256R1_KEY *key);
|
||||
int secp256r1_private_key_print(FILE *fp, int fmt, int ind, const char *label, const SECP256R1_KEY *key);
|
||||
|
||||
int secp256r1_public_key_to_bytes(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp256r1_public_key_from_bytes(SECP256R1_KEY *key, const uint8_t **in, size_t *inlen);
|
||||
int secp256r1_private_key_to_bytes(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp256r1_private_key_from_bytes(SECP256R1_KEY *key, const uint8_t **in, size_t *inlen);
|
||||
int secp256r1_public_key_to_der(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp256r1_public_key_from_der(SECP256R1_KEY *key, const uint8_t **in, size_t *inlen);
|
||||
int secp256r1_private_key_to_der(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp256r1_private_key_from_der(SECP256R1_KEY *key, const uint8_t **in, size_t *inlen);
|
||||
int secp256r1_private_key_info_to_der(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp256r1_private_key_info_from_der(SECP256R1_KEY *key, const uint8_t **attrs, size_t *attrslen,
|
||||
const uint8_t **in, size_t *inlen);
|
||||
int secp256r1_private_key_info_encrypt_to_der(const SECP256R1_KEY *ec_key, const char *pass,
|
||||
uint8_t **out, size_t *outlen);
|
||||
int secp256r1_private_key_info_decrypt_from_der(SECP256R1_KEY *ec_key,
|
||||
const uint8_t **attrs, size_t *attrs_len,
|
||||
const char *pass, const uint8_t **in, size_t *inlen);
|
||||
|
||||
int secp256r1_private_key_to_pem(const SECP256R1_KEY *key, FILE *fp);
|
||||
int secp256r1_private_key_from_pem(SECP256R1_KEY *key, FILE *fp);
|
||||
int secp256r1_private_key_info_encrypt_to_pem(const SECP256R1_KEY *key, const char *pass, FILE *fp);
|
||||
int secp256r1_private_key_info_decrypt_from_pem(SECP256R1_KEY *key, const char *pass, FILE *fp);
|
||||
|
||||
int secp256r1_do_ecdh(const SECP256R1_KEY *key, const SECP256R1_KEY *pub, uint8_t out[32]);
|
||||
int secp256r1_ecdh(const SECP256R1_KEY *key, const uint8_t uncompressed_point[65], uint8_t out[32]);
|
||||
|
||||
|
||||
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, size_t dgstlen, SECP256R1_ECDSA_SIGNATURE *sig);
|
||||
int secp256r1_ecdsa_do_sign(const SECP256R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, SECP256R1_ECDSA_SIGNATURE *sig);
|
||||
int secp256r1_ecdsa_do_verify(const SECP256R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, const SECP256R1_ECDSA_SIGNATURE *sig);
|
||||
|
||||
int secp256r1_ecdsa_sign(const SECP256R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, uint8_t *sig, size_t *siglen);
|
||||
int secp256r1_ecdsa_sign_fixlen(const SECP256R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, size_t siglen, uint8_t *sig);
|
||||
int secp256r1_ecdsa_verify(const SECP256R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, const uint8_t *sig, size_t siglen);
|
||||
|
||||
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
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* 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, size_t dgstlen, SECP256R1_ECDSA_SIGNATURE *sig);
|
||||
int secp256r1_ecdsa_do_sign(const SECP256R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, SECP256R1_ECDSA_SIGNATURE *sig);
|
||||
int secp256r1_ecdsa_do_verify(const SECP256R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, const SECP256R1_ECDSA_SIGNATURE *sig);
|
||||
|
||||
|
||||
// 这个函数应该改为将key的类型编程通用支持P256, P384的,摘要可以支持不同长度的
|
||||
int secp256r1_ecdsa_sign(const SECP256R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, uint8_t *sig, size_t *siglen);
|
||||
int secp256r1_ecdsa_sign_fixlen(const SECP256R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, size_t siglen, uint8_t *sig);
|
||||
int secp256r1_ecdsa_verify(const SECP256R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, 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
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* 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_KEY_H
|
||||
#define GMSSL_SECP256R1_KEY_H
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/secp256r1.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
SECP256R1_POINT public_key;
|
||||
secp256r1_t private_key;
|
||||
} SECP256R1_KEY;
|
||||
|
||||
int secp256r1_key_generate(SECP256R1_KEY *key);
|
||||
int secp256r1_key_set_private_key(SECP256R1_KEY *key, const secp256r1_t private_key);
|
||||
int secp256r1_public_key_equ(const SECP256R1_KEY *key, const SECP256R1_KEY *pub);
|
||||
|
||||
int secp256r1_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SECP256R1_KEY *key);
|
||||
int secp256r1_private_key_print(FILE *fp, int fmt, int ind, const char *label, const SECP256R1_KEY *key);
|
||||
|
||||
int secp256r1_public_key_to_bytes(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp256r1_public_key_from_bytes(SECP256R1_KEY *key, const uint8_t **in, size_t *inlen);
|
||||
int secp256r1_public_key_to_der(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp256r1_public_key_from_der(SECP256R1_KEY *key, const uint8_t **in, size_t *inlen);
|
||||
int secp256r1_private_key_to_der(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp256r1_private_key_from_der(SECP256R1_KEY *key, const uint8_t **in, size_t *inlen);
|
||||
int secp256r1_private_key_info_to_der(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp256r1_private_key_info_from_der(SECP256R1_KEY *key, const uint8_t **attrs, size_t *attrslen,
|
||||
const uint8_t **in, size_t *inlen);
|
||||
int secp256r1_private_key_info_encrypt_to_der(const SECP256R1_KEY *ec_key, const char *pass,
|
||||
uint8_t **out, size_t *outlen);
|
||||
int secp256r1_private_key_info_decrypt_from_der(SECP256R1_KEY *ec_key,
|
||||
const uint8_t **attrs, size_t *attrs_len,
|
||||
const char *pass, const uint8_t **in, size_t *inlen);
|
||||
|
||||
int secp256r1_private_key_to_pem(const SECP256R1_KEY *key, FILE *fp);
|
||||
int secp256r1_private_key_from_pem(SECP256R1_KEY *key, FILE *fp);
|
||||
int secp256r1_private_key_info_encrypt_to_pem(const SECP256R1_KEY *key, const char *pass, FILE *fp);
|
||||
int secp256r1_private_key_info_decrypt_from_pem(SECP256R1_KEY *key, const char *pass, FILE *fp);
|
||||
|
||||
int secp256r1_do_ecdh(const SECP256R1_KEY *key, const SECP256R1_KEY *pub, uint8_t out[32]);
|
||||
int secp256r1_ecdh(const SECP256R1_KEY *key, const uint8_t uncompressed_point[65], uint8_t out[32]);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/digest.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -103,6 +104,88 @@ int secp384r1_point_to_uncompressed_octets(const SECP384R1_POINT *P, uint8_t oc
|
||||
int secp384r1_point_from_uncompressed_octets(SECP384R1_POINT *P, const uint8_t octets[97]);
|
||||
|
||||
|
||||
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_private_key_to_bytes(const SECP384R1_KEY *key, uint8_t **out, size_t *outlen);
|
||||
int secp384r1_private_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]);
|
||||
|
||||
|
||||
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, size_t dgstlen, SECP384R1_ECDSA_SIGNATURE *sig);
|
||||
int secp384r1_ecdsa_do_sign(const SECP384R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, SECP384R1_ECDSA_SIGNATURE *sig);
|
||||
int secp384r1_ecdsa_do_verify(const SECP384R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, const SECP384R1_ECDSA_SIGNATURE *sig);
|
||||
|
||||
int secp384r1_ecdsa_sign(const SECP384R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, uint8_t *sig, size_t *siglen);
|
||||
int secp384r1_ecdsa_sign_fixlen(const SECP384R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, size_t siglen, uint8_t *sig);
|
||||
int secp384r1_ecdsa_verify(const SECP384R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, const uint8_t *sig, size_t siglen);
|
||||
|
||||
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
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* 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, size_t dgstlen, SECP384R1_ECDSA_SIGNATURE *sig);
|
||||
int secp384r1_ecdsa_do_sign(const SECP384R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, SECP384R1_ECDSA_SIGNATURE *sig);
|
||||
int secp384r1_ecdsa_do_verify(const SECP384R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, const SECP384R1_ECDSA_SIGNATURE *sig);
|
||||
|
||||
|
||||
// 这个函数应该改为将key的类型编程通用支持P256, P384的,摘要可以支持不同长度的
|
||||
int secp384r1_ecdsa_sign(const SECP384R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, uint8_t *sig, size_t *siglen);
|
||||
int secp384r1_ecdsa_sign_fixlen(const SECP384R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, size_t siglen, uint8_t *sig);
|
||||
int secp384r1_ecdsa_verify(const SECP384R1_KEY *key,
|
||||
const uint8_t *dgst, size_t dgstlen, 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
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* 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.1180"
|
||||
#define GMSSL_VERSION_STR "GmSSL 3.3.0-dev.1181"
|
||||
|
||||
int gmssl_version_num(void);
|
||||
const char *gmssl_version_str(void);
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#define GMSSL_X509_KEY_H
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
@@ -24,8 +25,10 @@
|
||||
#include <gmssl/sm9.h>
|
||||
#endif
|
||||
#ifdef ENABLE_SECP256R1
|
||||
#include <gmssl/secp256r1_key.h>
|
||||
#include <gmssl/secp256r1_ecdsa.h>
|
||||
#include <gmssl/secp256r1.h>
|
||||
#endif
|
||||
#ifdef ENABLE_SECP384R1
|
||||
#include <gmssl/secp384r1.h>
|
||||
#endif
|
||||
#ifdef ENABLE_LMS
|
||||
#include <gmssl/lms.h>
|
||||
@@ -54,6 +57,9 @@ typedef struct {
|
||||
#ifdef ENABLE_SECP256R1
|
||||
SECP256R1_KEY secp256r1_key;
|
||||
#endif
|
||||
#ifdef ENABLE_SECP384R1
|
||||
SECP384R1_KEY secp384r1_key;
|
||||
#endif
|
||||
#ifdef ENABLE_LMS
|
||||
LMS_KEY lms_key;
|
||||
HSS_KEY hss_key;
|
||||
@@ -79,6 +85,9 @@ int x509_key_set_sm2_key(X509_KEY *x509_key, const SM2_KEY *sm2_key);
|
||||
#ifdef ENABLE_SECP256R1
|
||||
int x509_key_set_secp256r1_key(X509_KEY *x509_key, const SECP256R1_KEY *secp256r1_key);
|
||||
#endif
|
||||
#ifdef ENABLE_SECP384R1
|
||||
int x509_key_set_secp384r1_key(X509_KEY *x509_key, const SECP384R1_KEY *secp384r1_key);
|
||||
#endif
|
||||
#ifdef ENABLE_LMS
|
||||
int x509_key_set_lms_key(X509_KEY *x509_key, const LMS_KEY *lms_key);
|
||||
int x509_key_set_hss_key(X509_KEY *x509_key, const HSS_KEY *hss_key);
|
||||
@@ -150,13 +159,6 @@ int x509_public_key_info_from_pem(X509_KEY *a, FILE *fp);
|
||||
int x509_public_key_info_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
|
||||
|
||||
|
||||
// ECPrivateKey when key->algor == OID_ec_public_key
|
||||
int ec_private_key_to_der(const X509_KEY *key, int encode_params, int encode_pubkey, uint8_t **out, size_t *outlen);
|
||||
int ec_private_key_from_der(X509_KEY *key, int opt_curve, const uint8_t **in, size_t *inlen);
|
||||
int ec_private_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen); // from <gmssl/ec.h>
|
||||
// X509_KEY lost some information of ECPrivateKey. so no ec_private_key_print_ex(X509_KEY)
|
||||
|
||||
|
||||
// PKCS #8 PrivateKeyInfo
|
||||
// PrivateKeyInfo.algor.parameters has the named_curve
|
||||
// so omit the optional ECPrivateKey params(named_curve) a nd omit the public_key
|
||||
|
||||
Reference in New Issue
Block a user