-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterfaces.ts
More file actions
120 lines (110 loc) · 3.9 KB
/
Copy pathinterfaces.ts
File metadata and controls
120 lines (110 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (C) 2022 Deliberative Technologies P.C.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export interface SignKeyPair {
publicKey: Uint8Array;
secretKey: Uint8Array;
}
export type TypedArray =
| Int8Array
| Uint8Array
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Uint8ClampedArray
| Float32Array
| Float64Array;
export const crypto_hash_sha512_BYTES = 64 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_secretbox_KEYBYTES = 32 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_secretbox_NONCEBYTES = 24 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_box_poly1305_AUTHTAGBYTES =
16 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_box_x25519_PUBLICKEYBYTES =
32 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_box_x25519_SECRETKEYBYTES =
32 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_box_x25519_NONCEBYTES = 12 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_kx_SESSIONKEYBYTES = 32 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_sign_ed25519_BYTES = 64 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_sign_ed25519_SEEDBYTES = 32 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_sign_ed25519_PUBLICKEYBYTES =
32 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_sign_ed25519_SECRETKEYBYTES =
64 * Uint8Array.BYTES_PER_ELEMENT;
export const crypto_pwhash_argon2id_SALTBYTES =
16 * Uint8Array.BYTES_PER_ELEMENT;
export const getEncryptedLen = (dataLen: number) => {
return (
crypto_box_x25519_NONCEBYTES + // xchacha uses 24 byte nonce while ietf 12
dataLen +
crypto_box_poly1305_AUTHTAGBYTES // 16 bytes poly1305 auth tag
);
};
export const getDecryptedLen = (encryptedLen: number) => {
return (
encryptedLen -
crypto_box_x25519_NONCEBYTES - // nonce
crypto_box_poly1305_AUTHTAGBYTES // authTag
);
};
export const getForwardSecretBoxEncryptedLen = (dataLen: number) => {
return (
crypto_box_x25519_PUBLICKEYBYTES + // ephemeral x25519 public key
crypto_box_x25519_NONCEBYTES + // xchacha uses 24 byte nonce while ietf 12
dataLen +
crypto_box_poly1305_AUTHTAGBYTES // 16 bytes poly1305 auth tag
);
};
export const getForwardSecretBoxDecryptedLen = (encryptedLen: number) => {
return (
encryptedLen -
crypto_box_x25519_PUBLICKEYBYTES - // x25519 ephemeral
crypto_box_x25519_NONCEBYTES - // nonce
crypto_box_poly1305_AUTHTAGBYTES // authTag
);
};
export const getE2EEncryptedSecretBoxEncryptedLen = (dataLen: number) => {
return (
crypto_box_x25519_NONCEBYTES + dataLen + crypto_box_poly1305_AUTHTAGBYTES
);
};
export const getE2EEncryptedSecretBoxDecryptedLen = (encryptedLen: number) => {
return (
encryptedLen -
crypto_box_x25519_NONCEBYTES - // nonce
crypto_box_poly1305_AUTHTAGBYTES // authTag
);
};
export default {
crypto_hash_sha512_BYTES,
crypto_secretbox_KEYBYTES,
crypto_secretbox_NONCEBYTES,
crypto_box_poly1305_AUTHTAGBYTES,
crypto_box_x25519_PUBLICKEYBYTES,
crypto_box_x25519_SECRETKEYBYTES,
crypto_box_x25519_NONCEBYTES,
crypto_kx_SESSIONKEYBYTES,
crypto_sign_ed25519_BYTES,
crypto_sign_ed25519_SEEDBYTES,
crypto_sign_ed25519_PUBLICKEYBYTES,
crypto_sign_ed25519_SECRETKEYBYTES,
crypto_pwhash_argon2id_SALTBYTES,
getEncryptedLen,
getDecryptedLen,
getForwardSecretBoxEncryptedLen,
getForwardSecretBoxDecryptedLen,
getE2EEncryptedSecretBoxEncryptedLen,
getE2EEncryptedSecretBoxDecryptedLen,
};