Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Apr 7, 2024
1 parent 1f45bf1 commit 02dc780
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 292 deletions.
7 changes: 0 additions & 7 deletions src/cryptojs_sm3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const rs = require('jsrsasign')
const KJUR = rs.KJUR
const C = rs.CryptoJS
const CLib = C.lib
const WordArray = CLib.WordArray
Expand Down Expand Up @@ -188,10 +187,4 @@ C.SM3 = Hasher._createHelper(SM3)
*/
C.HmacSM3 = Hasher._createHmacHelper(SM3)

KJUR.crypto.Util.DEFAULTPROVIDER.sm3 = 'cryptojs'
KJUR.crypto.Util.CRYPTOJSMESSAGEDIGESTNAME.sm3 = SM3

rs.asn1.x509.OID.name2oidList.sm3 = '1.2.156.10197.1.401.1'
rs.asn1.x509.OID.name2oidList.hmacWithSM3 = '1.2.156.10197.1.401.2'

module.exports = SM3
120 changes: 0 additions & 120 deletions src/cryptojs_sm4.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const rs = require('jsrsasign')
const KJUR = rs.KJUR
const C = rs.CryptoJS
const CAlgo = C.algo
const CLib = C.lib
Expand Down Expand Up @@ -217,123 +216,4 @@ C.pad.NoPadding = {
}
}

function aryval (val, keys, def) {
if (typeof val !== 'object') return undefined
const keyArr = String(keys).split('.')
for (let i = 0; i < keyArr.length && val; i++) {
let key = keyArr[i]
if (key.match(/^[0-9]+$/)) key = parseInt(key)
val = val[key]
}
return val || val === false ? val : def
}

/**
* encrypt raw string by specified key and algorithm<br/>
* @name encrypt
* @memberOf KJUR.crypto.Cipher
* @function
* @param {string} s input string to encrypt
* @param {string} hexadecimal string of symmetric cipher key
* @param {string} algName short/long algorithm name for encryption/decryption (OPTION)
* @param {object} param parameters for synchronous cipher such as initial vector (OPTION)
* @return {string} hexadecimal encrypted string
* @since jsrsasign 6.2.0 crypto 1.1.10
*
* @description
* This static method encrypts raw string with specified key and algorithm.
* <br/>
* NOTE: From jsrsasign 10.9.0, asymmetric cipher ({des-EDE3,sm4-CBC,aes{128,256}}-CBC) is also supported.
* NOTE2: From jsrsasign 11.0.0, RSA and RSAOAEP encryption/decryption support is removed
* because of Marvin attack vulnerability.
*
* @example
* KJUR.crypto.Cipher.encrypt("12abcd...", "5a7d...", "aes256-CBC", { iv: "1b3c..." })
* KJUR.crypto.Cipher.encrypt("12abcd...", "5a7d...", any, { encalg: "aes128-CBC", iv: "1b3c..." })
* KJUR.crypto.Cipher.encrypt("12abcd...", any, any, { encalg: "des-EDE3-CBC", iv: "1b3c...", key: "3d41..." })
* KJUR.crypto.Cipher.encrypt("12abcd...", "5a7d...", "sm4-CBC", { iv: "1b3c..." })
* KJUR.crypto.Cipher.encrypt(any, any, any, { encalg: "des-EDE3-CBC", iv: "1b3c...", key: "3d41...", enc: "12abcd..." })
*/
KJUR.crypto.Cipher.encrypt = function (s, keyObj, algName, param) {
if (aryval(param, 'enclag') !== undefined) algName = param.encalg

if (typeof algName === 'string' && algName.substr(-4) === '-CBC') {
let hKey = keyObj
const hPlain = s
if (aryval(param, 'key') !== undefined) hKey = param.key
// if (aryval(param, 'enc') !== undefined) hEnc = param.enc
const wKey = C.enc.Hex.parse(hKey)
const wPlain = C.enc.Hex.parse(hPlain)
const wIV = C.enc.Hex.parse(param.iv)
let wEnc
if (algName === 'des-EDE3-CBC') {
wEnc = C.TripleDES.encrypt(wPlain, wKey, { iv: wIV })
} else if (algName === 'aes128-CBC' || algName === 'aes256-CBC') {
wEnc = C.AES.encrypt(wPlain, wKey, { iv: wIV })
} else if (algName === 'sm4-CBC') {
wEnc = C.SM4.encrypt(wPlain, wKey, { iv: wIV })
} else {
throw new Error('unsupported algorithm: ' + algName)
}
return wEnc + ''
} else {
throw new Error('Cipher.encrypt: unsupported key or algorithm')
}
}

