From 57c1a03c4f224eb3a6b3a05baeeedb7c1252b5ef Mon Sep 17 00:00:00 2001 From: n1073645 Date: Fri, 24 Apr 2020 14:04:13 +0100 Subject: [PATCH] Option structures added for hashing algorithms --- src/core/config/Categories.json | 1 + src/core/operations/HAS160.mjs | 12 +++++-- src/core/operations/MD2.mjs | 12 +++++-- src/core/operations/SHA0.mjs | 12 +++++-- src/core/operations/SHA1.mjs | 12 +++++-- src/core/operations/SHA2.mjs | 9 ++++-- src/core/operations/SM3.mjs | 56 +++++++++++++++++++++++++++++++++ 7 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 src/core/operations/SM3.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 77e3d319..d8373c2f 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -313,6 +313,7 @@ "SHA1", "SHA2", "SHA3", + "SM3", "Keccak", "Shake", "RIPEMD", diff --git a/src/core/operations/HAS160.mjs b/src/core/operations/HAS160.mjs index aa1439ae..0ca62019 100644 --- a/src/core/operations/HAS160.mjs +++ b/src/core/operations/HAS160.mjs @@ -20,11 +20,17 @@ class HAS160 extends Operation { this.name = "HAS-160"; this.module = "Crypto"; - this.description = "HAS-160 is a cryptographic hash function designed for use with the Korean KCDSA digital signature algorithm. It is derived from SHA-1, with assorted changes intended to increase its security. It produces a 160-bit output.

HAS-160 is used in the same way as SHA-1. First it divides input in blocks of 512 bits each and pads the final block. A digest function updates the intermediate hash value by processing the input blocks in turn.

The message digest algorithm consists of 80 rounds."; + this.description = "HAS-160 is a cryptographic hash function designed for use with the Korean KCDSA digital signature algorithm. It is derived from SHA-1, with assorted changes intended to increase its security. It produces a 160-bit output.

HAS-160 is used in the same way as SHA-1. First it divides input in blocks of 512 bits each and pads the final block. A digest function updates the intermediate hash value by processing the input blocks in turn.

The message digest algorithm consists, by default, of 80 rounds."; this.infoURL = "https://wikipedia.org/wiki/HAS-160"; this.inputType = "ArrayBuffer"; this.outputType = "string"; - this.args = []; + this.args = [ + { + name: "Rounds", + type: "number", + value: 80 + } + ]; } /** @@ -33,7 +39,7 @@ class HAS160 extends Operation { * @returns {string} */ run(input, args) { - return runHash("has160", input); + return runHash("has160", input, {rounds: args[0]}); } } diff --git a/src/core/operations/MD2.mjs b/src/core/operations/MD2.mjs index ecfa699c..9fca4939 100644 --- a/src/core/operations/MD2.mjs +++ b/src/core/operations/MD2.mjs @@ -20,11 +20,17 @@ class MD2 extends Operation { this.name = "MD2"; this.module = "Crypto"; - this.description = "The MD2 (Message-Digest 2) algorithm is a cryptographic hash function developed by Ronald Rivest in 1989. The algorithm is optimized for 8-bit computers.

Although MD2 is no longer considered secure, even as of 2014, it remains in use in public key infrastructures as part of certificates generated with MD2 and RSA."; + this.description = "The MD2 (Message-Digest 2) algorithm is a cryptographic hash function developed by Ronald Rivest in 1989. The algorithm is optimized for 8-bit computers.

Although MD2 is no longer considered secure, even as of 2014, it remains in use in public key infrastructures as part of certificates generated with MD2 and RSA. The message digest algorithm consists, by default, of 18 rounds."; this.infoURL = "https://wikipedia.org/wiki/MD2_(cryptography)"; this.inputType = "ArrayBuffer"; this.outputType = "string"; - this.args = []; + this.args = [ + { + name: "Rounds", + type: "number", + value: 18 + } + ]; } /** @@ -33,7 +39,7 @@ class MD2 extends Operation { * @returns {string} */ run(input, args) { - return runHash("md2", input); + return runHash("md2", input, {rounds: args[0]}); } } diff --git a/src/core/operations/SHA0.mjs b/src/core/operations/SHA0.mjs index 93345eb2..34c4d1df 100644 --- a/src/core/operations/SHA0.mjs +++ b/src/core/operations/SHA0.mjs @@ -20,11 +20,17 @@ class SHA0 extends Operation { this.name = "SHA0"; this.module = "Crypto"; - this.description = "SHA-0 is a retronym applied to the original version of the 160-bit hash function published in 1993 under the name 'SHA'. It was withdrawn shortly after publication due to an undisclosed 'significant flaw' and replaced by the slightly revised version SHA-1."; + this.description = "SHA-0 is a retronym applied to the original version of the 160-bit hash function published in 1993 under the name 'SHA'. It was withdrawn shortly after publication due to an undisclosed 'significant flaw' and replaced by the slightly revised version SHA-1. The message digest algorithm consists, by default, of 80 rounds."; this.infoURL = "https://wikipedia.org/wiki/SHA-1#SHA-0"; this.inputType = "ArrayBuffer"; this.outputType = "string"; - this.args = []; + this.args = [ + { + name: "Rounds", + type: "number", + value: 80 + } + ]; } /** @@ -33,7 +39,7 @@ class SHA0 extends Operation { * @returns {string} */ run(input, args) { - return runHash("sha0", input); + return runHash("sha0", input, {rounds: args[0]}); } } diff --git a/src/core/operations/SHA1.mjs b/src/core/operations/SHA1.mjs index 41a0105a..ee744d56 100644 --- a/src/core/operations/SHA1.mjs +++ b/src/core/operations/SHA1.mjs @@ -20,11 +20,17 @@ class SHA1 extends Operation { this.name = "SHA1"; this.module = "Crypto"; - this.description = "The SHA (Secure Hash Algorithm) hash functions were designed by the NSA. SHA-1 is the most established of the existing SHA hash functions and it is used in a variety of security applications and protocols.

However, SHA-1's collision resistance has been weakening as new attacks are discovered or improved."; + this.description = "The SHA (Secure Hash Algorithm) hash functions were designed by the NSA. SHA-1 is the most established of the existing SHA hash functions and it is used in a variety of security applications and protocols.

However, SHA-1's collision resistance has been weakening as new attacks are discovered or improved. The message digest algorithm consists, by default, of 80 rounds."; this.infoURL = "https://wikipedia.org/wiki/SHA-1"; this.inputType = "ArrayBuffer"; this.outputType = "string"; - this.args = []; + this.args = [ + { + name: "Rounds", + type: "number", + value: 80 + } + ]; } /** @@ -33,7 +39,7 @@ class SHA1 extends Operation { * @returns {string} */ run(input, args) { - return runHash("sha1", input); + return runHash("sha1", input, {rounds: args[0]}); } } diff --git a/src/core/operations/SHA2.mjs b/src/core/operations/SHA2.mjs index c9599d24..3d015d26 100644 --- a/src/core/operations/SHA2.mjs +++ b/src/core/operations/SHA2.mjs @@ -20,7 +20,7 @@ class SHA2 extends Operation { this.name = "SHA2"; this.module = "Crypto"; - this.description = "The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2 includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224, SHA256, SHA384, SHA512.

"; + this.description = "The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2 includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224, SHA256, SHA384, SHA512.

The message digest algorithm consists, by default, of 64 rounds."; this.infoURL = "https://wikipedia.org/wiki/SHA-2"; this.inputType = "ArrayBuffer"; this.outputType = "string"; @@ -29,6 +29,11 @@ class SHA2 extends Operation { "name": "Size", "type": "option", "value": ["512", "256", "384", "224", "512/256", "512/224"] + }, + { + name: "Rounds", + type: "number", + value: 64 } ]; } @@ -40,7 +45,7 @@ class SHA2 extends Operation { */ run(input, args) { const size = args[0]; - return runHash("sha" + size, input); + return runHash("sha" + size, input, {rounds: args[1]}); } } diff --git a/src/core/operations/SM3.mjs b/src/core/operations/SM3.mjs new file mode 100644 index 00000000..c2c1fa5d --- /dev/null +++ b/src/core/operations/SM3.mjs @@ -0,0 +1,56 @@ +/** + * @author n1073645 [n1073645@gmail.com] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import Utils from "../Utils.mjs"; +import Sm3 from "crypto-api/src/hasher/sm3"; +import {toHex} from "crypto-api/src/encoder/hex"; + +/** + * SM3 operation + */ +class SM3 extends Operation { + + /** + * SM3 constructor + */ + constructor() { + super(); + + this.name = "SM3"; + this.module = "Crypto"; + this.description = "SM3 is a cryptographic hash function used in the Chinese National Standard. SM3 is mainly used in digital signatures, message authentication codes, and pseudorandom number generators. The message digest algorithm consists, by default, of 64 rounds and length of 256."; + this.infoURL = ""; + this.inputType = "ArrayBuffer"; + this.outputType = "string"; + this.args = [ + { + name: "Length", + type: "number", + value: 256 + }, + { + name: "Rounds", + type: "number", + value: 64 + } + ]; + } + + /** + * @param {ArrayBuffer} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const msg = Utils.arrayBufferToStr(input, false); + const hasher = new Sm3({length: args[0], rounds: args[1]}); + hasher.update(msg); + return toHex(hasher.finalize()); + } +} + +export default SM3;