Skip to content

Commit 86d5978

Browse files
committed
Improved GOST algorithm naming and block size selection
1 parent fb818c3 commit 86d5978

File tree

7 files changed

+157
-87
lines changed

7 files changed

+157
-87
lines changed

src/core/operations/GOSTDecrypt.mjs

+25-12
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,19 @@ class GOSTDecrypt extends Operation {
5555
type: "argSelector",
5656
value: [
5757
{
58-
name: "GOST 28147 (Magma, 1989)",
59-
off: [5],
58+
name: "GOST 28147 (1989)",
6059
on: [6]
6160
},
61+
{
62+
name: "GOST R 34.12 (Magma, 2015)",
63+
off: [5]
64+
},
6265
{
6366
name: "GOST R 34.12 (Kuznyechik, 2015)",
64-
on: [5],
65-
off: [6]
67+
off: [5]
6668
}
6769
]
6870
},
69-
{
70-
name: "Block length",
71-
type: "option",
72-
value: ["64", "128"]
73-
},
7471
{
7572
name: "sBox",
7673
type: "option",
@@ -100,14 +97,30 @@ class GOSTDecrypt extends Operation {
10097
* @returns {string}
10198
*/
10299
async run(input, args) {
103-
const [keyObj, ivObj, inputType, outputType, version, length, sBox, blockMode, keyMeshing, padding] = args;
100+
const [keyObj, ivObj, inputType, outputType, version, sBox, blockMode, keyMeshing, padding] = args;
104101

105102
const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option));
106103
const iv = toHexFast(Utils.convertToByteArray(ivObj.string, ivObj.option));
107104
input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input));
108105

109-
const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015;
110-
const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10);
106+
let blockLength, versionNum;
107+
switch (version) {
108+
case "GOST 28147 (1989)":
109+
versionNum = 1989;
110+
blockLength = 64;
111+
break;
112+
case "GOST R 34.12 (Magma, 2015)":
113+
versionNum = 2015;
114+
blockLength = 64;
115+
break;
116+
case "GOST R 34.12 (Kuznyechik, 2015)":
117+
versionNum = 2015;
118+
blockLength = 128;
119+
break;
120+
default:
121+
throw new OperationError(`Unknown algorithm version: ${version}`);
122+
}
123+
111124
const sBoxVal = versionNum === 1989 ? sBox : null;
112125

113126
const algorithm = {

src/core/operations/GOSTEncrypt.mjs

+25-12
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,19 @@ class GOSTEncrypt extends Operation {
5555
type: "argSelector",
5656
value: [
5757
{
58-
name: "GOST 28147 (Magma, 1989)",
59-
off: [5],
58+
name: "GOST 28147 (1989)",
6059
on: [6]
6160
},
61+
{
62+
name: "GOST R 34.12 (Magma, 2015)",
63+
off: [5]
64+
},
6265
{
6366
name: "GOST R 34.12 (Kuznyechik, 2015)",
64-
on: [5],
65-
off: [6]
67+
off: [5]
6668
}
6769
]
6870
},
69-
{
70-
name: "Block length",
71-
type: "option",
72-
value: ["64", "128"]
73-
},
7471
{
7572
name: "sBox",
7673
type: "option",
@@ -100,14 +97,30 @@ class GOSTEncrypt extends Operation {
10097
* @returns {string}
10198
*/
10299
async run(input, args) {
103-
const [keyObj, ivObj, inputType, outputType, version, length, sBox, blockMode, keyMeshing, padding] = args;
100+
const [keyObj, ivObj, inputType, outputType, version, sBox, blockMode, keyMeshing, padding] = args;
104101

105102
const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option));
106103
const iv = toHexFast(Utils.convertToByteArray(ivObj.string, ivObj.option));
107104
input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input));
108105

109-
const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015;
110-
const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10);
106+
let blockLength, versionNum;
107+
switch (version) {
108+
case "GOST 28147 (1989)":
109+
versionNum = 1989;
110+
blockLength = 64;
111+
break;
112+
case "GOST R 34.12 (Magma, 2015)":
113+
versionNum = 2015;
114+
blockLength = 64;
115+
break;
116+
case "GOST R 34.12 (Kuznyechik, 2015)":
117+
versionNum = 2015;
118+
blockLength = 128;
119+
break;
120+
default:
121+
throw new OperationError(`Unknown algorithm version: ${version}`);
122+
}
123+
111124
const sBoxVal = versionNum === 1989 ? sBox : null;
112125

