Repository navigation
Expand file tree
/
Copy pathencrypt.cc
More file actions
408 lines (317 loc) · 12.6 KB
/
Copy pathencrypt.cc
File metadata and controls
408 lines (317 loc) · 12.6 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Copyright (c) 2009-2011, Adrian Thurston <thurston@complang.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "encrypt.h"
#include "packet.h"
#include "dsnp.h"
#include "error.h"
#include <openssl/rand.h>
#include <openssl/objects.h>
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <openssl/sha.h>
#include <openssl/err.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <arpa/inet.h>
Allocated signEncrypt( Keys *pubEncVer, Keys *privDecSign, const String *iduri, const String &msg )
{
/* Need to make a buffer containing both the session key and message so we
* our signature is valid only using this encryption key. */
String nBin = bnToBin( pubEncVer->rsa->n );
String eBin = bnToBin( pubEncVer->rsa->e );
String signData = concatenate( 3, &nBin, &eBin, &msg );
/* Sign the message. */
u_char msgSha1[SHA_DIGEST_LENGTH];
SHA1( signData.binary(), signData.length, msgSha1 );
String sigBin( RSA_size(privDecSign->rsa) );
unsigned sigLen;
int signRes = RSA_sign( NID_sha1, msgSha1, SHA_DIGEST_LENGTH,
sigBin.binary(), &sigLen, privDecSign->rsa );
if ( signRes != 1 )
throw RsaSignFailed( ERR_get_error() );
/* Generate a session key just for this message. */
unsigned char keyBin[SK_SIZE];
RAND_bytes( keyBin, SK_SIZE );
/* Encrypt the session key. */
String protKey( RSA_size(pubEncVer->rsa) );
int protKeyLen = RSA_public_encrypt( SK_SIZE, keyBin, protKey.binary(),
pubEncVer->rsa, RSA_PKCS1_PADDING );
if ( protKeyLen < 0 )
throw RsaPublicEncryptFailed( ERR_get_error() );
/*
* Concatenate the signature and the message so we can encrypt it.
*/
/* Make the concatenated plain text. FIXME: this is a bit of an abuse. The
* sig does not apply to msg alone. */
String plainData = iduri == 0 ?
consSigned( sigBin, msg ) :
consSignedId( *iduri, sigBin, msg );
/* Encrypt using the session key. */
String enc( plainData.length );
RC4_KEY rc4Key;
RC4_set_key( &rc4Key, SK_SIZE, keyBin );
RC4( &rc4Key, plainData.length, plainData.binary(), enc.binary() );
/* Result. */
String encPacket = consSignedEncrypted( protKey, enc );
return encPacket.relinquish();
}
Allocated decryptVerify( Keys *pubEncVer, Keys *privDecSign, const String &msg )
{
PacketSignedEncrypted epp1( msg );
/* Decrypt the key. */
String sessionKey( RSA_size( privDecSign->rsa ) );
long skLen = RSA_private_decrypt( epp1.protKey.length, epp1.protKey.binary(),
sessionKey.binary(), privDecSign->rsa, RSA_PKCS1_PADDING );
if ( skLen < 0 )
throw RsaPrivateDecryptFailed( ERR_get_error() );
String dec( epp1.enc.length );
RC4_KEY rc4Key;
RC4_set_key( &rc4Key, skLen, sessionKey.binary() );
RC4( &rc4Key, epp1.enc.length, epp1.enc.binary(), dec.binary() );
PacketSigned epp2( dec );
String nBin = bnToBin( privDecSign->rsa->n );
String eBin = bnToBin( privDecSign->rsa->e );
String verifyData = concatenate( 3, &nBin, &eBin, &epp2.msg );
/* Verify the item. */
u_char decryptedSha1[SHA_DIGEST_LENGTH];
SHA1( verifyData.binary(), verifyData.length, decryptedSha1 );
int verifyres = RSA_verify( NID_sha1, decryptedSha1, SHA_DIGEST_LENGTH,
epp2.sig.binary(), epp2.sig.length, pubEncVer->rsa );
if ( verifyres != 1 )
throw RsaVerifyFailed( ERR_get_error() );
return epp2.msg.relinquish();
}
Allocated decryptVerify1( Keys *privDecSign, const String &msg )
{
PacketSignedEncrypted epp1( msg );
/* Decrypt the key. */
String sessionKey( RSA_size( privDecSign->rsa ) );
long skLen = RSA_private_decrypt( epp1.protKey.length, epp1.protKey.binary(),
sessionKey.binary(), privDecSign->rsa, RSA_PKCS1_PADDING );
if ( skLen < 0 )
throw RsaPrivateDecryptFailed( ERR_get_error() );
String dec( epp1.enc.length );
RC4_KEY rc4Key;
RC4_set_key( &rc4Key, skLen, sessionKey.binary() );
RC4( &rc4Key, epp1.enc.length, epp1.enc.binary(), dec.binary() );
return dec.relinquish();
}
Allocated decryptVerify2( Keys *pubEncVer, Keys *privDecSign, const String &dec )
{
PacketSignedId signedId( dec );
String nBin = bnToBin( privDecSign->rsa->n );
String eBin = bnToBin( privDecSign->rsa->e );
String verifyData = concatenate( 3, &nBin, &eBin, &signedId.msg );
/* Verify the item. */
u_char decryptedSha1[SHA_DIGEST_LENGTH];
SHA1( verifyData.binary(), verifyData.length, decryptedSha1 );
int verifyres = RSA_verify( NID_sha1, decryptedSha1, SHA_DIGEST_LENGTH,
signedId.sig.binary(), signedId.sig.length, pubEncVer->rsa );
if ( verifyres != 1 )
throw RsaVerifyFailed( ERR_get_error() );
return signedId.msg.relinquish();
}
Allocated bkEncrypt( const String &bk, const String &msg )
{
/* Convert the broadcst_key to binary. */
String broadcastKey = base64ToBin( bk );
/* Encrypt the message using the session key. */
String encrypted( msg.length );
RC4_KEY rc4Key;
RC4_set_key( &rc4Key, SK_SIZE, broadcastKey.binary() );
RC4( &rc4Key, msg.length, msg.binary(), encrypted.binary() );
return consBkEncrypted( encrypted );
}
Allocated bkDecrypt( const String &bk, const String &msg )
{
PacketBkEncrypted epp1( msg );
/* Convert the broadcst_key to binary. */
String broadcastKey = base64ToBin( bk );
/* Decrypt. */
String dec( epp1.enc.length );
RC4_KEY rc4Key;
RC4_set_key( &rc4Key, broadcastKey.length, broadcastKey.binary() );
RC4( &rc4Key, epp1.enc.length, epp1.enc.binary(), dec.binary() );
return dec.relinquish();
}
Allocated bkSignEncrypt( Keys *privDecSign, const String &bk, const String &msg )
{
/* Convert the broadcst_key to binary. */
String broadcastKey = base64ToBin( bk );
/* Need to make a buffer containing both the broadcast key and message so
* we can sign both. */
String signData = concatenate( 2, &broadcastKey, &msg );
/* Sign the message. */
u_char msgSha1[SHA_DIGEST_LENGTH];
SHA1( signData.binary(), signData.length, msgSha1 );
String sigBin( RSA_size(privDecSign->rsa) );
unsigned sigLen;
int signRes = RSA_sign( NID_sha1, msgSha1, SHA_DIGEST_LENGTH,
sigBin.binary(), &sigLen, privDecSign->rsa );
if ( signRes != 1 )
throw RsaSignFailed( ERR_get_error() ) ;
/* Make a single buffer containing the plain data. */
String plainData = consSigned( sigBin, msg );
/* Encrypt the message using the session key. */
String encrypted( plainData.length );
RC4_KEY rc4Key;
RC4_set_key( &rc4Key, SK_SIZE, broadcastKey.binary() );
RC4( &rc4Key, plainData.length, plainData.binary(), encrypted.binary() );
return consBkSignedEncrypted( encrypted );
}
Allocated bkDecryptVerify( Keys *pubEncVer, const String &bk, const String &msg )
{
PacketBkSignedEncrypted epp1( msg );
/* Convert the broadcst_key to binary. */
String broadcastKey = base64ToBin( bk );
/* Decrypt. */
String dec( epp1.enc.length );
RC4_KEY rc4Key;
RC4_set_key( &rc4Key, broadcastKey.length, broadcastKey.binary() );
RC4( &rc4Key, epp1.enc.length, epp1.enc.binary(), dec.binary() );
PacketSigned epp2( dec );
/* Need to make a buffer containing both the session key an message so we
* can verify the message was originally signed with this key. */
String verifyData = concatenate( 2, &broadcastKey, &epp2.msg );
/* Verify the message. */
u_char decryptedSha1[SHA_DIGEST_LENGTH];
SHA1( verifyData.binary(), verifyData.length, decryptedSha1 );
int verifyres = RSA_verify( NID_sha1, decryptedSha1,
SHA_DIGEST_LENGTH, epp2.sig.binary(), epp2.sig.length, pubEncVer->rsa );
if ( verifyres != 1 )
throw RsaVerifyFailed( ERR_get_error() );
return epp2.msg.relinquish();
}
Allocated bkSign( Keys *privDecSign, const String &bk, const String &msg )
{
/* Convert the broadcst_key to binary. */
String broadcastKey = base64ToBin( bk );
/* Need to make a buffer containing both the broadcast key and message so
* we can sign both. */
String signData = concatenate( 2, &broadcastKey, &msg );
/* Sign the message. */
u_char msgSha1[SHA_DIGEST_LENGTH];
SHA1( signData.binary(), signData.length, msgSha1 );
String sigBin( RSA_size(privDecSign->rsa) );
unsigned sigLen;
int signRes = RSA_sign( NID_sha1, msgSha1, SHA_DIGEST_LENGTH,
sigBin.binary(), &sigLen, privDecSign->rsa );
if ( signRes != 1 )
throw RsaSignFailed( ERR_get_error() ) ;
/* Make a single buffer containing the plain data. */
return consSigned( sigBin, msg );
}
Allocated bkVerify( Keys *pubEncVer, const String &bk, const String &msg )
{
/* Convert the broadcst_key to binary. */
String broadcastKey = base64ToBin( bk );
PacketSigned epp( msg );
/* Need to make a buffer containing both the session key an message so we
* can verify the message was originally signed with this key. */
String verifyData = concatenate( 2, &broadcastKey, &epp.msg );
/* Verify the message. */
u_char decryptedSha1[SHA_DIGEST_LENGTH];
SHA1( verifyData.binary(), verifyData.length, decryptedSha1 );
int verifyres = RSA_verify( NID_sha1, decryptedSha1,
SHA_DIGEST_LENGTH, epp.sig.binary(), epp.sig.length, pubEncVer->rsa );
if ( verifyres != 1 )
throw RsaVerifyFailed( ERR_get_error() );
return epp.msg.relinquish();
}
Allocated bkDetachedSign( Keys *privDecSign, const String &bk, const String &msg )
{
/* Convert the broadcst_key to binary. */
String broadcastKey = base64ToBin( bk );
/* Need to make a buffer containing both the broadcast key and message so
* we can sign both. */
String signData = concatenate( 2, &broadcastKey, &msg );
/* Sign the message. */
u_char msgSha1[SHA_DIGEST_LENGTH];
SHA1( signData.binary(), signData.length, msgSha1 );
String sigBin( RSA_size(privDecSign->rsa) );
unsigned sigLen;
int signRes = RSA_sign( NID_sha1, msgSha1, SHA_DIGEST_LENGTH,
sigBin.binary(), &sigLen, privDecSign->rsa );
if ( signRes != 1 )
throw RsaSignFailed( ERR_get_error() ) ;
/* Make a single buffer containing the plain data. */
return consDetachedSig( sigBin );
}
bool bkDetachedVerify( Keys *pubEncVer, const String &bk, const String &sig, const String &plainMsg )
{
PacketDetachedSig epp( sig );
/* Convert the broadcst_key to binary. */
String broadcastKey = base64ToBin( bk );
/* Need to make a buffer containing both the session key an message so we
* can verify the message was originally signed with this key. */
String verifyData = concatenate( 2, &broadcastKey, &plainMsg );
/* Verify the message. */
u_char decryptedSha1[SHA_DIGEST_LENGTH];
SHA1( verifyData.binary(), verifyData.length, decryptedSha1 );
int verifyres = RSA_verify( NID_sha1, decryptedSha1,
SHA_DIGEST_LENGTH, epp.sig.binary(), epp.sig.length, pubEncVer->rsa );
if ( verifyres != 1 )
throw RsaVerifyFailed( ERR_get_error() );
return true;
}
Allocated pwEncrypt( const String &pass, const String &msg )
{
u_char pwSha1[SHA_DIGEST_LENGTH];
SHA1( pass.binary(), pass.length, pwSha1 );
String encryptedBin( msg.length );
RC4_KEY rc4Key;
RC4_set_key( &rc4Key, SHA_DIGEST_LENGTH, pwSha1 );
RC4( &rc4Key, msg.length, msg.binary(), encryptedBin.binary() );
return consPwEncrypted( encryptedBin );
}
Allocated pwDecrypt( const String &pass, const String &msg )
{
PacketPwEncrypted epp( msg );
u_char pwSha1[SHA_DIGEST_LENGTH];
SHA1( pass.binary(), pass.length, pwSha1 );
String plain( epp.enc.length );
RC4_KEY rc4Key;
RC4_set_key( &rc4Key, SHA_DIGEST_LENGTH, pwSha1 );
RC4( &rc4Key, epp.enc.length, epp.enc.binary(), plain.binary() );
return plain.relinquish();
}
Allocated sign( Keys *privSign, const String &msg )
{
/* Sign the message. */
u_char msgSha1[SHA_DIGEST_LENGTH];
SHA1( msg.binary(), msg.length, msgSha1 );
String sigBin( RSA_size(privSign->rsa) );
unsigned sigLen;
int signRes = RSA_sign( NID_sha1, msgSha1, SHA_DIGEST_LENGTH,
sigBin.binary(), &sigLen, privSign->rsa );
if ( signRes != 1 )
throw RsaSignFailed( ERR_get_error() );
/* Result. */
return consSigned( sigBin, msg );
}
Allocated verify( Keys *pubVer, const String &msg )
{
PacketSigned epp( msg );
/* Verify the item. */
u_char msgSha1[SHA_DIGEST_LENGTH];
SHA1( epp.msg.binary(), epp.msg.length, msgSha1 );
int verifyres = RSA_verify( NID_sha1, msgSha1, SHA_DIGEST_LENGTH,
epp.sig.binary(), epp.sig.length, pubVer->rsa );
if ( verifyres != 1 )
throw RsaVerifyFailed( ERR_get_error() );
return epp.msg.relinquish();
}