-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycrypto.hpp
97 lines (75 loc) · 3.08 KB
/
mycrypto.hpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <string>
using std::string;
#ifndef __MYCRYPTO__
#define __MYCRYPTO__
typedef unsigned char BYTE;
typedef unsigned short WORD;
class ByteBlock {
BYTE * pBlocks;
size_t amount_of_bytes;
public:
// Construct block of bytes which contsists of
// size_ blocks each of them with init_value in it
ByteBlock(size_t size_ = 0, BYTE init_value = 0);
// Construct block with size_ first bytes of pBlocks_
// The value will be copied, source stays untouchable
ByteBlock(BYTE * pBlocks_, size_t size_);
// Move constructor
// Copy constructor thus implicitly deleted
// Object to move turn to null
ByteBlock(ByteBlock && rhs);
// Destructor, yeah
~ByteBlock();
// Move assigment operator
// Object to move turn to null
void operator = (ByteBlock && rhs);
// This cast may be convenient to use the ByteBlock
// in functions which takes raw pointers as argument
BYTE * byte_ptr();
const BYTE * byte_ptr() const;
// Indexing operator with evident functionality
BYTE & operator [] (size_t index);
BYTE operator [] (size_t index) const;
bool operator == (const ByteBlock & lhs) const;
bool operator != (const ByteBlock & lhs) const;
// Replace body of the current block with pBlocks_
// Old value will be zerod, and then, deleted
// New value copied into the block,
// source stays untouchable
void reset(const BYTE * pBlocks_, size_t size_);
// Return amount of bytes in block
size_t size() const;
// It'll return deep copy of the block, which
// points to different place in memory
ByteBlock deep_copy() const;
// It'll return slice of current ByteBlock
ByteBlock operator () (size_t begin, size_t length) const;
// Changes values between two ByteBlock-s
friend void swap(ByteBlock & lhs, ByteBlock & rhs);
};
// Some functions which will be useful for implementation of encryption algorithms
std::vector<ByteBlock> split_blocks(const ByteBlock & src, size_t length);
ByteBlock join_blocks(const std::vector<ByteBlock> & blocks);
void xor_blocks(ByteBlock & to_assign, const ByteBlock & lhs, const ByteBlock & rhs);
// Some I/O functions to work with hex representation of ByteBlock
string hex_representation(const ByteBlock & bb);
ByteBlock hex_to_bytes(const string & s);
// Template class that provides implementation of Cipher Feadback mode
// of operation with any block cipher (algorithm) which saticfy several
// requirement. It must have got:
// copy constructor, methods encrypt and decrypt with the same interface
// and public member-data block_lenght
template <typename CipherType>
class CFB_Mode {
const CipherType algorithm;
const ByteBlock iv;
void decrypt_with_iv(const ByteBlock & src, ByteBlock & dst, const ByteBlock & iv_) const;
public:
CFB_Mode(const CipherType & alg, const ByteBlock & init_vec);
void encrypt(const ByteBlock & src, ByteBlock & dst) const;
void decrypt(const ByteBlock & src, ByteBlock & dst) const;
void parallel_decrypt(const ByteBlock & src, ByteBlock & dst) const;
};
// Implementations of modes of encryption
#include "modes.hpp"
#endif