UNPKG

@li0ard/magma

Version:

Magma cipher implementation in pure TypeScript

33 lines (32 loc) 1.49 kB
import { BLOCK_SIZE, Magma, sboxes } from "../"; import { cfb_encrypt, cfb_decrypt } from "@li0ard/gost3413"; /** * Encrypts data using Cipher Feedback (CFB) mode with Magma cipher * * @param key Encryption key * @param data Data to be encrypted * @param iv Initialization vector * @param legacy Enable backward compatibility with old GOST 28147-89 * @param sbox Optional substitution box, defaults to `ID_TC26_GOST_28147_PARAM_Z` * @returns {Uint8Array} */ export const encryptCFB = (key, data, iv, legacy = false, sbox = sboxes.ID_TC26_GOST_28147_PARAM_Z) => { const cipher = new Magma(legacy ? Magma.reverseKey(key) : key, sbox); const encrypter = (buf) => (legacy ? cipher.encryptLegacy(buf) : cipher.encryptBlock(buf)); return cfb_encrypt(encrypter, BLOCK_SIZE, data, iv); }; /** * Decrypts data using Cipher Feedback (CFB) mode with Magma cipher * * @param key Encryption key * @param data Data to be decrypted * @param iv Initialization vector * @param legacy Enable backward compatibility with old GOST 28147-89 * @param sbox Optional substitution box, defaults to `ID_TC26_GOST_28147_PARAM_Z` * @returns {Uint8Array} */ export const decryptCFB = (key, data, iv, legacy = false, sbox = sboxes.ID_TC26_GOST_28147_PARAM_Z) => { const cipher = new Magma(legacy ? Magma.reverseKey(key) : key, sbox); const decrypter = (buf) => (legacy ? cipher.encryptLegacy(buf) : cipher.encryptBlock(buf)); return cfb_decrypt(decrypter, BLOCK_SIZE, data, iv); };