simple-hpke 0.0.8
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 +99 -0
- package/README.md +312 -0
- package/dist/index.cjs +1754 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +194 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1737 -0
- package/dist/index.js.map +7 -0
- package/dist/index.min.cjs +2 -0
- package/dist/index.min.cjs.map +7 -0
- package/dist/index.min.js +2 -0
- package/dist/index.min.js.map +7 -0
- package/dist/meta.json +818 -0
- package/package.json +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# Big Time Public License
|
|
2
|
+
|
|
3
|
+
Version 2.0.2
|
|
4
|
+
|
|
5
|
+
<https://bigtimelicense.com/versions/2.0.2>
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
These terms let you use and share this software for noncommercial purposes and in small business for free, while also guaranteeing that paid licenses for big businesses will be available on fair, reasonable, and nondiscriminatory terms.
|
|
10
|
+
|
|
11
|
+
## Acceptance
|
|
12
|
+
|
|
13
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
14
|
+
|
|
15
|
+
## Noncommercial Purposes
|
|
16
|
+
|
|
17
|
+
You may use the software for any noncommercial purpose.
|
|
18
|
+
|
|
19
|
+
## Personal Uses
|
|
20
|
+
|
|
21
|
+
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, count as use for noncommercial purposes.
|
|
22
|
+
|
|
23
|
+
## Noncommercial Organizations
|
|
24
|
+
|
|
25
|
+
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution counts as use for noncommercial purposes, regardless of the source of funding or obligations resulting from the funding.
|
|
26
|
+
|
|
27
|
+
## Small Business
|
|
28
|
+
|
|
29
|
+
You may use the software for the benefit of your company if it meets all these criteria:
|
|
30
|
+
|
|
31
|
+
1. had fewer than 20 total individuals working as employees and independent contractors at all times during the last tax year
|
|
32
|
+
|
|
33
|
+
2. earned less than $1,000,000 total revenue in the last tax year
|
|
34
|
+
|
|
35
|
+
3. received less than $1,000,000 total debt, equity, and other investment in the last five tax years, counting investment in predecessor companies that reorganized into, merged with, or spun out your company
|
|
36
|
+
|
|
37
|
+
All dollar figures are United States dollars as of 2019. Adjust for them inflation according to the United States Bureau of Labor Statistics' consumer price index for all urban consumers, United States city average, for all items, not seasonally adjusted, with 1982–1984=100 reference base.
|
|
38
|
+
|
|
39
|
+
## Big Business
|
|
40
|
+
|
|
41
|
+
You may use the software for the benefit of your company:
|
|
42
|
+
|
|
43
|
+
1. for 128 days after your company stops qualifying under [Small Business](#small-business)
|
|
44
|
+
|
|
45
|
+
2. indefinitely, if the licensor or their legal successor does not offer fair, reasonable, and nondiscriminatory terms for a commercial license for the software within 32 days of [written request](#how-to-request) and negotiate in good faith to conclude a deal
|
|
46
|
+
|
|
47
|
+
## How to Request
|
|
48
|
+
|
|
49
|
+
If this software includes an address for the licensor or an agent of the licensor in a standard place, such as in documentation, software package metadata, or an "about" page or screen, try to request a fair commercial license at that address. If this package includes both online and offline addresses, try online before offline. If you can't deliver a request that way, or this software doesn't include any addressees, spend one hour online researching an address, recording all your searches and inquiries as you go, and try any addresses that you find. If you can't find any addresses, or if those addresses also fail, that counts as failure to offer a fair commercial license by the licensor under [Big Business](#big-business).
|
|
50
|
+
|
|
51
|
+
## Fair, Reasonable, and Nondiscriminatory Terms
|
|
52
|
+
|
|
53
|
+
Fair, reasonable, and nondiscriminatory terms may license the software perpetually or for a term, and may or may not cover new versions of the software. If the licensor advertises license terms and a pricing structure for generally available commercial licenses, the licensor proposes license terms and a price as advertised, and a customer not affiliated with the licensor has bought a commercial license for the software on substantially equivalent terms in the past year, the proposal is fair, reasonable, and nondiscriminatory.
|
|
54
|
+
|
|
55
|
+
## Copyright License
|
|
56
|
+
|
|
57
|
+
The licensor grants you a copyright license to do everything with the software that would otherwise infringe the licensor's copyright in it for any purpose allowed by these terms.
|
|
58
|
+
|
|
59
|
+
## Notices
|
|
60
|
+
|
|
61
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
|
|
62
|
+
|
|
63
|
+
> Required Notice: Copyright Nick Thomas (https://nichoth.com)
|
|
64
|
+
|
|
65
|
+
## Patent License
|
|
66
|
+
|
|
67
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
68
|
+
|
|
69
|
+
## Fair Use
|
|
70
|
+
|
|
71
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
72
|
+
|
|
73
|
+
## No Other Rights
|
|
74
|
+
|
|
75
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
76
|
+
|
|
77
|
+
## Patent Defense
|
|
78
|
+
|
|
79
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
80
|
+
|
|
81
|
+
## Violations
|
|
82
|
+
|
|
83
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
84
|
+
|
|
85
|
+
## No Liability
|
|
86
|
+
|
|
87
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
88
|
+
|
|
89
|
+
## Definitions
|
|
90
|
+
|
|
91
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
92
|
+
|
|
93
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
94
|
+
|
|
95
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
96
|
+
|
|
97
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
98
|
+
|
|
99
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
package/README.md
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
# Simple HPKE
|
|
2
|
+
[](https://github.com/nichoth/simple-hpke/actions/workflows/nodejs.yml)
|
|
3
|
+
[](README.md)
|
|
4
|
+
[](README.md)
|
|
5
|
+
[](https://semver.org/)
|
|
6
|
+
[](./CHANGELOG.md)
|
|
7
|
+
[](https://packagephobia.com/result?p=@substrate-system/simple-hpke)
|
|
8
|
+
[](https://bundlephobia.com/package/@substrate-system/simple-hpke)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Hybrid Public Key Encryption
|
|
13
|
+
([RFC 9180](https://www.rfc-editor.org/rfc/rfc9180.html))
|
|
14
|
+
|
|
15
|
+
1 dependency -- `uint8arrays`.
|
|
16
|
+
|
|
17
|
+
<details><summary><h2>Contents</h2></summary>
|
|
18
|
+
|
|
19
|
+
<!-- toc -->
|
|
20
|
+
|
|
21
|
+
- [Install](#install)
|
|
22
|
+
- [Example](#example)
|
|
23
|
+
* [Key Wrapping](#key-wrapping)
|
|
24
|
+
* [Hybrid Encryption](#hybrid-encryption)
|
|
25
|
+
+ [Encrypt / Decrypt](#encrypt--decrypt)
|
|
26
|
+
- [`encrypt`](#encrypt)
|
|
27
|
+
- [Encrypt to a string](#encrypt-to-a-string)
|
|
28
|
+
- [`decrypt`](#decrypt)
|
|
29
|
+
- [`decrypt.asString`](#decryptasstring)
|
|
30
|
+
- [`decrypt.fromString`](#decryptfromstring)
|
|
31
|
+
- [Modules](#modules)
|
|
32
|
+
* [ESM](#esm)
|
|
33
|
+
* [Common JS](#common-js)
|
|
34
|
+
* [pre-built JS](#pre-built-js)
|
|
35
|
+
+ [copy](#copy)
|
|
36
|
+
+ [HTML](#html)
|
|
37
|
+
|
|
38
|
+
<!-- tocstop -->
|
|
39
|
+
|
|
40
|
+
</details>
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
npm i -S @substrate-system/simple-hpke
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Example
|
|
49
|
+
|
|
50
|
+
Wrap an AES key, or encrypt a message.
|
|
51
|
+
|
|
52
|
+
### Key Wrapping
|
|
53
|
+
|
|
54
|
+
Encrypt an AES key to yourself, then recover it later.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { seal, open } from '@substrate-system/simple-hpke'
|
|
58
|
+
|
|
59
|
+
// An X25519 keypair. The private key can be non-extractable.
|
|
60
|
+
// HPKE needs only `deriveBits`.
|
|
61
|
+
const keypair = await crypto.subtle.generateKey(
|
|
62
|
+
{ name: 'X25519' },
|
|
63
|
+
false, // extractable
|
|
64
|
+
['deriveBits']
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
// create a new AES key, and encrypt it to your public key.
|
|
68
|
+
const { wrapped, key } = await seal(keypair)
|
|
69
|
+
|
|
70
|
+
// Or wrap an existing AES key. The supplied key must be extractable.
|
|
71
|
+
const aesKey = await crypto.subtle.generateKey(
|
|
72
|
+
{ name: 'AES-GCM', length: 256 },
|
|
73
|
+
true, // extractable
|
|
74
|
+
['encrypt', 'decrypt']
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
// pass in a key. The return value is just the wrapped key
|
|
78
|
+
const { wrapped } = await seal(keypair, aesKey)
|
|
79
|
+
|
|
80
|
+
// Later, recover the same key with your private key.
|
|
81
|
+
const recoveredKey = await open(keypair, wrapped)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
See [docs/README.md](./docs/README.md) for the full API and rationale.
|
|
85
|
+
|
|
86
|
+
### Hybrid Encryption
|
|
87
|
+
|
|
88
|
+
Seal a key, then use it to encrypt a message with AES-GCM.
|
|
89
|
+
The wrapped key is concattenated with the cipher text, along with the
|
|
90
|
+
IV. The recipient uses their private key to open the AES key and decrypt
|
|
91
|
+
the message.
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { seal, open } from '@substrate-system/simple-hpke'
|
|
95
|
+
|
|
96
|
+
const recipient = await crypto.subtle.generateKey(
|
|
97
|
+
{ name: 'X25519' },
|
|
98
|
+
false, // not extractable
|
|
99
|
+
['deriveBits']
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
// Create and seal a fresh AES-GCM key, and encrypt a message with it.
|
|
103
|
+
const { wrapped, key } = await seal(recipient)
|
|
104
|
+
const iv = crypto.getRandomValues(new Uint8Array(12))
|
|
105
|
+
const ciphertext = await crypto.subtle.encrypt(
|
|
106
|
+
{ name: 'AES-GCM', iv },
|
|
107
|
+
key,
|
|
108
|
+
new TextEncoder().encode('attack at dawn')
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
// Send `wrapped`, `iv`, and `ciphertext` together. `wrapped` is a fixed
|
|
112
|
+
// 80 bytes for this suite, and the AES-GCM IV is 12 bytes, so the recipient
|
|
113
|
+
// can slice the payload back apart at known offsets.
|
|
114
|
+
const ciphertext = new Uint8Array(ciphertext)
|
|
115
|
+
const message = new Uint8Array(wrapped.length + iv.length + ciphertext.length)
|
|
116
|
+
message.set(wrapped, 0)
|
|
117
|
+
message.set(iv, wrapped.length)
|
|
118
|
+
message.set(ciphertext, wrapped.length + iv.length)
|
|
119
|
+
|
|
120
|
+
// On the other side, split the payload back into its parts.
|
|
121
|
+
const wrapped2 = message.subarray(0, 80)
|
|
122
|
+
const iv2 = message.subarray(80, 80 + 12)
|
|
123
|
+
const ciphertext2 = message.subarray(80 + 12)
|
|
124
|
+
|
|
125
|
+
// Recover the key, then decrypt the message.
|
|
126
|
+
const recovered = await open(recipient, wrapped2)
|
|
127
|
+
const plaintext = await crypto.subtle.decrypt(
|
|
128
|
+
{ name: 'AES-GCM', iv: iv2 },
|
|
129
|
+
recovered,
|
|
130
|
+
ciphertext2
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
new TextDecoder().decode(plaintext) // => 'attack at dawn'
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
#### Encrypt / Decrypt
|
|
137
|
+
|
|
138
|
+
So that was a lot of code to encrypt and decrypt a message...
|
|
139
|
+
This package exposes functions `encrypt` and `decrypt` which do the same thing.
|
|
140
|
+
|
|
141
|
+
`encrypt` seals an AES key to the recipient, encrypts the message under
|
|
142
|
+
that key, and returns a single envelope: `wrappedLen + wrapped + iv + ciphertext`
|
|
143
|
+
(a 2-byte length prefix, the wrapped key, the 12-byte AES-GCM IV, and the
|
|
144
|
+
cipher text). `decrypt` reverses it, returning the plaintext bytes.
|
|
145
|
+
|
|
146
|
+
>
|
|
147
|
+
> [!NOTE]
|
|
148
|
+
> See [`decrypt.asString`](#decryptasstring) &
|
|
149
|
+
> [`decrypt.fromString`](#decryptfromstring) below for a convenient way
|
|
150
|
+
> to decrypt from a string.
|
|
151
|
+
>
|
|
152
|
+
> See [`encrypt.asString`](#encrypt-to-a-string) for encrypting and ecoding to
|
|
153
|
+
> a string.
|
|
154
|
+
>
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import {
|
|
159
|
+
encrypt,
|
|
160
|
+
decrypt
|
|
161
|
+
} from '@substrate-system/simple-hpke'
|
|
162
|
+
|
|
163
|
+
// need a public key for the recipient
|
|
164
|
+
const recipient = await crypto.subtle.generateKey(
|
|
165
|
+
{ name: 'X25519' },
|
|
166
|
+
false,
|
|
167
|
+
['deriveBits']
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
// create a new AES key, encrypt a message, and get back one envelope
|
|
171
|
+
const encryptedMessage = await encrypt(recipient, 'hello encryption')
|
|
172
|
+
|
|
173
|
+
// the recipient recovers the message with their private key
|
|
174
|
+
const text = await decrypt.asString(recipient, encryptedMessage)
|
|
175
|
+
|
|
176
|
+
// use `decrypt` to get a Uint8Array
|
|
177
|
+
const bytes = await decrypt(recipient, encryptedMessage)
|
|
178
|
+
|
|
179
|
+
//
|
|
180
|
+
// use an existing AES key
|
|
181
|
+
//
|
|
182
|
+
const existingKey = await crypto.subtle.generateKey(
|
|
183
|
+
{ name: 'AES-GCM', length: 256 },
|
|
184
|
+
true, // extractable
|
|
185
|
+
['encrypt', 'decrypt']
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
const anotherEncryptedMsg = await encrypt(
|
|
189
|
+
recipient,
|
|
190
|
+
'hello again',
|
|
191
|
+
existingKey
|
|
192
|
+
)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
##### `encrypt`
|
|
196
|
+
|
|
197
|
+
The recipient can be a crypto key, a Uint8Array, or a string public key.
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
type RecipientKey =
|
|
201
|
+
| CryptoKey
|
|
202
|
+
| CryptoKeyPair
|
|
203
|
+
| Uint8Array
|
|
204
|
+
| { publicKey:string; encoding?:Uint8ArrayEncodings }
|
|
205
|
+
|
|
206
|
+
async function encrypt (
|
|
207
|
+
recipient:RecipientKey,
|
|
208
|
+
message:Uint8Array|string,
|
|
209
|
+
aesKey?:CryptoKey|Uint8Array|null,
|
|
210
|
+
opts?:{
|
|
211
|
+
keysize?:128|256
|
|
212
|
+
info?:Uint8Array|string
|
|
213
|
+
}
|
|
214
|
+
):Promise<Uint8Array>
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
##### Encrypt to a string
|
|
218
|
+
|
|
219
|
+
`encrypt.asString` is `encrypt` with the envelope encoded to a string, handy
|
|
220
|
+
for transports that carry text (JSON, URLs, headers). `opts.encoding` sets the
|
|
221
|
+
string encoding. Default encoding is `base64url`.
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
import { encrypt, decrypt } from '@substrate-system/ecies'
|
|
225
|
+
import { fromString } from 'uint8arrays'
|
|
226
|
+
|
|
227
|
+
// recipient is any RecipientKey; keypair holds the matching private key
|
|
228
|
+
const encryptedString = await encrypt.asString(
|
|
229
|
+
recipient,
|
|
230
|
+
'message for them',
|
|
231
|
+
null, // an AES key if you want
|
|
232
|
+
{ encoding: 'base64url' }
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
// Decode it back to bytes before decrypting.
|
|
236
|
+
const message = fromString(encryptedString, 'base64url')
|
|
237
|
+
const plaintext = await decrypt.asString(keypair, message)
|
|
238
|
+
// 'message for them'
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
The returned string encodes the same envelope `encrypt` returns, so the
|
|
242
|
+
recipient decodes it with a matching decoder (here `fromString`) and passes the
|
|
243
|
+
bytes to `decrypt` / `decrypt.asString`.
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
##### `decrypt`
|
|
247
|
+
|
|
248
|
+
Descrypt the given data, return a `Uint8Array`.
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
async function decrypt (
|
|
252
|
+
keypair:CryptoKeyPair,
|
|
253
|
+
message:Uint8Array,
|
|
254
|
+
opts?:{ info?:Uint8Array|string }
|
|
255
|
+
):Promise<Uint8Array>
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
##### `decrypt.asString`
|
|
259
|
+
|
|
260
|
+
Take a `Uint8Array`, return a sttring.
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
decrypt.asString = async function decryptToString (
|
|
264
|
+
keypair:CryptoKeyPair,
|
|
265
|
+
message:Uint8Array,
|
|
266
|
+
opts?:{ info?:Uint8Array|string }
|
|
267
|
+
):Promise<string>
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
##### `decrypt.fromString`
|
|
271
|
+
|
|
272
|
+
Take a `string` as input. Return either a string, or if `opts.buffer` is true,
|
|
273
|
+
a`Uint8Array`.
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
decrypt.fromString = async function decryptFromString (
|
|
277
|
+
keypair:CryptoKeyPair,
|
|
278
|
+
message:string,
|
|
279
|
+
opts?:{ info?:Uint8Array|string, buffer?:boolean }
|
|
280
|
+
):Promise<string|Uint8Array>
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
---------------------------------------------------------------
|
|
284
|
+
|
|
285
|
+
## Modules
|
|
286
|
+
|
|
287
|
+
This exposes ESM and common JS via
|
|
288
|
+
[package.json `exports` field](https://nodejs.org/api/packages.html#exports).
|
|
289
|
+
|
|
290
|
+
### ESM
|
|
291
|
+
```js
|
|
292
|
+
import { seal, open, encrypt, decrypt } from '@substrate-system/simple-hpke'
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### Common JS
|
|
296
|
+
```js
|
|
297
|
+
require('@substrate-system/simple-hpke')
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### pre-built JS
|
|
301
|
+
This package exposes minified JS files too. Copy them to a location that is
|
|
302
|
+
accessible to your web server, then link to them in HTML.
|
|
303
|
+
|
|
304
|
+
#### copy
|
|
305
|
+
```sh
|
|
306
|
+
cp ./node_modules/@substrate-system/simple-hpke/dist/index.min.js ./public/hpke.min.js
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
#### HTML
|
|
310
|
+
```html
|
|
311
|
+
<script type="module" src="./hpke.min.js"></script>
|
|
312
|
+
```
|