| 1 |
|
| 2 | |
| 3 | * Copyright (c) 2002 James Morris <[email protected]>
|
| 4 | * Copyright (c) 2002 David S. Miller ([email protected])
|
| 5 | * Copyright (c) 2005 Herbert Xu <[email protected]>
|
| 6 | *
|
| 7 | * Portions derived from Cryptoapi, by Alexander Kjeldaas <[email protected]>
|
| 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_spawn {
|
| 180 | struct crypto_spawn base;
|
| 181 | };
|
| 182 |
|
| 183 | static inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn,
|
| 184 | struct crypto_instance *inst,
|
| 185 | const char *name, u32 type, u32 mask)
|
| 186 | {
|
| 187 | type &= ~CRYPTO_ALG_TYPE_MASK;
|
| 188 | type |= CRYPTO_ALG_TYPE_CIPHER;
|
| 189 | mask |= CRYPTO_ALG_TYPE_MASK;
|
| 190 | return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
|
| 191 | }
|
| 192 |
|
| 193 | static inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn)
|
| 194 | {
|
| 195 | crypto_drop_spawn(&spawn->base);
|
| 196 | }
|
| 197 |
|
| 198 | static inline struct crypto_alg *crypto_spawn_cipher_alg(
|
| 199 | struct crypto_cipher_spawn *spawn)
|
| 200 | {
|
| 201 | return spawn->base.alg;
|
| 202 | }
|
| 203 |
|
| 204 | static inline struct crypto_cipher *crypto_spawn_cipher(
|
| 205 | struct crypto_cipher_spawn *spawn)
|
| 206 | {
|
| 207 | u32 type = CRYPTO_ALG_TYPE_CIPHER;
|
| 208 | u32 mask = CRYPTO_ALG_TYPE_MASK;
|
| 209 |
|
| 210 | return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask));
|
| 211 | }
|
| 212 |
|
| 213 | static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
|
| 214 | {
|
| 215 | return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
|
| 216 | }
|
| 217 |
|
| 218 | #endif
|