crypto.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * WPA Supplicant / wrapper functions for crypto libraries
  3. * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. *
  14. * This file defines the cryptographic functions that need to be implemented
  15. * for wpa_supplicant and hostapd. When TLS is not used, internal
  16. * implementation of MD5, SHA1, and AES is used and no external libraries are
  17. * required. When TLS is enabled (e.g., by enabling EAP-TLS or EAP-PEAP), the
  18. * crypto library used by the TLS implementation is expected to be used for
  19. * non-TLS needs, too, in order to save space by not implementing these
  20. * functions twice.
  21. *
  22. * Wrapper code for using each crypto library is in its own file (crypto*.c)
  23. * and one of these files is build and linked in to provide the functions
  24. * defined here.
  25. */
  26. #ifndef CRYPTO_H
  27. #define CRYPTO_H
  28. /**
  29. * md4_vector - MD4 hash for data vector
  30. * @num_elem: Number of elements in the data vector
  31. * @addr: Pointers to the data areas
  32. * @len: Lengths of the data blocks
  33. * @mac: Buffer for the hash
  34. */
  35. void md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
  36. /**
  37. * md5_vector - MD5 hash for data vector
  38. * @num_elem: Number of elements in the data vector
  39. * @addr: Pointers to the data areas
  40. * @len: Lengths of the data blocks
  41. * @mac: Buffer for the hash
  42. */
  43. void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
  44. /**
  45. * sha1_vector - SHA-1 hash for data vector
  46. * @num_elem: Number of elements in the data vector
  47. * @addr: Pointers to the data areas
  48. * @len: Lengths of the data blocks
  49. * @mac: Buffer for the hash
  50. */
  51. void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  52. u8 *mac);
  53. /**
  54. * fips186_2-prf - NIST FIPS Publication 186-2 change notice 1 PRF
  55. * @seed: Seed/key for the PRF
  56. * @seed_len: Seed length in bytes
  57. * @x: Buffer for PRF output
  58. * @xlen: Output length in bytes
  59. * Returns: 0 on success, -1 on failure
  60. *
  61. * This function implements random number generation specified in NIST FIPS
  62. * Publication 186-2 for EAP-SIM. This PRF uses a function that is similar to
  63. * SHA-1, but has different message padding.
  64. */
  65. int __must_check fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x,
  66. size_t xlen);
  67. /**
  68. * sha256_vector - SHA256 hash for data vector
  69. * @num_elem: Number of elements in the data vector
  70. * @addr: Pointers to the data areas
  71. * @len: Lengths of the data blocks
  72. * @mac: Buffer for the hash
  73. */
  74. void sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  75. u8 *mac);
  76. /**
  77. * des_encrypt - Encrypt one block with DES
  78. * @clear: 8 octets (in)
  79. * @key: 7 octets (in) (no parity bits included)
  80. * @cypher: 8 octets (out)
  81. */
  82. void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
  83. /**
  84. * aes_encrypt_init - Initialize AES for encryption
  85. * @key: Encryption key
  86. * @len: Key length in bytes (usually 16, i.e., 128 bits)
  87. * Returns: Pointer to context data or %NULL on failure
  88. */
  89. void * aes_encrypt_init(const u8 *key, size_t len);
  90. /**
  91. * aes_encrypt - Encrypt one AES block
  92. * @ctx: Context pointer from aes_encrypt_init()
  93. * @plain: Plaintext data to be encrypted (16 bytes)
  94. * @crypt: Buffer for the encrypted data (16 bytes)
  95. */
  96. void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
  97. /**
  98. * aes_encrypt_deinit - Deinitialize AES encryption
  99. * @ctx: Context pointer from aes_encrypt_init()
  100. */
  101. void aes_encrypt_deinit(void *ctx);
  102. /**
  103. * aes_decrypt_init - Initialize AES for decryption
  104. * @key: Decryption key
  105. * @len: Key length in bytes (usually 16, i.e., 128 bits)
  106. * Returns: Pointer to context data or %NULL on failure
  107. */
  108. void * aes_decrypt_init(const u8 *key, size_t len);
  109. /**
  110. * aes_decrypt - Decrypt one AES block
  111. * @ctx: Context pointer from aes_encrypt_init()
  112. * @crypt: Encrypted data (16 bytes)
  113. * @plain: Buffer for the decrypted data (16 bytes)
  114. */
  115. void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
  116. /**
  117. * aes_decrypt_deinit - Deinitialize AES decryption
  118. * @ctx: Context pointer from aes_encrypt_init()
  119. */
  120. void aes_decrypt_deinit(void *ctx);
  121. enum crypto_hash_alg {
  122. CRYPTO_HASH_ALG_MD5, CRYPTO_HASH_ALG_SHA1,
  123. CRYPTO_HASH_ALG_HMAC_MD5, CRYPTO_HASH_ALG_HMAC_SHA1
  124. };
  125. struct crypto_hash;
  126. /**
  127. * crypto_hash_init - Initialize hash/HMAC function
  128. * @alg: Hash algorithm
  129. * @key: Key for keyed hash (e.g., HMAC) or %NULL if not needed
  130. * @key_len: Length of the key in bytes
  131. * Returns: Pointer to hash context to use with other hash functions or %NULL
  132. * on failure
  133. *
  134. * This function is only used with internal TLSv1 implementation
  135. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  136. * to implement this.
  137. */
  138. struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
  139. size_t key_len);
  140. /**
  141. * crypto_hash_update - Add data to hash calculation
  142. * @ctx: Context pointer from crypto_hash_init()
  143. * @data: Data buffer to add
  144. * @len: Length of the buffer
  145. *
  146. * This function is only used with internal TLSv1 implementation
  147. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  148. * to implement this.
  149. */
  150. void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len);
  151. /**
  152. * crypto_hash_finish - Complete hash calculation
  153. * @ctx: Context pointer from crypto_hash_init()
  154. * @hash: Buffer for hash value or %NULL if caller is just freeing the hash
  155. * context
  156. * @len: Pointer to length of the buffer or %NULL if caller is just freeing the
  157. * hash context; on return, this is set to the actual length of the hash value
  158. * Returns: 0 on success, -1 if buffer is too small (len set to needed length),
  159. * or -2 on other failures (including failed crypto_hash_update() operations)
  160. *
  161. * This function calculates the hash value and frees the context buffer that
  162. * was used for hash calculation.
  163. *
  164. * This function is only used with internal TLSv1 implementation
  165. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  166. * to implement this.
  167. */
  168. int crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len);
  169. enum crypto_cipher_alg {
  170. CRYPTO_CIPHER_NULL = 0, CRYPTO_CIPHER_ALG_AES, CRYPTO_CIPHER_ALG_3DES,
  171. CRYPTO_CIPHER_ALG_DES, CRYPTO_CIPHER_ALG_RC2, CRYPTO_CIPHER_ALG_RC4
  172. };
  173. struct crypto_cipher;
  174. /**
  175. * crypto_cipher_init - Initialize block/stream cipher function
  176. * @alg: Cipher algorithm
  177. * @iv: Initialization vector for block ciphers or %NULL for stream ciphers
  178. * @key: Cipher key
  179. * @key_len: Length of key in bytes
  180. * Returns: Pointer to cipher context to use with other cipher functions or
  181. * %NULL on failure
  182. *
  183. * This function is only used with internal TLSv1 implementation
  184. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  185. * to implement this.
  186. */
  187. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  188. const u8 *iv, const u8 *key,
  189. size_t key_len);
  190. /**
  191. * crypto_cipher_encrypt - Cipher encrypt
  192. * @ctx: Context pointer from crypto_cipher_init()
  193. * @plain: Plaintext to cipher
  194. * @crypt: Resulting ciphertext
  195. * @len: Length of the plaintext
  196. * Returns: 0 on success, -1 on failure
  197. *
  198. * This function is only used with internal TLSv1 implementation
  199. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  200. * to implement this.
  201. */
  202. int __must_check crypto_cipher_encrypt(struct crypto_cipher *ctx,
  203. const u8 *plain, u8 *crypt, size_t len);
  204. /**
  205. * crypto_cipher_decrypt - Cipher decrypt
  206. * @ctx: Context pointer from crypto_cipher_init()
  207. * @crypt: Ciphertext to decrypt
  208. * @plain: Resulting plaintext
  209. * @len: Length of the cipher text
  210. * Returns: 0 on success, -1 on failure
  211. *
  212. * This function is only used with internal TLSv1 implementation
  213. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  214. * to implement this.
  215. */
  216. int __must_check crypto_cipher_decrypt(struct crypto_cipher *ctx,
  217. const u8 *crypt, u8 *plain, size_t len);
  218. /**
  219. * crypto_cipher_decrypt - Free cipher context
  220. * @ctx: Context pointer from crypto_cipher_init()
  221. *
  222. * This function is only used with internal TLSv1 implementation
  223. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  224. * to implement this.
  225. */
  226. void crypto_cipher_deinit(struct crypto_cipher *ctx);
  227. struct crypto_public_key;
  228. struct crypto_private_key;
  229. /**
  230. * crypto_public_key_import - Import an RSA public key
  231. * @key: Key buffer (DER encoded RSA public key)
  232. * @len: Key buffer length in bytes
  233. * Returns: Pointer to the public key or %NULL on failure
  234. *
  235. * This function can just return %NULL if the crypto library supports X.509
  236. * parsing. In that case, crypto_public_key_from_cert() is used to import the
  237. * public key from a certificate.
  238. *
  239. * This function is only used with internal TLSv1 implementation
  240. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  241. * to implement this.
  242. */
  243. struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len);
  244. /**
  245. * crypto_private_key_import - Import an RSA private key
  246. * @key: Key buffer (DER encoded RSA private key)
  247. * @len: Key buffer length in bytes
  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. /**
  257. * crypto_public_key_from_cert - Import an RSA public key from a certificate
  258. * @buf: DER encoded X.509 certificate
  259. * @len: Certificate buffer length in bytes
  260. * Returns: Pointer to public key or %NULL on failure
  261. *
  262. * This function can just return %NULL if the crypto library does not support
  263. * X.509 parsing. In that case, internal code will be used to parse the
  264. * certificate and public key is imported using crypto_public_key_import().
  265. *
  266. * This function is only used with internal TLSv1 implementation
  267. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  268. * to implement this.
  269. */
  270. struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
  271. size_t len);
  272. /**
  273. * crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5)
  274. * @key: Public key
  275. * @in: Plaintext buffer
  276. * @inlen: Length of plaintext buffer in bytes
  277. * @out: Output buffer for encrypted data
  278. * @outlen: Length of output buffer in bytes; set to used length on success
  279. * Returns: 0 on success, -1 on failure
  280. *
  281. * This function is only used with internal TLSv1 implementation
  282. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  283. * to implement this.
  284. */
  285. int __must_check crypto_public_key_encrypt_pkcs1_v15(
  286. struct crypto_public_key *key, const u8 *in, size_t inlen,
  287. u8 *out, size_t *outlen);
  288. /**
  289. * crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5)
  290. * @key: Private key
  291. * @in: Encrypted buffer
  292. * @inlen: Length of encrypted buffer in bytes
  293. * @out: Output buffer for encrypted data
  294. * @outlen: Length of output buffer in bytes; set to used length on success
  295. * Returns: 0 on success, -1 on failure
  296. *
  297. * This function is only used with internal TLSv1 implementation
  298. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  299. * to implement this.
  300. */
  301. int __must_check crypto_private_key_decrypt_pkcs1_v15(
  302. struct crypto_private_key *key, const u8 *in, size_t inlen,
  303. u8 *out, size_t *outlen);
  304. /**
  305. * crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1)
  306. * @key: Private key from crypto_private_key_import()
  307. * @in: Plaintext buffer
  308. * @inlen: Length of plaintext buffer in bytes
  309. * @out: Output buffer for encrypted (signed) data
  310. * @outlen: Length of output buffer in bytes; set to used length on success
  311. * Returns: 0 on success, -1 on failure
  312. *
  313. * This function is only used with internal TLSv1 implementation
  314. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  315. * to implement this.
  316. */
  317. int __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
  318. const u8 *in, size_t inlen,
  319. u8 *out, size_t *outlen);
  320. /**
  321. * crypto_public_key_free - Free public key
  322. * @key: Public key
  323. *
  324. * This function is only used with internal TLSv1 implementation
  325. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  326. * to implement this.
  327. */
  328. void crypto_public_key_free(struct crypto_public_key *key);
  329. /**
  330. * crypto_private_key_free - Free private key
  331. * @key: Private key from crypto_private_key_import()
  332. *
  333. * This function is only used with internal TLSv1 implementation
  334. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  335. * to implement this.
  336. */
  337. void crypto_private_key_free(struct crypto_private_key *key);
  338. /**
  339. * crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature
  340. * @key: Public key
  341. * @crypt: Encrypted signature data (using the private key)
  342. * @crypt_len: Encrypted signature data length
  343. * @plain: Buffer for plaintext (at least crypt_len bytes)
  344. * @plain_len: Plaintext length (max buffer size on input, real len on output);
  345. * Returns: 0 on success, -1 on failure
  346. */
  347. int __must_check crypto_public_key_decrypt_pkcs1(
  348. struct crypto_public_key *key, const u8 *crypt, size_t crypt_len,
  349. u8 *plain, size_t *plain_len);
  350. /**
  351. * crypto_global_init - Initialize crypto wrapper
  352. *
  353. * This function is only used with internal TLSv1 implementation
  354. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  355. * to implement this.
  356. */
  357. int __must_check crypto_global_init(void);
  358. /**
  359. * crypto_global_deinit - Deinitialize crypto wrapper
  360. *
  361. * This function is only used with internal TLSv1 implementation
  362. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  363. * to implement this.
  364. */
  365. void crypto_global_deinit(void);
  366. /**
  367. * crypto_mod_exp - Modular exponentiation of large integers
  368. * @base: Base integer (big endian byte array)
  369. * @base_len: Length of base integer in bytes
  370. * @power: Power integer (big endian byte array)
  371. * @power_len: Length of power integer in bytes
  372. * @modulus: Modulus integer (big endian byte array)
  373. * @modulus_len: Length of modulus integer in bytes
  374. * @result: Buffer for the result
  375. * @result_len: Result length (max buffer size on input, real len on output)
  376. * Returns: 0 on success, -1 on failure
  377. *
  378. * This function calculates result = base ^ power mod modulus. modules_len is
  379. * used as the maximum size of modulus buffer. It is set to the used size on
  380. * success.
  381. *
  382. * This function is only used with internal TLSv1 implementation
  383. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  384. * to implement this.
  385. */
  386. int __must_check crypto_mod_exp(const u8 *base, size_t base_len,
  387. const u8 *power, size_t power_len,
  388. const u8 *modulus, size_t modulus_len,
  389. u8 *result, size_t *result_len);
  390. #endif /* CRYPTO_H */