crypto.h revision 189251
1193645Ssimon/*
2296465Sdelphij * WPA Supplicant / wrapper functions for crypto libraries
3296465Sdelphij * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
4193645Ssimon *
5193645Ssimon * This program is free software; you can redistribute it and/or modify
6193645Ssimon * it under the terms of the GNU General Public License version 2 as
7193645Ssimon * published by the Free Software Foundation.
8193645Ssimon *
9193645Ssimon * Alternatively, this software may be distributed under the terms of BSD
10193645Ssimon * license.
11193645Ssimon *
12193645Ssimon * See README and COPYING for more details.
13193645Ssimon *
14296465Sdelphij * This file defines the cryptographic functions that need to be implemented
15193645Ssimon * for wpa_supplicant and hostapd. When TLS is not used, internal
16193645Ssimon * implementation of MD5, SHA1, and AES is used and no external libraries are
17193645Ssimon * required. When TLS is enabled (e.g., by enabling EAP-TLS or EAP-PEAP), the
18193645Ssimon * crypto library used by the TLS implementation is expected to be used for
19193645Ssimon * non-TLS needs, too, in order to save space by not implementing these
20193645Ssimon * functions twice.
21193645Ssimon *
22193645Ssimon * Wrapper code for using each crypto library is in its own file (crypto*.c)
23193645Ssimon * and one of these files is build and linked in to provide the functions
24193645Ssimon * defined here.
25193645Ssimon */
26193645Ssimon
27193645Ssimon#ifndef CRYPTO_H
28193645Ssimon#define CRYPTO_H
29193645Ssimon
30193645Ssimon/**
31193645Ssimon * md4_vector - MD4 hash for data vector
32193645Ssimon * @num_elem: Number of elements in the data vector
33193645Ssimon * @addr: Pointers to the data areas
34193645Ssimon * @len: Lengths of the data blocks
35193645Ssimon * @mac: Buffer for the hash
36193645Ssimon */
37193645Ssimonvoid md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
38193645Ssimon
39193645Ssimon/**
40193645Ssimon * md5_vector - MD5 hash for data vector
41193645Ssimon * @num_elem: Number of elements in the data vector
42193645Ssimon * @addr: Pointers to the data areas
43193645Ssimon * @len: Lengths of the data blocks
44193645Ssimon * @mac: Buffer for the hash
45193645Ssimon */
46193645Ssimonvoid md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
47193645Ssimon
48193645Ssimon/**
49193645Ssimon * sha1_vector - SHA-1 hash for data vector
50193645Ssimon * @num_elem: Number of elements in the data vector
51193645Ssimon * @addr: Pointers to the data areas
52193645Ssimon * @len: Lengths of the data blocks
53193645Ssimon * @mac: Buffer for the hash
54193645Ssimon */
55193645Ssimonvoid sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
56193645Ssimon		 u8 *mac);
57193645Ssimon
58193645Ssimon/**
59193645Ssimon * fips186_2-prf - NIST FIPS Publication 186-2 change notice 1 PRF
60193645Ssimon * @seed: Seed/key for the PRF
61193645Ssimon * @seed_len: Seed length in bytes
62215697Ssimon * @x: Buffer for PRF output
63205128Ssimon * @xlen: Output length in bytes
64215697Ssimon * Returns: 0 on success, -1 on failure
65296465Sdelphij *
66215697Ssimon * This function implements random number generation specified in NIST FIPS
67296465Sdelphij * Publication 186-2 for EAP-SIM. This PRF uses a function that is similar to
68296465Sdelphij * SHA-1, but has different message padding.
69296465Sdelphij */
70215697Ssimonint __must_check fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x,
71215697Ssimon			       size_t xlen);
72193645Ssimon
73296465Sdelphij/**
74296465Sdelphij * sha256_vector - SHA256 hash for data vector
75296465Sdelphij * @num_elem: Number of elements in the data vector
76193645Ssimon * @addr: Pointers to the data areas
77193645Ssimon * @len: Lengths of the data blocks
78193645Ssimon * @mac: Buffer for the hash
79296465Sdelphij */
80193645Ssimonvoid sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
81296465Sdelphij		   u8 *mac);
82296465Sdelphij
83296465Sdelphij/**
84296465Sdelphij * des_encrypt - Encrypt one block with DES
85296465Sdelphij * @clear: 8 octets (in)
86296465Sdelphij * @key: 7 octets (in) (no parity bits included)
87296465Sdelphij * @cypher: 8 octets (out)
88296465Sdelphij */
89296465Sdelphijvoid des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
90296465Sdelphij
91296465Sdelphij/**
92296465Sdelphij * aes_encrypt_init - Initialize AES for encryption
93296465Sdelphij * @key: Encryption key
94193645Ssimon * @len: Key length in bytes (usually 16, i.e., 128 bits)
95296465Sdelphij * Returns: Pointer to context data or %NULL on failure
96296465Sdelphij */
97248272Sdelphijvoid * aes_encrypt_init(const u8 *key, size_t len);
98193645Ssimon
99248272Sdelphij/**
100296465Sdelphij * aes_encrypt - Encrypt one AES block
101296465Sdelphij * @ctx: Context pointer from aes_encrypt_init()
102296465Sdelphij * @plain: Plaintext data to be encrypted (16 bytes)
103296465Sdelphij * @crypt: Buffer for the encrypted data (16 bytes)
104296465Sdelphij */
105248272Sdelphijvoid aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
106296465Sdelphij
107296465Sdelphij/**
108248272Sdelphij * aes_encrypt_deinit - Deinitialize AES encryption
109296465Sdelphij * @ctx: Context pointer from aes_encrypt_init()
110296465Sdelphij */
111248272Sdelphijvoid aes_encrypt_deinit(void *ctx);
112
113/**
114 * aes_decrypt_init - Initialize AES for decryption
115 * @key: Decryption key
116 * @len: Key length in bytes (usually 16, i.e., 128 bits)
117 * Returns: Pointer to context data or %NULL on failure
118 */
119void * aes_decrypt_init(const u8 *key, size_t len);
120
121/**
122 * aes_decrypt - Decrypt one AES block
123 * @ctx: Context pointer from aes_encrypt_init()
124 * @crypt: Encrypted data (16 bytes)
125 * @plain: Buffer for the decrypted data (16 bytes)
126 */
127void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
128
129/**
130 * aes_decrypt_deinit - Deinitialize AES decryption
131 * @ctx: Context pointer from aes_encrypt_init()
132 */
133void aes_decrypt_deinit(void *ctx);
134
135
136enum crypto_hash_alg {
137	CRYPTO_HASH_ALG_MD5, CRYPTO_HASH_ALG_SHA1,
138	CRYPTO_HASH_ALG_HMAC_MD5, CRYPTO_HASH_ALG_HMAC_SHA1
139};
140
141struct crypto_hash;
142
143/**
144 * crypto_hash_init - Initialize hash/HMAC function
145 * @alg: Hash algorithm
146 * @key: Key for keyed hash (e.g., HMAC) or %NULL if not needed
147 * @key_len: Length of the key in bytes
148 * Returns: Pointer to hash context to use with other hash functions or %NULL
149 * on failure
150 *
151 * This function is only used with internal TLSv1 implementation
152 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
153 * to implement this.
154 */
155struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
156				      size_t key_len);
157
158/**
159 * crypto_hash_update - Add data to hash calculation
160 * @ctx: Context pointer from crypto_hash_init()
161 * @data: Data buffer to add
162 * @len: Length of the buffer
163 *
164 * This function is only used with internal TLSv1 implementation
165 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
166 * to implement this.
167 */
168void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len);
169
170/**
171 * crypto_hash_finish - Complete hash calculation
172 * @ctx: Context pointer from crypto_hash_init()
173 * @hash: Buffer for hash value or %NULL if caller is just freeing the hash
174 * context
175 * @len: Pointer to length of the buffer or %NULL if caller is just freeing the
176 * hash context; on return, this is set to the actual length of the hash value
177 * Returns: 0 on success, -1 if buffer is too small (len set to needed length),
178 * or -2 on other failures (including failed crypto_hash_update() operations)
179 *
180 * This function calculates the hash value and frees the context buffer that
181 * was used for hash calculation.
182 *
183 * This function is only used with internal TLSv1 implementation
184 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
185 * to implement this.
186 */
187int crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len);
188
189
190enum crypto_cipher_alg {
191	CRYPTO_CIPHER_NULL = 0, CRYPTO_CIPHER_ALG_AES, CRYPTO_CIPHER_ALG_3DES,
192	CRYPTO_CIPHER_ALG_DES, CRYPTO_CIPHER_ALG_RC2, CRYPTO_CIPHER_ALG_RC4
193};
194
195struct crypto_cipher;
196
197/**
198 * crypto_cipher_init - Initialize block/stream cipher function
199 * @alg: Cipher algorithm
200 * @iv: Initialization vector for block ciphers or %NULL for stream ciphers
201 * @key: Cipher key
202 * @key_len: Length of key in bytes
203 * Returns: Pointer to cipher context to use with other cipher functions or
204 * %NULL on failure
205 *
206 * This function is only used with internal TLSv1 implementation
207 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
208 * to implement this.
209 */
210struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
211					  const u8 *iv, const u8 *key,
212					  size_t key_len);
213
214/**
215 * crypto_cipher_encrypt - Cipher encrypt
216 * @ctx: Context pointer from crypto_cipher_init()
217 * @plain: Plaintext to cipher
218 * @crypt: Resulting ciphertext
219 * @len: Length of the plaintext
220 * Returns: 0 on success, -1 on failure
221 *
222 * This function is only used with internal TLSv1 implementation
223 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
224 * to implement this.
225 */
226int __must_check crypto_cipher_encrypt(struct crypto_cipher *ctx,
227				       const u8 *plain, u8 *crypt, size_t len);
228
229/**
230 * crypto_cipher_decrypt - Cipher decrypt
231 * @ctx: Context pointer from crypto_cipher_init()
232 * @crypt: Ciphertext to decrypt
233 * @plain: Resulting plaintext
234 * @len: Length of the cipher text
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_decrypt(struct crypto_cipher *ctx,
242				       const u8 *crypt, u8 *plain, size_t len);
243
244/**
245 * crypto_cipher_decrypt - Free cipher context
246 * @ctx: Context pointer from crypto_cipher_init()
247 *
248 * This function is only used with internal TLSv1 implementation
249 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
250 * to implement this.
251 */
252void crypto_cipher_deinit(struct crypto_cipher *ctx);
253
254
255struct crypto_public_key;
256struct crypto_private_key;
257
258/**
259 * crypto_public_key_import - Import an RSA public key
260 * @key: Key buffer (DER encoded RSA public key)
261 * @len: Key buffer length in bytes
262 * Returns: Pointer to the public key or %NULL on failure
263 *
264 * This function can just return %NULL if the crypto library supports X.509
265 * parsing. In that case, crypto_public_key_from_cert() is used to import the
266 * public key from a certificate.
267 *
268 * This function is only used with internal TLSv1 implementation
269 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
270 * to implement this.
271 */
272struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len);
273
274/**
275 * crypto_private_key_import - Import an RSA private key
276 * @key: Key buffer (DER encoded RSA private key)
277 * @len: Key buffer length in bytes
278 * Returns: Pointer to the private key or %NULL on failure
279 *
280 * This function is only used with internal TLSv1 implementation
281 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
282 * to implement this.
283 */
284struct crypto_private_key * crypto_private_key_import(const u8 *key,
285						      size_t len);
286
287/**
288 * crypto_public_key_from_cert - Import an RSA public key from a certificate
289 * @buf: DER encoded X.509 certificate
290 * @len: Certificate buffer length in bytes
291 * Returns: Pointer to public key or %NULL on failure
292 *
293 * This function can just return %NULL if the crypto library does not support
294 * X.509 parsing. In that case, internal code will be used to parse the
295 * certificate and public key is imported using crypto_public_key_import().
296 *
297 * This function is only used with internal TLSv1 implementation
298 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
299 * to implement this.
300 */
301struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
302						       size_t len);
303
304/**
305 * crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5)
306 * @key: Public key
307 * @in: Plaintext buffer
308 * @inlen: Length of plaintext buffer in bytes
309 * @out: Output buffer for encrypted data
310 * @outlen: Length of output buffer in bytes; set to used length on success
311 * Returns: 0 on success, -1 on failure
312 *
313 * This function is only used with internal TLSv1 implementation
314 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
315 * to implement this.
316 */
317int __must_check crypto_public_key_encrypt_pkcs1_v15(
318	struct crypto_public_key *key, const u8 *in, size_t inlen,
319	u8 *out, size_t *outlen);
320
321/**
322 * crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5)
323 * @key: Private key
324 * @in: Encrypted buffer
325 * @inlen: Length of encrypted 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_private_key_decrypt_pkcs1_v15(
335	struct crypto_private_key *key, const u8 *in, size_t inlen,
336	u8 *out, size_t *outlen);
337
338/**
339 * crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1)
340 * @key: Private key from crypto_private_key_import()
341 * @in: Plaintext buffer
342 * @inlen: Length of plaintext buffer in bytes
343 * @out: Output buffer for encrypted (signed) 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_sign_pkcs1(struct crypto_private_key *key,
352					       const u8 *in, size_t inlen,
353					       u8 *out, size_t *outlen);
354
355/**
356 * crypto_public_key_free - Free public key
357 * @key: Public key
358 *
359 * This function is only used with internal TLSv1 implementation
360 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
361 * to implement this.
362 */
363void crypto_public_key_free(struct crypto_public_key *key);
364
365/**
366 * crypto_private_key_free - Free private key
367 * @key: Private key from crypto_private_key_import()
368 *
369 * This function is only used with internal TLSv1 implementation
370 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
371 * to implement this.
372 */
373void crypto_private_key_free(struct crypto_private_key *key);
374
375/**
376 * crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature
377 * @key: Public key
378 * @crypt: Encrypted signature data (using the private key)
379 * @crypt_len: Encrypted signature data length
380 * @plain: Buffer for plaintext (at least crypt_len bytes)
381 * @plain_len: Plaintext length (max buffer size on input, real len on output);
382 * Returns: 0 on success, -1 on failure
383 */
384int __must_check crypto_public_key_decrypt_pkcs1(
385	struct crypto_public_key *key, const u8 *crypt, size_t crypt_len,
386	u8 *plain, size_t *plain_len);
387
388/**
389 * crypto_global_init - Initialize crypto wrapper
390 *
391 * This function is only used with internal TLSv1 implementation
392 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
393 * to implement this.
394 */
395int __must_check crypto_global_init(void);
396
397/**
398 * crypto_global_deinit - Deinitialize crypto wrapper
399 *
400 * This function is only used with internal TLSv1 implementation
401 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
402 * to implement this.
403 */
404void crypto_global_deinit(void);
405
406/**
407 * crypto_mod_exp - Modular exponentiation of large integers
408 * @base: Base integer (big endian byte array)
409 * @base_len: Length of base integer in bytes
410 * @power: Power integer (big endian byte array)
411 * @power_len: Length of power integer in bytes
412 * @modulus: Modulus integer (big endian byte array)
413 * @modulus_len: Length of modulus integer in bytes
414 * @result: Buffer for the result
415 * @result_len: Result length (max buffer size on input, real len on output)
416 * Returns: 0 on success, -1 on failure
417 *
418 * This function calculates result = base ^ power mod modulus. modules_len is
419 * used as the maximum size of modulus buffer. It is set to the used size on
420 * success.
421 *
422 * This function is only used with internal TLSv1 implementation
423 * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
424 * to implement this.
425 */
426int __must_check crypto_mod_exp(const u8 *base, size_t base_len,
427				const u8 *power, size_t power_len,
428				const u8 *modulus, size_t modulus_len,
429				u8 *result, size_t *result_len);
430
431#endif /* CRYPTO_H */
432