Skip to content

Commit

Permalink
namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
arlolra committed Aug 25, 2012
1 parent f6ec569 commit dcbf9d5
Show file tree
Hide file tree
Showing 21 changed files with 780 additions and 827 deletions.
1,169 changes: 571 additions & 598 deletions build/otr.js

Large diffs are not rendered by default.

Binary file modified build/otr.min.js
Binary file not shown.
25 changes: 13 additions & 12 deletions grunt.js
Expand Up @@ -19,26 +19,27 @@ module.exports = function (grunt) {
pkg: '<json:package.json>'
, meta: {
banner:
'/*!\n\n <%= pkg.name %>.js v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
' (c) <%= grunt.template.today("yyyy") %> - <%= pkg.author %>\n' +
' Freely distributed under the <%= pkg.license %> license.\n\n' +
' This file is concatenated for the browser.\n' +
' Please see: <%= pkg.homepage %>' +
'\n\n*/',
cryptojs: 'module.exports = CryptoJS'
'/*!\n\n <%= pkg.name %>.js v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
' (c) <%= grunt.template.today("yyyy") %> - <%= pkg.author %>\n' +
' Freely distributed under the <%= pkg.license %> license.\n\n' +
' This file is concatenated for the browser.\n' +
' Please see: <%= pkg.homepage %>' +
'\n\n*/'
, cryptojs: 'module.exports = CryptoJS'
, globals: 'var OTR = {}, DSA = {}'
}
, concat: {
otr: {
src: [
'<banner:meta.banner>'
, 'lib/dh.js'
, 'lib/states.js'
, '<banner:meta.globals>'
, 'lib/const.js'
, 'lib/helpers.js'
, 'lib/dsa.js'
, 'lib/sm.js'
, 'lib/ake.js'
, 'lib/parse.js'
, 'lib/ake.js'
, 'lib/sm.js'
, 'lib/otr.js'
]
, dest: 'build/otr.js'
Expand Down
4 changes: 4 additions & 0 deletions index.js
@@ -0,0 +1,4 @@
module.exports = {
DSA: require('./lib/dsa.js')
, OTR: require('./lib/otr.js')
}
59 changes: 27 additions & 32 deletions lib/ake.js
Expand Up @@ -2,32 +2,27 @@

var root = this

var CryptoJS, BigInt, CONST, HLP, DSA
if (typeof exports !== 'undefined') {
module.exports = AKE
CryptoJS = require('../vendor/crypto.js')
BigInt = require('../vendor/bigint.js')
CONST = require('./const.js')
HLP = require('./helpers.js')
DSA = require('./dsa.js')
} else {
root.AKE = AKE
}

var CryptoJS = root.CryptoJS
, BigInt = root.BigInt
, DH = root.DH
, HLP = root.HLP
, DSA = root.DSA
, STATES = root.STATES

if (typeof require !== 'undefined') {
CryptoJS || (CryptoJS = require('../vendor/crypto.js'))
BigInt || (BigInt = require('../vendor/bigint.js'))
DH || (DH = require('./dh.js'))
HLP || (HLP = require('./helpers.js'))
DSA || (DSA = require('./dsa.js'))
STATES || (STATES = require('./states.js'))
root.OTR.AKE = AKE
CryptoJS = root.CryptoJS
BigInt = root.BigInt
CONST = root.OTR.CONST
HLP = root.OTR.HLP
DSA = root.DSA
}

// diffie-hellman modulus and generator
// see group 5, RFC 3526
var G = BigInt.str2bigInt(DH.G, 10)
var N = BigInt.str2bigInt(DH.N, 16)
var G = BigInt.str2bigInt(CONST.G, 10)
var N = BigInt.str2bigInt(CONST.N, 16)
var TWO = BigInt.str2bigInt('2', 10)
var N_MINUS_2 = BigInt.sub(N, TWO)

Expand Down Expand Up @@ -166,8 +161,8 @@
this.otr.smInit()

// go encrypted
this.otr.authstate = STATES.AUTHSTATE_NONE
this.otr.msgstate = STATES.MSGSTATE_ENCRYPTED
this.otr.authstate = CONST.AUTHSTATE_NONE
this.otr.msgstate = CONST.MSGSTATE_ENCRYPTED

// send stored msgs
this.otr.sendStored()
Expand All @@ -185,7 +180,7 @@

msg = HLP.splitype(['DATA', 'DATA'], msg.msg)

if (this.otr.authstate === STATES.AUTHSTATE_AWAITING_DHKEY) {
if (this.otr.authstate === CONST.AUTHSTATE_AWAITING_DHKEY) {
var ourHash = HLP.readMPI(this.myhashed)
var theirHash = HLP.readMPI(msg[1])
if (BigInt.greater(ourHash, theirHash)) {
Expand All @@ -194,15 +189,15 @@
} else {
// forget
this.our_dh = this.otr.dh()
this.otr.authstate = STATES.AUTHSTATE_NONE
this.otr.authstate = CONST.AUTHSTATE_NONE
this.r = null
this.myhashed = null
}
} else if (
this.otr.authstate === STATES.AUTHSTATE_AWAITING_SIG
this.otr.authstate === CONST.AUTHSTATE_AWAITING_SIG
) this.our_dh = this.otr.dh()

this.otr.authstate = STATES.AUTHSTATE_AWAITING_REVEALSIG
this.otr.authstate = CONST.AUTHSTATE_AWAITING_REVEALSIG

this.encrypted = msg[0].substring(4)
this.hashed = msg[1].substring(4)
Expand All @@ -218,15 +213,15 @@

msg = HLP.splitype(['MPI'], msg.msg)

if (this.otr.authstate !== STATES.AUTHSTATE_AWAITING_DHKEY) {
if (this.otr.authstate === STATES.AUTHSTATE_AWAITING_SIG) {
if (this.otr.authstate !== CONST.AUTHSTATE_AWAITING_DHKEY) {
if (this.otr.authstate === CONST.AUTHSTATE_AWAITING_SIG) {
if (!BigInt.equals(this.their_y, HLP.readMPI(msg[0]))) return
} else {
return // ignore
}
}

this.otr.authstate = STATES.AUTHSTATE_AWAITING_SIG
this.otr.authstate = CONST.AUTHSTATE_AWAITING_SIG

this.their_y = HLP.readMPI(msg[0])

Expand All @@ -245,7 +240,7 @@
debug('signature message')

if ( !this.otr.ALLOW_V2 ||
this.otr.authstate !== STATES.AUTHSTATE_AWAITING_REVEALSIG
this.otr.authstate !== CONST.AUTHSTATE_AWAITING_REVEALSIG
) return // ignore

msg = HLP.splitype(['DATA', 'DATA', 'MAC'], msg.msg)
Expand Down Expand Up @@ -303,7 +298,7 @@
debug('data message')

if ( !this.otr.ALLOW_V2 ||
this.otr.authstate !== STATES.AUTHSTATE_AWAITING_SIG
this.otr.authstate !== CONST.AUTHSTATE_AWAITING_SIG
) return // ignore

msg = HLP.splitype(['DATA', 'MAC'], msg.msg)
Expand Down Expand Up @@ -337,7 +332,7 @@
},

sendMsg: function (msg) {
msg = STATES.OTR_VERSION_2 + msg
msg = CONST.OTR_VERSION_2 + msg
msg = HLP.wrapMsg(msg, this.otr.fragment_size)
if (msg[0]) return this.otr.error(msg[0])
this.otr.sendMsg(msg[1], true)
Expand All @@ -349,7 +344,7 @@
// save in case we have to resend
this.dhcommit = '\x02'

this.otr.authstate = STATES.AUTHSTATE_AWAITING_DHKEY
this.otr.authstate = CONST.AUTHSTATE_AWAITING_DHKEY

var gxmpi = HLP.packMPI(this.our_dh.publicKey)

Expand Down
12 changes: 8 additions & 4 deletions lib/states.js → lib/const.js
Expand Up @@ -2,10 +2,14 @@

var root = this

var STATES = {
var CONST = {

// diffie-heilman
N : 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF'
, G : '2'

// otr message states
MSGSTATE_PLAINTEXT : 0
, MSGSTATE_PLAINTEXT : 0
, MSGSTATE_ENCRYPTED : 1
, MSGSTATE_FINISHED : 2

Expand All @@ -26,9 +30,9 @@
}

if (typeof exports !== 'undefined') {
module.exports = STATES
module.exports = CONST
} else {
root.STATES = STATES
root.OTR.CONST = CONST
}

}).call(this)
16 changes: 0 additions & 16 deletions lib/dh.js

This file was deleted.

40 changes: 19 additions & 21 deletions lib/dsa.js
Expand Up @@ -5,21 +5,21 @@

var root = this

var DSA
var CryptoJS, BigInt, HLP
if (typeof exports !== 'undefined') {
DSA = exports
module.exports = DSA
CryptoJS = require('../vendor/crypto.js')
BigInt = require('../vendor/bigint.js')
HLP = require('./helpers.js')
} else {
DSA = root.DSA = {}
}

var BigInt = root.BigInt
, CryptoJS = root.CryptoJS
, HLP = root.HLP

if (typeof require !== 'undefined') {
BigInt || (BigInt = require('../vendor/bigint.js'))
CryptoJS || (CryptoJS = require('../vendor/crypto.js'))
HLP || (HLP = require('./helpers.js'))
// copy over and expose internals
Object.keys(root.DSA).forEach(function (k) {
DSA[k] = root.DSA[k]
})
root.DSA = DSA
CryptoJS = root.CryptoJS
BigInt = root.BigInt
HLP = DSA.HLP
}

var ZERO = BigInt.str2bigInt('0', 10)
Expand Down Expand Up @@ -52,10 +52,8 @@
return k
}

DSA.Key = Key

function Key() {
if (!(this instanceof Key)) return new Key()
function DSA() {
if (!(this instanceof DSA)) return new DSA()

this.N = 160
this.L = 1024
Expand All @@ -69,9 +67,9 @@
this.y = BigInt.powMod(this.g, this.x, this.p)
}

Key.prototype = {
DSA.prototype = {

constructor: Key,
constructor: DSA,

makePQ: function() {
var g = this.N
Expand Down Expand Up @@ -217,8 +215,8 @@
}

DSA.inherit = function (key) {
key.__proto__ = DSA.Key.prototype
key.constructor = DSA.Key
key.__proto__ = DSA.prototype
key.constructor = DSA
key.type = '\x00\x00'
}

Expand Down
18 changes: 8 additions & 10 deletions lib/helpers.js
Expand Up @@ -2,19 +2,17 @@

var root = this

var HLP
var HLP, CryptoJS, BigInt
if (typeof exports !== 'undefined') {
HLP = exports
CryptoJS = require('../vendor/crypto.js')
BigInt = require('../vendor/bigint.js')
} else {
HLP = root.HLP = {}
}

var BigInt = root.BigInt
, CryptoJS = root.CryptoJS

if (typeof require !== 'undefined') {
BigInt || (BigInt = require('../vendor/bigint.js'))
CryptoJS || (CryptoJS = require('../vendor/crypto.js'))
HLP = {}
if (root.OTR) root.OTR.HLP = HLP
if (root.DSA) root.DSA.HLP = HLP
CryptoJS = root.CryptoJS
BigInt = root.BigInt
}

// data types (byte lengths)
Expand Down

0 comments on commit dcbf9d5

Please sign in to comment.