Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
0004  * Copyright (c) 2002 David S. Miller (davem@redhat.com)
0005  * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
0006  *
0007  * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
0008  * and Nettle, by Niels Möller.
0009  */
0010 
0011 #ifndef _CRYPTO_INTERNAL_CIPHER_H
0012 #define _CRYPTO_INTERNAL_CIPHER_H
0013 
0014 #include <crypto/algapi.h>
0015 
0016 struct crypto_cipher {
0017     struct crypto_tfm base;
0018 };
0019 
0020 /**
0021  * DOC: Single Block Cipher API
0022  *
0023  * The single block cipher API is used with the ciphers of type
0024  * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
0025  *
0026  * Using the single block cipher API calls, operations with the basic cipher
0027  * primitive can be implemented. These cipher primitives exclude any block
0028  * chaining operations including IV handling.
0029  *
0030  * The purpose of this single block cipher API is to support the implementation
0031  * of templates or other concepts that only need to perform the cipher operation
0032  * on one block at a time. Templates invoke the underlying cipher primitive
0033  * block-wise and process either the input or the output data of these cipher
0034  * operations.
0035  */
0036 
0037 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
0038 {
0039     return (struct crypto_cipher *)tfm;
0040 }
0041 
0042 /**
0043  * crypto_alloc_cipher() - allocate single block cipher handle
0044  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
0045  *       single block cipher
0046  * @type: specifies the type of the cipher
0047  * @mask: specifies the mask for the cipher
0048  *
0049  * Allocate a cipher handle for a single block cipher. The returned struct
0050  * crypto_cipher is the cipher handle that is required for any subsequent API
0051  * invocation for that single block cipher.
0052  *
0053  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
0054  *     of an error, PTR_ERR() returns the error code.
0055  */
0056 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
0057                             u32 type, u32 mask)
0058 {
0059     type &= ~CRYPTO_ALG_TYPE_MASK;
0060     type |= CRYPTO_ALG_TYPE_CIPHER;
0061     mask |= CRYPTO_ALG_TYPE_MASK;
0062 
0063     return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
0064 }
0065 
0066 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
0067 {
0068     return &tfm->base;
0069 }
0070 
0071 /**
0072  * crypto_free_cipher() - zeroize and free the single block cipher handle
0073  * @tfm: cipher handle to be freed
0074  */
0075 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
0076 {
0077     crypto_free_tfm(crypto_cipher_tfm(tfm));
0078 }
0079 
0080 /**
0081  * crypto_has_cipher() - Search for the availability of a single block cipher
0082  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
0083  *       single block cipher
0084  * @type: specifies the type of the cipher
0085  * @mask: specifies the mask for the cipher
0086  *
0087  * Return: true when the single block cipher is known to the kernel crypto API;
0088  *     false otherwise
0089  */
0090 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
0091 {
0092     type &= ~CRYPTO_ALG_TYPE_MASK;
0093     type |= CRYPTO_ALG_TYPE_CIPHER;
0094     mask |= CRYPTO_ALG_TYPE_MASK;
0095 
0096     return crypto_has_alg(alg_name, type, mask);
0097 }
0098 
0099 /**
0100  * crypto_cipher_blocksize() - obtain block size for cipher
0101  * @tfm: cipher handle
0102  *
0103  * The block size for the single block cipher referenced with the cipher handle
0104  * tfm is returned. The caller may use that information to allocate appropriate
0105  * memory for the data returned by the encryption or decryption operation
0106  *
0107  * Return: block size of cipher
0108  */
0109 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
0110 {
0111     return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
0112 }
0113 
0114 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
0115 {
0116     return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
0117 }
0118 
0119 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
0120 {
0121     return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
0122 }
0123 
0124 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
0125                        u32 flags)
0126 {
0127     crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
0128 }
0129 
0130 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
0131                          u32 flags)
0132 {
0133     crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
0134 }
0135 
0136 /**
0137  * crypto_cipher_setkey() - set key for cipher
0138  * @tfm: cipher handle
0139  * @key: buffer holding the key
0140  * @keylen: length of the key in bytes
0141  *
0142  * The caller provided key is set for the single block cipher referenced by the
0143  * cipher handle.
0144  *
0145  * Note, the key length determines the cipher type. Many block ciphers implement
0146  * different cipher modes depending on the key size, such as AES-128 vs AES-192
0147  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
0148  * is performed.
0149  *
0150  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
0151  */
0152 int crypto_cipher_setkey(struct crypto_cipher *tfm,
0153              const u8 *key, unsigned int keylen);
0154 
0155 /**
0156  * crypto_cipher_encrypt_one() - encrypt one block of plaintext
0157  * @tfm: cipher handle
0158  * @dst: points to the buffer that will be filled with the ciphertext
0159  * @src: buffer holding the plaintext to be encrypted
0160  *
0161  * Invoke the encryption operation of one block. The caller must ensure that
0162  * the plaintext and ciphertext buffers are at least one block in size.
0163  */
0164 void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
0165                    u8 *dst, const u8 *src);
0166 
0167 /**
0168  * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
0169  * @tfm: cipher handle
0170  * @dst: points to the buffer that will be filled with the plaintext
0171  * @src: buffer holding the ciphertext to be decrypted
0172  *
0173  * Invoke the decryption operation of one block. The caller must ensure that
0174  * the plaintext and ciphertext buffers are at least one block in size.
0175  */
0176 void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
0177                    u8 *dst, const u8 *src);
0178 
0179 struct crypto_cipher_spawn {
0180     struct crypto_spawn base;
0181 };
0182 
0183 static inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn,
0184                      struct crypto_instance *inst,
0185                      const char *name, u32 type, u32 mask)
0186 {
0187     type &= ~CRYPTO_ALG_TYPE_MASK;
0188     type |= CRYPTO_ALG_TYPE_CIPHER;
0189     mask |= CRYPTO_ALG_TYPE_MASK;
0190     return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
0191 }
0192 
0193 static inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn)
0194 {
0195     crypto_drop_spawn(&spawn->base);
0196 }
0197 
0198 static inline struct crypto_alg *crypto_spawn_cipher_alg(
0199        struct crypto_cipher_spawn *spawn)
0200 {
0201     return spawn->base.alg;
0202 }
0203 
0204 static inline struct crypto_cipher *crypto_spawn_cipher(
0205     struct crypto_cipher_spawn *spawn)
0206 {
0207     u32 type = CRYPTO_ALG_TYPE_CIPHER;
0208     u32 mask = CRYPTO_ALG_TYPE_MASK;
0209 
0210     return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask));
0211 }
0212 
0213 static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
0214 {
0215     return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
0216 }
0217 
0218 #endif