thingy-crypto-node 0.0.2 → 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 CHANGED
@@ -1,55 +1,100 @@
1
1
  # thingy-crypto-node
2
2
 
3
3
  # Background
4
- Thingy Nodes usually have an assymetric keypair - this way all authentication works between the nodes in the thingy-ecosystem.
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
- Current Functionality
9
- ---------------------
10
- ```coffescript
10
+ ```js
11
11
  import { ThingyCryptoNode } from "thingy-crypto-node"
12
12
 
13
- options = {
14
- secretKeyHex: "..." # required
15
- publicKeyHex: "..." # required
16
- context: "..." # optional
17
- }
18
- cryptoNode = new ThingyCryptoNode(options)
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
- ## Signing and Verifiying
22
- content = STRING
23
- signatureHex = await cryptoNode.sign(content)
24
- signatureHex = STRINGHEX
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
- true = await cryptoNode.verify(signatureHex, content)
31
+ `sigCheck` checks signatures of this node's own key.
27
32
 
28
33
  ## Encrypting and Decrypting
29
- content = STRING
30
- secretsObject = await cryptoNode.encrypt(content)
31
- secretsObject = { referencePointHex, encryptedContentHex }
32
- secretsObject = { STRINGHEX, STRINGHEX }
33
-
34
- content = await cryptoNode.decrypt(secretsObject)
35
-
36
- ## DiffieHellman For shared secret with specific foreign node
37
- id = STRINGHEX64
38
- salt = STRING
39
- timestamp = NUMBER
40
- sharedSecretHex = await cryptoNode.diffieHellmanFor(id, salt, timestamp)
41
- sharedSecretHex = STRINGHEX128
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
- # Further steps
48
-
49
- - Add more features when the requirement pops up.
50
-
51
-
52
- All sorts of inputs are welcome, thanks!
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
 
package/index.js CHANGED
@@ -1,44 +1,30 @@
1
- // Generated by CoffeeScript 2.7.0
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
- import * as secUtl from "secret-manager-crypto-utils";
4
+ const defaultContext = "standard ThingyCryptoNode"
6
5
 
