1189251Ssam/* 2281806Srpaulo * Wrapper functions for crypto libraries 3281806Srpaulo * Copyright (c) 2004-2013, 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 274281806Srpaulostruct crypto_public_key * 275281806Srpaulocrypto_public_key_import_parts(const u8 *n, size_t n_len, 276281806Srpaulo const u8 *e, size_t e_len); 277281806Srpaulo 278189251Ssam/** 279189251Ssam * crypto_private_key_import - Import an RSA private key 280189251Ssam * @key: Key buffer (DER encoded RSA private key) 281189251Ssam * @len: Key buffer length in bytes 282214734Srpaulo * @passwd: Key encryption password or %NULL if key is not encrypted 283189251Ssam * Returns: Pointer to the private key or %NULL on failure 284189251Ssam * 285189251Ssam * This function is only used with internal TLSv1 implementation 286189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 287189251Ssam * to implement this. 288189251Ssam */ 289189251Ssamstruct crypto_private_key * crypto_private_key_import(const u8 *key, 290214734Srpaulo size_t len, 291214734Srpaulo const char *passwd); 292189251Ssam 293189251Ssam/** 294189251Ssam * crypto_public_key_from_cert - Import an RSA public key from a certificate 295189251Ssam * @buf: DER encoded X.509 certificate 296189251Ssam * @len: Certificate buffer length in bytes 297189251Ssam * Returns: Pointer to public key or %NULL on failure 298189251Ssam * 299189251Ssam * This function can just return %NULL if the crypto library does not support 300189251Ssam * X.509 parsing. In that case, internal code will be used to parse the 301189251Ssam * certificate and public key is imported using crypto_public_key_import(). 302189251Ssam * 303189251Ssam * This function is only used with internal TLSv1 implementation 304189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 305189251Ssam * to implement this. 306189251Ssam */ 307189251Ssamstruct crypto_public_key * crypto_public_key_from_cert(const u8 *buf, 308189251Ssam size_t len); 309189251Ssam 310189251Ssam/** 311189251Ssam * crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5) 312189251Ssam * @key: Public key 313189251Ssam * @in: Plaintext buffer 314189251Ssam * @inlen: Length of plaintext buffer in bytes 315189251Ssam * @out: Output buffer for encrypted data 316189251Ssam * @outlen: Length of output buffer in bytes; set to used length on success 317189251Ssam * Returns: 0 on success, -1 on failure 318189251Ssam * 319189251Ssam * This function is only used with internal TLSv1 implementation 320189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 321189251Ssam * to implement this. 322189251Ssam */ 323189251Ssamint __must_check crypto_public_key_encrypt_pkcs1_v15( 324189251Ssam struct crypto_public_key *key, const u8 *in, size_t inlen, 325189251Ssam u8 *out, size_t *outlen); 326189251Ssam 327189251Ssam/** 328189251Ssam * crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5) 329189251Ssam * @key: Private key 330189251Ssam * @in: Encrypted buffer 331189251Ssam * @inlen: Length of encrypted buffer in bytes 332189251Ssam * @out: Output buffer for encrypted data 333189251Ssam * @outlen: Length of output buffer in bytes; set to used length on success 334189251Ssam * Returns: 0 on success, -1 on failure 335189251Ssam * 336189251Ssam * This function is only used with internal TLSv1 implementation 337189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 338189251Ssam * to implement this. 339189251Ssam */ 340189251Ssamint __must_check crypto_private_key_decrypt_pkcs1_v15( 341189251Ssam struct crypto_private_key *key, const u8 *in, size_t inlen, 342189251Ssam u8 *out, size_t *outlen); 343189251Ssam 344189251Ssam/** 345189251Ssam * crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1) 346189251Ssam * @key: Private key from crypto_private_key_import() 347189251Ssam * @in: Plaintext buffer 348189251Ssam * @inlen: Length of plaintext buffer in bytes 349189251Ssam * @out: Output buffer for encrypted (signed) data 350189251Ssam * @outlen: Length of output buffer in bytes; set to used length on success 351189251Ssam * Returns: 0 on success, -1 on failure 352189251Ssam * 353189251Ssam * This function is only used with internal TLSv1 implementation 354189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 355189251Ssam * to implement this. 356189251Ssam */ 357189251Ssamint __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key, 358189251Ssam const u8 *in, size_t inlen, 359189251Ssam u8 *out, size_t *outlen); 360189251Ssam 361189251Ssam/** 362189251Ssam * crypto_public_key_free - Free public key 363189251Ssam * @key: Public key 364189251Ssam * 365189251Ssam * This function is only used with internal TLSv1 implementation 366189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 367189251Ssam * to implement this. 368189251Ssam */ 369189251Ssamvoid crypto_public_key_free(struct crypto_public_key *key); 370189251Ssam 371189251Ssam/** 372189251Ssam * crypto_private_key_free - Free private key 373189251Ssam * @key: Private key from crypto_private_key_import() 374189251Ssam * 375189251Ssam * This function is only used with internal TLSv1 implementation 376189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 377189251Ssam * to implement this. 378189251Ssam */ 379189251Ssamvoid crypto_private_key_free(struct crypto_private_key *key); 380189251Ssam 381189251Ssam/** 382189251Ssam * crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature 383189251Ssam * @key: Public key 384189251Ssam * @crypt: Encrypted signature data (using the private key) 385189251Ssam * @crypt_len: Encrypted signature data length 386189251Ssam * @plain: Buffer for plaintext (at least crypt_len bytes) 387189251Ssam * @plain_len: Plaintext length (max buffer size on input, real len on output); 388189251Ssam * Returns: 0 on success, -1 on failure 389189251Ssam */ 390189251Ssamint __must_check crypto_public_key_decrypt_pkcs1( 391189251Ssam struct crypto_public_key *key, const u8 *crypt, size_t crypt_len, 392189251Ssam u8 *plain, size_t *plain_len); 393189251Ssam 394189251Ssam/** 395189251Ssam * crypto_global_init - Initialize crypto wrapper 396189251Ssam * 397189251Ssam * This function is only used with internal TLSv1 implementation 398189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 399189251Ssam * to implement this. 400189251Ssam */ 401189251Ssamint __must_check crypto_global_init(void); 402189251Ssam 403189251Ssam/** 404189251Ssam * crypto_global_deinit - Deinitialize crypto wrapper 405189251Ssam * 406189251Ssam * This function is only used with internal TLSv1 implementation 407189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 408189251Ssam * to implement this. 409189251Ssam */ 410189251Ssamvoid crypto_global_deinit(void); 411189251Ssam 412189251Ssam/** 413189251Ssam * crypto_mod_exp - Modular exponentiation of large integers 414189251Ssam * @base: Base integer (big endian byte array) 415189251Ssam * @base_len: Length of base integer in bytes 416189251Ssam * @power: Power integer (big endian byte array) 417189251Ssam * @power_len: Length of power integer in bytes 418189251Ssam * @modulus: Modulus integer (big endian byte array) 419189251Ssam * @modulus_len: Length of modulus integer in bytes 420189251Ssam * @result: Buffer for the result 421189251Ssam * @result_len: Result length (max buffer size on input, real len on output) 422189251Ssam * Returns: 0 on success, -1 on failure 423189251Ssam * 424189251Ssam * This function calculates result = base ^ power mod modulus. modules_len is 425189251Ssam * used as the maximum size of modulus buffer. It is set to the used size on 426189251Ssam * success. 427189251Ssam * 428189251Ssam * This function is only used with internal TLSv1 implementation 429189251Ssam * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need 430189251Ssam * to implement this. 431189251Ssam */ 432189251Ssamint __must_check crypto_mod_exp(const u8 *base, size_t base_len, 433189251Ssam const u8 *power, size_t power_len, 434189251Ssam const u8 *modulus, size_t modulus_len, 435189251Ssam u8 *result, size_t *result_len); 436189251Ssam 437214734Srpaulo/** 438214734Srpaulo * rc4_skip - XOR RC4 stream to given data with skip-stream-start 439214734Srpaulo * @key: RC4 key 440214734Srpaulo * @keylen: RC4 key length 441214734Srpaulo * @skip: number of bytes to skip from the beginning of the RC4 stream 442214734Srpaulo * @data: data to be XOR'ed with RC4 stream 443214734Srpaulo * @data_len: buf length 444214734Srpaulo * Returns: 0 on success, -1 on failure 445214734Srpaulo * 446214734Srpaulo * Generate RC4 pseudo random stream for the given key, skip beginning of the 447214734Srpaulo * stream, and XOR the end result with the data buffer to perform RC4 448214734Srpaulo * encryption/decryption. 449214734Srpaulo */ 450214734Srpauloint rc4_skip(const u8 *key, size_t keylen, size_t skip, 451214734Srpaulo u8 *data, size_t data_len); 452214734Srpaulo 453252726Srpaulo/** 454252726Srpaulo * crypto_get_random - Generate cryptographically strong pseudy-random bytes 455252726Srpaulo * @buf: Buffer for data 456252726Srpaulo * @len: Number of bytes to generate 457252726Srpaulo * Returns: 0 on success, -1 on failure 458252726Srpaulo * 459252726Srpaulo * If the PRNG does not have enough entropy to ensure unpredictable byte 460252726Srpaulo * sequence, this functions must return -1. 461252726Srpaulo */ 462252726Srpauloint crypto_get_random(void *buf, size_t len); 463252726Srpaulo 464281806Srpaulo 465281806Srpaulo/** 466281806Srpaulo * struct crypto_bignum - bignum 467281806Srpaulo * 468281806Srpaulo * Internal data structure for bignum implementation. The contents is specific 469281806Srpaulo * to the used crypto library. 470281806Srpaulo */ 471281806Srpaulostruct crypto_bignum; 472281806Srpaulo 473281806Srpaulo/** 474281806Srpaulo * crypto_bignum_init - Allocate memory for bignum 475281806Srpaulo * Returns: Pointer to allocated bignum or %NULL on failure 476281806Srpaulo */ 477281806Srpaulostruct crypto_bignum * crypto_bignum_init(void); 478281806Srpaulo 479281806Srpaulo/** 480281806Srpaulo * crypto_bignum_init_set - Allocate memory for bignum and set the value 481281806Srpaulo * @buf: Buffer with unsigned binary value 482281806Srpaulo * @len: Length of buf in octets 483281806Srpaulo * Returns: Pointer to allocated bignum or %NULL on failure 484281806Srpaulo */ 485281806Srpaulostruct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len); 486281806Srpaulo 487281806Srpaulo/** 488281806Srpaulo * crypto_bignum_deinit - Free bignum 489281806Srpaulo * @n: Bignum from crypto_bignum_init() or crypto_bignum_init_set() 490281806Srpaulo * @clear: Whether to clear the value from memory 491281806Srpaulo */ 492281806Srpaulovoid crypto_bignum_deinit(struct crypto_bignum *n, int clear); 493281806Srpaulo 494281806Srpaulo/** 495281806Srpaulo * crypto_bignum_to_bin - Set binary buffer to unsigned bignum 496281806Srpaulo * @a: Bignum 497281806Srpaulo * @buf: Buffer for the binary number 498281806Srpaulo * @len: Length of @buf in octets 499281806Srpaulo * @padlen: Length in octets to pad the result to or 0 to indicate no padding 500281806Srpaulo * Returns: Number of octets written on success, -1 on failure 501281806Srpaulo */ 502281806Srpauloint crypto_bignum_to_bin(const struct crypto_bignum *a, 503281806Srpaulo u8 *buf, size_t buflen, size_t padlen); 504281806Srpaulo 505281806Srpaulo/** 506281806Srpaulo * crypto_bignum_add - c = a + b 507281806Srpaulo * @a: Bignum 508281806Srpaulo * @b: Bignum 509281806Srpaulo * @c: Bignum; used to store the result of a + b 510281806Srpaulo * Returns: 0 on success, -1 on failure 511281806Srpaulo */ 512281806Srpauloint crypto_bignum_add(const struct crypto_bignum *a, 513281806Srpaulo const struct crypto_bignum *b, 514281806Srpaulo struct crypto_bignum *c); 515281806Srpaulo 516281806Srpaulo/** 517281806Srpaulo * crypto_bignum_mod - c = a % b 518281806Srpaulo * @a: Bignum 519281806Srpaulo * @b: Bignum 520281806Srpaulo * @c: Bignum; used to store the result of a % b 521281806Srpaulo * Returns: 0 on success, -1 on failure 522281806Srpaulo */ 523281806Srpauloint crypto_bignum_mod(const struct crypto_bignum *a, 524281806Srpaulo const struct crypto_bignum *b, 525281806Srpaulo struct crypto_bignum *c); 526281806Srpaulo 527281806Srpaulo/** 528281806Srpaulo * crypto_bignum_exptmod - Modular exponentiation: d = a^b (mod c) 529281806Srpaulo * @a: Bignum; base 530281806Srpaulo * @b: Bignum; exponent 531281806Srpaulo * @c: Bignum; modulus 532281806Srpaulo * @d: Bignum; used to store the result of a^b (mod c) 533281806Srpaulo * Returns: 0 on success, -1 on failure 534281806Srpaulo */ 535281806Srpauloint crypto_bignum_exptmod(const struct crypto_bignum *a, 536281806Srpaulo const struct crypto_bignum *b, 537281806Srpaulo const struct crypto_bignum *c, 538281806Srpaulo struct crypto_bignum *d); 539281806Srpaulo 540281806Srpaulo/** 541281806Srpaulo * crypto_bignum_inverse - Inverse a bignum so that a * c = 1 (mod b) 542281806Srpaulo * @a: Bignum 543281806Srpaulo * @b: Bignum 544281806Srpaulo * @c: Bignum; used to store the result 545281806Srpaulo * Returns: 0 on success, -1 on failure 546281806Srpaulo */ 547281806Srpauloint crypto_bignum_inverse(const struct crypto_bignum *a, 548281806Srpaulo const struct crypto_bignum *b, 549281806Srpaulo struct crypto_bignum *c); 550281806Srpaulo 551281806Srpaulo/** 552281806Srpaulo * crypto_bignum_sub - c = a - b 553281806Srpaulo * @a: Bignum 554281806Srpaulo * @b: Bignum 555281806Srpaulo * @c: Bignum; used to store the result of a - b 556281806Srpaulo * Returns: 0 on success, -1 on failure 557281806Srpaulo */ 558281806Srpauloint crypto_bignum_sub(const struct crypto_bignum *a, 559281806Srpaulo const struct crypto_bignum *b, 560281806Srpaulo struct crypto_bignum *c); 561281806Srpaulo 562281806Srpaulo/** 563281806Srpaulo * crypto_bignum_div - c = a / b 564281806Srpaulo * @a: Bignum 565281806Srpaulo * @b: Bignum 566281806Srpaulo * @c: Bignum; used to store the result of a / b 567281806Srpaulo * Returns: 0 on success, -1 on failure 568281806Srpaulo */ 569281806Srpauloint crypto_bignum_div(const struct crypto_bignum *a, 570281806Srpaulo const struct crypto_bignum *b, 571281806Srpaulo struct crypto_bignum *c); 572281806Srpaulo 573281806Srpaulo/** 574281806Srpaulo * crypto_bignum_mulmod - d = a * b (mod c) 575281806Srpaulo * @a: Bignum 576281806Srpaulo * @b: Bignum 577281806Srpaulo * @c: Bignum 578281806Srpaulo * @d: Bignum; used to store the result of (a * b) % c 579281806Srpaulo * Returns: 0 on success, -1 on failure 580281806Srpaulo */ 581281806Srpauloint crypto_bignum_mulmod(const struct crypto_bignum *a, 582281806Srpaulo const struct crypto_bignum *b, 583281806Srpaulo const struct crypto_bignum *c, 584281806Srpaulo struct crypto_bignum *d); 585281806Srpaulo 586281806Srpaulo/** 587281806Srpaulo * crypto_bignum_cmp - Compare two bignums 588281806Srpaulo * @a: Bignum 589281806Srpaulo * @b: Bignum 590281806Srpaulo * Returns: -1 if a < b, 0 if a == b, or 1 if a > b 591281806Srpaulo */ 592281806Srpauloint crypto_bignum_cmp(const struct crypto_bignum *a, 593281806Srpaulo const struct crypto_bignum *b); 594281806Srpaulo 595281806Srpaulo/** 596281806Srpaulo * crypto_bignum_bits - Get size of a bignum in bits 597281806Srpaulo * @a: Bignum 598281806Srpaulo * Returns: Number of bits in the bignum 599281806Srpaulo */ 600281806Srpauloint crypto_bignum_bits(const struct crypto_bignum *a); 601281806Srpaulo 602281806Srpaulo/** 603281806Srpaulo * crypto_bignum_is_zero - Is the given bignum zero 604281806Srpaulo * @a: Bignum 605281806Srpaulo * Returns: 1 if @a is zero or 0 if not 606281806Srpaulo */ 607281806Srpauloint crypto_bignum_is_zero(const struct crypto_bignum *a); 608281806Srpaulo 609281806Srpaulo/** 610281806Srpaulo * crypto_bignum_is_one - Is the given bignum one 611281806Srpaulo * @a: Bignum 612281806Srpaulo * Returns: 1 if @a is one or 0 if not 613281806Srpaulo */ 614281806Srpauloint crypto_bignum_is_one(const struct crypto_bignum *a); 615281806Srpaulo 616281806Srpaulo/** 617289549Srpaulo * crypto_bignum_legendre - Compute the Legendre symbol (a/p) 618289549Srpaulo * @a: Bignum 619289549Srpaulo * @p: Bignum 620289549Srpaulo * Returns: Legendre symbol -1,0,1 on success; -2 on calculation failure 621289549Srpaulo */ 622289549Srpauloint crypto_bignum_legendre(const struct crypto_bignum *a, 623289549Srpaulo const struct crypto_bignum *p); 624289549Srpaulo 625289549Srpaulo/** 626281806Srpaulo * struct crypto_ec - Elliptic curve context 627281806Srpaulo * 628281806Srpaulo * Internal data structure for EC implementation. The contents is specific 629281806Srpaulo * to the used crypto library. 630281806Srpaulo */ 631281806Srpaulostruct crypto_ec; 632281806Srpaulo 633281806Srpaulo/** 634281806Srpaulo * crypto_ec_init - Initialize elliptic curve context 635281806Srpaulo * @group: Identifying number for the ECC group (IANA "Group Description" 636281806Srpaulo * attribute registrty for RFC 2409) 637281806Srpaulo * Returns: Pointer to EC context or %NULL on failure 638281806Srpaulo */ 639281806Srpaulostruct crypto_ec * crypto_ec_init(int group); 640281806Srpaulo 641281806Srpaulo/** 642281806Srpaulo * crypto_ec_deinit - Deinitialize elliptic curve context 643281806Srpaulo * @e: EC context from crypto_ec_init() 644281806Srpaulo */ 645281806Srpaulovoid crypto_ec_deinit(struct crypto_ec *e); 646281806Srpaulo 647281806Srpaulo/** 648281806Srpaulo * crypto_ec_prime_len - Get length of the prime in octets 649281806Srpaulo * @e: EC context from crypto_ec_init() 650281806Srpaulo * Returns: Length of the prime defining the group 651281806Srpaulo */ 652281806Srpaulosize_t crypto_ec_prime_len(struct crypto_ec *e); 653281806Srpaulo 654281806Srpaulo/** 655281806Srpaulo * crypto_ec_prime_len_bits - Get length of the prime in bits 656281806Srpaulo * @e: EC context from crypto_ec_init() 657281806Srpaulo * Returns: Length of the prime defining the group in bits 658281806Srpaulo */ 659281806Srpaulosize_t crypto_ec_prime_len_bits(struct crypto_ec *e); 660281806Srpaulo 661281806Srpaulo/** 662281806Srpaulo * crypto_ec_get_prime - Get prime defining an EC group 663281806Srpaulo * @e: EC context from crypto_ec_init() 664281806Srpaulo * Returns: Prime (bignum) defining the group 665281806Srpaulo */ 666281806Srpauloconst struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e); 667281806Srpaulo 668281806Srpaulo/** 669281806Srpaulo * crypto_ec_get_order - Get order of an EC group 670281806Srpaulo * @e: EC context from crypto_ec_init() 671281806Srpaulo * Returns: Order (bignum) of the group 672281806Srpaulo */ 673281806Srpauloconst struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e); 674281806Srpaulo 675281806Srpaulo/** 676281806Srpaulo * struct crypto_ec_point - Elliptic curve point 677281806Srpaulo * 678281806Srpaulo * Internal data structure for EC implementation to represent a point. The 679281806Srpaulo * contents is specific to the used crypto library. 680281806Srpaulo */ 681281806Srpaulostruct crypto_ec_point; 682281806Srpaulo 683281806Srpaulo/** 684281806Srpaulo * crypto_ec_point_init - Initialize data for an EC point 685281806Srpaulo * @e: EC context from crypto_ec_init() 686281806Srpaulo * Returns: Pointer to EC point data or %NULL on failure 687281806Srpaulo */ 688281806Srpaulostruct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e); 689281806Srpaulo 690281806Srpaulo/** 691281806Srpaulo * crypto_ec_point_deinit - Deinitialize EC point data 692281806Srpaulo * @p: EC point data from crypto_ec_point_init() 693281806Srpaulo * @clear: Whether to clear the EC point value from memory 694281806Srpaulo */ 695281806Srpaulovoid crypto_ec_point_deinit(struct crypto_ec_point *p, int clear); 696281806Srpaulo 697281806Srpaulo/** 698281806Srpaulo * crypto_ec_point_to_bin - Write EC point value as binary data 699281806Srpaulo * @e: EC context from crypto_ec_init() 700281806Srpaulo * @p: EC point data from crypto_ec_point_init() 701281806Srpaulo * @x: Buffer for writing the binary data for x coordinate or %NULL if not used 702281806Srpaulo * @y: Buffer for writing the binary data for y coordinate or %NULL if not used 703281806Srpaulo * Returns: 0 on success, -1 on failure 704281806Srpaulo * 705281806Srpaulo * This function can be used to write an EC point as binary data in a format 706281806Srpaulo * that has the x and y coordinates in big endian byte order fields padded to 707281806Srpaulo * the length of the prime defining the group. 708281806Srpaulo */ 709281806Srpauloint crypto_ec_point_to_bin(struct crypto_ec *e, 710281806Srpaulo const struct crypto_ec_point *point, u8 *x, u8 *y); 711281806Srpaulo 712281806Srpaulo/** 713281806Srpaulo * crypto_ec_point_from_bin - Create EC point from binary data 714281806Srpaulo * @e: EC context from crypto_ec_init() 715281806Srpaulo * @val: Binary data to read the EC point from 716281806Srpaulo * Returns: Pointer to EC point data or %NULL on failure 717281806Srpaulo * 718281806Srpaulo * This function readers x and y coordinates of the EC point from the provided 719281806Srpaulo * buffer assuming the values are in big endian byte order with fields padded to 720281806Srpaulo * the length of the prime defining the group. 721281806Srpaulo */ 722281806Srpaulostruct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e, 723281806Srpaulo const u8 *val); 724281806Srpaulo 725281806Srpaulo/** 726281806Srpaulo * crypto_bignum_add - c = a + b 727281806Srpaulo * @e: EC context from crypto_ec_init() 728281806Srpaulo * @a: Bignum 729281806Srpaulo * @b: Bignum 730281806Srpaulo * @c: Bignum; used to store the result of a + b 731281806Srpaulo * Returns: 0 on success, -1 on failure 732281806Srpaulo */ 733281806Srpauloint crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a, 734281806Srpaulo const struct crypto_ec_point *b, 735281806Srpaulo struct crypto_ec_point *c); 736281806Srpaulo 737281806Srpaulo/** 738281806Srpaulo * crypto_bignum_mul - res = b * p 739281806Srpaulo * @e: EC context from crypto_ec_init() 740281806Srpaulo * @p: EC point 741281806Srpaulo * @b: Bignum 742281806Srpaulo * @res: EC point; used to store the result of b * p 743281806Srpaulo * Returns: 0 on success, -1 on failure 744281806Srpaulo */ 745281806Srpauloint crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p, 746281806Srpaulo const struct crypto_bignum *b, 747281806Srpaulo struct crypto_ec_point *res); 748281806Srpaulo 749281806Srpaulo/** 750281806Srpaulo * crypto_ec_point_invert - Compute inverse of an EC point 751281806Srpaulo * @e: EC context from crypto_ec_init() 752281806Srpaulo * @p: EC point to invert (and result of the operation) 753281806Srpaulo * Returns: 0 on success, -1 on failure 754281806Srpaulo */ 755281806Srpauloint crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p); 756281806Srpaulo 757281806Srpaulo/** 758281806Srpaulo * crypto_ec_point_solve_y_coord - Solve y coordinate for an x coordinate 759281806Srpaulo * @e: EC context from crypto_ec_init() 760281806Srpaulo * @p: EC point to use for the returning the result 761281806Srpaulo * @x: x coordinate 762281806Srpaulo * @y_bit: y-bit (0 or 1) for selecting the y value to use 763281806Srpaulo * Returns: 0 on success, -1 on failure 764281806Srpaulo */ 765281806Srpauloint crypto_ec_point_solve_y_coord(struct crypto_ec *e, 766281806Srpaulo struct crypto_ec_point *p, 767281806Srpaulo const struct crypto_bignum *x, int y_bit); 768281806Srpaulo 769281806Srpaulo/** 770289549Srpaulo * crypto_ec_point_compute_y_sqr - Compute y^2 = x^3 + ax + b 771289549Srpaulo * @e: EC context from crypto_ec_init() 772289549Srpaulo * @x: x coordinate 773289549Srpaulo * Returns: y^2 on success, %NULL failure 774289549Srpaulo */ 775289549Srpaulostruct crypto_bignum * 776289549Srpaulocrypto_ec_point_compute_y_sqr(struct crypto_ec *e, 777289549Srpaulo const struct crypto_bignum *x); 778289549Srpaulo 779289549Srpaulo/** 780281806Srpaulo * crypto_ec_point_is_at_infinity - Check whether EC point is neutral element 781281806Srpaulo * @e: EC context from crypto_ec_init() 782281806Srpaulo * @p: EC point 783281806Srpaulo * Returns: 1 if the specified EC point is the neutral element of the group or 784281806Srpaulo * 0 if not 785281806Srpaulo */ 786281806Srpauloint crypto_ec_point_is_at_infinity(struct crypto_ec *e, 787281806Srpaulo const struct crypto_ec_point *p); 788281806Srpaulo 789281806Srpaulo/** 790281806Srpaulo * crypto_ec_point_is_on_curve - Check whether EC point is on curve 791281806Srpaulo * @e: EC context from crypto_ec_init() 792281806Srpaulo * @p: EC point 793281806Srpaulo * Returns: 1 if the specified EC point is on the curve or 0 if not 794281806Srpaulo */ 795281806Srpauloint crypto_ec_point_is_on_curve(struct crypto_ec *e, 796281806Srpaulo const struct crypto_ec_point *p); 797281806Srpaulo 798289549Srpaulo/** 799289549Srpaulo * crypto_ec_point_cmp - Compare two EC points 800289549Srpaulo * @e: EC context from crypto_ec_init() 801289549Srpaulo * @a: EC point 802289549Srpaulo * @b: EC point 803289549Srpaulo * Returns: 0 on equal, non-zero otherwise 804289549Srpaulo */ 805289549Srpauloint crypto_ec_point_cmp(const struct crypto_ec *e, 806289549Srpaulo const struct crypto_ec_point *a, 807289549Srpaulo const struct crypto_ec_point *b); 808289549Srpaulo 809189251Ssam#endif /* CRYPTO_H */ 810