1189251Ssam/* 2189251Ssam * WPA Supplicant / wrapper functions for crypto libraries 3214734Srpaulo * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi> 4189251Ssam * 5252726Srpaulo * This software may be distributed under the terms of the BSD license. 6252726Srpaulo * See README for more details. 7189251Ssam * 8189251Ssam * This file defines the cryptographic functions that need to be implemented 9189251Ssam * for wpa_supplicant and hostapd. When TLS is not used, internal 10189251Ssam * implementation of MD5, SHA1, and AES is used and no external libraries are 11189251Ssam * required. When TLS is enabled (e.g., by enabling EAP-TLS or EAP-PEAP), the 12189251Ssam * crypto library used by the TLS implementation is expected to be used for 13189251Ssam * non-TLS needs, too, in order to save space by not implementing these 14189251Ssam * functions twice. 15189251Ssam * 16189251Ssam * Wrapper code for using each crypto library is in its own file (crypto*.c) 17189251Ssam * and one of these files is build and linked in to provide the functions 18189251Ssam * defined here. 19189251Ssam */ 20189251Ssam 21189251Ssam#ifndef CRYPTO_H 22189251Ssam#define CRYPTO_H 23189251Ssam 24189251Ssam/** 25189251Ssam * md4_vector - MD4 hash for data vector 26189251Ssam * @num_elem: Number of elements in the data vector 27189251Ssam * @addr: Pointers to the data areas 28189251Ssam * @len: Lengths of the data blocks 29189251Ssam * @mac: Buffer for the hash 30214734Srpaulo * Returns: 0 on success, -1 on failure 31189251Ssam */ 32214734Srpauloint md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac); 33189251Ssam 34189251Ssam/** 35189251Ssam * md5_vector - MD5 hash for data vector 36189251Ssam * @num_elem: Number of elements in the data vector 37189251Ssam * @addr: Pointers to the data areas 38189251Ssam * @len: Lengths of the data blocks 39189251Ssam * @mac: Buffer for the hash 40214734Srpaulo * Returns: 0 on success, -1 on failure 41189251Ssam */ 42214734Srpauloint md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac); 43189251Ssam 44214734Srpaulo 45214734Srpaulo/** 46189251Ssam * sha1_vector - SHA-1 hash for data vector 47189251Ssam * @num_elem: Number of elements in the data vector 48189251Ssam * @addr: Pointers to the data areas 49189251Ssam * @len: Lengths of the data blocks 50189251Ssam * @mac: Buffer for the hash 51214734Srpaulo * Returns: 0 on success, -1 on failure 52189251Ssam */ 53214734Srpauloint sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, 54214734Srpaulo u8 *mac); 55189251Ssam 56189251Ssam/** 57189251Ssam * fips186_2-prf - NIST FIPS Publication 186-2 change notice 1 PRF 58189251Ssam * @seed: Seed/key for the PRF 59189251Ssam * @seed_len: Seed length in bytes 60189251Ssam * @x: Buffer for PRF output 61189251Ssam * @xlen: Output length in bytes 62189251Ssam * Returns: 0 on success, -1 on failure 63189251Ssam * 64189251Ssam * This function implements random number generation specified in NIST FIPS 65189251Ssam * Publication 186-2 for EAP-SIM. This PRF uses a function that is similar to 66189251Ssam * SHA-1, but has different message padding. 67189251Ssam */ 68189251Ssamint __must_check fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, 69189251Ssam size_t xlen); 70189251Ssam 71189251Ssam/** 72189251Ssam * sha256_vector - SHA256 hash for data vector 73189251Ssam * @num_elem: Number of elements in the data vector 74189251Ssam * @addr: Pointers to the data areas 75189251Ssam * @len: Lengths of the data blocks 76189251Ssam * @mac: Buffer for the hash 77214734Srpaulo * Returns: 0 on success, -1 on failure 78189251Ssam */ 79214734Srpauloint sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len, 80214734Srpaulo u8 *mac); 81189251Ssam 82189251Ssam/** 83189251Ssam * des_encrypt - Encrypt one block with DES 84189251Ssam * @clear: 8 octets (in) 85189251Ssam * @key: 7 octets (in) (no parity bits included) 86189251Ssam * @cypher: 8 octets (out) 87189251Ssam */ 88189251Ssamvoid des_encrypt(const u8 *clear, const u8 *key, u8 *cypher); 89189251Ssam 90189251Ssam/** 91189251Ssam * aes_encrypt_init - Initialize AES for encryption 92189251Ssam * @key: Encryption key 93189251Ssam * @len: Key length in bytes (usually 16, i.e., 128 bits) 94189251Ssam * Returns: Pointer to context data or %NULL on failure 95189251Ssam */ 96189251Ssamvoid * aes_encrypt_init(const u8 *key, size_t len); 97189251Ssam 98189251Ssam/** 99189251Ssam * aes_encrypt - Encrypt one AES block 100189251Ssam * @ctx: Context pointer from aes_encrypt_init() 101189251Ssam * @plain: Plaintext data to be encrypted (16 bytes) 102189251Ssam * @crypt: Buffer for the encrypted data (16 bytes) 103189251Ssam */ 104189251Ssamvoid aes_encrypt(void *ctx, const u8 *plain, u8 *crypt); 105189251Ssam 106189251Ssam/** 107189251Ssam * aes_encrypt_deinit - Deinitialize AES encryption 108189251Ssam * @ctx: Context pointer from aes_encrypt_init() 109189251Ssam */ 110189251Ssamvoid aes_encrypt_deinit(void *ctx); 111189251Ssam 112189251Ssam/** 113189251Ssam * aes_decrypt_init - Initialize AES for decryption 114189251Ssam * @key: Decryption key 115189251Ssam * @len: Key length in bytes (usually 16, i.e., 128 bits) 116189251Ssam * Returns: Pointer to context data or %NULL on failure 117189251Ssam */ 118189251Ssamvoid * aes_decrypt_init(const u8 *key, size_t len); 119189251Ssam 120189251Ssam/** 121189251Ssam * aes_decrypt - Decrypt one AES block 122189251Ssam * @ctx: Context pointer from aes_encrypt_init() 123189251Ssam * @crypt: Encrypted data (16 bytes) 124189251Ssam * @plain: Buffer for the decrypted data (16 bytes) 125189251Ssam */ 126189251Ssamvoid aes_decrypt(void *ctx, const u8 *crypt, u8 *plain); 127189251Ssam 128189251Ssam/** 129189251Ssam * aes_decrypt_deinit - Deinitialize AES decryption 130189251Ssam * @ctx: Context pointer from aes_encrypt_init() 131189251Ssam */ 132189251Ssamvoid aes_decrypt_deinit(void *ctx); 133189251Ssam 134189251Ssam 135189251Ssamenum crypto_hash_alg { 136189251Ssam CRYPTO_HASH_ALG_MD5, CRYPTO_HASH_ALG_SHA1, 137252726Srpaulo CRYPTO_HASH_ALG_HMAC_MD5, CRYPTO_HASH_ALG_HMAC_SHA1, 138252726Srpaulo CRYPTO_HASH_ALG_SHA256, CRYPTO_HASH_ALG_HMAC_SHA256 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 278214734Srpaulo * @passwd: Key encryption password or %NULL if key is not encrypted 279189251Ssam * Returns: Pointer to the private key or %NULL on failure 280189251Ssam * 281189251Ssam * This function is only used with internal TLSv1 implementation 282189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 283189251Ssam * to implement this. 284189251Ssam */ 285189251Ssamstruct crypto_private_key * crypto_private_key_import(const u8 *key, 286214734Srpaulo size_t len, 287214734Srpaulo const char *passwd); 288189251Ssam 289189251Ssam/** 290189251Ssam * crypto_public_key_from_cert - Import an RSA public key from a certificate 291189251Ssam * @buf: DER encoded X.509 certificate 292189251Ssam * @len: Certificate buffer length in bytes 293189251Ssam * Returns: Pointer to public key or %NULL on failure 294189251Ssam * 295189251Ssam * This function can just return %NULL if the crypto library does not support 296189251Ssam * X.509 parsing. In that case, internal code will be used to parse the 297189251Ssam * certificate and public key is imported using crypto_public_key_import(). 298189251Ssam * 299189251Ssam * This function is only used with internal TLSv1 implementation 300189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 301189251Ssam * to implement this. 302189251Ssam */ 303189251Ssamstruct crypto_public_key * crypto_public_key_from_cert(const u8 *buf, 304189251Ssam size_t len); 305189251Ssam 306189251Ssam/** 307189251Ssam * crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5) 308189251Ssam * @key: Public key 309189251Ssam * @in: Plaintext buffer 310189251Ssam * @inlen: Length of plaintext buffer in bytes 311189251Ssam * @out: Output buffer for encrypted data 312189251Ssam * @outlen: Length of output buffer in bytes; set to used length on success 313189251Ssam * Returns: 0 on success, -1 on failure 314189251Ssam * 315189251Ssam * This function is only used with internal TLSv1 implementation 316189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 317189251Ssam * to implement this. 318189251Ssam */ 319189251Ssamint __must_check crypto_public_key_encrypt_pkcs1_v15( 320189251Ssam struct crypto_public_key *key, const u8 *in, size_t inlen, 321189251Ssam u8 *out, size_t *outlen); 322189251Ssam 323189251Ssam/** 324189251Ssam * crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5) 325189251Ssam * @key: Private key 326189251Ssam * @in: Encrypted buffer 327189251Ssam * @inlen: Length of encrypted buffer in bytes 328189251Ssam * @out: Output buffer for encrypted data 329189251Ssam * @outlen: Length of output buffer in bytes; set to used length on success 330189251Ssam * Returns: 0 on success, -1 on failure 331189251Ssam * 332189251Ssam * This function is only used with internal TLSv1 implementation 333189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 334189251Ssam * to implement this. 335189251Ssam */ 336189251Ssamint __must_check crypto_private_key_decrypt_pkcs1_v15( 337189251Ssam struct crypto_private_key *key, const u8 *in, size_t inlen, 338189251Ssam u8 *out, size_t *outlen); 339189251Ssam 340189251Ssam/** 341189251Ssam * crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1) 342189251Ssam * @key: Private key from crypto_private_key_import() 343189251Ssam * @in: Plaintext buffer 344189251Ssam * @inlen: Length of plaintext buffer in bytes 345189251Ssam * @out: Output buffer for encrypted (signed) data 346189251Ssam * @outlen: Length of output buffer in bytes; set to used length on success 347189251Ssam * Returns: 0 on success, -1 on failure 348189251Ssam * 349189251Ssam * This function is only used with internal TLSv1 implementation 350189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 351189251Ssam * to implement this. 352189251Ssam */ 353189251Ssamint __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key, 354189251Ssam const u8 *in, size_t inlen, 355189251Ssam u8 *out, size_t *outlen); 356189251Ssam 357189251Ssam/** 358189251Ssam * crypto_public_key_free - Free public key 359189251Ssam * @key: Public key 360189251Ssam * 361189251Ssam * This function is only used with internal TLSv1 implementation 362189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 363189251Ssam * to implement this. 364189251Ssam */ 365189251Ssamvoid crypto_public_key_free(struct crypto_public_key *key); 366189251Ssam 367189251Ssam/** 368189251Ssam * crypto_private_key_free - Free private key 369189251Ssam * @key: Private key from crypto_private_key_import() 370189251Ssam * 371189251Ssam * This function is only used with internal TLSv1 implementation 372189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 373189251Ssam * to implement this. 374189251Ssam */ 375189251Ssamvoid crypto_private_key_free(struct crypto_private_key *key); 376189251Ssam 377189251Ssam/** 378189251Ssam * crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature 379189251Ssam * @key: Public key 380189251Ssam * @crypt: Encrypted signature data (using the private key) 381189251Ssam * @crypt_len: Encrypted signature data length 382189251Ssam * @plain: Buffer for plaintext (at least crypt_len bytes) 383189251Ssam * @plain_len: Plaintext length (max buffer size on input, real len on output); 384189251Ssam * Returns: 0 on success, -1 on failure 385189251Ssam */ 386189251Ssamint __must_check crypto_public_key_decrypt_pkcs1( 387189251Ssam struct crypto_public_key *key, const u8 *crypt, size_t crypt_len, 388189251Ssam u8 *plain, size_t *plain_len); 389189251Ssam 390189251Ssam/** 391189251Ssam * crypto_global_init - Initialize crypto wrapper 392189251Ssam * 393189251Ssam * This function is only used with internal TLSv1 implementation 394189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 395189251Ssam * to implement this. 396189251Ssam */ 397189251Ssamint __must_check crypto_global_init(void); 398189251Ssam 399189251Ssam/** 400189251Ssam * crypto_global_deinit - Deinitialize crypto wrapper 401189251Ssam * 402189251Ssam * This function is only used with internal TLSv1 implementation 403189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 404189251Ssam * to implement this. 405189251Ssam */ 406189251Ssamvoid crypto_global_deinit(void); 407189251Ssam 408189251Ssam/** 409189251Ssam * crypto_mod_exp - Modular exponentiation of large integers 410189251Ssam * @base: Base integer (big endian byte array) 411189251Ssam * @base_len: Length of base integer in bytes 412189251Ssam * @power: Power integer (big endian byte array) 413189251Ssam * @power_len: Length of power integer in bytes 414189251Ssam * @modulus: Modulus integer (big endian byte array) 415189251Ssam * @modulus_len: Length of modulus integer in bytes 416189251Ssam * @result: Buffer for the result 417189251Ssam * @result_len: Result length (max buffer size on input, real len on output) 418189251Ssam * Returns: 0 on success, -1 on failure 419189251Ssam * 420189251Ssam * This function calculates result = base ^ power mod modulus. modules_len is 421189251Ssam * used as the maximum size of modulus buffer. It is set to the used size on 422189251Ssam * success. 423189251Ssam * 424189251Ssam * This function is only used with internal TLSv1 implementation 425189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 426189251Ssam * to implement this. 427189251Ssam */ 428189251Ssamint __must_check crypto_mod_exp(const u8 *base, size_t base_len, 429189251Ssam const u8 *power, size_t power_len, 430189251Ssam const u8 *modulus, size_t modulus_len, 431189251Ssam u8 *result, size_t *result_len); 432189251Ssam 433214734Srpaulo/** 434214734Srpaulo * rc4_skip - XOR RC4 stream to given data with skip-stream-start 435214734Srpaulo * @key: RC4 key 436214734Srpaulo * @keylen: RC4 key length 437214734Srpaulo * @skip: number of bytes to skip from the beginning of the RC4 stream 438214734Srpaulo * @data: data to be XOR'ed with RC4 stream 439214734Srpaulo * @data_len: buf length 440214734Srpaulo * Returns: 0 on success, -1 on failure 441214734Srpaulo * 442214734Srpaulo * Generate RC4 pseudo random stream for the given key, skip beginning of the 443214734Srpaulo * stream, and XOR the end result with the data buffer to perform RC4 444214734Srpaulo * encryption/decryption. 445214734Srpaulo */ 446214734Srpauloint rc4_skip(const u8 *key, size_t keylen, size_t skip, 447214734Srpaulo u8 *data, size_t data_len); 448214734Srpaulo 449252726Srpaulo/** 450252726Srpaulo * crypto_get_random - Generate cryptographically strong pseudy-random bytes 451252726Srpaulo * @buf: Buffer for data 452252726Srpaulo * @len: Number of bytes to generate 453252726Srpaulo * Returns: 0 on success, -1 on failure 454252726Srpaulo * 455252726Srpaulo * If the PRNG does not have enough entropy to ensure unpredictable byte 456252726Srpaulo * sequence, this functions must return -1. 457252726Srpaulo */ 458252726Srpauloint crypto_get_random(void *buf, size_t len); 459252726Srpaulo 460189251Ssam#endif /* CRYPTO_H */ 461