@waku/message-encryption 0.0.1
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/bundle/index.js +13514 -0
- package/dist/constants.d.ts +12 -0
- package/dist/constants.js +10 -0
- package/dist/constants.js.map +1 -0
- package/dist/crypto.d.ts +29 -0
- package/dist/crypto.js +60 -0
- package/dist/crypto.js.map +1 -0
- package/dist/ecies.d.ts +17 -0
- package/dist/ecies.js +126 -0
- package/dist/ecies.js.map +1 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.js +325 -0
- package/dist/index.js.map +1 -0
- package/dist/symmetric.d.ts +3 -0
- package/dist/symmetric.js +18 -0
- package/dist/symmetric.js.map +1 -0
- package/package.json +99 -0
- package/src/constants.ts +10 -0
- package/src/crypto.ts +76 -0
- package/src/ecies.ts +194 -0
- package/src/index.ts +474 -0
- package/src/symmetric.ts +32 -0
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,OAAO,EAAE,EAAE;IACX,MAAM,EAAE,EAAE;IACV,OAAO,EAAE,EAAE;IACX,SAAS,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE;CAC5C,CAAC;AAEF,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,OAAO,EAAE,EAAE;CACZ,CAAC"}
|
package/dist/crypto.d.ts
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
import * as secp from "@noble/secp256k1";
|
2
|
+
export declare function getSubtle(): SubtleCrypto;
|
3
|
+
export declare const randomBytes: (bytesLength?: number | undefined) => Uint8Array;
|
4
|
+
export declare const sha256: (...messages: Uint8Array[]) => Promise<Uint8Array>;
|
5
|
+
/**
|
6
|
+
* Generate a new private key to be used for asymmetric encryption.
|
7
|
+
*
|
8
|
+
* Use {@link getPublicKey} to get the corresponding Public Key.
|
9
|
+
*/
|
10
|
+
export declare function generatePrivateKey(): Uint8Array;
|
11
|
+
/**
|
12
|
+
* Generate a new symmetric key to be used for symmetric encryption.
|
13
|
+
*/
|
14
|
+
export declare function generateSymmetricKey(): Uint8Array;
|
15
|
+
/**
|
16
|
+
* Return the public key for the given private key, to be used for asymmetric
|
17
|
+
* encryption.
|
18
|
+
*/
|
19
|
+
export declare const getPublicKey: typeof secp.getPublicKey;
|
20
|
+
/**
|
21
|
+
* ECDSA Sign a message with the given private key.
|
22
|
+
*
|
23
|
+
* @param message The message to sign, usually a hash.
|
24
|
+
* @param privateKey The ECDSA private key to use to sign the message.
|
25
|
+
*
|
26
|
+
* @returns The signature and the recovery id concatenated.
|
27
|
+
*/
|
28
|
+
export declare function sign(message: Uint8Array, privateKey: Uint8Array): Promise<Uint8Array>;
|
29
|
+
export declare function keccak256(input: Uint8Array): Uint8Array;
|
package/dist/crypto.js
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
import nodeCrypto from "crypto";
|
2
|
+
import * as secp from "@noble/secp256k1";
|
3
|
+
import { concat } from "@waku/byte-utils";
|
4
|
+
import sha3 from "js-sha3";
|
5
|
+
import { Asymmetric, Symmetric } from "./constants.js";
|
6
|
+
const crypto = {
|
7
|
+
node: nodeCrypto,
|
8
|
+
web: typeof self === "object" && "crypto" in self ? self.crypto : undefined,
|
9
|
+
};
|
10
|
+
export function getSubtle() {
|
11
|
+
if (crypto.web) {
|
12
|
+
return crypto.web.subtle;
|
13
|
+
}
|
14
|
+
else if (crypto.node) {
|
15
|
+
return crypto.node.webcrypto.subtle;
|
16
|
+
}
|
17
|
+
else {
|
18
|
+
throw new Error("The environment doesn't have Crypto Subtle API (if in the browser, be sure to use to be in a secure context, ie, https)");
|
19
|
+
}
|
20
|
+
}
|
21
|
+
export const randomBytes = secp.utils.randomBytes;
|
22
|
+
export const sha256 = secp.utils.sha256;
|
23
|
+
/**
|
24
|
+
* Generate a new private key to be used for asymmetric encryption.
|
25
|
+
*
|
26
|
+
* Use {@link getPublicKey} to get the corresponding Public Key.
|
27
|
+
*/
|
28
|
+
export function generatePrivateKey() {
|
29
|
+
return randomBytes(Asymmetric.keySize);
|
30
|
+
}
|
31
|
+
/**
|
32
|
+
* Generate a new symmetric key to be used for symmetric encryption.
|
33
|
+
*/
|
34
|
+
export function generateSymmetricKey() {
|
35
|
+
return randomBytes(Symmetric.keySize);
|
36
|
+
}
|
37
|
+
/**
|
38
|
+
* Return the public key for the given private key, to be used for asymmetric
|
39
|
+
* encryption.
|
40
|
+
*/
|
41
|
+
export const getPublicKey = secp.getPublicKey;
|
42
|
+
/**
|
43
|
+
* ECDSA Sign a message with the given private key.
|
44
|
+
*
|
45
|
+
* @param message The message to sign, usually a hash.
|
46
|
+
* @param privateKey The ECDSA private key to use to sign the message.
|
47
|
+
*
|
48
|
+
* @returns The signature and the recovery id concatenated.
|
49
|
+
*/
|
50
|
+
export async function sign(message, privateKey) {
|
51
|
+
const [signature, recoveryId] = await secp.sign(message, privateKey, {
|
52
|
+
recovered: true,
|
53
|
+
der: false,
|
54
|
+
});
|
55
|
+
return concat([signature, new Uint8Array([recoveryId])], signature.length + 1);
|
56
|
+
}
|
57
|
+
export function keccak256(input) {
|
58
|
+
return new Uint8Array(sha3.keccak256.arrayBuffer(input));
|
59
|
+
}
|
60
|
+
//# sourceMappingURL=crypto.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,QAAQ,CAAC;AAEhC,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,IAAI,MAAM,SAAS,CAAC;AAE3B,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGvD,MAAM,MAAM,GAA8B;IACxC,IAAI,EAAE,UAAU;IAChB,G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;AACJ,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,KAAiB;IACzC,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC;AAC3D,CAAC"}
|
package/dist/ecies.d.ts
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
/**
|
2
|
+
* Encrypt message for given recipient's public key.
|
3
|
+
*
|
4
|
+
* @param publicKeyTo Recipient's public key (65 bytes)
|
5
|
+
* @param msg The message being encrypted
|
6
|
+
* @return A promise that resolves with the ECIES structure serialized
|
7
|
+
*/
|
8
|
+
export declare function encrypt(publicKeyTo: Uint8Array, msg: Uint8Array): Promise<Uint8Array>;
|
9
|
+
/**
|
10
|
+
* Decrypt message using given private key.
|
11
|
+
*
|
12
|
+
* @param privateKey A 32-byte private key of recipient of the message
|
13
|
+
* @param encrypted ECIES serialized structure (result of ECIES encryption)
|
14
|
+
* @returns The clear text
|
15
|
+
* @throws Error If decryption fails
|
16
|
+
*/
|
17
|
+
export declare function decrypt(privateKey: Uint8Array, encrypted: Uint8Array): Promise<Uint8Array>;
|
package/dist/ecies.js
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
import * as secp from "@noble/secp256k1";
|
2
|
+
import { concat, hexToBytes } from "@waku/byte-utils";
|
3
|
+
import { getSubtle, randomBytes, sha256 } from "./crypto.js";
|
4
|
+
/**
|
5
|
+
* HKDF as implemented in go-ethereum.
|
6
|
+
*/
|
7
|
+
function kdf(secret, outputLength) {
|
8
|
+
let ctr = 1;
|
9
|
+
let written = 0;
|
10
|
+
let willBeResult = Promise.resolve(new Uint8Array());
|
11
|
+
while (written < outputLength) {
|
12
|
+
const counters = new Uint8Array([ctr >> 24, ctr >> 16, ctr >> 8, ctr]);
|
13
|
+
const countersSecret = concat([counters, secret], counters.length + secret.length);
|
14
|
+
const willBeHashResult = sha256(countersSecret);
|
15
|
+
willBeResult = willBeResult.then((result) => willBeHashResult.then((hashResult) => {
|
16
|
+
const _hashResult = new Uint8Array(hashResult);
|
17
|
+
return concat([result, _hashResult], result.length + _hashResult.length);
|
18
|
+
}));
|
19
|
+
written += 32;
|
20
|
+
ctr += 1;
|
21
|
+
}
|
22
|
+
return willBeResult;
|
23
|
+
}
|
24
|
+
function aesCtrEncrypt(counter, key, data) {
|
25
|
+
return getSubtle()
|
26
|
+
.importKey("raw", key, "AES-CTR", false, ["encrypt"])
|
27
|
+
.then((cryptoKey) => getSubtle().encrypt({ name: "AES-CTR", counter: counter, length: 128 }, cryptoKey, data))
|
28
|
+
.then((bytes) => new Uint8Array(bytes));
|
29
|
+
}
|
30
|
+
function aesCtrDecrypt(counter, key, data) {
|
31
|
+
return getSubtle()
|
32
|
+
.importKey("raw", key, "AES-CTR", false, ["decrypt"])
|
33
|
+
.then((cryptoKey) => getSubtle().decrypt({ name: "AES-CTR", counter: counter, length: 128 }, cryptoKey, data))
|
34
|
+
.then((bytes) => new Uint8Array(bytes));
|
35
|
+
}
|
36
|
+
function hmacSha256Sign(key, msg) {
|
37
|
+
const algorithm = { name: "HMAC", hash: { name: "SHA-256" } };
|
38
|
+
return getSubtle()
|
39
|
+
.importKey("raw", key, algorithm, false, ["sign"])
|
40
|
+
.then((cryptoKey) => getSubtle().sign(algorithm, cryptoKey, msg))
|
41
|
+
.then((bytes) => new Uint8Array(bytes));
|
42
|
+
}
|
43
|
+
function hmacSha256Verify(key, msg, sig) {
|
44
|
+
const algorithm = { name: "HMAC", hash: { name: "SHA-256" } };
|
45
|
+
const _key = getSubtle().importKey("raw", key, algorithm, false, ["verify"]);
|
46
|
+
return _key.then((cryptoKey) => getSubtle().verify(algorithm, cryptoKey, sig, msg));
|
47
|
+
}
|
48
|
+
/**
|
49
|
+
* Derive shared secret for given private and public keys.
|
50
|
+
*
|
51
|
+
* @param privateKeyA Sender's private key (32 bytes)
|
52
|
+
* @param publicKeyB Recipient's public key (65 bytes)
|
53
|
+
* @returns A promise that resolves with the derived shared secret (Px, 32 bytes)
|
54
|
+
* @throws Error If arguments are invalid
|
55
|
+
*/
|
56
|
+
function derive(privateKeyA, publicKeyB) {
|
57
|
+
if (privateKeyA.length !== 32) {
|
58
|
+
throw new Error(`Bad private key, it should be 32 bytes but it's actually ${privateKeyA.length} bytes long`);
|
59
|
+
}
|
60
|
+
else if (publicKeyB.length !== 65) {
|
61
|
+
throw new Error(`Bad public key, it should be 65 bytes but it's actually ${publicKeyB.length} bytes long`);
|
62
|
+
}
|
63
|
+
else if (publicKeyB[0] !== 4) {
|
64
|
+
throw new Error("Bad public key, a valid public key would begin with 4");
|
65
|
+
}
|
66
|
+
else {
|
67
|
+
const px = secp.getSharedSecret(privateKeyA, publicKeyB, true);
|
68
|
+
// Remove the compression prefix
|
69
|
+
return new Uint8Array(hexToBytes(px).slice(1));
|
70
|
+
}
|
71
|
+
}
|
72
|
+
/**
|
73
|
+
* Encrypt message for given recipient's public key.
|
74
|
+
*
|
75
|
+
* @param publicKeyTo Recipient's public key (65 bytes)
|
76
|
+
* @param msg The message being encrypted
|
77
|
+
* @return A promise that resolves with the ECIES structure serialized
|
78
|
+
*/
|
79
|
+
export async function encrypt(publicKeyTo, msg) {
|
80
|
+
const ephemPrivateKey = randomBytes(32);
|
81
|
+
const sharedPx = await derive(ephemPrivateKey, publicKeyTo);
|
82
|
+
const hash = await kdf(sharedPx, 32);
|
83
|
+
const iv = randomBytes(16);
|
84
|
+
const encryptionKey = hash.slice(0, 16);
|
85
|
+
const cipherText = await aesCtrEncrypt(iv, encryptionKey, msg);
|
86
|
+
const ivCipherText = concat([iv, cipherText], iv.length + cipherText.length);
|
87
|
+
const macKey = await sha256(hash.slice(16));
|
88
|
+
const hmac = await hmacSha256Sign(macKey, ivCipherText);
|
89
|
+
const ephemPublicKey = secp.getPublicKey(ephemPrivateKey, false);
|
90
|
+
return concat([ephemPublicKey, ivCipherText, hmac], ephemPublicKey.length + ivCipherText.length + hmac.length);
|
91
|
+
}
|
92
|
+
const metaLength = 1 + 64 + 16 + 32;
|
93
|
+
/**
|
94
|
+
* Decrypt message using given private key.
|
95
|
+
*
|
96
|
+
* @param privateKey A 32-byte private key of recipient of the message
|
97
|
+
* @param encrypted ECIES serialized structure (result of ECIES encryption)
|
98
|
+
* @returns The clear text
|
99
|
+
* @throws Error If decryption fails
|
100
|
+
*/
|
101
|
+
export async function decrypt(privateKey, encrypted) {
|
102
|
+
if (encrypted.length <= metaLength) {
|
103
|
+
throw new Error(`Invalid Ciphertext. Data is too small. It should ba at least ${metaLength} bytes`);
|
104
|
+
}
|
105
|
+
else if (encrypted[0] !== 4) {
|
106
|
+
throw new Error(`Not a valid ciphertext. It should begin with 4 but actually begin with ${encrypted[0]}`);
|
107
|
+
}
|
108
|
+
else {
|
109
|
+
// deserialize
|
110
|
+
const ephemPublicKey = encrypted.slice(0, 65);
|
111
|
+
const cipherTextLength = encrypted.length - metaLength;
|
112
|
+
const iv = encrypted.slice(65, 65 + 16);
|
113
|
+
const cipherAndIv = encrypted.slice(65, 65 + 16 + cipherTextLength);
|
114
|
+
const ciphertext = cipherAndIv.slice(16);
|
115
|
+
const msgMac = encrypted.slice(65 + 16 + cipherTextLength);
|
116
|
+
// check HMAC
|
117
|
+
const px = derive(privateKey, ephemPublicKey);
|
118
|
+
const hash = await kdf(px, 32);
|
119
|
+
const [encryptionKey, macKey] = await sha256(hash.slice(16)).then((macKey) => [hash.slice(0, 16), macKey]);
|
120
|
+
if (!(await hmacSha256Verify(macKey, cipherAndIv, msgMac))) {
|
121
|
+
throw new Error("Incorrect MAC");
|
122
|
+
}
|
123
|
+
return aesCtrDecrypt(iv, encryptionKey, ciphertext);
|
124
|
+
}
|
125
|
+
}
|
126
|
+
//# sourceMappingURL=ecies.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"ecies.js","sourceRoot":"","sources":["../src/ecies.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM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E,aAAa,EAAE,UAAU,CAAC,CAAC;KACrD;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
import { DecoderV0, MessageV0, proto } from "@waku/core/lib/waku_message/version_0";
|
2
|
+
import type { DecodedMessage, Decoder, Encoder, Message, ProtoMessage } from "@waku/interfaces";
|
3
|
+
import { generatePrivateKey, generateSymmetricKey, getPublicKey } from "./crypto.js";
|
4
|
+
export { generatePrivateKey, generateSymmetricKey, getPublicKey };
|
5
|
+
export declare const Version = 1;
|
6
|
+
export declare type Signature = {
|
7
|
+
signature: Uint8Array;
|
8
|
+
publicKey: Uint8Array | undefined;
|
9
|
+
};
|
10
|
+
export declare class MessageV1 extends MessageV0 implements DecodedMessage {
|
11
|
+
signature?: Uint8Array | undefined;
|
12
|
+
signaturePublicKey?: Uint8Array | undefined;
|
13
|
+
private readonly _decodedPayload;
|
14
|
+
constructor(proto: proto.WakuMessage, decodedPayload: Uint8Array, signature?: Uint8Array | undefined, signaturePublicKey?: Uint8Array | undefined);
|
15
|
+
get payload(): Uint8Array;
|
16
|
+
}
|
17
|
+
export declare class AsymEncoder implements Encoder {
|
18
|
+
contentTopic: string;
|
19
|
+
private publicKey;
|
20
|
+
private sigPrivKey?;
|
21
|
+
constructor(contentTopic: string, publicKey: Uint8Array, sigPrivKey?: Uint8Array | undefined);
|
22
|
+
toWire(message: Partial<Message>): Promise<Uint8Array | undefined>;
|
23
|
+
toProtoObj(message: Partial<Message>): Promise<ProtoMessage | undefined>;
|
24
|
+
}
|
25
|
+
export declare class SymEncoder implements Encoder {
|
26
|
+
contentTopic: string;
|
27
|
+
private symKey;
|
28
|
+
private sigPrivKey?;
|
29
|
+
constructor(contentTopic: string, symKey: Uint8Array, sigPrivKey?: Uint8Array | undefined);
|
30
|
+
toWire(message: Partial<Message>): Promise<Uint8Array | undefined>;
|
31
|
+
toProtoObj(message: Partial<Message>): Promise<ProtoMessage | undefined>;
|
32
|
+
}
|
33
|
+
export declare class AsymDecoder extends DecoderV0 implements Decoder<MessageV1> {
|
34
|
+
private privateKey;
|
35
|
+
constructor(contentTopic: string, privateKey: Uint8Array);
|
36
|
+
fromProtoObj(protoMessage: ProtoMessage): Promise<MessageV1 | undefined>;
|
37
|
+
}
|
38
|
+
export declare class SymDecoder extends DecoderV0 implements Decoder<MessageV1> {
|
39
|
+
private symKey;
|
40
|
+
constructor(contentTopic: string, symKey: Uint8Array);
|
41
|
+
fromProtoObj(protoMessage: ProtoMessage): Promise<MessageV1 | undefined>;
|
42
|
+
}
|
43
|
+
/**
|
44
|
+
* Proceed with Asymmetric encryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
45
|
+
* The data MUST be flags | payload-length | payload | [signature].
|
46
|
+
* The returned result can be set to `WakuMessage.payload`.
|
47
|
+
*
|
48
|
+
* @internal
|
49
|
+
*/
|
50
|
+
export declare function encryptAsymmetric(data: Uint8Array, publicKey: Uint8Array | string): Promise<Uint8Array>;
|
51
|
+
/**
|
52
|
+
* Proceed with Asymmetric decryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
53
|
+
* The returned data is expected to be `flags | payload-length | payload | [signature]`.
|
54
|
+
*
|
55
|
+
* @internal
|
56
|
+
*/
|
57
|
+
export declare function decryptAsymmetric(payload: Uint8Array, privKey: Uint8Array): Promise<Uint8Array>;
|
58
|
+
/**
|
59
|
+
* Proceed with Symmetric encryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
60
|
+
*
|
61
|
+
* @param data The data to encrypt, expected to be `flags | payload-length | payload | [signature]`.
|
62
|
+
* @param key The key to use for encryption.
|
63
|
+
* @returns The decrypted data, `cipherText | tag | iv` and can be set to `WakuMessage.payload`.
|
64
|
+
*
|
65
|
+
* @internal
|
66
|
+
*/
|
67
|
+
export declare function encryptSymmetric(data: Uint8Array, key: Uint8Array | string): Promise<Uint8Array>;
|
68
|
+
/**
|
69
|
+
* Proceed with Symmetric decryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
70
|
+
*
|
71
|
+
* @param payload The cipher data, it is expected to be `cipherText | tag | iv`.
|
72
|
+
* @param key The key to use for decryption.
|
73
|
+
* @returns The decrypted data, expected to be `flags | payload-length | payload | [signature]`.
|
74
|
+
*
|
75
|
+
* @internal
|
76
|
+
*/
|
77
|
+
export declare function decryptSymmetric(payload: Uint8Array, key: Uint8Array | string): Promise<Uint8Array>;
|
78
|
+
/**
|
79
|
+
* Prepare the payload pre-encryption.
|
80
|
+
*
|
81
|
+
* @internal
|
82
|
+
* @returns The encoded payload, ready for encryption using {@link encryptAsymmetric}
|
83
|
+
* or {@link encryptSymmetric}.
|
84
|
+
*/
|
85
|
+
export declare function preCipher(messagePayload: Uint8Array, sigPrivKey?: Uint8Array): Promise<Uint8Array>;
|
86
|
+
/**
|
87
|
+
* Decode a decrypted payload.
|
88
|
+
*
|
89
|
+
* @internal
|
90
|
+
*/
|
91
|
+
export declare function postCipher(message: Uint8Array): {
|
92
|
+
payload: Uint8Array;
|
93
|
+
sig?: Signature;
|
94
|
+
} | undefined;
|