forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsodium.cpp
More file actions
184 lines (147 loc) · 4.59 KB
/
sodium.cpp
File metadata and controls
184 lines (147 loc) · 4.59 KB
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
/**
* @file sodium.cpp
* @brief Crypto layer using libsodium.
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "mega.h"
namespace mega
{
const std::string EdDSA::TLV_KEY = "prEd255";
EdDSA::EdDSA(PrnGen &rng, unsigned char *keySeed)
{
initializationOK = false;
if (sodium_init() == -1)
{
LOG_err << "Cannot initialize sodium library.";
return;
}
if (keySeed) // then use the value
{
memcpy(this->keySeed, keySeed, EdDSA::SEED_KEY_LENGTH);
}
else // make the new key seed.
{
rng.genblock(this->keySeed, EdDSA::SEED_KEY_LENGTH);
}
// derive public and private keys from the seed
if (crypto_sign_seed_keypair(this->pubKey, this->privKey, this->keySeed) != 0)
{
LOG_err << "Error generating an Ed25519 key pair.";
}
initializationOK = true;
}
EdDSA::~EdDSA()
{
}
// Computes the signature of a message.
int EdDSA::sign(const unsigned char* msg, const unsigned long long msglen,
unsigned char* sig)
{
if (!sig || !msg)
{
return 0;
}
int result = crypto_sign_detached(sig, NULL, msg, msglen, (const unsigned char*)privKey);
return int( (result == 0) ? (crypto_sign_BYTES + msglen) : 0 );
}
// Verifies the signature of a message.
int EdDSA::verify(const unsigned char* msg, unsigned long long msglen,
const unsigned char* sig, const unsigned char* pubKey)
{
if (!sig || !msg)
{
return 0;
}
return !crypto_sign_verify_detached(sig, msg, msglen, pubKey);
}
void EdDSA::signKey(const unsigned char *key, const unsigned long long keyLength, string *result, uint64_t ts)
{
if (!ts)
{
ts = (uint64_t) m_time();
}
string tsstr;
unsigned char digit;
for (int i = 0; i < 8; i++)
{
digit = ts & 0xFF;
tsstr.insert(0, 1, digit);
ts >>= 8;
}
string keyString = "keyauth";
keyString.append(tsstr);
keyString.append((char*)key, size_t(keyLength));
byte sigBuf[crypto_sign_BYTES];
sign((unsigned char *)keyString.data(), keyString.size(), sigBuf);
result->resize(crypto_sign_BYTES + sizeof(ts)); // 8 --> timestamp prefix
result->assign(tsstr.data(), sizeof(ts));
result->append((const char*)sigBuf, crypto_sign_BYTES);
}
bool EdDSA::verifyKey(const unsigned char *pubk, const unsigned long long pubkLen, const string *sig, const unsigned char* signingPubKey)
{
if (sig->size() < 72)
{
return false;
}
uint64_t ts;
memcpy(&ts, sig->substr(0, 8).data(), sizeof(ts));
string message = "keyauth";
message.append(sig->data(), 8);
message.append((char*)pubk, size_t(pubkLen));
string signature = sig->substr(8);
return verify((unsigned char*) message.data(), message.length(),
(unsigned char*) signature.data(), signingPubKey);
}
const std::string ECDH::TLV_KEY= "prCu255";
ECDH::ECDH(unsigned char *privKey)
{
initializationOK = false;
if (sodium_init() == -1)
{
LOG_err << "Cannot initialize sodium library.";
return;
}
if (privKey) // then use the value
{
memcpy(this->privKey, privKey, PRIVATE_KEY_LENGTH);
// derive public key from privKey
crypto_scalarmult_base(this->pubKey, this->privKey);
}
else
{
// no private key specified: create a new key pair
crypto_box_keypair(this->pubKey, this->privKey);
}
initializationOK = true;
}
ECDH::~ECDH()
{
}
int ECDH::encrypt(unsigned char *encmsg, const unsigned char *msg,
const unsigned long long msglen, const unsigned char *nonce,
const unsigned char *pubKey, const unsigned char *privKey)
{
return !crypto_box(encmsg, msg, msglen, nonce, pubKey, privKey);
}
int ECDH::decrypt(unsigned char *msg, const unsigned char *encmsg,
const unsigned long long encmsglen, const unsigned char *nonce,
const unsigned char *pubKey, const unsigned char *privKey)
{
return !crypto_box_open(msg, encmsg, encmsglen, nonce, pubKey, privKey);
}
} // namespace