mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
添加Openssl,Gmssl加密库
This commit is contained in:
BIN
thirdparty/GmSSL-3.1.1/bin/gmssl.dll
vendored
BIN
thirdparty/GmSSL-3.1.1/bin/gmssl.dll
vendored
Binary file not shown.
BIN
thirdparty/GmSSL-3.1.1/bin/gmssl.exe
vendored
BIN
thirdparty/GmSSL-3.1.1/bin/gmssl.exe
vendored
Binary file not shown.
108
thirdparty/GmSSL-3.1.1/include/gmssl/aead.h
vendored
108
thirdparty/GmSSL-3.1.1/include/gmssl/aead.h
vendored
@@ -1,108 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2023 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_AEAD_H
|
||||
#define GMSSL_AEAD_H
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/sm4.h>
|
||||
#include <gmssl/gcm.h>
|
||||
#include <gmssl/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
SM4_CBC_CTX enc_ctx;
|
||||
SM3_HMAC_CTX mac_ctx;
|
||||
uint8_t mac[SM3_HMAC_SIZE];
|
||||
size_t maclen;
|
||||
} SM4_CBC_SM3_HMAC_CTX;
|
||||
|
||||
#define SM4_CBC_SM3_HMAC_KEY_SIZE 48
|
||||
#define SM4_CBC_SM3_HMAC_IV_SIZE 16
|
||||
|
||||
_gmssl_export int sm4_cbc_sm3_hmac_encrypt_init(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen);
|
||||
_gmssl_export int sm4_cbc_sm3_hmac_encrypt_update(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
_gmssl_export int sm4_cbc_sm3_hmac_encrypt_finish(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
_gmssl_export int sm4_cbc_sm3_hmac_decrypt_init(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen);
|
||||
_gmssl_export int sm4_cbc_sm3_hmac_decrypt_update(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
_gmssl_export int sm4_cbc_sm3_hmac_decrypt_finish(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
typedef struct {
|
||||
SM4_CTR_CTX enc_ctx;
|
||||
SM3_HMAC_CTX mac_ctx;
|
||||
uint8_t mac[SM3_HMAC_SIZE];
|
||||
size_t maclen;
|
||||
} SM4_CTR_SM3_HMAC_CTX;
|
||||
|
||||
#define SM4_CTR_SM3_HMAC_KEY_SIZE 48
|
||||
#define SM4_CTR_SM3_HMAC_IV_SIZE 16
|
||||
|
||||
_gmssl_export int sm4_ctr_sm3_hmac_encrypt_init(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen);
|
||||
_gmssl_export int sm4_ctr_sm3_hmac_encrypt_update(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
_gmssl_export int sm4_ctr_sm3_hmac_encrypt_finish(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
_gmssl_export int sm4_ctr_sm3_hmac_decrypt_init(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen);
|
||||
_gmssl_export int sm4_ctr_sm3_hmac_decrypt_update(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
_gmssl_export int sm4_ctr_sm3_hmac_decrypt_finish(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
typedef struct {
|
||||
SM4_CTR_CTX enc_ctx;
|
||||
GHASH_CTX mac_ctx;
|
||||
uint8_t Y[16]; // E(K, Y_0)
|
||||
size_t taglen;
|
||||
uint8_t mac[16];
|
||||
size_t maclen;
|
||||
} SM4_GCM_CTX;
|
||||
|
||||
#define SM4_GCM_KEY_SIZE 16
|
||||
#define SM4_GCM_DEFAULT_TAG_SIZE 16
|
||||
|
||||
_gmssl_export int sm4_gcm_encrypt_init(SM4_GCM_CTX *ctx,
|
||||
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, size_t taglen);
|
||||
_gmssl_export int sm4_gcm_encrypt_update(SM4_GCM_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
_gmssl_export int sm4_gcm_encrypt_finish(SM4_GCM_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
_gmssl_export int sm4_gcm_decrypt_init(SM4_GCM_CTX *ctx,
|
||||
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, size_t taglen);
|
||||
_gmssl_export int sm4_gcm_decrypt_update(SM4_GCM_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
_gmssl_export int sm4_gcm_decrypt_finish(SM4_GCM_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
23
thirdparty/GmSSL-3.1.1/include/gmssl/api.h
vendored
23
thirdparty/GmSSL-3.1.1/include/gmssl/api.h
vendored
@@ -1,23 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2023 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_API_H
|
||||
#define GMSSL_API_H
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#define _gmssl_export __declspec(dllexport)
|
||||
#elif defined(__GNUC__)
|
||||
// use -fvisibility=hidden to change the "default" behavior
|
||||
#define _gmssl_export __attribute__((visibility("default")))
|
||||
#else
|
||||
#define _gmssl_export
|
||||
#endif
|
||||
|
||||
#endif
|
||||
20
thirdparty/GmSSL-3.1.1/include/gmssl/asm.h
vendored
Normal file
20
thirdparty/GmSSL-3.1.1/include/gmssl/asm.h
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2014-2024 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_ASM_H
|
||||
#define GMSSL_ASM_H
|
||||
|
||||
#ifdef ENABLE_ASM_UNDERSCORE_PREFIX
|
||||
# define func(foo) _##foo
|
||||
#else
|
||||
# define func(foo) foo
|
||||
#endif
|
||||
|
||||
#endif
|
||||
27
thirdparty/GmSSL-3.1.1/include/gmssl/asn1.h
vendored
27
thirdparty/GmSSL-3.1.1/include/gmssl/asn1.h
vendored
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
@@ -86,8 +86,8 @@ int asn1_type_from_der(int tag, const uint8_t **d, size_t *dlen, const uint8_t *
|
||||
int asn1_nonempty_type_to_der(int tag, const uint8_t *d, size_t dlen, uint8_t **out, size_t *outlen);
|
||||
int asn1_nonempty_type_from_der(int tag, const uint8_t **d, size_t *dlen, const uint8_t **in, size_t *inlen);
|
||||
int asn1_any_type_from_der(int *tag, const uint8_t **d, size_t *dlen, const uint8_t **in, size_t *inlen);
|
||||
int asn1_any_to_der(const uint8_t *a, size_t alen, uint8_t **out, size_t *outlen); // 调用方应保证a,alen为TLV
|
||||
int asn1_any_from_der(const uint8_t **a, size_t *alen, const uint8_t **in, size_t *inlen); // 该函数会检查输入是否为TLV
|
||||
int asn1_any_to_der(const uint8_t *a, size_t alen, uint8_t **out, size_t *outlen); // NOTE: a,alen MUST be TLV
|
||||
int asn1_any_from_der(const uint8_t **a, size_t *alen, const uint8_t **in, size_t *inlen); // asn1_any_from_der check if input is TLV
|
||||
|
||||
#define ASN1_TRUE 0xff
|
||||
#define ASN1_FALSE 0x00
|
||||
@@ -101,7 +101,7 @@ int asn1_boolean_from_der_ex(int tag, int *val, const uint8_t **in, size_t *inle
|
||||
#define asn1_implicit_boolean_to_der(i,val,out,outlen) asn1_boolean_to_der_ex(ASN1_TAG_IMPLICIT(i),val,out,outlen)
|
||||
#define asn1_implicit_boolean_from_der(i,val,in,inlen) asn1_boolean_from_der_ex(ASN1_TAG_IMPLICIT(i),val,in,inlen)
|
||||
|
||||
// asn1_integer_ 不支持负数编解码
|
||||
// asn1_integer_ does not support negative value
|
||||
int asn1_integer_to_der_ex(int tag, const uint8_t *d, size_t dlen, uint8_t **out, size_t *outlen);
|
||||
int asn1_integer_from_der_ex(int tag, const uint8_t **d, size_t *dlen, const uint8_t **in, size_t *inlen);
|
||||
#define asn1_integer_to_der(d,dlen,out,outlen) asn1_integer_to_der_ex(ASN1_TAG_INTEGER,d,dlen,out,outlen)
|
||||
@@ -109,15 +109,15 @@ int asn1_integer_from_der_ex(int tag, const uint8_t **d, size_t *dlen, const uin
|
||||
#define asn1_implicit_integer_to_der(i,d,dlen,out,outlen) asn1_integer_to_der_ex(ASN1_TAG_IMPLICIT(i),d,dlen,out,outlen)
|
||||
#define asn1_implicit_integer_from_der(i,d,dlen,in,inlen) asn1_integer_from_der_ex(ASN1_TAG_IMPLICIT(i),d,dlen,in,inlen)
|
||||
|
||||
// asn1_int_ 只支持小的无符号整数的编解码,不支持负数
|
||||
int asn1_int_to_der_ex(int tag, int val, uint8_t **out, size_t *outlen); // 当 val == -1 时,不输出,返回 0
|
||||
int asn1_int_from_der_ex(int tag, int *val, const uint8_t **in, size_t *inlen); // 不支持负数,返回0时 *val 设置为 -1
|
||||
// asn1_int_ only support small unsigned int, does not support negative integer
|
||||
int asn1_int_to_der_ex(int tag, int val, uint8_t **out, size_t *outlen); // when val == -1, return 0 and no output
|
||||
int asn1_int_from_der_ex(int tag, int *val, const uint8_t **in, size_t *inlen); // when return 0, *val is set to -1
|
||||
#define asn1_int_to_der(val,out,outlen) asn1_int_to_der_ex(ASN1_TAG_INTEGER,val,out,outlen)
|
||||
#define asn1_int_from_der(val,in,inlen) asn1_int_from_der_ex(ASN1_TAG_INTEGER,val,in,inlen)
|
||||
#define asn1_implicit_int_to_der(i,val,out,outlen) asn1_int_to_der_ex(ASN1_TAG_IMPLICIT(i),val,out,outlen)
|
||||
#define asn1_implicit_int_from_der(i,val,in,inlen) asn1_int_from_der_ex(ASN1_TAG_IMPLICIT(i),val,in,inlen)
|
||||
|
||||
// 比特长度不必须为8的整数倍
|
||||
// bit_string can be any length
|
||||
int asn1_bit_string_to_der_ex(int tag, const uint8_t *d, size_t nbits, uint8_t **out, size_t *outlen);
|
||||
int asn1_bit_string_from_der_ex(int tag, const uint8_t **d, size_t *nbits, const uint8_t **in, size_t *inlen);
|
||||
#define asn1_bit_string_to_der(d,nbits,out,outlen) asn1_bit_string_to_der_ex(ASN1_TAG_BIT_STRING,d,nbits,out,outlen)
|
||||
@@ -125,7 +125,7 @@ int asn1_bit_string_from_der_ex(int tag, const uint8_t **d, size_t *nbits, const
|
||||
#define asn1_implicit_bit_string_to_der(i,d,nbits,out,outlen) asn1_bit_string_to_der_ex(ASN1_TAG_IMPLICIT(i),d,nbits,out,outlen)
|
||||
#define asn1_implicit_bit_string_from_der(i,d,nbits,in,inlen) asn1_bit_string_from_der_ex(ASN1_TAG_IMPLICIT(i),d,nbits,in,inlen)
|
||||
|
||||
// 比特长度必须为8的整数倍,因此使用字节长度
|
||||
// bit string MUST be multiple 8 bits
|
||||
int asn1_bit_octets_to_der_ex(int tag, const uint8_t *d, size_t dlen, uint8_t **out, size_t *outlen);
|
||||
int asn1_bit_octets_from_der_ex(int tag, const uint8_t **d, size_t *dlen, const uint8_t **in, size_t *inlen);
|
||||
#define asn1_bit_octets_to_der(d,dlen,out,outlen) asn1_bit_octets_to_der_ex(ASN1_TAG_BIT_STRING,d,dlen,out,outlen)
|
||||
@@ -133,14 +133,14 @@ int asn1_bit_octets_from_der_ex(int tag, const uint8_t **d, size_t *dlen, const
|
||||
#define asn1_implicit_bit_octets_to_der(i,d,dlen,out,outlen) asn1_bit_octets_to_der_ex(ASN1_TAG_IMPLICIT(i),d,dlen,out,outlen)
|
||||
#define asn1_implicit_bit_octets_from_der(i,d,dlen,in,inlen) asn1_bit_octets_from_der_ex(ASN1_TAG_IMPLICIT(i),d,dlen,in,inlen)
|
||||
|
||||
// bits == -1 不编码,只支持较少的比特数量
|
||||
// bits == -1 means no input
|
||||
int asn1_bits_to_der_ex(int tag, int bits, uint8_t **out, size_t *outlen);
|
||||
int asn1_bits_from_der_ex(int tag, int *bits, const uint8_t **in, size_t *inlen);
|
||||
#define asn1_bits_to_der(bits,out,outlen) asn1_bits_to_der_ex(ASN1_TAG_BIT_STRING,bits,out,outlen)
|
||||
#define asn1_bits_from_der(bits,in,inlen) asn1_bits_from_der_ex(ASN1_TAG_BIT_STRING,bits,in,inlen)
|
||||
#define asn1_implicit_bits_to_der(i,bits,out,outlen) asn1_bits_to_der_ex(ASN1_TAG_IMPLICIT(i),bits,out,outlen)
|
||||
#define asn1_implicit_bits_from_der(i,bits,in,inlen) asn1_bits_from_der_ex(ASN1_TAG_IMPLICIT(i),bits,in,inlen)
|
||||
// names[i]对应第i个比特
|
||||
// names[i] is the i-th bit
|
||||
int asn1_bits_print(FILE *fp, int fmt, int ind, const char *label, const char **names, size_t names_cnt, int bits);
|
||||
|
||||
#define asn1_octet_string_to_der_ex(tag,d,dlen,out,outlen) asn1_type_to_der(tag,d,dlen,out,outlen)
|
||||
@@ -182,7 +182,8 @@ typedef struct {
|
||||
|
||||
const ASN1_OID_INFO *asn1_oid_info_from_name(const ASN1_OID_INFO *infos, size_t count, const char *name);
|
||||
const ASN1_OID_INFO *asn1_oid_info_from_oid(const ASN1_OID_INFO *infos, size_t count, int oid);
|
||||
// 如果一个正确解析的OID并不在infos列表中,那么仍然返回1,但是调用方必须检查返回的info是否为空
|
||||
|
||||
// well-formed but unknown OID will return 1, caller should check if *info == NULL when return 1
|
||||
int asn1_oid_info_from_der_ex(const ASN1_OID_INFO **info, uint32_t *nodes, size_t *nodes_cnt,
|
||||
const ASN1_OID_INFO *infos, size_t count, const uint8_t **in, size_t *inlen);
|
||||
int asn1_oid_info_from_der(const ASN1_OID_INFO **info,
|
||||
@@ -283,7 +284,7 @@ int asn1_header_to_der(int tag, size_t dlen, uint8_t **out, size_t *outlen);
|
||||
#define asn1_explicit_to_der(i,d,dlen,out,outlen) asn1_nonempty_type_to_der(ASN1_TAG_EXPLICIT(i),d,dlen,out,outlen)
|
||||
#define asn1_explicit_from_der(i,d,dlen,in,inlen) asn1_nonempty_type_from_der(ASN1_TAG_EXPLICIT(i),d,dlen,in,inlen)
|
||||
|
||||
// d,dlen 是 SEQUENCE OF, SET OF 中的值
|
||||
// d,dlen is the V (of TLV) of SEQUENCE OF, SET OF
|
||||
int asn1_types_get_count(const uint8_t *d, size_t dlen, int tag, size_t *cnt);
|
||||
int asn1_types_get_item_by_index(const uint8_t *d, size_t dlen, int tag,
|
||||
int index, const uint8_t **item_d, size_t *item_dlen);
|
||||
|
||||
15
thirdparty/GmSSL-3.1.1/include/gmssl/base64.h
vendored
15
thirdparty/GmSSL-3.1.1/include/gmssl/base64.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2023 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.
|
||||
@@ -19,19 +19,6 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
BASE64 Public API
|
||||
|
||||
BASE64_CTX
|
||||
base64_encode_init
|
||||
base64_encode_update
|
||||
base64_encode_finish
|
||||
base64_decode_init
|
||||
base64_decode_update
|
||||
base64_decode_finish
|
||||
|
||||
*/
|
||||
|
||||
|
||||
typedef struct {
|
||||
/* number saved in a partial encode/decode */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2023 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.
|
||||
@@ -7,9 +7,6 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#ifndef GMSSL_BLOCK_CIPHER_H
|
||||
#define GMSSL_BLOCK_CIPHER_H
|
||||
|
||||
@@ -17,8 +14,10 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/aes.h>
|
||||
#include <gmssl/sm4.h>
|
||||
#ifdef ENABLE_AES
|
||||
#include <gmssl/aes.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -37,7 +36,9 @@ typedef struct BLOCK_CIPHER_KEY BLOCK_CIPHER_KEY;
|
||||
struct BLOCK_CIPHER_KEY {
|
||||
union {
|
||||
SM4_KEY sm4_key;
|
||||
#ifdef ENABLE_AES
|
||||
AES_KEY aes_key;
|
||||
#endif
|
||||
} u;
|
||||
const BLOCK_CIPHER *cipher;
|
||||
};
|
||||
@@ -58,7 +59,9 @@ struct BLOCK_CIPHER {
|
||||
};
|
||||
|
||||
const BLOCK_CIPHER *BLOCK_CIPHER_sm4(void);
|
||||
#ifdef ENABLE_AES
|
||||
const BLOCK_CIPHER *BLOCK_CIPHER_aes128(void);
|
||||
#endif
|
||||
|
||||
const BLOCK_CIPHER *block_cipher_from_name(const char *name);
|
||||
const char *block_cipher_name(const BLOCK_CIPHER *cipher);
|
||||
|
||||
58
thirdparty/GmSSL-3.1.1/include/gmssl/cms.h
vendored
58
thirdparty/GmSSL-3.1.1/include/gmssl/cms.h
vendored
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
@@ -62,7 +62,7 @@ int cms_content_info_to_der(
|
||||
uint8_t **out, size_t *outlen);
|
||||
int cms_content_info_from_der(
|
||||
int *content_type,
|
||||
const uint8_t **content, size_t *content_len, // 这里获得的是完整的TLV
|
||||
const uint8_t **content, size_t *content_len, // content is the full TLV
|
||||
const uint8_t **in, size_t *inlen);
|
||||
int cms_content_info_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
|
||||
|
||||
@@ -269,13 +269,13 @@ typedef struct {
|
||||
|
||||
int cms_signed_data_sign_to_der(
|
||||
const CMS_CERTS_AND_KEY *signers, size_t signers_cnt,
|
||||
int content_type, const uint8_t *data, size_t datalen, // 当OID_cms_data时为raw data
|
||||
const uint8_t *crls, size_t crls_len, // 可以为空
|
||||
int content_type, const uint8_t *data, size_t datalen, // with OID_cms_data, `data` is the raw data
|
||||
const uint8_t *crls, size_t crls_len, // crls can be NULL
|
||||
uint8_t **out, size_t *outlen);
|
||||
int cms_signed_data_verify_from_der(
|
||||
const uint8_t *extra_certs, size_t extra_certs_len,
|
||||
const uint8_t *extra_crls, size_t extra_crls_len,
|
||||
int *content_type, const uint8_t **content, size_t *content_len, // 是否应该返回raw data呢?
|
||||
int *content_type, const uint8_t **content, size_t *content_len, // should we return raw data
|
||||
const uint8_t **certs, size_t *certs_len,
|
||||
const uint8_t **crls, size_t *crls_len,
|
||||
const uint8_t **signer_infos, size_t *signer_infos_len,
|
||||
@@ -289,8 +289,8 @@ RecipientInfo ::= SEQUENCE {
|
||||
keyEncryptionAlgorithm AlgorithmIdentifier,
|
||||
encryptedKey OCTET STRING -- DER-encoding of SM2Cipher
|
||||
}
|
||||
由于encryptedKey的类型为SM2Cipher, 而SM2Cipher中有2个INTEGER,因此长度是不固定的。
|
||||
因此不能预先确定输出长度
|
||||
|
||||
DER-encoding length of RecipientInfo is not fixed (caused by INTEGERs)
|
||||
*/
|
||||
int cms_recipient_info_to_der(
|
||||
int version,
|
||||
@@ -303,7 +303,7 @@ int cms_recipient_info_from_der(
|
||||
int *version,
|
||||
const uint8_t **issuer, size_t *issuer_len,
|
||||
const uint8_t **serial_number, size_t *serial_number_len,
|
||||
int *pke_algor, const uint8_t **params, size_t *params_len,// SM2加密只使用SM3,没有默认参数,但是ECIES可能有
|
||||
int *pke_algor, const uint8_t **params, size_t *params_len,// sm2encrypt has no params, but ECIES might have params
|
||||
const uint8_t **enced_key, size_t *enced_key_len,
|
||||
const uint8_t **in, size_t *inlen);
|
||||
int cms_recipient_info_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
|
||||
@@ -452,35 +452,29 @@ int cms_key_agreement_info_print(FILE *fp, int fmt, int ind, const char *label,
|
||||
|
||||
|
||||
|
||||
// 下面是公开API
|
||||
// 公开API的设计考虑:
|
||||
// 1. 不需要调用其他函数
|
||||
// 2. 在逻辑上容易理解
|
||||
// 3. 将cms,cmslen看做对象
|
||||
|
||||
|
||||
// 生成ContentInfo, type == data
|
||||
// generate ContentInfo, type == data
|
||||
int cms_set_data(uint8_t *cms, size_t *cmslen,
|
||||
const uint8_t *d, size_t dlen);
|
||||
|
||||
int cms_encrypt(
|
||||
uint8_t *cms, size_t *cmslen, // 输出的ContentInfo (type encryptedData)
|
||||
int enc_algor, const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen, // 对称加密算法、密钥和IV
|
||||
int content_type, const uint8_t *content, size_t content_len, // 待加密的输入数据
|
||||
const uint8_t *shared_info1, size_t shared_info1_len, // 附加信息
|
||||
uint8_t *cms, size_t *cmslen,
|
||||
int enc_algor, const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
|
||||
int content_type, const uint8_t *content, size_t content_len,
|
||||
const uint8_t *shared_info1, size_t shared_info1_len,
|
||||
const uint8_t *shared_info2, size_t shared_info2_len);
|
||||
|
||||
int cms_decrypt(
|
||||
const uint8_t *cms, size_t cmslen, // 输入的ContentInfo (type encryptedData)
|
||||
int *enc_algor, const uint8_t *key, size_t keylen, // 解密密钥(我们不知道解密算法)
|
||||
int *content_type, uint8_t *content, size_t *content_len, // 输出的解密数据类型及数据
|
||||
const uint8_t **shared_info1, size_t *shared_info1_len, // 附加信息
|
||||
const uint8_t *cms, size_t cmslen, // should be ContentInfo (type encryptedData)
|
||||
int *enc_algor, const uint8_t *key, size_t keylen,
|
||||
int *content_type, uint8_t *content, size_t *content_len,
|
||||
const uint8_t **shared_info1, size_t *shared_info1_len,
|
||||
const uint8_t **shared_info2, size_t *shared_info2_len);
|
||||
|
||||
int cms_sign(
|
||||
uint8_t *cms, size_t *cms_len,
|
||||
const CMS_CERTS_AND_KEY *signers, size_t signers_cnt, // 签名者的签名私钥和证书
|
||||
int content_type, const uint8_t *content, size_t content_len, // 待签名的输入数据
|
||||
const CMS_CERTS_AND_KEY *signers, size_t signers_cnt,
|
||||
int content_type, const uint8_t *content, size_t content_len,
|
||||
const uint8_t *crls, size_t crls_len);
|
||||
|
||||
int cms_verify(
|
||||
@@ -494,17 +488,17 @@ int cms_verify(
|
||||
|
||||
int cms_envelop(
|
||||
uint8_t *cms, size_t *cms_len,
|
||||
const uint8_t *rcpt_certs, size_t rcpt_certs_len, // 接收方证书,注意这个参数的类型可以容纳多个证书,但是只有在一个接受者时对调用方最方便
|
||||
int enc_algor, const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen, // 对称加密算法及参数
|
||||
int content_type, const uint8_t *content, size_t content_len, // 待加密的输入数据
|
||||
const uint8_t *shared_info1, size_t shared_info1_len, // 附加输入信息
|
||||
const uint8_t *rcpt_certs, size_t rcpt_certs_len,
|
||||
int enc_algor, const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
|
||||
int content_type, const uint8_t *content, size_t content_len,
|
||||
const uint8_t *shared_info1, size_t shared_info1_len,
|
||||
const uint8_t *shared_info2, size_t shared_info2_len);
|
||||
|
||||
int cms_deenvelop(
|
||||
const uint8_t *cms, size_t cms_len,
|
||||
const SM2_KEY *rcpt_key, const uint8_t *rcpt_cert, size_t rcpt_cert_len, // 接收方的解密私钥和对应的证书,注意只需要一个解密方
|
||||
const SM2_KEY *rcpt_key, const uint8_t *rcpt_cert, size_t rcpt_cert_len,
|
||||
int *content_type, uint8_t *content, size_t *content_len,
|
||||
const uint8_t **rcpt_infos, size_t *rcpt_infos_len, // 解析得到,用于显示
|
||||
const uint8_t **rcpt_infos, size_t *rcpt_infos_len,
|
||||
const uint8_t **shared_info1, size_t *shared_info1_len,
|
||||
const uint8_t **shared_info2, size_t *shared_info2_len);
|
||||
|
||||
@@ -531,7 +525,7 @@ int cms_deenvelop_and_verify(
|
||||
const uint8_t **shared_info1, size_t *shared_info1_len,
|
||||
const uint8_t **shared_info2, size_t *shared_info2_len);
|
||||
|
||||
// 生成ContentInfo, type == keyAgreementInfo
|
||||
// create ContentInfo, type == keyAgreementInfo
|
||||
int cms_set_key_agreement_info(
|
||||
uint8_t *cms, size_t *cms_len,
|
||||
const SM2_KEY *temp_public_key_r,
|
||||
|
||||
58
thirdparty/GmSSL-3.1.1/include/gmssl/des.h
vendored
58
thirdparty/GmSSL-3.1.1/include/gmssl/des.h
vendored
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2022 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
|
||||
*/
|
||||
|
||||
|
||||
/* FIPS PUB 46-3 "Data Encryption Standard (DES)" */
|
||||
|
||||
#ifndef GMSSL_DES_H
|
||||
#define GMSSL_DES_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define DES_KEY_BITS 56
|
||||
#define DES_BLOCK_BITS 64
|
||||
#define DES_KEY_SIZE ((DES_KEY_BITS)/7)
|
||||
#define DES_BLOCK_SIZE (DES_BLOCK_BITS/8)
|
||||
|
||||
#define DES_RK_BITS 48
|
||||
#define DES_RK_SIZE (DES_RK_BITS/8)
|
||||
#define DES_ROUNDS 16
|
||||
|
||||
#define DES_EDE_KEY_SIZE (DES_KEY_SIZE * 3)
|
||||
|
||||
typedef struct {
|
||||
uint64_t rk[DES_ROUNDS];
|
||||
} DES_KEY;
|
||||
|
||||
void des_set_encrypt_key(DES_KEY *key, const uint8_t raw_key[DES_KEY_SIZE]);
|
||||
void des_set_decrypt_key(DES_KEY *key, const uint8_t raw_key[DES_KEY_SIZE]);
|
||||
void des_encrypt(DES_KEY *key, const uint8_t in[DES_BLOCK_SIZE], uint8_t out[DES_BLOCK_SIZE]);
|
||||
|
||||
|
||||
typedef struct {
|
||||
DES_KEY K[3];
|
||||
} DES_EDE_KEY;
|
||||
|
||||
void des_ede_set_encrypt_key(DES_EDE_KEY *key, const uint8_t raw_key[DES_EDE_KEY_SIZE]);
|
||||
void des_ede_set_decrypt_key(DES_EDE_KEY *key, const uint8_t raw_key[DES_EDE_KEY_SIZE]);
|
||||
void des_ede_encrypt(DES_EDE_KEY *key, const uint8_t in[DES_BLOCK_SIZE], uint8_t out[DES_BLOCK_SIZE]);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
18
thirdparty/GmSSL-3.1.1/include/gmssl/digest.h
vendored
18
thirdparty/GmSSL-3.1.1/include/gmssl/digest.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2023 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.
|
||||
@@ -16,12 +16,12 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#ifdef ENABLE_BROKEN_CRYPTO
|
||||
#include <gmssl/md5.h>
|
||||
#ifdef ENABLE_SHA1
|
||||
#include <gmssl/sha1.h>
|
||||
#endif
|
||||
#ifdef ENABLE_SHA2
|
||||
#include <gmssl/sha2.h>
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -39,14 +39,15 @@ typedef struct DIGEST_CTX DIGEST_CTX;
|
||||
struct DIGEST_CTX {
|
||||
union {
|
||||
SM3_CTX sm3_ctx;
|
||||
#ifdef ENABLE_BROKEN_CRYPTO
|
||||
MD5_CTX md5_ctx;
|
||||
#ifdef ENABLE_SHA1
|
||||
SHA1_CTX sha1_ctx;
|
||||
#endif
|
||||
#ifdef ENABLE_SHA2
|
||||
SHA224_CTX sha224_ctx;
|
||||
SHA256_CTX sha256_ctx;
|
||||
SHA384_CTX sha384_ctx;
|
||||
SHA512_CTX sha512_ctx;
|
||||
#endif
|
||||
} u;
|
||||
const DIGEST *digest;
|
||||
};
|
||||
@@ -62,16 +63,17 @@ struct DIGEST {
|
||||
};
|
||||
|
||||
const DIGEST *DIGEST_sm3(void);
|
||||
#ifdef ENABLE_BROKEN_CRYPTO
|
||||
const DIGEST *DIGEST_md5(void);
|
||||
#ifdef ENABLE_SHA1
|
||||
const DIGEST *DIGEST_sha1(void);
|
||||
#endif
|
||||
#ifdef ENABLE_SHA2
|
||||
const DIGEST *DIGEST_sha224(void);
|
||||
const DIGEST *DIGEST_sha256(void);
|
||||
const DIGEST *DIGEST_sha384(void);
|
||||
const DIGEST *DIGEST_sha512(void);
|
||||
const DIGEST *DIGEST_sha512_224(void);
|
||||
const DIGEST *DIGEST_sha512_256(void);
|
||||
#endif
|
||||
|
||||
const DIGEST *digest_from_name(const char *name);
|
||||
const char *digest_name(const DIGEST *digest);
|
||||
|
||||
1
thirdparty/GmSSL-3.1.1/include/gmssl/ec.h
vendored
1
thirdparty/GmSSL-3.1.1/include/gmssl/ec.h
vendored
@@ -11,7 +11,6 @@
|
||||
#ifndef GMSSL_EC_H
|
||||
#define GMSSL_EC_H
|
||||
|
||||
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
(uint64_t)(p)[7])
|
||||
|
||||
|
||||
// 注意:PUTU32(buf, val++) 会出错!
|
||||
// WARNING: must not write PUTU32(buf, val++)
|
||||
#define PUTU16(p,V) \
|
||||
((p)[0] = (uint8_t)((V) >> 8), \
|
||||
(p)[1] = (uint8_t)(V))
|
||||
|
||||
8
thirdparty/GmSSL-3.1.1/include/gmssl/error.h
vendored
8
thirdparty/GmSSL-3.1.1/include/gmssl/error.h
vendored
@@ -33,16 +33,16 @@ extern "C" {
|
||||
#define DEBUG 1
|
||||
|
||||
#define warning_print() \
|
||||
do { if (DEBUG) fprintf(stderr, "%s:%d:%s():\n",__FILE__, __LINE__, __func__); } while (0)
|
||||
do { if (DEBUG) fprintf(stderr, "%s:%d:%s():\n",__FILE__, __LINE__, __FUNCTION__); } while (0)
|
||||
|
||||
#define error_print() \
|
||||
do { if (DEBUG) fprintf(stderr, "%s:%d:%s():\n",__FILE__, __LINE__, __func__); } while (0)
|
||||
do { if (DEBUG) fprintf(stderr, "%s:%d:%s():\n",__FILE__, __LINE__, __FUNCTION__); } while (0)
|
||||
|
||||
#define error_print_msg(fmt, ...) \
|
||||
do { if (DEBUG) fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, __VA_ARGS__); } while (0)
|
||||
do { if (DEBUG) fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__); } while (0)
|
||||
|
||||
#define error_puts(str) \
|
||||
do { if (DEBUG) fprintf(stderr, "%s: %d: %s: %s", __FILE__, __LINE__, __func__, str); } while (0)
|
||||
do { if (DEBUG) fprintf(stderr, "%s: %d: %s: %s\n", __FILE__, __LINE__, __FUNCTION__, str); } while (0)
|
||||
|
||||
|
||||
void print_der(const uint8_t *in, size_t inlen);
|
||||
|
||||
2
thirdparty/GmSSL-3.1.1/include/gmssl/file.h
vendored
2
thirdparty/GmSSL-3.1.1/include/gmssl/file.h
vendored
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
|
||||
73
thirdparty/GmSSL-3.1.1/include/gmssl/gcm.h
vendored
73
thirdparty/GmSSL-3.1.1/include/gmssl/gcm.h
vendored
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2022 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_GCM_H
|
||||
#define GMSSL_GCM_H
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/gf128.h>
|
||||
#include <gmssl/block_cipher.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define GCM_IV_MIN_SIZE 1
|
||||
#define GCM_IV_MAX_SIZE ((uint64_t)(1 << (64-3)))
|
||||
#define GCM_IV_DEFAULT_BITS 96
|
||||
#define GCM_IV_DEFAULT_SIZE 12
|
||||
|
||||
#define GCM_MIN_AAD_SIZE 0
|
||||
#define GCM_MAX_AAD_SIZE ((uint64_t)(1 << (64-3)))
|
||||
|
||||
#define GCM_MIN_PLAINTEXT_SIZE 0
|
||||
#define GCM_MAX_PLAINTEXT_SIZE ((((uint64_t)1 << 39) - 256) >> 3)
|
||||
|
||||
|
||||
#define GHASH_SIZE (16)
|
||||
|
||||
|
||||
#define GCM_IS_LITTLE_ENDIAN 1
|
||||
|
||||
|
||||
void ghash(const uint8_t h[16], const uint8_t *aad, size_t aadlen,
|
||||
const uint8_t *c, size_t clen, uint8_t out[16]);
|
||||
|
||||
typedef struct {
|
||||
gf128_t H;
|
||||
gf128_t X;
|
||||
size_t aadlen;
|
||||
size_t clen;
|
||||
uint8_t block[16];
|
||||
size_t num;
|
||||
} GHASH_CTX;
|
||||
|
||||
void ghash_init(GHASH_CTX *ctx, const uint8_t h[16], const uint8_t *aad, size_t aadlen);
|
||||
void ghash_update(GHASH_CTX *ctx, const uint8_t *c, size_t clen);
|
||||
void ghash_finish(GHASH_CTX *ctx, uint8_t out[16]);
|
||||
|
||||
|
||||
int gcm_encrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
|
||||
uint8_t *out, size_t taglen, uint8_t *tag);
|
||||
|
||||
int gcm_decrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
|
||||
const uint8_t *tag, size_t taglen, uint8_t *out);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
35
thirdparty/GmSSL-3.1.1/include/gmssl/gf128.h
vendored
35
thirdparty/GmSSL-3.1.1/include/gmssl/gf128.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2024 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.
|
||||
@@ -7,11 +7,7 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
/* GF(2^128) defined by f(x) = x^128 + x^7 + x^2 + x + 1
|
||||
* A + B mod f(x) = a xor b
|
||||
* A * 2 mod f(x)
|
||||
*/
|
||||
// GF(2^128) defined by f(x) = x^128 + x^7 + x^2 + x + 1
|
||||
|
||||
#ifndef GMSSL_GF128_H
|
||||
#define GMSSL_GF128_H
|
||||
@@ -27,24 +23,19 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//typedef unsigned __int128 gf128_t;
|
||||
|
||||
typedef struct {
|
||||
uint64_t hi;
|
||||
uint64_t lo;
|
||||
} gf128_t;
|
||||
typedef uint64_t gf128_t[2];
|
||||
|
||||
|
||||
// Note: send by value is comptabile with uint128_t and sse2
|
||||
gf128_t gf128_from_hex(const char *s);
|
||||
int gf128_equ_hex(gf128_t a, const char *s);
|
||||
gf128_t gf128_zero(void);
|
||||
gf128_t gf128_add(gf128_t a, gf128_t b);
|
||||
gf128_t gf128_mul(gf128_t a, gf128_t b);
|
||||
gf128_t gf128_mul2(gf128_t a);
|
||||
gf128_t gf128_from_bytes(const uint8_t p[16]);
|
||||
void gf128_to_bytes(gf128_t a, uint8_t p[16]);
|
||||
int gf128_print(FILE *fp, int fmt ,int ind, const char *label, gf128_t a);
|
||||
void gf128_set_zero(gf128_t r);
|
||||
void gf128_set_one(gf128_t r);
|
||||
void gf128_add(gf128_t r, const gf128_t a, const gf128_t b);
|
||||
void gf128_mul(gf128_t r, const gf128_t a, const gf128_t b);
|
||||
void gf128_mul_by_2(gf128_t r, const gf128_t a);
|
||||
void gf128_from_bytes(gf128_t r, const uint8_t p[16]);
|
||||
void gf128_to_bytes(const gf128_t a, uint8_t p[16]);
|
||||
int gf128_from_hex(gf128_t r, const char *s);
|
||||
int gf128_equ_hex(const gf128_t a, const char *s);
|
||||
int gf128_print(FILE *fp, int fmt, int ind, const char *label, const gf128_t a);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
49
thirdparty/GmSSL-3.1.1/include/gmssl/ghash.h
vendored
Normal file
49
thirdparty/GmSSL-3.1.1/include/gmssl/ghash.h
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2014-2024 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_GHASH_H
|
||||
#define GMSSL_GHASH_H
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/gf128.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define GHASH_SIZE (16)
|
||||
|
||||
|
||||
// h = ENC_k(0^128)
|
||||
void ghash(const uint8_t h[16], const uint8_t *aad, size_t aadlen,
|
||||
const uint8_t *c, size_t clen, uint8_t out[16]);
|
||||
|
||||
typedef struct {
|
||||
gf128_t H;
|
||||
gf128_t X;
|
||||
size_t aadlen;
|
||||
size_t clen;
|
||||
uint8_t block[16];
|
||||
size_t num;
|
||||
} GHASH_CTX;
|
||||
|
||||
void ghash_init(GHASH_CTX *ctx, const uint8_t h[16], const uint8_t *aad, size_t aadlen);
|
||||
void ghash_update(GHASH_CTX *ctx, const uint8_t *c, size_t clen);
|
||||
void ghash_finish(GHASH_CTX *ctx, uint8_t out[16]);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
79
thirdparty/GmSSL-3.1.1/include/gmssl/hash_drbg.h
vendored
79
thirdparty/GmSSL-3.1.1/include/gmssl/hash_drbg.h
vendored
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2022 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
|
||||
*/
|
||||
|
||||
|
||||
/* NIST SP800-90A Rev.1 "Recommendation for Random Number Generation
|
||||
* Using Deterministic Random Bit Generators", 10.1.1 Hash_DRBG */
|
||||
|
||||
#ifndef GMSSL_HASH_DRBG_H
|
||||
#define GMSSL_HASH_DRBG_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/digest.h>
|
||||
|
||||
|
||||
/* seedlen for hash_drgb, table 2 of nist sp 800-90a rev.1 */
|
||||
#define HASH_DRBG_SM3_SEED_BITS 440 /* 55 bytes */
|
||||
#define HASH_DRBG_SHA1_SEED_BITS 440
|
||||
#define HASH_DRBG_SHA224_SEED_BITS 440
|
||||
#define HASH_DRBG_SHA512_224_SEED_BITS 440
|
||||
#define HASH_DRBG_SHA256_SEED_BITS 440
|
||||
#define HASH_DRBG_SHA512_256_SEED_BITS 440
|
||||
#define HASH_DRBG_SHA384_SEED_BITS 888 /* 110 bytes */
|
||||
#define HASH_DRBG_SHA512_SEED_BITS 888
|
||||
#define HASH_DRBG_MAX_SEED_BITS 888
|
||||
|
||||
#define HASH_DRBG_SM3_SEED_SIZE (HASH_DRBG_SM3_SEED_BITS/8)
|
||||
#define HASH_DRBG_SHA1_SEED_SIZE (HASH_DRBG_SHA1_SEED_BITS/8)
|
||||
#define HASH_DRBG_SHA224_SEED_SIZE (HASH_DRBG_SHA224_SEED_BITS/8)
|
||||
#define HASH_DRBG_SHA512_224_SEED_SIZE (HASH_DRBG_SHA512_224_SEED_BITS/8)
|
||||
#define HASH_DRBG_SHA256_SEED_SIZE (HASH_DRBG_SHA256_SEED_BITS/8)
|
||||
#define HASH_DRBG_SHA512_256_SEED_SIZE (HASH_DRBG_SHA512_256_SEED_BITS/8)
|
||||
#define HASH_DRBG_SHA384_SEED_SIZE (HASH_DRBG_SHA384_SEED_BITS/8)
|
||||
#define HASH_DRBG_SHA512_SEED_SIZE (HASH_DRBG_SHA512_SEED_BITS/8)
|
||||
#define HASH_DRBG_MAX_SEED_SIZE (HASH_DRBG_MAX_SEED_BITS/8)
|
||||
|
||||
#define HASH_DRBG_RESEED_INTERVAL ((uint64_t)1 << 48)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
const DIGEST *digest;
|
||||
uint8_t V[HASH_DRBG_MAX_SEED_SIZE];
|
||||
uint8_t C[HASH_DRBG_MAX_SEED_SIZE];
|
||||
size_t seedlen;
|
||||
uint64_t reseed_counter;
|
||||
} HASH_DRBG;
|
||||
|
||||
|
||||
int hash_drbg_init(HASH_DRBG *drbg,
|
||||
const DIGEST *digest,
|
||||
const uint8_t *entropy, size_t entropy_len,
|
||||
const uint8_t *nonce, size_t nonce_len,
|
||||
const uint8_t *personalstr, size_t personalstr_len);
|
||||
|
||||
int hash_drbg_reseed(HASH_DRBG *drbg,
|
||||
const uint8_t *entropy, size_t entropy_len,
|
||||
const uint8_t *additional, size_t additional_len);
|
||||
|
||||
int hash_drbg_generate(HASH_DRBG *drbg,
|
||||
const uint8_t *additional, size_t additional_len,
|
||||
size_t outlen, uint8_t *out);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
4
thirdparty/GmSSL-3.1.1/include/gmssl/hkdf.h
vendored
4
thirdparty/GmSSL-3.1.1/include/gmssl/hkdf.h
vendored
@@ -32,9 +32,9 @@ int hkdf_expand(const DIGEST *digest, const uint8_t *prk, size_t prklen,
|
||||
|
||||
int sm3_hkdf_extract(const uint8_t *salt, size_t saltlen,
|
||||
const uint8_t *ikm, size_t ikmlen,
|
||||
uint8_t *prk, size_t *prklen);
|
||||
uint8_t prk[32]);
|
||||
|
||||
int sm3_hkdf_expand(const uint8_t *prk, size_t prklen,
|
||||
int sm3_hkdf_expand(const uint8_t prk[32],
|
||||
const uint8_t *opt_info, size_t opt_infolen,
|
||||
size_t L, uint8_t *okm);
|
||||
|
||||
|
||||
48
thirdparty/GmSSL-3.1.1/include/gmssl/md5.h
vendored
48
thirdparty/GmSSL-3.1.1/include/gmssl/md5.h
vendored
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2022 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_MD5_H
|
||||
#define GMSSL_MD5_H
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define MD5_IS_BIG_ENDIAN 0
|
||||
|
||||
#define MD5_DIGEST_SIZE 16
|
||||
#define MD5_BLOCK_SIZE 64
|
||||
#define MD5_STATE_WORDS (MD5_BLOCK_SIZE/sizeof(uint32_t))
|
||||
|
||||
typedef struct {
|
||||
uint32_t state[MD5_STATE_WORDS];
|
||||
uint64_t nblocks;
|
||||
uint8_t block[MD5_BLOCK_SIZE];
|
||||
size_t num;
|
||||
} MD5_CTX;
|
||||
|
||||
|
||||
void md5_init(MD5_CTX *ctx);
|
||||
void md5_update(MD5_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
void md5_finish(MD5_CTX *ctx, uint8_t dgst[MD5_DIGEST_SIZE]);
|
||||
void md5_digest(const uint8_t *data, size_t datalen, uint8_t dgst[MD5_DIGEST_SIZE]);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
2
thirdparty/GmSSL-3.1.1/include/gmssl/oid.h
vendored
2
thirdparty/GmSSL-3.1.1/include/gmssl/oid.h
vendored
@@ -147,7 +147,7 @@ enum {
|
||||
OID_aes192_cbc,
|
||||
OID_aes256_cbc,
|
||||
|
||||
OID_aes128, // 没有OID
|
||||
OID_aes128, // No OID
|
||||
|
||||
OID_ecdsa_with_sha1,
|
||||
OID_ecdsa_with_sha224,
|
||||
|
||||
12
thirdparty/GmSSL-3.1.1/include/gmssl/pbkdf2.h
vendored
12
thirdparty/GmSSL-3.1.1/include/gmssl/pbkdf2.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2023 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.
|
||||
@@ -22,16 +22,6 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
PBKDF2 Public API
|
||||
|
||||
PBKDF2_MIN_ITER
|
||||
PBKDF2_DEFAULT_SALT_SIZE
|
||||
PBKDF2_MAX_SALT_SIZE
|
||||
|
||||
pbkdf2_hmac_sm3_genkey
|
||||
*/
|
||||
|
||||
|
||||
#define PBKDF2_MIN_ITER 10000
|
||||
#define PBKDF2_MAX_ITER (INT_MAX)
|
||||
|
||||
4
thirdparty/GmSSL-3.1.1/include/gmssl/pkcs8.h
vendored
4
thirdparty/GmSSL-3.1.1/include/gmssl/pkcs8.h
vendored
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
@@ -34,7 +34,7 @@ PBKDF2-params ::= SEQUENCE {
|
||||
otherSource AlgorithmIdentifier {{PBKDF2-SaltSources}}
|
||||
},
|
||||
iterationCount INTEGER (1..MAX),
|
||||
keyLength INTEGER (1..MAX) OPTIONAL, -- 这个参数可以由函数指定
|
||||
keyLength INTEGER (1..MAX) OPTIONAL,
|
||||
prf AlgorithmIdentifier {{PBKDF2-PRFs}} DEFAULT algid-hmacWithSHA1
|
||||
}
|
||||
|
||||
|
||||
3
thirdparty/GmSSL-3.1.1/include/gmssl/rand.h
vendored
3
thirdparty/GmSSL-3.1.1/include/gmssl/rand.h
vendored
@@ -13,7 +13,6 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -22,7 +21,7 @@ extern "C" {
|
||||
|
||||
#define RAND_BYTES_MAX_SIZE (256)
|
||||
|
||||
_gmssl_export int rand_bytes(uint8_t *buf, size_t buflen);
|
||||
int rand_bytes(uint8_t *buf, size_t buflen);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
40
thirdparty/GmSSL-3.1.1/include/gmssl/rc4.h
vendored
40
thirdparty/GmSSL-3.1.1/include/gmssl/rc4.h
vendored
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2022 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_RC4_H
|
||||
#define GMSSL_RC4_H
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define RC4_MIN_KEY_BITS 40
|
||||
#define RC4_STATE_NUM_WORDS 256
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t d[RC4_STATE_NUM_WORDS];
|
||||
} RC4_STATE;
|
||||
|
||||
void rc4_init(RC4_STATE *state, const uint8_t *key, size_t keylen);
|
||||
void rc4_generate_keystream(RC4_STATE *state, size_t outlen, uint8_t *out);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
73
thirdparty/GmSSL-3.1.1/include/gmssl/sdf.h
vendored
73
thirdparty/GmSSL-3.1.1/include/gmssl/sdf.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2024 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.
|
||||
@@ -14,29 +14,13 @@
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm4.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
SDF Public API
|
||||
|
||||
sdf_load_library
|
||||
sdf_unload_library
|
||||
|
||||
SDF_DEVICE
|
||||
sdf_open_device
|
||||
sdf_close_device
|
||||
sdf_print_device_info
|
||||
sdf_rand_bytes
|
||||
sdf_load_sign_key
|
||||
|
||||
SDF_KEY
|
||||
sdf_sign
|
||||
sdf_release_key
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
void *handle;
|
||||
@@ -46,19 +30,60 @@ typedef struct {
|
||||
} SDF_DEVICE;
|
||||
|
||||
typedef struct {
|
||||
SM2_KEY public_key;
|
||||
void *session;
|
||||
} SDF_DIGEST_CTX;
|
||||
|
||||
typedef struct {
|
||||
void *session;
|
||||
void *handle;
|
||||
} SDF_KEY;
|
||||
|
||||
typedef struct {
|
||||
SDF_KEY key;
|
||||
uint8_t iv[SM4_BLOCK_SIZE];
|
||||
uint8_t block[SM4_BLOCK_SIZE];
|
||||
size_t block_nbytes;
|
||||
} SDF_CBC_CTX;
|
||||
|
||||
typedef struct {
|
||||
void *session;
|
||||
int index;
|
||||
} SDF_KEY;
|
||||
} SDF_PRIVATE_KEY;
|
||||
|
||||
typedef struct {
|
||||
SM3_CTX sm3_ctx;
|
||||
SM3_CTX saved_sm3_ctx;
|
||||
SDF_PRIVATE_KEY key;
|
||||
} SDF_SIGN_CTX;
|
||||
|
||||
|
||||
int sdf_load_library(const char *so_path, const char *vendor);
|
||||
int sdf_open_device(SDF_DEVICE *dev);
|
||||
int sdf_print_device_info(FILE *fp, int fmt, int ind, const char *lable, SDF_DEVICE *dev);
|
||||
int sdf_rand_bytes(SDF_DEVICE *dev, uint8_t *buf, size_t len);
|
||||
int sdf_load_sign_key(SDF_DEVICE *dev, SDF_KEY *key, int index, const char *pass);
|
||||
int sdf_sign(SDF_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen);
|
||||
int sdf_release_key(SDF_KEY *key);
|
||||
int sdf_digest_init(SDF_DIGEST_CTX *ctx, SDF_DEVICE *dev);
|
||||
int sdf_digest_update(SDF_DIGEST_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int sdf_digest_finish(SDF_DIGEST_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]);
|
||||
int sdf_digest_reset(SDF_DIGEST_CTX *ctx);
|
||||
int sdf_digest_cleanup(SDF_DIGEST_CTX *ctx);
|
||||
int sdf_generate_key(SDF_DEVICE *dev, SDF_KEY *key, const SM2_KEY *sm2_key, uint8_t *wrappedkey, size_t *wrappedkey_len);
|
||||
int sdf_import_key(SDF_DEVICE *dev, unsigned int key_index, const char *pass, const uint8_t *wrappedkey, size_t wrappedkey_len, SDF_KEY *key); // XXX: Is `pass` needed? see impl in sdf.c
|
||||
int sdf_cbc_encrypt_init(SDF_CBC_CTX *ctx, const SDF_KEY *key, const uint8_t iv[16]);
|
||||
int sdf_cbc_encrypt_update(SDF_CBC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sdf_cbc_encrypt_finish(SDF_CBC_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
int sdf_cbc_decrypt_init(SDF_CBC_CTX *ctx, const SDF_KEY *key, const uint8_t iv[16]);
|
||||
int sdf_cbc_decrypt_update(SDF_CBC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sdf_cbc_decrypt_finish(SDF_CBC_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
int sdf_destroy_key(SDF_KEY *key);
|
||||
int sdf_export_sign_public_key(SDF_DEVICE *dev, int key_index, SM2_KEY *public_key);
|
||||
int sdf_export_encrypt_public_key(SDF_DEVICE *dev, int key_index, SM2_KEY *public_key);
|
||||
int sdf_load_private_key(SDF_DEVICE *dev, SDF_PRIVATE_KEY *key, int key_index, const char *pass);
|
||||
int sdf_decrypt(const SDF_PRIVATE_KEY *key, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sdf_sign(const SDF_PRIVATE_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen);
|
||||
int sdf_sign_init(SDF_SIGN_CTX *ctx, const SDF_PRIVATE_KEY *key, const char *id, size_t idlen);
|
||||
int sdf_sign_update(SDF_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int sdf_sign_finish(SDF_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen);
|
||||
int sdf_sign_reset(SDF_SIGN_CTX *ctx);
|
||||
int sdf_release_private_key(SDF_PRIVATE_KEY *key);
|
||||
int sdf_close_device(SDF_DEVICE *dev);
|
||||
void sdf_unload_library(void);
|
||||
|
||||
|
||||
3
thirdparty/GmSSL-3.1.1/include/gmssl/sha1.h
vendored
3
thirdparty/GmSSL-3.1.1/include/gmssl/sha1.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2024 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.
|
||||
@@ -36,7 +36,6 @@ typedef struct {
|
||||
void sha1_init(SHA1_CTX *ctx);
|
||||
void sha1_update(SHA1_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
void sha1_finish(SHA1_CTX *ctx, uint8_t dgst[SHA1_DIGEST_SIZE]);
|
||||
void sha1_digest(const uint8_t *data, size_t datalen, uint8_t dgst[SHA1_DIGEST_SIZE]);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
10
thirdparty/GmSSL-3.1.1/include/gmssl/sha2.h
vendored
10
thirdparty/GmSSL-3.1.1/include/gmssl/sha2.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2024 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.
|
||||
@@ -38,8 +38,6 @@ typedef struct {
|
||||
void sha224_init(SHA224_CTX *ctx);
|
||||
void sha224_update(SHA224_CTX *ctx, const uint8_t* data, size_t datalen);
|
||||
void sha224_finish(SHA224_CTX *ctx, uint8_t dgst[SHA224_DIGEST_SIZE]);
|
||||
void sha224_digest(const uint8_t *data, size_t datalen,
|
||||
uint8_t dgst[SHA224_DIGEST_SIZE]);
|
||||
|
||||
|
||||
#define SHA256_DIGEST_SIZE 32
|
||||
@@ -56,8 +54,6 @@ typedef struct {
|
||||
void sha256_init(SHA256_CTX *ctx);
|
||||
void sha256_update(SHA256_CTX *ctx, const uint8_t* data, size_t datalen);
|
||||
void sha256_finish(SHA256_CTX *ctx, uint8_t dgst[SHA256_DIGEST_SIZE]);
|
||||
void sha256_digest(const uint8_t *data, size_t datalen,
|
||||
uint8_t dgst[SHA256_DIGEST_SIZE]);
|
||||
|
||||
|
||||
#define SHA384_DIGEST_SIZE 48
|
||||
@@ -74,8 +70,6 @@ typedef struct {
|
||||
void sha384_init(SHA384_CTX *ctx);
|
||||
void sha384_update(SHA384_CTX *ctx, const uint8_t* data, size_t datalen);
|
||||
void sha384_finish(SHA384_CTX *ctx, uint8_t dgst[SHA384_DIGEST_SIZE]);
|
||||
void sha384_digest(const uint8_t *data, size_t datalen,
|
||||
uint8_t dgst[SHA384_DIGEST_SIZE]);
|
||||
|
||||
|
||||
#define SHA512_DIGEST_SIZE 64
|
||||
@@ -92,8 +86,6 @@ typedef struct {
|
||||
void sha512_init(SHA512_CTX *ctx);
|
||||
void sha512_update(SHA512_CTX *ctx, const uint8_t* data, size_t datalen);
|
||||
void sha512_finish(SHA512_CTX *ctx, uint8_t dgst[SHA512_DIGEST_SIZE]);
|
||||
void sha512_digest(const uint8_t *data, size_t datalen,
|
||||
uint8_t dgst[SHA512_DIGEST_SIZE]);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
92
thirdparty/GmSSL-3.1.1/include/gmssl/sha3.h
vendored
92
thirdparty/GmSSL-3.1.1/include/gmssl/sha3.h
vendored
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2022 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_SHA3_H
|
||||
#define GMSSL_SHA3_H
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define SHA3_KECCAK_P_SIZE (1600/8)
|
||||
|
||||
#define SHA3_224_DIGEST_SIZE (224/8)
|
||||
#define SHA3_256_DIGEST_SIZE (256/8)
|
||||
#define SHA3_384_DIGEST_SIZE (384/8)
|
||||
#define SHA3_512_DIGEST_SIZE (512/8)
|
||||
|
||||
#define SHA3_224_CAPACITY (SHA3_224_DIGEST_SIZE * 2)
|
||||
#define SHA3_256_CAPACITY (SHA3_256_DIGEST_SIZE * 2)
|
||||
#define SHA3_384_CAPACITY (SHA3_384_DIGEST_SIZE * 2)
|
||||
#define SHA3_512_CAPACITY (SHA3_512_DIGEST_SIZE * 2)
|
||||
|
||||
#define SHA3_224_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 144
|
||||
#define SHA3_256_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 136
|
||||
#define SHA3_384_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 104
|
||||
#define SHA3_512_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 72
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint64_t A[5][5];
|
||||
uint8_t buf[SHA3_224_BLOCK_SIZE];
|
||||
int num;
|
||||
} SHA3_224_CTX;
|
||||
|
||||
void sha3_224_init(SHA3_224_CTX *ctx);
|
||||
void sha3_224_update(SHA3_224_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
void sha3_224_finish(SHA3_224_CTX *ctx, uint8_t dgst[SHA3_224_DIGEST_SIZE]);
|
||||
|
||||
typedef struct {
|
||||
uint64_t A[5][5];
|
||||
uint8_t buf[SHA3_256_BLOCK_SIZE];
|
||||
int num;
|
||||
} SHA3_256_CTX;
|
||||
|
||||
void sha3_256_init(SHA3_256_CTX *ctx);
|
||||
void sha3_256_update(SHA3_256_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
void sha3_256_finish(SHA3_256_CTX *ctx, uint8_t dgst[SHA3_256_DIGEST_SIZE]);
|
||||
|
||||
typedef struct {
|
||||
uint64_t A[5][5];
|
||||
uint8_t buf[SHA3_384_BLOCK_SIZE];
|
||||
int num;
|
||||
} SHA3_384_CTX;
|
||||
|
||||
void sha3_384_init(SHA3_384_CTX *ctx);
|
||||
void sha3_384_update(SHA3_384_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
void sha3_384_finish(SHA3_384_CTX *ctx, uint8_t dgst[SHA3_384_DIGEST_SIZE]);
|
||||
|
||||
typedef struct {
|
||||
uint64_t A[5][5];
|
||||
uint8_t buf[SHA3_512_BLOCK_SIZE];
|
||||
int num;
|
||||
} SHA3_512_CTX;
|
||||
|
||||
void sha3_512_init(SHA3_512_CTX *ctx);
|
||||
void sha3_512_update(SHA3_512_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
void sha3_512_finish(SHA3_512_CTX *ctx, uint8_t dgst[SHA3_512_DIGEST_SIZE]);
|
||||
|
||||
void sha3_shake128(const uint8_t *in, size_t *inlen, size_t outlen, uint8_t *out);
|
||||
void sha3_shake256(const uint8_t *in, size_t *inlen, size_t outlen, uint8_t *out);
|
||||
void sha3_keccak_p(uint8_t state[SHA3_KECCAK_P_SIZE]);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
38
thirdparty/GmSSL-3.1.1/include/gmssl/skf.h
vendored
38
thirdparty/GmSSL-3.1.1/include/gmssl/skf.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2023 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.
|
||||
@@ -22,42 +22,6 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
SKF Public API
|
||||
|
||||
skf_load_library
|
||||
skf_unload_library
|
||||
skf_list_devices
|
||||
skf_print_device_info
|
||||
|
||||
SKF_DEVICE
|
||||
skf_open_device
|
||||
skf_close_deivce
|
||||
skf_set_label
|
||||
skf_change_authkey
|
||||
skf_list_apps
|
||||
skf_create_app
|
||||
skf_delete_app
|
||||
skf_change_app_admin_pin
|
||||
skf_change_app_user_pin
|
||||
skf_unblock_user_pin
|
||||
skf_list_objects
|
||||
skf_import_object
|
||||
skf_export_object
|
||||
skf_delete_object
|
||||
skf_list_containers
|
||||
skf_create_container
|
||||
skf_delete_container
|
||||
skf_import_sign_cert
|
||||
skf_export_sign_cert
|
||||
skf_rand_bytes
|
||||
skf_load_sign_key
|
||||
|
||||
SKF_KEY
|
||||
skf_sign
|
||||
skf_release_key
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
void *handle;
|
||||
char manufacturer[65];
|
||||
|
||||
311
thirdparty/GmSSL-3.1.1/include/gmssl/sm2.h
vendored
311
thirdparty/GmSSL-3.1.1/include/gmssl/sm2.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
|
||||
/*
|
||||
* Copyright 2014-2024 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.
|
||||
@@ -16,205 +16,34 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/api.h>
|
||||
#include <gmssl/sm2_z256.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
SM2 Public API
|
||||
|
||||
SM2_DEFAULT_ID
|
||||
SM2_MAX_ID_LENGTH
|
||||
SM2_MAX_SIGNATURE_SIZE
|
||||
SM2_MAX_PLAINTEXT_SIZE
|
||||
SM2_MAX_CIPHERTEXT_SIZE
|
||||
|
||||
SM2_KEY
|
||||
sm2_key_generate
|
||||
sm2_private_key_info_encrypt_to_der
|
||||
sm2_private_key_info_decrypt_from_der
|
||||
sm2_private_key_info_encrypt_to_pem
|
||||
sm2_private_key_info_decrypt_from_pem
|
||||
sm2_public_key_info_to_der
|
||||
sm2_public_key_info_from_der
|
||||
sm2_public_key_info_to_pem
|
||||
sm2_public_key_info_from_pem
|
||||
|
||||
sm2_sign
|
||||
sm2_verify
|
||||
sm2_encrypt
|
||||
sm2_decrypt
|
||||
sm2_ecdh
|
||||
|
||||
SM2_SIGN_CTX
|
||||
sm2_sign_init
|
||||
sm2_sign_update
|
||||
sm2_sign_finish
|
||||
sm2_verify_init
|
||||
sm2_verify_update
|
||||
sm2_verify_finish
|
||||
*/
|
||||
|
||||
typedef uint64_t SM2_BN[8];
|
||||
|
||||
int sm2_bn_is_zero(const SM2_BN a);
|
||||
int sm2_bn_is_one(const SM2_BN a);
|
||||
int sm2_bn_is_odd(const SM2_BN a);
|
||||
int sm2_bn_cmp(const SM2_BN a, const SM2_BN b);
|
||||
int sm2_bn_from_hex(SM2_BN r, const char hex[64]);
|
||||
int sm2_bn_from_asn1_integer(SM2_BN r, const uint8_t *d, size_t dlen);
|
||||
int sm2_bn_equ_hex(const SM2_BN a, const char *hex);
|
||||
int sm2_bn_print(FILE *fp, int fmt, int ind, const char *label, const SM2_BN a);
|
||||
int sm2_bn_rshift(SM2_BN ret, const SM2_BN a, unsigned int nbits);
|
||||
|
||||
void sm2_bn_to_bytes(const SM2_BN a, uint8_t out[32]);
|
||||
void sm2_bn_from_bytes(SM2_BN r, const uint8_t in[32]);
|
||||
void sm2_bn_to_hex(const SM2_BN a, char hex[64]);
|
||||
void sm2_bn_to_bits(const SM2_BN a, char bits[256]);
|
||||
void sm2_bn_set_word(SM2_BN r, uint32_t a);
|
||||
void sm2_bn_add(SM2_BN r, const SM2_BN a, const SM2_BN b);
|
||||
void sm2_bn_sub(SM2_BN ret, const SM2_BN a, const SM2_BN b);
|
||||
int sm2_bn_rand_range(SM2_BN r, const SM2_BN range);
|
||||
|
||||
#define sm2_bn_init(r) memset((r),0,sizeof(SM2_BN))
|
||||
#define sm2_bn_set_zero(r) memset((r),0,sizeof(SM2_BN))
|
||||
#define sm2_bn_set_one(r) sm2_bn_set_word((r),1)
|
||||
#define sm2_bn_copy(r,a) memcpy((r),(a),sizeof(SM2_BN))
|
||||
#define sm2_bn_clean(r) memset((r),0,sizeof(SM2_BN))
|
||||
|
||||
|
||||
// GF(p)
|
||||
typedef SM2_BN SM2_Fp;
|
||||
|
||||
void sm2_fp_add(SM2_Fp r, const SM2_Fp a, const SM2_Fp b);
|
||||
void sm2_fp_sub(SM2_Fp r, const SM2_Fp a, const SM2_Fp b);
|
||||
void sm2_fp_mul(SM2_Fp r, const SM2_Fp a, const SM2_Fp b);
|
||||
void sm2_fp_exp(SM2_Fp r, const SM2_Fp a, const SM2_Fp e);
|
||||
void sm2_fp_dbl(SM2_Fp r, const SM2_Fp a);
|
||||
void sm2_fp_tri(SM2_Fp r, const SM2_Fp a);
|
||||
void sm2_fp_div2(SM2_Fp r, const SM2_Fp a);
|
||||
void sm2_fp_neg(SM2_Fp r, const SM2_Fp a);
|
||||
void sm2_fp_sqr(SM2_Fp r, const SM2_Fp a);
|
||||
void sm2_fp_inv(SM2_Fp r, const SM2_Fp a);
|
||||
int sm2_fp_rand(SM2_Fp r);
|
||||
|
||||
int sm2_fp_sqrt(SM2_Fp r, const SM2_Fp a);
|
||||
|
||||
#define sm2_fp_init(r) sm2_bn_init(r)
|
||||
#define sm2_fp_set_zero(r) sm2_bn_set_zero(r)
|
||||
#define sm2_fp_set_one(r) sm2_bn_set_one(r)
|
||||
#define sm2_fp_copy(r,a) sm2_bn_copy(r,a)
|
||||
#define sm2_fp_clean(r) sm2_bn_clean(r)
|
||||
|
||||
// GF(n)
|
||||
typedef SM2_BN SM2_Fn;
|
||||
|
||||
void sm2_fn_add(SM2_Fn r, const SM2_Fn a, const SM2_Fn b);
|
||||
void sm2_fn_sub(SM2_Fn r, const SM2_Fn a, const SM2_Fn b);
|
||||
void sm2_fn_mul(SM2_Fn r, const SM2_Fn a, const SM2_Fn b);
|
||||
void sm2_fn_mul_word(SM2_Fn r, const SM2_Fn a, uint32_t b);
|
||||
void sm2_fn_exp(SM2_Fn r, const SM2_Fn a, const SM2_Fn e);
|
||||
void sm2_fn_neg(SM2_Fn r, const SM2_Fn a);
|
||||
void sm2_fn_sqr(SM2_Fn r, const SM2_Fn a);
|
||||
void sm2_fn_inv(SM2_Fn r, const SM2_Fn a);
|
||||
int sm2_fn_rand(SM2_Fn r);
|
||||
|
||||
#define sm2_fn_init(r) sm2_bn_init(r)
|
||||
#define sm2_fn_set_zero(r) sm2_bn_set_zero(r)
|
||||
#define sm2_fn_set_one(r) sm2_bn_set_one(r)
|
||||
#define sm2_fn_copy(r,a) sm2_bn_copy(r,a)
|
||||
#define sm2_fn_clean(r) sm2_bn_clean(r)
|
||||
|
||||
|
||||
typedef struct {
|
||||
SM2_BN X;
|
||||
SM2_BN Y;
|
||||
SM2_BN Z;
|
||||
} SM2_JACOBIAN_POINT;
|
||||
|
||||
void sm2_jacobian_point_init(SM2_JACOBIAN_POINT *R);
|
||||
void sm2_jacobian_point_set_xy(SM2_JACOBIAN_POINT *R, const SM2_BN x, const SM2_BN y);
|
||||
void sm2_jacobian_point_get_xy(const SM2_JACOBIAN_POINT *P, SM2_BN x, SM2_BN y);
|
||||
void sm2_jacobian_point_neg(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P);
|
||||
void sm2_jacobian_point_dbl(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P);
|
||||
void sm2_jacobian_point_add(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P, const SM2_JACOBIAN_POINT *Q);
|
||||
void sm2_jacobian_point_sub(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P, const SM2_JACOBIAN_POINT *Q);
|
||||
void sm2_jacobian_point_mul(SM2_JACOBIAN_POINT *R, const SM2_BN k, const SM2_JACOBIAN_POINT *P);
|
||||
void sm2_jacobian_point_to_bytes(const SM2_JACOBIAN_POINT *P, uint8_t out[64]);
|
||||
void sm2_jacobian_point_from_bytes(SM2_JACOBIAN_POINT *P, const uint8_t in[64]);
|
||||
void sm2_jacobian_point_mul_generator(SM2_JACOBIAN_POINT *R, const SM2_BN k);
|
||||
void sm2_jacobian_point_mul_sum(SM2_JACOBIAN_POINT *R, const SM2_BN t, const SM2_JACOBIAN_POINT *P, const SM2_BN s);
|
||||
void sm2_jacobian_point_from_hex(SM2_JACOBIAN_POINT *P, const char hex[64 * 2]); // for testing only
|
||||
|
||||
int sm2_jacobian_point_is_at_infinity(const SM2_JACOBIAN_POINT *P);
|
||||
int sm2_jacobian_point_is_on_curve(const SM2_JACOBIAN_POINT *P);
|
||||
int sm2_jacobian_point_equ_hex(const SM2_JACOBIAN_POINT *P, const char hex[128]); // for testing only
|
||||
int sm2_jacobian_point_print(FILE *fp, int fmt, int ind, const char *label, const SM2_JACOBIAN_POINT *P);
|
||||
|
||||
#define sm2_jacobian_point_set_infinity(R) sm2_jacobian_point_init(R)
|
||||
#define sm2_jacobian_point_copy(R, P) memcpy((R), (P), sizeof(SM2_JACOBIAN_POINT))
|
||||
|
||||
typedef uint8_t sm2_bn_t[32];
|
||||
|
||||
typedef struct {
|
||||
uint8_t x[32];
|
||||
uint8_t y[32];
|
||||
} SM2_POINT;
|
||||
|
||||
#define sm2_point_init(P) memset((P),0,sizeof(SM2_POINT))
|
||||
#define sm2_point_set_infinity(P) sm2_point_init(P)
|
||||
int sm2_point_from_octets(SM2_POINT *P, const uint8_t *in, size_t inlen);
|
||||
void sm2_point_to_compressed_octets(const SM2_POINT *P, uint8_t out[33]);
|
||||
void sm2_point_to_uncompressed_octets(const SM2_POINT *P, uint8_t out[65]);
|
||||
|
||||
int sm2_point_from_x(SM2_POINT *P, const uint8_t x[32], int y);
|
||||
int sm2_point_from_xy(SM2_POINT *P, const uint8_t x[32], const uint8_t y[32]);
|
||||
int sm2_point_is_on_curve(const SM2_POINT *P);
|
||||
int sm2_point_is_at_infinity(const SM2_POINT *P);
|
||||
int sm2_point_add(SM2_POINT *R, const SM2_POINT *P, const SM2_POINT *Q);
|
||||
int sm2_point_sub(SM2_POINT *R, const SM2_POINT *P, const SM2_POINT *Q);
|
||||
int sm2_point_neg(SM2_POINT *R, const SM2_POINT *P);
|
||||
int sm2_point_dbl(SM2_POINT *R, const SM2_POINT *P);
|
||||
int sm2_point_mul(SM2_POINT *R, const uint8_t k[32], const SM2_POINT *P);
|
||||
int sm2_point_mul_generator(SM2_POINT *R, const uint8_t k[32]);
|
||||
int sm2_point_mul_sum(SM2_POINT *R, const uint8_t k[32], const SM2_POINT *P, const uint8_t s[32]); // R = k * P + s * G
|
||||
|
||||
/*
|
||||
RFC 5480 Elliptic Curve Cryptography Subject Public Key Information
|
||||
ECPoint ::= OCTET STRING
|
||||
*/
|
||||
#define SM2_POINT_MAX_SIZE (2 + 65)
|
||||
int sm2_point_to_der(const SM2_POINT *P, uint8_t **out, size_t *outlen);
|
||||
int sm2_point_from_der(SM2_POINT *P, const uint8_t **in, size_t *inlen);
|
||||
int sm2_point_print(FILE *fp, int fmt, int ind, const char *label, const SM2_POINT *P);
|
||||
int sm2_point_from_hash(SM2_POINT *R, const uint8_t *data, size_t datalen);
|
||||
|
||||
|
||||
typedef struct {
|
||||
SM2_POINT public_key;
|
||||
uint8_t private_key[32];
|
||||
SM2_Z256_POINT public_key;
|
||||
sm2_z256_t private_key;
|
||||
} SM2_KEY;
|
||||
|
||||
|
||||
_gmssl_export int sm2_key_generate(SM2_KEY *key);
|
||||
int sm2_key_set_private_key(SM2_KEY *key, const uint8_t private_key[32]); // key->public_key will be replaced
|
||||
int sm2_key_set_public_key(SM2_KEY *key, const SM2_POINT *public_key); // key->private_key will be cleared // FIXME: support octets as input?
|
||||
int sm2_key_generate(SM2_KEY *key);
|
||||
int sm2_key_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY *key);
|
||||
int sm2_key_set_private_key(SM2_KEY *key, const sm2_z256_t private_key);
|
||||
int sm2_key_set_public_key(SM2_KEY *key, const SM2_Z256_POINT *public_key);
|
||||
|
||||
int sm2_public_key_equ(const SM2_KEY *sm2_key, const SM2_KEY *pub_key);
|
||||
//int sm2_public_key_copy(SM2_KEY *sm2_key, const SM2_KEY *pub_key); // do we need this?
|
||||
int sm2_public_key_digest(const SM2_KEY *key, uint8_t dgst[32]);
|
||||
int sm2_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY *pub_key);
|
||||
|
||||
|
||||
/*
|
||||
from RFC 5915
|
||||
|
||||
ECPrivateKey ::= SEQUENCE {
|
||||
version INTEGER, -- value MUST be (1)
|
||||
privateKey OCTET STRING, -- big endian encoding of integer 这里不是以INTEGER编码的,因此长度固定
|
||||
privateKey OCTET STRING, -- big endian encoding of integer, fixed length
|
||||
parameters [0] EXPLICIT ECParameters OPTIONAL,
|
||||
-- ONLY namedCurve OID is permitted, by RFC 5480
|
||||
-- MUST always include this field, by RFC 5915
|
||||
@@ -247,10 +76,10 @@ SubjectPublicKeyInfo ::= SEQUENCE {
|
||||
algorithm AlgorithmIdentifier,
|
||||
subjectPublicKey BIT STRING -- uncompressed octets of ECPoint }
|
||||
*/
|
||||
_gmssl_export int sm2_public_key_info_to_der(const SM2_KEY *a, uint8_t **out, size_t *outlen);
|
||||
_gmssl_export int sm2_public_key_info_from_der(SM2_KEY *a, const uint8_t **in, size_t *inlen);
|
||||
_gmssl_export int sm2_public_key_info_to_pem(const SM2_KEY *a, FILE *fp);
|
||||
_gmssl_export int sm2_public_key_info_from_pem(SM2_KEY *a, FILE *fp);
|
||||
int sm2_public_key_info_to_der(const SM2_KEY *a, uint8_t **out, size_t *outlen);
|
||||
int sm2_public_key_info_from_der(SM2_KEY *a, const uint8_t **in, size_t *inlen);
|
||||
int sm2_public_key_info_to_pem(const SM2_KEY *a, FILE *fp);
|
||||
int sm2_public_key_info_from_pem(SM2_KEY *a, FILE *fp);
|
||||
|
||||
/*
|
||||
PKCS #8 PrivateKeyInfo from RFC 5208
|
||||
@@ -278,13 +107,14 @@ EncryptedPrivateKeyInfo ::= SEQUENCE {
|
||||
encryptionAlgorithm EncryptionAlgorithmIdentifier, -- id-PBES2
|
||||
encryptedData OCTET STRING }
|
||||
*/
|
||||
_gmssl_export int sm2_private_key_info_encrypt_to_der(const SM2_KEY *key,
|
||||
int sm2_private_key_info_encrypt_to_der(const SM2_KEY *key,
|
||||
const char *pass, uint8_t **out, size_t *outlen);
|
||||
_gmssl_export int sm2_private_key_info_decrypt_from_der(SM2_KEY *key, const uint8_t **attrs, size_t *attrs_len,
|
||||
int sm2_private_key_info_decrypt_from_der(SM2_KEY *key, const uint8_t **attrs, size_t *attrs_len,
|
||||
const char *pass, const uint8_t **in, size_t *inlen);
|
||||
_gmssl_export int sm2_private_key_info_encrypt_to_pem(const SM2_KEY *key, const char *pass, FILE *fp);
|
||||
int sm2_private_key_info_encrypt_to_pem(const SM2_KEY *key, const char *pass, FILE *fp);
|
||||
// FIXME: #define default buffer size
|
||||
_gmssl_export 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);
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
@@ -293,17 +123,31 @@ typedef struct {
|
||||
} SM2_SIGNATURE;
|
||||
|
||||
int sm2_do_sign(const SM2_KEY *key, const uint8_t dgst[32], SM2_SIGNATURE *sig);
|
||||
int sm2_do_sign_fast(const SM2_Fn d, const uint8_t dgst[32], SM2_SIGNATURE *sig);
|
||||
int sm2_do_verify(const SM2_KEY *key, const uint8_t dgst[32], const SM2_SIGNATURE *sig);
|
||||
|
||||
int sm2_fast_sign_compute_key(const SM2_KEY *key, sm2_z256_t fast_private);
|
||||
|
||||
typedef struct {
|
||||
sm2_z256_t k;
|
||||
sm2_z256_t x1_modn;
|
||||
} SM2_SIGN_PRE_COMP;
|
||||
|
||||
#define SM2_SIGN_PRE_COMP_COUNT 32
|
||||
|
||||
int sm2_fast_sign_pre_compute(SM2_SIGN_PRE_COMP pre_comp[32]);
|
||||
int sm2_fast_sign(const sm2_z256_t fast_private, SM2_SIGN_PRE_COMP *pre_comp,
|
||||
const uint8_t dgst[32], SM2_SIGNATURE *sig);
|
||||
int sm2_fast_verify(const SM2_Z256_POINT point_table[16],
|
||||
const uint8_t dgst[32], const SM2_SIGNATURE *sig);
|
||||
|
||||
|
||||
#define SM2_MIN_SIGNATURE_SIZE 8
|
||||
#define SM2_MAX_SIGNATURE_SIZE 72
|
||||
int sm2_signature_to_der(const SM2_SIGNATURE *sig, uint8_t **out, size_t *outlen);
|
||||
int sm2_signature_from_der(SM2_SIGNATURE *sig, const uint8_t **in, size_t *inlen);
|
||||
int sm2_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *sig, size_t siglen);
|
||||
_gmssl_export int sm2_sign(const SM2_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen);
|
||||
_gmssl_export int sm2_verify(const SM2_KEY *key, const uint8_t dgst[32], const uint8_t *sig, size_t siglen);
|
||||
int sm2_sign(const SM2_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen);
|
||||
int sm2_verify(const SM2_KEY *key, const uint8_t dgst[32], const uint8_t *sig, size_t siglen);
|
||||
|
||||
enum {
|
||||
SM2_signature_compact_size = 70,
|
||||
@@ -312,28 +156,48 @@ enum {
|
||||
};
|
||||
int sm2_sign_fixlen(const SM2_KEY *key, const uint8_t dgst[32], size_t siglen, uint8_t *sig);
|
||||
|
||||
|
||||
|
||||
#define SM2_DEFAULT_ID "1234567812345678"
|
||||
#define SM2_DEFAULT_ID_LENGTH (sizeof(SM2_DEFAULT_ID) - 1) // LENGTH for string and SIZE for bytes
|
||||
#define SM2_DEFAULT_ID_BITS (SM2_DEFAULT_ID_LENGTH * 8)
|
||||
#define SM2_MAX_ID_BITS 65535
|
||||
#define SM2_MAX_ID_LENGTH (SM2_MAX_ID_BITS/8)
|
||||
|
||||
int sm2_compute_z(uint8_t z[32], const SM2_POINT *pub, const char *id, size_t idlen);
|
||||
int sm2_compute_z(uint8_t z[32], const SM2_Z256_POINT *pub, const char *id, size_t idlen);
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
SM3_CTX sm3_ctx;
|
||||
SM3_CTX saved_sm3_ctx;
|
||||
SM2_KEY key;
|
||||
sm2_z256_t fast_sign_private;
|
||||
SM2_SIGN_PRE_COMP pre_comp[SM2_SIGN_PRE_COMP_COUNT];
|
||||
unsigned int num_pre_comp;
|
||||
|
||||
// verify public point table, P, 2P, ..., 16P
|
||||
SM2_Z256_POINT public_point_table[16];
|
||||
} SM2_SIGN_CTX;
|
||||
|
||||
_gmssl_export int sm2_sign_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen);
|
||||
_gmssl_export int sm2_sign_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
_gmssl_export int sm2_sign_finish(SM2_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen);
|
||||
int sm2_sign_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen);
|
||||
int sm2_sign_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int sm2_sign_finish(SM2_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen);
|
||||
int sm2_sign_reset(SM2_SIGN_CTX *ctx);
|
||||
int sm2_sign_finish_fixlen(SM2_SIGN_CTX *ctx, size_t siglen, uint8_t *sig);
|
||||
|
||||
_gmssl_export int sm2_verify_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen);
|
||||
_gmssl_export int sm2_verify_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
_gmssl_export int sm2_verify_finish(SM2_SIGN_CTX *ctx, const uint8_t *sig, size_t siglen);
|
||||
typedef struct {
|
||||
SM3_CTX sm3_ctx;
|
||||
SM3_CTX saved_sm3_ctx;
|
||||
SM2_KEY key;
|
||||
SM2_Z256_POINT public_point_table[16];
|
||||
} SM2_VERIFY_CTX;
|
||||
|
||||
int sm2_verify_init(SM2_VERIFY_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen);
|
||||
int sm2_verify_update(SM2_VERIFY_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int sm2_verify_finish(SM2_VERIFY_CTX *ctx, const uint8_t *sig, size_t siglen);
|
||||
int sm2_verify_reset(SM2_VERIFY_CTX *ctx);
|
||||
|
||||
|
||||
/*
|
||||
SM2Cipher ::= SEQUENCE {
|
||||
@@ -345,6 +209,11 @@ SM2Cipher ::= SEQUENCE {
|
||||
#define SM2_MIN_PLAINTEXT_SIZE 1 // re-compute SM2_MIN_CIPHERTEXT_SIZE when modify
|
||||
#define SM2_MAX_PLAINTEXT_SIZE 255 // re-compute SM2_MAX_CIPHERTEXT_SIZE when modify
|
||||
|
||||
typedef struct {
|
||||
uint8_t x[32];
|
||||
uint8_t y[32];
|
||||
} SM2_POINT;
|
||||
|
||||
typedef struct {
|
||||
SM2_POINT point;
|
||||
uint8_t hash[32];
|
||||
@@ -352,6 +221,9 @@ typedef struct {
|
||||
uint8_t ciphertext[SM2_MAX_PLAINTEXT_SIZE];
|
||||
} SM2_CIPHERTEXT;
|
||||
|
||||
|
||||
int sm2_kdf(const uint8_t *in, size_t inlen, size_t outlen, uint8_t *out);
|
||||
|
||||
int sm2_do_encrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, SM2_CIPHERTEXT *out);
|
||||
int sm2_do_decrypt(const SM2_KEY *key, const SM2_CIPHERTEXT *in, uint8_t *out, size_t *outlen);
|
||||
|
||||
@@ -360,8 +232,8 @@ int sm2_do_decrypt(const SM2_KEY *key, const SM2_CIPHERTEXT *in, uint8_t *out, s
|
||||
int sm2_ciphertext_to_der(const SM2_CIPHERTEXT *c, uint8_t **out, size_t *outlen);
|
||||
int sm2_ciphertext_from_der(SM2_CIPHERTEXT *c, const uint8_t **in, size_t *inlen);
|
||||
int sm2_ciphertext_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *a, size_t alen);
|
||||
_gmssl_export int sm2_encrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
_gmssl_export int sm2_decrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm2_encrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm2_decrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
|
||||
enum {
|
||||
SM2_ciphertext_compact_point_size = 68,
|
||||
@@ -372,8 +244,41 @@ int sm2_do_encrypt_fixlen(const SM2_KEY *key, const uint8_t *in, size_t inlen, i
|
||||
int sm2_encrypt_fixlen(const SM2_KEY *key, const uint8_t *in, size_t inlen, int point_size, uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
int sm2_do_ecdh(const SM2_KEY *key, const SM2_POINT *peer_public, SM2_POINT *out);
|
||||
_gmssl_export int sm2_ecdh(const SM2_KEY *key, const uint8_t *peer_public, size_t peer_public_len, SM2_POINT *out);
|
||||
int sm2_do_ecdh(const SM2_KEY *key, const SM2_Z256_POINT *peer_public, SM2_Z256_POINT *out);
|
||||
int sm2_ecdh(const SM2_KEY *key, const uint8_t *peer_public, size_t peer_public_len, uint8_t out[64]);
|
||||
|
||||
|
||||
typedef struct {
|
||||
sm2_z256_t k;
|
||||
SM2_POINT C1;
|
||||
} SM2_ENC_PRE_COMP;
|
||||
|
||||
#define SM2_ENC_PRE_COMP_NUM 8
|
||||
int sm2_encrypt_pre_compute(SM2_ENC_PRE_COMP pre_comp[SM2_ENC_PRE_COMP_NUM]);
|
||||
int sm2_do_encrypt_ex(const SM2_KEY *key, const SM2_ENC_PRE_COMP *pre_comp,
|
||||
const uint8_t *in, size_t inlen, SM2_CIPHERTEXT *out);
|
||||
|
||||
typedef struct {
|
||||
SM2_ENC_PRE_COMP pre_comp[SM2_ENC_PRE_COMP_NUM];
|
||||
size_t pre_comp_num;
|
||||
uint8_t buf[SM2_MAX_PLAINTEXT_SIZE];
|
||||
size_t buf_size;
|
||||
} SM2_ENC_CTX;
|
||||
|
||||
int sm2_encrypt_init(SM2_ENC_CTX *ctx);
|
||||
int sm2_encrypt_update(SM2_ENC_CTX *ctx, const uint8_t *in, size_t inlen);
|
||||
int sm2_encrypt_finish(SM2_ENC_CTX *ctx, const SM2_KEY *public_key, uint8_t *out, size_t *outlen);
|
||||
int sm2_encrypt_reset(SM2_ENC_CTX *ctx);
|
||||
|
||||
typedef struct {
|
||||
uint8_t buf[SM2_MAX_CIPHERTEXT_SIZE];
|
||||
size_t buf_size;
|
||||
} SM2_DEC_CTX;
|
||||
|
||||
int sm2_decrypt_init(SM2_DEC_CTX *ctx);
|
||||
int sm2_decrypt_update(SM2_DEC_CTX *ctx, const uint8_t *in, size_t inlen);
|
||||
int sm2_decrypt_finish(SM2_DEC_CTX *ctx, const SM2_KEY *key, uint8_t *out, size_t *outlen);
|
||||
int sm2_decrypt_reset(SM2_DEC_CTX *ctx);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
|
||||
156
thirdparty/GmSSL-3.1.1/include/gmssl/sm2_z256.h
vendored
Normal file
156
thirdparty/GmSSL-3.1.1/include/gmssl/sm2_z256.h
vendored
Normal file
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright 2014-2024 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_SM2_Z256_H
|
||||
#define GMSSL_SM2_Z256_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// z256 means compact presentation of uint256
|
||||
typedef uint64_t sm2_z256_t[4];
|
||||
typedef uint64_t sm2_z512_t[8];
|
||||
|
||||
|
||||
void sm2_z256_set_one(sm2_z256_t r);
|
||||
void sm2_z256_set_zero(sm2_z256_t r);
|
||||
|
||||
int sm2_z256_rand_range(sm2_z256_t r, const sm2_z256_t range);
|
||||
void sm2_z256_copy(sm2_z256_t r, const sm2_z256_t a);
|
||||
void sm2_z256_copy_conditional(sm2_z256_t dst, const sm2_z256_t src, uint64_t move);
|
||||
void sm2_z256_from_bytes(sm2_z256_t r, const uint8_t in[32]);
|
||||
void sm2_z256_to_bytes(const sm2_z256_t a, uint8_t out[32]);
|
||||
int sm2_z256_cmp(const sm2_z256_t a, const sm2_z256_t b);
|
||||
uint64_t sm2_z256_is_zero(const sm2_z256_t a);
|
||||
uint64_t sm2_z256_equ(const sm2_z256_t a, const sm2_z256_t b);
|
||||
void sm2_z256_rshift(sm2_z256_t r, const sm2_z256_t a, unsigned int nbits);
|
||||
uint64_t sm2_z256_add(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t b);
|
||||
uint64_t sm2_z256_sub(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t b);
|
||||
void sm2_z256_mul(sm2_z512_t r, const sm2_z256_t a, const sm2_z256_t b);
|
||||
int sm2_z256_get_booth(const sm2_z256_t a, unsigned int window_size, int i);
|
||||
void sm2_z256_from_hex(sm2_z256_t r, const char *hex);
|
||||
int sm2_z256_equ_hex(const sm2_z256_t a, const char *hex);
|
||||
int sm2_z256_print(FILE *fp, int ind, int fmt, const char *label, const sm2_z256_t a);
|
||||
|
||||
void sm2_z256_modp_add(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t b);
|
||||
void sm2_z256_modp_dbl(sm2_z256_t r, const sm2_z256_t a);
|
||||
void sm2_z256_modp_tri(sm2_z256_t r, const sm2_z256_t a);
|
||||
void sm2_z256_modp_sub(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t b);
|
||||
void sm2_z256_modp_neg(sm2_z256_t r, const sm2_z256_t a);
|
||||
void sm2_z256_modp_haf(sm2_z256_t r, const sm2_z256_t a);
|
||||
|
||||
void sm2_z256_modp_to_mont(const sm2_z256_t a, sm2_z256_t r);
|
||||
void sm2_z256_modp_from_mont(sm2_z256_t r, const sm2_z256_t a);
|
||||
void sm2_z256_modp_mont_mul(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t b);
|
||||
void sm2_z256_modp_mont_sqr(sm2_z256_t r, const sm2_z256_t a);
|
||||
void sm2_z256_modp_mont_exp(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t e);
|
||||
void sm2_z256_modp_mont_inv(sm2_z256_t r, const sm2_z256_t a);
|
||||
int sm2_z256_modp_mont_sqrt(sm2_z256_t r, const sm2_z256_t a);
|
||||
|
||||
void sm2_z256_modn_add(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t b);
|
||||
void sm2_z256_modn_sub(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t b);
|
||||
void sm2_z256_modn_neg(sm2_z256_t r, const sm2_z256_t a);
|
||||
void sm2_z256_modn_mul(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t b);
|
||||
void sm2_z256_modn_sqr(sm2_z256_t r, const sm2_z256_t a);
|
||||
void sm2_z256_modn_exp(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t e);
|
||||
void sm2_z256_modn_inv(sm2_z256_t r, const sm2_z256_t a);
|
||||
|
||||
void sm2_z256_modn_to_mont(const sm2_z256_t a, sm2_z256_t r);
|
||||
void sm2_z256_modn_from_mont(sm2_z256_t r, const sm2_z256_t a);
|
||||
void sm2_z256_modn_mont_mul(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t b);
|
||||
void sm2_z256_modn_mont_sqr(sm2_z256_t r, const sm2_z256_t a);
|
||||
void sm2_z256_modn_mont_exp(sm2_z256_t r, const sm2_z256_t a, const sm2_z256_t e);
|
||||
void sm2_z256_modn_mont_inv(sm2_z256_t r, const sm2_z256_t a);
|
||||
|
||||
|
||||
typedef struct {
|
||||
sm2_z256_t X;
|
||||
sm2_z256_t Y;
|
||||
sm2_z256_t Z;
|
||||
} SM2_Z256_POINT;
|
||||
|
||||
void sm2_z256_point_set_infinity(SM2_Z256_POINT *P);
|
||||
int sm2_z256_point_is_at_infinity(const SM2_Z256_POINT *P);
|
||||
int sm2_z256_point_to_bytes(const SM2_Z256_POINT *P, uint8_t out[64]);
|
||||
int sm2_z256_point_from_bytes(SM2_Z256_POINT *P, const uint8_t in[64]);
|
||||
int sm2_z256_point_from_hex(SM2_Z256_POINT *P, const char *hex);
|
||||
int sm2_z256_point_equ_hex(const SM2_Z256_POINT *P, const char *hex);
|
||||
int sm2_z256_point_is_on_curve(const SM2_Z256_POINT *P);
|
||||
int sm2_z256_point_equ(const SM2_Z256_POINT *P, const SM2_Z256_POINT *Q); // equivalent jacobian points
|
||||
int sm2_z256_point_get_xy(const SM2_Z256_POINT *P, uint64_t x[4], uint64_t y[4]);
|
||||
|
||||
void sm2_z256_point_dbl(SM2_Z256_POINT *R, const SM2_Z256_POINT *A);
|
||||
void sm2_z256_point_add(SM2_Z256_POINT *r, const SM2_Z256_POINT *a, const SM2_Z256_POINT *b);
|
||||
void sm2_z256_point_neg(SM2_Z256_POINT *R, const SM2_Z256_POINT *P);
|
||||
void sm2_z256_point_sub(SM2_Z256_POINT *R, const SM2_Z256_POINT *A, const SM2_Z256_POINT *B);
|
||||
void sm2_z256_point_get_affine(const SM2_Z256_POINT *P, uint64_t x[4], uint64_t y[4]);
|
||||
int sm2_z256_point_print(FILE *fp, int fmt, int ind, const char *label, const SM2_Z256_POINT *P);
|
||||
|
||||
|
||||
typedef struct {
|
||||
sm2_z256_t x;
|
||||
sm2_z256_t y;
|
||||
} SM2_Z256_AFFINE_POINT;
|
||||
|
||||
void sm2_z256_point_copy_affine(SM2_Z256_POINT *R, const SM2_Z256_AFFINE_POINT *P);
|
||||
void sm2_z256_point_add_affine(SM2_Z256_POINT *r, const SM2_Z256_POINT *a, const SM2_Z256_AFFINE_POINT *b);
|
||||
void sm2_z256_point_sub_affine(SM2_Z256_POINT *R, const SM2_Z256_POINT *A, const SM2_Z256_AFFINE_POINT *B);
|
||||
int sm2_z256_point_affine_print(FILE *fp, int fmt, int ind, const char *label, const SM2_Z256_AFFINE_POINT *P);
|
||||
|
||||
void sm2_z256_point_mul_generator(SM2_Z256_POINT *R, const sm2_z256_t k);
|
||||
void sm2_z256_point_mul_pre_compute(const SM2_Z256_POINT *P, SM2_Z256_POINT T[16]);
|
||||
void sm2_z256_point_mul_ex(SM2_Z256_POINT *R, const sm2_z256_t k, const SM2_Z256_POINT P_table[16]);
|
||||
void sm2_z256_point_mul(SM2_Z256_POINT *R, const sm2_z256_t k, const SM2_Z256_POINT *P);
|
||||
void sm2_z256_point_mul_sum(SM2_Z256_POINT *R, const sm2_z256_t t, const SM2_Z256_POINT *P, const sm2_z256_t s);
|
||||
|
||||
|
||||
const uint64_t *sm2_z256_prime(void);
|
||||
const uint64_t *sm2_z256_order(void);
|
||||
const uint64_t *sm2_z256_order_minus_one(void);
|
||||
const uint64_t *sm2_z256_one(void);
|
||||
|
||||
|
||||
enum {
|
||||
SM2_point_at_infinity = 0x00,
|
||||
SM2_point_compressed_y_even = 0x02,
|
||||
SM2_point_compressed_y_odd = 0x03,
|
||||
SM2_point_uncompressed = 0x04,
|
||||
SM2_point_uncompressed_y_even = 0x06,
|
||||
SM2_point_uncompressed_y_odd = 0x07,
|
||||
};
|
||||
|
||||
int sm2_z256_point_from_x_bytes(SM2_Z256_POINT *P, const uint8_t x_bytes[32], int y_is_odd);
|
||||
int sm2_z256_point_from_hash(SM2_Z256_POINT *R, const uint8_t *data, size_t datalen, int y_is_odd);
|
||||
int sm2_z256_point_from_octets(SM2_Z256_POINT *P, const uint8_t *in, size_t inlen);
|
||||
|
||||
int sm2_z256_point_to_uncompressed_octets(const SM2_Z256_POINT *P, uint8_t out[65]);
|
||||
int sm2_z256_point_to_compressed_octets(const SM2_Z256_POINT *P, uint8_t out[33]);
|
||||
|
||||
/*
|
||||
RFC 5480 Elliptic Curve Cryptography Subject Public Key Information
|
||||
ECPoint ::= OCTET STRING
|
||||
*/
|
||||
#define SM2_POINT_MAX_SIZE (2 + 65)
|
||||
int sm2_z256_point_to_der(const SM2_Z256_POINT *P, uint8_t **out, size_t *outlen);
|
||||
int sm2_z256_point_from_der(SM2_Z256_POINT *P, const uint8_t **in, size_t *inlen);
|
||||
int sm2_z256_point_print(FILE *fp, int fmt, int ind, const char *label, const SM2_Z256_POINT *P);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
58
thirdparty/GmSSL-3.1.1/include/gmssl/sm3.h
vendored
58
thirdparty/GmSSL-3.1.1/include/gmssl/sm3.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2023 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.
|
||||
@@ -18,32 +18,10 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
SM3 Public API
|
||||
|
||||
SM3_DIGEST_SIZE
|
||||
SM3_HMAC_SIZE
|
||||
|
||||
SM3_CTX
|
||||
sm3_init
|
||||
sm3_update
|
||||
sm3_finish
|
||||
|
||||
SM3_HMAC_CTX
|
||||
sm3_hmac_init
|
||||
sm3_hmac_update
|
||||
sm3_hmac_finish
|
||||
|
||||
sm3_digest
|
||||
sm3_hmac
|
||||
*/
|
||||
|
||||
#define SM3_IS_BIG_ENDIAN 1
|
||||
|
||||
#define SM3_DIGEST_SIZE 32
|
||||
#define SM3_BLOCK_SIZE 64
|
||||
#define SM3_STATE_WORDS 8
|
||||
#define SM3_HMAC_SIZE (SM3_DIGEST_SIZE)
|
||||
|
||||
|
||||
typedef struct {
|
||||
@@ -53,12 +31,14 @@ typedef struct {
|
||||
size_t num;
|
||||
} SM3_CTX;
|
||||
|
||||
void sm3_compress_blocks(uint32_t digest[8], const uint8_t *data, size_t blocks);
|
||||
|
||||
void sm3_init(SM3_CTX *ctx);
|
||||
void sm3_update(SM3_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
void sm3_finish(SM3_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]);
|
||||
void sm3_digest(const uint8_t *data, size_t datalen, uint8_t dgst[SM3_DIGEST_SIZE]);
|
||||
|
||||
void sm3_compress_blocks(uint32_t digest[8], const uint8_t *data, size_t blocks);
|
||||
|
||||
#define SM3_HMAC_SIZE (SM3_DIGEST_SIZE)
|
||||
|
||||
typedef struct {
|
||||
SM3_CTX sm3_ctx;
|
||||
@@ -68,9 +48,6 @@ typedef struct {
|
||||
void sm3_hmac_init(SM3_HMAC_CTX *ctx, const uint8_t *key, size_t keylen);
|
||||
void sm3_hmac_update(SM3_HMAC_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
void sm3_hmac_finish(SM3_HMAC_CTX *ctx, uint8_t mac[SM3_HMAC_SIZE]);
|
||||
void sm3_hmac(const uint8_t *key, size_t keylen,
|
||||
const uint8_t *data, size_t datalen,
|
||||
uint8_t mac[SM3_HMAC_SIZE]);
|
||||
|
||||
|
||||
typedef struct {
|
||||
@@ -79,10 +56,33 @@ typedef struct {
|
||||
} SM3_KDF_CTX;
|
||||
|
||||
void sm3_kdf_init(SM3_KDF_CTX *ctx, size_t outlen);
|
||||
void sm3_kdf_update(SM3_KDF_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
void sm3_kdf_update(SM3_KDF_CTX *ctx, const uint8_t *in, size_t inlen);
|
||||
void sm3_kdf_finish(SM3_KDF_CTX *ctx, uint8_t *out);
|
||||
|
||||
|
||||
#define SM3_PBKDF2_MIN_ITER 10000
|
||||
#define SM3_PBKDF2_MAX_ITER (16777216-1)
|
||||
#define SM3_PBKDF2_MAX_SALT_SIZE 64
|
||||
#define SM3_PBKDF2_DEFAULT_SALT_SIZE 8
|
||||
|
||||
int sm3_pbkdf2(const char *pass, size_t passlen,
|
||||
const uint8_t *salt, size_t saltlen, size_t count,
|
||||
size_t outlen, uint8_t *out);
|
||||
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
SM3_CTX sm3_ctx;
|
||||
SM3_HMAC_CTX hmac_ctx;
|
||||
};
|
||||
int state;
|
||||
} SM3_DIGEST_CTX;
|
||||
|
||||
int sm3_digest_init(SM3_DIGEST_CTX *ctx, const uint8_t *key, size_t keylen);
|
||||
int sm3_digest_update(SM3_DIGEST_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int sm3_digest_finish(SM3_DIGEST_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
42
thirdparty/GmSSL-3.1.1/include/gmssl/sm3_rng.h
vendored
42
thirdparty/GmSSL-3.1.1/include/gmssl/sm3_rng.h
vendored
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2022 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_SM3_RNG_H
|
||||
#define GMSSL_SM3_RNG_H
|
||||
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define SM3_RNG_MAX_RESEED_COUNTER (1<<20)
|
||||
#define SM3_RNG_MAX_RESEED_SECONDS 600
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t V[55];
|
||||
uint8_t C[55];
|
||||
uint32_t reseed_counter;
|
||||
time_t last_reseed_time;
|
||||
} SM3_RNG;
|
||||
|
||||
int sm3_rng_init(SM3_RNG *rng, const uint8_t *nonce, size_t nonce_len,
|
||||
const uint8_t *label, size_t label_len);
|
||||
int sm3_rng_reseed(SM3_RNG *rng, const uint8_t *addin, size_t addin_len);
|
||||
int sm3_rng_generate(SM3_RNG *rng, const uint8_t *addin, size_t addin_len,
|
||||
uint8_t *out, size_t outlen);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -7,7 +7,10 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
#ifndef GMSSL_SM3_X8_AVX2_H
|
||||
// TODO: a new header for coarse-grained parallelism SM3, implemented by sm3_avx2/avx512, sm3_sve/sve2, sm3_cl
|
||||
// and used by sm3_xmss or other algors
|
||||
|
||||
#ifndef GMSSL_SM3_X8_AVX2_H // GMSSL_SM3_MULTI_H ?
|
||||
#define GMSSL_SM3_X8_AVX2_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
127
thirdparty/GmSSL-3.1.1/include/gmssl/sm3_xmss.h
vendored
Normal file
127
thirdparty/GmSSL-3.1.1/include/gmssl/sm3_xmss.h
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright 2014-2023 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_SM3_XMSS_H
|
||||
#define GMSSL_SM3_XMSS_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#ifdef ENABLE_SHA2
|
||||
#include <gmssl/sha2.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// Crosscheck with data from xmss-reference (SHA-256), except the XMSS signature.
|
||||
#if defined(ENABLE_SM3_XMSS_CROSSCHECK) && defined(ENABLE_SHA2)
|
||||
# define HASH256_CTX SHA256_CTX
|
||||
# define hash256_init sha256_init
|
||||
# define hash256_update sha256_update
|
||||
# define hash256_finish sha256_finish
|
||||
# define hash256_digest sha256_digest
|
||||
#else
|
||||
# define HASH256_CTX SM3_CTX
|
||||
# define hash256_init sm3_init
|
||||
# define hash256_update sm3_update
|
||||
# define hash256_finish sm3_finish
|
||||
# define hash256_digest sm3_digest
|
||||
#endif
|
||||
|
||||
typedef uint8_t hash256_bytes_t[32];
|
||||
|
||||
// Derive wots+ sk from a secret seed use the spec of xmss-reference.
|
||||
void sm3_wots_derive_sk(const uint8_t secret[32],
|
||||
const uint8_t seed[32], const uint8_t in_adrs[32],
|
||||
hash256_bytes_t sk[67]);
|
||||
void sm3_wots_derive_pk(const hash256_bytes_t sk[67],
|
||||
const HASH256_CTX *prf_seed_ctx, const uint8_t in_adrs[32],
|
||||
hash256_bytes_t pk[67]);
|
||||
void sm3_wots_do_sign(const hash256_bytes_t sk[67],
|
||||
const HASH256_CTX *prf_seed_ctx, const uint8_t in_adrs[32],
|
||||
const uint8_t dgst[32], hash256_bytes_t sig[67]);
|
||||
void sm3_wots_sig_to_pk(const hash256_bytes_t sig[67], const uint8_t dgst[32],
|
||||
const HASH256_CTX *prf_seed_ctx, const uint8_t in_adrs[32],
|
||||
hash256_bytes_t pk[67]);
|
||||
|
||||
void sm3_xmss_derive_root(const uint8_t xmss_secret[32], int height,
|
||||
const uint8_t seed[32],
|
||||
hash256_bytes_t *tree, uint8_t xmss_root[32]);
|
||||
void sm3_xmss_do_sign(const uint8_t xmss_secret[32], int index,
|
||||
const uint8_t seed[32], const uint8_t in_adrs[32], int height,
|
||||
const hash256_bytes_t *tree,
|
||||
const uint8_t dgst[32],
|
||||
hash256_bytes_t wots_sig[67],
|
||||
hash256_bytes_t *auth_path);
|
||||
|
||||
void sm3_xmss_sig_to_root(const hash256_bytes_t wots_sig[67], int index, const hash256_bytes_t *auth_path,
|
||||
const uint8_t seed[32], const uint8_t in_adrs[32], int height,
|
||||
const uint8_t dgst[32],
|
||||
uint8_t xmss_root[32]);
|
||||
|
||||
enum {
|
||||
XMSS_SM3_10 = 0x10000001,
|
||||
XMSS_SM3_16 = 0x10000002,
|
||||
XMSS_SM3_20 = 0x10000003,
|
||||
XMSS_SHA256_10 = 0x00000001,
|
||||
XMSS_SHA256_16 = 0x00000002,
|
||||
XMSS_SHA256_20 = 0x00000003,
|
||||
};
|
||||
|
||||
int sm3_xmss_height_from_oid(uint32_t *height, uint32_t id);
|
||||
|
||||
typedef struct {
|
||||
uint32_t oid;
|
||||
uint8_t seed[32];
|
||||
uint8_t root[32];
|
||||
uint8_t secret[32];
|
||||
uint8_t prf_key[32];
|
||||
uint32_t index;
|
||||
hash256_bytes_t *tree;
|
||||
} SM3_XMSS_KEY;
|
||||
|
||||
int sm3_xmss_key_generate(SM3_XMSS_KEY *key, uint32_t oid);
|
||||
int sm3_xmss_key_print(FILE *fp, int fmt, int ind, const char *label, const SM3_XMSS_KEY *key);
|
||||
int sm3_xmss_key_get_height(const SM3_XMSS_KEY *key, uint32_t *height);
|
||||
int sm3_xmss_key_to_bytes(const SM3_XMSS_KEY *key, uint8_t *out, size_t *outlen);
|
||||
int sm3_xmss_key_from_bytes(SM3_XMSS_KEY *key, const uint8_t *in, size_t inlen);
|
||||
int sm3_xmss_public_key_to_bytes(const SM3_XMSS_KEY *key, uint8_t *out, size_t *outlen);
|
||||
int sm3_xmss_public_key_from_bytes(SM3_XMSS_KEY *key, const uint8_t *in, size_t inlen);
|
||||
void sm3_xmss_key_cleanup(SM3_XMSS_KEY *key);
|
||||
|
||||
typedef struct {
|
||||
uint8_t index[4];
|
||||
uint8_t random[32];
|
||||
hash256_bytes_t wots_sig[67];
|
||||
hash256_bytes_t auth_path[20];
|
||||
} SM3_XMSS_SIGNATURE;
|
||||
|
||||
int sm3_xmss_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *in, size_t inlen);
|
||||
|
||||
typedef struct {
|
||||
uint8_t random[32];
|
||||
HASH256_CTX hash256_ctx;
|
||||
} SM3_XMSS_SIGN_CTX;
|
||||
|
||||
int sm3_xmss_sign_init(SM3_XMSS_SIGN_CTX *ctx, const SM3_XMSS_KEY *key);
|
||||
int sm3_xmss_sign_update(SM3_XMSS_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int sm3_xmss_sign_finish(SM3_XMSS_SIGN_CTX *ctx, const SM3_XMSS_KEY *key, uint8_t *sigbuf, size_t *siglen);
|
||||
int sm3_xmss_verify_init(SM3_XMSS_SIGN_CTX *ctx, const SM3_XMSS_KEY *key, const uint8_t *sigbuf, size_t siglen);
|
||||
int sm3_xmss_verify_update(SM3_XMSS_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
int sm3_xmss_verify_finish(SM3_XMSS_SIGN_CTX *ctx, const SM3_XMSS_KEY *key, const uint8_t *sigbuf, size_t siglen);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
265
thirdparty/GmSSL-3.1.1/include/gmssl/sm4.h
vendored
265
thirdparty/GmSSL-3.1.1/include/gmssl/sm4.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2024 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.
|
||||
@@ -13,35 +13,13 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/ghash.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
SM4 Public API
|
||||
|
||||
SM4_KEY_SIZE
|
||||
SM4_BLOCK_SIZE
|
||||
|
||||
SM4_CBC_CTX
|
||||
sm4_cbc_encrypt_init
|
||||
sm4_cbc_encrypt_update
|
||||
sm4_cbc_encrypt_finish
|
||||
sm4_cbc_decrypt_init
|
||||
sm4_cbc_decrypt_update
|
||||
sm4_cbc_decrypt_finish
|
||||
|
||||
SM4_CTR_CTX
|
||||
sm4_ctr_encrypt_init
|
||||
sm4_ctr_encrypt_update
|
||||
sm4_ctr_encrypt_finish
|
||||
sm4_ctr_decrypt_init
|
||||
sm4_ctr_decrypt_update
|
||||
sm4_ctr_decrypt_finish
|
||||
*/
|
||||
|
||||
#define SM4_KEY_SIZE (16)
|
||||
#define SM4_BLOCK_SIZE (16)
|
||||
#define SM4_NUM_ROUNDS (32)
|
||||
@@ -54,54 +32,21 @@ typedef struct {
|
||||
void sm4_set_encrypt_key(SM4_KEY *key, const uint8_t raw_key[SM4_KEY_SIZE]);
|
||||
void sm4_set_decrypt_key(SM4_KEY *key, const uint8_t raw_key[SM4_KEY_SIZE]);
|
||||
void sm4_encrypt(const SM4_KEY *key, const uint8_t in[SM4_BLOCK_SIZE], uint8_t out[SM4_BLOCK_SIZE]);
|
||||
#define sm4_decrypt(key,in,out) sm4_encrypt(key,in,out)
|
||||
|
||||
|
||||
void sm4_cbc_encrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
|
||||
void sm4_encrypt_blocks(const SM4_KEY *key, const uint8_t *in, size_t nblocks, uint8_t *out);
|
||||
void sm4_cbc_encrypt_blocks(const SM4_KEY *key, uint8_t iv[SM4_BLOCK_SIZE],
|
||||
const uint8_t *in, size_t nblocks, uint8_t *out);
|
||||
void sm4_cbc_decrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
|
||||
void sm4_cbc_decrypt_blocks(const SM4_KEY *key, uint8_t iv[SM4_BLOCK_SIZE],
|
||||
const uint8_t *in, size_t nblocks, uint8_t *out);
|
||||
int sm4_cbc_padding_encrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
|
||||
void sm4_ctr_encrypt_blocks(const SM4_KEY *key, uint8_t ctr[16], const uint8_t *in, size_t nblocks, uint8_t *out);
|
||||
void sm4_ctr32_encrypt_blocks(const SM4_KEY *key, uint8_t ctr[16], const uint8_t *in, size_t nblocks, uint8_t *out);
|
||||
|
||||
int sm4_cbc_padding_encrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_cbc_padding_decrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
|
||||
int sm4_cbc_padding_decrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
void sm4_ctr_encrypt(const SM4_KEY *key, uint8_t ctr[SM4_BLOCK_SIZE],
|
||||
const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
#define sm4_ctr_decrypt(key,ctr,in,inlen,out) sm4_ctr_encrypt(key,ctr,in,inlen,out)
|
||||
|
||||
|
||||
#define SM4_GCM_IV_MIN_SIZE 1
|
||||
#define SM4_GCM_IV_MAX_SIZE (((uint64_t)1 << (64-3)) - 1) // 2305843009213693951
|
||||
|
||||
#define SM4_GCM_IV_DEFAULT_BITS 96
|
||||
#define SM4_GCM_IV_DEFAULT_SIZE 12
|
||||
|
||||
//#define NIST_SP800_GCM_MAX_IV_SIZE (((uint64_t)1 << (64-3)) - 1) // 2305843009213693951
|
||||
|
||||
#define SM4_GCM_MAX_IV_SIZE 64
|
||||
#define SM4_GCM_MIN_IV_SIZE 1
|
||||
#define SM4_GCM_DEFAULT_IV_SIZE 12
|
||||
|
||||
#define SM4_GCM_MIN_AAD_SIZE 0
|
||||
#define SM4_GCM_MAX_AAD_SIZE (((uint64_t)1 << (64-3)) - 1) // 2305843009213693951
|
||||
|
||||
#define SM4_GCM_MIN_PLAINTEXT_SIZE 0
|
||||
#define SM4_GCM_MAX_PLAINTEXT_SIZE ((((uint64_t)1 << 39) - 256) >> 3) // 68719476704
|
||||
|
||||
#define SM4_GCM_MAX_TAG_SIZE 16
|
||||
#define SM4_GCM_MIN_TAG_SIZE 12
|
||||
// For certain applications (voice or video), tag may be 64 or 32 bits
|
||||
// see NIST Special Publication 800-38D, Appendix C for more details
|
||||
|
||||
|
||||
int sm4_gcm_encrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
|
||||
uint8_t *out, size_t taglen, uint8_t *tag);
|
||||
int sm4_gcm_decrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
|
||||
const uint8_t *tag, size_t taglen, uint8_t *out);
|
||||
void sm4_ctr_encrypt(const SM4_KEY *key, uint8_t ctr[16], const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
void sm4_ctr32_encrypt(const SM4_KEY *key, uint8_t ctr[16], const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
|
||||
|
||||
typedef struct {
|
||||
@@ -114,12 +59,16 @@ typedef struct {
|
||||
int sm4_cbc_encrypt_init(SM4_CBC_CTX *ctx, const uint8_t key[SM4_KEY_SIZE], const uint8_t iv[SM4_BLOCK_SIZE]);
|
||||
int sm4_cbc_encrypt_update(SM4_CBC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_cbc_encrypt_finish(SM4_CBC_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
|
||||
int sm4_cbc_decrypt_init(SM4_CBC_CTX *ctx, const uint8_t key[SM4_KEY_SIZE], const uint8_t iv[SM4_BLOCK_SIZE]);
|
||||
int sm4_cbc_decrypt_update(SM4_CBC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_cbc_decrypt_finish(SM4_CBC_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
void sm4_ctr_encrypt(const SM4_KEY *key, uint8_t ctr[SM4_BLOCK_SIZE],
|
||||
const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
void sm4_ctr32_encrypt(const SM4_KEY *key, uint8_t ctr[SM4_BLOCK_SIZE],
|
||||
const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
|
||||
typedef struct {
|
||||
SM4_KEY sm4_key;
|
||||
uint8_t ctr[SM4_BLOCK_SIZE];
|
||||
@@ -130,10 +79,184 @@ typedef struct {
|
||||
int sm4_ctr_encrypt_init(SM4_CTR_CTX *ctx, const uint8_t key[SM4_KEY_SIZE], const uint8_t ctr[SM4_BLOCK_SIZE]);
|
||||
int sm4_ctr_encrypt_update(SM4_CTR_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_ctr_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
int sm4_ctr32_encrypt_init(SM4_CTR_CTX *ctx, const uint8_t key[SM4_KEY_SIZE], const uint8_t ctr[SM4_BLOCK_SIZE]);
|
||||
int sm4_ctr32_encrypt_update(SM4_CTR_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_ctr32_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
|
||||
#define sm4_ctr_decrypt_init(ctx,key,ctr) sm4_ctr_encrypt_init(ctx,key,ctr)
|
||||
#define sm4_ctr_decrypt_update(ctx,in,inlen,out,outlen) sm4_ctr_encrypt_update(ctx,in,inlen,out,outlen)
|
||||
#define sm4_ctr_decrypt_finish(ctx,out,outlen) sm4_ctr_encrypt_finish(ctx,out,outlen)
|
||||
|
||||
#define NIST_SP800_GCM_MAX_IV_SIZE (((uint64_t)1 << (64-3)) - 1) // 2305843009213693951
|
||||
#define SM4_GCM_MAX_IV_SIZE 64
|
||||
#define SM4_GCM_MIN_IV_SIZE 1
|
||||
#define SM4_GCM_DEFAULT_IV_SIZE 12
|
||||
|
||||
#define NIST_SP800_GCM_MAX_AAD_SIZE (((uint64_t)1 << (64-3)) - 1) // 2305843009213693951
|
||||
#define SM4_GCM_MIN_AAD_SIZE 0
|
||||
#define SM4_GCM_MAX_AAD_SIZE (1<<24) // 16MiB
|
||||
|
||||
#define SM4_GCM_MIN_PLAINTEXT_SIZE 0
|
||||
#define SM4_GCM_MAX_PLAINTEXT_NBLOCKS (((uint64_t)1 << 32) - 2)
|
||||
#define SM4_GCM_MAX_PLAINTEXT_SIZE (SM4_GCM_MAX_PLAINTEXT_NBLOCKS * 16) // 68719476704
|
||||
|
||||
#define SM4_GCM_MAX_TAG_SIZE 16
|
||||
#define SM4_GCM_MIN_TAG_SIZE 12
|
||||
#define SM4_GCM_DEFAULT_TAG_SIZE 16
|
||||
// For certain applications (voice or video), tag may be 64 or 32 bits
|
||||
// see NIST Special Publication 800-38D, Appendix C for more details
|
||||
|
||||
int sm4_gcm_encrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
|
||||
uint8_t *out, size_t taglen, uint8_t *tag);
|
||||
int sm4_gcm_decrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
|
||||
const uint8_t *tag, size_t taglen, uint8_t *out);
|
||||
|
||||
|
||||
typedef struct {
|
||||
SM4_CTR_CTX enc_ctx;
|
||||
GHASH_CTX mac_ctx;
|
||||
uint8_t Y[16]; // E(K, Y_0)
|
||||
size_t taglen;
|
||||
uint8_t mac[16];
|
||||
size_t maclen;
|
||||
uint64_t encedlen;
|
||||
} SM4_GCM_CTX;
|
||||
|
||||
int sm4_gcm_encrypt_init(SM4_GCM_CTX *ctx,
|
||||
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, size_t taglen);
|
||||
int sm4_gcm_encrypt_update(SM4_GCM_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_gcm_encrypt_finish(SM4_GCM_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
int sm4_gcm_decrypt_init(SM4_GCM_CTX *ctx,
|
||||
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, size_t taglen);
|
||||
int sm4_gcm_decrypt_update(SM4_GCM_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_gcm_decrypt_finish(SM4_GCM_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
#ifdef ENABLE_SM4_ECB
|
||||
// call `sm4_set_decrypt_key` before decrypt
|
||||
|
||||
typedef struct {
|
||||
SM4_KEY sm4_key;
|
||||
uint8_t block[SM4_BLOCK_SIZE];
|
||||
size_t block_nbytes;
|
||||
} SM4_ECB_CTX;
|
||||
|
||||
int sm4_ecb_encrypt_init(SM4_ECB_CTX *ctx, const uint8_t key[SM4_BLOCK_SIZE]);
|
||||
int sm4_ecb_encrypt_update(SM4_ECB_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_ecb_encrypt_finish(SM4_ECB_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
|
||||
int sm4_ecb_decrypt_init(SM4_ECB_CTX *ctx, const uint8_t key[SM4_BLOCK_SIZE]);
|
||||
int sm4_ecb_decrypt_update(SM4_ECB_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_ecb_decrypt_finish(SM4_ECB_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
#endif // ENABLE_SM4_ECB
|
||||
|
||||
|
||||
#ifdef ENABLE_SM4_OFB
|
||||
// always call `sm4_set_encrypt_key` before encrypt/decrypt
|
||||
// `sm4_ofb_encrypt` will change the param `iv`
|
||||
void sm4_ofb_encrypt(const SM4_KEY *key, uint8_t iv[16], const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
|
||||
typedef struct {
|
||||
SM4_KEY sm4_key;
|
||||
uint8_t iv[SM4_BLOCK_SIZE];
|
||||
uint8_t block[SM4_BLOCK_SIZE];
|
||||
size_t block_nbytes;
|
||||
} SM4_OFB_CTX;
|
||||
|
||||
int sm4_ofb_encrypt_init(SM4_OFB_CTX *ctx,
|
||||
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t iv[SM4_BLOCK_SIZE]);
|
||||
int sm4_ofb_encrypt_update(SM4_OFB_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_ofb_encrypt_finish(SM4_OFB_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
#endif // ENABLE_SM4_OFB
|
||||
|
||||
|
||||
#ifdef ENABLE_SM4_CFB
|
||||
#define SM4_CFB_MIN_SBYTES 1
|
||||
#define SM4_CFB_MAX_SBYTES 16
|
||||
|
||||
// pre-defined values for `sbytes`
|
||||
#define SM4_CFB_8 1
|
||||
#define SM4_CFB_64 8
|
||||
#define SM4_CFB_128 16
|
||||
|
||||
// always call `sm4_set_encrypt_key` before encrypt/decrypt
|
||||
// `sm4_cfb_encrypt/decrypt` will change the param `iv`
|
||||
void sm4_cfb_encrypt(const SM4_KEY *key, size_t sbytes, uint8_t iv[16],
|
||||
const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
void sm4_cfb_decrypt(const SM4_KEY *key, size_t sbytes, uint8_t iv[16],
|
||||
const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
|
||||
typedef struct {
|
||||
SM4_KEY sm4_key;
|
||||
uint8_t iv[SM4_BLOCK_SIZE];
|
||||
uint8_t block[SM4_BLOCK_SIZE];
|
||||
size_t block_nbytes;
|
||||
size_t sbytes;
|
||||
} SM4_CFB_CTX;
|
||||
|
||||
int sm4_cfb_encrypt_init(SM4_CFB_CTX *ctx, size_t sbytes,
|
||||
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t iv[SM4_BLOCK_SIZE]);
|
||||
int sm4_cfb_encrypt_update(SM4_CFB_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_cfb_encrypt_finish(SM4_CFB_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
|
||||
int sm4_cfb_decrypt_init(SM4_CFB_CTX *ctx, size_t sbytes,
|
||||
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t iv[SM4_BLOCK_SIZE]);
|
||||
int sm4_cfb_decrypt_update(SM4_CFB_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_cfb_decrypt_finish(SM4_CFB_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
#endif // ENABLE_SM4_CFB
|
||||
|
||||
|
||||
#ifdef ENABLE_SM4_CCM
|
||||
#define SM4_CCM_MIN_IV_SIZE 7
|
||||
#define SM4_CCM_MAX_IV_SIZE 13
|
||||
#define SM4_CCM_MIN_TAG_SIZE 4
|
||||
#define SM4_CCM_MAX_TAG_SIZE 16
|
||||
#define SM4_CCM_DEFAULT_TAG_SIZE 16
|
||||
|
||||
// make sure inlen < 2^((15 - ivlen) * 8)
|
||||
int sm4_ccm_encrypt(const SM4_KEY *sm4_key, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
|
||||
uint8_t *out, size_t taglen, uint8_t *tag);
|
||||
int sm4_ccm_decrypt(const SM4_KEY *sm4_key, const uint8_t *iv, size_t ivlen,
|
||||
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
|
||||
const uint8_t *tag, size_t taglen, uint8_t *out);
|
||||
#endif // ENABLE_SM4_CCM
|
||||
|
||||
|
||||
#ifdef ENABLE_SM4_XTS
|
||||
// call `sm4_set_encrypt_key` to set both `key1` and `key2`
|
||||
int sm4_xts_encrypt(const SM4_KEY *key1, const SM4_KEY *key2, const uint8_t tweak[16],
|
||||
const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
// call `sm4_set_decrypt_key(key1)` and `sm4_set_encrypt_key(key2)`
|
||||
int sm4_xts_decrypt(const SM4_KEY *key1, const SM4_KEY *key2, const uint8_t tweak[16],
|
||||
const uint8_t *in, size_t inlen, uint8_t *out);
|
||||
|
||||
typedef struct {
|
||||
SM4_KEY key1;
|
||||
SM4_KEY key2;
|
||||
uint8_t tweak[16];
|
||||
size_t data_unit_size;
|
||||
uint8_t *block;
|
||||
size_t block_nbytes;
|
||||
} SM4_XTS_CTX;
|
||||
|
||||
int sm4_xts_encrypt_init(SM4_XTS_CTX *ctx, const uint8_t key[32], const uint8_t iv[16], size_t data_unit_size);
|
||||
int sm4_xts_encrypt_update(SM4_XTS_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_xts_encrypt_finish(SM4_XTS_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
int sm4_xts_decrypt_init(SM4_XTS_CTX *ctx, const uint8_t key[32], const uint8_t iv[16], size_t data_unit_size);
|
||||
int sm4_xts_decrypt_update(SM4_XTS_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_xts_decrypt_finish(SM4_XTS_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
#endif // ENABLE_SM4_XTS
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2023 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.
|
||||
@@ -24,6 +24,8 @@ typedef struct {
|
||||
size_t ivlen;
|
||||
} SM4_CBC_MAC_CTX;
|
||||
|
||||
#define SM4_CBC_MAC_SIZE (SM4_BLOCK_SIZE)
|
||||
|
||||
void sm4_cbc_mac_init(SM4_CBC_MAC_CTX *ctx, const uint8_t key[16]);
|
||||
void sm4_cbc_mac_update(SM4_CBC_MAC_CTX *ctx, const uint8_t *data, size_t datalen);
|
||||
void sm4_cbc_mac_finish(SM4_CBC_MAC_CTX *ctx, uint8_t mac[16]);
|
||||
|
||||
52
thirdparty/GmSSL-3.1.1/include/gmssl/sm4_cbc_sm3_hmac.h
vendored
Normal file
52
thirdparty/GmSSL-3.1.1/include/gmssl/sm4_cbc_sm3_hmac.h
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2014-2024 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_SM4_CBC_SM3_HMAC_H
|
||||
#define GMSSL_SM4_CBC_SM3_HMAC_H
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm4.h>
|
||||
#include <gmssl/sm3.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
SM4_CBC_CTX enc_ctx;
|
||||
SM3_HMAC_CTX mac_ctx;
|
||||
uint8_t mac[SM3_HMAC_SIZE];
|
||||
size_t maclen;
|
||||
} SM4_CBC_SM3_HMAC_CTX;
|
||||
|
||||
#define SM4_CBC_SM3_HMAC_KEY_SIZE 48
|
||||
#define SM4_CBC_SM3_HMAC_IV_SIZE 16
|
||||
|
||||
int sm4_cbc_sm3_hmac_encrypt_init(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t key[48], const uint8_t iv[16],
|
||||
const uint8_t *aad, size_t aadlen);
|
||||
int sm4_cbc_sm3_hmac_encrypt_update(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_cbc_sm3_hmac_encrypt_finish(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
int sm4_cbc_sm3_hmac_decrypt_init(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t key[48], const uint8_t iv[16],
|
||||
const uint8_t *aad, size_t aadlen);
|
||||
int sm4_cbc_sm3_hmac_decrypt_update(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_cbc_sm3_hmac_decrypt_finish(SM4_CBC_SM3_HMAC_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
21
thirdparty/GmSSL-3.1.1/include/gmssl/sm4_cl.h
vendored
21
thirdparty/GmSSL-3.1.1/include/gmssl/sm4_cl.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2024 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.
|
||||
@@ -10,44 +10,39 @@
|
||||
#ifndef GMSSL_SM4_CL_H
|
||||
#define GMSSL_SM4_CL_H
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm4.h>
|
||||
|
||||
|
||||
#ifdef APPLE
|
||||
#ifdef MACOS
|
||||
#include <OpenCL/OpenCL.h>
|
||||
#else
|
||||
#include <CL/cl.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint32_t rk[32];
|
||||
//size_t workgroup_size;
|
||||
cl_context context;
|
||||
cl_command_queue queue;
|
||||
cl_program program;
|
||||
cl_kernel kernel;
|
||||
cl_mem mem_rk;
|
||||
cl_mem mem_io;
|
||||
size_t workgroup_size;
|
||||
} SM4_CL_CTX;
|
||||
|
||||
|
||||
int sm4_cl_set_encrypt_key(SM4_CL_CTX *ctx, const uint8_t key[16]);
|
||||
int sm4_cl_set_decrypt_key(SM4_CL_CTX *ctx, const uint8_t key[16]);
|
||||
int sm4_cl_encrypt(SM4_CL_CTX *ctx, const uint8_t *in, size_t nblocks, uint8_t *out);
|
||||
int sm4_cl_ctr32_encrypt_blocks(SM4_CL_CTX *ctx, uint8_t iv[16], const uint8_t *in, size_t nblocks, uint8_t *out);
|
||||
void sm4_cl_cleanup(SM4_CL_CTX *ctx);
|
||||
|
||||
int test_sm4_cl_encrypt(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
52
thirdparty/GmSSL-3.1.1/include/gmssl/sm4_ctr_sm3_hmac.h
vendored
Normal file
52
thirdparty/GmSSL-3.1.1/include/gmssl/sm4_ctr_sm3_hmac.h
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2014-2024 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_SM4_CTR_SM3_HMAC_H
|
||||
#define GMSSL_SM4_CTR_SM3_HMAC_H
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm4.h>
|
||||
#include <gmssl/sm3.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
SM4_CTR_CTX enc_ctx;
|
||||
SM3_HMAC_CTX mac_ctx;
|
||||
uint8_t mac[SM3_HMAC_SIZE];
|
||||
size_t maclen;
|
||||
} SM4_CTR_SM3_HMAC_CTX;
|
||||
|
||||
#define SM4_CTR_SM3_HMAC_KEY_SIZE 48
|
||||
#define SM4_CTR_SM3_HMAC_IV_SIZE 16
|
||||
|
||||
int sm4_ctr_sm3_hmac_encrypt_init(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t key[48], const uint8_t iv[16],
|
||||
const uint8_t *aad, size_t aadlen);
|
||||
int sm4_ctr_sm3_hmac_encrypt_update(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_ctr_sm3_hmac_encrypt_finish(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
int sm4_ctr_sm3_hmac_decrypt_init(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t key[48], const uint8_t iv[16],
|
||||
const uint8_t *aad, size_t aadlen);
|
||||
int sm4_ctr_sm3_hmac_decrypt_update(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int sm4_ctr_sm3_hmac_decrypt_finish(SM4_CTR_SM3_HMAC_CTX *ctx,
|
||||
uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
43
thirdparty/GmSSL-3.1.1/include/gmssl/sm4_rng.h
vendored
43
thirdparty/GmSSL-3.1.1/include/gmssl/sm4_rng.h
vendored
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2022 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_SM4_RNG_H
|
||||
#define GMSSL_SM4_RNG_H
|
||||
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define SM4_RNG_MAX_RESEED_COUNTER (1<<20)
|
||||
#define SM4_RNG_MAX_RESEED_SECONDS 600
|
||||
|
||||
typedef struct {
|
||||
uint8_t V[16];
|
||||
uint8_t K[16];
|
||||
uint32_t reseed_counter;
|
||||
time_t last_reseed_time;
|
||||
} SM4_RNG;
|
||||
|
||||
|
||||
int sm4_rng_init(SM4_RNG *rng, const uint8_t *nonce, size_t nonce_len,
|
||||
const uint8_t *label, size_t label_len);
|
||||
int sm4_rng_update(SM4_RNG *rng, const uint8_t seed[32]);
|
||||
int sm4_rng_reseed(SM4_RNG *rng, const uint8_t *addin, size_t addin_len);
|
||||
int sm4_rng_generate(SM4_RNG *rng, const uint8_t *addin, size_t addin_len,
|
||||
uint8_t *out, size_t outlen);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
388
thirdparty/GmSSL-3.1.1/include/gmssl/sm9.h
vendored
388
thirdparty/GmSSL-3.1.1/include/gmssl/sm9.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2024 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.
|
||||
@@ -7,6 +7,8 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
#ifndef GMSSL_SM9_H
|
||||
#define GMSSL_SM9_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -14,346 +16,15 @@
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/sm2.h>
|
||||
|
||||
|
||||
#ifndef GMSSL_SM9_H
|
||||
#define GMSSL_SM9_H
|
||||
#include <gmssl/sm9_z256.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
SM9 Public API
|
||||
|
||||
SM9_SIGNATURE_SIZE
|
||||
SM9_MAX_PLAINTEXT_SIZE
|
||||
SM9_MAX_CIPHERTEXT_SIZE
|
||||
|
||||
SM9_SIGN_MASTER_KEY
|
||||
sm9_sign_master_key_generate
|
||||
sm9_sign_master_key_extract_key
|
||||
sm9_sign_master_key_info_encrypt_to_der
|
||||
sm9_sign_master_key_info_decrypt_from_der
|
||||
sm9_sign_master_key_info_encrypt_to_pem
|
||||
sm9_sign_master_key_info_decrypt_from_pem
|
||||
sm9_sign_master_public_key_to_der
|
||||
sm9_sign_master_public_key_from_der
|
||||
sm9_sign_master_public_key_to_pem
|
||||
sm9_sign_master_public_key_from_pem
|
||||
|
||||
SM9_SIGN_KEY
|
||||
sm9_sign_key_info_encrypt_to_der
|
||||
sm9_sign_key_info_decrypt_from_der
|
||||
sm9_sign_key_info_encrypt_to_pem
|
||||
sm9_sign_key_info_decrypt_from_pem
|
||||
|
||||
SM9_SIGN_CTX
|
||||
sm9_sign_init
|
||||
sm9_sign_update
|
||||
sm9_sign_finish
|
||||
sm9_verify_init
|
||||
sm9_verify_update
|
||||
sm9_verify_finish
|
||||
|
||||
SM9_ENC_MASTER_KEY
|
||||
sm9_enc_master_key_generate
|
||||
sm9_enc_master_key_extract_key
|
||||
sm9_enc_master_key_info_encrypt_to_der
|
||||
sm9_enc_master_key_info_decrypt_from_der
|
||||
sm9_enc_master_key_info_encrypt_to_pem
|
||||
sm9_enc_master_key_info_decrypt_from_pem
|
||||
sm9_enc_master_public_key_to_der
|
||||
sm9_enc_master_public_key_from_der
|
||||
sm9_enc_master_public_key_to_pem
|
||||
sm9_enc_master_public_key_from_pem
|
||||
|
||||
SM9_ENC_KEY
|
||||
sm9_enc_key_info_encrypt_to_der
|
||||
sm9_enc_key_info_decrypt_from_der
|
||||
sm9_enc_key_info_encrypt_to_pem
|
||||
sm9_enc_key_info_decrypt_from_pem
|
||||
|
||||
sm9_encrypt
|
||||
sm9_decrypt
|
||||
*/
|
||||
|
||||
#define SM9_HEX_SEP '\n'
|
||||
|
||||
typedef uint64_t sm9_bn_t[8];
|
||||
|
||||
#define sm9_bn_init(r) sm9_bn_set_zero(r)
|
||||
#define sm9_bn_clean(r) sm9_bn_set_zero(r)
|
||||
|
||||
void sm9_bn_set_zero(sm9_bn_t r);
|
||||
void sm9_bn_set_one(sm9_bn_t r);
|
||||
int sm9_bn_is_zero(const sm9_bn_t a);
|
||||
int sm9_bn_is_one(const sm9_bn_t a);
|
||||
void sm9_bn_set_word(sm9_bn_t r, uint32_t a);
|
||||
void sm9_bn_copy(sm9_bn_t r, const sm9_bn_t a);
|
||||
int sm9_bn_rand_range(sm9_bn_t r, const sm9_bn_t range);
|
||||
int sm9_bn_equ(const sm9_bn_t a, const sm9_bn_t b);
|
||||
int sm9_bn_cmp(const sm9_bn_t a, const sm9_bn_t b);
|
||||
void sm9_bn_add(sm9_bn_t r, const sm9_bn_t a, const sm9_bn_t b);
|
||||
void sm9_bn_sub(sm9_bn_t ret, const sm9_bn_t a, const sm9_bn_t b);
|
||||
void sm9_bn_to_bits(const sm9_bn_t a, char bits[256]);
|
||||
void sm9_bn_to_bytes(const sm9_bn_t a, uint8_t out[32]);
|
||||
void sm9_bn_from_bytes(sm9_bn_t r, const uint8_t in[32]);
|
||||
void sm9_bn_to_hex(const sm9_bn_t a, char hex[64]);
|
||||
int sm9_bn_from_hex(sm9_bn_t r, const char hex[64]);
|
||||
int sm9_bn_print(FILE *fp, int fmt, int ind, const char *label, const sm9_bn_t a);
|
||||
void sm9_print_bn(const char *prefix, const sm9_bn_t a); // 标准打印格式
|
||||
|
||||
|
||||
typedef sm9_bn_t sm9_fp_t;
|
||||
|
||||
#define sm9_fp_init(r) sm9_fp_set_zero(r)
|
||||
#define sm9_fp_clean(f) sm9_fp_set_zero(r)
|
||||
#define sm9_fp_set_zero(r) sm9_bn_set_zero(r)
|
||||
#define sm9_fp_set_one(r) sm9_bn_set_one(r)
|
||||
#define sm9_fp_copy(r,a) sm9_bn_copy((r),(a))
|
||||
#define sm9_fp_rand(r) sm9_bn_rand_range((r), SM9_P)
|
||||
#define sm9_fp_is_zero(a) sm9_bn_is_zero(a)
|
||||
#define sm9_fp_is_one(a) sm9_bn_is_one(a)
|
||||
#define sm9_fp_equ(a,b) sm9_bn_equ((a),(b))
|
||||
#define sm9_fp_to_bytes(a,buf) sm9_bn_to_bytes((a),(buf))
|
||||
#define sm9_fp_to_hex(a,s) sm9_bn_to_hex((a),(s))
|
||||
#define sm9_fp_print(fp,fmt,ind,label,a) sm9_bn_print(fp,fmt,ind,label,a)
|
||||
|
||||
void sm9_fp_add(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
|
||||
void sm9_fp_sub(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
|
||||
void sm9_fp_dbl(sm9_fp_t r, const sm9_fp_t a);
|
||||
void sm9_fp_tri(sm9_fp_t r, const sm9_fp_t a);
|
||||
void sm9_fp_neg(sm9_fp_t r, const sm9_fp_t a);
|
||||
void sm9_fp_mul(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
|
||||
void sm9_fp_sqr(sm9_fp_t r, const sm9_fp_t a);
|
||||
void sm9_fp_pow(sm9_fp_t r, const sm9_fp_t a, const sm9_bn_t e);
|
||||
void sm9_fp_inv(sm9_fp_t r, const sm9_fp_t a);
|
||||
void sm9_fp_div2(sm9_fp_t r, const sm9_fp_t a);
|
||||
int sm9_fp_from_bytes(sm9_fp_t r, const uint8_t buf[32]);
|
||||
int sm9_fp_from_hex(sm9_fp_t r, const char hex[64]);
|
||||
|
||||
|
||||
typedef sm9_bn_t sm9_fn_t;
|
||||
|
||||
#define sm9_fn_init(r) sm9_fn_set_zero(r)
|
||||
#define sm9_fn_clean(f) sm9_fn_set_zero(r)
|
||||
#define sm9_fn_set_zero(r) sm9_bn_set_zero(r)
|
||||
#define sm9_fn_set_one(r) sm9_bn_set_one(r)
|
||||
#define sm9_fn_copy(r,a) sm9_bn_copy((r),(a))
|
||||
#define sm9_fn_rand(r) sm9_bn_rand_range((r), SM9_N)
|
||||
#define sm9_fn_is_zero(a) sm9_bn_is_zero(a)
|
||||
#define sm9_fn_is_one(a) sm9_bn_is_one(a)
|
||||
#define sm9_fn_equ(a,b) sm9_bn_equ((a),(b))
|
||||
#define sm9_fn_to_bytes(a,out) sm9_bn_to_bytes((a),(out))
|
||||
#define sm9_fn_to_hex(a,s) sm9_bn_to_hex((a),(s))
|
||||
#define sm9_fn_print(fp,fmt,ind,label,a) sm9_bn_print(fp,fmt,ind,label,a)
|
||||
|
||||
void sm9_fn_add(sm9_fn_t r, const sm9_fn_t a, const sm9_fn_t b);
|
||||
void sm9_fn_sub(sm9_fn_t r, const sm9_fn_t a, const sm9_fn_t b);
|
||||
void sm9_fn_mul(sm9_fn_t r, const sm9_fn_t a, const sm9_fn_t b);
|
||||
void sm9_fn_pow(sm9_fn_t r, const sm9_fn_t a, const sm9_bn_t e);
|
||||
void sm9_fn_inv(sm9_fn_t r, const sm9_fn_t a);
|
||||
void sm9_fn_from_hash(sm9_fn_t h, const uint8_t Ha[40]);
|
||||
int sm9_fn_from_bytes(sm9_fn_t a, const uint8_t in[32]);
|
||||
int sm9_fn_from_hex(sm9_fn_t r, const char hex[64]);
|
||||
|
||||
|
||||
typedef uint64_t sm9_barrett_bn_t[9];
|
||||
|
||||
int sm9_barrett_bn_cmp(const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
|
||||
void sm9_barrett_bn_add(sm9_barrett_bn_t r, const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
|
||||
void sm9_barrett_bn_sub(sm9_barrett_bn_t ret, const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
|
||||
|
||||
|
||||
typedef sm9_fp_t sm9_fp2_t[2];
|
||||
extern const sm9_fp2_t SM9_FP2_ZERO;
|
||||
extern const sm9_fp2_t SM9_FP2_ONE;
|
||||
extern const sm9_fp2_t SM9_FP2_U;
|
||||
|
||||
#define sm9_fp2_init(a) sm9_fp2_set_zero(a)
|
||||
#define sm9_fp2_clean(a) sm9_fp2_set_zero(a)
|
||||
#define sm9_fp2_set_zero(a) sm9_fp2_copy((a), SM9_FP2_ZERO)
|
||||
#define sm9_fp2_set_one(a) sm9_fp2_copy((a), SM9_FP2_ONE)
|
||||
#define sm9_fp2_set_u(a) sm9_fp2_copy((a), SM9_FP2_U)
|
||||
#define sm9_fp2_is_zero(a) sm9_fp2_equ((a), SM9_FP2_ZERO)
|
||||
#define sm9_fp2_is_one(a) sm9_fp2_equ((a), SM9_FP2_ONE)
|
||||
|
||||
void sm9_fp2_set_fp(sm9_fp2_t r, const sm9_fp_t a);
|
||||
void sm9_fp2_set(sm9_fp2_t r, const sm9_fp_t a0, const sm9_fp_t a1);
|
||||
void sm9_fp2_copy(sm9_fp2_t r, const sm9_fp2_t a);
|
||||
int sm9_fp2_rand(sm9_fp2_t r);
|
||||
int sm9_fp2_equ(const sm9_fp2_t a, const sm9_fp2_t b);
|
||||
void sm9_fp2_add(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
|
||||
void sm9_fp2_dbl(sm9_fp2_t r, const sm9_fp2_t a);
|
||||
void sm9_fp2_tri(sm9_fp2_t r, const sm9_fp2_t a);
|
||||
void sm9_fp2_sub(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
|
||||
void sm9_fp2_neg(sm9_fp2_t r, const sm9_fp2_t a);
|
||||
void sm9_fp2_mul(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
|
||||
void sm9_fp2_mul_u(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
|
||||
void sm9_fp2_mul_fp(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp_t k);
|
||||
void sm9_fp2_sqr(sm9_fp2_t r, const sm9_fp2_t a);
|
||||
void sm9_fp2_sqr_u(sm9_fp2_t r, const sm9_fp2_t a);
|
||||
void sm9_fp2_inv(sm9_fp2_t r, const sm9_fp2_t a);
|
||||
void sm9_fp2_div(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
|
||||
void sm9_fp2_div2(sm9_fp2_t r, const sm9_fp2_t a);
|
||||
void sm9_fp2_to_hex(const sm9_fp2_t a, char hex[129]);
|
||||
int sm9_fp2_from_hex(sm9_fp2_t r, const char hex[129]);
|
||||
int sm9_fp2_print(FILE *fp, int fmt, int ind, const char *label, const sm9_fp2_t a);
|
||||
|
||||
|
||||
typedef sm9_fp2_t sm9_fp4_t[2];
|
||||
extern const sm9_fp4_t SM9_FP4_ZERO;
|
||||
extern const sm9_fp4_t SM9_FP4_ONE;
|
||||
extern const sm9_fp4_t SM9_FP4_U;
|
||||
extern const sm9_fp4_t SM9_FP4_V;
|
||||
|
||||
#define sm9_fp4_init(a) sm9_fp4_set_zero(a)
|
||||
#define sm9_fp4_clean(a) sm9_fp4_set_zero(a)
|
||||
#define sm9_fp4_set_zero(a) sm9_fp4_copy((a), SM9_FP4_ZERO)
|
||||
#define sm9_fp4_set_one(a) sm9_fp4_copy((a), SM9_FP4_ONE)
|
||||
#define sm9_fp4_is_zero(a) sm9_fp4_equ((a), SM9_FP4_ZERO)
|
||||
#define sm9_fp4_is_one(a) sm9_fp4_equ((a), SM9_FP4_ONE)
|
||||
|
||||
void sm9_fp4_set_u(sm9_fp4_t r);
|
||||
void sm9_fp4_set_v(sm9_fp4_t r);
|
||||
void sm9_fp4_set_fp(sm9_fp4_t r, const sm9_fp_t a);
|
||||
void sm9_fp4_set_fp2(sm9_fp4_t r, const sm9_fp2_t a);
|
||||
void sm9_fp4_set(sm9_fp4_t r, const sm9_fp2_t a0, const sm9_fp2_t a1);
|
||||
void sm9_fp4_copy(sm9_fp4_t r, const sm9_fp4_t a);
|
||||
int sm9_fp4_rand(sm9_fp4_t r);
|
||||
int sm9_fp4_equ(const sm9_fp4_t a, const sm9_fp4_t b);
|
||||
void sm9_fp4_add(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
|
||||
void sm9_fp4_dbl(sm9_fp4_t r, const sm9_fp4_t a);
|
||||
void sm9_fp4_sub(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
|
||||
void sm9_fp4_neg(sm9_fp4_t r, const sm9_fp4_t a);
|
||||
void sm9_fp4_mul(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
|
||||
void sm9_fp4_mul_fp(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp_t k);
|
||||
void sm9_fp4_mul_fp2(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp2_t b0);
|
||||
void sm9_fp4_mul_v(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
|
||||
void sm9_fp4_sqr(sm9_fp4_t r, const sm9_fp4_t a);
|
||||
void sm9_fp4_sqr_v(sm9_fp4_t r, const sm9_fp4_t a);
|
||||
void sm9_fp4_inv(sm9_fp4_t r, const sm9_fp4_t a);
|
||||
void sm9_fp4_to_bytes(const sm9_fp4_t a, uint8_t buf[128]);
|
||||
int sm9_fp4_from_bytes(sm9_fp4_t r, const uint8_t buf[128]);
|
||||
void sm9_fp4_to_hex(const sm9_fp4_t a, char hex[259]);
|
||||
int sm9_fp4_from_hex(sm9_fp4_t r, const char hex[259]);
|
||||
|
||||
|
||||
typedef sm9_fp4_t sm9_fp12_t[3];
|
||||
|
||||
#define sm9_fp12_init(r) sm9_fp12_set_zero(a)
|
||||
#define sm9_fp12_clean(r) sm9_fp12_set_zero(a)
|
||||
|
||||
void sm9_fp12_set_zero(sm9_fp12_t r);
|
||||
void sm9_fp12_set_one(sm9_fp12_t r);
|
||||
void sm9_fp12_set_u(sm9_fp12_t r);
|
||||
void sm9_fp12_set_v(sm9_fp12_t r);
|
||||
void sm9_fp12_set_w(sm9_fp12_t r);
|
||||
void sm9_fp12_set_w_sqr(sm9_fp12_t r);
|
||||
void sm9_fp12_set_fp(sm9_fp12_t r, const sm9_fp_t a);
|
||||
void sm9_fp12_set_fp2(sm9_fp12_t r, const sm9_fp2_t a);
|
||||
void sm9_fp12_set_fp4(sm9_fp12_t r, const sm9_fp4_t a);
|
||||
void sm9_fp12_set(sm9_fp12_t r, const sm9_fp4_t a0, const sm9_fp4_t a1, const sm9_fp4_t a2);
|
||||
void sm9_fp12_copy(sm9_fp12_t r, const sm9_fp12_t a);
|
||||
int sm9_fp12_rand(sm9_fp12_t r);
|
||||
int sm9_fp12_is_one(const sm9_fp12_t a);
|
||||
int sm9_fp12_is_zero(const sm9_fp12_t a);
|
||||
int sm9_fp12_equ(const sm9_fp12_t a, const sm9_fp12_t b);
|
||||
void sm9_fp12_add(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
|
||||
void sm9_fp12_dbl(sm9_fp12_t r, const sm9_fp12_t a);
|
||||
void sm9_fp12_tri(sm9_fp12_t r, const sm9_fp12_t a);
|
||||
void sm9_fp12_sub(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
|
||||
void sm9_fp12_neg(sm9_fp12_t r, const sm9_fp12_t a);
|
||||
void sm9_fp12_mul(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
|
||||
void sm9_fp12_sqr(sm9_fp12_t r, const sm9_fp12_t a);
|
||||
void sm9_fp12_inv(sm9_fp12_t r, const sm9_fp12_t a);
|
||||
void sm9_fp12_pow(sm9_fp12_t r, const sm9_fp12_t a, const sm9_bn_t k);
|
||||
void sm9_fp12_to_bytes(const sm9_fp12_t a, uint8_t buf[32 * 12]);
|
||||
int sm9_fp12_from_bytes(sm9_fp12_t r, const uint8_t in[32 * 12]);
|
||||
void sm9_fp12_to_hex(const sm9_fp12_t a, char hex[65 * 12]);
|
||||
int sm9_fp12_from_hex(sm9_fp12_t r, const char hex[65 * 12]); // 这个明显是不对的
|
||||
void sm9_fp12_print(const char *prefix, const sm9_fp12_t a);
|
||||
|
||||
|
||||
void sm9_fp2_conjugate(sm9_fp2_t r, const sm9_fp2_t a);
|
||||
void sm9_fp2_frobenius(sm9_fp2_t r, const sm9_fp2_t a);
|
||||
void sm9_fp4_frobenius(sm9_fp4_t r, const sm9_fp4_t a);
|
||||
void sm9_fp4_conjugate(sm9_fp4_t r, const sm9_fp4_t a);
|
||||
void sm9_fp4_frobenius2(sm9_fp4_t r, const sm9_fp4_t a);
|
||||
void sm9_fp4_frobenius3(sm9_fp4_t r, const sm9_fp4_t a);
|
||||
void sm9_fp12_frobenius(sm9_fp12_t r, const sm9_fp12_t x);
|
||||
void sm9_fp12_frobenius2(sm9_fp12_t r, const sm9_fp12_t x);
|
||||
void sm9_fp12_frobenius3(sm9_fp12_t r, const sm9_fp12_t x);
|
||||
void sm9_fp12_frobenius6(sm9_fp12_t r, const sm9_fp12_t x);
|
||||
|
||||
|
||||
typedef struct {
|
||||
sm9_fp_t X;
|
||||
sm9_fp_t Y;
|
||||
sm9_fp_t Z;
|
||||
} SM9_POINT;
|
||||
|
||||
#define sm9_point_init(R) sm9_point_set_infinity(R)
|
||||
#define sm9_point_clean(R) sm9_point_set_infinity(R)
|
||||
|
||||
void sm9_point_set_infinity(SM9_POINT *R);
|
||||
void sm9_point_copy(SM9_POINT *R, const SM9_POINT *P);
|
||||
void sm9_point_get_xy(const SM9_POINT *P, sm9_fp_t x, sm9_fp_t y);
|
||||
int sm9_point_is_at_infinity(const SM9_POINT *P);
|
||||
int sm9_point_equ(const SM9_POINT *P, const SM9_POINT *Q);
|
||||
int sm9_point_is_on_curve(const SM9_POINT *P);
|
||||
void sm9_point_dbl(SM9_POINT *R, const SM9_POINT *P);
|
||||
void sm9_point_add(SM9_POINT *R, const SM9_POINT *P, const SM9_POINT *Q);
|
||||
void sm9_point_neg(SM9_POINT *R, const SM9_POINT *P);
|
||||
void sm9_point_sub(SM9_POINT *R, const SM9_POINT *P, const SM9_POINT *Q);
|
||||
void sm9_point_mul(SM9_POINT *R, const sm9_bn_t k, const SM9_POINT *P);
|
||||
void sm9_point_mul_generator(SM9_POINT *R, const sm9_bn_t k);
|
||||
void sm9_point_from_hex(SM9_POINT *R, const char hex[65 * 2]);
|
||||
int sm9_point_to_uncompressed_octets(const SM9_POINT *P, uint8_t octets[65]);
|
||||
int sm9_point_from_uncompressed_octets(SM9_POINT *P, const uint8_t octets[65]);
|
||||
int sm9_point_print(FILE *fp, int fmt, int ind, const char *label, const SM9_POINT *P);
|
||||
|
||||
|
||||
typedef struct {
|
||||
sm9_fp2_t X;
|
||||
sm9_fp2_t Y;
|
||||
sm9_fp2_t Z;
|
||||
} SM9_TWIST_POINT;
|
||||
|
||||
#define sm9_twist_point_copy(R, P) memcpy((R), (P), sizeof(SM9_TWIST_POINT))
|
||||
|
||||
int sm9_twist_point_to_uncompressed_octets(const SM9_TWIST_POINT *P, uint8_t octets[129]);
|
||||
int sm9_twist_point_from_uncompressed_octets(SM9_TWIST_POINT *P, const uint8_t octets[129]);
|
||||
|
||||
|
||||
void sm9_twist_point_from_hex(SM9_TWIST_POINT *R, const char hex[65 * 4]);
|
||||
int sm9_twist_point_is_at_infinity(const SM9_TWIST_POINT *P);
|
||||
void sm9_twist_point_set_infinity(SM9_TWIST_POINT *R);
|
||||
void sm9_twist_point_get_xy(const SM9_TWIST_POINT *P, sm9_fp2_t x, sm9_fp2_t y);
|
||||
|
||||
int sm9_twist_point_equ(const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q);
|
||||
int sm9_twist_point_is_on_curve(const SM9_TWIST_POINT *P);
|
||||
void sm9_twist_point_neg(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P);
|
||||
void sm9_twist_point_dbl(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P);
|
||||
void sm9_twist_point_add(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q);
|
||||
void sm9_twist_point_sub(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q);
|
||||
void sm9_twist_point_add_full(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P, const SM9_TWIST_POINT *Q);
|
||||
void sm9_twist_point_mul(SM9_TWIST_POINT *R, const sm9_bn_t k, const SM9_TWIST_POINT *P);
|
||||
void sm9_twist_point_mul_generator(SM9_TWIST_POINT *R, const sm9_bn_t k);
|
||||
int sm9_twist_point_print(FILE *fp, int fmt, int ind, const char *label, const SM9_TWIST_POINT *P);
|
||||
|
||||
|
||||
|
||||
void sm9_eval_g_tangent(sm9_fp12_t num, sm9_fp12_t den, const SM9_TWIST_POINT *P, const SM9_POINT *Q);
|
||||
void sm9_eval_g_line(sm9_fp12_t num, sm9_fp12_t den, const SM9_TWIST_POINT *T, const SM9_TWIST_POINT *P, const SM9_POINT *Q);
|
||||
void sm9_twist_point_pi1(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P);
|
||||
void sm9_twist_point_pi2(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P);
|
||||
void sm9_twist_point_neg_pi2(SM9_TWIST_POINT *R, const SM9_TWIST_POINT *P);
|
||||
void sm9_final_exponent_hard_part(sm9_fp12_t r, const sm9_fp12_t f);
|
||||
void sm9_final_exponent(sm9_fp12_t r, const sm9_fp12_t f);
|
||||
void sm9_pairing(sm9_fp12_t r, const SM9_TWIST_POINT *Q, const SM9_POINT *P);
|
||||
|
||||
int sm9_z256_hash1(sm9_z256_t h1, const char *id, size_t idlen, uint8_t hid);
|
||||
|
||||
/* private key extract algorithms */
|
||||
#define SM9_HID_SIGN 0x01
|
||||
@@ -363,9 +34,6 @@ void sm9_pairing(sm9_fp12_t r, const SM9_TWIST_POINT *Q, const SM9_POINT *P);
|
||||
#define SM9_HASH1_PREFIX 0x01
|
||||
#define SM9_HASH2_PREFIX 0x02
|
||||
|
||||
int sm9_hash1(sm9_bn_t h1, const char *id, size_t idlen, uint8_t hid);
|
||||
|
||||
|
||||
const char *sm9_oid_name(int oid);
|
||||
int sm9_oid_from_name(const char *name);
|
||||
int sm9_oid_to_der(int oid, uint8_t **out, size_t *outlen);
|
||||
@@ -397,13 +65,13 @@ SM9SignPrivateKey ::= SEQUENCE {
|
||||
Ppubs BIT STRING -- uncompressed octets of twisted point }
|
||||
*/
|
||||
typedef struct {
|
||||
SM9_TWIST_POINT Ppubs; // Ppubs = ks * P2
|
||||
sm9_fn_t ks;
|
||||
SM9_Z256_TWIST_POINT Ppubs; // Ppubs = ks * P2
|
||||
sm9_z256_t ks;
|
||||
} SM9_SIGN_MASTER_KEY;
|
||||
|
||||
typedef struct {
|
||||
SM9_TWIST_POINT Ppubs;
|
||||
SM9_POINT ds;
|
||||
SM9_Z256_TWIST_POINT Ppubs;
|
||||
SM9_Z256_POINT ds;
|
||||
} SM9_SIGN_KEY;
|
||||
|
||||
int sm9_sign_master_key_generate(SM9_SIGN_MASTER_KEY *master);
|
||||
@@ -443,8 +111,8 @@ SM9Signature ::= SEQUENCE {
|
||||
S BIT STRING -- uncompressed octets of ECPoint }
|
||||
*/
|
||||
typedef struct {
|
||||
sm9_fn_t h;
|
||||
SM9_POINT S;
|
||||
sm9_z256_t h;
|
||||
SM9_Z256_POINT S;
|
||||
} SM9_SIGNATURE;
|
||||
|
||||
int sm9_do_sign(const SM9_SIGN_KEY *key, const SM3_CTX *sm3_ctx, SM9_SIGNATURE *sig);
|
||||
@@ -468,6 +136,7 @@ int sm9_verify_finish(SM9_SIGN_CTX *ctx, const uint8_t *sig, size_t siglen,
|
||||
const SM9_SIGN_MASTER_KEY *mpk, const char *id, size_t idlen);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
SM9EncMasterKey ::= SEQUENCE {
|
||||
de INTEGER,
|
||||
@@ -482,13 +151,13 @@ SM9EncPrivateKey ::= SEQUENCE {
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
SM9_POINT Ppube; // Ppube = ke * P1
|
||||
sm9_fn_t ke;
|
||||
SM9_Z256_POINT Ppube; // Ppube = ke * P1
|
||||
sm9_z256_t ke;
|
||||
} SM9_ENC_MASTER_KEY;
|
||||
|
||||
typedef struct {
|
||||
SM9_POINT Ppube;
|
||||
SM9_TWIST_POINT de;
|
||||
SM9_Z256_POINT Ppube;
|
||||
SM9_Z256_TWIST_POINT de;
|
||||
} SM9_ENC_KEY;
|
||||
|
||||
int sm9_enc_master_key_generate(SM9_ENC_MASTER_KEY *master);
|
||||
@@ -534,18 +203,18 @@ SM9Cipher ::= SEQUENCE {
|
||||
CipherText OCTET STRING }
|
||||
*/
|
||||
|
||||
int sm9_kem_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen, size_t klen, uint8_t *kbuf, SM9_POINT *C);
|
||||
int sm9_kem_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen, const SM9_POINT *C, size_t klen, uint8_t *kbuf);
|
||||
int sm9_kem_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen, size_t klen, uint8_t *kbuf, SM9_Z256_POINT *C);
|
||||
int sm9_kem_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen, const SM9_Z256_POINT *C, size_t klen, uint8_t *kbuf);
|
||||
int sm9_do_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen,
|
||||
const uint8_t *in, size_t inlen, SM9_POINT *C1, uint8_t *c2, uint8_t c3[SM3_HMAC_SIZE]);
|
||||
const uint8_t *in, size_t inlen, SM9_Z256_POINT *C1, uint8_t *c2, uint8_t c3[SM3_HMAC_SIZE]);
|
||||
int sm9_do_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen,
|
||||
const SM9_POINT *C1, const uint8_t *c2, size_t c2len, const uint8_t c3[SM3_HMAC_SIZE], uint8_t *out);
|
||||
const SM9_Z256_POINT *C1, const uint8_t *c2, size_t c2len, const uint8_t c3[SM3_HMAC_SIZE], uint8_t *out);
|
||||
|
||||
#define SM9_MAX_PLAINTEXT_SIZE 255
|
||||
#define SM9_MAX_CIPHERTEXT_SIZE 367 // calculated in test_sm9_ciphertext()
|
||||
int sm9_ciphertext_to_der(const SM9_POINT *C1, const uint8_t *c2, size_t c2len,
|
||||
int sm9_ciphertext_to_der(const SM9_Z256_POINT *C1, const uint8_t *c2, size_t c2len,
|
||||
const uint8_t c3[SM3_HMAC_SIZE], uint8_t **out, size_t *outlen);
|
||||
int sm9_ciphertext_from_der(SM9_POINT *C1, const uint8_t **c2, size_t *c2len,
|
||||
int sm9_ciphertext_from_der(SM9_Z256_POINT *C1, const uint8_t **c2, size_t *c2len,
|
||||
const uint8_t **c3, const uint8_t **in, size_t *inlen);
|
||||
int sm9_ciphertext_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *a, size_t alen);
|
||||
int sm9_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen,
|
||||
@@ -554,6 +223,19 @@ int sm9_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen,
|
||||
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
// SM9 Key Exchange (To be continued)
|
||||
#define SM9_EXCH_MASTER_KEY SM9_ENC_MASTER_KEY
|
||||
#define SM9_EXCH_KEY SM9_ENC_KEY
|
||||
#define sm9_exch_master_key_generate(msk) sm9_enc_master_key_generate(msk)
|
||||
int sm9_exch_master_key_extract_key(SM9_EXCH_MASTER_KEY *master, const char *id, size_t idlen, SM9_EXCH_KEY *key);
|
||||
|
||||
int sm9_exch_step_1A(const SM9_EXCH_MASTER_KEY *mpk, const char *idB, size_t idBlen, SM9_Z256_POINT *RA, sm9_z256_t rA);
|
||||
int sm9_exch_step_1B(const SM9_EXCH_MASTER_KEY *mpk, const char *idA, size_t idAlen, const char *idB, size_t idBlen,
|
||||
const SM9_EXCH_KEY *key, const SM9_Z256_POINT *RA, SM9_Z256_POINT *RB, uint8_t *sk, size_t klen);
|
||||
int sm9_exch_step_2A(const SM9_EXCH_MASTER_KEY *mpk, const char *idA, size_t idAlen, const char *idB, size_t idBlen,
|
||||
const SM9_EXCH_KEY *key, const sm9_z256_t rA, const SM9_Z256_POINT *RA, const SM9_Z256_POINT *RB, uint8_t *sk, size_t klen);
|
||||
int sm9_exch_step_2B();
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
247
thirdparty/GmSSL-3.1.1/include/gmssl/sm9_z256.h
vendored
Normal file
247
thirdparty/GmSSL-3.1.1/include/gmssl/sm9_z256.h
vendored
Normal file
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
* Copyright 2014-2024 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_SM9_Z256_H
|
||||
#define GMSSL_SM9_Z256_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/sm2.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef uint64_t sm9_z256_t[4];
|
||||
|
||||
void sm9_z256_set_one(sm9_z256_t r);
|
||||
void sm9_z256_set_zero(sm9_z256_t r);
|
||||
void sm9_z256_copy(sm9_z256_t r, const sm9_z256_t a);
|
||||
void sm9_z256_copy_conditional(sm9_z256_t r, const sm9_z256_t a, uint64_t move);
|
||||
int sm9_z256_cmp(const sm9_z256_t a, const sm9_z256_t b);
|
||||
uint64_t sm9_z256_is_zero(const sm9_z256_t a);
|
||||
uint64_t sm9_z256_equ(const sm9_z256_t a, const sm9_z256_t b);
|
||||
uint64_t sm9_z256_add(sm9_z256_t r, const sm9_z256_t a, const sm9_z256_t b);
|
||||
uint64_t sm9_z256_sub(sm9_z256_t r, const sm9_z256_t a, const sm9_z256_t b);
|
||||
void sm9_z256_mul(uint64_t r[8], const sm9_z256_t a, const sm9_z256_t b);
|
||||
void sm9_z256_from_bytes(sm9_z256_t r, const uint8_t in[32]);
|
||||
void sm9_z256_to_bytes(const sm9_z256_t a, uint8_t out[32]);
|
||||
int sm9_z256_from_hex(sm9_z256_t r, const char *hex);
|
||||
void sm9_z256_to_hex(const sm9_z256_t r, char hex[64]);
|
||||
int sm9_z256_equ_hex(const sm9_z256_t a, const char *hex);
|
||||
void sm9_z256_to_bits(const sm9_z256_t a, char bits[256]);
|
||||
int sm9_z256_rand_range(sm9_z256_t r, const sm9_z256_t range);
|
||||
void sm9_z256_print_bn(const char *prefix, const sm9_z256_t a);
|
||||
int sm9_z256_print(FILE *fp, int ind, int fmt, const char *label, const sm9_z256_t a);
|
||||
|
||||
const uint64_t *sm9_z256_prime(void);
|
||||
|
||||
void sm9_z256_modp_add(sm9_z256_t r, const sm9_z256_t a, const sm9_z256_t b);
|
||||
void sm9_z256_modp_sub(sm9_z256_t r, const sm9_z256_t a, const sm9_z256_t b);
|
||||
void sm9_z256_modp_dbl(sm9_z256_t r, const sm9_z256_t a);
|
||||
void sm9_z256_modp_tri(sm9_z256_t r, const sm9_z256_t a);
|
||||
void sm9_z256_modp_haf(sm9_z256_t r, const sm9_z256_t a);
|
||||
void sm9_z256_modp_neg(sm9_z256_t r, const sm9_z256_t a);
|
||||
|
||||
void sm9_z256_modp_to_mont(sm9_z256_t r, const sm9_z256_t a);
|
||||
void sm9_z256_modp_from_mont(sm9_z256_t r, const sm9_z256_t a);
|
||||
void sm9_z256_modp_mont_mul(sm9_z256_t r, const sm9_z256_t a, const sm9_z256_t b);
|
||||
void sm9_z256_modp_mont_sqr(sm9_z256_t r, const sm9_z256_t a);
|
||||
void sm9_z256_modp_mont_pow(sm9_z256_t r, const sm9_z256_t a, const sm9_z256_t e);
|
||||
void sm9_z256_modp_mont_inv(sm9_z256_t r, const sm9_z256_t a);
|
||||
|
||||
const uint64_t *sm9_z256_order(void);
|
||||
|
||||
void sm9_z256_modn_add(sm9_z256_t r, const sm9_z256_t a, const sm9_z256_t b);
|
||||
void sm9_z256_modn_sub(sm9_z256_t r, const sm9_z256_t a, const sm9_z256_t b);
|
||||
void sm9_z256_modn_mul(sm9_z256_t r, const sm9_z256_t a, const sm9_z256_t b);
|
||||
void sm9_z256_modn_pow(sm9_z256_t r, const sm9_z256_t a, const sm9_z256_t e);
|
||||
void sm9_z256_modn_inv(sm9_z256_t r, const sm9_z256_t a);
|
||||
void sm9_z256_modn_from_hash(sm9_z256_t h, const uint8_t Ha[40]);
|
||||
|
||||
|
||||
typedef sm9_z256_t sm9_z256_fp2_t[2];
|
||||
|
||||
void sm9_z256_fp2_set_one(sm9_z256_fp2_t r);
|
||||
void sm9_z256_fp2_set_zero(sm9_z256_fp2_t r);
|
||||
int sm9_z256_fp2_is_one(const sm9_z256_fp2_t a);
|
||||
int sm9_z256_fp2_is_zero(const sm9_z256_fp2_t a);
|
||||
int sm9_z256_fp2_equ(const sm9_z256_fp2_t a, const sm9_z256_fp2_t b);
|
||||
void sm9_z256_fp2_copy(sm9_z256_fp2_t r, const sm9_z256_fp2_t a);
|
||||
int sm9_z256_fp2_rand(sm9_z256_fp2_t r);
|
||||
void sm9_z256_fp2_to_bytes(const sm9_z256_fp2_t a, uint8_t buf[64]);
|
||||
int sm9_z256_fp2_from_bytes(sm9_z256_fp2_t r, const uint8_t buf[64]);
|
||||
void sm9_z256_fp2_to_hex(const sm9_z256_fp2_t a, char hex[129]);
|
||||
int sm9_z256_fp2_from_hex(sm9_z256_fp2_t r, const char hex[129]);
|
||||
void sm9_z256_fp2_add(sm9_z256_fp2_t r, const sm9_z256_fp2_t a, const sm9_z256_fp2_t b);
|
||||
void sm9_z256_fp2_dbl(sm9_z256_fp2_t r, const sm9_z256_fp2_t a);
|
||||
void sm9_z256_fp2_tri(sm9_z256_fp2_t r, const sm9_z256_fp2_t a);
|
||||
void sm9_z256_fp2_sub(sm9_z256_fp2_t r, const sm9_z256_fp2_t a, const sm9_z256_fp2_t b);
|
||||
void sm9_z256_fp2_neg(sm9_z256_fp2_t r, const sm9_z256_fp2_t a);
|
||||
void sm9_z256_fp2_a_mul_u(sm9_z256_fp2_t r, sm9_z256_fp2_t a);
|
||||
void sm9_z256_fp2_mul(sm9_z256_fp2_t r, const sm9_z256_fp2_t a, const sm9_z256_fp2_t b);
|
||||
void sm9_z256_fp2_mul_u(sm9_z256_fp2_t r, const sm9_z256_fp2_t a, const sm9_z256_fp2_t b);
|
||||
void sm9_z256_fp2_mul_fp(sm9_z256_fp2_t r, const sm9_z256_fp2_t a, const sm9_z256_t k);
|
||||
void sm9_z256_fp2_sqr(sm9_z256_fp2_t r, const sm9_z256_fp2_t a);
|
||||
void sm9_z256_fp2_sqr_u(sm9_z256_fp2_t r, const sm9_z256_fp2_t a);
|
||||
void sm9_z256_fp2_inv(sm9_z256_fp2_t r, const sm9_z256_fp2_t a);
|
||||
void sm9_z256_fp2_div(sm9_z256_fp2_t r, const sm9_z256_fp2_t a, const sm9_z256_fp2_t b);
|
||||
void sm9_z256_fp2_haf(sm9_z256_fp2_t r, const sm9_z256_fp2_t a);
|
||||
void sm9_z256_fp2_conjugate(sm9_z256_fp2_t r, const sm9_z256_fp2_t a);
|
||||
void sm9_z256_fp2_frobenius(sm9_z256_fp2_t r, const sm9_z256_fp2_t a);
|
||||
|
||||
|
||||
typedef sm9_z256_fp2_t sm9_z256_fp4_t[2];
|
||||
|
||||
int sm9_z256_fp4_is_zero(const sm9_z256_fp4_t a);
|
||||
int sm9_z256_fp4_equ(const sm9_z256_fp4_t a, const sm9_z256_fp4_t b);
|
||||
int sm9_z256_fp4_rand(sm9_z256_fp4_t r);
|
||||
void sm9_z256_fp4_copy(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_to_bytes(const sm9_z256_fp4_t a, uint8_t buf[128]);
|
||||
int sm9_z256_fp4_from_bytes(sm9_z256_fp4_t r, const uint8_t buf[128]);
|
||||
int sm9_z256_fp4_from_hex(sm9_z256_fp4_t r, const char hex[259]);
|
||||
void sm9_z256_fp4_to_hex(const sm9_z256_fp4_t a, char hex[259]);
|
||||
void sm9_z256_fp4_add(sm9_z256_fp4_t r, const sm9_z256_fp4_t a, const sm9_z256_fp4_t b);
|
||||
void sm9_z256_fp4_dbl(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_sub(sm9_z256_fp4_t r, const sm9_z256_fp4_t a, const sm9_z256_fp4_t b);
|
||||
void sm9_z256_fp4_neg(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_haf(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_a_mul_v(sm9_z256_fp4_t r, sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_mul(sm9_z256_fp4_t r, const sm9_z256_fp4_t a, const sm9_z256_fp4_t b);
|
||||
void sm9_z256_fp4_mul_fp(sm9_z256_fp4_t r, const sm9_z256_fp4_t a, const sm9_z256_t k);
|
||||
void sm9_z256_fp4_mul_fp2(sm9_z256_fp4_t r, const sm9_z256_fp4_t a, const sm9_z256_fp2_t b0);
|
||||
void sm9_z256_fp4_mul_v(sm9_z256_fp4_t r, const sm9_z256_fp4_t a, const sm9_z256_fp4_t b);
|
||||
void sm9_z256_fp4_sqr(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_sqr_v(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_inv(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_frobenius(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_conjugate(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_frobenius2(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
void sm9_z256_fp4_frobenius3(sm9_z256_fp4_t r, const sm9_z256_fp4_t a);
|
||||
|
||||
|
||||
typedef sm9_z256_fp4_t sm9_z256_fp12_t[3];
|
||||
|
||||
void sm9_z256_fp12_set_one(sm9_z256_fp12_t r);
|
||||
void sm9_z256_fp12_set_zero(sm9_z256_fp12_t r);
|
||||
void sm9_z256_fp12_copy(sm9_z256_fp12_t r, const sm9_z256_fp12_t a);
|
||||
int sm9_z256_fp12_rand(sm9_z256_fp12_t r);
|
||||
int sm9_z256_fp12_from_hex(sm9_z256_fp12_t r, const char hex[779]); // 779 = 64*12 + 11
|
||||
void sm9_z256_fp12_to_hex(const sm9_z256_fp12_t a, char hex[779]);
|
||||
void sm9_z256_fp12_to_bytes(const sm9_z256_fp12_t a, uint8_t buf[384]);
|
||||
int sm9_z256_fp12_from_bytes(sm9_z256_fp12_t r, const uint8_t buf[384]);
|
||||
|
||||
void sm9_z256_fp12_print(const char *prefix, const sm9_z256_fp12_t a);
|
||||
void sm9_z256_fp12_set(sm9_z256_fp12_t r, const sm9_z256_fp4_t a0, const sm9_z256_fp4_t a1, const sm9_z256_fp4_t a2);
|
||||
int sm9_z256_fp12_equ(const sm9_z256_fp12_t a, const sm9_z256_fp12_t b);
|
||||
void sm9_z256_fp12_add(sm9_z256_fp12_t r, const sm9_z256_fp12_t a, const sm9_z256_fp12_t b);
|
||||
void sm9_z256_fp12_dbl(sm9_z256_fp12_t r, const sm9_z256_fp12_t a);
|
||||
void sm9_z256_fp12_tri(sm9_z256_fp12_t r, const sm9_z256_fp12_t a);
|
||||
void sm9_z256_fp12_sub(sm9_z256_fp12_t r, const sm9_z256_fp12_t a, const sm9_z256_fp12_t b);
|
||||
void sm9_z256_fp12_neg(sm9_z256_fp12_t r, const sm9_z256_fp12_t a);
|
||||
void sm9_z256_fp12_mul(sm9_z256_fp12_t r, const sm9_z256_fp12_t a, const sm9_z256_fp12_t b);
|
||||
void sm9_z256_fp12_sqr(sm9_z256_fp12_t r, const sm9_z256_fp12_t a);
|
||||
void sm9_z256_fp12_inv(sm9_z256_fp12_t r, const sm9_z256_fp12_t a);
|
||||
void sm9_z256_fp12_pow(sm9_z256_fp12_t r, const sm9_z256_fp12_t a, const sm9_z256_t k);
|
||||
void sm9_z256_fp12_frobenius(sm9_z256_fp12_t r, const sm9_z256_fp12_t x);
|
||||
void sm9_z256_fp12_frobenius2(sm9_z256_fp12_t r, const sm9_z256_fp12_t x);
|
||||
void sm9_z256_fp12_frobenius3(sm9_z256_fp12_t r, const sm9_z256_fp12_t x);
|
||||
void sm9_z256_fp12_frobenius6(sm9_z256_fp12_t r, const sm9_z256_fp12_t x);
|
||||
|
||||
|
||||
// E(F_p): y^2 = x^3 + 5
|
||||
|
||||
typedef struct {
|
||||
sm9_z256_t X; // is mont(X)
|
||||
sm9_z256_t Y; // is mont(Y)
|
||||
sm9_z256_t Z; // is mont(Z)
|
||||
} SM9_Z256_POINT;
|
||||
|
||||
const SM9_Z256_POINT *sm9_z256_generator(void);
|
||||
|
||||
int sm9_z256_point_from_hex(SM9_Z256_POINT *R, const char hex[129]);
|
||||
int sm9_z256_point_is_at_infinity(const SM9_Z256_POINT *P);
|
||||
void sm9_z256_point_set_infinity(SM9_Z256_POINT *R);
|
||||
void sm9_z256_point_get_xy(const SM9_Z256_POINT *P, sm9_z256_t x, sm9_z256_t y);
|
||||
int sm9_z256_point_equ(const SM9_Z256_POINT *P, const SM9_Z256_POINT *Q);
|
||||
int sm9_z256_point_is_on_curve(const SM9_Z256_POINT *P);
|
||||
void sm9_z256_point_dbl(SM9_Z256_POINT *R, const SM9_Z256_POINT *P);
|
||||
void sm9_z256_point_neg(SM9_Z256_POINT *R, const SM9_Z256_POINT *P);
|
||||
void sm9_z256_point_add(SM9_Z256_POINT *R, const SM9_Z256_POINT *P, const SM9_Z256_POINT *Q);
|
||||
void sm9_z256_point_sub(SM9_Z256_POINT *R, const SM9_Z256_POINT *P, const SM9_Z256_POINT *Q);
|
||||
void sm9_z256_point_mul(SM9_Z256_POINT *R, const sm9_z256_t k, const SM9_Z256_POINT *P);
|
||||
void sm9_z256_point_mul_generator(SM9_Z256_POINT *R, const sm9_z256_t k);
|
||||
int sm9_z256_point_print(FILE *fp, int fmt, int ind, const char *label, const SM9_Z256_POINT *P);
|
||||
int sm9_z256_point_to_uncompressed_octets(const SM9_Z256_POINT *P, uint8_t octets[65]);
|
||||
int sm9_z256_point_from_uncompressed_octets(SM9_Z256_POINT *P, const uint8_t octets[65]);
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint64_t X[4];
|
||||
uint64_t Y[4];
|
||||
} SM9_Z256_AFFINE_POINT;
|
||||
|
||||
void sm9_z256_point_copy_affine(SM9_Z256_POINT *R, const SM9_Z256_AFFINE_POINT *P);
|
||||
void sm9_z256_point_add_affine(SM9_Z256_POINT *R, const SM9_Z256_POINT *P, const SM9_Z256_AFFINE_POINT *Q);
|
||||
void sm9_z256_point_sub_affine(SM9_Z256_POINT *R, const SM9_Z256_POINT *P, const SM9_Z256_AFFINE_POINT *Q);
|
||||
|
||||
|
||||
typedef struct {
|
||||
sm9_z256_fp2_t X;
|
||||
sm9_z256_fp2_t Y;
|
||||
sm9_z256_fp2_t Z;
|
||||
} SM9_Z256_TWIST_POINT;
|
||||
|
||||
const SM9_Z256_TWIST_POINT *sm9_z256_twist_generator(void);
|
||||
|
||||
int sm9_z256_twist_point_to_uncompressed_octets(const SM9_Z256_TWIST_POINT *P, uint8_t octets[129]);
|
||||
int sm9_z256_twist_point_from_uncompressed_octets(SM9_Z256_TWIST_POINT *P, const uint8_t octets[129]);
|
||||
|
||||
int sm9_z256_twist_point_print(FILE *fp, int fmt, int ind, const char *label, const SM9_Z256_TWIST_POINT *P);
|
||||
void sm9_z256_twist_point_from_hex(SM9_Z256_TWIST_POINT *R, const char hex[259]); // 259 = 64 * 4 + 3
|
||||
int sm9_z256_twist_point_is_at_infinity(const SM9_Z256_TWIST_POINT *P);
|
||||
void sm9_z256_twist_point_set_infinity(SM9_Z256_TWIST_POINT *R);
|
||||
void sm9_z256_twist_point_get_xy(const SM9_Z256_TWIST_POINT *P, sm9_z256_fp2_t x, sm9_z256_fp2_t y);
|
||||
int sm9_z256_twist_point_equ(const SM9_Z256_TWIST_POINT *P, const SM9_Z256_TWIST_POINT *Q);
|
||||
int sm9_z256_twist_point_is_on_curve(const SM9_Z256_TWIST_POINT *P);
|
||||
void sm9_z256_twist_point_neg(SM9_Z256_TWIST_POINT *R, const SM9_Z256_TWIST_POINT *P);
|
||||
void sm9_z256_twist_point_dbl(SM9_Z256_TWIST_POINT *R, const SM9_Z256_TWIST_POINT *P);
|
||||
void sm9_z256_twist_point_add(SM9_Z256_TWIST_POINT *R, const SM9_Z256_TWIST_POINT *P, const SM9_Z256_TWIST_POINT *Q);
|
||||
void sm9_z256_twist_point_sub(SM9_Z256_TWIST_POINT *R, const SM9_Z256_TWIST_POINT *P, const SM9_Z256_TWIST_POINT *Q);
|
||||
void sm9_z256_twist_point_add_full(SM9_Z256_TWIST_POINT *R, const SM9_Z256_TWIST_POINT *P, const SM9_Z256_TWIST_POINT *Q);
|
||||
void sm9_z256_twist_point_mul(SM9_Z256_TWIST_POINT *R, const sm9_z256_t k, const SM9_Z256_TWIST_POINT *P);
|
||||
void sm9_z256_twist_point_mul_generator(SM9_Z256_TWIST_POINT *R, const sm9_z256_t k);
|
||||
|
||||
|
||||
void sm9_z256_point_to_affine(SM9_Z256_AFFINE_POINT *Q, const SM9_Z256_POINT *P);
|
||||
void sm9_z256_eval_g_tangent(SM9_Z256_TWIST_POINT *R, sm9_z256_fp2_t lw[3],
|
||||
const SM9_Z256_TWIST_POINT *P, const SM9_Z256_AFFINE_POINT *Q);
|
||||
void sm9_z256_eval_g_line(SM9_Z256_TWIST_POINT *R, sm9_z256_fp2_t lw[3], sm9_z256_fp2_t pre[5],
|
||||
const SM9_Z256_TWIST_POINT *P, const SM9_Z256_TWIST_POINT *T, const SM9_Z256_AFFINE_POINT *Q);
|
||||
void sm9_z256_eval_g_line_no_pre(SM9_Z256_TWIST_POINT *R, sm9_z256_fp2_t lw[3],
|
||||
const SM9_Z256_TWIST_POINT *P, const SM9_Z256_TWIST_POINT *T, const SM9_Z256_AFFINE_POINT *Q);
|
||||
void sm9_z256_fp12_line_mul(sm9_z256_fp12_t r, const sm9_z256_fp12_t a, const sm9_z256_fp2_t lw[3]);
|
||||
//void sm9_z256_eval_g_tangent(sm9_z256_fp12_t num, sm9_z256_fp12_t den, const SM9_Z256_TWIST_POINT *P, const SM9_Z256_POINT *Q);
|
||||
//void sm9_z256_eval_g_line(sm9_z256_fp12_t num, sm9_z256_fp12_t den, const SM9_Z256_TWIST_POINT *T, const SM9_Z256_TWIST_POINT *P, const SM9_Z256_POINT *Q);
|
||||
void sm9_z256_twist_point_pi1(SM9_Z256_TWIST_POINT *R, const SM9_Z256_TWIST_POINT *P);
|
||||
void sm9_z256_twist_point_pi2(SM9_Z256_TWIST_POINT *R, const SM9_Z256_TWIST_POINT *P);
|
||||
void sm9_z256_twist_point_neg_pi2(SM9_Z256_TWIST_POINT *R, const SM9_Z256_TWIST_POINT *P);
|
||||
void sm9_z256_final_exponent_hard_part(sm9_z256_fp12_t r, const sm9_z256_fp12_t f);
|
||||
void sm9_z256_final_exponent(sm9_z256_fp12_t r, const sm9_z256_fp12_t f);
|
||||
void sm9_z256_pairing(sm9_z256_fp12_t r, const SM9_Z256_TWIST_POINT *Q, const SM9_Z256_POINT *P);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -35,7 +36,7 @@ typedef int tls_socklen_t;
|
||||
#define tls_socket_send(sock,buf,len,flags) send(sock,buf,(int)(len),flags)
|
||||
#define tls_socket_recv(sock,buf,len,flags) recv(sock,buf,(int)(len),flags)
|
||||
#define tls_socket_close(sock) closesocket(sock)
|
||||
|
||||
#define tls_socket_wait() Sleep(1)
|
||||
|
||||
#else
|
||||
|
||||
@@ -55,6 +56,7 @@ typedef socklen_t tls_socklen_t;
|
||||
#define tls_socket_send(sock,buf,len,flags) send(sock,buf,len,flags)
|
||||
#define tls_socket_recv(sock,buf,len,flags) recv(sock,buf,len,flags)
|
||||
#define tls_socket_close(sock) close(sock)
|
||||
#define tls_socket_wait() usleep(1000)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
94
thirdparty/GmSSL-3.1.1/include/gmssl/tls.h
vendored
94
thirdparty/GmSSL-3.1.1/include/gmssl/tls.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
/*
|
||||
* Copyright 2014-2023 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.
|
||||
@@ -27,39 +27,6 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
TLS Public API
|
||||
|
||||
TLS_PROTOCOL
|
||||
TLS_protocol_tlcp
|
||||
TLS_protocol_tls12
|
||||
TLS_protocol_tls13
|
||||
|
||||
TLS_CIPHER_SUITE
|
||||
TLS_cipher_ecc_sm4_cbc_sm3
|
||||
TLS_cipher_ecc_sm4_gcm_sm3
|
||||
TLS_cipher_ecdhe_sm4_cbc_sm3
|
||||
TLS_cipher_ecdhe_sm4_gcm_sm3
|
||||
TLS_cipher_sm4_gcm_sm3
|
||||
|
||||
TLS_CTX
|
||||
tls_ctx_init
|
||||
tls_ctx_set_cipher_suites
|
||||
tls_ctx_set_ca_certificates
|
||||
tls_ctx_set_certificate_and_key
|
||||
tls_ctx_set_tlcp_server_certificate_and_keys
|
||||
tls_ctx_cleanup
|
||||
|
||||
TLS_CONNECT
|
||||
tls_init
|
||||
tls_set_socket
|
||||
tls_do_handshake
|
||||
tls_send
|
||||
tls_recv
|
||||
tls_shutdown
|
||||
tls_cleanup
|
||||
*/
|
||||
|
||||
typedef uint32_t uint24_t;
|
||||
|
||||
#define tls_uint8_size() 1
|
||||
@@ -108,7 +75,7 @@ typedef enum {
|
||||
TLS_cipher_sm4_ccm_sm3 = 0x00c7,
|
||||
|
||||
// TLCP, GB/T 38636-2020, GM/T 0024-2012
|
||||
TLS_cipher_ecdhe_sm4_cbc_sm3 = 0xe011, // 可以让TLSv1.2使用这个
|
||||
TLS_cipher_ecdhe_sm4_cbc_sm3 = 0xe011, // TODO: let TLSv1.2 use this as default cipher suite
|
||||
TLS_cipher_ecdhe_sm4_gcm_sm3 = 0xe051,
|
||||
TLS_cipher_ecc_sm4_cbc_sm3 = 0xe013,
|
||||
TLS_cipher_ecc_sm4_gcm_sm3 = 0xe053,
|
||||
@@ -286,8 +253,6 @@ typedef enum {
|
||||
const char *tls_curve_type_name(int type);
|
||||
|
||||
|
||||
// 与其支持v2,还不如直接修改v2,让v2和v3兼容
|
||||
|
||||
typedef enum {
|
||||
TLS_curve_secp256k1 = 22,
|
||||
TLS_curve_secp256r1 = 23,
|
||||
@@ -304,7 +269,7 @@ typedef enum {
|
||||
TLS_curve_sm2p256v1 = 41, // GmSSLv2: 30
|
||||
} TLS_NAMED_CURVE;
|
||||
|
||||
const char *tls_named_curve_name(int curve);
|
||||
const char *tls_curve_name(int curve);
|
||||
|
||||
|
||||
typedef enum {
|
||||
@@ -342,6 +307,7 @@ typedef enum {
|
||||
|
||||
|
||||
typedef enum {
|
||||
TLS_alert_level_undefined = 0,
|
||||
TLS_alert_level_warning = 1,
|
||||
TLS_alert_level_fatal = 2,
|
||||
} TLS_ALERT_LEVEL;
|
||||
@@ -440,14 +406,15 @@ typedef struct {
|
||||
#define tls_record_protocol(record) (((uint16_t)((record)[1]) << 8) | (record)[2])
|
||||
#define tls_record_data(record) ((record)+TLS_RECORD_HEADER_SIZE)
|
||||
#define tls_record_data_length(record) (((uint16_t)((record)[3]) << 8) | (record)[4])
|
||||
#define tls_record_length(record) (TLS_RECORD_HEADER_SIZE + tls_record_data_length(record))
|
||||
#define tls_record_length(record) ((size_t)(TLS_RECORD_HEADER_SIZE + tls_record_data_length(record)))
|
||||
|
||||
int tls_record_set_type(uint8_t *record, int type);
|
||||
int tls_record_set_protocol(uint8_t *record, int protocol);
|
||||
int tls_record_set_data_length(uint8_t *record, size_t length);
|
||||
int tls_record_set_data(uint8_t *record, const uint8_t *data, size_t datalen);
|
||||
|
||||
// 握手消息ServerKeyExchange, ClientKeyExchange的解析依赖当前密码套件
|
||||
|
||||
// parse ServerKeyExchange, ClientKeyExchange depends on current cipher_suite
|
||||
#define tls_format_set_cipher_suite(fmt,cipher) do {(fmt)|=((cipher)<<8);} while (0)
|
||||
int tls_record_print(FILE *fp, const uint8_t *record, size_t recordlen, int format, int indent);
|
||||
int tlcp_record_print(FILE *fp, const uint8_t *record, size_t recordlen, int format, int indent);
|
||||
@@ -534,13 +501,13 @@ int tls13_process_client_supported_versions(const uint8_t *ext_data, size_t ext_
|
||||
|
||||
int tls13_process_server_supported_versions(const uint8_t *ext_data, size_t ext_datalen);
|
||||
|
||||
int tls13_key_share_entry_to_bytes(const SM2_POINT *point, uint8_t **out, size_t *outlen);
|
||||
int tls13_client_key_share_ext_to_bytes(const SM2_POINT *point, uint8_t **out, size_t *outlen);
|
||||
int tls13_server_key_share_ext_to_bytes(const SM2_POINT *point, uint8_t **out, size_t *outlen);
|
||||
int tls13_key_share_entry_to_bytes(const SM2_Z256_POINT *point, uint8_t **out, size_t *outlen);
|
||||
int tls13_client_key_share_ext_to_bytes(const SM2_Z256_POINT *point, uint8_t **out, size_t *outlen);
|
||||
int tls13_server_key_share_ext_to_bytes(const SM2_Z256_POINT *point, uint8_t **out, size_t *outlen);
|
||||
int tls13_process_client_key_share(const uint8_t *ext_data, size_t ext_datalen,
|
||||
const SM2_KEY *server_ecdhe_key, SM2_POINT *client_ecdhe_public,
|
||||
const SM2_KEY *server_ecdhe_key, SM2_Z256_POINT *client_ecdhe_public,
|
||||
uint8_t **out, size_t *outlen);
|
||||
int tls13_process_server_key_share(const uint8_t *ext_data, size_t ext_datalen, SM2_POINT *point);
|
||||
int tls13_process_server_key_share(const uint8_t *ext_data, size_t ext_datalen, SM2_Z256_POINT *point);
|
||||
|
||||
|
||||
int tls13_certificate_authorities_ext_to_bytes(const uint8_t *ca_names, size_t ca_names_len,
|
||||
@@ -555,8 +522,8 @@ int tls_process_server_exts(const uint8_t *exts, size_t extslen,
|
||||
// Certificate
|
||||
int tls_record_set_handshake_certificate(uint8_t *record, size_t *recordlen,
|
||||
const uint8_t *certs, size_t certslen);
|
||||
// 这个函数比较特殊,是直接解析了证书链,而不是返回指针
|
||||
// 应该提供一个独立的解析函数来解析TLS的证书链
|
||||
// see the impl of tls_record_get_handshake_certificate
|
||||
// a standalone cert-chain parsing function should be given
|
||||
int tls_record_get_handshake_certificate(const uint8_t *record, uint8_t *certs, size_t *certslen);
|
||||
|
||||
// ServerKeyExchange
|
||||
@@ -565,14 +532,14 @@ int tls_server_key_exchange_print(FILE *fp, const uint8_t *ske, size_t skelen, i
|
||||
#define TLS_MAX_SIGNATURE_SIZE SM2_MAX_SIGNATURE_SIZE
|
||||
int tls_sign_server_ecdh_params(const SM2_KEY *server_sign_key,
|
||||
const uint8_t client_random[32], const uint8_t server_random[32],
|
||||
int curve, const SM2_POINT *point, uint8_t *sig, size_t *siglen);
|
||||
int curve, const SM2_Z256_POINT *point, uint8_t *sig, size_t *siglen);
|
||||
int tls_verify_server_ecdh_params(const SM2_KEY *server_sign_key,
|
||||
const uint8_t client_random[32], const uint8_t server_random[32],
|
||||
int curve, const SM2_POINT *point, const uint8_t *sig, size_t siglen);
|
||||
int curve, const SM2_Z256_POINT *point, const uint8_t *sig, size_t siglen);
|
||||
int tls_record_set_handshake_server_key_exchange_ecdhe(uint8_t *record, size_t *recordlen,
|
||||
int curve, const SM2_POINT *point, const uint8_t *sig, size_t siglen);
|
||||
int curve, const SM2_Z256_POINT *point, const uint8_t *sig, size_t siglen);
|
||||
int tls_record_get_handshake_server_key_exchange_ecdhe(const uint8_t *record,
|
||||
int *curve, SM2_POINT *point, const uint8_t **sig, size_t *siglen);
|
||||
int *curve, SM2_Z256_POINT *point, const uint8_t **sig, size_t *siglen);
|
||||
int tls_server_key_exchange_ecdhe_print(FILE *fp, const uint8_t *data, size_t datalen,
|
||||
int format, int indent);
|
||||
|
||||
@@ -615,8 +582,8 @@ int tls_client_key_exchange_pke_print(FILE *fp, const uint8_t *cke, size_t ckele
|
||||
int tls_client_key_exchange_print(FILE *fp, const uint8_t *cke, size_t ckelen, int format, int indent);
|
||||
|
||||
int tls_record_set_handshake_client_key_exchange_ecdhe(uint8_t *record, size_t *recordlen,
|
||||
const SM2_POINT *point); // 这里不应该支持SM2_POINT类型
|
||||
int tls_record_get_handshake_client_key_exchange_ecdhe(const uint8_t *record, SM2_POINT *point);
|
||||
const SM2_Z256_POINT *point); // shoulde we use SM2_Z256_POITN?
|
||||
int tls_record_get_handshake_client_key_exchange_ecdhe(const uint8_t *record, SM2_Z256_POINT *point);
|
||||
int tls_client_key_exchange_ecdhe_print(FILE *fp, const uint8_t *data, size_t datalen,
|
||||
int format, int indent);
|
||||
|
||||
@@ -650,8 +617,8 @@ int tls_client_verify_finish(TLS_CLIENT_VERIFY_CTX *ctx, const uint8_t *sig, siz
|
||||
void tls_client_verify_cleanup(TLS_CLIENT_VERIFY_CTX *ctx);
|
||||
|
||||
// Finished
|
||||
// FIXME: 支持TLS 1.3 提供MIN, MAX或TLS12, TLS13, TLCP...
|
||||
#define TLS_VERIFY_DATA_SIZE 12 // TLS 1.3或者其他版本支持更长的verify_data
|
||||
// FIXME: to support TLS 1.3 need MIN, MAX or TLS12, TLS13, TLCP...
|
||||
#define TLS_VERIFY_DATA_SIZE 12 // TLS 1.3 use longer verify_data (>= 12 bytes)
|
||||
#define TLS_FINISHED_RECORD_SIZE (TLS_RECORD_HEADER_SIZE + TLS_HANDSHAKE_HEADER_SIZE + TLS_VERIFY_DATA_SIZE) // 21
|
||||
#define TLS_MAX_PADDING_SIZE (1 + 255)
|
||||
#define TLS_MAC_SIZE SM3_HMAC_SIZE
|
||||
@@ -716,6 +683,8 @@ typedef struct {
|
||||
SM2_KEY signkey;
|
||||
SM2_KEY kenckey;
|
||||
int verify_depth;
|
||||
|
||||
int quiet;
|
||||
} TLS_CTX;
|
||||
|
||||
int tls_ctx_init(TLS_CTX *ctx, int protocol, int is_client);
|
||||
@@ -748,15 +717,14 @@ typedef struct {
|
||||
|
||||
uint8_t record[TLS_MAX_RECORD_SIZE];
|
||||
|
||||
// 其实这个就不太对了,还是应该有一个完整的密文记录
|
||||
uint8_t databuf[TLS_MAX_PLAINTEXT_SIZE];
|
||||
uint8_t databuf[TLS_MAX_RECORD_SIZE];
|
||||
uint8_t *data;
|
||||
size_t datalen;
|
||||
|
||||
int cipher_suite;
|
||||
uint8_t session_id[32];
|
||||
size_t session_id_len;
|
||||
uint8_t server_certs[TLS_MAX_CERTIFICATES_SIZE]; // 动态的可能会好一点
|
||||
uint8_t server_certs[TLS_MAX_CERTIFICATES_SIZE]; // TODO: use ptr and malloc
|
||||
size_t server_certs_len;
|
||||
uint8_t client_certs[TLS_MAX_CERTIFICATES_SIZE];
|
||||
size_t client_certs_len;
|
||||
@@ -783,10 +751,11 @@ typedef struct {
|
||||
BLOCK_CIPHER_KEY client_write_key;
|
||||
BLOCK_CIPHER_KEY server_write_key;
|
||||
|
||||
int quiet;
|
||||
} TLS_CONNECT;
|
||||
|
||||
|
||||
#define TLS_MAX_EXTENSIONS_SIZE 512 // 这个应该再考虑一下数值,是否可以用其他的缓冲区装载?
|
||||
#define TLS_MAX_EXTENSIONS_SIZE 512 // FIXME: no reason to give fixed max length
|
||||
|
||||
|
||||
int tls_init(TLS_CONNECT *conn, const TLS_CTX *ctx);
|
||||
@@ -854,20 +823,23 @@ int tls13_gcm_decrypt(const BLOCK_CIPHER_KEY *key, const uint8_t iv[12],
|
||||
int *record_type, uint8_t *out, size_t *outlen);
|
||||
|
||||
|
||||
#ifdef TLS_DEBUG
|
||||
#ifdef ENABLE_TLS_DEBUG
|
||||
# define tls_trace(s) fprintf(stderr,(s))
|
||||
# define tls_record_trace(fp,rec,reclen,fmt,ind) tls_record_print(fp,rec,reclen,fmt,ind)
|
||||
# define tls_encrypted_record_trace(fp,rec,reclen,fmt,ind) tls_encrypted_record_print(fp,rec,reclen,fmt,ind)
|
||||
# define tlcp_record_trace(fp,rec,reclen,fmt,ind) tlcp_record_print(fp,rec,reclen,fmt,ind)
|
||||
# define tls12_record_trace(fp,rec,reclen,fmt,ind) tls12_record_print(fp,rec,reclen,fmt,ind)
|
||||
# define tls13_record_trace(fp,rec,reclen,fmt,ind) tls13_record_print(fp,fmt,ind,rec,reclen)
|
||||
#else
|
||||
# define tls_trace(s)
|
||||
# define tls_record_trace(fp,rec,reclen,fmt,ind)
|
||||
# define tls_encrypted_record_trace(fp,rec,reclen,fmt,ind)
|
||||
# define tlcp_record_trace(fp,rec,reclen,fmt,ind)
|
||||
# define tls12_record_trace(fp,rec,reclen,fmt,ind)
|
||||
# define tls13_record_trace(fp,rec,reclen,fmt,ind)
|
||||
#endif
|
||||
|
||||
int tls_encrypted_record_print(FILE *fp, const uint8_t *record, size_t recordlen, int format, int indent);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
10
thirdparty/GmSSL-3.1.1/include/gmssl/version.h
vendored
10
thirdparty/GmSSL-3.1.1/include/gmssl/version.h
vendored
@@ -12,19 +12,17 @@
|
||||
#ifndef GMSSL_VERSION_H
|
||||
#define GMSSL_VERSION_H
|
||||
|
||||
#include <gmssl/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// Also update CPACK_PACKAGE_VERSION in CMakeLists.txt
|
||||
#define GMSSL_VERSION_NUM 30101
|
||||
#define GMSSL_VERSION_STR "GmSSL 3.1.1"
|
||||
#define GMSSL_VERSION_NUM 30102
|
||||
#define GMSSL_VERSION_STR "GmSSL 3.1.2 Dev"
|
||||
|
||||
_gmssl_export int gmssl_version_num(void);
|
||||
_gmssl_export const char *gmssl_version_str(void);
|
||||
int gmssl_version_num(void);
|
||||
const char *gmssl_version_str(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
2
thirdparty/GmSSL-3.1.1/include/gmssl/x509.h
vendored
2
thirdparty/GmSSL-3.1.1/include/gmssl/x509.h
vendored
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
|
||||
39
thirdparty/GmSSL-3.1.1/include/gmssl/zuc.h
vendored
39
thirdparty/GmSSL-3.1.1/include/gmssl/zuc.h
vendored
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2024 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.
|
||||
@@ -11,41 +11,14 @@
|
||||
#ifndef GMSSL_ZUC_H
|
||||
#define GMSSL_ZUC_H
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
ZUC Public API
|
||||
|
||||
ZUC_KEY_SIZE
|
||||
ZUC_IV_SIZE
|
||||
ZUC_MAC_SIZE
|
||||
|
||||
ZUC_CTX
|
||||
zuc_encrypt_init
|
||||
zuc_encrypt_update
|
||||
zuc_encrypt_finish
|
||||
zuc_decrypt_init
|
||||
zuc_decrypt_update
|
||||
zuc_decrypt_finish
|
||||
|
||||
ZUC_MAC_CTX
|
||||
zuc_mac_init
|
||||
zuc_mac_update
|
||||
zuc_mac_finish
|
||||
|
||||
zuc_eea_encrypt
|
||||
zuc_eia_generate_mac
|
||||
*/
|
||||
|
||||
|
||||
# define ZUC_KEY_SIZE 16
|
||||
# define ZUC_IV_SIZE 16
|
||||
# define ZUC_MAC_SIZE 4
|
||||
@@ -103,8 +76,8 @@ ZUC_UINT32 zuc_eia_generate_mac(const ZUC_UINT32 *data, size_t nbits,
|
||||
typedef ZUC_STATE ZUC256_STATE;
|
||||
|
||||
void zuc256_init(ZUC256_STATE *state, const uint8_t key[ZUC256_KEY_SIZE], const uint8_t iv[ZUC256_IV_SIZE]);
|
||||
#define zuc256_generate_keystream(state,nwords,words) zuc_generate_keystream(state,nwords,words)
|
||||
#define zuc256_generate_keyword(state) zuc_generate_keyword(state)
|
||||
void zuc256_generate_keystream(ZUC_STATE *state, size_t nwords, ZUC_UINT32 *words);
|
||||
ZUC_UINT32 zuc256_generate_keyword(ZUC_STATE *state);
|
||||
|
||||
|
||||
typedef struct ZUC256_MAC_CTX_st {
|
||||
@@ -124,8 +97,6 @@ void zuc256_mac_update(ZUC256_MAC_CTX *ctx, const uint8_t *data, size_t len);
|
||||
void zuc256_mac_finish(ZUC256_MAC_CTX *ctx, const uint8_t *data, size_t nbits, uint8_t mac[ZUC_MAC_SIZE]);
|
||||
|
||||
|
||||
// Public API
|
||||
|
||||
typedef struct {
|
||||
ZUC_STATE zuc_state;
|
||||
uint8_t block[4];
|
||||
@@ -136,10 +107,6 @@ int zuc_encrypt_init(ZUC_CTX *ctx, const uint8_t key[ZUC_KEY_SIZE], const uint8_
|
||||
int zuc_encrypt_update(ZUC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
|
||||
int zuc_encrypt_finish(ZUC_CTX *ctx, uint8_t *out, size_t *outlen);
|
||||
|
||||
#define zuc_decrypt_init(ctx,key,iv) zuc_encrypt_init(ctx,key,iv)
|
||||
#define zuc_decrypt_update(ctx,in,inlen,out,outlen) zuc_encrypt_update(ctx,in,inlen,out,outlen)
|
||||
#define zuc_decrypt_finish(ctx,out,outlen) zuc_encrypt_finish(ctx,out,outlen)
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
BIN
thirdparty/GmSSL-3.1.1/lib/gmssl.lib
vendored
BIN
thirdparty/GmSSL-3.1.1/lib/gmssl.lib
vendored
Binary file not shown.
Reference in New Issue
Block a user