7
- //###########################################################
8
- defaultContext = "standard ThingyCryptoNode";
9
-
10
- //###########################################################
11
- export var ThingyCryptoNode = class ThingyCryptoNode {
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
- async encrypt(content) {
31
- return (await secUtl.asymmetricEncryptHex(content, this.id));
32
- }
33
-
34
- async decrypt(secretsObj) {
35
- return (await secUtl.asymmetricDecryptHex(secretsObj, this.key));
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
- async diffieHellmanFor(id, salt, timestamp) {
39
- var context;
40
- context = `${salt}:${this.ctx}_${timestamp}`;
41
- return (await secUtl.diffieHellmanSecretHashHex(this.key, id, context));
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,8 +1,11 @@
1
1
  {
2
2
  "name": "thingy-crypto-node",
3
- "version": "0.0.2",
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
+ "files": [
7
+ "index.js"
8
+ ],
6
9
  "keywords": [
7
10
  "thingy",
8
11
  "crypto",
@@ -13,7 +16,7 @@
13
16
  "diffie-hellman"
14
17
  ],
15
18
  "author": "Lenard Frommelt lenard@frommelt.dev",
16
- "license": "CC0",
19
+ "license": "CC0-1.0",
17
20
  "repository": {
18
21
  "type": "git",
19
22
  "url": "git+https://github.com/JhonnyJason/thingy-crypto-node.git"
@@ -23,9 +26,12 @@
23
26
  },
24
27
  "homepage": "https://github.com/JhonnyJason/thingy-crypto-node-output#readme",
25
28
  "dependencies": {
26
- "secret-manager-crypto-utils": "^0.3.1"
29
+ "secret-manager-crypto-utils": "^0.4.0"
27
30
  },
28
31
  "type": "module",
32
+ "engines": {
33
+ "node": ">=22"
34
+ },
29
35
  "scripts": {
30
36
  "test": "echo \"Error: no test specified\" && exit 1"
31
37
  }
package/LICENSE DELETED
@@ -1,121 +0,0 @@
1
- Creative Commons Legal Code
2
-
3
- CC0 1.0 Universal
4
-
5
- CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6
- LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7
- ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8
- INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9
- REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10
- PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11
- THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12
- HEREUNDER.
13
-
14
- Statement of Purpose
15
-
16
- The laws of most jurisdictions throughout the world automatically confer
17
- exclusive Copyright and Related Rights (defined below) upon the creator
18
- and subsequent owner(s) (each and all, an "owner") of an original work of
19
- authorship and/or a database (each, a "Work").
20
-
21
- Certain owners wish to permanently relinquish those rights to a Work for
22
- the purpose of contributing to a commons of creative, cultural and
23
- scientific works ("Commons") that the public can reliably and without fear
24
- of later claims of infringement build upon, modify, incorporate in other
25
- works, reuse and redistribute as freely as possible in any form whatsoever
26
- and for any purposes, including without limitation commercial purposes.
27
- These owners may contribute to the Commons to promote the ideal of a free
28
- culture and the further production of creative, cultural and scientific
29
- works, or to gain reputation or greater distribution for their Work in
30
- part through the use and efforts of others.
31
-
32
- For these and/or other purposes and motivations, and without any
33
- expectation of additional consideration or compensation, the person
34
- associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35
- is an owner of Copyright and Related Rights in the Work, voluntarily
36
- elects to apply CC0 to the Work and publicly distribute the Work under its
37
- terms, with knowledge of his or her Copyright and Related Rights in the
38
- Work and the meaning and intended legal effect of CC0 on those rights.
39
-
40
- 1. Copyright and Related Rights. A Work made available under CC0 may be
41
- protected by copyright and related or neighboring rights ("Copyright and
42
- Related Rights"). Copyright and Related Rights include, but are not
43
- limited to, the following:
44
-
45
- i. the right to reproduce, adapt, distribute, perform, display,
46
- communicate, and translate a Work;
47
- ii. moral rights retained by the original author(s) and/or performer(s);
48
- iii. publicity and privacy rights pertaining to a person's image or
49
- likeness depicted in a Work;
50
- iv. rights protecting against unfair competition in regards to a Work,
51
- subject to the limitations in paragraph 4(a), below;
52
- v. rights protecting the extraction, dissemination, use and reuse of data
53
- in a Work;
54
- vi. database rights (such as those arising under Directive 96/9/EC of the
55
- European Parliament and of the Council of 11 March 1996 on the legal
56
- protection of databases, and under any national implementation
57
- thereof, including any amended or successor version of such
58
- directive); and
59
- vii. other similar, equivalent or corresponding rights throughout the
60
- world based on applicable law or treaty, and any national
61
- implementations thereof.
62
-
63
- 2. Waiver. To the greatest extent permitted by, but not in contravention
64
- of, applicable law, Affirmer hereby overtly, fully, permanently,
65
- irrevocably and unconditionally waives, abandons, and surrenders all of
66
- Affirmer's Copyright and Related Rights and associated claims and causes
67
- of action, whether now known or unknown (including existing as well as
68
- future claims and causes of action), in the Work (i) in all territories
69
- worldwide, (ii) for the maximum duration provided by applicable law or
70
- treaty (including future time extensions), (iii) in any current or future
71
- medium and for any number of copies, and (iv) for any purpose whatsoever,
72
- including without limitation commercial, advertising or promotional
73
- purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74
- member of the public at large and to the detriment of Affirmer's heirs and
75
- successors, fully intending that such Waiver shall not be subject to
76
- revocation, rescission, cancellation, termination, or any other legal or
77
- equitable action to disrupt the quiet enjoyment of the Work by the public
78
- as contemplated by Affirmer's express Statement of Purpose.
79
-
80
- 3. Public License Fallback. Should any part of the Waiver for any reason
81
- be judged legally invalid or ineffective under applicable law, then the
82
- Waiver shall be preserved to the maximum extent permitted taking into
83
- account Affirmer's express Statement of Purpose. In addition, to the
84
- extent the Waiver is so judged Affirmer hereby grants to each affected
85
- person a royalty-free, non transferable, non sublicensable, non exclusive,
86
- irrevocable and unconditional license to exercise Affirmer's Copyright and
87
- Related Rights in the Work (i) in all territories worldwide, (ii) for the
88
- maximum duration provided by applicable law or treaty (including future
89
- time extensions), (iii) in any current or future medium and for any number
90
- of copies, and (iv) for any purpose whatsoever, including without
91
- limitation commercial, advertising or promotional purposes (the
92
- "License"). The License shall be deemed effective as of the date CC0 was
93
- applied by Affirmer to the Work. Should any part of the License for any
94
- reason be judged legally invalid or ineffective under applicable law, such
95
- partial invalidity or ineffectiveness shall not invalidate the remainder
96
- of the License, and in such case Affirmer hereby affirms that he or she
97
- will not (i) exercise any of his or her remaining Copyright and Related
98
- Rights in the Work or (ii) assert any associated claims and causes of
99
- action with respect to the Work, in either case contrary to Affirmer's
100
- express Statement of Purpose.
101
-
102
- 4. Limitations and Disclaimers.
103
-
104
- a. No trademark or patent rights held by Affirmer are waived, abandoned,
105
- surrendered, licensed or otherwise affected by this document.
106
- b. Affirmer offers the Work as-is and makes no representations or
107
- warranties of any kind concerning the Work, express, implied,
108
- statutory or otherwise, including without limitation warranties of
109
- title, merchantability, fitness for a particular purpose, non
110
- infringement, or the absence of latent or other defects, accuracy, or
111
- the present or absence of errors, whether or not discoverable, all to
112
- the greatest extent permissible under applicable law.
113
- c. Affirmer disclaims responsibility for clearing rights of other persons
114
- that may apply to the Work or any use thereof, including without
115
- limitation any person's Copyright and Related Rights in the Work.
116
- Further, Affirmer disclaims responsibility for obtaining any necessary
117
- consents, permissions or other rights required for any use of the
118
- Work.
119
- d. Affirmer understands and acknowledges that Creative Commons is not a
120
- party to this document and has no duty or obligation with respect to
121
- this CC0 or use of the Work.
package/unbreaker.js DELETED
File without changes