-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support SM3 and SM4 #271
Support SM3 and SM4 #271
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -358,11 +358,16 @@ static const struct hnames { | |
.name = "sha512", | ||
.hashAlg = ALG_SHA512_VALUE, | ||
}, { | ||
#endif | ||
#if ALG_SM3_256 | ||
.name = "sm3", | ||
.hashAlg = ALG_SM3_256_VALUE, | ||
}, { | ||
#endif | ||
.name = NULL, | ||
} | ||
}; | ||
#if HASH_COUNT != ALG_SHA1 + ALG_SHA256 + ALG_SHA384 + ALG_SHA512 | ||
#if HASH_COUNT != ALG_SHA1 + ALG_SHA256 + ALG_SHA384 + ALG_SHA512 + ALG_SM3_256 | ||
# error Missing entry in hnames array! | ||
#endif | ||
|
||
|
@@ -623,3 +628,28 @@ OpenSSLCryptRsaGenerateKey( | |
} | ||
|
||
#endif // USE_OPENSSL_FUNCTIONS_RSA | ||
#if ALG_SM3_256 | ||
int sm3_init(SM3_TPM_CTX *c) | ||
{ | ||
*c = EVP_MD_CTX_new(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is correct. It's probably better to initialize SM3_TPM_CTX ourselves here. |
||
if (*c == NULL) { | ||
return SM3_FAIL; | ||
} | ||
return EVP_DigestInit_ex(*c, EVP_sm3(), NULL); | ||
} | ||
int sm3_update(SM3_TPM_CTX *c, const void *data, size_t len) | ||
JerryDevis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return EVP_DigestUpdate(*c, data, len); | ||
} | ||
int sm3_final(unsigned char *md, SM3_TPM_CTX *c) | ||
{ | ||
uint32_t len = SM3_256_DIGEST_SIZE; | ||
int ret = EVP_DigestFinal_ex(*c, md, &len); | ||
if (ret != SM3_SUCCESS || len != SM3_256_DIGEST_SIZE) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty line after variables declaration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please just use '1' for OpenSSL function success checking, no SM3_SUCCESS. |
||
ret = SM3_FAIL; | ||
} | ||
EVP_MD_CTX_destroy(*c); | ||
*c = NULL; | ||
return ret; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return SM3_SUCCESS |
||
} | ||
#endif | ||
JerryDevis marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef SM3_HELPER_FP | ||
#define SM3_HELPER_FP | ||
#if ALG_SM3_256 | ||
#include <openssl/evp.h> | ||
|
||
typedef EVP_MD_CTX* SM3_TPM_CTX; | ||
#define SM3_SUCCESS 1 | ||
#define SM3_FAIL 0 | ||
# define SM3_DIGEST_LENGTH 32 | ||
# define SM3_WORD unsigned int | ||
|
||
# define SM3_CBLOCK 64 | ||
# define SM3_LBLOCK (SM3_CBLOCK/4) | ||
|
||
typedef struct SM3state_st { | ||
SM3_WORD A, B, C, D, E, F, G, H; | ||
SM3_WORD Nl, Nh; | ||
SM3_WORD data[SM3_LBLOCK]; | ||
unsigned int num; | ||
} SM3_CTX; | ||
int sm3_init(SM3_TPM_CTX *c); | ||
int sm3_update(SM3_TPM_CTX *c, const void *data, size_t len); | ||
int sm3_final(unsigned char *md, SM3_TPM_CTX *c); | ||
#endif | ||
#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Large parts of this file are already in TpmToOsslHash.h and shouldn't be duplicated. Please try to use that file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,25 +75,9 @@ | |
# endif // libtpms added end | ||
# undef ALG_SM3_256 | ||
# define ALG_SM3_256 ALG_NO | ||
# elif OPENSSL_VERSION_NUMBER >= 0x10200000L | ||
# include <openssl/sm3.h> | ||
# else | ||
// OpenSSL 1.1.1 keeps smX.h headers in the include/crypto directory, | ||
// and they do not get installed as part of the libssl package | ||
# define SM3_LBLOCK (64/4) | ||
|
||
# error Check support for this version of SM3 in OpenSSL (libtpms) | ||
typedef struct SM3state_st { | ||
unsigned int A, B, C, D, E, F, G, H; | ||
unsigned int Nl, Nh; | ||
unsigned int data[SM3_LBLOCK]; | ||
unsigned int num; | ||
} SM3_CTX; | ||
|
||
int sm3_init(SM3_CTX *c); | ||
int sm3_update(SM3_CTX *c, const void *data, size_t len); | ||
int sm3_final(unsigned char *md, SM3_CTX *c); | ||
# endif // OpenSSL < 1.2 | ||
#include "Sm3Helper_fp.h" | ||
#endif | ||
#endif // ALG_SM3_256 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you remove it from here? I'd rather not move this here to keep the differences to the reference code at a minimum. |
||
|
||
#include <openssl/ossl_typ.h> | ||
|
@@ -108,10 +92,7 @@ int sm3_final(unsigned char *md, SM3_CTX *c); | |
#define tpmHashStateSHA256_t SHA256_CTX | ||
#define tpmHashStateSHA384_t SHA512_CTX | ||
#define tpmHashStateSHA512_t SHA512_CTX | ||
#define tpmHashStateSM3_256_t SM3_CTX | ||
#if ALG_SM3_256 | ||
# error "The version of OpenSSL used by this code does not support SM3" | ||
#endif | ||
#define tpmHashStateSM3_256_t SM3_TPM_CTX | ||
/* The defines below are only needed when compiling CryptHash.c or CryptSmac.c. This isolation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this should stay here. |
||
is primarily to avoid name space collision. However, if there is a real collision, it will | ||
likely show up when the linker tries to put things together. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we shouldn't create a context here but fill the data just like we fill it with sha1 etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The risk with sm3 is that its data structure has always been opaque it seems. That leaves OpenSSL the opportunity to shuffle things around. They may not do that but the risk is there. In contrast to that we have the SHAT_CTX, SHA256_CTX, etc. that are available via sha.h, so there's less of a risk with them on that level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the OpenSSL 3.0 migration guide , users are strongly encouraged to update their code to use the high level APIs instead. Low level APIs have been deprecated in OpenSSL 3.0, and will most likely be removed in a future versions.
OpenSSL intentionally avoids the user to perceive the internal structure of XXX_CTX, we can only use its pointer. So we don't need to care about its structure.
In the long term, we need to switch to the "high level" APIs (such as the EVP APIs).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know... but it's not going to be that simple. See my comment over there: #215 (comment)