Updated to hostap_2_6
[mech_eap.git] / libeap / src / crypto / crypto.h
1 /*
2  * Wrapper functions for crypto libraries
3  * Copyright (c) 2004-2013, 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 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28
29 /**
30  * md4_vector - MD4 hash for data vector
31  * @num_elem: Number of elements in the data vector
32  * @addr: Pointers to the data areas
33  * @len: Lengths of the data blocks
34  * @mac: Buffer for the hash
35  * Returns: 0 on success, -1 on failure
36  */
37 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
38
39 /**
40  * md5_vector - MD5 hash for data vector
41  * @num_elem: Number of elements in the data vector
42  * @addr: Pointers to the data areas
43  * @len: Lengths of the data blocks
44  * @mac: Buffer for the hash
45  * Returns: 0 on success, -1 on failure
46  */
47 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
48
49
50 /**
51  * sha1_vector - SHA-1 hash for data vector
52  * @num_elem: Number of elements in the data vector
53  * @addr: Pointers to the data areas
54  * @len: Lengths of the data blocks
55  * @mac: Buffer for the hash
56  * Returns: 0 on success, -1 on failure
57  */
58 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
59                 u8 *mac);
60
61 /**
62  * fips186_2-prf - NIST FIPS Publication 186-2 change notice 1 PRF
63  * @seed: Seed/key for the PRF
64  * @seed_len: Seed length in bytes
65  * @x: Buffer for PRF output
66  * @xlen: Output length in bytes
67  * Returns: 0 on success, -1 on failure
68  *
69  * This function implements random number generation specified in NIST FIPS
70  * Publication 186-2 for EAP-SIM. This PRF uses a function that is similar to
71  * SHA-1, but has different message padding.
72  */
73 int __must_check fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x,
74                                size_t xlen);
75
76 /**
77  * sha256_vector - SHA256 hash for data vector
78  * @num_elem: Number of elements in the data vector
79  * @addr: Pointers to the data areas
80  * @len: Lengths of the data blocks
81  * @mac: Buffer for the hash
82  * Returns: 0 on success, -1 on failure
83  */
84 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
85                   u8 *mac);
86
87 /**
88  * sha384_vector - SHA384 hash for data vector
89  * @num_elem: Number of elements in the data vector
90  * @addr: Pointers to the data areas
91  * @len: Lengths of the data blocks
92  * @mac: Buffer for the hash
93  * Returns: 0 on success, -1 on failure
94  */
95 int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
96                   u8 *mac);
97
98 /**
99  * sha512_vector - SHA512 hash for data vector
100  * @num_elem: Number of elements in the data vector
101  * @addr: Pointers to the data areas
102  * @len: Lengths of the data blocks
103  * @mac: Buffer for the hash
104  * Returns: 0 on success, -1 on failure
105  */
106 int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
107                   u8 *mac);
108
109 /**
110  * des_encrypt - Encrypt one block with DES
111  * @clear: 8 octets (in)
112  * @key: 7 octets (in) (no parity bits included)
113  * @cypher: 8 octets (out)
114  */
115 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
116
117 /**
118  * aes_encrypt_init - Initialize AES for encryption
119  * @key: Encryption key
120  * @len: Key length in bytes (usually 16, i.e., 128 bits)
121  * Returns: Pointer to context data or %NULL on failure
122  */
123 void * aes_encrypt_init(const u8 *key, size_t len);
124
125 /**
126  * aes_encrypt - Encrypt one AES block
127  * @ctx: Context pointer from aes_encrypt_init()
128  * @plain: Plaintext data to be encrypted (16 bytes)
129  * @crypt: Buffer for the encrypted data (16 bytes)
130  */
131 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
132
133 /**
134  * aes_encrypt_deinit - Deinitialize AES encryption
135  * @ctx: Context pointer from aes_encrypt_init()
136  */
137 void aes_encrypt_deinit(void *ctx);
138
139 /**
140  * aes_decrypt_init - Initialize AES for decryption
141  * @key: Decryption key
142  * @len: Key length in bytes (usually 16, i.e., 128 bits)
143  * Returns: Pointer to context data or %NULL on failure
144  */
145 void * aes_decrypt_init(const u8 *key, size_t len);
146
147 /**
148  * aes_decrypt - Decrypt one AES block
149  * @ctx: Context pointer from aes_encrypt_init()
150  * @crypt: Encrypted data (16 bytes)
151  * @plain: Buffer for the decrypted data (16 bytes)
152  */
153 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
154
155 /**
156  * aes_decrypt_deinit - Deinitialize AES decryption
157  * @ctx: Context pointer from aes_encrypt_init()
158  */
159 void aes_decrypt_deinit(void *ctx);
160
161
162 enum crypto_hash_alg {
163         CRYPTO_HASH_ALG_MD5, CRYPTO_HASH_ALG_SHA1,
164         CRYPTO_HASH_ALG_HMAC_MD5, CRYPTO_HASH_ALG_HMAC_SHA1,
165         CRYPTO_HASH_ALG_SHA256, CRYPTO_HASH_ALG_HMAC_SHA256,
166         CRYPTO_HASH_ALG_SHA384, CRYPTO_HASH_ALG_SHA512
167 };
168
169 struct crypto_hash;
170
171 /**
172  * crypto_hash_init - Initialize hash/HMAC function
173  * @alg: Hash algorithm
174  * @key: Key for keyed hash (e.g., HMAC) or %NULL if not needed
175  * @key_len: Length of the key in bytes
176  * Returns: Pointer to hash context to use with other hash functions or %NULL
177  * on failure
178  *
179  * This function is only used with internal TLSv1 implementation
180  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
181  * to implement this.
182  */
183 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
184                                       size_t key_len);
185
186 /**
187  * crypto_hash_update - Add data to hash calculation
188  * @ctx: Context pointer from crypto_hash_init()
189  * @data: Data buffer to add
190  * @len: Length of the buffer
191  *
192  * This function is only used with internal TLSv1 implementation
193  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
194  * to implement this.
195  */
196 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len);
197
198 /**
199  * crypto_hash_finish - Complete hash calculation
200  * @ctx: Context pointer from crypto_hash_init()
201  * @hash: Buffer for hash value or %NULL if caller is just freeing the hash
202  * context
203  * @len: Pointer to length of the buffer or %NULL if caller is just freeing the
204  * hash context; on return, this is set to the actual length of the hash value
205  * Returns: 0 on success, -1 if buffer is too small (len set to needed length),
206  * or -2 on other failures (including failed crypto_hash_update() operations)
207  *
208  * This function calculates the hash value and frees the context buffer that
209  * was used for hash calculation.
210  *
211  * This function is only used with internal TLSv1 implementation
212  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
213  * to implement this.
214  */
215 int crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len);
216
217
218 enum crypto_cipher_alg {
219         CRYPTO_CIPHER_NULL = 0, CRYPTO_CIPHER_ALG_AES, CRYPTO_CIPHER_ALG_3DES,
220         CRYPTO_CIPHER_ALG_DES, CRYPTO_CIPHER_ALG_RC2, CRYPTO_CIPHER_ALG_RC4
221 };
222
223 struct crypto_cipher;
224
225 /**
226  * crypto_cipher_init - Initialize block/stream cipher function
227  * @alg: Cipher algorithm
228  * @iv: Initialization vector for block ciphers or %NULL for stream ciphers
229  * @key: Cipher key
230  * @key_len: Length of key in bytes
231  * Returns: Pointer to cipher context to use with other cipher functions or
232  * %NULL on failure
233  *
234  * This function is only used with internal TLSv1 implementation
235  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
236  * to implement this.
237  */
238 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
239                                           const u8 *iv, const u8 *key,
240                                           size_t key_len);
241
242 /**
243  * crypto_cipher_encrypt - Cipher encrypt
244  * @ctx: Context pointer from crypto_cipher_init()
245  * @plain: Plaintext to cipher
246  * @crypt: Resulting ciphertext
247  * @len: Length of the plaintext
248  * Returns: 0 on success, -1 on failure
249  *
250  * This function is only used with internal TLSv1 implementation
251  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
252  * to implement this.
253  */
254 int __must_check crypto_cipher_encrypt(struct crypto_cipher *ctx,
255                                        const u8 *plain, u8 *crypt, size_t len);
256
257 /**
258  * crypto_cipher_decrypt - Cipher decrypt
259  * @ctx: Context pointer from crypto_cipher_init()
260  * @crypt: Ciphertext to decrypt
261  * @plain: Resulting plaintext
262  * @len: Length of the cipher text
263  * Returns: 0 on success, -1 on failure
264  *
265  * This function is only used with internal TLSv1 implementation
266  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
267  * to implement this.
268  */
269 int __must_check crypto_cipher_decrypt(struct crypto_cipher *ctx,
270                                        const u8 *crypt, u8 *plain, size_t len);
271
272 /**
273  * crypto_cipher_decrypt - Free cipher context
274  * @ctx: Context pointer from crypto_cipher_init()
275  *
276  * This function is only used with internal TLSv1 implementation
277  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
278  * to implement this.
279  */
280 void crypto_cipher_deinit(struct crypto_cipher *ctx);
281
282
283 struct crypto_public_key;
284 struct crypto_private_key;
285
286 /**
287  * crypto_public_key_import - Import an RSA public key
288  * @key: Key buffer (DER encoded RSA public key)
289  * @len: Key buffer length in bytes
290  * Returns: Pointer to the public key or %NULL on failure
291  *
292  * This function can just return %NULL if the crypto library supports X.509
293  * parsing. In that case, crypto_public_key_from_cert() is used to import the
294  * public key from a certificate.
295  *
296  * This function is only used with internal TLSv1 implementation
297  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
298  * to implement this.
299  */
300 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len);
301
302 struct crypto_public_key *
303 crypto_public_key_import_parts(const u8 *n, size_t n_len,
304                                const u8 *e, size_t e_len);
305
306 /**
307  * crypto_private_key_import - Import an RSA private key
308  * @key: Key buffer (DER encoded RSA private key)
309  * @len: Key buffer length in bytes
310  * @passwd: Key encryption password or %NULL if key is not encrypted
311  * Returns: Pointer to the private key or %NULL on failure
312  *
313  * This function is only used with internal TLSv1 implementation
314  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
315  * to implement this.
316  */
317 struct crypto_private_key * crypto_private_key_import(const u8 *key,
318                                                       size_t len,
319                                                       const char *passwd);
320
321 /**
322  * crypto_public_key_from_cert - Import an RSA public key from a certificate
323  * @buf: DER encoded X.509 certificate
324  * @len: Certificate buffer length in bytes
325  * Returns: Pointer to public key or %NULL on failure
326  *
327  * This function can just return %NULL if the crypto library does not support
328  * X.509 parsing. In that case, internal code will be used to parse the
329  * certificate and public key is imported using crypto_public_key_import().
330  *
331  * This function is only used with internal TLSv1 implementation
332  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
333  * to implement this.
334  */
335 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
336                                                        size_t len);
337
338 /**
339  * crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5)
340  * @key: Public key
341  * @in: Plaintext buffer
342  * @inlen: Length of plaintext buffer in bytes
343  * @out: Output buffer for encrypted data
344  * @outlen: Length of output buffer in bytes; set to used length on success
345  * Returns: 0 on success, -1 on failure
346  *
347  * This function is only used with internal TLSv1 implementation
348  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
349  * to implement this.
350  */
351 int __must_check crypto_public_key_encrypt_pkcs1_v15(
352         struct crypto_public_key *key, const u8 *in, size_t inlen,
353         u8 *out, size_t *outlen);
354
355 /**
356  * crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5)
357  * @key: Private key
358  * @in: Encrypted buffer
359  * @inlen: Length of encrypted buffer in bytes
360  * @out: Output buffer for encrypted data
361  * @outlen: Length of output buffer in bytes; set to used length on success
362  * Returns: 0 on success, -1 on failure
363  *
364  * This function is only used with internal TLSv1 implementation
365  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
366  * to implement this.
367  */
368 int __must_check crypto_private_key_decrypt_pkcs1_v15(
369         struct crypto_private_key *key, const u8 *in, size_t inlen,
370         u8 *out, size_t *outlen);
371
372 /**
373  * crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1)
374  * @key: Private key from crypto_private_key_import()
375  * @in: Plaintext buffer
376  * @inlen: Length of plaintext buffer in bytes
377  * @out: Output buffer for encrypted (signed) data
378  * @outlen: Length of output buffer in bytes; set to used length on success
379  * Returns: 0 on success, -1 on failure
380  *
381  * This function is only used with internal TLSv1 implementation
382  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
383  * to implement this.
384  */
385 int __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
386                                                const u8 *in, size_t inlen,
387                                                u8 *out, size_t *outlen);
388
389 /**
390  * crypto_public_key_free - Free public key
391  * @key: Public key
392  *
393  * This function is only used with internal TLSv1 implementation
394  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
395  * to implement this.
396  */
397 void crypto_public_key_free(struct crypto_public_key *key);
398
399 /**
400  * crypto_private_key_free - Free private key
401  * @key: Private key from crypto_private_key_import()
402  *
403  * This function is only used with internal TLSv1 implementation
404  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
405  * to implement this.
406  */
407 void crypto_private_key_free(struct crypto_private_key *key);
408
409 /**
410  * crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature
411  * @key: Public key
412  * @crypt: Encrypted signature data (using the private key)
413  * @crypt_len: Encrypted signature data length
414  * @plain: Buffer for plaintext (at least crypt_len bytes)
415  * @plain_len: Plaintext length (max buffer size on input, real len on output);
416  * Returns: 0 on success, -1 on failure
417  */
418 int __must_check crypto_public_key_decrypt_pkcs1(
419         struct crypto_public_key *key, const u8 *crypt, size_t crypt_len,
420         u8 *plain, size_t *plain_len);
421
422 /**
423  * crypto_global_init - Initialize crypto wrapper
424  *
425  * This function is only used with internal TLSv1 implementation
426  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
427  * to implement this.
428  */
429 int __must_check crypto_global_init(void);
430
431 /**
432  * crypto_global_deinit - Deinitialize crypto wrapper
433  *
434  * This function is only used with internal TLSv1 implementation
435  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
436  * to implement this.
437  */
438 void crypto_global_deinit(void);
439
440 /**
441  * crypto_mod_exp - Modular exponentiation of large integers
442  * @base: Base integer (big endian byte array)
443  * @base_len: Length of base integer in bytes
444  * @power: Power integer (big endian byte array)
445  * @power_len: Length of power integer in bytes
446  * @modulus: Modulus integer (big endian byte array)
447  * @modulus_len: Length of modulus integer in bytes
448  * @result: Buffer for the result
449  * @result_len: Result length (max buffer size on input, real len on output)
450  * Returns: 0 on success, -1 on failure
451  *
452  * This function calculates result = base ^ power mod modulus. modules_len is
453  * used as the maximum size of modulus buffer. It is set to the used size on
454  * success.
455  *
456  * This function is only used with internal TLSv1 implementation
457  * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
458  * to implement this.
459  */
460 int __must_check crypto_mod_exp(const u8 *base, size_t base_len,
461                                 const u8 *power, size_t power_len,
462                                 const u8 *modulus, size_t modulus_len,
463                                 u8 *result, size_t *result_len);
464
465 /**
466  * rc4_skip - XOR RC4 stream to given data with skip-stream-start
467  * @key: RC4 key
468  * @keylen: RC4 key length
469  * @skip: number of bytes to skip from the beginning of the RC4 stream
470  * @data: data to be XOR'ed with RC4 stream
471  * @data_len: buf length
472  * Returns: 0 on success, -1 on failure
473  *
474  * Generate RC4 pseudo random stream for the given key, skip beginning of the
475  * stream, and XOR the end result with the data buffer to perform RC4
476  * encryption/decryption.
477  */
478 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
479              u8 *data, size_t data_len);
480
481 /**
482  * crypto_get_random - Generate cryptographically strong pseudy-random bytes
483  * @buf: Buffer for data
484  * @len: Number of bytes to generate
485  * Returns: 0 on success, -1 on failure
486  *
487  * If the PRNG does not have enough entropy to ensure unpredictable byte
488  * sequence, this functions must return -1.
489  */
490 int crypto_get_random(void *buf, size_t len);
491
492
493 /**
494  * struct crypto_bignum - bignum
495  *
496  * Internal data structure for bignum implementation. The contents is specific
497  * to the used crypto library.
498  */
499 struct crypto_bignum;
500
501 /**
502  * crypto_bignum_init - Allocate memory for bignum
503  * Returns: Pointer to allocated bignum or %NULL on failure
504  */
505 struct crypto_bignum * crypto_bignum_init(void);
506
507 /**
508  * crypto_bignum_init_set - Allocate memory for bignum and set the value
509  * @buf: Buffer with unsigned binary value
510  * @len: Length of buf in octets
511  * Returns: Pointer to allocated bignum or %NULL on failure
512  */
513 struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len);
514
515 /**
516  * crypto_bignum_deinit - Free bignum
517  * @n: Bignum from crypto_bignum_init() or crypto_bignum_init_set()
518  * @clear: Whether to clear the value from memory
519  */
520 void crypto_bignum_deinit(struct crypto_bignum *n, int clear);
521
522 /**
523  * crypto_bignum_to_bin - Set binary buffer to unsigned bignum
524  * @a: Bignum
525  * @buf: Buffer for the binary number
526  * @len: Length of @buf in octets
527  * @padlen: Length in octets to pad the result to or 0 to indicate no padding
528  * Returns: Number of octets written on success, -1 on failure
529  */
530 int crypto_bignum_to_bin(const struct crypto_bignum *a,
531                          u8 *buf, size_t buflen, size_t padlen);
532
533 /**
534  * crypto_bignum_add - c = a + b
535  * @a: Bignum
536  * @b: Bignum
537  * @c: Bignum; used to store the result of a + b
538  * Returns: 0 on success, -1 on failure
539  */
540 int crypto_bignum_add(const struct crypto_bignum *a,
541                       const struct crypto_bignum *b,
542                       struct crypto_bignum *c);
543
544 /**
545  * crypto_bignum_mod - c = a % b
546  * @a: Bignum
547  * @b: Bignum
548  * @c: Bignum; used to store the result of a % b
549  * Returns: 0 on success, -1 on failure
550  */
551 int crypto_bignum_mod(const struct crypto_bignum *a,
552                       const struct crypto_bignum *b,
553                       struct crypto_bignum *c);
554
555 /**
556  * crypto_bignum_exptmod - Modular exponentiation: d = a^b (mod c)
557  * @a: Bignum; base
558  * @b: Bignum; exponent
559  * @c: Bignum; modulus
560  * @d: Bignum; used to store the result of a^b (mod c)
561  * Returns: 0 on success, -1 on failure
562  */
563 int crypto_bignum_exptmod(const struct crypto_bignum *a,
564                           const struct crypto_bignum *b,
565                           const struct crypto_bignum *c,
566                           struct crypto_bignum *d);
567
568 /**
569  * crypto_bignum_inverse - Inverse a bignum so that a * c = 1 (mod b)
570  * @a: Bignum
571  * @b: Bignum
572  * @c: Bignum; used to store the result
573  * Returns: 0 on success, -1 on failure
574  */
575 int crypto_bignum_inverse(const struct crypto_bignum *a,
576                           const struct crypto_bignum *b,
577                           struct crypto_bignum *c);
578
579 /**
580  * crypto_bignum_sub - c = a - b
581  * @a: Bignum
582  * @b: Bignum
583  * @c: Bignum; used to store the result of a - b
584  * Returns: 0 on success, -1 on failure
585  */
586 int crypto_bignum_sub(const struct crypto_bignum *a,
587                       const struct crypto_bignum *b,
588                       struct crypto_bignum *c);
589
590 /**
591  * crypto_bignum_div - c = a / b
592  * @a: Bignum
593  * @b: Bignum
594  * @c: Bignum; used to store the result of a / b
595  * Returns: 0 on success, -1 on failure
596  */
597 int crypto_bignum_div(const struct crypto_bignum *a,
598                       const struct crypto_bignum *b,
599                       struct crypto_bignum *c);
600
601 /**
602  * crypto_bignum_mulmod - d = a * b (mod c)
603  * @a: Bignum
604  * @b: Bignum
605  * @c: Bignum
606  * @d: Bignum; used to store the result of (a * b) % c
607  * Returns: 0 on success, -1 on failure
608  */
609 int crypto_bignum_mulmod(const struct crypto_bignum *a,
610                          const struct crypto_bignum *b,
611                          const struct crypto_bignum *c,
612                          struct crypto_bignum *d);
613
614 /**
615  * crypto_bignum_cmp - Compare two bignums
616  * @a: Bignum
617  * @b: Bignum
618  * Returns: -1 if a < b, 0 if a == b, or 1 if a > b
619  */
620 int crypto_bignum_cmp(const struct crypto_bignum *a,
621                       const struct crypto_bignum *b);
622
623 /**
624  * crypto_bignum_bits - Get size of a bignum in bits
625  * @a: Bignum
626  * Returns: Number of bits in the bignum
627  */
628 int crypto_bignum_bits(const struct crypto_bignum *a);
629
630 /**
631  * crypto_bignum_is_zero - Is the given bignum zero
632  * @a: Bignum
633  * Returns: 1 if @a is zero or 0 if not
634  */
635 int crypto_bignum_is_zero(const struct crypto_bignum *a);
636
637 /**
638  * crypto_bignum_is_one - Is the given bignum one
639  * @a: Bignum
640  * Returns: 1 if @a is one or 0 if not
641  */
642 int crypto_bignum_is_one(const struct crypto_bignum *a);
643
644 /**
645  * crypto_bignum_legendre - Compute the Legendre symbol (a/p)
646  * @a: Bignum
647  * @p: Bignum
648  * Returns: Legendre symbol -1,0,1 on success; -2 on calculation failure
649  */
650 int crypto_bignum_legendre(const struct crypto_bignum *a,
651                            const struct crypto_bignum *p);
652
653 /**
654  * struct crypto_ec - Elliptic curve context
655  *
656  * Internal data structure for EC implementation. The contents is specific
657  * to the used crypto library.
658  */
659 struct crypto_ec;
660
661 /**
662  * crypto_ec_init - Initialize elliptic curve context
663  * @group: Identifying number for the ECC group (IANA "Group Description"
664  *      attribute registrty for RFC 2409)
665  * Returns: Pointer to EC context or %NULL on failure
666  */
667 struct crypto_ec * crypto_ec_init(int group);
668
669 /**
670  * crypto_ec_deinit - Deinitialize elliptic curve context
671  * @e: EC context from crypto_ec_init()
672  */
673 void crypto_ec_deinit(struct crypto_ec *e);
674
675 /**
676  * crypto_ec_prime_len - Get length of the prime in octets
677  * @e: EC context from crypto_ec_init()
678  * Returns: Length of the prime defining the group
679  */
680 size_t crypto_ec_prime_len(struct crypto_ec *e);
681
682 /**
683  * crypto_ec_prime_len_bits - Get length of the prime in bits
684  * @e: EC context from crypto_ec_init()
685  * Returns: Length of the prime defining the group in bits
686  */
687 size_t crypto_ec_prime_len_bits(struct crypto_ec *e);
688
689 /**
690  * crypto_ec_get_prime - Get prime defining an EC group
691  * @e: EC context from crypto_ec_init()
692  * Returns: Prime (bignum) defining the group
693  */
694 const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e);
695
696 /**
697  * crypto_ec_get_order - Get order of an EC group
698  * @e: EC context from crypto_ec_init()
699  * Returns: Order (bignum) of the group
700  */
701 const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e);
702
703 /**
704  * struct crypto_ec_point - Elliptic curve point
705  *
706  * Internal data structure for EC implementation to represent a point. The
707  * contents is specific to the used crypto library.
708  */
709 struct crypto_ec_point;
710
711 /**
712  * crypto_ec_point_init - Initialize data for an EC point
713  * @e: EC context from crypto_ec_init()
714  * Returns: Pointer to EC point data or %NULL on failure
715  */
716 struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e);
717
718 /**
719  * crypto_ec_point_deinit - Deinitialize EC point data
720  * @p: EC point data from crypto_ec_point_init()
721  * @clear: Whether to clear the EC point value from memory
722  */
723 void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear);
724
725 /**
726  * crypto_ec_point_to_bin - Write EC point value as binary data
727  * @e: EC context from crypto_ec_init()
728  * @p: EC point data from crypto_ec_point_init()
729  * @x: Buffer for writing the binary data for x coordinate or %NULL if not used
730  * @y: Buffer for writing the binary data for y coordinate or %NULL if not used
731  * Returns: 0 on success, -1 on failure
732  *
733  * This function can be used to write an EC point as binary data in a format
734  * that has the x and y coordinates in big endian byte order fields padded to
735  * the length of the prime defining the group.
736  */
737 int crypto_ec_point_to_bin(struct crypto_ec *e,
738                            const struct crypto_ec_point *point, u8 *x, u8 *y);
739
740 /**
741  * crypto_ec_point_from_bin - Create EC point from binary data
742  * @e: EC context from crypto_ec_init()
743  * @val: Binary data to read the EC point from
744  * Returns: Pointer to EC point data or %NULL on failure
745  *
746  * This function readers x and y coordinates of the EC point from the provided
747  * buffer assuming the values are in big endian byte order with fields padded to
748  * the length of the prime defining the group.
749  */
750 struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
751                                                   const u8 *val);
752
753 /**
754  * crypto_bignum_add - c = a + b
755  * @e: EC context from crypto_ec_init()
756  * @a: Bignum
757  * @b: Bignum
758  * @c: Bignum; used to store the result of a + b
759  * Returns: 0 on success, -1 on failure
760  */
761 int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
762                         const struct crypto_ec_point *b,
763                         struct crypto_ec_point *c);
764
765 /**
766  * crypto_bignum_mul - res = b * p
767  * @e: EC context from crypto_ec_init()
768  * @p: EC point
769  * @b: Bignum
770  * @res: EC point; used to store the result of b * p
771  * Returns: 0 on success, -1 on failure
772  */
773 int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
774                         const struct crypto_bignum *b,
775                         struct crypto_ec_point *res);
776
777 /**
778  * crypto_ec_point_invert - Compute inverse of an EC point
779  * @e: EC context from crypto_ec_init()
780  * @p: EC point to invert (and result of the operation)
781  * Returns: 0 on success, -1 on failure
782  */
783 int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p);
784
785 /**
786  * crypto_ec_point_solve_y_coord - Solve y coordinate for an x coordinate
787  * @e: EC context from crypto_ec_init()
788  * @p: EC point to use for the returning the result
789  * @x: x coordinate
790  * @y_bit: y-bit (0 or 1) for selecting the y value to use
791  * Returns: 0 on success, -1 on failure
792  */
793 int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
794                                   struct crypto_ec_point *p,
795                                   const struct crypto_bignum *x, int y_bit);
796
797 /**
798  * crypto_ec_point_compute_y_sqr - Compute y^2 = x^3 + ax + b
799  * @e: EC context from crypto_ec_init()
800  * @x: x coordinate
801  * Returns: y^2 on success, %NULL failure
802  */
803 struct crypto_bignum *
804 crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
805                               const struct crypto_bignum *x);
806
807 /**
808  * crypto_ec_point_is_at_infinity - Check whether EC point is neutral element
809  * @e: EC context from crypto_ec_init()
810  * @p: EC point
811  * Returns: 1 if the specified EC point is the neutral element of the group or
812  *      0 if not
813  */
814 int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
815                                    const struct crypto_ec_point *p);
816
817 /**
818  * crypto_ec_point_is_on_curve - Check whether EC point is on curve
819  * @e: EC context from crypto_ec_init()
820  * @p: EC point
821  * Returns: 1 if the specified EC point is on the curve or 0 if not
822  */
823 int crypto_ec_point_is_on_curve(struct crypto_ec *e,
824                                 const struct crypto_ec_point *p);
825
826 /**
827  * crypto_ec_point_cmp - Compare two EC points
828  * @e: EC context from crypto_ec_init()
829  * @a: EC point
830  * @b: EC point
831  * Returns: 0 on equal, non-zero otherwise
832  */
833 int crypto_ec_point_cmp(const struct crypto_ec *e,
834                         const struct crypto_ec_point *a,
835                         const struct crypto_ec_point *b);
836
837
838 #ifdef __cplusplus
839 }
840 #endif
841
842 #endif /* CRYPTO_H */