FreeRDP
custom-crypto.h
1 
20 #ifndef WINPR_CUSTOM_CRYPTO_H
21 #define WINPR_CUSTOM_CRYPTO_H
22 
23 #include <winpr/winpr.h>
24 #include <winpr/wtypes.h>
25 
26 #include <winpr/error.h>
27 
32 #define WINPR_MD4_DIGEST_LENGTH 16
33 #define WINPR_MD5_DIGEST_LENGTH 16
34 #define WINPR_SHA1_DIGEST_LENGTH 20
35 #define WINPR_SHA224_DIGEST_LENGTH 28
36 #define WINPR_SHA256_DIGEST_LENGTH 32
37 #define WINPR_SHA384_DIGEST_LENGTH 48
38 #define WINPR_SHA512_DIGEST_LENGTH 64
39 #define WINPR_RIPEMD160_DIGEST_LENGTH 20
40 #define WINPR_SHA3_224_DIGEST_LENGTH 28
41 #define WINPR_SHA3_256_DIGEST_LENGTH 32
42 #define WINPR_SHA3_384_DIGEST_LENGTH 48
43 #define WINPR_SHA3_512_DIGEST_LENGTH 64
44 #define WINPR_SHAKE128_DIGEST_LENGTH 16
45 #define WINPR_SHAKE256_DIGEST_LENGTH 32
46 
50 typedef enum
51 {
52  WINPR_MD_NONE = 0,
53  WINPR_MD_MD2 = 1,
54  WINPR_MD_MD4 = 2,
55  WINPR_MD_MD5 = 3,
56  WINPR_MD_SHA1 = 4,
57  WINPR_MD_SHA224 = 5,
58  WINPR_MD_SHA256 = 6,
59  WINPR_MD_SHA384 = 7,
60  WINPR_MD_SHA512 = 8,
61  WINPR_MD_RIPEMD160 = 9,
62  WINPR_MD_SHA3_224 = 10,
63  WINPR_MD_SHA3_256 = 11,
64  WINPR_MD_SHA3_384 = 12,
65  WINPR_MD_SHA3_512 = 13,
66  WINPR_MD_SHAKE128 = 14,
67  WINPR_MD_SHAKE256 = 15
68 } WINPR_MD_TYPE;
69 
70 typedef struct winpr_hmac_ctx_private_st WINPR_HMAC_CTX;
71 
72 #ifdef __cplusplus
73 extern "C"
74 {
75 #endif
76 
77  WINPR_API WINPR_MD_TYPE winpr_md_type_from_string(const char* name);
78  WINPR_API const char* winpr_md_type_to_string(WINPR_MD_TYPE md);
79 
80  WINPR_API void winpr_HMAC_Free(WINPR_HMAC_CTX* ctx);
81 
82  WINPR_ATTR_MALLOC(winpr_HMAC_Free, 1)
83  WINPR_API WINPR_HMAC_CTX* winpr_HMAC_New(void);
84  WINPR_API BOOL winpr_HMAC_Init(WINPR_HMAC_CTX* ctx, WINPR_MD_TYPE md, const void* key,
85  size_t keylen);
86  WINPR_API BOOL winpr_HMAC_Update(WINPR_HMAC_CTX* ctx, const void* input, size_t ilen);
87  WINPR_API BOOL winpr_HMAC_Final(WINPR_HMAC_CTX* ctx, void* output, size_t ilen);
88 
89  WINPR_API BOOL winpr_HMAC(WINPR_MD_TYPE md, const void* key, size_t keylen, const void* input,
90  size_t ilen, void* output, size_t olen);
91 
92 #ifdef __cplusplus
93 }
94 #endif
95 
100 typedef struct winpr_digest_ctx_private_st WINPR_DIGEST_CTX;
101 
102 #ifdef __cplusplus
103 extern "C"
104 {
105 #endif
106 
107  WINPR_API void winpr_Digest_Free(WINPR_DIGEST_CTX* ctx);
108 
109  WINPR_ATTR_MALLOC(winpr_Digest_Free, 1)
110  WINPR_API WINPR_DIGEST_CTX* winpr_Digest_New(void);
111  WINPR_API BOOL winpr_Digest_Init_Allow_FIPS(WINPR_DIGEST_CTX* ctx, WINPR_MD_TYPE md);
112  WINPR_API BOOL winpr_Digest_Init(WINPR_DIGEST_CTX* ctx, WINPR_MD_TYPE md);
113  WINPR_API BOOL winpr_Digest_Update(WINPR_DIGEST_CTX* ctx, const void* input, size_t ilen);
114  WINPR_API BOOL winpr_Digest_Final(WINPR_DIGEST_CTX* ctx, void* output, size_t ilen);
115 
116  WINPR_API BOOL winpr_Digest_Allow_FIPS(WINPR_MD_TYPE md, const void* input, size_t ilen,
117  void* output, size_t olen);
118  WINPR_API BOOL winpr_Digest(WINPR_MD_TYPE md, const void* input, size_t ilen, void* output,
119  size_t olen);
120 
121  WINPR_API BOOL winpr_DigestSign_Init(WINPR_DIGEST_CTX* ctx, WINPR_MD_TYPE md, void* key);
122  WINPR_API BOOL winpr_DigestSign_Update(WINPR_DIGEST_CTX* ctx, const void* input, size_t ilen);
123  WINPR_API BOOL winpr_DigestSign_Final(WINPR_DIGEST_CTX* ctx, void* output, size_t* piolen);
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
133 #ifdef __cplusplus
134 extern "C"
135 {
136 #endif
137 
138  WINPR_API int winpr_RAND(void* output, size_t len);
139  WINPR_API int winpr_RAND_pseudo(void* output, size_t len);
140 
141 #ifdef __cplusplus
142 }
143 #endif
144 
149 typedef struct winpr_rc4_ctx_private_st WINPR_RC4_CTX;
150 
151 #ifdef __cplusplus
152 extern "C"
153 {
154 #endif
155 
156  WINPR_API void winpr_RC4_Free(WINPR_RC4_CTX* ctx);
157 
158  WINPR_ATTR_MALLOC(winpr_RC4_Free, 1)
159  WINPR_API WINPR_RC4_CTX* winpr_RC4_New_Allow_FIPS(const void* key, size_t keylen);
160 
161  WINPR_ATTR_MALLOC(winpr_RC4_Free, 1)
162  WINPR_API WINPR_RC4_CTX* winpr_RC4_New(const void* key, size_t keylen);
163  WINPR_API BOOL winpr_RC4_Update(WINPR_RC4_CTX* ctx, size_t length, const void* input,
164  void* output);
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
174 #define WINPR_AES_BLOCK_SIZE 16
175 
176 /* cipher operation types */
177 #define WINPR_CIPHER_MAX_IV_LENGTH 16u
178 #define WINPR_CIPHER_MAX_KEY_LENGTH 64u
179 
180 typedef enum
181 {
182  WINPR_ENCRYPT = 0,
183  WINPR_DECRYPT = 1
184 } WINPR_CRYPTO_OPERATION;
185 
186 /* cipher types */
187 typedef enum
188 {
189  WINPR_CIPHER_NONE = 0,
190  WINPR_CIPHER_NULL = 1,
191  WINPR_CIPHER_AES_128_ECB = 2,
192  WINPR_CIPHER_AES_192_ECB = 3,
193  WINPR_CIPHER_AES_256_ECB = 4,
194  WINPR_CIPHER_AES_128_CBC = 5,
195  WINPR_CIPHER_AES_192_CBC = 6,
196  WINPR_CIPHER_AES_256_CBC = 7,
197  WINPR_CIPHER_AES_128_CFB128 = 8,
198  WINPR_CIPHER_AES_192_CFB128 = 9,
199  WINPR_CIPHER_AES_256_CFB128 = 10,
200  WINPR_CIPHER_AES_128_CTR = 11,
201  WINPR_CIPHER_AES_192_CTR = 12,
202  WINPR_CIPHER_AES_256_CTR = 13,
203  WINPR_CIPHER_AES_128_GCM = 14,
204  WINPR_CIPHER_AES_192_GCM = 15,
205  WINPR_CIPHER_AES_256_GCM = 16,
206  WINPR_CIPHER_CAMELLIA_128_ECB = 17,
207  WINPR_CIPHER_CAMELLIA_192_ECB = 18,
208  WINPR_CIPHER_CAMELLIA_256_ECB = 19,
209  WINPR_CIPHER_CAMELLIA_128_CBC = 20,
210  WINPR_CIPHER_CAMELLIA_192_CBC = 21,
211  WINPR_CIPHER_CAMELLIA_256_CBC = 22,
212  WINPR_CIPHER_CAMELLIA_128_CFB128 = 23,
213  WINPR_CIPHER_CAMELLIA_192_CFB128 = 24,
214  WINPR_CIPHER_CAMELLIA_256_CFB128 = 25,
215  WINPR_CIPHER_CAMELLIA_128_CTR = 26,
216  WINPR_CIPHER_CAMELLIA_192_CTR = 27,
217  WINPR_CIPHER_CAMELLIA_256_CTR = 28,
218  WINPR_CIPHER_CAMELLIA_128_GCM = 29,
219  WINPR_CIPHER_CAMELLIA_192_GCM = 30,
220  WINPR_CIPHER_CAMELLIA_256_GCM = 31,
221  WINPR_CIPHER_DES_ECB = 32,
222  WINPR_CIPHER_DES_CBC = 33,
223  WINPR_CIPHER_DES_EDE_ECB = 34,
224  WINPR_CIPHER_DES_EDE_CBC = 35,
225  WINPR_CIPHER_DES_EDE3_ECB = 36,
226  WINPR_CIPHER_DES_EDE3_CBC = 37,
227  WINPR_CIPHER_BLOWFISH_ECB = 38,
228  WINPR_CIPHER_BLOWFISH_CBC = 39,
229  WINPR_CIPHER_BLOWFISH_CFB64 = 40,
230  WINPR_CIPHER_BLOWFISH_CTR = 41,
231  WINPR_CIPHER_ARC4_128 = 42,
232  WINPR_CIPHER_AES_128_CCM = 43,
233  WINPR_CIPHER_AES_192_CCM = 44,
234  WINPR_CIPHER_AES_256_CCM = 45,
235  WINPR_CIPHER_CAMELLIA_128_CCM = 46,
236  WINPR_CIPHER_CAMELLIA_192_CCM = 47,
237  WINPR_CIPHER_CAMELLIA_256_CCM = 48,
238 } WINPR_CIPHER_TYPE;
239 
240 typedef struct winpr_cipher_ctx_private_st WINPR_CIPHER_CTX;
241 
242 #ifdef __cplusplus
243 extern "C"
244 {
245 #endif
246 
254  WINPR_API WINPR_CIPHER_TYPE winpr_cipher_type_from_string(const char* name);
255 
263  WINPR_API const char* winpr_cipher_type_to_string(WINPR_CIPHER_TYPE md);
264 
265  WINPR_API void winpr_Cipher_Free(WINPR_CIPHER_CTX* ctx);
266 
267  WINPR_ATTR_MALLOC(winpr_Cipher_Free, 1)
268  WINPR_API WINPR_DEPRECATED_VAR("[since 3.10.0] use winpr_Cipher_NewEx",
269  WINPR_CIPHER_CTX* winpr_Cipher_New(WINPR_CIPHER_TYPE cipher,
270  WINPR_CRYPTO_OPERATION op,
271  const void* key,
272  const void* iv));
273 
290  WINPR_ATTR_MALLOC(winpr_Cipher_Free, 1)
291  WINPR_API WINPR_CIPHER_CTX* winpr_Cipher_NewEx(WINPR_CIPHER_TYPE cipher,
292  WINPR_CRYPTO_OPERATION op, const void* key,
293  size_t keylen, const void* iv, size_t ivlen);
294  WINPR_API BOOL winpr_Cipher_SetPadding(WINPR_CIPHER_CTX* ctx, BOOL enabled);
295  WINPR_API BOOL winpr_Cipher_Update(WINPR_CIPHER_CTX* ctx, const void* input, size_t ilen,
296  void* output, size_t* olen);
297  WINPR_API BOOL winpr_Cipher_Final(WINPR_CIPHER_CTX* ctx, void* output, size_t* olen);
298 
299 #ifdef __cplusplus
300 }
301 #endif
302 
307 #ifdef __cplusplus
308 extern "C"
309 {
310 #endif
311 
312  WINPR_API int winpr_Cipher_BytesToKey(int cipher, WINPR_MD_TYPE md, const void* salt,
313  const void* data, size_t datal, size_t count, void* key,
314  void* iv);
315 
316 #ifdef __cplusplus
317 }
318 #endif
319 
320 #endif /* WINPR_CUSTOM_CRYPTO_H */