Permalink
Browse files

smp in webworker

  • Loading branch information...
1 parent ac9d74b commit 1d7d9d3df3479e21cc83f1aa627b2610b8b857a9 @arlolra committed Jun 29, 2013
Showing with 628 additions and 404 deletions.
  1. +8 −5 .jshintrc
  2. +1 −4 lib/const.js
  3. +85 −2 lib/otr.js
  4. +52 −0 lib/sm-webworker.js
  5. +362 −378 lib/sm.js
  6. +4 −1 package.json
  7. +99 −1 test/spec/unit/sm.js
  8. +17 −13 vendor/bigint.js
View
@@ -12,10 +12,13 @@
, "white" : false
, "multistr" : true
, "globals" : {
- "it" : true
- , "beforeEach" : true
- , "before" : true
- , "describe" : true
- , "define" : true
+ "it" : true
+ , "beforeEach" : true
+ , "before" : true
+ , "describe" : true
+ , "define" : true
+ , "postMessage" : true
+ , "onmessage" : true
+ , "importScripts" : true
}
}
View
@@ -42,10 +42,7 @@
, STATUS_SEND_QUERY : 0
, STATUS_AKE_INIT : 1
, STATUS_AKE_SUCCESS : 2
- , STATUS_SMP_INIT : 3
- , STATUS_SMP_SECRET : 4
- , STATUS_SMP_ANSWER : 5
- , STATUS_END_OTR : 6
+ , STATUS_END_OTR : 3
}
View
@@ -3,12 +3,15 @@
var root = this
- var CryptoJS, BigInt, EventEmitter, CONST, HLP, Parse, AKE, SM, DSA
+ var CryptoJS, BigInt, EventEmitter, Worker, SMWPath
+ , CONST, HLP, Parse, AKE, SM, DSA
if (typeof module !== 'undefined' && module.exports) {
module.exports = OTR
CryptoJS = require('../vendor/crypto.js')
BigInt = require('../vendor/bigint.js')
EventEmitter = require('../vendor/eventemitter.js').EventEmitter
+ Worker = require('webworker-threads').Worker
+ SMWPath = require('path').join(__dirname, '/sm-webworker.js')
CONST = require('./const.js')
HLP = require('./helpers.js')
Parse = require('./parse.js')
@@ -26,6 +29,8 @@
CryptoJS = root.CryptoJS
BigInt = root.BigInt
EventEmitter = root.EventEmitter
+ Worker = root.Worker
+ SMWPath = 'sm-webworker.js'
CONST = OTR.CONST
HLP = OTR.HLP
Parse = OTR.Parse
@@ -72,6 +77,10 @@
// debug
this.debug = !!options.debug
+ // smp in webworker options
+ // this is still experimental and undocumented
+ this.smw = options.smw
+
// init vals
this.init()
@@ -142,8 +151,78 @@
this.ssid = null
}
+ // smp over webworker
+ OTR.prototype._SMW = function (otr, reqs) {
+ this.otr = otr
+ var opts = {
+ path: SMWPath
+ , seed: BigInt.getSeed
+ }
+ if (typeof otr.smw === 'object')
+ Object.keys(otr.smw).forEach(function (k) {
+ opts[k] = otr.smw[k]
+ })
+ this.worker = new Worker(opts.path)
+ var self = this
+ this.worker.onmessage = function (e) {
+ var d = e.data
+ if (!d) return
+ self.trigger(d.method, d.args)
+ }
+ this.worker.postMessage({
+ type: 'seed'
+ , seed: opts.seed()
+ , imports: opts.imports
+ })
+ this.worker.postMessage({
+ type: 'init'
+ , reqs: reqs
+ })
+ }
+
+ // inherit from EE
+ HLP.extend(OTR.prototype._SMW, EventEmitter)
+
+ // shim sm methods
+ ;['handleSM', 'rcvSecret', 'abort'].forEach(function (m) {
+ OTR.prototype._SMW.prototype[m] = function () {
+ this.worker.postMessage({
+ type: 'method'
+ , method: m
+ , args: Array.prototype.slice.call(arguments, 0)
+ })
+ }
+ })
+
OTR.prototype._smInit = function () {
- this.sm = new SM(this)
+ var reqs = {
+ ssid: this.ssid
+ , our_fp: this.priv.fingerprint()
+ , their_fp: this.their_priv_pk.fingerprint()
+ , debug: this.debug
+ }
+ if (this.smw) {
+ if (this.sm) this.sm.worker.close() // destroy prev webworker
+ this.sm = new this._SMW(this, reqs)
+ } else {
+ this.sm = new SM(reqs)
+ }
+ var self = this
+ this.sm.on('trust', function (trust) {
+ self.trust = trust
+ self.trigger('smp', ['trust', trust])
+ if (self.smw) {
+ this.worker.close()
+ self.sm = null
+ }
+ })
+ this.sm.on('question', function (question) {
+ self.trigger('smp', ['question', question])
+ })
+ this.sm.on('send', function (ssid, send) {
+ if (self.ssid === ssid)
+ self._sendMsg(send)
+ })
}
OTR.prototype.io = function (msg) {
@@ -406,6 +485,10 @@
case 2: case 3: case 4:
case 5: case 6: case 7:
// SMP
+ if (this.msgstate !== CONST.MSGSTATE_ENCRYPTED) {
+ if (this.sm) this.sm.abort()
+ return
+ }
this.sm.handleSM({ msg: msg, type: type })
break
case 8:
View
@@ -0,0 +1,52 @@
+;(function (root) {
+ "use strict";
+
+ root.OTR = {}
+ root.crypto = {}
+
+ // default imports
+ var imports = [
+ 'vendor/salsa20.js'
+ , 'vendor/bigint.js'
+ , 'vendor/crypto.js'
+ , 'vendor/eventemitter.js'
+ , 'lib/const.js'
+ , 'lib/helpers.js'
+ , 'lib/sm.js'
+ ]
+
+ function wrapPostMessage(method) {
+ return function () {
+ postMessage({
+ method: method
+ , args: Array.prototype.slice.call(arguments, 0)
+ })
+ }
+ }
+
+ var sm
+ onmessage = function (msg) {
+ var d = msg.data
+ switch (d.type) {
+ case 'seed':
+ root.crypto.randomBytes = function () {
+ return d.seed
+ }
+ if (d.imports) imports = d.imports
+ imports.forEach(function (i) {
+ importScripts(i)
+ })
+ break
+ case 'init':
+ sm = new root.OTR.SM(d.reqs)
+ sm.on('trust', wrapPostMessage('trust'))
+ sm.on('question', wrapPostMessage('question'))
+ sm.on('send', wrapPostMessage('send'))
+ break
+ case 'method':
+ sm[d.method].apply(sm, d.args)
+ break
+ }
+ }
+
+}(this))
Oops, something went wrong.

0 comments on commit 1d7d9d3

Please sign in to comment.