113126
const algorithm = {

src/core/operations/GOSTKeyUnwrap.mjs

+25-12
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,19 @@ class GOSTKeyUnwrap extends Operation {
5555
type: "argSelector",
5656
value: [
5757
{
58-
name: "GOST 28147 (Magma, 1989)",
59-
off: [5],
58+
name: "GOST 28147 (1989)",
6059
on: [6]
6160
},
61+
{
62+
name: "GOST R 34.12 (Magma, 2015)",
63+
off: [5]
64+
},
6265
{
6366
name: "GOST R 34.12 (Kuznyechik, 2015)",
64-
on: [5],
65-
off: [6]
67+
off: [5]
6668
}
6769
]
6870
},
69-
{
70-
name: "Block length",
71-
type: "option",
72-
value: ["64", "128"]
73-
},
7471
{
7572
name: "sBox",
7673
type: "option",
@@ -90,14 +87,30 @@ class GOSTKeyUnwrap extends Operation {
9087
* @returns {string}
9188
*/
9289
async run(input, args) {
93-
const [keyObj, ukmObj, inputType, outputType, version, length, sBox, keyWrapping] = args;
90+
const [keyObj, ukmObj, inputType, outputType, version, sBox, keyWrapping] = args;
9491

9592
const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option));
9693
const ukm = toHexFast(Utils.convertToByteArray(ukmObj.string, ukmObj.option));
9794
input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input));
9895

99-
const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015;
100-
const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10);
96+
let blockLength, versionNum;
97+
switch (version) {
98+
case "GOST 28147 (1989)":
99+
versionNum = 1989;
100+
blockLength = 64;
101+
break;
102+
case "GOST R 34.12 (Magma, 2015)":
103+
versionNum = 2015;
104+
blockLength = 64;
105+
break;
106+
case "GOST R 34.12 (Kuznyechik, 2015)":
107+
versionNum = 2015;
108+
blockLength = 128;
109+
break;
110+
default:
111+
throw new OperationError(`Unknown algorithm version: ${version}`);
112+
}
113+
101114
const sBoxVal = versionNum === 1989 ? sBox : null;
102115

103116
const algorithm = {

src/core/operations/GOSTKeyWrap.mjs

+25-12
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,19 @@ class GOSTKeyWrap extends Operation {
5555
type: "argSelector",
5656
value: [
5757
{
58-
name: "GOST 28147 (Magma, 1989)",
59-
off: [5],
58+
name: "GOST 28147 (1989)",
6059
on: [6]
6160
},
61+
{
62+
name: "GOST R 34.12 (Magma, 2015)",
63+
off: [5]
64+
},
6265
{
6366
name: "GOST R 34.12 (Kuznyechik, 2015)",
64-
on: [5],
65-
off: [6]
67+
off: [5]
6668
}
6769
]
6870
},
69-
{
70-
name: "Block length",
71-
type: "option",
72-
value: ["64", "128"]
73-
},
7471
{
7572
name: "sBox",
7673
type: "option",
@@ -90,14 +87,30 @@ class GOSTKeyWrap extends Operation {
9087
* @returns {string}
9188
*/
9289
async run(input, args) {
93-
const [keyObj, ukmObj, inputType, outputType, version, length, sBox, keyWrapping] = args;
90+
const [keyObj, ukmObj, inputType, outputType, version, sBox, keyWrapping] = args;
9491

9592
const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option));
9693
const ukm = toHexFast(Utils.convertToByteArray(ukmObj.string, ukmObj.option));
9794
input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input));
9895

99-
const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015;
100-
const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10);
96+
let blockLength, versionNum;
97+
switch (version) {
98+
case "GOST 28147 (1989)":
99+
versionNum = 1989;
100+
blockLength = 64;
101+
break;
102+
case "GOST R 34.12 (Magma, 2015)":
103+
versionNum = 2015;
104+
blockLength = 64;
105+
break;
106+
case "GOST R 34.12 (Kuznyechik, 2015)":
107+
versionNum = 2015;
108+
blockLength = 128;
109+
break;
110+
default:
111+
throw new OperationError(`Unknown algorithm version: ${version}`);
112+
}
113+
101114
const sBoxVal = versionNum === 1989 ? sBox : null;
102115

