Skip to content

Commit

Permalink
shim support sm2 and sm3 algorithm
Browse files Browse the repository at this point in the history
Signed-off-by: Sinong Chen <costinchen@tencent.com>
  • Loading branch information
costinchen committed Oct 14, 2024
1 parent 9a95a96 commit 7433af1
Show file tree
Hide file tree
Showing 48 changed files with 2,430 additions and 43 deletions.
231 changes: 231 additions & 0 deletions Cryptlib/Hash/CryptSm3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/** @file
SM3 Digest Wrapper Implementation over OpenSSL.
Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/

#include "InternalCryptLib.h"
#include <openssl/sm3.h>

/**
Retrieves the size, in bytes, of the context buffer required for SM3 hash operations.
@return The size, in bytes, of the context buffer required for SM3 hash operations.
**/
UINTN
EFIAPI
Sm3GetContextSize (
VOID
)
{
//
// Retrieves OpenSSL SM3 Context Size
//
return (UINTN) (sizeof (SM3_CTX));
}

/**
Initializes user-supplied memory pointed by Sm3Context as SM3 hash context for
subsequent use.
If Sm3Context is NULL, then return FALSE.
@param[out] Sm3Context Pointer to SM3 context being initialized.
@retval TRUE SM3 context initialization succeeded.
@retval FALSE SM3 context initialization failed.
**/
BOOLEAN
EFIAPI
Sm3Init (
OUT VOID *Sm3Context
)
{
//
// Check input parameters.
//
if (Sm3Context == NULL) {
return FALSE;
}

//
// OpenSSL SM3 Context Initialization
//
return (BOOLEAN) (sm3_init ((SM3_CTX *) Sm3Context));
}

/**
Makes a copy of an existing SM3 context.
If Sm3Context is NULL, then return FALSE.
If NewSm3Context is NULL, then return FALSE.
@param[in] Sm3Context Pointer to SM3 context being copied.
@param[out] NewSm3Context Pointer to new SM3 context.
@retval TRUE SM3 context copy succeeded.
@retval FALSE SM3 context copy failed.
**/
BOOLEAN
EFIAPI
Sm3Duplicate (
IN CONST VOID *Sm3Context,
OUT VOID *NewSm3Context
)
{
//
// Check input parameters.
//
if (Sm3Context == NULL || NewSm3Context == NULL) {
return FALSE;
}

CopyMem (NewSm3Context, (void *)Sm3Context, sizeof (SM3_CTX));

return TRUE;
}

/**
Digests the input data and updates SM3 context.
This function performs SM3 digest on a data buffer of the specified size.
It can be called multiple times to compute the digest of long or discontinuous data streams.
SM3 context should be already correctly initialized by Sm3Init(), and should not be finalized
by Sm3Final(). Behavior with invalid context is undefined.
If Sm3Context is NULL, then return FALSE.
@param[in, out] Sm3Context Pointer to the SM3 context.
@param[in] Data Pointer to the buffer containing the data to be hashed.
@param[in] DataSize Size of Data buffer in bytes.
@retval TRUE SM3 data digest succeeded.
@retval FALSE SM3 data digest failed.
**/
BOOLEAN
EFIAPI
Sm3Update (
IN OUT VOID *Sm3Context,
IN CONST VOID *Data,
IN UINTN DataSize
)
{
//
// Check input parameters.
//
if (Sm3Context == NULL) {
return FALSE;
}

//
// Check invalid parameters, in case that only DataLength was checked in OpenSSL
//
if (Data == NULL && DataSize != 0) {
return FALSE;
}

//
// OpenSSL SM3 Hash Update
//
return (BOOLEAN) (sm3_update ((SM3_CTX *) Sm3Context, Data, DataSize));
}

/**
Completes computation of the SM3 digest value.
This function completes SM3 hash computation and retrieves the digest value into
the specified memory. After this function has been called, the SM3 context cannot
be used again.
SM3 context should be already correctly initialized by Sm3Init(), and should not be
finalized by Sm3Final(). Behavior with invalid SM3 context is undefined.
If Sm3Context is NULL, then return FALSE.
If HashValue is NULL, then return FALSE.
@param[in, out] Sm3Context Pointer to the SM3 context.
@param[out] HashValue Pointer to a buffer that receives the SM3 digest
value (32 bytes).
@retval TRUE SM3 digest computation succeeded.
@retval FALSE SM3 digest computation failed.
**/
BOOLEAN
EFIAPI
Sm3Final (
IN OUT VOID *Sm3Context,
OUT UINT8 *HashValue
)
{
//
// Check input parameters.
//
if (Sm3Context == NULL || HashValue == NULL) {
return FALSE;
}

//
// OpenSSL SM3 Hash Finalization
//
return (BOOLEAN) (sm3_final (HashValue, (SM3_CTX *) Sm3Context));
}

