-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcrypt.nim
1927 lines (1524 loc) · 78 KB
/
gcrypt.nim
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
## gcrypt.h - GNU Cryptographic Library Interface
## Copyright (C) 1998-2017 Free Software Foundation, Inc.
## Copyright (C) 2012-2017 g10 Code GmbH
## Copyright (C) 2019 Federico Ceratto <federico.ceratto@gmail.com>
##
## Libgcrypt is free software; you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as
## published by the Free Software Foundation; either version 2.1 of
## the License, or (at your option) any later version.
##
## Libgcrypt is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public
## License along with this program; if not, see <http://www.gnu.org/licenses/>.
when defined(windows):
const lib_fn* = "libgcrypt.dll"
elif defined(macosx):
const lib_fn* = "libgcrypt.dylib"
else:
const lib_fn* = "libgcrypt.so(.11|.20)"
{.passL: "-lgcrypt".}
{.pragma: import_gcrypt, importc, dynlib: lib_fn.}
type
gpg_error_t = cint
gcry_error_t* = gpg_error_t
template check_rc(rc: gcry_error_t): untyped =
## Expect return code to be 0, raise an exception otherwise
if rc != 0.cint:
raise newException(Exception, $gcry_strerror(rc))
type
socklen_t = cint
gcry_socklen_t* = socklen_t
## This is required for error code compatibility.
const
GPG_ERR_SOURCE_GCRYPT = 1
GCRY_ERR_SOURCE_DEFAULT* = GPG_ERR_SOURCE_GCRYPT
## The version of this header should match the one of the library. It
## should not be used by a program because gcry_check_version() should
## return the same version. The purpose of this macro is to let
## autoconf (using the AM_PATH_GCRYPT macro) check that this header
## matches the installed library.
const
GCRYPT_VERSION* = "1.8.4"
## The version number of this header. It may be used to handle minor
## API incompatibilities.
const
GCRYPT_VERSION_NUMBER* = 0x00010804
## Internal: We can't use the convenience macros for the multi
## precision integer functions when building this library.
#when defined(GCRYPT_IN_LIBGCRYPT):
## We want to use gcc attributes when possible. Warning: Don't use
## these macros in your programs: As indicated by the leading
## underscore they are subject to change without notice.
#when defined(_GNUC__):
# const
# GCRY_GCC_VERSION* = (
# GNUC__ * 10000 + GNUC_MINOR__ * 100 + GNUC_PATCHLEVEL__)
# when GCRY_GCC_VERSION >= 30100:
# const
# GCRY_GCC_ATTR_DEPRECATED* = attribute__((_deprecated__))
# when GCRY_GCC_VERSION >= 29600:
# const
# GCRY_GCC_ATTR_PURE* = attribute__((_pure__))
# when GCRY_GCC_VERSION >= 30200:
# const
# GCRY_GCC_ATTR_MALLOC* = attribute__((_malloc__))
# template GCRY_GCC_ATTR_PRINTF*(f, a: untyped): untyped =
# attribute__((format(printf, f, a)))
#
# when GCRY_GCC_VERSION >= 40000:
# template GCRY_GCC_ATTR_SENTINEL*(a: untyped): untyped =
# attribute__((sentinel(a)))
when not defined(GCRY_GCC_ATTR_PRINTF):
template GCRY_GCC_ATTR_PRINTF*(f, a: untyped): void =
nil
when not defined(GCRY_GCC_ATTR_SENTINEL):
template GCRY_GCC_ATTR_SENTINEL*(a: untyped): void =
nil
## Make up an attribute to mark functions and types as deprecated but
## allow internal use by Libgcrypt.
#when defined(GCRYPT_IN_LIBGCRYPT):
# const
# GCRY_ATTR_INTERNAL* = true
#else:
# const
# GCRY_ATTR_INTERNAL* = GCRY_GCC_ATTR_DEPRECATED
## Wrappers for the libgpg-error library.
type
gpg_err_code_t = pointer
gpg_err_source_t = pointer
gcry_err_code_t* = gpg_err_code_t
gcry_err_source_t* = gpg_err_source_t
##
## static GPG_ERR_INLINE gcry_error_t
## gcry_err_make (gcry_err_source_t source, gcry_err_code_t code)
## {
## return gpg_err_make (source, code);
## }
##
## The user can define GPG_ERR_SOURCE_DEFAULT before including this
## file to specify a default source for gpg_error.
##
## static GPG_ERR_INLINE gcry_error_t
## gcry_error (gcry_err_code_t code)
## {
## return gcry_err_make (GCRY_ERR_SOURCE_DEFAULT, code);
## }
##
## static GPG_ERR_INLINE gcry_err_code_t
## gcry_err_code (gcry_error_t err)
## {
## return gpg_err_code (err);
## }
##
##
## static GPG_ERR_INLINE gcry_err_source_t
## gcry_err_source (gcry_error_t err)
## {
## return gpg_err_source (err);
## }
##
## Return a pointer to a string containing a description of the error
## code in the error value ERR.
proc gcry_strerror*(err: gcry_error_t): cstring {.importc: "gcry_strerror",
import_gcrypt.}
## Return a pointer to a string containing a description of the error
## source in the error value ERR.
proc gcry_strsource*(err: gcry_error_t): cstring {.importc: "gcry_strsource",
import_gcrypt.}
## Retrieve the error code for the system error ERR. This returns
## GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report
## this).
proc gcry_err_code_from_errno*(err: cint): gcry_err_code_t {.
importc: "gcry_err_code_from_errno", import_gcrypt.}
## Retrieve the system error for the error code CODE. This returns 0
## if CODE is not a system error code.
proc gcry_err_code_to_errno*(code: gcry_err_code_t): cint {.
importc: "gcry_err_code_to_errno", import_gcrypt.}
## Return an error value with the error source SOURCE and the system
## error ERR.
proc gcry_err_make_from_errno*(source: gcry_err_source_t; err: cint): gcry_error_t {.
importc: "gcry_err_make_from_errno", import_gcrypt.}
## Return an error value with the system error ERR.
proc gcry_error_from_errno*(err: cint): gcry_error_t {.
importc: "gcry_error_from_errno", import_gcrypt.}
## NOTE: Since Libgcrypt 1.6 the thread callbacks are not anymore
## used. However we keep it to allow for some source code
## compatibility if used in the standard way.
## Constants defining the thread model to use. Used with the OPTION
## field of the struct gcry_thread_cbs.
const
GCRY_THREAD_OPTION_DEFAULT* = 0
GCRY_THREAD_OPTION_USER* = 1
GCRY_THREAD_OPTION_PTH* = 2
GCRY_THREAD_OPTION_PTHREAD* = 3
## The version number encoded in the OPTION field of the struct
## gcry_thread_cbs.
const
GCRY_THREAD_OPTION_VERSION* = 1
## Wrapper for struct ath_ops.
type
gcry_thread_cbs* {.bycopy.} = object
option*: cuint ## The OPTION field encodes the thread model and the version number
## of this structure.
## Bits 7 - 0 are used for the thread model
## Bits 15 - 8 are used for the version number.
# var GCRY_ATTR_INTERNAL* {.importc: "GCRY_ATTR_INTERNAL", import_gcrypt.}: gcry_thread_cbs
##
## #define GCRY_THREAD_OPTION_PTH_IMPL \
## static struct gcry_thread_cbs gcry_threads_pth = { \
## (GCRY_THREAD_OPTION_PTH | (GCRY_THREAD_OPTION_VERSION << 8))}
##
## #define GCRY_THREAD_OPTION_PTHREAD_IMPL \
## static struct gcry_thread_cbs gcry_threads_pthread = { \
## (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8))}
##
## A generic context object as used by some functions.
type
gcry_context* {.bycopy.} = object
gcry_ctx_t* = ptr gcry_context
## The data objects used to hold multi precision integers.
type
gcry_mpi* {.bycopy.} = object
gcry_mpi_t* = ptr gcry_mpi
gcry_mpi_point* {.bycopy.} = object
gcry_mpi_point_t* = ptr gcry_mpi_point
##
## #ifndef GCRYPT_NO_DEPRECATED
## typedef struct gcry_mpi *GCRY_MPI GCRY_GCC_ATTR_DEPRECATED;
## typedef struct gcry_mpi *GcryMPI GCRY_GCC_ATTR_DEPRECATED;
## #endif
##
## A structure used for scatter gather hashing.
type
gcry_buffer_t* {.bycopy.} = object
size*: uint ## The allocated size of the buffer or 0.
off*: uint ## Offset into the buffer.
len*: uint ## The used length of the buffer.
data*: pointer ## The buffer.
## Check that the library fulfills the version requirement.
proc gcry_check_version*(req_version: cstring): cstring {.
importc: "gcry_check_version", import_gcrypt.}
## Codes for function dispatchers.
## Codes used with the gcry_control function.
type
gcry_ctl_cmds* {.size: sizeof(cint).} = enum ## Note: 1 .. 2 are not anymore used.
GCRYCTL_CFB_SYNC = 3, GCRYCTL_RESET = 4, ## e.g. for MDs
GCRYCTL_FINALIZE = 5, GCRYCTL_GET_KEYLEN = 6, GCRYCTL_GET_BLKLEN = 7,
GCRYCTL_TEST_ALGO = 8, GCRYCTL_IS_SECURE = 9, GCRYCTL_GET_ASNOID = 10,
GCRYCTL_ENABLE_ALGO = 11, GCRYCTL_DISABLE_ALGO = 12,
GCRYCTL_DUMP_RANDOM_STATS = 13, GCRYCTL_DUMP_SECMEM_STATS = 14,
GCRYCTL_GET_ALGO_NPKEY = 15, GCRYCTL_GET_ALGO_NSKEY = 16,
GCRYCTL_GET_ALGO_NSIGN = 17, GCRYCTL_GET_ALGO_NENCR = 18,
GCRYCTL_SET_VERBOSITY = 19, GCRYCTL_SET_DEBUG_FLAGS = 20,
GCRYCTL_CLEAR_DEBUG_FLAGS = 21, GCRYCTL_USE_SECURE_RNDPOOL = 22,
GCRYCTL_DUMP_MEMORY_STATS = 23, GCRYCTL_INIT_SECMEM = 24,
GCRYCTL_TERM_SECMEM = 25, GCRYCTL_DISABLE_SECMEM_WARN = 27,
GCRYCTL_SUSPEND_SECMEM_WARN = 28, GCRYCTL_RESUME_SECMEM_WARN = 29,
GCRYCTL_DROP_PRIVS = 30, GCRYCTL_ENABLE_M_GUARD = 31, GCRYCTL_START_DUMP = 32,
GCRYCTL_STOP_DUMP = 33, GCRYCTL_GET_ALGO_USAGE = 34, GCRYCTL_IS_ALGO_ENABLED = 35,
GCRYCTL_DISABLE_INTERNAL_LOCKING = 36, GCRYCTL_DISABLE_SECMEM = 37,
GCRYCTL_INITIALIZATION_FINISHED = 38, GCRYCTL_INITIALIZATION_FINISHED_P = 39,
GCRYCTL_ANY_INITIALIZATION_P = 40, GCRYCTL_SET_CBC_CTS = 41, GCRYCTL_SET_CBC_MAC = 42, ## Note: 43 is not anymore used.
GCRYCTL_ENABLE_QUICK_RANDOM = 44, GCRYCTL_SET_RANDOM_SEED_FILE = 45,
GCRYCTL_UPDATE_RANDOM_SEED_FILE = 46, GCRYCTL_SET_THREAD_CBS = 47,
GCRYCTL_FAST_POLL = 48, GCRYCTL_SET_RANDOM_DAEMON_SOCKET = 49,
GCRYCTL_USE_RANDOM_DAEMON = 50, GCRYCTL_FAKED_RANDOM_P = 51,
GCRYCTL_SET_RNDEGD_SOCKET = 52, GCRYCTL_PRINT_CONFIG = 53,
GCRYCTL_OPERATIONAL_P = 54, GCRYCTL_FIPS_MODE_P = 55,
GCRYCTL_FORCE_FIPS_MODE = 56, GCRYCTL_SELFTEST = 57, ## Note: 58 .. 62 are used internally.
GCRYCTL_DISABLE_HWF = 63, GCRYCTL_SET_ENFORCED_FIPS_FLAG = 64,
GCRYCTL_SET_PREFERRED_RNG_TYPE = 65, GCRYCTL_GET_CURRENT_RNG_TYPE = 66,
GCRYCTL_DISABLE_LOCKED_SECMEM = 67, GCRYCTL_DISABLE_PRIV_DROP = 68,
GCRYCTL_SET_CCM_LENGTHS = 69, GCRYCTL_CLOSE_RANDOM_DEVICE = 70,
GCRYCTL_INACTIVATE_FIPS_FLAG = 71, GCRYCTL_REACTIVATE_FIPS_FLAG = 72,
GCRYCTL_SET_SBOX = 73, GCRYCTL_DRBG_REINIT = 74, GCRYCTL_SET_TAGLEN = 75,
GCRYCTL_GET_TAGLEN = 76, GCRYCTL_REINIT_SYSCALL_CLAMP = 77
## Perform various operations defined by CMD.
proc gcry_control*(CMD: gcry_ctl_cmds): gcry_error_t {.varargs,
importc: "gcry_control", import_gcrypt.}
## S-expression management.
## The object to represent an S-expression as used with the public key
## functions.
type
gcry_sexp* {.bycopy.} = object
gcry_sexp_t* = ptr gcry_sexp
##
## #ifndef GCRYPT_NO_DEPRECATED
## typedef struct gcry_sexp *GCRY_SEXP GCRY_GCC_ATTR_DEPRECATED;
## typedef struct gcry_sexp *GcrySexp GCRY_GCC_ATTR_DEPRECATED;
## #endif
##
## The possible values for the S-expression format.
type
gcry_sexp_format* {.size: sizeof(cint).} = enum
GCRYSEXP_FMT_DEFAULT = 0, GCRYSEXP_FMT_CANON = 1, GCRYSEXP_FMT_BASE64 = 2,
GCRYSEXP_FMT_ADVANCED = 3
## Create an new S-expression object from BUFFER of size LENGTH and
## return it in RETSEXP. With AUTODETECT set to 0 the data in BUFFER
## is expected to be in canonized format.
proc gcry_sexp_new*(retsexp: ptr gcry_sexp_t; buffer: pointer; length: uint;
autodetect: cint): gcry_error_t {.importc: "gcry_sexp_new",
import_gcrypt.}
## Same as gcry_sexp_new but allows to pass a FREEFNC which has the
## effect to transfer ownership of BUFFER to the created object.
proc gcry_new_sexp*(s: string): gcry_sexp_t =
## Convenience function to create S-expression object
var param: gcry_sexp_t
check_rc gcry_sexp_new(addr param, s.cstring, 0, 1)
return param
proc gcry_sexp_create*(retsexp: ptr gcry_sexp_t; buffer: pointer; length: uint;
autodetect: cint; freefnc: proc (a1: pointer)): gcry_error_t {.
importc: "gcry_sexp_create", import_gcrypt.}
## Scan BUFFER and return a new S-expression object in RETSEXP. This
## function expects a printf like string in BUFFER.
proc gcry_sexp_sscan*(retsexp: ptr gcry_sexp_t; erroff: ptr uint; buffer: cstring;
length: uint): gcry_error_t {.importc: "gcry_sexp_sscan",
import_gcrypt.}
## Same as gcry_sexp_sscan but expects a string in FORMAT and can thus
## only be used for certain encodings.
proc gcry_sexp_build*(retsexp: ptr gcry_sexp_t; erroff: ptr uint; format: cstring): gcry_error_t {.
varargs, importc: "gcry_sexp_build", import_gcrypt.}
proc gcry_new_data_sexp*(data: string): gcry_sexp_t =
## Convenience function to create S-expression object from a data string
## "(data(value <data>))"
let input = "(data(value " & data & "))"
check_rc gcry_sexp_build(addr result, nil, input.cstring)
## Like gcry_sexp_build, but uses an array instead of variable
## function arguments.
proc gcry_sexp_build_array*(retsexp: ptr gcry_sexp_t; erroff: ptr uint;
format: cstring; arg_list: ptr pointer): gcry_error_t {.
importc: "gcry_sexp_build_array", import_gcrypt.}
## Release the S-expression object SEXP
proc gcry_sexp_release*(sexp: gcry_sexp_t) {.importc: "gcry_sexp_release",
import_gcrypt.}
## Calculate the length of an canonized S-expression in BUFFER and
## check for a valid encoding.
proc gcry_sexp_canon_len*(buffer: ptr cuchar; length: uint; erroff: ptr uint;
errcode: ptr gcry_error_t): uint {.
importc: "gcry_sexp_canon_len", import_gcrypt.}
## Copies the S-expression object SEXP into BUFFER using the format
## specified in MODE.
proc gcry_sexp_sprint*(sexp: gcry_sexp_t; mode: gcry_sexp_format; buffer: pointer; maxlength: uint): uint {.
importc: "gcry_sexp_sprint", import_gcrypt.}
proc fromCString*(p: pointer, len: int): string =
result = newString(len)
copyMem(result.cstring, p, len)
proc gcry_malloc*(n: uint): pointer {.importc: "gcry_malloc", import_gcrypt.}
proc gcry_sexp_sprint*(sexp: gcry_sexp_t; mode: gcry_sexp_format): string =
## Convert S-expression to string
let size = gcry_sexp_sprint(sexp, mode, nil, 0)
let buf = gcry_malloc(size)
discard gcry_sexp_sprint(sexp, mode, buf, size)
return fromCString(buf, size.int - 1)
proc `$`*(sexp: gcry_sexp_t): string =
## Convert S-expression in advanced mode
gcry_sexp_sprint(sexp, GCRYSEXP_FMT_ADVANCED)# [0..^1]
## Dumps the S-expression object A in a format suitable for debugging
## to Libgcrypt's logging stream.
proc gcry_sexp_dump*(a: gcry_sexp_t) {.importc: "gcry_sexp_dump", import_gcrypt.}
proc gcry_sexp_cons*(a: gcry_sexp_t; b: gcry_sexp_t): gcry_sexp_t {.
importc: "gcry_sexp_cons", import_gcrypt.}
proc gcry_sexp_alist*(array: ptr gcry_sexp_t): gcry_sexp_t {.
importc: "gcry_sexp_alist", import_gcrypt.}
proc gcry_sexp_vlist*(a: gcry_sexp_t): gcry_sexp_t {.varargs,
importc: "gcry_sexp_vlist", import_gcrypt.}
proc gcry_sexp_append*(a: gcry_sexp_t; n: gcry_sexp_t): gcry_sexp_t {.
importc: "gcry_sexp_append", import_gcrypt.}
proc gcry_sexp_prepend*(a: gcry_sexp_t; n: gcry_sexp_t): gcry_sexp_t {.
importc: "gcry_sexp_prepend", import_gcrypt.}
## Scan the S-expression for a sublist with a type (the car of the
## list) matching the string TOKEN. If TOKLEN is not 0, the token is
## assumed to be raw memory of this length. The function returns a
## newly allocated S-expression consisting of the found sublist or
## `NULL' when not found.
proc gcry_sexp_find_token*(list: gcry_sexp_t; tok: cstring; toklen: uint): gcry_sexp_t {.
importc: "gcry_sexp_find_token", import_gcrypt.}
## Return the length of the LIST. For a valid S-expression this
## should be at least 1.
proc gcry_sexp_length*(list: gcry_sexp_t): cint {.importc: "gcry_sexp_length",
import_gcrypt.}
## Create and return a new S-expression from the element with index
## NUMBER in LIST. Note that the first element has the index 0. If
## there is no such element, `NULL' is returned.
proc gcry_sexp_nth*(list: gcry_sexp_t; number: cint): gcry_sexp_t {.
importc: "gcry_sexp_nth", import_gcrypt.}
## Create and return a new S-expression from the first element in
## LIST; this called the "type" and should always exist and be a
## string. `NULL' is returned in case of a problem.
proc gcry_sexp_car*(list: gcry_sexp_t): gcry_sexp_t {.importc: "gcry_sexp_car",
import_gcrypt.}
## Create and return a new list form all elements except for the first
## one. Note, that this function may return an invalid S-expression
## because it is not guaranteed, that the type exists and is a string.
## However, for parsing a complex S-expression it might be useful for
## intermediate lists. Returns `NULL' on error.
proc gcry_sexp_cdr*(list: gcry_sexp_t): gcry_sexp_t {.importc: "gcry_sexp_cdr",
import_gcrypt.}
proc gcry_sexp_cadr*(list: gcry_sexp_t): gcry_sexp_t {.importc: "gcry_sexp_cadr",
import_gcrypt.}
## This function is used to get data from a LIST. A pointer to the
## actual data with index NUMBER is returned and the length of this
## data will be stored to DATALEN. If there is no data at the given
## index or the index represents another list, `NULL' is returned.
## Note:* The returned pointer is valid as long as LIST is not
## modified or released.
proc gcry_sexp_nth_data*(list: gcry_sexp_t; number: cint; datalen: ptr uint): cstring {.
importc: "gcry_sexp_nth_data", import_gcrypt.}
## This function is used to get data from a LIST. A malloced buffer to the
## data with index NUMBER is returned and the length of this
## data will be stored to RLENGTH. If there is no data at the given
## index or the index represents another list, `NULL' is returned.
proc gcry_sexp_nth_buffer*(list: gcry_sexp_t; number: cint; rlength: ptr uint): pointer {.
importc: "gcry_sexp_nth_buffer", import_gcrypt.}
## This function is used to get and convert data from a LIST. The
## data is assumed to be a Nul terminated string. The caller must
## release the returned value using `gcry_free'. If there is no data
## at the given index, the index represents a list or the value can't
## be converted to a string, `NULL' is returned.
proc gcry_sexp_nth_string*(list: gcry_sexp_t; number: cint): cstring {.
importc: "gcry_sexp_nth_string", import_gcrypt.}
## This function is used to get and convert data from a LIST. This
## data is assumed to be an MPI stored in the format described by
## MPIFMT and returned as a standard Libgcrypt MPI. The caller must
## release this returned value using `gcry_mpi_release'. If there is
## no data at the given index, the index represents a list or the
## value can't be converted to an MPI, `NULL' is returned.
proc gcry_sexp_nth_mpi*(list: gcry_sexp_t; number: cint; mpifmt: cint): gcry_mpi_t {.
importc: "gcry_sexp_nth_mpi", import_gcrypt.}
## Extract MPIs from an s-expression using a list of parameters. The
## names of these parameters are given by the string LIST. Some
## special characters may be given to control the conversion:
##
## + :: Switch to unsigned integer format (default).
## - :: Switch to standard signed format.
## / :: Switch to opaque format.
## & :: Switch to buffer descriptor mode - see below.
## ? :: The previous parameter is optional.
##
## In general parameter names are single letters. To use a string for
## a parameter name, enclose the name in single quotes.
##
## Unless in gcry_buffer_t mode for each parameter name a pointer to
## an MPI variable is expected that must be set to NULL prior to
## invoking this function, and finally a NULL is expected. Example:
##
## gcry_sexp_extract_param (key, NULL, "n/x+ed",
## &mpi_n, &mpi_x, &mpi_e, NULL)
##
## This stores the parameter "N" from KEY as an unsigned MPI into
## MPI_N, the parameter "X" as an opaque MPI into MPI_X, and the
## parameter "E" again as an unsigned MPI into MPI_E.
##
## If in buffer descriptor mode a pointer to gcry_buffer_t descriptor
## is expected instead of a pointer to an MPI. The caller may use two
## different operation modes: If the DATA field of the provided buffer
## descriptor is NULL, the function allocates a new buffer and stores
## it at DATA; the other fields are set accordingly with OFF being 0.
## If DATA is not NULL, the function assumes that DATA, SIZE, and OFF
## describe a buffer where to but the data; on return the LEN field
## receives the number of bytes copied to that buffer; if the buffer
## is too small, the function immediately returns with an error code
## (and LEN set to 0).
##
## PATH is an optional string used to locate a token. The exclamation
## mark separated tokens are used to via gcry_sexp_find_token to find
## a start point inside SEXP.
##
## The function returns 0 on success. On error an error code is
## returned, all passed MPIs that might have been allocated up to this
## point are deallocated and set to NULL, and all passed buffers are
## either truncated if the caller supplied the buffer, or deallocated
## if the function allocated the buffer.
##
##
## gpg_error_t gcry_sexp_extract_param (gcry_sexp_t sexp,
## const char *path,
## const char *list,
## ...) GCRY_GCC_ATTR_SENTINEL(0);
##
## ******************************************
## *
## Multi Precision Integer Functions *
## *
## *****************************************
## Different formats of external big integer representation.
type
gcry_mpi_format* {.size: sizeof(cint).} = enum
GCRYMPI_FMT_NONE = 0, GCRYMPI_FMT_STD = 1, ## Twos complement stored without length.
GCRYMPI_FMT_PGP = 2, ## As used by OpenPGP (unsigned only).
GCRYMPI_FMT_SSH = 3, ## As used by SSH (like STD but with length).
GCRYMPI_FMT_HEX = 4, ## Hex format.
GCRYMPI_FMT_USG = 5, ## Like STD but unsigned.
GCRYMPI_FMT_OPAQUE = 8
## Flags used for creating big integers.
type
gcry_mpi_flag* {.size: sizeof(cint).} = enum
GCRYMPI_FLAG_SECURE = 1, ## Allocate the number in "secure" memory.
GCRYMPI_FLAG_OPAQUE = 2, ## The number is not a real one but just
## a way to store some bytes. This is
## useful for encrypted big integers.
GCRYMPI_FLAG_IMMUTABLE = 4, ## Mark the MPI as immutable.
GCRYMPI_FLAG_CONST = 8, ## Mark the MPI as a constant.
GCRYMPI_FLAG_USER1 = 0x00000100, ## User flag 1.
GCRYMPI_FLAG_USER2 = 0x00000200, ## User flag 2.
GCRYMPI_FLAG_USER3 = 0x00000400, ## User flag 3.
GCRYMPI_FLAG_USER4 = 0x00000800
## Macros to return pre-defined MPI constants.
#proc gcry_mpi_get_const*(no: cint): gcry_mpi_t {.importc: "_gcry_mpi_get_const", import_gcrypt.}
#
#let
# GCRYMPI_CONST_ONE* = (gcry_mpi_get_const(1))
# GCRYMPI_CONST_TWO* = (gcry_mpi_get_const(2))
# GCRYMPI_CONST_THREE* = (gcry_mpi_get_const(3))
# GCRYMPI_CONST_FOUR* = (gcry_mpi_get_const(4))
# GCRYMPI_CONST_EIGHT* = (gcry_mpi_get_const(8))
## Allocate a new big integer object, initialize it with 0 and
## initially allocate memory for a number of at least NBITS.
proc gcry_mpi_new*(nbits: cuint): gcry_mpi_t {.importc: "gcry_mpi_new", import_gcrypt.}
## Same as gcry_mpi_new() but allocate in "secure" memory.
proc gcry_mpi_snew*(nbits: cuint): gcry_mpi_t {.importc: "gcry_mpi_snew", import_gcrypt.}
## Release the number A and free all associated resources.
proc gcry_mpi_release*(a: gcry_mpi_t) {.importc: "gcry_mpi_release", import_gcrypt.}
## Create a new number with the same value as A.
proc gcry_mpi_copy*(a: gcry_mpi_t): gcry_mpi_t {.importc: "gcry_mpi_copy", import_gcrypt.}
## Store the big integer value U in W and release U.
proc gcry_mpi_snatch*(w: gcry_mpi_t; u: gcry_mpi_t) {.importc: "gcry_mpi_snatch",
import_gcrypt.}
## Store the big integer value U in W.
proc gcry_mpi_set*(w: gcry_mpi_t; u: gcry_mpi_t): gcry_mpi_t {.
importc: "gcry_mpi_set", import_gcrypt.}
## Store the unsigned integer value U in W.
proc gcry_mpi_set_ui*(w: gcry_mpi_t; u: culong): gcry_mpi_t {.
importc: "gcry_mpi_set_ui", import_gcrypt.}
## Swap the values of A and B.
proc gcry_mpi_swap*(a: gcry_mpi_t; b: gcry_mpi_t) {.importc: "gcry_mpi_swap",
import_gcrypt.}
## Return 1 if A is negative; 0 if zero or positive.
proc gcry_mpi_is_neg*(a: gcry_mpi_t): cint {.importc: "gcry_mpi_is_neg", import_gcrypt.}
## W = - U
proc gcry_mpi_neg*(w: gcry_mpi_t; u: gcry_mpi_t) {.importc: "gcry_mpi_neg", import_gcrypt.}
## W = [W]
proc gcry_mpi_abs*(w: gcry_mpi_t) {.importc: "gcry_mpi_abs", import_gcrypt.}
## Compare the big integer number U and V returning 0 for equality, a
## positive value for U > V and a negative for U < V.
proc gcry_mpi_cmp*(u: gcry_mpi_t; v: gcry_mpi_t): cint {.importc: "gcry_mpi_cmp",
import_gcrypt.}
## Compare the big integer number U with the unsigned integer V
## returning 0 for equality, a positive value for U > V and a negative
## for U < V.
proc gcry_mpi_cmp_ui*(u: gcry_mpi_t; v: culong): cint {.importc: "gcry_mpi_cmp_ui",
import_gcrypt.}
## Convert the external representation of an integer stored in BUFFER
## with a length of BUFLEN into a newly create MPI returned in
## RET_MPI. If NSCANNED is not NULL, it will receive the number of
## bytes actually scanned after a successful operation.
proc gcry_mpi_scan*(ret_mpi: ptr gcry_mpi_t; format: gcry_mpi_format; buffer: pointer;
buflen: uint; nscanned: ptr uint): gcry_error_t {.
importc: "gcry_mpi_scan", import_gcrypt.}
## Convert the big integer A into the external representation
## described by FORMAT and store it in the provided BUFFER which has
## been allocated by the user with a size of BUFLEN bytes. NWRITTEN
## receives the actual length of the external representation unless it
## has been passed as NULL.
proc gcry_mpi_print*(format: gcry_mpi_format; buffer: ptr cuchar; buflen: uint;
nwritten: ptr uint; a: gcry_mpi_t): gcry_error_t {.
importc: "gcry_mpi_print", import_gcrypt.}
## Convert the big integer A into the external representation described
## by FORMAT and store it in a newly allocated buffer which address
## will be put into BUFFER. NWRITTEN receives the actual lengths of the
## external representation.
proc gcry_mpi_aprint*(format: gcry_mpi_format; buffer: ptr ptr cuchar;
nwritten: ptr uint; a: gcry_mpi_t): gcry_error_t {.
importc: "gcry_mpi_aprint", import_gcrypt.}
## Dump the value of A in a format suitable for debugging to
## Libgcrypt's logging stream. Note that one leading space but no
## trailing space or linefeed will be printed. It is okay to pass
## NULL for A.
proc gcry_mpi_dump*(a: gcry_mpi_t) {.importc: "gcry_mpi_dump", import_gcrypt.}
## W = U + V.
proc gcry_mpi_add*(w: gcry_mpi_t; u: gcry_mpi_t; v: gcry_mpi_t) {.
importc: "gcry_mpi_add", import_gcrypt.}
## W = U + V. V is an unsigned integer.
proc gcry_mpi_add_ui*(w: gcry_mpi_t; u: gcry_mpi_t; v: culong) {.
importc: "gcry_mpi_add_ui", import_gcrypt.}
## W = U + V mod M.
proc gcry_mpi_addm*(w: gcry_mpi_t; u: gcry_mpi_t; v: gcry_mpi_t; m: gcry_mpi_t) {.
importc: "gcry_mpi_addm", import_gcrypt.}
## W = U - V.
proc gcry_mpi_sub*(w: gcry_mpi_t; u: gcry_mpi_t; v: gcry_mpi_t) {.
importc: "gcry_mpi_sub", import_gcrypt.}
## W = U - V. V is an unsigned integer.
proc gcry_mpi_sub_ui*(w: gcry_mpi_t; u: gcry_mpi_t; v: culong) {.
importc: "gcry_mpi_sub_ui", import_gcrypt.}
## W = U - V mod M
proc gcry_mpi_subm*(w: gcry_mpi_t; u: gcry_mpi_t; v: gcry_mpi_t; m: gcry_mpi_t) {.
importc: "gcry_mpi_subm", import_gcrypt.}
## W = U * V.
proc gcry_mpi_mul*(w: gcry_mpi_t; u: gcry_mpi_t; v: gcry_mpi_t) {.
importc: "gcry_mpi_mul", import_gcrypt.}
## W = U * V. V is an unsigned integer.
proc gcry_mpi_mul_ui*(w: gcry_mpi_t; u: gcry_mpi_t; v: culong) {.
importc: "gcry_mpi_mul_ui", import_gcrypt.}
## W = U * V mod M.
proc gcry_mpi_mulm*(w: gcry_mpi_t; u: gcry_mpi_t; v: gcry_mpi_t; m: gcry_mpi_t) {.
importc: "gcry_mpi_mulm", import_gcrypt.}
## W = U * (2 ^ CNT).
proc gcry_mpi_mul_2exp*(w: gcry_mpi_t; u: gcry_mpi_t; cnt: culong) {.
importc: "gcry_mpi_mul_2exp", import_gcrypt.}
## Q = DIVIDEND / DIVISOR, R = DIVIDEND % DIVISOR,
## Q or R may be passed as NULL. ROUND should be negative or 0.
proc gcry_mpi_div*(q: gcry_mpi_t; r: gcry_mpi_t; dividend: gcry_mpi_t;
divisor: gcry_mpi_t; round: cint) {.importc: "gcry_mpi_div",
import_gcrypt.}
## R = DIVIDEND % DIVISOR
proc gcry_mpi_mod*(r: gcry_mpi_t; dividend: gcry_mpi_t; divisor: gcry_mpi_t) {.
importc: "gcry_mpi_mod", import_gcrypt.}
## W = B ^ E mod M.
proc gcry_mpi_powm*(w: gcry_mpi_t; b: gcry_mpi_t; e: gcry_mpi_t; m: gcry_mpi_t) {.
importc: "gcry_mpi_powm", import_gcrypt.}
## Set G to the greatest common divisor of A and B.
## Return true if the G is 1.
proc gcry_mpi_gcd*(g: gcry_mpi_t; a: gcry_mpi_t; b: gcry_mpi_t): cint {.
importc: "gcry_mpi_gcd", import_gcrypt.}
## Set X to the multiplicative inverse of A mod M.
## Return true if the value exists.
proc gcry_mpi_invm*(x: gcry_mpi_t; a: gcry_mpi_t; m: gcry_mpi_t): cint {.
importc: "gcry_mpi_invm", import_gcrypt.}
## Create a new point object. NBITS is usually 0.
proc gcry_mpi_point_new*(nbits: cuint): gcry_mpi_point_t {.
importc: "gcry_mpi_point_new", import_gcrypt.}
## Release the object POINT. POINT may be NULL.
proc gcry_mpi_point_release*(point: gcry_mpi_point_t) {.
importc: "gcry_mpi_point_release", import_gcrypt.}
## Return a copy of POINT.
proc gcry_mpi_point_copy*(point: gcry_mpi_point_t): gcry_mpi_point_t {.
importc: "gcry_mpi_point_copy", import_gcrypt.}
## Store the projective coordinates from POINT into X, Y, and Z.
proc gcry_mpi_point_get*(x: gcry_mpi_t; y: gcry_mpi_t; z: gcry_mpi_t;
point: gcry_mpi_point_t) {.importc: "gcry_mpi_point_get",
import_gcrypt.}
## Store the projective coordinates from POINT into X, Y, and Z and
## release POINT.
proc gcry_mpi_point_snatch_get*(x: gcry_mpi_t; y: gcry_mpi_t; z: gcry_mpi_t;
point: gcry_mpi_point_t) {.
importc: "gcry_mpi_point_snatch_get", import_gcrypt.}
## Store the projective coordinates X, Y, and Z into POINT.
proc gcry_mpi_point_set*(point: gcry_mpi_point_t; x: gcry_mpi_t; y: gcry_mpi_t;
z: gcry_mpi_t): gcry_mpi_point_t {.
importc: "gcry_mpi_point_set", import_gcrypt.}
## Store the projective coordinates X, Y, and Z into POINT and release
## X, Y, and Z.
proc gcry_mpi_point_snatch_set*(point: gcry_mpi_point_t; x: gcry_mpi_t;
y: gcry_mpi_t; z: gcry_mpi_t): gcry_mpi_point_t {.
importc: "gcry_mpi_point_snatch_set", import_gcrypt.}
## Allocate a new context for elliptic curve operations based on the
## parameters given by KEYPARAM or using CURVENAME.
proc gcry_mpi_ec_new*(r_ctx: ptr gcry_ctx_t; keyparam: gcry_sexp_t; curvename: cstring): gpg_error_t {.
importc: "gcry_mpi_ec_new", import_gcrypt.}
## Get a named MPI from an elliptic curve context.
proc gcry_mpi_ec_get_mpi*(name: cstring; ctx: gcry_ctx_t; copy: cint): gcry_mpi_t {.
importc: "gcry_mpi_ec_get_mpi", import_gcrypt.}
## Get a named point from an elliptic curve context.
proc gcry_mpi_ec_get_point*(name: cstring; ctx: gcry_ctx_t; copy: cint): gcry_mpi_point_t {.
importc: "gcry_mpi_ec_get_point", import_gcrypt.}
## Store a named MPI into an elliptic curve context.
proc gcry_mpi_ec_set_mpi*(name: cstring; newvalue: gcry_mpi_t; ctx: gcry_ctx_t): gpg_error_t {.
importc: "gcry_mpi_ec_set_mpi", import_gcrypt.}
## Store a named point into an elliptic curve context.
proc gcry_mpi_ec_set_point*(name: cstring; newvalue: gcry_mpi_point_t;
ctx: gcry_ctx_t): gpg_error_t {.
importc: "gcry_mpi_ec_set_point", import_gcrypt.}
## Decode and store VALUE into RESULT.
proc gcry_mpi_ec_decode_point*(result: gcry_mpi_point_t; value: gcry_mpi_t;
ctx: gcry_ctx_t): gpg_error_t {.
importc: "gcry_mpi_ec_decode_point", import_gcrypt.}
## Store the affine coordinates of POINT into X and Y.
proc gcry_mpi_ec_get_affine*(x: gcry_mpi_t; y: gcry_mpi_t; point: gcry_mpi_point_t;
ctx: gcry_ctx_t): cint {.
importc: "gcry_mpi_ec_get_affine", import_gcrypt.}
## W = 2 * U.
proc gcry_mpi_ec_dup*(w: gcry_mpi_point_t; u: gcry_mpi_point_t; ctx: gcry_ctx_t) {.
importc: "gcry_mpi_ec_dup", import_gcrypt.}
## W = U + V.
proc gcry_mpi_ec_add*(w: gcry_mpi_point_t; u: gcry_mpi_point_t; v: gcry_mpi_point_t;
ctx: gcry_ctx_t) {.importc: "gcry_mpi_ec_add", import_gcrypt.}
## W = U - V.
proc gcry_mpi_ec_sub*(w: gcry_mpi_point_t; u: gcry_mpi_point_t; v: gcry_mpi_point_t;
ctx: gcry_ctx_t) {.importc: "gcry_mpi_ec_sub", import_gcrypt.}
## W = N * U.
proc gcry_mpi_ec_mul*(w: gcry_mpi_point_t; n: gcry_mpi_t; u: gcry_mpi_point_t;
ctx: gcry_ctx_t) {.importc: "gcry_mpi_ec_mul", import_gcrypt.}
## Return true if POINT is on the curve described by CTX.
proc gcry_mpi_ec_curve_point*(w: gcry_mpi_point_t; ctx: gcry_ctx_t): cint {.
importc: "gcry_mpi_ec_curve_point", import_gcrypt.}
## Return the number of bits required to represent A.
proc gcry_mpi_get_nbits*(a: gcry_mpi_t): cuint {.importc: "gcry_mpi_get_nbits",
import_gcrypt.}
## Return true when bit number N (counting from 0) is set in A.
proc gcry_mpi_test_bit*(a: gcry_mpi_t; n: cuint): cint {.importc: "gcry_mpi_test_bit",
import_gcrypt.}
## Set bit number N in A.
proc gcry_mpi_set_bit*(a: gcry_mpi_t; n: cuint) {.importc: "gcry_mpi_set_bit",
import_gcrypt.}
## Clear bit number N in A.
proc gcry_mpi_clear_bit*(a: gcry_mpi_t; n: cuint) {.importc: "gcry_mpi_clear_bit",
import_gcrypt.}
## Set bit number N in A and clear all bits greater than N.
proc gcry_mpi_set_highbit*(a: gcry_mpi_t; n: cuint) {.
importc: "gcry_mpi_set_highbit", import_gcrypt.}
## Clear bit number N in A and all bits greater than N.
proc gcry_mpi_clear_highbit*(a: gcry_mpi_t; n: cuint) {.
importc: "gcry_mpi_clear_highbit", import_gcrypt.}
## Shift the value of A by N bits to the right and store the result in X.
proc gcry_mpi_rshift*(x: gcry_mpi_t; a: gcry_mpi_t; n: cuint) {.
importc: "gcry_mpi_rshift", import_gcrypt.}
## Shift the value of A by N bits to the left and store the result in X.
proc gcry_mpi_lshift*(x: gcry_mpi_t; a: gcry_mpi_t; n: cuint) {.
importc: "gcry_mpi_lshift", import_gcrypt.}
## Store NBITS of the value P points to in A and mark A as an opaque
## value. On success A received the the ownership of the value P.
## WARNING: Never use an opaque MPI for anything thing else than
## gcry_mpi_release, gcry_mpi_get_opaque.
proc gcry_mpi_set_opaque*(a: gcry_mpi_t; p: pointer; nbits: cuint): gcry_mpi_t {.
importc: "gcry_mpi_set_opaque", import_gcrypt.}
## Store NBITS of the value P points to in A and mark A as an opaque
## value. The function takes a copy of the provided value P.
## WARNING: Never use an opaque MPI for anything thing else than
## gcry_mpi_release, gcry_mpi_get_opaque.
proc gcry_mpi_set_opaque_copy*(a: gcry_mpi_t; p: pointer; nbits: cuint): gcry_mpi_t {.
importc: "gcry_mpi_set_opaque_copy", import_gcrypt.}
## Return a pointer to an opaque value stored in A and return its size
## in NBITS. Note that the returned pointer is still owned by A and
## that the function should never be used for an non-opaque MPI.
proc gcry_mpi_get_opaque*(a: gcry_mpi_t; nbits: ptr cuint): pointer {.
importc: "gcry_mpi_get_opaque", import_gcrypt.}
## Set the FLAG for the big integer A. Currently only the flag
## GCRYMPI_FLAG_SECURE is allowed to convert A into an big intger
## stored in "secure" memory.
proc gcry_mpi_set_flag*(a: gcry_mpi_t; flag: gcry_mpi_flag) {.
importc: "gcry_mpi_set_flag", import_gcrypt.}
## Clear FLAG for the big integer A. Note that this function is
## currently useless as no flags are allowed.
proc gcry_mpi_clear_flag*(a: gcry_mpi_t; flag: gcry_mpi_flag) {.
importc: "gcry_mpi_clear_flag", import_gcrypt.}
## Return true if the FLAG is set for A.
proc gcry_mpi_get_flag*(a: gcry_mpi_t; flag: gcry_mpi_flag): cint {.
importc: "gcry_mpi_get_flag", import_gcrypt.}
## Private function - do not use.
## Unless the GCRYPT_NO_MPI_MACROS is used, provide a couple of
## convenience macros for the big integer functions.
when not defined(GCRYPT_NO_MPI_MACROS):
template mpi_new*(n: untyped): untyped =
gcry_mpi_new((n))
template mpi_secure_new*(n: untyped): untyped =
gcry_mpi_snew((n))
template mpi_release*(a: untyped): void =
while true:
gcry_mpi_release((a))
(a) = nil
if not 0:
break
template mpi_copy*(a: untyped): untyped =
gcry_mpi_copy((a))
template mpi_snatch*(w, u: untyped): untyped =
gcry_mpi_snatch((w), (u))
template mpi_set*(w, u: untyped): untyped =
gcry_mpi_set((w), (u))
template mpi_set_ui*(w, u: untyped): untyped =
gcry_mpi_set_ui((w), (u))
template mpi_abs*(w: untyped): untyped =
gcry_mpi_abs((w))
template mpi_neg*(w, u: untyped): untyped =
gcry_mpi_neg((w), (u))
template mpi_cmp*(u, v: untyped): untyped =
gcry_mpi_cmp((u), (v))
template mpi_cmp_ui*(u, v: untyped): untyped =
gcry_mpi_cmp_ui((u), (v))
template mpi_is_neg*(a: untyped): untyped =
gcry_mpi_is_neg((a))
template mpi_add_ui*(w, u, v: untyped): untyped =
gcry_mpi_add_ui((w), (u), (v))
template mpi_add*(w, u, v: untyped): untyped =
gcry_mpi_add((w), (u), (v))
template mpi_addm*(w, u, v, m: untyped): untyped =
gcry_mpi_addm((w), (u), (v), (m))
template mpi_sub_ui*(w, u, v: untyped): untyped =
gcry_mpi_sub_ui((w), (u), (v))
template mpi_sub*(w, u, v: untyped): untyped =
gcry_mpi_sub((w), (u), (v))
template mpi_subm*(w, u, v, m: untyped): untyped =
gcry_mpi_subm((w), (u), (v), (m))
template mpi_mul_ui*(w, u, v: untyped): untyped =
gcry_mpi_mul_ui((w), (u), (v))
template mpi_mul_2exp*(w, u, v: untyped): untyped =
gcry_mpi_mul_2exp((w), (u), (v))
template mpi_mul*(w, u, v: untyped): untyped =
gcry_mpi_mul((w), (u), (v))
template mpi_mulm*(w, u, v, m: untyped): untyped =
gcry_mpi_mulm((w), (u), (v), (m))
template mpi_powm*(w, b, e, m: untyped): untyped =
gcry_mpi_powm((w), (b), (e), (m))
template mpi_tdiv*(q, r, a, m: untyped): untyped =
gcry_mpi_div((q), (r), (a), (m), 0)
template mpi_fdiv*(q, r, a, m: untyped): untyped =
gcry_mpi_div((q), (r), (a), (m), -1)
template mpi_mod*(r, a, m: untyped): untyped =
gcry_mpi_mod((r), (a), (m))
template mpi_gcd*(g, a, b: untyped): untyped =
gcry_mpi_gcd((g), (a), (b))
template mpi_invm*(g, a, b: untyped): untyped =
gcry_mpi_invm((g), (a), (b))
template mpi_point_new*(n: untyped): untyped =
gcry_mpi_point_new((n))
template mpi_point_release*(p: untyped): void =
while true:
gcry_mpi_point_release((p))
(p) = nil
if not 0:
break
template mpi_point_copy*(p: untyped): untyped =
gcry_mpi_point_copy((p))
template mpi_point_get*(x, y, z, p: untyped): untyped =
gcry_mpi_point_get((x), (y), (z), (p))
template mpi_point_snatch_get*(x, y, z, p: untyped): untyped =
gcry_mpi_point_snatch_get((x), (y), (z), (p))
template mpi_point_set*(p, x, y, z: untyped): untyped =
gcry_mpi_point_set((p), (x), (y), (z))
template mpi_point_snatch_set*(p, x, y, z: untyped): untyped =
gcry_mpi_point_snatch_set((p), (x), (y), (z))
template mpi_get_nbits*(a: untyped): untyped =
gcry_mpi_get_nbits((a))
template mpi_test_bit*(a, b: untyped): untyped =
gcry_mpi_test_bit((a), (b))
template mpi_set_bit*(a, b: untyped): untyped =
gcry_mpi_set_bit((a), (b))
template mpi_set_highbit*(a, b: untyped): untyped =
gcry_mpi_set_highbit((a), (b))
template mpi_clear_bit*(a, b: untyped): untyped =
gcry_mpi_clear_bit((a), (b))