aboutsummaryrefslogtreecommitdiff
path: root/test/webcrypto/aes_decoding.t.mjs
blob: 80eaf6429a5acb566b30f9e503987916e62e7bed (plain)
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
/*---
includes: [compatFs.js, compatBuffer.js, compatWebcrypto.js, runTsuite.js, webCryptoUtils.js]
flags: [async]
---*/

async function test(params) {
    let enc = base64decode(fs.readFileSync(`test/webcrypto/${params.file}`));
    let key = await crypto.subtle.importKey("raw", params.key,
                                            {name: params.name},
                                            false, ["decrypt"]);

    let plaintext = await crypto.subtle.decrypt(params, key, enc);
    plaintext = new TextDecoder().decode(plaintext);

    if (params.expected != plaintext) {
        throw Error(`${params.name} decoding failed expected: "${params.expected}" vs "${plaintext}"`);
    }

    return 'SUCCESS';
}

function p(args, default_opts) {
    let params = Object.assign({}, default_opts, args);

    params.key = Buffer.from(params.key, "hex");
    params.iv = Buffer.from(params.iv, "hex");
    params.counter = Buffer.from(params.counter, "hex");

    switch (params.name) {
    case "AES-GCM":
        if (params.additionalData) {
            params.additionalData = Buffer.from(params.additionalData, "hex");
        }

        break;
    }

    return params;
}

let aes_tsuite = {
    name: "AES decoding",
    skip: () => (!has_buffer() || !has_webcrypto()),
    T: test,
    prepare_args: p,
    opts: {
        key: "00112233001122330011223300112233",
        iv: "44556677445566774455667744556677",
        counter: "44556677445566774455667744556677",
        length: 64
    },

    tests: [
        { name: "AES-GCM", file: "text.base64.aes-gcm128.enc",
          expected: "AES-GCM-SECRET-TEXT" },
        { name: "AES-GCM", file: "text.base64.aes-gcm128-96.enc",
          exception: "Error: EVP_DecryptFinal_ex() failed" },
        { name: "AES-GCM", file: "text.base64.aes-gcm128-96.enc", tagLength: 96,
          expected: "AES-GCM-96-TAG-LENGTH-SECRET-TEXT" },
        { name: "AES-GCM", file: "text.base64.aes-gcm128-extra.enc", additionalData: "deadbeef",
          expected: "AES-GCM-ADDITIONAL-DATA-SECRET-TEXT" },
        { name: "AES-GCM", file: "text.base64.aes-gcm256.enc",
          key: "0011223300112233001122330011223300112233001122330011223300112233",
          expected: "AES-GCM-256-SECRET-TEXT" },
        { name: "AES-GCM", file: "text.base64.aes-gcm256.enc",
          key: "00112233001122330011223300112233001122330011223300112233001122",
          exception: "TypeError: AES-GCM Invalid key length" },
        { name: "AES-CTR", file: "text.base64.aes-ctr128.enc",
          expected: "AES-CTR-SECRET-TEXT" },
        { name: "AES-CTR", file: "text.base64.aes-ctr256.enc",
          key: "0011223300112233001122330011223300112233001122330011223300112233",
          expected: "AES-CTR-256-SECRET-TEXT" },
        { name: "AES-CBC", file: "text.base64.aes-cbc128.enc",
          expected: "AES-CBC-SECRET-TEXT" },
        { name: "AES-CBC", file: "text.base64.aes-cbc256.enc",
          key: "0011223300112233001122330011223300112233001122330011223300112233",
          expected: "AES-CBC-256-SECRET-TEXT" },
]};

run([aes_tsuite])
.then($DONE, $DONE);