2024-09-20 15:46:04 +02:00
|
|
|
/**
|
|
|
|
* @author flakjacket95 [dflack95@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2024
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2024-09-22 18:58:36 +02:00
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
2024-09-20 15:46:04 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
2024-09-20 19:17:00 +02:00
|
|
|
|
|
|
|
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";
|
2024-09-22 18:58:36 +02:00
|
|
|
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) {
|
2024-09-20 19:17:00 +02:00
|
|
|
const [publicKeyX, publicKeyY, outputFormat, curveName] = args;
|
2024-09-20 15:46:04 +02:00
|
|
|
this.outputFormat = outputFormat;
|
|
|
|
|
2024-09-22 18:58:36 +02:00
|
|
|
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);
|
2024-09-20 19:17:00 +02:00
|
|
|
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;
|