From 86d59783fa40a5a4a6b0fbe6bc0eddd14fc39e31 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Thu, 16 May 2024 18:09:12 +0100 Subject: [PATCH] Improved GOST algorithm naming and block size selection --- src/core/operations/GOSTDecrypt.mjs | 37 ++++++++++++++++++--------- src/core/operations/GOSTEncrypt.mjs | 37 ++++++++++++++++++--------- src/core/operations/GOSTKeyUnwrap.mjs | 37 ++++++++++++++++++--------- src/core/operations/GOSTKeyWrap.mjs | 37 ++++++++++++++++++--------- src/core/operations/GOSTSign.mjs | 37 ++++++++++++++++++--------- src/core/operations/GOSTVerify.mjs | 37 ++++++++++++++++++--------- tests/operations/tests/GOST.mjs | 22 +++++----------- 7 files changed, 157 insertions(+), 87 deletions(-) diff --git a/src/core/operations/GOSTDecrypt.mjs b/src/core/operations/GOSTDecrypt.mjs index 8259a0d4..e9699f84 100644 --- a/src/core/operations/GOSTDecrypt.mjs +++ b/src/core/operations/GOSTDecrypt.mjs @@ -55,22 +55,19 @@ class GOSTDecrypt extends Operation { type: "argSelector", value: [ { - name: "GOST 28147 (Magma, 1989)", - off: [5], + name: "GOST 28147 (1989)", on: [6] }, + { + name: "GOST R 34.12 (Magma, 2015)", + off: [5] + }, { name: "GOST R 34.12 (Kuznyechik, 2015)", - on: [5], - off: [6] + off: [5] } ] }, - { - name: "Block length", - type: "option", - value: ["64", "128"] - }, { name: "sBox", type: "option", @@ -100,14 +97,30 @@ class GOSTDecrypt extends Operation { * @returns {string} */ async run(input, args) { - const [keyObj, ivObj, inputType, outputType, version, length, sBox, blockMode, keyMeshing, padding] = args; + const [keyObj, ivObj, inputType, outputType, version, sBox, blockMode, keyMeshing, padding] = args; const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option)); const iv = toHexFast(Utils.convertToByteArray(ivObj.string, ivObj.option)); input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input)); - const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015; - const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10); + let blockLength, versionNum; + switch (version) { + case "GOST 28147 (1989)": + versionNum = 1989; + blockLength = 64; + break; + case "GOST R 34.12 (Magma, 2015)": + versionNum = 2015; + blockLength = 64; + break; + case "GOST R 34.12 (Kuznyechik, 2015)": + versionNum = 2015; + blockLength = 128; + break; + default: + throw new OperationError(`Unknown algorithm version: ${version}`); + } + const sBoxVal = versionNum === 1989 ? sBox : null; const algorithm = { diff --git a/src/core/operations/GOSTEncrypt.mjs b/src/core/operations/GOSTEncrypt.mjs index ce92ecda..cb67476c 100644 --- a/src/core/operations/GOSTEncrypt.mjs +++ b/src/core/operations/GOSTEncrypt.mjs @@ -55,22 +55,19 @@ class GOSTEncrypt extends Operation { type: "argSelector", value: [ { - name: "GOST 28147 (Magma, 1989)", - off: [5], + name: "GOST 28147 (1989)", on: [6] }, + { + name: "GOST R 34.12 (Magma, 2015)", + off: [5] + }, { name: "GOST R 34.12 (Kuznyechik, 2015)", - on: [5], - off: [6] + off: [5] } ] }, - { - name: "Block length", - type: "option", - value: ["64", "128"] - }, { name: "sBox", type: "option", @@ -100,14 +97,30 @@ class GOSTEncrypt extends Operation { * @returns {string} */ async run(input, args) { - const [keyObj, ivObj, inputType, outputType, version, length, sBox, blockMode, keyMeshing, padding] = args; + const [keyObj, ivObj, inputType, outputType, version, sBox, blockMode, keyMeshing, padding] = args; const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option)); const iv = toHexFast(Utils.convertToByteArray(ivObj.string, ivObj.option)); input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input)); - const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015; - const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10); + let blockLength, versionNum; + switch (version) { + case "GOST 28147 (1989)": + versionNum = 1989; + blockLength = 64; + break; + case "GOST R 34.12 (Magma, 2015)": + versionNum = 2015; + blockLength = 64; + break; + case "GOST R 34.12 (Kuznyechik, 2015)": + versionNum = 2015; + blockLength = 128; + break; + default: + throw new OperationError(`Unknown algorithm version: ${version}`); + } + const sBoxVal = versionNum === 1989 ? sBox : null; const algorithm = { diff --git a/src/core/operations/GOSTKeyUnwrap.mjs b/src/core/operations/GOSTKeyUnwrap.mjs index afcd6287..7a3e29cb 100644 --- a/src/core/operations/GOSTKeyUnwrap.mjs +++ b/src/core/operations/GOSTKeyUnwrap.mjs @@ -55,22 +55,19 @@ class GOSTKeyUnwrap extends Operation { type: "argSelector", value: [ { - name: "GOST 28147 (Magma, 1989)", - off: [5], + name: "GOST 28147 (1989)", on: [6] }, + { + name: "GOST R 34.12 (Magma, 2015)", + off: [5] + }, { name: "GOST R 34.12 (Kuznyechik, 2015)", - on: [5], - off: [6] + off: [5] } ] }, - { - name: "Block length", - type: "option", - value: ["64", "128"] - }, { name: "sBox", type: "option", @@ -90,14 +87,30 @@ class GOSTKeyUnwrap extends Operation { * @returns {string} */ async run(input, args) { - const [keyObj, ukmObj, inputType, outputType, version, length, sBox, keyWrapping] = args; + const [keyObj, ukmObj, inputType, outputType, version, sBox, keyWrapping] = args; const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option)); const ukm = toHexFast(Utils.convertToByteArray(ukmObj.string, ukmObj.option)); input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input)); - const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015; - const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10); + let blockLength, versionNum; + switch (version) { + case "GOST 28147 (1989)": + versionNum = 1989; + blockLength = 64; + break; + case "GOST R 34.12 (Magma, 2015)": + versionNum = 2015; + blockLength = 64; + break; + case "GOST R 34.12 (Kuznyechik, 2015)": + versionNum = 2015; + blockLength = 128; + break; + default: + throw new OperationError(`Unknown algorithm version: ${version}`); + } + const sBoxVal = versionNum === 1989 ? sBox : null; const algorithm = { diff --git a/src/core/operations/GOSTKeyWrap.mjs b/src/core/operations/GOSTKeyWrap.mjs index 5a3fd4e6..d8692023 100644 --- a/src/core/operations/GOSTKeyWrap.mjs +++ b/src/core/operations/GOSTKeyWrap.mjs @@ -55,22 +55,19 @@ class GOSTKeyWrap extends Operation { type: "argSelector", value: [ { - name: "GOST 28147 (Magma, 1989)", - off: [5], + name: "GOST 28147 (1989)", on: [6] }, + { + name: "GOST R 34.12 (Magma, 2015)", + off: [5] + }, { name: "GOST R 34.12 (Kuznyechik, 2015)", - on: [5], - off: [6] + off: [5] } ] }, - { - name: "Block length", - type: "option", - value: ["64", "128"] - }, { name: "sBox", type: "option", @@ -90,14 +87,30 @@ class GOSTKeyWrap extends Operation { * @returns {string} */ async run(input, args) { - const [keyObj, ukmObj, inputType, outputType, version, length, sBox, keyWrapping] = args; + const [keyObj, ukmObj, inputType, outputType, version, sBox, keyWrapping] = args; const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option)); const ukm = toHexFast(Utils.convertToByteArray(ukmObj.string, ukmObj.option)); input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input)); - const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015; - const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10); + let blockLength, versionNum; + switch (version) { + case "GOST 28147 (1989)": + versionNum = 1989; + blockLength = 64; + break; + case "GOST R 34.12 (Magma, 2015)": + versionNum = 2015; + blockLength = 64; + break; + case "GOST R 34.12 (Kuznyechik, 2015)": + versionNum = 2015; + blockLength = 128; + break; + default: + throw new OperationError(`Unknown algorithm version: ${version}`); + } + const sBoxVal = versionNum === 1989 ? sBox : null; const algorithm = { diff --git a/src/core/operations/GOSTSign.mjs b/src/core/operations/GOSTSign.mjs index 9195f469..d83caab7 100644 --- a/src/core/operations/GOSTSign.mjs +++ b/src/core/operations/GOSTSign.mjs @@ -55,22 +55,19 @@ class GOSTSign extends Operation { type: "argSelector", value: [ { - name: "GOST 28147 (Magma, 1989)", - off: [5], + name: "GOST 28147 (1989)", on: [6] }, + { + name: "GOST R 34.12 (Magma, 2015)", + off: [5] + }, { name: "GOST R 34.12 (Kuznyechik, 2015)", - on: [5], - off: [6] + off: [5] } ] }, - { - name: "Block length", - type: "option", - value: ["64", "128"] - }, { name: "sBox", type: "option", @@ -93,14 +90,30 @@ class GOSTSign extends Operation { * @returns {string} */ async run(input, args) { - const [keyObj, ivObj, inputType, outputType, version, length, sBox, macLength] = args; + const [keyObj, ivObj, inputType, outputType, version, sBox, macLength] = args; const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option)); const iv = toHexFast(Utils.convertToByteArray(ivObj.string, ivObj.option)); input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input)); - const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015; - const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10); + let blockLength, versionNum; + switch (version) { + case "GOST 28147 (1989)": + versionNum = 1989; + blockLength = 64; + break; + case "GOST R 34.12 (Magma, 2015)": + versionNum = 2015; + blockLength = 64; + break; + case "GOST R 34.12 (Kuznyechik, 2015)": + versionNum = 2015; + blockLength = 128; + break; + default: + throw new OperationError(`Unknown algorithm version: ${version}`); + } + const sBoxVal = versionNum === 1989 ? sBox : null; const algorithm = { diff --git a/src/core/operations/GOSTVerify.mjs b/src/core/operations/GOSTVerify.mjs index a270e7c5..085a46c2 100644 --- a/src/core/operations/GOSTVerify.mjs +++ b/src/core/operations/GOSTVerify.mjs @@ -56,22 +56,19 @@ class GOSTVerify extends Operation { type: "argSelector", value: [ { - name: "GOST 28147 (Magma, 1989)", - off: [5], + name: "GOST 28147 (1989)", on: [6] }, + { + name: "GOST R 34.12 (Magma, 2015)", + off: [5] + }, { name: "GOST R 34.12 (Kuznyechik, 2015)", - on: [5], - off: [6] + off: [5] } ] }, - { - name: "Block length", - type: "option", - value: ["64", "128"] - }, { name: "sBox", type: "option", @@ -86,15 +83,31 @@ class GOSTVerify extends Operation { * @returns {string} */ async run(input, args) { - const [keyObj, ivObj, macObj, inputType, version, length, sBox] = args; + const [keyObj, ivObj, macObj, inputType, version, sBox] = args; const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option)); const iv = toHexFast(Utils.convertToByteArray(ivObj.string, ivObj.option)); const mac = toHexFast(Utils.convertToByteArray(macObj.string, macObj.option)); input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input)); - const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015; - const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10); + let blockLength, versionNum; + switch (version) { + case "GOST 28147 (1989)": + versionNum = 1989; + blockLength = 64; + break; + case "GOST R 34.12 (Magma, 2015)": + versionNum = 2015; + blockLength = 64; + break; + case "GOST R 34.12 (Kuznyechik, 2015)": + versionNum = 2015; + blockLength = 128; + break; + default: + throw new OperationError(`Unknown algorithm version: ${version}`); + } + const sBoxVal = versionNum === 1989 ? sBox : null; const algorithm = { diff --git a/tests/operations/tests/GOST.mjs b/tests/operations/tests/GOST.mjs index f332051b..bf1f7c19 100644 --- a/tests/operations/tests/GOST.mjs +++ b/tests/operations/tests/GOST.mjs @@ -14,7 +14,7 @@ import TestRegister from "../../lib/TestRegister.mjs"; TestRegister.addTests([ { - name: "GOST Encrypt: Magma", + name: "GOST Encrypt: 1989", input: "Hello, World!", expectedOutput: "f124ac5c0853870906dbaf9b56", recipeConfig: [ @@ -25,8 +25,7 @@ TestRegister.addTests([ { "option": "Hex", "string": "0011223344556677" }, "Raw", "Hex", - "GOST 28147 (Magma, 1989)", - "64", + "GOST 28147 (1989)", "E-SC", "OFB", "CP", @@ -48,7 +47,6 @@ TestRegister.addTests([ "Raw", "Hex", "GOST R 34.12 (Kuznyechik, 2015)", - "128", "E-SC", "CBC", "CP", @@ -58,7 +56,7 @@ TestRegister.addTests([ ], }, { - name: "GOST Decrypt: Magma", + name: "GOST Decrypt: 1989", input: "f124ac5c0853870906dbaf9b56", expectedOutput: "Hello, World!", recipeConfig: [ @@ -69,8 +67,7 @@ TestRegister.addTests([ { "option": "Hex", "string": "0011223344556677" }, "Hex", "Raw", - "GOST 28147 (Magma, 1989)", - "128", + "GOST 28147 (1989)", "E-SC", "OFB", "CP", @@ -92,7 +89,6 @@ TestRegister.addTests([ "Hex", "Raw", "GOST R 34.12 (Kuznyechik, 2015)", - "128", "E-TEST", "CBC", "CP", @@ -113,8 +109,7 @@ TestRegister.addTests([ { "option": "Hex", "string": "0011223344556677" }, "Raw", "Hex", - "GOST 28147 (Magma, 1989)", - "64", + "GOST 28147 (1989)", "E-C", 48 ] @@ -134,7 +129,6 @@ TestRegister.addTests([ { "option": "Hex", "string": "42b77fb3d6f6bf04" }, "Raw", "GOST R 34.12 (Kuznyechik, 2015)", - "128", "E-TEST" ] } @@ -152,8 +146,7 @@ TestRegister.addTests([ { "option": "Hex", "string": "0011223344556677" }, "Raw", "Hex", - "GOST R 34.12 (Kuznyechik, 2015)", - "64", + "GOST R 34.12 (Magma, 2015)", "E-TEST", "CP" ] @@ -172,8 +165,7 @@ TestRegister.addTests([ { "option": "Latin1", "string": "00112233" }, "Hex", "Raw", - "GOST 28147 (Magma, 1989)", - "64", + "GOST 28147 (1989)", "E-Z", "CP" ]