From ded55f50536d852b544c7255551fc3b8c1e7b044 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 27 Dec 2018 18:16:56 -0500 Subject: initial implementation of short authentication string generation --- javascript/olm_post.js | 1 + javascript/olm_sas.js | 71 +++++++++++++++++++++++++++++++++++++++++++++ javascript/test/sas.spec.js | 53 +++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 javascript/olm_sas.js create mode 100644 javascript/test/sas.spec.js (limited to 'javascript') diff --git a/javascript/olm_post.js b/javascript/olm_post.js index 8c06fff..3bf0d66 100644 --- a/javascript/olm_post.js +++ b/javascript/olm_post.js @@ -534,6 +534,7 @@ olm_exports["Session"] = Session; olm_exports["Utility"] = Utility; olm_exports["PkEncryption"] = PkEncryption; olm_exports["PkDecryption"] = PkDecryption; +olm_exports["SAS"] = SAS; olm_exports["get_library_version"] = restore_stack(function() { var buf = stack(3); diff --git a/javascript/olm_sas.js b/javascript/olm_sas.js new file mode 100644 index 0000000..8fc535b --- /dev/null +++ b/javascript/olm_sas.js @@ -0,0 +1,71 @@ +function SAS() { + var size = Module['_olm_sas_size'](); + var random_length = Module['_olm_create_sas_random_length'](); + var random = random_stack(random_length); + this.buf = malloc(size); + this.ptr = Module['_olm_sas'](this.buf); + Module['_olm_create_sas'](this.ptr, random, random_length); + bzero(random, random_length); +} + +function sas_method(wrapped) { + return function() { + var result = wrapped.apply(this, arguments); + if (result === OLM_ERROR) { + var message = Pointer_stringify( + Module['_olm_sas_last_error'](arguments[0]) + ); + throw new Error("OLM." + message); + } + return result; + } +} + +SAS.prototype['free'] = function() { + Module['_olm_clear_sas'](this.ptr); + free(this.ptr); +}; + +SAS.prototype['get_pubkey'] = restore_stack(function() { + var pubkey_length = sas_method(Module['_olm_sas_pubkey_length'])(this.ptr); + var pubkey_buffer = stack(pubkey_length + NULL_BYTE_PADDING_LENGTH); + sas_method(Module['_olm_sas_get_pubkey'])(this.ptr, pubkey_buffer, pubkey_length); + return Pointer_stringify(pubkey_buffer); +}); + +SAS.prototype['set_their_key'] = restore_stack(function(their_key) { + var their_key_array = array_from_string(their_key); + var their_key_buffer = stack(their_key_array); + sas_method(Module['_olm_sas_set_their_key'])( + this.ptr, + their_key_buffer, their_key_array.length + ); +}); + +SAS.prototype['generate_bytes'] = restore_stack(function(length) { + var output_buffer = stack(length); + sas_method(Module['_olm_sas_generate_bytes'])( + this.ptr, + output_buffer, length + ); + // The inner Uint8Array creates a view of the buffer. The outer Uint8Array + // copies it to a new array to return, since the original buffer will get + // deallocated from the stack and could get overwritten. + var output_arr = new Uint8Array( + new Uint8Array(Module['HEAPU8'].buffer, output_buffer, length) + ); + return output_arr; +}); + +SAS.prototype['calculate_mac'] = restore_stack(function(input) { + var input_array = array_from_string(input); + var input_buffer = stack(input_array) + var mac_length = sas_method(Module['_olm_sas_mac_length'])(this.ptr); + var mac_buffer = stack(mac_length + NULL_BYTE_PADDING_LENGTH); + sas_method(Module['_olm_sas_calculate_mac'])( + this.ptr, + input_buffer, input_array.length, + mac_buffer, mac_length + ); + return Pointer_stringify(mac_buffer); +}); diff --git a/javascript/test/sas.spec.js b/javascript/test/sas.spec.js new file mode 100644 index 0000000..544131d --- /dev/null +++ b/javascript/test/sas.spec.js @@ -0,0 +1,53 @@ +/* +Copyright 2018 New Vector Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +var Olm = require('../olm'); + +describe("sas", function() { + var alice, bob; + + beforeEach(async function(done) { + Olm.init().then(function() { + alice = new Olm.SAS(); + bob = new Olm.SAS(); + + done(); + }); + }); + + afterEach(function () { + if (alice !== undefined) { + alice.free(); + alice = undefined; + } + if (bob !== undefined) { + bob.free(); + bob = undefined; + } + }); + + it('should create matching SAS bytes', function () { + alice.set_their_key(bob.get_pubkey()); + bob.set_their_key(alice.get_pubkey()); + expect(alice.generate_bytes(5).toString()).toEqual(bob.generate_bytes(5).toString()); + }); + + it('should create matching MACs', function () { + alice.set_their_key(bob.get_pubkey()); + bob.set_their_key(alice.get_pubkey()); + expect(alice.calculate_mac("test").toString()).toEqual(bob.calculate_mac("test").toString()); + }); +}); -- cgit v1.2.3