-
Notifications
You must be signed in to change notification settings - Fork 0
/
modcryptography.c
6473 lines (5451 loc) · 226 KB
/
modcryptography.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
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019-2024 Damiano Mazzella
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY of ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES of MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION of CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT of OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "py/runtime.h"
#include "py/mperrno.h"
#include "py/objtype.h"
#include "py/objstr.h"
#include "py/objint.h"
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
#include "py/mpz.h"
#endif
#ifndef MBEDTLS_USER_CONFIG_FILE
#define MBEDTLS_USER_CONFIG_FILE "modcryptography_config.h"
#endif // MBEDTLS_USER_CONFIG_FILE
#if defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
#if MICROPY_HW_ENABLE_RNG
#include "rng.h"
#define rand() rng_get()
#endif // MICROPY_HW_ENABLE_RNG
#endif
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_MPZ
#error "MICROPY_LONGINT_IMPL must be MICROPY_LONGINT_IMPL_MPZ"
#endif
MP_DEFINE_EXCEPTION(InvalidSignature, Exception);
MP_DEFINE_EXCEPTION(AlreadyFinalized, Exception);
MP_DEFINE_EXCEPTION(NotYetFinalized, Exception);
MP_DEFINE_EXCEPTION(UnsupportedAlgorithm, Exception);
MP_DEFINE_EXCEPTION(InvalidKey, Exception);
MP_DEFINE_EXCEPTION(InvalidToken, Exception);
#define CHK_NE_GOTO(EC, ERR, LABEL) \
if ((EC) != (ERR)) \
{ \
goto LABEL; \
}
#define CHK_EQ_GOTO(EC, ERR, LABEL) \
if ((EC) == (ERR)) \
{ \
goto LABEL; \
}
static int mp_random(void *rng_state, byte *output, size_t len)
{
size_t use_len;
int rnd;
if (rng_state != NULL)
{
rng_state = NULL;
}
while (len > 0)
{
use_len = len;
if (use_len > sizeof(int))
use_len = sizeof(int);
rnd = rand();
memcpy(output, &rnd, use_len);
output += use_len;
len -= use_len;
}
return 0;
}
#include "mbedtls/x509.h"
#include "mbedtls/x509_crt.h"
#include "mbedtls/oid.h"
#include "mbedtls/sha1.h"
#include "mbedtls/sha256.h"
#include "mbedtls/sha512.h"
#include "mbedtls/cipher.h"
#include "mbedtls/gcm.h"
#include "mbedtls/aes.h"
#include "mbedtls/des.h"
#include "mbedtls/ecdh.h"
#include "mbedtls/asn1write.h"
#include "mbedtls/rsa.h"
#include "rsa_alt_helpers.h"
#include "BLAKE2/ref/blake2.h"
#include "c25519/src/edsign.h"
struct _mp_ec_ecdsa_t;
struct _mp_ec_ecdh_t;
struct _mp_ec_curve_t;
struct _mp_ec_public_numbers_t;
struct _mp_ec_private_numbers_t;
struct _mp_ec_public_key_t;
struct _mp_ec_private_key_t;
struct _mp_ed25519_public_key_t;
struct _mp_ed25519_private_key_t;
struct _mp_rsa_public_numbers_t;
struct _mp_rsa_private_numbers_t;
struct _mp_rsa_public_key_t;
struct _mp_rsa_private_key_t;
struct _mp_hash_algorithm_t;
struct _mp_hash_context_t;
struct _mp_hmac_context_t;
struct _mp_x509_certificate_t;
struct _mp_ciphers_aesgcm_t;
struct _mp_ciphers_cipher_t;
struct _mp_ciphers_cipher_encryptor_t;
struct _mp_ciphers_cipher_decryptor_t;
struct _mp_ciphers_algorithms_t;
struct _mp_ciphers_modes_cbc_t;
struct _mp_ciphers_modes_gcm_t;
struct _mp_util_prehashed_t;
struct _mp_padding_pkcs1v15_t;
struct _mp_padding_pss_t;
struct _mp_padding_oaep_t;
struct _mp_padding_mgf1_t;
struct _mp_twofactor_hotp_t;
struct _mp_twofactor_totp_t;
typedef struct _mp_ec_curve_t
{
mp_obj_base_t base;
mp_int_t ecp_group_id;
mp_obj_t p;
mp_obj_t a;
mp_obj_t b;
mp_obj_t n;
mp_obj_t G_x;
mp_obj_t G_y;
} mp_ec_curve_t;
typedef struct _mp_ec_public_numbers_t
{
mp_obj_base_t base;
struct _mp_ec_curve_t *curve;
mp_obj_t x;
mp_obj_t y;
struct _mp_ec_public_key_t *public_key;
} mp_ec_public_numbers_t;
typedef struct _mp_ec_private_numbers_t
{
mp_obj_base_t base;
struct _mp_ec_public_numbers_t *public_numbers;
mp_obj_t private_value;
struct _mp_ec_private_key_t *private_key;
} mp_ec_private_numbers_t;
typedef struct _mp_ec_public_key_t
{
mp_obj_base_t base;
struct _mp_ec_public_numbers_t *public_numbers;
mp_obj_t public_bytes;
} mp_ec_public_key_t;
typedef struct _mp_ec_private_key_t
{
mp_obj_base_t base;
struct _mp_ec_curve_t *curve;
struct _mp_ec_private_numbers_t *private_numbers;
struct _mp_ec_public_key_t *public_key;
mp_obj_t private_bytes;
} mp_ec_private_key_t;
typedef struct _mp_ed25519_public_key_t
{
mp_obj_base_t base;
mp_obj_t public_bytes;
} mp_ed25519_public_key_t;
typedef struct _mp_ed25519_private_key_t
{
mp_obj_base_t base;
struct _mp_ed25519_public_key_t *public_key;
mp_obj_t private_bytes;
} mp_ed25519_private_key_t;
typedef struct _mp_rsa_public_numbers_t
{
mp_obj_base_t base;
mp_obj_t e;
mp_obj_t n;
struct _mp_rsa_public_key_t *public_key;
} mp_rsa_public_numbers_t;
typedef struct _mp_rsa_private_numbers_t
{
mp_obj_base_t base;
mp_obj_t p;
mp_obj_t q;
mp_obj_t d;
mp_obj_t dmp1;
mp_obj_t dmq1;
mp_obj_t iqmp;
struct _mp_rsa_public_numbers_t *public_numbers;
struct _mp_rsa_private_key_t *private_key;
} mp_rsa_private_numbers_t;
typedef struct _mp_rsa_public_key_t
{
mp_obj_base_t base;
struct _mp_rsa_public_numbers_t *public_numbers;
mp_obj_t public_bytes;
} mp_rsa_public_key_t;
typedef struct _mp_rsa_private_key_t
{
mp_obj_base_t base;
struct _mp_rsa_private_numbers_t *private_numbers;
struct _mp_rsa_public_key_t *public_key;
mp_obj_t private_bytes;
} mp_rsa_private_key_t;
typedef struct _mp_hash_algorithm_t
{
mp_obj_base_t base;
mp_int_t md_type;
mp_int_t digest_size;
} mp_hash_algorithm_t;
typedef struct _mp_hash_context_t
{
mp_obj_base_t base;
struct _mp_hash_algorithm_t *algorithm;
vstr_t *data;
bool finalized;
} mp_hash_context_t;
typedef struct _mp_hmac_context_t
{
mp_obj_base_t base;
vstr_t *key;
vstr_t *data;
struct _mp_hash_context_t *hash_context;
bool finalized;
} mp_hmac_context_t;
typedef struct _mp_x509_certificate_t
{
mp_obj_base_t base;
mp_obj_t version;
mp_obj_t serial_number;
mp_obj_t not_valid_before;
mp_obj_t not_valid_after;
mp_obj_t subject;
mp_obj_t issuer;
mp_obj_t signature;
mp_obj_t signature_algorithm_oid;
struct _mp_hash_algorithm_t *signature_hash_algorithm;
mp_obj_t extensions;
mp_obj_t public_bytes;
struct _mp_ec_public_key_t *ec_public_key;
struct _mp_rsa_public_key_t *rsa_public_key;
mp_obj_t tbs_certificate_bytes;
} mp_x509_certificate_t;
typedef struct _mp_ciphers_aesgcm_t
{
mp_obj_base_t base;
vstr_t *key;
} mp_ciphers_aesgcm_t;
typedef struct _mp_ciphers_algorithms_t
{
mp_obj_base_t base;
vstr_t *key;
mp_int_t type;
} mp_ciphers_algorithms_t;
typedef struct _mp_ciphers_modes_cbc_t
{
mp_obj_base_t base;
vstr_t *initialization_vector;
} mp_ciphers_modes_cbc_t;
typedef struct _mp_ciphers_modes_gcm_t
{
mp_obj_base_t base;
vstr_t *initialization_vector;
vstr_t *tag;
mp_int_t min_tag_length;
} mp_ciphers_modes_gcm_t;
typedef struct _mp_ciphers_modes_ecb_t
{
mp_obj_base_t base;
} mp_ciphers_modes_ecb_t;
typedef struct _mp_ciphers_cipher_t
{
mp_obj_base_t base;
struct _mp_ciphers_algorithms_t *algorithm;
mp_obj_t mode;
mp_int_t mode_type;
struct _mp_ciphers_cipher_encryptor_t *encryptor;
struct _mp_ciphers_cipher_decryptor_t *decryptor;
} mp_ciphers_cipher_t;
typedef struct _mp_ciphers_cipher_encryptor_t
{
mp_obj_base_t base;
struct _mp_ciphers_cipher_t *cipher;
vstr_t *data;
vstr_t *aadata;
bool finalized;
} mp_ciphers_cipher_encryptor_t;
typedef struct _mp_ciphers_cipher_decryptor_t
{
mp_obj_base_t base;
struct _mp_ciphers_cipher_t *cipher;
vstr_t *data;
vstr_t *aadata;
bool finalized;
} mp_ciphers_cipher_decryptor_t;
typedef struct _mp_ec_ecdh_t
{
mp_obj_base_t base;
} mp_ec_ecdh_t;
typedef struct _mp_ec_ecdsa_t
{
mp_obj_base_t base;
struct _mp_hash_algorithm_t *algorithm;
} mp_ec_ecdsa_t;
typedef struct _mp_util_prehashed_t
{
mp_obj_base_t base;
struct _mp_hash_algorithm_t *algorithm;
} mp_util_prehashed_t;
typedef struct _mp_util_rfc6979_t
{
mp_obj_base_t base;
mp_obj_t msg;
mp_obj_t x;
mp_obj_t q;
mp_int_t qlen;
mp_int_t rlen;
struct _mp_hash_algorithm_t *algorithm;
} mp_util_rfc6979_t;
typedef struct _mp_padding_pkcs1v15_t
{
mp_obj_base_t base;
mp_obj_t name;
} mp_padding_pkcs1v15_t;
typedef struct _mp_padding_pss_t
{
mp_obj_base_t base;
mp_obj_t name;
struct _mp_padding_mgf1_t *mgf;
mp_int_t salt_length;
mp_int_t max_length;
} mp_padding_pss_t;
typedef struct _mp_padding_oaep_t
{
mp_obj_base_t base;
mp_obj_t name;
struct _mp_padding_mgf1_t *mgf;
struct _mp_hash_algorithm_t *algorithm;
mp_obj_t label;
} mp_padding_oaep_t;
typedef struct _mp_padding_mgf1_t
{
mp_obj_base_t base;
struct _mp_hash_algorithm_t *algorithm;
} mp_padding_mgf1_t;
typedef struct _mp_twofactor_hotp_t
{
mp_obj_base_t base;
mp_obj_t key;
mp_int_t length;
struct _mp_hash_algorithm_t *algorithm;
bool enforce_key_length;
} mp_twofactor_hotp_t;
typedef struct _mp_twofactor_totp_t
{
mp_obj_base_t base;
mp_obj_t key;
mp_int_t length;
struct _mp_hash_algorithm_t *algorithm;
mp_int_t time_step;
bool enforce_key_length;
} mp_twofactor_totp_t;
enum
{
CIPHER_ALGORITHM_AES = 1,
#ifdef MBEDTLS_DES_C
CIPHER_ALGORITHM_3DES = 2,
#endif
};
enum
{
CIPHER_MODE_CBC = 1,
CIPHER_MODE_GCM = 2,
CIPHER_MODE_ECB = 3,
};
enum
{
SERIALIZATION_ENCODING_DER = 1,
SERIALIZATION_ENCODING_PEM = 2,
SERIALIZATION_ENCODING_X962 = 3,
};
enum
{
MBEDTLS_MD_NONE_BLAKE2S = -1,
};
// constants for block protocol ioctl
#define BLOCKDEV_IOCTL_INIT (1)
#define BLOCKDEV_IOCTL_DEINIT (2)
#define BLOCKDEV_IOCTL_SYNC (3)
#define BLOCKDEV_IOCTL_BLOCK_COUNT (4)
#define BLOCKDEV_IOCTL_BLOCK_SIZE (5)
#define BLOCKDEV_IOCTL_BLOCK_ERASE (6)
static const mp_obj_type_t ec_ecdsa_type;
static const mp_obj_type_t ec_ecdh_type;
static const mp_obj_type_t ec_curve_secp256r1_type;
#ifdef MBEDTLS_ECP_DP_SECP384R1_ENABLED
static const mp_obj_type_t ec_curve_secp384r1_type;
#endif
#ifdef MBEDTLS_ECP_DP_SECP521R1_ENABLED
static const mp_obj_type_t ec_curve_secp521r1_type;
#endif
static const mp_obj_type_t ec_public_numbers_type;
static const mp_obj_type_t ec_private_numbers_type;
static const mp_obj_type_t ec_public_key_type;
static const mp_obj_type_t ec_private_key_type;
static const mp_obj_type_t ed25519_private_key_type;
static const mp_obj_type_t ed25519_public_key_type;
static const mp_obj_type_t ed25519_type;
static const mp_obj_type_t rsa_public_numbers_type;
static const mp_obj_type_t rsa_private_numbers_type;
static const mp_obj_type_t rsa_public_key_type;
static const mp_obj_type_t rsa_private_key_type;
static const mp_obj_type_t hash_algorithm_sha1_type;
static const mp_obj_type_t hash_algorithm_sha256_type;
static const mp_obj_type_t hash_algorithm_sha384_type;
static const mp_obj_type_t hash_algorithm_sha512_type;
static const mp_obj_type_t hash_algorithm_blake2s_type;
static const mp_obj_type_t hash_algorithm_prehashed_type;
static const mp_obj_type_t hash_context_type;
static const mp_obj_type_t hmac_context_type;
static const mp_obj_type_t x509_certificate_type;
static const mp_obj_type_t ciphers_aesgcm_type;
static const mp_obj_type_t ciphers_cipher_type;
static const mp_obj_type_t ciphers_cipher_encryptor_type;
static const mp_obj_type_t ciphers_cipher_decryptor_type;
static const mp_obj_type_t ciphers_algorithms_aes_type;
static const mp_obj_type_t ciphers_algorithms_3des_type;
static const mp_obj_type_t ciphers_modes_cbc_type;
static const mp_obj_type_t ciphers_modes_gcm_type;
static const mp_obj_type_t ciphers_modes_ecb_type;
#if 0
static const mp_obj_type_t utils_rfc6979_type;
#endif
static const mp_obj_type_t padding_pkcs1v15_type;
static const mp_obj_type_t padding_pss_type;
static const mp_obj_type_t padding_oaep_type;
static const mp_obj_type_t padding_mgf1_type;
static const mp_obj_type_t twofactor_hotp_type;
static const mp_obj_type_t twofactor_totp_type;
#ifdef STM32WB
#ifdef MBEDTLS_GCM_ALT || MBEDTLS_AES_ALT
void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
{
if (hcryp->Instance == AES1)
{
__HAL_RCC_AES1_CLK_ENABLE();
}
else if (hcryp->Instance == AES2)
{
__HAL_RCC_AES2_CLK_ENABLE();
}
}
void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
{
if (hcryp->Instance == AES1)
{
__HAL_RCC_AES1_FORCE_RESET();
__HAL_RCC_AES1_RELEASE_RESET();
__HAL_RCC_AES1_CLK_DISABLE();
}
else if (hcryp->Instance == AES2)
{
__HAL_RCC_AES2_FORCE_RESET();
__HAL_RCC_AES2_RELEASE_RESET();
__HAL_RCC_AES2_CLK_DISABLE();
}
}
#endif
#ifdef MBEDTLS_ECP_ALT
void HAL_PKA_MspInit(PKA_HandleTypeDef *hpka)
{
if (hpka->Instance == PKA)
{
__HAL_RCC_PKA_CLK_ENABLE();
}
}
void HAL_PKA_MspDeInit(PKA_HandleTypeDef *hpka)
{
if (hpka->Instance == PKA)
{
__HAL_RCC_PKA_FORCE_RESET();
__HAL_RCC_PKA_RELEASE_RESET();
__HAL_RCC_PKA_CLK_DISABLE();
}
}
#endif
#endif
#if 0
#define DEBUG_MICROPYTHON_NEXTCHR "\n:"
static inline void micropython_printh(const uint8_t *b, size_t l)
{
for (size_t i = 0; i < l; i++)
{
printf("%02X%c", b[i], DEBUG_MICROPYTHON_NEXTCHR[i < l - 1]);
}
}
static vstr_t *vstr_hexlify(vstr_t *vstr_out, const byte *in, size_t in_len)
{
vstr_init(vstr_out, in_len);
if (in != NULL && in_len)
{
for (mp_uint_t i = in_len; i--;)
{
byte d = (*in >> 4);
if (d > 9)
{
d += 'a' - '9' - 1;
}
vstr_add_char(vstr_out, d + '0');
d = (*in++ & 0xf);
if (d > 9)
{
d += 'a' - '9' - 1;
}
vstr_add_char(vstr_out, d + '0');
}
}
return vstr_out;
}
#endif
#if 0
static void print_exception(vstr_t *vstr_print, mp_obj_t exc)
{
mp_print_t print;
vstr_clear(vstr_print);
vstr_init_print(vstr_print, 16, &print);
if (mp_obj_is_exception_instance(exc))
{
size_t n, *values;
mp_obj_exception_get_traceback(exc, &n, &values);
if (n > 0)
{
assert(n % 3 == 0);
mp_print_str(&print, "Traceback (most recent call last):\n");
for (int i = n - 3; i >= 0; i -= 3)
{
#if MICROPY_ENABLE_SOURCE_LINE
mp_printf(&print, " File \"%q\", line %d", values[i], (int)values[i + 1]);
#else
mp_printf(&print, " File \"%q\"", values[i]);
#endif
// the block name can be NULL if it's unknown
qstr block = values[i + 2];
if (block == MP_QSTR_NULL)
{
mp_print_str(&print, "\n");
}
else
{
mp_printf(&print, ", in %q\n", block);
}
}
}
}
mp_obj_print_helper(&print, exc, PRINT_EXC);
mp_print_str(&print, "\n");
}
#endif
static mpz_t *mp_mpz_for_int(mp_obj_t arg, mpz_t *temp)
{
if (mp_obj_is_small_int(arg))
{
mpz_init_from_int(temp, MP_OBJ_SMALL_INT_VALUE(arg));
return temp;
}
else
{
mp_obj_int_t *arp_p = MP_OBJ_TO_PTR(arg);
return &(arp_p->mpz);
}
}
static vstr_t *vstr_new_from_mpz(const mpz_t *i)
{
size_t len = mp_int_format_size(mpz_max_num_bits(i), 10, NULL, '\0');
vstr_t *vstr = vstr_new(len);
size_t fmt_len = mpz_as_str_inpl(i, 10, NULL, 'a', '\0', vstr_str(vstr));
vstr_cut_tail_bytes(vstr, len - fmt_len);
vstr->len = fmt_len;
return vstr;
}
static mp_obj_t int_bit_length(mp_obj_t x)
{
mpz_t n_temp;
mpz_t *n = mp_mpz_for_int(x, &n_temp);
if (mpz_is_zero(n))
{
return mp_obj_new_int_from_uint(0);
}
mpz_t *dest = m_new_obj(mpz_t);
dest->neg = n->neg;
dest->fixed_dig = 0;
dest->alloc = n->alloc;
dest->len = n->len;
dest->dig = m_new(mpz_dig_t, n->alloc);
memcpy(dest->dig, n->dig, n->alloc * sizeof(mpz_dig_t));
mpz_abs_inpl(dest, dest);
mp_uint_t num_bits = 0;
while (dest->len > 0)
{
mpz_shr_inpl(dest, dest, 1);
num_bits++;
}
if (dest != NULL)
{
m_del(mpz_dig_t, dest->dig, dest->alloc);
m_del_obj(mpz_t, dest);
}
if (n == &n_temp)
{
mpz_deinit(n);
}
return mp_obj_new_int_from_ull(num_bits);
}
static MP_DEFINE_CONST_FUN_OBJ_1(mod_int_bit_length_obj, int_bit_length);
static MP_DEFINE_CONST_STATICMETHOD_OBJ(mod_static_int_bit_length_obj, MP_ROM_PTR(&mod_int_bit_length_obj));
static mp_obj_t cryptography_small_to_big_int(mp_obj_t arg)
{
if (!mp_obj_is_int(arg))
{
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("int required, got %s"), mp_obj_get_type_str(arg)));
}
if (mp_obj_is_small_int(arg))
{
mp_obj_int_t *o = mp_obj_int_new_mpz();
mpz_init_from_int(&o->mpz, MP_OBJ_SMALL_INT_VALUE(arg));
return MP_OBJ_FROM_PTR(o);
}
return arg;
}
static void cryptography_get_buffer(const mp_obj_t o, bool big_endian, mp_buffer_info_t *bufinfo)
{
mp_obj_t oo = o;
if (mp_obj_is_int(oo))
{
mpz_t o_temp;
mpz_t *o_temp_p = mp_mpz_for_int(o, &o_temp);
bool is_neg = mpz_is_neg(o_temp_p);
if (is_neg)
{
mpz_abs_inpl(o_temp_p, o_temp_p);
}
vstr_t vstr;
vstr_init_len(&vstr, (mp_obj_get_int(int_bit_length(oo)) + 7) / 8);
mpz_as_bytes(o_temp_p, big_endian, is_neg, vstr.len, (byte *)vstr.buf);
if (is_neg)
{
mpz_neg_inpl(o_temp_p, o_temp_p);
}
if (o_temp_p == &o_temp)
{
mpz_deinit(o_temp_p);
}
oo = mp_obj_new_bytes((byte *)vstr.buf, vstr.len);
vstr_clear(&vstr);
}
if (!mp_get_buffer(oo, bufinfo, MP_BUFFER_READ))
{
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("object with buffer protocol or int required, got %s"), mp_obj_get_type_str(oo)));
}
}
static void mbedtls_mpi_read_binary_from_mp_obj(mbedtls_mpi *mpi, const mp_obj_t o, bool big_endian)
{
mp_buffer_info_t bufinfo_o;
cryptography_get_buffer(o, big_endian, &bufinfo_o);
if (big_endian)
{
mbedtls_mpi_read_binary(mpi, (const byte *)bufinfo_o.buf, bufinfo_o.len);
}
else
{
mbedtls_mpi_read_binary_le(mpi, (const byte *)bufinfo_o.buf, bufinfo_o.len);
}
}
static mp_obj_t mbedtls_mpi_write_binary_to_mp_obj(const mbedtls_mpi *mpi, bool big_endian)
{
vstr_t vstr_mpi;
vstr_init_len(&vstr_mpi, mbedtls_mpi_size(mpi));
if (big_endian)
{
mbedtls_mpi_write_binary(mpi, (byte *)vstr_mpi.buf, vstr_mpi.len);
}
else
{
mbedtls_mpi_write_binary_le(mpi, (byte *)vstr_mpi.buf, vstr_mpi.len);
}
mp_obj_t oo = mp_obj_int_from_bytes_impl(big_endian, vstr_mpi.len, (const byte *)vstr_mpi.buf);
vstr_clear(&vstr_mpi);
return oo;
}
static uint8_t constant_time_bytes_eq(uint8_t *a, size_t len_a, uint8_t *b, size_t len_b)
{
size_t i = 0;
uint8_t mismatch = 0;
if (len_a != len_b)
{
return 0;
}
for (i = 0; i < len_a; i++)
{
mismatch |= a[i] ^ b[i];
}
mismatch |= mismatch >> 4;
mismatch |= mismatch >> 2;
mismatch |= mismatch >> 1;
return (mismatch & 1) == 0;
}
static mp_obj_t mod_constant_time_bytes_eq(mp_obj_t a, mp_obj_t b)
{
mp_buffer_info_t bufinfo_a;
mp_get_buffer_raise(a, &bufinfo_a, MP_BUFFER_READ);
mp_buffer_info_t bufinfo_b;
mp_get_buffer_raise(b, &bufinfo_b, MP_BUFFER_READ);
return mp_obj_new_bool(constant_time_bytes_eq(bufinfo_a.buf, bufinfo_a.len, bufinfo_b.buf, bufinfo_b.len));
}
static MP_DEFINE_CONST_FUN_OBJ_2(mod_constant_time_bytes_eq_obj, mod_constant_time_bytes_eq);
static MP_DEFINE_CONST_STATICMETHOD_OBJ(mod_static_constant_time_bytes_eq_obj, MP_ROM_PTR(&mod_constant_time_bytes_eq_obj));
static int util_decode_dss_signature(const unsigned char *sig, size_t slen, mbedtls_mpi *r, mbedtls_mpi *s)
{
int ret;
unsigned char *p = (unsigned char *)sig;
const unsigned char *end = sig + slen;
size_t len;
if(sig == NULL) {
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}
if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0)
{
ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}
if (p + len != end)
{
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA + MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
goto cleanup;
}
if ((ret = mbedtls_asn1_get_mpi(&p, end, r)) != 0 || (ret = mbedtls_asn1_get_mpi(&p, end, s)) != 0)
{
ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}
if (p != end)
{
ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
}
cleanup:
return (ret);
}
static mp_obj_t mod_decode_dss_signature(mp_obj_t signature_obj)
{
mp_buffer_info_t bufinfo_signature;
mp_get_buffer_raise(signature_obj, &bufinfo_signature, MP_BUFFER_READ);
mbedtls_mpi r;
mbedtls_mpi_init(&r);
mbedtls_mpi s;
mbedtls_mpi_init(&s);
util_decode_dss_signature(bufinfo_signature.buf, bufinfo_signature.len, &r, &s);
mp_obj_t rs[2] = {mbedtls_mpi_write_binary_to_mp_obj(&r, true), mbedtls_mpi_write_binary_to_mp_obj(&s, true)};
mbedtls_mpi_free(&r);
mbedtls_mpi_free(&s);
return mp_obj_new_tuple(2, rs);
}
static MP_DEFINE_CONST_FUN_OBJ_1(mod_decode_dss_signature_obj, mod_decode_dss_signature);
static MP_DEFINE_CONST_STATICMETHOD_OBJ(mod_static_decode_dss_signature_obj, MP_ROM_PTR(&mod_decode_dss_signature_obj));
static int util_encode_dss_signature(const mbedtls_mpi *r, const mbedtls_mpi *s, unsigned char *sig, size_t *slen)
{
int ret;
unsigned char buf[MBEDTLS_ECDSA_MAX_LEN] = {0};
unsigned char *p = buf + sizeof(buf);
size_t len = 0;
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, (const byte *)buf, s));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, (const byte *)buf, r));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
memcpy(sig, p, len);
*slen = len;
return (0);
}
static mp_obj_t mod_encode_dss_signature(mp_obj_t r_obj, mp_obj_t s_obj)
{
mbedtls_mpi r;
mbedtls_mpi_init(&r);
mbedtls_mpi_read_binary_from_mp_obj(&r, r_obj, true);
mbedtls_mpi s;
mbedtls_mpi_init(&s);
mbedtls_mpi_read_binary_from_mp_obj(&s, s_obj, true);
vstr_t vstr_sig;
vstr_init_len(&vstr_sig, MBEDTLS_ECDSA_MAX_LEN);
size_t size_sig = 0;
int res = util_encode_dss_signature(&r, &s, (byte *)vstr_sig.buf, &size_sig);
mbedtls_mpi_free(&r);
mbedtls_mpi_free(&s);
if (res != 0)
{
mp_raise_ValueError(MP_ERROR_TEXT("signature malformed"));
}
mp_obj_t oo = mp_obj_new_bytes((const byte *)vstr_sig.buf, size_sig);
vstr_clear(&vstr_sig);
return oo;
}
static MP_DEFINE_CONST_FUN_OBJ_2(mod_encode_dss_signature_obj, mod_encode_dss_signature);
static MP_DEFINE_CONST_STATICMETHOD_OBJ(mod_static_encode_dss_signature_obj, MP_ROM_PTR(&mod_encode_dss_signature_obj));
static mp_obj_t ec_ecdsa_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args)
{
mp_arg_check_num(n_args, n_kw, 1, 1, true);
enum
{
ARG_hash_algorithm
};
static const mp_arg_t allowed_args[] = {
{MP_QSTR_hash_algorithm, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_obj_t hash_algorithm = args[ARG_hash_algorithm].u_obj;
if (!mp_obj_is_type(hash_algorithm, &hash_algorithm_sha1_type) && !mp_obj_is_type(hash_algorithm, &hash_algorithm_sha256_type) && !mp_obj_is_type(hash_algorithm, &hash_algorithm_sha384_type) && !mp_obj_is_type(hash_algorithm, &hash_algorithm_sha512_type) && !mp_obj_is_type(hash_algorithm, &hash_algorithm_prehashed_type) && !(mp_obj_get_type(hash_algorithm) == &mp_type_NoneType))
{
mp_raise_msg(&mp_type_UnsupportedAlgorithm, MP_ERROR_TEXT("Expected instance of hashes algorithm or None"));
}
mp_ec_ecdsa_t *ECDSA = m_new_obj(mp_ec_ecdsa_t);
ECDSA->base.type = &ec_ecdsa_type;
ECDSA->algorithm = hash_algorithm;
return MP_OBJ_FROM_PTR(ECDSA);
}
static void ec_ecdsa_attr(mp_obj_t obj, qstr attr, mp_obj_t *dest)
{
mp_ec_ecdsa_t *self = MP_OBJ_TO_PTR(obj);
if (dest[0] == MP_OBJ_NULL)
{
const mp_obj_type_t *type = mp_obj_get_type(obj);
mp_map_t *locals_map = (mp_map_t *)mp_obj_dict_get_map(MP_OBJ_TYPE_GET_SLOT(type, locals_dict));
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
if (elem != NULL)
{
if (attr == MP_QSTR_algorithm || attr == MP_QSTR__algorithm)
{
dest[0] = self->algorithm;
return;
}
mp_convert_member_lookup(obj, type, elem->value, dest);
}
}
}
static const mp_rom_map_elem_t ec_ecdsa_locals_dict_table[] = {
{MP_ROM_QSTR(MP_QSTR_algorithm), MP_ROM_PTR(mp_const_none)},
{MP_ROM_QSTR(MP_QSTR__algorithm), MP_ROM_PTR(mp_const_none)},
};
static MP_DEFINE_CONST_DICT(ec_ecdsa_locals_dict, ec_ecdsa_locals_dict_table);
static MP_DEFINE_CONST_OBJ_TYPE(
ec_ecdsa_type,
MP_QSTR_ECDSA,
MP_TYPE_FLAG_NONE,
make_new, ec_ecdsa_make_new,
attr, ec_ecdsa_attr,
locals_dict, &ec_ecdsa_locals_dict);
static mp_obj_t ec_ecdh_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
{
mp_arg_check_num(n_args, n_kw, 0, 1, true);
mp_ec_ecdh_t *ECDH = m_new_obj(mp_ec_ecdh_t);
ECDH->base.type = &ec_ecdh_type;
return MP_OBJ_FROM_PTR(ECDH);
}
static MP_DEFINE_CONST_OBJ_TYPE(
ec_ecdh_type,
MP_QSTR_ECDH,
MP_TYPE_FLAG_NONE,
make_new, ec_ecdh_make_new);
static mp_obj_t ec_parse_keypair(const mbedtls_ecp_keypair *ecp_keypair, bool private)
{
mp_ec_curve_t *EllipticCurve = m_new_obj(mp_ec_curve_t);