-
Notifications
You must be signed in to change notification settings - Fork 0
/
trivium.h
66 lines (53 loc) · 2.32 KB
/
trivium.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Author: Konstantin Yakovlev A.
// Date: 08/29/2024
#include <iostream>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
class Trivium{
public:
// struct that holds result of de/encryption
// contains: data, key and IV used
struct EncryptionResult {
std::bitset<80> key;
std::bitset<80> iv;
std::string encodedText;
};
// generate 80 bit key/IV
static std::bitset<80> generateKeyIV();
// encrypt with no key and IV
static EncryptionResult encrypt(const std::string& input);
// encript with custom key and no IV
static EncryptionResult encrypt(const std::string& input, const std::bitset<80>& KEY);
// encript with custom key and IV
static EncryptionResult encrypt(const std::string& input, const std::bitset<80>& KEY, const std::bitset<80>& IV);
// Decrypt encoded data
// requires same Key and IV that was used during encryption
static EncryptionResult decrypt(const std::string& input, const std::bitset<80>& KEY, const std::bitset<80>& IV);
private:
// execute system specific command for number generation
static std::string exec(const char* command);
// initialize registers
static void initPhase(std::vector<bool>& A, std::vector<bool>& B, std::vector<bool>& C, const std::bitset<80>& KEY, const std::bitset<80>& IV);
// Warm up cipher without recording output
static void warmUpCipher(std::vector<bool>& A, std::vector<bool>& B, std::vector<bool>& C);
// record output from cipher
// calculate output of each register to use as input next round
// shift register by 1
// returns output
static bool clock(std::vector<bool>& A, std::vector<bool>& B, std::vector<bool>& C);
// convert binary number in string format to boolean vector format
// modifies passed vector
static void stringToBits(std::string input, std::vector<bool>& X);
// convert a bitset to string
// creates a new bitset
// returns bitset
static std::bitset<80> stringToBits(std::string input);
// convert boolean vector to string format
// returns string
static std::string bitsToString(std::vector<bool>& bits);
// en/decrypts data by calling clock function on each character in X vector
// stores result in Y vector
static void encode(std::vector<bool>& X, std::vector<bool>& Y, std::vector<bool>& A, std::vector<bool>& B, std::vector<bool>& C);
};