git ssb

1+

Dominic / ssb-private-groups



Commit e39f1662dc45833d09248b820abe6f694160a37a

fix groups

Dominic Tarr committed on 12/29/2018, 8:32:50 AM
Parent: 4de96c011f2e32da25a86df102a8af549f39d44c

Files changed

index.jschanged
package.jsonchanged
util.jschanged
index.jsView
@@ -3,9 +3,9 @@
33 var Reduce = require('flumeview-reduce')
44 var group_box = require('group-box')
55 var mkdirp = require('mkdirp')
66 var u = require('./util')
7-var cl = require('chloride')
7 +var ref = require('ssb-ref')
88
99 //by deriving the message key from the group id (the founding
1010 //message id) and the unbox key for that message, this ensures
1111 //someone can't decrypt the message without knowing the founding
@@ -16,17 +16,8 @@
1616 //and, you can verify this property from the design! you can't
1717 //rewrite this code so they don't know the founding message
1818 //and still be able to decrypt these messages.
1919
20-function hmac (a, b) {
21- return cl.crypto_auth(u.toBuffer(a), u.toBuffer(b))
22-}
23-
24-function getGroupMsgKey(previous, group) {
25- //or would it be better to use generic hash (with key?)
26- return hmac(Buffer.concat([previous, group.id]), u.toBuffer(group.unbox))
27-}
28-
2920 exports.name = 'private-groups'
3021
3122 exports.init = function (sbot, config) {
3223
@@ -103,28 +94,24 @@
10394 }
10495 if(!a_key) return console.log('no author key')
10596
10697 if(!keys_to_try)
107- keys_to_try = cache[value.author] = keyState.msgKeys.map(function (curve) {
108- return cl.crypto_scalarmult(
109- Buffer.from(curve.private, 'base64'),
110- Buffer.from(a_key, 'base64')
111- )
112- })
98 + keys_to_try = cache[value.author] = u.scalarmultKeys(a_key, keyState.msgKeys)
11399
114100 //the very first message cannot be a group_box.
115101 if(value.previous == null) return
116102 var ctxt = u.ctxt2Buffer(content)
117103 var nonce = u.id2Buffer(value.previous)
104 + console.log(content, ctxt, ctxt.length)
118105
119106 var key = group_box.unboxKey(ctxt, nonce, keys_to_try, 8)
120107 if(key) return key
121108
122109 //should group keys be included in this plugin?
123110 //yes, because box2 supports both direct keys and group keys.
124111 var group_keys = []
125112 for(var id in keyState.groupKeys)
126- group_keys.push(getGroupMsgKey(nonce, keyState.groupKeys[id]))
113 + group_keys.push(u.getGroupMsgKey(nonce, keyState.groupKeys[id]))
127114
128115 //note: if we only allow groups in the first 4 slots
129116 //that means better sort them before any individuals
130117 key = group_box.unboxKey( //groups we are in
@@ -145,11 +132,14 @@
145132
146133 return {
147134 get: remoteKeys.get,
148135 addGroupKey: function (group, cb) {
136 + console.log(group, u.isUnboxKey(group.unbox))
137 + if(!ref.isMsg(group.id)) return cb(new Error('id must be a message id'))
138 + if(!u.isUnboxKey(group.unbox)) return cb(new Error('id must be a 32 byte base64 value'))
149139 af.get(function () {
150- keyState.groupKeys[hmac(group.id, group.unbox)] = group
151- af.set(keys, cb)
140 + keyState.groupKeys[u.hmac(u.id2Buffer(group.id), Buffer.from(group.unbox, 'base64'))] = group
141 + af.set(keyState, cb)
152142 })
153143 },
154144 addCurvePair: function (curve_keys, cb) {
155145 onReady(function () {
@@ -176,4 +166,5 @@
176166 }
177167 }
178168 }
179169
170 +
package.jsonView
@@ -7,9 +7,11 @@
77 "type": "git",
88 "url": "git://github.com/dominictarr/ssb-private-groups.git"
99 },
1010 "dependencies": {
11- "atomic-file": "^1.1.5"
11 + "atomic-file": "^1.1.5",
12 + "group-box": "^0.1.0",
13 + "is-canonical-base64": "^1.1.1"
1214 },
1315 "devDependencies": {
1416 "ssb-server": "^13.4.0",
1517 "tape": "^4.9.1"
util.jsView
@@ -1,5 +1,11 @@
1 +var cl = require('chloride')
12
3 +//var rx = require('is-canonical-base64')(null, null, 32)
4 +exports.isUnboxKey = function (s) {
5 + return s === Buffer.from(s, 'base64').toString('base64')
6 +// return rx.test(s)
7 +}
28 exports.id2Buffer = function (id) {
39 return Buffer.from(id.substring(1, id.indexOf('.')), 'base64')
410 }
511
@@ -16,4 +22,29 @@
1622 exports.ctxt2Buffer = function (ctxt) {
1723 return exports.isBox2(ctxt) && Buffer.from(ctxt.substring(0, ctxt.indexOf('.')), 'base64')
1824 }
1925
26 +function toBuffer(b) {
27 + return Buffer.isBuffer(b) ? b : Buffer.from(b, 'base64')
28 +}
29 +
30 +exports.hmac = function (a, b) {
31 + return cl.crypto_auth(toBuffer(a), toBuffer(b))
32 +}
33 +
34 +exports.getGroupMsgKey = function (previous, group) {
35 + //or would it be better to use generic hash (with key?)
36 + return exports.hmac(Buffer.concat([previous, exports.id2Buffer(group.id)]), Buffer.from(group.unbox, 'base64'))
37 +}
38 +
39 +exports.scalarmultKeys = function (a_key, recps) {
40 + return recps.map(function (curve) {
41 + return cl.crypto_scalarmult(
42 + toBuffer(curve.private),
43 + toBuffer(a_key)
44 + )
45 + })
46 +}
47 +
48 +
49 +
50 +

Built with git-ssb-web