skcipher.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. unsigned int keysize;
  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. static inline const char *crypto_skcipher_driver_name(
  202. struct crypto_skcipher *tfm)
  203. {
  204. return crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
  205. }
  206. /**
  207. * crypto_skcipher_ivsize() - obtain IV size
  208. * @tfm: cipher handle
  209. *
  210. * The size of the IV for the skcipher referenced by the cipher handle is
  211. * returned. This IV size may be zero if the cipher does not need an IV.
  212. *
  213. * Return: IV size in bytes
  214. */
  215. static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm)
  216. {
  217. return tfm->ivsize;
  218. }
  219. /**
  220. * crypto_skcipher_blocksize() - obtain block size of cipher
  221. * @tfm: cipher handle
  222. *
  223. * The block size for the skcipher referenced with the cipher handle is
  224. * returned. The caller may use that information to allocate appropriate
  225. * memory for the data returned by the encryption or decryption operation
  226. *
  227. * Return: block size of cipher
  228. */
  229. static inline unsigned int crypto_skcipher_blocksize(
  230. struct crypto_skcipher *tfm)
  231. {
  232. return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm));
  233. }
  234. static inline unsigned int crypto_skcipher_alignmask(
  235. struct crypto_skcipher *tfm)
  236. {
  237. return crypto_tfm_alg_alignmask(crypto_skcipher_tfm(tfm));
  238. }
  239. static inline u32 crypto_skcipher_get_flags(struct crypto_skcipher *tfm)
  240. {
  241. return crypto_tfm_get_flags(crypto_skcipher_tfm(tfm));
  242. }
  243. static inline void crypto_skcipher_set_flags(struct crypto_skcipher *tfm,
  244. u32 flags)
  245. {
  246. crypto_tfm_set_flags(crypto_skcipher_tfm(tfm), flags);
  247. }
  248. static inline void crypto_skcipher_clear_flags(struct crypto_skcipher *tfm,
  249. u32 flags)
  250. {
  251. crypto_tfm_clear_flags(crypto_skcipher_tfm(tfm), flags);
  252. }
  253. /**
  254. * crypto_skcipher_setkey() - set key for cipher
  255. * @tfm: cipher handle
  256. * @key: buffer holding the key
  257. * @keylen: length of the key in bytes
  258. *
  259. * The caller provided key is set for the skcipher referenced by the cipher
  260. * handle.
  261. *
  262. * Note, the key length determines the cipher type. Many block ciphers implement
  263. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  264. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  265. * is performed.
  266. *
  267. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  268. */
  269. static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm,
  270. const u8 *key, unsigned int keylen)
  271. {
  272. return tfm->setkey(tfm, key, keylen);
  273. }
  274. static inline bool crypto_skcipher_has_setkey(struct crypto_skcipher *tfm)
  275. {
  276. return tfm->keysize;
  277. }
  278. static inline unsigned int crypto_skcipher_default_keysize(
  279. struct crypto_skcipher *tfm)
  280. {
  281. return tfm->keysize;
  282. }
  283. /**
  284. * crypto_skcipher_reqtfm() - obtain cipher handle from request
  285. * @req: skcipher_request out of which the cipher handle is to be obtained
  286. *
  287. * Return the crypto_skcipher handle when furnishing an skcipher_request
  288. * data structure.
  289. *
  290. * Return: crypto_skcipher handle
  291. */
  292. static inline struct crypto_skcipher *crypto_skcipher_reqtfm(
  293. struct skcipher_request *req)
  294. {
  295. return __crypto_skcipher_cast(req->base.tfm);
  296. }
  297. /**
  298. * crypto_skcipher_encrypt() - encrypt plaintext
  299. * @req: reference to the skcipher_request handle that holds all information
  300. * needed to perform the cipher operation
  301. *
  302. * Encrypt plaintext data using the skcipher_request handle. That data
  303. * structure and how it is filled with data is discussed with the
  304. * skcipher_request_* functions.
  305. *
  306. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  307. */
  308. static inline int crypto_skcipher_encrypt(struct skcipher_request *req)
  309. {
  310. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  311. return tfm->encrypt(req);
  312. }
  313. /**
  314. * crypto_skcipher_decrypt() - decrypt ciphertext
  315. * @req: reference to the skcipher_request handle that holds all information
  316. * needed to perform the cipher operation
  317. *
  318. * Decrypt ciphertext data using the skcipher_request handle. That data
  319. * structure and how it is filled with data is discussed with the
  320. * skcipher_request_* functions.
  321. *
  322. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  323. */
  324. static inline int crypto_skcipher_decrypt(struct skcipher_request *req)
  325. {
  326. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  327. return tfm->decrypt(req);
  328. }
  329. /**
  330. * DOC: Symmetric Key Cipher Request Handle
  331. *
  332. * The skcipher_request data structure contains all pointers to data
  333. * required for the symmetric key cipher operation. This includes the cipher
  334. * handle (which can be used by multiple skcipher_request instances), pointer
  335. * to plaintext and ciphertext, asynchronous callback function, etc. It acts
  336. * as a handle to the skcipher_request_* API calls in a similar way as
  337. * skcipher handle to the crypto_skcipher_* API calls.
  338. */
  339. /**
  340. * crypto_skcipher_reqsize() - obtain size of the request data structure
  341. * @tfm: cipher handle
  342. *
  343. * Return: number of bytes
  344. */
  345. static inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm)
  346. {
  347. return tfm->reqsize;
  348. }
  349. /**
  350. * skcipher_request_set_tfm() - update cipher handle reference in request
  351. * @req: request handle to be modified
  352. * @tfm: cipher handle that shall be added to the request handle
  353. *
  354. * Allow the caller to replace the existing skcipher handle in the request
  355. * data structure with a different one.
  356. */
  357. static inline void skcipher_request_set_tfm(struct skcipher_request *req,
  358. struct crypto_skcipher *tfm)
  359. {
  360. req->base.tfm = crypto_skcipher_tfm(tfm);
  361. }
  362. static inline struct skcipher_request *skcipher_request_cast(
  363. struct crypto_async_request *req)
  364. {
  365. return container_of(req, struct skcipher_request, base);
  366. }
  367. /**
  368. * skcipher_request_alloc() - allocate request data structure
  369. * @tfm: cipher handle to be registered with the request
  370. * @gfp: memory allocation flag that is handed to kmalloc by the API call.
  371. *
  372. * Allocate the request data structure that must be used with the skcipher
  373. * encrypt and decrypt API calls. During the allocation, the provided skcipher
  374. * handle is registered in the request data structure.
  375. *
  376. * Return: allocated request handle in case of success; IS_ERR() is true in case
  377. * of an error, PTR_ERR() returns the error code.
  378. */
  379. static inline struct skcipher_request *skcipher_request_alloc(
  380. struct crypto_skcipher *tfm, gfp_t gfp)
  381. {
  382. struct skcipher_request *req;
  383. req = kmalloc(sizeof(struct skcipher_request) +
  384. crypto_skcipher_reqsize(tfm), gfp);
  385. if (likely(req))
  386. skcipher_request_set_tfm(req, tfm);
  387. return req;
  388. }
  389. /**
  390. * skcipher_request_free() - zeroize and free request data structure
  391. * @req: request data structure cipher handle to be freed
  392. */
  393. static inline void skcipher_request_free(struct skcipher_request *req)
  394. {
  395. kzfree(req);
  396. }
  397. static inline void skcipher_request_zero(struct skcipher_request *req)
  398. {
  399. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  400. memzero_explicit(req, sizeof(*req) + crypto_skcipher_reqsize(tfm));
  401. }
  402. /**
  403. * skcipher_request_set_callback() - set asynchronous callback function
  404. * @req: request handle
  405. * @flags: specify zero or an ORing of the flags
  406. * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
  407. * increase the wait queue beyond the initial maximum size;
  408. * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
  409. * @compl: callback function pointer to be registered with the request handle
  410. * @data: The data pointer refers to memory that is not used by the kernel
  411. * crypto API, but provided to the callback function for it to use. Here,
  412. * the caller can provide a reference to memory the callback function can
  413. * operate on. As the callback function is invoked asynchronously to the
  414. * related functionality, it may need to access data structures of the
  415. * related functionality which can be referenced using this pointer. The
  416. * callback function can access the memory via the "data" field in the
  417. * crypto_async_request data structure provided to the callback function.
  418. *
  419. * This function allows setting the callback function that is triggered once the
  420. * cipher operation completes.
  421. *
  422. * The callback function is registered with the skcipher_request handle and
  423. * must comply with the following template
  424. *
  425. * void callback_function(struct crypto_async_request *req, int error)
  426. */
  427. static inline void skcipher_request_set_callback(struct skcipher_request *req,
  428. u32 flags,
  429. crypto_completion_t compl,
  430. void *data)
  431. {
  432. req->base.complete = compl;
  433. req->base.data = data;
  434. req->base.flags = flags;
  435. }
  436. /**
  437. * skcipher_request_set_crypt() - set data buffers
  438. * @req: request handle
  439. * @src: source scatter / gather list
  440. * @dst: destination scatter / gather list
  441. * @cryptlen: number of bytes to process from @src
  442. * @iv: IV for the cipher operation which must comply with the IV size defined
  443. * by crypto_skcipher_ivsize
  444. *
  445. * This function allows setting of the source data and destination data
  446. * scatter / gather lists.
  447. *
  448. * For encryption, the source is treated as the plaintext and the
  449. * destination is the ciphertext. For a decryption operation, the use is
  450. * reversed - the source is the ciphertext and the destination is the plaintext.
  451. */
  452. static inline void skcipher_request_set_crypt(
  453. struct skcipher_request *req,
  454. struct scatterlist *src, struct scatterlist *dst,
  455. unsigned int cryptlen, void *iv)
  456. {
  457. req->src = src;
  458. req->dst = dst;
  459. req->cryptlen = cryptlen;
  460. req->iv = iv;
  461. }
  462. #endif /* _CRYPTO_SKCIPHER_H */