skcipher.h 16 KB

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