devolutions-crypto 0.9.2

An abstraction layer for the cryptography used by Devolutions
Documentation
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
#pragma warning disable SA1600 // Elements should be documented

namespace Devolutions.Cryptography
{
    using System;
    using System.Runtime.InteropServices;
#if WIN
    using System.IO;
#endif
#if !DEBUG
    using System.Reflection;
#endif

    /// <summary>
    /// Contains the bindings to the native rust library.
    /// </summary>
    public static partial class Native
    {
#if WIN
        [DllImport("Kernel32", SetLastError = true)]
        public static extern IntPtr LoadLibrary(string path);

        private const string LibName64 = "DevolutionsCrypto";
        private const string LibName86 = "DevolutionsCrypto";
#endif

#if !ANDROID && !IOS && !MACOS && !WIN && !NETCOREAPP && !NETSTANDARD
        private const string LibName64 = "DevolutionsCrypto-x64";

        private const string LibName86 = "DevolutionsCrypto-x86";
#endif

#if !DEBUG
        private const string NativeVersion = "||NATIVE_VERSION||";
        private const string ManagedVersion = "||MANAGED_VERSION||";
#endif

        static Native()
        {
#if WIN
            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DEVOLUTIONS_CRYPTO_SKIP_NATIVE_PRELOAD")))
            {
                string baseLocation = Assembly.GetEntryAssembly()?.Location ?? Assembly.GetExecutingAssembly().Location;
                string baseDir = Path.GetDirectoryName(baseLocation);
                string rid = "win-" + RuntimeInformation.ProcessArchitecture.ToString().ToLower();
                string path = Path.Combine(
                    baseDir,
                    "runtimes", 
                    rid, 
                    "native",
                    $"{ LibName64 }.dll");

                if (LoadLibrary(path) == IntPtr.Zero)
                {
                    throw new DevolutionsCryptoException(ManagedError.NativeLibraryLoad, $"LoadLibrary failed for { path }");
                }
            }
#endif

#if !DEBUG
            Assembly assembly = Assembly.GetExecutingAssembly();

            Version assemblyVersion = assembly.GetName().Version;
            Version managedVersion = Version.Parse(ManagedVersion);
            
            if(managedVersion.Revision == -1)
            {
                managedVersion = Version.Parse(ManagedVersion + ".0");
            }

            string nativeVersion = Utils.Version();

            if (managedVersion != assemblyVersion || NativeVersion != nativeVersion)
            {
                throw new DevolutionsCryptoException(ManagedError.IncompatibleVersion, "Non-matching versions - Managed: " + assemblyVersion + " Native: " + nativeVersion + " Supported : managed(" + ManagedVersion + ") native (" + NativeVersion + ")");
            }
#endif
        }

        [Obsolete("This method has been deprecated. Use Managed.Decrypt instead.")]
        public static byte[] Decrypt(byte[] data, byte[] key)
        {
            return Managed.Decrypt(data, key);
        }

        [Obsolete("This method has been deprecated. Use Managed.DerivePassword instead.")]
        public static byte[] DerivePassword(string password, string salt, uint iterations = 10000)
        {
            return Managed.DerivePassword(password, salt, iterations);
        }

        [Obsolete("This method has been deprecated. Use Managed.DeriveKey instead.")]
        public static byte[] DeriveKey(byte[] key, byte[] salt, uint iterations = 10000, uint length = 32)
        {
            return Managed.DeriveKey(key, salt, iterations, length);
        }

        [Obsolete("This method has been deprecated. Use Managed.Encrypt instead.")]
        public static byte[] Encrypt(byte[] data, byte[] key, uint version = 0)
        {
            return Managed.Encrypt(data, key, null, (CipherTextVersion)version);
        }

        [Obsolete("This method has been deprecated. Use Managed.GenerateKey instead.")]
        public static byte[] GenerateKey(uint keySize)
        {
            return Managed.GenerateKey(keySize);
        }

        [Obsolete("This method has been deprecated. Use Managed.GenerateKeyPair instead.")]
        public static KeyPair GenerateKeyPair()
        {
            return Managed.GenerateKeyPair();
        }

        [Obsolete("This method has been deprecated. Use Managed.HashPassword instead.")]
        public static byte[] HashPassword(byte[] password, uint iterations = 10000)
        {
            return Managed.HashPassword(password, iterations);
        }

