-
Notifications
You must be signed in to change notification settings - Fork 16
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
use key type and key user as part of the internal keyId #22
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d288352
use key type and key user as part of the internal keyId
jpbland1 c97f70a
update based on PR comments
jpbland1 133aa3c
rebase and update mutual exclusion between multiple
jpbland1 a547c03
fix instance of user with client_id
jpbland1 0936dea
add set key function for rsa
jpbland1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,17 +22,17 @@ | |
#include "wolfhsm/wh_server_crypto.h" | ||
|
||
#ifndef NO_RSA | ||
static int hsmCacheKeyRsa(whServerContext* server, RsaKey* key) | ||
static int hsmCacheKeyRsa(whServerContext* server, RsaKey* key, whKeyId* outId) | ||
{ | ||
int ret = 0; | ||
int slotIdx = 0; | ||
whKeyId keyId = 0; | ||
whKeyId keyId = WOLFHSM_KEYTYPE_CRYPTO; | ||
/* get a free slot */ | ||
ret = slotIdx = hsmCacheFindSlot(server); | ||
if (ret >= 0) { | ||
ret = keyId = hsmGetUniqueId(server); | ||
ret = hsmGetUniqueId(server, &keyId); | ||
} | ||
if (ret > 0 ) { | ||
if (ret == 0) { | ||
/* export key */ | ||
/* TODO: Fix wolfCrypto to allow KeyToDer when KEY_GEN is NOT set */ | ||
ret = wc_RsaKeyToDer(key, server->cache[slotIdx].buffer, | ||
|
@@ -45,7 +45,8 @@ static int hsmCacheKeyRsa(whServerContext* server, RsaKey* key) | |
server->cache[slotIdx].meta->id = keyId; | ||
server->cache[slotIdx].meta->len = ret; | ||
/* export keyId */ | ||
ret = keyId; | ||
*outId = keyId; | ||
ret = 0; | ||
} | ||
return ret; | ||
} | ||
|
@@ -56,6 +57,7 @@ static int hsmLoadKeyRsa(whServerContext* server, RsaKey* key, whKeyId keyId) | |
int slotIdx = 0; | ||
uint32_t idx = 0; | ||
uint32_t size; | ||
keyId |= (WOLFHSM_KEYTYPE_CRYPTO | (server->comm->client_id << 8)); | ||
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. Probably should be a macro or function as I suspect we'll need this a lot. |
||
/* freshen the key */ | ||
ret = slotIdx = hsmFreshenKey(server, keyId); | ||
/* decode the key */ | ||
|
@@ -69,19 +71,20 @@ static int hsmLoadKeyRsa(whServerContext* server, RsaKey* key, whKeyId keyId) | |
#endif /* !NO_RSA */ | ||
|
||
#ifdef HAVE_CURVE25519 | ||
static int hsmCacheKeyCurve25519(whServerContext* server, curve25519_key* key) | ||
static int hsmCacheKeyCurve25519(whServerContext* server, curve25519_key* key, | ||
whKeyId* outId) | ||
{ | ||
int ret; | ||
int slotIdx = 0; | ||
word32 privSz = CURVE25519_KEYSIZE; | ||
word32 pubSz = CURVE25519_KEYSIZE; | ||
whKeyId keyId = 0; | ||
whKeyId keyId = WOLFHSM_KEYTYPE_CRYPTO; | ||
/* get a free slot */ | ||
ret = slotIdx = hsmCacheFindSlot(server); | ||
if (ret >= 0) { | ||
ret = keyId = hsmGetUniqueId(server); | ||
ret = hsmGetUniqueId(server, &keyId); | ||
} | ||
if (ret > 0) { | ||
if (ret == 0) { | ||
/* export key */ | ||
ret = wc_curve25519_export_key_raw(key, | ||
server->cache[slotIdx].buffer + CURVE25519_KEYSIZE, &privSz, | ||
|
@@ -94,7 +97,8 @@ static int hsmCacheKeyCurve25519(whServerContext* server, curve25519_key* key) | |
server->cache[slotIdx].meta->id = keyId; | ||
server->cache[slotIdx].meta->len = CURVE25519_KEYSIZE * 2; | ||
/* export keyId */ | ||
ret = keyId; | ||
*outId = keyId; | ||
ret = 0; | ||
} | ||
return ret; | ||
} | ||
|
@@ -106,6 +110,7 @@ static int hsmLoadKeyCurve25519(whServerContext* server, curve25519_key* key, | |
int slotIdx = 0; | ||
uint32_t privSz = CURVE25519_KEYSIZE; | ||
uint32_t pubSz = CURVE25519_KEYSIZE; | ||
keyId |= WOLFHSM_KEYTYPE_CRYPTO; | ||
/* freshen the key */ | ||
ret = slotIdx = hsmFreshenKey(server, keyId); | ||
/* decode the key */ | ||
|
@@ -128,6 +133,7 @@ int wh_Server_HandleCryptoRequest(whServerContext* server, | |
int ret = 0; | ||
uint32_t field; | ||
uint8_t* in; | ||
whKeyId keyId; | ||
uint8_t* out; | ||
whPacket* packet = (whPacket*)data; | ||
#ifdef WOLFHSM_SYMMETRIC_INTERNAL | ||
|
@@ -156,12 +162,13 @@ int wh_Server_HandleCryptoRequest(whServerContext* server, | |
} | ||
/* cache the generated key, data will be blown away */ | ||
if (ret == 0) { | ||
ret = hsmCacheKeyRsa(server, server->crypto->rsa); | ||
ret = hsmCacheKeyRsa(server, server->crypto->rsa, &keyId); | ||
} | ||
wc_FreeRsaKey(server->crypto->rsa); | ||
if (ret > 0) { | ||
if (ret == 0) { | ||
/* set the assigned id */ | ||
packet->pkRsakgRes.keyId = ret; | ||
packet->pkRsakgRes.keyId = | ||
(keyId & ~WOLFHSM_KEYUSER_MASK); | ||
*size = WOLFHSM_PACKET_STUB_SIZE + | ||
sizeof(packet->pkRsakgRes); | ||
ret = 0; | ||
|
@@ -241,15 +248,16 @@ int wh_Server_HandleCryptoRequest(whServerContext* server, | |
/* cache the generated key */ | ||
if (ret == 0) { | ||
ret = hsmCacheKeyCurve25519(server, | ||
server->crypto->curve25519Private); | ||
server->crypto->curve25519Private, &keyId); | ||
} | ||
/* set the assigned id */ | ||
wc_curve25519_free(server->crypto->curve25519Private); | ||
if (ret > 0) { | ||
packet->pkCurve25519kgRes.keyId = ret; | ||
if (ret == 0) { | ||
/* strip client_id */ | ||
packet->pkCurve25519kgRes.keyId = | ||
(keyId & ~WOLFHSM_KEYUSER_MASK); | ||
*size = WOLFHSM_PACKET_STUB_SIZE + | ||
sizeof(packet->pkCurve25519kgRes); | ||
ret = 0; | ||
} | ||
else | ||
ret = BAD_FUNC_ARG; | ||
|
@@ -268,13 +276,17 @@ int wh_Server_HandleCryptoRequest(whServerContext* server, | |
if (ret == 0) { | ||
ret = hsmLoadKeyCurve25519(server, | ||
server->crypto->curve25519Private, | ||
packet->pkCurve25519Req.privateKeyId); | ||
MAKE_WOLFHSM_KEYID(WOLFHSM_KEYTYPE_CRYPTO, | ||
server->comm->client_id, | ||
packet->pkCurve25519Req.privateKeyId)); | ||
} | ||
/* load the public key */ | ||
if (ret == 0) { | ||
ret = hsmLoadKeyCurve25519(server, | ||
server->crypto->curve25519Public, | ||
packet->pkEcdhReq.publicKeyId); | ||
MAKE_WOLFHSM_KEYID(WOLFHSM_KEYTYPE_CRYPTO, | ||
server->comm->client_id, | ||
packet->pkCurve25519Req.publicKeyId)); | ||
} | ||
/* make shared secret */ | ||
if (ret == 0) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This will likely not set the entire devCtx value since whNvmId is probably a smaller size than void*. Recommend to use uintptr_t to let the compiler promote the unsigned integer up to the proper size.
Like: key->devCtx = (void*) ((uintptr_t)keyId);