skcipher.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Symmetric key ciphers.
  3. *
  4. * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #ifndef _CRYPTO_SKCIPHER_H
  13. #define _CRYPTO_SKCIPHER_H
  14. #include <linux/crypto.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. /**
  18. * struct skcipher_request - Symmetric key cipher request
  19. * @cryptlen: Number of bytes to encrypt or decrypt
  20. * @iv: Initialisation Vector
  21. * @src: Source SG list
  22. * @dst: Destination SG list
  23. * @base: Underlying async request request
  24. * @__ctx: Start of private context data
  25. */
  26. struct skcipher_request {
  27. unsigned int cryptlen;
  28. u8 *iv;
  29. struct scatterlist *src;
  30. struct scatterlist *dst;
  31. struct crypto_async_request base;
  32. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  33. };
  34. /**
  35. * struct skcipher_givcrypt_request - Crypto request with IV generation
  36. * @seq: Sequence number for IV generation
  37. * @giv: Space for generated IV
  38. * @creq: The crypto request itself
  39. */
  40. struct skcipher_givcrypt_request {
  41. u64 seq;
  42. u8 *giv;
  43. struct ablkcipher_request creq;
  44. };
  45. struct crypto_skcipher {
  46. int (*setkey)(struct crypto_skcipher *tfm, const u8 *key,
  47. unsigned int keylen);
  48. int (*encrypt)(struct skcipher_request *req);
  49. int (*decrypt)(struct skcipher_request *req);
  50. unsigned int ivsize;
  51. unsigned int reqsize;
  52. bool has_setkey;
  53. struct crypto_tfm base;
  54. };
  55. #define SKCIPHER_REQUEST_ON_STACK(name, tfm) \
  56. char __##name##_desc[sizeof(struct skcipher_request) + \
  57. crypto_skcipher_reqsize(tfm)] CRYPTO_MINALIGN_ATTR; \
  58. struct skcipher_request *name = (void *)__##name##_desc
  59. static inline struct crypto_ablkcipher *skcipher_givcrypt_reqtfm(
  60. struct skcipher_givcrypt_request *req)
  61. {
  62. return crypto_ablkcipher_reqtfm(&req->creq);
  63. }
  64. static inline int crypto_skcipher_givencrypt(
  65. struct skcipher_givcrypt_request *req)
  66. {
  67. struct ablkcipher_tfm *crt =
  68. crypto_ablkcipher_crt(skcipher_givcrypt_reqtfm(req));
  69. return crt->givencrypt(req);
  70. };
  71. static inline int crypto_skcipher_givdecrypt(
  72. struct skcipher_givcrypt_request *req)
  73. {
  74. struct ablkcipher_tfm *crt =
  75. crypto_ablkcipher_crt(skcipher_givcrypt_reqtfm(req));
  76. return crt->givdecrypt(req);
  77. };
  78. static inline void skcipher_givcrypt_set_tfm(
  79. struct skcipher_givcrypt_request *req, struct crypto_ablkcipher *tfm)
  80. {
  81. req->creq.base.tfm = crypto_ablkcipher_tfm(tfm);
  82. }
  83. static inline struct skcipher_givcrypt_request *skcipher_givcrypt_cast(
  84. struct crypto_async_request *req)
  85. {
  86. return container_of(ablkcipher_request_cast(req),
  87. struct skcipher_givcrypt_request, creq);
  88. }
  89. static inline struct skcipher_givcrypt_request *skcipher_givcrypt_alloc(
  90. struct crypto_ablkcipher *tfm, gfp_t gfp)
  91. {
  92. struct skcipher_givcrypt_request *req;
  93. req = kmalloc(sizeof(struct skcipher_givcrypt_request) +
  94. crypto_ablkcipher_reqsize(tfm), gfp);
  95. if (likely(req))
  96. skcipher_givcrypt_set_tfm(req, tfm);
  97. return req;
  98. }
  99. static inline void skcipher_givcrypt_free(struct skcipher_givcrypt_request *req)
  100. {
  101. kfree(req);
  102. }
  103. static inline void skcipher_givcrypt_set_callback(
  104. struct skcipher_givcrypt_request *req, u32 flags,
  105. crypto_completion_t compl, void *data)
  106. {
  107. ablkcipher_request_set_callback(&req->creq, flags, compl, data);
  108. }
  109. static inline void skcipher_givcrypt_set_crypt(
  110. struct skcipher_givcrypt_request *req,
  111. struct scatterlist *src, struct scatterlist *dst,
  112. unsigned int nbytes, void *iv)
  113. {
  114. ablkcipher_request_set_crypt(&req->creq, src, dst, nbytes, iv);
  115. }
  116. static inline void skcipher_givcrypt_set_giv(
  117. struct skcipher_givcrypt_request *req, u8 *giv, u64 seq)
  118. {
  119. req->giv = giv;
  120. req->seq = seq;
  121. }
  122. /**
  123. * DOC: Symmetric Key Cipher API
  124. *
  125. * Symmetric key cipher API is used with the ciphers of type
  126. * CRYPTO_ALG_TYPE_SKCIPHER (listed as type "skcipher" in /proc/crypto).
  127. *
  128. * Asynchronous cipher operations imply that the function invocation for a
  129. * cipher request returns immediately before the completion of the operation.
  130. * The cipher request is scheduled as a separate kernel thread and therefore
  131. * load-balanced on the different CPUs via the process scheduler. To allow
  132. * the kernel crypto API to inform the caller about the completion of a cipher
  133. * request, the caller must provide a callback function. That function is
  134. * invoked with the cipher handle when the request completes.
  135. *
  136. * To support the asynchronous operation, additional information than just the
  137. * cipher handle must be supplied to the kernel crypto API. That additional
  138. * information is given by filling in the skcipher_request data structure.
  139. *
  140. * For the symmetric key cipher API, the state is maintained with the tfm
  141. * cipher handle. A single tfm can be used across multiple calls and in
  142. * parallel. For asynchronous block cipher calls, context data supplied and
  143. * only used by the caller can be referenced the request data structure in
  144. * addition to the IV used for the cipher request. The maintenance of such
  145. * state information would be important for a crypto driver implementer to
  146. * have, because when calling the callback function upon completion of the
  147. * cipher operation, that callback function may need some information about
  148. * which operation just finished if it invoked multiple in parallel. This
  149. * state information is unused by the kernel crypto API.
  150. */
  151. static inline struct crypto_skcipher *__crypto_skcipher_cast(
  152. struct crypto_tfm *tfm)
  153. {
  154. return container_of(tfm, struct crypto_skcipher, base);
  155. }
  156. /**
  157. * crypto_alloc_skcipher() - allocate symmetric key cipher handle
  158. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  159. * skcipher cipher
  160. * @type: specifies the type of the cipher
  161. * @mask: specifies the mask for the cipher
  162. *
  163. * Allocate a cipher handle for an skcipher. The returned struct
  164. * crypto_skcipher is the cipher handle that is required for any subsequent
  165. * API invocation for that skcipher.
  166. *
  167. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  168. * of an error, PTR_ERR() returns the error code.
  169. */
  170. struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  171. u32 type, u32 mask);
  172. static inline struct crypto_tfm *crypto_skcipher_tfm(
  173. struct crypto_skcipher *tfm)
  174. {
  175. return &tfm->base;
  176. }
  177. /**
  178. * crypto_free_skcipher() - zeroize and free cipher handle
  179. * @tfm: cipher handle to be freed
  180. */
  181. static inline void crypto_free_skcipher(struct crypto_skcipher *tfm)
  182. {
  183. crypto_destroy_tfm(tfm, crypto_skcipher_tfm(tfm));
  184. }
  185. /**
  186. * crypto_has_skcipher() - Search for the availability of an skcipher.
  187. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  188. * skcipher
  189. * @type: specifies the type of the cipher
  190. * @mask: specifies the mask for the cipher
  191. *
  192. * Return: true when the skcipher is known to the kernel crypto API; false
  193. * otherwise
  194. */
  195. static inline int crypto_has_skcipher(const char *alg_name, u32 type,
  196. u32 mask)
  197. {
  198. return crypto_has_alg(alg_name, crypto_skcipher_type(type),
  199. crypto_skcipher_mask(mask));
  200. }
  201. /**
  202. * crypto_skcipher_ivsize() - obtain IV size
  203. * @tfm: cipher handle
  204. *
  205. * The size of the IV for the skcipher referenced by the cipher handle is
  206. * returned. This IV size may be zero if the cipher does not need an IV.
  207. *
  208. * Return: IV size in bytes
  209. */
  210. static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm)
  211. {
  212. return tfm->ivsize;
  213. }
  214. /**
  215. * crypto_skcipher_blocksize() - obtain block size of cipher
  216. * @tfm: cipher handle
  217. *
  218. * The block size for the skcipher referenced with the cipher handle is
  219. * returned. The caller may use that information to allocate appropriate
  220. * memory for the data returned by the encryption or decryption operation
  221. *
  222. * Return: block size of cipher
  223. */
  224. static inline unsigned int crypto_skcipher_blocksize(
  225. struct crypto_skcipher *tfm)
  226. {
  227. return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm));
  228. }
  229. static inline unsigned int crypto_skcipher_alignmask(
  230. struct crypto_skcipher *tfm)
  231. {
  232. return crypto_tfm_alg_alignmask(crypto_skcipher_tfm(tfm));
  233. }
  234. static inline u32 crypto_skcipher_get_flags(struct crypto_skcipher *tfm)
  235. {
  236. return crypto_tfm_get_flags(crypto_skcipher_tfm(tfm));
  237. }
  238. static inline void crypto_skcipher_set_flags(struct crypto_skcipher *tfm,
  239. u32 flags)
  240. {
  241. crypto_tfm_set_flags(crypto_skcipher_tfm(tfm), flags);
  242. }
  243. static inline void crypto_skcipher_clear_flags(struct crypto_skcipher *tfm,
  244. u32 flags)
  245. {
  246. crypto_tfm_clear_flags(crypto_skcipher_tfm(tfm), flags);
  247. }
  248. /**
  249. * crypto_skcipher_setkey() - set key for cipher
  250. * @tfm: cipher handle
  251. * @key: buffer holding the key
  252. * @keylen: length of the key in bytes
  253. *
  254. * The caller provided key is set for the skcipher referenced by the cipher
  255. * handle.
  256. *
  257. * Note, the key length determines the cipher type. Many block ciphers implement
  258. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  259. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  260. * is performed.
  261. *
  262. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  263. */
  264. static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm,
  265. const u8 *key, unsigned int keylen)
  266. {
  267. return tfm->setkey(tfm, key, keylen);
  268. }
  269. static inline bool crypto_skcipher_has_setkey(struct crypto_skcipher *tfm)
  270. {
  271. return tfm->has_setkey;
  272. }
  273. /**
  274. * crypto_skcipher_reqtfm() - obtain cipher handle from request
  275. * @req: skcipher_request out of which the cipher handle is to be obtained
  276. *
  277. * Return the crypto_skcipher handle when furnishing an skcipher_request
  278. * data structure.
  279. *
  280. * Return: crypto_skcipher handle
  281. */
  282. static inline struct crypto_skcipher *crypto_skcipher_reqtfm(
  283. struct skcipher_request *req)
  284. {
  285. return __crypto_skcipher_cast(req->base.tfm);
  286. }
  287. /**
  288. * crypto_skcipher_encrypt() - encrypt plaintext
  289. * @req: reference to the skcipher_request handle that holds all information
  290. * needed to perform the cipher operation
  291. *
  292. * Encrypt plaintext data using the skcipher_request handle. That data
  293. * structure and how it is filled with data is discussed with the
  294. * skcipher_request_* functions.
  295. *
  296. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  297. */
  298. static inline int crypto_skcipher_encrypt(struct skcipher_request *req)
  299. {
  300. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  301. return tfm->encrypt(req);
  302. }
  303. /**
  304. * crypto_skcipher_decrypt() - decrypt ciphertext
  305. * @req: reference to the skcipher_request handle that holds all information
  306. * needed to perform the cipher operation
  307. *
  308. * Decrypt ciphertext data using the skcipher_request handle. That data
  309. * structure and how it is filled with data is discussed with the
  310. * skcipher_request_* functions.
  311. *
  312. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  313. */
  314. static inline int crypto_skcipher_decrypt(struct skcipher_request *req)
  315. {
  316. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  317. return tfm->decrypt(req);
  318. }
  319. /**
  320. * DOC: Symmetric Key Cipher Request Handle
  321. *
  322. * The skcipher_request data structure contains all pointers to data
  323. * required for the symmetric key cipher operation. This includes the cipher
  324. * handle (which can be used by multiple skcipher_request instances), pointer
  325. * to plaintext and ciphertext, asynchronous callback function, etc. It acts
  326. * as a handle to the skcipher_request_* API calls in a similar way as
  327. * skcipher handle to the crypto_skcipher_* API calls.
  328. */
  329. /**
  330. * crypto_skcipher_reqsize() - obtain size of the request data structure
  331. * @tfm: cipher handle
  332. *
  333. * Return: number of bytes
  334. */
  335. static inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm)
  336. {
  337. return tfm->reqsize;
  338. }
  339. /**
  340. * skcipher_request_set_tfm() - update cipher handle reference in request
  341. * @req: request handle to be modified
  342. * @tfm: cipher handle that shall be added to the request handle
  343. *
  344. * Allow the caller to replace the existing skcipher handle in the request
  345. * data structure with a different one.
  346. */
  347. static inline void skcipher_request_set_tfm(struct skcipher_request *req,
  348. struct crypto_skcipher *tfm)
  349. {
  350. req->base.tfm = crypto_skcipher_tfm(tfm);
  351. }
  352. static inline struct skcipher_request *skcipher_request_cast(
  353. struct crypto_async_request *req)
  354. {
  355. return container_of(req, struct skcipher_request, base);
  356. }
  357. /**
  358. * skcipher_request_alloc() - allocate request data structure
  359. * @tfm: cipher handle to be registered with the request
  360. * @gfp: memory allocation flag that is handed to kmalloc by the API call.
  361. *
  362. * Allocate the request data structure that must be used with the skcipher
  363. * encrypt and decrypt API calls. During the allocation, the provided skcipher
  364. * handle is registered in the request data structure.
  365. *
  366. * Return: allocated request handle in case of success; IS_ERR() is true in case
  367. * of an error, PTR_ERR() returns the error code.
  368. */
  369. static inline struct skcipher_request *skcipher_request_alloc(
  370. struct crypto_skcipher *tfm, gfp_t gfp)
  371. {
  372. struct skcipher_request *req;
  373. req = kmalloc(sizeof(struct skcipher_request) +
  374. crypto_skcipher_reqsize(tfm), gfp);
  375. if (likely(req))
  376. skcipher_request_set_tfm(req, tfm);
  377. return req;
  378. }
  379. /**
  380. * skcipher_request_free() - zeroize and free request data structure
  381. * @req: request data structure cipher handle to be freed
  382. */
  383. static inline void skcipher_request_free(struct skcipher_request *req)
  384. {
  385. kzfree(req);
  386. }
  387. /**
  388. * skcipher_request_set_callback() - set asynchronous callback function
  389. * @req: request handle
  390. * @flags: specify zero or an ORing of the flags
  391. * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
  392. * increase the wait queue beyond the initial maximum size;
  393. * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
  394. * @compl: callback function pointer to be registered with the request handle
  395. * @data: The data pointer refers to memory that is not used by the kernel
  396. * crypto API, but provided to the callback function for it to use. Here,
  397. * the caller can provide a reference to memory the callback function can
  398. * operate on. As the callback function is invoked asynchronously to the
  399. * related functionality, it may need to access data structures of the
  400. * related functionality which can be referenced using this pointer. The
  401. * callback function can access the memory via the "data" field in the
  402. * crypto_async_request data structure provided to the callback function.
  403. *
  404. * This function allows setting the callback function that is triggered once the
  405. * cipher operation completes.
  406. *
  407. * The callback function is registered with the skcipher_request handle and
  408. * must comply with the following template
  409. *
  410. * void callback_function(struct crypto_async_request *req, int error)
  411. */
  412. static inline void skcipher_request_set_callback(struct skcipher_request *req,
  413. u32 flags,
  414. crypto_completion_t compl,
  415. void *data)
  416. {
  417. req->base.complete = compl;
  418. req->base.data = data;
  419. req->base.flags = flags;
  420. }
  421. /**
  422. * skcipher_request_set_crypt() - set data buffers
  423. * @req: request handle
  424. * @src: source scatter / gather list
  425. * @dst: destination scatter / gather list
  426. * @cryptlen: number of bytes to process from @src
  427. * @iv: IV for the cipher operation which must comply with the IV size defined
  428. * by crypto_skcipher_ivsize
  429. *
  430. * This function allows setting of the source data and destination data
  431. * scatter / gather lists.
  432. *
  433. * For encryption, the source is treated as the plaintext and the
  434. * destination is the ciphertext. For a decryption operation, the use is
  435. * reversed - the source is the ciphertext and the destination is the plaintext.
  436. */
  437. static inline void skcipher_request_set_crypt(
  438. struct skcipher_request *req,
  439. struct scatterlist *src, struct scatterlist *dst,
  440. unsigned int cryptlen, void *iv)
  441. {
  442. req->src = src;
  443. req->dst = dst;
  444. req->cryptlen = cryptlen;
  445. req->iv = iv;
  446. }
  447. #endif /* _CRYPTO_SKCIPHER_H */