1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 
11 /* ---- LTC_BASE64 Routines ---- */
12 #ifdef LTC_BASE64
13 int base64_encode(const unsigned char *in,  unsigned long inlen,
14                                  char *out, unsigned long *outlen);
15 
16 int base64_decode(const char *in,  unsigned long inlen,
17                         unsigned char *out, unsigned long *outlen);
18 int base64_strict_decode(const char *in,  unsigned long inlen,
19                         unsigned char *out, unsigned long *outlen);
20 int base64_sane_decode(const char *in,  unsigned long inlen,
21                         unsigned char *out, unsigned long *outlen);
22 #endif
23 
24 #ifdef LTC_BASE64_URL
25 int base64url_encode(const unsigned char *in,  unsigned long inlen,
26                                     char *out, unsigned long *outlen);
27 int base64url_strict_encode(const unsigned char *in,  unsigned long inlen,
28                                            char *out, unsigned long *outlen);
29 
30 int base64url_decode(const char *in,  unsigned long inlen,
31                         unsigned char *out, unsigned long *outlen);
32 int base64url_strict_decode(const char *in,  unsigned long inlen,
33                         unsigned char *out, unsigned long *outlen);
34 int base64url_sane_decode(const char *in,  unsigned long inlen,
35                         unsigned char *out, unsigned long *outlen);
36 #endif
37 
38 /* ---- BASE32 Routines ---- */
39 #ifdef LTC_BASE32
40 typedef enum {
41    BASE32_RFC4648   = 0,
42    BASE32_BASE32HEX = 1,
43    BASE32_ZBASE32   = 2,
44    BASE32_CROCKFORD = 3
45 } base32_alphabet;
46 int base32_encode(const unsigned char *in,  unsigned long inlen,
47                                  char *out, unsigned long *outlen,
48                         base32_alphabet id);
49 int base32_decode(const          char *in,  unsigned long inlen,
50                         unsigned char *out, unsigned long *outlen,
51                         base32_alphabet id);
52 #endif
53 
54 /* ---- BASE16 Routines ---- */
55 #ifdef LTC_BASE16
56 int base16_encode(const unsigned char *in,  unsigned long  inlen,
57                                  char *out, unsigned long *outlen,
58                         unsigned int   options);
59 int base16_decode(const          char *in,  unsigned long  inlen,
60                         unsigned char *out, unsigned long *outlen);
61 #endif
62 
63 /* ===> LTC_HKDF -- RFC5869 HMAC-based Key Derivation Function <=== */
64 #ifdef LTC_HKDF
65 
66 int hkdf_test(void);
67 
68 int hkdf_extract(int hash_idx,
69                  const unsigned char *salt, unsigned long saltlen,
70                  const unsigned char *in,   unsigned long inlen,
71                        unsigned char *out,  unsigned long *outlen);
72 
73 int hkdf_expand(int hash_idx,
74                 const unsigned char *info, unsigned long infolen,
75                 const unsigned char *in,   unsigned long inlen,
76                       unsigned char *out,  unsigned long outlen);
77 
78 int hkdf(int hash_idx,
79          const unsigned char *salt, unsigned long saltlen,
80          const unsigned char *info, unsigned long infolen,
81          const unsigned char *in,   unsigned long inlen,
82                unsigned char *out,  unsigned long outlen);
83 
84 #endif  /* LTC_HKDF */
85 
86 /* ---- MEM routines ---- */
87 int mem_neq(const void *a, const void *b, size_t len);
88 void zeromem(volatile void *out, size_t outlen);
89 void burn_stack(unsigned long len);
90 
91 const char *error_to_string(int err);
92 
93 extern const char *crypt_build_settings;
94 
95 /* ---- HMM ---- */
96 int crypt_fsa(void *mp, ...);
97 
98 /* ---- Dynamic language support ---- */
99 int crypt_get_constant(const char* namein, int *valueout);
100 int crypt_list_all_constants(char *names_list, unsigned int *names_list_size);
101 
102 int crypt_get_size(const char* namein, unsigned int *sizeout);
103 int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size);
104 
105 #ifdef LTM_DESC
106 LTC_DEPRECATED void init_LTM(void);
107 #endif
108 #ifdef TFM_DESC
109 LTC_DEPRECATED void init_TFM(void);
110 #endif
111 #ifdef GMP_DESC
112 LTC_DEPRECATED void init_GMP(void);
113 #endif
114 int crypt_mp_init(const char* mpi);
115 
116 #ifdef LTC_ADLER32
117 typedef struct adler32_state_s
118 {
119    unsigned short s[2];
120 } adler32_state;
121 
122 void adler32_init(adler32_state *ctx);
123 void adler32_update(adler32_state *ctx, const unsigned char *input, unsigned long length);
124 void adler32_finish(const adler32_state *ctx, void *hash, unsigned long size);
125 int adler32_test(void);
126 #endif
127 
128 #ifdef LTC_CRC32
129 typedef struct crc32_state_s
130 {
131    ulong32 crc;
132 } crc32_state;
133 
134 void crc32_init(crc32_state *ctx);
135 void crc32_update(crc32_state *ctx, const unsigned char *input, unsigned long length);
136 void crc32_finish(const crc32_state *ctx, void *hash, unsigned long size);
137 int crc32_test(void);
138 #endif
139 
140 
141 #ifdef LTC_PADDING
142 
143 enum padding_type {
144    LTC_PAD_PKCS7        = 0x0000U,
145 #ifdef LTC_RNG_GET_BYTES
146    LTC_PAD_ISO_10126    = 0x1000U,
147 #endif
148    LTC_PAD_ANSI_X923    = 0x2000U,
149    LTC_PAD_ONE_AND_ZERO = 0x8000U,
150    LTC_PAD_ZERO         = 0x9000U,
151    LTC_PAD_ZERO_ALWAYS  = 0xA000U,
152 };
153 
154 int padding_pad(unsigned char *data, unsigned long length, unsigned long* padded_length, unsigned long mode);
155 int padding_depad(const unsigned char *data, unsigned long *length, unsigned long mode);
156 #endif  /* LTC_PADDING */
157 
158 #ifdef LTC_SSH
159 typedef enum ssh_data_type_ {
160    LTC_SSHDATA_BYTE,
161    LTC_SSHDATA_BOOLEAN,
162    LTC_SSHDATA_UINT32,
163    LTC_SSHDATA_UINT64,
164    LTC_SSHDATA_STRING,
165    LTC_SSHDATA_MPINT,
166    LTC_SSHDATA_NAMELIST,
167    LTC_SSHDATA_EOL
168 } ssh_data_type;
169 
170 /* VA list handy helpers with tuples of <type, data> */
171 int ssh_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
172 int ssh_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
173 #endif /* LTC_SSH */
174 
175 int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which);
176 
177 /* ref:         $Format:%D$ */
178 /* git commit:  $Format:%H$ */
179 /* commit time: $Format:%ai$ */
180