-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcrypto4pl.c
2430 lines (2027 loc) · 62.6 KB
/
crypto4pl.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
/* Part of SWI-Prolog
Author: Matt Lilley and Markus Triska
WWW: http://www.swi-prolog.org
Copyright (c) 2004-2024, SWI-Prolog Foundation
VU University Amsterdam
SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#define _CRT_SECURE_NO_WARNINGS 1
#include <config.h>
#include <assert.h>
#include <string.h>
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#ifdef HAVE_OPENSSL_CORE_NAMES_H
#include <openssl/core_names.h>
#endif
#ifdef HAVE_OPENSSL_PARAM_BUILD_H
#include <openssl/param_build.h>
#endif
#ifdef HAVE_OPENSSL_KDF_H
#include <openssl/kdf.h>
#endif
#include "crypt_blowfish.h"
#include "common.h"
#include "cryptolib.c"
static atom_t ATOM_sslv23;
static atom_t ATOM_minus; /* "-" */
static atom_t ATOM_text;
static atom_t ATOM_octet;
static atom_t ATOM_utf8;
static atom_t ATOM_md5;
static atom_t ATOM_sha1;
static atom_t ATOM_sha224;
static atom_t ATOM_sha256;
static atom_t ATOM_sha384;
static atom_t ATOM_sha512;
static atom_t ATOM_blake2s256;
static atom_t ATOM_blake2b512;
static atom_t ATOM_sha3_224;
static atom_t ATOM_sha3_256;
static atom_t ATOM_sha3_384;
static atom_t ATOM_sha3_512;
static atom_t ATOM_ripemd160;
static atom_t ATOM_pkcs1;
static atom_t ATOM_pkcs1_oaep;
static atom_t ATOM_none;
static atom_t ATOM_block;
static atom_t ATOM_algorithm;
static atom_t ATOM_hmac;
static atom_t ATOM_close_parent;
static atom_t ATOM_encoding;
static atom_t ATOM_padding;
static functor_t FUNCTOR_public_key1;
static functor_t FUNCTOR_private_key1;
typedef enum
{ RSA_MODE, EVP_MODE
} crypt_mode_t;
#if defined HAVE_EVP_PKEY_NEW && defined HAVE_EVP_PKEY_FREE && defined HAVE_EVP_PKEY_GET_BN_PARAM && defined HAVE_EVP_PKEY_GET_OCTET_STRING_PARAM && defined HAVE_EVP_PKEY_GET_SIZE && defined HAVE_EVP_PKEY_DECRYPT && defined HAVE_EVP_PKEY_ENCRYPT && defined HAVE_EVP_PKEY_SIGN && defined HAVE_EVP_PKEY_VERIFY && defined HAVE_EVP_PKEY_Q_KEYGEN && defined HAVE_OSSL_PARAM_CONSTRUCT_UTF8_STRING && defined HAVE_BN_CHECK_PRIME && defined HAVE_OSSL_PARAM_BLD_NEW
#define USE_EVP_API 1
#endif
#ifdef USE_EVP_API
#define RSAKEY EVP_PKEY
#else
#define RSAKEY RSA
#endif
/***************************
* RANDOM BYTES *
****************************/
static foreign_t
pl_crypto_n_random_bytes(term_t tn, term_t tout)
{ size_t len;
int rc;
unsigned char *buffer;
if ( !PL_get_size_ex(tn, &len) )
return FALSE;
if ( !(buffer = malloc(len)) )
return PL_resource_error("memory");
if ( RAND_bytes(buffer, (int)len) == 0 )
{ free(buffer);
return raise_ssl_error(ERR_get_error());
}
rc = PL_unify_chars(tout, PL_CODE_LIST|REP_ISO_LATIN_1,
len, (const char *) buffer);
free(buffer);
return rc;
}
/***************************
* HASHING *
****************************/
#define HASH_CONTEXT_MAGIC (~ 0x53481284L)
typedef struct hash_context
{ int magic;
atom_t atom;
IOENC encoding;
const EVP_MD *algorithm;
IOSTREAM *parent_stream; /* Original stream */
IOSTREAM *hash_stream;
IOENC parent_encoding;
int close_parent;
EVP_MD_CTX *ctx;
#if defined USE_EVP_API
EVP_MAC *mac;
EVP_MAC_CTX *mac_ctx;
#else
HMAC_CTX *mac_ctx;
#endif
char *mac_key;
size_t mac_key_len;
} PL_CRYPTO_HASH_CONTEXT;
static void
free_crypto_hash_context(PL_CRYPTO_HASH_CONTEXT *c)
{ EVP_MD_CTX_free(c->ctx);
if ( c->mac_key )
PL_free(c->mac_key); /* allocated using BUF_MALLOC */
#ifdef USE_EVP_API
EVP_MAC_free(c->mac);
EVP_MAC_CTX_free(c->mac_ctx);
#elif defined HAVE_HMAC_CTX_FREE
HMAC_CTX_free(c->mac_ctx);
#endif
free(c);
}
static int
release_hash_context(atom_t atom)
{ PL_CRYPTO_HASH_CONTEXT **cp = PL_blob_data(atom, NULL, NULL);
PL_CRYPTO_HASH_CONTEXT *c = *cp;
ssl_deb(4, "Releasing PL_CRYPTO_HASH_CONTEXT %p\n", c);
free_crypto_hash_context(c);
return TRUE;
}
static int
compare_hash_context(atom_t a, atom_t b)
{ PL_CRYPTO_HASH_CONTEXT **cp1 = PL_blob_data(a, NULL, NULL);
PL_CRYPTO_HASH_CONTEXT **cp2 = PL_blob_data(b, NULL, NULL);
PL_CRYPTO_HASH_CONTEXT *c1 = *cp1;
PL_CRYPTO_HASH_CONTEXT *c2 = *cp2;
return ( c1 > c2 ? 1 :
c1 < c2 ? -1 : 0
);
}
static int
write_hash_context(IOSTREAM *s, atom_t symbol, int flags)
{ PL_CRYPTO_HASH_CONTEXT **cp = PL_blob_data(symbol, NULL, NULL);
PL_CRYPTO_HASH_CONTEXT *c = *cp;
Sfprintf(s, "<crypto_hash_context>(%p)", c);
return TRUE;
}
static void
acquire_hash_context(atom_t atom)
{ PL_CRYPTO_HASH_CONTEXT **cp = PL_blob_data(atom, NULL, NULL);
PL_CRYPTO_HASH_CONTEXT *c = *cp;
c->atom = atom;
}
static PL_blob_t crypto_hash_context_type =
{ PL_BLOB_MAGIC,
0,
"crypto_hash_context",
release_hash_context,
compare_hash_context,
write_hash_context,
acquire_hash_context
};
static int
unify_hash_context(term_t tcontext, PL_CRYPTO_HASH_CONTEXT *context)
{ if ( PL_unify_blob(tcontext, &context, sizeof(context), &crypto_hash_context_type) )
return TRUE;
free_crypto_hash_context(context);
if ( !PL_exception(0) )
return PL_uninstantiation_error(tcontext);
return FALSE;
}
static int
get_hash_context(term_t tcontext, PL_CRYPTO_HASH_CONTEXT **context)
{ PL_blob_t *type;
void *data;
if ( PL_get_blob(tcontext, &data, NULL, &type) &&
type == &crypto_hash_context_type )
{ PL_CRYPTO_HASH_CONTEXT *c = *(PL_CRYPTO_HASH_CONTEXT**)data;
assert(c->magic == HASH_CONTEXT_MAGIC);
*context = c;
return TRUE;
}
return PL_type_error("crypto_hash_context", tcontext);
}
typedef struct algorithm_pair {
atom_t a_algorithm;
const EVP_MD *algorithm;
} ALGORITHM_PAIR;
#define ALGO(a) { ATOM_## a , EVP_## a() }
#define NELEMS(array) (sizeof(array)/sizeof((array)[0]))
static int
get_hash_algorithm(atom_t a_algorithm, const EVP_MD **algorithm)
{ int i;
ALGORITHM_PAIR algorithms[] =
{ ALGO(md5), ALGO(ripemd160),
#if defined(HAVE_EVP_BLAKE2B512) && defined(HAVE_EVP_BLAKE2S256)
ALGO(blake2s256), ALGO(blake2b512),
#endif
#if defined(HAVE_EVP_SHA3_224) && defined(HAVE_EVP_SHA3_256) && \
defined(HAVE_EVP_SHA3_384) && defined(HAVE_EVP_SHA3_512)
ALGO(sha3_224), ALGO(sha3_256), ALGO(sha3_384), ALGO(sha3_512),
#endif
ALGO(sha1), ALGO(sha224), ALGO(sha256), ALGO(sha384), ALGO(sha512)
};
for (i = 0; i < NELEMS(algorithms); i++)
{ if (a_algorithm == algorithms[i].a_algorithm)
{ *algorithm = algorithms[i].algorithm;
return TRUE;
}
}
return FALSE;
}
static int
get_text_representation(term_t t, int *rep)
{ atom_t a;
if ( PL_get_atom_ex(t, &a) )
{ if ( a == ATOM_octet ) *rep = REP_ISO_LATIN_1;
else if ( a == ATOM_utf8 ) *rep = REP_UTF8;
else if ( a == ATOM_text ) *rep = REP_MB;
else return PL_domain_error("encoding", t);
return TRUE;
}
return FALSE;
}
static int
hash_options(term_t options, PL_CRYPTO_HASH_CONTEXT *result)
{ term_t opts = PL_copy_term_ref(options);
term_t opt = PL_new_term_ref();
/* defaults */
result->encoding = REP_UTF8;
result->algorithm = EVP_sha256();
while(PL_get_list(opts, opt, opts))
{ atom_t aname;
size_t arity;
if ( PL_get_name_arity(opt, &aname, &arity) && arity == 1 )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, opt, a);
if ( aname == ATOM_algorithm )
{ atom_t a_algorithm;
if ( !PL_get_atom_ex(a, &a_algorithm) )
return FALSE;
if ( !get_hash_algorithm(a_algorithm, &result->algorithm) )
return PL_domain_error("algorithm", a);
} else if ( aname == ATOM_hmac )
{ size_t key_len;
char *key;
if ( !PL_get_nchars(a, &key_len, &key,
CVT_ATOM|CVT_STRING|CVT_LIST|
CVT_EXCEPTION|BUF_MALLOC) )
return FALSE;
result->mac_key = key;
result->mac_key_len = key_len;
} else if ( aname == ATOM_close_parent )
{ if ( !PL_get_bool_ex(a, &result->close_parent) )
return FALSE;
} else if ( aname == ATOM_encoding )
{ int rep;
if ( !get_text_representation(a, &rep) )
return PL_domain_error("encoding", a);
result->encoding = ( rep == REP_UTF8 ) ? REP_UTF8 : REP_ISO_LATIN_1;
}
} else
{ return PL_type_error("option", opt);
}
}
if ( !PL_get_nil_ex(opts) )
return FALSE;
return TRUE;
}
static foreign_t
pl_crypto_hash_context_new(term_t tcontext, term_t options)
{ PL_CRYPTO_HASH_CONTEXT *context;
if ( !(context = malloc(sizeof(*context))) )
return PL_resource_error("memory");
memset(context, 0, sizeof(*context));
context->magic = HASH_CONTEXT_MAGIC;
if ( !hash_options(options, context) )
return FALSE;
#ifdef USE_EVP_API
if ( context->mac_key )
{ OSSL_PARAM params[2];
context->mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
if ( context->mac == NULL )
{ return FALSE;
}
context->mac_ctx = EVP_MAC_CTX_new(context->mac);
if ( context->mac_ctx == NULL )
{ EVP_MAC_free(context->mac);
return FALSE;
}
params[0] = OSSL_PARAM_construct_utf8_string("digest", (char *)EVP_MD_name(context->algorithm), 0);
params[1] = OSSL_PARAM_construct_end();
if ( !EVP_MAC_init(context->mac_ctx,
(unsigned char*)context->mac_key, context->mac_key_len,
params) )
{ EVP_MAC_CTX_free(context->mac_ctx);
EVP_MAC_free(context->mac);
return FALSE;
}
}
#elif defined HAVE_HMAC_CTX_NEW
if ( context->mac_key )
{ context->mac_ctx = HMAC_CTX_new();
if ( !HMAC_Init_ex(context->mac_ctx,
context->mac_key, context->mac_key_len,
context->algorithm, NULL) )
{ HMAC_CTX_free(context->mac_ctx);
return FALSE;
}
}
#endif
if ( !context->mac_ctx )
{ context->ctx = EVP_MD_CTX_new();
if ( !EVP_DigestInit_ex(context->ctx, context->algorithm, NULL) )
{ EVP_MD_CTX_free(context->ctx);
return FALSE;
}
}
return unify_hash_context(tcontext, context);
}
static foreign_t
pl_crypto_hash_context_copy(term_t tin, term_t tout)
{
PL_CRYPTO_HASH_CONTEXT *in, *out;
int rc = 0;
if ( !get_hash_context(tin, &in) )
return FALSE;
if ( !(out = malloc(sizeof(*out))) )
return PL_resource_error("memory");
memset(out, 0, sizeof(*out));
out->magic = HASH_CONTEXT_MAGIC;
out->encoding = in->encoding;
out->algorithm = in->algorithm;
if ( in->mac_key )
{ char *tmp = PL_malloc(in->mac_key_len+1);
memcpy(tmp, in->mac_key, in->mac_key_len+1);
out->mac_key = tmp;
out->mac_key_len = in->mac_key_len;
}
out->ctx = in->ctx ? EVP_MD_CTX_new() : NULL;
if ( out->ctx )
{ if ( !EVP_DigestInit_ex(out->ctx, out->algorithm, NULL) )
{ EVP_MD_CTX_free(out->ctx);
return FALSE;
}
rc = EVP_MD_CTX_copy_ex(out->ctx, in->ctx);
}
#if defined(USE_EVP_API) && defined(USE_EVP_API)
out->mac = in->mac;
if ( in->mac != NULL )
{ EVP_MAC_up_ref(in->mac);
}
out->mac_ctx = in->mac_ctx ? EVP_MAC_CTX_dup(in->mac_ctx) : NULL;
rc = TRUE;
#elif defined(HAVE_HMAC_CTX_NEW) && defined(HAVE_HMAC_CTX_FREE)
out->mac_ctx = in->mac_ctx ? HMAC_CTX_new() : NULL;
if ( out->mac_ctx )
{ if ( !HMAC_Init_ex(out->mac_ctx,
out->mac_key, out->mac_key_len,
out->algorithm, NULL) )
{ HMAC_CTX_free(out->mac_ctx);
return FALSE;
}
rc = HMAC_CTX_copy(out->mac_ctx, in->mac_ctx);
}
#else
out->mac_ctx = NULL;
#endif
return unify_hash_context(tout, out) && rc;
}
static int
hash_append(PL_CRYPTO_HASH_CONTEXT *context, void *data, size_t size)
{
if ( context->mac_ctx )
{
#ifdef USE_EVP_API
return EVP_MAC_update(context->mac_ctx, data, size);
#else
return HMAC_Update(context->mac_ctx, data, size);
#endif
}
return EVP_DigestUpdate(context->ctx, data, size);
}
static foreign_t
pl_crypto_update_hash_context(term_t from, term_t tcontext)
{
PL_CRYPTO_HASH_CONTEXT *context = NULL;
size_t datalen;
char *data;
if ( !get_hash_context(tcontext, &context) )
return FALSE;
if ( !PL_get_nchars(from, &datalen, &data,
CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION|context->encoding) )
return FALSE;
return hash_append(context, data, datalen);
}
static foreign_t
pl_crypto_hash_context_hash(term_t tcontext, term_t hash)
{
PL_CRYPTO_HASH_CONTEXT *context = NULL;
unsigned char digest[EVP_MAX_MD_SIZE];
size_t len;
if ( !get_hash_context(tcontext, &context) )
return FALSE;
if ( context->mac_ctx )
{
#ifdef USE_EVP_API
EVP_MAC_final(context->mac_ctx, digest, &len, EVP_MAX_MD_SIZE);
#else
unsigned int ulen;
HMAC_Final(context->mac_ctx, digest, &ulen);
len = ulen;
#endif
} else
{ unsigned int ulen;
EVP_DigestFinal_ex(context->ctx, digest, &ulen);
len = ulen;
}
return PL_unify_list_ncodes(hash, len, (char *) digest);
}
/***************************
* Hashes on streams *
****************************/
static ssize_t /* range-limited read */
hash_read(void *handle, char *buf, size_t size)
{ PL_CRYPTO_HASH_CONTEXT *ctx = handle;
ssize_t rd;
if ( (rd = Sfread(buf, sizeof(char), size, ctx->parent_stream)) >= 0 )
{ hash_append(ctx, buf, rd);
return rd;
}
return rd;
}
static ssize_t
hash_write(void *handle, char *buf, size_t size)
{ PL_CRYPTO_HASH_CONTEXT *ctx = handle;
size_t written = 0;
hash_append(ctx, buf, size);
while ( written < size )
{ ssize_t wr = Sfwrite(buf+written, sizeof(char), size, ctx->parent_stream);
if ( wr >= 0 )
{ written += wr;
} else
return wr;
}
return size;
}
static int
hash_control(void *handle, int op, void *data)
{ PL_CRYPTO_HASH_CONTEXT *ctx = handle;
switch(op)
{ case SIO_SETENCODING:
return 0; /* allow switching encoding */
default:
if ( ctx->parent_stream->functions->control )
return (*ctx->parent_stream->functions->control)(ctx->parent_stream->handle, op, data);
return -1;
}
}
static int
hash_close(void *handle)
{ int rc = 0;
PL_CRYPTO_HASH_CONTEXT *ctx = handle;
ctx->parent_stream->encoding = ctx->parent_encoding;
if ( ctx->parent_stream->upstream )
Sset_filter(ctx->parent_stream, NULL);
if ( ctx->close_parent )
rc = Sclose(ctx->parent_stream);
free_crypto_hash_context(ctx);
return rc;
}
static IOFUNCTIONS hash_functions =
{ hash_read,
hash_write,
NULL, /* seek */
hash_close,
hash_control,
NULL, /* seek64 */
};
#define COPY_FLAGS (SIO_INPUT|SIO_OUTPUT| \
SIO_TEXT| \
SIO_REPXML|SIO_REPPL|\
SIO_RECORDPOS)
static foreign_t
pl_crypto_open_hash_stream(term_t org, term_t new, term_t tcontext)
{ PL_CRYPTO_HASH_CONTEXT *context;
IOSTREAM *s, *s2;
if ( !get_hash_context(tcontext, &context) )
return FALSE;
if ( !PL_get_stream_handle(org, &s) )
return FALSE; /* Error */
context->parent_encoding = s->encoding;
context->parent_stream = s;
if ( !(s2 = Snew(context,
(s->flags©_FLAGS)|SIO_FBUF,
&hash_functions)) )
{ PL_release_stream(s);
return FALSE;
}
s2->encoding = s->encoding;
s->encoding = ENC_OCTET;
context->hash_stream = s2;
if ( PL_unify_stream(new, s2) )
{ Sset_filter(s, s2);
PL_release_stream(s);
/* Increase atom reference count so that the context is not
GCd until this session is complete */
PL_register_atom(context->atom);
return TRUE;
} else
{ PL_release_stream(s);
return FALSE;
}
}
static foreign_t
pl_crypto_stream_hash_context(term_t stream, term_t tcontext)
{ IOSTREAM *s;
int rc;
if ( PL_get_stream_handle(stream, &s) )
{ PL_CRYPTO_HASH_CONTEXT *ctx = s->handle;
rc = unify_hash_context(tcontext, ctx);
PL_release_stream(s);
return rc;
}
return FALSE;
}
/***************************
* Hashes of passwords *
****************************/
#define PBKDF2_DIGEST_LEN 64
static foreign_t
pl_crypto_password_hash_pbkdf2(term_t tpw, term_t tsalt, term_t titer, term_t tdigest)
{ char *pw, *salt;
size_t pwlen, saltlen;
int iter;
unsigned char digest[PBKDF2_DIGEST_LEN];
if ( !PL_get_nchars(tpw, &pwlen, &pw,
CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION|REP_UTF8) ||
!PL_get_nchars(tsalt, &saltlen, &salt, CVT_LIST) ||
!PL_get_integer_ex(titer, &iter) )
return FALSE;
PKCS5_PBKDF2_HMAC((const char *) pw, (int)pwlen,
(const unsigned char *) salt, (int)saltlen,
iter, EVP_sha512(), PBKDF2_DIGEST_LEN, digest);
return PL_unify_list_ncodes(tdigest, PBKDF2_DIGEST_LEN, (char *) digest);
}
#define BCRYPT_DIGEST_LEN (7 + 22 + 31 + 1)
static foreign_t
pl_crypto_password_hash_bcrypt(term_t tpw, term_t tsetting, term_t tdigest)
{ char *pw, *setting;
size_t pwlen, settinglen;
char digest[BCRYPT_DIGEST_LEN];
if ( !PL_get_nchars(tpw, &pwlen, &pw,
CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION|REP_UTF8) ||
!PL_get_nchars(tsetting, &settinglen, &setting,
CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION|REP_UTF8) )
return FALSE;
char* ret = _crypt_blowfish_rn(pw, setting, (char *) digest, BCRYPT_DIGEST_LEN);
if ( ret == NULL )
return PL_domain_error("setting", tsetting);
return PL_unify_chars(tdigest, PL_ATOM | REP_UTF8, BCRYPT_DIGEST_LEN - 1, (char *) digest);
}
static foreign_t
pl_crypto_data_hkdf(term_t tkey, term_t tsalt, term_t tinfo, term_t talg,
term_t tencoding, term_t toutlen, term_t tout)
{
#if defined(HAVE_OPENSSL_KDF_H) && defined(EVP_PKEY_HKDF)
EVP_PKEY_CTX *pctx;
char *salt, *key, *info;
size_t keylen, infolen, outlen, saltlen;
int rep;
const EVP_MD *alg;
unsigned char *out;
atom_t a_algorithm;
if ( !PL_get_nchars(tsalt, &saltlen, &salt, CVT_LIST) ||
!PL_get_size_ex(toutlen, &outlen) ||
!PL_get_atom_ex(talg, &a_algorithm) )
return FALSE;
if ( !get_text_representation(tencoding, &rep) )
return PL_domain_error("encoding", tencoding);
if ( !PL_get_nchars(tkey, &keylen, &key,
CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION|rep) ||
!PL_get_nchars(tinfo, &infolen, &info,
CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION) )
return FALSE;
if ( !get_hash_algorithm(a_algorithm, &alg) )
return PL_domain_error("algorithm", a_algorithm);
if ( !(out = malloc(outlen)) )
return PL_resource_error("memory");
pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
if ( (EVP_PKEY_derive_init(pctx) > 0) &&
(EVP_PKEY_CTX_set_hkdf_md(pctx, alg) > 0) &&
(EVP_PKEY_CTX_set1_hkdf_salt(pctx, (unsigned char*)salt, (int)saltlen) > 0) &&
(EVP_PKEY_CTX_set1_hkdf_key(pctx, (unsigned char*)key, (int)keylen) > 0) &&
(EVP_PKEY_CTX_add1_hkdf_info(pctx, (unsigned char*)info, (int)infolen) > 0) &&
(EVP_PKEY_derive(pctx, out, &outlen) > 0) )
{ int rc = PL_unify_list_ncodes(tout, outlen, (char *) out);
free(out);
EVP_PKEY_CTX_free(pctx);
return rc;
}
free(out);
EVP_PKEY_CTX_free(pctx);
return raise_ssl_error(ERR_get_error());
#else
return ssl_missing("HKDF");
#endif
}
/***************************
* Bignums & Keys *
****************************/
static int
get_bn_arg(int a, term_t t, BIGNUM **bn)
{ term_t arg;
char *hex;
if ( (arg=PL_new_term_ref()) &&
PL_get_arg(a, t, arg) &&
PL_get_chars(arg, &hex,
CVT_ATOM|CVT_STRING|REP_ISO_LATIN_1|CVT_EXCEPTION) )
{ if ( strcmp(hex, "-") == 0 )
*bn = NULL;
else
BN_hex2bn(bn, hex);
return TRUE;
}
return FALSE;
}
#ifndef OPENSSL_NO_EC
#ifdef USE_EVP_API
#define ECKEY EVP_PKEY
#else
#define ECKEY EC_KEY
#endif
static int
recover_ec(term_t t, ECKEY **rec)
{
ECKEY *key;
BIGNUM *privkey = NULL;
term_t pubkey;
unsigned char *codes;
size_t codes_len;
term_t tcurve = PL_new_term_ref();
char *curve;
if ( !(tcurve &&
PL_get_arg(3, t, tcurve) &&
PL_get_chars(tcurve, &curve, CVT_ATOM|CVT_STRING|CVT_EXCEPTION) &&
#ifdef USE_EVP_API
(key = EVP_EC_gen(curve))
#else
(key = EC_KEY_new_by_curve_name(OBJ_sn2nid(curve)))
#endif
) )
return FALSE;
if ( !get_bn_arg(1, t, &privkey) )
{
#ifdef USE_EVP_API
EVP_PKEY_free(key);
#else
EC_KEY_free(key);
#endif
return FALSE;
}
if ( privkey )
{
#ifdef USE_EVP_API
EVP_PKEY_set_bn_param(key, "priv", privkey);
#else
EC_KEY_set_private_key(key, privkey);
#endif
}
if ( (pubkey=PL_new_term_ref()) &&
PL_get_arg(2, t, pubkey) &&
PL_get_nchars(pubkey, &codes_len, (char **) &codes,
CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION) &&
#ifdef USE_EVP_API
EVP_PKEY_set_octet_string_param(key, "pub", (const unsigned char*) codes, codes_len)
#else
(key = o2i_ECPublicKey(&key, (const unsigned char**) &codes, codes_len))
#endif
)
{ *rec = key;
return TRUE;
}
#ifdef USE_EVP_API
EVP_PKEY_free(key);
#else
EC_KEY_free(key);
#endif
return FALSE;
}
#endif
static int
recover_rsa(term_t t, RSAKEY** keyp)
{
#ifdef USE_EVP_API
RSAKEY* key = EVP_PKEY_new();
#else
RSAKEY *key = RSA_new();
#endif
#if SSL_API_0
if ( get_bn_arg(1, t, &key->n) &&
get_bn_arg(2, t, &key->e) &&
get_bn_arg(3, t, &key->d) &&
get_bn_arg(4, t, &key->p) &&
get_bn_arg(5, t, &key->q) &&
get_bn_arg(6, t, &key->dmp1) &&
get_bn_arg(7, t, &key->dmq1) &&
get_bn_arg(8, t, &key->iqmp)
)
{
#else
BIGNUM *n = NULL, *e = NULL, *d = NULL, *p = NULL,
*q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
if ( get_bn_arg(1, t, &n) &&
get_bn_arg(2, t, &e) &&
get_bn_arg(3, t, &d) &&
get_bn_arg(4, t, &p) &&
get_bn_arg(5, t, &q) &&
get_bn_arg(6, t, &dmp1) &&
get_bn_arg(7, t, &dmq1) &&
get_bn_arg(8, t, &iqmp) )
{
#ifdef USE_EVP_API
OSSL_PARAM_BLD *param_builder;
OSSL_PARAM *params = NULL;
EVP_PKEY_CTX* ctx;
ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
if (!ctx)
{ return FALSE;
}
param_builder = OSSL_PARAM_BLD_new();
if ( ! ( OSSL_PARAM_BLD_push_BN(param_builder, OSSL_PKEY_PARAM_RSA_N, n) &&
OSSL_PARAM_BLD_push_BN(param_builder, OSSL_PKEY_PARAM_RSA_E, e) &&
OSSL_PARAM_BLD_push_BN(param_builder, OSSL_PKEY_PARAM_RSA_D, d) ) ||
( ( p || q ) && ! ( OSSL_PARAM_BLD_push_BN(param_builder, OSSL_PKEY_PARAM_RSA_FACTOR1, p) &&
OSSL_PARAM_BLD_push_BN(param_builder, OSSL_PKEY_PARAM_RSA_FACTOR2, q) ) ) ||
( ( dmp1 || dmq1 || iqmp ) && ! ( OSSL_PARAM_BLD_push_BN(param_builder, OSSL_PKEY_PARAM_RSA_EXPONENT1, dmp1) &&
OSSL_PARAM_BLD_push_BN(param_builder, OSSL_PKEY_PARAM_RSA_EXPONENT2, dmq1) &&
OSSL_PARAM_BLD_push_BN(param_builder, OSSL_PKEY_PARAM_RSA_COEFFICIENT1, iqmp) ) ) )
{ EVP_PKEY_free(key);
OSSL_PARAM_BLD_free(param_builder);
return raise_ssl_error(ERR_get_error());
}
params = OSSL_PARAM_BLD_to_param(param_builder);
if (!params)
{ EVP_PKEY_CTX_free(ctx);
return FALSE;
}
OSSL_PARAM_BLD_free(param_builder);
if (EVP_PKEY_fromdata_init(ctx) <= 0)
{ EVP_PKEY_CTX_free(ctx);
OSSL_PARAM_free(params);
return raise_ssl_error(ERR_get_error());
}
if (EVP_PKEY_fromdata(ctx, &key, EVP_PKEY_KEYPAIR, params) <= 0)
{ EVP_PKEY_CTX_free(ctx);
OSSL_PARAM_free(params);
return raise_ssl_error(ERR_get_error());
}
EVP_PKEY_CTX_free(ctx);
OSSL_PARAM_free(params);
*keyp = key;
return TRUE;
#else
if ( !RSA_set0_key(key, n, e, d) ||
( (p || q) && !RSA_set0_factors(key, p, q) ) ||
( (dmp1 || dmq1 || iqmp) &&
!RSA_set0_crt_params(key, dmp1, dmq1, iqmp)) )
{ RSA_free(key);
return FALSE;
}
#endif
#endif
*keyp = key;
return TRUE;
}
#ifdef USE_EVP_API
EVP_PKEY_free(key);
#else
RSA_free(key);
#endif
return FALSE;
}
static int
recover_private_key(term_t t, RSAKEY** rsap)
{ if ( PL_is_functor(t, FUNCTOR_private_key1) )
{ term_t arg;
if ( (arg = PL_new_term_ref()) &&
PL_get_arg(1, t, arg) )
return recover_rsa(arg, rsap);
return FALSE;
}
return PL_type_error("private_key", t);
}
static int
recover_public_key(term_t t, RSAKEY** rsap)