Skip to content

Commit

Permalink
Merge pull request wolfSSL#78 from bigbrett/bugfix-hsmFreshenKey
Browse files Browse the repository at this point in the history
hsmFreshenKey bugfix
  • Loading branch information
billphipps authored and jefferyq2 committed Nov 10, 2024
1 parent 18dc976 commit e5f49db
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
26 changes: 21 additions & 5 deletions src/wh_server_keystore.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,33 @@ int hsmFreshenKey(whServerContext* server, whKeyId keyId, uint8_t** outBuf,
int ret = 0;
int foundIndex = -1;
int foundBigIndex = -1;
whNvmMetadata meta[1];
whNvmMetadata tmpMeta[1];

if ( (server == NULL) ||
WH_KEYID_ISERASED(keyId)) {
return WH_ERROR_BADARGS;
}

ret = _FindInCache(server, keyId, &foundIndex, &foundBigIndex, outBuf, outMeta);
if (ret != 0) {
/* Not in cache. Check if it is in the NVM */
ret = wh_Nvm_GetMetadata(server->nvm, keyId, meta);
ret = _FindInCache(server, keyId, &foundIndex, &foundBigIndex, outBuf,
outMeta);
if (ret != WH_ERROR_OK) {
/* Not in cache. Check if it is in NVM */
ret = wh_Nvm_GetMetadata(server->nvm, keyId, tmpMeta);
if (ret == WH_ERROR_OK) {
/* Key found in NVM, get a free cache slot */
ret = hsmCacheFindSlotAndZero(server, tmpMeta->len, outBuf,
outMeta);
if (ret == WH_ERROR_OK) {
/* Read the key from NVM into the cache slot */
ret = wh_Nvm_Read(server->nvm, keyId, 0, tmpMeta->len, *outBuf);
if (ret == WH_ERROR_OK) {
/* Copy the metadata to the cache slot if key read is
* successful*/
XMEMCPY((uint8_t*)*outMeta, (uint8_t*)tmpMeta,
sizeof(whNvmMetadata));
}
}
}
}
return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/whnvmtool/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
whnvmtool
whNvmImage.bin
whNvmImage.hex
test/test_whnvmtool
*.bin
*.hex
22 changes: 22 additions & 0 deletions tools/whnvmtool/test/test_whnvmtool.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
* Copyright (C) 2024 wolfSSL Inc.
*
* This file is part of wolfHSM.
*
* wolfHSM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfHSM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wolfHSM. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* tools/whnvmtool/test/test_whnvmtool.c
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
Expand Down
22 changes: 22 additions & 0 deletions tools/whnvmtool/whnvmtool.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
* Copyright (C) 2024 wolfSSL Inc.
*
* This file is part of wolfHSM.
*
* wolfHSM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfHSM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wolfHSM. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* tools/whnvmtool/whnvmtool.c
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down

0 comments on commit e5f49db

Please sign in to comment.