        [Obsolete("This method has been deprecated. Use Managed.HashPassword instead.")]
        public static bool VerifyPassword(byte[] password, byte[] hash)
        {
            return Managed.VerifyPassword(password, hash);
        }

#if !ANDROID && !IOS && !MACOS && !NETCOREAPP && !NETSTANDARD
        internal static long GenerateSharedKeyNative(UIntPtr nbShares, UIntPtr threshold, UIntPtr size, IntPtr[] shares)
        {
            if (Environment.Is64BitProcess)
            {
                return GenerateSharedKey64(nbShares, threshold, size, shares);
            }

            return GenerateSharedKey86(nbShares, threshold, size, shares);
        }

        internal static long JoinSharesNative(UIntPtr nbShares, UIntPtr sharesLength, IntPtr[] shares, byte[] secret, UIntPtr secretLength)
        {
            if (Environment.Is64BitProcess)
            {
                return JoinShares64(nbShares, sharesLength, shares, secret, secretLength);
            }

            return JoinShares86(nbShares, sharesLength, shares, secret, secretLength);
        }

        internal static long JoinSharesSizeNative(UIntPtr size)
        {
            if (Environment.Is64BitProcess)
            {
                return JoinSharesSize64(size);
            }

            return JoinSharesSize86(size);
        }

        internal static long GenerateSharedKeySizeNative(UIntPtr secretLength)
        {
            if (Environment.Is64BitProcess)
            {
                return GenerateSharedKeySize64(secretLength);
            }

            return GenerateSharedKeySize86(secretLength);
        }

        internal static long DecryptNative(
            byte[] data,
            UIntPtr dataLength,
            byte[] key,
            UIntPtr keyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength)
        {
            if (Environment.Is64BitProcess)
            {
                return DecryptNative64(data, dataLength, key, keyLength, aad, aadLength, result, resultLength);
            }

            return DecryptNative86(data, dataLength, key, keyLength, aad, aadLength, result, resultLength);
        }

        internal static long DecryptAsymmetricNative(
            byte[] data,
            UIntPtr dataLength,
            byte[] privateKey,
            UIntPtr privateKeyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength)
        {
            if (Environment.Is64BitProcess)
            {
                return DecryptAsymmetricNative64(data, dataLength, privateKey, privateKeyLength, aad, aadLength, result, resultLength);
            }

            return DecryptAsymmetricNative86(data, dataLength, privateKey, privateKeyLength, aad, aadLength, result, resultLength);
        }

        internal static long DeriveKeyArgon2Native(byte[] key, UIntPtr keyLength, byte[] argon2Parameters, UIntPtr argon2ParametersLength, byte[] result, UIntPtr resultLength)
        {
            if (Environment.Is64BitProcess)
            {
                return DeriveKeyArgon2Native64(key, keyLength, argon2Parameters, argon2ParametersLength, result, resultLength);
            }

            return DeriveKeyArgon2Native86(key, keyLength, argon2Parameters, argon2ParametersLength, result, resultLength);
        }

        internal static long DeriveKeyPbkdf2Native(byte[] key, UIntPtr keyLength, byte[] salt, UIntPtr saltLength, uint iterations, byte[] result, UIntPtr resultLength)
        {
            if (Environment.Is64BitProcess)
            {
                return DeriveKeyPbkdf2Native64(key, keyLength, salt, saltLength, iterations, result, resultLength);
            }

            return DeriveKeyPbkdf2Native86(key, keyLength, salt, saltLength, iterations, result, resultLength);
        }

        internal static long GetDefaultArgon2ParametersNative(byte[] argon2Parameters, UIntPtr argon2ParametersLength)
        {
            if (Environment.Is64BitProcess)
            {
                return GetDefaultArgon2ParametersNative64(argon2Parameters, argon2ParametersLength);
            }

            return GetDefaultArgon2ParametersNative86(argon2Parameters, argon2ParametersLength);
        }

        internal static long GetDefaultArgon2ParametersSizeNative()
        {
            if (Environment.Is64BitProcess)
            {
                return GetDefaultArgon2ParametersSizeNative64();
            }

            return GetDefaultArgon2ParametersSizeNative86();
        }

        internal static long EncryptNative(
            byte[] data,
            UIntPtr dataLength,
            byte[] key,
            UIntPtr keyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength,
            ushort version)
        {
            if (Environment.Is64BitProcess)
            {
                return EncryptNative64(data, dataLength, key, keyLength, aad, aadLength, result, resultLength, version);
            }

            return EncryptNative86(data, dataLength, key, keyLength, aad, aadLength, result, resultLength, version);
        }

