Merge pull request #69 from vector-im/bwindels/add-olm

Add olm as a dependency + prototype on how to use
This commit is contained in:
Bruno Windels 2020-08-27 10:49:01 +00:00 committed by GitHub
commit d81c739b82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 130 additions and 4 deletions

View file

@ -1 +0,0 @@
../node_modules/olm/olm.js

View file

@ -1 +0,0 @@
../node_modules/olm/olm.wasm

1
lib/olm/olm.js Symbolic link
View file

@ -0,0 +1 @@
../../node_modules/olm/olm.js

1
lib/olm/olm.wasm Symbolic link
View file

@ -0,0 +1 @@
../../node_modules/olm/olm.wasm

1
lib/olm/olm_legacy.js Symbolic link
View file

@ -0,0 +1 @@
../../node_modules/olm/olm_legacy.js

View file

@ -44,5 +44,8 @@
"rollup-plugin-cleanup": "^3.1.1",
"serve-static": "^1.13.2",
"xxhash": "^0.3.0"
},
"dependencies": {
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz"
}
}

View file

@ -0,0 +1,118 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
pre {
font-family: monospace;
display: block;
white-space: pre;
font-size: 2em;
}
</style>
</head>
<body>
<script type="text/javascript">
if (!Math.imul) Math.imul = function(a,b) {return (a*b)|0;}/* function(a, b) {
var aHi = (a >>> 16) & 0xffff;
var aLo = a & 0xffff;
var bHi = (b >>> 16) & 0xffff;
var bLo = b & 0xffff;
// the shift by 0 fixes the sign on the high part
// the final |0 converts the unsigned value into a signed value
return ((aLo * bLo) + (((aHi * bLo + aLo * bHi) << 16) >>> 0) | 0);
};*/
if (!Math.clz32) Math.clz32 = (function(log, LN2){
return function(x) {
// Let n be ToUint32(x).
// Let p be the number of leading zero bits in
// the 32-bit binary representation of n.
// Return p.
var asUint = x >>> 0;
if (asUint === 0) {
return 32;
}
return 31 - (log(asUint) / LN2 | 0) |0; // the "| 0" acts like math.floor
};
})(Math.log, Math.LN2);
</script>
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
<script type="text/javascript" src="../lib/olm/olm_legacy.js"></script>
<script type="text/javascript">
function doit(log) {
var alice = new Olm.Account();
alice.create();
log("alice", alice.identity_keys());
var bob = new Olm.Account();
bob.unpickle("secret", "EWfA87or4GgQ+wqVkyuFiW9gUk3FI6QSXgp8E2dS5RFLvXgy4oFvxwQ1gVnbMkdJz2Hy9ex9UmJ/ZyuRU0aRt0IwXpw/SUNq4IQeVJ7J/miXW7rV4Ep+4RSEf945KbDrokDCS2CoL5PIfv/NYyey32gA0hMi8wWIfIlOxFBV4SBJYSC+Qd54VjprwCg0Sn9vjQouKVrM/+5jzsv9+JK5OpWW0Vrb3qrXwyAOEAQ4WlOQcqZHAyPQIw");
log("bob", bob.identity_keys());
// generate OTK on receiver side
bob.generate_one_time_keys(1);
var bobOneTimeKeys = JSON.parse(bob.one_time_keys());
var otkName = Object.getOwnPropertyNames(bobOneTimeKeys.curve25519)[0];
var bobOneTimeKey = bobOneTimeKeys.curve25519[otkName];
// encrypt
var aliceSession = new Olm.Session();
aliceSession.create_outbound(
alice,
JSON.parse(bob.identity_keys()).curve25519,
bobOneTimeKey
);
var message = aliceSession.encrypt("hello secret world");
log("message", message);
// decrypt
var bobSession = new Olm.Session();
bobSession.create_inbound(bob, message.body);
var plaintext = bobSession.decrypt(message.type, message.body);
log("plaintext", plaintext);
// remove Bob's OTK as it was used to start an olm session
log("bob OTK before removing", bob.one_time_keys());
bob.remove_one_time_keys(bobSession);
log("bob OTK after removing", bob.one_time_keys());
}
if (window.msCrypto && !window.crypto) {
window.crypto = window.msCrypto;
}
function doRun(e) {
e.target.setAttribute("disabled", "disabled");
var logEl = document.getElementById("log");
logEl.innerText = "";
var startTime = performance.now();
function log() {
var timeDiff = Math.round(performance.now() - startTime).toString();
while (timeDiff.length < 5) {
timeDiff = "0" + timeDiff;
}
logEl.appendChild(document.createTextNode(timeDiff + " "));
for (var i = 0; i < arguments.length; i += 1) {
var value = arguments[i];
if (typeof value !== "string") {
value = JSON.stringify(value);
}
logEl.appendChild(document.createTextNode(value + " "));
}
logEl.appendChild(document.createTextNode("\n"));
}
doit(log);
e.target.removeAttribute("disabled");
}
function main() {
Olm.init( ).then(function() {
var startButton = document.getElementById("start");
startButton.innerText = "Start";
startButton.addEventListener("click", doRun);
});
}
document.addEventListener("DOMContentLoaded", main);
</script>
<pre id="log"></pre>
<button id="start">Loading...</button>
</body>
</html>

View file

@ -12,13 +12,13 @@
</style>
</head>
<body>
<script type="text/javascript" src="../lib/olm.js"></script>
<script type="text/javascript" src="../lib/olm/olm.js"></script>
<script type="module">
async function main() {
const Olm = window.Olm;
await Olm.init({
locateFile: () => "../lib/olm.wasm",
locateFile: () => "../lib/olm/olm.wasm",
});
const alice = new Olm.Account();
alice.create();

View file

@ -1496,6 +1496,10 @@ object.assign@^4.1.0:
has-symbols "^1.0.0"
object-keys "^1.0.11"
"olm@https://packages.matrix.org/npm/olm/olm-3.1.4.tgz":
version "3.1.4"
resolved "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz#0f03128b7d3b2f614d2216409a1dfccca765fdb3"
on-finished@~2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947"