diff options
author | George Hazan <ghazan@miranda.im> | 2022-10-26 12:42:50 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2022-10-26 12:42:50 +0300 |
commit | 976a3f93925a28feb107ae505834ce7a728a42b8 (patch) | |
tree | 71f02b66d9ac2498ae137f8966aa3c82ced1e525 /protocols/WhatsApp/src/crypt.cpp | |
parent | b7959c62b1c479277a211504733aed14b6b914ff (diff) |
WhatsApp: message sending
Diffstat (limited to 'protocols/WhatsApp/src/crypt.cpp')
-rw-r--r-- | protocols/WhatsApp/src/crypt.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/protocols/WhatsApp/src/crypt.cpp b/protocols/WhatsApp/src/crypt.cpp index 1efb93dde4..20e8ddd1bd 100644 --- a/protocols/WhatsApp/src/crypt.cpp +++ b/protocols/WhatsApp/src/crypt.cpp @@ -42,6 +42,41 @@ MBinBuffer aesDecrypt( return ret; } +MBinBuffer aesEncrypt( + const EVP_CIPHER *cipher, + const uint8_t *key, + const uint8_t *iv, + const void *data, size_t dataLen, + const void *additionalData, size_t additionalLen) +{ + int tag_len = 0, dec_len = 0, final_len = 0; + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv); + + if (additionalLen) + EVP_EncryptUpdate(ctx, nullptr, &tag_len, (uint8_t *)additionalData, (int)additionalLen); + + if (cipher == EVP_aes_256_gcm()) { + dataLen -= 16; + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, (uint8_t *)data + dataLen); + } + + MBinBuffer ret; + uint8_t outbuf[2000]; + for (size_t len = 0; len < dataLen; len += 1024) { + size_t portionSize = dataLen - len; + EVP_EncryptUpdate(ctx, outbuf, &dec_len, (uint8_t *)data + len, (int)min(portionSize, 1024)); + ret.append(outbuf, dec_len); + } + + EVP_EncryptFinal_ex(ctx, outbuf, &final_len); + if (final_len) + ret.append(outbuf, final_len); + + EVP_CIPHER_CTX_free(ctx); + return ret; +} + ///////////////////////////////////////////////////////////////////////////////////////// static unsigned char *HKDF_Extract(const EVP_MD *evp_md, |