From f709b062bb8dfebff3dd428fe468cf15a864c7fd Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 28 Jun 2018 17:10:36 -0400 Subject: add functions for pickling/unpickling a decryption object --- javascript/olm_pk.js | 29 +++++++++++++++++++++++++++++ javascript/test/pk.spec.js | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+) (limited to 'javascript') diff --git a/javascript/olm_pk.js b/javascript/olm_pk.js index 580d854..2542707 100644 --- a/javascript/olm_pk.js +++ b/javascript/olm_pk.js @@ -135,6 +135,35 @@ PkDecryption.prototype['generate_key'] = restore_stack(function () { return Pointer_stringify(pubkey_buffer); }); +PkDecryption.prototype['pickle'] = restore_stack(function (key) { + var key_array = array_from_string(key); + var pickle_length = pk_decryption_method( + Module['_olm_pickle_pk_decryption_length'] + )(this.ptr); + var key_buffer = stack(key_array); + var pickle_buffer = stack(pickle_length + NULL_BYTE_PADDING_LENGTH); + pk_decryption_method(Module['_olm_pickle_pk_decryption'])( + this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length + ); + return Pointer_stringify(pickle_buffer); +}); + +PkDecryption.prototype['unpickle'] = restore_stack(function (key, pickle) { + var key_array = array_from_string(key); + var key_buffer = stack(key_array); + var pickle_array = array_from_string(pickle); + var pickle_buffer = stack(pickle_array); + var ephemeral_length = pk_decryption_method( + Module["_olm_pk_key_length"] + )(); + var ephemeral_buffer = stack(ephemeral_length + NULL_BYTE_PADDING_LENGTH); + pk_decryption_method(Module['_olm_unpickle_pk_decryption'])( + this.ptr, key_buffer, key_array.length, pickle_buffer, + pickle_array.length, ephemeral_buffer, ephemeral_length + ); + return Pointer_stringify(ephemeral_buffer); +}); + PkDecryption.prototype['decrypt'] = restore_stack(function ( ephemeral_key, mac, ciphertext ) { diff --git a/javascript/test/pk.spec.js b/javascript/test/pk.spec.js index 9eec47e..aec90ac 100644 --- a/javascript/test/pk.spec.js +++ b/javascript/test/pk.spec.js @@ -61,4 +61,22 @@ describe("pk", function() { console.log(TEST_TEXT, "->", decrypted); expect(decrypted).toEqual(TEST_TEXT); }); + + it('should pickle and unpickle', function () { + var TEST_TEXT = 'têst1'; + var pubkey = decryption.generate_key(); + encryption.set_recipient_key(pubkey); + var encrypted = encryption.encrypt(TEST_TEXT); + + var PICKLE_KEY = 'secret_key'; + var pickle = decryption.pickle(PICKLE_KEY); + + var new_decryption = new Olm.PkDecryption(); + var new_pubkey = new_decryption.unpickle(PICKLE_KEY, pickle); + expect(new_pubkey).toEqual(pubkey); + var decrypted = new_decryption.decrypt(encrypted.ephemeral, encrypted.mac, encrypted.ciphertext); + console.log(TEST_TEXT, "->", decrypted); + expect(decrypted).toEqual(TEST_TEXT); + new_decryption.free(); + }); }); -- cgit v1.2.3