crypto.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. struct crypto_public_key *
  244. crypto_public_key_import_parts(const u8 *n, size_t n_len,
  245. const u8 *e, size_t e_len);
  246. /**
  247. * crypto_private_key_import - Import an RSA private key
  248. * @key: Key buffer (DER encoded RSA private key)
  249. * @len: Key buffer length in bytes
  250. * @passwd: Key encryption password or %NULL if key is not encrypted
  251. * Returns: Pointer to the private key or %NULL on failure
  252. *
  253. * This function is only used with internal TLSv1 implementation
  254. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  255. * to implement this.
  256. */
  257. struct crypto_private_key * crypto_private_key_import(const u8 *key,
  258. size_t len,
  259. const char *passwd);
  260. /**
  261. * crypto_public_key_from_cert - Import an RSA public key from a certificate
  262. * @buf: DER encoded X.509 certificate
  263. * @len: Certificate buffer length in bytes
  264. * Returns: Pointer to public key or %NULL on failure
  265. *
  266. * This function can just return %NULL if the crypto library does not support
  267. * X.509 parsing. In that case, internal code will be used to parse the
  268. * certificate and public key is imported using crypto_public_key_import().
  269. *
  270. * This function is only used with internal TLSv1 implementation
  271. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  272. * to implement this.
  273. */
  274. struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
  275. size_t len);
  276. /**
  277. * crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5)
  278. * @key: Public key
  279. * @in: Plaintext buffer
  280. * @inlen: Length of plaintext buffer in bytes
  281. * @out: Output buffer for encrypted data
  282. * @outlen: Length of output buffer in bytes; set to used length on success
  283. * Returns: 0 on success, -1 on failure
  284. *
  285. * This function is only used with internal TLSv1 implementation
  286. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  287. * to implement this.
  288. */
  289. int __must_check crypto_public_key_encrypt_pkcs1_v15(
  290. struct crypto_public_key *key, const u8 *in, size_t inlen,
  291. u8 *out, size_t *outlen);
  292. /**
  293. * crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5)
  294. * @key: Private key
  295. * @in: Encrypted buffer
  296. * @inlen: Length of encrypted buffer in bytes
  297. * @out: Output buffer for encrypted data
  298. * @outlen: Length of output buffer in bytes; set to used length on success
  299. * Returns: 0 on success, -1 on failure
  300. *
  301. * This function is only used with internal TLSv1 implementation
  302. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  303. * to implement this.
  304. */
  305. int __must_check crypto_private_key_decrypt_pkcs1_v15(
  306. struct crypto_private_key *key, const u8 *in, size_t inlen,
  307. u8 *out, size_t *outlen);
  308. /**
  309. * crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1)
  310. * @key: Private key from crypto_private_key_import()
  311. * @in: Plaintext buffer
  312. * @inlen: Length of plaintext buffer in bytes
  313. * @out: Output buffer for encrypted (signed) data
  314. * @outlen: Length of output buffer in bytes; set to used length on success
  315. * Returns: 0 on success, -1 on failure
  316. *
  317. * This function is only used with internal TLSv1 implementation
  318. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  319. * to implement this.
  320. */
  321. int __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
  322. const u8 *in, size_t inlen,
  323. u8 *out, size_t *outlen);
  324. /**
  325. * crypto_public_key_free - Free public key
  326. * @key: Public key
  327. *
  328. * This function is only used with internal TLSv1 implementation
  329. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  330. * to implement this.
  331. */
  332. void crypto_public_key_free(struct crypto_public_key *key);
  333. /**
  334. * crypto_private_key_free - Free private key
  335. * @key: Private key from crypto_private_key_import()
  336. *
  337. * This function is only used with internal TLSv1 implementation
  338. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  339. * to implement this.
  340. */
  341. void crypto_private_key_free(struct crypto_private_key *key);
  342. /**
  343. * crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature
  344. * @key: Public key
  345. * @crypt: Encrypted signature data (using the private key)
  346. * @crypt_len: Encrypted signature data length
  347. * @plain: Buffer for plaintext (at least crypt_len bytes)
  348. * @plain_len: Plaintext length (max buffer size on input, real len on output);
  349. * Returns: 0 on success, -1 on failure
  350. */
  351. int __must_check crypto_public_key_decrypt_pkcs1(
  352. struct crypto_public_key *key, const u8 *crypt, size_t crypt_len,
  353. u8 *plain, size_t *plain_len);
  354. /**
  355. * crypto_global_init - Initialize crypto wrapper
  356. *
  357. * This function is only used with internal TLSv1 implementation
  358. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  359. * to implement this.
  360. */
  361. int __must_check crypto_global_init(void);
  362. /**
  363. * crypto_global_deinit - Deinitialize crypto wrapper
  364. *
  365. * This function is only used with internal TLSv1 implementation
  366. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  367. * to implement this.
  368. */
  369. void crypto_global_deinit(void);
  370. /**
  371. * crypto_mod_exp - Modular exponentiation of large integers
  372. * @base: Base integer (big endian byte array)
  373. * @base_len: Length of base integer in bytes
  374. * @power: Power integer (big endian byte array)
  375. * @power_len: Length of power integer in bytes
  376. * @modulus: Modulus integer (big endian byte array)
  377. * @modulus_len: Length of modulus integer in bytes
  378. * @result: Buffer for the result
  379. * @result_len: Result length (max buffer size on input, real len on output)
  380. * Returns: 0 on success, -1 on failure
  381. *
  382. * This function calculates result = base ^ power mod modulus. modules_len is
  383. * used as the maximum size of modulus buffer. It is set to the used size on
  384. * success.
  385. *
  386. * This function is only used with internal TLSv1 implementation
  387. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  388. * to implement this.
  389. */
  390. int __must_check crypto_mod_exp(const u8 *base, size_t base_len,
  391. const u8 *power, size_t power_len,
  392. const u8 *modulus, size_t modulus_len,
  393. u8 *result, size_t *result_len);
  394. /**
  395. * rc4_skip - XOR RC4 stream to given data with skip-stream-start
  396. * @key: RC4 key
  397. * @keylen: RC4 key length
  398. * @skip: number of bytes to skip from the beginning of the RC4 stream
  399. * @data: data to be XOR'ed with RC4 stream
  400. * @data_len: buf length
  401. * Returns: 0 on success, -1 on failure
  402. *
  403. * Generate RC4 pseudo random stream for the given key, skip beginning of the
  404. * stream, and XOR the end result with the data buffer to perform RC4
  405. * encryption/decryption.
  406. */
  407. int rc4_skip(const u8 *key, size_t keylen, size_t skip,
  408. u8 *data, size_t data_len);
  409. /**
  410. * crypto_get_random - Generate cryptographically strong pseudy-random bytes
  411. * @buf: Buffer for data
  412. * @len: Number of bytes to generate
  413. * Returns: 0 on success, -1 on failure
  414. *
  415. * If the PRNG does not have enough entropy to ensure unpredictable byte
  416. * sequence, this functions must return -1.
  417. */
  418. int crypto_get_random(void *buf, size_t len);
  419. /**
  420. * struct crypto_bignum - bignum
  421. *
  422. * Internal data structure for bignum implementation. The contents is specific
  423. * to the used crypto library.
  424. */
  425. struct crypto_bignum;
  426. /**
  427. * crypto_bignum_init - Allocate memory for bignum
  428. * Returns: Pointer to allocated bignum or %NULL on failure
  429. */
  430. struct crypto_bignum * crypto_bignum_init(void);
  431. /**
  432. * crypto_bignum_init_set - Allocate memory for bignum and set the value
  433. * @buf: Buffer with unsigned binary value
  434. * @len: Length of buf in octets
  435. * Returns: Pointer to allocated bignum or %NULL on failure
  436. */
  437. struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len);
  438. /**
  439. * crypto_bignum_deinit - Free bignum
  440. * @n: Bignum from crypto_bignum_init() or crypto_bignum_init_set()
  441. * @clear: Whether to clear the value from memory
  442. */
  443. void crypto_bignum_deinit(struct crypto_bignum *n, int clear);
  444. /**
  445. * crypto_bignum_to_bin - Set binary buffer to unsigned bignum
  446. * @a: Bignum
  447. * @buf: Buffer for the binary number
  448. * @len: Length of @buf in octets
  449. * @padlen: Length in octets to pad the result to or 0 to indicate no padding
  450. * Returns: Number of octets written on success, -1 on failure
  451. */
  452. int crypto_bignum_to_bin(const struct crypto_bignum *a,
  453. u8 *buf, size_t buflen, size_t padlen);
  454. /**
  455. * crypto_bignum_add - c = a + b
  456. * @a: Bignum
  457. * @b: Bignum
  458. * @c: Bignum; used to store the result of a + b
  459. * Returns: 0 on success, -1 on failure
  460. */
  461. int crypto_bignum_add(const struct crypto_bignum *a,
  462. const struct crypto_bignum *b,
  463. struct crypto_bignum *c);
  464. /**
  465. * crypto_bignum_mod - c = a % b
  466. * @a: Bignum
  467. * @b: Bignum
  468. * @c: Bignum; used to store the result of a % b
  469. * Returns: 0 on success, -1 on failure
  470. */
  471. int crypto_bignum_mod(const struct crypto_bignum *a,
  472. const struct crypto_bignum *b,
  473. struct crypto_bignum *c);
  474. /**
  475. * crypto_bignum_exptmod - Modular exponentiation: d = a^b (mod c)
  476. * @a: Bignum; base
  477. * @b: Bignum; exponent
  478. * @c: Bignum; modulus
  479. * @d: Bignum; used to store the result of a^b (mod c)
  480. * Returns: 0 on success, -1 on failure
  481. */
  482. int crypto_bignum_exptmod(const struct crypto_bignum *a,
  483. const struct crypto_bignum *b,
  484. const struct crypto_bignum *c,
  485. struct crypto_bignum *d);
  486. /**
  487. * crypto_bignum_inverse - Inverse a bignum so that a * c = 1 (mod b)
  488. * @a: Bignum
  489. * @b: Bignum
  490. * @c: Bignum; used to store the result
  491. * Returns: 0 on success, -1 on failure
  492. */
  493. int crypto_bignum_inverse(const struct crypto_bignum *a,
  494. const struct crypto_bignum *b,
  495. struct crypto_bignum *c);
  496. /**
  497. * crypto_bignum_sub - c = a - b
  498. * @a: Bignum
  499. * @b: Bignum
  500. * @c: Bignum; used to store the result of a - b
  501. * Returns: 0 on success, -1 on failure
  502. */
  503. int crypto_bignum_sub(const struct crypto_bignum *a,
  504. const struct crypto_bignum *b,
  505. struct crypto_bignum *c);
  506. /**
  507. * crypto_bignum_div - c = a / b
  508. * @a: Bignum
  509. * @b: Bignum
  510. * @c: Bignum; used to store the result of a / b
  511. * Returns: 0 on success, -1 on failure
  512. */
  513. int crypto_bignum_div(const struct crypto_bignum *a,
  514. const struct crypto_bignum *b,
  515. struct crypto_bignum *c);
  516. /**
  517. * crypto_bignum_mulmod - d = a * b (mod c)
  518. * @a: Bignum
  519. * @b: Bignum
  520. * @c: Bignum
  521. * @d: Bignum; used to store the result of (a * b) % c
  522. * Returns: 0 on success, -1 on failure
  523. */
  524. int crypto_bignum_mulmod(const struct crypto_bignum *a,
  525. const struct crypto_bignum *b,
  526. const struct crypto_bignum *c,
  527. struct crypto_bignum *d);
  528. /**
  529. * crypto_bignum_cmp - Compare two bignums
  530. * @a: Bignum
  531. * @b: Bignum
  532. * Returns: -1 if a < b, 0 if a == b, or 1 if a > b
  533. */
  534. int crypto_bignum_cmp(const struct crypto_bignum *a,
  535. const struct crypto_bignum *b);
  536. /**
  537. * crypto_bignum_bits - Get size of a bignum in bits
  538. * @a: Bignum
  539. * Returns: Number of bits in the bignum
  540. */
  541. int crypto_bignum_bits(const struct crypto_bignum *a);
  542. /**
  543. * crypto_bignum_is_zero - Is the given bignum zero
  544. * @a: Bignum
  545. * Returns: 1 if @a is zero or 0 if not
  546. */
  547. int crypto_bignum_is_zero(const struct crypto_bignum *a);
  548. /**
  549. * crypto_bignum_is_one - Is the given bignum one
  550. * @a: Bignum
  551. * Returns: 1 if @a is one or 0 if not
  552. */
  553. int crypto_bignum_is_one(const struct crypto_bignum *a);
  554. /**
  555. * crypto_bignum_legendre - Compute the Legendre symbol (a/p)
  556. * @a: Bignum
  557. * @p: Bignum
  558. * Returns: Legendre symbol -1,0,1 on success; -2 on calculation failure
  559. */
  560. int crypto_bignum_legendre(const struct crypto_bignum *a,
  561. const struct crypto_bignum *p);
  562. /**
  563. * struct crypto_ec - Elliptic curve context
  564. *
  565. * Internal data structure for EC implementation. The contents is specific
  566. * to the used crypto library.
  567. */
  568. struct crypto_ec;
  569. /**
  570. * crypto_ec_init - Initialize elliptic curve context
  571. * @group: Identifying number for the ECC group (IANA "Group Description"
  572. * attribute registrty for RFC 2409)
  573. * Returns: Pointer to EC context or %NULL on failure
  574. */
  575. struct crypto_ec * crypto_ec_init(int group);
  576. /**
  577. * crypto_ec_deinit - Deinitialize elliptic curve context
  578. * @e: EC context from crypto_ec_init()
  579. */
  580. void crypto_ec_deinit(struct crypto_ec *e);
  581. /**
  582. * crypto_ec_prime_len - Get length of the prime in octets
  583. * @e: EC context from crypto_ec_init()
  584. * Returns: Length of the prime defining the group
  585. */
  586. size_t crypto_ec_prime_len(struct crypto_ec *e);
  587. /**
  588. * crypto_ec_prime_len_bits - Get length of the prime in bits
  589. * @e: EC context from crypto_ec_init()
  590. * Returns: Length of the prime defining the group in bits
  591. */
  592. size_t crypto_ec_prime_len_bits(struct crypto_ec *e);
  593. /**
  594. * crypto_ec_get_prime - Get prime defining an EC group
  595. * @e: EC context from crypto_ec_init()
  596. * Returns: Prime (bignum) defining the group
  597. */
  598. const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e);
  599. /**
  600. * crypto_ec_get_order - Get order of an EC group
  601. * @e: EC context from crypto_ec_init()
  602. * Returns: Order (bignum) of the group
  603. */
  604. const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e);
  605. /**
  606. * struct crypto_ec_point - Elliptic curve point
  607. *
  608. * Internal data structure for EC implementation to represent a point. The
  609. * contents is specific to the used crypto library.
  610. */
  611. struct crypto_ec_point;
  612. /**
  613. * crypto_ec_point_init - Initialize data for an EC point
  614. * @e: EC context from crypto_ec_init()
  615. * Returns: Pointer to EC point data or %NULL on failure
  616. */
  617. struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e);
  618. /**
  619. * crypto_ec_point_deinit - Deinitialize EC point data
  620. * @p: EC point data from crypto_ec_point_init()
  621. * @clear: Whether to clear the EC point value from memory
  622. */
  623. void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear);
  624. /**
  625. * crypto_ec_point_to_bin - Write EC point value as binary data
  626. * @e: EC context from crypto_ec_init()
  627. * @p: EC point data from crypto_ec_point_init()
  628. * @x: Buffer for writing the binary data for x coordinate or %NULL if not used
  629. * @y: Buffer for writing the binary data for y coordinate or %NULL if not used
  630. * Returns: 0 on success, -1 on failure
  631. *
  632. * This function can be used to write an EC point as binary data in a format
  633. * that has the x and y coordinates in big endian byte order fields padded to
  634. * the length of the prime defining the group.
  635. */
  636. int crypto_ec_point_to_bin(struct crypto_ec *e,
  637. const struct crypto_ec_point *point, u8 *x, u8 *y);
  638. /**
  639. * crypto_ec_point_from_bin - Create EC point from binary data
  640. * @e: EC context from crypto_ec_init()
  641. * @val: Binary data to read the EC point from
  642. * Returns: Pointer to EC point data or %NULL on failure
  643. *
  644. * This function readers x and y coordinates of the EC point from the provided
  645. * buffer assuming the values are in big endian byte order with fields padded to
  646. * the length of the prime defining the group.
  647. */
  648. struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
  649. const u8 *val);
  650. /**
  651. * crypto_bignum_add - c = a + b
  652. * @e: EC context from crypto_ec_init()
  653. * @a: Bignum
  654. * @b: Bignum
  655. * @c: Bignum; used to store the result of a + b
  656. * Returns: 0 on success, -1 on failure
  657. */
  658. int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
  659. const struct crypto_ec_point *b,
  660. struct crypto_ec_point *c);
  661. /**
  662. * crypto_bignum_mul - res = b * p
  663. * @e: EC context from crypto_ec_init()
  664. * @p: EC point
  665. * @b: Bignum
  666. * @res: EC point; used to store the result of b * p
  667. * Returns: 0 on success, -1 on failure
  668. */
  669. int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
  670. const struct crypto_bignum *b,
  671. struct crypto_ec_point *res);
  672. /**
  673. * crypto_ec_point_invert - Compute inverse of an EC point
  674. * @e: EC context from crypto_ec_init()
  675. * @p: EC point to invert (and result of the operation)
  676. * Returns: 0 on success, -1 on failure
  677. */
  678. int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p);
  679. /**
  680. * crypto_ec_point_solve_y_coord - Solve y coordinate for an x coordinate
  681. * @e: EC context from crypto_ec_init()
  682. * @p: EC point to use for the returning the result
  683. * @x: x coordinate
  684. * @y_bit: y-bit (0 or 1) for selecting the y value to use
  685. * Returns: 0 on success, -1 on failure
  686. */
  687. int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
  688. struct crypto_ec_point *p,
  689. const struct crypto_bignum *x, int y_bit);
  690. /**
  691. * crypto_ec_point_compute_y_sqr - Compute y^2 = x^3 + ax + b
  692. * @e: EC context from crypto_ec_init()
  693. * @x: x coordinate
  694. * Returns: y^2 on success, %NULL failure
  695. */
  696. struct crypto_bignum *
  697. crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
  698. const struct crypto_bignum *x);
  699. /**
  700. * crypto_ec_point_is_at_infinity - Check whether EC point is neutral element
  701. * @e: EC context from crypto_ec_init()
  702. * @p: EC point
  703. * Returns: 1 if the specified EC point is the neutral element of the group or
  704. * 0 if not
  705. */
  706. int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
  707. const struct crypto_ec_point *p);
  708. /**
  709. * crypto_ec_point_is_on_curve - Check whether EC point is on curve
  710. * @e: EC context from crypto_ec_init()
  711. * @p: EC point
  712. * Returns: 1 if the specified EC point is on the curve or 0 if not
  713. */
  714. int crypto_ec_point_is_on_curve(struct crypto_ec *e,
  715. const struct crypto_ec_point *p);
  716. /**
  717. * crypto_ec_point_cmp - Compare two EC points
  718. * @e: EC context from crypto_ec_init()
  719. * @a: EC point
  720. * @b: EC point
  721. * Returns: 0 on equal, non-zero otherwise
  722. */
  723. int crypto_ec_point_cmp(const struct crypto_ec *e,
  724. const struct crypto_ec_point *a,
  725. const struct crypto_ec_point *b);
  726. #endif /* CRYPTO_H */