From 0346145a813cfb719fdf218956cb2f29030134a8 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 2 Oct 2018 12:02:56 +0100 Subject: Work with PkDecryption keys by their private keys Change interface to allow the app to get the private part of the key and instantiate a decryption object from just the private part of the key. Changes the function generating a key from random bytes to be initialising a key with a private key (because it's exactly the same thing). Exports & imports private key parts as ArrayBuffer at JS level rather than base64 assuming we are moving that way in general. --- include/olm/error.h | 7 +++++++ include/olm/pk.h | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 11 deletions(-) (limited to 'include/olm') diff --git a/include/olm/error.h b/include/olm/error.h index 9d44a94..ee2187c 100644 --- a/include/olm/error.h +++ b/include/olm/error.h @@ -51,6 +51,13 @@ enum OlmErrorCode { */ OLM_BAD_SIGNATURE = 14, + OLM_INPUT_BUFFER_TOO_SMALL = 15, + + // Not an error code, just here to pad out the enum past 16 because + // otherwise the compiler warns about a redunant check. If you're + // adding an error code, replace this one! + OLM_ERROR_NOT_INVENTED_YET = 16, + /* remember to update the list of string constants in error.c when updating * this list. */ }; diff --git a/include/olm/pk.h b/include/olm/pk.h index 1f3f9ff..5e779ce 100644 --- a/include/olm/pk.h +++ b/include/olm/pk.h @@ -76,7 +76,7 @@ size_t olm_pk_encrypt_random_length( * ciphertext, mac, or ephemeral_key buffers were too small then * olm_pk_encryption_last_error() will be "OUTPUT_BUFFER_TOO_SMALL". If there * weren't enough random bytes then olm_pk_encryption_last_error() will be - * "NOT_ENOUGH_RANDOM". */ + * "OLM_INPUT_BUFFER_TOO_SMALL". */ size_t olm_pk_encrypt( OlmPkEncryption *encryption, void const * plaintext, size_t plaintext_length, @@ -108,18 +108,24 @@ size_t olm_clear_pk_decryption( OlmPkDecryption *decryption ); -/** The number of random bytes needed to generate a new key. */ -size_t olm_pk_generate_key_random_length(void); - -/** Generate a new key to use for decrypting messages. The associated public - * key 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 - * olm_pk_decryption_last_error() will be "NOT_ENOUGH_RANDOM". */ -size_t olm_pk_generate_key( +/** Get the number of bytes required to store an olm private key + */ +size_t olm_pk_private_key_length(); + +/** Initialise the key from the private part of a key as returned by + * olm_pk_get_private_key(). The associated public key 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 the private key was not long enough then olm_pk_decryption_last_error() + * will be "OLM_INPUT_BUFFER_TOO_SMALL". + * + * Note that the pubkey is a base64 encoded string, but the private key is + * an unencoded byte array + */ +size_t olm_pk_key_from_private( OlmPkDecryption * decryption, void * pubkey, size_t pubkey_length, - void * random, size_t random_length + void * privkey, size_t privkey_length ); /** Returns the number of bytes needed to store a decryption object. */ @@ -171,6 +177,19 @@ size_t olm_pk_decrypt( void * plaintext, size_t max_plaintext_length ); +/** + * Get the private key for an OlmDecryption object as an unencoded byte array + * private_key must be a pointer to a buffer of at least + * olm_pk_private_key_length() bytes and this length must be passed in + * private_key_length. If the given buffer is too small, returns olm_error() + * and olm_pk_encryption_last_error() will be "OUTPUT_BUFFER_TOO_SMALL". + * Returns the number of bytes written. + */ +size_t olm_pk_get_private_key( + OlmPkDecryption * decryption, + void *private_key, size_t private_key_length +); + #ifdef __cplusplus } #endif -- cgit v1.2.3 From 8520168e0b4c8172847a051e532ca4deaec46a95 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Mon, 9 Jul 2018 23:21:55 -0400 Subject: fix some code style issues and typos --- include/olm/pk.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'include/olm') diff --git a/include/olm/pk.h b/include/olm/pk.h index 1f3f9ff..07e6077 100644 --- a/include/olm/pk.h +++ b/include/olm/pk.h @@ -111,9 +111,10 @@ size_t olm_clear_pk_decryption( /** The number of random bytes needed to generate a new key. */ size_t olm_pk_generate_key_random_length(void); -/** Generate a new key to use for decrypting messages. The associated public - * key 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 +/** Generate a new key pair to use for decrypting messages. The private key is + * stored in the decryption object, and the associated public key 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 * olm_pk_decryption_last_error() will be "NOT_ENOUGH_RANDOM". */ 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 * be "OUTPUT_BUFFER_TOO_SMALL". */ size_t olm_pk_decrypt( - OlmPkDecryption * decrytion, + OlmPkDecryption * decryption, void const * ephemeral_key, size_t ephemeral_key_length, void const * mac, size_t mac_length, void * ciphertext, size_t ciphertext_length, -- cgit v1.2.3 From 173339ae9accddd184bc83f2c23c5ffae3b08d00 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 12 Jul 2018 17:54:03 -0400 Subject: add more comments describing the pk encrypt/decrypt functions --- include/olm/pk.h | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'include/olm') diff --git a/include/olm/pk.h b/include/olm/pk.h index 07e6077..8804d1f 100644 --- a/include/olm/pk.h +++ b/include/olm/pk.h @@ -72,11 +72,15 @@ size_t olm_pk_encrypt_random_length( ); /** Encrypt a plaintext for the recipient set using - * olm_pk_encryption_set_recipient_key. Returns olm_error() on failure. If the - * ciphertext, mac, or ephemeral_key buffers were too small then - * olm_pk_encryption_last_error() will be "OUTPUT_BUFFER_TOO_SMALL". If there - * weren't enough random bytes then olm_pk_encryption_last_error() will be - * "NOT_ENOUGH_RANDOM". */ + * olm_pk_encryption_set_recipient_key. Writes to the ciphertext, mac, and + * ephemeral_key buffers, whose values should be sent to the recipient. mac is + * a Message Authentication Code to ensure that the data is received and + * decrypted properly. ephemeral_key is the public part of the ephemeral key + * used (together with the recipient's key) to generate a symmetric encryption + * key. Returns olm_error() on failure. If the ciphertext, mac, or + * ephemeral_key buffers were too small then olm_pk_encryption_last_error() + * will be "OUTPUT_BUFFER_TOO_SMALL". If there weren't enough random bytes then + * olm_pk_encryption_last_error() will be "NOT_ENOUGH_RANDOM". */ size_t olm_pk_encrypt( OlmPkEncryption *encryption, void const * plaintext, size_t plaintext_length, @@ -160,10 +164,11 @@ size_t olm_pk_max_plaintext_length( size_t ciphertext_length ); -/** Decrypt a ciphertext. The input ciphertext buffer is destroyed. Returns - * the length of the plaintext on success. Returns olm_error() on failure. If - * the plaintext buffer is too small then olm_pk_encryption_last_error() will - * be "OUTPUT_BUFFER_TOO_SMALL". */ +/** Decrypt a ciphertext. The input ciphertext buffer is destroyed. See the + * olm_pk_encrypt function for descriptions of the ephemeral_key and mac + * arguments. Returns the length of the plaintext on success. Returns + * olm_error() on failure. If the plaintext buffer is too small then + * olm_pk_encryption_last_error() will be "OUTPUT_BUFFER_TOO_SMALL". */ size_t olm_pk_decrypt( OlmPkDecryption * decryption, void const * ephemeral_key, size_t ephemeral_key_length, -- cgit v1.2.3 From fac1d52dfe25d8bf6119cc41645a84c9111c6f6e Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 11 Oct 2018 18:16:39 +0100 Subject: Add aliases for deprecated functions --- include/olm/pk.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'include/olm') diff --git a/include/olm/pk.h b/include/olm/pk.h index 8748506..4278fca 100644 --- a/include/olm/pk.h +++ b/include/olm/pk.h @@ -116,6 +116,10 @@ size_t olm_clear_pk_decryption( */ size_t olm_pk_private_key_length(); +/** DEPRECATED: Use olm_pk_private_key_length() + */ +size_t olm_pk_generate_key_random_length(void); + /** Initialise the key from the private part of a key as returned by * olm_pk_get_private_key(). The associated public key will be written to the * pubkey buffer. Returns olm_error() on failure. If the pubkey buffer is too @@ -132,6 +136,14 @@ size_t olm_pk_key_from_private( void * privkey, size_t privkey_length ); +/** DEPRECATED: Use olm_pk_key_from_private + */ +size_t olm_pk_generate_key( + OlmPkDecryption * decryption, + void * pubkey, size_t pubkey_length, + void * privkey, size_t privkey_length +); + /** Returns the number of bytes needed to store a decryption object. */ size_t olm_pickle_pk_decryption_length( OlmPkDecryption * decryption -- cgit v1.2.3