@li0ard/magma
Version:
Magma cipher implementation in pure TypeScript
29 lines (28 loc) • 1.2 kB
JavaScript
import { BLOCK_SIZE, Magma } from "../";
import { MGM } from "@li0ard/gost3413";
/**
* Encrypts data using the Multilinear Galois Mode (MGM) with Magma cipher.
* @param key Encryption key
* @param data Data to be encrypted and authenticated
* @param iv Initialization vector
* @param additionalData Additional data to be authenticated
*/
export const encryptMGM = (key, data, iv, additionalData = new Uint8Array()) => {
const cipher = new Magma(key);
const encrypter = (buf) => cipher.encryptBlock(buf);
let mgm = new MGM(encrypter, BLOCK_SIZE);
return mgm.seal(iv.slice(), data.slice(), additionalData.slice());
};
/**
* Decrypts data using the Multilinear Galois Mode (MGM) with Magma cipher.
* @param key Encryption key
* @param data Data to be decrypted and authenticated
* @param iv Initialization vector
* @param additionalData Additional data to be authenticated
*/
export const decryptMGM = (key, data, iv, additionalData = new Uint8Array()) => {
const cipher = new Magma(key);
const encrypter = (buf) => cipher.encryptBlock(buf);
let mgm = new MGM(encrypter, BLOCK_SIZE);
return mgm.open(iv.slice(), data.slice(), additionalData.slice());
};