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

Improve the key management in the session and optimizing the three-st… #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 46 additions & 34 deletions emu/src/emulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static int _encrypt_with_kek(int kek_index,
int out_len = 0, total_len = 0;
EVP_CIPHER_CTX *ctx = nullptr;

if ((rc = _get_kek(kek_index, kek_value) != 0)) {
if ((rc = _get_kek(kek_index, kek_value)) != 0) {
goto cleanup;
}

Expand Down Expand Up @@ -218,7 +218,7 @@ static int _decrypt_with_kek(int kek_index,
int out_len, total_len = 0;
EVP_CIPHER_CTX *ctx = nullptr;

if ((rc = _get_kek(kek_index, kek_value) != 0)) {
if ((rc = _get_kek(kek_index, kek_value)) != 0) {
goto cleanup;
}

Expand Down Expand Up @@ -912,24 +912,19 @@ int SDF_GenerateKeyWithKEK(void *hSessionHandle,
rc = SDR_KEYERR;
goto cleanup;
}
if (puiKeyLength == nullptr || *puiKeyLength < kc_len) {
if (puiKeyLength != nullptr) {
*puiKeyLength = kc_len;
}
if (pucKey == nullptr || puiKeyLength == nullptr || *puiKeyLength < kc_len
|| phKeyHandle == nullptr) {
rc = SDR_OUTARGERR;
goto cleanup;
}
if (pucKey != nullptr) {
memcpy(pucKey, key_cipher, kc_len);
*puiKeyLength = kc_len;
}
memcpy(pucKey, key_cipher, kc_len);
*puiKeyLength = kc_len;

key_ptr = std::make_shared<hsmc::emu::Session::Key>(
key_plain, kp_len, uiKEKIndex);
session->addKey(key_ptr);
if (phKeyHandle != nullptr) {
*phKeyHandle = key_ptr.get();
}
*phKeyHandle = key_ptr.get();

rc = SDR_OK;

cleanup:
Expand Down Expand Up @@ -978,13 +973,15 @@ int SDF_ImportKeyWithKEK(void *hSessionHandle,
goto cleanup;
}

if (phKeyHandle == nullptr) {
rc = SDR_OUTARGERR;
goto cleanup;
}
key_ptr = std::make_shared<hsmc::emu::Session::Key>(
key_plain, kp_len, uiKEKIndex);
session->addKey(key_ptr);

if (phKeyHandle != nullptr) {
*phKeyHandle = key_ptr.get();
}
*phKeyHandle = key_ptr.get();

rc = SDR_OK;

cleanup:
Expand All @@ -1007,13 +1004,13 @@ int SDF_ImportKey(void *hSessionHandle,
return SDR_OPENSESSION;
}

if (phKeyHandle == nullptr) {
return SDR_OUTARGERR;
}
auto key_ptr = std::make_shared<hsmc::emu::Session::Key>(
pucKey, uiKeyLength);
session->addKey(key_ptr);

if (phKeyHandle != nullptr) {
*phKeyHandle = key_ptr.get();
}
*phKeyHandle = key_ptr.get();

return SDR_OK;
}
Expand Down Expand Up @@ -2050,43 +2047,58 @@ int SDF_HashInit(void *hSessionHandle,
int SDF_HashUpdate(void *hSessionHandle,
unsigned char *pucData,
unsigned int uiDataLength) {
if (nullptr == pucData || uiDataLength <= 0) {
return SDR_INARGERR;
}
int rc = 0;

hsmc::emu::SessionPtr session;
if (!_query_session(hSessionHandle, &session)) {
return SDR_OPENSESSION;
rc = SDR_OPENSESSION;
goto cleanup;
}

if (!(SDF_UserData_t*)session->getUserdata()
|| nullptr == pucData || uiDataLength <= 0) {
rc = SDR_INARGERR;
goto cleanup;
}

if (EVP_DigestUpdate(((SDF_UserData_t*)session->getUserdata())->mdCtx,
pucData,
uiDataLength) != 1) {
return SDR_UNKNOWERR;
rc = SDR_UNKNOWERR;
}

return SDR_OK;
cleanup:
if (rc != 0) {
session->resetUserdata();
}

return rc;
}

int SDF_HashFinal(void *hSessionHandle,
unsigned char *pucHash,
unsigned int *puiHashLength) {
if (nullptr == pucHash) {
return SDR_INARGERR;
}
int rc = 0;

hsmc::emu::SessionPtr session;
if (!_query_session(hSessionHandle, &session)) {
return SDR_OPENSESSION;
rc = SDR_OPENSESSION;
goto cleanup;
}

if (!(SDF_UserData_t*)session->getUserdata() || nullptr == pucHash) {
rc = SDR_INARGERR;
goto cleanup;
}

if (EVP_DigestFinal(((SDF_UserData_t*)session->getUserdata())->mdCtx,
pucHash,
puiHashLength) != 1) {
return SDR_UNKNOWERR;
rc = SDR_UNKNOWERR;
}

return SDR_OK;
cleanup:
session->resetUserdata();
return rc;
}

int SDF_CreateFile(void *hSessionHandle,
Expand Down