/**
Computes the SM3 message digest of a input data buffer.
This function performs the SM3 message digest of a given data buffer, and places
the digest value into the specified memory.
If this interface is not supported, then return FALSE.
@param[in] Data Pointer to the buffer containing the data to be hashed.
@param[in] DataSize Size of Data buffer in bytes.
@param[out] HashValue Pointer to a buffer that receives the SM3 digest
value (32 bytes).
@retval TRUE SM3 digest computation succeeded.
@retval FALSE SM3 digest computation failed.
@retval FALSE This interface is not supported.
**/
BOOLEAN
EFIAPI
Sm3HashAll (
IN CONST VOID *Data,
IN UINTN DataSize,
OUT UINT8 *HashValue
)
{
//
// Check input parameters.
//
if (HashValue == NULL) {
return FALSE;
}
if (Data == NULL && DataSize != 0) {
return FALSE;
}

//
// OpenSSL SM3 Hash Computation.
//

SM3_CTX c;
sm3_init(&c);
sm3_update(&c, Data, DataSize);
sm3_final(HashValue, &c);

return TRUE;
}
10 changes: 10 additions & 0 deletions Cryptlib/Include/openssl/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ int CRYPTO_is_mem_check_on(void);
# define is_MemCheck_on() CRYPTO_is_mem_check_on()

# define OPENSSL_malloc(num) CRYPTO_malloc((int)num,OPENSSL_FILE,OPENSSL_LINE)
# define OPENSSL_zalloc(num) CRYPTO_zalloc((int)num,OPENSSL_FILE,OPENSSL_LINE)
# define OPENSSL_strdup(str) CRYPTO_strdup((str),OPENSSL_FILE,OPENSSL_LINE)
# define OPENSSL_realloc(addr,num) \
CRYPTO_realloc((char *)addr,(int)num,OPENSSL_FILE,OPENSSL_LINE)
Expand All @@ -389,6 +390,8 @@ int CRYPTO_is_mem_check_on(void);
CRYPTO_remalloc((char **)addr,(int)num,OPENSSL_FILE,OPENSSL_LINE)
# define OPENSSL_freeFunc CRYPTO_free
# define OPENSSL_free(addr) CRYPTO_free(addr)
# define OPENSSL_clear_free(addr, num) \
CRYPTO_clear_free(addr,num,OPENSSL_FILE,OPENSSL_LINE)

# define OPENSSL_malloc_locked(num) \
CRYPTO_malloc_locked((int)num,OPENSSL_FILE,OPENSSL_LINE)
Expand All @@ -399,6 +402,8 @@ unsigned long SSLeay(void);

int OPENSSL_issetugid(void);

unsigned char *OPENSSL_hexstr2buf(const char *str, long *len);

/* An opaque type representing an implementation of "ex_data" support */
typedef struct st_CRYPTO_EX_DATA_IMPL CRYPTO_EX_DATA_IMPL;
/* Return an opaque pointer to the current "ex_data" implementation */
Expand Down Expand Up @@ -533,12 +538,14 @@ void CRYPTO_get_mem_debug_functions(void (**m)
void *CRYPTO_malloc_locked(int num, const char *file, int line);
void CRYPTO_free_locked(void *ptr);
void *CRYPTO_malloc(int num, const char *file, int line);
void *CRYPTO_zalloc(int num, const char *file, int line);
char *CRYPTO_strdup(const char *str, const char *file, int line);
void CRYPTO_free(void *ptr);
void *CRYPTO_realloc(void *addr, int num, const char *file, int line);
void *CRYPTO_realloc_clean(void *addr, int old_num, int num, const char *file,
int line);
void *CRYPTO_remalloc(void *addr, int num, const char *file, int line);
void CRYPTO_clear_free(void *ptr, int num, const char *file, int line);

void OPENSSL_cleanse(void *ptr, size_t len);

Expand Down Expand Up @@ -651,10 +658,13 @@ void ERR_load_CRYPTO_strings(void);
# define CRYPTO_F_INT_DUP_EX_DATA 106
# define CRYPTO_F_INT_FREE_EX_DATA 107
# define CRYPTO_F_INT_NEW_EX_DATA 108
# define CRYPTO_F_OPENSSL_HEXSTR2BUF 118

/* Reason codes. */
# define CRYPTO_R_FIPS_MODE_NOT_SUPPORTED 101
# define CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK 100
# define CRYPTO_R_ILLEGAL_HEX_DIGIT 102
# define CRYPTO_R_ODD_NUMBER_OF_DIGITS 103

#ifdef __cplusplus
}
Expand Down
20 changes: 20 additions & 0 deletions Cryptlib/Include/openssl/ec.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group);
*/
int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx);

