mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2025-08-18 01:15:46 +00:00
Further optimization to avoid needless calls to EVP_aes_128_gcm() and EVP_aes_128_cbc()
This commit is contained in:
parent
8354c403f4
commit
13041e0323
@ -103,31 +103,16 @@ bool PltEncryptMessage(PPLT_CRYPTO_CONTEXT ctx, int algorithm, int flags,
|
|||||||
*outputDataLength = outLength;
|
*outputDataLength = outLength;
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
const EVP_CIPHER* cipher;
|
LC_ASSERT(keyLength == 16);
|
||||||
|
|
||||||
switch (algorithm) {
|
|
||||||
case ALGORITHM_AES_CBC:
|
|
||||||
LC_ASSERT(keyLength == 16);
|
|
||||||
LC_ASSERT(tag == NULL);
|
|
||||||
LC_ASSERT(tagLength == 0);
|
|
||||||
cipher = EVP_aes_128_cbc();
|
|
||||||
break;
|
|
||||||
case ALGORITHM_AES_GCM:
|
|
||||||
LC_ASSERT(keyLength == 16);
|
|
||||||
LC_ASSERT(tag != NULL);
|
|
||||||
LC_ASSERT(tagLength > 0);
|
|
||||||
cipher = EVP_aes_128_gcm();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
LC_ASSERT(false);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (algorithm == ALGORITHM_AES_GCM) {
|
if (algorithm == ALGORITHM_AES_GCM) {
|
||||||
|
LC_ASSERT(tag != NULL);
|
||||||
|
LC_ASSERT(tagLength > 0);
|
||||||
|
|
||||||
if (!ctx->initialized || (flags & CIPHER_FLAG_RESET_IV)) {
|
if (!ctx->initialized || (flags & CIPHER_FLAG_RESET_IV)) {
|
||||||
// Perform a full initialization. This codepath also allows
|
// Perform a full initialization. This codepath also allows
|
||||||
// us to change the IV length if required.
|
// us to change the IV length if required.
|
||||||
if (EVP_EncryptInit_ex(ctx->ctx, cipher, NULL, NULL, NULL) != 1) {
|
if (EVP_EncryptInit_ex(ctx->ctx, EVP_aes_128_gcm(), NULL, NULL, NULL) != 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,10 +134,13 @@ bool PltEncryptMessage(PPLT_CRYPTO_CONTEXT ctx, int algorithm, int flags,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else if (algorithm == ALGORITHM_AES_CBC) {
|
||||||
|
LC_ASSERT(tag == NULL);
|
||||||
|
LC_ASSERT(tagLength == 0);
|
||||||
|
|
||||||
if (!ctx->initialized) {
|
if (!ctx->initialized) {
|
||||||
// Perform a full initialization
|
// Perform a full initialization
|
||||||
if (EVP_EncryptInit_ex(ctx->ctx, cipher, NULL, key, iv) != 1) {
|
if (EVP_EncryptInit_ex(ctx->ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,6 +158,10 @@ bool PltEncryptMessage(PPLT_CRYPTO_CONTEXT ctx, int algorithm, int flags,
|
|||||||
inputDataLength = addPkcs7PaddingInPlace(inputData, inputDataLength);
|
inputDataLength = addPkcs7PaddingInPlace(inputData, inputDataLength);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
LC_ASSERT(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (EVP_EncryptUpdate(ctx->ctx, outputData, outputDataLength, inputData, inputDataLength) != 1) {
|
if (EVP_EncryptUpdate(ctx->ctx, outputData, outputDataLength, inputData, inputDataLength) != 1) {
|
||||||
return false;
|
return false;
|
||||||
@ -279,31 +271,16 @@ bool PltDecryptMessage(PPLT_CRYPTO_CONTEXT ctx, int algorithm, int flags,
|
|||||||
*outputDataLength = outLength;
|
*outputDataLength = outLength;
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
const EVP_CIPHER* cipher;
|
LC_ASSERT(keyLength == 16);
|
||||||
|
|
||||||
switch (algorithm) {
|
|
||||||
case ALGORITHM_AES_CBC:
|
|
||||||
LC_ASSERT(keyLength == 16);
|
|
||||||
LC_ASSERT(tag == NULL);
|
|
||||||
LC_ASSERT(tagLength == 0);
|
|
||||||
cipher = EVP_aes_128_cbc();
|
|
||||||
break;
|
|
||||||
case ALGORITHM_AES_GCM:
|
|
||||||
LC_ASSERT(keyLength == 16);
|
|
||||||
LC_ASSERT(tag != NULL);
|
|
||||||
LC_ASSERT(tagLength > 0);
|
|
||||||
cipher = EVP_aes_128_gcm();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
LC_ASSERT(false);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (algorithm == ALGORITHM_AES_GCM) {
|
if (algorithm == ALGORITHM_AES_GCM) {
|
||||||
|
LC_ASSERT(tag != NULL);
|
||||||
|
LC_ASSERT(tagLength > 0);
|
||||||
|
|
||||||
if (!ctx->initialized || (flags & CIPHER_FLAG_RESET_IV)) {
|
if (!ctx->initialized || (flags & CIPHER_FLAG_RESET_IV)) {
|
||||||
// Perform a full initialization. This codepath also allows
|
// Perform a full initialization. This codepath also allows
|
||||||
// us to change the IV length if required.
|
// us to change the IV length if required.
|
||||||
if (EVP_DecryptInit_ex(ctx->ctx, cipher, NULL, NULL, NULL) != 1) {
|
if (EVP_DecryptInit_ex(ctx->ctx, EVP_aes_128_gcm(), NULL, NULL, NULL) != 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,10 +302,13 @@ bool PltDecryptMessage(PPLT_CRYPTO_CONTEXT ctx, int algorithm, int flags,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else if (algorithm == ALGORITHM_AES_CBC) {
|
||||||
|
LC_ASSERT(tag == NULL);
|
||||||
|
LC_ASSERT(tagLength == 0);
|
||||||
|
|
||||||
if (!ctx->initialized) {
|
if (!ctx->initialized) {
|
||||||
// Perform a full initialization
|
// Perform a full initialization
|
||||||
if (EVP_DecryptInit_ex(ctx->ctx, cipher, NULL, key, iv) != 1) {
|
if (EVP_DecryptInit_ex(ctx->ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -342,6 +322,10 @@ bool PltDecryptMessage(PPLT_CRYPTO_CONTEXT ctx, int algorithm, int flags,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
LC_ASSERT(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (EVP_DecryptUpdate(ctx->ctx, outputData, outputDataLength, inputData, inputDataLength) != 1) {
|
if (EVP_DecryptUpdate(ctx->ctx, outputData, outputDataLength, inputData, inputDataLength) != 1) {
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user