This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
/
crypto_drv.c
1706 lines (1557 loc) · 50.4 KB
/
crypto_drv.c
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1999-2009. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
/*
* Purpose: Dynamically loadable driver for cryptography libraries.
* Based on OpenSSL.
*/
#ifdef __WIN32__
#include <windows.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "erl_driver.h"
#define OPENSSL_THREAD_DEFINES
#include "openssl/opensslconf.h"
#ifndef OPENSSL_THREADS
# ifdef __GNUC__
# warning No thread support by openssl. Driver will use coarse grain locking.
# endif
#endif
#include "openssl/crypto.h"
#include "openssl/des.h"
/* #include <openssl/idea.h> This is not supported on the openssl OTP requires */
#include "openssl/dsa.h"
#include "openssl/rsa.h"
#include "openssl/aes.h"
#include "openssl/md5.h"
#include "openssl/md4.h"
#include "openssl/sha.h"
#include "openssl/bn.h"
#include "openssl/objects.h"
#include "openssl/rc4.h"
#include "openssl/rc2.h"
#include "openssl/blowfish.h"
#include "openssl/rand.h"
#ifdef VALGRIND
# include <valgrind/memcheck.h>
/* libcrypto mixes supplied buffer contents into its entropy pool,
which makes valgrind complain about the use of uninitialized data.
We use this valgrind "request" to make sure that no such seemingly
undefined data escapes the driver.
*/
# define ERL_VALGRIND_MAKE_MEM_DEFINED(ptr,size) \
VALGRIND_MAKE_MEM_DEFINED(ptr,size)
# define ERL_VALGRIND_ASSERT_MEM_DEFINED(ptr,size) \
((void) ((VALGRIND_CHECK_MEM_IS_DEFINED(ptr,size) == 0) ? 1 : \
(fprintf(stderr,"\r\n####### VALGRIND_ASSSERT(%p,%d) failed at %s:%d\r\n",\
(ptr),(size), __FILE__, __LINE__), abort(), 0)))
#else
# define ERL_VALGRIND_MAKE_MEM_DEFINED(ptr,size)
# define ERL_VALGRIND_ASSERT_MEM_DEFINED(ptr,size)
#endif
#ifdef DEBUG
# define ASSERT(e) \
((void) ((e) ? 1 : (fprintf(stderr,"Assert '%s' failed at %s:%d\n",\
#e, __FILE__, __LINE__), abort(), 0)))
#else
# define ASSERT(e) ((void) 1)
#endif
#ifdef __GNUC__
# define INLINE __inline__
#elif defined(__WIN32__)
# define INLINE __forceinline
#else
# define INLINE
#endif
#define get_int32(s) ((((unsigned char*) (s))[0] << 24) | \
(((unsigned char*) (s))[1] << 16) | \
(((unsigned char*) (s))[2] << 8) | \
(((unsigned char*) (s))[3]))
#define put_int32(s,i) \
{ (s)[0] = (char)(((i) >> 24) & 0xff);\
(s)[1] = (char)(((i) >> 16) & 0xff);\
(s)[2] = (char)(((i) >> 8) & 0xff);\
(s)[3] = (char)((i) & 0xff);\
}
/* Driver interface declarations */
int crypto_init();
void crypto_finish();
ErlDrvData crypto_start(ErlDrvPort port, char *command);
void crypto_stop(ErlDrvData drv_data);
int crypto_control(ErlDrvData drv_data, unsigned int command, char *buf,
int len, char **rbuf, int rlen);
/* openssl callbacks */
#ifdef OPENSSL_THREADS
void locking_function(int mode, int n, const char *file, int line);
unsigned long id_function(void);
struct CRYPTO_dynlock_value* dyn_create_function(const char *file,
int line);
void dyn_lock_function(int mode, struct CRYPTO_dynlock_value* ptr,
const char *file, int line);
void dyn_destroy_function(struct CRYPTO_dynlock_value *ptr,
const char *file, int line);
#endif /* OPENSSL_THREADS */
/* helpers */
void hmac_md5(char *key, int klen, char *dbuf, int dlen,
char *hmacbuf);
void hmac_sha1(char *key, int klen, char *dbuf, int dlen,
char *hmacbuf);
ErlDrvEntry crypto_driver_entry = {
crypto_init,
crypto_start,
crypto_stop,
NULL, /* output */
NULL, /* ready_input */
NULL, /* ready_output */
"crypto_drv",
crypto_finish,
NULL, /* handle */
crypto_control,
NULL, /* timeout */
NULL, /* outputv */
NULL, /* ready_async */
NULL, /* flush */
NULL, /* call */
NULL, /* event */
ERL_DRV_EXTENDED_MARKER,
ERL_DRV_EXTENDED_MAJOR_VERSION,
ERL_DRV_EXTENDED_MINOR_VERSION,
#ifdef OPENSSL_THREADS
ERL_DRV_FLAG_USE_PORT_LOCKING,
#else
0,
#endif
NULL, /* handle2 */
NULL /* process_exit */
};
/* Keep the following definitions in alignment with the FUNC_LIST
* in crypto.erl.
*/
#define DRV_INFO 0
#define DRV_MD5 1
#define DRV_MD5_INIT 2
#define DRV_MD5_UPDATE 3
#define DRV_MD5_FINAL 4
#define DRV_SHA 5
#define DRV_SHA_INIT 6
#define DRV_SHA_UPDATE 7
#define DRV_SHA_FINAL 8
#define DRV_MD5_MAC 9
#define DRV_MD5_MAC_96 10
#define DRV_SHA_MAC 11
#define DRV_SHA_MAC_96 12
#define DRV_CBC_DES_ENCRYPT 13
#define DRV_CBC_DES_DECRYPT 14
#define DRV_EDE3_CBC_DES_ENCRYPT 15
#define DRV_EDE3_CBC_DES_DECRYPT 16
#define DRV_AES_CFB_128_ENCRYPT 17
#define DRV_AES_CFB_128_DECRYPT 18
#define DRV_RAND_BYTES 19
#define DRV_RAND_UNIFORM 20
#define DRV_MOD_EXP 21
#define DRV_DSS_VERIFY 22
#define DRV_RSA_VERIFY_SHA 23
/* #define DRV_RSA_VERIFY_MD5 35 */
#define DRV_CBC_AES128_ENCRYPT 24
#define DRV_CBC_AES128_DECRYPT 25
#define DRV_XOR 26
#define DRV_RC4_ENCRYPT 27 /* no decrypt needed; symmetric */
#define DRV_RC4_SETKEY 28
#define DRV_RC4_ENCRYPT_WITH_STATE 29
#define DRV_CBC_RC2_40_ENCRYPT 30
#define DRV_CBC_RC2_40_DECRYPT 31
#define DRV_CBC_AES256_ENCRYPT 32
#define DRV_CBC_AES256_DECRYPT 33
#define DRV_INFO_LIB 34
/* #define DRV_RSA_VERIFY_SHA 23 */
#define DRV_RSA_VERIFY_MD5 35
#define DRV_RSA_SIGN_SHA 36
#define DRV_RSA_SIGN_MD5 37
#define DRV_DSS_SIGN 38
#define DRV_RSA_PUBLIC_ENCRYPT 39
#define DRV_RSA_PRIVATE_DECRYPT 40
#define DRV_RSA_PRIVATE_ENCRYPT 41
#define DRV_RSA_PUBLIC_DECRYPT 42
#define DRV_DH_GENERATE_PARAMS 43
#define DRV_DH_CHECK 44
#define DRV_DH_GENERATE_KEY 45
#define DRV_DH_COMPUTE_KEY 46
#define DRV_MD4 47
#define DRV_MD4_INIT 48
#define DRV_MD4_UPDATE 49
#define DRV_MD4_FINAL 50
#define SSL_VERSION_0_9_8 0
#if SSL_VERSION_0_9_8
#define DRV_SHA256 51
#define DRV_SHA256_INIT 52
#define DRV_SHA256_UPDATE 53
#define DRV_SHA256_FINAL 54
#define DRV_SHA512 55
#define DRV_SHA512_INIT 56
#define DRV_SHA512_UPDATE 57
#define DRV_SHA512_FINAL 58
#endif
#define DRV_BF_CFB64_ENCRYPT 59
#define DRV_BF_CFB64_DECRYPT 60
/* #define DRV_CBC_IDEA_ENCRYPT 34 */
/* #define DRV_CBC_IDEA_DECRYPT 35 */
/* Not DRV_DH_GENERATE_PARAMS DRV_DH_CHECK
* Calc RSA_VERIFY_* and RSA_SIGN once */
#define NUM_CRYPTO_FUNCS 46
#define MD5_CTX_LEN (sizeof(MD5_CTX))
#define MD5_LEN 16
#define MD5_LEN_96 12
#define MD4_CTX_LEN (sizeof(MD4_CTX))
#define MD4_LEN 16
#define SHA_CTX_LEN (sizeof(SHA_CTX))
#define SHA_LEN 20
#define SHA_LEN_96 12
#define HMAC_INT_LEN 64
#define HMAC_IPAD 0x36
#define HMAC_OPAD 0x5c
#if SSL_VERSION_0_9_8
#define SHA256_CTX_LEN (sizeof(SHA256_CTX))
#define SHA256_LEN 32
#define SHA512_CTX_LEN (sizeof(SHA512_CTX))
#define SHA512_LEN 64
#endif
///STATIC NOW-- removed dyn init code
ErlDrvRWLock** lock_vec = NULL; /* Static locks used by openssl */
/* DRIVER INTERFACE */
int crypto_init(void)
{
ErlDrvSysInfo sys_info;
int i;
CRYPTO_set_mem_functions(driver_alloc, driver_realloc, driver_free);
#ifdef OPENSSL_THREADS
driver_system_info(&sys_info, sizeof(sys_info));
if(sys_info.scheduler_threads > 1) {
lock_vec = driver_alloc(CRYPTO_num_locks()*sizeof(*lock_vec));
if (lock_vec==NULL) return -1;
memset(lock_vec,0,CRYPTO_num_locks()*sizeof(*lock_vec));
for(i=CRYPTO_num_locks()-1; i>=0; --i) {
lock_vec[i] = erl_drv_rwlock_create("crypto_drv_stat");
if (lock_vec[i]==NULL) return -1;
}
CRYPTO_set_locking_callback(locking_function);
CRYPTO_set_id_callback(id_function);
CRYPTO_set_dynlock_create_callback(dyn_create_function);
CRYPTO_set_dynlock_lock_callback(dyn_lock_function);
CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function);
}
/* else no need for locks */
#endif /* OPENSSL_THREADS */
return 0;
}
void crypto_finish(void)
{
/* Moved here from crypto_control() as it's not thread safe */
CRYPTO_cleanup_all_ex_data();
if(lock_vec != NULL) {
int i;
for(i=CRYPTO_num_locks()-1; i>=0; --i) {
if (lock_vec[i] != NULL) {
erl_drv_rwlock_destroy(lock_vec[i]);
}
}
driver_free(lock_vec);
}
}
ErlDrvData crypto_start(ErlDrvPort port, char *command)
{
set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);
return 0; /* not used */
}
void crypto_stop(ErlDrvData drv_data)
{
return;
}
/* Helper functions for 'crypto_control'
*/
unsigned char* return_binary(char **rbuf, int rlen, int len)
{
if (len <= rlen) {
return (unsigned char *) *rbuf;
}
else {
ErlDrvBinary* bin;
*rbuf = (char*) (bin = driver_alloc_binary(len));
return (bin==NULL) ? NULL : (unsigned char *) bin->orig_bytes;
}
}
unsigned char* return_binary_shrink(char **rbuf, int rlen, unsigned char* data, int len)
{
if ((char *) data == *rbuf) { /* default buffer */
ASSERT(len <= rlen);
return (unsigned char *) data;
}
else {
ErlDrvBinary* bin = (ErlDrvBinary*) *rbuf;
*rbuf = (char*) (bin=driver_realloc_binary(bin, len));
return (bin==NULL) ? NULL : (unsigned char *) bin->orig_bytes;
}
}
/* Nowadays (R13) it does matter what value control returns
* as it may return data in default buffer.
*/
int crypto_control(ErlDrvData drv_data, unsigned int command, char *buf,
int len, char **rbuf, int rlen)
{
int klen, dlen, macsize, from_len, to_len, i;
int base_len, exponent_len, modulo_len;
int data_len, dsa_p_len, dsa_q_len;
int dsa_s_len, dsa_g_len, dsa_y_len;
int rsa_e_len, rsa_n_len, rsa_d_len, padding;
int or_mask;
int prime_len, generator;
int privkey_len, pubkey_len, dh_p_len, dh_g_len;
unsigned int rsa_s_len, j;
char *key, *key2, *dbuf;
unsigned char *p;
const_DES_cblock *des_key, *des_key2, *des_key3;
const unsigned char *des_dbuf;
BIGNUM *bn_from, *bn_to, *bn_rand, *bn_result;
BIGNUM *bn_base, *bn_exponent, *bn_modulo;
BIGNUM *dsa_p, *dsa_q, *dsa_g, *dsa_y;
BIGNUM *rsa_n, *rsa_e, *rsa_d;
BIGNUM *dh_p, *dh_g, *privkey, *pubkey;
DES_cblock *des_ivec;
unsigned char* bin;
DES_key_schedule schedule, schedule2, schedule3;
DH *dh_params;
/* IDEA_KEY_SCHEDULE idea, idea2; */
char hmacbuf[SHA_DIGEST_LENGTH];
unsigned char *rsa_s, *dsa_s;
/* char hmacbuf[SHA_LEN]; */
#if SSL_VERSION_0_9_8
SHA256_CTX sha256_ctx;
SHA512_CTX sha512_ctx;
#endif
MD5_CTX md5_ctx;
MD4_CTX md4_ctx;
SHA_CTX sha_ctx;
int new_ivlen = 0;
BN_CTX *bn_ctx;
DSA *dsa;
RSA *rsa;
AES_KEY aes_key;
RC4_KEY rc4_key;
RC2_KEY rc2_key;
switch(command) {
case DRV_INFO:
bin = return_binary(rbuf,rlen,NUM_CRYPTO_FUNCS);
if (bin==NULL) return -1;
for (i = 0; i < NUM_CRYPTO_FUNCS; i++) {
bin[i] = i + 1;
}
return NUM_CRYPTO_FUNCS;
case DRV_MD5:
bin = return_binary(rbuf,rlen,MD5_LEN);
if (bin==NULL) return -1;
MD5((unsigned char *) buf, len, bin);
return MD5_LEN;
case DRV_MD5_INIT:
bin = return_binary(rbuf,rlen,MD5_CTX_LEN);
if (bin==NULL) return -1;
MD5_Init((MD5_CTX *)bin);
return MD5_CTX_LEN;
case DRV_MD5_UPDATE:
if (len < MD5_CTX_LEN)
return -1;
bin = return_binary(rbuf,rlen,MD5_CTX_LEN);
if (bin==NULL) return -1;
memcpy(bin, buf, MD5_CTX_LEN);
MD5_Update((MD5_CTX *)bin, buf + MD5_CTX_LEN,
len - MD5_CTX_LEN);
return MD5_CTX_LEN;
case DRV_MD5_FINAL:
if (len != MD5_CTX_LEN)
return -1;
memcpy(&md5_ctx, buf, MD5_CTX_LEN); /* XXX Use buf only? */
bin = return_binary(rbuf,rlen,MD5_LEN);
if (bin==NULL) return -1;
MD5_Final(bin, &md5_ctx);
return MD5_LEN;
case DRV_SHA:
bin = return_binary(rbuf,rlen,SHA_LEN);
if (bin==NULL) return -1;
SHA1((unsigned char *) buf, len, bin);
return SHA_LEN;
case DRV_SHA_INIT:
bin = return_binary(rbuf,rlen,SHA_CTX_LEN);
if (bin==NULL) return -1;
SHA1_Init((SHA_CTX*)bin);
return SHA_CTX_LEN;
case DRV_SHA_UPDATE:
if (len < SHA_CTX_LEN)
return -1;
bin = return_binary(rbuf,rlen,SHA_CTX_LEN);
if (bin==NULL) return -1;
memcpy(bin, buf, SHA_CTX_LEN);
SHA1_Update((SHA_CTX*)bin, buf + SHA_CTX_LEN, len - SHA_CTX_LEN);
return SHA_CTX_LEN;
case DRV_SHA_FINAL:
if (len != SHA_CTX_LEN)
return -1;
memcpy(&sha_ctx, buf, SHA_CTX_LEN); /* XXX Use buf only? */
bin = return_binary(rbuf,rlen,SHA_LEN);
if (bin==NULL) return -1;
SHA1_Final(bin, &sha_ctx);
return SHA_LEN;
case DRV_MD5_MAC:
case DRV_MD5_MAC_96:
/* buf = klen[4] key data */
klen = get_int32(buf);
key = buf + 4;
dlen = len - klen - 4;
dbuf = key + klen;
hmac_md5(key, klen, dbuf, dlen, hmacbuf);
macsize = (command == DRV_MD5_MAC) ? MD5_LEN : MD5_LEN_96;
bin = return_binary(rbuf,rlen,macsize);
if (bin==NULL) return -1;
memcpy(bin, hmacbuf, macsize);
return macsize;
case DRV_SHA_MAC:
case DRV_SHA_MAC_96:
/* buf = klen[4] key data */
klen = get_int32(buf);
key = buf + 4;
dlen = len - klen - 4;
dbuf = key + klen;
hmac_sha1(key, klen, dbuf, dlen, hmacbuf);
macsize = (command == DRV_SHA_MAC) ? SHA_LEN : SHA_LEN_96;
bin = return_binary(rbuf,rlen,macsize);
if (bin==NULL) return -1;
memcpy(bin, (unsigned char *) hmacbuf, macsize);
return macsize;
case DRV_CBC_DES_ENCRYPT:
case DRV_CBC_DES_DECRYPT:
/* buf = key[8] ivec[8] data */
dlen = len - 16;
if (dlen < 0)
return -1;
if (dlen % 8 != 0)
return -1;
des_key = (const_DES_cblock*) buf;
des_ivec = (DES_cblock*)(buf + 8);
des_dbuf = (unsigned char *) (buf + 16);
bin = return_binary(rbuf,rlen,dlen);
if (bin==NULL) return -1;
DES_set_key(des_key, &schedule);
DES_ncbc_encrypt(des_dbuf, bin, dlen, &schedule, des_ivec,
(command == DRV_CBC_DES_ENCRYPT));
return dlen;
case DRV_BF_CFB64_ENCRYPT:
case DRV_BF_CFB64_DECRYPT:
{
/* buf = klen[4] key ivec[8] data */
unsigned char* ivec;
unsigned char bf_tkey[8]; /* blowfish ivec */
int bf_n; /* blowfish ivec pos */
int bf_direction;
const unsigned char *bf_dbuf; /* blowfish input data */
BF_KEY bf_key; /* blowfish key 8 */
klen = get_int32(buf);
key = buf + 4;
ivec = (unsigned char *) (key + klen);
bf_dbuf = ivec + 8;
dlen = len - 4 - klen - 8;
if (dlen < 0) return -1;
BF_set_key(&bf_key, klen, (unsigned char *) key);
memcpy(bf_tkey, ivec, 8);
bin = return_binary(rbuf,rlen,dlen);
if (bin==NULL) return -1;
bf_direction = command == DRV_BF_CFB64_ENCRYPT ? BF_ENCRYPT : BF_DECRYPT;
bf_n = 0;
BF_cfb64_encrypt(bf_dbuf, bin, dlen, &bf_key, bf_tkey, &bf_n, bf_direction);
return dlen;
}
/* case DRV_CBC_IDEA_ENCRYPT: */
/* case DRV_CBC_IDEA_DECRYPT: */
/* buf = key[16] ivec[8] data */
/* dlen = len - 24; */
/* if (dlen < 0) */
/* return -1; */
/* if (dlen % 8 != 0) */
/* return -1; */
/* bin = return_binary(rbuf,rlen,dlen); */
/* idea_set_encrypt_key(buf, &idea); */
/* if (command == DRV_CBC_IDEA_DECRYPT) { */
/* idea_set_decrypt_key(&idea, &idea2); */
/* memcpy(&idea, &idea2, sizeof(idea)); */
/* } */
/* idea_cbc_encrypt(buf + 24, bin, dlen, &idea, buf + 8, */
/* (command == DRV_CBC_IDEA_ENCRYPT)); */
/* return dlen; */
case DRV_CBC_RC2_40_ENCRYPT:
case DRV_CBC_RC2_40_DECRYPT:
/* buf = key[5] ivec[8] data */
dlen = len - 13;
if (dlen < 0)
return -1;
bin = return_binary(rbuf,rlen,dlen);
if (bin==NULL) return -1;
RC2_set_key(&rc2_key, 5, (unsigned char *) buf, 40);
RC2_cbc_encrypt((unsigned char *) (buf + 13), bin, dlen, &rc2_key,
(unsigned char *) (buf + 5),
(command == DRV_CBC_RC2_40_ENCRYPT));
return dlen;
case DRV_EDE3_CBC_DES_ENCRYPT:
case DRV_EDE3_CBC_DES_DECRYPT:
dlen = len - 32;
if (dlen < 0)
return -1;
des_key = (const_DES_cblock*) buf;
des_key2 = (const_DES_cblock*) (buf + 8);
des_key3 = (const_DES_cblock*) (buf + 16);
des_ivec = (DES_cblock*) (buf + 24);
des_dbuf = (unsigned char *) (buf + 32);
bin = return_binary(rbuf,rlen,dlen);
if (bin==NULL) return -1;
DES_set_key(des_key, &schedule);
DES_set_key(des_key2, &schedule2);
DES_set_key(des_key3, &schedule3);
DES_ede3_cbc_encrypt(des_dbuf, bin, dlen, &schedule,
&schedule2, &schedule3, des_ivec,
(command == DRV_EDE3_CBC_DES_ENCRYPT));
return dlen;
case DRV_AES_CFB_128_ENCRYPT:
case DRV_AES_CFB_128_DECRYPT:
/* buf = key[16] ivec[16] data */
dlen = len - 32;
if (dlen < 0)
return -1;
bin = return_binary(rbuf,rlen,dlen);
if (bin==NULL) return -1;
AES_set_encrypt_key((unsigned char *) buf, 128, &aes_key);
AES_cfb128_encrypt((unsigned char *) (buf+32), bin, dlen, &aes_key,
(unsigned char *) (buf+16), &new_ivlen,
(command == DRV_AES_CFB_128_ENCRYPT));
return dlen;
case DRV_RC4_ENCRYPT:
/* buf = klen[4] key data */
klen = get_int32(buf);
key = buf + 4;
dlen = len - klen - 4;
dbuf = key + klen;
bin = return_binary(rbuf,rlen,dlen);
if (bin==NULL) return -1;
RC4_set_key(&rc4_key, klen, (unsigned char *) key);
RC4(&rc4_key, dlen, (unsigned char *) dbuf, bin);
return dlen;
case DRV_RC4_SETKEY:
/* buf = key */
dlen = sizeof(rc4_key);
bin = return_binary(rbuf,rlen,dlen);
if (bin==NULL) return -1;
RC4_set_key(&rc4_key, len, (unsigned char *) buf);
memcpy(bin, &rc4_key, dlen);
return dlen;
case DRV_RC4_ENCRYPT_WITH_STATE:
/* buf = statelength[4] state data, return statelength[4] state data */
klen = get_int32(buf);
key = buf + 4;
dlen = len - klen - 4;
dbuf = key + klen;
bin = return_binary(rbuf,rlen,len);
if (bin==NULL) return -1;
memcpy(&rc4_key, key, klen);
RC4(&rc4_key, dlen, (unsigned char *) dbuf, bin + klen + 4);
memcpy(bin, buf, 4);
memcpy(bin + 4, &rc4_key, klen);
return len;
case DRV_RAND_BYTES:
/* buf = <<rlen:32/integer,topmask:8/integer,bottommask:8/integer>> */
if (len != 6)
return -1;
dlen = get_int32(buf);
bin = return_binary(rbuf,rlen,dlen);
if (bin==NULL) return -1;
RAND_pseudo_bytes(bin,dlen);
ERL_VALGRIND_MAKE_MEM_DEFINED(bin, dlen);
or_mask = ((unsigned char*)buf)[4];
bin[dlen-1] |= or_mask; /* topmask */
or_mask = ((unsigned char*)buf)[5];
bin[0] |= or_mask; /* bottommask */
return dlen;
case DRV_RAND_UNIFORM:
/* buf = <<from_len:32/integer,bn_from:from_len/binary, *
* to_len:32/integer,bn_to:to_len/binary>> */
if (len < 8)
return -1;
from_len = get_int32(buf);
if (len < (8 + from_len))
return -1;
to_len = get_int32(buf + 4 + from_len);
if (len != (8 + from_len + to_len))
return -1;
ERL_VALGRIND_ASSERT_MEM_DEFINED(buf, 4 + from_len + 4 + to_len);
bn_from = BN_new();
BN_bin2bn((unsigned char *)(buf + 4), from_len, bn_from);
bn_rand = BN_new();
BN_bin2bn((unsigned char *)(buf + 8 + from_len), to_len, bn_rand);
bn_to = BN_new();
BN_sub(bn_to, bn_rand, bn_from);
BN_pseudo_rand_range(bn_rand, bn_to);
BN_add(bn_rand, bn_rand, bn_from);
dlen = BN_num_bytes(bn_rand);
bin = return_binary(rbuf,rlen,dlen + 4);
if (bin==NULL) return -1;
put_int32(bin, dlen);
BN_bn2bin(bn_rand,(unsigned char*)(bin + 4));
ERL_VALGRIND_MAKE_MEM_DEFINED(bin+4, dlen);
BN_free(bn_rand);
BN_free(bn_from);
BN_free(bn_to);
return dlen + 4;
case DRV_MOD_EXP:
/* buf = <<base_len:32/integer,base/binary, *
* exponent_len:32/integer,exponent/binary, *
* modulo_len:32/integer, modulo/binary>> */
if (len < 12)
return -1;
base_len = get_int32(buf);
if (len < (12 + base_len))
return -1;
exponent_len = get_int32(buf + 4 + base_len);
if (len < (12 + base_len + exponent_len))
return -1;
modulo_len = get_int32(buf + 8 + base_len + exponent_len);
if (len != (12 + base_len + exponent_len + modulo_len))
return -1;
bn_base = BN_new();
BN_bin2bn((unsigned char *)(buf + 4),
base_len, bn_base);
bn_exponent = BN_new();
BN_bin2bn((unsigned char *)(buf + 8 + base_len),
exponent_len, bn_exponent);
bn_modulo = BN_new();
BN_bin2bn((unsigned char *)(buf + 12 + base_len + exponent_len),
modulo_len, bn_modulo);
bn_result = BN_new();
bn_ctx = BN_CTX_new();
BN_mod_exp(bn_result, bn_base, bn_exponent,
bn_modulo, bn_ctx);
dlen = BN_num_bytes(bn_result);
bin = return_binary(rbuf,rlen,dlen + 4);
if (bin==NULL) return -1;
put_int32(bin, dlen);
BN_bn2bin(bn_result,(unsigned char*)(bin + 4));
BN_free(bn_result);
BN_free(bn_modulo);
BN_free(bn_exponent);
BN_free(bn_base);
BN_CTX_free(bn_ctx);
return dlen + 4;
case DRV_DSS_VERIFY:
/* buf = <<data_len:32/integer, data:data_len/binary,
* dsa_s_len:32/integer, dsa_s:dsa_s_len/binary,
* dsa_p_len:32/integer, dsa_p:dsa_p_len/binary,
* dsa_q_len:32/integer, dsa_q:dsa_q_len/binary,
* dsa_g_len:32/integer, dsa_g:dsa_g_len/binary,
* dsa_y_len:32/integer, dsa_y:dsa_y_len/binary>> */
i = 0;
j = 0;
if (len < 24)
return -1;
data_len = get_int32(buf + i + j);
j += data_len; i += 4;
if (len < (24 + j))
return -1;
dsa_s_len = get_int32(buf + i + j);
j += dsa_s_len; i += 4;
if (len < (24 + j))
return -1;
dsa_p_len = get_int32(buf + i + j);
j += dsa_p_len; i += 4;
if (len < (24 + j))
return -1;
dsa_q_len = get_int32(buf + i + j);
j += dsa_q_len; i += 4;
if (len < (24 + j))
return -1;
dsa_g_len = get_int32(buf + i + j);
j += dsa_g_len; i += 4;
if (len < (24 + j))
return -1;
dsa_y_len = get_int32(buf + i + j);
j += dsa_y_len;
if (len != (24 + j))
return -1;
i = 4;
SHA1((unsigned char *) (buf + i), data_len, (unsigned char *) hmacbuf);
i += data_len + 4;
dsa_s = (unsigned char *)(buf + i);
i += (dsa_s_len + 4);
dsa_p = BN_new();
BN_bin2bn((unsigned char *)(buf + i), dsa_p_len, dsa_p);
i += (dsa_p_len + 4);
dsa_q = BN_new();
BN_bin2bn((unsigned char *)(buf + i), dsa_q_len, dsa_q);
i += (dsa_q_len + 4);
dsa_g = BN_new();
BN_bin2bn((unsigned char *)(buf + i), dsa_g_len, dsa_g);
i += (dsa_g_len + 4);
dsa_y = BN_new();
BN_bin2bn((unsigned char *)(buf + i), dsa_y_len, dsa_y);
dsa = DSA_new();
dsa->p = dsa_p;
dsa->q = dsa_q;
dsa->g = dsa_g;
dsa->priv_key = NULL;
dsa->pub_key = dsa_y;
i = DSA_verify(0, (unsigned char *) hmacbuf, SHA_DIGEST_LENGTH,
dsa_s, dsa_s_len, dsa);
bin = return_binary(rbuf,rlen,1);
if (bin==NULL) return -1;
DSA_free(dsa);
bin[0] = (i > 0) ? 1 : 0;
return 1;
case DRV_DSS_SIGN:
/* buf = <<data_len:32/integer, data:data_len/binary,
* dsa_p_len:32/integer, dsa_p:dsa_p_len/binary,
* dsa_q_len:32/integer, dsa_q:dsa_q_len/binary,
* dsa_g_len:32/integer, dsa_g:dsa_g_len/binary,
* dsa_y_len:32/integer, dsa_y:dsa_y_len/binary,
* dsa_x_len:32/integer, dsa_s:dsa_x_len/binary>> */
i = 0;
j = 0;
if (len < 20)
return -1;
data_len = get_int32(buf + i + j);
j += data_len; i += 4;
if (len < (20 + j))
return -1;
dsa_p_len = get_int32(buf + i + j);
j += dsa_p_len; i += 4;
if (len < (20 + j))
return -1;
dsa_q_len = get_int32(buf + i + j);
j += dsa_q_len; i += 4;
if (len < (20 + j))
return -1;
dsa_g_len = get_int32(buf + i + j);
j += dsa_g_len; i += 4;
if (len < (20 + j))
return -1;
dsa_y_len = get_int32(buf + i + j);
j += dsa_y_len;
if (len < (20 + j))
return -1;
if (len != (20 + j))
return -1;
i = 4;
SHA1((unsigned char *) (buf + i), data_len, (unsigned char *) hmacbuf);
i += data_len + 4;
dsa_p = BN_new();
BN_bin2bn((unsigned char *)(buf + i), dsa_p_len, dsa_p);
i += (dsa_p_len + 4);
dsa_q = BN_new();
BN_bin2bn((unsigned char *)(buf + i), dsa_q_len, dsa_q);
i += (dsa_q_len + 4);
dsa_g = BN_new();
BN_bin2bn((unsigned char *)(buf + i), dsa_g_len, dsa_g);
i += (dsa_g_len + 4);
dsa_y = BN_new();
BN_bin2bn((unsigned char *)(buf + i), dsa_y_len, dsa_y);
/* i += (dsa_y_len + 4); */
dsa = DSA_new();
dsa->p = dsa_p;
dsa->q = dsa_q;
dsa->g = dsa_g;
dsa->priv_key = dsa_y;
dsa->pub_key = NULL;
dlen = DSA_size(dsa);
bin = return_binary(rbuf,rlen, dlen+1);
if (bin==NULL) return -1;
i = DSA_sign(NID_sha1,
(unsigned char *) hmacbuf,SHA_DIGEST_LENGTH,
(unsigned char *) &bin[1],
(unsigned int *) &dsa_s_len, dsa);
DSA_free(dsa);
if (i) {
if (dsa_s_len != dlen) {
bin = return_binary_shrink(rbuf,rlen,bin,dsa_s_len+1);
}
bin[0] = 1;
return dsa_s_len + 1;
}
else {
bin[0] = 0;
return 1;
}
case DRV_RSA_VERIFY_MD5:
case DRV_RSA_VERIFY_SHA:
/* buf = <<data_len:32/integer, data:data_len/binary,
* rsa_s_len:32/integer, rsa_s:rsa_s_len/binary,
* rsa_e_len:32/integer, rsa_e:rsa_e_len/binary,
* rsa_n_len:32/integer, rsa_n:rsa_n_len/binary>> */
i = 0;
j = 0;
if (len < 16)
return -1;
data_len = get_int32(buf + i + j);
j += data_len; i += 4;
if (len < (16 + j))
return -1;
rsa_s_len = get_int32(buf + i + j);
j += rsa_s_len; i += 4;
if (len < (16 + j))
return -1;
rsa_e_len = get_int32(buf + i + j);
j += rsa_e_len; i += 4;
if (len < (16 + j))
return -1;
rsa_n_len = get_int32(buf + i + j);
j += rsa_n_len; i += 4;
if (len != (16 + j))
return -1;
i = 4;
i += (data_len + 4);
rsa_s = (unsigned char *)(buf + i);
i += (rsa_s_len + 4);
rsa_e = BN_new();
BN_bin2bn((unsigned char *)(buf + i), rsa_e_len, rsa_e);
i += (rsa_e_len + 4);
rsa_n = BN_new();
BN_bin2bn((unsigned char *)(buf + i), rsa_n_len, rsa_n);
rsa = RSA_new();
rsa->n = rsa_n;
rsa->e = rsa_e;
i = 4;
if(command == DRV_RSA_VERIFY_SHA) {
SHA1((unsigned char *) (buf + i), data_len,
(unsigned char *) hmacbuf);
i = RSA_verify(NID_sha1, (unsigned char *) hmacbuf, SHA_DIGEST_LENGTH,
rsa_s, rsa_s_len, rsa);
} else {
MD5((unsigned char *) (buf + i), data_len, (unsigned char *) hmacbuf);
i = RSA_verify(NID_md5, (unsigned char *) hmacbuf, MD5_DIGEST_LENGTH,
rsa_s, rsa_s_len, rsa);
}
bin = return_binary(rbuf,rlen,1);
if (bin==NULL) return -1;
bin[0] = (char)(i & 0xff);
RSA_free(rsa);
return 1;
case DRV_RSA_SIGN_MD5:
case DRV_RSA_SIGN_SHA:
/* buf = <<data_len:32/integer, data:data_len/binary,
* rsa_e_len:32/integer, rsa_e:rsa_e_len/binary,
* rsa_n_len:32/integer, rsa_n:rsa_n_len/binary,
* rsa_d_len:32/integer, rsa_d:rsa_d_len/binary>> */
ERL_VALGRIND_ASSERT_MEM_DEFINED(buf,len);
i = 0;
j = 0;
if (len < 16)
return -1;
data_len = get_int32(buf + i + j);
j += data_len; i += 4;
if (len < (16 + j))
return -1;
rsa_e_len = get_int32(buf + i + j);
j += rsa_e_len; i += 4;
if (len < (16 + j))
return -1;
rsa_n_len = get_int32(buf + i + j);
j += rsa_n_len; i += 4;
if (len < (16 + j))
return -1;
rsa_d_len = get_int32(buf + i + j);
j += rsa_d_len; i += 4;
if (len != (16 + j))
return -1;
i = 4;
i += (data_len + 4);
rsa_e = BN_new();
BN_bin2bn((unsigned char *)(buf + i), rsa_e_len, rsa_e);
i += (rsa_e_len + 4);
rsa_n = BN_new();
BN_bin2bn((unsigned char *)(buf + i), rsa_n_len, rsa_n);
i += (rsa_n_len + 4);
rsa_d = BN_new();
BN_bin2bn((unsigned char *)(buf + i), rsa_d_len, rsa_d);
i += (rsa_d_len + 4);
rsa = RSA_new();
rsa->e = rsa_e;
rsa->n = rsa_n;
rsa->d = rsa_d;
dlen = RSA_size(rsa);
bin = return_binary(rbuf,rlen,dlen+1);
if (bin==NULL) return -1;
i = 4;
if (command == DRV_RSA_SIGN_MD5) {
MD5((unsigned char *) (buf + i), data_len, (unsigned char *) hmacbuf);
ERL_VALGRIND_ASSERT_MEM_DEFINED(hmacbuf, MD5_DIGEST_LENGTH);
i = RSA_sign(NID_md5,
(unsigned char *) hmacbuf,MD5_DIGEST_LENGTH,
(unsigned char *) &bin[1],
&rsa_s_len, rsa);
} else {
SHA1((unsigned char *) (buf + i), data_len,
(unsigned char *) hmacbuf);
ERL_VALGRIND_ASSERT_MEM_DEFINED(hmacbuf, SHA_DIGEST_LENGTH);
i = RSA_sign(NID_sha1,
(unsigned char *) hmacbuf,SHA_DIGEST_LENGTH,
(unsigned char *) &bin[1],
&rsa_s_len, rsa);