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

Add TEE_ALG_RSASSA_PKCS1_V1_5 #2524

Merged
merged 5 commits into from Nov 15, 2018
Merged

Conversation

EDVTAZ
Copy link
Contributor

@EDVTAZ EDVTAZ commented Sep 6, 2018

I've been working with @etienne-lms, to add asymmetrical algorithms to the
SKS proposal. While testing with OpenSSH, I came across the usage of the
CKM_RSA_PKCS mechanism with the C_Sign function, which I couldn't implement
with the current cryptographic TEE API.

This extension allows signing and verifying raw/pre-hashed data,
without specifying a hash function. It is needed for implementing
the pkcs#11 CKM_RSA_PKCS mechanism for signing and verifying in SKS.
This patch adds the extension to the API and updates parts of
libtomcrypt to a newer version, to support this algorithm.

Signed-off-by: Gabor Szekely szvgabor@gmail.com

@jenswi-linaro
Copy link
Contributor

This PR consists of two parts, one update of LTC and one update of OP-TEE.
Please split into two commits.
How about upstreaming to LTC, is that started?
Do you have a link to a PR?

@EDVTAZ
Copy link
Contributor Author

EDVTAZ commented Sep 6, 2018

Ok, I will split it into two commits.
Regarding libtomcrypt, I just took their latest version, and copied the necessary files into optee. The only modification I made had to do with some optimisation that was added some time back in optee to ltc. I modified the copied files, so they were in line with those.

@jenswi-linaro
Copy link
Contributor

OK, good. :-)

@jforissier
Copy link
Contributor

Yes good indeed, it means we should take as a separate (and prerequisite) task to update LTC to the latest version...

@EDVTAZ EDVTAZ force-pushed the RSA_extension branch 2 times, most recently from ab2c007 to e9cbd3c Compare September 7, 2018 08:15
@EDVTAZ
Copy link
Contributor Author

EDVTAZ commented Sep 7, 2018

