Skip to content

Add ECB/NoPadding and CBC/NoPadding support to AES encryption #2013

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

Merged
merged 4 commits into from
Apr 5, 2025
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/core/operations/AESEncrypt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class AESEncrypt extends Operation {
{
name: "ECB",
off: [5]
},
{
name: "CBC/NoPadding",
off: [5]
},
{
name: "ECB/NoPadding",
off: [5]
}
]
},
Expand Down Expand Up @@ -98,7 +106,8 @@ class AESEncrypt extends Operation {
run(input, args) {
const key = Utils.convertToByteString(args[0].string, args[0].option),
iv = Utils.convertToByteString(args[1].string, args[1].option),
mode = args[2],
mode = args[2].substring(0, 3),
noPadding = args[2].endsWith("NoPadding"),
inputType = args[3],
outputType = args[4],
aad = Utils.convertToByteString(args[5].string, args[5].option);
Expand All @@ -114,11 +123,20 @@ The following algorithms will be used based on the size of the key:

input = Utils.convertToByteString(input, inputType);

// Handle NoPadding modes
if (noPadding && input.length % 16 !== 0) {
throw new OperationError("Input length must be a multiple of 16 bytes for NoPadding modes.");
}
const cipher = forge.cipher.createCipher("AES-" + mode, key);
cipher.start({
iv: iv,
additionalData: mode === "GCM" ? aad : undefined
});
if (noPadding) {
cipher.mode.pad = function(output, options) {
return true;
};
}
cipher.update(forge.util.createBuffer(input));
cipher.finish();

Expand Down
Loading