CyberChef/src/core/operations/SM2Encrypt.mjs

78 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-09-20 15:46:04 +02:00
/**
* @author flakjacket95 [dflack95@gmail.com]
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/
import OperationError from "../errors/OperationError.mjs";
2024-09-20 15:46:04 +02:00
import Operation from "../Operation.mjs";
import { SM2 } from "../lib/SM2.mjs";
2024-09-20 15:46:04 +02:00
/**
* SM2 Encrypt operation
*/
class SM2Encrypt extends Operation {
/**
* SM2Encrypt constructor
*/
constructor() {
super();
this.name = "SM2 Encrypt";
this.module = "Crypto";
2024-09-20 15:46:04 +02:00
this.description = "Encrypts a message utilizing the SM2 standard";
this.infoURL = ""; // Usually a Wikipedia link. Remember to remove localisation (i.e. https://wikipedia.org/etc rather than https://en.wikipedia.org/etc)
this.inputType = "ArrayBuffer";
this.outputType = "string";
this.args = [
{
name: "Public Key X",
type: "string",
value: "DEADBEEF"
},
{
name: "Public Key Y",
type: "string",
value: "DEADBEEF"
},
{
"name": "Output Format",
"type": "option",
2024-09-20 21:07:24 +02:00
"value": ["C1C3C2", "C1C2C3"],
"defaultIndex": 0
2024-09-20 15:46:04 +02:00
},
{
name: "Curve",
type: "option",
2024-09-20 21:07:24 +02:00
"value": ["sm2p256v1"],
"defaultIndex": 0
2024-09-20 15:46:04 +02:00
}
];
}
/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {byteArray}
*/
run(input, args) {
const [publicKeyX, publicKeyY, outputFormat, curveName] = args;
2024-09-20 15:46:04 +02:00
this.outputFormat = outputFormat;
if (publicKeyX.length !== 64 || publicKeyY.length !== 64) {
throw new OperationError("Invalid Public Key - Ensure each component is 32 bytes in size and in hex");
}
2024-09-21 12:00:37 +02:00
const sm2 = new SM2(curveName, outputFormat);
sm2.setPublicKey(publicKeyX, publicKeyY);
2024-09-20 15:46:04 +02:00
2024-09-21 12:00:37 +02:00
const result = sm2.encrypt(new Uint8Array(input));
return result;
2024-09-20 15:46:04 +02:00
}
}
export default SM2Encrypt;