aboutsummaryrefslogtreecommitdiffstats
path: root/library/sodium-plus/lib/util.js
blob: e19030d0ed097a03060b5b929d490f9f98fd4bf1 (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
"use strict";

/* istanbul ignore if */
if (typeof (Buffer) === 'undefined') {
    let Buffer = require('buffer/').Buffer;
}

const arrayToBuffer = require('typedarray-to-buffer');

module.exports = class Util
{
    static async cloneBuffer(buf) {
        return Buffer.from(buf);
    }

    /**
     * Define the sodium constants
     *
     * @param {object} anyobject
     * @return {object}
     */
    static populateConstants(anyobject) {
        anyobject.LIBRARY_VERSION_MAJOR = 10;
        anyobject.LIBRARY_VERSION_MINOR = 2;
        anyobject.VERSION_STRING = '1.0.17';
        anyobject.BASE64_VARIANT_ORIGINAL = 1;
        anyobject.BASE64_VARIANT_ORIGINAL_NO_PADDING = 3;
        anyobject.BASE64_VARIANT_URLSAFE = 5;
        anyobject.BASE64_VARIANT_URLSAFE_NO_PADDING = 7;
        anyobject.CRYPTO_AEAD_AES256GCM_KEYBYTES = 32;
        anyobject.CRYPTO_AEAD_AES256GCM_NSECBYTES = 0;
        anyobject.CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12;
        anyobject.CRYPTO_AEAD_AES256GCM_ABYTES = 16;
        anyobject.CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32;
        anyobject.CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0;
        anyobject.CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8;
        anyobject.CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16;
        anyobject.CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = 32;
        anyobject.CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES = 0;
        anyobject.CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = 12;
        anyobject.CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES = 16;
        anyobject.CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES = 32;
        anyobject.CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES = 0;
        anyobject.CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES = 24;
        anyobject.CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES = 16;
        anyobject.CRYPTO_AUTH_BYTES = 32;
        anyobject.CRYPTO_AUTH_KEYBYTES = 32;
        anyobject.CRYPTO_BOX_SEALBYTES = 16;
        anyobject.CRYPTO_BOX_SECRETKEYBYTES = 32;
        anyobject.CRYPTO_BOX_PUBLICKEYBYTES = 32;
        anyobject.CRYPTO_BOX_KEYPAIRBYTES = 64;
        anyobject.CRYPTO_BOX_MACBYTES = 16;
        anyobject.CRYPTO_BOX_NONCEBYTES = 24;
        anyobject.CRYPTO_BOX_SEEDBYTES = 32;
        anyobject.CRYPTO_KDF_BYTES_MIN = 16;
        anyobject.CRYPTO_KDF_BYTES_MAX = 64;
        anyobject.CRYPTO_KDF_CONTEXTBYTES = 8;
        anyobject.CRYPTO_KDF_KEYBYTES = 32;
        anyobject.CRYPTO_KX_BYTES = 32;
        anyobject.CRYPTO_KX_PRIMITIVE = 'x25519blake2b';
        anyobject.CRYPTO_KX_SEEDBYTES = 32;
        anyobject.CRYPTO_KX_KEYPAIRBYTES = 64;
        anyobject.CRYPTO_KX_PUBLICKEYBYTES = 32;
        anyobject.CRYPTO_KX_SECRETKEYBYTES = 32;
        anyobject.CRYPTO_KX_SESSIONKEYBYTES = 32;
        anyobject.CRYPTO_GENERICHASH_BYTES = 32;
        anyobject.CRYPTO_GENERICHASH_BYTES_MIN = 16;
        anyobject.CRYPTO_GENERICHASH_BYTES_MAX = 64;
        anyobject.CRYPTO_GENERICHASH_KEYBYTES = 32;
        anyobject.CRYPTO_GENERICHASH_KEYBYTES_MIN = 16;
        anyobject.CRYPTO_GENERICHASH_KEYBYTES_MAX = 64;
        anyobject.CRYPTO_GENERICHASH_STATEBYTES = 384;
        anyobject.CRYPTO_PWHASH_SALTBYTES = 16;
        anyobject.CRYPTO_PWHASH_STRPREFIX = '$argon2id$';
        anyobject.CRYPTO_PWHASH_ALG_ARGON2I13 = 1;
        anyobject.CRYPTO_PWHASH_ALG_ARGON2ID13 = 2;
        anyobject.CRYPTO_PWHASH_ALG_DEFAULT = anyobject.CRYPTO_PWHASH_ALG_ARGON2ID13;
        anyobject.CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 2;
        anyobject.CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 67108864;
        anyobject.CRYPTO_PWHASH_OPSLIMIT_MODERATE = 3;
        anyobject.CRYPTO_PWHASH_MEMLIMIT_MODERATE = 268435456;
        anyobject.CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 4;
        anyobject.CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 1073741824;
        anyobject.CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES = 32;
        anyobject.CRYPTO_SCALARMULT_BYTES = 32;
        anyobject.CRYPTO_SCALARMULT_SCALARBYTES = 32;
        anyobject.CRYPTO_SHORTHASH_BYTES = 8;
        anyobject.CRYPTO_SHORTHASH_KEYBYTES = 16;
        anyobject.CRYPTO_SECRETBOX_KEYBYTES = 32;
        anyobject.CRYPTO_SECRETBOX_MACBYTES = 16;
        anyobject.CRYPTO_SECRETBOX_NONCEBYTES = 24;
        anyobject.CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_STATEBYTES = 52;
        anyobject.CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES = 17;
        anyobject.CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES = 24;
        anyobject.CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES = 32;
        anyobject.CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH = 0;
        anyobject.CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL = 1;
        anyobject.CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY = 2;
        anyobject.CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL = 3;
        anyobject.CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX = 0x3fffffff80;
        anyobject.CRYPTO_SIGN_BYTES = 64;
        anyobject.CRYPTO_SIGN_SEEDBYTES = 32;
        anyobject.CRYPTO_SIGN_PUBLICKEYBYTES = 32;
        anyobject.CRYPTO_SIGN_SECRETKEYBYTES = 64;
        anyobject.CRYPTO_SIGN_KEYPAIRBYTES = 96;
        anyobject.CRYPTO_STREAM_KEYBYTES = 32;
        anyobject.CRYPTO_STREAM_NONCEBYTES = 24;
        return anyobject;
    }

    /**
     * Coerce input to a Buffer, throwing a TypeError if it cannot be coerced.
     *
     * @param {string|Buffer|Uint8Array|Promise<Buffer>} stringOrBuffer
     * @returns {Buffer}
     */
    static async toBuffer(stringOrBuffer)
    {
        if (Buffer.isBuffer(stringOrBuffer)) {
            return stringOrBuffer;
        } else if (stringOrBuffer === null) {
            return null;
        } else if (typeof(stringOrBuffer) === 'string') {
            return Buffer.from(stringOrBuffer, 'binary');
        } else if (stringOrBuffer instanceof Uint8Array) {
            return arrayToBuffer(stringOrBuffer);
        } else if (stringOrBuffer instanceof Promise) {
            return await stringOrBuffer;
        } else {
            throw new TypeError('Invalid type; string or buffer expected');
        }
    }
};