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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
|
#!/usr/bin/python
"""
Python wrapper of the botan crypto library
https://botan.randombit.net
(C) 2015,2017,2018,2019 Jack Lloyd
(C) 2015 Uri Blumenthal (extensions and patches)
Botan is released under the Simplified BSD License (see license.txt)
This module uses the ctypes module and is usable by programs running
under at least CPython 2.7, CPython 3.x, and PyPy
It uses botan's ffi module, which exposes a C API. This version of the
module requires FFI API version 20180713, which was introduced in
Botan 2.8
"""
from ctypes import CDLL, POINTER, byref, create_string_buffer, \
c_void_p, c_size_t, c_uint8, c_uint32, c_uint64, c_int, c_uint, c_char_p
from sys import version_info, platform
from time import strptime, mktime
from binascii import hexlify
from datetime import datetime
BOTAN_FFI_VERSION = 20180713
#
# Base exception for all exceptions raised from this module
#
class BotanException(Exception):
def __init__(self, message, rc=0):
self.__rc = rc
if rc == 0:
super(BotanException, self).__init__(message)
else:
descr = _DLL.botan_error_description(rc).decode('ascii')
super(BotanException, self).__init__("%s: %d (%s)" % (message, rc, descr))
def error_code(self):
return self.__rc
#
# Module initialization
#
def _load_botan_dll(expected_version):
possible_dll_names = []
if platform in ['win32', 'cygwin', 'msys']:
possible_dll_names.append('botan.dll')
elif platform in ['darwin', 'macos']:
possible_dll_names.append('libbotan-2.dylib')
else:
# assumed to be some Unix/Linux system
possible_dll_names.append('libbotan-2.so')
possible_dll_names += ['libbotan-2.so.%d' % (v) for v in reversed(range(8, 16))]
for dll_name in possible_dll_names:
try:
dll = CDLL(dll_name)
dll.botan_ffi_supports_api.argtypes = [c_uint32]
dll.botan_ffi_supports_api.restype = c_int
if dll.botan_ffi_supports_api(expected_version) == 0:
return dll
except OSError:
pass
raise BotanException("Could not find a usable Botan shared object library")
def _errcheck(rc, fn, _args):
# This errcheck should only be used for int-returning functions
assert isinstance(rc, int)
if rc >= 0 or rc in fn.allowed_errors:
return rc
raise BotanException('%s failed' % (fn.__name__), rc)
def _set_prototypes(dll):
# pylint: disable=too-many-statements,line-too-long
def ffi_api(fn, args, allowed_errors=None):
if allowed_errors is None:
allowed_errors = [-10]
fn.argtypes = args
fn.restype = c_int
fn.errcheck = _errcheck
fn.allowed_errors = allowed_errors
dll.botan_version_string.argtypes = []
dll.botan_version_string.restype = c_char_p
dll.botan_version_string.argtypes = []
dll.botan_version_string.restype = c_char_p
dll.botan_version_major.argtypes = []
dll.botan_version_major.restype = c_uint32
dll.botan_version_minor.argtypes = []
dll.botan_version_minor.restype = c_uint32
dll.botan_version_patch.argtypes = []
dll.botan_version_patch.restype = c_uint32
dll.botan_ffi_api_version.argtypes = []
dll.botan_ffi_api_version.restype = c_uint32
dll.botan_error_description.argtypes = [c_int]
dll.botan_error_description.restype = c_char_p
# These are generated using src/scripts/ffi_decls.py:
ffi_api(dll.botan_constant_time_compare, [c_void_p, c_void_p, c_size_t], [-1])
ffi_api(dll.botan_scrub_mem, [c_void_p, c_size_t])
ffi_api(dll.botan_hex_encode, [c_char_p, c_size_t, c_char_p, c_uint32])
ffi_api(dll.botan_hex_decode, [c_char_p, c_size_t, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_base64_encode, [c_char_p, c_size_t, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_base64_decode, [c_char_p, c_size_t, c_char_p, POINTER(c_size_t)])
# RNG
ffi_api(dll.botan_rng_init, [c_void_p, c_char_p])
ffi_api(dll.botan_rng_get, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_rng_reseed, [c_void_p, c_size_t])
ffi_api(dll.botan_rng_reseed_from_rng, [c_void_p, c_void_p, c_size_t])
ffi_api(dll.botan_rng_add_entropy, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_rng_destroy, [c_void_p])
# HASH
ffi_api(dll.botan_hash_init, [c_void_p, c_char_p, c_uint32])
ffi_api(dll.botan_hash_copy_state, [c_void_p, c_void_p])
ffi_api(dll.botan_hash_output_length, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_hash_block_size, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_hash_update, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_hash_final, [c_void_p, c_char_p])
ffi_api(dll.botan_hash_clear, [c_void_p])
ffi_api(dll.botan_hash_destroy, [c_void_p])
ffi_api(dll.botan_hash_name, [c_void_p, c_char_p, POINTER(c_size_t)])
# MAC
ffi_api(dll.botan_mac_init, [c_void_p, c_char_p, c_uint32])
ffi_api(dll.botan_mac_output_length, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_mac_set_key, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_mac_update, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_mac_final, [c_void_p, c_char_p])
ffi_api(dll.botan_mac_clear, [c_void_p])
ffi_api(dll.botan_mac_name, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_mac_get_keyspec, [c_void_p, POINTER(c_size_t), POINTER(c_size_t), POINTER(c_size_t)])
ffi_api(dll.botan_mac_destroy, [c_void_p])
# CIPHER
ffi_api(dll.botan_cipher_init, [c_void_p, c_char_p, c_uint32])
ffi_api(dll.botan_cipher_name, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_cipher_output_length, [c_void_p, c_size_t, POINTER(c_size_t)])
ffi_api(dll.botan_cipher_valid_nonce_length, [c_void_p, c_size_t])
ffi_api(dll.botan_cipher_get_tag_length, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_cipher_get_default_nonce_length, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_cipher_get_update_granularity, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_cipher_query_keylen, [c_void_p, POINTER(c_size_t), POINTER(c_size_t)])
ffi_api(dll.botan_cipher_get_keyspec, [c_void_p, POINTER(c_size_t), POINTER(c_size_t), POINTER(c_size_t)])
ffi_api(dll.botan_cipher_set_key, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_cipher_reset, [c_void_p])
ffi_api(dll.botan_cipher_set_associated_data, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_cipher_start, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_cipher_update,
[c_void_p, c_uint32, c_char_p, c_size_t, POINTER(c_size_t), c_char_p, c_size_t, POINTER(c_size_t)])
ffi_api(dll.botan_cipher_clear, [c_void_p])
ffi_api(dll.botan_cipher_destroy, [c_void_p])
ffi_api(dll.botan_pbkdf,
[c_char_p, c_char_p, c_size_t, c_char_p, c_char_p, c_size_t, c_size_t])
ffi_api(dll.botan_pbkdf_timed,
[c_char_p, c_char_p, c_size_t, c_char_p, c_char_p, c_size_t, c_size_t, POINTER(c_size_t)])
ffi_api(dll.botan_pwdhash,
[c_char_p, c_size_t, c_size_t, c_size_t, c_char_p, c_size_t, c_char_p, c_size_t, c_char_p, c_size_t])
ffi_api(dll.botan_pwdhash_timed,
[c_char_p, c_uint32, POINTER(c_size_t), POINTER(c_size_t), POINTER(c_size_t), c_char_p, c_size_t, c_char_p, c_size_t, c_char_p, c_size_t])
ffi_api(dll.botan_scrypt,
[c_char_p, c_size_t, c_char_p, c_char_p, c_size_t, c_size_t, c_size_t, c_size_t])
ffi_api(dll.botan_kdf,
[c_char_p, c_char_p, c_size_t, c_char_p, c_size_t, c_char_p, c_size_t, c_char_p, c_size_t])
# BLOCK
ffi_api(dll.botan_block_cipher_init, [c_void_p, c_char_p])
ffi_api(dll.botan_block_cipher_destroy, [c_void_p])
ffi_api(dll.botan_block_cipher_clear, [c_void_p])
ffi_api(dll.botan_block_cipher_set_key, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_block_cipher_block_size, [c_void_p])
ffi_api(dll.botan_block_cipher_encrypt_blocks, [c_void_p, c_char_p, c_char_p, c_size_t])
ffi_api(dll.botan_block_cipher_decrypt_blocks, [c_void_p, c_char_p, c_char_p, c_size_t])
ffi_api(dll.botan_block_cipher_name, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_block_cipher_get_keyspec, [c_void_p, POINTER(c_size_t), POINTER(c_size_t), POINTER(c_size_t)])
# MP
ffi_api(dll.botan_mp_init, [c_void_p])
ffi_api(dll.botan_mp_destroy, [c_void_p])
ffi_api(dll.botan_mp_to_hex, [c_void_p, c_char_p])
ffi_api(dll.botan_mp_to_str, [c_void_p, c_uint8, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_mp_clear, [c_void_p])
ffi_api(dll.botan_mp_set_from_int, [c_void_p, c_int])
ffi_api(dll.botan_mp_set_from_mp, [c_void_p, c_void_p])
ffi_api(dll.botan_mp_set_from_str, [c_void_p, c_char_p])
ffi_api(dll.botan_mp_set_from_radix_str, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_mp_num_bits, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_mp_num_bytes, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_mp_to_bin, [c_void_p, c_char_p])
ffi_api(dll.botan_mp_from_bin, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_mp_to_uint32, [c_void_p, POINTER(c_uint32)])
ffi_api(dll.botan_mp_is_positive, [c_void_p])
ffi_api(dll.botan_mp_is_negative, [c_void_p])
ffi_api(dll.botan_mp_flip_sign, [c_void_p])
ffi_api(dll.botan_mp_is_zero, [c_void_p])
ffi_api(dll.botan_mp_is_odd, [c_void_p])
ffi_api(dll.botan_mp_is_even, [c_void_p])
ffi_api(dll.botan_mp_add_u32, [c_void_p, c_void_p, c_uint32])
ffi_api(dll.botan_mp_sub_u32, [c_void_p, c_void_p, c_uint32])
ffi_api(dll.botan_mp_add, [c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_mp_sub, [c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_mp_mul, [c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_mp_div, [c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_mp_mod_mul, [c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_mp_equal, [c_void_p, c_void_p])
ffi_api(dll.botan_mp_cmp, [POINTER(c_int), c_void_p, c_void_p])
ffi_api(dll.botan_mp_swap, [c_void_p, c_void_p])
ffi_api(dll.botan_mp_powmod, [c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_mp_lshift, [c_void_p, c_void_p, c_size_t])
ffi_api(dll.botan_mp_rshift, [c_void_p, c_void_p, c_size_t])
ffi_api(dll.botan_mp_mod_inverse, [c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_mp_rand_bits, [c_void_p, c_void_p, c_size_t])
ffi_api(dll.botan_mp_rand_range, [c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_mp_gcd, [c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_mp_is_prime, [c_void_p, c_void_p, c_size_t])
ffi_api(dll.botan_mp_get_bit, [c_void_p, c_size_t])
ffi_api(dll.botan_mp_set_bit, [c_void_p, c_size_t])
ffi_api(dll.botan_mp_clear_bit, [c_void_p, c_size_t])
ffi_api(dll.botan_bcrypt_generate,
[c_char_p, POINTER(c_size_t), c_char_p, c_void_p, c_size_t, c_uint32])
ffi_api(dll.botan_bcrypt_is_valid, [c_char_p, c_char_p])
# PUBKEY
ffi_api(dll.botan_privkey_create, [c_void_p, c_char_p, c_char_p, c_void_p])
ffi_api(dll.botan_privkey_check_key, [c_void_p, c_void_p, c_uint32], [-1])
ffi_api(dll.botan_privkey_create_rsa, [c_void_p, c_void_p, c_size_t])
ffi_api(dll.botan_privkey_create_ecdsa, [c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_privkey_create_ecdh, [c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_privkey_create_mceliece, [c_void_p, c_void_p, c_size_t, c_size_t])
ffi_api(dll.botan_privkey_create_dh, [c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_privkey_create_dsa, [c_void_p, c_void_p, c_size_t, c_size_t])
ffi_api(dll.botan_privkey_create_elgamal, [c_void_p, c_void_p, c_size_t, c_size_t])
ffi_api(dll.botan_privkey_load,
[c_void_p, c_void_p, c_char_p, c_size_t, c_char_p])
ffi_api(dll.botan_privkey_destroy, [c_void_p])
ffi_api(dll.botan_privkey_export, [c_void_p, c_char_p, POINTER(c_size_t), c_uint32])
ffi_api(dll.botan_privkey_algo_name, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_privkey_export_encrypted,
[c_void_p, c_char_p, POINTER(c_size_t), c_void_p, c_char_p, c_char_p, c_uint32])
ffi_api(dll.botan_privkey_export_encrypted_pbkdf_msec,
[c_void_p, c_char_p, POINTER(c_size_t), c_void_p, c_char_p, c_uint32, POINTER(c_size_t), c_char_p, c_char_p, c_uint32])
ffi_api(dll.botan_privkey_export_encrypted_pbkdf_iter,
[c_void_p, c_char_p, POINTER(c_size_t), c_void_p, c_char_p, c_size_t, c_char_p, c_char_p, c_uint32])
ffi_api(dll.botan_privkey_export_pubkey, [c_void_p, c_void_p])
ffi_api(dll.botan_pubkey_load, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_pubkey_export, [c_void_p, c_char_p, POINTER(c_size_t), c_uint32])
ffi_api(dll.botan_pubkey_algo_name, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_pubkey_check_key, [c_void_p, c_void_p, c_uint32], [-1])
ffi_api(dll.botan_pubkey_estimated_strength, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_pubkey_fingerprint, [c_void_p, c_char_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_pubkey_destroy, [c_void_p])
ffi_api(dll.botan_pubkey_get_field, [c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_privkey_get_field, [c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_privkey_load_rsa, [c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_privkey_load_rsa_pkcs1, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_privkey_rsa_get_p, [c_void_p, c_void_p])
ffi_api(dll.botan_privkey_rsa_get_q, [c_void_p, c_void_p])
ffi_api(dll.botan_privkey_rsa_get_d, [c_void_p, c_void_p])
ffi_api(dll.botan_privkey_rsa_get_n, [c_void_p, c_void_p])
ffi_api(dll.botan_privkey_rsa_get_e, [c_void_p, c_void_p])
ffi_api(dll.botan_privkey_rsa_get_privkey, [c_void_p, c_char_p, POINTER(c_size_t), c_uint32])
ffi_api(dll.botan_pubkey_load_rsa, [c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_pubkey_rsa_get_e, [c_void_p, c_void_p])
ffi_api(dll.botan_pubkey_rsa_get_n, [c_void_p, c_void_p])
ffi_api(dll.botan_privkey_load_dsa,
[c_void_p, c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_pubkey_load_dsa,
[c_void_p, c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_privkey_dsa_get_x, [c_void_p, c_void_p])
ffi_api(dll.botan_pubkey_dsa_get_p, [c_void_p, c_void_p])
ffi_api(dll.botan_pubkey_dsa_get_q, [c_void_p, c_void_p])
ffi_api(dll.botan_pubkey_dsa_get_g, [c_void_p, c_void_p])
ffi_api(dll.botan_pubkey_dsa_get_y, [c_void_p, c_void_p])
ffi_api(dll.botan_privkey_load_dh, [c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_pubkey_load_dh, [c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_pubkey_load_elgamal, [c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_privkey_load_elgamal, [c_void_p, c_void_p, c_void_p, c_void_p])
ffi_api(dll.botan_privkey_load_ed25519, [c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_load_ed25519, [c_void_p, c_char_p])
ffi_api(dll.botan_privkey_ed25519_get_privkey, [c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_ed25519_get_pubkey, [c_void_p, c_char_p])
ffi_api(dll.botan_privkey_load_x25519, [c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_load_x25519, [c_void_p, c_char_p])
ffi_api(dll.botan_privkey_x25519_get_privkey, [c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_x25519_get_pubkey, [c_void_p, c_char_p])
ffi_api(dll.botan_privkey_load_ecdsa, [c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_load_ecdsa, [c_void_p, c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_load_ecdh, [c_void_p, c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_privkey_load_ecdh, [c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_load_sm2, [c_void_p, c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_privkey_load_sm2, [c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_load_sm2_enc, [c_void_p, c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_privkey_load_sm2_enc, [c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_sm2_compute_za,
[c_char_p, POINTER(c_size_t), c_char_p, c_char_p, c_void_p])
# PK
ffi_api(dll.botan_pk_op_encrypt_create, [c_void_p, c_void_p, c_char_p, c_uint32])
ffi_api(dll.botan_pk_op_encrypt_destroy, [c_void_p])
ffi_api(dll.botan_pk_op_encrypt_output_length, [c_void_p, c_size_t, POINTER(c_size_t)])
ffi_api(dll.botan_pk_op_encrypt,
[c_void_p, c_void_p, c_char_p, POINTER(c_size_t), c_char_p, c_size_t])
ffi_api(dll.botan_pk_op_decrypt_create, [c_void_p, c_void_p, c_char_p, c_uint32])
ffi_api(dll.botan_pk_op_decrypt_destroy, [c_void_p])
ffi_api(dll.botan_pk_op_decrypt_output_length, [c_void_p, c_size_t, POINTER(c_size_t)])
ffi_api(dll.botan_pk_op_decrypt,
[c_void_p, c_char_p, POINTER(c_size_t), c_char_p, c_size_t])
ffi_api(dll.botan_pk_op_sign_create, [c_void_p, c_void_p, c_char_p, c_uint32])
ffi_api(dll.botan_pk_op_sign_destroy, [c_void_p])
ffi_api(dll.botan_pk_op_sign_output_length, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_pk_op_sign_update, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_pk_op_sign_finish, [c_void_p, c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_pk_op_verify_create, [c_void_p, c_void_p, c_char_p, c_uint32])
ffi_api(dll.botan_pk_op_verify_destroy, [c_void_p])
ffi_api(dll.botan_pk_op_verify_update, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_pk_op_verify_finish, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_pk_op_key_agreement_create, [c_void_p, c_void_p, c_char_p, c_uint32])
ffi_api(dll.botan_pk_op_key_agreement_destroy, [c_void_p])
ffi_api(dll.botan_pk_op_key_agreement_export_public, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_pk_op_key_agreement_size, [c_void_p, POINTER(c_size_t)])
ffi_api(dll.botan_pk_op_key_agreement,
[c_void_p, c_char_p, POINTER(c_size_t), c_char_p, c_size_t, c_char_p, c_size_t])
ffi_api(dll.botan_pkcs_hash_id, [c_char_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_mceies_encrypt,
[c_void_p, c_void_p, c_char_p, c_char_p, c_size_t, c_char_p, c_size_t, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_mceies_decrypt,
[c_void_p, c_char_p, c_char_p, c_size_t, c_char_p, c_size_t, c_char_p, POINTER(c_size_t)])
# X509
ffi_api(dll.botan_x509_cert_load, [c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_x509_cert_load_file, [c_void_p, c_char_p])
ffi_api(dll.botan_x509_cert_destroy, [c_void_p])
ffi_api(dll.botan_x509_cert_dup, [c_void_p, c_void_p])
ffi_api(dll.botan_x509_cert_get_time_starts, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_x509_cert_get_time_expires, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_x509_cert_not_before, [c_void_p, POINTER(c_uint64)])
ffi_api(dll.botan_x509_cert_not_after, [c_void_p, POINTER(c_uint64)])
ffi_api(dll.botan_x509_cert_get_fingerprint, [c_void_p, c_char_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_x509_cert_get_serial_number, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_x509_cert_get_authority_key_id, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_x509_cert_get_subject_key_id, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_x509_cert_get_public_key_bits, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_x509_cert_get_public_key, [c_void_p, c_void_p])
ffi_api(dll.botan_x509_cert_get_issuer_dn,
[c_void_p, c_char_p, c_size_t, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_x509_cert_get_subject_dn,
[c_void_p, c_char_p, c_size_t, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_x509_cert_to_string, [c_void_p, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_x509_cert_allowed_usage, [c_void_p, c_uint])
ffi_api(dll.botan_x509_cert_hostname_match, [c_void_p, c_char_p])
ffi_api(dll.botan_x509_cert_verify,
[POINTER(c_int), c_void_p, c_void_p, c_size_t, c_void_p, c_size_t, c_char_p, c_size_t, c_char_p, c_uint64])
dll.botan_x509_cert_validation_status.argtypes = [c_int]
dll.botan_x509_cert_validation_status.restype = c_char_p
ffi_api(dll.botan_key_wrap3394,
[c_char_p, c_size_t, c_char_p, c_size_t, c_char_p, POINTER(c_size_t)])
ffi_api(dll.botan_key_unwrap3394,
[c_char_p, c_size_t, c_char_p, c_size_t, c_char_p, POINTER(c_size_t)])
# HOTP
ffi_api(dll.botan_hotp_init,
[c_void_p, c_char_p, c_size_t, c_char_p, c_size_t])
ffi_api(dll.botan_hotp_destroy, [c_void_p])
ffi_api(dll.botan_hotp_generate, [c_void_p, POINTER(c_uint32), c_uint64])
ffi_api(dll.botan_hotp_check,
[c_void_p, POINTER(c_uint64), c_uint32, c_uint64, c_size_t])
# TOTP
ffi_api(dll.botan_totp_init,
[c_void_p, c_char_p, c_size_t, c_char_p, c_size_t, c_size_t])
ffi_api(dll.botan_totp_destroy, [c_void_p])
ffi_api(dll.botan_totp_generate, [c_void_p, POINTER(c_uint32), c_uint64])
ffi_api(dll.botan_totp_check, [c_void_p, c_uint32, c_uint64, c_size_t])
# FPE
ffi_api(dll.botan_fpe_fe1_init,
[c_void_p, c_void_p, c_char_p, c_size_t, c_size_t, c_uint32])
ffi_api(dll.botan_fpe_destroy, [c_void_p])
ffi_api(dll.botan_fpe_encrypt, [c_void_p, c_void_p, c_char_p, c_size_t])
ffi_api(dll.botan_fpe_decrypt, [c_void_p, c_void_p, c_char_p, c_size_t])
return dll
#
# Load the DLL and set prototypes on it
#
_DLL = _set_prototypes(_load_botan_dll(BOTAN_FFI_VERSION))
#
# Internal utilities
#
def _call_fn_returning_sz(fn):
sz = c_size_t(0)
fn(byref(sz))
return int(sz.value)
def _call_fn_returning_vec(guess, fn):
buf = create_string_buffer(guess)
buf_len = c_size_t(len(buf))
rc = fn(buf, byref(buf_len))
if rc == -10 and buf_len.value > len(buf):
return _call_fn_returning_vec(buf_len.value, fn)
assert buf_len.value <= len(buf)
return buf.raw[0:int(buf_len.value)]
def _call_fn_returning_str(guess, fn):
# Assumes that anything called with this is returning plain ASCII strings
# (base64 data, algorithm names, etc)
v = _call_fn_returning_vec(guess, fn)
return v.decode('ascii')[:-1]
def _ctype_str(s):
if s is None:
return None
assert isinstance(s, str)
if version_info[0] < 3:
return s
else:
return s.encode('utf-8')
def _ctype_to_str(s):
if version_info[0] < 3:
return s.encode('utf-8')
else:
return s.decode('utf-8')
def _ctype_bits(s):
if version_info[0] < 3:
if isinstance(s, str):
return s
elif isinstance(s, unicode): # pylint: disable=undefined-variable
return s.decode('utf-8')
else:
raise Exception("Internal error - unexpected type %s provided to _ctype_bits" % (type(s).__name__))
else:
if isinstance(s, bytes):
return s
elif isinstance(s, str):
return s.encode('utf-8')
else:
raise Exception("Internal error - unexpected type %s provided to _ctype_bits" % (type(s).__name__))
def _ctype_bufout(buf):
if version_info[0] < 3:
return str(buf.raw)
else:
return buf.raw
def _hex_encode(buf):
return hexlify(buf).decode('ascii')
#
# Versioning
#
def version_major():
return int(_DLL.botan_version_major())
def version_minor():
return int(_DLL.botan_version_minor())
def version_patch():
return int(_DLL.botan_version_patch())
def ffi_api_version():
return int(_DLL.botan_ffi_api_version())
def version_string():
return _DLL.botan_version_string().decode('ascii')
#
# Utilities
#
def const_time_compare(x, y):
len_x = len(x)
len_y = len(y)
if len_x != len_y:
return False
rc = _DLL.botan_constant_time_compare(_ctype_bits(x), _ctype_bits(y), c_size_t(len_x))
return rc == 0
#
# RNG
#
class RandomNumberGenerator(object):
# Can also use type "system"
def __init__(self, rng_type='system'):
self.__obj = c_void_p(0)
_DLL.botan_rng_init(byref(self.__obj), _ctype_str(rng_type))
def __del__(self):
_DLL.botan_rng_destroy(self.__obj)
def handle_(self):
return self.__obj
def reseed(self, bits=256):
_DLL.botan_rng_reseed(self.__obj, bits)
def reseed_from_rng(self, source_rng, bits=256):
_DLL.botan_rng_reseed_from_rng(self.__obj, source_rng.handle_(), bits)
def add_entropy(self, seed):
_DLL.botan_rng_add_entropy(self.__obj, _ctype_bits(seed), len(seed))
def get(self, length):
out = create_string_buffer(length)
l = c_size_t(length)
_DLL.botan_rng_get(self.__obj, out, l)
return _ctype_bufout(out)
#
# Block cipher
#
class BlockCipher(object):
def __init__(self, algo):
if isinstance(algo, c_void_p):
self.__obj = algo
else:
flags = c_uint32(0) # always zero in this API version
self.__obj = c_void_p(0)
_DLL.botan_block_cipher_init(byref(self.__obj), _ctype_str(algo), flags)
min_keylen = c_size_t(0)
max_keylen = c_size_t(0)
mod_keylen = c_size_t(0)
_DLL.botan_block_cipher_get_keyspec(self.__obj, byref(min_keylen), byref(max_keylen), byref(mod_keylen))
self.__min_keylen = min_keylen.value
self.__max_keylen = max_keylen.value
self.__mod_keylen = mod_keylen.value
self.__block_size = _DLL.botan_block_cipher_block_size(self.__obj)
def __del__(self):
_DLL.botan_block_cipher_destroy(self.__obj)
def set_key(self, key):
_DLL.botan_block_cipher_set_key(self.__obj, key, len(key))
def encrypt(self, pt):
if len(pt) % self.block_size() != 0:
raise Exception("Invalid input must be multiple of block size")
blocks = c_size_t(len(pt) // self.block_size())
output = create_string_buffer(len(pt))
_DLL.botan_block_cipher_encrypt_blocks(self.__obj, pt, output, blocks)
return output
def decrypt(self, ct):
if len(ct) % self.block_size() != 0:
raise Exception("Invalid input must be multiple of block size")
blocks = c_size_t(len(ct) // self.block_size())
output = create_string_buffer(len(ct))
_DLL.botan_block_cipher_decrypt_blocks(self.__obj, ct, output, blocks)
return output
def algo_name(self):
return _call_fn_returning_str(32, lambda b, bl: _DLL.botan_block_cipher_name(self.__obj, b, bl))
def clear(self):
_DLL.botan_block_cipher_clear(self.__obj)
def block_size(self):
return self.__block_size
def minimum_keylength(self):
return self.__min_keylen
def maximum_keylength(self):
return self.__max_keylen
#
# Hash function
#
class HashFunction(object):
def __init__(self, algo):
if isinstance(algo, c_void_p):
self.__obj = algo
else:
flags = c_uint32(0) # always zero in this API version
self.__obj = c_void_p(0)
_DLL.botan_hash_init(byref(self.__obj), _ctype_str(algo), flags)
self.__output_length = _call_fn_returning_sz(lambda l: _DLL.botan_hash_output_length(self.__obj, l))
self.__block_size = _call_fn_returning_sz(lambda l: _DLL.botan_hash_block_size(self.__obj, l))
def __del__(self):
_DLL.botan_hash_destroy(self.__obj)
def copy_state(self):
copy = c_void_p(0)
_DLL.botan_hash_copy_state(byref(copy), self.__obj)
return HashFunction(copy)
def algo_name(self):
return _call_fn_returning_str(32, lambda b, bl: _DLL.botan_hash_name(self.__obj, b, bl))
def clear(self):
_DLL.botan_hash_clear(self.__obj)
def output_length(self):
return self.__output_length
def block_size(self):
return self.__block_size
def update(self, x):
_DLL.botan_hash_update(self.__obj, _ctype_bits(x), len(x))
def final(self):
out = create_string_buffer(self.output_length())
_DLL.botan_hash_final(self.__obj, out)
return _ctype_bufout(out)
#
# Message authentication codes
#
class MsgAuthCode(object):
def __init__(self, algo):
flags = c_uint32(0) # always zero in this API version
self.__obj = c_void_p(0)
_DLL.botan_mac_init(byref(self.__obj), _ctype_str(algo), flags)
min_keylen = c_size_t(0)
max_keylen = c_size_t(0)
mod_keylen = c_size_t(0)
_DLL.botan_mac_get_keyspec(self.__obj, byref(min_keylen), byref(max_keylen), byref(mod_keylen))
self.__min_keylen = min_keylen.value
self.__max_keylen = max_keylen.value
self.__mod_keylen = mod_keylen.value
output_length = c_size_t(0)
_DLL.botan_mac_output_length(self.__obj, byref(output_length))
self.__output_length = output_length.value
def __del__(self):
_DLL.botan_mac_destroy(self.__obj)
def clear(self):
_DLL.botan_mac_clear(self.__obj)
def algo_name(self):
return _call_fn_returning_str(32, lambda b, bl: _DLL.botan_mac_name(self.__obj, b, bl))
def output_length(self):
return self.__output_length
def minimum_keylength(self):
return self.__min_keylen
def maximum_keylength(self):
return self.__max_keylen
def set_key(self, key):
_DLL.botan_mac_set_key(self.__obj, key, len(key))
def update(self, x):
_DLL.botan_mac_update(self.__obj, x, len(x))
def final(self):
out = create_string_buffer(self.output_length())
_DLL.botan_mac_final(self.__obj, out)
return _ctype_bufout(out)
class SymmetricCipher(object):
def __init__(self, algo, encrypt=True):
flags = 0 if encrypt else 1
self.__obj = c_void_p(0)
_DLL.botan_cipher_init(byref(self.__obj), _ctype_str(algo), flags)
def __del__(self):
_DLL.botan_cipher_destroy(self.__obj)
def algo_name(self):
return _call_fn_returning_str(32, lambda b, bl: _DLL.botan_cipher_name(self.__obj, b, bl))
def default_nonce_length(self):
l = c_size_t(0)
_DLL.botan_cipher_get_default_nonce_length(self.__obj, byref(l))
return l.value
def update_granularity(self):
l = c_size_t(0)
_DLL.botan_cipher_get_update_granularity(self.__obj, byref(l))
return l.value
def key_length(self):
kmin = c_size_t(0)
kmax = c_size_t(0)
_DLL.botan_cipher_query_keylen(self.__obj, byref(kmin), byref(kmax))
return kmin.value, kmax.value
def minimum_keylength(self):
l = c_size_t(0)
_DLL.botan_cipher_get_keyspec(self.__obj, byref(l), None, None)
return l.value
def maximum_keylength(self):
l = c_size_t(0)
_DLL.botan_cipher_get_keyspec(self.__obj, None, byref(l), None)
return l.value
def tag_length(self):
l = c_size_t(0)
_DLL.botan_cipher_get_tag_length(self.__obj, byref(l))
return l.value
def is_authenticated(self):
return self.tag_length() > 0
def valid_nonce_length(self, nonce_len):
rc = _DLL.botan_cipher_valid_nonce_length(self.__obj, nonce_len)
return rc == 1
def reset(self):
_DLL.botan_cipher_reset(self.__obj)
def clear(self):
_DLL.botan_cipher_clear(self.__obj)
def set_key(self, key):
_DLL.botan_cipher_set_key(self.__obj, key, len(key))
def set_assoc_data(self, ad):
_DLL.botan_cipher_set_associated_data(self.__obj, ad, len(ad))
def start(self, nonce):
_DLL.botan_cipher_start(self.__obj, nonce, len(nonce))
def _update(self, txt, final):
inp = txt if txt else ''
inp_sz = c_size_t(len(inp))
inp_consumed = c_size_t(0)
out = create_string_buffer(inp_sz.value + (self.tag_length() if final else 0))
out_sz = c_size_t(len(out))
out_written = c_size_t(0)
flags = c_uint32(1 if final else 0)
_DLL.botan_cipher_update(self.__obj, flags,
out, out_sz, byref(out_written),
_ctype_bits(inp), inp_sz, byref(inp_consumed))
# buffering not supported yet
assert inp_consumed.value == inp_sz.value
return out.raw[0:int(out_written.value)]
def update(self, txt):
return self._update(txt, False)
def finish(self, txt=None):
return self._update(txt, True)
def bcrypt(passwd, rng_obj, work_factor=10):
"""
Bcrypt password hashing
"""
out_len = c_size_t(64)
out = create_string_buffer(out_len.value)
flags = c_uint32(0)
_DLL.botan_bcrypt_generate(out, byref(out_len), _ctype_str(passwd),
rng_obj.handle_(), c_size_t(work_factor), flags)
b = out.raw[0:int(out_len.value)-1]
if b[-1] == '\x00':
b = b[:-1]
return _ctype_to_str(b)
def check_bcrypt(passwd, passwd_hash):
rc = _DLL.botan_bcrypt_is_valid(_ctype_str(passwd), _ctype_str(passwd_hash))
return rc == 0
#
# PBKDF
#
def pbkdf(algo, password, out_len, iterations=10000, salt=None):
if salt is None:
salt = RandomNumberGenerator().get(12)
out_buf = create_string_buffer(out_len)
_DLL.botan_pwdhash(_ctype_str(algo), iterations, 0, 0,
out_buf, out_len,
_ctype_str(password), len(password),
salt, len(salt))
return (salt, iterations, out_buf.raw)
def pbkdf_timed(algo, password, out_len, ms_to_run=300, salt=None):
if salt is None:
salt = RandomNumberGenerator().get(12)
out_buf = create_string_buffer(out_len)
iterations = c_size_t(0)
_DLL.botan_pwdhash_timed(_ctype_str(algo), c_uint32(ms_to_run),
byref(iterations), None, None,
out_buf, out_len,
_ctype_str(password), len(password),
salt, len(salt))
return (salt, iterations.value, out_buf.raw)
#
# Scrypt
#
def scrypt(out_len, password, salt, n=1024, r=8, p=8):
out_buf = create_string_buffer(out_len)
_DLL.botan_pwdhash(_ctype_str("Scrypt"), n, r, p,
out_buf, out_len,
_ctype_str(password), len(password),
_ctype_bits(salt), len(salt))
return out_buf.raw
#
# KDF
#
def kdf(algo, secret, out_len, salt, label):
out_buf = create_string_buffer(out_len)
out_sz = c_size_t(out_len)
_DLL.botan_kdf(_ctype_str(algo), out_buf, out_sz,
secret, len(secret),
salt, len(salt),
label, len(label))
return out_buf.raw[0:int(out_sz.value)]
#
# Public key
#
class PublicKey(object): # pylint: disable=invalid-name
def __init__(self, obj=c_void_p(0)):
self.__obj = obj
@classmethod
def load(cls, val):
obj = c_void_p(0)
_DLL.botan_pubkey_load(byref(obj), _ctype_bits(val), len(val))
return PublicKey(obj)
@classmethod
def load_rsa(cls, n, e):
obj = c_void_p(0)
n = MPI(n)
e = MPI(e)
_DLL.botan_pubkey_load_rsa(byref(obj), n.handle_(), e.handle_())
return PublicKey(obj)
@classmethod
def load_dsa(cls, p, q, g, y):
obj = c_void_p(0)
p = MPI(p)
q = MPI(q)
g = MPI(g)
y = MPI(y)
_DLL.botan_pubkey_load_dsa(byref(obj), p.handle_(), q.handle_(), g.handle_(), y.handle_())
return PublicKey(obj)
@classmethod
def load_dh(cls, p, g, y):
obj = c_void_p(0)
p = MPI(p)
g = MPI(g)
y = MPI(y)
_DLL.botan_pubkey_load_dh(byref(obj), p.handle_(), g.handle_(), y.handle_())
return PublicKey(obj)
@classmethod
def load_elgamal(cls, p, q, g, y):
obj = c_void_p(0)
p = MPI(p)
q = MPI(q)
g = MPI(g)
y = MPI(y)
_DLL.botan_pubkey_load_elgamal(byref(obj), p.handle_(), q.handle_(), g.handle_(), y.handle_())
return PublicKey(obj)
@classmethod
def load_ecdsa(cls, curve, pub_x, pub_y):
obj = c_void_p(0)
pub_x = MPI(pub_x)
pub_y = MPI(pub_y)
_DLL.botan_pubkey_load_ecdsa(byref(obj), pub_x.handle_(), pub_y.handle_(), _ctype_str(curve))
return PublicKey(obj)
@classmethod
def load_ecdh(cls, curve, pub_x, pub_y):
obj = c_void_p(0)
pub_x = MPI(pub_x)
pub_y = MPI(pub_y)
_DLL.botan_pubkey_load_ecdh(byref(obj), pub_x.handle_(), pub_y.handle_(), _ctype_str(curve))
return PublicKey(obj)
@classmethod
def load_sm2(cls, curve, pub_x, pub_y):
obj = c_void_p(0)
pub_x = MPI(pub_x)
pub_y = MPI(pub_y)
_DLL.botan_pubkey_load_sm2(byref(obj), pub_x.handle_(), pub_y.handle_(), _ctype_str(curve))
return PublicKey(obj)
def __del__(self):
_DLL.botan_pubkey_destroy(self.__obj)
def handle_(self):
return self.__obj
def check_key(self, rng_obj, strong=True):
flags = 1 if strong else 0
rc = _DLL.botan_pubkey_check_key(self.__obj, rng_obj.handle_(), flags)
return rc == 0
def estimated_strength(self):
r = c_size_t(0)
_DLL.botan_pubkey_estimated_strength(self.__obj, byref(r))
return r.value
def algo_name(self):
return _call_fn_returning_str(32, lambda b, bl: _DLL.botan_pubkey_algo_name(self.__obj, b, bl))
def export(self, pem=False):
if pem:
return _call_fn_returning_str(4096, lambda b, bl: _DLL.botan_pubkey_export(self.__obj, b, bl, 1))
else:
return _call_fn_returning_vec(4096, lambda b, bl: _DLL.botan_pubkey_export(self.__obj, b, bl, 0))
def encoding(self, pem=False):
return self.export(pem)
def to_der(self):
return self.export(False)
def to_pem(self):
return self.export(True)
def fingerprint(self, hash_algorithm='SHA-256'):
n = HashFunction(hash_algorithm).output_length()
buf = create_string_buffer(n)
buf_len = c_size_t(n)
_DLL.botan_pubkey_fingerprint(self.__obj, _ctype_str(hash_algorithm), buf, byref(buf_len))
return _hex_encode(buf[0:int(buf_len.value)])
def get_field(self, field_name):
v = MPI()
_DLL.botan_pubkey_get_field(v.handle_(), self.__obj, _ctype_str(field_name))
return int(v)
#
# Private Key
#
class PrivateKey(object):
def __init__(self, obj=c_void_p(0)):
self.__obj = obj
@classmethod
def load(cls, val, passphrase=""):
obj = c_void_p(0)
rng_obj = c_void_p(0) # unused in recent versions
_DLL.botan_privkey_load(byref(obj), rng_obj, _ctype_bits(val), len(val), _ctype_str(passphrase))
return PrivateKey(obj)
@classmethod
def create(cls, algo, params, rng_obj):
if algo == 'rsa':
algo = 'RSA'
params = "%d" % (params)
elif algo == 'ecdsa':
algo = 'ECDSA'
elif algo in ['ecdh', 'ECDH']:
if params == 'curve25519':
algo = 'Curve25519'
params = ''
else:
algo = 'ECDH'
elif algo in ['mce', 'mceliece']:
algo = 'McEliece'
params = "%d,%d" % (params[0], params[1])
obj = c_void_p(0)
_DLL.botan_privkey_create(byref(obj), _ctype_str(algo), _ctype_str(params), rng_obj.handle_())
return PrivateKey(obj)
@classmethod
def load_rsa(cls, p, q, e):
obj = c_void_p(0)
p = MPI(p)
q = MPI(q)
e = MPI(e)
_DLL.botan_privkey_load_rsa(byref(obj), p.handle_(), q.handle_(), e.handle_())
return PrivateKey(obj)
@classmethod
def load_dsa(cls, p, q, g, x):
obj = c_void_p(0)
p = MPI(p)
q = MPI(q)
g = MPI(g)
x = MPI(x)
_DLL.botan_privkey_load_dsa(byref(obj), p.handle_(), q.handle_(), g.handle_(), x.handle_())
return PrivateKey(obj)
@classmethod
def load_dh(cls, p, g, x):
obj = c_void_p(0)
p = MPI(p)
g = MPI(g)
x = MPI(x)
_DLL.botan_privkey_load_dh(byref(obj), p.handle_(), g.handle_(), x.handle_())
return PrivateKey(obj)
@classmethod
def load_elgamal(cls, p, q, g, x):
obj = c_void_p(0)
p = MPI(p)
q = MPI(q)
g = MPI(g)
x = MPI(x)
_DLL.botan_privkey_load_elgamal(byref(obj), p.handle_(), q.handle_(), g.handle_(), x.handle_())
return PrivateKey(obj)
@classmethod
def load_ecdsa(cls, curve, x):
obj = c_void_p(0)
x = MPI(x)
_DLL.botan_privkey_load_ecdsa(byref(obj), x.handle_(), _ctype_str(curve))
return PrivateKey(obj)
@classmethod
def load_ecdh(cls, curve, x):
obj = c_void_p(0)
x = MPI(x)
_DLL.botan_privkey_load_ecdh(byref(obj), x.handle_(), _ctype_str(curve))
return PrivateKey(obj)
@classmethod
def load_sm2(cls, curve, x):
obj = c_void_p(0)
x = MPI(x)
_DLL.botan_privkey_load_sm2(byref(obj), x.handle_(), _ctype_str(curve))
return PrivateKey(obj)
def __del__(self):
_DLL.botan_privkey_destroy(self.__obj)
def handle_(self):
return self.__obj
def check_key(self, rng_obj, strong=True):
flags = 1 if strong else 0
rc = _DLL.botan_privkey_check_key(self.__obj, rng_obj.handle_(), flags)
return rc == 0
def algo_name(self):
return _call_fn_returning_str(32, lambda b, bl: _DLL.botan_privkey_algo_name(self.__obj, b, bl))
def get_public_key(self):
pub = c_void_p(0)
_DLL.botan_privkey_export_pubkey(byref(pub), self.__obj)
return PublicKey(pub)
def to_der(self):
return self.export(False)
def to_pem(self):
return self.export(True)
def export(self, pem=False):
if pem:
return _call_fn_returning_str(4096, lambda b, bl: _DLL.botan_privkey_export(self.__obj, b, bl, 1))
else:
return _call_fn_returning_vec(4096, lambda b, bl: _DLL.botan_privkey_export(self.__obj, b, bl, 0))
def export_encrypted(self, passphrase, rng_obj, pem=False, msec=300, cipher=None, pbkdf=None): # pylint: disable=redefined-outer-name
flags = 1 if pem else 0
msec = c_uint32(msec)
_iters = c_size_t(0)
cb = lambda b, bl: _DLL.botan_privkey_export_encrypted_pbkdf_msec(
self.__obj, b, bl, rng_obj.handle_(), _ctype_str(passphrase),
msec, byref(_iters), _ctype_str(cipher), _ctype_str(pbkdf), flags)
if pem:
return _call_fn_returning_str(8192, cb)
else:
return _call_fn_returning_vec(4096, cb)
def get_field(self, field_name):
v = MPI()
_DLL.botan_privkey_get_field(v.handle_(), self.__obj, _ctype_str(field_name))
return int(v)
class PKEncrypt(object):
def __init__(self, key, padding):
self.__obj = c_void_p(0)
flags = c_uint32(0) # always zero in this ABI
_DLL.botan_pk_op_encrypt_create(byref(self.__obj), key.handle_(), _ctype_str(padding), flags)
def __del__(self):
_DLL.botan_pk_op_encrypt_destroy(self.__obj)
def encrypt(self, msg, rng_obj):
outbuf_sz = c_size_t(0)
_DLL.botan_pk_op_encrypt_output_length(self.__obj, len(msg), byref(outbuf_sz))
outbuf = create_string_buffer(outbuf_sz.value)
_DLL.botan_pk_op_encrypt(self.__obj, rng_obj.handle_(), outbuf, byref(outbuf_sz), msg, len(msg))
return outbuf.raw[0:int(outbuf_sz.value)]
class PKDecrypt(object):
def __init__(self, key, padding):
self.__obj = c_void_p(0)
flags = c_uint32(0) # always zero in this ABI
_DLL.botan_pk_op_decrypt_create(byref(self.__obj), key.handle_(), _ctype_str(padding), flags)
def __del__(self):
_DLL.botan_pk_op_decrypt_destroy(self.__obj)
def decrypt(self, msg):
outbuf_sz = c_size_t(0)
_DLL.botan_pk_op_decrypt_output_length(self.__obj, len(msg), byref(outbuf_sz))
outbuf = create_string_buffer(outbuf_sz.value)
_DLL.botan_pk_op_decrypt(self.__obj, outbuf, byref(outbuf_sz), _ctype_bits(msg), len(msg))
return outbuf.raw[0:int(outbuf_sz.value)]
class PKSign(object): # pylint: disable=invalid-name
def __init__(self, key, padding):
self.__obj = c_void_p(0)
flags = c_uint32(0) # always zero in this ABI
_DLL.botan_pk_op_sign_create(byref(self.__obj), key.handle_(), _ctype_str(padding), flags)
def __del__(self):
_DLL.botan_pk_op_sign_destroy(self.__obj)
def update(self, msg):
_DLL.botan_pk_op_sign_update(self.__obj, _ctype_str(msg), len(msg))
def finish(self, rng_obj):
outbuf_sz = c_size_t(0)
_DLL.botan_pk_op_sign_output_length(self.__obj, byref(outbuf_sz))
outbuf = create_string_buffer(outbuf_sz.value)
_DLL.botan_pk_op_sign_finish(self.__obj, rng_obj.handle_(), outbuf, byref(outbuf_sz))
return outbuf.raw[0:int(outbuf_sz.value)]
class PKVerify(object):
def __init__(self, key, padding):
self.__obj = c_void_p(0)
flags = c_uint32(0) # always zero in this ABI
_DLL.botan_pk_op_verify_create(byref(self.__obj), key.handle_(), _ctype_str(padding), flags)
def __del__(self):
_DLL.botan_pk_op_verify_destroy(self.__obj)
def update(self, msg):
_DLL.botan_pk_op_verify_update(self.__obj, _ctype_bits(msg), len(msg))
def check_signature(self, signature):
rc = _DLL.botan_pk_op_verify_finish(self.__obj, _ctype_bits(signature), len(signature))
if rc == 0:
return True
return False
class PKKeyAgreement(object):
def __init__(self, key, kdf_name):
self.__obj = c_void_p(0)
flags = c_uint32(0) # always zero in this ABI
_DLL.botan_pk_op_key_agreement_create(byref(self.__obj), key.handle_(), _ctype_str(kdf_name), flags)
self.m_public_value = _call_fn_returning_vec(
0, lambda b, bl: _DLL.botan_pk_op_key_agreement_export_public(key.handle_(), b, bl))
def __del__(self):
_DLL.botan_pk_op_key_agreement_destroy(self.__obj)
def public_value(self):
return self.m_public_value
def underlying_output_length(self):
out_len = c_size_t(0)
_DLL.botan_pk_op_key_agreement_size(self.__obj, byref(out_len))
return out_len.value
def agree(self, other, key_len, salt):
if key_len == 0:
key_len = self.underlying_output_length()
return _call_fn_returning_vec(key_len, lambda b, bl:
_DLL.botan_pk_op_key_agreement(self.__obj, b, bl,
other, len(other),
salt, len(salt)))
#
# MCEIES encryption
# Must be used with McEliece keys
#
def mceies_encrypt(mce, rng_obj, aead, pt, ad):
return _call_fn_returning_vec(len(pt) + 1024, lambda b, bl:
_DLL.botan_mceies_encrypt(mce.handle_(),
rng_obj.handle_(),
_ctype_str(aead),
_ctype_bits(pt),
len(pt),
_ctype_bits(ad),
len(ad),
b, bl))
def mceies_decrypt(mce, aead, ct, ad):
#msg = cast(msg, c_char_p)
#ll = c_size_t(ll)
return _call_fn_returning_vec(len(ct), lambda b, bl:
_DLL.botan_mceies_decrypt(mce.handle_(),
_ctype_str(aead),
_ctype_bits(ct),
len(ct),
_ctype_bits(ad),
len(ad),
b, bl))
#
# X.509 certificates
#
class X509Cert(object): # pylint: disable=invalid-name
def __init__(self, filename=None, buf=None):
if filename is None and buf is None:
raise BotanException("No filename or buf given")
if filename is not None and buf is not None:
raise BotanException("Both filename and buf given")
if filename is not None:
self.__obj = c_void_p(0)
_DLL.botan_x509_cert_load_file(byref(self.__obj), _ctype_str(filename))
elif buf is not None:
self.__obj = c_void_p(0)
_DLL.botan_x509_cert_load(byref(self.__obj), _ctype_bits(buf), len(buf))
def __del__(self):
_DLL.botan_x509_cert_destroy(self.__obj)
def time_starts(self):
starts = _call_fn_returning_str(
16, lambda b, bl: _DLL.botan_x509_cert_get_time_starts(self.__obj, b, bl))
if len(starts) == 13:
# UTC time
struct_time = strptime(starts, "%y%m%d%H%M%SZ")
elif len(starts) == 15:
# Generalized time
struct_time = strptime(starts, "%Y%m%d%H%M%SZ")
else:
raise BotanException("Unexpected date/time format for x509 start time")
return datetime.fromtimestamp(mktime(struct_time))
def time_expires(self):
expires = _call_fn_returning_str(
16, lambda b, bl: _DLL.botan_x509_cert_get_time_expires(self.__obj, b, bl))
if len(expires) == 13:
# UTC time
struct_time = strptime(expires, "%y%m%d%H%M%SZ")
elif len(expires) == 15:
# Generalized time
struct_time = strptime(expires, "%Y%m%d%H%M%SZ")
else:
raise BotanException("Unexpected date/time format for x509 expire time")
return datetime.fromtimestamp(mktime(struct_time))
def to_string(self):
return _call_fn_returning_str(
4096, lambda b, bl: _DLL.botan_x509_cert_to_string(self.__obj, b, bl))
def fingerprint(self, hash_algo='SHA-256'):
n = HashFunction(hash_algo).output_length() * 3
return _call_fn_returning_str(
n, lambda b, bl: _DLL.botan_x509_cert_get_fingerprint(self.__obj, _ctype_str(hash_algo), b, bl))
def serial_number(self):
return _call_fn_returning_vec(
32, lambda b, bl: _DLL.botan_x509_cert_get_serial_number(self.__obj, b, bl))
def authority_key_id(self):
return _call_fn_returning_vec(
32, lambda b, bl: _DLL.botan_x509_cert_get_authority_key_id(self.__obj, b, bl))
def subject_key_id(self):
return _call_fn_returning_vec(
32, lambda b, bl: _DLL.botan_x509_cert_get_subject_key_id(self.__obj, b, bl))
def subject_public_key_bits(self):
return _call_fn_returning_vec(
512, lambda b, bl: _DLL.botan_x509_cert_get_public_key_bits(self.__obj, b, bl))
def subject_public_key(self):
pub = c_void_p(0)
_DLL.botan_x509_cert_get_public_key(self.__obj, byref(pub))
return PublicKey(pub)
def subject_dn(self, key, index):
return _call_fn_returning_str(
0, lambda b, bl: _DLL.botan_x509_cert_get_subject_dn(self.__obj, _ctype_str(key), index, b, bl))
class MPI(object):
def __init__(self, initial_value=None):
self.__obj = c_void_p(0)
_DLL.botan_mp_init(byref(self.__obj))
if initial_value is None:
pass # left as zero
elif isinstance(initial_value, MPI):
_DLL.botan_mp_set_from_mp(self.__obj, initial_value.handle_())
elif isinstance(initial_value, str):
_DLL.botan_mp_set_from_str(self.__obj, _ctype_str(initial_value))
else:
# For int or long (or whatever else), try converting to string:
_DLL.botan_mp_set_from_str(self.__obj, _ctype_str(str(initial_value)))
@classmethod
def random(cls, rng_obj, bits):
bn = MPI()
_DLL.botan_mp_rand_bits(bn.handle_(), rng_obj.handle_(), c_size_t(bits))
return bn
@classmethod
def random_range(cls, rng_obj, lower, upper):
bn = MPI()
_DLL.botan_mp_rand_range(bn.handle_(), rng_obj.handle_(), lower.handle_(), upper.handle_())
return bn
def __del__(self):
_DLL.botan_mp_destroy(self.__obj)
def handle_(self):
return self.__obj
def __int__(self):
out = create_string_buffer(2*self.byte_count() + 1)
_DLL.botan_mp_to_hex(self.__obj, out)
val = int(out.value, 16)
if self.is_negative():
return -val
else:
return val
def __repr__(self):
# Should have a better size estimate than this ...
out_len = c_size_t(self.bit_count() // 2)
out = create_string_buffer(out_len.value)
_DLL.botan_mp_to_str(self.__obj, c_uint8(10), out, byref(out_len))
out = out.raw[0:int(out_len.value)]
if out[-1] == '\x00':
out = out[:-1]
s = _ctype_to_str(out)
if s[0] == '0':
return s[1:]
else:
return s
def to_bytes(self):
byte_count = self.byte_count()
out_len = c_size_t(byte_count)
out = create_string_buffer(out_len.value)
_DLL.botan_mp_to_bin(self.__obj, out, byref(out_len))
assert out_len.value == byte_count
return out
def is_negative(self):
rc = _DLL.botan_mp_is_negative(self.__obj)
return rc == 1
def is_positive(self):
rc = _DLL.botan_mp_is_positive(self.__obj)
return rc == 1
def is_zero(self):
rc = _DLL.botan_mp_is_zero(self.__obj)
return rc == 1
def is_odd(self):
return self.get_bit(0) == 1
def is_even(self):
return self.get_bit(0) == 0
def flip_sign(self):
_DLL.botan_mp_flip_sign(self.__obj)
def cmp(self, other):
r = c_int(0)
_DLL.botan_mp_cmp(byref(r), self.__obj, other.handle_())
return r.value
def __hash__(self):
return hash(self.to_bytes())
def __eq__(self, other):
return self.cmp(other) == 0
def __ne__(self, other):
return self.cmp(other) != 0
def __lt__(self, other):
return self.cmp(other) < 0
def __le__(self, other):
return self.cmp(other) <= 0
def __gt__(self, other):
return self.cmp(other) > 0
def __ge__(self, other):
return self.cmp(other) >= 0
def __add__(self, other):
r = MPI()
_DLL.botan_mp_add(r.handle_(), self.__obj, other.handle_())
return r
def __iadd__(self, other):
_DLL.botan_mp_add(self.__obj, self.__obj, other.handle_())
return self
def __sub__(self, other):
r = MPI()
_DLL.botan_mp_sub(r.handle_(), self.__obj, other.handle_())
return r
def __isub__(self, other):
_DLL.botan_mp_sub(self.__obj, self.__obj, other.handle_())
return self
def __mul__(self, other):
r = MPI()
_DLL.botan_mp_mul(r.handle_(), self.__obj, other.handle_())
return r
def __imul__(self, other):
_DLL.botan_mp_mul(self.__obj, self.__obj, other.handle_())
return self
def __divmod__(self, other):
d = MPI()
q = MPI()
_DLL.botan_mp_div(d.handle_(), q.handle_(), self.__obj, other.handle_())
return (d, q)
def __mod__(self, other):
d = MPI()
q = MPI()
_DLL.botan_mp_div(d.handle_(), q.handle_(), self.__obj, other.handle_())
return q
def __lshift__(self, shift):
shift = c_size_t(shift)
r = MPI()
_DLL.botan_mp_lshift(r.handle_(), self.__obj, shift)
return r
def __ilshift__(self, shift):
shift = c_size_t(shift)
_DLL.botan_mp_lshift(self.__obj, self.__obj, shift)
return self
def __rshift__(self, shift):
shift = c_size_t(shift)
r = MPI()
_DLL.botan_mp_rshift(r.handle_(), self.__obj, shift)
return r
def __irshift__(self, shift):
shift = c_size_t(shift)
_DLL.botan_mp_rshift(self.__obj, self.__obj, shift)
return self
def pow_mod(self, exponent, modulus):
r = MPI()
_DLL.botan_mp_powmod(r.handle_(), self.__obj, exponent.handle_(), modulus.handle_())
return r
def is_prime(self, rng_obj, prob=128):
return _DLL.botan_mp_is_prime(self.__obj, rng_obj.handle_(), c_size_t(prob)) == 1
def inverse_mod(self, modulus):
r = MPI()
_DLL.botan_mp_mod_inverse(r.handle_(), self.__obj, modulus.handle_())
return r
def bit_count(self):
b = c_size_t(0)
_DLL.botan_mp_num_bits(self.__obj, byref(b))
return b.value
def byte_count(self):
b = c_size_t(0)
_DLL.botan_mp_num_bytes(self.__obj, byref(b))
return b.value
def get_bit(self, bit):
return _DLL.botan_mp_get_bit(self.__obj, c_size_t(bit)) == 1
def clear_bit(self, bit):
_DLL.botan_mp_clear_bit(self.__obj, c_size_t(bit))
def set_bit(self, bit):
_DLL.botan_mp_set_bit(self.__obj, c_size_t(bit))
class FormatPreservingEncryptionFE1(object):
def __init__(self, modulus, key, rounds=5, compat_mode=False):
flags = c_uint32(1 if compat_mode else 0)
self.__obj = c_void_p(0)
_DLL.botan_fpe_fe1_init(byref(self.__obj), modulus.handle_(), key, len(key), c_size_t(rounds), flags)
def __del__(self):
_DLL.botan_fpe_destroy(self.__obj)
def encrypt(self, msg, tweak):
r = MPI(msg)
_DLL.botan_fpe_encrypt(self.__obj, r.handle_(), _ctype_bits(tweak), len(tweak))
return r
def decrypt(self, msg, tweak):
r = MPI(msg)
_DLL.botan_fpe_decrypt(self.__obj, r.handle_(), _ctype_bits(tweak), len(tweak))
return r
class HOTP(object):
def __init__(self, key, digest="SHA-1", digits=6):
self.__obj = c_void_p(0)
_DLL.botan_hotp_init(byref(self.__obj), key, len(key), _ctype_str(digest), digits)
def __del__(self):
_DLL.botan_hotp_destroy(self.__obj)
def generate(self, counter):
code = c_uint32(0)
_DLL.botan_hotp_generate(self.__obj, byref(code), counter)
return code.value
def check(self, code, counter, resync_range=0):
next_ctr = c_uint64(0)
rc = _DLL.botan_hotp_check(self.__obj, byref(next_ctr), code, counter, resync_range)
if rc == 0:
return (True, next_ctr.value)
else:
return (False, counter)
def nist_key_wrap(kek, key):
output = create_string_buffer(len(key) + 8)
out_len = c_size_t(len(output))
_DLL.botan_key_wrap3394(key, len(key), kek, len(kek), output, byref(out_len))
return output[0:int(out_len.value)]
def nist_key_unwrap(kek, wrapped):
output = create_string_buffer(len(wrapped))
out_len = c_size_t(len(output))
_DLL.botan_key_unwrap3394(wrapped, len(wrapped), kek, len(kek), output, byref(out_len))
return output[0:int(out_len.value)]
# Typedefs for compat with older versions
# Will be removed in a future major release
cipher = SymmetricCipher # pylint: disable=invalid-name
rng = RandomNumberGenerator # pylint: disable=invalid-name
hash_function = HashFunction # pylint: disable=invalid-name
message_authentication_code = MsgAuthCode # pylint: disable=invalid-name
x509_cert = X509Cert # pylint: disable=invalid-name
public_key = PublicKey # pylint: disable=invalid-name
private_key = PrivateKey # pylint: disable=invalid-name
pk_op_encrypt = PKEncrypt # pylint: disable=invalid-name
pk_op_decrypt = PKDecrypt # pylint: disable=invalid-name
pk_op_sign = PKSign # pylint: disable=invalid-name
pk_op_verify = PKVerify # pylint: disable=invalid-name
pk_op_key_agreement = PKKeyAgreement # pylint: disable=invalid-name
|