blob: 91df6079d4ba8ba61320cf75756f1147a1db64b9 [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/**
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700488 * crypto_get_random - Generate cryptographically strong pseudo-random bytes
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700489 * @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
Hai Shaloma20dcd72022-02-04 13:43:00 -0800498/**
499 * crypto_pkcs7_get_certificates - Extract X.509 certificates from PKCS#7 data
500 * @pkcs7: DER encoded PKCS#7 data
501 * Returns: Buffer of the extracted PEM X.509 certificates or %NULL on failure
502 */
503struct wpabuf * crypto_pkcs7_get_certificates(const struct wpabuf *pkcs7);
504
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800505
506/**
507 * struct crypto_bignum - bignum
508 *
509 * Internal data structure for bignum implementation. The contents is specific
510 * to the used crypto library.
511 */
512struct crypto_bignum;
513
514/**
515 * crypto_bignum_init - Allocate memory for bignum
516 * Returns: Pointer to allocated bignum or %NULL on failure
517 */
518struct crypto_bignum * crypto_bignum_init(void);
519
520/**
521 * crypto_bignum_init_set - Allocate memory for bignum and set the value
522 * @buf: Buffer with unsigned binary value
523 * @len: Length of buf in octets
524 * Returns: Pointer to allocated bignum or %NULL on failure
525 */
526struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len);
527
528/**
Hai Shalomc3565922019-10-28 11:58:20 -0700529 * crypto_bignum_init_set - Allocate memory for bignum and set the value (uint)
530 * @val: Value to set
531 * Returns: Pointer to allocated bignum or %NULL on failure
532 */
533struct crypto_bignum * crypto_bignum_init_uint(unsigned int val);
534
535/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800536 * crypto_bignum_deinit - Free bignum
537 * @n: Bignum from crypto_bignum_init() or crypto_bignum_init_set()
538 * @clear: Whether to clear the value from memory
539 */
540void crypto_bignum_deinit(struct crypto_bignum *n, int clear);
541
542/**
543 * crypto_bignum_to_bin - Set binary buffer to unsigned bignum
544 * @a: Bignum
545 * @buf: Buffer for the binary number
546 * @len: Length of @buf in octets
547 * @padlen: Length in octets to pad the result to or 0 to indicate no padding
548 * Returns: Number of octets written on success, -1 on failure
549 */
550int crypto_bignum_to_bin(const struct crypto_bignum *a,
551 u8 *buf, size_t buflen, size_t padlen);
552
553/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700554 * crypto_bignum_rand - Create a random number in range of modulus
555 * @r: Bignum; set to a random value
556 * @m: Bignum; modulus
557 * Returns: 0 on success, -1 on failure
558 */
559int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m);
560
561/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800562 * crypto_bignum_add - c = a + b
563 * @a: Bignum
564 * @b: Bignum
565 * @c: Bignum; used to store the result of a + b
566 * Returns: 0 on success, -1 on failure
567 */
568int crypto_bignum_add(const struct crypto_bignum *a,
569 const struct crypto_bignum *b,
570 struct crypto_bignum *c);
571
572/**
573 * crypto_bignum_mod - c = a % b
574 * @a: Bignum
575 * @b: Bignum
576 * @c: Bignum; used to store the result of a % b
577 * Returns: 0 on success, -1 on failure
578 */
579int crypto_bignum_mod(const struct crypto_bignum *a,
580 const struct crypto_bignum *b,
581 struct crypto_bignum *c);
582
583/**
584 * crypto_bignum_exptmod - Modular exponentiation: d = a^b (mod c)
585 * @a: Bignum; base
586 * @b: Bignum; exponent
587 * @c: Bignum; modulus
588 * @d: Bignum; used to store the result of a^b (mod c)
589 * Returns: 0 on success, -1 on failure
590 */
591int crypto_bignum_exptmod(const struct crypto_bignum *a,
592 const struct crypto_bignum *b,
593 const struct crypto_bignum *c,
594 struct crypto_bignum *d);
595
596/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800597 * crypto_bignum_inverse - Inverse a bignum so that a * c = 1 (mod b)
598 * @a: Bignum
599 * @b: Bignum
600 * @c: Bignum; used to store the result
601 * Returns: 0 on success, -1 on failure
602 */
603int crypto_bignum_inverse(const struct crypto_bignum *a,
604 const struct crypto_bignum *b,
605 struct crypto_bignum *c);
606
607/**
608 * crypto_bignum_sub - c = a - b
609 * @a: Bignum
610 * @b: Bignum
611 * @c: Bignum; used to store the result of a - b
612 * Returns: 0 on success, -1 on failure
613 */
614int crypto_bignum_sub(const struct crypto_bignum *a,
615 const struct crypto_bignum *b,
616 struct crypto_bignum *c);
617
618/**
619 * crypto_bignum_div - c = a / b
620 * @a: Bignum
621 * @b: Bignum
622 * @c: Bignum; used to store the result of a / b
623 * Returns: 0 on success, -1 on failure
624 */
625int crypto_bignum_div(const struct crypto_bignum *a,
626 const struct crypto_bignum *b,
627 struct crypto_bignum *c);
628
629/**
Hai Shalomc3565922019-10-28 11:58:20 -0700630 * crypto_bignum_addmod - d = a + b (mod c)
631 * @a: Bignum
632 * @b: Bignum
633 * @c: Bignum
634 * @d: Bignum; used to store the result of (a + b) % c
635 * Returns: 0 on success, -1 on failure
636 */
637int crypto_bignum_addmod(const struct crypto_bignum *a,
638 const struct crypto_bignum *b,
639 const struct crypto_bignum *c,
640 struct crypto_bignum *d);
641
642/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800643 * crypto_bignum_mulmod - d = a * b (mod c)
644 * @a: Bignum
645 * @b: Bignum
646 * @c: Bignum
647 * @d: Bignum; used to store the result of (a * b) % c
648 * Returns: 0 on success, -1 on failure
649 */
650int crypto_bignum_mulmod(const struct crypto_bignum *a,
651 const struct crypto_bignum *b,
652 const struct crypto_bignum *c,
653 struct crypto_bignum *d);
654
655/**
Hai Shalomc3565922019-10-28 11:58:20 -0700656 * crypto_bignum_sqrmod - c = a^2 (mod b)
657 * @a: Bignum
658 * @b: Bignum
659 * @c: Bignum; used to store the result of a^2 % b
660 * Returns: 0 on success, -1 on failure
661 */
662int crypto_bignum_sqrmod(const struct crypto_bignum *a,
663 const struct crypto_bignum *b,
664 struct crypto_bignum *c);
665
666/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700667 * crypto_bignum_rshift - r = a >> n
668 * @a: Bignum
669 * @n: Number of bits
670 * @r: Bignum; used to store the result of a >> n
671 * Returns: 0 on success, -1 on failure
672 */
673int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
674 struct crypto_bignum *r);
675
676/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800677 * crypto_bignum_cmp - Compare two bignums
678 * @a: Bignum
679 * @b: Bignum
680 * Returns: -1 if a < b, 0 if a == b, or 1 if a > b
681 */
682int crypto_bignum_cmp(const struct crypto_bignum *a,
683 const struct crypto_bignum *b);
684
685/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800686 * crypto_bignum_is_zero - Is the given bignum zero
687 * @a: Bignum
688 * Returns: 1 if @a is zero or 0 if not
689 */
690int crypto_bignum_is_zero(const struct crypto_bignum *a);
691
692/**
693 * crypto_bignum_is_one - Is the given bignum one
694 * @a: Bignum
695 * Returns: 1 if @a is one or 0 if not
696 */
697int crypto_bignum_is_one(const struct crypto_bignum *a);
698
699/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700700 * crypto_bignum_is_odd - Is the given bignum odd
701 * @a: Bignum
702 * Returns: 1 if @a is odd or 0 if not
703 */
704int crypto_bignum_is_odd(const struct crypto_bignum *a);
705
706/**
Dmitry Shmidt41712582015-06-29 11:02:15 -0700707 * crypto_bignum_legendre - Compute the Legendre symbol (a/p)
708 * @a: Bignum
709 * @p: Bignum
710 * Returns: Legendre symbol -1,0,1 on success; -2 on calculation failure
711 */
712int crypto_bignum_legendre(const struct crypto_bignum *a,
713 const struct crypto_bignum *p);
714
715/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800716 * struct crypto_ec - Elliptic curve context
717 *
718 * Internal data structure for EC implementation. The contents is specific
719 * to the used crypto library.
720 */
721struct crypto_ec;
722
723/**
Hai Shaloma20dcd72022-02-04 13:43:00 -0800724 * struct crypto_ec_point - Elliptic curve point
725 *
726 * Internal data structure for EC implementation to represent a point. The
727 * contents is specific to the used crypto library.
728 */
729struct crypto_ec_point;
730
731/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800732 * crypto_ec_init - Initialize elliptic curve context
733 * @group: Identifying number for the ECC group (IANA "Group Description"
734 * attribute registrty for RFC 2409)
735 * Returns: Pointer to EC context or %NULL on failure
736 */
737struct crypto_ec * crypto_ec_init(int group);
738
739/**
740 * crypto_ec_deinit - Deinitialize elliptic curve context
741 * @e: EC context from crypto_ec_init()
742 */
743void crypto_ec_deinit(struct crypto_ec *e);
744
745/**
746 * crypto_ec_prime_len - Get length of the prime in octets
747 * @e: EC context from crypto_ec_init()
748 * Returns: Length of the prime defining the group
749 */
750size_t crypto_ec_prime_len(struct crypto_ec *e);
751
752/**
753 * crypto_ec_prime_len_bits - Get length of the prime in bits
754 * @e: EC context from crypto_ec_init()
755 * Returns: Length of the prime defining the group in bits
756 */
757size_t crypto_ec_prime_len_bits(struct crypto_ec *e);
758
759/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700760 * crypto_ec_order_len - Get length of the order in octets
761 * @e: EC context from crypto_ec_init()
762 * Returns: Length of the order defining the group
763 */
764size_t crypto_ec_order_len(struct crypto_ec *e);
765
766/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800767 * crypto_ec_get_prime - Get prime defining an EC group
768 * @e: EC context from crypto_ec_init()
769 * Returns: Prime (bignum) defining the group
770 */
771const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e);
772
773/**
774 * crypto_ec_get_order - Get order of an EC group
775 * @e: EC context from crypto_ec_init()
776 * Returns: Order (bignum) of the group
777 */
778const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e);
779
Hai Shaloma20dcd72022-02-04 13:43:00 -0800780/**
781 * crypto_ec_get_a - Get 'a' coefficient of an EC group's curve
782 * @e: EC context from crypto_ec_init()
783 * Returns: 'a' coefficient (bignum) of the group
784 */
Hai Shalomc3565922019-10-28 11:58:20 -0700785const struct crypto_bignum * crypto_ec_get_a(struct crypto_ec *e);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800786
787/**
788 * crypto_ec_get_b - Get 'b' coeffiecient of an EC group's curve
789 * @e: EC context from crypto_ec_init()
790 * Returns: 'b' coefficient (bignum) of the group
791 */
Hai Shalomc3565922019-10-28 11:58:20 -0700792const struct crypto_bignum * crypto_ec_get_b(struct crypto_ec *e);
793
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800794/**
Hai Shaloma20dcd72022-02-04 13:43:00 -0800795 * crypto_ec_get_generator - Get generator point of the EC group's curve
796 * @e: EC context from crypto_ec_init()
797 * Returns: Pointer to generator point
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800798 */
Hai Shaloma20dcd72022-02-04 13:43:00 -0800799const struct crypto_ec_point * crypto_ec_get_generator(struct crypto_ec *e);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800800
801/**
802 * crypto_ec_point_init - Initialize data for an EC point
803 * @e: EC context from crypto_ec_init()
804 * Returns: Pointer to EC point data or %NULL on failure
805 */
806struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e);
807
808/**
809 * crypto_ec_point_deinit - Deinitialize EC point data
810 * @p: EC point data from crypto_ec_point_init()
811 * @clear: Whether to clear the EC point value from memory
812 */
813void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear);
814
815/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700816 * crypto_ec_point_x - Copies the x-ordinate point into big number
817 * @e: EC context from crypto_ec_init()
818 * @p: EC point data
819 * @x: Big number to set to the copy of x-ordinate
820 * Returns: 0 on success, -1 on failure
821 */
822int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p,
823 struct crypto_bignum *x);
824
825/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800826 * crypto_ec_point_to_bin - Write EC point value as binary data
827 * @e: EC context from crypto_ec_init()
828 * @p: EC point data from crypto_ec_point_init()
829 * @x: Buffer for writing the binary data for x coordinate or %NULL if not used
830 * @y: Buffer for writing the binary data for y coordinate or %NULL if not used
831 * Returns: 0 on success, -1 on failure
832 *
833 * This function can be used to write an EC point as binary data in a format
834 * that has the x and y coordinates in big endian byte order fields padded to
835 * the length of the prime defining the group.
836 */
837int crypto_ec_point_to_bin(struct crypto_ec *e,
838 const struct crypto_ec_point *point, u8 *x, u8 *y);
839
840/**
841 * crypto_ec_point_from_bin - Create EC point from binary data
842 * @e: EC context from crypto_ec_init()
843 * @val: Binary data to read the EC point from
844 * Returns: Pointer to EC point data or %NULL on failure
845 *
846 * This function readers x and y coordinates of the EC point from the provided
847 * buffer assuming the values are in big endian byte order with fields padded to
848 * the length of the prime defining the group.
849 */
850struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
851 const u8 *val);
852
853/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700854 * crypto_ec_point_add - c = a + b
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800855 * @e: EC context from crypto_ec_init()
856 * @a: Bignum
857 * @b: Bignum
858 * @c: Bignum; used to store the result of a + b
859 * Returns: 0 on success, -1 on failure
860 */
861int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
862 const struct crypto_ec_point *b,
863 struct crypto_ec_point *c);
864
865/**
Roshan Pius3a1667e2018-07-03 15:17:14 -0700866 * crypto_ec_point_mul - res = b * p
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800867 * @e: EC context from crypto_ec_init()
868 * @p: EC point
869 * @b: Bignum
870 * @res: EC point; used to store the result of b * p
871 * Returns: 0 on success, -1 on failure
872 */
873int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
874 const struct crypto_bignum *b,
875 struct crypto_ec_point *res);
876
877/**
878 * crypto_ec_point_invert - Compute inverse of an EC point
879 * @e: EC context from crypto_ec_init()
880 * @p: EC point to invert (and result of the operation)
881 * Returns: 0 on success, -1 on failure
882 */
883int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p);
884
885/**
Dmitry Shmidt41712582015-06-29 11:02:15 -0700886 * crypto_ec_point_compute_y_sqr - Compute y^2 = x^3 + ax + b
887 * @e: EC context from crypto_ec_init()
888 * @x: x coordinate
889 * Returns: y^2 on success, %NULL failure
890 */
891struct crypto_bignum *
892crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
893 const struct crypto_bignum *x);
894
895/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800896 * crypto_ec_point_is_at_infinity - Check whether EC point is neutral element
897 * @e: EC context from crypto_ec_init()
898 * @p: EC point
899 * Returns: 1 if the specified EC point is the neutral element of the group or
900 * 0 if not
901 */
902int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
903 const struct crypto_ec_point *p);
904
905/**
906 * crypto_ec_point_is_on_curve - Check whether EC point is on curve
907 * @e: EC context from crypto_ec_init()
908 * @p: EC point
909 * Returns: 1 if the specified EC point is on the curve or 0 if not
910 */
911int crypto_ec_point_is_on_curve(struct crypto_ec *e,
912 const struct crypto_ec_point *p);
913
Dmitry Shmidt41712582015-06-29 11:02:15 -0700914/**
915 * crypto_ec_point_cmp - Compare two EC points
916 * @e: EC context from crypto_ec_init()
917 * @a: EC point
918 * @b: EC point
919 * Returns: 0 on equal, non-zero otherwise
920 */
921int crypto_ec_point_cmp(const struct crypto_ec *e,
922 const struct crypto_ec_point *a,
923 const struct crypto_ec_point *b);
924
Hai Shaloma20dcd72022-02-04 13:43:00 -0800925/**
926 * crypto_ec_point_debug_print - Dump EC point to debug log
927 * @e: EC context from crypto_ec_init()
928 * @p: EC point
929 * @title: Name of the EC point in the trace
930 */
931void crypto_ec_point_debug_print(const struct crypto_ec *e,
932 const struct crypto_ec_point *p,
933 const char *title);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700934
Hai Shaloma20dcd72022-02-04 13:43:00 -0800935/**
936 * struct crypto_ec_key - Elliptic curve key pair
937 *
938 * Internal data structure for EC key pair. The contents is specific to the used
939 * crypto library.
940 */
Hai Shalom899fcc72020-10-19 14:38:18 -0700941struct crypto_ec_key;
942
Hai Shaloma20dcd72022-02-04 13:43:00 -0800943/**
944 * struct crypto_ecdh - Elliptic Curve Diffie–Hellman context
945 *
946 * Internal data structure for ECDH. The contents is specific to the used
947 * crypto library.
948 */
949struct crypto_ecdh;
950
951/**
952 * crypto_ecdh_init - Initialize elliptic curve Diffie–Hellman context
953 * @group: Identifying number for the ECC group (IANA "Group Description"
954 * attribute registry for RFC 2409)
955 * This function generates an ephemeral key pair.
956 * Returns: Pointer to ECDH context or %NULL on failure
957 */
958struct crypto_ecdh * crypto_ecdh_init(int group);
959
960/**
961 * crypto_ecdh_init2 - Initialize elliptic curve Diffie–Hellman context with a
962 * given EC key
963 * @group: Identifying number for the ECC group (IANA "Group Description"
964 * attribute registry for RFC 2409)
965 * @own_key: Our own EC Key
966 * Returns: Pointer to ECDH context or %NULL on failure
967 */
968struct crypto_ecdh * crypto_ecdh_init2(int group,
969 struct crypto_ec_key *own_key);
970
971/**
972 * crypto_ecdh_get_pubkey - Retrieve public key from ECDH context
973 * @ecdh: ECDH context from crypto_ecdh_init() or crypto_ecdh_init2()
974 * @inc_y: Whether public key should include y coordinate (explicit form)
975 * or not (compressed form)
976 * Returns: Binary data f the public key or %NULL on failure
977 */
978struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y);
979
980/**
981 * crypto_ecdh_set_peerkey - Compute ECDH secret
982 * @ecdh: ECDH context from crypto_ecdh_init() or crypto_ecdh_init2()
983 * @inc_y: Whether peer's public key includes y coordinate (explicit form)
984 * or not (compressed form)
985 * @key: Binary data of the peer's public key
986 * @len: Length of the @key buffer
987 * Returns: Binary data with the EDCH secret or %NULL on failure
988 */
989struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
990 const u8 *key, size_t len);
991
992/**
993 * crypto_ecdh_deinit - Free ECDH context
994 * @ecdh: ECDH context from crypto_ecdh_init() or crypto_ecdh_init2()
995 */
996void crypto_ecdh_deinit(struct crypto_ecdh *ecdh);
997
998/**
999 * crypto_ecdh_prime_len - Get length of the prime in octets
1000 * @e: ECDH context from crypto_ecdh_init()
1001 * Returns: Length of the prime defining the group
1002 */
1003size_t crypto_ecdh_prime_len(struct crypto_ecdh *ecdh);
1004
1005/**
1006 * crypto_ec_key_parse_priv - Initialize EC key pair from ECPrivateKey ASN.1
1007 * @der: DER encoding of ASN.1 ECPrivateKey
1008 * @der_len: Length of @der buffer
1009 * Returns: EC key or %NULL on failure
1010 */
Hai Shalom899fcc72020-10-19 14:38:18 -07001011struct crypto_ec_key * crypto_ec_key_parse_priv(const u8 *der, size_t der_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001012
1013/**
Sunil Ravi89eba102022-09-13 21:04:37 -07001014 * crypto_ec_key_set_priv - Initialize EC key pair from raw key data
1015 * @group: Identifying number for the ECC group
1016 * @raw: Raw key data
1017 * @raw_len: Length of @raw buffer
1018 * Returns: EC key or %NULL on failure
1019 */
1020struct crypto_ec_key * crypto_ec_key_set_priv(int group,
1021 const u8 *raw, size_t raw_len);
1022
1023/**
Hai Shaloma20dcd72022-02-04 13:43:00 -08001024 * crypto_ec_key_parse_pub - Initialize EC key pair from SubjectPublicKeyInfo ASN.1
1025 * @der: DER encoding of ASN.1 SubjectPublicKeyInfo
1026 * @der_len: Length of @der buffer
1027 * Returns: EC key or %NULL on failure
1028 */
Hai Shalom899fcc72020-10-19 14:38:18 -07001029struct crypto_ec_key * crypto_ec_key_parse_pub(const u8 *der, size_t der_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001030
1031/**
1032 * crypto_ec_key_set_pub - Initialize an EC public key from EC point coordinates
1033 * @group: Identifying number for the ECC group
1034 * @x: X coordinate of the public key
1035 * @y: Y coordinate of the public key
1036 * @len: Length of @x and @y buffer
1037 * Returns: EC key or %NULL on failure
1038 *
1039 * This function initialize an EC key from public key coordinates, in big endian
1040 * byte order padded to the length of the prime defining the group.
1041 */
1042struct crypto_ec_key * crypto_ec_key_set_pub(int group, const u8 *x,
1043 const u8 *y, size_t len);
1044
1045/**
1046 * crypto_ec_key_set_pub_point - Initialize an EC public key from EC point
1047 * @e: EC context from crypto_ec_init()
1048 * @pub: Public key point
1049 * Returns: EC key or %NULL on failure
1050 */
1051struct crypto_ec_key *
1052crypto_ec_key_set_pub_point(struct crypto_ec *e,
1053 const struct crypto_ec_point *pub);
1054
1055/**
1056 * crypto_ec_key_gen - Generate EC key pair
1057 * @group: Identifying number for the ECC group
1058 * Returns: EC key or %NULL on failure
1059 */
1060struct crypto_ec_key * crypto_ec_key_gen(int group);
1061
1062/**
1063 * crypto_ec_key_deinit - Free EC key
1064 * @key: EC key from crypto_ec_key_parse_pub/priv() or crypto_ec_key_gen()
1065 */
Hai Shalom899fcc72020-10-19 14:38:18 -07001066void crypto_ec_key_deinit(struct crypto_ec_key *key);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001067
1068/**
1069 * crypto_ec_key_get_subject_public_key - Get SubjectPublicKeyInfo ASN.1 for an EC key
1070 * @key: EC key from crypto_ec_key_parse/set_pub/priv() or crypto_ec_key_gen()
1071 * Returns: Buffer with DER encoding of ASN.1 SubjectPublicKeyInfo or %NULL on failure
1072 */
Hai Shalom899fcc72020-10-19 14:38:18 -07001073struct wpabuf * crypto_ec_key_get_subject_public_key(struct crypto_ec_key *key);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001074
1075/**
1076 * crypto_ec_key_get_ecprivate_key - Get ECPrivateKey ASN.1 for a EC key
1077 * @key: EC key from crypto_ec_key_parse_priv() or crypto_ec_key_gen()
1078 * @include_pub: Whether to include public key in the ASN.1 sequence
1079 * Returns: Buffer with DER encoding of ASN.1 ECPrivateKey or %NULL on failure
1080 */
1081struct wpabuf * crypto_ec_key_get_ecprivate_key(struct crypto_ec_key *key,
1082 bool include_pub);
1083
1084/**
1085 * crypto_ec_key_get_pubkey_point - Get public key point coordinates
1086 * @key: EC key from crypto_ec_key_parse/set_pub() or crypto_ec_key_parse_priv()
1087 * @prefix: Whether output buffer should include the octet to indicate
1088 * coordinate form (as defined for SubjectPublicKeyInfo)
1089 * Returns: Buffer with coordinates of public key in uncompressed form or %NULL
1090 * on failure
1091 */
1092struct wpabuf * crypto_ec_key_get_pubkey_point(struct crypto_ec_key *key,
1093 int prefix);
1094
1095/**
1096 * crypto_ec_key_get_public_key - Get EC public key as an EC point
1097 * @key: EC key from crypto_ec_key_parse/set_pub() or crypto_ec_key_parse_priv()
1098 * Returns: Public key as an EC point or %NULL on failure
Sunil8cd6f4d2022-06-28 18:40:46 +00001099 *
1100 * The caller needs to free the returned value with crypto_ec_point_deinit().
Hai Shaloma20dcd72022-02-04 13:43:00 -08001101 */
Sunil8cd6f4d2022-06-28 18:40:46 +00001102struct crypto_ec_point *
Hai Shaloma20dcd72022-02-04 13:43:00 -08001103crypto_ec_key_get_public_key(struct crypto_ec_key *key);
1104
1105/**
1106 * crypto_ec_key_get_private_key - Get EC private key as a bignum
1107 * @key: EC key from crypto_ec_key_parse/set_pub() or crypto_ec_key_parse_priv()
1108 * Returns: Private key as a bignum or %NULL on failure
Sunil8cd6f4d2022-06-28 18:40:46 +00001109 *
1110 * The caller needs to free the returned value with crypto_bignum_deinit().
Hai Shaloma20dcd72022-02-04 13:43:00 -08001111 */
Sunil8cd6f4d2022-06-28 18:40:46 +00001112struct crypto_bignum *
Hai Shaloma20dcd72022-02-04 13:43:00 -08001113crypto_ec_key_get_private_key(struct crypto_ec_key *key);
1114
1115/**
1116 * crypto_ec_key_sign - Sign a buffer with an EC key
1117 * @key: EC key from crypto_ec_key_parse_priv() or crypto_ec_key_gen()
1118 * @data: Data to sign
1119 * @len: Length of @data buffer
1120 * Returns: Buffer with DER encoding of ASN.1 Ecdsa-Sig-Value or %NULL on failure
1121 */
Hai Shalom899fcc72020-10-19 14:38:18 -07001122struct wpabuf * crypto_ec_key_sign(struct crypto_ec_key *key, const u8 *data,
1123 size_t len);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001124
1125/**
1126 * crypto_ec_key_sign_r_s - Sign a buffer with an EC key
1127 * @key: EC key from crypto_ec_key_parse_priv() or crypto_ec_key_gen()
1128 * @data: Data to sign
1129 * @len: Length of @data buffer
1130 * Returns: Buffer with the concatenated r and s values. Each value is in big
1131 * endian byte order padded to the length of the prime defining the group of
1132 * the key.
1133 */
1134struct wpabuf * crypto_ec_key_sign_r_s(struct crypto_ec_key *key,
1135 const u8 *data, size_t len);
1136
1137/**
1138 * crypto_ec_key_verify_signature - Verify ECDSA signature
1139 * @key: EC key from crypto_ec_key_parse/set_pub() or crypto_ec_key_gen()
1140 * @data: Data to be signed
1141 * @len: Length of @data buffer
1142 * @sig: DER encoding of ASN.1 Ecdsa-Sig-Value
1143 * @sig_len: Length of @sig buffer
1144 * Returns: 1 if signature is valid, 0 if signature is invalid and -1 on failure
1145 */
Hai Shalom899fcc72020-10-19 14:38:18 -07001146int crypto_ec_key_verify_signature(struct crypto_ec_key *key, const u8 *data,
1147 size_t len, const u8 *sig, size_t sig_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001148
1149/**
1150 * crypto_ec_key_verify_signature_r_s - Verify signature
1151 * @key: EC key from crypto_ec_key_parse/set_pub() or crypto_ec_key_gen()
1152 * @data: Data to signed
1153 * @len: Length of @data buffer
1154 * @r: Binary data, in big endian byte order, of the 'r' field of the ECDSA
1155 * signature.
1156 * @s: Binary data, in big endian byte order, of the 's' field of the ECDSA
1157 * signature.
1158 * @r_len: Length of @r buffer
1159 * @s_len: Length of @s buffer
1160 * Returns: 1 if signature is valid, 0 if signature is invalid, or -1 on failure
1161 */
1162int crypto_ec_key_verify_signature_r_s(struct crypto_ec_key *key,
1163 const u8 *data, size_t len,
1164 const u8 *r, size_t r_len,
1165 const u8 *s, size_t s_len);
1166
1167/**
1168 * crypto_ec_key_group - Get IANA group identifier for an EC key
1169 * @key: EC key from crypto_ec_key_parse/set_pub/priv() or crypto_ec_key_gen()
1170 * Returns: IANA group identifier and -1 on failure
1171 */
Hai Shalom899fcc72020-10-19 14:38:18 -07001172int crypto_ec_key_group(struct crypto_ec_key *key);
1173
Hai Shaloma20dcd72022-02-04 13:43:00 -08001174/**
1175 * crypto_ec_key_cmp - Compare two EC public keys
1176 * @key1: Key 1
1177 * @key2: Key 2
1178 * Returns: 0 if public keys are identical, -1 otherwise
1179 */
1180int crypto_ec_key_cmp(struct crypto_ec_key *key1, struct crypto_ec_key *key2);
1181
1182/**
1183 * crypto_ec_key_debug_print - Dump EC key to debug log
1184 * @key: EC key from crypto_ec_key_parse/set_pub/priv() or crypto_ec_key_gen()
1185 * @title: Name of the EC point in the trace
1186 */
1187void crypto_ec_key_debug_print(const struct crypto_ec_key *key,
1188 const char *title);
1189
1190/**
1191 * struct crypto_csr - Certification Signing Request
1192 *
1193 * Internal data structure for CSR. The contents is specific to the used
1194 * crypto library.
1195 * For now it is assumed that only an EC public key can be used
1196 */
1197struct crypto_csr;
1198
1199/**
1200 * enum crypto_csr_name - CSR name type
1201 */
1202enum crypto_csr_name {
1203 CSR_NAME_CN,
1204 CSR_NAME_SN,
1205 CSR_NAME_C,
1206 CSR_NAME_O,
1207 CSR_NAME_OU,
1208};
1209
1210/**
1211 * enum crypto_csr_attr - CSR attribute
1212 */
1213enum crypto_csr_attr {
1214 CSR_ATTR_CHALLENGE_PASSWORD,
1215};
1216
1217/**
1218 * crypto_csr_init - Initialize empty CSR
1219 * Returns: Pointer to CSR data or %NULL on failure
1220 */
1221struct crypto_csr * crypto_csr_init(void);
1222
1223/**
1224 * crypto_csr_verify - Initialize CSR from CertificationRequest
1225 * @req: DER encoding of ASN.1 CertificationRequest
1226 *
1227 * Returns: Pointer to CSR data or %NULL on failure or if signature is invalid
1228 */
1229struct crypto_csr * crypto_csr_verify(const struct wpabuf *req);
1230
1231/**
1232 * crypto_csr_deinit - Free CSR structure
1233 * @csr: CSR structure from @crypto_csr_init() or crypto_csr_verify()
1234 */
1235void crypto_csr_deinit(struct crypto_csr *csr);
1236
1237/**
1238 * crypto_csr_set_ec_public_key - Set public key in CSR
1239 * @csr: CSR structure from @crypto_csr_init()
1240 * @key: EC public key to set as public key in the CSR
1241 * Returns: 0 on success, -1 on failure
1242 */
1243int crypto_csr_set_ec_public_key(struct crypto_csr *csr,
1244 struct crypto_ec_key *key);
1245
1246/**
1247 * crypto_csr_set_name - Set name entry in CSR SubjectName
1248 * @csr: CSR structure from @crypto_csr_init()
1249 * @type: Name type to add into the CSR SubjectName
1250 * @name: UTF-8 string to write in the CSR SubjectName
1251 * Returns: 0 on success, -1 on failure
1252 */
1253int crypto_csr_set_name(struct crypto_csr *csr, enum crypto_csr_name type,
1254 const char *name);
1255
1256/**
1257 * crypto_csr_set_attribute - Set attribute in CSR
1258 * @csr: CSR structure from @crypto_csr_init()
1259 * @attr: Attribute identifier
1260 * @attr_type: ASN.1 type of @value buffer
1261 * @value: Attribute value
1262 * @len: length of @value buffer
1263 * Returns: 0 on success, -1 on failure
1264 */
1265int crypto_csr_set_attribute(struct crypto_csr *csr, enum crypto_csr_attr attr,
1266 int attr_type, const u8 *value, size_t len);
1267
1268/**
1269 * crypto_csr_get_attribute - Get attribute from CSR
1270 * @csr: CSR structure from @crypto_csr_verify()
1271 * @attr: Updated with atribute identifier
1272 * @len: Updated with length of returned buffer
1273 * @type: ASN.1 type of the attribute buffer
1274 * Returns: Type, length, and pointer on attribute value or %NULL on failure
1275 */
1276const u8 * crypto_csr_get_attribute(struct crypto_csr *csr,
1277 enum crypto_csr_attr attr,
1278 size_t *len, int *type);
1279
1280/**
1281 * crypto_csr_sign - Sign CSR and return ASN.1 CertificationRequest
1282 * @csr: CSR structure from @crypto_csr_init()
1283 * @key: Private key to sign the CSR (for now ony EC key are supported)
1284 * @algo: Hash algorithm to use for the signature
1285 * Returns: DER encoding of ASN.1 CertificationRequest for the CSR or %NULL on
1286 * failure
1287 */
1288struct wpabuf * crypto_csr_sign(struct crypto_csr *csr,
1289 struct crypto_ec_key *key,
1290 enum crypto_hash_alg algo);
1291
Sunil Ravia04bd252022-05-02 22:54:18 -07001292struct crypto_rsa_key;
1293
1294/**
1295 * crypto_rsa_key_read - Read an RSA key
1296 * @file: File from which to read (PEM encoded, can be X.509v3 certificate)
1297 * @private_key: Whether to read the private key instead of public key
1298 * Returns: RSA key or %NULL on failure
1299 */
1300struct crypto_rsa_key * crypto_rsa_key_read(const char *file, bool private_key);
1301
1302/**
1303 * crypto_rsa_oaep_sha256_encrypt - RSA-OAEP-SHA-256 encryption
1304 * @key: RSA key from crypto_rsa_key_read()
1305 * @in: Plaintext input data
1306 * Returns: Encrypted output data or %NULL on failure
1307 */
1308struct wpabuf * crypto_rsa_oaep_sha256_encrypt(struct crypto_rsa_key *key,
1309 const struct wpabuf *in);
1310
1311/**
1312 * crypto_rsa_oaep_sha256_decrypt - RSA-OAEP-SHA-256 decryption
1313 * @key: RSA key from crypto_rsa_key_read()
1314 * @in: Encrypted input data
1315 * Returns: Decrypted output data or %NULL on failure
1316 */
1317struct wpabuf * crypto_rsa_oaep_sha256_decrypt(struct crypto_rsa_key *key,
1318 const struct wpabuf *in);
1319
1320/**
1321 * crypto_rsa_key_free - Free an RSA key
1322 * @key: RSA key from crypto_rsa_key_read()
1323 */
1324void crypto_rsa_key_free(struct crypto_rsa_key *key);
1325
Sunil Ravi89eba102022-09-13 21:04:37 -07001326enum hpke_mode {
1327 HPKE_MODE_BASE = 0x00,
1328 HPKE_MODE_PSK = 0x01,
1329 HPKE_MODE_AUTH = 0x02,
1330 HPKE_MODE_AUTH_PSK = 0x03,
1331};
1332
1333enum hpke_kem_id {
1334 HPKE_DHKEM_P256_HKDF_SHA256 = 0x0010,
1335 HPKE_DHKEM_P384_HKDF_SHA384 = 0x0011,
1336 HPKE_DHKEM_P521_HKDF_SHA512 = 0x0012,
1337 HPKE_DHKEM_X5519_HKDF_SHA256 = 0x0020,
1338 HPKE_DHKEM_X448_HKDF_SHA512 = 0x0021,
1339};
1340
1341enum hpke_kdf_id {
1342 HPKE_KDF_HKDF_SHA256 = 0x0001,
1343 HPKE_KDF_HKDF_SHA384 = 0x0002,
1344 HPKE_KDF_HKDF_SHA512 = 0x0003,
1345};
1346
1347enum hpke_aead_id {
1348 HPKE_AEAD_AES_128_GCM = 0x0001,
1349 HPKE_AEAD_AES_256_GCM = 0x0002,
1350 HPKE_AEAD_CHACHA20POLY1305 = 0x0003,
1351};
1352
1353/**
1354 * hpke_base_seal - HPKE base mode single-shot encrypt
1355 * Returns: enc | ct; or %NULL on failure
1356 */
1357struct wpabuf * hpke_base_seal(enum hpke_kem_id kem_id,
1358 enum hpke_kdf_id kdf_id,
1359 enum hpke_aead_id aead_id,
1360 struct crypto_ec_key *peer_pub,
1361 const u8 *info, size_t info_len,
1362 const u8 *aad, size_t aad_len,
1363 const u8 *pt, size_t pt_len);
1364
1365/**
1366 * hpke_base_open - HPKE base mode single-shot decrypt
1367 * @enc_ct: enc | ct
1368 * Returns: pt; or %NULL on failure
1369 */
1370struct wpabuf * hpke_base_open(enum hpke_kem_id kem_id,
1371 enum hpke_kdf_id kdf_id,
1372 enum hpke_aead_id aead_id,
1373 struct crypto_ec_key *own_priv,
1374 const u8 *info, size_t info_len,
1375 const u8 *aad, size_t aad_len,
1376 const u8 *enc_ct, size_t enc_ct_len);
1377
Sunil Ravia04bd252022-05-02 22:54:18 -07001378/**
1379 * crypto_unload - Unload crypto resources
1380 *
1381 * This function is called just before the process exits to allow dynamic
1382 * resource allocations to be freed.
1383 */
1384void crypto_unload(void);
1385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386#endif /* CRYPTO_H */