~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/crypto/internal/cipher.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php