103116
const algorithm = {

src/core/operations/GOSTSign.mjs

+25-12
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,19 @@ class GOSTSign extends Operation {
5555
type: "argSelector",
5656
value: [
5757
{
58-
name: "GOST 28147 (Magma, 1989)",
59-
off: [5],
58+
name: "GOST 28147 (1989)",
6059
on: [6]
6160
},
61+
{
62+
name: "GOST R 34.12 (Magma, 2015)",
63+
off: [5]
64+
},
6265
{
6366
name: "GOST R 34.12 (Kuznyechik, 2015)",
64-
on: [5],
65-
off: [6]
67+
off: [5]
6668
}
6769
]
6870
},
69-
{
70-
name: "Block length",
71-
type: "option",
72-
value: ["64", "128"]
73-
},
7471
{
7572
name: "sBox",
7673
type: "option",
@@ -93,14 +90,30 @@ class GOSTSign extends Operation {
9390
* @returns {string}
9491
*/
9592
async run(input, args) {
96-
const [keyObj, ivObj, inputType, outputType, version, length, sBox, macLength] = args;
93+
const [keyObj, ivObj, inputType, outputType, version, sBox, macLength] = args;
9794

9895
const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option));
9996
const iv = toHexFast(Utils.convertToByteArray(ivObj.string, ivObj.option));
10097
input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input));
10198

102-
const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015;
103-
const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10);
99+
let blockLength, versionNum;
100+
switch (version) {
101+
case "GOST 28147 (1989)":
102+
versionNum = 1989;
103+
blockLength = 64;
104+
break;
105+
case "GOST R 34.12 (Magma, 2015)":
106+
versionNum = 2015;
107+
blockLength = 64;
108+
break;
109+
case "GOST R 34.12 (Kuznyechik, 2015)":
110+
versionNum = 2015;
111+
blockLength = 128;
112+
break;
113+
default:
114+
throw new OperationError(`Unknown algorithm version: ${version}`);
115+
}
116+
104117
const sBoxVal = versionNum === 1989 ? sBox : null;
105118

106119
const algorithm = {

src/core/operations/GOSTVerify.mjs

+25-12
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,19 @@ class GOSTVerify extends Operation {
5656
type: "argSelector",
5757
value: [
5858
{
59-
name: "GOST 28147 (Magma, 1989)",
60-
off: [5],
59+
name: "GOST 28147 (1989)",
6160
on: [6]
6261
},
62+
{
63+
name: "GOST R 34.12 (Magma, 2015)",
64+
off: [5]
65+
},
6366
{
6467
name: "GOST R 34.12 (Kuznyechik, 2015)",
65-
on: [5],
66-
off: [6]
68+
off: [5]
6769
}
6870
]
6971
},
70-
{
71-
name: "Block length",
72-
type: "option",
73-
value: ["64", "128"]
74-
},
7572
{
7673
name: "sBox",
7774
type: "option",
@@ -86,15 +83,31 @@ class GOSTVerify extends Operation {
8683
* @returns {string}
8784
*/
8885
async run(input, args) {
89-
const [keyObj, ivObj, macObj, inputType, version, length, sBox] = args;
86+
const [keyObj, ivObj, macObj, inputType, version, sBox] = args;
9087

9188
const key = toHexFast(Utils.convertToByteArray(keyObj.string, keyObj.option));
9289
const iv = toHexFast(Utils.convertToByteArray(ivObj.string, ivObj.option));
9390
const mac = toHexFast(Utils.convertToByteArray(macObj.string, macObj.option));
9491
input = inputType === "Hex" ? input : toHexFast(Utils.strToArrayBuffer(input));
9592

96-
const versionNum = version === "GOST 28147 (Magma, 1989)" ? 1989 : 2015;
97-
const blockLength = versionNum === 1989 ? 64 : parseInt(length, 10);
93+
let blockLength, versionNum;
94+
switch (version) {
95+
case "GOST 28147 (1989)":
96+
versionNum = 1989;
97+
blockLength = 64;
98+
break;
99+
case "GOST R 34.12 (Magma, 2015)":
100+
versionNum = 2015;
101+
blockLength = 64;
102+
break;
103+
case "GOST R 34.12 (Kuznyechik, 2015)":
104+
versionNum = 2015;
105+
blockLength = 128;
106+
break;
107+
default:
108+
throw new OperationError(`Unknown algorithm version: ${version}`);
109+
}
110+
98111
const sBoxVal = versionNum === 1989 ? sBox : null;
99112

100113
const algorithm = {

0 commit comments

Comments
 (0)