crypto.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * WPA Supplicant / wrapper functions for crypto libraries
  3. * Copyright (c) 2004-2009, 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. * Returns: 0 on success, -1 on failure
  35. */
  36. int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
  37. /**
  38. * md5_vector - MD5 hash for data vector
  39. * @num_elem: Number of elements in the data vector
  40. * @addr: Pointers to the data areas
  41. * @len: Lengths of the data blocks
  42. * @mac: Buffer for the hash
  43. * Returns: 0 on success, -1 on failure
  44. */
  45. int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
  46. #ifdef CONFIG_FIPS
  47. /**
  48. * md5_vector_non_fips_allow - MD5 hash for data vector (non-FIPS use allowed)
  49. * @num_elem: Number of elements in the data vector
  50. * @addr: Pointers to the data areas
  51. * @len: Lengths of the data blocks
  52. * @mac: Buffer for the hash
  53. * Returns: 0 on success, -1 on failure
  54. */
  55. int md5_vector_non_fips_allow(size_t num_elem, const u8 *addr[],
  56. const size_t *len, u8 *mac);
  57. #else /* CONFIG_FIPS */
  58. #define md5_vector_non_fips_allow md5_vector
  59. #endif /* CONFIG_FIPS */
  60. /**
  61. * sha1_vector - SHA-1 hash for data vector
  62. * @num_elem: Number of elements in the data vector
  63. * @addr: Pointers to the data areas
  64. * @len: Lengths of the data blocks
  65. * @mac: Buffer for the hash
  66. * Returns: 0 on success, -1 on failure
  67. */
  68. int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  69. u8 *mac);
  70. /**
  71. * fips186_2-prf - NIST FIPS Publication 186-2 change notice 1 PRF
  72. * @seed: Seed/key for the PRF
  73. * @seed_len: Seed length in bytes
  74. * @x: Buffer for PRF output
  75. * @xlen: Output length in bytes
  76. * Returns: 0 on success, -1 on failure
  77. *
  78. * This function implements random number generation specified in NIST FIPS
  79. * Publication 186-2 for EAP-SIM. This PRF uses a function that is similar to
  80. * SHA-1, but has different message padding.
  81. */
  82. int __must_check fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x,
  83. size_t xlen);
  84. /**
  85. * sha256_vector - SHA256 hash for data vector
  86. * @num_elem: Number of elements in the data vector
  87. * @addr: Pointers to the data areas
  88. * @len: Lengths of the data blocks
  89. * @mac: Buffer for the hash
  90. * Returns: 0 on success, -1 on failure
  91. */
  92. int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  93. u8 *mac);
  94. /**
  95. * des_encrypt - Encrypt one block with DES
  96. * @clear: 8 octets (in)
  97. * @key: 7 octets (in) (no parity bits included)
  98. * @cypher: 8 octets (out)
  99. */
  100. void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
  101. /**
  102. * aes_encrypt_init - Initialize AES for encryption
  103. * @key: Encryption key
  104. * @len: Key length in bytes (usually 16, i.e., 128 bits)
  105. * Returns: Pointer to context data or %NULL on failure
  106. */
  107. void * aes_encrypt_init(const u8 *key, size_t len);
  108. /**
  109. * aes_encrypt - Encrypt one AES block
  110. * @ctx: Context pointer from aes_encrypt_init()
  111. * @plain: Plaintext data to be encrypted (16 bytes)
  112. * @crypt: Buffer for the encrypted data (16 bytes)
  113. */
  114. void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
  115. /**
  116. * aes_encrypt_deinit - Deinitialize AES encryption
  117. * @ctx: Context pointer from aes_encrypt_init()
  118. */
  119. void aes_encrypt_deinit(void *ctx);
  120. /**
  121. * aes_decrypt_init - Initialize AES for decryption
  122. * @key: Decryption key
  123. * @len: Key length in bytes (usually 16, i.e., 128 bits)
  124. * Returns: Pointer to context data or %NULL on failure
  125. */
  126. void * aes_decrypt_init(const u8 *key, size_t len);
  127. /**
  128. * aes_decrypt - Decrypt one AES block
  129. * @ctx: Context pointer from aes_encrypt_init()
  130. * @crypt: Encrypted data (16 bytes)
  131. * @plain: Buffer for the decrypted data (16 bytes)
  132. */
  133. void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
  134. /**
  135. * aes_decrypt_deinit - Deinitialize AES decryption
  136. * @ctx: Context pointer from aes_encrypt_init()
  137. */
  138. void aes_decrypt_deinit(void *ctx);
  139. enum crypto_hash_alg {
  140. CRYPTO_HASH_ALG_MD5, CRYPTO_HASH_ALG_SHA1,
  141. CRYPTO_HASH_ALG_HMAC_MD5, CRYPTO_HASH_ALG_HMAC_SHA1
  142. };
  143. struct crypto_hash;
  144. /**
  145. * crypto_hash_init - Initialize hash/HMAC function
  146. * @alg: Hash algorithm
  147. * @key: Key for keyed hash (e.g., HMAC) or %NULL if not needed
  148. * @key_len: Length of the key in bytes
  149. * Returns: Pointer to hash context to use with other hash functions or %NULL
  150. * on failure
  151. *
  152. * This function is only used with internal TLSv1 implementation
  153. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  154. * to implement this.
  155. */
  156. struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
  157. size_t key_len);
  158. /**
  159. * crypto_hash_update - Add data to hash calculation
  160. * @ctx: Context pointer from crypto_hash_init()
  161. * @data: Data buffer to add
  162. * @len: Length of the buffer
  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. void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len);
  169. /**
  170. * crypto_hash_finish - Complete hash calculation
  171. * @ctx: Context pointer from crypto_hash_init()
  172. * @hash: Buffer for hash value or %NULL if caller is just freeing the hash
  173. * context
  174. * @len: Pointer to length of the buffer or %NULL if caller is just freeing the
  175. * hash context; on return, this is set to the actual length of the hash value
  176. * Returns: 0 on success, -1 if buffer is too small (len set to needed length),
  177. * or -2 on other failures (including failed crypto_hash_update() operations)
  178. *
  179. * This function calculates the hash value and frees the context buffer that
  180. * was used for hash calculation.
  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. int crypto_hash_finish(struct crypto_hash *ctx, u8 *hash, size_t *len);
  187. enum crypto_cipher_alg {
  188. CRYPTO_CIPHER_NULL = 0, CRYPTO_CIPHER_ALG_AES, CRYPTO_CIPHER_ALG_3DES,
  189. CRYPTO_CIPHER_ALG_DES, CRYPTO_CIPHER_ALG_RC2, CRYPTO_CIPHER_ALG_RC4
  190. };
  191. struct crypto_cipher;
  192. /**
  193. * crypto_cipher_init - Initialize block/stream cipher function
  194. * @alg: Cipher algorithm
  195. * @iv: Initialization vector for block ciphers or %NULL for stream ciphers
  196. * @key: Cipher key
  197. * @key_len: Length of key in bytes
  198. * Returns: Pointer to cipher context to use with other cipher functions or
  199. * %NULL on failure
  200. *
  201. * This function is only used with internal TLSv1 implementation
  202. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  203. * to implement this.
  204. */
  205. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  206. const u8 *iv, const u8 *key,
  207. size_t key_len);
  208. /**
  209. * crypto_cipher_encrypt - Cipher encrypt
  210. * @ctx: Context pointer from crypto_cipher_init()
  211. * @plain: Plaintext to cipher
  212. * @crypt: Resulting ciphertext
  213. * @len: Length of the plaintext
  214. * Returns: 0 on success, -1 on failure
  215. *
  216. * This function is only used with internal TLSv1 implementation
  217. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  218. * to implement this.
  219. */
  220. int __must_check crypto_cipher_encrypt(struct crypto_cipher *ctx,
  221. const u8 *plain, u8 *crypt, size_t len);
  222. /**
  223. * crypto_cipher_decrypt - Cipher decrypt
  224. * @ctx: Context pointer from crypto_cipher_init()
  225. * @crypt: Ciphertext to decrypt
  226. * @plain: Resulting plaintext
  227. * @len: Length of the cipher text
  228. * Returns: 0 on success, -1 on failure
  229. *
  230. * This function is only used with internal TLSv1 implementation
  231. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  232. * to implement this.
  233. */
  234. int __must_check crypto_cipher_decrypt(struct crypto_cipher *ctx,
  235. const u8 *crypt, u8 *plain, size_t len);
  236. /**
  237. * crypto_cipher_decrypt - Free cipher context
  238. * @ctx: Context pointer from crypto_cipher_init()
  239. *
  240. * This function is only used with internal TLSv1 implementation
  241. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  242. * to implement this.
  243. */
  244. void crypto_cipher_deinit(struct crypto_cipher *ctx);
  245. struct crypto_public_key;
  246. struct crypto_private_key;
  247. /**
  248. * crypto_public_key_import - Import an RSA public key
  249. * @key: Key buffer (DER encoded RSA public key)
  250. * @len: Key buffer length in bytes
  251. * Returns: Pointer to the public key or %NULL on failure
  252. *
  253. * This function can just return %NULL if the crypto library supports X.509
  254. * parsing. In that case, crypto_public_key_from_cert() is used to import the
  255. * public key from a certificate.
  256. *
  257. * This function is only used with internal TLSv1 implementation
  258. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  259. * to implement this.
  260. */
  261. struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len);
  262. /**
  263. * crypto_private_key_import - Import an RSA private key
  264. * @key: Key buffer (DER encoded RSA private key)
  265. * @len: Key buffer length in bytes
  266. * @passwd: Key encryption password or %NULL if key is not encrypted
  267. * Returns: Pointer to the private key or %NULL on failure
  268. *
  269. * This function is only used with internal TLSv1 implementation
  270. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  271. * to implement this.
  272. */
  273. struct crypto_private_key * crypto_private_key_import(const u8 *key,
  274. size_t len,
  275. const char *passwd);
  276. /**
  277. * crypto_public_key_from_cert - Import an RSA public key from a certificate
  278. * @buf: DER encoded X.509 certificate
  279. * @len: Certificate buffer length in bytes
  280. * Returns: Pointer to public key or %NULL on failure
  281. *
  282. * This function can just return %NULL if the crypto library does not support
  283. * X.509 parsing. In that case, internal code will be used to parse the
  284. * certificate and public key is imported using crypto_public_key_import().
  285. *
  286. * This function is only used with internal TLSv1 implementation
  287. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  288. * to implement this.
  289. */
  290. struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
  291. size_t len);
  292. /**
  293. * crypto_public_key_encrypt_pkcs1_v15 - Public key encryption (PKCS #1 v1.5)
  294. * @key: Public key
  295. * @in: Plaintext buffer
  296. * @inlen: Length of plaintext 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_public_key_encrypt_pkcs1_v15(
  306. struct crypto_public_key *key, const u8 *in, size_t inlen,
  307. u8 *out, size_t *outlen);
  308. /**
  309. * crypto_private_key_decrypt_pkcs1_v15 - Private key decryption (PKCS #1 v1.5)
  310. * @key: Private key
  311. * @in: Encrypted buffer
  312. * @inlen: Length of encrypted buffer in bytes
  313. * @out: Output buffer for encrypted 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_decrypt_pkcs1_v15(
  322. struct crypto_private_key *key, const u8 *in, size_t inlen,
  323. u8 *out, size_t *outlen);
  324. /**
  325. * crypto_private_key_sign_pkcs1 - Sign with private key (PKCS #1)
  326. * @key: Private key from crypto_private_key_import()
  327. * @in: Plaintext buffer
  328. * @inlen: Length of plaintext buffer in bytes
  329. * @out: Output buffer for encrypted (signed) data
  330. * @outlen: Length of output buffer in bytes; set to used length on success
  331. * Returns: 0 on success, -1 on failure
  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. int __must_check crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
  338. const u8 *in, size_t inlen,
  339. u8 *out, size_t *outlen);
  340. /**
  341. * crypto_public_key_free - Free public key
  342. * @key: Public key
  343. *
  344. * This function is only used with internal TLSv1 implementation
  345. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  346. * to implement this.
  347. */
  348. void crypto_public_key_free(struct crypto_public_key *key);
  349. /**
  350. * crypto_private_key_free - Free private key
  351. * @key: Private key from crypto_private_key_import()
  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. void crypto_private_key_free(struct crypto_private_key *key);
  358. /**
  359. * crypto_public_key_decrypt_pkcs1 - Decrypt PKCS #1 signature
  360. * @key: Public key
  361. * @crypt: Encrypted signature data (using the private key)
  362. * @crypt_len: Encrypted signature data length
  363. * @plain: Buffer for plaintext (at least crypt_len bytes)
  364. * @plain_len: Plaintext length (max buffer size on input, real len on output);
  365. * Returns: 0 on success, -1 on failure
  366. */
  367. int __must_check crypto_public_key_decrypt_pkcs1(
  368. struct crypto_public_key *key, const u8 *crypt, size_t crypt_len,
  369. u8 *plain, size_t *plain_len);
  370. /**
  371. * crypto_global_init - Initialize crypto wrapper
  372. *
  373. * This function is only used with internal TLSv1 implementation
  374. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  375. * to implement this.
  376. */
  377. int __must_check crypto_global_init(void);
  378. /**
  379. * crypto_global_deinit - Deinitialize crypto wrapper
  380. *
  381. * This function is only used with internal TLSv1 implementation
  382. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  383. * to implement this.
  384. */
  385. void crypto_global_deinit(void);
  386. /**
  387. * crypto_mod_exp - Modular exponentiation of large integers
  388. * @base: Base integer (big endian byte array)
  389. * @base_len: Length of base integer in bytes
  390. * @power: Power integer (big endian byte array)
  391. * @power_len: Length of power integer in bytes
  392. * @modulus: Modulus integer (big endian byte array)
  393. * @modulus_len: Length of modulus integer in bytes
  394. * @result: Buffer for the result
  395. * @result_len: Result length (max buffer size on input, real len on output)
  396. * Returns: 0 on success, -1 on failure
  397. *
  398. * This function calculates result = base ^ power mod modulus. modules_len is
  399. * used as the maximum size of modulus buffer. It is set to the used size on
  400. * success.
  401. *
  402. * This function is only used with internal TLSv1 implementation
  403. * (CONFIG_TLS=internal). If that is not used, the crypto wrapper does not need
  404. * to implement this.
  405. */
  406. int __must_check crypto_mod_exp(const u8 *base, size_t base_len,
  407. const u8 *power, size_t power_len,
  408. const u8 *modulus, size_t modulus_len,
  409. u8 *result, size_t *result_len);
  410. /**
  411. * rc4_skip - XOR RC4 stream to given data with skip-stream-start
  412. * @key: RC4 key
  413. * @keylen: RC4 key length
  414. * @skip: number of bytes to skip from the beginning of the RC4 stream
  415. * @data: data to be XOR'ed with RC4 stream
  416. * @data_len: buf length
  417. * Returns: 0 on success, -1 on failure
  418. *
  419. * Generate RC4 pseudo random stream for the given key, skip beginning of the
  420. * stream, and XOR the end result with the data buffer to perform RC4
  421. * encryption/decryption.
  422. */
  423. int rc4_skip(const u8 *key, size_t keylen, size_t skip,
  424. u8 *data, size_t data_len);
  425. #endif /* CRYPTO_H */