forked from calccrypto/OpenPGP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt.cpp
187 lines (154 loc) · 6.45 KB
/
decrypt.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "decrypt.h"
namespace OpenPGP {
namespace Decrypt {
Message data(const uint8_t sym,
const Message & message,
const std::string & session_key){
if (!message.meaningful()){
// "Error: Bad message.\n";
return Message();
}
// find start of encrypted data
PGP::Packets::size_type i = 0;
PGP::Packets packets = message.get_packets();
while ((i < packets.size()) && !Packet::is_sym_protected_data(packets[i] -> get_tag())){
i++;
}
if (i == packets.size()){
// "Error: No encrypted data found.\n";
return Message();
}
uint8_t tag = Packet::RESERVED;
std::string data = "";
// copy initial data to string
if (packets[i] -> get_tag() == Packet::SYMMETRICALLY_ENCRYPTED_DATA){
data = std::static_pointer_cast <Packet::Tag9> (packets[i]) -> get_encrypted_data();
tag = Packet::SYMMETRICALLY_ENCRYPTED_DATA;
}
else if (packets[i] -> get_tag() == Packet::SYM_ENCRYPTED_INTEGRITY_PROTECTED_DATA){
data = std::static_pointer_cast <Packet::Tag18> (packets[i]) -> get_protected_data();
tag = Packet::SYM_ENCRYPTED_INTEGRITY_PROTECTED_DATA;
}
if (!data.size()){
// "Error: No encrypted data packet(s) found.\n";
return Message();
}
// decrypt data
data = use_OpenPGP_CFB_decrypt(sym, tag, data, session_key);
// get blocksize of symmetric key algorithm
const unsigned int BS = Sym::BLOCK_LENGTH.at(sym) >> 3;
// strip extra data
if (tag == Packet::SYM_ENCRYPTED_INTEGRITY_PROTECTED_DATA){
const std::string checksum = data.substr(data.size() - 20, 20); // get given SHA1 checksum
data = data.substr(0, data.size() - 20); // remove SHA1 checksum
if (Hash::use(Hash::ID::SHA1, data) != checksum){ // check SHA1 checksum
// "Error: Given checksum and calculated checksum do not match.";
return Message();
}
data = data.substr(0, data.size() - 2); // get rid of \xd3\x14
}
data = data.substr(BS + 2, data.size() - BS - 2); // get rid of prefix
// decompress and parse decrypted data
return Message(data);
}
Message pka(const SecretKey & pri,
const std::string & passphrase,
const Message & message){
if (!pri.meaningful()){
// "Error: Bad private key.\n";
return Message();
}
if (!message.meaningful()){
// "Error: No encrypted message found.\n";
return Message();
}
// find Public-Key Encrypted Session Key Packet (Tag 1)
// should be first packet
Packet::Tag1::Ptr tag1 = nullptr;
for(Packet::Tag::Ptr const & p : message.get_packets()){
if (p -> get_tag() == Packet::PUBLIC_KEY_ENCRYPTED_SESSION_KEY){
tag1 = std::static_pointer_cast <Packet::Tag1> (p);
break;
}
}
if (!tag1){
// "Error: No " + Packet::NAME.at(Packet::PUBLIC_KEY_ENCRYPTED_SESSION_KEY) + " (Tag " + std::to_string(Packet::PUBLIC_KEY_ENCRYPTED_SESSION_KEY) + ") found.\n";
return Message();
}
if (!PKA::can_encrypt(tag1 -> get_pka())){
// "Error: Public Key Algorithm detected cannot be used to encrypt/decrypt.\n";
return Message();
}
// find corresponding secret key
Packet::Tag5::Ptr sec = nullptr;
for(Packet::Tag::Ptr const & p : pri.get_packets()){
sec = nullptr;
if (Packet::is_secret(p -> get_tag())){
sec = std::static_pointer_cast <Packet::Tag5> (p);
// encrypted packet Key ID has to match decrypting Key ID, not main Key ID
if (sec -> get_public_ptr() -> get_keyid() != tag1 -> get_keyid()){
continue;
}
// make sure key has encrypting keys
if (PKA::can_encrypt(sec -> get_pka())){
break;
}
}
}
if (!sec){
// "Error: Correct Private Key not found.\n";
return Message();
}
// decrypt secret keys
std::string symkey;
if ((tag1 -> get_pka() == PKA::ID::RSA_ENCRYPT_OR_SIGN) ||
(tag1 -> get_pka() == PKA::ID::RSA_ENCRYPT_ONLY)){
symkey = mpitoraw(PKA::RSA::decrypt(tag1 -> get_mpi()[0], sec -> decrypt_secret_keys(passphrase), sec -> get_mpi()));
}
else if (tag1 -> get_pka() == PKA::ID::ELGAMAL){
symkey = PKA::ElGamal::decrypt(tag1 -> get_mpi(), sec -> decrypt_secret_keys(passphrase), sec -> get_mpi());
}
// get symmetric algorithm, session key, 2 octet checksum wrapped in EME_PKCS1_ENCODE
symkey = zero + symkey;
if (!(symkey = EME_PKCS1v1_5_DECODE(symkey)).size()){ // remove EME_PKCS1 encoding
// "Error: EME_PKCS1v1_5_DECODE failure.\n";
return Message();
}
const uint8_t sym = symkey[0]; // get symmetric algorithm
const std::string checksum = symkey.substr(symkey.size() - 2, 2); // get 2 octet checksum
symkey = symkey.substr(1, symkey.size() - 3); // remove both from session key
uint16_t sum = 0;
for(char & c : symkey){ // calculate session key checksum
sum += static_cast <uint8_t> (c);
}
if (unhexlify(makehex(sum, 4)) != checksum){ // check session key checksums
// "Error: Calculated session key checksum does not match given checksum.\n";
return Message();
}
// decrypt the data with the extracted key
return data(sym, message, symkey);
}
Message sym(const Message & message,
const std::string & passphrase){
if (!message.meaningful()){
// "Error: Bad message.\n";
return Message();
}
// find Symmetric Key Encrypted Session Key (Tag 3)
// should be first packet
Packet::Tag3::Ptr tag3 = nullptr;
for(Packet::Tag::Ptr const & p : message.get_packets()){
if (p -> get_tag() == Packet::SYMMETRIC_KEY_ENCRYPTED_SESSION_KEY){
tag3 = std::static_pointer_cast <Packet::Tag3> (p);
break;
}
}
if (!tag3){
// "Error: No " + Packet::NAME.at(Packet::SYMMETRIC_KEY_ENCRYPTED_SESSION_KEY) + " (Tag " + std::to_string(Packet::SYMMETRIC_KEY_ENCRYPTED_SESSION_KEY) + ") found.\n";
return Message();
}
const std::string symkey = tag3 -> get_session_key(passphrase);
return data(symkey[0], message, symkey.substr(1, symkey.size() - 1));
}
}
}