fix some code style issues and typos

This commit is contained in:
Hubert Chathi 2018-07-09 23:21:55 -04:00
parent 9b652bb127
commit 8520168e0b
4 changed files with 91 additions and 39 deletions

View file

@ -29,7 +29,10 @@ OlmPkEncryption * initializePkEncryptionMemory()
{ {
// init encryption object // init encryption object
encryptionPtr = olm_pk_encryption(encryptionPtr); encryptionPtr = olm_pk_encryption(encryptionPtr);
LOGD("## initializePkEncryptionMemory(): success - OLM encryption size=%lu",static_cast<long unsigned int>(encryptionSize)); LOGD(
"## initializePkEncryptionMemory(): success - OLM encryption size=%lu",
static_cast<long unsigned int>(encryptionSize)
);
} }
else else
{ {
@ -53,7 +56,10 @@ JNIEXPORT jlong OLM_PK_ENCRYPTION_FUNC_DEF(createNewPkEncryptionJni)(JNIEnv *env
else else
{ {
LOGD("## createNewPkEncryptionJni(): success - OLM encryption created"); LOGD("## createNewPkEncryptionJni(): success - OLM encryption created");
LOGD("## createNewPkEncryptionJni(): encryptionPtr=%p (jlong)(intptr_t)encryptionPtr=%lld", encryptionPtr, (jlong)(intptr_t)encryptionPtr); LOGD(
"## createNewPkEncryptionJni(): encryptionPtr=%p (jlong)(intptr_t)encryptionPtr=%lld",
encryptionPtr, (jlong)(intptr_t)encryptionPtr
);
} }
if (errorMessage) if (errorMessage)
@ -93,8 +99,9 @@ JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(releasePkEncryptionJni)(JNIEnv *env, j
} }
} }
JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(setRecipientKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer) JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(setRecipientKeyJni)(
{ JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer
) {
const char *errorMessage = NULL; const char *errorMessage = NULL;
jbyte *keyPtr = NULL; jbyte *keyPtr = NULL;
@ -119,7 +126,10 @@ JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(setRecipientKeyJni)(JNIEnv *env, jobje
if (olm_pk_encryption_set_recipient_key(encryptionPtr, keyPtr, (size_t)env->GetArrayLength(aKeyBuffer)) == olm_error()) if (olm_pk_encryption_set_recipient_key(encryptionPtr, keyPtr, (size_t)env->GetArrayLength(aKeyBuffer)) == olm_error())
{ {
errorMessage = olm_pk_encryption_last_error(encryptionPtr); errorMessage = olm_pk_encryption_last_error(encryptionPtr);
LOGE(" ## pkSetRecipientKeyJni(): failure - olm_pk_encryption_set_recipient_key Msg=%s", errorMessage); LOGE(
" ## pkSetRecipientKeyJni(): failure - olm_pk_encryption_set_recipient_key Msg=%s",
errorMessage
);
} }
} }
@ -134,8 +144,9 @@ JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(setRecipientKeyJni)(JNIEnv *env, jobje
} }
} }
JNIEXPORT jbyteArray OLM_PK_ENCRYPTION_FUNC_DEF(encryptJni)(JNIEnv *env, jobject thiz, jbyteArray aPlaintextBuffer, jobject aEncryptedMsg) JNIEXPORT jbyteArray OLM_PK_ENCRYPTION_FUNC_DEF(encryptJni)(
{ JNIEnv *env, jobject thiz, jbyteArray aPlaintextBuffer, jobject aEncryptedMsg
) {
jbyteArray encryptedMsgRet = 0; jbyteArray encryptedMsgRet = 0;
const char* errorMessage = NULL; const char* errorMessage = NULL;
jbyte *plaintextPtr = NULL; jbyte *plaintextPtr = NULL;
@ -161,8 +172,8 @@ JNIEXPORT jbyteArray OLM_PK_ENCRYPTION_FUNC_DEF(encryptJni)(JNIEnv *env, jobject
} }
else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg)))
{ {
LOGE(" ## pkEncryptJni(): failure - unable to get crypted message class"); LOGE(" ## pkEncryptJni(): failure - unable to get encrypted message class");
errorMessage = "unable to get crypted message class"; errorMessage = "unable to get encrypted message class";
} }
else if (!(macFieldId = env->GetFieldID(encryptedMsgJClass, "mMac", "Ljava/lang/String;"))) else if (!(macFieldId = env->GetFieldID(encryptedMsgJClass, "mMac", "Ljava/lang/String;")))
{ {
@ -226,7 +237,9 @@ JNIEXPORT jbyteArray OLM_PK_ENCRYPTION_FUNC_DEF(encryptJni)(JNIEnv *env, jobject
else else
{ {
encryptedMsgRet = env->NewByteArray(ciphertextLength); encryptedMsgRet = env->NewByteArray(ciphertextLength);
env->SetByteArrayRegion(encryptedMsgRet, 0, ciphertextLength, (jbyte*)ciphertextPtr); env->SetByteArrayRegion(
encryptedMsgRet, 0, ciphertextLength, (jbyte*)ciphertextPtr
);
jstring macStr = env->NewStringUTF((char*)macPtr); jstring macStr = env->NewStringUTF((char*)macPtr);
env->SetObjectField(aEncryptedMsg, macFieldId, macStr); env->SetObjectField(aEncryptedMsg, macFieldId, macStr);
@ -276,7 +289,10 @@ OlmPkDecryption * initializePkDecryptionMemory()
{ {
// init decryption object // init decryption object
decryptionPtr = olm_pk_decryption(decryptionPtr); decryptionPtr = olm_pk_decryption(decryptionPtr);
LOGD("## initializePkDecryptionMemory(): success - OLM decryption size=%lu",static_cast<long unsigned int>(decryptionSize)); LOGD(
"## initializePkDecryptionMemory(): success - OLM decryption size=%lu",
static_cast<long unsigned int>(decryptionSize)
);
} }
else else
{ {
@ -300,7 +316,10 @@ JNIEXPORT jlong OLM_PK_DECRYPTION_FUNC_DEF(createNewPkDecryptionJni)(JNIEnv *env
else else
{ {
LOGD("## createNewPkDecryptionJni(): success - OLM decryption created"); LOGD("## createNewPkDecryptionJni(): success - OLM decryption created");
LOGD("## createNewPkDecryptionJni(): decryptionPtr=%p (jlong)(intptr_t)decryptionPtr=%lld", decryptionPtr, (jlong)(intptr_t)decryptionPtr); LOGD(
"## createNewPkDecryptionJni(): decryptionPtr=%p (jlong)(intptr_t)decryptionPtr=%lld",
decryptionPtr, (jlong)(intptr_t)decryptionPtr
);
} }
if (errorMessage) if (errorMessage)
@ -402,8 +421,9 @@ JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(generateKeyJni)(JNIEnv *env, job
return publicKeyRet; return publicKeyRet;
} }
JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(decryptJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg) JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(decryptJni)(
{ JNIEnv *env, jobject thiz, jobject aEncryptedMsg
) {
const char* errorMessage = NULL; const char* errorMessage = NULL;
OlmPkDecryption *decryptionPtr = getPkDecryptionInstanceId(env, thiz); OlmPkDecryption *decryptionPtr = getPkDecryptionInstanceId(env, thiz);
@ -528,7 +548,10 @@ JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(decryptJni)(JNIEnv *env, jobject
{ {
decryptedMsgRet = env->NewByteArray(plaintextLength); decryptedMsgRet = env->NewByteArray(plaintextLength);
env->SetByteArrayRegion(decryptedMsgRet, 0, plaintextLength, (jbyte*)plaintextPtr); env->SetByteArrayRegion(decryptedMsgRet, 0, plaintextLength, (jbyte*)plaintextPtr);
LOGD("## pkDecryptJni(): success returnedLg=%lu OK", static_cast<long unsigned int>(plaintextLength)); LOGD(
"## pkDecryptJni(): success returnedLg=%lu OK",
static_cast<long unsigned int>(plaintextLength)
);
} }
} }

View file

@ -111,9 +111,10 @@ size_t olm_clear_pk_decryption(
/** The number of random bytes needed to generate a new key. */ /** The number of random bytes needed to generate a new key. */
size_t olm_pk_generate_key_random_length(void); size_t olm_pk_generate_key_random_length(void);
/** Generate a new key to use for decrypting messages. The associated public /** Generate a new key pair to use for decrypting messages. The private key is
* key will be written to the pubkey buffer. Returns olm_error() on failure. If * stored in the decryption object, and the associated public key will be
* the pubkey buffer is too small then olm_pk_decryption_last_error() will be * written to the pubkey buffer. Returns olm_error() on failure. If the pubkey
* buffer is too small then olm_pk_decryption_last_error() will be
* "OUTPUT_BUFFER_TOO_SMALL". If there weren't enough random bytes then * "OUTPUT_BUFFER_TOO_SMALL". If there weren't enough random bytes then
* olm_pk_decryption_last_error() will be "NOT_ENOUGH_RANDOM". */ * olm_pk_decryption_last_error() will be "NOT_ENOUGH_RANDOM". */
size_t olm_pk_generate_key( size_t olm_pk_generate_key(
@ -164,7 +165,7 @@ size_t olm_pk_max_plaintext_length(
* the plaintext buffer is too small then olm_pk_encryption_last_error() will * the plaintext buffer is too small then olm_pk_encryption_last_error() will
* be "OUTPUT_BUFFER_TOO_SMALL". */ * be "OUTPUT_BUFFER_TOO_SMALL". */
size_t olm_pk_decrypt( size_t olm_pk_decrypt(
OlmPkDecryption * decrytion, OlmPkDecryption * decryption,
void const * ephemeral_key, size_t ephemeral_key_length, void const * ephemeral_key, size_t ephemeral_key_length,
void const * mac, size_t mac_length, void const * mac, size_t mac_length,
void * ciphertext, size_t ciphertext_length, void * ciphertext, size_t ciphertext_length,

View file

@ -22,8 +22,6 @@
#include "olm/pickle_encoding.h" #include "olm/pickle_encoding.h"
#include "olm/pickle.hh" #include "olm/pickle.hh"
extern "C" {
static const std::size_t MAC_LENGTH = 8; static const std::size_t MAC_LENGTH = 8;
const struct _olm_cipher_aes_sha_256 olm_pk_cipher_aes_sha256 = const struct _olm_cipher_aes_sha_256 olm_pk_cipher_aes_sha256 =
@ -31,6 +29,8 @@ static const std::size_t MAC_LENGTH = 8;
const struct _olm_cipher *olm_pk_cipher = const struct _olm_cipher *olm_pk_cipher =
OLM_CIPHER_BASE(&olm_pk_cipher_aes_sha256); OLM_CIPHER_BASE(&olm_pk_cipher_aes_sha256);
extern "C" {
struct OlmPkEncryption { struct OlmPkEncryption {
OlmErrorCode last_error; OlmErrorCode last_error;
_olm_curve25519_public_key recipient_key; _olm_curve25519_public_key recipient_key;
@ -73,7 +73,11 @@ size_t olm_pk_encryption_set_recipient_key (
OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; // FIXME: OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; // FIXME:
return std::size_t(-1); return std::size_t(-1);
} }
olm::decode_base64((const uint8_t*)key, olm_pk_key_length(), (uint8_t *)encryption->recipient_key.public_key); olm::decode_base64(
(const uint8_t*)key,
olm_pk_key_length(),
(uint8_t *)encryption->recipient_key.public_key
);
return 0; return 0;
} }
@ -81,7 +85,9 @@ size_t olm_pk_ciphertext_length(
OlmPkEncryption *encryption, OlmPkEncryption *encryption,
size_t plaintext_length size_t plaintext_length
) { ) {
return olm::encode_base64_length(_olm_cipher_aes_sha_256_ops.encrypt_ciphertext_length(olm_pk_cipher, plaintext_length)); return olm::encode_base64_length(
_olm_cipher_aes_sha_256_ops.encrypt_ciphertext_length(olm_pk_cipher, plaintext_length)
);
} }
size_t olm_pk_mac_length( size_t olm_pk_mac_length(
@ -122,11 +128,16 @@ size_t olm_pk_encrypt(
_olm_curve25519_key_pair ephemeral_keypair; _olm_curve25519_key_pair ephemeral_keypair;
_olm_crypto_curve25519_generate_key((uint8_t *) random, &ephemeral_keypair); _olm_crypto_curve25519_generate_key((uint8_t *) random, &ephemeral_keypair);
olm::encode_base64((const uint8_t *)ephemeral_keypair.public_key.public_key, CURVE25519_KEY_LENGTH, (uint8_t *)ephemeral_key); olm::encode_base64(
(const uint8_t *)ephemeral_keypair.public_key.public_key,
CURVE25519_KEY_LENGTH,
(uint8_t *)ephemeral_key
);
olm::SharedKey secret; olm::SharedKey secret;
_olm_crypto_curve25519_shared_secret(&ephemeral_keypair, &encryption->recipient_key, secret); _olm_crypto_curve25519_shared_secret(&ephemeral_keypair, &encryption->recipient_key, secret);
size_t raw_ciphertext_length = _olm_cipher_aes_sha_256_ops.encrypt_ciphertext_length(olm_pk_cipher, plaintext_length); size_t raw_ciphertext_length =
_olm_cipher_aes_sha_256_ops.encrypt_ciphertext_length(olm_pk_cipher, plaintext_length);
uint8_t *ciphertext_pos = (uint8_t *) ciphertext + ciphertext_length - raw_ciphertext_length; uint8_t *ciphertext_pos = (uint8_t *) ciphertext + ciphertext_length - raw_ciphertext_length;
uint8_t raw_mac[MAC_LENGTH]; uint8_t raw_mac[MAC_LENGTH];
size_t result = _olm_cipher_aes_sha_256_ops.encrypt( size_t result = _olm_cipher_aes_sha_256_ops.encrypt(
@ -201,7 +212,11 @@ size_t olm_pk_generate_key(
} }
_olm_crypto_curve25519_generate_key((uint8_t *) random, &decryption->key_pair); _olm_crypto_curve25519_generate_key((uint8_t *) random, &decryption->key_pair);
olm::encode_base64((const uint8_t *)decryption->key_pair.public_key.public_key, CURVE25519_KEY_LENGTH, (uint8_t *)pubkey); olm::encode_base64(
(const uint8_t *)decryption->key_pair.public_key.public_key,
CURVE25519_KEY_LENGTH,
(uint8_t *)pubkey
);
return 0; return 0;
} }
@ -267,7 +282,10 @@ size_t olm_pickle_pk_decryption(
return std::size_t(-1); return std::size_t(-1);
} }
pickle(_olm_enc_output_pos(reinterpret_cast<std::uint8_t *>(pickled), raw_length), object); pickle(_olm_enc_output_pos(reinterpret_cast<std::uint8_t *>(pickled), raw_length), object);
return _olm_enc_output(reinterpret_cast<std::uint8_t const *>(key), key_length, reinterpret_cast<std::uint8_t *>(pickled), raw_length); return _olm_enc_output(
reinterpret_cast<std::uint8_t const *>(key), key_length,
reinterpret_cast<std::uint8_t *>(pickled), raw_length
);
} }
size_t olm_unpickle_pk_decryption( size_t olm_unpickle_pk_decryption(
@ -283,7 +301,8 @@ size_t olm_unpickle_pk_decryption(
} }
std::uint8_t * const pos = reinterpret_cast<std::uint8_t *>(pickled); std::uint8_t * const pos = reinterpret_cast<std::uint8_t *>(pickled);
std::size_t raw_length = _olm_enc_input( std::size_t raw_length = _olm_enc_input(
reinterpret_cast<std::uint8_t const *>(key), key_length, pos, pickled_length, &object.last_error reinterpret_cast<std::uint8_t const *>(key), key_length,
pos, pickled_length, &object.last_error
); );
if (raw_length == std::size_t(-1)) { if (raw_length == std::size_t(-1)) {
return std::size_t(-1); return std::size_t(-1);
@ -300,7 +319,11 @@ size_t olm_unpickle_pk_decryption(
return std::size_t(-1); return std::size_t(-1);
} }
if (pubkey != NULL) { if (pubkey != NULL) {
olm::encode_base64((const uint8_t *)object.key_pair.public_key.public_key, CURVE25519_KEY_LENGTH, (uint8_t *)pubkey); olm::encode_base64(
(const uint8_t *)object.key_pair.public_key.public_key,
CURVE25519_KEY_LENGTH,
(uint8_t *)pubkey
);
} }
return pickled_length; return pickled_length;
} }
@ -309,7 +332,9 @@ size_t olm_pk_max_plaintext_length(
OlmPkDecryption * decryption, OlmPkDecryption * decryption,
size_t ciphertext_length size_t ciphertext_length
) { ) {
return _olm_cipher_aes_sha_256_ops.decrypt_max_plaintext_length(olm_pk_cipher, olm::decode_base64_length(ciphertext_length)); return _olm_cipher_aes_sha_256_ops.decrypt_max_plaintext_length(
olm_pk_cipher, olm::decode_base64_length(ciphertext_length)
);
} }
size_t olm_pk_decrypt( size_t olm_pk_decrypt(
@ -327,7 +352,10 @@ size_t olm_pk_decrypt(
} }
struct _olm_curve25519_public_key ephemeral; struct _olm_curve25519_public_key ephemeral;
olm::decode_base64((const uint8_t*)ephemeral_key, ephemeral_key_length, (uint8_t *)ephemeral.public_key); olm::decode_base64(
(const uint8_t*)ephemeral_key, ephemeral_key_length,
(uint8_t *)ephemeral.public_key
);
olm::SharedKey secret; olm::SharedKey secret;
_olm_crypto_curve25519_shared_secret(&decryption->key_pair, &ephemeral, secret); _olm_crypto_curve25519_shared_secret(&decryption->key_pair, &ephemeral, secret);
uint8_t raw_mac[MAC_LENGTH]; uint8_t raw_mac[MAC_LENGTH];