Refactor X509_KEY

This commit is contained in:
Zhi Guan
2026-06-27 11:36:55 +08:00
parent 52f0f1619e
commit c924503a05
28 changed files with 2692 additions and 3477 deletions

View File

@@ -537,7 +537,7 @@ if (ENABLE_SECP256R1)
set(ENABLE_SHA2 ON) set(ENABLE_SHA2 ON)
endif() endif()
add_definitions(-DENABLE_SECP256R1) add_definitions(-DENABLE_SECP256R1)
list(APPEND src src/bn.c src/secp256r1.c src/secp256r1_key.c src/ecdsa.c src/ecdh.c) list(APPEND src src/bn.c src/secp256r1.c)
list(APPEND tools tools/p256keygen.c) list(APPEND tools tools/p256keygen.c)
list(APPEND tests bn secp256r1 secp256r1_key ecdsa) list(APPEND tests bn secp256r1 secp256r1_key ecdsa)
endif() endif()
@@ -550,17 +550,12 @@ if (ENABLE_SECP384R1)
endif() endif()
add_definitions(-DENABLE_SECP384R1) add_definitions(-DENABLE_SECP384R1)
if (NOT ENABLE_SECP256R1) if (NOT ENABLE_SECP256R1)
list(APPEND src src/bn.c src/ecdh.c) list(APPEND src src/bn.c)
endif() endif()
list(APPEND src src/secp384r1.c src/secp384r1_key.c src/secp384r1_ecdsa.c) list(APPEND src src/secp384r1.c)
list(APPEND tests secp384r1 secp384r1_key secp384r1_ecdsa) list(APPEND tests secp384r1 secp384r1_key secp384r1_ecdsa)
endif() endif()
if (ENABLE_SECP256R1 OR ENABLE_SECP384R1)
list(APPEND src src/ec_ecdsa.c)
endif()
if (ENABLE_LMS) if (ENABLE_LMS)
message(STATUS "ENABLE_LMS is ON") message(STATUS "ENABLE_LMS is ON")
add_definitions(-DENABLE_LMS) add_definitions(-DENABLE_LMS)
@@ -1025,7 +1020,7 @@ endif()
# #
set(CPACK_PACKAGE_NAME "GmSSL") set(CPACK_PACKAGE_NAME "GmSSL")
set(CPACK_PACKAGE_VENDOR "GmSSL develop team") set(CPACK_PACKAGE_VENDOR "GmSSL develop team")
set(CPACK_PACKAGE_VERSION "3.3.0-dev.1180") set(CPACK_PACKAGE_VERSION "3.3.0-dev.1181")
set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/README.md) set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/README.md)
set(CPACK_NSIS_MODIFY_PATH ON) set(CPACK_NSIS_MODIFY_PATH ON)
include(CPack) include(CPack)

View File

@@ -11,20 +11,13 @@
#ifndef GMSSL_EC_H #ifndef GMSSL_EC_H
#define GMSSL_EC_H #define GMSSL_EC_H
#include <stdio.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <gmssl/sm2.h>
#include <gmssl/oid.h> #include <gmssl/oid.h>
#include <gmssl/asn1.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 #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_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); 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 #ifdef __cplusplus
} }
#endif #endif

View File

@@ -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

View File

@@ -16,6 +16,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <gmssl/digest.h>
#ifdef __cplusplus #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]); 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 #ifdef __cplusplus
} }
#endif #endif

View File

@@ -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

View File

@@ -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

View File

@@ -16,6 +16,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <gmssl/digest.h>
#ifdef __cplusplus #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]); 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 #ifdef __cplusplus
} }
#endif #endif

View File

@@ -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

View File

@@ -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

View File

@@ -18,7 +18,7 @@ extern "C" {
#define GMSSL_VERSION_NUM 30300 #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); int gmssl_version_num(void);
const char *gmssl_version_str(void); const char *gmssl_version_str(void);

View File

@@ -12,6 +12,7 @@
#define GMSSL_X509_KEY_H #define GMSSL_X509_KEY_H
#include <stdio.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
@@ -24,8 +25,10 @@
#include <gmssl/sm9.h> #include <gmssl/sm9.h>
#endif #endif
#ifdef ENABLE_SECP256R1 #ifdef ENABLE_SECP256R1
#include <gmssl/secp256r1_key.h> #include <gmssl/secp256r1.h>
#include <gmssl/secp256r1_ecdsa.h> #endif
#ifdef ENABLE_SECP384R1
#include <gmssl/secp384r1.h>
#endif #endif
#ifdef ENABLE_LMS #ifdef ENABLE_LMS
#include <gmssl/lms.h> #include <gmssl/lms.h>
@@ -54,6 +57,9 @@ typedef struct {
#ifdef ENABLE_SECP256R1 #ifdef ENABLE_SECP256R1
SECP256R1_KEY secp256r1_key; SECP256R1_KEY secp256r1_key;
#endif #endif
#ifdef ENABLE_SECP384R1
SECP384R1_KEY secp384r1_key;
#endif
#ifdef ENABLE_LMS #ifdef ENABLE_LMS
LMS_KEY lms_key; LMS_KEY lms_key;
HSS_KEY hss_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 #ifdef ENABLE_SECP256R1
int x509_key_set_secp256r1_key(X509_KEY *x509_key, const SECP256R1_KEY *secp256r1_key); int x509_key_set_secp256r1_key(X509_KEY *x509_key, const SECP256R1_KEY *secp256r1_key);
#endif #endif
#ifdef ENABLE_SECP384R1
int x509_key_set_secp384r1_key(X509_KEY *x509_key, const SECP384R1_KEY *secp384r1_key);
#endif
#ifdef ENABLE_LMS #ifdef ENABLE_LMS
int x509_key_set_lms_key(X509_KEY *x509_key, const LMS_KEY *lms_key); 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); 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); 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 // PKCS #8 PrivateKeyInfo
// PrivateKeyInfo.algor.parameters has the named_curve // PrivateKeyInfo.algor.parameters has the named_curve
// so omit the optional ECPrivateKey params(named_curve) a nd omit the public_key // so omit the optional ECPrivateKey params(named_curve) a nd omit the public_key

View File

@@ -8,9 +8,6 @@
*/ */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/ec.h> #include <gmssl/ec.h>
#include <gmssl/error.h> #include <gmssl/error.h>
@@ -21,12 +18,12 @@ static uint32_t oid_sm2[] = { oid_sm_scheme,301 };
#define oid_x9_62_curves oid_x9_62,3 #define oid_x9_62_curves oid_x9_62,3
#define oid_x9_62_prime_curves oid_x9_62_curves,1 #define oid_x9_62_prime_curves oid_x9_62_curves,1
static uint32_t oid_prime192v1[] = { oid_x9_62_prime_curves,1 }; static uint32_t oid_prime192v1[] = { oid_x9_62_prime_curves,1 };
static uint32_t oid_prime256v1[] = { oid_x9_62_prime_curves,7 }; // NIST P-256 static uint32_t oid_prime256v1[] = { oid_x9_62_prime_curves,7 };
#define oid_secg_curve 1,3,132,0 #define oid_secg_curve 1,3,132,0
static uint32_t oid_secp256k1[] = { oid_secg_curve,10 }; static uint32_t oid_secp256k1[] = { oid_secg_curve,10 };
static uint32_t oid_secp384r1[] = { oid_secg_curve,34 }; // NIST P-384 static uint32_t oid_secp384r1[] = { oid_secg_curve,34 };
static uint32_t oid_secp521r1[] = { oid_secg_curve,35 }; // NIST P-521 static uint32_t oid_secp521r1[] = { oid_secg_curve,35 };
static const ASN1_OID_INFO ec_named_curves[] = { static const ASN1_OID_INFO ec_named_curves[] = {
@@ -87,54 +84,3 @@ int ec_named_curve_from_der(int *oid, const uint8_t **in, size_t *inlen)
*oid = info->oid; *oid = info->oid;
return 1; return 1;
} }
int ec_point_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen)
{
const uint8_t *p;
size_t len;
if (asn1_octet_string_from_der(&p, &len, &d, &dlen) != 1) goto err;
format_bytes(fp, fmt, ind, label, p, len);
if (asn1_length_is_zero(dlen) != 1) goto err;
return 1;
err:
error_print();
return -1;
}
int ec_private_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen)
{
int ret;
const uint8_t *a;
size_t alen;
const uint8_t *p;
size_t len;
int val;
format_print(fp, fmt, ind, "%s\n", label);
ind += 4;
if (asn1_int_from_der(&val, &d, &dlen) != 1) goto err;
format_print(fp, fmt, ind, "version: %d\n", val);
if (asn1_octet_string_from_der(&p, &len, &d, &dlen) != 1) goto err;
format_bytes(fp, fmt, ind, "privateKey", p, len);
if ((ret = asn1_explicit_from_der(0, &a, &alen, &d, &dlen)) < 0) goto err;
else if (ret) {
if (ec_named_curve_from_der(&val, &a, &alen) != 1) goto err;
format_print(fp, fmt, ind, "parameters: %s\n", ec_named_curve_name(val));
if (asn1_length_is_zero(alen) != 1) goto err;
}
format_print(fp, fmt, ind, "publicKey\n");
ind += 4;
if ((ret = asn1_explicit_from_der(1, &a, &alen, &d, &dlen)) < 0) goto err;
else if (ret) {
if (asn1_bit_octets_from_der(&p, &len, &a, &alen) != 1) goto err;
format_bytes(fp, fmt, ind, "ECPoint", p, len);
if (asn1_length_is_zero(alen) != 1) goto err;
}
if (asn1_length_is_zero(dlen) != 1) goto err;
return 1;
err:
error_print();
return -1;
}

View File

@@ -1,114 +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
*/
#include <gmssl/ecdsa.h>
#ifdef ENABLE_SECP256R1
#include <gmssl/secp256r1_ecdsa.h>
#endif
#ifdef ENABLE_SECP384R1
#include <gmssl/secp384r1_ecdsa.h>
#endif
#include <gmssl/error.h>
int ecdsa_sign(const EC_KEY *key, const uint8_t *dgst, size_t dgstlen,
uint8_t *sig, size_t *siglen)
{
if (!key || !dgst || !sig || !siglen) {
error_print();
return -1;
}
switch (key->oid) {
#ifdef ENABLE_SECP256R1
case OID_secp256r1:
if (secp256r1_ecdsa_sign(&key->u.secp256r1_key, dgst, dgstlen, sig, siglen) != 1) {
error_print();
return -1;
}
return 1;
#endif
#ifdef ENABLE_SECP384R1
case OID_secp384r1:
if (secp384r1_ecdsa_sign(&key->u.secp384r1_key, dgst, dgstlen, sig, siglen) != 1) {
error_print();
return -1;
}
return 1;
#endif
default:
error_print();
return -1;
}
}
int ecdsa_sign_fixed_len(const EC_KEY *key, const uint8_t *dgst, size_t dgstlen,
size_t siglen, uint8_t *sig)
{
if (!key || !dgst || !sig || !siglen) {
error_print();
return -1;
}
switch (key->oid) {
#ifdef ENABLE_SECP256R1
case OID_secp256r1:
if (secp256r1_ecdsa_sign_fixlen(&key->u.secp256r1_key, dgst, dgstlen, siglen, sig) != 1) {
error_print();
return -1;
}
return 1;
#endif
#ifdef ENABLE_SECP384R1
case OID_secp384r1:
if (secp384r1_ecdsa_sign_fixlen(&key->u.secp384r1_key, dgst, dgstlen, siglen, sig) != 1) {
error_print();
return -1;
}
return 1;
#endif
default:
error_print();
return -1;
}
}
int ecdsa_verify(const EC_KEY *key, const uint8_t *dgst, size_t dgstlen,
const uint8_t *sig, size_t siglen)
{
if (!key || !dgst || !sig || !siglen) {
error_print();
return -1;
}
switch (key->oid) {
#ifdef ENABLE_SECP256R1
case OID_secp256r1:
{
int ret;
if ((ret = secp256r1_ecdsa_verify(&key->u.secp256r1_key, dgst, dgstlen, sig, siglen)) < 0) {
error_print();
return -1;
}
return ret;
}
#endif
#ifdef ENABLE_SECP384R1
case OID_secp384r1:
{
int ret;
if ((ret = secp384r1_ecdsa_verify(&key->u.secp384r1_key, dgst, dgstlen, sig, siglen)) < 0) {
error_print();
return -1;
}
return ret;
}
#endif
default:
error_print();
return -1;
}
}

View File

@@ -1,131 +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
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/mem.h>
#include <gmssl/error.h>
#ifdef ENABLE_SECP256R1
#include <gmssl/secp256r1_key.h>
#endif
#ifdef ENABLE_SECP384R1
#include <gmssl/secp384r1_key.h>
#endif
#ifdef ENABLE_SECP256R1
int secp256r1_do_ecdh(const SECP256R1_KEY *key, const SECP256R1_KEY *peer_key, uint8_t out[32])
{
SECP256R1_POINT point;
secp256r1_t x;
secp256r1_t y;
if (!key || !peer_key || !out) {
error_print();
return -1;
}
if (secp256r1_point_mul(&point, key->private_key, &peer_key->public_key) != 1
|| secp256r1_point_get_xy(&point, x, y) != 1
|| secp256r1_to_32bytes(x, out) != 1) {
error_print();
gmssl_secure_clear(&point, sizeof(SECP256R1_POINT));
return -1;
}
gmssl_secure_clear(&point, sizeof(SECP256R1_POINT));
gmssl_secure_clear(x, sizeof(secp256r1_t));
gmssl_secure_clear(y, sizeof(secp256r1_t));
return 1;
}
int secp256r1_ecdh(const SECP256R1_KEY *key, const uint8_t uncompressed_point[65], uint8_t out[32])
{
SECP256R1_POINT point;
secp256r1_t x;
secp256r1_t y;
if (!key || !uncompressed_point || !out) {
error_print();
return -1;
}
if (secp256r1_point_from_uncompressed_octets(&point, uncompressed_point) != 1) {
error_print();
return -1;
}
if (secp256r1_point_mul(&point, key->private_key, &point) != 1
|| secp256r1_point_get_xy(&point, x,y) != 1
|| secp256r1_to_32bytes(x, out) != 1) {
error_print();
gmssl_secure_clear(&point, sizeof(SECP256R1_POINT));
return -1;
}
gmssl_secure_clear(&point, sizeof(SECP256R1_POINT));
gmssl_secure_clear(x, sizeof(secp256r1_t));
gmssl_secure_clear(y, sizeof(secp256r1_t));
return 1;
}
#endif
#ifdef ENABLE_SECP384R1
int secp384r1_do_ecdh(const SECP384R1_KEY *key, const SECP384R1_KEY *peer_key, uint8_t out[48])
{
SECP384R1_POINT point;
secp384r1_t x;
secp384r1_t y;
if (!key || !peer_key || !out) {
error_print();
return -1;
}
if (secp384r1_point_mul(&point, key->private_key, &peer_key->public_key) != 1
|| secp384r1_point_get_xy(&point, x, y) != 1
|| secp384r1_to_48bytes(x, out) != 1) {
error_print();
gmssl_secure_clear(&point, sizeof(SECP384R1_POINT));
return -1;
}
gmssl_secure_clear(&point, sizeof(SECP384R1_POINT));
gmssl_secure_clear(x, sizeof(secp384r1_t));
gmssl_secure_clear(y, sizeof(secp384r1_t));
return 1;
}
int secp384r1_ecdh(const SECP384R1_KEY *key, const uint8_t uncompressed_point[97], uint8_t out[48])
{
SECP384R1_POINT point;
secp384r1_t x;
secp384r1_t y;
if (!key || !uncompressed_point || !out) {
error_print();
return -1;
}
if (secp384r1_point_from_uncompressed_octets(&point, uncompressed_point) != 1) {
error_print();
return -1;
}
if (secp384r1_point_mul(&point, key->private_key, &point) != 1
|| secp384r1_point_get_xy(&point, x, y) != 1
|| secp384r1_to_48bytes(x, out) != 1) {
error_print();
gmssl_secure_clear(&point, sizeof(SECP384R1_POINT));
return -1;
}
gmssl_secure_clear(&point, sizeof(SECP384R1_POINT));
gmssl_secure_clear(x, sizeof(secp384r1_t));
gmssl_secure_clear(y, sizeof(secp384r1_t));
return 1;
}
#endif

View File

