1 /*
2  * Wrapper functions for crypto libraries
3  * Copyright (c) 2004-2017, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  *
8  * This file defines the cryptographic functions that need to be implemented
9  * for wpa_supplicant and hostapd. When TLS is not used, internal
10  * implementation of MD5, SHA1, and AES is used and no external libraries are
11  * required. When TLS is enabled (e.g., by enabling EAP-TLS or EAP-PEAP), the
12  * crypto library used by the TLS implementation is expected to be used for
13  * non-TLS needs, too, in order to save space by not implementing these
14  * functions twice.
15  *
16  * Wrapper code for using each crypto library is in its own file (crypto*.c)
17  * and one of these files is build and linked in to provide the functions
18  * defined here.
19  */
20 
21 #ifndef CRYPTO_H
22 #define CRYPTO_H
23 
24 /**
25  * md4_vector - MD4 hash for data vector
26  * @num_elem: Number of elements in the data vector
27  * @addr: Pointers to the data areas
28  * @len: Lengths of the data blocks
29  * @mac: Buffer for the hash
30  * Returns: 0 on success, -1 on failure
31  */
32 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
33 
34 /**
35  * md5_vector - MD5 hash for data vector
36  * @num_elem: Number of elements in the data vector
37  * @addr: Pointers to the data areas
38  * @len: Lengths of the data blocks
39  * @mac: Buffer for the hash
40  * Returns: 0 on success, -1 on failure
41  */
42 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
43 
44 
45 /**
46  * sha1_vector - SHA-1 hash for data vector
47  * @num_elem: Number of elements in the data vector
48  * @addr: Pointers to the data areas
49  * @len: Lengths of the data blocks
50  * @mac: Buffer for the hash
51  * Returns: 0 on success, -1 on failure
52  */
53 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
54 		u8 *mac);
55 
56 /**
57  * fips186_2-prf - NIST FIPS Publication 186-2 change notice 1 PRF
58  * @seed: Seed/key for the PRF
59  * @seed_len: Seed length in bytes
60  * @x: Buffer for PRF output
61  * @xlen: Output length in bytes
62  * Returns: 0 on success, -1 on failure
63  *
64  * This function implements random number generation specified in NIST FIPS
65  * Publication 186-2 for EAP-SIM. This PRF uses a function that is similar to
66  * SHA-1, but has different message padding.
67  */
68 int __must_check fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x,
69 			       size_t xlen);
70 
71 /**
72  * sha256_vector - SHA256 hash for data vector
73  * @num_elem: Number of elements in the data vector
74  * @addr: Pointers to the data areas
75  * @len: Lengths of the data blocks
76  * @mac: Buffer for the hash
77  * Returns: 0 on success, -1 on failure
78  */
79 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
80 		  u8 *mac);
81 
82 /**
83  * sha384_vector - SHA384 hash for data vector
84  * @num_elem: Number of elements in the data vector
85  * @addr: Pointers to the data areas
86  * @len: Lengths of the data blocks
87  * @mac: Buffer for the hash
88  * Returns: 0 on success, -1 on failure
89  */
90 int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
91 		  u8 *mac);
92 
93 /**
94  * sha512_vector - SHA512 hash for data vector
95  * @num_elem: Number of elements in the data vector
96  * @addr: Pointers to the data areas
97  * @len: Lengths of the data blocks
98  * @mac: Buffer for the hash
99  * Returns: 0 on success, -1 on failure
100  */
101 int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
102 		  u8 *mac);
103 
104 /**
105  * des_encrypt - Encrypt one block with DES
106  * @clear: 8 octets (in)
107  * @key: 7 octets (in) (no parity bits included)
108  * @cypher: 8 octets (out)
109  * Returns: 0 on success, -1 on failure
110  */
111 int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
112 
113 /**
114  * aes_encrypt_init - Initialize AES for encryption
115  * @key: Encryption key
116  * @len: Key length in bytes (usually 16, i.e., 128 bits)
117  * Returns: Pointer to context data or %NULL on failure
118  */
119 void * aes_encrypt_init(const u8 *key, size_t len);
120 
121 /**
122  * aes_encrypt - Encrypt one AES block
123  * @ctx: Context pointer from aes_encrypt_init()
124  * @plain: Plaintext data to be encrypted (16 bytes)
125  * @crypt: Buffer for the encrypted data (16 bytes)
126  * Returns: 0 on success, -1 on failure
127  */
128 int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
129 
130 /**
131  * aes_encrypt_deinit - Deinitialize AES encryption
132  * @ctx: Context pointer from aes_encrypt_init()
133  */
134 void aes_encrypt_deinit(void *ctx);
135 
136 /**
137  * aes_decrypt_init - Initialize AES for decryption
138  * @key: Decryption key
139  * @len: Key length in bytes (usually 16, i.e., 128 bits)
140  * Returns: Pointer to context data or %NULL on failure
141  */
142 void * aes_decrypt_init(const u8 *key, size_t len);
143 
144 /**
145  * aes_decrypt - Decrypt one AES block
146  * @ctx: Context pointer from aes_encrypt_init()
147  * @crypt: Encrypted data (16 bytes)
148  * @plain: Buffer for the decrypted data (16 bytes)
149  * Returns: 0 on success, -1 on failure
150  */
151 int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
152 
153 /**
154  * aes_decrypt_deinit - Deinitialize AES decryption
155  * @ctx: Context pointer from aes_encrypt_init()
156  */
157 void aes_decrypt_deinit(void *ctx);
158 
159 
160 enum crypto_hash_alg {
161 	CRYPTO_HASH_ALG_MD5, CRYPTO_HASH_ALG_SHA1,
162 	CRYPTO_HASH_ALG_HMAC_MD5, CRYPTO_HASH_ALG_HMAC_SHA1,
163 	CRYPTO_HASH_ALG_SHA256, CRYPTO_HASH_ALG_HMAC_SHA256,
164 	CRYPTO_HASH_ALG_SHA384, CRYPTO_HASH_ALG_SHA512
165 };
166 
167 struct crypto_hash;
168 
169 /**
170  * crypto_hash_init - Initialize hash/HMAC function
171  * @alg: Hash algorithm
172  * @key: Key for keyed hash (e.g., HMAC) or %NULL if not needed
173  * @key_len: Length of the key in bytes
174  * Returns: Pointer to hash context to use with other hash functions or %NULL
175  * on failure
176  *
177  * This function is only used with internal TLSv1 implementation
178  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
179  * to implement this.
180  */
181 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
182 				      size_t key_len);
183 
184 /**
185  * crypto_hash_update - Add data to hash calculation
186  * @ctx: Context pointer from crypto_hash_init()
187  * @data: Data buffer to add
188  * @len: Length of the buffer
189  *
190  * This function is only used with internal TLSv1 implementation
191  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
192  * to implement this.
193  */
194 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len);
195 
196 /**
197  * crypto_hash_finish - Complete hash calculation
198  * @ctx: Context pointer from crypto_hash_init()
199  * @hash: Buffer for hash value or %NULL if caller is just freeing the hash
200  * context
201  * @len: Pointer to length of the buffer or %NULL if caller is just freeing the
202  * hash context; on return, this is set to the actual length of the hash value
203  * Returns: 0 on success, -1 if buffer is too small (len set to needed length),
204  * or -2 on other failures (including failed crypto_hash_update() operations)
205  *
206  * This function calculates the hash value and frees the context buffer that
207  * was used for hash calculation.
208  *
209  * This function is only used with internal TLSv1 implementation
210  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
211  * to implement this.
212  */
213 int crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len);
214 
215 
216 enum crypto_cipher_alg {
217 	CRYPTO_CIPHER_NULL = 0, CRYPTO_CIPHER_ALG_AES, CRYPTO_CIPHER_ALG_3DES,
218 	CRYPTO_CIPHER_ALG_DES, CRYPTO_CIPHER_ALG_RC2, CRYPTO_CIPHER_ALG_RC4
219 };
220 
221 struct crypto_cipher;
222 
223 /**
224  * crypto_cipher_init - Initialize block/stream cipher function
225  * @alg: Cipher algorithm
226  * @iv: Initialization vector for block ciphers or %NULL for stream ciphers
227  * @key: Cipher key
228  * @key_len: Length of key in bytes
229  * Returns: Pointer to cipher context to use with other cipher functions or
230  * %NULL on failure
231  *
232  * This function is only used with internal TLSv1 implementation
233  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
234  * to implement this.
235  */
236 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
237 					  const u8 *iv, const u8 *key,
238 					  size_t key_len);
239 
240 /**
241  * crypto_cipher_encrypt - Cipher encrypt
242  * @ctx: Context pointer from crypto_cipher_init()
243  * @plain: Plaintext to cipher
244  * @crypt: Resulting ciphertext
245  * @len: Length of the plaintext
246  * Returns: 0 on success, -1 on failure
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  */
252 int __must_check crypto_cipher_encrypt(struct crypto_cipher *ctx,
253 				       const u8 *plain, u8 *crypt, size_t len);
254 
255 /**
256  * crypto_cipher_decrypt - Cipher decrypt
257  * @ctx: Context pointer from crypto_cipher_init()
258  * @crypt: Ciphertext to decrypt
259  * @plain: Resulting plaintext
260  * @len: Length of the cipher text
261  * Returns: 0 on success, -1 on failure
262  *
263  * This function is only used with internal TLSv1 implementation
264  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
265  * to implement this.
266  */
267 int __must_check crypto_cipher_decrypt(struct crypto_cipher *ctx,
268 				       const u8 *crypt, u8 *plain, size_t len);
269 
270 /**
271  * crypto_cipher_decrypt - Free cipher context
272  * @ctx: Context pointer from crypto_cipher_init()
273  *
274  * This function is only used with internal TLSv1 implementation
275  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
276  * to implement this.
277  */
278 void crypto_cipher_deinit(struct crypto_cipher *ctx);
279 
280 
281 struct crypto_public_key;
282 struct crypto_private_key;
283 
284 /**
285  * crypto_public_key_import - Import an RSA public key
286  * @key: Key buffer (DER encoded RSA public key)
287  * @len: Key buffer length in bytes
288  * Returns: Pointer to the public key or %NULL on failure
289  *
290  * This function can just return %NULL if the crypto library supports X.509
291  * parsing. In that case, crypto_public_key_from_cert() is used to import the
292  * public key from a certificate.
293  *
294  * This function is only used with internal TLSv1 implementation
295  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
296  * to implement this.
297  */
298 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len);
299 
300 struct crypto_public_key *
301 crypto_public_key_import_parts(const u8 *n, size_t n_len,
302 			       const u8 *e, size_t e_len);
303 
304 /**
305  * crypto_private_key_import - Import an RSA private key
306  * @key: Key buffer (DER encoded RSA private key)
307  * @len: Key buffer length in bytes
308  * @passwd: Key encryption password or %NULL if key is not encrypted
309  * Returns: Pointer to the private key or %NULL on failure
310  *
311  * This function is only used with internal TLSv1 implementation
312  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
313  * to implement this.
314  */
315 struct crypto_private_key * crypto_private_key_import(const u8 *key,
316 						      size_t len,
317 						      const char *passwd);
318 
319 /**
320  * crypto_public_key_from_cert - Import an RSA public key from a certificate
321  * @buf: DER encoded X.509 certificate
322  * @len: Certificate buffer length in bytes
323  * Returns: Pointer to public key or %NULL on failure
324  *
325  * This function can just return %NULL if the crypto library does not support
326  * X.509 parsing. In that case, internal code will be used to parse the
327  * certificate and public key is imported using crypto_public_key_import().
328  *
329  * This function is only used with internal TLSv1 implementation
330  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
331  * to implement this.
332  */
333 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
334 						       size_t len);
335 
336 /**
337  * crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5)
338  * @key: Public key
339  * @in: Plaintext buffer
340  * @inlen: Length of plaintext buffer in bytes
341  * @out: Output buffer for encrypted data
342  * @outlen: Length of output buffer in bytes; set to used length on success
343  * Returns: 0 on success, -1 on failure
344  *
345  * This function is only used with internal TLSv1 implementation
346  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
347  * to implement this.
348  */
349 int __must_check crypto_public_key_encrypt_pkcs1_v15(
350 	struct crypto_public_key *key, const u8 *in, size_t inlen,
351 	u8 *out, size_t *outlen);
352 
353 /**
354  * crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5)
355  * @key: Private key
356  * @in: Encrypted buffer
357  * @inlen: Length of encrypted buffer in bytes
358  * @out: Output buffer for encrypted data
359  * @outlen: Length of output buffer in bytes; set to used length on success
360  * Returns: 0 on success, -1 on failure
361  *
362  * This function is only used with internal TLSv1 implementation
363  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
364  * to implement this.
365  */
366 int __must_check crypto_private_key_decrypt_pkcs1_v15(
367 	struct crypto_private_key *key, const u8 *in, size_t inlen,
368 	u8 *out, size_t *outlen);
369 
370 /**
371  * crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1)
372  * @key: Private key from crypto_private_key_import()
373  * @in: Plaintext buffer
374  * @inlen: Length of plaintext buffer in bytes
375  * @out: Output buffer for encrypted (signed) data
376  * @outlen: Length of output buffer in bytes; set to used length on success
377  * Returns: 0 on success, -1 on failure
378  *
379  * This function is only used with internal TLSv1 implementation
380  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
381  * to implement this.
382  */
383 int __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
384 					       const u8 *in, size_t inlen,
385 					       u8 *out, size_t *outlen);
386 
387 /**
388  * crypto_public_key_free - Free public key
389  * @key: Public key
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  */
395 void crypto_public_key_free(struct crypto_public_key *key);
396 
397 /**
398  * crypto_private_key_free - Free private key
399  * @key: Private key from crypto_private_key_import()
400  *
401  * This function is only used with internal TLSv1 implementation
402  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
403  * to implement this.
404  */
405 void crypto_private_key_free(struct crypto_private_key *key);
406 
407 /**
408  * crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature
409  * @key: Public key
410  * @crypt: Encrypted signature data (using the private key)
411  * @crypt_len: Encrypted signature data length
412  * @plain: Buffer for plaintext (at least crypt_len bytes)
413  * @plain_len: Plaintext length (max buffer size on input, real len on output);
414  * Returns: 0 on success, -1 on failure
415  */
416 int __must_check crypto_public_key_decrypt_pkcs1(
417 	struct crypto_public_key *key, const u8 *crypt, size_t crypt_len,
418 	u8 *plain, size_t *plain_len);
419 
420 int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
421 		   u8 *pubkey);
422 int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
423 			    const u8 *order, size_t order_len,
424 			    const u8 *privkey, size_t privkey_len,
425 			    const u8 *pubkey, size_t pubkey_len,
426 			    u8 *secret, size_t *len);
427 
428 /**
429  * crypto_global_init - Initialize crypto wrapper
430  *
431  * This function is only used with internal TLSv1 implementation
432  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
433  * to implement this.
434  */
435 int __must_check crypto_global_init(void);
436 
437 /**
438  * crypto_global_deinit - Deinitialize crypto wrapper
439  *
440  * This function is only used with internal TLSv1 implementation
441  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
442  * to implement this.
443  */
444 void crypto_global_deinit(void);
445 
446 /**
447  * crypto_mod_exp - Modular exponentiation of large integers
448  * @base: Base integer (big endian byte array)
449  * @base_len: Length of base integer in bytes
450  * @power: Power integer (big endian byte array)
451  * @power_len: Length of power integer in bytes
452  * @modulus: Modulus integer (big endian byte array)
453  * @modulus_len: Length of modulus integer in bytes
454  * @result: Buffer for the result
455  * @result_len: Result length (max buffer size on input, real len on output)
456  * Returns: 0 on success, -1 on failure
457  *
458  * This function calculates result = base ^ power mod modulus. modules_len is
459  * used as the maximum size of modulus buffer. It is set to the used size on
460  * success.
461  *
462  * This function is only used with internal TLSv1 implementation
463  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
464  * to implement this.
465  */
466 int __must_check crypto_mod_exp(const u8 *base, size_t base_len,
467 				const u8 *power, size_t power_len,
468 				const u8 *modulus, size_t modulus_len,
469 				u8 *result, size_t *result_len);
470 
471 /**
472  * rc4_skip - XOR RC4 stream to given data with skip-stream-start
473  * @key: RC4 key
474  * @keylen: RC4 key length
475  * @skip: number of bytes to skip from the beginning of the RC4 stream
476  * @data: data to be XOR'ed with RC4 stream
477  * @data_len: buf length
478  * Returns: 0 on success, -1 on failure
479  *
480  * Generate RC4 pseudo random stream for the given key, skip beginning of the
481  * stream, and XOR the end result with the data buffer to perform RC4
482  * encryption/decryption.
483  */
484 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
485 	     u8 *data, size_t data_len);
486 
487 /**
488  * crypto_get_random - Generate cryptographically strong pseudo-random bytes
489  * @buf: Buffer for data
490  * @len: Number of bytes to generate
491  * Returns: 0 on success, -1 on failure
492  *
493  * If the PRNG does not have enough entropy to ensure unpredictable byte
494  * sequence, this functions must return -1.
495  */
496 int crypto_get_random(void *buf, size_t len);
497 
498 
499 /**
500  * struct crypto_bignum - bignum
501  *
502  * Internal data structure for bignum implementation. The contents is specific
503  * to the used crypto library.
504  */
505 struct crypto_bignum;
506 
507 /**
508  * struct crypto_key - key
509  *
510  * Internal data structure for ssl key. The contents is specific
511  * to the used crypto library.
512  */
513 struct crypto_key;
514 
515 /**
516  * crypto_bignum_init - Allocate memory for bignum
517  * Returns: Pointer to allocated bignum or %NULL on failure
518  */
519 struct crypto_bignum * crypto_bignum_init(void);
520 
521 /**
522  * crypto_bignum_init_set - Allocate memory for bignum and set the value
523  * @buf: Buffer with unsigned binary value
524  * @len: Length of buf in octets
525  * Returns: Pointer to allocated bignum or %NULL on failure
526  */
527 struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len);
528 
529 /**
530  * crypto_bignum_init_set - Allocate memory for bignum and set the value (uint)
531  * @val: Value to set
532  * Returns: Pointer to allocated bignum or %NULL on failure
533  */
534 struct crypto_bignum * crypto_bignum_init_uint(unsigned int val);
535 
536 /**
537  * crypto_bignum_deinit - Free bignum
538  * @n: Bignum from crypto_bignum_init() or crypto_bignum_init_set()
539  * @clear: Whether to clear the value from memory
540  */
541 void crypto_bignum_deinit(struct crypto_bignum *n, int clear);
542 
543 /**
544  * crypto_bignum_to_bin - Set binary buffer to unsigned bignum
545  * @a: Bignum
546  * @buf: Buffer for the binary number
547  * @len: Length of @buf in octets
548  * @padlen: Length in octets to pad the result to or 0 to indicate no padding
549  * Returns: Number of octets written on success, -1 on failure
550  */
551 int crypto_bignum_to_bin(const struct crypto_bignum *a,
552 			 u8 *buf, size_t buflen, size_t padlen);
553 
554 /**
555  * crypto_bignum_rand - Create a random number in range of modulus
556  * @r: Bignum; set to a random value
557  * @m: Bignum; modulus
558  * Returns: 0 on success, -1 on failure
559  */
560 int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m);
561 
562 /**
563  * crypto_bignum_add - c = a + b
564  * @a: Bignum
565  * @b: Bignum
566  * @c: Bignum; used to store the result of a + b
567  * Returns: 0 on success, -1 on failure
568  */
569 int crypto_bignum_add(const struct crypto_bignum *a,
570 		      const struct crypto_bignum *b,
571 		      struct crypto_bignum *c);
572 
573 /**
574  * crypto_bignum_mod - c = a % b
575  * @a: Bignum
576  * @b: Bignum
577  * @c: Bignum; used to store the result of a % b
578  * Returns: 0 on success, -1 on failure
579  */
580 int crypto_bignum_mod(const struct crypto_bignum *a,
581 		      const struct crypto_bignum *b,
582 		      struct crypto_bignum *c);
583 
584 /**
585  * crypto_bignum_exptmod - Modular exponentiation: d = a^b (mod c)
586  * @a: Bignum; base
587  * @b: Bignum; exponent
588  * @c: Bignum; modulus
589  * @d: Bignum; used to store the result of a^b (mod c)
590  * Returns: 0 on success, -1 on failure
591  */
592 int crypto_bignum_exptmod(const struct crypto_bignum *a,
593 			  const struct crypto_bignum *b,
594 			  const struct crypto_bignum *c,
595 			  struct crypto_bignum *d);
596 
597 /**
598  * crypto_bignum_inverse - Inverse a bignum so that a * c = 1 (mod b)
599  * @a: Bignum
600  * @b: Bignum
601  * @c: Bignum; used to store the result
602  * Returns: 0 on success, -1 on failure
603  */
604 int crypto_bignum_inverse(const struct crypto_bignum *a,
605 			  const struct crypto_bignum *b,
606 			  struct crypto_bignum *c);
607 
608 /**
609  * crypto_bignum_sub - c = a - b
610  * @a: Bignum
611  * @b: Bignum
612  * @c: Bignum; used to store the result of a - b
613  * Returns: 0 on success, -1 on failure
614  */
615 int crypto_bignum_sub(const struct crypto_bignum *a,
616 		      const struct crypto_bignum *b,
617 		      struct crypto_bignum *c);
618 
619 /**
620  * crypto_bignum_div - c = a / b
621  * @a: Bignum
622  * @b: Bignum
623  * @c: Bignum; used to store the result of a / b
624  * Returns: 0 on success, -1 on failure
625  */
626 int crypto_bignum_div(const struct crypto_bignum *a,
627 		      const struct crypto_bignum *b,
628 		      struct crypto_bignum *c);
629 
630 /**
631  * crypto_bignum_addmod - d = a + b (mod c)
632  * @a: Bignum
633  * @b: Bignum
634  * @c: Bignum
635  * @d: Bignum; used to store the result of (a + b) % c
636  * Returns: 0 on success, -1 on failure
637  */
638 int crypto_bignum_addmod(const struct crypto_bignum *a,
639 			 const struct crypto_bignum *b,
640 			 const struct crypto_bignum *c,
641 			 struct crypto_bignum *d);
642 
643 /**
644  * crypto_bignum_mulmod - d = a * b (mod c)
645  * @a: Bignum
646  * @b: Bignum
647  * @c: Bignum
648  * @d: Bignum; used to store the result of (a * b) % c
649  * Returns: 0 on success, -1 on failure
650  */
651 int crypto_bignum_mulmod(const struct crypto_bignum *a,
652 			 const struct crypto_bignum *b,
653 			 const struct crypto_bignum *c,
654 			 struct crypto_bignum *d);
655 
656 /**
657  * crypto_bignum_cmp - Compare two bignums
658  * @a: Bignum
659  * @b: Bignum
660  * Returns: -1 if a < b, 0 if a == b, or 1 if a > b
661  */
662 int crypto_bignum_cmp(const struct crypto_bignum *a,
663 		      const struct crypto_bignum *b);
664 
665 /**
666  * crypto_bignum_bits - Get size of a bignum in bits
667  * @a: Bignum
668  * Returns: Number of bits in the bignum
669  */
670 int crypto_bignum_bits(const struct crypto_bignum *a);
671 
672 /**
673  * crypto_bignum_is_zero - Is the given bignum zero
674  * @a: Bignum
675  * Returns: 1 if @a is zero or 0 if not
676  */
677 int crypto_bignum_is_zero(const struct crypto_bignum *a);
678 
679 /**
680  * crypto_bignum_is_one - Is the given bignum one
681  * @a: Bignum
682  * Returns: 1 if @a is one or 0 if not
683  */
684 int crypto_bignum_is_one(const struct crypto_bignum *a);
685 
686 /**
687  * crypto_bignum_legendre - Compute the Legendre symbol (a/p)
688  * @a: Bignum
689  * @p: Bignum
690  * Returns: Legendre symbol -1,0,1 on success; -2 on calculation failure
691  */
692 int crypto_bignum_legendre(const struct crypto_bignum *a,
693         const struct crypto_bignum *p);
694 
695 
696 /**
697  * struct crypto_ec - Elliptic curve context
698  *
699  * Internal data structure for EC implementation. The contents is specific
700  * to the used crypto library.
701  */
702 struct crypto_ec;
703 
704 /**
705  * crypto_ec_init - Initialize elliptic curve context
706  * @group: Identifying number for the ECC group (IANA "Group Description"
707  *  attribute registrty for RFC 2409)
708  * Returns: Pointer to EC context or %NULL on failure
709  */
710 struct crypto_ec * crypto_ec_init(int group);
711 
712 /**
713  * crypto_ec_deinit - Deinitialize elliptic curve context
714  * @e: EC context from crypto_ec_init()
715  */
716 void crypto_ec_deinit(struct crypto_ec *e);
717 
718 /**
719  * crypto_ec_prime_len - Get length of the prime in octets
720  * @e: EC context from crypto_ec_init()
721  * Returns: Length of the prime defining the group
722  */
723 size_t crypto_ec_prime_len(struct crypto_ec *e);
724 
725 /**
726  * crypto_ec_prime_len_bits - Get length of the prime in bits
727  * @e: EC context from crypto_ec_init()
728  * Returns: Length of the prime defining the group in bits
729  */
730 size_t crypto_ec_prime_len_bits(struct crypto_ec *e);
731 
732 /**
733  * crypto_ec_get_prime - Get prime defining an EC group
734  * @e: EC context from crypto_ec_init()
735  * Returns: Prime (bignum) defining the group
736  */
737 const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e);
738 
739 /**
740  * crypto_ec_get_order - Get order of an EC group
741  * @e: EC context from crypto_ec_init()
742  * Returns: Order (bignum) of the group
743  */
744 const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e);
745 
746 /**
747  * struct crypto_ec_point - Elliptic curve point
748  *
749  * Internal data structure for EC implementation to represent a point. The
750  * contents is specific to the used crypto library.
751  */
752 struct crypto_ec_point;
753 
754 /**
755  * crypto_ec_point_init - Initialize data for an EC point
756  * @e: EC context from crypto_ec_init()
757  * Returns: Pointer to EC point data or %NULL on failure
758  */
759 struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e);
760 
761 /**
762  * crypto_ec_point_deinit - Deinitialize EC point data
763  * @p: EC point data from crypto_ec_point_init()
764  * @clear: Whether to clear the EC point value from memory
765  */
766 void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear);
767 
768 /**
769  * crypto_ec_point_to_bin - Write EC point value as binary data
770  * @e: EC context from crypto_ec_init()
771  * @p: EC point data from crypto_ec_point_init()
772  * @x: Buffer for writing the binary data for x coordinate or %NULL if not used
773  * @y: Buffer for writing the binary data for y coordinate or %NULL if not used
774  * Returns: 0 on success, -1 on failure
775  *
776  * This function can be used to write an EC point as binary data in a format
777  * that has the x and y coordinates in big endian byte order fields padded to
778  * the length of the prime defining the group.
779  */
780 int crypto_ec_point_to_bin(struct crypto_ec *e,
781         const struct crypto_ec_point *point, u8 *x, u8 *y);
782 
783 /**
784  * crypto_ec_point_from_bin - Create EC point from binary data
785  * @e: EC context from crypto_ec_init()
786  * @val: Binary data to read the EC point from
787  * Returns: Pointer to EC point data or %NULL on failure
788  *
789  * This function readers x and y coordinates of the EC point from the provided
790  * buffer assuming the values are in big endian byte order with fields padded to
791  * the length of the prime defining the group.
792  */
793 struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
794         const u8 *val);
795 
796 /**
797  * crypto_bignum_add - c = a + b
798  * @e: EC context from crypto_ec_init()
799  * @a: Bignum
800  * @b: Bignum
801  * @c: Bignum; used to store the result of a + b
802  * Returns: 0 on success, -1 on failure
803  */
804 int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
805         const struct crypto_ec_point *b,
806         struct crypto_ec_point *c);
807 
808 /**
809  * crypto_bignum_mul - res = b * p
810  * @e: EC context from crypto_ec_init()
811  * @p: EC point
812  * @b: Bignum
813  * @res: EC point; used to store the result of b * p
814  * Returns: 0 on success, -1 on failure
815  */
816 int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
817         const struct crypto_bignum *b,
818         struct crypto_ec_point *res);
819 
820 /**
821  * crypto_ec_point_invert - Compute inverse of an EC point
822  * @e: EC context from crypto_ec_init()
823  * @p: EC point to invert (and result of the operation)
824  * Returns: 0 on success, -1 on failure
825  */
826 int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p);
827 
828 /**
829  * crypto_ec_point_solve_y_coord - Solve y coordinate for an x coordinate
830  * @e: EC context from crypto_ec_init()
831  * @p: EC point to use for the returning the result
832  * @x: x coordinate
833  * @y_bit: y-bit (0 or 1) for selecting the y value to use
834  * Returns: 0 on success, -1 on failure
835  */
836 int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
837         struct crypto_ec_point *p,
838         const struct crypto_bignum *x, int y_bit);
839 
840 /**
841  * crypto_ec_point_compute_y_sqr - Compute y^2 = x^3 + ax + b
842  * @e: EC context from crypto_ec_init()
843  * @x: x coordinate
844  * Returns: y^2 on success, %NULL failure
845  */
846 struct crypto_bignum *
847 crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
848         const struct crypto_bignum *x);
849 
850 /**
851  * crypto_ec_point_is_at_infinity - Check whether EC point is neutral element
852  * @e: EC context from crypto_ec_init()
853  * @p: EC point
854  * Returns: 1 if the specified EC point is the neutral element of the group or
855  *   0 if not
856  */
857 int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
858         const struct crypto_ec_point *p);
859 
860 /**
861  * crypto_ec_point_is_on_curve - Check whether EC point is on curve
862  * @e: EC context from crypto_ec_init()
863  * @p: EC point
864  * Returns: 1 if the specified EC point is on the curve or 0 if not
865  */
866 int crypto_ec_point_is_on_curve(struct crypto_ec *e,
867         const struct crypto_ec_point *p);
868 
869 /**
870  * crypto_ec_point_cmp - Compare two EC points
871  * @e: EC context from crypto_ec_init()
872  * @a: EC point
873  * @b: EC point
874  * Returns: 0 on equal, non-zero otherwise
875  */
876 int crypto_ec_point_cmp(const struct crypto_ec *e,
877         const struct crypto_ec_point *a,
878         const struct crypto_ec_point *b);
879 
880 /**
881  * crypto_ec_get_publickey_buf - Write EC public key to buffer
882  * @key: crypto key
883  * @key_buf: key buffer
884  * @len: length of buffer
885  * Returns: 0 on success, non-zero otherwise
886  */
887 int crypto_ec_get_publickey_buf(struct crypto_key *key, u8 *key_buf, int len);
888 
889 /**
890  * crypto_ec_get_group_from_key - Write EC group from key
891  * @key: crypto key
892  * Returns: EC group
893  */
894 struct crypto_ec_group *crypto_ec_get_group_from_key(struct crypto_key *key);
895 
896 /**
897  * crypto_ec_get_private_key - Get EC private key (in bignum format)
898  * @key: crypto key
899  * Returns: Private key
900  */
901 struct crypto_bignum *crypto_ec_get_private_key(struct crypto_key *key);
902 
903 /**
904  * crypto_ec_get_key - Read key from character stream
905  * @privkey: Private key
906  * @privkey_len: private key len
907  * Returns: Crypto key
908  */
909 struct crypto_key *crypto_ec_get_key(const u8 *privkey, size_t privkey_len);
910 
911 /**
912  * crypto_ec_get_mbedtls_to_nist_group_id - get nist group from mbedtls internal group
913  * @id: mbedtls group
914  * Returns: NIST group
915  */
916 unsigned int crypto_ec_get_mbedtls_to_nist_group_id(int id);
917 
918 /**
919  * crypto_ec_get_curve_id - get curve id from ec group
920  * @group: EC group
921  * Returns: curve ID
922  */
923 int crypto_ec_get_curve_id(const struct crypto_ec_group *group);
924 
925 /**
926  * crypto_ecdh: crypto ecdh
927  * @key_own: own key
928  * @key_peer: peer key
929  * @secret: secret
930  * @secret_len: secret len
931  * Returns: 0 if success else negative value
932  */
933 int crypto_ecdh(struct crypto_key *key_own, struct crypto_key *key_peer,
934 		    u8 *secret, size_t *secret_len);
935 
936 /**
937  * crypto_ecdsa_get_sign: get crypto ecdsa signed hash
938  * @hash: signed hash
939  * @r: ecdsa r
940  * @s: ecdsa s
941  * @csign: csign
942  * @hash_len: length of hash
943  * Return: 0 if success else negative value
944  */
945 int crypto_ecdsa_get_sign(unsigned char *hash,
946 		const struct crypto_bignum *r, const struct crypto_bignum *s,
947 		struct crypto_key *csign, int hash_len);
948 
949 /**
950  * crypto_edcsa_sign_verify: verify crypto ecdsa signed hash
951  * @hash: signed hash
952  * @r: ecdsa r
953  * @s: ecdsa s
954  * @csign: csign
955  * @hlen: length of hash
956  * Return: 0 if success else negative value
957  */
958 int crypto_edcsa_sign_verify(const unsigned char *hash, const struct crypto_bignum *r,
959 			const struct crypto_bignum *s, struct crypto_key *csign, int hlen);
960 
961 /**
962  * crypto_ec_parse_subpub_key: get EC key context from sub public key
963  * @p: data
964  * @len: data len
965  * Return: crypto_key
966  */
967 struct crypto_key *crypto_ec_parse_subpub_key(const unsigned char *p, size_t len);
968 
969 /**
970  * crypto_is_ec_key: check whether a key is EC key or not
971  * @key: crypto key
972  * Return: true if key else false
973  */
974 int crypto_is_ec_key(struct crypto_key *key);
975 
976 /**
977  * crypto_ec_gen_keypair: generate crypto ec keypair
978  * @ike_group: grpup
979  * Return: crypto key
980  */
981 struct crypto_key * crypto_ec_gen_keypair(u16 ike_group);
982 
983 /**
984  * crypto_ec_write_pub_key: return public key in charater buffer
985  * @key: crypto key
986  * @der_len: buffer len
987  * Return: public key buffer
988  */
989 int crypto_ec_write_pub_key(struct crypto_key *key, unsigned char **key_buf);
990 
991 /**
992  * crypto_ec_set_pubkey_point: set bignum point on ec curve
993  * @group: ec group
994  * @buf: x,y coordinate
995  * @len: length of x and y coordiate
996  * Return : crypto key
997  */
998 struct crypto_key * crypto_ec_set_pubkey_point(const struct crypto_ec_group *group,
999 					     const u8 *buf, size_t len);
1000 /**
1001  * crypto_ec_free_key: free crypto key
1002  * Return : None
1003  */
1004 void crypto_ec_free_key(struct crypto_key *key);
1005 /**
1006  * crypto_debug_print_ec_key: print ec key
1007  * @title: title
1008  * @key: crypto key
1009  * Return: None
1010  */
1011 void crypto_debug_print_ec_key(const char *title, struct crypto_key *key);
1012 
1013 /**
1014  * crypto_ec_get_public_key: Public key from crypto key
1015  * @key: crypto key
1016  * Return : Public key
1017  */
1018 struct crypto_ec_point *crypto_ec_get_public_key(struct crypto_key *key);
1019 
1020 /**
1021  * crypto_get_order: free crypto key
1022  * Return : None
1023  */
1024 int crypto_get_order(struct crypto_ec_group *group, struct crypto_bignum *x);
1025 
1026 /**
1027  * crypto_ec_get_affine_coordinates : get affine corrdinate of ec curve
1028  * @e: ec curve
1029  * @pt: point
1030  * @x: x coordinate
1031  * @y: y coordinate
1032  * Return : 0 if success
1033  */
1034 int crypto_ec_get_affine_coordinates(struct crypto_ec *e, struct crypto_ec_point *pt,
1035         struct crypto_bignum *x, struct crypto_bignum *y);
1036 
1037 /**
1038  * crypto_ec_get_group_byname: get ec curve group by name
1039  * @name: ec curve name
1040  * Return : EC group
1041  */
1042 struct crypto_ec_group *crypto_ec_get_group_byname(const char *name);
1043 
1044 /**
1045  * crypto_key_compare: check whether two keys belong to same
1046  * Return : 1 if yes else 0
1047  */
1048 int crypto_key_compare(struct crypto_key *key1, struct crypto_key *key2);
1049 
1050 /*
1051  * crypto_write_pubkey_der: get public key in der format
1052  * @csign: key
1053  * @key_buf: key buffer in charater format
1054  * Return : len of char buffer if success
1055  */
1056 int crypto_write_pubkey_der(struct crypto_key *csign, unsigned char **key_buf);
1057 
1058 /**
1059  * crypto_free_buffer: free buffer allocated by crypto API
1060  * @buf: buffer pointer
1061  * Return : None
1062  */
1063 void crypto_free_buffer(unsigned char *buf);
1064 
1065 /**
1066  * @crypto_ec_get_priv_key_der: get private key in der format
1067  * @key: key structure
1068  * @key_data: key data in charater buffer
1069  * @key_len = key length of charater buffer
1070  * Return : 0 if success
1071  */
1072 int crypto_ec_get_priv_key_der(struct crypto_key *key, unsigned char **key_data, int *key_len);
1073 
1074 /**
1075  * crypto_bignum_to_string: get big number in ascii format
1076  * @a: big number
1077  * @buf: buffer in which number will written to
1078  * @buflen: buffer length
1079  * @padlen: padding length
1080  * Return : 0 if success
1081  */
1082 int crypto_bignum_to_string(const struct crypto_bignum *a,
1083                          u8 *buf, size_t buflen, size_t padlen);
1084 #endif /* CRYPTO_H */
1085