Skip to content
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

Closed
wants to merge 4 commits into from
Closed

Conversation

JerryDevis
Copy link
Contributor

we add a compiling macro(i.e. --with-openssl-smX) to control SM3 and SM4, default not support.

JerryDevis added 3 commits October 11, 2021 14:37
Signed-off-by: JerryDevis <seclab@huawei.com>
Signed-off-by: JerryDevis <seclab@huawei.com>
Signed-off-by: JerryDevis <seclab@huawei.com>
@coveralls
Copy link

coveralls commented Oct 11, 2021

Pull Request Test Coverage Report for Build 2312

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.008%) to 77.466%

Totals Coverage Status
Change from base Build 2298: 0.008%
Covered Lines: 29207
Relevant Lines: 37703

💛 - Coveralls

@JerryDevis
Copy link
Contributor Author

We do need to add a CI machine that supports SM3/SM4, like ubuntu 20.04 as you mentioned.

Copy link
Owner

@stefanberger stefanberger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only looked at Sm3 so far ...

src/tpm2/crypto/openssl/Helpers.c Show resolved Hide resolved
src/tpm2/crypto/openssl/Helpers.c Show resolved Hide resolved
{
uint32_t len = SM3_256_DIGEST_SIZE;
int ret = EVP_DigestFinal_ex(*c, md, &len);
if (ret != SM3_SUCCESS || len != SM3_256_DIGEST_SIZE) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty line after variables declaration.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just use '1' for OpenSSL function success checking, no SM3_SUCCESS.

NV_HEADER hdr;
SM3_CTX *sm3_ctx = NULL;

(*data) = EVP_MD_CTX_new();
Copy link
Owner

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.

Copy link
Owner

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.

Copy link
Contributor Author

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).

Copy link
Owner

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)

}
EVP_MD_CTX_destroy(*c);
*c = NULL;
return ret;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return SM3_SUCCESS

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
Copy link
Owner

Choose a reason for hiding this comment

The 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.

int sm3_final(unsigned char *md, SM3_CTX *c);
# endif // OpenSSL < 1.2
#include "Sm3Helper_fp.h"
#endif
#endif // ALG_SM3_256
Copy link
Owner

Choose a reason for hiding this comment

The 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.

#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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this should stay here.

#if ALG_SM3_256
int sm3_init(SM3_TPM_CTX *c)
{
*c = EVP_MD_CTX_new();
Copy link
Owner

Choose a reason for hiding this comment

The 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.

@stefanberger
Copy link
Owner

I don't think that SM3 will be an easy fit with the code considering the level at which OpenSSL makes it accessible via the EVP API versus the lower level API in use by the SHA functions. My suggestion is to split SM3 and SM4 into two independent PRs because SM4 seems to work like the recently enabled Camellia does but has that other issue that not all distros are enabling it, which in turn breaks the single-TpmProfile.h-for-all-distros .

Signed-off-by: JerryDevis <seclab@huawei.com>
@JerryDevis JerryDevis marked this pull request as draft October 11, 2021 15:07
@JerryDevis
Copy link
Contributor Author

The support of SM3 also bothered me for a long time, and it was hard to find an elegant way to implement it.
Not only is it not supported by many distributions, but its interface also differs from other hash algorithms.
So for SM3, do you have any good suggestions? @stefanberger

@stefanberger
Copy link
Owner

So for SM3, do you have any good suggestions? @stefanberger

The only answer I have is to have our own sm3 implementation ... not that I like the answer but I don't know of any other way to do this. But this is also the only way to support the SHA 1&2 families in the long-term if OpenSSL was to ever get rid of the context structures but then for SHA 3 the answer is the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants