blob: 7e61cb97272c29b9e9e337e0abc70dd9fb371063 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / wrapper functions for crypto libraries
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
8 * This file defines the cryptographic functions that need to be implemented
9 * for wpa_supplicant and hostapd. When TLS is not used, internal
10 * implementation of MD5, SHA1, and AES is used and no external libraries are
11 * required. When TLS is enabled (e.g., by enabling EAP-TLS or EAP-PEAP), the
12 * crypto library used by the TLS implementation is expected to be used for
13 * non-TLS needs, too, in order to save space by not implementing these
14 * functions twice.
15 *
16 * Wrapper code for using each crypto library is in its own file (crypto*.c)
17 * and one of these files is build and linked in to provide the functions
18 * defined here.
19 */
20
21#ifndef CRYPTO_H
22#define CRYPTO_H
23
24/**
25 * md4_vector - MD4 hash for data vector
26 * @num_elem: Number of elements in the data vector
27 * @addr: Pointers to the data areas
28 * @len: Lengths of the data blocks
29 * @mac: Buffer for the hash
30 * Returns: 0 on success, -1 on failure
31 */
32int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
33
34/**
35 * md5_vector - MD5 hash for data vector
36 * @num_elem: Number of elements in the data vector
37 * @addr: Pointers to the data areas
38 * @len: Lengths of the data blocks
39 * @mac: Buffer for the hash
40 * Returns: 0 on success, -1 on failure
41 */
42int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
43
44#ifdef CONFIG_FIPS
45/**
46 * md5_vector_non_fips_allow - MD5 hash for data vector (non-FIPS use allowed)
47 * @num_elem: Number of elements in the data vector
48 * @addr: Pointers to the data areas
49 * @len: Lengths of the data blocks
50 * @mac: Buffer for the hash
51 * Returns: 0 on success, -1 on failure
52 */
53int md5_vector_non_fips_allow(size_t num_elem, const u8 *addr[],
54 const size_t *len, u8 *mac);
55#else /* CONFIG_FIPS */
56#define md5_vector_non_fips_allow md5_vector
57#endif /* CONFIG_FIPS */
58
59
60/**
61 * sha1_vector - SHA-1 hash for data vector
62 * @num_elem: Number of elements in the data vector
63 * @addr: Pointers to the data areas
64 * @len: Lengths of the data blocks
65 * @mac: Buffer for the hash
66 * Returns: 0 on success, -1 on failure
67 */
68int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
69 u8 *mac);
70
71/**
72 * fips186_2-prf - NIST FIPS Publication 186-2 change notice 1 PRF
73 * @seed: Seed/key for the PRF
74 * @seed_len: Seed length in bytes
75 * @x: Buffer for PRF output
76 * @xlen: Output length in bytes
77 * Returns: 0 on success, -1 on failure
78 *
79 * This function implements random number generation specified in NIST FIPS
80 * Publication 186-2 for EAP-SIM. This PRF uses a function that is similar to
81 * SHA-1, but has different message padding.
82 */
83int __must_check fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x,
84 size_t xlen);
85
86/**
87 * sha256_vector - SHA256 hash for data vector
88 * @num_elem: Number of elements in the data vector
89 * @addr: Pointers to the data areas
90 * @len: Lengths of the data blocks
91 * @mac: Buffer for the hash
92 * Returns: 0 on success, -1 on failure
93 */
94int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
95 u8 *mac);
96
97/**
98 * des_encrypt - Encrypt one block with DES
99 * @clear: 8 octets (in)
100 * @key: 7 octets (in) (no parity bits included)
101 * @cypher: 8 octets (out)
102 */
103void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
104
105/**
106 * aes_encrypt_init - Initialize AES for encryption
107 * @key: Encryption key
108 * @len: Key length in bytes (usually 16, i.e., 128 bits)
109 * Returns: Pointer to context data or %NULL on failure
110 */
111void * aes_encrypt_init(const u8 *key, size_t len);
112
113/**
114 * aes_encrypt - Encrypt one AES block
115 * @ctx: Context pointer from aes_encrypt_init()
116 * @plain: Plaintext data to be encrypted (16 bytes)
117 * @crypt: Buffer for the encrypted data (16 bytes)
118 */
119void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
120
121/**
122 * aes_encrypt_deinit - Deinitialize AES encryption
123 * @ctx: Context pointer from aes_encrypt_init()
124 */
125void aes_encrypt_deinit(void *ctx);
126
127/**
128 * aes_decrypt_init - Initialize AES for decryption
129 * @key: Decryption key
130 * @len: Key length in bytes (usually 16, i.e., 128 bits)
131 * Returns: Pointer to context data or %NULL on failure
132 */
133void * aes_decrypt_init(const u8 *key, size_t len);
134
135/**
136 * aes_decrypt - Decrypt one AES block
137 * @ctx: Context pointer from aes_encrypt_init()
138 * @crypt: Encrypted data (16 bytes)
139 * @plain: Buffer for the decrypted data (16 bytes)
140 */
141void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
142
143/**
144 * aes_decrypt_deinit - Deinitialize AES decryption
145 * @ctx: Context pointer from aes_encrypt_init()
146 */
147void aes_decrypt_deinit(void *ctx);
148
149
150enum crypto_hash_alg {
151 CRYPTO_HASH_ALG_MD5, CRYPTO_HASH_ALG_SHA1,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800152 CRYPTO_HASH_ALG_HMAC_MD5, CRYPTO_HASH_ALG_HMAC_SHA1,
153 CRYPTO_HASH_ALG_SHA256, CRYPTO_HASH_ALG_HMAC_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154};
155
156struct crypto_hash;
157
158/**
159 * crypto_hash_init - Initialize hash/HMAC function
160 * @alg: Hash algorithm
161 * @key: Key for keyed hash (e.g., HMAC) or %NULL if not needed
162 * @key_len: Length of the key in bytes
163 * Returns: Pointer to hash context to use with other hash functions or %NULL
164 * on failure
165 *
166 * This function is only used with internal TLSv1 implementation
167 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
168 * to implement this.
169 */
170struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
171 size_t key_len);
172
173/**
174 * crypto_hash_update - Add data to hash calculation
175 * @ctx: Context pointer from crypto_hash_init()
176 * @data: Data buffer to add
177 * @len: Length of the buffer
178 *
179 * This function is only used with internal TLSv1 implementation
180 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
181 * to implement this.
182 */
183void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len);
184
185/**
186 * crypto_hash_finish - Complete hash calculation
187 * @ctx: Context pointer from crypto_hash_init()
188 * @hash: Buffer for hash value or %NULL if caller is just freeing the hash
189 * context
190 * @len: Pointer to length of the buffer or %NULL if caller is just freeing the
191 * hash context; on return, this is set to the actual length of the hash value
192 * Returns: 0 on success, -1 if buffer is too small (len set to needed length),
193 * or -2 on other failures (including failed crypto_hash_update() operations)
194 *
195 * This function calculates the hash value and frees the context buffer that
196 * was used for hash calculation.
197 *
198 * This function is only used with internal TLSv1 implementation
199 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
200 * to implement this.
201 */
202int crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len);
203
204
205enum crypto_cipher_alg {
206 CRYPTO_CIPHER_NULL = 0, CRYPTO_CIPHER_ALG_AES, CRYPTO_CIPHER_ALG_3DES,
207 CRYPTO_CIPHER_ALG_DES, CRYPTO_CIPHER_ALG_RC2, CRYPTO_CIPHER_ALG_RC4
208};
209
210struct crypto_cipher;
211
212/**
213 * crypto_cipher_init - Initialize block/stream cipher function
214 * @alg: Cipher algorithm
215 * @iv: Initialization vector for block ciphers or %NULL for stream ciphers
216 * @key: Cipher key
217 * @key_len: Length of key in bytes
218 * Returns: Pointer to cipher context to use with other cipher functions or
219 * %NULL on failure
220 *
221 * This function is only used with internal TLSv1 implementation
222 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
223 * to implement this.
224 */
225struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
226 const u8 *iv, const u8 *key,
227 size_t key_len);
228
229/**
230 * crypto_cipher_encrypt - Cipher encrypt
231 * @ctx: Context pointer from crypto_cipher_init()
232 * @plain: Plaintext to cipher
233 * @crypt: Resulting ciphertext
234 * @len: Length of the plaintext
235 * Returns: 0 on success, -1 on failure
236 *
237 * This function is only used with internal TLSv1 implementation
238 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
239 * to implement this.
240 */
241int __must_check crypto_cipher_encrypt(struct crypto_cipher *ctx,
242 const u8 *plain, u8 *crypt, size_t len);
243
244/**
245 * crypto_cipher_decrypt - Cipher decrypt
246 * @ctx: Context pointer from crypto_cipher_init()
247 * @crypt: Ciphertext to decrypt
248 * @plain: Resulting plaintext
249 * @len: Length of the cipher text
250 * Returns: 0 on success, -1 on failure
251 *
252 * This function is only used with internal TLSv1 implementation
253 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
254 * to implement this.
255 */
256int __must_check crypto_cipher_decrypt(struct crypto_cipher *ctx,
257 const u8 *crypt, u8 *plain, size_t len);
258
259/**
260 * crypto_cipher_decrypt - Free cipher context
261 * @ctx: Context pointer from crypto_cipher_init()
262 *
263 * This function is only used with internal TLSv1 implementation
264 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
265 * to implement this.
266 */
267void crypto_cipher_deinit(struct crypto_cipher *ctx);
268
269
270struct crypto_public_key;
271struct crypto_private_key;
272
273/**
274 * crypto_public_key_import - Import an RSA public key
275 * @key: Key buffer (DER encoded RSA public key)
276 * @len: Key buffer length in bytes
277 * Returns: Pointer to the public key or %NULL on failure
278 *
279 * This function can just return %NULL if the crypto library supports X.509
280 * parsing. In that case, crypto_public_key_from_cert() is used to import the
281 * public key from a certificate.
282 *
283 * This function is only used with internal TLSv1 implementation
284 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
285 * to implement this.
286 */
287struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len);
288
289/**
290 * crypto_private_key_import - Import an RSA private key
291 * @key: Key buffer (DER encoded RSA private key)
292 * @len: Key buffer length in bytes
293 * @passwd: Key encryption password or %NULL if key is not encrypted
294 * Returns: Pointer to the private key or %NULL on failure
295 *
296 * This function is only used with internal TLSv1 implementation
297 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
298 * to implement this.
299 */
300struct crypto_private_key * crypto_private_key_import(const u8 *key,
301 size_t len,
302 const char *passwd);
303
304/**
305 * crypto_public_key_from_cert - Import an RSA public key from a certificate
306 * @buf: DER encoded X.509 certificate
307 * @len: Certificate buffer length in bytes
308 * Returns: Pointer to public key or %NULL on failure
309 *
310 * This function can just return %NULL if the crypto library does not support
311 * X.509 parsing. In that case, internal code will be used to parse the
312 * certificate and public key is imported using crypto_public_key_import().
313 *
314 * This function is only used with internal TLSv1 implementation
315 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
316 * to implement this.
317 */
318struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
319 size_t len);
320
321/**
322 * crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5)
323 * @key: Public key
324 * @in: Plaintext buffer
325 * @inlen: Length of plaintext buffer in bytes
326 * @out: Output buffer for encrypted data
327 * @outlen: Length of output buffer in bytes; set to used length on success
328 * Returns: 0 on success, -1 on failure
329 *
330 * This function is only used with internal TLSv1 implementation
331 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
332 * to implement this.
333 */
334int __must_check crypto_public_key_encrypt_pkcs1_v15(
335 struct crypto_public_key *key, const u8 *in, size_t inlen,
336 u8 *out, size_t *outlen);
337
338/**
339 * crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5)
340 * @key: Private key
341 * @in: Encrypted buffer
342 * @inlen: Length of encrypted buffer in bytes
343 * @out: Output buffer for encrypted data
344 * @outlen: Length of output buffer in bytes; set to used length on success
345 * Returns: 0 on success, -1 on failure
346 *
347 * This function is only used with internal TLSv1 implementation
348 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
349 * to implement this.
350 */
351int __must_check crypto_private_key_decrypt_pkcs1_v15(
352 struct crypto_private_key *key, const u8 *in, size_t inlen,
353 u8 *out, size_t *outlen);
354
355/**
356 * crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1)
357 * @key: Private key from crypto_private_key_import()
358 * @in: Plaintext buffer
359 * @inlen: Length of plaintext buffer in bytes
360 * @out: Output buffer for encrypted (signed) data
361 * @outlen: Length of output buffer in bytes; set to used length on success
362 * Returns: 0 on success, -1 on failure
363 *
364 * This function is only used with internal TLSv1 implementation
365 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
366 * to implement this.
367 */
368int __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
369 const u8 *in, size_t inlen,
370 u8 *out, size_t *outlen);
371
372/**
373 * crypto_public_key_free - Free public key
374 * @key: Public key
375 *
376 * This function is only used with internal TLSv1 implementation
377 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
378 * to implement this.
379 */
380void crypto_public_key_free(struct crypto_public_key *key);
381
382/**
383 * crypto_private_key_free - Free private key
384 * @key: Private key from crypto_private_key_import()
385 *
386 * This function is only used with internal TLSv1 implementation
387 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
388 * to implement this.
389 */
390void crypto_private_key_free(struct crypto_private_key *key);
391
392/**
393 * crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature
394 * @key: Public key
395 * @crypt: Encrypted signature data (using the private key)
396 * @crypt_len: Encrypted signature data length
397 * @plain: Buffer for plaintext (at least crypt_len bytes)
398 * @plain_len: Plaintext length (max buffer size on input, real len on output);
399 * Returns: 0 on success, -1 on failure
400 */
401int __must_check crypto_public_key_decrypt_pkcs1(
402 struct crypto_public_key *key, const u8 *crypt, size_t crypt_len,
403 u8 *plain, size_t *plain_len);
404
405/**
406 * crypto_global_init - Initialize crypto wrapper
407 *
408 * This function is only used with internal TLSv1 implementation
409 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
410 * to implement this.
411 */
412int __must_check crypto_global_init(void);
413
414/**
415 * crypto_global_deinit - Deinitialize crypto wrapper
416 *
417 * This function is only used with internal TLSv1 implementation
418 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
419 * to implement this.
420 */
421void crypto_global_deinit(void);
422
423/**
424 * crypto_mod_exp - Modular exponentiation of large integers
425 * @base: Base integer (big endian byte array)
426 * @base_len: Length of base integer in bytes
427 * @power: Power integer (big endian byte array)
428 * @power_len: Length of power integer in bytes
429 * @modulus: Modulus integer (big endian byte array)
430 * @modulus_len: Length of modulus integer in bytes
431 * @result: Buffer for the result
432 * @result_len: Result length (max buffer size on input, real len on output)
433 * Returns: 0 on success, -1 on failure
434 *
435 * This function calculates result = base ^ power mod modulus. modules_len is
436 * used as the maximum size of modulus buffer. It is set to the used size on
437 * success.
438 *
439 * This function is only used with internal TLSv1 implementation
440 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
441 * to implement this.
442 */
443int __must_check crypto_mod_exp(const u8 *base, size_t base_len,
444 const u8 *power, size_t power_len,
445 const u8 *modulus, size_t modulus_len,
446 u8 *result, size_t *result_len);
447
448/**
449 * rc4_skip - XOR RC4 stream to given data with skip-stream-start
450 * @key: RC4 key
451 * @keylen: RC4 key length
452 * @skip: number of bytes to skip from the beginning of the RC4 stream
453 * @data: data to be XOR'ed with RC4 stream
454 * @data_len: buf length
455 * Returns: 0 on success, -1 on failure
456 *
457 * Generate RC4 pseudo random stream for the given key, skip beginning of the
458 * stream, and XOR the end result with the data buffer to perform RC4
459 * encryption/decryption.
460 */
461int rc4_skip(const u8 *key, size_t keylen, size_t skip,
462 u8 *data, size_t data_len);
463
464#endif /* CRYPTO_H */