/**
* decrypt encrypted hexadecimal string with specified key and algorithm<br/>
* @name decrypt
* @memberOf KJUR.crypto.Cipher
* @function
* @param {string} hex hexadecimal string of encrypted message
* @param {object} hexadecimal string of symmetric cipher key
* @param {string} algName short/long algorithm name for encryption/decryption (OPTION)
* @param {object} param parameters for synchronous cipher such as initial vector (OPTION)
* @return {string} decrypted raw string
* @since jsrsasign 6.2.0 crypto 1.1.10
*
* @description
* This static method decrypts encrypted hexadecimal string with specified key and algorithm.
* <br/>
* NOTE: From jsrsasign 10.9.0, asymmetric cipher ({des-EDE3,sm4-CBC,aes{128,256}}-CBC) is also supported.
* NOTE2: From jsrsasign 11.0.0, RSA and RSAOAEP encryption/decryption support is removed
* because of Marvin attack vulnerability.
*
* @example
* KJUR.crypto.Cipher.decrypt("12abcd...", "5a7d...", "aes256-CBC", { iv: "1b3c..." })
* KJUR.crypto.Cipher.decrypt("12abcd...", "5a7d...", any, { encalg: "aes128-CBC", iv: "1b3c..." })
* KJUR.crypto.Cipher.decrypt("12abcd...", any, any, { encalg: "des-EDE3-CBC", iv: "1b3c...", key: "3d41..." })
* KJUR.crypto.Cipher.decrypt("12abcd...", "5a7d...", "sm4-CBC", { iv: "1b3c..." })
* KJUR.crypto.Cipher.decrypt(any, any, any, { encalg: "des-EDE3-CBC", iv: "1b3c...", key: "3d41...", enc: "12abcd..." })
*/
KJUR.crypto.Cipher.decrypt = function (hex, keyObj, algName, param) {
if (aryval(param, 'enclag') !== undefined) algName = param.encalg

if (typeof algName === 'string' && algName.substr(-4) === '-CBC') {
let hKey = keyObj
const hEnc = hex
if (aryval(param, 'key') !== undefined) hKey = param.key
// if (aryval(param, 'enc') !== undefined) hEnc = param.enc
const wKey = C.enc.Hex.parse(hKey)
const wEnc = C.enc.Hex.parse(hEnc)
const wIV = C.enc.Hex.parse(param.iv)
let wDec
if (algName === 'des-EDE3-CBC') {
wDec = C.TripleDES.decrypt({ ciphertext: wEnc }, wKey, { iv: wIV })
} else if (algName === 'aes128-CBC' || algName === 'aes256-CBC') {
wDec = C.AES.decrypt({ ciphertext: wEnc }, wKey, { iv: wIV })
} else if (algName === 'sm4-CBC') {
wDec = C.SM4.decrypt({ ciphertext: wEnc }, wKey, { iv: wIV })
} else {
throw new Error('unsupported algorithm: ' + algName)
}
return C.enc.Hex.stringify(wDec)
} else {
throw new Error('Cipher.decrypt: unsupported key or algorithm')
}
}

rs.asn1.x509.OID.name2oidList['sm4-CBC'] = '1.2.156.10197.1.104.2'

module.exports = SM4
86 changes: 0 additions & 86 deletions src/cryptojs_sm4_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require('./cryptojs_sm4')
const test = require('tape')
const rs = require('jsrsasign')
const KJUR = rs.KJUR
const C = rs.CryptoJS

test('test sample 1', (t) => {
Expand Down Expand Up @@ -61,88 +60,3 @@ test('test sm4-cbc', (t) => {
}
t.end()
})

test('test sm4-cbc with default mode', (t) => {
const cases = [
{
key: '30313233343536373839414243444546',
iv: '30313233343536373839414243444546',
plaintext: '48656c6c6f20576f726c64',
ciphertext: '0a67062f0cd2dce26a7b978ebf2134f9'
},
{
key: '30313233343536373839414243444546',
iv: '30313233343536373839414243444546',
plaintext:
'48656c6c6f20576f726c642048656c6c6f20576f726c642048656c6c6f20576f726c642048656c6c6f20576f726c6464',
ciphertext:
'd31e3683e4fc9b516a2c0f983676a9eb1fdcc32af38408978157a2065de34c6a068d0fef4e2bfab4bcaba66441fde0fe92c164eca170247572de1202952ec727'
},
{
key: '0123456789abcdeffedcba9876543210',
iv: '00000000000000000000000000000000',
plaintext: '0123456789abcdeffedcba9876543210',
ciphertext:
'681edf34d206965e86b3e94f536e4246677d307e844d7aa24579d556490dc7aa'
}
]
for (const c of cases) {
const ciphertext = C.SM4.encrypt(
C.enc.Hex.parse(c.plaintext),
C.enc.Hex.parse(c.key),
{ iv: C.enc.Hex.parse(c.iv) }
).ciphertext.toString()

t.equal(ciphertext, c.ciphertext)
const plaintext = C.SM4.decrypt(
{ ciphertext: C.enc.Hex.parse(c.ciphertext) },
C.enc.Hex.parse(c.key),
{ iv: C.enc.Hex.parse(c.iv) }
)
t.equal(plaintext.toString(), c.plaintext)
}
t.end()
})

test('test sm4-cbc with KJUR.crypto.Cipher', (t) => {
const cases = [
{
key: '30313233343536373839414243444546',
iv: '30313233343536373839414243444546',
plaintext: '48656c6c6f20576f726c64',
ciphertext: '0a67062f0cd2dce26a7b978ebf2134f9'
},
{
key: '30313233343536373839414243444546',
iv: '30313233343536373839414243444546',
plaintext:
'48656c6c6f20576f726c642048656c6c6f20576f726c642048656c6c6f20576f726c642048656c6c6f20576f726c6464',
ciphertext:
'd31e3683e4fc9b516a2c0f983676a9eb1fdcc32af38408978157a2065de34c6a068d0fef4e2bfab4bcaba66441fde0fe92c164eca170247572de1202952ec727'
},
{
key: '0123456789abcdeffedcba9876543210',
iv: '00000000000000000000000000000000',
plaintext: '0123456789abcdeffedcba9876543210',
ciphertext:
'681edf34d206965e86b3e94f536e4246677d307e844d7aa24579d556490dc7aa'
}
]
for (const c of cases) {
const ciphertext = KJUR.crypto.Cipher.encrypt(
c.plaintext,
c.key,
'sm4-CBC',
{ iv: c.iv }
)
t.equal(ciphertext, c.ciphertext)
const plaintext = KJUR.crypto.Cipher.decrypt(
c.ciphertext,
c.key,
'sm4-CBC',
{ iv: c.iv }
)
t.equal(plaintext, c.plaintext)
}
t.end()
})
Loading

0 comments on commit 02dc780

Please sign in to comment.