Skip to content

Commit

Permalink
partial fix
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua committed Feb 15, 2024
1 parent 9979f2e commit b52deba
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 360 deletions.
1 change: 1 addition & 0 deletions test_util/std
Submodule std added at e0ef24
103 changes: 102 additions & 1 deletion tests/unit_node/crypto/crypto_cipher_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import crypto from "node:crypto";
import { Buffer } from "node:buffer";
import { Readable } from "node:stream";
import { buffer, text } from "node:stream/consumers";
import { assertEquals, assertThrows } from "@std/assert/mod.ts";
import {
assertEquals,
assertStrictEquals,
assertThrows,
} from "@std/assert/mod.ts";

const rsaPrivateKey = Deno.readTextFileSync(
new URL("../testdata/rsa_private.pem", import.meta.url),
Expand Down Expand Up @@ -256,3 +260,100 @@ Deno.test({
);
},
});

function setAutoPaddingTest(
{ algorithm, keyLength, pad }: {
algorithm: string;
keyLength: number;
pad: boolean;
},
) {
const key = crypto.randomBytes(keyLength);
const iv = algorithm.endsWith("ecb") ? null : crypto.randomBytes(16);
const data = pad
? "0123456789abcdef0123456789abcde" // Not a multiple of block size
: "0123456789abcdef0123456789abcdef"; // Multiple of block size

const cipher = crypto.createCipheriv(algorithm, key, iv);
cipher.setAutoPadding(pad);
const encrypted = cipher.update(data, "utf8", "latin1") +
cipher.final("latin1");

const decipher = crypto.createDecipheriv(algorithm, key, iv);
decipher.setAutoPadding(pad);
const decrypted = decipher.update(encrypted, "latin1", "utf8") +
decipher.final("utf8");

assertStrictEquals(decrypted, data);
}

/**
* @todo(iuioiua) Add `*-gcm` algorithms once `Cipher.getAuthTag()` and
* `Decipher.setAuthTag()` are implemented.
*/
[
{
algorithm: "aes-128-cbc",
keyLength: 16,
pad: false,
},
{
algorithm: "aes-128-cbc",
keyLength: 16,
pad: true,
},
{
algorithm: "aes-128-ecb",
keyLength: 16,
pad: false,
},
{
algorithm: "aes-128-ecb",
keyLength: 16,
pad: true,
},
{
algorithm: "aes-192-ecb",
keyLength: 24,
pad: false,
},
{
algorithm: "aes-192-ecb",
keyLength: 24,
pad: true,
},
{
algorithm: "aes256",
keyLength: 32,
pad: false,
},
{
algorithm: "aes256",
keyLength: 32,
pad: true,
},
{
algorithm: "aes-256-cbc",
keyLength: 32,
pad: false,
},
{
algorithm: "aes-256-cbc",
keyLength: 32,
pad: true,
},
{
algorithm: "aes-256-ecb",
keyLength: 32,
pad: false,
},
{
algorithm: "aes-256-ecb",
keyLength: 32,
pad: true,
},
].forEach((options) => {
Deno.test(`cipher.setAutoPadding() and decipher.setAutoPadding() - ${options.algorithm} ${options.pad ? "with" : "without"} padding`, () => {
setAutoPaddingTest(options);
});
});
Loading

0 comments on commit b52deba

Please sign in to comment.