Repository navigation
Expand file tree
/
Copy pathlibrecrypt.h
More file actions
1613 lines (1500 loc) · 61.7 KB
/
Copy pathlibrecrypt.h
File metadata and controls
1613 lines (1500 loc) · 61.7 KB
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
/* See LICENSE file for copyright and license details. */
#ifndef LIBRECRYPT_H
#define LIBRECRYPT_H
#include <sys/types.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#if defined(__GNUC__)
# define LIBRECRYPT_PURE__ __attribute__((__pure__))
# define LIBRECRYPT_CONST__ __attribute__((__const__))
# define LIBRECRYPT_NONNULL__ __attribute__((__nonnull__))
# define LIBRECRYPT_NONNULL_I__(I) __attribute__((__nonnull__(I)))
# define LIBRECRYPT_WUR__ __attribute__((__warn_unused_result__))
#else
# define LIBRECRYPT_PURE__
# define LIBRECRYPT_CONST__
# define LIBRECRYPT_NONNULL__
# define LIBRECRYPT_NONNULL_I__(I)
# define LIBRECRYPT_WUR__
#endif
#if defined(__GNUC__) && !defined(__clang__)
# define LIBRECRYPT_READ_STR__(S) __attribute__((__access__(read_only, S)))
# define LIBRECRYPT_READ_MEM__(B, N) __attribute__((__access__(read_only, B, N)))
# define LIBRECRYPT_WRITE_MEM__(B, N) __attribute__((__access__(write_only, B, N)))
# define LIBRECRYPT_READ_WRITE_STR__(S) __attribute__((__access__(read_write, S)))
# define LIBRECRYPT_MALLOC__(D, D_ARG) __attribute__((__malloc__(D, D_ARG)))
#else
# define LIBRECRYPT_READ_STR__(S)
# define LIBRECRYPT_READ_MEM__(B, N)
# define LIBRECRYPT_WRITE_MEM__(B, N)
# define LIBRECRYPT_READ_WRITE_STR__(S)
# define LIBRECRYPT_MALLOC__(D, D_ARG)
#endif
#define LIBRECRYPT_NONNULL_1__ LIBRECRYPT_NONNULL_I__(1)
/* Silence clang's warnings for glibc */
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
# pragma clang diagnostic ignored "-Wc11-extensions"
# pragma clang diagnostic ignored "-Wunsafe-buffer-usage" /* as well as this one, which is completely broken */
#endif
#define LIBRECRYPT_VERSION_CMP__(A1, B1, A2, B2, A3, B3)\
((A1) < (B1) ? -3 : (A1) > (B1) ? +3 :\
(A2) < (B2) ? -2 : (A2) > (B2) ? +2 :\
(A3) < (B3) ? -1 : (A3) > (B3) ? +1 : 0)
/**
* The major number of the version of librecrypt
* that that the application is compiled against
*
* Increased when there are backwards incompatible
* changes, necessitating the that the application
* is recompiled or even rewritten
*
* Use `#if !defined(LIBRECRYPT_VERSION_MAJOR)` to
* detect when the version number is lower than 1.2.
* There are no macros defined between 1.0 and 1.2,
* so getting the exact number is not possible.
*
* @seealso librecrypt_version_major
* @seealso LIBRECRYPT_VERSION_MINOR
* @seealso LIBRECRYPT_VERSION_PATCH
* @seealso LIBRECRYPT_VERSION_CMP_BUILD
*
* @since 1.2
*/
#define LIBRECRYPT_VERSION_MAJOR 1
/**
* The minor number of the version of librecrypt
* that that the application is compiled against
*
* Increased when the API is extended, or reset
* to zero when the major number is modified.
*
* @seealso librecrypt_version_minor
* @seealso LIBRECRYPT_VERSION_MAJOR
* @seealso LIBRECRYPT_VERSION_PATCH
* @seealso LIBRECRYPT_VERSION_CMP_BUILD
*
* @since 1.2
*/
#define LIBRECRYPT_VERSION_MINOR 2
/**
* The patch number of the version of librecrypt
* that that the application is compiled against
*
* Increased when the software is modified but
* the API and ABI are unmodified. It is reset
* to zero when the major or minor number is
* modified
*
* @seealso librecrypt_version_patch
* @seealso LIBRECRYPT_VERSION_MAJOR
* @seealso LIBRECRYPT_VERSION_MINOR
* @seealso LIBRECRYPT_VERSION_CMP_BUILD
*
* @since 1.2
*/
#define LIBRECRYPT_VERSION_PATCH 0
/**
* The major number of the version of librecrypt
* that that the application is linked against
*
* See `LIBRECRYPT_VERSION_MAJOR` for more information
*
* Because variable was introduced in 1.2, it
* is impossible to check the exact version if
* it is lower than 1.2, however, using weak
* linkage it is possible to check if the linked
* version is lower than 1.2: in your application
* define this variable with weak linkage. You
* can set it to 0 and let the library replace it
* if it has defined it, or you can simply declare
* it as an external symbol without providing it:
* then taking the address of it will return `NULL`
* if it doesn't exist: you can use this trick with
* any function may be want to use as well.
*
* @seealso LIBRECRYPT_VERSION_MAJOR
* @seealso librecrypt_version_minor
* @seealso librecrypt_version_patch
* @seealso LIBRECRYPT_VERSION_CMP_LINK
*
* @since 1.2
*/
extern const int librecrypt_version_major;
/**
* The minor number of the version of librecrypt
* that that the application is linked against
*
* See `LIBRECRYPT_VERSION_MINOR` for more information
*
* @seealso LIBRECRYPT_VERSION_MINOR
* @seealso librecrypt_version_major
* @seealso librecrypt_version_patch
* @seealso LIBRECRYPT_VERSION_CMP_LINK
*
* @since 1.2
*/
extern const int librecrypt_version_minor;
/**
* The patch number of the version of librecrypt
* that that the application is linked against
*
* See `LIBRECRYPT_VERSION_PATCH` for more information
*
* @seealso LIBRECRYPT_VERSION_PATCH
* @seealso librecrypt_version_major
* @seealso librecrypt_version_minor
* @seealso LIBRECRYPT_VERSION_CMP_LINK
*
* @since 1.2
*/
extern const int librecrypt_version_patch;
/**
* Compare a specific version against the
* version of librecrypt the application is
* compiled against
*
* @param MAJOR:int The major number of the version to compare against
* @param MINOR:int The minor number of the version to compare against
* @param PATCH:int The patch number of the version to compare against
* @return :int Negative if the specified version is older than the compilation-target,
* positive if the specified version is newer than the compilation-target,
* zero if the compilation-target has the same version number
* (ignore numbers less significant than the patch number,
* which do not affect the software of the library).
* If the versions have different major numbers, the absolue value is 3,
* otherwise if the versions have different minor numbers, the absolue value is 2,
* otherwise if the versions have different patch numbers, the absolue value is 1.
*
* @seealso LIBRECRYPT_VERSION_MAJOR
* @seealso LIBRECRYPT_VERSION_MINOR
* @seealso LIBRECRYPT_VERSION_PATCH
* @seealso LIBRECRYPT_VERSION_CMP_LINK
*
* @since 1.2
*/
#define LIBRECRYPT_VERSION_CMP_BUILD(MAJOR, MINOR, PATCH)\
LIBRECRYPT_VERSION_CMP__(MAJOR, LIBRECRYPT_VERSION_MAJOR,\
MINOR, LIBRECRYPT_VERSION_MINOR,\
PATCH, LIBRECRYPT_VERSION_PATCH)
/**
* Compare a specific version against the
* version of librecrypt the application is
* compiled against
*
* Note that this macro requires the linked version
* be version 1.2 or newer. See `librecrypt_version_major`
* for more information.
*
* @param MAJOR:int The major number of the version to compare against
* @param MINOR:int The minor number of the version to compare against
* @param PATCH:int The patch number of the version to compare against
* @return :int Negative if the specified version is older than the linked version,
* positive if the specified version is newer than the linked version,
* zero if the linked version has the same version number
* (ignore numbers less significant than the patch number,
* which do not affect the software of the library).
* If the versions have different major numbers, the absolue value is 3,
* otherwise if the versions have different minor numbers, the absolue value is 2,
* otherwise if the versions have different patch numbers, the absolue value is 1.
*
* @seealso librecrypt_version_major
* @seealso librecrypt_version_minor
* @seealso librecrypt_version_patch
* @seealso LIBRECRYPT_VERSION_CMP_BUILD
*
* @since 1.2
*/
#define LIBRECRYPT_VERSION_CMP_LINK(MAJOR, MINOR, PATCH)\
LIBRECRYPT_VERSION_CMP__(MAJOR, librecrypt_version_major,\
MINOR, librecrypt_version_minor,\
PATCH, librecrypt_version_patch)
/**
* Symbol used as a general delimiter
*
* @since 1.0
*/
#define LIBRECRYPT_HASH_COMPOSITION_DELIMITER '$'
/**
* Symbol used to delimit algorithms in a chain
*
* @since 1.0
*/
#define LIBRECRYPT_ALGORITHM_LINK_DELIMITER '>'
/**
* Opaque structure for library tweaking
*
* @seealso librecrypt_create_context
*
* @since 1.1
*/
typedef struct librecrypt_context LIBRECRYPT_CONTEXT;
/**
* Hash algorithms that the library might support
*
* @seealso librecrypt_hash_algorithm_end
*
* @since 1.1
*/
enum librecrypt_hash_algorithm {
/**
* Argon2i, version 1.0 ("$argon2i$v=13$", optionally without "$v=13")
*
* @since 1.1
*/
LIBRECRYPT_ARGON2I_V1_0,
/**
* Argon2i, version 1.3 ("$argon2i$v=19$")
*
* @since 1.1
*/
LIBRECRYPT_ARGON2I_V1_3,
/**
* Argon2d, version 1.0 ("$argon2d$v=13$", optionally without "$v=13")
*
* @since 1.1
*/
LIBRECRYPT_ARGON2D_V1_0,
/**
* Argon2d, version 1.3 ("$argon2d$v=19$")
*
* @since 1.1
*/
LIBRECRYPT_ARGON2D_V1_3,
/**
* Argon2id, version 1.0 ("$argon2id$v=13$", optionally without "$v=13")
*
* @since 1.1
*/
LIBRECRYPT_ARGON2ID_V1_0,
/**
* Argon2id, version 1.3 ("$argon2id$v=19$")
*
* @since 1.1
*/
LIBRECRYPT_ARGON2ID_V1_3,
/**
* Argon2ds, version 1.0 ("$argon2ds$v=13$", optionally without "$v=13")
*
* @since 1.1
*/
LIBRECRYPT_ARGON2DS_V1_0,
/**
* Argon2ds, version 1.3 ("$argon2ds$v=19$")
*
* @since 1.1
*/
LIBRECRYPT_ARGON2DS_V1_3,
/** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - **/
/**
* Marks the end of this enum, so you can iterate over it
*
* @since 1.1
*/
LIBRECRYPT_HASH_ALGORITHM_END
};
/**
* Hash algorithm information and implementation
*
* This is provided so you can implement custom
* hash algorithms; must user's will not need this
*
* Current limitations:
*
* - The algorithm must not use the '*' symbol except
* in the way the librecrypt library uses '*' for
* specifying sizes, in bytes (after base64-decoding),
* of randomised data (salt) and the hash result
*
* - The algorithm must not use the '>' symbol
*
* - The hash must be att the end, immediately after
* the last '$' (some expections exists for legacy
* hash algorithms), and empty must be usable to
* specify default hash size
*
* - Salts and hashes must be encoded in some variant
* of base64 where the bytes (represented with the
* most significant bit first) a₇a₆a₅a₄a₃a₂a₁a₀,
* b₇b₆b₅b₄b₃b₂b₁b₀, and c₇c₆c₅c₄c₃c₂c₁c₀ are
* rearranged to a₇a₆a₅a₄a₃a₂, a₁a₀b₇b₆b₅b₄,
* b₃b₂b₁b₀c₇c₆, c₅c₄c₃c₂c₁c₀, and missing bits
* are set to 0; and padding if supported at all,
* is only allowed, up to 3 pad letters, at the
* end to pad the output to a multiple of 4 letters
*
* @since 1.1
*/
struct librecrypt_algorithm {
/**
* Determine if a password hash string
* selects the algorithm
*
* @param settings The password hash string, containing a single algorithm
* @param len The number of bytes in `settings`
* @return A positive value if the string matches the algorithm,
* 0 otherwise
*
* If non-zero is returned for multiple algorithm,
* the first with the highest value wins
*
* This function shall be MT-Safe and AS-Safe
*/
unsigned (*is_algorithm)(const char *settings, size_t len);
/**
* Implements `librecrypt_hash_binary` for a single hash algorithm;
* see `librecrypt_hash_binary` for more information
*
* @param out_buffer See `librecrypt_hash_binary`
* @param size See `librecrypt_hash_binary`
* @param phrase See `librecrypt_hash_binary`; may be `NULL`
* even if `len` is positive, this happens when
* `size` is too small and the hash result will
* not be included, so there is no need to actually
* calculate the hash, however `len` and `settings`
* should still be checked
* @param len See `librecrypt_hash_binary`
* @param settings See `librecrypt_hash_binary`,
* will not contains asterisk-encoding
* @param prefix The length of `settings`, in bytes
* @param ctx See `librecrypt_hash_binary`
* @return 0 on success, -1 on failure
* @throws See `librecrypt_hash_binary`
*
* This function shall be MT-Safe but may be AS-Unsafe
*/
int (*hash)(char *restrict out_buffer, size_t size, const char *phrase, size_t len,
const char *settings, size_t prefix, LIBRECRYPT_CONTEXT *ctx);
/**
* Check whether the hash algorithm is supported for given
* configuration and input
*
* @param phrase The password to hash, may contain NUL bytes;
* may be `NULL` even if `len` is non-zero
* @param len The number of bytes in `phrase`, if `phrase` is `NULL`,
* the function will check that the specified number of
* bytes is supported as well as any byte sequence unless
* `text` is non-zero
* @param text Assume the password is valid UTF-8 text (without NUL bytes)
* iff non-zero; ignored if `phrase` is non-`NULL`
* @param settings The password hash string; it is allowed for algorithm
* tuning parameters, and the hash result, to be omitted
* @param prefix The number of bytes in `settings`
* @param len_out Output parameter for the binary hash size, in bytes
* @return 1 if the configuration is supported and correctly
* configured, 0 otherwise
*
* This function shall be MT-Safe and AS-Safe
*/
int (*test_supported)(const char *phrase, size_t len, int text, const char *settings,
size_t prefix, size_t *len_out);
/**
* See `librecrypt_make_settings`
*
* @param out_buffer See `librecrypt_make_settings`
* @param size See `librecrypt_make_settings`
* @param algorithm See `librecrypt_make_settings`,
* will match the algorithm or be `NULL`
* @param memcost See `librecrypt_make_settings`
* @param timecost See `librecrypt_make_settings`
* @param gensalt See `librecrypt_make_settings`
* @param rng See `librecrypt_make_settings`,
* except the function will not be called
* with `rng` set to `NULL`
* @param user See `librecrypt_make_settings`
* @return See `librecrypt_make_settings`
* @throws See `librecrypt_make_settings`
*
* This function shall be MT-Safe but may be AS-Unsafe
*/
ssize_t (*make_settings)(char *out_buffer, size_t size, const char *algorithm,
size_t memcost, uintmax_t timecost, int gensalt,
ssize_t (*rng)(void *out, size_t n, void *user), void *user);
/**
* Expected argument for the `lut` parameter
* of the `librecrypt_encode` function
*
* This shall repeat a 64 character ASCII
* alphabet 4 times
*/
const char *encoding_lut;
/**
* Expected argument for the `lut` parameter
* of the `librecrypt_decode` function
*
* This shall unique map the letters in
* `.encoding_lut` to there initial position
* in `.encoding_lut` (that's, uniquely to
* the range [0, 63]). All other bytes
* (including `.pad`) shall map to `0xFFu`
*/
const unsigned char *decoding_lut;
/**
* The algoritm's hash result size, in number
* of bytes when using binary encoding
*/
size_t hash_size;
/**
* 1 if `.hash_size` is just a default,
* 0 if `.hash_size` is always used
*
* @since 1.1
* @since 1.2 Proper support for .flexible_hash_size=0
*/
signed char flexible_hash_size;
/**
* Expected argument for the `strict_pad` parameter
* of the `librecrypt_decode` function
*
* Shall be either 1 (always pad when encoding,
* and require padding when decoding) or 0
* (do not pad when encoding, but allow padding
* (provided that `.pad != 0`) when decoding)
*/
signed char strict_pad;
/**
* Expected argument for the `pad` parameter
* of the `librecrypt_decode` function
*
* The pad character, used to pad base64-encoding
* to a multiple of 4 letters, shall be `'\0'` if
* not specified
*/
char pad;
};
/**
* The value `LIBRECRYPT_HASH_ALGORITHM_END` as in the
* version of the library the application is linked against
* (rather than compiled against)
*
* @since 1.1
*/
extern const enum librecrypt_hash_algorithm librecrypt_hash_algorithm_end;
/**
* Get number of bytes in a password hash string
* that make up the algorithm configuration
*
* Some algorithms have flexible hash size which
* is encoded either with an actual hash (its
* length after decoding to binary is checked)
* or using asterisk-notation in place of the
* result (that is, as '*' followed by an unsigned,
* decimal integer, which may have leading zeroes).
* This part is always excluded (from the last
* algorithm in the algorithm chain in `hash`) in
* the return value.
*
* @param hash The password hash string; must not be `NULL`
* @param hashsize_out Unless `NULL`; if the hash size is fixed,
* the value 0 is stored in the provided memory,
* otherwise a non-zero value will be stored,
* which is the number bytes in the output hash,
* however if the a hash or hash size is not
* available (in which case the function shall
* return `strlen(hash)` if `hash` is properly
* formatted) the value 0 is stored, indicating
* that a default hash size shall be used
* @param ctx Library configuration
* @return The number of bytes, from the front of `hash`,
* that make up the algorithm configuration; may be 0
*
* For the return value `r`, `&hash[r]` points to the
* hash result proper
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
* @since 1.1 `void *reserved` was replaced with `LIBRECRYPT_CONTEXT *ctx`
*/
LIBRECRYPT_READ_STR__(1) LIBRECRYPT_NONNULL_1__ LIBRECRYPT_WUR__ LIBRECRYPT_PURE__
size_t librecrypt_settings_prefix(const char *hash, size_t *hashsize_out, LIBRECRYPT_CONTEXT *ctx);
/**
* Get the number of chained hash algorithms specified in
* a password hash string
*
* @param hash The password hash string; must not be `NULL`
* @return The number of chained hash algorithms; always non-zero
*
* @seealso librecrypt_decompose_chain
* @seealso librecrypt_decompose_chain1
* @seealso librecrypt_next_algorithm
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
*/
LIBRECRYPT_READ_STR__(1) LIBRECRYPT_NONNULL__ LIBRECRYPT_WUR__ LIBRECRYPT_PURE__
inline size_t
librecrypt_chain_length(const char *hash)
{
size_t n = 1u;
for (; *hash; hash++)
if (*hash == LIBRECRYPT_ALGORITHM_LINK_DELIMITER)
n += 1u;
return n;
}
/**
* Decompose the chain of hash algorithms specified in
* a password hash string
*
* @param hash The password hash string to decompose; may be modified; must not be `NULL`
* @param chain_out_array Output array for the hash algorithms; each element will be an offset of `hash`
* @param size The number of elements `chain_out_buffer` fit
* @return The number of chained hashes (same as `librecrypt_chain_length(hash)`)
*
* Algorithms are delimited using `LIBRECRYPT_ALGORITHM_LINK_DELIMITER`
* (which is `'>'`). The first `size - 1u` occurences will be set to `NULL`.
* `hash` can be restored by setting the terminating NUL byte in each but
* the last string, that is stored to `chain_out_array`, to
* `LIBRECRYPT_ALGORITHM_LINK_DELIMITER`
*
* If positive `size` is smaller than the returned value,
* `chain_out_array[size - 1u]` will contain all algorithms
* (still delimited by `LIBRECRYPT_ALGORITHM_LINK_DELIMITER`)
* that where not placed in earlier slots
*
* Unless already stripped out before input,
* `chain_out_array[size - 1u]` (provided that `size` is
* positive) will end with the resulting hash
*
* @seealso librecrypt_chain_length
* @seealso librecrypt_decompose_chain1
* @seealso librecrypt_next_algorithm
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
*/
LIBRECRYPT_READ_WRITE_STR__(1) LIBRECRYPT_NONNULL_1__ LIBRECRYPT_WUR__
inline size_t
librecrypt_decompose_chain(char *hash, char **chain_out_array, size_t size)
{
size_t n = 1u;
if (size)
chain_out_array[0u] = hash;
for (; *hash; hash++) {
if (*hash == LIBRECRYPT_ALGORITHM_LINK_DELIMITER) {
if (n < size) {
*hash = '\0';
chain_out_array[n] = &hash[1u];
}
n += 1u;
}
}
return n;
}
/**
* Replace each `LIBRECRYPT_ALGORITHM_LINK_DELIMITER`
* (which is `'>'`) in a password hash string with NUL
* bytes, effectively decomposing it to one string per
* hash algorithm, with the hashing result still attached
* to the last one if it as in the string
*
* @param hash The password hash string to decompose; may be modified; must not be `NULL`
* @return The number of chained hashes (same as `librecrypt_chain_length(hash)`)
*
* @seealso librecrypt_decompose_chain
* @seealso librecrypt_next_algorithm
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
*/
LIBRECRYPT_READ_WRITE_STR__(1) LIBRECRYPT_NONNULL__ LIBRECRYPT_WUR__
inline size_t
librecrypt_decompose_chain1(char *hash)
{
size_t n = 1u;
for (; *hash; hash++) {
if (*hash == LIBRECRYPT_ALGORITHM_LINK_DELIMITER) {
*hash = '\0';
n += 1u;
}
}
return n;
}
/**
* This function is called repeatedly to get each
* hash algorithm (with parameters) that shall
* be chained together according to a provided
* hash string
*
* @param hash On the initial call, this argument shall point
* to the password hash string; on each call the
* function will update it to the current parsing
* state, meaning it will set `*hash` to point to
* an offset of `*hash` or point to `NULL`;
* additionally at each call, except on the final
* call, the string will be modified
* @return On the first call, this will be the first hash
* algorithm to use, and the second call it will
* be the second algorithm to use (if there is a
* second one), and so on. Once all algorithms have
* been extracted and returned, `NULL` is returned.
*
* Except once the function has returned `NULL`, overwriting
* the terminating NUL byte in the previous returned string
* with `LIBRECRYPT_ALGORITHM_LINK_DELIMITER` (which is `'>'`)
* will restore the password hash string to its original content
*
* Unless already stripped out before input, the last returned
* string will end with the resulting hash
*
* @seealso librecrypt_decompose_chain
* @seealso librecrypt_decompose_chain1
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
*/
LIBRECRYPT_NONNULL__ LIBRECRYPT_WUR__
inline char *
librecrypt_next_algorithm(char **hash)
{
char *ret;
if (!*hash)
return NULL;
ret = *hash;
*hash = strchr(*hash, LIBRECRYPT_ALGORITHM_LINK_DELIMITER);
if (*hash)
*(*hash)++ = '\0';
return ret;
}
/**
* Encode a hash result or salt in ASCII, from binary
*
* This is the inverse of `librecrypt_decode`
*
* @param out_buffer Output buffer for the ASCII representation
* @param size The number bytes the function may write to `out_buffer`
* @param binary The raw binary data to encode
* @param len The number of bytes in `binary`
* @param lut The encoding alphabet, consisting of 64 characters,
* repeated 4 times
* @param pad The padding character to use at the end; the NUL byte if none
* @return len_out The number of bytes that would have been written to `out_buffer`,
* excluding the terminating NUL byte, if `size` was sufficiently
* large; `SIZE_MAX` is returned on failure
*
* @throws EOVERFLOW The for value `*len_out` cannot be represented
* because it is greater than `SIZE_MAX` (`len` is
* too great)
*
* Despite being used as the failure indicator, `SIZE_MAX`
* is a legal return value on successful, is and returned
* (without modifying `errno`) when `pad` is the NUL byte
* and `len` is (`SIZE_MAX` - 3) / 4 * 3 + 2
*
* On successful completion, the N bytes is written to
* `out_buffer` where N is the lesser of `size` and 1 in
* excess of the return value (this count includes the
* terminating NUL byte)
*
* On successful completion, if `size` is positive,
* the output to `out_buffer` is NUL terminated even
* if this truncates the string
*
* @seealso librecrypt_get_encoding
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
*/
LIBRECRYPT_WRITE_MEM__(1, 2) LIBRECRYPT_READ_MEM__(3, 4) LIBRECRYPT_NONNULL_I__(5) LIBRECRYPT_WUR__
size_t librecrypt_encode(char *out_buffer, size_t size, const void *binary, size_t len,
const char lut[restrict static 256], char pad);
/**
* Decode a hash result or salt, from ASCII to binary
*
* This is the inverse of `librecrypt_encode`
*
* @param out_buffer Output buffer for the raw binary data
* @param size The number bytes the function may write to `out_buffer`
* @param ascii The ASCII encoding of the data, the text to decode
* @param len The number of bytes in `ascii`
* @param lut Alphabet reverse lookup table, shall map any valid
* character (except the padding character) to the value
* of that character in the encoding alphabet, and map
* any other character to the value `0xFF`
* @param pad The padding character to used at the end; the NUL byte if none
* @param strict_pad Zero if the padding at the end is optional, non-zero otherwise
* @return The number of bytes that would have been written to `out_buffer`,
* if `size` was sufficiently large, -1 on failure
*
* @throws EINVAL `ascii` uses an invalid encoding
*
* On successful completion, the N bytes is written to
* `out_buffer` where N is the lesser of `size` and the
* return value
*
* @seealso librecrypt_get_encoding
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
*/
LIBRECRYPT_WRITE_MEM__(1, 2) LIBRECRYPT_READ_MEM__(3, 4) LIBRECRYPT_NONNULL_I__(5) LIBRECRYPT_WUR__
ssize_t librecrypt_decode(void *out_buffer, size_t size, const char *ascii, size_t len,
const unsigned char lut[restrict static 256], char pad, int strict_pad);
/**
* Get the ASCII (base-64) encoding used for the
* last algorithm in a chain
*
* @param settings The algorithm chain
* @param len The number of bytes in `settings`
* @param pad_out Output parameter for the padding character,
* will be set to the NUL byte if there is none
* @param strict_pad_out Set to 1 if encodings must be padded to a
* multiple of 4 bytes using `*pad_out` at the end,
* 0 otherwise; set arbitrarily if `*pad_out` is
* set to the NUL byte
* @param decoding 0 if the returned pointer should be useful for,
* `librecrypt_encode`, otherwise it will be useful
* for `librecrypt_decode`
* @param ctx Library configuration
* @return If `decoding` is 0:
* the encoding alphabet, consisting of 64 characters,
* repeated 4 times;
* otherwise:
* alphabet reverse lookup table, mapping any valid
* character (except the padding character) to the
* value of that character in the encoding alphabet,
* and any other character to the value `0xFF`;
* but `NULL` on failure
*
* @throws ENOSYS The last algorithm in `settings` is not recognised
* or was disabled at compile-time
*
* The return type is `const char *` if `decoding` is 0,
* and `const unsigned char *` otherwise
*
* `*pad_out` shall be used for the `pad` parameter of the `librecrypt_encode`
* and `librecrypt_decode` functions, however the NUL byte may also be used
* for the `librecrypt_encode` function if `*strict_pad_out` is set to 0
*
* `*strict_pad_out` shall be used for the `strict_pad` parameter of the
* `librecrypt_decode` function
*
* The returned pointer (unless `NULL`) shall be used for the `lut` parameter
* of the `librecrypt_encode` function if `decoding` is 0, and for the `lut`
* parameter of the `librecrypt_decode` function otherwise
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
* @since 1.1 `void *reserved` was replaced with `LIBRECRYPT_CONTEXT *ctx`;
* provided `NULL` to this parameter no longer causes `EINVAL`-failure
*/
LIBRECRYPT_READ_STR__(1) LIBRECRYPT_NONNULL_I__(1) LIBRECRYPT_NONNULL_I__(3)
LIBRECRYPT_NONNULL_I__(4) LIBRECRYPT_WUR__
const void *librecrypt_get_encoding(const char *settings, size_t len, char *pad_out,
int *strict_pad_out, int decoding, LIBRECRYPT_CONTEXT *ctx);
/**
* Securely erase a memory buffer
*
* @param buffer The memory to erase
* @param size The number of bytes in `buffer` to erase
*
* @seealso librecrypt_wipe_str
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
*/
LIBRECRYPT_WRITE_MEM__(1, 2)
void librecrypt_wipe(void *buffer, size_t size);
/**
* Securely erase a string
*
* @param string The string to erase
*
* @seealso librecrypt_wipe
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
*/
LIBRECRYPT_READ_WRITE_STR__(1)
inline void
librecrypt_wipe_str(char *string)
{
if (string)
librecrypt_wipe(string, strlen(string));
}
/**
* Compare two memory segments in constant-time
*
* @param a One of the segments to compare
* @param b The other segments to compare
* @param len The number of bytes to compare
* @return 1 if the `a` and `b` are equal in their
* first `len` bytes, 0 otherwise
*
* @seealso librecrypt_equal
* @seealso librecrypt_verify
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
*/
LIBRECRYPT_READ_MEM__(1, 3) LIBRECRYPT_READ_MEM__(2, 3) LIBRECRYPT_WUR__
int librecrypt_equal_binary(const void *a, const void *b, size_t len);
/**
* Compare two strings in constant-time
*
* If the strings are of different length,
* it will stop immediately after measurement;
* this should not happen in the real world as
* this function is used to compare password
* hashes, whose length depends strictly on the
* configuration, not the input password
*
* @param a One of the strings to compare
* @param b The other string to compare
* @return 1 if the `a` and `b` are equal, 0 otherwise
*
* @seealso librecrypt_equal_binary
* @seealso librecrypt_verify
*
* This function is MT-Safe and AS-Safe
*
* @since 1.0
*/
LIBRECRYPT_READ_STR__(1) LIBRECRYPT_READ_STR__(2) LIBRECRYPT_WUR__ LIBRECRYPT_NONNULL__
inline int
librecrypt_equal(const char *a, const char *b)
{
size_t n = strlen(a);
size_t m = strlen(b);
return n == m && librecrypt_equal_binary(a, b, n);
}
/**
* Locate all asteriskes followed by a non-negative
* decimal number and replace each with ASCII-encded
* random bytes (as many bytes as the number specifies),
* except those used to specify the desired hash size
*
* @param out_buffer Output buffer for the ASCII representation
* @param size The number bytes the function may write to `out_buffer`
* @param settings The string to replace the asterisk-encoding in
* (the asterisk-free version is written to `out_buffer`)
* @param rng Random number generating function, `NULL` can be used
* for a default cryptographic random number generator;
* the function shall generate between 1 and `n` (inclusively)
* bytes and write them to the front of `out`, and return
* the number of generated bytes, or -1 on failure
* @param user Passed as the third argument to `*rng` for user-defined
* purposes, ignored if `rng` is `NULL`
* @param ctx Library configuration
* @return The number of bytes that would have been written to `out_buffer`,
* if `size` was sufficiently large, -1 on failure
* @throws ERANGE The expected return value is greater than {SSIZE_MAX}
* @throws ENOSYS `settings` contain an algorithm that is not recognised
* or was disabled at compile-time
*
* If `rng` is `NULL`, any encountered `EINTR` is ignored,
* however, if it is encountered `errno` will be set to `EINTR`,
* but `errno` will remain unmodified otherwise, except if
* the function fails due to one of the above listed reasons
*
* Failure can only happen through `*rng`, it is
* responsible for setting `errno` if desired, or
* because of the conditions listed above`
*
* On successful completion, the N bytes is written to
* `out_buffer` where N is the lesser of `size` and 1 in
* excess of the return value (this count includes the
* terminating NUL byte)
*
* On successful completion, if `size` is positive,
* the output to `out_buffer` is NUL terminated even
* if this truncates the string
*
* On failure, `out_buffer` may be partially written
*
* The encoding depend on the algorithm, which is why
* it can fail with `ENOSYS`
*
* When `rng` is non-`NULL`, this function inherits any