/** Gets the order of an EC_GROUP
* \param group EC_GROUP object
* \return the group order
*/
const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group);

/** Gets the cofactor of a EC_GROUP
* \param group EC_GROUP object
* \param cofactor BIGNUM to which the cofactor is copied
Expand Down Expand Up @@ -1053,6 +1059,17 @@ int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
EVP_PKEY_OP_DERIVE, \
EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)p)
# define EVP_PKEY_CTX_set1_id(ctx, id, id_len) \
EVP_PKEY_CTX_ctrl(ctx, -1, -1, \
EVP_PKEY_CTRL_SET1_ID, (int)id_len, (void*)(id))

# define EVP_PKEY_CTX_get1_id(ctx, id) \
EVP_PKEY_CTX_ctrl(ctx, -1, -1, \
EVP_PKEY_CTRL_GET1_ID, 0, (void*)(id))

# define EVP_PKEY_CTX_get1_id_len(ctx, id_len) \
EVP_PKEY_CTX_ctrl(ctx, -1, -1, \
EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)(id_len))

# define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1)
# define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2)
Expand All @@ -1064,6 +1081,9 @@ int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
# define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8)
# define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9)
# define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10)
# define EVP_PKEY_CTRL_SET1_ID (EVP_PKEY_ALG_CTRL + 11)
# define EVP_PKEY_CTRL_GET1_ID (EVP_PKEY_ALG_CTRL + 12)
# define EVP_PKEY_CTRL_GET1_ID_LEN (EVP_PKEY_ALG_CTRL + 13)
/* KDF types */
# define EVP_PKEY_ECDH_KDF_NONE 1
# define EVP_PKEY_ECDH_KDF_X9_62 2
Expand Down
7 changes: 7 additions & 0 deletions Cryptlib/Include/openssl/ecdsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ ECDSA_SIG *ECDSA_SIG_new(void);
*/
void ECDSA_SIG_free(ECDSA_SIG *sig);

/** Accessor for r and s fields of ECDSA_SIG
* \param sig pointer to ECDSA_SIG structure
* \param pr pointer to BIGNUM pointer for r (may be NULL)
* \param ps pointer to BIGNUM pointer for s (may be NULL)
*/
void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);

/** DER encode content of ECDSA_SIG object (note: this function modifies *pp
* (*pp += length of the DER encoded signature)).
* \param sig pointer to the ECDSA_SIG object
Expand Down
6 changes: 6 additions & 0 deletions Cryptlib/Include/openssl/err.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ typedef struct err_state_st {
# define ERR_LIB_TS 47
# define ERR_LIB_HMAC 48
# define ERR_LIB_JPAKE 49
# define ERR_LIB_SM2 53


# define ERR_LIB_USER 128

Expand Down Expand Up @@ -233,6 +235,7 @@ typedef struct err_state_st {
# define TSerr(f,r) ERR_PUT_error(ERR_LIB_TS,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
# define HMACerr(f,r) ERR_PUT_error(ERR_LIB_HMAC,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
# define JPAKEerr(f,r) ERR_PUT_error(ERR_LIB_JPAKE,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
# define SM2err(f,r) ERR_PUT_error(ERR_LIB_SM2,(f),(r),OPENSSL_FILE,OPENSSL_LINE)

/*
* Borland C seems too stupid to be able to shift and do longs in the
Expand Down Expand Up @@ -304,6 +307,9 @@ typedef struct err_state_st {
# define ERR_R_PASSED_NULL_PARAMETER (3|ERR_R_FATAL)
# define ERR_R_INTERNAL_ERROR (4|ERR_R_FATAL)
# define ERR_R_DISABLED (5|ERR_R_FATAL)
# define ERR_R_INIT_FAIL (6|ERR_R_FATAL)
# define ERR_R_PASSED_INVALID_ARGUMENT (7)
# define ERR_R_OPERATION_FAIL (8|ERR_R_FATAL)

/*
* 99 is the maximum possible ERR_R_... code, higher values are reserved for
Expand Down
Loading

0 comments on commit 7433af1

Please sign in to comment.