#ifndef __LowMC_h__ #define __LowMC_h__ // Vendored from https://github.com/LowMC/lowmc // commit e847fb160ad8ca1f373efd91a55b6d67f7deb425 // Local changes: namespace lowmc, and the parameters below. // LowMCv3 instance for a 128-bit block and key with 10 S-boxes, at data // complexity 2^128: `determine_rounds.py 128 10 128 128` reports 32 rounds. #include #include #include namespace lowmc { const unsigned numofboxes = 10; // Number of Sboxes const unsigned blocksize = 128; // Block size in bits const unsigned keysize = 128; // Key size in bits const unsigned rounds = 32; // Number of rounds const unsigned identitysize = blocksize - 3*numofboxes; // Size of the identity part in the Sbox layer typedef std::bitset block; // Store messages and states typedef std::bitset keyblock; class LowMC { public: LowMC (keyblock k = 0) { key = k; instantiate_LowMC(); keyschedule(); }; block encrypt (const block message); block decrypt (const block message); void set_key (keyblock k); void print_matrices(); private: // LowMC private data members // // The Sbox and its inverse const std::vector Sbox = {0x00, 0x01, 0x03, 0x06, 0x07, 0x04, 0x05, 0x02}; const std::vector invSbox = {0x00, 0x01, 0x07, 0x02, 0x05, 0x06, 0x03, 0x04}; std::vector> LinMatrices; // Stores the binary matrices for each round std::vector> invLinMatrices; // Stores the inverses of LinMatrices std::vector roundconstants; // Stores the round constants keyblock key = 0; //Stores the master key std::vector> KeyMatrices; // Stores the matrices that generate the round keys std::vector roundkeys; // Stores the round keys // LowMC private functions // block Substitution (const block message); // The substitution layer block invSubstitution (const block message); // The inverse substitution layer block MultiplyWithGF2Matrix (const std::vector matrix, const block message); // For the linear layer block MultiplyWithGF2Matrix_Key (const std::vector matrix, const keyblock k); // For generating the round keys void keyschedule (); //Creates the round keys from the master key void instantiate_LowMC (); //Fills the matrices and roundconstants with pseudorandom bits // Binary matrix functions // unsigned rank_of_Matrix (const std::vector matrix); unsigned rank_of_Matrix_Key (const std::vector matrix); std::vector invert_Matrix (const std::vector matrix); // Random bits functions // block getrandblock (); keyblock getrandkeyblock (); bool getrandbit (); }; } // namespace lowmc #endif