Reduce KCAPI ECC page memory allocation sizes.

This commit is contained in:
David Garske
2022-03-28 15:40:07 -07:00
parent 05f377011b
commit 1abcd4b035

View File

@@ -204,11 +204,10 @@ int KcapiEcc_SharedSecret(ecc_key* private_key, ecc_key* public_key, byte* out,
{ {
int ret = 0; int ret = 0;
word32 kcapiCurveId = 0; word32 kcapiCurveId = 0;
byte* buf_aligned = NULL;
byte* pub_aligned = NULL; byte* pub_aligned = NULL;
byte* out_aligned = NULL; byte* out_aligned = NULL;
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE); size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
byte* pub; byte* pub = NULL;
word32 keySz; word32 keySz;
if (private_key == NULL || private_key->dp == NULL || public_key == NULL) { if (private_key == NULL || private_key->dp == NULL || public_key == NULL) {
@@ -243,25 +242,32 @@ int KcapiEcc_SharedSecret(ecc_key* private_key, ecc_key* public_key, byte* out,
} }
} }
if (ret == 0) { if (ret == 0) {
/* setup aligned pointers */ if ((size_t)pub % pageSz != 0) {
if (((size_t)pub % pageSz != 0) || ret = posix_memalign((void*)&pub_aligned, pageSz, keySz * 2);
((size_t)out % pageSz != 0)) { if (ret == 0) {
ret = posix_memalign((void*)&buf_aligned, pageSz, pageSz * 2); XMEMCPY(pub_aligned, pub, keySz * 2);
if (ret < 0) { }
else {
ret = MEMORY_E; ret = MEMORY_E;
} }
} }
else {
pub_aligned = pub;
}
} }
if (ret == 0) { if (ret == 0) {
out_aligned = ((size_t)out % pageSz == 0) ? out : buf_aligned; if ((size_t)out % pageSz != 0) {
if ((size_t)pub % pageSz == 0) { ret = posix_memalign((void*)&out_aligned, pageSz, keySz * 2);
pub_aligned = (byte*)pub; if (ret != 0) {
ret = MEMORY_E;
}
} }
else { else {
pub_aligned = buf_aligned + pageSz; out_aligned = out;
XMEMCPY(pub_aligned, pub, keySz * 2); }
} }
if (ret == 0) {
ret = (int)kcapi_kpp_ssgen(private_key->handle, pub_aligned, ret = (int)kcapi_kpp_ssgen(private_key->handle, pub_aligned,
keySz * 2, out_aligned, keySz * 2, KCAPI_ACCESS_HEURISTIC); keySz * 2, out_aligned, keySz * 2, KCAPI_ACCESS_HEURISTIC);
if (ret >= 0) { if (ret >= 0) {
@@ -278,8 +284,11 @@ int KcapiEcc_SharedSecret(ecc_key* private_key, ecc_key* public_key, byte* out,
/* Using free as this is in an environment that will have it /* Using free as this is in an environment that will have it
* available along with posix_memalign. */ * available along with posix_memalign. */
if (buf_aligned != NULL) { if (pub_aligned != NULL && pub != pub_aligned) {
free(buf_aligned); free(pub_aligned);
}
if (out_aligned != NULL && out != out_aligned) {
free(out_aligned);
} }
return ret; return ret;
@@ -319,7 +328,6 @@ int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
word32 sigLen) word32 sigLen)
{ {
int ret = 0; int ret = 0;
byte* buf_aligned = NULL;
byte* hash_aligned = NULL; byte* hash_aligned = NULL;
byte* sig_aligned = NULL; byte* sig_aligned = NULL;
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE); size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
@@ -340,14 +348,7 @@ int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
ret = KcapiEcc_SetPrivKey(key); ret = KcapiEcc_SetPrivKey(key);
} }
} }
if (ret == 0) {
if (((size_t)sig % pageSz != 0) || ((size_t)hash % pageSz != 0)) {
ret = posix_memalign((void*)&buf_aligned, pageSz, pageSz * 2);
if (ret < 0) {
ret = MEMORY_E;
}
}
}
if (ret == 0) { if (ret == 0) {
/* make sure signature output is large enough */ /* make sure signature output is large enough */
keySz = key->dp->size; keySz = key->dp->size;
@@ -356,14 +357,31 @@ int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
} }
} }
if (ret == 0) { if (ret == 0) {
sig_aligned = ((size_t)sig % pageSz == 0) ? sig : buf_aligned; if ((size_t)sig % pageSz != 0) {
if ((size_t)hash % pageSz == 0) { ret = posix_memalign((void*)&sig_aligned, pageSz, keySz * 2);
hash_aligned = (byte*)hash; if (ret != 0) {
ret = MEMORY_E;
}
} }
else { else {
hash_aligned = buf_aligned + pageSz; sig_aligned = sig;
}
}
if (ret == 0) {
if ((size_t)hash % pageSz != 0) {
ret = posix_memalign((void*)&hash_aligned, pageSz, hashLen);
if (ret == 0) {
XMEMCPY(hash_aligned, hash, hashLen); XMEMCPY(hash_aligned, hash, hashLen);
} }
else {
ret = MEMORY_E;
}
}
else {
hash_aligned = (byte*)hash;
}
}
if (ret == 0) {
ret = (int)kcapi_akcipher_sign(key->handle, hash_aligned, hashLen, ret = (int)kcapi_akcipher_sign(key->handle, hash_aligned, hashLen,
sig_aligned, keySz*2, KCAPI_ACCESS_HEURISTIC); sig_aligned, keySz*2, KCAPI_ACCESS_HEURISTIC);
if (ret >= 0) { if (ret >= 0) {
@@ -376,8 +394,11 @@ int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
/* Using free as this is in an environment that will have it /* Using free as this is in an environment that will have it
* available along with posix_memalign. */ * available along with posix_memalign. */
if (buf_aligned != NULL) { if (sig_aligned != NULL && sig != sig_aligned) {
free(buf_aligned); free(sig_aligned);
}
if (hash_aligned != NULL && hash != hash_aligned) {
free(hash_aligned);
} }
if (handleInit) { if (handleInit) {
@@ -445,14 +466,14 @@ int KcapiEcc_Verify(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
} }
if (ret == 0) { if (ret == 0) {
ret = posix_memalign((void*)&sigHash_aligned, pageSz, sigLen + hashLen); ret = posix_memalign((void*)&sigHash_aligned, pageSz, sigLen + hashLen);
if (ret < 0) { if (ret != 0) {
ret = MEMORY_E; ret = MEMORY_E;
} }
} }
if (ret == 0) { if (ret == 0) {
keySz = key->dp->size; keySz = key->dp->size;
ret = posix_memalign((void*)&outbuf, pageSz, keySz * 2); ret = posix_memalign((void*)&outbuf, pageSz, keySz * 2);
if (ret < 0) { if (ret != 0) {
ret = MEMORY_E; ret = MEMORY_E;
} }
} }