thingy-crypto-node 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +83 -38
- package/index.js +24 -38
- package/package.json +22 -8
- package/LICENSE +0 -9
- package/unbreaker.js +0 -4
package/README.md
CHANGED
|
@@ -1,57 +1,102 @@
|
|
|
1
1
|
# thingy-crypto-node
|
|
2
2
|
|
|
3
3
|
# Background
|
|
4
|
-
Thingy Nodes usually have an
|
|
4
|
+
Thingy Nodes usually have an asymmetric keypair - this way all authentication works between the nodes in the thingy-ecosystem.
|
|
5
5
|
The thingy-crypto-node is the keeper of the keypair of this node - implementing all basic crypto functionality for this specific keypair.
|
|
6
6
|
|
|
7
|
+
It is a convenience layer over [secret-manager-crypto-utils](https://www.npmjs.com/package/secret-manager-crypto-utils) - see there for the cryptographic details and security notes.
|
|
8
|
+
|
|
7
9
|
# Usage
|
|
8
|
-
|
|
9
|
-
---------------------
|
|
10
|
-
```coffescript
|
|
10
|
+
```js
|
|
11
11
|
import { ThingyCryptoNode } from "thingy-crypto-node"
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
secretKeyHex: "..."
|
|
15
|
-
publicKeyHex: "..."
|
|
16
|
-
context: "..."
|
|
17
|
-
}
|
|
18
|
-
|
|
13
|
+
const cryptoNode = new ThingyCryptoNode({
|
|
14
|
+
secretKeyHex: "...", // required
|
|
15
|
+
publicKeyHex: "...", // required
|
|
16
|
+
context: "..." // optional, default "standard ThingyCryptoNode"
|
|
17
|
+
})
|
|
18
|
+
```
|
|
19
19
|
|
|
20
|
+
## Signing and Verifying
|
|
21
|
+
```js
|
|
22
|
+
const signatureHex = await cryptoNode.sign(content) // content: string
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
const err = await cryptoNode.sigCheck(signatureHex, content)
|
|
25
|
+
if (err) throw new Error(err) // "Invalid signature!" or "Malformed input!"
|
|
26
|
+
// valid from here
|
|
27
|
+
```
|
|
28
|
+
**`sigCheck` resolves `undefined` when the signature is valid** and an error string when it is not. It never throws.
|
|
29
|
+
So the check is `if (err)` - and a forgotten `await` fails closed, because a Promise is truthy.
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
`sigCheck` checks signatures of this node's own key.
|
|
27
32
|
|
|
28
33
|
## Encrypting and Decrypting
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
secretsObject = { referencePointHex, encryptedContentHex }
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
##
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
sharedSecretHex
|
|
42
|
-
|
|
34
|
+
Encrypts for this node's own key - e.g. for storing secrets only this node can read.
|
|
35
|
+
```js
|
|
36
|
+
const secretsObject = await cryptoNode.encrypt(content) // { referencePointHex, encryptedContentHex }
|
|
37
|
+
const content = await cryptoNode.decrypt(secretsObject)
|
|
38
|
+
```
|
|
39
|
+
`decrypt` rejects with `Error("Decryption failed!")` on a wrong key or changed data.
|
|
40
|
+
|
|
41
|
+
## Diffie-Hellman shared secret with a specific foreign node
|
|
42
|
+
```js
|
|
43
|
+
const sharedSecretHex = await cryptoNode.diffieHellmanFor(id, salt, timestamp)
|
|
44
|
+
// id: the other node's publicKeyHex
|
|
45
|
+
// salt: string, timestamp: number
|
|
46
|
+
// sharedSecretHex: 128 hex chars (64 bytes)
|
|
47
|
+
```
|
|
48
|
+
Both nodes get the same secret when they use the same `salt`, `timestamp` and `context`.
|
|
49
|
+
|
|
50
|
+
# Breaking Updates
|
|
51
|
+
|
|
52
|
+
`0.0.x` -> `0.1.y`
|
|
53
|
+
------------------
|
|
54
|
+
Updated to secret-manager-crypto-utils 0.4 - new encryption format and new shared secrets.
|
|
55
|
+
|
|
56
|
+
| 0.0.x | 0.1.x | What changed |
|
|
57
|
+
|---|---|---|
|
|
58
|
+
| `sign` | `sign` | nothing - same signatures |
|
|
59
|
+
| `verify` → `true` when valid | `sigCheck` → `undefined` when valid | **removed, inverted meaning** - see below |
|
|
60
|
+
| `encrypt` | `encrypt` | new format, 0.0.x cannot decrypt it |
|
|
61
|
+
| `decrypt` | `decrypt` | reads the new format only |
|
|
62
|
+
| `diffieHellmanFor` | `diffieHellmanFor` | different secret than 0.0.x |
|
|
63
|
+
|
|
64
|
+
- `verify` is removed on purpose, so every old call fails loudly with `TypeError: cryptoNode.verify is not a function` - instead of silently accepting invalid signatures. Mind the inverted meaning:
|
|
65
|
+
```js
|
|
66
|
+
// before
|
|
67
|
+
if (!(await cryptoNode.verify(sig, content))) return reject()
|
|
68
|
+
// now
|
|
69
|
+
if (await cryptoNode.sigCheck(sig, content)) return reject()
|
|
70
|
+
```
|
|
71
|
+
- Signatures are compatible both ways: 0.0.x nodes verify signatures of 0.1.x nodes and vice versa.
|
|
72
|
+
|
|
73
|
+
## Legacy functions
|
|
74
|
+
**Only for compatibility with data and nodes of 0.0.x. Never use them in new code. They will be removed in a future version.**
|
|
75
|
+
|
|
76
|
+
| Function | 1:1 like 0.0.x | Use for |
|
|
77
|
+
|---|---|---|
|
|
78
|
+
| `verifyLegacy(sig, content)` | `verify` → `true` when valid (malformed input → `false` instead of throwing) | a quick fix until you switch to `sigCheck` |
|
|
79
|
+
| `decryptLegacy(secretsObject)` | `decrypt` | reading data encrypted with 0.0.x -> encrypting it in new format is recommended |
|
|
80
|
+
| `diffieHellmanForLegacy(id, salt, timestamp)` | `diffieHellmanFor` | nodes that still run 0.0.x shared secrets |
|
|
81
|
+
|
|
82
|
+
Migrating encrypted data:
|
|
83
|
+
```js
|
|
84
|
+
let content
|
|
85
|
+
try { content = await cryptoNode.decrypt(secretsObject) }
|
|
86
|
+
catch { content = await cryptoNode.decryptLegacy(secretsObject) }
|
|
87
|
+
secretsObject = await cryptoNode.encrypt(content) // store this one
|
|
43
88
|
```
|
|
44
89
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
90
|
+
Talking to a node that still runs 0.0.x:
|
|
91
|
+
```js
|
|
92
|
+
const sharedSecretHex = peerIsLegacy
|
|
93
|
+
? await cryptoNode.diffieHellmanForLegacy(id, salt, timestamp)
|
|
94
|
+
: await cryptoNode.diffieHellmanFor(id, salt, timestamp)
|
|
95
|
+
```
|
|
96
|
+
There is no way to detect a legacy node from the secret itself: you have to know which version the other node runs.
|
|
97
|
+
Anything that was keyed with an old shared secret has to be re-derived.
|
|
53
98
|
|
|
54
99
|
---
|
|
55
100
|
|
|
56
101
|
# License
|
|
57
|
-
[
|
|
102
|
+
[CC0](https://creativecommons.org/publicdomain/zero/1.0/)
|
package/index.js
CHANGED
|
@@ -1,44 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var defaultContext;
|
|
1
|
+
import * as secUtl from "secret-manager-crypto-utils"
|
|
2
|
+
import * as legacy from "secret-manager-crypto-utils/legacy"
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
const defaultContext = "standard ThingyCryptoNode"
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
constructor(o) {
|
|
13
|
-
this.key = o.secretKeyHex;
|
|
14
|
-
this.id = o.publicKeyHex;
|
|
15
|
-
this.ctx = o.context;
|
|
16
|
-
if (this.ctx == null) {
|
|
17
|
-
this.ctx = defaultContext;
|
|
6
|
+
export class ThingyCryptoNode {
|
|
7
|
+
constructor(o) {
|
|
8
|
+
this.key = o.secretKeyHex
|
|
9
|
+
this.id = o.publicKeyHex
|
|
10
|
+
this.ctx = o.context ?? defaultContext
|
|
18
11
|
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
//#######################################################
|
|
22
|
-
async sign(content) {
|
|
23
|
-
return (await secUtl.createSignatureHex(content, this.key));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async verify(sig, content) {
|
|
27
|
-
return (await secUtl.verifyHex(sig, this.id, content));
|
|
28
|
-
}
|
|
29
12
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
13
|
+
sign(content) { return secUtl.createSignature(content, this.key) }
|
|
14
|
+
// undefined when valid, an error string otherwise - never throws
|
|
15
|
+
sigCheck(sig, content) { return secUtl.verifySignature(sig, this.id, content) }
|
|
16
|
+
encrypt(content) { return secUtl.asymmetricEncrypt(content, this.id) }
|
|
17
|
+
decrypt(secretsObj) { return secUtl.asymmetricDecrypt(secretsObj, this.key) }
|
|
18
|
+
diffieHellmanFor(id, salt, timestamp) {
|
|
19
|
+
return secUtl.diffieHellmanSecretHash(this.key, id, contextFor(salt, this.ctx, timestamp))
|
|
20
|
+
}
|
|
37
21
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
22
|
+
// Legacy: 1:1 with thingy-crypto-node 0.0.x - only for compatibility, will be removed
|
|
23
|
+
async verifyLegacy(sig, content) { return !(await this.sigCheck(sig, content)) }
|
|
24
|
+
decryptLegacy(secretsObj) { return legacy.asymmetricDecryptV0_3_1(secretsObj, this.key) }
|
|
25
|
+
diffieHellmanForLegacy(id, salt, timestamp) {
|
|
26
|
+
return legacy.diffieHellmanSecretHashV0_3_1(this.key, id, contextFor(salt, this.ctx, timestamp))
|
|
27
|
+
}
|
|
28
|
+
}
|
|
43
29
|
|
|
44
|
-
}
|
|
30
|
+
const contextFor = (salt, ctx, timestamp) => `${salt}:${ctx}_${timestamp}`
|
package/package.json
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "thingy-crypto-node",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "The thingy-crypto-node is the keeper of an ed25519 keypair, implementing all basic crypto functionality for this specific keypair",
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
"keywords": [
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js"
|
|
8
|
+
],
|
|
9
|
+
"keywords": [
|
|
10
|
+
"thingy",
|
|
11
|
+
"crypto",
|
|
12
|
+
"encryption",
|
|
13
|
+
"signature",
|
|
14
|
+
"ed25519",
|
|
15
|
+
"el-gamal",
|
|
16
|
+
"diffie-hellman"
|
|
17
|
+
],
|
|
10
18
|
"author": "Lenard Frommelt lenard@frommelt.dev",
|
|
11
|
-
"license": "
|
|
19
|
+
"license": "CC0-1.0",
|
|
12
20
|
"repository": {
|
|
13
21
|
"type": "git",
|
|
14
22
|
"url": "git+https://github.com/JhonnyJason/thingy-crypto-node.git"
|
|
@@ -18,7 +26,13 @@
|
|
|
18
26
|
},
|
|
19
27
|
"homepage": "https://github.com/JhonnyJason/thingy-crypto-node-output#readme",
|
|
20
28
|
"dependencies": {
|
|
21
|
-
"secret-manager-crypto-utils": "^0.
|
|
29
|
+
"secret-manager-crypto-utils": "^0.4.0"
|
|
22
30
|
},
|
|
23
|
-
"type": "module"
|
|
31
|
+
"type": "module",
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
37
|
+
}
|
|
24
38
|
}
|
package/LICENSE
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
This is free and unencumbered software released into the public domain.
|
|
2
|
-
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
|
|
3
|
-
|
|
4
|
-
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and
|
|
5
|
-
successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
-
|
|
9
|
-
For more information, please refer to <http://unlicense.org/>
|
package/unbreaker.js
DELETED