allow 16-byte keys for Triple DES in CMAC operation

This commit is contained in:
MikeCAT 2022-11-03 01:20:37 +09:00
parent c0bd6645ce
commit fe9eb08648
2 changed files with 6 additions and 4 deletions

View File

@ -57,15 +57,17 @@ class CMAC extends Operation {
} }
return { return {
"algorithm": "AES-ECB", "algorithm": "AES-ECB",
"key": key,
"blockSize": 16, "blockSize": 16,
"Rb": new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x87]), "Rb": new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x87]),
}; };
case "Triple DES": case "Triple DES":
if (key.length !== 24) { if (key.length !== 16 && key.length !== 24) {
throw new OperationError("the key for Triple DES must be 24 bytes (currently " + key.length + " bytes)"); throw new OperationError("the key for Triple DES must be 16 or 24 bytes (currently " + key.length + " bytes)");
} }
return { return {
"algorithm": "3DES-ECB", "algorithm": "3DES-ECB",
"key": key.length === 16 ? key + key.substring(0, 8) : key,
"blockSize": 8, "blockSize": 8,
"Rb": new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0x1b]), "Rb": new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0x1b]),
}; };
@ -89,7 +91,7 @@ class CMAC extends Operation {
} }
return out; return out;
}; };
const cipher = forge.cipher.createCipher(info.algorithm, key); const cipher = forge.cipher.createCipher(info.algorithm, info.key);
const encrypt = function(a, out) { const encrypt = function(a, out) {
if (!out) out = new Uint8Array(a.length); if (!out) out = new Uint8Array(a.length);
cipher.start(); cipher.start();

View File

@ -303,7 +303,7 @@ TestRegister.addTests([
{ {
"name": "CMAC-TDES: invalid key length", "name": "CMAC-TDES: invalid key length",
"input": "", "input": "",
"expectedOutput": "the key for Triple DES must be 24 bytes (currently 20 bytes)", "expectedOutput": "the key for Triple DES must be 16 or 24 bytes (currently 20 bytes)",
"recipeConfig": [ "recipeConfig": [
{ {
"op": "CMAC", "op": "CMAC",