@@ -1,481 +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
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/asn1.h>
#include <gmssl/digest.h>
#include <gmssl/sha2.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
#include <gmssl/secp256r1_ecdsa.h>
#include <gmssl/bn.h>
#include <gmssl/sm2.h>
int secp256r1_ecdsa_signature_print_ex(FILE *fp, int fmt, int ind, const char *label, const SECP256R1_ECDSA_SIGNATURE *sig)
{
format_print(fp, fmt, ind, "%s\n", label);
ind += 4;
if (secp256r1_print(fp, fmt, ind, "r", sig->r) != 1
|| secp256r1_print(fp, fmt, ind, "s", sig->s) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *sigbuf, size_t siglen)
{
SECP256R1_ECDSA_SIGNATURE sig;
if (secp256r1_ecdsa_signature_from_der(&sig, &sigbuf, &siglen) != 1) {
error_print();
return -1;
}
secp256r1_ecdsa_signature_print_ex(fp, fmt, ind, label, &sig);
if (siglen) {
error_print();
return -1;
}
return 1;
}
static int secp256r1_ecdsa_digest_to_e(secp256r1_t e, const uint8_t *dgst, size_t dgstlen)
{
uint8_t buf[SHA256_DIGEST_SIZE];
if (!dgst) {
error_print();
return -1;
}
if (dgstlen != SHA256_DIGEST_SIZE && dgstlen != SHA384_DIGEST_SIZE) {
error_print();
return -1;
}
memcpy(buf, dgst, sizeof(buf));
if (secp256r1_from_32bytes(e, buf) != 1
|| secp256r1_modn(e, e) != 1) {
error_print();
return -1;
}
return 1;
}
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)
{
secp256r1_t e;
secp256r1_t x1;
secp256r1_t y1;
secp256r1_t k_inv;
SECP256R1_POINT P;
// e = hash(m)
if (secp256r1_ecdsa_digest_to_e(e, dgst, dgstlen) != 1) {
error_print();
return -1;
}
// (x1, y1) = k*G
if (secp256r1_point_mul_generator(&P, k) != 1
|| secp256r1_point_get_xy(&P, x1, y1) != 1) {
error_print();
return -1;
}
// r = x1 mod n
if (secp256r1_modn(sig->r, x1) != 1) {
error_print();
return -1;
}
// s = k^-1 * (e + d * r) mod n
if (secp256r1_modn_inv(k_inv, k) != 1
|| secp256r1_modn_mul(sig->s, key->private_key, sig->r) != 1
|| secp256r1_modn_add(sig->s, sig->s, e) != 1
|| secp256r1_modn_mul(sig->s, sig->s, k_inv) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_do_sign(const SECP256R1_KEY *key,
const uint8_t *dgst, size_t dgstlen, SECP256R1_ECDSA_SIGNATURE *sig)
{
secp256r1_t k;
// rand k in [1, n-1]
do {
if (rand_bytes((uint8_t *)k, sizeof(k)) != 1) {
error_print();
return -1;
}
} while (secp256r1_is_zero(k) || secp256r1_cmp(k, SECP256R1_N) >= 0);
if (secp256r1_ecdsa_do_sign_ex(key, k, dgst, dgstlen, sig) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_do_verify(const SECP256R1_KEY *key,
const uint8_t *dgst, size_t dgstlen, const SECP256R1_ECDSA_SIGNATURE *sig)
{
secp256r1_t e;
secp256r1_t w;
secp256r1_t u1;
secp256r1_t u2;
secp256r1_t x1;
secp256r1_t y1;
SECP256R1_POINT P;
SECP256R1_POINT Q;
SECP256R1_POINT R;
// check r, s in [1, n-1]
if (secp256r1_is_zero(sig->r)
|| secp256r1_cmp(sig->r, SECP256R1_N) >= 0
|| secp256r1_is_zero(sig->s)
|| secp256r1_cmp(sig->s, SECP256R1_N) >= 0) {
error_print();
return -1;
}
// e = hash(m)
if (secp256r1_ecdsa_digest_to_e(e, dgst, dgstlen) != 1) {
error_print();
return -1;
}
// w = s^-1 (mod n)
if (secp256r1_modn_inv(w, sig->s) != 1) {
error_print();
return -1;
}
// u1 = e * w (mod n)
if (secp256r1_modn_mul(u1, e, w) != 1) {
error_print();
return -1;
}
// u2 = r * w (mod n)
if (secp256r1_modn_mul(u2, sig->r, w) != 1) {
error_print();
return -1;
}
// (x1, y1) = u1*G + u2*Q
if (secp256r1_point_mul_generator(&P, u1) != 1
|| secp256r1_point_mul(&Q, u2, &key->public_key) != 1
|| secp256r1_point_add(&R, &P, &Q) != 1) {
error_print();
return -1;
}
if (secp256r1_point_get_xy(&R, x1, y1) != 1) {
return 0;
}
// x1 = x1 mod n
if (secp256r1_modn(x1, x1) != 1) {
error_print();
return -1;
}
if (secp256r1_cmp(x1, sig->r) != 0) {
return 0;
}
return 1;
}
int secp256r1_ecdsa_signature_to_der(const SECP256R1_ECDSA_SIGNATURE *sig, uint8_t **out, size_t *outlen)
{
size_t len = 0;
uint8_t r[32];
uint8_t s[32];
if (!sig) {
return 0;
}
if (secp256r1_to_32bytes(sig->r, r) != 1
|| secp256r1_to_32bytes(sig->s, s) != 1) {
error_print();
return -1;
}
if (asn1_integer_to_der(r, 32, NULL, &len) != 1
|| asn1_integer_to_der(s, 32, NULL, &len) != 1
|| asn1_sequence_header_to_der(len, out, outlen) != 1
|| asn1_integer_to_der(r, 32, out, outlen) != 1
|| asn1_integer_to_der(s, 32, out, outlen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_signature_from_der(SECP256R1_ECDSA_SIGNATURE *sig, const uint8_t **in, size_t *inlen)
{
int ret;
const uint8_t *d;
const uint8_t *r;
const uint8_t *s;
size_t dlen, rlen, slen;
if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) {
if (ret < 0) error_print();
return ret;
}
if (asn1_integer_from_der(&r, &rlen, &d, &dlen) != 1
|| asn1_integer_from_der(&s, &slen, &d, &dlen) != 1
|| asn1_length_le(rlen, 32) != 1
|| asn1_length_le(slen, 32) != 1
|| asn1_length_is_zero(dlen) != 1) {
error_print();
return -1;
}
if (secp256r1_from_32bytes(sig->r, r) != 1
|| secp256r1_from_32bytes(sig->s, s) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_sign(const SECP256R1_KEY *key,
const uint8_t *dgst, size_t dgstlen, uint8_t *sigbuf, size_t *siglen)
{
SECP256R1_ECDSA_SIGNATURE sig;
if (secp256r1_ecdsa_do_sign(key, dgst, dgstlen, &sig) != 1) {
error_print();
return -1;
}
*siglen = 0;
if (secp256r1_ecdsa_signature_to_der(&sig, &sigbuf, siglen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_sign_fixlen(const SECP256R1_KEY *key,
const uint8_t *dgst, size_t dgstlen, size_t siglen, uint8_t *sig)
{
unsigned int trys = 200;
uint8_t buf[SECP256R1_ECDSA_SIGNATURE_MAX_SIZE];
size_t len;
switch (siglen) {
case SECP256R1_ECDSA_SIGNATURE_COMPACT_SIZE:
case SECP256R1_ECDSA_SIGNATURE_TYPICAL_SIZE:
case SECP256R1_ECDSA_SIGNATURE_MAX_SIZE:
break;
default:
error_print();
return -1;
}
while (trys--) {
if (secp256r1_ecdsa_sign(key, dgst, dgstlen, buf, &len) != 1) {
error_print();
return -1;
}
if (len == siglen) {
memcpy(sig, buf, len);
return 1;
}
}
// might caused by bad randomness
error_print();
return -1;
}
int secp256r1_ecdsa_verify(const SECP256R1_KEY *key,
const uint8_t *dgst, size_t dgstlen, const uint8_t *sigbuf, size_t siglen)
{
int ret;
SECP256R1_ECDSA_SIGNATURE sig;
if (secp256r1_ecdsa_signature_from_der(&sig, &sigbuf, &siglen) != 1) {
error_print();
return -1;
}
if (siglen) {
error_print();
return -1;
}
if ((ret = secp256r1_ecdsa_do_verify(key, dgst, dgstlen, &sig)) < 0) {
error_print();
return -1;
}
return ret;
}
int secp256r1_ecdsa_sign_init(SECP256R1_ECDSA_SIGN_CTX *ctx, const SECP256R1_KEY *key, const DIGEST *digest)
{
if (!ctx || !key) {
error_print();
return -1;
}
if (!digest) {
digest = DIGEST_sha256();
}
memset(ctx, 0, sizeof(SECP256R1_ECDSA_SIGN_CTX));
ctx->key = *key;
if (digest_init(&ctx->digest_ctx, digest) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_sign_update(SECP256R1_ECDSA_SIGN_CTX *ctx, const uint8_t *data, size_t datalen)
{
if (!ctx) {
error_print();
return -1;
}
if (digest_update(&ctx->digest_ctx, data, datalen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_sign_finish(SECP256R1_ECDSA_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen)
{
uint8_t dgst[DIGEST_MAX_SIZE];
size_t dgstlen;
if (!ctx || !sig || !siglen) {
error_print();
return -1;
}
if (digest_finish(&ctx->digest_ctx, dgst, &dgstlen) != 1) {
error_print();
return -1;
}
if (secp256r1_ecdsa_sign(&ctx->key, dgst, dgstlen, sig, siglen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_sign_finish_fixlen(SECP256R1_ECDSA_SIGN_CTX *ctx, size_t siglen, uint8_t *sig)
{
uint8_t dgst[DIGEST_MAX_SIZE];
size_t dgstlen;
if (!ctx || !sig || !siglen) {
error_print();
return -1;
}
if (digest_finish(&ctx->digest_ctx, dgst, &dgstlen) != 1) {
error_print();
return -1;
}
if (secp256r1_ecdsa_sign_fixlen(&ctx->key, dgst, dgstlen, siglen, sig) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_verify_init(SECP256R1_ECDSA_SIGN_CTX *ctx, const SECP256R1_KEY *key, const DIGEST *digest,
const uint8_t *sig, size_t siglen)
{
if (!ctx || !key || !sig || !siglen) {
error_print();
return -1;
}
if (secp256r1_ecdsa_signature_from_der(&ctx->sig, &sig, &siglen) != 1) {
error_print();
return -1;
}
if (siglen) {
error_print();
return -1;
}
ctx->key = *key;
if (!digest) {
digest = DIGEST_sha256();
}
if (digest_init(&ctx->digest_ctx, digest) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_verify_update(SECP256R1_ECDSA_SIGN_CTX *ctx, const uint8_t *data, size_t datalen)
{
if (!ctx) {
error_print();
return -1;
}
if (digest_update(&ctx->digest_ctx, data, datalen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_ecdsa_verify_finish(SECP256R1_ECDSA_SIGN_CTX *ctx)
{
uint8_t dgst[DIGEST_MAX_SIZE];
size_t dgstlen;
int ret;
if (!ctx) {
error_print();
return -1;
}
if (digest_finish(&ctx->digest_ctx, dgst, &dgstlen) != 1) {
error_print();
return -1;
}
if ((ret = secp256r1_ecdsa_do_verify(&ctx->key, dgst, dgstlen, &ctx->sig)) < 0) {
error_print();
return -1;
}
return ret;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,587 +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
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <gmssl/bn.h>
#include <gmssl/ec.h>
#include <gmssl/mem.h>
#include <gmssl/oid.h>
#include <gmssl/sm4.h>
#include <gmssl/rand.h>
#include <gmssl/asn1.h>
#include <gmssl/error.h>
#include <gmssl/pkcs8.h>
#include <gmssl/x509_alg.h>
#include <gmssl/secp256r1_key.h>
int secp256r1_key_generate(SECP256R1_KEY *key)
{
do {
if (rand_bytes((uint8_t *)key->private_key, sizeof(secp256r1_t)) != 1) {
error_print();
return -1;
}
} while (secp256r1_is_zero(key->private_key) || secp256r1_cmp(key->private_key, SECP256R1_N) >= 0);
if (secp256r1_point_mul_generator(&key->public_key, key->private_key) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_key_set_private_key(SECP256R1_KEY *key, const secp256r1_t private_key)
{
if (!key || !private_key) {
error_print();
return -1;
}
if (secp256r1_is_zero(private_key) || secp256r1_cmp(private_key, SECP256R1_N) >= 0) {
error_print();
return -1;
}
memset(key, 0, sizeof(SECP256R1_KEY));
if (secp256r1_copy(key->private_key, private_key) != 1
|| secp256r1_point_mul_generator(&key->public_key, key->private_key) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_public_key_equ(const SECP256R1_KEY *key, const SECP256R1_KEY *pub)
{
if (secp256r1_point_equ(&key->public_key, &pub->public_key) == 1) {
return 1;
} else {
return 0;
}
}
// SM2将这个命名为_to_octets应该更准确一些
int secp256r1_public_key_to_bytes(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen)
{
if (!key || !outlen) {
error_print();
return -1;
}
if (out && *out) {
if (secp256r1_point_to_uncompressed_octets(&key->public_key, *out) != 1) {
error_print();
return -1;
}
*out += 65;
}
*outlen += 65;
return 1;
}
int secp256r1_public_key_from_bytes(SECP256R1_KEY *key, const uint8_t **in, size_t *inlen)
{
if (!key || !in || !(*in) || !inlen) {
error_print();
return -1;
}
if (*inlen < 65) {
error_print();
return -1;
}
memset(key, 0, sizeof(SECP256R1_KEY));
if (secp256r1_point_from_uncompressed_octets(&key->public_key, *in) != 1) {
error_print();
return -1;
}
*in += 65;
*inlen -= 65;
return 1;
}
int secp256r1_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SECP256R1_KEY *key)
{
secp256r1_t x;
secp256r1_t y;
format_print(fp, fmt, ind, "%s\n", label);
ind += 4;
if (secp256r1_print(fp, fmt, ind, "X", key->public_key.X) != 1
|| secp256r1_print(fp, fmt, ind, "Y", key->public_key.Y) != 1
|| secp256r1_print(fp, fmt, ind, "Z", key->public_key.Z) != 1) {
error_print();
return -1;
}
if (secp256r1_point_get_xy(&key->public_key, x, y) != 1
|| secp256r1_print(fp, fmt, ind, "x", x) != 1
|| secp256r1_print(fp, fmt, ind, "y", y) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_private_key_print(FILE *fp, int fmt, int ind, const char *label, const SECP256R1_KEY *key)
{
uint8_t buf[32];
if (secp256r1_to_32bytes(key->private_key, buf) != 1) {
error_print();
return -1;
}
format_print(fp, fmt, ind, "%s\n", label);
ind += 4;
if (secp256r1_public_key_print(fp, fmt, ind, "public_key", key) != 1) {
error_print();
return -1;
}
format_bytes(fp, fmt, ind, "private_key", buf, 32);
return 1;
}
int secp256r1_public_key_to_der(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen)
{
uint8_t octets[65];
uint8_t *p = octets;
size_t len = 0;
if (!key) {
return 0;
}
// different from SM2
if (out && *out) {
if (secp256r1_public_key_to_bytes(key, &p, &len) != 1) {
error_print();
return -1;
}
}
if (asn1_bit_octets_to_der(octets, sizeof(octets), out, outlen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_public_key_from_der(SECP256R1_KEY *key, const uint8_t **in, size_t *inlen)
{
int ret;
const uint8_t *d;
size_t dlen;
if ((ret = asn1_bit_octets_from_der(&d, &dlen, in, inlen)) != 1) {
if (ret < 0) error_print();
return ret;
}
if (secp256r1_public_key_from_bytes(key, &d, &dlen) != 1) {
error_print();
return -1;
}
if (dlen) {
error_print();
return -1;
}
return 1;
}
// 这里应该提供public_key_info_to_pem 和SM2完全一样的功能
// 这样才可以生成证书
int secp256r1_private_key_to_der(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen)
{
int curve = OID_secp256r1;
uint8_t params[64];
uint8_t pubkey[128];
uint8_t *params_ptr = params;
uint8_t *pubkey_ptr = pubkey;
size_t params_len = 0;
size_t pubkey_len = 0;
uint8_t prikey[32];
size_t len = 0;
if (!key) {
error_print();
return -1;
}
if (ec_named_curve_to_der(curve, &params_ptr, &params_len) != 1
|| secp256r1_public_key_to_der(key, &pubkey_ptr, &pubkey_len) != 1) {
error_print();
return -1;
}
// fprintf(stderr, "%s %d: params_len = %zu\n", params_len);
// fprintf(stderr, "%s %d: pubkey_len = %zu\n", pubkey_len);
if (secp256r1_to_32bytes(key->private_key, prikey) != 1) {
error_print();
return -1;
}
if (asn1_int_to_der(EC_private_key_version, NULL, &len) != 1
|| asn1_octet_string_to_der(prikey, 32, NULL, &len) != 1
|| asn1_explicit_to_der(0, params, params_len, NULL, &len) != 1
|| asn1_explicit_to_der(1, pubkey, pubkey_len, NULL, &len) != 1
|| asn1_sequence_header_to_der(len, out, outlen) != 1
|| asn1_int_to_der(EC_private_key_version, out, outlen) != 1
|| asn1_octet_string_to_der(prikey, 32, out, outlen) != 1
|| asn1_explicit_to_der(0, params, params_len, out, outlen) != 1
|| asn1_explicit_to_der(1, pubkey, pubkey_len, out, outlen) != 1) {
gmssl_secure_clear(prikey, 32);
error_print();
return -1;
}
gmssl_secure_clear(prikey, 32);
return 1;
}
int secp256r1_private_key_from_der(SECP256R1_KEY *key, const uint8_t **in, size_t *inlen)
{
int ret;
const uint8_t *d;
size_t dlen;
int ver;
const uint8_t *prikey;
const uint8_t *params;
const uint8_t *pubkey;
size_t prikey_len, params_len, pubkey_len;
int curve;
SECP256R1_KEY tmp_key;
secp256r1_t private_key;
if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) {
if (ret < 0) error_print();
return ret;
}
if (asn1_int_from_der(&ver, &d, &dlen) != 1
|| asn1_octet_string_from_der(&prikey, &prikey_len, &d, &dlen) != 1
|| asn1_explicit_from_der(0, &params, &params_len, &d, &dlen) != 1
|| asn1_explicit_from_der(1, &pubkey, &pubkey_len, &d, &dlen) != 1
|| asn1_check(ver == EC_private_key_version) != 1
|| asn1_length_is_zero(dlen) != 1) {
error_print();
return -1;
}
if (!params || !pubkey) {
error_print();
return -1;
}
// public_key
if (ec_named_curve_from_der(&curve, &params, &params_len) != 1
|| asn1_check(curve == OID_secp256r1) != 1
|| asn1_length_is_zero(params_len) != 1) {
error_print();
return -1;
}
if (secp256r1_public_key_from_der(&tmp_key, &pubkey, &pubkey_len) != 1
|| asn1_length_is_zero(pubkey_len) != 1) {
error_print();
return -1;
}
// private_key
if (!prikey || prikey_len != 32) {
error_print();
return -1;
}
if (secp256r1_from_32bytes(private_key, prikey) != 1) {
error_print();
return -1;
}
if (secp256r1_key_set_private_key(key, private_key) != 1) {
gmssl_secure_clear(private_key, 32);
error_print();
return -1;
}
gmssl_secure_clear(private_key, 32);
// check
if (secp256r1_public_key_equ(key, &tmp_key) != 1) {
gmssl_secure_clear(key, sizeof(SECP256R1_KEY));
error_print();
return -1;
}
return 1;
}
int secp256r1_private_key_info_to_der(const SECP256R1_KEY *key, uint8_t **out, size_t *outlen)
{
int algor = OID_ec_public_key;
int algor_param = OID_secp256r1;
size_t len = 0;
uint8_t prikey[256];
uint8_t *p = prikey;
size_t prikey_len = 0;
if (secp256r1_private_key_to_der(key, &p, &prikey_len) != 1) {
error_print();
return -1;
}
//fprintf(stderr, "%s %d: prikey_len = %zu\n", __FILE__, __LINE__, prikey_len);
if (asn1_int_to_der(PKCS8_private_key_info_version, NULL, &len) != 1
|| x509_public_key_algor_to_der(algor, algor_param, NULL, &len) != 1
|| asn1_octet_string_to_der(prikey, prikey_len, NULL, &len) != 1
|| asn1_sequence_header_to_der(len, out, outlen) != 1
|| asn1_int_to_der(PKCS8_private_key_info_version, out, outlen) != 1
|| x509_public_key_algor_to_der(algor, algor_param, out, outlen) != 1
|| asn1_octet_string_to_der(prikey, prikey_len, out, outlen) != 1) {
memset(prikey, 0, sizeof(prikey));
error_print();
return -1;
}
memset(prikey, 0, sizeof(prikey));
return 1;
}
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 ret;
const uint8_t *d;
size_t dlen;
int version;
int algor;
int algor_param;
const uint8_t *prikey;
size_t prikey_len;
if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) {
if (ret < 0) error_print();
return ret;
}
if (asn1_int_from_der(&version, &d, &dlen) != 1
|| x509_public_key_algor_from_der(&algor, &algor_param, &d, &dlen) != 1
|| asn1_octet_string_from_der(&prikey, &prikey_len, &d, &dlen) != 1
|| asn1_implicit_set_from_der(0, attrs, attrslen, &d, &dlen) < 0
|| asn1_length_is_zero(dlen) != 1) {
error_print();
return -1;
}
if (asn1_check(version == PKCS8_private_key_info_version) != 1
|| asn1_check(algor == OID_ec_public_key) != 1
|| asn1_check(algor_param == OID_secp256r1) != 1
|| secp256r1_private_key_from_der(key, &prikey, &prikey_len) != 1
|| asn1_length_is_zero(prikey_len) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_private_key_info_encrypt_to_der(const SECP256R1_KEY *ec_key, const char *pass,
uint8_t **out, size_t *outlen)
{
int ret = -1;
uint8_t pkey_info[512];
uint8_t *p = pkey_info;
size_t pkey_info_len = 0;
uint8_t salt[16];
int iter = 65536;
uint8_t iv[16];
uint8_t key[16];
SM4_KEY sm4_key;
uint8_t enced_pkey_info[sizeof(pkey_info) + 32];
size_t enced_pkey_info_len;
if (!ec_key || !pass || !outlen) {
error_print();
return -1;
}
if (secp256r1_private_key_info_to_der(ec_key, &p, &pkey_info_len) != 1
|| rand_bytes(salt, sizeof(salt)) != 1
|| rand_bytes(iv, sizeof(iv)) != 1
|| sm3_pbkdf2(pass, strlen(pass), salt, sizeof(salt), iter, sizeof(key), key) != 1) {
error_print();
goto end;
}
/*
if (pkey_info_len != sizeof(pkey_info)) {
error_print();
goto end;
}
*/
sm4_set_encrypt_key(&sm4_key, key);
if (sm4_cbc_padding_encrypt(
&sm4_key, iv, pkey_info, pkey_info_len,
enced_pkey_info, &enced_pkey_info_len) != 1
|| pkcs8_enced_private_key_info_to_der(
salt, sizeof(salt), iter, sizeof(key), OID_hmac_sm3,
OID_sm4_cbc, iv, sizeof(iv),
enced_pkey_info, enced_pkey_info_len, out, outlen) != 1) {
error_print();
goto end;
}
ret = 1;
end:
gmssl_secure_clear(pkey_info, sizeof(pkey_info));
gmssl_secure_clear(key, sizeof(key));
gmssl_secure_clear(&sm4_key, sizeof(sm4_key));
return ret;
}
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 ret = -1;
const uint8_t *salt;
size_t saltlen;
int iter;
int keylen;
int prf;
int cipher;
const uint8_t *iv;
size_t ivlen;
uint8_t key[16];
SM4_KEY sm4_key;
const uint8_t *enced_pkey_info;
size_t enced_pkey_info_len;
uint8_t pkey_info[256];
const uint8_t *cp = pkey_info;
size_t pkey_info_len;
if (!ec_key || !attrs || !attrs_len || !pass || !in || !(*in) || !inlen) {
error_print();
return -1;
}
if (pkcs8_enced_private_key_info_from_der(&salt, &saltlen, &iter, &keylen, &prf,
&cipher, &iv, &ivlen, &enced_pkey_info, &enced_pkey_info_len, in, inlen) != 1
|| asn1_check(keylen == -1 || keylen == 16) != 1
|| asn1_check(prf == - 1 || prf == OID_hmac_sm3) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == 16) != 1
|| asn1_length_le(enced_pkey_info_len, sizeof(pkey_info)) != 1) {
error_print();
return -1;
}
if (sm3_pbkdf2(pass, strlen(pass), salt, saltlen, iter, sizeof(key), key) != 1) {
error_print();
goto end;
}
sm4_set_decrypt_key(&sm4_key, key);
if (sm4_cbc_padding_decrypt(&sm4_key, iv, enced_pkey_info, enced_pkey_info_len,
pkey_info, &pkey_info_len) != 1
|| secp256r1_private_key_info_from_der(ec_key, attrs, attrs_len, &cp, &pkey_info_len) != 1
|| asn1_length_is_zero(pkey_info_len) != 1) {
error_print();
goto end;
}
ret = 1;
end:
gmssl_secure_clear(&sm4_key, sizeof(sm4_key));
gmssl_secure_clear(key, sizeof(key));
gmssl_secure_clear(pkey_info, sizeof(pkey_info));
return ret;
}
int secp256r1_private_key_info_encrypt_to_pem(const SECP256R1_KEY *key, const char *pass, FILE *fp)
{
uint8_t buf[1024];
uint8_t *p = buf;
size_t len = 0;
if (!fp) {
error_print();
return -1;
}
if (secp256r1_private_key_info_encrypt_to_der(key, pass, &p, &len) != 1) {
error_print();
return -1;
}
if (pem_write(fp, "ENCRYPTED PRIVATE KEY", buf, len) != 1) {
error_print();
return -1;
}
return 1;
}
int secp256r1_private_key_info_decrypt_from_pem(SECP256R1_KEY *key, const char *pass, FILE *fp)
{
uint8_t buf[512];
const uint8_t *cp = buf;
size_t len;
const uint8_t *attrs;
size_t attrs_len;
if (!key || !pass || !fp) {
error_print();
return -1;
}
if (pem_read(fp, "ENCRYPTED PRIVATE KEY", buf, &len, sizeof(buf)) != 1
|| secp256r1_private_key_info_decrypt_from_der(key, &attrs, &attrs_len, pass, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
return 1;
}
// FIXME: side-channel of Base64
int secp256r1_private_key_to_pem(const SECP256R1_KEY *a, FILE *fp)
{
uint8_t buf[512];
uint8_t *p = buf;
size_t len = 0;
if (secp256r1_private_key_to_der(a, &p, &len) != 1) {
error_print();
return -1;
}
if (pem_write(fp, "EC PRIVATE KEY", buf, len) <= 0) {
error_print();
return -1;
}
return 1;
}
int secp256r1_private_key_from_pem(SECP256R1_KEY *a, FILE *fp)
{
uint8_t buf[512];
const uint8_t *cp = buf;
size_t len;
if (pem_read(fp, "EC PRIVATE KEY", buf, &len, sizeof(buf)) != 1) {
error_print();
return -1;
}
if (secp256r1_private_key_from_der(a, &cp, &len) != 1
|| len > 0) {
error_print();
return -1;
}
return 1;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,489 +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
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/asn1.h>
#include <gmssl/digest.h>
#include <gmssl/sha2.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
#include <gmssl/secp384r1_ecdsa.h>
#include <gmssl/bn.h>
#include <gmssl/sm2.h>
int secp384r1_ecdsa_signature_print_ex(FILE *fp, int fmt, int ind, const char *label, const SECP384R1_ECDSA_SIGNATURE *sig)
{
format_print(fp, fmt, ind, "%s\n", label);
ind += 4;
if (secp384r1_print(fp, fmt, ind, "r", sig->r) != 1
|| secp384r1_print(fp, fmt, ind, "s", sig->s) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *sigbuf, size_t siglen)
{
SECP384R1_ECDSA_SIGNATURE sig;
if (secp384r1_ecdsa_signature_from_der(&sig, &sigbuf, &siglen) != 1) {
error_print();
return -1;
}
secp384r1_ecdsa_signature_print_ex(fp, fmt, ind, label, &sig);
if (siglen) {
error_print();
return -1;
}
return 1;
}
static int secp384r1_ecdsa_digest_to_e(secp384r1_t e, const uint8_t *dgst, size_t dgstlen)
{
uint8_t buf[SHA384_DIGEST_SIZE];
if (!dgst) {
error_print();
return -1;
}
if (dgstlen == SHA256_DIGEST_SIZE) {
memset(buf, 0, SHA384_DIGEST_SIZE - SHA256_DIGEST_SIZE);
memcpy(buf + SHA384_DIGEST_SIZE - SHA256_DIGEST_SIZE, dgst, SHA256_DIGEST_SIZE);
} else if (dgstlen == SHA384_DIGEST_SIZE) {
memcpy(buf, dgst, sizeof(buf));
} else {
error_print();
return -1;
}
if (secp384r1_from_48bytes(e, buf) != 1
|| secp384r1_modn(e, e) != 1) {
error_print();
return -1;
}
return 1;
}
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)
{
secp384r1_t e;
secp384r1_t x1;
secp384r1_t y1;
secp384r1_t k_inv;
SECP384R1_POINT P;
// e = hash(m)
if (secp384r1_ecdsa_digest_to_e(e, dgst, dgstlen) != 1) {
error_print();
return -1;
}
// (x1, y1) = k*G
if (secp384r1_point_mul_generator(&P, k) != 1
|| secp384r1_point_get_xy(&P, x1, y1) != 1) {
error_print();
return -1;
}
// r = x1 mod n
if (secp384r1_modn(sig->r, x1) != 1) {
error_print();
return -1;
}
// s = k^-1 * (e + d * r) mod n
if (secp384r1_modn_inv(k_inv, k) != 1
|| secp384r1_modn_mul(sig->s, key->private_key, sig->r) != 1
|| secp384r1_modn_add(sig->s, sig->s, e) != 1
|| secp384r1_modn_mul(sig->s, sig->s, k_inv) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_do_sign(const SECP384R1_KEY *key,
const uint8_t *dgst, size_t dgstlen, SECP384R1_ECDSA_SIGNATURE *sig)
{
secp384r1_t k;
// rand k in [1, n-1]
do {
if (rand_bytes((uint8_t *)k, sizeof(k)) != 1) {
error_print();
return -1;
}
} while (secp384r1_is_zero(k) || secp384r1_cmp(k, SECP384R1_N) >= 0);
if (secp384r1_ecdsa_do_sign_ex(key, k, dgst, dgstlen, sig) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_do_verify(const SECP384R1_KEY *key,
const uint8_t *dgst, size_t dgstlen, const SECP384R1_ECDSA_SIGNATURE *sig)
{
secp384r1_t e;
secp384r1_t w;
secp384r1_t u1;
secp384r1_t u2;
secp384r1_t x1;
secp384r1_t y1;
SECP384R1_POINT P;
SECP384R1_POINT Q;
SECP384R1_POINT R;
// check r, s in [1, n-1]
if (secp384r1_is_zero(sig->r)
|| secp384r1_cmp(sig->r, SECP384R1_N) >= 0
|| secp384r1_is_zero(sig->s)
|| secp384r1_cmp(sig->s, SECP384R1_N) >= 0) {
error_print();
return -1;
}
// e = hash(m)
if (secp384r1_ecdsa_digest_to_e(e, dgst, dgstlen) != 1) {
error_print();
return -1;
}
// w = s^-1 (mod n)
if (secp384r1_modn_inv(w, sig->s) != 1) {
error_print();
return -1;
}
// u1 = e * w (mod n)
if (secp384r1_modn_mul(u1, e, w) != 1) {
error_print();
return -1;
}
// u2 = r * w (mod n)
if (secp384r1_modn_mul(u2, sig->r, w) != 1) {
error_print();
return -1;
}
// (x1, y1) = u1*G + u2*Q
if (secp384r1_point_mul_generator(&P, u1) != 1
|| secp384r1_point_mul(&Q, u2, &key->public_key) != 1
|| secp384r1_point_add(&R, &P, &Q) != 1) {
error_print();
return -1;
}
if (secp384r1_point_get_xy(&R, x1, y1) != 1) {
return 0;
}
// x1 = x1 mod n
if (secp384r1_modn(x1, x1) != 1) {
error_print();
return -1;
}
if (secp384r1_cmp(x1, sig->r) != 0) {
return 0;
}
return 1;
}
int secp384r1_ecdsa_signature_to_der(const SECP384R1_ECDSA_SIGNATURE *sig, uint8_t **out, size_t *outlen)
{
size_t len = 0;
uint8_t r[48];
uint8_t s[48];
if (!sig) {
return 0;
}
if (secp384r1_to_48bytes(sig->r, r) != 1
|| secp384r1_to_48bytes(sig->s, s) != 1) {
error_print();
return -1;
}
if (asn1_integer_to_der(r, 48, NULL, &len) != 1
|| asn1_integer_to_der(s, 48, NULL, &len) != 1
|| asn1_sequence_header_to_der(len, out, outlen) != 1
|| asn1_integer_to_der(r, 48, out, outlen) != 1
|| asn1_integer_to_der(s, 48, out, outlen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_signature_from_der(SECP384R1_ECDSA_SIGNATURE *sig, const uint8_t **in, size_t *inlen)
{
int ret;
const uint8_t *d;
const uint8_t *r;
const uint8_t *s;
uint8_t rbuf[48] = {0};
uint8_t sbuf[48] = {0};
size_t dlen, rlen, slen;
if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) {
if (ret < 0) error_print();
return ret;
}
if (asn1_integer_from_der(&r, &rlen, &d, &dlen) != 1
|| asn1_integer_from_der(&s, &slen, &d, &dlen) != 1
|| asn1_length_le(rlen, 48) != 1
|| asn1_length_le(slen, 48) != 1
|| asn1_length_is_zero(dlen) != 1) {
error_print();
return -1;
}
memcpy(rbuf + sizeof(rbuf) - rlen, r, rlen);
memcpy(sbuf + sizeof(sbuf) - slen, s, slen);
if (secp384r1_from_48bytes(sig->r, rbuf) != 1
|| secp384r1_from_48bytes(sig->s, sbuf) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_sign(const SECP384R1_KEY *key,
const uint8_t *dgst, size_t dgstlen, uint8_t *sigbuf, size_t *siglen)
{
SECP384R1_ECDSA_SIGNATURE sig;
if (secp384r1_ecdsa_do_sign(key, dgst, dgstlen, &sig) != 1) {
error_print();
return -1;
}
*siglen = 0;
if (secp384r1_ecdsa_signature_to_der(&sig, &sigbuf, siglen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_sign_fixlen(const SECP384R1_KEY *key,
const uint8_t *dgst, size_t dgstlen, size_t siglen, uint8_t *sig)
{
unsigned int trys = 200;
uint8_t buf[SECP384R1_ECDSA_SIGNATURE_MAX_SIZE];
size_t len;
switch (siglen) {
case SECP384R1_ECDSA_SIGNATURE_COMPACT_SIZE:
case SECP384R1_ECDSA_SIGNATURE_TYPICAL_SIZE:
case SECP384R1_ECDSA_SIGNATURE_MAX_SIZE:
break;
default:
error_print();
return -1;
}
while (trys--) {
if (secp384r1_ecdsa_sign(key, dgst, dgstlen, buf, &len) != 1) {
error_print();
return -1;
}
if (len == siglen) {
memcpy(sig, buf, len);
return 1;
}
}
// might caused by bad randomness
error_print();
return -1;
}
int secp384r1_ecdsa_verify(const SECP384R1_KEY *key,
const uint8_t *dgst, size_t dgstlen, const uint8_t *sigbuf, size_t siglen)
{
int ret;
SECP384R1_ECDSA_SIGNATURE sig;
if (secp384r1_ecdsa_signature_from_der(&sig, &sigbuf, &siglen) != 1) {
error_print();
return -1;
}
if (siglen) {
error_print();
return -1;
}
if ((ret = secp384r1_ecdsa_do_verify(key, dgst, dgstlen, &sig)) < 0) {
error_print();
return -1;
}
return ret;
}
int secp384r1_ecdsa_sign_init(SECP384R1_ECDSA_SIGN_CTX *ctx, const SECP384R1_KEY *key, const DIGEST *digest)
{
if (!ctx || !key) {
error_print();
return -1;
}
if (!digest) {
digest = DIGEST_sha384();
}
memset(ctx, 0, sizeof(SECP384R1_ECDSA_SIGN_CTX));
ctx->key = *key;
if (digest_init(&ctx->digest_ctx, digest) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_sign_update(SECP384R1_ECDSA_SIGN_CTX *ctx, const uint8_t *data, size_t datalen)
{
if (!ctx) {
error_print();
return -1;
}
if (digest_update(&ctx->digest_ctx, data, datalen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_sign_finish(SECP384R1_ECDSA_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen)
{
uint8_t dgst[DIGEST_MAX_SIZE];
size_t dgstlen;
if (!ctx || !sig || !siglen) {
error_print();
return -1;
}
if (digest_finish(&ctx->digest_ctx, dgst, &dgstlen) != 1) {
error_print();
return -1;
}
if (secp384r1_ecdsa_sign(&ctx->key, dgst, dgstlen, sig, siglen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_sign_finish_fixlen(SECP384R1_ECDSA_SIGN_CTX *ctx, size_t siglen, uint8_t *sig)
{
uint8_t dgst[DIGEST_MAX_SIZE];
size_t dgstlen;
if (!ctx || !sig || !siglen) {
error_print();
return -1;
}
if (digest_finish(&ctx->digest_ctx, dgst, &dgstlen) != 1) {
error_print();
return -1;
}
if (secp384r1_ecdsa_sign_fixlen(&ctx->key, dgst, dgstlen, siglen, sig) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_verify_init(SECP384R1_ECDSA_SIGN_CTX *ctx, const SECP384R1_KEY *key, const DIGEST *digest,
const uint8_t *sig, size_t siglen)
{
if (!ctx || !key || !sig || !siglen) {
error_print();
return -1;
}
if (secp384r1_ecdsa_signature_from_der(&ctx->sig, &sig, &siglen) != 1) {
error_print();
return -1;
}
if (siglen) {
error_print();
return -1;
}
ctx->key = *key;
if (!digest) {
digest = DIGEST_sha384();
}
if (digest_init(&ctx->digest_ctx, digest) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_verify_update(SECP384R1_ECDSA_SIGN_CTX *ctx, const uint8_t *data, size_t datalen)
{
if (!ctx) {
error_print();
return -1;
}
if (digest_update(&ctx->digest_ctx, data, datalen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_ecdsa_verify_finish(SECP384R1_ECDSA_SIGN_CTX *ctx)
{
uint8_t dgst[DIGEST_MAX_SIZE];
size_t dgstlen;
int ret;
if (!ctx) {
error_print();
return -1;
}
if (digest_finish(&ctx->digest_ctx, dgst, &dgstlen) != 1) {
error_print();
return -1;
}
if ((ret = secp384r1_ecdsa_do_verify(&ctx->key, dgst, dgstlen, &ctx->sig)) < 0) {
error_print();
return -1;
}
return ret;
}

View File

@@ -1,587 +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
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <gmssl/bn.h>
#include <gmssl/ec.h>
#include <gmssl/mem.h>
#include <gmssl/oid.h>
#include <gmssl/sm4.h>
#include <gmssl/rand.h>
#include <gmssl/asn1.h>
#include <gmssl/error.h>
#include <gmssl/pkcs8.h>
#include <gmssl/x509_alg.h>
#include <gmssl/secp384r1_key.h>
int secp384r1_key_generate(SECP384R1_KEY *key)
{
do {
if (rand_bytes((uint8_t *)key->private_key, sizeof(secp384r1_t)) != 1) {
error_print();
return -1;
}
} while (secp384r1_is_zero(key->private_key) || secp384r1_cmp(key->private_key, SECP384R1_N) >= 0);
if (secp384r1_point_mul_generator(&key->public_key, key->private_key) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_key_set_private_key(SECP384R1_KEY *key, const secp384r1_t private_key)
{
if (!key || !private_key) {
error_print();
return -1;
}
if (secp384r1_is_zero(private_key) || secp384r1_cmp(private_key, SECP384R1_N) >= 0) {
error_print();
return -1;
}
memset(key, 0, sizeof(SECP384R1_KEY));
if (secp384r1_copy(key->private_key, private_key) != 1
|| secp384r1_point_mul_generator(&key->public_key, key->private_key) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_public_key_equ(const SECP384R1_KEY *key, const SECP384R1_KEY *pub)
{
if (secp384r1_point_equ(&key->public_key, &pub->public_key) == 1) {
return 1;
} else {
return 0;
}
}
// SM2将这个命名为_to_octets应该更准确一些
int secp384r1_public_key_to_bytes(const SECP384R1_KEY *key, uint8_t **out, size_t *outlen)
{
if (!key || !outlen) {
error_print();
return -1;
}
if (out && *out) {
if (secp384r1_point_to_uncompressed_octets(&key->public_key, *out) != 1) {
error_print();
return -1;
}
*out += 97;
}
*outlen += 97;
return 1;
}
int secp384r1_public_key_from_bytes(SECP384R1_KEY *key, const uint8_t **in, size_t *inlen)
{
if (!key || !in || !(*in) || !inlen) {
error_print();
return -1;
}
if (*inlen < 97) {
error_print();
return -1;
}
memset(key, 0, sizeof(SECP384R1_KEY));
if (secp384r1_point_from_uncompressed_octets(&key->public_key, *in) != 1) {
error_print();
return -1;
}
*in += 97;
*inlen -= 97;
return 1;
}
int secp384r1_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SECP384R1_KEY *key)
{
secp384r1_t x;
secp384r1_t y;
format_print(fp, fmt, ind, "%s\n", label);
ind += 4;
if (secp384r1_print(fp, fmt, ind, "X", key->public_key.X) != 1
|| secp384r1_print(fp, fmt, ind, "Y", key->public_key.Y) != 1
|| secp384r1_print(fp, fmt, ind, "Z", key->public_key.Z) != 1) {
error_print();
return -1;
}
if (secp384r1_point_get_xy(&key->public_key, x, y) != 1
|| secp384r1_print(fp, fmt, ind, "x", x) != 1
|| secp384r1_print(fp, fmt, ind, "y", y) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_private_key_print(FILE *fp, int fmt, int ind, const char *label, const SECP384R1_KEY *key)
{
uint8_t buf[48];
if (secp384r1_to_48bytes(key->private_key, buf) != 1) {
error_print();
return -1;
}
format_print(fp, fmt, ind, "%s\n", label);
ind += 4;
if (secp384r1_public_key_print(fp, fmt, ind, "public_key", key) != 1) {
error_print();
return -1;
}
format_bytes(fp, fmt, ind, "private_key", buf, 48);
return 1;
}
int secp384r1_public_key_to_der(const SECP384R1_KEY *key, uint8_t **out, size_t *outlen)
{
uint8_t octets[97];
uint8_t *p = octets;
size_t len = 0;
if (!key) {
return 0;
}
// different from SM2
if (out && *out) {
if (secp384r1_public_key_to_bytes(key, &p, &len) != 1) {
error_print();
return -1;
}
}
if (asn1_bit_octets_to_der(octets, sizeof(octets), out, outlen) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_public_key_from_der(SECP384R1_KEY *key, const uint8_t **in, size_t *inlen)
{
int ret;
const uint8_t *d;
size_t dlen;
if ((ret = asn1_bit_octets_from_der(&d, &dlen, in, inlen)) != 1) {
if (ret < 0) error_print();
return ret;
}
if (secp384r1_public_key_from_bytes(key, &d, &dlen) != 1) {
error_print();
return -1;
}
if (dlen) {
error_print();
return -1;
}
return 1;
}
// 这里应该提供public_key_info_to_pem 和SM2完全一样的功能
// 这样才可以生成证书
int secp384r1_private_key_to_der(const SECP384R1_KEY *key, uint8_t **out, size_t *outlen)
{
int curve = OID_secp384r1;
uint8_t params[64];
uint8_t pubkey[128];
uint8_t *params_ptr = params;
uint8_t *pubkey_ptr = pubkey;
size_t params_len = 0;
size_t pubkey_len = 0;
uint8_t prikey[48];
size_t len = 0;
if (!key) {
error_print();
return -1;
}
if (ec_named_curve_to_der(curve, &params_ptr, &params_len) != 1
|| secp384r1_public_key_to_der(key, &pubkey_ptr, &pubkey_len) != 1) {
error_print();
return -1;
}
// fprintf(stderr, "%s %d: params_len = %zu\n", params_len);
// fprintf(stderr, "%s %d: pubkey_len = %zu\n", pubkey_len);
if (secp384r1_to_48bytes(key->private_key, prikey) != 1) {
error_print();
return -1;
}
if (asn1_int_to_der(EC_private_key_version, NULL, &len) != 1
|| asn1_octet_string_to_der(prikey, 48, NULL, &len) != 1
|| asn1_explicit_to_der(0, params, params_len, NULL, &len) != 1
|| asn1_explicit_to_der(1, pubkey, pubkey_len, NULL, &len) != 1
|| asn1_sequence_header_to_der(len, out, outlen) != 1
|| asn1_int_to_der(EC_private_key_version, out, outlen) != 1
|| asn1_octet_string_to_der(prikey, 48, out, outlen) != 1
|| asn1_explicit_to_der(0, params, params_len, out, outlen) != 1
|| asn1_explicit_to_der(1, pubkey, pubkey_len, out, outlen) != 1) {
gmssl_secure_clear(prikey, 48);
error_print();
return -1;
}
gmssl_secure_clear(prikey, 48);
return 1;
}
int secp384r1_private_key_from_der(SECP384R1_KEY *key, const uint8_t **in, size_t *inlen)
{
int ret;
const uint8_t *d;
size_t dlen;
int ver;
const uint8_t *prikey;
const uint8_t *params;
const uint8_t *pubkey;
size_t prikey_len, params_len, pubkey_len;
int curve;
SECP384R1_KEY tmp_key;
secp384r1_t private_key;
if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) {
if (ret < 0) error_print();
return ret;
}
if (asn1_int_from_der(&ver, &d, &dlen) != 1
|| asn1_octet_string_from_der(&prikey, &prikey_len, &d, &dlen) != 1
|| asn1_explicit_from_der(0, &params, &params_len, &d, &dlen) != 1
|| asn1_explicit_from_der(1, &pubkey, &pubkey_len, &d, &dlen) != 1
|| asn1_check(ver == EC_private_key_version) != 1
|| asn1_length_is_zero(dlen) != 1) {
error_print();
return -1;
}
if (!params || !pubkey) {
error_print();
return -1;
}
// public_key
if (ec_named_curve_from_der(&curve, &params, &params_len) != 1
|| asn1_check(curve == OID_secp384r1) != 1
|| asn1_length_is_zero(params_len) != 1) {
error_print();
return -1;
}
if (secp384r1_public_key_from_der(&tmp_key, &pubkey, &pubkey_len) != 1
|| asn1_length_is_zero(pubkey_len) != 1) {
error_print();
return -1;
}
// private_key
if (!prikey || prikey_len != 48) {
error_print();
return -1;
}
if (secp384r1_from_48bytes(private_key, prikey) != 1) {
error_print();
return -1;
}
if (secp384r1_key_set_private_key(key, private_key) != 1) {
gmssl_secure_clear(private_key, 48);
error_print();
return -1;
}
gmssl_secure_clear(private_key, 48);
// check
if (secp384r1_public_key_equ(key, &tmp_key) != 1) {
gmssl_secure_clear(key, sizeof(SECP384R1_KEY));
error_print();
return -1;
}
return 1;
}
int secp384r1_private_key_info_to_der(const SECP384R1_KEY *key, uint8_t **out, size_t *outlen)
{
int algor = OID_ec_public_key;
int algor_param = OID_secp384r1;
size_t len = 0;
uint8_t prikey[256];
uint8_t *p = prikey;
size_t prikey_len = 0;
if (secp384r1_private_key_to_der(key, &p, &prikey_len) != 1) {
error_print();
return -1;
}
//fprintf(stderr, "%s %d: prikey_len = %zu\n", __FILE__, __LINE__, prikey_len);
if (asn1_int_to_der(PKCS8_private_key_info_version, NULL, &len) != 1
|| x509_public_key_algor_to_der(algor, algor_param, NULL, &len) != 1
|| asn1_octet_string_to_der(prikey, prikey_len, NULL, &len) != 1
|| asn1_sequence_header_to_der(len, out, outlen) != 1
|| asn1_int_to_der(PKCS8_private_key_info_version, out, outlen) != 1
|| x509_public_key_algor_to_der(algor, algor_param, out, outlen) != 1
|| asn1_octet_string_to_der(prikey, prikey_len, out, outlen) != 1) {
memset(prikey, 0, sizeof(prikey));
error_print();
return -1;
}
memset(prikey, 0, sizeof(prikey));
return 1;
}
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 ret;
const uint8_t *d;
size_t dlen;
int version;
int algor;
int algor_param;
const uint8_t *prikey;
size_t prikey_len;
if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) {
if (ret < 0) error_print();
return ret;
}
if (asn1_int_from_der(&version, &d, &dlen) != 1
|| x509_public_key_algor_from_der(&algor, &algor_param, &d, &dlen) != 1
|| asn1_octet_string_from_der(&prikey, &prikey_len, &d, &dlen) != 1
|| asn1_implicit_set_from_der(0, attrs, attrslen, &d, &dlen) < 0
|| asn1_length_is_zero(dlen) != 1) {
error_print();
return -1;
}
if (asn1_check(version == PKCS8_private_key_info_version) != 1
|| asn1_check(algor == OID_ec_public_key) != 1
|| asn1_check(algor_param == OID_secp384r1) != 1
|| secp384r1_private_key_from_der(key, &prikey, &prikey_len) != 1
|| asn1_length_is_zero(prikey_len) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_private_key_info_encrypt_to_der(const SECP384R1_KEY *ec_key, const char *pass,
uint8_t **out, size_t *outlen)
{
int ret = -1;
uint8_t pkey_info[512];
uint8_t *p = pkey_info;
size_t pkey_info_len = 0;
uint8_t salt[16];
int iter = 65536;
uint8_t iv[16];
uint8_t key[16];
SM4_KEY sm4_key;
uint8_t enced_pkey_info[sizeof(pkey_info) + 32];
size_t enced_pkey_info_len;
if (!ec_key || !pass || !outlen) {
error_print();
return -1;
}
if (secp384r1_private_key_info_to_der(ec_key, &p, &pkey_info_len) != 1
|| rand_bytes(salt, sizeof(salt)) != 1
|| rand_bytes(iv, sizeof(iv)) != 1
|| sm3_pbkdf2(pass, strlen(pass), salt, sizeof(salt), iter, sizeof(key), key) != 1) {
error_print();
goto end;
}
/*
if (pkey_info_len != sizeof(pkey_info)) {
error_print();
goto end;
}
*/
sm4_set_encrypt_key(&sm4_key, key);
if (sm4_cbc_padding_encrypt(
&sm4_key, iv, pkey_info, pkey_info_len,
enced_pkey_info, &enced_pkey_info_len) != 1
|| pkcs8_enced_private_key_info_to_der(
salt, sizeof(salt), iter, sizeof(key), OID_hmac_sm3,
OID_sm4_cbc, iv, sizeof(iv),
enced_pkey_info, enced_pkey_info_len, out, outlen) != 1) {
error_print();
goto end;
}
ret = 1;
end:
gmssl_secure_clear(pkey_info, sizeof(pkey_info));
gmssl_secure_clear(key, sizeof(key));
gmssl_secure_clear(&sm4_key, sizeof(sm4_key));
return ret;
}
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 ret = -1;
const uint8_t *salt;
size_t saltlen;
int iter;
int keylen;
int prf;
int cipher;
const uint8_t *iv;
size_t ivlen;
uint8_t key[16];
SM4_KEY sm4_key;
const uint8_t *enced_pkey_info;
size_t enced_pkey_info_len;
uint8_t pkey_info[256];
const uint8_t *cp = pkey_info;
size_t pkey_info_len;
if (!ec_key || !attrs || !attrs_len || !pass || !in || !(*in) || !inlen) {
error_print();
return -1;
}
if (pkcs8_enced_private_key_info_from_der(&salt, &saltlen, &iter, &keylen, &prf,
&cipher, &iv, &ivlen, &enced_pkey_info, &enced_pkey_info_len, in, inlen) != 1
|| asn1_check(keylen == -1 || keylen == 16) != 1
|| asn1_check(prf == - 1 || prf == OID_hmac_sm3) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == 16) != 1
|| asn1_length_le(enced_pkey_info_len, sizeof(pkey_info)) != 1) {
error_print();
return -1;
}
if (sm3_pbkdf2(pass, strlen(pass), salt, saltlen, iter, sizeof(key), key) != 1) {
error_print();
goto end;
}
sm4_set_decrypt_key(&sm4_key, key);
if (sm4_cbc_padding_decrypt(&sm4_key, iv, enced_pkey_info, enced_pkey_info_len,
pkey_info, &pkey_info_len) != 1
|| secp384r1_private_key_info_from_der(ec_key, attrs, attrs_len, &cp, &pkey_info_len) != 1
|| asn1_length_is_zero(pkey_info_len) != 1) {
error_print();
goto end;
}
ret = 1;
end:
gmssl_secure_clear(&sm4_key, sizeof(sm4_key));
gmssl_secure_clear(key, sizeof(key));
gmssl_secure_clear(pkey_info, sizeof(pkey_info));
return ret;
}
int secp384r1_private_key_info_encrypt_to_pem(const SECP384R1_KEY *key, const char *pass, FILE *fp)
{
uint8_t buf[1024];
uint8_t *p = buf;
size_t len = 0;
if (!fp) {
error_print();
return -1;
}
if (secp384r1_private_key_info_encrypt_to_der(key, pass, &p, &len) != 1) {
error_print();
return -1;
}
if (pem_write(fp, "ENCRYPTED PRIVATE KEY", buf, len) != 1) {
error_print();
return -1;
}
return 1;
}
int secp384r1_private_key_info_decrypt_from_pem(SECP384R1_KEY *key, const char *pass, FILE *fp)
{
uint8_t buf[512];
const uint8_t *cp = buf;
size_t len;
const uint8_t *attrs;
size_t attrs_len;
if (!key || !pass || !fp) {
error_print();
return -1;
}
if (pem_read(fp, "ENCRYPTED PRIVATE KEY", buf, &len, sizeof(buf)) != 1
|| secp384r1_private_key_info_decrypt_from_der(key, &attrs, &attrs_len, pass, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
return 1;
}
// FIXME: side-channel of Base64
int secp384r1_private_key_to_pem(const SECP384R1_KEY *a, FILE *fp)
{
uint8_t buf[512];
uint8_t *p = buf;
size_t len = 0;
if (secp384r1_private_key_to_der(a, &p, &len) != 1) {
error_print();
return -1;
}
if (pem_write(fp, "EC PRIVATE KEY", buf, len) <= 0) {
error_print();
return -1;
}
return 1;
}
int secp384r1_private_key_from_pem(SECP384R1_KEY *a, FILE *fp)
{
uint8_t buf[512];
const uint8_t *cp = buf;
size_t len;
if (pem_read(fp, "EC PRIVATE KEY", buf, &len, sizeof(buf)) != 1) {
error_print();
return -1;
}
if (secp384r1_private_key_from_der(a, &cp, &len) != 1
|| len > 0) {
error_print();
return -1;
}
return 1;
}

View File

@@ -19,6 +19,7 @@
#include <gmssl/pkcs8.h> #include <gmssl/pkcs8.h>
#include <gmssl/error.h> #include <gmssl/error.h>
#include <gmssl/ec.h> #include <gmssl/ec.h>
#include <gmssl/x509_key.h>
#include <gmssl/mem.h> #include <gmssl/mem.h>
#include <gmssl/x509_alg.h> #include <gmssl/x509_alg.h>
@@ -164,75 +165,89 @@ int sm2_public_key_algor_from_der(const uint8_t **in, size_t *inlen)
#define SM2_PRIVATE_KEY_DER_SIZE 121 #define SM2_PRIVATE_KEY_DER_SIZE 121
int sm2_private_key_to_der(const SM2_KEY *key, uint8_t **out, size_t *outlen) int sm2_private_key_to_der(const SM2_KEY *key, uint8_t **out, size_t *outlen)
{ {
uint8_t params[64];
uint8_t pubkey[128];
uint8_t *params_ptr = params;
uint8_t *pubkey_ptr = pubkey;
size_t params_len = 0;
size_t pubkey_len = 0;
uint8_t prikey[32]; uint8_t prikey[32];
uint8_t pubkey[65];
uint8_t params[32];
uint8_t pubkey_der[128];
uint8_t *params_ptr = params;
uint8_t *pubkey_ptr = pubkey_der;
size_t params_len = 0;
size_t pubkey_der_len = 0;
size_t len = 0; size_t len = 0;
int ret;
if (!key) { if (!key) {
error_print(); error_print();
return -1; return -1;
} }
if (ec_named_curve_to_der(OID_sm2, &params_ptr, &params_len) != 1
|| sm2_public_key_to_der(key, &pubkey_ptr, &pubkey_len) != 1) {
error_print();
return -1;
}
sm2_z256_to_bytes(key->private_key, prikey); sm2_z256_to_bytes(key->private_key, prikey);
if (asn1_int_to_der(EC_private_key_version, NULL, &len) != 1 if (sm2_z256_point_to_uncompressed_octets(&key->public_key, pubkey) != 1) {
|| asn1_octet_string_to_der(prikey, 32, NULL, &len) != 1 gmssl_secure_clear(prikey, sizeof(prikey));
|| asn1_explicit_to_der(0, params, params_len, NULL, &len) != 1
|| asn1_explicit_to_der(1, pubkey, pubkey_len, NULL, &len) != 1
|| asn1_sequence_header_to_der(len, out, outlen) != 1
|| asn1_int_to_der(EC_private_key_version, out, outlen) != 1
|| asn1_octet_string_to_der(prikey, 32, out, outlen) != 1
|| asn1_explicit_to_der(0, params, params_len, out, outlen) != 1
|| asn1_explicit_to_der(1, pubkey, pubkey_len, out, outlen) != 1) {
gmssl_secure_clear(prikey, 32);
error_print(); error_print();
return -1; return -1;
} }
gmssl_secure_clear(prikey, 32); ret = ec_named_curve_to_der(OID_sm2, &params_ptr, &params_len);
return 1; if (ret == 1) {
ret = asn1_bit_octets_to_der(pubkey, sizeof(pubkey), &pubkey_ptr, &pubkey_der_len);
}
if (ret == 1
&& (asn1_int_to_der(1, NULL, &len) != 1
|| asn1_octet_string_to_der(prikey, sizeof(prikey), NULL, &len) != 1
|| asn1_explicit_to_der(0, params, params_len, NULL, &len) < 0
|| asn1_explicit_to_der(1, pubkey_der, pubkey_der_len, NULL, &len) < 0
|| asn1_sequence_header_to_der(len, out, outlen) != 1
|| asn1_int_to_der(1, out, outlen) != 1
|| asn1_octet_string_to_der(prikey, sizeof(prikey), out, outlen) != 1
|| asn1_explicit_to_der(0, params, params_len, out, outlen) < 0
|| asn1_explicit_to_der(1, pubkey_der, pubkey_der_len, out, outlen) < 0)) {
ret = -1;
}
gmssl_secure_clear(prikey, sizeof(prikey));
if (ret != 1) {
error_print();
return -1;
}
return ret;
} }
int sm2_private_key_from_der(SM2_KEY *key, const uint8_t **in, size_t *inlen) int sm2_private_key_from_der(SM2_KEY *key, const uint8_t **in, size_t *inlen)
{ {
int ret; int ret;
const uint8_t *prikey;
const uint8_t *pubkey;
const uint8_t *params;
size_t prikey_len, pubkey_len, params_len;
int curve;
int version;
const uint8_t *d; const uint8_t *d;
size_t dlen; size_t dlen;
int ver;
const uint8_t *prikey;
const uint8_t *params;
const uint8_t *pubkey;
size_t prikey_len, params_len, pubkey_len;
sm2_z256_t private_key; sm2_z256_t private_key;
if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) { if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) {
if (ret < 0) error_print(); if (ret < 0) error_print();
return ret; return ret;
} }
if (asn1_int_from_der(&ver, &d, &dlen) != 1 if (asn1_int_from_der(&version, &d, &dlen) != 1
|| asn1_octet_string_from_der(&prikey, &prikey_len, &d, &dlen) != 1 || asn1_octet_string_from_der(&prikey, &prikey_len, &d, &dlen) != 1
|| asn1_explicit_from_der(0, &params, &params_len, &d, &dlen) != 1 || asn1_explicit_from_der(0, &params, &params_len, &d, &dlen) < 0
|| asn1_explicit_from_der(1, &pubkey, &pubkey_len, &d, &dlen) != 1 || asn1_explicit_from_der(1, &pubkey, &pubkey_len, &d, &dlen) < 0
|| asn1_check(ver == EC_private_key_version) != 1 || asn1_check(version == 1) != 1
|| asn1_length_is_zero(dlen) != 1) { || asn1_length_is_zero(dlen) != 1) {
error_print(); error_print();
return -1; return -1;
} }
if (params) { if (params) {
int curve;
if (ec_named_curve_from_der(&curve, &params, &params_len) != 1 if (ec_named_curve_from_der(&curve, &params, &params_len) != 1
|| asn1_check(curve == OID_sm2) != 1
|| asn1_length_is_zero(params_len) != 1) { || asn1_length_is_zero(params_len) != 1) {
error_print(); error_print();
return -1; return -1;
} }
} else {
curve = OID_undef;
}
if (curve != OID_undef && curve != OID_sm2) {
error_print();
return -1;
} }
if (asn1_check(prikey_len == 32) != 1) { if (asn1_check(prikey_len == 32) != 1) {
error_print(); error_print();
@@ -246,11 +261,14 @@ int sm2_private_key_from_der(SM2_KEY *key, const uint8_t **in, size_t *inlen)
} }
gmssl_secure_clear(private_key, 32); gmssl_secure_clear(private_key, 32);
// check if the public key is correct
if (pubkey) { if (pubkey) {
const uint8_t *pubkey_octets;
size_t pubkey_octets_len;
SM2_KEY tmp_key; SM2_KEY tmp_key;
if (sm2_public_key_from_der(&tmp_key, &pubkey, &pubkey_len) != 1 if (asn1_bit_octets_from_der(&pubkey_octets, &pubkey_octets_len, &pubkey, &pubkey_len) != 1
|| asn1_length_is_zero(pubkey_len) != 1) { || asn1_length_is_zero(pubkey_len) != 1
|| pubkey_octets_len != 65
|| sm2_z256_point_from_octets(&tmp_key.public_key, pubkey_octets, pubkey_octets_len) != 1) {
error_print(); error_print();
return -1; return -1;
} }
@@ -264,34 +282,71 @@ int sm2_private_key_from_der(SM2_KEY *key, const uint8_t **in, size_t *inlen)
int sm2_private_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen) int sm2_private_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen)
{ {
return ec_private_key_print(fp, fmt, ind, label, d, dlen); int ret;
const uint8_t *a;
size_t alen;
const uint8_t *p;
size_t len;
int val;
format_print(fp, fmt, ind, "%s\n", label);
ind += 4;
if (asn1_int_from_der(&val, &d, &dlen) != 1) goto err;
format_print(fp, fmt, ind, "version: %d\n", val);
if (asn1_octet_string_from_der(&p, &len, &d, &dlen) != 1) goto err;
format_bytes(fp, fmt, ind, "privateKey", p, len);
if ((ret = asn1_explicit_from_der(0, &a, &alen, &d, &dlen)) < 0) goto err;
else if (ret) {
if (ec_named_curve_from_der(&val, &a, &alen) != 1) goto err;
format_print(fp, fmt, ind, "parameters: %s\n", ec_named_curve_name(val));
if (asn1_length_is_zero(alen) != 1) goto err;
}
format_print(fp, fmt, ind, "publicKey\n");
ind += 4;
if ((ret = asn1_explicit_from_der(1, &a, &alen, &d, &dlen)) < 0) goto err;
else if (ret) {
if (asn1_bit_octets_from_der(&p, &len, &a, &alen) != 1) goto err;
format_bytes(fp, fmt, ind, "ECPoint", p, len);
if (asn1_length_is_zero(alen) != 1) goto err;
}
if (asn1_length_is_zero(dlen) != 1) goto err;
return 1;
err:
error_print();
return -1;
} }
#define SM2_PRIVATE_KEY_INFO_DER_SIZE 150 #define SM2_PRIVATE_KEY_INFO_DER_SIZE 150
int sm2_private_key_info_to_der(const SM2_KEY *sm2_key, uint8_t **out, size_t *outlen) int sm2_private_key_info_to_der(const SM2_KEY *sm2_key, uint8_t **out, size_t *outlen)
{ {
size_t len = 0;
uint8_t prikey[SM2_PRIVATE_KEY_DER_SIZE]; uint8_t prikey[SM2_PRIVATE_KEY_DER_SIZE];
uint8_t *p = prikey; uint8_t *p = prikey;
size_t prikey_len = 0; size_t prikey_len = 0;
size_t len = 0;
int ret;
if (sm2_private_key_to_der(sm2_key, &p, &prikey_len) != 1) { if (sm2_private_key_to_der(sm2_key, &p, &prikey_len) != 1) {
error_print(); error_print();
return -1; return -1;
} }
if (asn1_int_to_der(PKCS8_private_key_info_version, NULL, &len) != 1 if (asn1_int_to_der(PKCS8_private_key_info_version, NULL, &len) != 1
|| sm2_public_key_algor_to_der(NULL, &len) != 1 || x509_public_key_algor_to_der(OID_ec_public_key, OID_sm2, NULL, &len) != 1
|| asn1_octet_string_to_der(prikey, prikey_len, NULL, &len) != 1 || asn1_octet_string_to_der(prikey, prikey_len, NULL, &len) != 1
|| asn1_sequence_header_to_der(len, out, outlen) != 1 || asn1_sequence_header_to_der(len, out, outlen) != 1
|| asn1_int_to_der(PKCS8_private_key_info_version, out, outlen) != 1 || asn1_int_to_der(PKCS8_private_key_info_version, out, outlen) != 1
|| sm2_public_key_algor_to_der(out, outlen) != 1 || x509_public_key_algor_to_der(OID_ec_public_key, OID_sm2, out, outlen) != 1
|| asn1_octet_string_to_der(prikey, prikey_len, out, outlen) != 1) { || asn1_octet_string_to_der(prikey, prikey_len, out, outlen) != 1) {
memset(prikey, 0, sizeof(prikey)); ret = -1;
} else {
ret = 1;
}
gmssl_secure_clear(prikey, sizeof(prikey));
if (ret != 1) {
error_print(); error_print();
return -1; return -1;
} }
memset(prikey, 0, sizeof(prikey));
return 1; return 1;
} }
@@ -302,6 +357,8 @@ int sm2_private_key_info_from_der(SM2_KEY *sm2_key, const uint8_t **attrs, size_
const uint8_t *d; const uint8_t *d;
size_t dlen; size_t dlen;
int version; int version;
int algor;
int curve;
const uint8_t *prikey; const uint8_t *prikey;
size_t prikey_len; size_t prikey_len;
@@ -310,16 +367,15 @@ int sm2_private_key_info_from_der(SM2_KEY *sm2_key, const uint8_t **attrs, size_
return ret; return ret;
} }
if (asn1_int_from_der(&version, &d, &dlen) != 1 if (asn1_int_from_der(&version, &d, &dlen) != 1
|| sm2_public_key_algor_from_der(&d, &dlen) != 1 || x509_public_key_algor_from_der(&algor, &curve, &d, &dlen) != 1
|| asn1_octet_string_from_der(&prikey, &prikey_len, &d, &dlen) != 1 || asn1_octet_string_from_der(&prikey, &prikey_len, &d, &dlen) != 1
|| asn1_implicit_set_from_der(0, attrs, attrslen, &d, &dlen) < 0 || asn1_implicit_set_from_der(0, attrs, attrslen, &d, &dlen) < 0
|| asn1_length_is_zero(dlen) != 1) { || asn1_check(version == PKCS8_private_key_info_version) != 1
error_print(); || asn1_check(algor == OID_ec_public_key) != 1
return -1; || asn1_check(curve == OID_sm2) != 1
}
if (asn1_check(version == PKCS8_private_key_info_version) != 1
|| sm2_private_key_from_der(sm2_key, &prikey, &prikey_len) != 1 || sm2_private_key_from_der(sm2_key, &prikey, &prikey_len) != 1
|| asn1_length_is_zero(prikey_len) != 1) { || asn1_length_is_zero(prikey_len) != 1
|| asn1_length_is_zero(dlen) != 1) {
error_print(); error_print();
return -1; return -1;
} }
@@ -344,7 +400,7 @@ int sm2_private_key_info_print(FILE *fp, int fmt, int ind, const char *label, co
x509_public_key_algor_print(fp, fmt, ind, "privateKeyAlgorithm", p, len); x509_public_key_algor_print(fp, fmt, ind, "privateKeyAlgorithm", p, len);
if (asn1_octet_string_from_der(&p, &len, &d, &dlen) != 1) goto err; if (asn1_octet_string_from_der(&p, &len, &d, &dlen) != 1) goto err;
if (asn1_sequence_from_der(&prikey, &prikey_len, &p, &len) != 1) goto err; if (asn1_sequence_from_der(&prikey, &prikey_len, &p, &len) != 1) goto err;
ec_private_key_print(fp, fmt, ind + 4, "privateKey", prikey, prikey_len); sm2_private_key_print(fp, fmt, ind + 4, "privateKey", prikey, prikey_len);
if (asn1_length_is_zero(len) != 1) goto err; if (asn1_length_is_zero(len) != 1) goto err;
if ((ret = asn1_implicit_set_from_der(0, &p, &len, &d, &dlen)) < 0) goto err; if ((ret = asn1_implicit_set_from_der(0, &p, &len, &d, &dlen)) < 0) goto err;
else if (ret) format_bytes(fp, fmt, ind, "attributes", p, len); else if (ret) format_bytes(fp, fmt, ind, "attributes", p, len);
@@ -366,15 +422,8 @@ int sm2_private_key_info_to_pem(const SM2_KEY *key, FILE *fp)
error_print(); error_print();
return -1; return -1;
} }
if (sm2_private_key_info_to_der(key, &p, &len) != 1) { if (sm2_private_key_info_to_der(key, &p, &len) != 1
error_print(); || pem_write(fp, "PRIVATE KEY", buf, len) != 1) {
goto end;
}
if (len != sizeof(buf)) {
error_print();
goto end;
}
if (pem_write(fp, "PRIVATE KEY", buf, len) != 1) {
error_print(); error_print();
goto end; goto end;
} }
@@ -394,13 +443,11 @@ int sm2_private_key_info_from_pem(SM2_KEY *sm2_key, FILE *fp)
if (pem_read(fp, "PRIVATE KEY", buf, &len, sizeof(buf)) != 1 if (pem_read(fp, "PRIVATE KEY", buf, &len, sizeof(buf)) != 1
|| sm2_private_key_info_from_der(sm2_key, &attrs, &attrs_len, &cp, &len) != 1 || sm2_private_key_info_from_der(sm2_key, &attrs, &attrs_len, &cp, &len) != 1
|| asn1_length_is_zero(attrs_len) != 1
|| asn1_length_is_zero(len) != 1) { || asn1_length_is_zero(len) != 1) {
error_print(); error_print();
return -1; return -1;
} }
if (attrs_len) {
error_print();
}
return 1; return 1;
} }
@@ -448,7 +495,7 @@ int sm2_private_key_to_pem(const SM2_KEY *a, FILE *fp)
error_print(); error_print();
return -1; return -1;
} }
if (pem_write(fp, "EC PRIVATE KEY", buf, len) <= 0) { if (pem_write(fp, "EC PRIVATE KEY", buf, len) != 1) {
error_print(); error_print();
return -1; return -1;
} }
@@ -461,11 +508,8 @@ int sm2_private_key_from_pem(SM2_KEY *a, FILE *fp)
const uint8_t *cp = buf; const uint8_t *cp = buf;
size_t len; size_t len;
if (pem_read(fp, "EC PRIVATE KEY", buf, &len, sizeof(buf)) != 1) { if (pem_read(fp, "EC PRIVATE KEY", buf, &len, sizeof(buf)) != 1
error_print(); || sm2_private_key_from_der(a, &cp, &len) != 1
return -1;
}
if (sm2_private_key_from_der(a, &cp, &len) != 1
|| len > 0) { || len > 0) {
error_print(); error_print();
return -1; return -1;
@@ -534,125 +578,50 @@ int sm2_public_key_digest(const SM2_KEY *sm2_key, uint8_t dgst[32])
int sm2_private_key_info_encrypt_to_der(const SM2_KEY *sm2_key, const char *pass, int sm2_private_key_info_encrypt_to_der(const SM2_KEY *sm2_key, const char *pass,
uint8_t **out, size_t *outlen) uint8_t **out, size_t *outlen)
{ {
int ret = -1; X509_KEY x509_key;
uint8_t pkey_info[SM2_PRIVATE_KEY_INFO_DER_SIZE];
uint8_t *p = pkey_info;
size_t pkey_info_len = 0;
uint8_t salt[16];
int iter = 65536;
uint8_t iv[16];
uint8_t key[16];
SM4_KEY sm4_key;
uint8_t enced_pkey_info[sizeof(pkey_info) + 32];
size_t enced_pkey_info_len;
if (!sm2_key || !pass || !outlen) { if (!sm2_key || !pass || !outlen) {
error_print(); error_print();
return -1; return -1;
} }
if (sm2_private_key_info_to_der(sm2_key, &p, &pkey_info_len) != 1 if (x509_key_set_sm2_key(&x509_key, sm2_key) != 1
|| rand_bytes(salt, sizeof(salt)) != 1 || x509_private_key_info_encrypt_to_der(&x509_key, pass, out, outlen) != 1) {
|| rand_bytes(iv, sizeof(iv)) != 1
|| sm3_pbkdf2(pass, strlen(pass), salt, sizeof(salt), iter, sizeof(key), key) != 1) {
error_print(); error_print();
goto end; return -1;
} }
/* return 1;
if (pkey_info_len != sizeof(pkey_info)) {
error_print();
goto end;
}
*/
sm4_set_encrypt_key(&sm4_key, key);
if (sm4_cbc_padding_encrypt(
&sm4_key, iv, pkey_info, pkey_info_len,
enced_pkey_info, &enced_pkey_info_len) != 1
|| pkcs8_enced_private_key_info_to_der(
salt, sizeof(salt), iter, sizeof(key), OID_hmac_sm3,
OID_sm4_cbc, iv, sizeof(iv),
enced_pkey_info, enced_pkey_info_len, out, outlen) != 1) {
error_print();
goto end;
}
ret = 1;
end:
gmssl_secure_clear(pkey_info, sizeof(pkey_info));
gmssl_secure_clear(key, sizeof(key));
gmssl_secure_clear(&sm4_key, sizeof(sm4_key));
return ret;
} }
int sm2_private_key_info_decrypt_from_der(SM2_KEY *sm2, int sm2_private_key_info_decrypt_from_der(SM2_KEY *sm2,
const uint8_t **attrs, size_t *attrs_len, const uint8_t **attrs, size_t *attrs_len,
const char *pass, const uint8_t **in, size_t *inlen) const char *pass, const uint8_t **in, size_t *inlen)
{ {
int ret = -1; X509_KEY x509_key;
const uint8_t *salt;
size_t saltlen;
int iter;
int keylen;
int prf;
int cipher;
const uint8_t *iv;
size_t ivlen;
uint8_t key[16];
SM4_KEY sm4_key;
const uint8_t *enced_pkey_info;
size_t enced_pkey_info_len;
uint8_t pkey_info[256];
const uint8_t *cp = pkey_info;
size_t pkey_info_len;
if (!sm2 || !attrs || !attrs_len || !pass || !in || !(*in) || !inlen) { if (!sm2 || !attrs || !attrs_len || !pass || !in || !(*in) || !inlen) {
error_print(); error_print();
return -1; return -1;
} }
if (pkcs8_enced_private_key_info_from_der(&salt, &saltlen, &iter, &keylen, &prf, if (x509_private_key_info_decrypt_from_der(&x509_key, attrs, attrs_len, pass, in, inlen) != 1
&cipher, &iv, &ivlen, &enced_pkey_info, &enced_pkey_info_len, in, inlen) != 1 || x509_key.algor != OID_ec_public_key
|| asn1_check(keylen == -1 || keylen == 16) != 1 || x509_key.algor_param != OID_sm2) {
|| asn1_check(prf == - 1 || prf == OID_hmac_sm3) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == 16) != 1
|| asn1_length_le(enced_pkey_info_len, sizeof(pkey_info)) != 1) {
error_print(); error_print();
return -1; return -1;
} }
if (sm3_pbkdf2(pass, strlen(pass), salt, saltlen, iter, sizeof(key), key) != 1) { *sm2 = x509_key.u.sm2_key;
error_print(); return 1;
goto end;
}
sm4_set_decrypt_key(&sm4_key, key);
if (sm4_cbc_padding_decrypt(&sm4_key, iv, enced_pkey_info, enced_pkey_info_len,
pkey_info, &pkey_info_len) != 1
|| sm2_private_key_info_from_der(sm2, attrs, attrs_len, &cp, &pkey_info_len) != 1
|| asn1_length_is_zero(pkey_info_len) != 1) {
error_print();
goto end;
}
ret = 1;
end:
gmssl_secure_clear(&sm4_key, sizeof(sm4_key));
gmssl_secure_clear(key, sizeof(key));
gmssl_secure_clear(pkey_info, sizeof(pkey_info));
return ret;
} }
int sm2_private_key_info_encrypt_to_pem(const SM2_KEY *sm2_key, const char *pass, FILE *fp) int sm2_private_key_info_encrypt_to_pem(const SM2_KEY *sm2_key, const char *pass, FILE *fp)
{ {
uint8_t buf[1024]; X509_KEY x509_key;
uint8_t *p = buf;
size_t len = 0;
if (!fp) { if (!fp) {
error_print(); error_print();
return -1; return -1;
} }
if (sm2_private_key_info_encrypt_to_der(sm2_key, pass, &p, &len) != 1) { if (x509_key_set_sm2_key(&x509_key, sm2_key) != 1
error_print(); || x509_private_key_info_encrypt_to_pem(&x509_key, pass, fp) != 1) {
return -1;
}
if (pem_write(fp, "ENCRYPTED PRIVATE KEY", buf, len) != 1) {
error_print(); error_print();
return -1; return -1;
} }
@@ -661,9 +630,7 @@ int sm2_private_key_info_encrypt_to_pem(const SM2_KEY *sm2_key, const char *pass
int sm2_private_key_info_decrypt_from_pem(SM2_KEY *key, const char *pass, FILE *fp) int sm2_private_key_info_decrypt_from_pem(SM2_KEY *key, const char *pass, FILE *fp)
{ {
uint8_t buf[512]; X509_KEY x509_key;
const uint8_t *cp = buf;
size_t len;
const uint8_t *attrs; const uint8_t *attrs;
size_t attrs_len; size_t attrs_len;
@@ -671,11 +638,12 @@ int sm2_private_key_info_decrypt_from_pem(SM2_KEY *key, const char *pass, FILE *
error_print(); error_print();
return -1; return -1;
} }
if (pem_read(fp, "ENCRYPTED PRIVATE KEY", buf, &len, sizeof(buf)) != 1 if (x509_private_key_info_decrypt_from_pem(&x509_key, &attrs, &attrs_len, pass, fp) != 1
|| sm2_private_key_info_decrypt_from_der(key, &attrs, &attrs_len, pass, &cp, &len) != 1 || x509_key.algor != OID_ec_public_key
|| asn1_length_is_zero(len) != 1) { || x509_key.algor_param != OID_sm2) {
error_print(); error_print();
return -1; return -1;
} }
*key = x509_key.u.sm2_key;
return 1; return 1;
} }

View File

@@ -25,15 +25,6 @@
#include <gmssl/x509_key.h> #include <gmssl/x509_key.h>
/*
TODO:
x509_sign_init/update/finish
x509_verify_init/update/finish
当使用ECDSA算法时需要可选多个哈希函数
特别是很多CA证书如icloud.com的证书链其中CA证书使用的是ecdsa_secp256r1_sha384
因此需要x509_sign/verify_init接口中增加一个表示算法的参数
*/
int x509_key_set_sm2_key(X509_KEY *x509_key, const SM2_KEY *sm2_key) int x509_key_set_sm2_key(X509_KEY *x509_key, const SM2_KEY *sm2_key)
{ {
if (!x509_key || !sm2_key) { if (!x509_key || !sm2_key) {
@@ -62,6 +53,21 @@ int x509_key_set_secp256r1_key(X509_KEY *x509_key, const SECP256R1_KEY *secp256r
} }
#endif #endif
#ifdef ENABLE_SECP384R1
int x509_key_set_secp384r1_key(X509_KEY *x509_key, const SECP384R1_KEY *secp384r1_key)
{
if (!x509_key || !secp384r1_key) {
error_print();
return -1;
}
memset(x509_key, 0, sizeof(X509_KEY));
x509_key->algor = OID_ec_public_key;
x509_key->algor_param = OID_secp384r1;
x509_key->u.secp384r1_key = *secp384r1_key;
return 1;
}
#endif
#ifdef ENABLE_LMS #ifdef ENABLE_LMS
int x509_key_set_lms_key(X509_KEY *x509_key, const LMS_KEY *lms_key) int x509_key_set_lms_key(X509_KEY *x509_key, const LMS_KEY *lms_key)
{ {
@@ -261,6 +267,14 @@ int x509_key_generate(X509_KEY *key, int algor, const void *param, size_t paraml
return -1; return -1;
} }
break; break;
#endif
#ifdef ENABLE_SECP384R1
case OID_secp384r1:
if (secp384r1_key_generate(&key->u.secp384r1_key) != 1) {
error_print();
return -1;
}
break;
#endif #endif
default: default:
error_print(); error_print();
@@ -336,6 +350,11 @@ void x509_key_cleanup(X509_KEY *key)
case OID_secp256r1: case OID_secp256r1:
gmssl_secure_clear(&key->u.secp256r1_key, sizeof(SECP256R1_KEY)); gmssl_secure_clear(&key->u.secp256r1_key, sizeof(SECP256R1_KEY));
break; break;
#endif
#ifdef ENABLE_SECP384R1
case OID_secp384r1:
gmssl_secure_clear(&key->u.secp384r1_key, sizeof(SECP384R1_KEY));
break;
#endif #endif
default: default:
error_print(); error_print();
@@ -414,6 +433,14 @@ int x509_public_key_to_bytes(const X509_KEY *key, uint8_t **out, size_t *outlen)
return -1; return -1;
} }
break; break;
#endif
#ifdef ENABLE_SECP384R1
case OID_secp384r1:
if (secp384r1_public_key_to_bytes(&key->u.secp384r1_key, out, outlen) != 1) {
error_print();
return -1;
}
break;
#endif #endif
default: default:
error_print(); error_print();
@@ -522,6 +549,14 @@ int x509_public_key_from_bytes(X509_KEY *key, int algor, int algor_param, const
return -1; return -1;
} }
break; break;
#endif
#ifdef ENABLE_SECP384R1
case OID_secp384r1:
if (secp384r1_public_key_from_bytes(&key->u.secp384r1_key, in, inlen) != 1) {
error_print();
return -1;
}
break;
#endif #endif
default: default:
error_print(); error_print();
@@ -663,6 +698,13 @@ int x509_public_key_equ(const X509_KEY *key, const X509_KEY *pub)
error_print(); error_print();
return ret; return ret;
} }
#endif
#ifdef ENABLE_SECP384R1
} else if (key->algor_param == OID_secp384r1) {
if ((ret = secp384r1_public_key_equ(&key->u.secp384r1_key, &pub->u.secp384r1_key)) != 1) {
error_print();
return ret;
}
#endif #endif
} else { } else {
error_print(); error_print();
@@ -762,6 +804,13 @@ int x509_public_key_print(FILE *fp, int fmt, int ind, const char *label, const X
error_print(); error_print();
return -1; return -1;
} }
#endif
#ifdef ENABLE_SECP384R1
} else if (key->algor_param == OID_secp384r1) {
if (secp384r1_public_key_print(fp, fmt, ind, label, &key->u.secp384r1_key) != 1) {
error_print();
return -1;
}
#endif #endif
} else { } else {
error_print(); error_print();
@@ -989,6 +1038,8 @@ err:
return -1; return -1;
} }
#define X509_EC_PRIVATE_KEY_VERSION 1
int x509_private_key_print_ex(FILE *fp, int fmt, int ind, const char *label, const X509_KEY *key) int x509_private_key_print_ex(FILE *fp, int fmt, int ind, const char *label, const X509_KEY *key)
{ {
// TODO: change lms_private_key_print to lms_private_key_print_ex and xmss ... // TODO: change lms_private_key_print to lms_private_key_print_ex and xmss ...
@@ -996,207 +1047,6 @@ int x509_private_key_print_ex(FILE *fp, int fmt, int ind, const char *label, con
return -1; return -1;
} }
#define SM2_PRIVATE_KEY_DER_SIZE 121
int ec_private_key_to_der(const X509_KEY *key, int encode_params, int encode_pubkey,
uint8_t **out, size_t *outlen)
{
uint8_t params_buf[16]; // = 10 for sm2, p256
uint8_t pubkey_buf[68]; // = 68 for sm2, p256
uint8_t *params = NULL;
uint8_t *pubkey = NULL;
size_t params_len = 0;
size_t pubkey_len = 0;
uint8_t prikey[32];
size_t len = 0;
if (!key) {
error_print();
return -1;
}
if (key->algor != OID_ec_public_key) {
error_print();
return -1;
}
if (encode_params) {
params = params_buf;
if (ec_named_curve_to_der(key->algor_param, &params, &params_len) != 1) {
gmssl_secure_clear(prikey, 32);
error_print();
return -1;
}
params = params_buf;
}
switch (key->algor_param) {
case OID_sm2:
if (encode_pubkey) {
pubkey = pubkey_buf;
if (sm2_public_key_to_der(&key->u.sm2_key, &pubkey, &pubkey_len) != 1) {
error_print();
return -1;
}
pubkey = pubkey_buf;
}
sm2_z256_to_bytes(key->u.sm2_key.private_key, prikey);
break;
#ifdef ENABLE_SECP256R1
case OID_secp256r1:
if (encode_pubkey) {
pubkey = pubkey_buf;
if (secp256r1_public_key_to_der(&key->u.secp256r1_key, &pubkey, &pubkey_len) != 1) {
error_print();
return -1;
}
pubkey = pubkey_buf;
}
if (secp256r1_to_32bytes(key->u.secp256r1_key.private_key, prikey) != 1) {
error_print();
return -1;
}
break;
#endif
default:
error_print();
return -1;
}
if (asn1_int_to_der(EC_private_key_version, NULL, &len) != 1
|| asn1_octet_string_to_der(prikey, 32, NULL, &len) != 1
|| asn1_explicit_to_der(0, params, params_len, NULL, &len) < 0
|| asn1_explicit_to_der(1, pubkey, pubkey_len, NULL, &len) < 0
|| asn1_sequence_header_to_der(len, out, outlen) != 1
|| asn1_int_to_der(EC_private_key_version, out, outlen) != 1
|| asn1_octet_string_to_der(prikey, 32, out, outlen) != 1
|| asn1_explicit_to_der(0, params, params_len, out, outlen) < 0
|| asn1_explicit_to_der(1, pubkey, pubkey_len, out, outlen) < 0) {
gmssl_secure_clear(prikey, 32);
error_print();
return -1;
}
gmssl_secure_clear(prikey, 32);
return 1;
}
// when params(curve) is omitted in ECPrivateKey, curve should be given explicitly
int ec_private_key_from_der(X509_KEY *key, int opt_curve, const uint8_t **in, size_t *inlen)
{
int ret;
const uint8_t *d;
size_t dlen;
int ver;
const uint8_t *prikey;
const uint8_t *params;
const uint8_t *pubkey;
size_t prikey_len, params_len, pubkey_len;
int curve;
if (!key || !in || !(*in) || !inlen) {
error_print();
return -1;
}
if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) {
if (ret < 0) error_print();
return ret;
}
if (asn1_int_from_der(&ver, &d, &dlen) != 1
|| asn1_octet_string_from_der(&prikey, &prikey_len, &d, &dlen) != 1
|| asn1_explicit_from_der(0, &params, &params_len, &d, &dlen) < 0
|| asn1_explicit_from_der(1, &pubkey, &pubkey_len, &d, &dlen) < 0
|| asn1_check(ver == EC_private_key_version) != 1
|| asn1_length_is_zero(dlen) != 1) {
error_print();
return -1;
}
if (!prikey || prikey_len != 32) {
error_print();
return -1;
}
if (params) {
if (ec_named_curve_from_der(&curve, &params, &params_len) != 1
|| asn1_length_is_zero(params_len) != 1) {
error_print();
return -1;
}
if (curve != opt_curve && opt_curve != OID_undef) {
error_print();
return -1;
}
} else {
curve = opt_curve;
}
memset(key, 0, sizeof(X509_KEY));
if (curve == OID_sm2) {
sm2_z256_t sm2_private;
SM2_KEY sm2_pub;
sm2_z256_from_bytes(sm2_private, prikey);
if (sm2_key_set_private_key(&key->u.sm2_key, sm2_private) != 1) {
gmssl_secure_clear(sm2_private, sizeof(sm2_z256_t));
error_print();
return -1;
}
gmssl_secure_clear(sm2_private, sizeof(sm2_z256_t));
if (pubkey) {
if (sm2_public_key_from_der(&sm2_pub, &pubkey, &pubkey_len) != 1
|| asn1_length_is_zero(pubkey_len) != 1) {
error_print();
return -1;
}
if (sm2_public_key_equ(&key->u.sm2_key, &sm2_pub) != 1) {
gmssl_secure_clear(&key->u.sm2_key, sizeof(SM2_KEY)); // sm2_key_cleanup?
error_print();
return -1;
}
}
}
#ifdef ENABLE_SECP256R1
else if (curve == OID_secp256r1) {
secp256r1_t p256_private;
SECP256R1_KEY p256_pub;
if (secp256r1_from_32bytes(p256_private, prikey) != 1) {
error_print();
return -1;
}
if (secp256r1_key_set_private_key(&key->u.secp256r1_key, p256_private) != 1) {
gmssl_secure_clear(p256_private, sizeof(secp256r1_t));
error_print();
return -1;
}
gmssl_secure_clear(p256_private, sizeof(secp256r1_t));
if (pubkey) {
if (secp256r1_public_key_from_der(&p256_pub, &pubkey, &pubkey_len) != 1
|| asn1_length_is_zero(pubkey_len) != 1) {
error_print();
return -1;
}
if (secp256r1_public_key_equ(&key->u.secp256r1_key, &p256_pub) != 1) {
gmssl_secure_clear(&key->u.secp256r1_key, sizeof(SECP256R1_KEY));
error_print();
return -1;
}
}
}
#endif
else {
error_print();
return -1;
}
key->algor = OID_ec_public_key;
key->algor_param = curve;
return 1;
}
int x509_private_key_info_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen) int x509_private_key_info_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen)
{ {
if (sm2_private_key_info_print(fp, fmt, ind, label, d, dlen) != 1) { if (sm2_private_key_info_print(fp, fmt, ind, label, d, dlen) != 1) {
@@ -1208,7 +1058,7 @@ int x509_private_key_info_print(FILE *fp, int fmt, int ind, const char *label, c
int x509_private_key_info_to_der(const X509_KEY *key, uint8_t **out, size_t *outlen) int x509_private_key_info_to_der(const X509_KEY *key, uint8_t **out, size_t *outlen)
{ {
uint8_t private_key[128]; // 121 uint8_t private_key[512];
uint8_t *p = private_key; uint8_t *p = private_key;
size_t private_key_len = 0; size_t private_key_len = 0;
size_t len = 0; size_t len = 0;
@@ -1220,11 +1070,74 @@ int x509_private_key_info_to_der(const X509_KEY *key, uint8_t **out, size_t *out
switch (key->algor) { switch (key->algor) {
case OID_ec_public_key: case OID_ec_public_key:
if (ec_private_key_to_der(key, {
X509_ENCODE_EC_PRIVATE_KEY_PARAMS, X509_ENCODE_EC_PRIVATE_KEY_PUBKEY, uint8_t ec_private_key[64];
&p, &private_key_len) != 1) { uint8_t ec_public_key[129];
error_print(); uint8_t params[64];
return -1; uint8_t pubkey[256];
uint8_t *q = ec_private_key;
uint8_t *params_ptr = params;
uint8_t *pubkey_ptr = pubkey;
size_t ec_private_key_len = 0;
size_t ec_public_key_len = 0;
size_t params_len = 0;
size_t pubkey_len = 0;
size_t ec_len = 0;
switch (key->algor_param) {
case OID_sm2:
sm2_z256_to_bytes(key->u.sm2_key.private_key, q);
ec_private_key_len = 32;
break;
#ifdef ENABLE_SECP256R1
case OID_secp256r1:
if (secp256r1_private_key_to_bytes(&key->u.secp256r1_key, &q, &ec_private_key_len) != 1) {
error_print();
return -1;
}
break;
#endif
#ifdef ENABLE_SECP384R1
case OID_secp384r1:
if (secp384r1_private_key_to_bytes(&key->u.secp384r1_key, &q, &ec_private_key_len) != 1) {
error_print();
return -1;
}
break;
#endif
default:
error_print();
return -1;
}
if (X509_ENCODE_EC_PRIVATE_KEY_PARAMS
&& ec_named_curve_to_der(key->algor_param, &params_ptr, &params_len) != 1) {
gmssl_secure_clear(ec_private_key, sizeof(ec_private_key));
error_print();
return -1;
}
if (X509_ENCODE_EC_PRIVATE_KEY_PUBKEY) {
q = ec_public_key;
if (x509_public_key_to_bytes(key, &q, &ec_public_key_len) != 1
|| asn1_bit_octets_to_der(ec_public_key, ec_public_key_len, &pubkey_ptr, &pubkey_len) != 1) {
gmssl_secure_clear(ec_private_key, sizeof(ec_private_key));
error_print();
return -1;
}
}
if (asn1_int_to_der(X509_EC_PRIVATE_KEY_VERSION, NULL, &ec_len) != 1
|| asn1_octet_string_to_der(ec_private_key, ec_private_key_len, NULL, &ec_len) != 1
|| asn1_explicit_to_der(0, params_len ? params : NULL, params_len, NULL, &ec_len) < 0
|| asn1_explicit_to_der(1, pubkey_len ? pubkey : NULL, pubkey_len, NULL, &ec_len) < 0
|| asn1_sequence_header_to_der(ec_len, &p, &private_key_len) != 1
|| asn1_int_to_der(X509_EC_PRIVATE_KEY_VERSION, &p, &private_key_len) != 1
|| asn1_octet_string_to_der(ec_private_key, ec_private_key_len, &p, &private_key_len) != 1
|| asn1_explicit_to_der(0, params_len ? params : NULL, params_len, &p, &private_key_len) < 0
|| asn1_explicit_to_der(1, pubkey_len ? pubkey : NULL, pubkey_len, &p, &private_key_len) < 0) {
gmssl_secure_clear(ec_private_key, sizeof(ec_private_key));
error_print();
return -1;
}
gmssl_secure_clear(ec_private_key, sizeof(ec_private_key));
} }
break; break;
case OID_lms_hashsig: case OID_lms_hashsig:
@@ -1289,10 +1202,135 @@ int x509_private_key_info_from_der(X509_KEY *key, const uint8_t **attrs, size_t
} }
switch (algor) { switch (algor) {
case OID_ec_public_key: case OID_ec_public_key:
if (ec_private_key_from_der(key, algor_param, &private_key, &private_key_len) != 1 {
|| asn1_length_is_zero(private_key_len) != 1) { const uint8_t *ec_key_der;
error_print(); size_t ec_key_der_len;
return -1; int ver;
const uint8_t *prikey;
size_t prikey_len;
const uint8_t *params = NULL;
size_t params_len = 0;
const uint8_t *pubkey = NULL;
size_t pubkey_len = 0;
int curve;
if (asn1_sequence_from_der(&ec_key_der, &ec_key_der_len, &private_key, &private_key_len) != 1
|| asn1_int_from_der(&ver, &ec_key_der, &ec_key_der_len) != 1
|| asn1_octet_string_from_der(&prikey, &prikey_len, &ec_key_der, &ec_key_der_len) != 1
|| asn1_explicit_from_der(0, &params, &params_len, &ec_key_der, &ec_key_der_len) < 0
|| asn1_explicit_from_der(1, &pubkey, &pubkey_len, &ec_key_der, &ec_key_der_len) < 0
|| asn1_check(ver == X509_EC_PRIVATE_KEY_VERSION) != 1
|| asn1_length_is_zero(ec_key_der_len) != 1
|| asn1_length_is_zero(private_key_len) != 1) {
error_print();
return -1;
}
if (params) {
if (ec_named_curve_from_der(&curve, &params, &params_len) != 1
|| asn1_length_is_zero(params_len) != 1) {
error_print();
return -1;
}
if (curve != algor_param && algor_param != OID_undef) {
error_print();
return -1;
}
} else {
curve = algor_param;
}
memset(key, 0, sizeof(X509_KEY));
key->algor = OID_ec_public_key;
key->algor_param = curve;
if (curve == OID_sm2) {
sm2_z256_t sm2_private;
if (prikey_len != 32) {
error_print();
return -1;
}
sm2_z256_from_bytes(sm2_private, prikey);
if (sm2_key_set_private_key(&key->u.sm2_key, sm2_private) != 1) {
gmssl_secure_clear(sm2_private, sizeof(sm2_private));
error_print();
return -1;
}
gmssl_secure_clear(sm2_private, sizeof(sm2_private));
if (pubkey) {
const uint8_t *public_key;
size_t public_key_len;
SM2_KEY sm2_pub;
if (asn1_bit_octets_from_der(&public_key, &public_key_len, &pubkey, &pubkey_len) != 1
|| asn1_length_is_zero(pubkey_len) != 1
|| public_key_len != 65
|| sm2_z256_point_from_octets(&sm2_pub.public_key, public_key, public_key_len) != 1
|| sm2_public_key_equ(&key->u.sm2_key, &sm2_pub) != 1) {
gmssl_secure_clear(&key->u.sm2_key, sizeof(SM2_KEY));
error_print();
return -1;
}
}
#ifdef ENABLE_SECP256R1
} else if (curve == OID_secp256r1) {
const uint8_t *cp = prikey;
size_t len = prikey_len;
if (prikey_len != 32
|| secp256r1_private_key_from_bytes(&key->u.secp256r1_key, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
if (pubkey) {
const uint8_t *public_key;
size_t public_key_len;
SECP256R1_KEY p256_pub;
if (asn1_bit_octets_from_der(&public_key, &public_key_len, &pubkey, &pubkey_len) != 1
|| asn1_length_is_zero(pubkey_len) != 1
|| public_key_len != 65
|| secp256r1_public_key_from_bytes(&p256_pub, &public_key, &public_key_len) != 1
|| asn1_length_is_zero(public_key_len) != 1
|| secp256r1_public_key_equ(&key->u.secp256r1_key, &p256_pub) != 1) {
gmssl_secure_clear(&key->u.secp256r1_key, sizeof(SECP256R1_KEY));
error_print();
return -1;
}
}
#endif
#ifdef ENABLE_SECP384R1
} else if (curve == OID_secp384r1) {
const uint8_t *cp = prikey;
size_t len = prikey_len;
if (prikey_len != 48
|| secp384r1_private_key_from_bytes(&key->u.secp384r1_key, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
if (pubkey) {
const uint8_t *public_key;
size_t public_key_len;
SECP384R1_KEY p384_pub;
if (asn1_bit_octets_from_der(&public_key, &public_key_len, &pubkey, &pubkey_len) != 1
|| asn1_length_is_zero(pubkey_len) != 1
|| public_key_len != 97
|| secp384r1_public_key_from_bytes(&p384_pub, &public_key, &public_key_len) != 1
|| asn1_length_is_zero(public_key_len) != 1
|| secp384r1_public_key_equ(&key->u.secp384r1_key, &p384_pub) != 1) {
gmssl_secure_clear(&key->u.secp384r1_key, sizeof(SECP384R1_KEY));
error_print();
return -1;
}
}
#endif
} else {
error_print();
return -1;
}
} }
break; break;
#ifdef ENABLE_SM9 #ifdef ENABLE_SM9
@@ -1331,7 +1369,7 @@ int x509_private_key_info_encrypt_to_der(const X509_KEY *x509_key, const char *p
uint8_t **out, size_t *outlen) uint8_t **out, size_t *outlen)
{ {
int ret = -1; int ret = -1;
uint8_t private_key_info[168]; // 150 uint8_t private_key_info[512];
uint8_t *p = private_key_info; uint8_t *p = private_key_info;
size_t private_key_info_len = 0; size_t private_key_info_len = 0;
uint8_t salt[16]; uint8_t salt[16];
@@ -1401,7 +1439,7 @@ int x509_private_key_info_decrypt_from_der(X509_KEY *x509_key,
SM4_KEY sm4_key; SM4_KEY sm4_key;
const uint8_t *enced_private_key_info; const uint8_t *enced_private_key_info;
size_t enced_private_key_info_len; // 160 size_t enced_private_key_info_len; // 160
uint8_t private_key_info[168]; uint8_t private_key_info[512];
const uint8_t *cp = private_key_info; const uint8_t *cp = private_key_info;
size_t private_key_info_len; size_t private_key_info_len;

View File

@@ -16,8 +16,7 @@
#include <gmssl/hex.h> #include <gmssl/hex.h>
#include <gmssl/rand.h> #include <gmssl/rand.h>
#include <gmssl/error.h> #include <gmssl/error.h>
#include <gmssl/ecdsa.h> #include <gmssl/secp256r1.h>
#include <gmssl/secp256r1_ecdsa.h>
/* /*
d 0x5 d 0x5
@@ -154,60 +153,11 @@ static int test_ecdsa_digest_lengths(void)
return 1; return 1;
} }
static int test_ecdsa_generic(void)
{
EC_KEY key;
uint8_t dgst32[32];
uint8_t dgst48[48];
uint8_t sig[SECP256R1_ECDSA_SIGNATURE_MAX_SIZE];
size_t siglen;
key.oid = OID_secp256r1;
if (secp256r1_key_generate(&key.u.secp256r1_key) != 1) {
error_print();
return -1;
}
memset(dgst32, 0x11, sizeof(dgst32));
memset(dgst48, 0x22, sizeof(dgst48));
if (ecdsa_sign(&key, dgst32, sizeof(dgst32), sig, &siglen) != 1
|| siglen > sizeof(sig)
|| ecdsa_verify(&key, dgst32, sizeof(dgst32), sig, siglen) != 1) {
error_print();
return -1;
}
dgst32[0] ^= 0x01;
if (ecdsa_verify(&key, dgst32, sizeof(dgst32), sig, siglen) != 0) {
error_print();
return -1;
}
if (ecdsa_sign(&key, dgst48, sizeof(dgst48), sig, &siglen) != 1
|| siglen > sizeof(sig)
|| ecdsa_verify(&key, dgst48, sizeof(dgst48), sig, siglen) != 1) {
error_print();
return -1;
}
if (ecdsa_sign_fixed_len(&key, dgst48, sizeof(dgst48), siglen, sig) != 1
|| ecdsa_verify(&key, dgst48, sizeof(dgst48), sig, siglen) != 1) {
error_print();
return -1;
}
if (ecdsa_sign(&key, dgst32, 31, sig, &siglen) >= 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void) int main(void)
{ {
if (test_ecdsa() != 1) goto err; if (test_ecdsa() != 1) goto err;
if (test_ecdsa_verify_infinity() != 1) goto err; if (test_ecdsa_verify_infinity() != 1) goto err;
if (test_ecdsa_digest_lengths() != 1) goto err; if (test_ecdsa_digest_lengths() != 1) goto err;
if (test_ecdsa_generic() != 1) goto err;
printf("%s all tests passed\n", __FILE__); printf("%s all tests passed\n", __FILE__);
return 0; return 0;

View File

@@ -11,7 +11,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <gmssl/sm2.h>
#include <gmssl/ec.h> #include <gmssl/ec.h>
#include <gmssl/error.h> #include <gmssl/error.h>
@@ -61,58 +60,9 @@ static int test_ec_named_curve(void)
return 1; return 1;
} }
static int test_ec_point_print(void)
{
SM2_KEY sm2_key;
uint8_t buf[256];
uint8_t *p = buf;
size_t len = 0;
if (sm2_key_generate(&sm2_key) != 1) {
error_print();
return -1;
}
if (sm2_z256_point_to_der(&(sm2_key.public_key), &p, &len) != 1) {
error_print();
return -1;
}
ec_point_print(stderr, 0, 4, "ECPoint", buf, len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_ec_private_key_print(void)
{
SM2_KEY sm2_key;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
if (sm2_key_generate(&sm2_key) != 1) {
error_print();
return -1;
}
if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
ec_private_key_print(stderr, 0, 4, "ECPrivateKey", d, dlen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void) int main(void)
{ {
if (test_ec_named_curve() != 1) goto err; if (test_ec_named_curve() != 1) goto err;
if (test_ec_point_print() != 1) goto err;
if (test_ec_private_key_print() != 1) goto err;
printf("%s all tests passed\n", __FILE__); printf("%s all tests passed\n", __FILE__);
return 0; return 0;
err: err:

View File

@@ -16,7 +16,7 @@
#include <gmssl/rand.h> #include <gmssl/rand.h>
#include <gmssl/mem.h> #include <gmssl/mem.h>
#include <gmssl/error.h> #include <gmssl/error.h>
#include <gmssl/secp256r1_key.h> #include <gmssl/secp256r1.h>
static int test_secp256r1_key_generate(void) static int test_secp256r1_key_generate(void)

View File

@@ -16,8 +16,7 @@
#include <gmssl/hex.h> #include <gmssl/hex.h>
#include <gmssl/rand.h> #include <gmssl/rand.h>
#include <gmssl/error.h> #include <gmssl/error.h>
#include <gmssl/ecdsa.h> #include <gmssl/secp384r1.h>
#include <gmssl/secp384r1_ecdsa.h>
static int test_secp384r1_ecdsa_do_sign(void) static int test_secp384r1_ecdsa_do_sign(void)
{ {
@@ -170,61 +169,12 @@ static int test_secp384r1_ecdsa_verify_infinity(void)
return 1; return 1;
} }
static int test_secp384r1_ecdsa_generic(void)
{
EC_KEY key;
uint8_t dgst32[32];
uint8_t dgst48[48];
uint8_t sig[SECP384R1_ECDSA_SIGNATURE_MAX_SIZE];
size_t siglen;
key.oid = OID_secp384r1;
if (secp384r1_key_generate(&key.u.secp384r1_key) != 1) {
error_print();
return -1;
}
memset(dgst32, 0x33, sizeof(dgst32));
memset(dgst48, 0x44, sizeof(dgst48));
if (ecdsa_sign(&key, dgst48, sizeof(dgst48), sig, &siglen) != 1
|| siglen > sizeof(sig)
|| ecdsa_verify(&key, dgst48, sizeof(dgst48), sig, siglen) != 1) {
error_print();
return -1;
}
dgst48[0] ^= 0x01;
if (ecdsa_verify(&key, dgst48, sizeof(dgst48), sig, siglen) != 0) {
error_print();
return -1;
}
if (ecdsa_sign(&key, dgst32, sizeof(dgst32), sig, &siglen) != 1
|| siglen > sizeof(sig)
|| ecdsa_verify(&key, dgst32, sizeof(dgst32), sig, siglen) != 1) {
error_print();
return -1;
}
if (ecdsa_sign_fixed_len(&key, dgst32, sizeof(dgst32), siglen, sig) != 1
|| ecdsa_verify(&key, dgst32, sizeof(dgst32), sig, siglen) != 1) {
error_print();
return -1;
}
if (ecdsa_sign(&key, dgst32, 31, sig, &siglen) >= 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void) int main(void)
{ {
if (test_secp384r1_ecdsa_do_sign() != 1) goto err; if (test_secp384r1_ecdsa_do_sign() != 1) goto err;
if (test_secp384r1_ecdsa_sign() != 1) goto err; if (test_secp384r1_ecdsa_sign() != 1) goto err;
if (test_secp384r1_ecdsa_ctx() != 1) goto err; if (test_secp384r1_ecdsa_ctx() != 1) goto err;
if (test_secp384r1_ecdsa_verify_infinity() != 1) goto err; if (test_secp384r1_ecdsa_verify_infinity() != 1) goto err;
if (test_secp384r1_ecdsa_generic() != 1) goto err;
printf("%s all tests passed\n", __FILE__); printf("%s all tests passed\n", __FILE__);
return 0; return 0;

View File

@@ -16,7 +16,7 @@
#include <gmssl/rand.h> #include <gmssl/rand.h>
#include <gmssl/mem.h> #include <gmssl/mem.h>
#include <gmssl/error.h> #include <gmssl/error.h>
#include <gmssl/secp384r1_key.h> #include <gmssl/secp384r1.h>
static int test_secp384r1_key_generate(void) static int test_secp384r1_key_generate(void)

View File

@@ -35,6 +35,9 @@ struct {
#ifdef ENABLE_SECP256R1 #ifdef ENABLE_SECP256R1
{ OID_ec_public_key, OID_secp256r1 }, { OID_ec_public_key, OID_secp256r1 },
#endif #endif
#ifdef ENABLE_SECP384R1
{ OID_ec_public_key, OID_secp384r1 },
#endif
#ifdef ENABLE_LMS #ifdef ENABLE_LMS
{ OID_lms_hashsig, LMS_SM3_M32_H5 }, { OID_lms_hashsig, LMS_SM3_M32_H5 },
{ OID_hss_lms_hashsig, OID_undef }, // use lms_types[] { OID_hss_lms_hashsig, OID_undef }, // use lms_types[]
@@ -195,96 +198,6 @@ static int test_x509_public_key_info_to_der(void)
return 1; return 1;
} }
static int test_ec_private_key_to_der(void)
{
X509_KEY key;
uint8_t buf[512];
int i;
for (i = 0; i < sizeof(tests)/sizeof(tests[0]) && tests[i].algor == OID_ec_public_key; i++) {
const uint8_t *cp = buf;
uint8_t *p = buf;
size_t len = 0;
int encode_params;
int encode_pubkey;
// test 1
encode_params = 0;
encode_pubkey = 0;
if (ec_private_key_to_der(&x509_keys[i], encode_params, encode_pubkey, &p, &len) != 1) {
error_print();
return -1;
}
//format_print(stderr, 0, 0, "ECPrivateKey encode_params = %d, encode_pubkey = %d\n", encode_params, encode_pubkey);
//format_bytes(stderr, 0, 0, "ECPrivateKey", buf, len);
if (ec_private_key_from_der(&key, tests[i].algor_param, &cp, &len) != 1) {
error_print();
return -1;
}
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
error_print();
return -1;
}
// test 2
encode_params = 0;
encode_pubkey = 1;
if (ec_private_key_to_der(&x509_keys[i], encode_params, encode_pubkey, &p, &len) != 1) {
error_print();
return -1;
}
//format_print(stderr, 0, 0, "ECPrivateKey encode_params = %d, encode_pubkey = %d\n", encode_params, encode_pubkey);
//format_bytes(stderr, 0, 0, "ECPrivateKey", buf, len);
if (ec_private_key_from_der(&key, tests[i].algor_param, &cp, &len) != 1) {
error_print();
return -1;
}
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
error_print();
return -1;
}
// test 3
encode_params = 1;
encode_pubkey = 0;
if (ec_private_key_to_der(&x509_keys[i], encode_params, encode_pubkey, &p, &len) != 1) {
error_print();
return -1;
}
//format_print(stderr, 0, 0, "ECPrivateKey encode_params = %d, encode_pubkey = %d\n", encode_params, encode_pubkey);
//format_bytes(stderr, 0, 0, "ECPrivateKey", buf, len);
if (ec_private_key_from_der(&key, tests[i].algor_param, &cp, &len) != 1) {
error_print();
return -1;
}
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
error_print();
return -1;
}
// test 4
encode_params = 1;
encode_pubkey = 1;
if (ec_private_key_to_der(&x509_keys[i], encode_params, encode_pubkey, &p, &len) != 1) {
error_print();
return -1;
}
//format_print(stderr, 0, 0, "ECPrivateKey encode_params = %d, encode_pubkey = %d\n", encode_params, encode_pubkey);
//format_bytes(stderr, 0, 0, "ECPrivateKey", buf, len);
if (ec_private_key_from_der(&key, tests[i].algor_param, &cp, &len) != 1) {
error_print();
return -1;
}
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
error_print();
return -1;
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_private_key_info_to_der(void) static int test_x509_private_key_info_to_der(void)
{ {
X509_KEY key; X509_KEY key;
@@ -455,11 +368,17 @@ static int test_x509_sign(void)
memset(msg, 0xa5, sizeof(msg)); memset(msg, 0xa5, sizeof(msg));
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) { for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
int sign_algor;
if (tests[i].algor == OID_kyber_kem) { if (tests[i].algor == OID_kyber_kem) {
continue; continue;
} }
sign_algor = test_sign_algor(&x509_keys[i]);
if (sign_algor == OID_undef) {
continue;
}
//format_print(stderr, 0, 4, "%s\n", x509_public_key_algor_name(tests[i].algor)); //format_print(stderr, 0, 4, "%s\n", x509_public_key_algor_name(tests[i].algor));
if (x509_sign_init(&sign_ctx, &x509_keys[i], test_sign_algor(&x509_keys[i]), args, argslen) != 1) { if (x509_sign_init(&sign_ctx, &x509_keys[i], sign_algor, args, argslen) != 1) {
error_print(); error_print();
return -1; return -1;
} }
@@ -468,7 +387,7 @@ static int test_x509_sign(void)
return -1; return -1;
} }
format_print(stderr, 0, 4, "%s: %zu\n", x509_public_key_algor_name(tests[i].algor), siglen); format_print(stderr, 0, 4, "%s: %zu\n", x509_public_key_algor_name(tests[i].algor), siglen);
if (x509_verify_init(&sign_ctx, &x509_keys[i], test_sign_algor(&x509_keys[i]), args, argslen, sig, siglen) != 1) { if (x509_verify_init(&sign_ctx, &x509_keys[i], sign_algor, args, argslen, sig, siglen) != 1) {
error_print(); error_print();
return -1; return -1;
} }
@@ -558,6 +477,9 @@ static int test_x509_key_exchange(void)
if (tests[i].algor != OID_ec_public_key) { if (tests[i].algor != OID_ec_public_key) {
continue; continue;
} }
if (tests[i].algor_param == OID_secp384r1) {
continue;
}
if (x509_key_generate(&key, tests[i].algor, &tests[i].algor_param, sizeof(tests[i].algor_param)) != 1) { if (x509_key_generate(&key, tests[i].algor, &tests[i].algor_param, sizeof(tests[i].algor_param)) != 1) {
error_print(); error_print();
return -1; return -1;
@@ -660,7 +582,6 @@ int main(void)
if (test_x509_key_generate() != 1) goto err; if (test_x509_key_generate() != 1) goto err;
if (test_x509_public_key_to_bytes() != 1) goto err; if (test_x509_public_key_to_bytes() != 1) goto err;
if (test_x509_public_key_info_to_der() != 1) goto err; if (test_x509_public_key_info_to_der() != 1) goto err;
if (test_ec_private_key_to_der() != 1) goto err;
if (test_x509_private_key_info_to_der() != 1) goto err; if (test_x509_private_key_info_to_der() != 1) goto err;
if (test_x509_private_key_info_encrypt_to_der() != 1) goto err; if (test_x509_private_key_info_encrypt_to_der() != 1) goto err;
if (test_x509_private_key_info_encrypt_to_pem() != 1) goto err; if (test_x509_private_key_info_encrypt_to_pem() != 1) goto err;