Browse Source

Merge branch 'pack_unpack' of ssh://ledger-git.dyne.org:2240/CoBox/cobox-crypto into pack_unpack

Kieran Gibb 3 years ago
parent
commit
3265b70190
No account linked to committer's email address
2 changed files with 32 additions and 12 deletions
  1. 12
    1
      index.js
  2. 20
    11
      test/index.test.js

+ 12
- 1
index.js View File

@@ -33,6 +33,8 @@ class Crypto {
33 33
   }
34 34
 
35 35
   pack (pubKey, symKey) {
36
+    pubKey = this.toBuf(pubKey, sodium.crypto_sign_PUBLICKEYBYTES)
37
+    symKey = this.toBuf(symKey, sodium.crypto_secretbox_KEYBYTES)
36 38
     const accessKey = sodium.sodium_malloc(sodium.crypto_sign_PUBLICKEYBYTES + sodium.crypto_secretbox_KEYBYTES)
37 39
     pubKey.copy(accessKey)
38 40
     symKey.copy(accessKey, sodium.crypto_secretbox_KEYBYTES)
@@ -40,7 +42,7 @@ class Crypto {
40 42
   }
41 43
 
42 44
   unpack (key) {
43
-    if (key.length < sodium.crypto_sign_PUBLICKEYBYTES) throw new Error('Invalid key length')
45
+    key = this.toBuf(key, [sodium.crypto_sign_PUBLICKEYBYTES, sodium.crypto_sign_PUBLICKEYBYTES + sodium.crypto_secretbox_KEYBYTES])
44 46
     if (key.length === sodium.crypto_sign_PUBLICKEYBYTES) return { publicKey: key }
45 47
     const publicKey = key.slice(0, sodium.crypto_sign_PUBLICKEYBYTES)
46 48
     const symmetricKey = key.slice(sodium.crypto_secretbox_KEYBYTES)
@@ -56,6 +58,15 @@ class Crypto {
56 58
       length === sodium.crypto_sign_PUBLICKEYBYTES + sodium.crypto_secretbox_KEYBYTES
57 59
   }
58 60
 
61
+  toBuf (stringOrBuffer, lengths) {
62
+    if (typeof lengths === 'number') lengths = [lengths]
63
+    if ((Buffer.isBuffer(stringOrBuffer)) && (lengths.indexOf(stringOrBuffer.length) > -1)) return stringOrBuffer
64
+    assert((typeof stringOrBuffer === 'string'), 'Key is incorrect type')
65
+    const res = Buffer.from(stringOrBuffer, 'hex')
66
+    assert((lengths.indexOf(res.length) > -1), 'Invalid key')
67
+    return res
68
+  }
69
+
59 70
   encoder (encryptionKey, opts = {}) {
60 71
     var can = Buffer.isBuffer(encryptionKey) &&
61 72
       (encryptionKey.length === sodium.crypto_secretbox_KEYBYTES)

+ 20
- 11
test/index.test.js View File

@@ -45,6 +45,19 @@ describe('key generation', (context) => {
45 45
     next()
46 46
   })
47 47
 
48
+  context('pack an access key given as strings', (assert, next) => {
49
+    const pubKey = crypto.keyPair().publicKey
50
+    const secretKey = crypto.symmetricKey()
51
+
52
+    const accessKey = crypto.pack(pubKey.toString('hex'), secretKey.toString('hex'))
53
+    assert.ok(accessKey, 'Key successfully generated')
54
+    assert.ok(accessKey instanceof Buffer, 'Access key is a buffer')
55
+    assert.same(accessKey.length, 64, 'read key is 64 bytes')
56
+    assert.same(accessKey.slice(0, 32), pubKey)
57
+    assert.same(accessKey.slice(32, 64), secretKey)
58
+    next()
59
+  })
60
+
48 61
   context('unpack an access key', (assert, next) => {
49 62
     const accessKey = crypto.accessKey()
50 63
 
@@ -67,19 +80,15 @@ describe('key generation', (context) => {
67 80
     next()
68 81
   })
69 82
 
70
-  context('check a key is a valid blind replication / public key', (assert, next) => {
83
+  context('unpack an access key given as a string', (assert, next) => {
71 84
     const accessKey = crypto.accessKey()
72
-    assert.same(false, crypto.isBlindKey(accessKey), 'access key is not a valid blind replication key')
73
-    const blindKey = crypto.keyPair().publicKey
74
-    assert.same(true, crypto.isBlindKey(blindKey), '32 byte public key is a valid blind replication key')
75
-    next()
76
-  })
77 85
 
78
-  context('check a key is a valid access key', (assert, next) => {
79
-    const blindKey = crypto.keyPair().publicKey
80
-    assert.same(crypto.isAccessKey(blindKey), false, '32 byte public key is not a valid access key')
81
-    const accessKey = crypto.accessKey()
82
-    assert.same(crypto.isAccessKey(accessKey), true, '64 byte access key is a valid access key')
86
+    const keys = crypto.unpack(accessKey.toString('hex'))
87
+    const pubKey = accessKey.slice(0, 32)
88
+    const symKey = accessKey.slice(32, 64)
89
+
90
+    assert.same(keys.publicKey, pubKey, 'Unpacks the public key')
91
+    assert.same(keys.symmetricKey, symKey, 'Unpacks the symmetric key')
83 92
     next()
84 93
   })
85 94
 })