1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /* ---- LTC_BASE64 Routines ---- */
5 #ifdef LTC_BASE64
6 int base64_encode(const unsigned char *in,  unsigned long inlen,
7                                  char *out, unsigned long *outlen);
8 
9 int base64_decode(const char *in,  unsigned long inlen,
10                         unsigned char *out, unsigned long *outlen);
11 int base64_strict_decode(const char *in,  unsigned long inlen,
12                         unsigned char *out, unsigned long *outlen);
13 int base64_sane_decode(const char *in,  unsigned long inlen,
14                         unsigned char *out, unsigned long *outlen);
15 #endif
16 
17 #ifdef LTC_BASE64_URL
18 int base64url_encode(const unsigned char *in,  unsigned long inlen,
19                                     char *out, unsigned long *outlen);
20 int base64url_strict_encode(const unsigned char *in,  unsigned long inlen,
21                                            char *out, unsigned long *outlen);
22 
23 int base64url_decode(const char *in,  unsigned long inlen,
24                         unsigned char *out, unsigned long *outlen);
25 int base64url_strict_decode(const char *in,  unsigned long inlen,
26                         unsigned char *out, unsigned long *outlen);
27 int base64url_sane_decode(const char *in,  unsigned long inlen,
28                         unsigned char *out, unsigned long *outlen);
29 #endif
30 
31 /* ---- BASE32 Routines ---- */
32 #ifdef LTC_BASE32
33 typedef enum {
34    BASE32_RFC4648   = 0,
35    BASE32_BASE32HEX = 1,
36    BASE32_ZBASE32   = 2,
37    BASE32_CROCKFORD = 3
38 } base32_alphabet;
39 int base32_encode(const unsigned char *in,  unsigned long inlen,
40                                  char *out, unsigned long *outlen,
41                         base32_alphabet id);
42 int base32_decode(const          char *in,  unsigned long inlen,
43                         unsigned char *out, unsigned long *outlen,
44                         base32_alphabet id);
45 #endif
46 
47 /* ---- BASE16 Routines ---- */
48 #ifdef LTC_BASE16
49 int base16_encode(const unsigned char *in,  unsigned long  inlen,
50                                  char *out, unsigned long *outlen,
51                         unsigned int   options);
52 int base16_decode(const          char *in,  unsigned long  inlen,
53                         unsigned char *out, unsigned long *outlen);
54 #endif
55 
56 #ifdef LTC_BCRYPT
57 int bcrypt_pbkdf_openbsd(const          void *secret, unsigned long secret_len,
58                          const unsigned char *salt,   unsigned long salt_len,
59                                unsigned int  rounds,            int hash_idx,
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, ...) LTC_NULL_TERMINATED;
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(crypt_mp_init) void init_LTM(void);
107 #endif
108 #ifdef TFM_DESC
109 LTC_DEPRECATED(crypt_mp_init) void init_TFM(void);
110 #endif
111 #ifdef GMP_DESC
112 LTC_DEPRECATED(crypt_mp_init) 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_SSH          = 0x3000U,
150    /* The following padding modes don't contain the padding
151     * length as last byte of the padding.
152     */
153    LTC_PAD_ONE_AND_ZERO = 0x8000U,
154    LTC_PAD_ZERO         = 0x9000U,
155    LTC_PAD_ZERO_ALWAYS  = 0xA000U,
156 };
157 
158 int padding_pad(unsigned char *data, unsigned long length, unsigned long* padded_length, unsigned long mode);
159 int padding_depad(const unsigned char *data, unsigned long *length, unsigned long mode);
160 #endif  /* LTC_PADDING */
161 
162 #ifdef LTC_SSH
163 typedef enum ssh_data_type_ {
164    LTC_SSHDATA_EOL,
165    LTC_SSHDATA_BYTE,
166    LTC_SSHDATA_BOOLEAN,
167    LTC_SSHDATA_UINT32,
168    LTC_SSHDATA_UINT64,
169    LTC_SSHDATA_STRING,
170    LTC_SSHDATA_MPINT,
171    LTC_SSHDATA_NAMELIST,
172 } ssh_data_type;
173 
174 /* VA list handy helpers with tuples of <type, data> */
175 int ssh_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...) LTC_NULL_TERMINATED;
176 int ssh_decode_sequence_multi(const unsigned char *in, unsigned long *inlen, ...) LTC_NULL_TERMINATED;
177 #endif /* LTC_SSH */
178 
179 int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which);
180