        internal static long EncryptAsymmetricNative(
            byte[] data,
            UIntPtr dataLength,
            byte[] publicKey,
            UIntPtr publicKeyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength,
            ushort version)
        {
            if (Environment.Is64BitProcess)
            {
                return EncryptAsymmetricNative64(data, dataLength, publicKey, publicKeyLength, aad, aadLength, result, resultLength, version);
            }

            return EncryptAsymmetricNative86(data, dataLength, publicKey, publicKeyLength, aad, aadLength, result, resultLength, version);
        }

        internal static long EncryptAsymmetricSizeNative(UIntPtr dataLength, ushort version)
        {
            if (Environment.Is64BitProcess)
            {
                return EncryptAsymmetricSizeNative64(dataLength, version);
            }

            return EncryptAsymmetricSizeNative86(dataLength, version);
        }

        internal static long EncryptSizeNative(UIntPtr dataLength, ushort version)
        {
            if (Environment.Is64BitProcess)
            {
                return EncryptSizeNative64(dataLength, version);
            }

            return EncryptSizeNative86(dataLength, version);
        }

        internal static long GenerateKeyPairNative(byte[] privateKey, UIntPtr privateKeySize, byte[] publicKey, UIntPtr publicKeySize)
        {
            if (Environment.Is64BitProcess)
            {
                return GenerateKeyPairNative64(privateKey, privateKeySize, publicKey, publicKeySize);
            }

            return GenerateKeyPairNative86(privateKey, privateKeySize, publicKey, publicKeySize);
        }

        internal static long GenerateKeyPairSizeNative()
        {
            if (Environment.Is64BitProcess)
            {
                return GenerateKeyPairSizeNative64();
            }

            return GenerateKeyPairSizeNative86();
        }

        internal static long GenerateKeyNative(byte[] key, UIntPtr keyLength)
        {
            if (Environment.Is64BitProcess)
            {
                return GenerateKeyNative64(key, keyLength);
            }

            return GenerateKeyNative86(key, keyLength);
        }

        internal static long HashPasswordLengthNative()
        {
            if (Environment.Is64BitProcess)
            {
                return HashPasswordLengthNative64();
            }

            return HashPasswordLengthNative86();
        }

        internal static long HashPasswordNative(byte[] password, UIntPtr passwordLength, uint iterations, byte[] result, UIntPtr resultLength)
        {
            if (Environment.Is64BitProcess)
            {
                return HashPasswordNative64(password, passwordLength, iterations, result, resultLength);
            }

            return HashPasswordNative86(password, passwordLength, iterations, result, resultLength);
        }

        internal static uint KeySizeNative()
        {
            if (Environment.Is64BitProcess)
            {
                return KeySizeNative64();
            }

            return KeySizeNative86();
        }

        internal static long MixKeyExchangeNative(byte[] privateKey, UIntPtr privateKeySize, byte[] publicKey, UIntPtr publicKeySize, byte[] shared, UIntPtr sharedSize)
        {
            if (Environment.Is64BitProcess)
            {
                return MixKeyExchangeNative64(privateKey, privateKeySize, publicKey, publicKeySize, shared, sharedSize);
            }

            return MixKeyExchangeNative86(privateKey, privateKeySize, publicKey, publicKeySize, shared, sharedSize);
        }

        internal static long MixKeyExchangeSizeNative()
        {
            if (Environment.Is64BitProcess)
            {
                return MixKeyExchangeSizeNative64();
            }

            return MixKeyExchangeSizeNative86();
        }

        internal static long VerifyPasswordNative(byte[] password, UIntPtr passwordLength, byte[] hash, UIntPtr hashLength)
        {
            if (Environment.Is64BitProcess)
            {
                return VerifyPasswordNative64(password, passwordLength, hash, hashLength);
            }

            return VerifyPasswordNative86(password, passwordLength, hash, hashLength);
        }

        internal static long DecodeNative(string input, UIntPtr input_length, byte[] output, UIntPtr output_length)
        {
            if (Environment.Is64BitProcess)
            {
                return Decode64(input, input_length, output, output_length);
            }

            return Decode86(input, input_length, output, output_length);
        }

        internal static long DecodeUrlNative(string input, UIntPtr input_length, byte[] output, UIntPtr output_length)
        {
            if (Environment.Is64BitProcess)
            {
                return DecodeUrl64(input, input_length, output, output_length);
            }

            return DecodeUrl86(input, input_length, output, output_length);
        }

