Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): implement aes-128-ctr, aes-192-ctr, and aes-256-ctr #27630

Merged
merged 9 commits into from
Jan 28, 2025

Conversation

nathanwhit
Copy link
Member

Fixes #24864

Need to add some tests, also unsure about the right counter size (went with 128 bit to be safe)

@nathanwhit nathanwhit requested review from littledivy and kt3k January 10, 2025 23:52
@nathanwhit nathanwhit changed the title implement aes-128-ctr and aes-256-ctr fix(ext/node): implement aes-128-ctr and aes-256-ctr Jan 11, 2025
@kt3k
Copy link
Member

kt3k commented Jan 15, 2025

I confirmed this works in the same way as Node.js. Nice work!

import crypto from "node:crypto";
import { Buffer } from "node:buffer";

var algorithm = "aes-128-ctr",
  key = Buffer.from("5ebe2294ecd0e0f08eab7690d2a6ee69", "hex"),
  iv = Buffer.from("26ae5cc854e36b6bdfca366848dea6bb", "hex");

function encrypt(text) {
  const cipher = crypto.createCipheriv(algorithm, key, iv);
  var crypted = cipher.update(text, "utf8", "hex");
  crypted += cipher.final("hex");
  return crypted;
}

function decrypt(text) {
  var decipher = crypto.createDecipheriv(algorithm, key, iv);
  var dec = decipher.update(text, "hex", "utf8");
  dec += decipher.final("utf8");
  return dec;
}

var hw = encrypt("hello world");
console.log(hw);
console.log(decrypt(hw));
$ node a.mjs 
e50b8429a7297331661217
hello world
$ debug_deno a.mjs 
e50b8429a7297331661217
hello world

also unsure about the right counter size (went with 128 bit to be safe)

This seems fine to me. Node.js seems throwing TypeError: Invalid initialization vector when IV is not exactly 16 bytes long.

@nathanwhit Can you add some simple test cases?

@nathanwhit
Copy link
Member Author

@kt3k added tests, including for invalid key/iv length

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@kt3k kt3k changed the title fix(ext/node): implement aes-128-ctr and aes-256-ctr fix(ext/node): implement aes-128-ctr, aes-192-ctr, and aes-256-ctr Jan 28, 2025
@nathanwhit nathanwhit merged commit 094e268 into denoland:main Jan 28, 2025
18 checks passed
@nathanwhit nathanwhit deleted the aes-ctr branch January 28, 2025 07:36
bartlomieju pushed a commit that referenced this pull request Jan 30, 2025
…tr` (#27630)

Fixes #24864

Need to add some tests, also unsure about the right counter size (went
with 128 bit to be safe)

---------

Co-authored-by: Yoshiya Hinosawa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

crypto.createCipheriv('aes-256-ctr', …) fails with Unknown cipher
2 participants