git ssb

1+

Dominic / ssb-keys



Commit 4a7f18b92164ae999f72c2edcdebf98a06b763c8

expose nacl and eccjs together

Dominic Tarr committed on 5/28/2015, 8:05:46 PM
Parent: 282cefe13443ab844ba055dac8ac8678bcb1f65d

Files changed

index.jschanged
index.jsView
@@ -1,15 +1,19 @@
11 var fs = require('fs')
22 var crypto = require('crypto')
33 var ecc = require('eccjs')
4-var k256 = ecc.curves.k256
4+
55 var Blake2s = require('blake2s')
66 var mkdirp = require('mkdirp')
77 var path = require('path')
8-var curve = ecc.curves.k256
8+var nacl = require('ecma-nacl')
9+
910 var createHmac = require('hmac')
1011 var deepEqual = require('deep-equal')
1112
13+var k256 = curve = ecc.curves.k256
14+
15+
1216 function clone (obj) {
1317 var _obj = {}
1418 for(var k in obj) {
1519 if(Object.hasOwnProperty.call(obj, k))
@@ -21,9 +25,8 @@
2125 function hash (data, enc) {
2226 return new Blake2s().update(data, enc).digest('base64') + '.blake2s'
2327 }
2428
25-
2629 function isHash (data) {
2730 return isString(data) && /^[A-Za-z0-9\/+]{43}=\.blake2s$/.test(data)
2831 }
2932
@@ -40,39 +43,47 @@
4043
4144 function empty(v) { return !!v }
4245
4346 function constructKeys() {
44- var privateKey = crypto.randomBytes(32)
45- var k = keysToBase64(ecc.restore(k256, privateKey))
46- k.keyfile = [
47+ var keys = exports.generate()
48+ //var privateKey = crypto.randomBytes(32)
49+ //var k = keysToBase64(ecc.restore(k256, privateKey))
50+ keys.keyfile = [
4751 '# this is your SECRET name.',
4852 '# this name gives you magical powers.',
4953 '# with it you can mark your messages so that your friends can verify',
5054 '# that they really did come from you.',
5155 '#',
5256 '# if any one learns this name, they can use it to destroy your identity',
5357 '# NEVER show this to anyone!!!',
5458 '',
55- k.private.toString('hex'),
59+ keys.private,
5660 '',
5761 '# WARNING! It\'s vital that you DO NOT edit OR share your secret name',
5862 '# instead, share your public name',
59- '# your public name: ' + k.id.toString('hex')
63+ '# your public name: ' + keys.id
6064 ].join('\n')
61- return k
65+ return keys
6266 }
6367
6468
6569 function toBuffer(buf) {
6670 if(buf == null) return buf
6771 return new Buffer(buf.substring(0, buf.indexOf('.')), 'base64')
6872 }
6973
74+function toUint8(buf) {
75+ return new Uint8Array(toBuffer(buf))
76+}
77+
7078 function keysToBase64 (keys) {
71- var pub = tag(keys.public, 'k256')
79+
80+ var pub = tag(new Buffer(keys.public), keys.curve)
7281 return {
82+ curve: keys.curve,
83+ type: keys.type,
7384 public: pub,
74- private: tag(keys.private, 'k256'),
85+ private: tag(keys.private, keys.curve),
7586 id: hash(pub)
7687 }
7788 }
7889
@@ -87,23 +98,46 @@
8798 private: toBuffer(key.private)
8899 }
89100 }
90101
91-function reconstructKeys(privateKeyStr) {
92- privateKeyStr = privateKeyStr
102+function reconstructKeys(keyfile) {
103+ var private = keyfile
93104 .replace(/\s*\#[^\n]*/g, '')
94105 .split('\n').filter(empty).join('')
95106
96- var privateKey = (
97- !/\./.test(privateKeyStr)
98- ? new Buffer(privateKeyStr, 'hex')
99- : toBuffer(privateKeyStr)
100- )
107+ var i = private.indexOf('.')
101108
102- return keysToBase64(ecc.restore(k256, privateKey))
109+ var curve = private.substring(i+1)
110+// private = private.substring(0, i)
111+
112+
113+// var privateKey = (
114+// !/\./.test(privateKeyStr)
115+// ? new Buffer(privateKeyStr, 'hex')
116+// : toBuffer(privateKeyStr)
117+// )
118+
119+
120+ if(curve === 'ed25519') {
121+ var pub = tag(
122+ new Buffer(nacl.signing.extract_pkey(toUint8(private))),
123+ curve
124+ )
125+ return {
126+ type: curve === 'ed25519' ? 'nacl' : 'eccjs',
127+ curve: curve,
128+ private: private,
129+ public: pub,
130+ id: hash(pub)
131+ }
132+
133+ }
134+
135+ return keysToBase64(ecc.restore(k256, toBuffer(private)))
103136 }
104137
105138 function tag (key, tag) {
139+ if(!tag) throw new Error('no tag for:' + key.toString('base64'))
106140 return key.toString('base64')+'.' + tag.replace(/^\./, '')
107141 }
108142
109143 var toNameFile = exports.toNameFile = function (namefile) {
@@ -166,26 +200,77 @@
166200
167201 //this should return a key pair:
168202 // {public: Buffer, private: Buffer}
169203
170-exports.generate = function () {
171- return keysToBase64(ecc.restore(curve, crypto.randomBytes(32)))
204+exports.generate = function (curve) {
205+ var _keys = nacl.signing.generate_keypair(
206+ new Uint8Array(crypto.randomBytes(32))
207+ )
208+
209+ if(!curve) curve = 'ed25519'
210+
211+ if(curve === 'ed25519')
212+ return keysToBase64({
213+ type: 'nacl',
214+ curve: 'ed25519',
215+ public: new Buffer(_keys.pkey),
216+ private: new Buffer(_keys.skey),
217+ })
218+
219+ else if(curve === 'k256')
220+ return keysToBase64(ecc.restore(curve, crypto.randomBytes(32)))
172221 }
173222
174223 //takes a public key and a hash and returns a signature.
175224 //(a signature must be a node buffer)
176225 exports.sign = function (keys, hash) {
177226 var hashTag = hash.substring(hash.indexOf('.'))
178- return tag(
179- ecc.sign(curve, keysToBuffer(keys), hashToBuffer(hash)),
180- hashTag + '.k256'
181- )
227+
228+ if(keys.curve === 'ed25519')
229+ return tag(new Buffer(nacl.signing.sign(
230+ new Uint8Array(hashToBuffer(hash)),
231+ new Uint8Array(toBuffer(keys.private))
232+ )),
233+ hashTag + '.ed25519'
234+ )
235+
236+ else if(keys.curve === 'k256')
237+ return tag(
238+ ecc.sign(curve, keysToBuffer(keys), hashToBuffer(hash)),
239+ hashTag + '.k256'
240+ )
241+
242+ else
243+ throw new Error('unknown keys')
182244 }
183245
184246 //takes a public key, signature, and a hash
185247 //and returns true if the signature was valid.
186-exports.verify = function (pub, sig, hash) {
187- return ecc.verify(curve, keysToBuffer(pub), toBuffer(sig), hashToBuffer(hash))
248+exports.verify = function (keys, sig, hash) {
249+ //types all match.
250+ var curve = keys.curve
251+ if(!keys.curve && isString(keys.public))
252+ keys = keys.public
253+
254+ if(isString(keys))
255+ curve = keys.substring(keys.indexOf('.')+1)
256+
257+ if(curve === 'ed25519') {
258+ return nacl.signing.verify(
259+ new Uint8Array(toBuffer(sig)),
260+ new Uint8Array(hashToBuffer(hash)),
261+ new Uint8Array(toBuffer(keys.public || keys))
262+ )
263+ }
264+ else if(keys.curve === 'k256')
265+ return ecc.verify(
266+ curve,
267+ keysToBuffer(keys),
268+ toBuffer(sig),
269+ hashToBuffer(hash)
270+ )
271+ else
272+ throw new Error('unknown curve:' + JSON.stringify(keys))
188273 }
189274
190275 function createHash() {
191276 return new Blake2s()
@@ -235,16 +320,4 @@
235320 ts: Date.now(),
236321 public: keys.public
237322 })
238323 }
239-
240-exports.codec = {
241- decode: function (string) {
242- return JSON.parse(string)
243- },
244- encode: function (obj) {
245- return JSON.stringify(obj, null, 2)
246- },
247- buffer: false
248-}
249-
250-exports.keys = exports

Built with git-ssb-web