···738738The Pocket Quake team (Dan East and others)
739739The bzip2 team
740740The bsdiff team
741741+The libtomcrypt team
+29
utils/tomcrypt/LICENSE
···11+LibTomCrypt is licensed under DUAL licensing terms.
22+33+Choose and use the license of your needs.
44+55+[LICENSE #1]
66+77+LibTomCrypt is public domain. As should all quality software be.
88+99+Tom St Denis
1010+1111+[/LICENSE #1]
1212+1313+[LICENSE #2]
1414+1515+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
1616+ Version 2, December 2004
1717+1818+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
1919+2020+ Everyone is permitted to copy and distribute verbatim or modified
2121+ copies of this license document, and changing it is allowed as long
2222+ as the name is changed.
2323+2424+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2525+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
2626+2727+ 0. You just DO WHAT THE FUCK YOU WANT TO.
2828+2929+[/LICENSE #2]
+7
utils/tomcrypt/README.ROCKBOX
···11+This folder contains the source from libtomcrypt.
22+33+Only the source files that are actually used have been added. If more
44+functionality is needed add the missing files.
55+66+The source tree has last been synced with libtomcrypt 1.18.2 on 2020-08-08.
77+
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+1010+/** math functions **/
1111+1212+#define LTC_MP_LT -1
1313+#define LTC_MP_EQ 0
1414+#define LTC_MP_GT 1
1515+1616+#define LTC_MP_NO 0
1717+#define LTC_MP_YES 1
1818+1919+#ifndef LTC_MECC
2020+ typedef void ecc_point;
2121+#endif
2222+2323+#ifndef LTC_MRSA
2424+ typedef void rsa_key;
2525+#endif
2626+2727+#ifndef LTC_MILLER_RABIN_REPS
2828+ /* Number of rounds of the Miller-Rabin test
2929+ * "Reasonable values of reps are between 15 and 50." c.f. gmp doc of mpz_probab_prime_p()
3030+ * As of https://security.stackexchange.com/a/4546 we should use 40 rounds */
3131+ #define LTC_MILLER_RABIN_REPS 40
3232+#endif
3333+3434+int radix_to_bin(const void *in, int radix, void *out, unsigned long *len);
3535+3636+/** math descriptor */
3737+typedef struct {
3838+ /** Name of the math provider */
3939+ const char *name;
4040+4141+ /** Bits per digit, amount of bits must fit in an unsigned long */
4242+ int bits_per_digit;
4343+4444+/* ---- init/deinit functions ---- */
4545+4646+ /** initialize a bignum
4747+ @param a The number to initialize
4848+ @return CRYPT_OK on success
4949+ */
5050+ int (*init)(void **a);
5151+5252+ /** init copy
5353+ @param dst The number to initialize and write to
5454+ @param src The number to copy from
5555+ @return CRYPT_OK on success
5656+ */
5757+ int (*init_copy)(void **dst, void *src);
5858+5959+ /** deinit
6060+ @param a The number to free
6161+ @return CRYPT_OK on success
6262+ */
6363+ void (*deinit)(void *a);
6464+6565+/* ---- data movement ---- */
6666+6767+ /** negate
6868+ @param src The number to negate
6969+ @param dst The destination
7070+ @return CRYPT_OK on success
7171+ */
7272+ int (*neg)(void *src, void *dst);
7373+7474+ /** copy
7575+ @param src The number to copy from
7676+ @param dst The number to write to
7777+ @return CRYPT_OK on success
7878+ */
7979+ int (*copy)(void *src, void *dst);
8080+8181+/* ---- trivial low level functions ---- */
8282+8383+ /** set small constant
8484+ @param a Number to write to
8585+ @param n Source upto bits_per_digit (actually meant for very small constants)
8686+ @return CRYPT_OK on success
8787+ */
8888+ int (*set_int)(void *a, ltc_mp_digit n);
8989+9090+ /** get small constant
9191+ @param a Small number to read,
9292+ only fetches up to bits_per_digit from the number
9393+ @return The lower bits_per_digit of the integer (unsigned)
9494+ */
9595+ unsigned long (*get_int)(void *a);
9696+9797+ /** get digit n
9898+ @param a The number to read from
9999+ @param n The number of the digit to fetch
100100+ @return The bits_per_digit sized n'th digit of a
101101+ */
102102+ ltc_mp_digit (*get_digit)(void *a, int n);
103103+104104+ /** Get the number of digits that represent the number
105105+ @param a The number to count
106106+ @return The number of digits used to represent the number
107107+ */
108108+ int (*get_digit_count)(void *a);
109109+110110+ /** compare two integers
111111+ @param a The left side integer
112112+ @param b The right side integer
113113+ @return LTC_MP_LT if a < b,
114114+ LTC_MP_GT if a > b and
115115+ LTC_MP_EQ otherwise. (signed comparison)
116116+ */
117117+ int (*compare)(void *a, void *b);
118118+119119+ /** compare against int
120120+ @param a The left side integer
121121+ @param b The right side integer (upto bits_per_digit)
122122+ @return LTC_MP_LT if a < b,
123123+ LTC_MP_GT if a > b and
124124+ LTC_MP_EQ otherwise. (signed comparison)
125125+ */
126126+ int (*compare_d)(void *a, ltc_mp_digit n);
127127+128128+ /** Count the number of bits used to represent the integer
129129+ @param a The integer to count
130130+ @return The number of bits required to represent the integer
131131+ */
132132+ int (*count_bits)(void * a);
133133+134134+ /** Count the number of LSB bits which are zero
135135+ @param a The integer to count
136136+ @return The number of contiguous zero LSB bits
137137+ */
138138+ int (*count_lsb_bits)(void *a);
139139+140140+ /** Compute a power of two
141141+ @param a The integer to store the power in
142142+ @param n The power of two you want to store (a = 2^n)
143143+ @return CRYPT_OK on success
144144+ */
145145+ int (*twoexpt)(void *a , int n);
146146+147147+/* ---- radix conversions ---- */
148148+149149+ /** read ascii string
150150+ @param a The integer to store into
151151+ @param str The string to read
152152+ @param radix The radix the integer has been represented in (2-64)
153153+ @return CRYPT_OK on success
154154+ */
155155+ int (*read_radix)(void *a, const char *str, int radix);
156156+157157+ /** write number to string
158158+ @param a The integer to store
159159+ @param str The destination for the string
160160+ @param radix The radix the integer is to be represented in (2-64)
161161+ @return CRYPT_OK on success
162162+ */
163163+ int (*write_radix)(void *a, char *str, int radix);
164164+165165+ /** get size as unsigned char string
166166+ @param a The integer to get the size (when stored in array of octets)
167167+ @return The length of the integer in octets
168168+ */
169169+ unsigned long (*unsigned_size)(void *a);
170170+171171+ /** store an integer as an array of octets
172172+ @param src The integer to store
173173+ @param dst The buffer to store the integer in
174174+ @return CRYPT_OK on success
175175+ */
176176+ int (*unsigned_write)(void *src, unsigned char *dst);
177177+178178+ /** read an array of octets and store as integer
179179+ @param dst The integer to load
180180+ @param src The array of octets
181181+ @param len The number of octets
182182+ @return CRYPT_OK on success
183183+ */
184184+ int (*unsigned_read)( void *dst,
185185+ unsigned char *src,
186186+ unsigned long len);
187187+188188+/* ---- basic math ---- */
189189+190190+ /** add two integers
191191+ @param a The first source integer
192192+ @param b The second source integer
193193+ @param c The destination of "a + b"
194194+ @return CRYPT_OK on success
195195+ */
196196+ int (*add)(void *a, void *b, void *c);
197197+198198+ /** add two integers
199199+ @param a The first source integer
200200+ @param b The second source integer
201201+ (single digit of upto bits_per_digit in length)
202202+ @param c The destination of "a + b"
203203+ @return CRYPT_OK on success
204204+ */
205205+ int (*addi)(void *a, ltc_mp_digit b, void *c);
206206+207207+ /** subtract two integers
208208+ @param a The first source integer
209209+ @param b The second source integer
210210+ @param c The destination of "a - b"
211211+ @return CRYPT_OK on success
212212+ */
213213+ int (*sub)(void *a, void *b, void *c);
214214+215215+ /** subtract two integers
216216+ @param a The first source integer
217217+ @param b The second source integer
218218+ (single digit of upto bits_per_digit in length)
219219+ @param c The destination of "a - b"
220220+ @return CRYPT_OK on success
221221+ */
222222+ int (*subi)(void *a, ltc_mp_digit b, void *c);
223223+224224+ /** multiply two integers
225225+ @param a The first source integer
226226+ @param b The second source integer
227227+ (single digit of upto bits_per_digit in length)
228228+ @param c The destination of "a * b"
229229+ @return CRYPT_OK on success
230230+ */
231231+ int (*mul)(void *a, void *b, void *c);
232232+233233+ /** multiply two integers
234234+ @param a The first source integer
235235+ @param b The second source integer
236236+ (single digit of upto bits_per_digit in length)
237237+ @param c The destination of "a * b"
238238+ @return CRYPT_OK on success
239239+ */
240240+ int (*muli)(void *a, ltc_mp_digit b, void *c);
241241+242242+ /** Square an integer
243243+ @param a The integer to square
244244+ @param b The destination
245245+ @return CRYPT_OK on success
246246+ */
247247+ int (*sqr)(void *a, void *b);
248248+249249+ /** Divide an integer
250250+ @param a The dividend
251251+ @param b The divisor
252252+ @param c The quotient (can be NULL to signify don't care)
253253+ @param d The remainder (can be NULL to signify don't care)
254254+ @return CRYPT_OK on success
255255+ */
256256+ int (*mpdiv)(void *a, void *b, void *c, void *d);
257257+258258+ /** divide by two
259259+ @param a The integer to divide (shift right)
260260+ @param b The destination
261261+ @return CRYPT_OK on success
262262+ */
263263+ int (*div_2)(void *a, void *b);
264264+265265+ /** Get remainder (small value)
266266+ @param a The integer to reduce
267267+ @param b The modulus (upto bits_per_digit in length)
268268+ @param c The destination for the residue
269269+ @return CRYPT_OK on success
270270+ */
271271+ int (*modi)(void *a, ltc_mp_digit b, ltc_mp_digit *c);
272272+273273+ /** gcd
274274+ @param a The first integer
275275+ @param b The second integer
276276+ @param c The destination for (a, b)
277277+ @return CRYPT_OK on success
278278+ */
279279+ int (*gcd)(void *a, void *b, void *c);
280280+281281+ /** lcm
282282+ @param a The first integer
283283+ @param b The second integer
284284+ @param c The destination for [a, b]
285285+ @return CRYPT_OK on success
286286+ */
287287+ int (*lcm)(void *a, void *b, void *c);
288288+289289+ /** Modular multiplication
290290+ @param a The first source
291291+ @param b The second source
292292+ @param c The modulus
293293+ @param d The destination (a*b mod c)
294294+ @return CRYPT_OK on success
295295+ */
296296+ int (*mulmod)(void *a, void *b, void *c, void *d);
297297+298298+ /** Modular squaring
299299+ @param a The first source
300300+ @param b The modulus
301301+ @param c The destination (a*a mod b)
302302+ @return CRYPT_OK on success
303303+ */
304304+ int (*sqrmod)(void *a, void *b, void *c);
305305+306306+ /** Modular inversion
307307+ @param a The value to invert
308308+ @param b The modulus
309309+ @param c The destination (1/a mod b)
310310+ @return CRYPT_OK on success
311311+ */
312312+ int (*invmod)(void *, void *, void *);
313313+314314+/* ---- reduction ---- */
315315+316316+ /** setup Montgomery
317317+ @param a The modulus
318318+ @param b The destination for the reduction digit
319319+ @return CRYPT_OK on success
320320+ */
321321+ int (*montgomery_setup)(void *a, void **b);
322322+323323+ /** get normalization value
324324+ @param a The destination for the normalization value
325325+ @param b The modulus
326326+ @return CRYPT_OK on success
327327+ */
328328+ int (*montgomery_normalization)(void *a, void *b);
329329+330330+ /** reduce a number
331331+ @param a The number [and dest] to reduce
332332+ @param b The modulus
333333+ @param c The value "b" from montgomery_setup()
334334+ @return CRYPT_OK on success
335335+ */
336336+ int (*montgomery_reduce)(void *a, void *b, void *c);
337337+338338+ /** clean up (frees memory)
339339+ @param a The value "b" from montgomery_setup()
340340+ @return CRYPT_OK on success
341341+ */
342342+ void (*montgomery_deinit)(void *a);
343343+344344+/* ---- exponentiation ---- */
345345+346346+ /** Modular exponentiation
347347+ @param a The base integer
348348+ @param b The power (can be negative) integer
349349+ @param c The modulus integer
350350+ @param d The destination
351351+ @return CRYPT_OK on success
352352+ */
353353+ int (*exptmod)(void *a, void *b, void *c, void *d);
354354+355355+ /** Primality testing
356356+ @param a The integer to test
357357+ @param b The number of Miller-Rabin tests that shall be executed
358358+ @param c The destination of the result (FP_YES if prime)
359359+ @return CRYPT_OK on success
360360+ */
361361+ int (*isprime)(void *a, int b, int *c);
362362+363363+/* ---- (optional) ecc point math ---- */
364364+365365+ /** ECC GF(p) point multiplication (from the NIST curves)
366366+ @param k The integer to multiply the point by
367367+ @param G The point to multiply
368368+ @param R The destination for kG
369369+ @param modulus The modulus for the field
370370+ @param map Boolean indicated whether to map back to affine or not
371371+ (can be ignored if you work in affine only)
372372+ @return CRYPT_OK on success
373373+ */
374374+ int (*ecc_ptmul)( void *k,
375375+ ecc_point *G,
376376+ ecc_point *R,
377377+ void *modulus,
378378+ int map);
379379+380380+ /** ECC GF(p) point addition
381381+ @param P The first point
382382+ @param Q The second point
383383+ @param R The destination of P + Q
384384+ @param modulus The modulus
385385+ @param mp The "b" value from montgomery_setup()
386386+ @return CRYPT_OK on success
387387+ */
388388+ int (*ecc_ptadd)(ecc_point *P,
389389+ ecc_point *Q,
390390+ ecc_point *R,
391391+ void *modulus,
392392+ void *mp);
393393+394394+ /** ECC GF(p) point double
395395+ @param P The first point
396396+ @param R The destination of 2P
397397+ @param modulus The modulus
398398+ @param mp The "b" value from montgomery_setup()
399399+ @return CRYPT_OK on success
400400+ */
401401+ int (*ecc_ptdbl)(ecc_point *P,
402402+ ecc_point *R,
403403+ void *modulus,
404404+ void *mp);
405405+406406+ /** ECC mapping from projective to affine,
407407+ currently uses (x,y,z) => (x/z^2, y/z^3, 1)
408408+ @param P The point to map
409409+ @param modulus The modulus
410410+ @param mp The "b" value from montgomery_setup()
411411+ @return CRYPT_OK on success
412412+ @remark The mapping can be different but keep in mind a
413413+ ecc_point only has three integers (x,y,z) so if
414414+ you use a different mapping you have to make it fit.
415415+ */
416416+ int (*ecc_map)(ecc_point *P, void *modulus, void *mp);
417417+418418+ /** Computes kA*A + kB*B = C using Shamir's Trick
419419+ @param A First point to multiply
420420+ @param kA What to multiple A by
421421+ @param B Second point to multiply
422422+ @param kB What to multiple B by
423423+ @param C [out] Destination point (can overlap with A or B)
424424+ @param modulus Modulus for curve
425425+ @return CRYPT_OK on success
426426+ */
427427+ int (*ecc_mul2add)(ecc_point *A, void *kA,
428428+ ecc_point *B, void *kB,
429429+ ecc_point *C,
430430+ void *modulus);
431431+432432+/* ---- (optional) rsa optimized math (for internal CRT) ---- */
433433+434434+ /** RSA Key Generation
435435+ @param prng An active PRNG state
436436+ @param wprng The index of the PRNG desired
437437+ @param size The size of the key in octets
438438+ @param e The "e" value (public key).
439439+ e==65537 is a good choice
440440+ @param key [out] Destination of a newly created private key pair
441441+ @return CRYPT_OK if successful, upon error all allocated ram is freed
442442+ */
443443+ int (*rsa_keygen)(prng_state *prng,
444444+ int wprng,
445445+ int size,
446446+ long e,
447447+ rsa_key *key);
448448+449449+ /** RSA exponentiation
450450+ @param in The octet array representing the base
451451+ @param inlen The length of the input
452452+ @param out The destination (to be stored in an octet array format)
453453+ @param outlen The length of the output buffer and the resulting size
454454+ (zero padded to the size of the modulus)
455455+ @param which PK_PUBLIC for public RSA and PK_PRIVATE for private RSA
456456+ @param key The RSA key to use
457457+ @return CRYPT_OK on success
458458+ */
459459+ int (*rsa_me)(const unsigned char *in, unsigned long inlen,
460460+ unsigned char *out, unsigned long *outlen, int which,
461461+ rsa_key *key);
462462+463463+/* ---- basic math continued ---- */
464464+465465+ /** Modular addition
466466+ @param a The first source
467467+ @param b The second source
468468+ @param c The modulus
469469+ @param d The destination (a + b mod c)
470470+ @return CRYPT_OK on success
471471+ */
472472+ int (*addmod)(void *a, void *b, void *c, void *d);
473473+474474+ /** Modular substraction
475475+ @param a The first source
476476+ @param b The second source
477477+ @param c The modulus
478478+ @param d The destination (a - b mod c)
479479+ @return CRYPT_OK on success
480480+ */
481481+ int (*submod)(void *a, void *b, void *c, void *d);
482482+483483+/* ---- misc stuff ---- */
484484+485485+ /** Make a pseudo-random mpi
486486+ @param a The mpi to make random
487487+ @param size The desired length
488488+ @return CRYPT_OK on success
489489+ */
490490+ int (*rand)(void *a, int size);
491491+} ltc_math_descriptor;
492492+493493+extern ltc_math_descriptor ltc_mp;
494494+495495+int ltc_init_multi(void **a, ...);
496496+void ltc_deinit_multi(void *a, ...);
497497+void ltc_cleanup_multi(void **a, ...);
498498+499499+#ifdef LTM_DESC
500500+extern const ltc_math_descriptor ltm_desc;
501501+#endif
502502+503503+#ifdef TFM_DESC
504504+extern const ltc_math_descriptor tfm_desc;
505505+#endif
506506+507507+#ifdef GMP_DESC
508508+extern const ltc_math_descriptor gmp_desc;
509509+#endif
510510+511511+#if !defined(DESC_DEF_ONLY) && defined(LTC_SOURCE)
512512+513513+#define MP_DIGIT_BIT ltc_mp.bits_per_digit
514514+515515+/* some handy macros */
516516+#define mp_init(a) ltc_mp.init(a)
517517+#define mp_init_multi ltc_init_multi
518518+#define mp_clear(a) ltc_mp.deinit(a)
519519+#define mp_clear_multi ltc_deinit_multi
520520+#define mp_cleanup_multi ltc_cleanup_multi
521521+#define mp_init_copy(a, b) ltc_mp.init_copy(a, b)
522522+523523+#define mp_neg(a, b) ltc_mp.neg(a, b)
524524+#define mp_copy(a, b) ltc_mp.copy(a, b)
525525+526526+#define mp_set(a, b) ltc_mp.set_int(a, b)
527527+#define mp_set_int(a, b) ltc_mp.set_int(a, b)
528528+#define mp_get_int(a) ltc_mp.get_int(a)
529529+#define mp_get_digit(a, n) ltc_mp.get_digit(a, n)
530530+#define mp_get_digit_count(a) ltc_mp.get_digit_count(a)
531531+#define mp_cmp(a, b) ltc_mp.compare(a, b)
532532+#define mp_cmp_d(a, b) ltc_mp.compare_d(a, b)
533533+#define mp_count_bits(a) ltc_mp.count_bits(a)
534534+#define mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a)
535535+#define mp_2expt(a, b) ltc_mp.twoexpt(a, b)
536536+537537+#define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c)
538538+#define mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c)
539539+#define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
540540+#define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
541541+#define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
542542+543543+#define mp_add(a, b, c) ltc_mp.add(a, b, c)
544544+#define mp_add_d(a, b, c) ltc_mp.addi(a, b, c)
545545+#define mp_sub(a, b, c) ltc_mp.sub(a, b, c)
546546+#define mp_sub_d(a, b, c) ltc_mp.subi(a, b, c)
547547+#define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
548548+#define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
549549+#define mp_sqr(a, b) ltc_mp.sqr(a, b)
550550+#define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
551551+#define mp_div_2(a, b) ltc_mp.div_2(a, b)
552552+#define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)
553553+#define mp_mod_d(a, b, c) ltc_mp.modi(a, b, c)
554554+#define mp_gcd(a, b, c) ltc_mp.gcd(a, b, c)
555555+#define mp_lcm(a, b, c) ltc_mp.lcm(a, b, c)
556556+557557+#define mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d)
558558+#define mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d)
559559+#define mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d)
560560+#define mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c)
561561+#define mp_invmod(a, b, c) ltc_mp.invmod(a, b, c)
562562+563563+#define mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b)
564564+#define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b)
565565+#define mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c)
566566+#define mp_montgomery_free(a) ltc_mp.montgomery_deinit(a)
567567+568568+#define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
569569+#define mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c)
570570+571571+#define mp_iszero(a) (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
572572+#define mp_isodd(a) (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
573573+#define mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0)
574574+575575+#define mp_tohex(a, b) mp_toradix(a, b, 16)
576576+577577+#define mp_rand(a, b) ltc_mp.rand(a, b)
578578+579579+#endif
580580+581581+/* ref: HEAD -> master, tag: v1.18.2 */
582582+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
583583+/* commit time: 2018-07-01 22:49:01 +0200 */
+113
utils/tomcrypt/src/headers/tomcrypt_misc.h
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+1010+/* ---- LTC_BASE64 Routines ---- */
1111+#ifdef LTC_BASE64
1212+int base64_encode(const unsigned char *in, unsigned long len,
1313+ unsigned char *out, unsigned long *outlen);
1414+1515+int base64_decode(const unsigned char *in, unsigned long len,
1616+ unsigned char *out, unsigned long *outlen);
1717+int base64_strict_decode(const unsigned char *in, unsigned long len,
1818+ unsigned char *out, unsigned long *outlen);
1919+#endif
2020+2121+#ifdef LTC_BASE64_URL
2222+int base64url_encode(const unsigned char *in, unsigned long len,
2323+ unsigned char *out, unsigned long *outlen);
2424+int base64url_strict_encode(const unsigned char *in, unsigned long inlen,
2525+ unsigned char *out, unsigned long *outlen);
2626+2727+int base64url_decode(const unsigned char *in, unsigned long len,
2828+ unsigned char *out, unsigned long *outlen);
2929+int base64url_strict_decode(const unsigned char *in, unsigned long len,
3030+ unsigned char *out, unsigned long *outlen);
3131+#endif
3232+3333+/* ===> LTC_HKDF -- RFC5869 HMAC-based Key Derivation Function <=== */
3434+#ifdef LTC_HKDF
3535+3636+int hkdf_test(void);
3737+3838+int hkdf_extract(int hash_idx,
3939+ const unsigned char *salt, unsigned long saltlen,
4040+ const unsigned char *in, unsigned long inlen,
4141+ unsigned char *out, unsigned long *outlen);
4242+4343+int hkdf_expand(int hash_idx,
4444+ const unsigned char *info, unsigned long infolen,
4545+ const unsigned char *in, unsigned long inlen,
4646+ unsigned char *out, unsigned long outlen);
4747+4848+int hkdf(int hash_idx,
4949+ const unsigned char *salt, unsigned long saltlen,
5050+ const unsigned char *info, unsigned long infolen,
5151+ const unsigned char *in, unsigned long inlen,
5252+ unsigned char *out, unsigned long outlen);
5353+5454+#endif /* LTC_HKDF */
5555+5656+/* ---- MEM routines ---- */
5757+int mem_neq(const void *a, const void *b, size_t len);
5858+void zeromem(volatile void *dst, size_t len);
5959+void burn_stack(unsigned long len);
6060+6161+const char *error_to_string(int err);
6262+6363+extern const char *crypt_build_settings;
6464+6565+/* ---- HMM ---- */
6666+int crypt_fsa(void *mp, ...);
6767+6868+/* ---- Dynamic language support ---- */
6969+int crypt_get_constant(const char* namein, int *valueout);
7070+int crypt_list_all_constants(char *names_list, unsigned int *names_list_size);
7171+7272+int crypt_get_size(const char* namein, unsigned int *sizeout);
7373+int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size);
7474+7575+#ifdef LTM_DESC
7676+void init_LTM(void);
7777+#endif
7878+#ifdef TFM_DESC
7979+void init_TFM(void);
8080+#endif
8181+#ifdef GMP_DESC
8282+void init_GMP(void);
8383+#endif
8484+8585+#ifdef LTC_ADLER32
8686+typedef struct adler32_state_s
8787+{
8888+ unsigned short s[2];
8989+} adler32_state;
9090+9191+void adler32_init(adler32_state *ctx);
9292+void adler32_update(adler32_state *ctx, const unsigned char *input, unsigned long length);
9393+void adler32_finish(adler32_state *ctx, void *hash, unsigned long size);
9494+int adler32_test(void);
9595+#endif
9696+9797+#ifdef LTC_CRC32
9898+typedef struct crc32_state_s
9999+{
100100+ ulong32 crc;
101101+} crc32_state;
102102+103103+void crc32_init(crc32_state *ctx);
104104+void crc32_update(crc32_state *ctx, const unsigned char *input, unsigned long length);
105105+void crc32_finish(crc32_state *ctx, void *hash, unsigned long size);
106106+int crc32_test(void);
107107+#endif
108108+109109+int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which);
110110+111111+/* ref: HEAD -> master, tag: v1.18.2 */
112112+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
113113+/* commit time: 2018-07-01 22:49:01 +0200 */
+747
utils/tomcrypt/src/headers/tomcrypt_pk.h
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+1010+/* ---- NUMBER THEORY ---- */
1111+1212+enum {
1313+ PK_PUBLIC=0,
1414+ PK_PRIVATE=1
1515+};
1616+1717+/* Indicates standard output formats that can be read e.g. by OpenSSL or GnuTLS */
1818+#define PK_STD 0x1000
1919+2020+int rand_prime(void *N, long len, prng_state *prng, int wprng);
2121+2222+#ifdef LTC_SOURCE
2323+/* internal helper functions */
2424+int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
2525+int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
2626+2727+enum public_key_algorithms {
2828+ PKA_RSA,
2929+ PKA_DSA
3030+};
3131+3232+typedef struct Oid {
3333+ unsigned long OID[16];
3434+ /** Number of OID digits in use */
3535+ unsigned long OIDlen;
3636+} oid_st;
3737+3838+int pk_get_oid(int pk, oid_st *st);
3939+#endif /* LTC_SOURCE */
4040+4141+/* ---- RSA ---- */
4242+#ifdef LTC_MRSA
4343+4444+/** RSA PKCS style key */
4545+typedef struct Rsa_key {
4646+ /** Type of key, PK_PRIVATE or PK_PUBLIC */
4747+ int type;
4848+ /** The public exponent */
4949+ void *e;
5050+ /** The private exponent */
5151+ void *d;
5252+ /** The modulus */
5353+ void *N;
5454+ /** The p factor of N */
5555+ void *p;
5656+ /** The q factor of N */
5757+ void *q;
5858+ /** The 1/q mod p CRT param */
5959+ void *qP;
6060+ /** The d mod (p - 1) CRT param */
6161+ void *dP;
6262+ /** The d mod (q - 1) CRT param */
6363+ void *dQ;
6464+} rsa_key;
6565+6666+int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
6767+6868+int rsa_get_size(rsa_key *key);
6969+7070+int rsa_exptmod(const unsigned char *in, unsigned long inlen,
7171+ unsigned char *out, unsigned long *outlen, int which,
7272+ rsa_key *key);
7373+7474+void rsa_free(rsa_key *key);
7575+7676+/* These use PKCS #1 v2.0 padding */
7777+#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
7878+ rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_PKCS_1_OAEP, _key)
7979+8080+#define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
8181+ rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_PKCS_1_OAEP, _stat, _key)
8282+8383+#define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
8484+ rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
8585+8686+#define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
8787+ rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
8888+8989+#define rsa_sign_saltlen_get_max(_hash_idx, _key) \
9090+ rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, _hash_idx, _key)
9191+9292+/* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
9393+int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
9494+ unsigned char *out, unsigned long *outlen,
9595+ const unsigned char *lparam, unsigned long lparamlen,
9696+ prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key);
9797+9898+int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
9999+ unsigned char *out, unsigned long *outlen,
100100+ const unsigned char *lparam, unsigned long lparamlen,
101101+ int hash_idx, int padding,
102102+ int *stat, rsa_key *key);
103103+104104+int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen,
105105+ unsigned char *out, unsigned long *outlen,
106106+ int padding,
107107+ prng_state *prng, int prng_idx,
108108+ int hash_idx, unsigned long saltlen,
109109+ rsa_key *key);
110110+111111+int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
112112+ const unsigned char *hash, unsigned long hashlen,
113113+ int padding,
114114+ int hash_idx, unsigned long saltlen,
115115+ int *stat, rsa_key *key);
116116+117117+int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, rsa_key *key);
118118+119119+/* PKCS #1 import/export */
120120+int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
121121+int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
122122+123123+int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key);
124124+int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen,
125125+ const void *passwd, unsigned long passwdlen, rsa_key *key);
126126+127127+int rsa_set_key(const unsigned char *N, unsigned long Nlen,
128128+ const unsigned char *e, unsigned long elen,
129129+ const unsigned char *d, unsigned long dlen,
130130+ rsa_key *key);
131131+int rsa_set_factors(const unsigned char *p, unsigned long plen,
132132+ const unsigned char *q, unsigned long qlen,
133133+ rsa_key *key);
134134+int rsa_set_crt_params(const unsigned char *dP, unsigned long dPlen,
135135+ const unsigned char *dQ, unsigned long dQlen,
136136+ const unsigned char *qP, unsigned long qPlen,
137137+ rsa_key *key);
138138+#endif
139139+140140+/* ---- Katja ---- */
141141+#ifdef LTC_MKAT
142142+143143+/* Min and Max KAT key sizes (in bits) */
144144+#define MIN_KAT_SIZE 1024
145145+#define MAX_KAT_SIZE 4096
146146+147147+/** Katja PKCS style key */
148148+typedef struct KAT_key {
149149+ /** Type of key, PK_PRIVATE or PK_PUBLIC */
150150+ int type;
151151+ /** The private exponent */
152152+ void *d;
153153+ /** The modulus */
154154+ void *N;
155155+ /** The p factor of N */
156156+ void *p;
157157+ /** The q factor of N */
158158+ void *q;
159159+ /** The 1/q mod p CRT param */
160160+ void *qP;
161161+ /** The d mod (p - 1) CRT param */
162162+ void *dP;
163163+ /** The d mod (q - 1) CRT param */
164164+ void *dQ;
165165+ /** The pq param */
166166+ void *pq;
167167+} katja_key;
168168+169169+int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key);
170170+171171+int katja_exptmod(const unsigned char *in, unsigned long inlen,
172172+ unsigned char *out, unsigned long *outlen, int which,
173173+ katja_key *key);
174174+175175+void katja_free(katja_key *key);
176176+177177+/* These use PKCS #1 v2.0 padding */
178178+int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
179179+ unsigned char *out, unsigned long *outlen,
180180+ const unsigned char *lparam, unsigned long lparamlen,
181181+ prng_state *prng, int prng_idx, int hash_idx, katja_key *key);
182182+183183+int katja_decrypt_key(const unsigned char *in, unsigned long inlen,
184184+ unsigned char *out, unsigned long *outlen,
185185+ const unsigned char *lparam, unsigned long lparamlen,
186186+ int hash_idx, int *stat,
187187+ katja_key *key);
188188+189189+/* PKCS #1 import/export */
190190+int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key);
191191+int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
192192+193193+#endif
194194+195195+/* ---- DH Routines ---- */
196196+#ifdef LTC_MDH
197197+198198+typedef struct {
199199+ int type;
200200+ void *x;
201201+ void *y;
202202+ void *base;
203203+ void *prime;
204204+} dh_key;
205205+206206+int dh_get_groupsize(dh_key *key);
207207+208208+int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
209209+int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
210210+211211+int dh_set_pg(const unsigned char *p, unsigned long plen,
212212+ const unsigned char *g, unsigned long glen,
213213+ dh_key *key);
214214+int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
215215+int dh_set_pg_groupsize(int groupsize, dh_key *key);
216216+217217+int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
218218+int dh_generate_key(prng_state *prng, int wprng, dh_key *key);
219219+220220+int dh_shared_secret(dh_key *private_key, dh_key *public_key,
221221+ unsigned char *out, unsigned long *outlen);
222222+223223+void dh_free(dh_key *key);
224224+225225+int dh_export_key(void *out, unsigned long *outlen, int type, dh_key *key);
226226+227227+#ifdef LTC_SOURCE
228228+typedef struct {
229229+ int size;
230230+ const char *name, *base, *prime;
231231+} ltc_dh_set_type;
232232+233233+extern const ltc_dh_set_type ltc_dh_sets[];
234234+235235+/* internal helper functions */
236236+int dh_check_pubkey(dh_key *key);
237237+#endif
238238+239239+#endif /* LTC_MDH */
240240+241241+242242+/* ---- ECC Routines ---- */
243243+#ifdef LTC_MECC
244244+245245+/* size of our temp buffers for exported keys */
246246+#define ECC_BUF_SIZE 256
247247+248248+/* max private key size */
249249+#define ECC_MAXSIZE 66
250250+251251+/** Structure defines a NIST GF(p) curve */
252252+typedef struct {
253253+ /** The size of the curve in octets */
254254+ int size;
255255+256256+ /** name of curve */
257257+ const char *name;
258258+259259+ /** The prime that defines the field the curve is in (encoded in hex) */
260260+ const char *prime;
261261+262262+ /** The fields B param (hex) */
263263+ const char *B;
264264+265265+ /** The order of the curve (hex) */
266266+ const char *order;
267267+268268+ /** The x co-ordinate of the base point on the curve (hex) */
269269+ const char *Gx;
270270+271271+ /** The y co-ordinate of the base point on the curve (hex) */
272272+ const char *Gy;
273273+} ltc_ecc_set_type;
274274+275275+/** A point on a ECC curve, stored in Jacbobian format such that (x,y,z) => (x/z^2, y/z^3, 1) when interpretted as affine */
276276+typedef struct {
277277+ /** The x co-ordinate */
278278+ void *x;
279279+280280+ /** The y co-ordinate */
281281+ void *y;
282282+283283+ /** The z co-ordinate */
284284+ void *z;
285285+} ecc_point;
286286+287287+/** An ECC key */
288288+typedef struct {
289289+ /** Type of key, PK_PRIVATE or PK_PUBLIC */
290290+ int type;
291291+292292+ /** Index into the ltc_ecc_sets[] for the parameters of this curve; if -1, then this key is using user supplied curve in dp */
293293+ int idx;
294294+295295+ /** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */
296296+ const ltc_ecc_set_type *dp;
297297+298298+ /** The public key */
299299+ ecc_point pubkey;
300300+301301+ /** The private key */
302302+ void *k;
303303+} ecc_key;
304304+305305+/** the ECC params provided */
306306+extern const ltc_ecc_set_type ltc_ecc_sets[];
307307+308308+int ecc_test(void);
309309+void ecc_sizes(int *low, int *high);
310310+int ecc_get_size(ecc_key *key);
311311+312312+int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
313313+int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp);
314314+void ecc_free(ecc_key *key);
315315+316316+int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
317317+int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
318318+int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp);
319319+320320+int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
321321+int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
322322+int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
323323+324324+int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
325325+ unsigned char *out, unsigned long *outlen);
326326+327327+int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
328328+ unsigned char *out, unsigned long *outlen,
329329+ prng_state *prng, int wprng, int hash,
330330+ ecc_key *key);
331331+332332+int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
333333+ unsigned char *out, unsigned long *outlen,
334334+ ecc_key *key);
335335+336336+int ecc_sign_hash_rfc7518(const unsigned char *in, unsigned long inlen,
337337+ unsigned char *out, unsigned long *outlen,
338338+ prng_state *prng, int wprng, ecc_key *key);
339339+340340+int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
341341+ unsigned char *out, unsigned long *outlen,
342342+ prng_state *prng, int wprng, ecc_key *key);
343343+344344+int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen,
345345+ const unsigned char *hash, unsigned long hashlen,
346346+ int *stat, ecc_key *key);
347347+348348+int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
349349+ const unsigned char *hash, unsigned long hashlen,
350350+ int *stat, ecc_key *key);
351351+352352+/* low level functions */
353353+ecc_point *ltc_ecc_new_point(void);
354354+void ltc_ecc_del_point(ecc_point *p);
355355+int ltc_ecc_is_valid_idx(int n);
356356+357357+/* point ops (mp == montgomery digit) */
358358+#if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
359359+/* R = 2P */
360360+int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
361361+362362+/* R = P + Q */
363363+int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
364364+#endif
365365+366366+#if defined(LTC_MECC_FP)
367367+/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
368368+int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
369369+370370+/* functions for saving/loading/freeing/adding to fixed point cache */
371371+int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
372372+int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
373373+void ltc_ecc_fp_free(void);
374374+int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
375375+376376+/* lock/unlock all points currently in fixed point cache */
377377+void ltc_ecc_fp_tablelock(int lock);
378378+#endif
379379+380380+/* R = kG */
381381+int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
382382+383383+#ifdef LTC_ECC_SHAMIR
384384+/* kA*A + kB*B = C */
385385+int ltc_ecc_mul2add(ecc_point *A, void *kA,
386386+ ecc_point *B, void *kB,
387387+ ecc_point *C,
388388+ void *modulus);
389389+390390+#ifdef LTC_MECC_FP
391391+/* Shamir's trick with optimized point multiplication using fixed point cache */
392392+int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
393393+ ecc_point *B, void *kB,
394394+ ecc_point *C, void *modulus);
395395+#endif
396396+397397+#endif
398398+399399+400400+/* map P to affine from projective */
401401+int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
402402+403403+#endif
404404+405405+#ifdef LTC_MDSA
406406+407407+/* Max diff between group and modulus size in bytes */
408408+#define LTC_MDSA_DELTA 512
409409+410410+/* Max DSA group size in bytes (default allows 4k-bit groups) */
411411+#define LTC_MDSA_MAX_GROUP 512
412412+413413+/** DSA key structure */
414414+typedef struct {
415415+ /** The key type, PK_PRIVATE or PK_PUBLIC */
416416+ int type;
417417+418418+ /** The order of the sub-group used in octets */
419419+ int qord;
420420+421421+ /** The generator */
422422+ void *g;
423423+424424+ /** The prime used to generate the sub-group */
425425+ void *q;
426426+427427+ /** The large prime that generats the field the contains the sub-group */
428428+ void *p;
429429+430430+ /** The private key */
431431+ void *x;
432432+433433+ /** The public key */
434434+ void *y;
435435+} dsa_key;
436436+437437+int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
438438+439439+int dsa_set_pqg(const unsigned char *p, unsigned long plen,
440440+ const unsigned char *q, unsigned long qlen,
441441+ const unsigned char *g, unsigned long glen,
442442+ dsa_key *key);
443443+int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
444444+int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
445445+446446+int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
447447+int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
448448+449449+void dsa_free(dsa_key *key);
450450+451451+int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
452452+ void *r, void *s,
453453+ prng_state *prng, int wprng, dsa_key *key);
454454+455455+int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
456456+ unsigned char *out, unsigned long *outlen,
457457+ prng_state *prng, int wprng, dsa_key *key);
458458+459459+int dsa_verify_hash_raw( void *r, void *s,
460460+ const unsigned char *hash, unsigned long hashlen,
461461+ int *stat, dsa_key *key);
462462+463463+int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
464464+ const unsigned char *hash, unsigned long hashlen,
465465+ int *stat, dsa_key *key);
466466+467467+int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
468468+ unsigned char *out, unsigned long *outlen,
469469+ prng_state *prng, int wprng, int hash,
470470+ dsa_key *key);
471471+472472+int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,
473473+ unsigned char *out, unsigned long *outlen,
474474+ dsa_key *key);
475475+476476+int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
477477+int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
478478+int dsa_verify_key(dsa_key *key, int *stat);
479479+#ifdef LTC_SOURCE
480480+/* internal helper functions */
481481+int dsa_int_validate_xy(dsa_key *key, int *stat);
482482+int dsa_int_validate_pqg(dsa_key *key, int *stat);
483483+int dsa_int_validate_primes(dsa_key *key, int *stat);
484484+#endif
485485+int dsa_shared_secret(void *private_key, void *base,
486486+ dsa_key *public_key,
487487+ unsigned char *out, unsigned long *outlen);
488488+#endif
489489+490490+#ifdef LTC_DER
491491+/* DER handling */
492492+493493+typedef enum ltc_asn1_type_ {
494494+ /* 0 */
495495+ LTC_ASN1_EOL,
496496+ LTC_ASN1_BOOLEAN,
497497+ LTC_ASN1_INTEGER,
498498+ LTC_ASN1_SHORT_INTEGER,
499499+ LTC_ASN1_BIT_STRING,
500500+ /* 5 */
501501+ LTC_ASN1_OCTET_STRING,
502502+ LTC_ASN1_NULL,
503503+ LTC_ASN1_OBJECT_IDENTIFIER,
504504+ LTC_ASN1_IA5_STRING,
505505+ LTC_ASN1_PRINTABLE_STRING,
506506+ /* 10 */
507507+ LTC_ASN1_UTF8_STRING,
508508+ LTC_ASN1_UTCTIME,
509509+ LTC_ASN1_CHOICE,
510510+ LTC_ASN1_SEQUENCE,
511511+ LTC_ASN1_SET,
512512+ /* 15 */
513513+ LTC_ASN1_SETOF,
514514+ LTC_ASN1_RAW_BIT_STRING,
515515+ LTC_ASN1_TELETEX_STRING,
516516+ LTC_ASN1_CONSTRUCTED,
517517+ LTC_ASN1_CONTEXT_SPECIFIC,
518518+ /* 20 */
519519+ LTC_ASN1_GENERALIZEDTIME,
520520+} ltc_asn1_type;
521521+522522+/** A LTC ASN.1 list type */
523523+typedef struct ltc_asn1_list_ {
524524+ /** The LTC ASN.1 enumerated type identifier */
525525+ ltc_asn1_type type;
526526+ /** The data to encode or place for decoding */
527527+ void *data;
528528+ /** The size of the input or resulting output */
529529+ unsigned long size;
530530+ /** The used flag, this is used by the CHOICE ASN.1 type to indicate which choice was made */
531531+ int used;
532532+ /** prev/next entry in the list */
533533+ struct ltc_asn1_list_ *prev, *next, *child, *parent;
534534+} ltc_asn1_list;
535535+536536+#define LTC_SET_ASN1(list, index, Type, Data, Size) \
537537+ do { \
538538+ int LTC_MACRO_temp = (index); \
539539+ ltc_asn1_list *LTC_MACRO_list = (list); \
540540+ LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \
541541+ LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data); \
542542+ LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \
543543+ LTC_MACRO_list[LTC_MACRO_temp].used = 0; \
544544+ } while (0)
545545+546546+/* SEQUENCE */
547547+int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
548548+ unsigned char *out, unsigned long *outlen, int type_of);
549549+550550+#define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
551551+552552+int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
553553+ ltc_asn1_list *list, unsigned long outlen, int ordered);
554554+555555+#define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1)
556556+557557+int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
558558+ unsigned long *outlen);
559559+560560+561561+#ifdef LTC_SOURCE
562562+/* internal helper functions */
563563+int der_length_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
564564+ unsigned long *outlen, unsigned long *payloadlen);
565565+/* SUBJECT PUBLIC KEY INFO */
566566+int der_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
567567+ unsigned int algorithm, void* public_key, unsigned long public_key_len,
568568+ unsigned long parameters_type, void* parameters, unsigned long parameters_len);
569569+570570+int der_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
571571+ unsigned int algorithm, void* public_key, unsigned long* public_key_len,
572572+ unsigned long parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
573573+#endif /* LTC_SOURCE */
574574+575575+/* SET */
576576+#define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0)
577577+#define der_length_set der_length_sequence
578578+int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
579579+ unsigned char *out, unsigned long *outlen);
580580+581581+int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
582582+ unsigned char *out, unsigned long *outlen);
583583+584584+/* VA list handy helpers with triplets of <type, size, data> */
585585+int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
586586+int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
587587+588588+/* FLEXI DECODER handle unknown list decoder */
589589+int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
590590+#define der_free_sequence_flexi der_sequence_free
591591+void der_sequence_free(ltc_asn1_list *in);
592592+void der_sequence_shrink(ltc_asn1_list *in);
593593+594594+/* BOOLEAN */
595595+int der_length_boolean(unsigned long *outlen);
596596+int der_encode_boolean(int in,
597597+ unsigned char *out, unsigned long *outlen);
598598+int der_decode_boolean(const unsigned char *in, unsigned long inlen,
599599+ int *out);
600600+/* INTEGER */
601601+int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
602602+int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
603603+int der_length_integer(void *num, unsigned long *len);
604604+605605+/* INTEGER -- handy for 0..2^32-1 values */
606606+int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num);
607607+int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen);
608608+int der_length_short_integer(unsigned long num, unsigned long *outlen);
609609+610610+/* BIT STRING */
611611+int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
612612+ unsigned char *out, unsigned long *outlen);
613613+int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
614614+ unsigned char *out, unsigned long *outlen);
615615+int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
616616+ unsigned char *out, unsigned long *outlen);
617617+int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
618618+ unsigned char *out, unsigned long *outlen);
619619+int der_length_bit_string(unsigned long nbits, unsigned long *outlen);
620620+621621+/* OCTET STRING */
622622+int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
623623+ unsigned char *out, unsigned long *outlen);
624624+int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
625625+ unsigned char *out, unsigned long *outlen);
626626+int der_length_octet_string(unsigned long noctets, unsigned long *outlen);
627627+628628+/* OBJECT IDENTIFIER */
629629+int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
630630+ unsigned char *out, unsigned long *outlen);
631631+int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
632632+ unsigned long *words, unsigned long *outlen);
633633+int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen);
634634+unsigned long der_object_identifier_bits(unsigned long x);
635635+636636+/* IA5 STRING */
637637+int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
638638+ unsigned char *out, unsigned long *outlen);
639639+int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
640640+ unsigned char *out, unsigned long *outlen);
641641+int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
642642+643643+int der_ia5_char_encode(int c);
644644+int der_ia5_value_decode(int v);
645645+646646+/* TELETEX STRING */
647647+int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
648648+ unsigned char *out, unsigned long *outlen);
649649+int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
650650+651651+#ifdef LTC_SOURCE
652652+/* internal helper functions */
653653+int der_teletex_char_encode(int c);
654654+int der_teletex_value_decode(int v);
655655+#endif /* LTC_SOURCE */
656656+657657+658658+/* PRINTABLE STRING */
659659+int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
660660+ unsigned char *out, unsigned long *outlen);
661661+int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
662662+ unsigned char *out, unsigned long *outlen);
663663+int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
664664+665665+int der_printable_char_encode(int c);
666666+int der_printable_value_decode(int v);
667667+668668+/* UTF-8 */
669669+#if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(__WCHAR_MAX__) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
670670+ #if defined(__WCHAR_MAX__)
671671+ #define LTC_WCHAR_MAX __WCHAR_MAX__
672672+ #else
673673+ #include <wchar.h>
674674+ #define LTC_WCHAR_MAX WCHAR_MAX
675675+ #endif
676676+/* please note that it might happen that LTC_WCHAR_MAX is undefined */
677677+#else
678678+ typedef ulong32 wchar_t;
679679+ #define LTC_WCHAR_MAX 0xFFFFFFFF
680680+#endif
681681+682682+int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
683683+ unsigned char *out, unsigned long *outlen);
684684+685685+int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
686686+ wchar_t *out, unsigned long *outlen);
687687+unsigned long der_utf8_charsize(const wchar_t c);
688688+#ifdef LTC_SOURCE
689689+/* internal helper functions */
690690+int der_utf8_valid_char(const wchar_t c);
691691+#endif /* LTC_SOURCE */
692692+int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
693693+694694+695695+/* CHOICE */
696696+int der_decode_choice(const unsigned char *in, unsigned long *inlen,
697697+ ltc_asn1_list *list, unsigned long outlen);
698698+699699+/* UTCTime */
700700+typedef struct {
701701+ unsigned YY, /* year */
702702+ MM, /* month */
703703+ DD, /* day */
704704+ hh, /* hour */
705705+ mm, /* minute */
706706+ ss, /* second */
707707+ off_dir, /* timezone offset direction 0 == +, 1 == - */
708708+ off_hh, /* timezone offset hours */
709709+ off_mm; /* timezone offset minutes */
710710+} ltc_utctime;
711711+712712+int der_encode_utctime(ltc_utctime *utctime,
713713+ unsigned char *out, unsigned long *outlen);
714714+715715+int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
716716+ ltc_utctime *out);
717717+718718+int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen);
719719+720720+/* GeneralizedTime */
721721+typedef struct {
722722+ unsigned YYYY, /* year */
723723+ MM, /* month */
724724+ DD, /* day */
725725+ hh, /* hour */
726726+ mm, /* minute */
727727+ ss, /* second */
728728+ fs, /* fractional seconds */
729729+ off_dir, /* timezone offset direction 0 == +, 1 == - */
730730+ off_hh, /* timezone offset hours */
731731+ off_mm; /* timezone offset minutes */
732732+} ltc_generalizedtime;
733733+734734+int der_encode_generalizedtime(ltc_generalizedtime *gtime,
735735+ unsigned char *out, unsigned long *outlen);
736736+737737+int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
738738+ ltc_generalizedtime *out);
739739+740740+int der_length_generalizedtime(ltc_generalizedtime *gtime, unsigned long *outlen);
741741+742742+743743+#endif
744744+745745+/* ref: HEAD -> master, tag: v1.18.2 */
746746+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
747747+/* commit time: 2018-07-01 22:49:01 +0200 */
+108
utils/tomcrypt/src/headers/tomcrypt_pkcs.h
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+1010+/* PKCS Header Info */
1111+1212+/* ===> PKCS #1 -- RSA Cryptography <=== */
1313+#ifdef LTC_PKCS_1
1414+1515+enum ltc_pkcs_1_v1_5_blocks
1616+{
1717+ LTC_PKCS_1_EMSA = 1, /* Block type 1 (PKCS #1 v1.5 signature padding) */
1818+ LTC_PKCS_1_EME = 2 /* Block type 2 (PKCS #1 v1.5 encryption padding) */
1919+};
2020+2121+enum ltc_pkcs_1_paddings
2222+{
2323+ LTC_PKCS_1_V1_5 = 1, /* PKCS #1 v1.5 padding (\sa ltc_pkcs_1_v1_5_blocks) */
2424+ LTC_PKCS_1_OAEP = 2, /* PKCS #1 v2.0 encryption padding */
2525+ LTC_PKCS_1_PSS = 3, /* PKCS #1 v2.1 signature padding */
2626+ LTC_PKCS_1_V1_5_NA1 = 4 /* PKCS #1 v1.5 padding - No ASN.1 (\sa ltc_pkcs_1_v1_5_blocks) */
2727+};
2828+2929+int pkcs_1_mgf1( int hash_idx,
3030+ const unsigned char *seed, unsigned long seedlen,
3131+ unsigned char *mask, unsigned long masklen);
3232+3333+int pkcs_1_i2osp(void *n, unsigned long modulus_len, unsigned char *out);
3434+int pkcs_1_os2ip(void *n, unsigned char *in, unsigned long inlen);
3535+3636+/* *** v1.5 padding */
3737+int pkcs_1_v1_5_encode(const unsigned char *msg,
3838+ unsigned long msglen,
3939+ int block_type,
4040+ unsigned long modulus_bitlen,
4141+ prng_state *prng,
4242+ int prng_idx,
4343+ unsigned char *out,
4444+ unsigned long *outlen);
4545+4646+int pkcs_1_v1_5_decode(const unsigned char *msg,
4747+ unsigned long msglen,
4848+ int block_type,
4949+ unsigned long modulus_bitlen,
5050+ unsigned char *out,
5151+ unsigned long *outlen,
5252+ int *is_valid);
5353+5454+/* *** v2.1 padding */
5555+int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
5656+ const unsigned char *lparam, unsigned long lparamlen,
5757+ unsigned long modulus_bitlen, prng_state *prng,
5858+ int prng_idx, int hash_idx,
5959+ unsigned char *out, unsigned long *outlen);
6060+6161+int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
6262+ const unsigned char *lparam, unsigned long lparamlen,
6363+ unsigned long modulus_bitlen, int hash_idx,
6464+ unsigned char *out, unsigned long *outlen,
6565+ int *res);
6666+6767+int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,
6868+ unsigned long saltlen, prng_state *prng,
6969+ int prng_idx, int hash_idx,
7070+ unsigned long modulus_bitlen,
7171+ unsigned char *out, unsigned long *outlen);
7272+7373+int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
7474+ const unsigned char *sig, unsigned long siglen,
7575+ unsigned long saltlen, int hash_idx,
7676+ unsigned long modulus_bitlen, int *res);
7777+7878+#endif /* LTC_PKCS_1 */
7979+8080+/* ===> PKCS #5 -- Password Based Cryptography <=== */
8181+#ifdef LTC_PKCS_5
8282+8383+/* Algorithm #1 (PBKDF1) */
8484+int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
8585+ const unsigned char *salt,
8686+ int iteration_count, int hash_idx,
8787+ unsigned char *out, unsigned long *outlen);
8888+8989+/* Algorithm #1 (PBKDF1) - OpenSSL-compatible variant for arbitrarily-long keys.
9090+ Compatible with EVP_BytesToKey() */
9191+int pkcs_5_alg1_openssl(const unsigned char *password,
9292+ unsigned long password_len,
9393+ const unsigned char *salt,
9494+ int iteration_count, int hash_idx,
9595+ unsigned char *out, unsigned long *outlen);
9696+9797+/* Algorithm #2 (PBKDF2) */
9898+int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
9999+ const unsigned char *salt, unsigned long salt_len,
100100+ int iteration_count, int hash_idx,
101101+ unsigned char *out, unsigned long *outlen);
102102+103103+int pkcs_5_test (void);
104104+#endif /* LTC_PKCS_5 */
105105+106106+/* ref: HEAD -> master, tag: v1.18.2 */
107107+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
108108+/* commit time: 2018-07-01 22:49:01 +0200 */
+447
utils/tomcrypt/src/headers/tomcrypt_private.h
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
22+/* SPDX-License-Identifier: Unlicense */
33+44+#include "tomcrypt.h"
55+66+/*
77+ * Internal Macros
88+ */
99+1010+#define LTC_PAD_MASK (0xF000U)
1111+1212+/*
1313+ * Internal Enums
1414+ */
1515+1616+enum ltc_oid_id {
1717+ PKA_RSA,
1818+ PKA_DSA,
1919+ PKA_EC,
2020+ PKA_EC_PRIMEF,
2121+ PKA_X25519,
2222+ PKA_ED25519,
2323+};
2424+2525+/*
2626+ * Internal Types
2727+ */
2828+2929+typedef struct {
3030+ int size;
3131+ const char *name, *base, *prime;
3232+} ltc_dh_set_type;
3333+3434+3535+typedef int (*fn_kdf_t)(const unsigned char *password, unsigned long password_len,
3636+ const unsigned char *salt, unsigned long salt_len,
3737+ int iteration_count, int hash_idx,
3838+ unsigned char *out, unsigned long *outlen);
3939+4040+typedef struct {
4141+ /* KDF */
4242+ fn_kdf_t kdf;
4343+ /* Hash or HMAC */
4444+ const char* h;
4545+ /* cipher */
4646+ const char* c;
4747+ unsigned long keylen;
4848+ /* not used for pbkdf2 */
4949+ unsigned long blocklen;
5050+} pbes_properties;
5151+5252+typedef struct
5353+{
5454+ pbes_properties type;
5555+ const void *pwd;
5656+ unsigned long pwdlen;
5757+ ltc_asn1_list *enc_data;
5858+ ltc_asn1_list *salt;
5959+ ltc_asn1_list *iv;
6060+ unsigned long iterations;
6161+ /* only used for RC2 */
6262+ unsigned long key_bits;
6363+} pbes_arg;
6464+6565+/*
6666+ * Internal functions
6767+ */
6868+6969+7070+/* tomcrypt_cipher.h */
7171+7272+void blowfish_enc(ulong32 *data, unsigned long blocks, const symmetric_key *skey);
7373+int blowfish_expand(const unsigned char *key, int keylen,
7474+ const unsigned char *data, int datalen,
7575+ symmetric_key *skey);
7676+int blowfish_setup_with_data(const unsigned char *key, int keylen,
7777+ const unsigned char *data, int datalen,
7878+ symmetric_key *skey);
7979+8080+/* tomcrypt_hash.h */
8181+8282+/* a simple macro for making hash "process" functions */
8383+#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
8484+int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
8585+{ \
8686+ unsigned long n; \
8787+ int err; \
8888+ LTC_ARGCHK(md != NULL); \
8989+ LTC_ARGCHK(in != NULL); \
9090+ if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
9191+ return CRYPT_INVALID_ARG; \
9292+ } \
9393+ if ((md-> state_var .length + inlen) < md-> state_var .length) { \
9494+ return CRYPT_HASH_OVERFLOW; \
9595+ } \
9696+ while (inlen > 0) { \
9797+ if (md-> state_var .curlen == 0 && inlen >= block_size) { \
9898+ if ((err = compress_name (md, in)) != CRYPT_OK) { \
9999+ return err; \
100100+ } \
101101+ md-> state_var .length += block_size * 8; \
102102+ in += block_size; \
103103+ inlen -= block_size; \
104104+ } else { \
105105+ n = MIN(inlen, (block_size - md-> state_var .curlen)); \
106106+ XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
107107+ md-> state_var .curlen += n; \
108108+ in += n; \
109109+ inlen -= n; \
110110+ if (md-> state_var .curlen == block_size) { \
111111+ if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
112112+ return err; \
113113+ } \
114114+ md-> state_var .length += 8*block_size; \
115115+ md-> state_var .curlen = 0; \
116116+ } \
117117+ } \
118118+ } \
119119+ return CRYPT_OK; \
120120+}
121121+122122+123123+/* tomcrypt_mac.h */
124124+125125+int ocb3_int_ntz(unsigned long x);
126126+void ocb3_int_xor_blocks(unsigned char *out, const unsigned char *block_a, const unsigned char *block_b, unsigned long block_len);
127127+128128+129129+/* tomcrypt_math.h */
130130+131131+#if !defined(DESC_DEF_ONLY)
132132+133133+#define MP_DIGIT_BIT ltc_mp.bits_per_digit
134134+135135+/* some handy macros */
136136+#define mp_init(a) ltc_mp.init(a)
137137+#define mp_init_multi ltc_init_multi
138138+#define mp_clear(a) ltc_mp.deinit(a)
139139+#define mp_clear_multi ltc_deinit_multi
140140+#define mp_cleanup_multi ltc_cleanup_multi
141141+#define mp_init_copy(a, b) ltc_mp.init_copy(a, b)
142142+143143+#define mp_neg(a, b) ltc_mp.neg(a, b)
144144+#define mp_copy(a, b) ltc_mp.copy(a, b)
145145+146146+#define mp_set(a, b) ltc_mp.set_int(a, b)
147147+#define mp_set_int(a, b) ltc_mp.set_int(a, b)
148148+#define mp_get_int(a) ltc_mp.get_int(a)
149149+#define mp_get_digit(a, n) ltc_mp.get_digit(a, n)
150150+#define mp_get_digit_count(a) ltc_mp.get_digit_count(a)
151151+#define mp_cmp(a, b) ltc_mp.compare(a, b)
152152+#define mp_cmp_d(a, b) ltc_mp.compare_d(a, b)
153153+#define mp_count_bits(a) ltc_mp.count_bits(a)
154154+#define mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a)
155155+#define mp_2expt(a, b) ltc_mp.twoexpt(a, b)
156156+157157+#define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c)
158158+#define mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c)
159159+#define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
160160+#define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
161161+#define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
162162+163163+#define mp_add(a, b, c) ltc_mp.add(a, b, c)
164164+#define mp_add_d(a, b, c) ltc_mp.addi(a, b, c)
165165+#define mp_sub(a, b, c) ltc_mp.sub(a, b, c)
166166+#define mp_sub_d(a, b, c) ltc_mp.subi(a, b, c)
167167+#define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
168168+#define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
169169+#define mp_sqr(a, b) ltc_mp.sqr(a, b)
170170+#define mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c)
171171+#define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
172172+#define mp_div_2(a, b) ltc_mp.div_2(a, b)
173173+#define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)
174174+#define mp_mod_d(a, b, c) ltc_mp.modi(a, b, c)
175175+#define mp_gcd(a, b, c) ltc_mp.gcd(a, b, c)
176176+#define mp_lcm(a, b, c) ltc_mp.lcm(a, b, c)
177177+178178+#define mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d)
179179+#define mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d)
180180+#define mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d)
181181+#define mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c)
182182+#define mp_invmod(a, b, c) ltc_mp.invmod(a, b, c)
183183+184184+#define mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b)
185185+#define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b)
186186+#define mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c)
187187+#define mp_montgomery_free(a) ltc_mp.montgomery_deinit(a)
188188+189189+#define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
190190+#define mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c)
191191+192192+#define mp_iszero(a) (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
193193+#define mp_isodd(a) (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
194194+#define mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0)
195195+196196+#define mp_tohex(a, b) mp_toradix(a, b, 16)
197197+198198+#define mp_rand(a, b) ltc_mp.rand(a, b)
199199+200200+#endif
201201+202202+203203+/* tomcrypt_misc.h */
204204+205205+void copy_or_zeromem(const unsigned char* src, unsigned char* dest, unsigned long len, int coz);
206206+207207+int pbes_decrypt(const pbes_arg *arg, unsigned char *dec_data, unsigned long *dec_size);
208208+209209+int pbes1_extract(const ltc_asn1_list *s, pbes_arg *res);
210210+int pbes2_extract(const ltc_asn1_list *s, pbes_arg *res);
211211+212212+213213+/* tomcrypt_pk.h */
214214+215215+int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
216216+int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
217217+218218+int pk_get_oid(enum ltc_oid_id id, const char **st);
219219+int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen);
220220+int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen);
221221+222222+/* ---- DH Routines ---- */
223223+#ifdef LTC_MRSA
224224+int rsa_init(rsa_key *key);
225225+void rsa_shrink_key(rsa_key *key);
226226+#endif /* LTC_MRSA */
227227+228228+/* ---- DH Routines ---- */
229229+#ifdef LTC_MDH
230230+extern const ltc_dh_set_type ltc_dh_sets[];
231231+232232+int dh_check_pubkey(const dh_key *key);
233233+#endif /* LTC_MDH */
234234+235235+/* ---- ECC Routines ---- */
236236+#ifdef LTC_MECC
237237+int ecc_set_curve_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key);
238238+int ecc_copy_curve(const ecc_key *srckey, ecc_key *key);
239239+int ecc_set_curve_by_size(int size, ecc_key *key);
240240+int ecc_import_subject_public_key_info(const unsigned char *in, unsigned long inlen, ecc_key *key);
241241+242242+#ifdef LTC_SSH
243243+int ecc_ssh_ecdsa_encode_name(char *buffer, unsigned long *buflen, const ecc_key *key);
244244+#endif
245245+246246+/* low level functions */
247247+ecc_point *ltc_ecc_new_point(void);
248248+void ltc_ecc_del_point(ecc_point *p);
249249+int ltc_ecc_set_point_xyz(ltc_mp_digit x, ltc_mp_digit y, ltc_mp_digit z, ecc_point *p);
250250+int ltc_ecc_copy_point(const ecc_point *src, ecc_point *dst);
251251+int ltc_ecc_is_point(const ltc_ecc_dp *dp, void *x, void *y);
252252+int ltc_ecc_is_point_at_infinity(const ecc_point *P, void *modulus, int *retval);
253253+int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *prime, void *a, void *b, void *x, void *y);
254254+int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed);
255255+int ltc_ecc_verify_key(const ecc_key *key);
256256+257257+/* point ops (mp == montgomery digit) */
258258+#if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
259259+/* R = 2P */
260260+int ltc_ecc_projective_dbl_point(const ecc_point *P, ecc_point *R, void *ma, void *modulus, void *mp);
261261+262262+/* R = P + Q */
263263+int ltc_ecc_projective_add_point(const ecc_point *P, const ecc_point *Q, ecc_point *R, void *ma, void *modulus, void *mp);
264264+#endif
265265+266266+#if defined(LTC_MECC_FP)
267267+/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
268268+int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
269269+270270+/* functions for saving/loading/freeing/adding to fixed point cache */
271271+int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
272272+int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
273273+void ltc_ecc_fp_free(void);
274274+int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
275275+276276+/* lock/unlock all points currently in fixed point cache */
277277+void ltc_ecc_fp_tablelock(int lock);
278278+#endif
279279+280280+/* R = kG */
281281+int ltc_ecc_mulmod(void *k, const ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
282282+283283+#ifdef LTC_ECC_SHAMIR
284284+/* kA*A + kB*B = C */
285285+int ltc_ecc_mul2add(const ecc_point *A, void *kA,
286286+ const ecc_point *B, void *kB,
287287+ ecc_point *C,
288288+ void *ma,
289289+ void *modulus);
290290+291291+#ifdef LTC_MECC_FP
292292+/* Shamir's trick with optimized point multiplication using fixed point cache */
293293+int ltc_ecc_fp_mul2add(const ecc_point *A, void *kA,
294294+ const ecc_point *B, void *kB,
295295+ ecc_point *C,
296296+ void *ma,
297297+ void *modulus);
298298+#endif
299299+300300+#endif
301301+302302+303303+/* map P to affine from projective */
304304+int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
305305+#endif /* LTC_MECC */
306306+307307+#ifdef LTC_MDSA
308308+int dsa_int_validate_xy(const dsa_key *key, int *stat);
309309+int dsa_int_validate_pqg(const dsa_key *key, int *stat);
310310+int dsa_int_validate_primes(const dsa_key *key, int *stat);
311311+#endif /* LTC_MDSA */
312312+313313+314314+#ifdef LTC_CURVE25519
315315+316316+int tweetnacl_crypto_sign(
317317+ unsigned char *sm,unsigned long long *smlen,
318318+ const unsigned char *m,unsigned long long mlen,
319319+ const unsigned char *sk, const unsigned char *pk);
320320+int tweetnacl_crypto_sign_open(
321321+ int *stat,
322322+ unsigned char *m,unsigned long long *mlen,
323323+ const unsigned char *sm,unsigned long long smlen,
324324+ const unsigned char *pk);
325325+int tweetnacl_crypto_sign_keypair(prng_state *prng, int wprng, unsigned char *pk,unsigned char *sk);
326326+int tweetnacl_crypto_sk_to_pk(unsigned char *pk, const unsigned char *sk);
327327+int tweetnacl_crypto_scalarmult(unsigned char *q, const unsigned char *n, const unsigned char *p);
328328+int tweetnacl_crypto_scalarmult_base(unsigned char *q,const unsigned char *n);
329329+330330+typedef int (*sk_to_pk)(unsigned char *pk ,const unsigned char *sk);
331331+int ec25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
332332+ const void *pwd, unsigned long pwdlen,
333333+ enum ltc_oid_id id, sk_to_pk fp,
334334+ curve25519_key *key);
335335+int ec25519_export( unsigned char *out, unsigned long *outlen,
336336+ int which,
337337+ const curve25519_key *key);
338338+#endif /* LTC_CURVE25519 */
339339+340340+#ifdef LTC_DER
341341+342342+#define LTC_ASN1_IS_TYPE(e, t) (((e) != NULL) && ((e)->type == (t)))
343343+344344+/* DER handling */
345345+int der_decode_custom_type_ex(const unsigned char *in, unsigned long inlen,
346346+ ltc_asn1_list *root,
347347+ ltc_asn1_list *list, unsigned long outlen, unsigned int flags);
348348+349349+int der_encode_asn1_identifier(const ltc_asn1_list *id, unsigned char *out, unsigned long *outlen);
350350+int der_decode_asn1_identifier(const unsigned char *in, unsigned long *inlen, ltc_asn1_list *id);
351351+int der_length_asn1_identifier(const ltc_asn1_list *id, unsigned long *idlen);
352352+353353+int der_encode_asn1_length(unsigned long len, unsigned char* out, unsigned long* outlen);
354354+int der_decode_asn1_length(const unsigned char *in, unsigned long *inlen, unsigned long *outlen);
355355+int der_length_asn1_length(unsigned long len, unsigned long *outlen);
356356+357357+int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
358358+ unsigned long *outlen, unsigned long *payloadlen);
359359+360360+extern const ltc_asn1_type der_asn1_tag_to_type_map[];
361361+extern const unsigned long der_asn1_tag_to_type_map_sz;
362362+363363+extern const int der_asn1_type_to_identifier_map[];
364364+extern const unsigned long der_asn1_type_to_identifier_map_sz;
365365+366366+int der_decode_sequence_multi_ex(const unsigned char *in, unsigned long inlen, unsigned int flags, ...);
367367+368368+int der_teletex_char_encode(int c);
369369+int der_teletex_value_decode(int v);
370370+371371+int der_utf8_valid_char(const wchar_t c);
372372+373373+typedef int (*public_key_decode_cb)(const unsigned char *in, unsigned long inlen, void *ctx);
374374+375375+int x509_decode_public_key_from_certificate(const unsigned char *in, unsigned long inlen,
376376+ enum ltc_oid_id algorithm, ltc_asn1_type param_type,
377377+ ltc_asn1_list* parameters, unsigned long *parameters_len,
378378+ public_key_decode_cb callback, void *ctx);
379379+380380+/* SUBJECT PUBLIC KEY INFO */
381381+int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
382382+ unsigned int algorithm, const void* public_key, unsigned long public_key_len,
383383+ ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
384384+385385+int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
386386+ unsigned int algorithm, void* public_key, unsigned long* public_key_len,
387387+ ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len);
388388+389389+int pk_oid_cmp_with_ulong(const char *o1, const unsigned long *o2, unsigned long o2size);
390390+int pk_oid_cmp_with_asn1(const char *o1, const ltc_asn1_list *o2);
391391+392392+#endif /* LTC_DER */
393393+394394+/* tomcrypt_pkcs.h */
395395+396396+#ifdef LTC_PKCS_8
397397+398398+int pkcs8_decode_flexi(const unsigned char *in, unsigned long inlen,
399399+ const void *pwd, unsigned long pwdlen,
400400+ ltc_asn1_list **decoded_list);
401401+402402+#endif /* LTC_PKCS_8 */
403403+404404+405405+#ifdef LTC_PKCS_12
406406+407407+int pkcs12_utf8_to_utf16(const unsigned char *in, unsigned long inlen,
408408+ unsigned char *out, unsigned long *outlen);
409409+410410+int pkcs12_kdf( int hash_id,
411411+ const unsigned char *pw, unsigned long pwlen,
412412+ const unsigned char *salt, unsigned long saltlen,
413413+ unsigned int iterations, unsigned char purpose,
414414+ unsigned char *out, unsigned long outlen);
415415+416416+#endif /* LTC_PKCS_12 */
417417+418418+/* tomcrypt_prng.h */
419419+420420+#define LTC_PRNG_EXPORT(which) \
421421+int which ## _export(unsigned char *out, unsigned long *outlen, prng_state *prng) \
422422+{ \
423423+ unsigned long len = which ## _desc.export_size; \
424424+ \
425425+ LTC_ARGCHK(prng != NULL); \
426426+ LTC_ARGCHK(out != NULL); \
427427+ LTC_ARGCHK(outlen != NULL); \
428428+ \
429429+ if (*outlen < len) { \
430430+ *outlen = len; \
431431+ return CRYPT_BUFFER_OVERFLOW; \
432432+ } \
433433+ \
434434+ if (which ## _read(out, len, prng) != len) { \
435435+ return CRYPT_ERROR_READPRNG; \
436436+ } \
437437+ \
438438+ *outlen = len; \
439439+ return CRYPT_OK; \
440440+}
441441+442442+/* extract a byte portably */
443443+#ifdef _MSC_VER
444444+ #define LTC_BYTE(x, n) ((unsigned char)((x) >> (8 * (n))))
445445+#else
446446+ #define LTC_BYTE(x, n) (((x) >> (8 * (n))) & 255)
447447+#endif
+232
utils/tomcrypt/src/headers/tomcrypt_prng.h
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+1010+/* ---- PRNG Stuff ---- */
1111+#ifdef LTC_YARROW
1212+struct yarrow_prng {
1313+ int cipher, hash;
1414+ unsigned char pool[MAXBLOCKSIZE];
1515+ symmetric_CTR ctr;
1616+};
1717+#endif
1818+1919+#ifdef LTC_RC4
2020+struct rc4_prng {
2121+ rc4_state s;
2222+};
2323+#endif
2424+2525+#ifdef LTC_CHACHA20_PRNG
2626+struct chacha20_prng {
2727+ chacha_state s; /* chacha state */
2828+ unsigned char ent[40]; /* entropy buffer */
2929+ unsigned long idx; /* entropy counter */
3030+};
3131+#endif
3232+3333+#ifdef LTC_FORTUNA
3434+struct fortuna_prng {
3535+ hash_state pool[LTC_FORTUNA_POOLS]; /* the pools */
3636+3737+ symmetric_key skey;
3838+3939+ unsigned char K[32], /* the current key */
4040+ IV[16]; /* IV for CTR mode */
4141+4242+ unsigned long pool_idx, /* current pool we will add to */
4343+ pool0_len, /* length of 0'th pool */
4444+ wd;
4545+4646+ ulong64 reset_cnt; /* number of times we have reset */
4747+};
4848+#endif
4949+5050+#ifdef LTC_SOBER128
5151+struct sober128_prng {
5252+ sober128_state s; /* sober128 state */
5353+ unsigned char ent[40]; /* entropy buffer */
5454+ unsigned long idx; /* entropy counter */
5555+};
5656+#endif
5757+5858+typedef struct {
5959+ union {
6060+ char dummy[1];
6161+#ifdef LTC_YARROW
6262+ struct yarrow_prng yarrow;
6363+#endif
6464+#ifdef LTC_RC4
6565+ struct rc4_prng rc4;
6666+#endif
6767+#ifdef LTC_CHACHA20_PRNG
6868+ struct chacha20_prng chacha;
6969+#endif
7070+#ifdef LTC_FORTUNA
7171+ struct fortuna_prng fortuna;
7272+#endif
7373+#ifdef LTC_SOBER128
7474+ struct sober128_prng sober128;
7575+#endif
7676+ };
7777+ short ready; /* ready flag 0-1 */
7878+ LTC_MUTEX_TYPE(lock) /* lock */
7979+} prng_state;
8080+8181+/** PRNG descriptor */
8282+extern struct ltc_prng_descriptor {
8383+ /** Name of the PRNG */
8484+ const char *name;
8585+ /** size in bytes of exported state */
8686+ int export_size;
8787+ /** Start a PRNG state
8888+ @param prng [out] The state to initialize
8989+ @return CRYPT_OK if successful
9090+ */
9191+ int (*start)(prng_state *prng);
9292+ /** Add entropy to the PRNG
9393+ @param in The entropy
9494+ @param inlen Length of the entropy (octets)\
9595+ @param prng The PRNG state
9696+ @return CRYPT_OK if successful
9797+ */
9898+ int (*add_entropy)(const unsigned char *in, unsigned long inlen, prng_state *prng);
9999+ /** Ready a PRNG state to read from
100100+ @param prng The PRNG state to ready
101101+ @return CRYPT_OK if successful
102102+ */
103103+ int (*ready)(prng_state *prng);
104104+ /** Read from the PRNG
105105+ @param out [out] Where to store the data
106106+ @param outlen Length of data desired (octets)
107107+ @param prng The PRNG state to read from
108108+ @return Number of octets read
109109+ */
110110+ unsigned long (*read)(unsigned char *out, unsigned long outlen, prng_state *prng);
111111+ /** Terminate a PRNG state
112112+ @param prng The PRNG state to terminate
113113+ @return CRYPT_OK if successful
114114+ */
115115+ int (*done)(prng_state *prng);
116116+ /** Export a PRNG state
117117+ @param out [out] The destination for the state
118118+ @param outlen [in/out] The max size and resulting size of the PRNG state
119119+ @param prng The PRNG to export
120120+ @return CRYPT_OK if successful
121121+ */
122122+ int (*pexport)(unsigned char *out, unsigned long *outlen, prng_state *prng);
123123+ /** Import a PRNG state
124124+ @param in The data to import
125125+ @param inlen The length of the data to import (octets)
126126+ @param prng The PRNG to initialize/import
127127+ @return CRYPT_OK if successful
128128+ */
129129+ int (*pimport)(const unsigned char *in, unsigned long inlen, prng_state *prng);
130130+ /** Self-test the PRNG
131131+ @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
132132+ */
133133+ int (*test)(void);
134134+} prng_descriptor[];
135135+136136+#ifdef LTC_YARROW
137137+int yarrow_start(prng_state *prng);
138138+int yarrow_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
139139+int yarrow_ready(prng_state *prng);
140140+unsigned long yarrow_read(unsigned char *out, unsigned long outlen, prng_state *prng);
141141+int yarrow_done(prng_state *prng);
142142+int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
143143+int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
144144+int yarrow_test(void);
145145+extern const struct ltc_prng_descriptor yarrow_desc;
146146+#endif
147147+148148+#ifdef LTC_FORTUNA
149149+int fortuna_start(prng_state *prng);
150150+int fortuna_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
151151+int fortuna_ready(prng_state *prng);
152152+unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state *prng);
153153+int fortuna_done(prng_state *prng);
154154+int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
155155+int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
156156+int fortuna_test(void);
157157+extern const struct ltc_prng_descriptor fortuna_desc;
158158+#endif
159159+160160+#ifdef LTC_RC4
161161+int rc4_start(prng_state *prng);
162162+int rc4_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
163163+int rc4_ready(prng_state *prng);
164164+unsigned long rc4_read(unsigned char *out, unsigned long outlen, prng_state *prng);
165165+int rc4_done(prng_state *prng);
166166+int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
167167+int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
168168+int rc4_test(void);
169169+extern const struct ltc_prng_descriptor rc4_desc;
170170+#endif
171171+172172+#ifdef LTC_CHACHA20_PRNG
173173+int chacha20_prng_start(prng_state *prng);
174174+int chacha20_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
175175+int chacha20_prng_ready(prng_state *prng);
176176+unsigned long chacha20_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng);
177177+int chacha20_prng_done(prng_state *prng);
178178+int chacha20_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
179179+int chacha20_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
180180+int chacha20_prng_test(void);
181181+extern const struct ltc_prng_descriptor chacha20_prng_desc;
182182+#endif
183183+184184+#ifdef LTC_SPRNG
185185+int sprng_start(prng_state *prng);
186186+int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
187187+int sprng_ready(prng_state *prng);
188188+unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng);
189189+int sprng_done(prng_state *prng);
190190+int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
191191+int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
192192+int sprng_test(void);
193193+extern const struct ltc_prng_descriptor sprng_desc;
194194+#endif
195195+196196+#ifdef LTC_SOBER128
197197+int sober128_start(prng_state *prng);
198198+int sober128_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
199199+int sober128_ready(prng_state *prng);
200200+unsigned long sober128_read(unsigned char *out, unsigned long outlen, prng_state *prng);
201201+int sober128_done(prng_state *prng);
202202+int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
203203+int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
204204+int sober128_test(void);
205205+extern const struct ltc_prng_descriptor sober128_desc;
206206+#endif
207207+208208+int find_prng(const char *name);
209209+int register_prng(const struct ltc_prng_descriptor *prng);
210210+int unregister_prng(const struct ltc_prng_descriptor *prng);
211211+int register_all_prngs(void);
212212+int prng_is_valid(int idx);
213213+LTC_MUTEX_PROTO(ltc_prng_mutex)
214214+215215+/* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this
216216+ * might not work on all platforms as planned
217217+ */
218218+unsigned long rng_get_bytes(unsigned char *out,
219219+ unsigned long outlen,
220220+ void (*callback)(void));
221221+222222+int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void));
223223+224224+#ifdef LTC_PRNG_ENABLE_LTC_RNG
225225+extern unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen,
226226+ void (*callback)(void));
227227+#endif
228228+229229+230230+/* ref: HEAD -> master, tag: v1.18.2 */
231231+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
232232+/* commit time: 2018-07-01 22:49:01 +0200 */
+87
utils/tomcrypt/src/misc/compare_testvector.c
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+1010+#include "tomcrypt.h"
1111+1212+/**
1313+ @file compare_testvector.c
1414+ Function to compare two testvectors and print a (detailed) error-message if required, Steffen Jaeckel
1515+*/
1616+1717+#if defined(LTC_TEST) && defined(LTC_TEST_DBG)
1818+static void _print_hex(const char* what, const void* v, const unsigned long l)
1919+{
2020+ const unsigned char* p = v;
2121+ unsigned long x, y = 0, z;
2222+ fprintf(stderr, "%s contents: \n", what);
2323+ for (x = 0; x < l; ) {
2424+ fprintf(stderr, "%02X ", p[x]);
2525+ if (!(++x % 16) || x == l) {
2626+ if((x % 16) != 0) {
2727+ z = 16 - (x % 16);
2828+ if(z >= 8)
2929+ fprintf(stderr, " ");
3030+ for (; z != 0; --z) {
3131+ fprintf(stderr, " ");
3232+ }
3333+ }
3434+ fprintf(stderr, " | ");
3535+ for(; y < x; y++) {
3636+ if((y % 8) == 0)
3737+ fprintf(stderr, " ");
3838+ if(isgraph(p[y]))
3939+ fprintf(stderr, "%c", p[y]);
4040+ else
4141+ fprintf(stderr, ".");
4242+ }
4343+ fprintf(stderr, "\n");
4444+ }
4545+ else if((x % 8) == 0) {
4646+ fprintf(stderr, " ");
4747+ }
4848+ }
4949+}
5050+#endif
5151+5252+/**
5353+ Compare two test-vectors
5454+5555+ @param is The data as it is
5656+ @param is_len The length of is
5757+ @param should The data as it should
5858+ @param should_len The length of should
5959+ @param what The type of the data
6060+ @param which The iteration count
6161+ @return 0 on equality, -1 or 1 on difference
6262+*/
6363+int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which)
6464+{
6565+ int res = 0;
6666+ if(is_len != should_len)
6767+ res = is_len > should_len ? -1 : 1;
6868+ else
6969+ res = XMEMCMP(is, should, is_len);
7070+7171+#if defined(LTC_TEST) && defined(LTC_TEST_DBG)
7272+ if (res != 0) {
7373+ fprintf(stderr, "Testvector #%i of %s failed:\n", which, what);
7474+ _print_hex("SHOULD", should, should_len);
7575+ _print_hex("IS ", is, is_len);
7676+ }
7777+#else
7878+ LTC_UNUSED_PARAM(which);
7979+ LTC_UNUSED_PARAM(what);
8080+#endif
8181+8282+ return res;
8383+}
8484+8585+/* ref: HEAD -> master, tag: v1.18.2 */
8686+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
8787+/* commit time: 2018-07-01 22:49:01 +0200 */
+27
utils/tomcrypt/src/misc/crypt/crypt_argchk.c
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+#include "tomcrypt.h"
1010+1111+/**
1212+ @file crypt_argchk.c
1313+ Perform argument checking, Tom St Denis
1414+*/
1515+1616+#if (ARGTYPE == 0)
1717+void crypt_argchk(const char *v, const char *s, int d)
1818+{
1919+ fprintf(stderr, "LTC_ARGCHK '%s' failure on line %d of file %s\n",
2020+ v, d, s);
2121+ abort();
2222+}
2323+#endif
2424+2525+/* ref: HEAD -> master, tag: v1.18.2 */
2626+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
2727+/* commit time: 2018-07-01 22:49:01 +0200 */
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+#include "tomcrypt.h"
1010+1111+/**
1212+ @file crypt_cipher_is_valid.c
1313+ Determine if cipher is valid, Tom St Denis
1414+*/
1515+1616+/*
1717+ Test if a cipher index is valid
1818+ @param idx The index of the cipher to search for
1919+ @return CRYPT_OK if valid
2020+*/
2121+int cipher_is_valid(int idx)
2222+{
2323+ LTC_MUTEX_LOCK(<c_cipher_mutex);
2424+ if (idx < 0 || idx >= TAB_SIZE || cipher_descriptor[idx].name == NULL) {
2525+ LTC_MUTEX_UNLOCK(<c_cipher_mutex);
2626+ return CRYPT_INVALID_CIPHER;
2727+ }
2828+ LTC_MUTEX_UNLOCK(<c_cipher_mutex);
2929+ return CRYPT_OK;
3030+}
3131+3232+/* ref: HEAD -> master, tag: v1.18.2 */
3333+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
3434+/* commit time: 2018-07-01 22:49:01 +0200 */
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+#include "tomcrypt.h"
1010+1111+/**
1212+ @file crypt_register_cipher.c
1313+ Register a cipher, Tom St Denis
1414+*/
1515+1616+/**
1717+ Register a cipher with the descriptor table
1818+ @param cipher The cipher you wish to register
1919+ @return value >= 0 if successfully added (or already present), -1 if unsuccessful
2020+*/
2121+int register_cipher(const struct ltc_cipher_descriptor *cipher)
2222+{
2323+ int x;
2424+2525+ LTC_ARGCHK(cipher != NULL);
2626+2727+ /* is it already registered? */
2828+ LTC_MUTEX_LOCK(<c_cipher_mutex);
2929+ for (x = 0; x < TAB_SIZE; x++) {
3030+ if (cipher_descriptor[x].name != NULL && cipher_descriptor[x].ID == cipher->ID) {
3131+ LTC_MUTEX_UNLOCK(<c_cipher_mutex);
3232+ return x;
3333+ }
3434+ }
3535+3636+ /* find a blank spot */
3737+ for (x = 0; x < TAB_SIZE; x++) {
3838+ if (cipher_descriptor[x].name == NULL) {
3939+ XMEMCPY(&cipher_descriptor[x], cipher, sizeof(struct ltc_cipher_descriptor));
4040+ LTC_MUTEX_UNLOCK(<c_cipher_mutex);
4141+ return x;
4242+ }
4343+ }
4444+4545+ /* no spot */
4646+ LTC_MUTEX_UNLOCK(<c_cipher_mutex);
4747+ return -1;
4848+}
4949+5050+/* ref: HEAD -> master, tag: v1.18.2 */
5151+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
5252+/* commit time: 2018-07-01 22:49:01 +0200 */
+32
utils/tomcrypt/src/misc/zeromem.c
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+#include "tomcrypt.h"
1010+1111+/**
1212+ @file zeromem.c
1313+ Zero a block of memory, Tom St Denis
1414+*/
1515+1616+/**
1717+ Zero a block of memory
1818+ @param out The destination of the area to zero
1919+ @param outlen The length of the area to zero (octets)
2020+*/
2121+void zeromem(volatile void *out, size_t outlen)
2222+{
2323+ volatile char *mem = out;
2424+ LTC_ARGCHKVD(out != NULL);
2525+ while (outlen-- > 0) {
2626+ *mem++ = '\0';
2727+ }
2828+}
2929+3030+/* ref: HEAD -> master, tag: v1.18.2 */
3131+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
3232+/* commit time: 2018-07-01 22:49:01 +0200 */
+95
utils/tomcrypt/src/modes/cbc/cbc_decrypt.c
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+#include "tomcrypt.h"
1010+1111+/**
1212+ @file cbc_decrypt.c
1313+ CBC implementation, encrypt block, Tom St Denis
1414+*/
1515+1616+1717+#ifdef LTC_CBC_MODE
1818+1919+/**
2020+ CBC decrypt
2121+ @param ct Ciphertext
2222+ @param pt [out] Plaintext
2323+ @param len The number of bytes to process (must be multiple of block length)
2424+ @param cbc CBC state
2525+ @return CRYPT_OK if successful
2626+*/
2727+int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc)
2828+{
2929+ int x, err;
3030+ unsigned char tmp[16];
3131+#ifdef LTC_FAST
3232+ LTC_FAST_TYPE tmpy;
3333+#else
3434+ unsigned char tmpy;
3535+#endif
3636+3737+ LTC_ARGCHK(pt != NULL);
3838+ LTC_ARGCHK(ct != NULL);
3939+ LTC_ARGCHK(cbc != NULL);
4040+4141+ if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
4242+ return err;
4343+ }
4444+4545+ /* is blocklen valid? */
4646+ if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV) || cbc->blocklen > (int)sizeof(tmp)) {
4747+ return CRYPT_INVALID_ARG;
4848+ }
4949+5050+ if (len % cbc->blocklen) {
5151+ return CRYPT_INVALID_ARG;
5252+ }
5353+#ifdef LTC_FAST
5454+ if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
5555+ return CRYPT_INVALID_ARG;
5656+ }
5757+#endif
5858+5959+ if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
6060+ return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
6161+ } else {
6262+ while (len) {
6363+ /* decrypt */
6464+ if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
6565+ return err;
6666+ }
6767+6868+ /* xor IV against plaintext */
6969+ #if defined(LTC_FAST)
7070+ for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
7171+ tmpy = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)tmp + x));
7272+ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
7373+ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) = tmpy;
7474+ }
7575+ #else
7676+ for (x = 0; x < cbc->blocklen; x++) {
7777+ tmpy = tmp[x] ^ cbc->IV[x];
7878+ cbc->IV[x] = ct[x];
7979+ pt[x] = tmpy;
8080+ }
8181+ #endif
8282+8383+ ct += cbc->blocklen;
8484+ pt += cbc->blocklen;
8585+ len -= cbc->blocklen;
8686+ }
8787+ }
8888+ return CRYPT_OK;
8989+}
9090+9191+#endif
9292+9393+/* ref: HEAD -> master, tag: v1.18.2 */
9494+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
9595+/* commit time: 2018-07-01 22:49:01 +0200 */
+96
utils/tomcrypt/src/modes/cbc/cbc_encrypt.c
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+#include "tomcrypt.h"
1010+1111+/**
1212+ @file cbc_encrypt.c
1313+ CBC implementation, encrypt block, Tom St Denis
1414+*/
1515+1616+1717+#ifdef LTC_CBC_MODE
1818+1919+/**
2020+ CBC encrypt
2121+ @param pt Plaintext
2222+ @param ct [out] Ciphertext
2323+ @param len The number of bytes to process (must be multiple of block length)
2424+ @param cbc CBC state
2525+ @return CRYPT_OK if successful
2626+*/
2727+int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CBC *cbc)
2828+{
2929+ int x, err;
3030+3131+ LTC_ARGCHK(pt != NULL);
3232+ LTC_ARGCHK(ct != NULL);
3333+ LTC_ARGCHK(cbc != NULL);
3434+3535+ if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
3636+ return err;
3737+ }
3838+3939+ /* is blocklen valid? */
4040+ if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) {
4141+ return CRYPT_INVALID_ARG;
4242+ }
4343+4444+ if (len % cbc->blocklen) {
4545+ return CRYPT_INVALID_ARG;
4646+ }
4747+#ifdef LTC_FAST
4848+ if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
4949+ return CRYPT_INVALID_ARG;
5050+ }
5151+#endif
5252+5353+ if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) {
5454+ return cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key);
5555+ } else {
5656+ while (len) {
5757+ /* xor IV against plaintext */
5858+ #if defined(LTC_FAST)
5959+ for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
6060+ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x));
6161+ }
6262+ #else
6363+ for (x = 0; x < cbc->blocklen; x++) {
6464+ cbc->IV[x] ^= pt[x];
6565+ }
6666+ #endif
6767+6868+ /* encrypt */
6969+ if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) {
7070+ return err;
7171+ }
7272+7373+ /* store IV [ciphertext] for a future block */
7474+ #if defined(LTC_FAST)
7575+ for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
7676+ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
7777+ }
7878+ #else
7979+ for (x = 0; x < cbc->blocklen; x++) {
8080+ cbc->IV[x] = ct[x];
8181+ }
8282+ #endif
8383+8484+ ct += cbc->blocklen;
8585+ pt += cbc->blocklen;
8686+ len -= cbc->blocklen;
8787+ }
8888+ }
8989+ return CRYPT_OK;
9090+}
9191+9292+#endif
9393+9494+/* ref: HEAD -> master, tag: v1.18.2 */
9595+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
9696+/* commit time: 2018-07-01 22:49:01 +0200 */
+60
utils/tomcrypt/src/modes/cbc/cbc_start.c
···11+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
22+ *
33+ * LibTomCrypt is a library that provides various cryptographic
44+ * algorithms in a highly modular and flexible manner.
55+ *
66+ * The library is free for all purposes without any express
77+ * guarantee it works.
88+ */
99+#include "tomcrypt.h"
1010+1111+/**
1212+ @file cbc_start.c
1313+ CBC implementation, start chain, Tom St Denis
1414+*/
1515+1616+#ifdef LTC_CBC_MODE
1717+1818+/**
1919+ Initialize a CBC context
2020+ @param cipher The index of the cipher desired
2121+ @param IV The initialization vector
2222+ @param key The secret key
2323+ @param keylen The length of the secret key (octets)
2424+ @param num_rounds Number of rounds in the cipher desired (0 for default)
2525+ @param cbc The CBC state to initialize
2626+ @return CRYPT_OK if successful
2727+*/
2828+int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
2929+ int keylen, int num_rounds, symmetric_CBC *cbc)
3030+{
3131+ int x, err;
3232+3333+ LTC_ARGCHK(IV != NULL);
3434+ LTC_ARGCHK(key != NULL);
3535+ LTC_ARGCHK(cbc != NULL);
3636+3737+ /* bad param? */
3838+ if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
3939+ return err;
4040+ }
4141+4242+ /* setup cipher */
4343+ if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cbc->key)) != CRYPT_OK) {
4444+ return err;
4545+ }
4646+4747+ /* copy IV */
4848+ cbc->blocklen = cipher_descriptor[cipher].block_length;
4949+ cbc->cipher = cipher;
5050+ for (x = 0; x < cbc->blocklen; x++) {
5151+ cbc->IV[x] = IV[x];
5252+ }
5353+ return CRYPT_OK;
5454+}
5555+5656+#endif
5757+5858+/* ref: HEAD -> master, tag: v1.18.2 */
5959+/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
6060+/* commit time: 2018-07-01 22:49:01 +0200 */