        internal static long EncodeNative(byte[] input, UIntPtr input_length, byte[] output, UIntPtr output_length)
        {
            if (Environment.Is64BitProcess)
            {
                return Encode64(input, input_length, output, output_length);
            }

            return Encode86(input, input_length, output, output_length);
        }

        internal static long EncodeUrlNative(byte[] input, UIntPtr input_length, byte[] output, UIntPtr output_length)
        {
            if (Environment.Is64BitProcess)
            {
                return EncodeUrl64(input, input_length, output, output_length);
            }

            return EncodeUrl86(input, input_length, output, output_length);
        }

        internal static long ValidateHeader(byte[] data, UIntPtr dataLength, ushort dataType)
        {
            if (Environment.Is64BitProcess)
            {
                return ValidateHeader64(data, dataLength, dataType);
            }

            return ValidateHeader86(data, dataLength, dataType);
        }

        internal static long ScryptSimple(byte[] password, UIntPtr passwordLength, byte[] salt, UIntPtr saltLength, byte logN, uint r, uint p, byte[] output, UIntPtr outputLength)
        {
            if (Environment.Is64BitProcess)
            {
                return ScryptSimple64(password, passwordLength, salt, saltLength, logN, r, p, output, outputLength);
            }

            return ScryptSimple86(password, passwordLength, salt, saltLength, logN, r, p, output, outputLength);
        }

        internal static long ScryptSimpleSize()
        {
            if (Environment.Is64BitProcess)
            {
                return ScryptSimpleSize64();
            }

            return ScryptSimpleSize86();
        }

        internal static long Sign(byte[] data, UIntPtr dataLength, byte[] keypair, UIntPtr keypairLength, byte[] result, UIntPtr resultLength, ushort version)
        {
            if (Environment.Is64BitProcess)
            {
                return Sign64(data, dataLength, keypair, keypairLength, result, resultLength, version);
            }

            return Sign86(data, dataLength, keypair, keypairLength, result, resultLength, version);
        }

        internal static long SignSize(ushort version)
        {
            if (Environment.Is64BitProcess)
            {
                return SignSize64(version);
            }

            return SignSize86(version);
        }

        internal static long VerifySignature(byte[] data, UIntPtr dataLength, byte[] publicKey, UIntPtr publicKeyLength, byte[] signature, UIntPtr signatureLength)
        {
            if (Environment.Is64BitProcess)
            {
                return VerifySignature64(data, dataLength, publicKey, publicKeyLength, signature, signatureLength);
            }

            return VerifySignature86(data, dataLength, publicKey, publicKeyLength, signature, signatureLength);
        }

        internal static long VersionNative(byte[] output, UIntPtr outputLength)
        {
            if (Environment.Is64BitProcess)
            {
                return Version64(output, outputLength);
            }

            return Version86(output, outputLength);
        }

        internal static long GenerateSigningKeyPair(byte[] keypair, UIntPtr keypairLength, ushort version)
        {
            if (Environment.Is64BitProcess)
            {
                return GenerateSigningKeyPair64(keypair, keypairLength, version);
            }

            return GenerateSigningKeyPair86(keypair, keypairLength, version);
        }

        internal static long GenerateSigningKeyPairSize(ushort version)
        {
            if (Environment.Is64BitProcess)
            {
                return GenerateSigningKeyPairSize64(version);
            }

            return GenerateSigningKeyPairSize86(version);
        }

        internal static long GetSigningPublicKey(byte[] keypair, UIntPtr keypairLength, byte[] publicKey, UIntPtr publicKeyLength)
        {
            if (Environment.Is64BitProcess)
            {
                return GetSigningPublicKey64(keypair, keypairLength, publicKey, publicKeyLength);
            }

            return GetSigningPublicKey86(keypair, keypairLength, publicKey, publicKeyLength);
        }

        internal static long GetSigningPublicKeySize(byte[] keypair, UIntPtr keypairLength)
        {
            if (Environment.Is64BitProcess)
            {
                return GetSigningPublicKeySize64(keypair, keypairLength);
            }

            return GetSigningPublicKeySize86(keypair, keypairLength);
        }

        internal static long VersionSizeNative()
        {
            if (Environment.Is64BitProcess)
            {
                return VersionSize64();
            }

            return VersionSize86();
        }

        internal static long ConstantTimeEquals(byte[] x, UIntPtr xLength, byte[] y, UIntPtr yLength)
        {
            if (Environment.Is64BitProcess)
            {
                return ConstantTimeEquals64(x, xLength, y, yLength);
            }

            return ConstantTimeEquals86(x, xLength, y, yLength);
        }

