crypto.h 25 KB

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