Yes, it would be nice to update LTC altogether (judging by the timestamps in some of the files it's as old as 2007, and there have been releases since then in LTC), but as far as I can tell, that would be a very lengthy process, given how many modifications were made to it over the time in this repository compared to the original one.

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

rephrase the commit. something like :

libtomcrypt: port LTC_PKCS_1_V1_5_NA1 from ltc v1.18.2

This change ports LTC_PKCS_1_V1_5_NA1 from libtomcrypt v1.18.2. This scheme is used for...

Chnage in ltc_provider could go in the 2nd commit.

Remove core/lib/libtomcrypt/src/pk/rsa/rsa_sign_hash.c.bak from the change.

@@ -13,6 +13,9 @@
#include <string_ext.h>
#include <string.h>
#include <tee_api_types.h>
#if defined(CFG_SECURE_KEY_SERVICES)
Copy link
Contributor

Choose a reason for hiding this comment

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

can remove the condition

@@ -270,6 +273,9 @@ static TEE_Result tee_algo_to_ltc_hashindex(uint32_t algo, int *ltc_hashindex)
case TEE_ALG_HMAC_SHA512:
*ltc_hashindex = find_hash("sha512");
break;
#endif
#if defined(CFG_SECURE_KEY_SERVICES)
Copy link
Contributor

Choose a reason for hiding this comment

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

can remove the condition, here and below.

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

Rephrase the commit comment. Something like:

utee: support  prehash RSA sign and verify

Add TEE Core Internal API extension TEE_ALG_RSASSA_PKCS1_V1_5 to sing/verify pre-hashed ... This rely on libtomcryp LTC_PKCS_1_V1_5_NA1 ...

@@ -19,7 +19,7 @@
#include <utee_defines.h>
#include <util.h>
#if defined(CFG_CRYPTO_HKDF) || defined(CFG_CRYPTO_CONCAT_KDF) || \
defined(CFG_CRYPTO_PBKDF2)
defined(CFG_CRYPTO_PBKDF2) || defined(CFG_SECURE_KEY_STORAGE)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer another CFG_. Maybe introduce CFG_CRYPTO_RSASSA to default enable in the related make script.
I even wonder if we could not simply drop the condition and always include tee_api_defines_extensions.h.

@@ -3277,6 +3277,9 @@ TEE_Result syscall_asymm_operate(unsigned long state,
}
break;

#if defined(CFG_SECURE_KEY_SERVICES)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer another CFG_. Maybe introduce CFG_CRYPTO_RSASSA to default enable in the related make script.

@@ -0,0 +1,162 @@
// SPDX-License-Identifier: BSD-2-Clause
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this file

@etienne-lms
Copy link
Contributor

Hi @EDVTAZ and thank for this PR. I think it is clean. Please try to format the commit comments as those already merged in the optee_os git tree. It helps maintenance.

@jforissier I have had a quick look a ltc update. Most changes are ok but few descriptors which requires updates in the tee_ltc_provider.c. Maybe there are other things to take care of..
The proposed changes here only syncs op-tee with libtomcrypt for this very LTC_PKCS_1_V1_5_NA1 support. Would it be ok to merge it before a libtomcrypt full resync?

@jforissier
Copy link
Contributor

judging by the timestamps in some of the files it's as old as 2007

No, the last global sync was against a50cb361 2 years ago, and we have picked a few upstream commits since then.

@jforissier
Copy link
Contributor

jforissier commented Sep 7, 2018

The first commit should be split further. It should contain only the LTC changes (no change to any OP-TEE files). From what I can tell, it is exactly a cherry-pick of upstream commit aa4bae5ae9a2 ("add option to do PKCS#1 v1.5 EMSA without ASN.1 around hash"), correct? If so, please mention this commit in the commit comment.

The IBART error (4003.18) has to be investigated, too.

@jforissier
Copy link
Contributor

@etienne-lms yes, we can merge this feature without a full LTC sync.

@jforissier
Copy link
Contributor

...and then we also want:

  • a new test added to xtest
  • some documentation in documentation/extensions.

@EDVTAZ
Copy link
Contributor Author

EDVTAZ commented Sep 10, 2018

I removed the accidentally committed backup file, and some of the #ifs. Also renamed CFG_SECURE_KEY_SERVICES to CFG_CRYPTO_RSASSA_NA1. I split the first commit into two and tried to rewrite the commit messages to be better.

Tomorrow I will investigate the IBART error and start working on an xtest test case and some documentation.

@EDVTAZ
Copy link
Contributor Author

EDVTAZ commented Sep 11, 2018

IBART error was present, because I was working with an older version of OPTEE, that didn't have this commit: e770203 . I rebased my branch, so the tests should be good now.

EDVTAZ added a commit to EDVTAZ/optee_test that referenced this pull request Sep 14, 2018
This test checks the TEE_ALG_RSASSA_PKCS1_V1_5 extension, that allows to
sign data without including the hash OID in the signature. The test
vector data is copied from one of the already present vectors, with the
hash OID manually removed in the signature.

Link: <OP-TEE/optee_os#2524>

Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
EDVTAZ added a commit to EDVTAZ/optee_test that referenced this pull request Sep 14, 2018
This test checks the TEE_ALG_RSASSA_PKCS1_V1_5 extension, that allows to
sign data without including the hash OID in the signature. The test
vector data is copied from one of the already present vectors, with the
hash OID manually removed in the signature.

Link: <OP-TEE/optee_os#2524>

Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
EDVTAZ added a commit to EDVTAZ/optee_test that referenced this pull request Sep 14, 2018
This test checks the TEE_ALG_RSASSA_PKCS1_V1_5 extension, that allows to
sign data without including the hash OID in the signature. The test
vector data is copied from one of the already present vectors, with the
hash OID manually removed in the signature.

Link: <OP-TEE/optee_os#2524>

Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
@EDVTAZ
Copy link
Contributor Author

EDVTAZ commented Sep 14, 2018

I created the xtest test case, see: OP-TEE/optee_test#309

I also found some some errors in my code while testing, which I corrected in 06f2d21

@EDVTAZ
Copy link
Contributor Author

EDVTAZ commented Sep 16, 2018

I have created the documentation for the extension, please let me know if it's all right.

* PKCS#1 v1.5 RSASSA pre-hashed sign/verify
*/

#define TEE_ALG_RSASSA_PKCS1_V1_5 0x70007830
Copy link
Contributor

Choose a reason for hiding this comment

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

This defines a value in a range that as far as I can tell is reserved for future specifications.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jenswi-linaro indeed this is a problem. The future Internal Core API specification v1.2 (currently numbered v1.1.2.51) gets rid of the structure of algorithm identifiers (table 6-12) and defines a whole range for implementation-defined identifiers (0xF0000000-0xF0FFFFFF). A value in that range would be an option I guess, but that certainly means more invasive changes to the OP-TEE code which currently assumes the structure defined in the current spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jenswi-linaro @jforissier then would 0xF0000830 be all right for this define?

Copy link
Contributor

Choose a reason for hiding this comment

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

@EDVTAZ I think it should be OK.

Copy link
Contributor

Choose a reason for hiding this comment

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

@EDVTAZ ...but 0xF0000001 would be even better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried to make this work, but a lot of macros that are used all over the place rely on the identifier's value having the above mentioned structure. Most of the time the macros are called in switch statements, so I can't insert cases for an identifier that doesn't follow the structure. I've inserted if-else statements before switch statements to get around this, but it seems I'm missing something somewhere, because it fails to work. The resulting code is also ugly and hard to read.

Could we maybe find a value that works with the current macros, or just keep the current value until the changes @jforissier mentioned get implemented?

Copy link
Contributor

Choose a reason for hiding this comment

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

@jforissier @EDVTAZ , just a gentle reminder about this, since it seems like the discussion suddenly stopped (a since I also think it is a useful addition).

EDVTAZ added a commit to EDVTAZ/optee_test that referenced this pull request Sep 27, 2018
This test checks the TEE_ALG_RSASSA_PKCS1_V1_5 extension, that allows to
sign data without including the hash OID in the signature. The test
vector data is copied from one of the already present vectors, with the
hash OID manually removed in the signature.

Link: <OP-TEE/optee_os#2524>

Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
@EDVTAZ
Copy link
Contributor Author

EDVTAZ commented Nov 1, 2018

Sorry this took so long, I've had a lot to do in the past month. The above two commits change the TEE_ALG_RSASSA_PKCS1_V1_5 identifier from 0x70007830 to 0xF0000830, so it is in the range of implementation-defined identifiers. This is the cleanest way I could come up with, to solve this problem without changing how TEE_ALG_GET_CLASS works. Please let me know if it's ok like this and if yes I will adjust the documentation to hold the right value as well.

Copy link
Contributor

@jenswi-linaro jenswi-linaro left a comment

Choose a reason for hiding this comment

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

This seems a good way to approach the algorithm class problem.

return CRYPT_PK_INVALID_PADDING;
}

