-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
crypto.h
82 lines (64 loc) · 2.47 KB
/
crypto.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once
#include <string>
#include <stdexcept>
#define CIPHER_CODE_MIN 0
#define CIPHER_CODE_NONE 0
#define CIPHER_CODE_BLOWFISH_128 1
#define CIPHER_CODE_BLOWFISH_448 2
#define CIPHER_CODE_AES_128 3
#define CIPHER_CODE_AES_256 4
#define CIPHER_CODE_AES_256_RANDOM_IV 5
#define CIPHER_CODE_MAX 5
inline bool isValidCipherCode(uint8_t code) {
return code >= CIPHER_CODE_MIN && code <= CIPHER_CODE_MAX;
}
class BadPaddingException : public std::runtime_error {
public:
BadPaddingException() : std::runtime_error("Bad padding") {
}
};
class Code42Cipher {
public:
virtual std::string decrypt(const std::string & cipherText, const std::string & key) const = 0;
};
class Code42NullCipher : public Code42Cipher {
std::string decrypt(const std::string & cipherText, const std::string & key) const override {
return cipherText;
}
};
class Code42Blowfish448 : public Code42Cipher {
public:
std::string decrypt(const std::string & cipherText, const std::string &key) const override;
};
class Code42Blowfish128 : public Code42Blowfish448 {
public:
std::string decrypt(const std::string & cipherText, const std::string &key) const override {
return Code42Blowfish448::decrypt(cipherText, key.substr(0, 128 / 8));
}
};
class Code42AESStaticIV : public Code42Cipher {
public:
std::string decrypt(const std::string & cipherText, const std::string &key) const override;
};
class Code42AES128 : public Code42AESStaticIV {
public:
std::string decrypt(const std::string & cipherText, const std::string &key) const override {
return Code42AESStaticIV::decrypt(cipherText, key.substr(0, 128 / 8));
}
};
class Code42AES256 : public Code42AESStaticIV {
public:
std::string decrypt(const std::string & cipherText, const std::string &key) const override {
return Code42AESStaticIV::decrypt(cipherText, key.substr(0, 256 / 8));
}
};
class Code42AES256RandomIV : public Code42Cipher {
public:
std::string decrypt(const std::string & cipherText, const std::string &key) const override;
};
// Use CIPHER_CODE_* as indexes:
extern const Code42Cipher* code42Ciphers[];
std::string deriveCustomArchiveKeyV2(const std::string &userID, const std::string &passphrase);
bool passwordUnlocksSecureDataKey(const std::string &decoded, const std::string &password);
std::string decryptSecureDataKey(const std::string &decoded, const std::string &password);
std::string generateSmallBusinessKeyV2(const std::string &passphrase, const std::string &salt);