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