if (padding == LTC_PKCS_1_PSS) {
if (padding != LTC_PKCS_1_V1_5_NA1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

So now we're testing the hash for LTC_PKCS_1_V1_5 too. A bugfix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes were taken from the libtomcrypt repo: libtom/libtomcrypt@aa4bae5#diff-f8abc0f8fcc24d9959cf4792e56ccce5L58 I think it is intentional, although it is not mentioned in the commit message.

/* test OID */
if ((reallen == outlen) &&
(digestinfo[0].size == hash_descriptor[hash_idx]->OIDlen) &&
(XMEMCMP(digestinfo[0].data, hash_descriptor[hash_idx]->OID, sizeof(unsigned long) * hash_descriptor[hash_idx]->OIDlen) == 0) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change from XMEM_NEQ() to XMEMCMP() (same for the other lines below)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -2062,6 +2069,9 @@ TEE_Result syscall_cryp_state_alloc(unsigned long algo, unsigned long mode,
break;
case TEE_OPERATION_ASYMMETRIC_CIPHER:
case TEE_OPERATION_ASYMMETRIC_SIGNATURE:
#ifdef CFG_CRYPTO_RSASSA_NA1
RSASSA_NA1:
Copy link
Contributor

Choose a reason for hiding this comment

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

Labels should use lower case letters.
I think it's possible to the the __maybe_unused attribute on labels too, if that works please remove the ifdef here.

if (data_len != hash_size) {
res = TEE_ERROR_BAD_PARAMETERS;
break;
#ifdef CFG_CRYPTO_RSASSA_NA1
Copy link
Contributor

Choose a reason for hiding this comment

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

This ifdef and the one below clutters the code. It seems it would be harmless to remove them. If you agree please remove them, else we'll need to find some other way to avoid having them here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I'm not mistaken, TEE_ALG_RSASSA_PKCS1_V1_5 is included here in this file, so to remove the ifdef in question, we would need to remove all the ifdefs that guard the include that I linked.

Copy link
Contributor

Choose a reason for hiding this comment

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

This ifdeffing isn't OK so it will need to be fixed. I'm OK with unconditionally including tee_api_defines_extensions.h, another option is to put in a helper function cs->algo != TEE_ALG_RSASSA_PKCS1_V1_5 that would return true if CFG_CRYPTO_RSASSA_NA1 isn't defined. However, I'd prefer the less complicated solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the guards

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

hello @EDVTAZ,
Could you rebase your commits on latest HEAD of the master branch?
Also, since your current patch serie is a collection of changes and late fixup, could you squash the fixup commits into their related change commit? to make a well formed patch serie.

@EDVTAZ
Copy link
Contributor Author

EDVTAZ commented Nov 11, 2018

I've rebased and squashed the commits that belong together, I hope it's all right like this.
I can't tell why the travis-ci fails, xtest passes locally.

@jbech-linaro
Copy link
Contributor

@EDVTAZ , Travis failed because it hit the time limit. I don't think we have to bother about that. IBART passes which indicates that things are fine.

@jenswi-linaro
Copy link
Contributor

For "libtomcrypt: port LTC_PKCS_1_V1_5_NA1 from ltc v1.18.2" please apply:
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
For the others:
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>

This change ports LTC_PKCS_1_V1_5_NA1 from libtomcrypt v1.18.2. This
scheme allows to do PKCS#1 v1.5 EMSA without ASN.1 around the hash. It
is used for implementing the pkcs#11 CKM_RSA_PKCS mechanism for signing
and verifying in SKS. This commit is a cherry pick of aa4bae5ae9a2 from
the libtomcrypt repository.

Link: <libtom/libtomcrypt@aa4bae5ae9a2>

Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
This change integrates the LTC_PKCS_1_V1_5_NA1 into OPTEE as an
extension as TEE_ALG_RSASSA_PKCS1_V1_5. This scheme allows to do
PKCS#1 v1.5 EMSA without ASN.1 around the hash. It is used for
implementing the pkcs#11 CKM_RSA_PKCS mechanism for signing and
verifying in SKS.

Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
Add TEE Core Internal API extension TEE_ALG_RSASSA_PKCS1_V1_5 to
sign/verify pre-hashed PKCS#1 v1.5 EMSA without ASN.1 around the hash.
This relies on libtomcrypt LTC_PKCS_1_V1_5_NA1. The extension can be
turned on with CFG_CRYPTO_RSASSA_NA1.

Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
Add documentation for the the TEE_ALG_RSASSA_V1_5 extension.

Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
@etienne-lms
Copy link
Contributor

Look ok to me, just a late comment:
CFG_CRYPTO_RSASSA_NA1 is introduced in 2nd change ("core: crypto: add TEE_ALG_RSASSA_PKCS1_V1_5") but described in the comment of the 3rd change ("utee: support prehashed RSA sign/ver without ASN.1"). This is not a big issue.
Yet, a comment related to CFG_CRYPTO_RSASSA_NA1: can we add a change to default enable the feature (i.e CFG_CRYPTO_RSASSA_NA1=y in mk/config.mk). I see no reason to no support this extension in the default configuration.

EDVTAZ added a commit to EDVTAZ/optee_test that referenced this pull request Nov 13, 2018
This test checks the TEE_ALG_RSASSA_PKCS1_V1_5 extension, that allows to
sign data without including the hash OID in the signature. The test
vector data is copied from one of the already present vectors, with the
hash OID manually removed in the signature.

Link: <OP-TEE/optee_os#2524>

Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org>
Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
@EDVTAZ
Copy link
Contributor Author

EDVTAZ commented Nov 13, 2018

I have added CFG_CRYPTO_RSASSA_NA1 to the default config.

@jforissier
Copy link
Contributor

For "mk/config.mk: default enable CFG_CRYPTO_RSASSA_NA1":
Acked-by: Jerome Forissier <jerome.forissier@linaro.org>

Enable the TEE_ALG_RSASSA_PKCS1_V1_5 extension by default.

Acked-by: Jerome Forissier <jerome.forissier@linaro.org>
Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
@jforissier jforissier merged commit 017dfaf into OP-TEE:master Nov 15, 2018
jforissier pushed a commit to OP-TEE/optee_test that referenced this pull request Nov 22, 2018
This test checks the TEE_ALG_RSASSA_PKCS1_V1_5 extension, that allows to
sign data without including the hash OID in the signature. The test
vector data is copied from one of the already present vectors, with the
hash OID manually removed in the signature.

Link: <OP-TEE/optee_os#2524>

Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org>
Signed-off-by: Gabor Szekely <szvgabor@gmail.com>
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.

None yet

5 participants