cipher.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  4. * Copyright (c) 2002 David S. Miller (davem@redhat.com)
  5. * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
  6. *
  7. * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
  8. * and Nettle, by Niels Möller.
  9. */
  10. #ifndef _CRYPTO_INTERNAL_CIPHER_H
  11. #define _CRYPTO_INTERNAL_CIPHER_H
  12. #include <crypto/algapi.h>
  13. struct crypto_cipher {
  14. struct crypto_tfm base;
  15. };
  16. /**
  17. * DOC: Single Block Cipher API
  18. *
  19. * The single block cipher API is used with the ciphers of type
  20. * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
  21. *
  22. * Using the single block cipher API calls, operations with the basic cipher
  23. * primitive can be implemented. These cipher primitives exclude any block
  24. * chaining operations including IV handling.
  25. *
  26. * The purpose of this single block cipher API is to support the implementation
  27. * of templates or other concepts that only need to perform the cipher operation
  28. * on one block at a time. Templates invoke the underlying cipher primitive
  29. * block-wise and process either the input or the output data of these cipher
  30. * operations.
  31. */
  32. static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
  33. {
  34. return (struct crypto_cipher *)tfm;
  35. }
  36. /**
  37. * crypto_alloc_cipher() - allocate single block cipher handle
  38. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  39. * single block cipher
  40. * @type: specifies the type of the cipher
  41. * @mask: specifies the mask for the cipher
  42. *
  43. * Allocate a cipher handle for a single block cipher. The returned struct
  44. * crypto_cipher is the cipher handle that is required for any subsequent API
  45. * invocation for that single block cipher.
  46. *
  47. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  48. * of an error, PTR_ERR() returns the error code.
  49. */
  50. static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
  51. u32 type, u32 mask)
  52. {
  53. type &= ~CRYPTO_ALG_TYPE_MASK;
  54. type |= CRYPTO_ALG_TYPE_CIPHER;
  55. mask |= CRYPTO_ALG_TYPE_MASK;
  56. return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
  57. }
  58. static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
  59. {
  60. return &tfm->base;
  61. }
  62. /**
  63. * crypto_free_cipher() - zeroize and free the single block cipher handle
  64. * @tfm: cipher handle to be freed
  65. */
  66. static inline void crypto_free_cipher(struct crypto_cipher *tfm)
  67. {
  68. crypto_free_tfm(crypto_cipher_tfm(tfm));
  69. }
  70. /**
  71. * crypto_has_cipher() - Search for the availability of a single block cipher
  72. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  73. * single block cipher
  74. * @type: specifies the type of the cipher
  75. * @mask: specifies the mask for the cipher
  76. *
  77. * Return: true when the single block cipher is known to the kernel crypto API;
  78. * false otherwise
  79. */
  80. static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
  81. {
  82. type &= ~CRYPTO_ALG_TYPE_MASK;
  83. type |= CRYPTO_ALG_TYPE_CIPHER;
  84. mask |= CRYPTO_ALG_TYPE_MASK;
  85. return crypto_has_alg(alg_name, type, mask);
  86. }
  87. /**
  88. * crypto_cipher_blocksize() - obtain block size for cipher
  89. * @tfm: cipher handle
  90. *
  91. * The block size for the single block cipher referenced with the cipher handle
  92. * tfm is returned. The caller may use that information to allocate appropriate
  93. * memory for the data returned by the encryption or decryption operation
  94. *
  95. * Return: block size of cipher
  96. */
  97. static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
  98. {
  99. return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
  100. }
  101. static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
  102. {
  103. return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
  104. }
  105. static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
  106. {
  107. return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
  108. }
  109. static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
  110. u32 flags)
  111. {
  112. crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
  113. }
  114. static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
  115. u32 flags)
  116. {
  117. crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
  118. }
  119. /**
  120. * crypto_cipher_setkey() - set key for cipher
  121. * @tfm: cipher handle
  122. * @key: buffer holding the key
  123. * @keylen: length of the key in bytes
  124. *
  125. * The caller provided key is set for the single block cipher referenced by the
  126. * cipher handle.
  127. *
  128. * Note, the key length determines the cipher type. Many block ciphers implement
  129. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  130. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  131. * is performed.
  132. *
  133. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  134. */
  135. int crypto_cipher_setkey(struct crypto_cipher *tfm,
  136. const u8 *key, unsigned int keylen);
  137. /**
  138. * crypto_cipher_encrypt_one() - encrypt one block of plaintext
  139. * @tfm: cipher handle
  140. * @dst: points to the buffer that will be filled with the ciphertext
  141. * @src: buffer holding the plaintext to be encrypted
  142. *
  143. * Invoke the encryption operation of one block. The caller must ensure that
  144. * the plaintext and ciphertext buffers are at least one block in size.
  145. */
  146. void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
  147. u8 *dst, const u8 *src);
  148. /**
  149. * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
  150. * @tfm: cipher handle
  151. * @dst: points to the buffer that will be filled with the plaintext
  152. * @src: buffer holding the ciphertext to be decrypted
  153. *
  154. * Invoke the decryption operation of one block. The caller must ensure that
  155. * the plaintext and ciphertext buffers are at least one block in size.
  156. */
  157. void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
  158. u8 *dst, const u8 *src);
  159. struct crypto_cipher *crypto_clone_cipher(struct crypto_cipher *cipher);
  160. struct crypto_cipher_spawn {
  161. struct crypto_spawn base;
  162. };
  163. static inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn,
  164. struct crypto_instance *inst,
  165. const char *name, u32 type, u32 mask)
  166. {
  167. type &= ~CRYPTO_ALG_TYPE_MASK;
  168. type |= CRYPTO_ALG_TYPE_CIPHER;
  169. mask |= CRYPTO_ALG_TYPE_MASK;
  170. return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
  171. }
  172. static inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn)
  173. {
  174. crypto_drop_spawn(&spawn->base);
  175. }
  176. static inline struct crypto_alg *crypto_spawn_cipher_alg(
  177. struct crypto_cipher_spawn *spawn)
  178. {
  179. return spawn->base.alg;
  180. }
  181. static inline struct crypto_cipher *crypto_spawn_cipher(
  182. struct crypto_cipher_spawn *spawn)
  183. {
  184. u32 type = CRYPTO_ALG_TYPE_CIPHER;
  185. u32 mask = CRYPTO_ALG_TYPE_MASK;
  186. return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask));
  187. }
  188. static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
  189. {
  190. return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
  191. }
  192. #endif