Further KCAPI AES/ECC memory optimizations. Adds KCAPI_USE_XMALLOC option for AES and ECC to reduce page memory use in certain KCAPI cases that allow it.

This commit is contained in:
David Garske
2022-03-31 08:25:40 -07:00
parent 1de5165dcc
commit f8007b2d8f
2 changed files with 71 additions and 16 deletions

View File

@@ -236,7 +236,9 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
byte* data = NULL; byte* data = NULL;
word32 dataSz; word32 dataSz;
int inbuflen = 0, outbuflen = 0; int inbuflen = 0, outbuflen = 0;
#ifndef KCAPI_USE_XMALLOC
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE); size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
#endif
/* argument checks */ /* argument checks */
if (aes == NULL || authTagSz > AES_BLOCK_SIZE) { if (aes == NULL || authTagSz > AES_BLOCK_SIZE) {
@@ -267,11 +269,17 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
outbuflen = (int)kcapi_aead_outbuflen_enc(aes->handle, sz, authInSz, outbuflen = (int)kcapi_aead_outbuflen_enc(aes->handle, sz, authInSz,
authTagSz); authTagSz);
dataSz = (inbuflen > outbuflen) ? inbuflen : outbuflen; dataSz = (inbuflen > outbuflen) ? inbuflen : outbuflen;
#ifdef KCAPI_USE_XMALLOC
data = (byte *)XMALLOC(dataSz, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
if (data == NULL) {
ret = MEMORY_E;
}
#else
ret = posix_memalign((void*)&data, pageSz, dataSz); ret = posix_memalign((void*)&data, pageSz, dataSz);
if (ret < 0) { if (ret < 0) {
ret = MEMORY_E; ret = MEMORY_E;
} }
#endif
} }
if (ret >= 0) { if (ret >= 0) {
@@ -312,10 +320,12 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
XMEMCPY(authTag, data + authInSz + sz, authTagSz); XMEMCPY(authTag, data + authInSz + sz, authTagSz);
} }
/* Using free as this is in an environment that will have it
* available along with posix_memalign. */
if (data != NULL) { if (data != NULL) {
#ifdef KCAPI_USE_XMALLOC
XFREE(data, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
#else
free(data); free(data);
#endif
} }
if (aes != NULL && aes->handle != NULL) { if (aes != NULL && aes->handle != NULL) {
kcapi_aead_destroy(aes->handle); kcapi_aead_destroy(aes->handle);
@@ -337,7 +347,9 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
byte* data = NULL; byte* data = NULL;
word32 dataSz; word32 dataSz;
int inbuflen = 0, outbuflen = 0; int inbuflen = 0, outbuflen = 0;
#ifndef KCAPI_USE_XMALLOC
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE); size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
#endif
/* argument checks */ /* argument checks */
if (aes == NULL || (sz != 0 && (in == NULL || out == NULL)) || if (aes == NULL || (sz != 0 && (in == NULL || out == NULL)) ||
@@ -369,11 +381,17 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
outbuflen = (int)kcapi_aead_outbuflen_dec(aes->handle, sz, authInSz, outbuflen = (int)kcapi_aead_outbuflen_dec(aes->handle, sz, authInSz,
authTagSz); authTagSz);
dataSz = (inbuflen > outbuflen) ? inbuflen : outbuflen; dataSz = (inbuflen > outbuflen) ? inbuflen : outbuflen;
#ifdef KCAPI_USE_XMALLOC
data = (byte*)XMALLOC(dataSz, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
if (data == NULL) {
ret = MEMORY_E;
}
#else
ret = posix_memalign((void*)&data, pageSz, dataSz); ret = posix_memalign((void*)&data, pageSz, dataSz);
if (ret < 0) { if (ret < 0) {
ret = MEMORY_E; ret = MEMORY_E;
} }
#endif
} }
if (ret >= 0) { if (ret >= 0) {
@@ -413,10 +431,12 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
XMEMCPY(out, data + authInSz, sz); XMEMCPY(out, data + authInSz, sz);
} }
/* Using free as this is in an environment that will have it
* available along with posix_memalign. */
if (data != NULL) { if (data != NULL) {
#ifdef KCAPI_USE_XMALLOC
XFREE(data, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
#else
free(data); free(data);
#endif
} }
if (aes != NULL && aes->handle != NULL) { if (aes != NULL && aes->handle != NULL) {
kcapi_aead_destroy(aes->handle); kcapi_aead_destroy(aes->handle);

View File

@@ -205,9 +205,11 @@ 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* buf_aligned = NULL;
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
byte* pub = NULL; byte* pub = NULL;
word32 keySz; word32 keySz;
#ifndef KCAPI_USE_XMALLOC
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
#endif
if (private_key == NULL || private_key->dp == NULL || public_key == NULL) { if (private_key == NULL || private_key->dp == NULL || public_key == NULL) {
ret = BAD_FUNC_ARG; ret = BAD_FUNC_ARG;
@@ -241,10 +243,18 @@ int KcapiEcc_SharedSecret(ecc_key* private_key, ecc_key* public_key, byte* out,
} }
} }
if (ret == 0) { if (ret == 0) {
#ifdef KCAPI_USE_XMALLOC
buf_aligned = (byte*)XMALLOC(keySz * 2, private_key->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (buf_aligned == NULL) {
ret = MEMORY_E;
}
#else
ret = posix_memalign((void*)&buf_aligned, pageSz, keySz * 2); ret = posix_memalign((void*)&buf_aligned, pageSz, keySz * 2);
if (ret != 0) { if (ret != 0) {
ret = MEMORY_E; ret = MEMORY_E;
} }
#endif
} }
if (ret == 0) { if (ret == 0) {
XMEMCPY(buf_aligned, pub, keySz * 2); XMEMCPY(buf_aligned, pub, keySz * 2);
@@ -259,10 +269,12 @@ 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
* available along with posix_memalign. */
if (buf_aligned != NULL) { if (buf_aligned != NULL) {
#ifdef KCAPI_USE_XMALLOC
XFREE(buf_aligned, private_key->heap, DYNAMIC_TYPE_TMP_BUFFER);
#else
free(buf_aligned); free(buf_aligned);
#endif
} }
return ret; return ret;
@@ -303,10 +315,12 @@ int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
{ {
int ret = 0; int ret = 0;
byte* buf_aligned = NULL; byte* buf_aligned = NULL;
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
int handleInit = 0; int handleInit = 0;
word32 keySz; word32 keySz;
word32 maxBufSz; word32 maxBufSz;
#ifndef KCAPI_USE_XMALLOC
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
#endif
if (key == NULL || key->dp == NULL) { if (key == NULL || key->dp == NULL) {
ret = BAD_FUNC_ARG; ret = BAD_FUNC_ARG;
@@ -332,10 +346,18 @@ int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
} }
if (ret == 0) { if (ret == 0) {
maxBufSz = (hashLen > keySz * 2) ? hashLen : (keySz * 2); maxBufSz = (hashLen > keySz * 2) ? hashLen : (keySz * 2);
#ifdef KCAPI_USE_XMALLOC
buf_aligned = (unsigned char*)XMALLOC(maxBufSz, key->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (buf_aligned == NULL) {
ret = MEMORY_E;
}
#else
ret = posix_memalign((void*)&buf_aligned, pageSz, maxBufSz); ret = posix_memalign((void*)&buf_aligned, pageSz, maxBufSz);
if (ret != 0) { if (ret != 0) {
ret = MEMORY_E; ret = MEMORY_E;
} }
#endif
} }
if (ret == 0) { if (ret == 0) {
XMEMCPY(buf_aligned, hash, hashLen); XMEMCPY(buf_aligned, hash, hashLen);
@@ -348,10 +370,12 @@ 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
* available along with posix_memalign. */
if (buf_aligned != NULL) { if (buf_aligned != NULL) {
#ifdef KCAPI_USE_XMALLOC
XFREE(buf_aligned, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
#else
free(buf_aligned); free(buf_aligned);
#endif
} }
if (handleInit) { if (handleInit) {
@@ -398,9 +422,11 @@ int KcapiEcc_Verify(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
{ {
int ret = 0; int ret = 0;
byte* buf_aligned = NULL; byte* buf_aligned = NULL;
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
int handleInit = 0; int handleInit = 0;
word32 keySz = 0; word32 keySz = 0;
#ifndef KCAPI_USE_XMALLOC
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
#endif
if (key == NULL || key->dp == NULL) { if (key == NULL || key->dp == NULL) {
ret = BAD_FUNC_ARG; ret = BAD_FUNC_ARG;
@@ -418,11 +444,18 @@ int KcapiEcc_Verify(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
} }
if (ret == 0) { if (ret == 0) {
keySz = key->dp->size; keySz = key->dp->size;
#ifdef KCAPI_USE_XMALLOC
buf_aligned = (byte*)XMALLOC(sigLen + hashLen, key->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (buf_aligned == NULL) {
ret = MEMORY_E;
}
#else
ret = posix_memalign((void*)&buf_aligned, pageSz, sigLen + hashLen); ret = posix_memalign((void*)&buf_aligned, pageSz, sigLen + hashLen);
if (ret != 0) { if (ret != 0) {
ret = MEMORY_E; ret = MEMORY_E;
} }
#endif
} }
if (ret == 0) { if (ret == 0) {
XMEMCPY(buf_aligned, sig, sigLen); XMEMCPY(buf_aligned, sig, sigLen);
@@ -437,10 +470,12 @@ int KcapiEcc_Verify(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
} }
} }
/* Using free as this is in an environment that will have it
* available along with posix_memalign. */
if (buf_aligned != NULL) { if (buf_aligned != NULL) {
#ifdef KCAPI_USE_XMALLOC
XFREE(buf_aligned, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
#else
free(buf_aligned); free(buf_aligned);
#endif
} }
if (handleInit) { if (handleInit) {