blob: 440da0302967d469ba626d329e72ae168f260c5f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002 * Wrapper functions for crypto libraries
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003 * Copyright (c) 2004-2017, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
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 */
32int 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 */
42int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
43
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
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 */
53int 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 */
68int __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 */
79int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
80 u8 *mac);
81
82/**
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080083 * 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 */
90int 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 */
101int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
102 u8 *mac);
103
104/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 * 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)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700109 * Returns: 0 on success, -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700111int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112
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 */
119void * 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)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700126 * Returns: 0 on success, -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700127 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700128int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129
130/**
131 * aes_encrypt_deinit - Deinitialize AES encryption
132 * @ctx: Context pointer from aes_encrypt_init()
133 */
134void 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 */
142void * 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)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700149 * Returns: 0 on success, -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700151int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152
153/**
154 * aes_decrypt_deinit - Deinitialize AES decryption
155 * @ctx: Context pointer from aes_encrypt_init()
156 */
157void aes_decrypt_deinit(void *ctx);
158
159
160enum crypto_hash_alg {
161 CRYPTO_HASH_ALG_MD5, CRYPTO_HASH_ALG_SHA1,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800162 CRYPTO_HASH_ALG_HMAC_MD5, CRYPTO_HASH_ALG_HMAC_SHA1,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800163 CRYPTO_HASH_ALG_SHA256, CRYPTO_HASH_ALG_HMAC_SHA256,
164 CRYPTO_HASH_ALG_SHA384, CRYPTO_HASH_ALG_SHA512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165};
166
167struct 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 */
181struct 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 */
194void 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 */
213int crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len);
214
215
216enum 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
221struct 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 */
236struct 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 */
252int __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 */
267int __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 */
278void crypto_cipher_deinit(struct crypto_cipher *ctx);
279
280
281struct crypto_public_key;
282struct 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 */
298struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len);
299
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700300struct crypto_public_key *
301crypto_public_key_import_parts(const u8 *n, size_t n_len,
302 const u8 *e, size_t e_len);
303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304/**
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 */
315struct 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 */
333struct 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 */
349int __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 */
366int __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 */
383int __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 */
395void 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 */
405void 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 */
416int __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
Roshan Pius3a1667e2018-07-03 15:17:14 -0700420int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
421 u8 *pubkey);
422int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
Hai Shalom021b0b52019-04-10 11:17:58 -0700423 const u8 *order, size_t order_len,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700424 const u8 *privkey, size_t privkey_len,
425 const u8 *pubkey, size_t pubkey_len,
426 u8 *secret, size_t *len);
427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428/**
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 */
435int __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 */
444void 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 */
466int __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 */
484int rc4_skip(const u8 *key, size_t keylen, size_t skip,
485 u8 *data, size_t data_len);
486
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700487/**
488 * crypto_get_random - Generate cryptographically strong pseudy-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 */
496int crypto_get_random(void *buf, size_t len);
497
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800498
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 */
505struct crypto_bignum;
506
507/**
508 * crypto_bignum_init - Allocate memory for bignum
509 * Returns: Pointer to allocated bignum or %NULL on failure
510 */
511struct crypto_bignum * crypto_bignum_init(void);
512
513/**
514 * crypto_bignum_init_set - Allocate memory for bignum and set the value
515 * @buf: Buffer with unsigned binary value
516 * @len: Length of buf in octets
517 * Returns: Pointer to allocated bignum or %NULL on failure
518 */
519struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len);
520
521/**
Hai Shalomc3565922019-10-28 11:58:20 -0700522 * crypto_bignum_init_set - Allocate memory for bignum and set the value (uint)
523 * @val: Value to set
524 * Returns: Pointer to allocated bignum or %NULL on failure
525 */
526struct crypto_bignum * crypto_bignum_init_uint(unsigned int val);
527
528/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800529 * crypto_bignum_deinit - Free bignum
530 * @n: Bignum from crypto_bignum_init() or crypto_bignum_init_set()
531 * @clear: Whether to clear the value from memory
532 */
533void crypto_bignum_deinit(struct crypto_bignum *n, int clear);
534
535/**
536 * crypto_bignum_to_bin - Set binary buffer to unsigned bignum
537 * @a: Bignum
538 * @buf: Buffer for the binary number
539 * @len: Length of @buf in octets
540 * @padlen: Length in octets to pad the result to or 0 to indicate no padding
541 * Returns: Number of octets written on success, -1 on failure
542 */
543int crypto_bignum_to_bin(const struct crypto_bignum *a,
544 u8 *buf, size_t buflen, size_t padlen);
545
546/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700547 * crypto_bignum_rand - Create a random number in range of modulus
548 * @r: Bignum; set to a random value
549 * @m: Bignum; modulus
550 * Returns: 0 on success, -1 on failure
551 */
552int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m);
553
554/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800555 * crypto_bignum_add - c = a + b
556 * @a: Bignum
557 * @b: Bignum
558 * @c: Bignum; used to store the result of a + b
559 * Returns: 0 on success, -1 on failure
560 */
561int crypto_bignum_add(const struct crypto_bignum *a,
562 const struct crypto_bignum *b,
563 struct crypto_bignum *c);
564
565/**
566 * crypto_bignum_mod - c = a % b
567 * @a: Bignum
568 * @b: Bignum
569 * @c: Bignum; used to store the result of a % b
570 * Returns: 0 on success, -1 on failure
571 */
572int crypto_bignum_mod(const struct crypto_bignum *a,
573 const struct crypto_bignum *b,
574 struct crypto_bignum *c);
575
576/**
577 * crypto_bignum_exptmod - Modular exponentiation: d = a^b (mod c)
578 * @a: Bignum; base
579 * @b: Bignum; exponent
580 * @c: Bignum; modulus
581 * @d: Bignum; used to store the result of a^b (mod c)
582 * Returns: 0 on success, -1 on failure
583 */
584int crypto_bignum_exptmod(const struct crypto_bignum *a,
585 const struct crypto_bignum *b,
586 const struct crypto_bignum *c,
587 struct crypto_bignum *d);
588
589/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800590 * crypto_bignum_inverse - Inverse a bignum so that a * c = 1 (mod b)
591 * @a: Bignum
592 * @b: Bignum
593 * @c: Bignum; used to store the result
594 * Returns: 0 on success, -1 on failure
595 */
596int crypto_bignum_inverse(const struct crypto_bignum *a,
597 const struct crypto_bignum *b,
598 struct crypto_bignum *c);
599
600/**
601 * crypto_bignum_sub - c = a - b
602 * @a: Bignum
603 * @b: Bignum
604 * @c: Bignum; used to store the result of a - b
605 * Returns: 0 on success, -1 on failure
606 */
607int crypto_bignum_sub(const struct crypto_bignum *a,
608 const struct crypto_bignum *b,
609 struct crypto_bignum *c);
610
611/**
612 * crypto_bignum_div - c = a / b
613 * @a: Bignum
614 * @b: Bignum
615 * @c: Bignum; used to store the result of a / b
616 * Returns: 0 on success, -1 on failure
617 */
618int crypto_bignum_div(const struct crypto_bignum *a,
619 const struct crypto_bignum *b,
620 struct crypto_bignum *c);
621
622/**
Hai Shalomc3565922019-10-28 11:58:20 -0700623 * crypto_bignum_addmod - d = a + b (mod c)
624 * @a: Bignum
625 * @b: Bignum
626 * @c: Bignum
627 * @d: Bignum; used to store the result of (a + b) % c
628 * Returns: 0 on success, -1 on failure
629 */
630int crypto_bignum_addmod(const struct crypto_bignum *a,
631 const struct crypto_bignum *b,
632 const struct crypto_bignum *c,
633 struct crypto_bignum *d);
634
635/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800636 * crypto_bignum_mulmod - d = a * b (mod c)
637 * @a: Bignum
638 * @b: Bignum
639 * @c: Bignum
640 * @d: Bignum; used to store the result of (a * b) % c
641 * Returns: 0 on success, -1 on failure
642 */
643int crypto_bignum_mulmod(const struct crypto_bignum *a,
644 const struct crypto_bignum *b,
645 const struct crypto_bignum *c,
646 struct crypto_bignum *d);
647
648/**
Hai Shalomc3565922019-10-28 11:58:20 -0700649 * crypto_bignum_sqrmod - c = a^2 (mod b)
650 * @a: Bignum
651 * @b: Bignum
652 * @c: Bignum; used to store the result of a^2 % b
653 * Returns: 0 on success, -1 on failure
654 */
655int crypto_bignum_sqrmod(const struct crypto_bignum *a,
656 const struct crypto_bignum *b,
657 struct crypto_bignum *c);
658
659/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700660 * crypto_bignum_rshift - r = a >> n
661 * @a: Bignum
662 * @n: Number of bits
663 * @r: Bignum; used to store the result of a >> n
664 * Returns: 0 on success, -1 on failure
665 */
666int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
667 struct crypto_bignum *r);
668
669/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800670 * crypto_bignum_cmp - Compare two bignums
671 * @a: Bignum
672 * @b: Bignum
673 * Returns: -1 if a < b, 0 if a == b, or 1 if a > b
674 */
675int crypto_bignum_cmp(const struct crypto_bignum *a,
676 const struct crypto_bignum *b);
677
678/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800679 * crypto_bignum_is_zero - Is the given bignum zero
680 * @a: Bignum
681 * Returns: 1 if @a is zero or 0 if not
682 */
683int crypto_bignum_is_zero(const struct crypto_bignum *a);
684
685/**
686 * crypto_bignum_is_one - Is the given bignum one
687 * @a: Bignum
688 * Returns: 1 if @a is one or 0 if not
689 */
690int crypto_bignum_is_one(const struct crypto_bignum *a);
691
692/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700693 * crypto_bignum_is_odd - Is the given bignum odd
694 * @a: Bignum
695 * Returns: 1 if @a is odd or 0 if not
696 */
697int crypto_bignum_is_odd(const struct crypto_bignum *a);
698
699/**
Dmitry Shmidt41712582015-06-29 11:02:15 -0700700 * crypto_bignum_legendre - Compute the Legendre symbol (a/p)
701 * @a: Bignum
702 * @p: Bignum
703 * Returns: Legendre symbol -1,0,1 on success; -2 on calculation failure
704 */
705int crypto_bignum_legendre(const struct crypto_bignum *a,
706 const struct crypto_bignum *p);
707
708/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800709 * struct crypto_ec - Elliptic curve context
710 *
711 * Internal data structure for EC implementation. The contents is specific
712 * to the used crypto library.
713 */
714struct crypto_ec;
715
716/**
717 * crypto_ec_init - Initialize elliptic curve context
718 * @group: Identifying number for the ECC group (IANA "Group Description"
719 * attribute registrty for RFC 2409)
720 * Returns: Pointer to EC context or %NULL on failure
721 */
722struct crypto_ec * crypto_ec_init(int group);
723
724/**
725 * crypto_ec_deinit - Deinitialize elliptic curve context
726 * @e: EC context from crypto_ec_init()
727 */
728void crypto_ec_deinit(struct crypto_ec *e);
729
730/**
731 * crypto_ec_prime_len - Get length of the prime in octets
732 * @e: EC context from crypto_ec_init()
733 * Returns: Length of the prime defining the group
734 */
735size_t crypto_ec_prime_len(struct crypto_ec *e);
736
737/**
738 * crypto_ec_prime_len_bits - Get length of the prime in bits
739 * @e: EC context from crypto_ec_init()
740 * Returns: Length of the prime defining the group in bits
741 */
742size_t crypto_ec_prime_len_bits(struct crypto_ec *e);
743
744/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700745 * crypto_ec_order_len - Get length of the order in octets
746 * @e: EC context from crypto_ec_init()
747 * Returns: Length of the order defining the group
748 */
749size_t crypto_ec_order_len(struct crypto_ec *e);
750
751/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800752 * crypto_ec_get_prime - Get prime defining an EC group
753 * @e: EC context from crypto_ec_init()
754 * Returns: Prime (bignum) defining the group
755 */
756const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e);
757
758/**
759 * crypto_ec_get_order - Get order of an EC group
760 * @e: EC context from crypto_ec_init()
761 * Returns: Order (bignum) of the group
762 */
763const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e);
764
Hai Shalomc3565922019-10-28 11:58:20 -0700765const struct crypto_bignum * crypto_ec_get_a(struct crypto_ec *e);
766const struct crypto_bignum * crypto_ec_get_b(struct crypto_ec *e);
767
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800768/**
769 * struct crypto_ec_point - Elliptic curve point
770 *
771 * Internal data structure for EC implementation to represent a point. The
772 * contents is specific to the used crypto library.
773 */
774struct crypto_ec_point;
775
776/**
777 * crypto_ec_point_init - Initialize data for an EC point
778 * @e: EC context from crypto_ec_init()
779 * Returns: Pointer to EC point data or %NULL on failure
780 */
781struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e);
782
783/**
784 * crypto_ec_point_deinit - Deinitialize EC point data
785 * @p: EC point data from crypto_ec_point_init()
786 * @clear: Whether to clear the EC point value from memory
787 */
788void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear);
789
790/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700791 * crypto_ec_point_x - Copies the x-ordinate point into big number
792 * @e: EC context from crypto_ec_init()
793 * @p: EC point data
794 * @x: Big number to set to the copy of x-ordinate
795 * Returns: 0 on success, -1 on failure
796 */
797int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p,
798 struct crypto_bignum *x);
799
800/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800801 * crypto_ec_point_to_bin - Write EC point value as binary data
802 * @e: EC context from crypto_ec_init()
803 * @p: EC point data from crypto_ec_point_init()
804 * @x: Buffer for writing the binary data for x coordinate or %NULL if not used
805 * @y: Buffer for writing the binary data for y coordinate or %NULL if not used
806 * Returns: 0 on success, -1 on failure
807 *
808 * This function can be used to write an EC point as binary data in a format
809 * that has the x and y coordinates in big endian byte order fields padded to
810 * the length of the prime defining the group.
811 */
812int crypto_ec_point_to_bin(struct crypto_ec *e,
813 const struct crypto_ec_point *point, u8 *x, u8 *y);
814
815/**
816 * crypto_ec_point_from_bin - Create EC point from binary data
817 * @e: EC context from crypto_ec_init()
818 * @val: Binary data to read the EC point from
819 * Returns: Pointer to EC point data or %NULL on failure
820 *
821 * This function readers x and y coordinates of the EC point from the provided
822 * buffer assuming the values are in big endian byte order with fields padded to
823 * the length of the prime defining the group.
824 */
825struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
826 const u8 *val);
827
828/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700829 * crypto_ec_point_add - c = a + b
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800830 * @e: EC context from crypto_ec_init()
831 * @a: Bignum
832 * @b: Bignum
833 * @c: Bignum; used to store the result of a + b
834 * Returns: 0 on success, -1 on failure
835 */
836int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
837 const struct crypto_ec_point *b,
838 struct crypto_ec_point *c);
839
840/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700841 * crypto_ec_point_mul - res = b * p
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800842 * @e: EC context from crypto_ec_init()
843 * @p: EC point
844 * @b: Bignum
845 * @res: EC point; used to store the result of b * p
846 * Returns: 0 on success, -1 on failure
847 */
848int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
849 const struct crypto_bignum *b,
850 struct crypto_ec_point *res);
851
852/**
853 * crypto_ec_point_invert - Compute inverse of an EC point
854 * @e: EC context from crypto_ec_init()
855 * @p: EC point to invert (and result of the operation)
856 * Returns: 0 on success, -1 on failure
857 */
858int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p);
859
860/**
861 * crypto_ec_point_solve_y_coord - Solve y coordinate for an x coordinate
862 * @e: EC context from crypto_ec_init()
863 * @p: EC point to use for the returning the result
864 * @x: x coordinate
865 * @y_bit: y-bit (0 or 1) for selecting the y value to use
866 * Returns: 0 on success, -1 on failure
867 */
868int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
869 struct crypto_ec_point *p,
870 const struct crypto_bignum *x, int y_bit);
871
872/**
Dmitry Shmidt41712582015-06-29 11:02:15 -0700873 * crypto_ec_point_compute_y_sqr - Compute y^2 = x^3 + ax + b
874 * @e: EC context from crypto_ec_init()
875 * @x: x coordinate
876 * Returns: y^2 on success, %NULL failure
877 */
878struct crypto_bignum *
879crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
880 const struct crypto_bignum *x);
881
882/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800883 * crypto_ec_point_is_at_infinity - Check whether EC point is neutral element
884 * @e: EC context from crypto_ec_init()
885 * @p: EC point
886 * Returns: 1 if the specified EC point is the neutral element of the group or
887 * 0 if not
888 */
889int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
890 const struct crypto_ec_point *p);
891
892/**
893 * crypto_ec_point_is_on_curve - Check whether EC point is on curve
894 * @e: EC context from crypto_ec_init()
895 * @p: EC point
896 * Returns: 1 if the specified EC point is on the curve or 0 if not
897 */
898int crypto_ec_point_is_on_curve(struct crypto_ec *e,
899 const struct crypto_ec_point *p);
900
Dmitry Shmidt41712582015-06-29 11:02:15 -0700901/**
902 * crypto_ec_point_cmp - Compare two EC points
903 * @e: EC context from crypto_ec_init()
904 * @a: EC point
905 * @b: EC point
906 * Returns: 0 on equal, non-zero otherwise
907 */
908int crypto_ec_point_cmp(const struct crypto_ec *e,
909 const struct crypto_ec_point *a,
910 const struct crypto_ec_point *b);
911
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700912struct crypto_ecdh;
913
914struct crypto_ecdh * crypto_ecdh_init(int group);
915struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y);
916struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
917 const u8 *key, size_t len);
918void crypto_ecdh_deinit(struct crypto_ecdh *ecdh);
919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920#endif /* CRYPTO_H */