ths-csprng 1.0.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/LICENSE +202 -0
- package/README.md +99 -0
- package/package.json +41 -0
- package/src/index.js +124 -0
- package/src/util.js +169 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Aries Harbinger
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="./media/banner.png" alt="Banner" width="1250">
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<br />
|
|
6
|
+
|
|
7
|
+
Standard RNGs are built on the naive assumption that once a seed is "randomized," the history of its creation just... disappears.
|
|
8
|
+
|
|
9
|
+
In reality, if an observer can violate causality or reconstruct the precise physical state of a system, your "randomness" is just an entry in a ledger.
|
|
10
|
+
|
|
11
|
+
<br />
|
|
12
|
+
|
|
13
|
+
If you are on to a **Block Universe** or **Superdeterministic** model, your "random" seed is just a static coordinate in spacetime. To an observer with enough compute or a vantage point further down the temporal axis, your private key isn't a secret — it’s a historical fact.
|
|
14
|
+
|
|
15
|
+
**THS (Temporal-Hardening Solution)** is an attempt to fix this. It is a cryptographic wrapper that forces an adversary to move from attacking logic to attacking physical work, which makes “retrocausal observation” or state reconstruction exponentially challenging.
|
|
16
|
+
|
|
17
|
+
To put it simply, it is a “Macroscopic Chaos as a defense against Microscopic Observation.”
|
|
18
|
+
|
|
19
|
+
<br />
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
<br />
|
|
24
|
+
|
|
25
|
+
<div align="center">
|
|
26
|
+
<img src="./media/illustration.png" alt="Illustration" width="1250">
|
|
27
|
+
</div>
|
|
28
|
+
|
|
29
|
+
<br />
|
|
30
|
+
|
|
31
|
+
## The "Observer from the Future" Problem
|
|
32
|
+
|
|
33
|
+
Information doesn't just vanish (shout out to the **[Quantum No-Hiding Theorem](https://en.wikipedia.org/wiki/No-hiding_theorem)**). It leaks into the environment. If someone compromises your hardware or develops a way to reconstruct historical system states, your static seeds are toast.
|
|
34
|
+
|
|
35
|
+
THS creates **Computational Fog**. Instead of a mathematical point, it binds your secret to the "metabolic noise" of your CPU at a specific nanosecond.
|
|
36
|
+
|
|
37
|
+
<br />
|
|
38
|
+
|
|
39
|
+
### How it actually works:
|
|
40
|
+
|
|
41
|
+
* **Micro-Architectural Jitter:** Harvesting the tiny, unpredictable timing variances in your CPU clock caused by interrupts and thread scheduling.
|
|
42
|
+
* **Physical Binding:** Capturing "ghost data" from uninitialized memory, we inject artifacts of the machine's immediate past into the entropy pool.
|
|
43
|
+
* **Memory-Hardened Anchors:** We use **Argon2id** to spin up a shifting memory state. This forces an attacker to simulate a massive slice of physical RAM history just to see what happened in that one millisecond.
|
|
44
|
+
|
|
45
|
+
<br />
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
<br />
|
|
50
|
+
|
|
51
|
+
## Getting Started
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install ths-csprng
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
<br />
|
|
58
|
+
|
|
59
|
+
### Quick Start
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
import THS from 'ths-csprng';
|
|
63
|
+
|
|
64
|
+
(async () => {
|
|
65
|
+
// Get a 512-bit seed tied to this specific moment in hardware history
|
|
66
|
+
const seed = await THS.random(64);
|
|
67
|
+
|
|
68
|
+
// Deep hardening for keys meant to outlast the current paradigm
|
|
69
|
+
const key = await THS.random(32, {
|
|
70
|
+
layers: 500, // Increase the temporal iterations (slices)
|
|
71
|
+
harden: 3, // Full metabolic collection + Argon2id
|
|
72
|
+
mem: 256 * 1024, // Prevents the "Cold Start Problem"
|
|
73
|
+
memoryH: 1, // Run Argon2id at once
|
|
74
|
+
trng: true // Use randomness from /dev/urandom directly (Unix-like only)
|
|
75
|
+
});
|
|
76
|
+
})();
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
<br />
|
|
80
|
+
|
|
81
|
+
## Why should you care?
|
|
82
|
+
|
|
83
|
+
Most people **don't need this**.
|
|
84
|
+
|
|
85
|
+
If you're building a todo app, move on. If you believe that the future cannot affect the past, that hardware manufacturers never lie, and that “random” numbers are truly disconnected from the physical state of the machine, then this tool is just a very complex way to waste CPU cycles.
|
|
86
|
+
|
|
87
|
+
Temporal-Hardening Solution (THS) is designed for high-integrity cryptographic secrets where the threat model includes **historical state reconstruction** or **backdoored hardware**. It is a defense against the reality that even if your randomness is 100% deterministic within a closed system, it must be **Computationally Irreducible** to an outsider.
|
|
88
|
+
|
|
89
|
+
<br />
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
Licensed under the **Apache License, Version 2.0**; a robust, permissive license that includes an explicit grant of patent rights and provides protection against contributor liability.
|
|
94
|
+
|
|
95
|
+
Copyright © 2026 Aries Harbinger. See the **[LICENSE](./LICENSE)** file for full details.
|
|
96
|
+
|
|
97
|
+
<br />
|
|
98
|
+
<br />
|
|
99
|
+
<br />
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ths-csprng",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Temporal-Hardening Solution: A CSPRNG wrapper designed to resist historical state reconstruction and hardware backdoors.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"author": "Aries Harbinger",
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"homepage": "https://github.com/harnuma9/temporal-hardening-solution-csprng",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/harnuma9/temporal-hardening-solution-csprng.git"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/harnuma9/temporal-hardening-solution-csprng/issues"
|
|
19
|
+
},
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=18"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"csprng",
|
|
25
|
+
"cryptography",
|
|
26
|
+
"entropy",
|
|
27
|
+
"random",
|
|
28
|
+
"temporal-hardening",
|
|
29
|
+
"argon2",
|
|
30
|
+
"sha3",
|
|
31
|
+
"keccak",
|
|
32
|
+
"security",
|
|
33
|
+
"kmac"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@noble/hashes": "^2.2.0"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THS (Temporal-Hardening Solution)
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2026 Aries Harbinger
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import Utils from './util.js';
|
|
20
|
+
import { kmac256 } from '@noble/hashes/sha3-addons.js';
|
|
21
|
+
import { createHash } from 'node:crypto';
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Temporal-Hardening Solution - Applied in Random Number Generator
|
|
26
|
+
*/
|
|
27
|
+
export const THS = {
|
|
28
|
+
/**
|
|
29
|
+
* Generates cryptographically secure random bytes with temporal hardening.
|
|
30
|
+
*/
|
|
31
|
+
async random(length = 32, {
|
|
32
|
+
|
|
33
|
+
layers = 128, // number of timeline slices (iterations)
|
|
34
|
+
harden = 2, // levels: 0 (hrtime), 1 (+entropy), 2 (+internal), 3 (+Argon2id)
|
|
35
|
+
trng = false, // attempts to read /dev/urandom, fallback to randomBytes()
|
|
36
|
+
hashOut = true, // whether to hash every moments instead of processing it as raw data
|
|
37
|
+
|
|
38
|
+
memoryH = 1, // number of Argon2id runs (requires harden=3 level)
|
|
39
|
+
mem = 16384, // memory hardness of Argon2id (default: 16MB)
|
|
40
|
+
label = 'THS-Default-v1' // kmac label
|
|
41
|
+
|
|
42
|
+
} = {}) {
|
|
43
|
+
|
|
44
|
+
if (length <= 0)
|
|
45
|
+
return Buffer.alloc(0);
|
|
46
|
+
|
|
47
|
+
if (layers < 2 || layers > 65536)
|
|
48
|
+
throw new Error(`Layer count [${layers}] is invalid. Requires a temporal span of 2-65536 cycles to ensure metabolic variance.`);
|
|
49
|
+
|
|
50
|
+
if (harden < 0 || harden > 3)
|
|
51
|
+
throw new Error(`Hardening level [${harden}] is unknown. Use 0 (Basic) through 3 (Heavy).`);
|
|
52
|
+
|
|
53
|
+
if (harden >= 3 && (memoryH < 1 || memoryH > layers))
|
|
54
|
+
throw new Error(`Memory hardening cycle count [${memoryH}] exceeds available temporal layers [${layers}].`);
|
|
55
|
+
|
|
56
|
+
if ('number' !== typeof mem || mem < 1024)
|
|
57
|
+
throw new Error(`Memory set [${mem}] for Argon2id must be a valid number and not be less than 1024 or 1KB.`);
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
// Collector for metabolic moments (The Message)
|
|
61
|
+
const moments = [];
|
|
62
|
+
let seed;
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
for (let i = 0; i < layers; i++) {
|
|
66
|
+
const moment = await Utils.snapshot(harden, memoryH, mem, i, trng);
|
|
67
|
+
|
|
68
|
+
if (!hashOut) { moments.push(moment); }
|
|
69
|
+
|
|
70
|
+
else {
|
|
71
|
+
const h = createHash('sha3-512') // Structurally immune to length-extension attacks
|
|
72
|
+
.update(moment)
|
|
73
|
+
.digest();
|
|
74
|
+
|
|
75
|
+
moment.fill(0); // immediate wipe
|
|
76
|
+
moments.push(h);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Supplemental entropy injected into the moment collection
|
|
80
|
+
if (harden >= 1) {
|
|
81
|
+
const sup = await Utils.getRandom(12, trng);
|
|
82
|
+
moments.push(sup);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (i < layers - 1) await Utils.jitter(trng);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Initial KMAC Key
|
|
89
|
+
seed = await Utils.getRandom(32, trng);
|
|
90
|
+
|
|
91
|
+
// Finalize with KMAC256
|
|
92
|
+
const result = kmac256(seed, Buffer.concat(moments), {
|
|
93
|
+
personalization: Utils.toBytes(label),
|
|
94
|
+
dkLen: length
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return Buffer.from(result);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
catch (e) { throw e }
|
|
101
|
+
|
|
102
|
+
finally {
|
|
103
|
+
// Cleanup sensitive memory
|
|
104
|
+
seed && seed.fill(0);
|
|
105
|
+
|
|
106
|
+
for (const m of moments) {
|
|
107
|
+
(m && typeof m.fill === 'function') && m.fill(0);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
async rand(len, o) { return await THS.random(len, o) },
|
|
113
|
+
async rnd (len, o) { return await THS.random(len, o) },
|
|
114
|
+
|
|
115
|
+
// direct use raw random (fallback to randomBytes)
|
|
116
|
+
async raw(len) { return await Utils.getRandom(len, true) }
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// Must use await
|
|
120
|
+
export const randomBytes = async (len, o) => {
|
|
121
|
+
return await THS.random(len, o);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export default THS;
|
package/src/util.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THS (Temporal-Hardening Solution)
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2026 Aries Harbinger
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import fs from 'node:fs/promises';
|
|
20
|
+
import { promisify } from 'node:util';
|
|
21
|
+
import { randomBytes, timingSafeEqual, argon2 as _argon2 } from 'node:crypto';
|
|
22
|
+
|
|
23
|
+
const argon2 = promisify(_argon2);
|
|
24
|
+
const defined_past = process.hrtime.bigint();
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
export const Utils = {
|
|
28
|
+
/**
|
|
29
|
+
* Captures a high-resolution snapshot of the Node.js metabolic state.
|
|
30
|
+
* Uses concatenation to prevent entropy collisions.
|
|
31
|
+
*/
|
|
32
|
+
async snapshot(h, mh, mem, s, useUrandom) {
|
|
33
|
+
// Level 1: Minimal data
|
|
34
|
+
if (h <= 1) return this.bigIntToBuffer(process.hrtime.bigint());
|
|
35
|
+
|
|
36
|
+
const u = process.resourceUsage();
|
|
37
|
+
const m = process.memoryUsage();
|
|
38
|
+
|
|
39
|
+
// Primary metabolic collection (Machine State)
|
|
40
|
+
// We use an array to preserve individual entropy of each metric
|
|
41
|
+
const metrics = [
|
|
42
|
+
process.hrtime.bigint(), // CPU Nanoseconds
|
|
43
|
+
BigInt(Math.round(process.uptime() * 1_000_000)),
|
|
44
|
+
BigInt(u.userCPUTime), // User-space CPU usage
|
|
45
|
+
BigInt(u.systemCPUTime), // Kernel-space CPU usage
|
|
46
|
+
BigInt(u.minorPageFault), // Soft memory faults
|
|
47
|
+
BigInt(u.majorPageFault), // Hard I/O faults
|
|
48
|
+
BigInt(u.voluntaryContextSwitches),
|
|
49
|
+
BigInt(u.involuntaryContextSwitches),
|
|
50
|
+
BigInt(m.heapUsed), // V8 actual usage
|
|
51
|
+
BigInt(m.heapTotal), // V8 allocated total
|
|
52
|
+
BigInt(m.external), // C++ object memory
|
|
53
|
+
BigInt(m.arrayBuffers || 0), // Raw buffer memory
|
|
54
|
+
BigInt(m.rss), // Resident Set Size (Physical RAM)
|
|
55
|
+
BigInt(Math.round(performance.now() * 1_000_000)),
|
|
56
|
+
defined_past, // Process start anchor
|
|
57
|
+
BigInt(Date.now()), // Wall clock (Temporal anchor)
|
|
58
|
+
BigInt(s) // Sequential salt
|
|
59
|
+
].map(n => this.bigIntToBuffer(n));
|
|
60
|
+
|
|
61
|
+
// Map metrics to uniquely structured buffers
|
|
62
|
+
const b = Buffer.concat([
|
|
63
|
+
|
|
64
|
+
// Inject 128 bytes of raw, uninitialized system memory
|
|
65
|
+
// (May contain fragments of previous internal function calls/pointers)
|
|
66
|
+
Buffer.allocUnsafe(128),
|
|
67
|
+
|
|
68
|
+
...metrics
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
if (h <= 2 || s >= mh) return b;
|
|
72
|
+
|
|
73
|
+
// Level 3: Argon2id Memory-Hardening
|
|
74
|
+
const nonce = await this.getRandom(16, useUrandom);
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const memHard = await argon2('argon2id', {
|
|
78
|
+
nonce,
|
|
79
|
+
message: b,
|
|
80
|
+
memory: mem,
|
|
81
|
+
passes: 3,
|
|
82
|
+
parallelism: 4,
|
|
83
|
+
tagLength: 64
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return (h <= 3)
|
|
87
|
+
? Buffer.concat([b, memHard])
|
|
88
|
+
: memHard;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
catch (e) { throw e }
|
|
92
|
+
finally { nonce.fill(0); }
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Attempts to harvest raw entropy from the OS device.
|
|
97
|
+
* Fallback to hardware-backed Node.js randomBytes on failure.
|
|
98
|
+
*/
|
|
99
|
+
async getRandom(len, useUrandom) {
|
|
100
|
+
if (!useUrandom) return randomBytes(len);
|
|
101
|
+
|
|
102
|
+
let fd;
|
|
103
|
+
try {
|
|
104
|
+
fd = await fs.open('/dev/urandom', 'r');
|
|
105
|
+
const buffer = Buffer.allocUnsafe(len);
|
|
106
|
+
const { bytesRead } = await fd.read(buffer, 0, len, 0);
|
|
107
|
+
|
|
108
|
+
// If we didn't read enough bytes for some reason, fallback
|
|
109
|
+
return (bytesRead < len)
|
|
110
|
+
? randomBytes(len)
|
|
111
|
+
: buffer;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
catch (e) { return randomBytes(len); }
|
|
115
|
+
finally { if (fd) await fd.close(); }
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Micro-architectural noise generator.
|
|
120
|
+
*/
|
|
121
|
+
async jitter(useUrandom) {
|
|
122
|
+
const b = await this.getRandom(64, useUrandom);
|
|
123
|
+
timingSafeEqual(b, b);
|
|
124
|
+
b.fill(0);
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Optimized BigInt to Buffer conversion with 16-bit Length Header.
|
|
129
|
+
* Prepends a 2-byte length header (Big-Endian) followed by the data.
|
|
130
|
+
*/
|
|
131
|
+
bigIntToBuffer(n) {
|
|
132
|
+
if (typeof n !== 'bigint') n = BigInt(n);
|
|
133
|
+
if (n < 0n) throw new RangeError('Negative values not supported');
|
|
134
|
+
|
|
135
|
+
// Fast path for zero: [Length: 0x0001] [Value: 0x00]
|
|
136
|
+
if (n === 0n) {
|
|
137
|
+
const buf = Buffer.allocUnsafe(3);
|
|
138
|
+
buf.writeUInt16BE(1, 0); // Length is 1 byte
|
|
139
|
+
buf[2] = 0; // Value is 0
|
|
140
|
+
return buf;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Convert BigInt to Buffer via Hex
|
|
144
|
+
let hex = n.toString(16);
|
|
145
|
+
if (hex.length % 2 !== 0) hex = '0' + hex;
|
|
146
|
+
const data = Buffer.from(hex, 'hex');
|
|
147
|
+
|
|
148
|
+
// Safety check for 16-bit overflow
|
|
149
|
+
if (data.length > 0xFFFF)
|
|
150
|
+
throw new RangeError('BigInt exceeds 16-bit length header capacity');
|
|
151
|
+
|
|
152
|
+
// Structure: [Header (2 bytes)] + [Data]
|
|
153
|
+
const header = Buffer.allocUnsafe(2);
|
|
154
|
+
header.writeUInt16BE(data.length, 0);
|
|
155
|
+
|
|
156
|
+
return Buffer.concat([header, data]);
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Normalizes various input types into a Uint8Array.
|
|
161
|
+
*/
|
|
162
|
+
toBytes(input) {
|
|
163
|
+
if (input instanceof Uint8Array) return input;
|
|
164
|
+
if (typeof input === 'string') return new TextEncoder().encode(input);
|
|
165
|
+
return new TextEncoder().encode(JSON.stringify(input) || '');
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export default Utils;
|