        [DllImport(LibName86, EntryPoint = "GenerateKeyExchangeSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateKeyExchangeSizeNative86();

        [DllImport(LibName64, EntryPoint = "GenerateKeyExchangeSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateKeyExchangeSizeNative64();

        [DllImport(LibName64, EntryPoint = "GenerateSharedKey", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateSharedKey64(UIntPtr nbShares, UIntPtr threshold, UIntPtr size, IntPtr[] shares);

        [DllImport(LibName86, EntryPoint = "GenerateSharedKey", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateSharedKey86(UIntPtr nbShares, UIntPtr threshold, UIntPtr size, IntPtr[] shares);

        [DllImport(LibName64, EntryPoint = "GenerateSharedKeySize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateSharedKeySize64(UIntPtr secretLength);

        [DllImport(LibName86, EntryPoint = "GenerateSharedKeySize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateSharedKeySize86(UIntPtr secretLength);

        [DllImport(LibName64, EntryPoint = "JoinShares", CallingConvention = CallingConvention.Cdecl)]
        private static extern long JoinShares64(UIntPtr nbShares, UIntPtr sharesLength, IntPtr[] shares, byte[] secret, UIntPtr secretLength);

        [DllImport(LibName86, EntryPoint = "JoinShares", CallingConvention = CallingConvention.Cdecl)]
        private static extern long JoinShares86(UIntPtr nbShares, UIntPtr sharesLength, IntPtr[] shares, byte[] secret, UIntPtr secretLength);

        [DllImport(LibName64, EntryPoint = "JoinSharesSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long JoinSharesSize64(UIntPtr size);

        [DllImport(LibName86, EntryPoint = "JoinSharesSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long JoinSharesSize86(UIntPtr size);

#pragma warning disable CA2101 // Specify marshaling for P/Invoke string arguments
        [DllImport(LibName86, EntryPoint = "Decode", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        private static extern long Decode86(string input, UIntPtr input_length, byte[] output, UIntPtr output_length);

        [DllImport(LibName64, EntryPoint = "Decode", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        private static extern long Decode64(string input, UIntPtr input_length, byte[] output, UIntPtr output_length);

        [DllImport(LibName86, EntryPoint = "DecodeUrl", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        private static extern long DecodeUrl86(string input, UIntPtr input_length, byte[] output, UIntPtr output_length);

        [DllImport(LibName64, EntryPoint = "DecodeUrl", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        private static extern long DecodeUrl64(string input, UIntPtr input_length, byte[] output, UIntPtr output_length);
#pragma warning restore CA2101 // Specify marshaling for P/Invoke string arguments

        [DllImport(LibName64, EntryPoint = "Decrypt", CallingConvention = CallingConvention.Cdecl)]
        private static extern long DecryptNative64(
            byte[] data,
            UIntPtr dataLength,
            byte[] key,
            UIntPtr keyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength);

        [DllImport(LibName86, EntryPoint = "Decrypt", CallingConvention = CallingConvention.Cdecl)]
        private static extern long DecryptNative86(
            byte[] data,
            UIntPtr dataLength,
            byte[] key,
            UIntPtr keyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength);

        [DllImport(LibName64, EntryPoint = "DecryptAsymmetric", CallingConvention = CallingConvention.Cdecl)]
        private static extern long DecryptAsymmetricNative64(
            byte[] data,
            UIntPtr dataLength,
            byte[] privateKey,
            UIntPtr privateKeyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength);

        [DllImport(LibName86, EntryPoint = "DecryptAsymmetric", CallingConvention = CallingConvention.Cdecl)]
        private static extern long DecryptAsymmetricNative86(
            byte[] data,
            UIntPtr dataLength,
            byte[] privateKey,
            UIntPtr privateKeyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength);

        [DllImport(LibName86, EntryPoint = "DeriveKeyArgon2", CallingConvention = CallingConvention.Cdecl)]
        private static extern long DeriveKeyArgon2Native86(
            byte[] key,
            UIntPtr keyLength,
            byte[] argon2Parameters,
            UIntPtr argon2ParametersLength,
            byte[] result,
            UIntPtr resultLength);

        [DllImport(LibName64, EntryPoint = "DeriveKeyArgon2", CallingConvention = CallingConvention.Cdecl)]
        private static extern long DeriveKeyArgon2Native64(
            byte[] key,
            UIntPtr keyLength,
            byte[] argon2Parameters,
            UIntPtr argon2ParametersLength,
            byte[] result,
            UIntPtr resultLength);

        [DllImport(LibName86, EntryPoint = "DeriveKeyPbkdf2", CallingConvention = CallingConvention.Cdecl)]
        private static extern long DeriveKeyPbkdf2Native86(
            byte[] key,
            UIntPtr keyLength,
            byte[] salt,
            UIntPtr saltLength,
            uint iterations,
            byte[] result,
            UIntPtr resultLength);

        [DllImport(LibName64, EntryPoint = "DeriveKeyPbkdf2", CallingConvention = CallingConvention.Cdecl)]
        private static extern long DeriveKeyPbkdf2Native64(
            byte[] key,
            UIntPtr keyLength,
            byte[] salt,
            UIntPtr saltLength,
            uint iterations,
            byte[] result,
            UIntPtr resultLength);

        [DllImport(LibName86, EntryPoint = "Encode", CallingConvention = CallingConvention.Cdecl)]
        private static extern long Encode86(byte[] input, UIntPtr input_length, byte[] output, UIntPtr output_length);

        [DllImport(LibName64, EntryPoint = "Encode", CallingConvention = CallingConvention.Cdecl)]
        private static extern long Encode64(byte[] input, UIntPtr input_length, byte[] output, UIntPtr output_length);

        [DllImport(LibName86, EntryPoint = "EncodeUrl", CallingConvention = CallingConvention.Cdecl)]
        private static extern long EncodeUrl86(byte[] input, UIntPtr input_length, byte[] output, UIntPtr output_length);

        [DllImport(LibName64, EntryPoint = "EncodeUrl", CallingConvention = CallingConvention.Cdecl)]
        private static extern long EncodeUrl64(byte[] input, UIntPtr input_length, byte[] output, UIntPtr output_length);

        [DllImport(LibName86, EntryPoint = "Encrypt", CallingConvention = CallingConvention.Cdecl)]
        private static extern long EncryptNative86(
            byte[] data,
            UIntPtr dataLength,
            byte[] key,
            UIntPtr keyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength,
            ushort version);

        [DllImport(LibName64, EntryPoint = "Encrypt", CallingConvention = CallingConvention.Cdecl)]
        private static extern long EncryptNative64(
            byte[] data,
            UIntPtr dataLength,
            byte[] key,
            UIntPtr keyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength,
            ushort version);

        [DllImport(LibName86, EntryPoint = "EncryptAsymmetric", CallingConvention = CallingConvention.Cdecl)]
        private static extern long EncryptAsymmetricNative86(
            byte[] data,
            UIntPtr dataLength,
            byte[] publicKey,
            UIntPtr publicKeyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength,
            ushort version);

        [DllImport(LibName64, EntryPoint = "EncryptAsymmetric", CallingConvention = CallingConvention.Cdecl)]
        private static extern long EncryptAsymmetricNative64(
            byte[] data,
            UIntPtr dataLength,
            byte[] publicKey,
            UIntPtr publicKeyLength,
            byte[] aad,
            UIntPtr aadLength,
            byte[] result,
            UIntPtr resultLength,
            ushort version);

        [DllImport(LibName86, EntryPoint = "EncryptAsymmetricSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long EncryptAsymmetricSizeNative86(UIntPtr dataLength, ushort version);

        [DllImport(LibName64, EntryPoint = "EncryptAsymmetricSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long EncryptAsymmetricSizeNative64(UIntPtr dataLength, ushort version);

        [DllImport(LibName86, EntryPoint = "EncryptSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long EncryptSizeNative86(UIntPtr dataLength, ushort version);

        [DllImport(LibName64, EntryPoint = "EncryptSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long EncryptSizeNative64(UIntPtr dataLength, ushort version);

        [DllImport(LibName86, EntryPoint = "GetDefaultArgon2Parameters", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GetDefaultArgon2ParametersNative86(byte[] argon2Parameters, UIntPtr argon2ParametersLength);

        [DllImport(LibName64, EntryPoint = "GetDefaultArgon2Parameters", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GetDefaultArgon2ParametersNative64(byte[] argon2Parameters, UIntPtr argon2ParametersLength);

        [DllImport(LibName86, EntryPoint = "GetDefaultArgon2ParametersSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GetDefaultArgon2ParametersSizeNative86();

        [DllImport(LibName64, EntryPoint = "GetDefaultArgon2ParametersSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GetDefaultArgon2ParametersSizeNative64();

        [DllImport(LibName86, EntryPoint = "GenerateKey", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateKeyNative86(byte[] key, UIntPtr keyLength);

        [DllImport(LibName64, EntryPoint = "GenerateKey", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateKeyNative64(byte[] key, UIntPtr keyLength);

        [DllImport(LibName86, EntryPoint = "GenerateKeyPair", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateKeyPairNative86(byte[] privateKey, UIntPtr privateKeySize, byte[] publicKey, UIntPtr publicKeySize);

        [DllImport(LibName64, EntryPoint = "GenerateKeyPair", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateKeyPairNative64(byte[] privateKey, UIntPtr privateKeySize, byte[] publicKey, UIntPtr publicKeySize);

        [DllImport(LibName86, EntryPoint = "GenerateKeyPairSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateKeyPairSizeNative86();

        [DllImport(LibName64, EntryPoint = "GenerateKeyPairSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateKeyPairSizeNative64();

        [DllImport(LibName86, EntryPoint = "HashPassword", CallingConvention = CallingConvention.Cdecl)]
        private static extern long HashPasswordNative86(byte[] password, UIntPtr passwordLength, uint iterations, byte[] result, UIntPtr resultLength);

        [DllImport(LibName64, EntryPoint = "HashPassword", CallingConvention = CallingConvention.Cdecl)]
        private static extern long HashPasswordNative64(byte[] password, UIntPtr passwordLength, uint iterations, byte[] result, UIntPtr resultLength);

        [DllImport(LibName86, EntryPoint = "HashPasswordLength", CallingConvention = CallingConvention.Cdecl)]
        private static extern long HashPasswordLengthNative86();

        [DllImport(LibName64, EntryPoint = "HashPasswordLength", CallingConvention = CallingConvention.Cdecl)]
        private static extern long HashPasswordLengthNative64();

        [DllImport(LibName86, EntryPoint = "KeySize", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint KeySizeNative86();

        [DllImport(LibName64, EntryPoint = "KeySize", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint KeySizeNative64();

        [DllImport(LibName86, EntryPoint = "MixKeyExchange", CallingConvention = CallingConvention.Cdecl)]
        private static extern long MixKeyExchangeNative86(byte[] privateKey, UIntPtr privateKeySize, byte[] publicKey, UIntPtr publicKeySize, byte[] shared, UIntPtr sharedSize);

        [DllImport(LibName64, EntryPoint = "MixKeyExchange", CallingConvention = CallingConvention.Cdecl)]
        private static extern long MixKeyExchangeNative64(byte[] privateKey, UIntPtr privateKeySize, byte[] publicKey, UIntPtr publicKeySize, byte[] shared, UIntPtr sharedSize);

        [DllImport(LibName86, EntryPoint = "MixKeyExchangeSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long MixKeyExchangeSizeNative86();

        [DllImport(LibName64, EntryPoint = "MixKeyExchangeSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long MixKeyExchangeSizeNative64();

        [DllImport(LibName86, EntryPoint = "ValidateHeader", CallingConvention = CallingConvention.Cdecl)]
        private static extern long ValidateHeader86(byte[] data, UIntPtr dataLength, ushort dataType);

        [DllImport(LibName64, EntryPoint = "ValidateHeader", CallingConvention = CallingConvention.Cdecl)]
        private static extern long ValidateHeader64(byte[] data, UIntPtr dataLength, ushort dataType);

        [DllImport(LibName86, EntryPoint = "ScryptSimple", CallingConvention = CallingConvention.Cdecl)]
        private static extern long ScryptSimple86(
            byte[] password,
            UIntPtr passwordLength,
            byte[] salt,
            UIntPtr saltLength,
            byte logN,
            uint r,
            uint p,
            byte[] output,
            UIntPtr outputLength);

        [DllImport(LibName64, EntryPoint = "ScryptSimple", CallingConvention = CallingConvention.Cdecl)]
        private static extern long ScryptSimple64(
            byte[] password,
            UIntPtr passwordLength,
            byte[] salt,
            UIntPtr saltLength,
            byte logN,
            uint r,
            uint p,
            byte[] output,
            UIntPtr outputLength);

        [DllImport(LibName86, EntryPoint = "ScryptSimpleSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long ScryptSimpleSize86();

        [DllImport(LibName64, EntryPoint = "ScryptSimpleSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long ScryptSimpleSize64();

        [DllImport(LibName64, EntryPoint = "Sign", CallingConvention = CallingConvention.Cdecl)]
        private static extern long Sign64(byte[] data, UIntPtr dataLength, byte[] keypair, UIntPtr keypairLength, byte[] result, UIntPtr resultLength, ushort version);

        [DllImport(LibName86, EntryPoint = "Sign", CallingConvention = CallingConvention.Cdecl)]
        private static extern long Sign86(byte[] data, UIntPtr dataLength, byte[] keypair, UIntPtr keypairLength, byte[] result, UIntPtr resultLength, ushort version);

        [DllImport(LibName64, EntryPoint = "SignSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long SignSize64(ushort version);

        [DllImport(LibName86, EntryPoint = "SignSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long SignSize86(ushort version);

        [DllImport(LibName64, EntryPoint = "VerifySignature", CallingConvention = CallingConvention.Cdecl)]
        private static extern long VerifySignature64(byte[] data, UIntPtr dataLength, byte[] publicKey, UIntPtr publicKeyLength, byte[] signature, UIntPtr signatureLength);

        [DllImport(LibName86, EntryPoint = "VerifySignature", CallingConvention = CallingConvention.Cdecl)]
        private static extern long VerifySignature86(byte[] data, UIntPtr dataLength, byte[] publicKey, UIntPtr publicKeyLength, byte[] signature, UIntPtr signatureLength);

        [DllImport(LibName64, EntryPoint = "GenerateSigningKeyPair", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateSigningKeyPair64(byte[] keypair, UIntPtr keypairLength, ushort version);

        [DllImport(LibName86, EntryPoint = "GenerateSigningKeyPair", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateSigningKeyPair86(byte[] keypair, UIntPtr keypairLength, ushort version);

        [DllImport(LibName64, EntryPoint = "GenerateSigningKeyPairSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateSigningKeyPairSize64(ushort version);

        [DllImport(LibName86, EntryPoint = "GenerateSigningKeyPairSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GenerateSigningKeyPairSize86(ushort version);

        [DllImport(LibName64, EntryPoint = "GetSigningPublicKey", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GetSigningPublicKey64(byte[] keypair, UIntPtr keypairLength, byte[] publicKey, UIntPtr publicKeyLength);

        [DllImport(LibName86, EntryPoint = "GetSigningPublicKey", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GetSigningPublicKey86(byte[] keypair, UIntPtr keypairLength, byte[] publicKey, UIntPtr publicKeyLength);

        [DllImport(LibName64, EntryPoint = "GetSigningPublicKeySize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GetSigningPublicKeySize64(byte[] keypair, UIntPtr keypairLength);

        [DllImport(LibName86, EntryPoint = "GetSigningPublicKeySize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long GetSigningPublicKeySize86(byte[] keypair, UIntPtr keypairLength);

        [DllImport(LibName86, EntryPoint = "Version", CallingConvention = CallingConvention.Cdecl)]
        private static extern long Version86(byte[] output, UIntPtr outputLength);

        [DllImport(LibName64, EntryPoint = "Version", CallingConvention = CallingConvention.Cdecl)]
        private static extern long Version64(byte[] output, UIntPtr outputLength);

        [DllImport(LibName86, EntryPoint = "VersionSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long VersionSize86();

        [DllImport(LibName64, EntryPoint = "VersionSize", CallingConvention = CallingConvention.Cdecl)]
        private static extern long VersionSize64();

        [DllImport(LibName86, EntryPoint = "VerifyPassword", CallingConvention = CallingConvention.Cdecl)]
        private static extern long VerifyPasswordNative86(byte[] password, UIntPtr passwordLength, byte[] hash, UIntPtr hashLength);

        [DllImport(LibName64, EntryPoint = "VerifyPassword", CallingConvention = CallingConvention.Cdecl)]
        private static extern long VerifyPasswordNative64(byte[] password, UIntPtr passwordLength, byte[] hash, UIntPtr hashLength);

        [DllImport(LibName86, EntryPoint = "ConstantTimeEquals", CallingConvention = CallingConvention.Cdecl)]
        private static extern long ConstantTimeEquals86(byte[] x, UIntPtr xLength, byte[] y, UIntPtr yLength);

        [DllImport(LibName64, EntryPoint = "ConstantTimeEquals", CallingConvention = CallingConvention.Cdecl)]
        private static extern long ConstantTimeEquals64(byte[] x, UIntPtr xLength, byte[] y, UIntPtr yLength);
#endif
    }
}
#pragma warning restore SA1600 // Elements should be documented