crypto.h revision 189251
1189251Ssam/*
2189251Ssam * WPA Supplicant / wrapper functions for crypto libraries
3189251Ssam * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
4189251Ssam *
5189251Ssam * This program is free software; you can redistribute it and/or modify
6189251Ssam * it under the terms of the GNU General Public License version 2 as
7189251Ssam * published by the Free Software Foundation.
8189251Ssam *
9189251Ssam * Alternatively, this software may be distributed under the terms of BSD
10189251Ssam * license.
11189251Ssam *
12189251Ssam * See README and COPYING for more details.
13189251Ssam *
14189251Ssam * This file defines the cryptographic functions that need to be implemented
15189251Ssam * for wpa_supplicant and hostapd. When TLS is not used, internal
16189251Ssam * implementation of MD5, SHA1, and AES is used and no external libraries are
17189251Ssam * required. When TLS is enabled (e.g., by enabling EAP-TLS or EAP-PEAP), the
18189251Ssam * crypto library used by the TLS implementation is expected to be used for
19189251Ssam * non-TLS needs, too, in order to save space by not implementing these
20189251Ssam * functions twice.
21189251Ssam *
22189251Ssam * Wrapper code for using each crypto library is in its own file (crypto*.c)
23189251Ssam * and one of these files is build and linked in to provide the functions
24189251Ssam * defined here.
25189251Ssam */
26189251Ssam
27189251Ssam#ifndef CRYPTO_H
28189251Ssam#define CRYPTO_H
29189251Ssam
30189251Ssam/**
31189251Ssam * md4_vector - MD4 hash for data vector
32189251Ssam * @num_elem: Number of elements in the data vector
33189251Ssam * @addr: Pointers to the data areas
34189251Ssam * @len: Lengths of the data blocks
35189251Ssam * @mac: Buffer for the hash
36189251Ssam */
37189251Ssamvoid md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
38189251Ssam
39189251Ssam/**
40189251Ssam * md5_vector - MD5 hash for data vector
41189251Ssam * @num_elem: Number of elements in the data vector
42189251Ssam * @addr: Pointers to the data areas
43189251Ssam * @len: Lengths of the data blocks
44189251Ssam * @mac: Buffer for the hash
45189251Ssam */
46189251Ssamvoid md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
47189251Ssam
48189251Ssam/**
49189251Ssam * sha1_vector - SHA-1 hash for data vector
50189251Ssam * @num_elem: Number of elements in the data vector
51189251Ssam * @addr: Pointers to the data areas
52189251Ssam * @len: Lengths of the data blocks
53189251Ssam * @mac: Buffer for the hash
54189251Ssam */
55189251Ssamvoid sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
56189251Ssam		 u8 *mac);
57189251Ssam
58189251Ssam/**
59189251Ssam * fips186_2-prf - NIST FIPS Publication 186-2 change notice 1 PRF
60189251Ssam * @seed: Seed/key for the PRF
61189251Ssam * @seed_len: Seed length in bytes
62189251Ssam * @x: Buffer for PRF output
63189251Ssam * @xlen: Output length in bytes
64189251Ssam * Returns: 0 on success, -1 on failure
65189251Ssam *
66189251Ssam * This function implements random number generation specified in NIST FIPS
67189251Ssam * Publication 186-2 for EAP-SIM. This PRF uses a function that is similar to
68189251Ssam * SHA-1, but has different message padding.
69189251Ssam */
70189251Ssamint __must_check fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x,
71189251Ssam			       size_t xlen);
72189251Ssam
73189251Ssam/**
74189251Ssam * sha256_vector - SHA256 hash for data vector
75189251Ssam * @num_elem: Number of elements in the data vector
76189251Ssam * @addr: Pointers to the data areas
77189251Ssam * @len: Lengths of the data blocks
78189251Ssam * @mac: Buffer for the hash
79189251Ssam */
80189251Ssamvoid sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
81189251Ssam		   u8 *mac);
82189251Ssam
83189251Ssam/**
84189251Ssam * des_encrypt - Encrypt one block with DES
85189251Ssam * @clear: 8 octets (in)
86189251Ssam * @key: 7 octets (in) (no parity bits included)
87189251Ssam * @cypher: 8 octets (out)
88189251Ssam */
89189251Ssamvoid des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
90189251Ssam
91189251Ssam/**
92189251Ssam * aes_encrypt_init - Initialize AES for encryption
93189251Ssam * @key: Encryption key
94189251Ssam * @len: Key length in bytes (usually 16, i.e., 128 bits)
95189251Ssam * Returns: Pointer to context data or %NULL on failure
96189251Ssam */
97189251Ssamvoid * aes_encrypt_init(const u8 *key, size_t len);
98189251Ssam
99189251Ssam/**
100189251Ssam * aes_encrypt - Encrypt one AES block
101189251Ssam * @ctx: Context pointer from aes_encrypt_init()
102189251Ssam * @plain: Plaintext data to be encrypted (16 bytes)
103189251Ssam * @crypt: Buffer for the encrypted data (16 bytes)
104189251Ssam */
105189251Ssamvoid aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
106189251Ssam
107189251Ssam/**
108189251Ssam * aes_encrypt_deinit - Deinitialize AES encryption
109189251Ssam * @ctx: Context pointer from aes_encrypt_init()
110189251Ssam */
111189251Ssamvoid aes_encrypt_deinit(void *ctx);
112189251Ssam
113189251Ssam/**
114189251Ssam * aes_decrypt_init - Initialize AES for decryption
115189251Ssam * @key: Decryption key
116189251Ssam * @len: Key length in bytes (usually 16, i.e., 128 bits)
117189251Ssam * Returns: Pointer to context data or %NULL on failure
118189251Ssam */
119189251Ssamvoid * aes_decrypt_init(const u8 *key, size_t len);
120189251Ssam
121189251Ssam/**
122189251Ssam * aes_decrypt - Decrypt one AES block
123189251Ssam * @ctx: Context pointer from aes_encrypt_init()
124189251Ssam * @crypt: Encrypted data (16 bytes)
125189251Ssam * @plain: Buffer for the decrypted data (16 bytes)
126189251Ssam */
127189251Ssamvoid aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
128189251Ssam
129189251Ssam/**
130189251Ssam * aes_decrypt_deinit - Deinitialize AES decryption
131189251Ssam * @ctx: Context pointer from aes_encrypt_init()
132189251Ssam */
133189251Ssamvoid aes_decrypt_deinit(void *ctx);
134189251Ssam
135189251Ssam
136189251Ssamenum crypto_hash_alg {
137189251Ssam	CRYPTO_HASH_ALG_MD5, CRYPTO_HASH_ALG_SHA1,
138189251Ssam	CRYPTO_HASH_ALG_HMAC_MD5, CRYPTO_HASH_ALG_HMAC_SHA1
139189251Ssam};
140189251Ssam
141189251Ssamstruct crypto_hash;
142189251Ssam
143189251Ssam/**
144189251Ssam * crypto_hash_init - Initialize hash/HMAC function
145189251Ssam * @alg: Hash algorithm
146189251Ssam * @key: Key for keyed hash (e.g., HMAC) or %NULL if not needed
147189251Ssam * @key_len: Length of the key in bytes
148189251Ssam * Returns: Pointer to hash context to use with other hash functions or %NULL
149189251Ssam * on failure
150189251Ssam *
151189251Ssam * This function is only used with internal TLSv1 implementation
152189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
153189251Ssam * to implement this.
154189251Ssam */
155189251Ssamstruct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
156189251Ssam				      size_t key_len);
157189251Ssam
158189251Ssam/**
159189251Ssam * crypto_hash_update - Add data to hash calculation
160189251Ssam * @ctx: Context pointer from crypto_hash_init()
161189251Ssam * @data: Data buffer to add
162189251Ssam * @len: Length of the buffer
163189251Ssam *
164189251Ssam * This function is only used with internal TLSv1 implementation
165189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
166189251Ssam * to implement this.
167189251Ssam */
168189251Ssamvoid crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len);
169189251Ssam
170189251Ssam/**
171189251Ssam * crypto_hash_finish - Complete hash calculation
172189251Ssam * @ctx: Context pointer from crypto_hash_init()
173189251Ssam * @hash: Buffer for hash value or %NULL if caller is just freeing the hash
174189251Ssam * context
175189251Ssam * @len: Pointer to length of the buffer or %NULL if caller is just freeing the
176189251Ssam * hash context; on return, this is set to the actual length of the hash value
177189251Ssam * Returns: 0 on success, -1 if buffer is too small (len set to needed length),
178189251Ssam * or -2 on other failures (including failed crypto_hash_update() operations)
179189251Ssam *
180189251Ssam * This function calculates the hash value and frees the context buffer that
181189251Ssam * was used for hash calculation.
182189251Ssam *
183189251Ssam * This function is only used with internal TLSv1 implementation
184189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
185189251Ssam * to implement this.
186189251Ssam */
187189251Ssamint crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len);
188189251Ssam
189189251Ssam
190189251Ssamenum crypto_cipher_alg {
191189251Ssam	CRYPTO_CIPHER_NULL = 0, CRYPTO_CIPHER_ALG_AES, CRYPTO_CIPHER_ALG_3DES,
192189251Ssam	CRYPTO_CIPHER_ALG_DES, CRYPTO_CIPHER_ALG_RC2, CRYPTO_CIPHER_ALG_RC4
193189251Ssam};
194189251Ssam
195189251Ssamstruct crypto_cipher;
196189251Ssam
197189251Ssam/**
198189251Ssam * crypto_cipher_init - Initialize block/stream cipher function
199189251Ssam * @alg: Cipher algorithm
200189251Ssam * @iv: Initialization vector for block ciphers or %NULL for stream ciphers
201189251Ssam * @key: Cipher key
202189251Ssam * @key_len: Length of key in bytes
203189251Ssam * Returns: Pointer to cipher context to use with other cipher functions or
204189251Ssam * %NULL on failure
205189251Ssam *
206189251Ssam * This function is only used with internal TLSv1 implementation
207189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
208189251Ssam * to implement this.
209189251Ssam */
210189251Ssamstruct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
211189251Ssam					  const u8 *iv, const u8 *key,
212189251Ssam					  size_t key_len);
213189251Ssam
214189251Ssam/**
215189251Ssam * crypto_cipher_encrypt - Cipher encrypt
216189251Ssam * @ctx: Context pointer from crypto_cipher_init()
217189251Ssam * @plain: Plaintext to cipher
218189251Ssam * @crypt: Resulting ciphertext
219189251Ssam * @len: Length of the plaintext
220189251Ssam * Returns: 0 on success, -1 on failure
221189251Ssam *
222189251Ssam * This function is only used with internal TLSv1 implementation
223189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
224189251Ssam * to implement this.
225189251Ssam */
226189251Ssamint __must_check crypto_cipher_encrypt(struct crypto_cipher *ctx,
227189251Ssam				       const u8 *plain, u8 *crypt, size_t len);
228189251Ssam
229189251Ssam/**
230189251Ssam * crypto_cipher_decrypt - Cipher decrypt
231189251Ssam * @ctx: Context pointer from crypto_cipher_init()
232189251Ssam * @crypt: Ciphertext to decrypt
233189251Ssam * @plain: Resulting plaintext
234189251Ssam * @len: Length of the cipher text
235189251Ssam * Returns: 0 on success, -1 on failure
236189251Ssam *
237189251Ssam * This function is only used with internal TLSv1 implementation
238189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
239189251Ssam * to implement this.
240189251Ssam */
241189251Ssamint __must_check crypto_cipher_decrypt(struct crypto_cipher *ctx,
242189251Ssam				       const u8 *crypt, u8 *plain, size_t len);
243189251Ssam
244189251Ssam/**
245189251Ssam * crypto_cipher_decrypt - Free cipher context
246189251Ssam * @ctx: Context pointer from crypto_cipher_init()
247189251Ssam *
248189251Ssam * This function is only used with internal TLSv1 implementation
249189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
250189251Ssam * to implement this.
251189251Ssam */
252189251Ssamvoid crypto_cipher_deinit(struct crypto_cipher *ctx);
253189251Ssam
254189251Ssam
255189251Ssamstruct crypto_public_key;
256189251Ssamstruct crypto_private_key;
257189251Ssam
258189251Ssam/**
259189251Ssam * crypto_public_key_import - Import an RSA public key
260189251Ssam * @key: Key buffer (DER encoded RSA public key)
261189251Ssam * @len: Key buffer length in bytes
262189251Ssam * Returns: Pointer to the public key or %NULL on failure
263189251Ssam *
264189251Ssam * This function can just return %NULL if the crypto library supports X.509
265189251Ssam * parsing. In that case, crypto_public_key_from_cert() is used to import the
266189251Ssam * public key from a certificate.
267189251Ssam *
268189251Ssam * This function is only used with internal TLSv1 implementation
269189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
270189251Ssam * to implement this.
271189251Ssam */
272189251Ssamstruct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len);
273189251Ssam
274189251Ssam/**
275189251Ssam * crypto_private_key_import - Import an RSA private key
276189251Ssam * @key: Key buffer (DER encoded RSA private key)
277189251Ssam * @len: Key buffer length in bytes
278189251Ssam * Returns: Pointer to the private key or %NULL on failure
279189251Ssam *
280189251Ssam * This function is only used with internal TLSv1 implementation
281189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
282189251Ssam * to implement this.
283189251Ssam */
284189251Ssamstruct crypto_private_key * crypto_private_key_import(const u8 *key,
285189251Ssam						      size_t len);
286189251Ssam
287189251Ssam/**
288189251Ssam * crypto_public_key_from_cert - Import an RSA public key from a certificate
289189251Ssam * @buf: DER encoded X.509 certificate
290189251Ssam * @len: Certificate buffer length in bytes
291189251Ssam * Returns: Pointer to public key or %NULL on failure
292189251Ssam *
293189251Ssam * This function can just return %NULL if the crypto library does not support
294189251Ssam * X.509 parsing. In that case, internal code will be used to parse the
295189251Ssam * certificate and public key is imported using crypto_public_key_import().
296189251Ssam *
297189251Ssam * This function is only used with internal TLSv1 implementation
298189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
299189251Ssam * to implement this.
300189251Ssam */
301189251Ssamstruct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
302189251Ssam						       size_t len);
303189251Ssam
304189251Ssam/**
305189251Ssam * crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5)
306189251Ssam * @key: Public key
307189251Ssam * @in: Plaintext buffer
308189251Ssam * @inlen: Length of plaintext buffer in bytes
309189251Ssam * @out: Output buffer for encrypted data
310189251Ssam * @outlen: Length of output buffer in bytes; set to used length on success
311189251Ssam * Returns: 0 on success, -1 on failure
312189251Ssam *
313189251Ssam * This function is only used with internal TLSv1 implementation
314189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
315189251Ssam * to implement this.
316189251Ssam */
317189251Ssamint __must_check crypto_public_key_encrypt_pkcs1_v15(
318189251Ssam	struct crypto_public_key *key, const u8 *in, size_t inlen,
319189251Ssam	u8 *out, size_t *outlen);
320189251Ssam
321189251Ssam/**
322189251Ssam * crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5)
323189251Ssam * @key: Private key
324189251Ssam * @in: Encrypted buffer
325189251Ssam * @inlen: Length of encrypted buffer in bytes
326189251Ssam * @out: Output buffer for encrypted data
327189251Ssam * @outlen: Length of output buffer in bytes; set to used length on success
328189251Ssam * Returns: 0 on success, -1 on failure
329189251Ssam *
330189251Ssam * This function is only used with internal TLSv1 implementation
331189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
332189251Ssam * to implement this.
333189251Ssam */
334189251Ssamint __must_check crypto_private_key_decrypt_pkcs1_v15(
335189251Ssam	struct crypto_private_key *key, const u8 *in, size_t inlen,
336189251Ssam	u8 *out, size_t *outlen);
337189251Ssam
338189251Ssam/**
339189251Ssam * crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1)
340189251Ssam * @key: Private key from crypto_private_key_import()
341189251Ssam * @in: Plaintext buffer
342189251Ssam * @inlen: Length of plaintext buffer in bytes
343189251Ssam * @out: Output buffer for encrypted (signed) data
344189251Ssam * @outlen: Length of output buffer in bytes; set to used length on success
345189251Ssam * Returns: 0 on success, -1 on failure
346189251Ssam *
347189251Ssam * This function is only used with internal TLSv1 implementation
348189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
349189251Ssam * to implement this.
350189251Ssam */
351189251Ssamint __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
352189251Ssam					       const u8 *in, size_t inlen,
353189251Ssam					       u8 *out, size_t *outlen);
354189251Ssam
355189251Ssam/**
356189251Ssam * crypto_public_key_free - Free public key
357189251Ssam * @key: Public key
358189251Ssam *
359189251Ssam * This function is only used with internal TLSv1 implementation
360189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
361189251Ssam * to implement this.
362189251Ssam */
363189251Ssamvoid crypto_public_key_free(struct crypto_public_key *key);
364189251Ssam
365189251Ssam/**
366189251Ssam * crypto_private_key_free - Free private key
367189251Ssam * @key: Private key from crypto_private_key_import()
368189251Ssam *
369189251Ssam * This function is only used with internal TLSv1 implementation
370189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
371189251Ssam * to implement this.
372189251Ssam */
373189251Ssamvoid crypto_private_key_free(struct crypto_private_key *key);
374189251Ssam
375189251Ssam/**
376189251Ssam * crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature
377189251Ssam * @key: Public key
378189251Ssam * @crypt: Encrypted signature data (using the private key)
379189251Ssam * @crypt_len: Encrypted signature data length
380189251Ssam * @plain: Buffer for plaintext (at least crypt_len bytes)
381189251Ssam * @plain_len: Plaintext length (max buffer size on input, real len on output);
382189251Ssam * Returns: 0 on success, -1 on failure
383189251Ssam */
384189251Ssamint __must_check crypto_public_key_decrypt_pkcs1(
385189251Ssam	struct crypto_public_key *key, const u8 *crypt, size_t crypt_len,
386189251Ssam	u8 *plain, size_t *plain_len);
387189251Ssam
388189251Ssam/**
389189251Ssam * crypto_global_init - Initialize crypto wrapper
390189251Ssam *
391189251Ssam * This function is only used with internal TLSv1 implementation
392189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
393189251Ssam * to implement this.
394189251Ssam */
395189251Ssamint __must_check crypto_global_init(void);
396189251Ssam
397189251Ssam/**
398189251Ssam * crypto_global_deinit - Deinitialize crypto wrapper
399189251Ssam *
400189251Ssam * This function is only used with internal TLSv1 implementation
401189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
402189251Ssam * to implement this.
403189251Ssam */
404189251Ssamvoid crypto_global_deinit(void);
405189251Ssam
406189251Ssam/**
407189251Ssam * crypto_mod_exp - Modular exponentiation of large integers
408189251Ssam * @base: Base integer (big endian byte array)
409189251Ssam * @base_len: Length of base integer in bytes
410189251Ssam * @power: Power integer (big endian byte array)
411189251Ssam * @power_len: Length of power integer in bytes
412189251Ssam * @modulus: Modulus integer (big endian byte array)
413189251Ssam * @modulus_len: Length of modulus integer in bytes
414189251Ssam * @result: Buffer for the result
415189251Ssam * @result_len: Result length (max buffer size on input, real len on output)
416189251Ssam * Returns: 0 on success, -1 on failure
417189251Ssam *
418189251Ssam * This function calculates result = base ^ power mod modulus. modules_len is
419189251Ssam * used as the maximum size of modulus buffer. It is set to the used size on
420189251Ssam * success.
421189251Ssam *
422189251Ssam * This function is only used with internal TLSv1 implementation
423189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
424189251Ssam * to implement this.
425189251Ssam */
426189251Ssamint __must_check crypto_mod_exp(const u8 *base, size_t base_len,
427189251Ssam				const u8 *power, size_t power_len,
428189251Ssam				const u8 *modulus, size_t modulus_len,
429189251Ssam				u8 *result, size_t *result_len);
430189251Ssam
431189251Ssam#endif /* CRYPTO_H */
432