Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,246 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Asynchronous Compression operations
*
* Copyright (c) 2016, Intel Corporation
* Authors: Weigang Li <weigang.li@intel.com>
* Giovanni Cabiddu <giovanni.cabiddu@intel.com>
*/
#ifndef _CRYPTO_ACOMP_INT_H
#define _CRYPTO_ACOMP_INT_H
#include <crypto/acompress.h>
#include <crypto/algapi.h>
#include <crypto/scatterwalk.h>
#include <linux/compiler_types.h>
#include <linux/cpumask_types.h>
#include <linux/spinlock.h>
#include <linux/workqueue_types.h>
#define ACOMP_FBREQ_ON_STACK(name, req) \
char __##name##_req[sizeof(struct acomp_req) + \
MAX_SYNC_COMP_REQSIZE] CRYPTO_MINALIGN_ATTR; \
struct acomp_req *name = acomp_fbreq_on_stack_init( \
__##name##_req, (req))
/**
* struct acomp_alg - asynchronous compression algorithm
*
* @compress: Function performs a compress operation
* @decompress: Function performs a de-compress operation
* @init: Initialize the cryptographic transformation object.
* This function is used to initialize the cryptographic
* transformation object. This function is called only once at
* the instantiation time, right after the transformation context
* was allocated. In case the cryptographic hardware has some
* special requirements which need to be handled by software, this
* function shall check for the precise requirement of the
* transformation and put any software fallbacks in place.
* @exit: Deinitialize the cryptographic transformation object. This is a
* counterpart to @init, used to remove various changes set in
* @init.
*
* @base: Common crypto API algorithm data structure
* @calg: Cmonn algorithm data structure shared with scomp
* @COMP_ALG_COMMON: see struct comp_alg_common
*/
struct acomp_alg {
int (*compress)(struct acomp_req *req);
int (*decompress)(struct acomp_req *req);
int (*init)(struct crypto_acomp *tfm);
void (*exit)(struct crypto_acomp *tfm);
union {
struct COMP_ALG_COMMON;
struct comp_alg_common calg;
};
};
struct crypto_acomp_stream {
spinlock_t lock;
void *ctx;
};
struct crypto_acomp_streams {
/* These must come first because of struct scomp_alg. */
void *(*alloc_ctx)(void);
void (*free_ctx)(void *);
struct crypto_acomp_stream __percpu *streams;
struct work_struct stream_work;
cpumask_t stream_want;
};
struct acomp_walk {
union {
/* Virtual address of the source. */
struct {
struct {
const void *const addr;
} virt;
} src;
/* Private field for the API, do not use. */
struct scatter_walk in;
};
union {
/* Virtual address of the destination. */
struct {
struct {
void *const addr;
} virt;
} dst;
/* Private field for the API, do not use. */
struct scatter_walk out;
};
unsigned int slen;
unsigned int dlen;
int flags;
};
/*
* Transform internal helpers.
*/
static inline void *acomp_request_ctx(struct acomp_req *req)
{
return req->__ctx;
}
static inline void *acomp_tfm_ctx(struct crypto_acomp *tfm)
{
return tfm->base.__crt_ctx;
}
static inline void acomp_request_complete(struct acomp_req *req,
int err)
{
crypto_request_complete(&req->base, err);
}
/**
* crypto_register_acomp() -- Register asynchronous compression algorithm
*
* Function registers an implementation of an asynchronous
* compression algorithm
*
* @alg: algorithm definition
*
* Return: zero on success; error code in case of error
*/
int crypto_register_acomp(struct acomp_alg *alg);
/**
* crypto_unregister_acomp() -- Unregister asynchronous compression algorithm
*
* Function unregisters an implementation of an asynchronous
* compression algorithm
*
* @alg: algorithm definition
*/
void crypto_unregister_acomp(struct acomp_alg *alg);
int crypto_register_acomps(struct acomp_alg *algs, int count);
void crypto_unregister_acomps(struct acomp_alg *algs, int count);
static inline bool acomp_request_issg(struct acomp_req *req)
{
return !(req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT |
CRYPTO_ACOMP_REQ_DST_VIRT));
}
static inline bool acomp_request_src_isvirt(struct acomp_req *req)
{
return req->base.flags & CRYPTO_ACOMP_REQ_SRC_VIRT;
}
static inline bool acomp_request_dst_isvirt(struct acomp_req *req)
{
return req->base.flags & CRYPTO_ACOMP_REQ_DST_VIRT;
}
static inline bool acomp_request_isvirt(struct acomp_req *req)
{
return req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT |
CRYPTO_ACOMP_REQ_DST_VIRT);
}
static inline bool acomp_request_src_isnondma(struct acomp_req *req)
{
return req->base.flags & CRYPTO_ACOMP_REQ_SRC_NONDMA;
}
static inline bool acomp_request_dst_isnondma(struct acomp_req *req)
{
return req->base.flags & CRYPTO_ACOMP_REQ_DST_NONDMA;
}
static inline bool acomp_request_isnondma(struct acomp_req *req)
{
return req->base.flags & (CRYPTO_ACOMP_REQ_SRC_NONDMA |
CRYPTO_ACOMP_REQ_DST_NONDMA);
}
static inline bool crypto_acomp_req_virt(struct crypto_acomp *tfm)
{
return crypto_tfm_req_virt(&tfm->base);
}
void crypto_acomp_free_streams(struct crypto_acomp_streams *s);
int crypto_acomp_alloc_streams(struct crypto_acomp_streams *s);
#define crypto_acomp_lock_stream_bh(...) __acquire_ret(_crypto_acomp_lock_stream_bh(__VA_ARGS__), &__ret->lock);
struct crypto_acomp_stream *_crypto_acomp_lock_stream_bh(
struct crypto_acomp_streams *s) __acquires_ret;
static inline void crypto_acomp_unlock_stream_bh(
struct crypto_acomp_stream *stream) __releases(&stream->lock)
{
spin_unlock_bh(&stream->lock);
}
void acomp_walk_done_src(struct acomp_walk *walk, int used);
void acomp_walk_done_dst(struct acomp_walk *walk, int used);
int acomp_walk_next_src(struct acomp_walk *walk);
int acomp_walk_next_dst(struct acomp_walk *walk);
int acomp_walk_virt(struct acomp_walk *__restrict walk,
struct acomp_req *__restrict req, bool atomic);
static inline bool acomp_walk_more_src(const struct acomp_walk *walk, int cur)
{
return walk->slen != cur;
}
static inline u32 acomp_request_flags(struct acomp_req *req)
{
return crypto_request_flags(&req->base) & ~CRYPTO_ACOMP_REQ_PRIVATE;
}
static inline struct crypto_acomp *crypto_acomp_fb(struct crypto_acomp *tfm)
{
return __crypto_acomp_tfm(crypto_acomp_tfm(tfm)->fb);
}
static inline struct acomp_req *acomp_fbreq_on_stack_init(
char *buf, struct acomp_req *old)
{
struct crypto_acomp *tfm = crypto_acomp_reqtfm(old);
struct acomp_req *req = (void *)buf;
crypto_stack_request_init(&req->base,
crypto_acomp_tfm(crypto_acomp_fb(tfm)));
acomp_request_set_callback(req, acomp_request_flags(old), NULL, NULL);
req->base.flags &= ~CRYPTO_ACOMP_REQ_PRIVATE;
req->base.flags |= old->base.flags & CRYPTO_ACOMP_REQ_PRIVATE;
req->src = old->src;
req->dst = old->dst;
req->slen = old->slen;
req->dlen = old->dlen;
return req;
}
#endif
+168
View File
@@ -0,0 +1,168 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* AEAD: Authenticated Encryption with Associated Data
*
* Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
*/
#ifndef _CRYPTO_INTERNAL_AEAD_H
#define _CRYPTO_INTERNAL_AEAD_H
#include <crypto/aead.h>
#include <crypto/algapi.h>
#include <linux/stddef.h>
#include <linux/types.h>
struct rtattr;
struct aead_instance {
void (*free)(struct aead_instance *inst);
union {
struct {
char head[offsetof(struct aead_alg, base)];
struct crypto_instance base;
} s;
struct aead_alg alg;
};
};
struct crypto_aead_spawn {
struct crypto_spawn base;
};
struct aead_queue {
struct crypto_queue base;
};
static inline void *crypto_aead_ctx(struct crypto_aead *tfm)
{
return crypto_tfm_ctx(&tfm->base);
}
static inline void *crypto_aead_ctx_dma(struct crypto_aead *tfm)
{
return crypto_tfm_ctx_dma(&tfm->base);
}
static inline struct crypto_instance *aead_crypto_instance(
struct aead_instance *inst)
{
return container_of(&inst->alg.base, struct crypto_instance, alg);
}
static inline struct aead_instance *aead_instance(struct crypto_instance *inst)
{
return container_of(&inst->alg, struct aead_instance, alg.base);
}
static inline struct aead_instance *aead_alg_instance(struct crypto_aead *aead)
{
return aead_instance(crypto_tfm_alg_instance(&aead->base));
}
static inline void *aead_instance_ctx(struct aead_instance *inst)
{
return crypto_instance_ctx(aead_crypto_instance(inst));
}
static inline void *aead_request_ctx(struct aead_request *req)
{
return req->__ctx;
}
static inline void *aead_request_ctx_dma(struct aead_request *req)
{
unsigned int align = crypto_dma_align();
if (align <= crypto_tfm_ctx_alignment())
align = 1;
return PTR_ALIGN(aead_request_ctx(req), align);
}
static inline void aead_request_complete(struct aead_request *req, int err)
{
crypto_request_complete(&req->base, err);
}
static inline u32 aead_request_flags(struct aead_request *req)
{
return req->base.flags;
}
static inline struct aead_request *aead_request_cast(
struct crypto_async_request *req)
{
return container_of(req, struct aead_request, base);
}
int crypto_grab_aead(struct crypto_aead_spawn *spawn,
struct crypto_instance *inst,
const char *name, u32 type, u32 mask);
static inline void crypto_drop_aead(struct crypto_aead_spawn *spawn)
{
crypto_drop_spawn(&spawn->base);
}
static inline struct aead_alg *crypto_spawn_aead_alg(
struct crypto_aead_spawn *spawn)
{
return container_of(spawn->base.alg, struct aead_alg, base);
}
static inline struct crypto_aead *crypto_spawn_aead(
struct crypto_aead_spawn *spawn)
{
return crypto_spawn_tfm2(&spawn->base);
}
static inline void crypto_aead_set_reqsize(struct crypto_aead *aead,
unsigned int reqsize)
{
aead->reqsize = reqsize;
}
static inline void crypto_aead_set_reqsize_dma(struct crypto_aead *aead,
unsigned int reqsize)
{
reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
aead->reqsize = reqsize;
}
static inline void aead_init_queue(struct aead_queue *queue,
unsigned int max_qlen)
{
crypto_init_queue(&queue->base, max_qlen);
}
static inline unsigned int crypto_aead_alg_chunksize(struct aead_alg *alg)
{
return alg->chunksize;
}
/**
* crypto_aead_chunksize() - obtain chunk size
* @tfm: cipher handle
*
* The block size is set to one for ciphers such as CCM. However,
* you still need to provide incremental updates in multiples of
* the underlying block size as the IV does not have sub-block
* granularity. This is known in this API as the chunk size.
*
* Return: chunk size in bytes
*/
static inline unsigned int crypto_aead_chunksize(struct crypto_aead *tfm)
{
return crypto_aead_alg_chunksize(crypto_aead_alg(tfm));
}
int crypto_register_aead(struct aead_alg *alg);
void crypto_unregister_aead(struct aead_alg *alg);
int crypto_register_aeads(struct aead_alg *algs, int count);
void crypto_unregister_aeads(struct aead_alg *algs, int count);
int aead_register_instance(struct crypto_template *tmpl,
struct aead_instance *inst);
#endif /* _CRYPTO_INTERNAL_AEAD_H */
@@ -0,0 +1,155 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Public Key Encryption
*
* Copyright (c) 2015, Intel Corporation
* Authors: Tadeusz Struk <tadeusz.struk@intel.com>
*/
#ifndef _CRYPTO_AKCIPHER_INT_H
#define _CRYPTO_AKCIPHER_INT_H
#include <crypto/akcipher.h>
#include <crypto/algapi.h>
struct akcipher_instance {
void (*free)(struct akcipher_instance *inst);
union {
struct {
char head[offsetof(struct akcipher_alg, base)];
struct crypto_instance base;
} s;
struct akcipher_alg alg;
};
};
struct crypto_akcipher_spawn {
struct crypto_spawn base;
};
/*
* Transform internal helpers.
*/
static inline void *akcipher_request_ctx(struct akcipher_request *req)
{
return req->__ctx;
}
static inline void *akcipher_request_ctx_dma(struct akcipher_request *req)
{
unsigned int align = crypto_dma_align();
if (align <= crypto_tfm_ctx_alignment())
align = 1;
return PTR_ALIGN(akcipher_request_ctx(req), align);
}
static inline void akcipher_set_reqsize(struct crypto_akcipher *akcipher,
unsigned int reqsize)
{
akcipher->reqsize = reqsize;
}
static inline void akcipher_set_reqsize_dma(struct crypto_akcipher *akcipher,
unsigned int reqsize)
{
reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
akcipher->reqsize = reqsize;
}
static inline void *akcipher_tfm_ctx(struct crypto_akcipher *tfm)
{
return crypto_tfm_ctx(&tfm->base);
}
static inline void *akcipher_tfm_ctx_dma(struct crypto_akcipher *tfm)
{
return crypto_tfm_ctx_dma(&tfm->base);
}
static inline void akcipher_request_complete(struct akcipher_request *req,
int err)
{
crypto_request_complete(&req->base, err);
}
static inline const char *akcipher_alg_name(struct crypto_akcipher *tfm)
{
return crypto_akcipher_tfm(tfm)->__crt_alg->cra_name;
}
static inline struct crypto_instance *akcipher_crypto_instance(
struct akcipher_instance *inst)
{
return container_of(&inst->alg.base, struct crypto_instance, alg);
}
static inline struct akcipher_instance *akcipher_instance(
struct crypto_instance *inst)
{
return container_of(&inst->alg, struct akcipher_instance, alg.base);
}
static inline struct akcipher_instance *akcipher_alg_instance(
struct crypto_akcipher *akcipher)
{
return akcipher_instance(crypto_tfm_alg_instance(&akcipher->base));
}
static inline void *akcipher_instance_ctx(struct akcipher_instance *inst)
{
return crypto_instance_ctx(akcipher_crypto_instance(inst));
}
int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
struct crypto_instance *inst,
const char *name, u32 type, u32 mask);
static inline struct crypto_akcipher *crypto_spawn_akcipher(
struct crypto_akcipher_spawn *spawn)
{
return crypto_spawn_tfm2(&spawn->base);
}
static inline void crypto_drop_akcipher(struct crypto_akcipher_spawn *spawn)
{
crypto_drop_spawn(&spawn->base);
}
static inline struct akcipher_alg *crypto_spawn_akcipher_alg(
struct crypto_akcipher_spawn *spawn)
{
return container_of(spawn->base.alg, struct akcipher_alg, base);
}
/**
* crypto_register_akcipher() -- Register public key algorithm
*
* Function registers an implementation of a public key cipher algorithm
*
* @alg: algorithm definition
*
* Return: zero on success; error code in case of error
*/
int crypto_register_akcipher(struct akcipher_alg *alg);
/**
* crypto_unregister_akcipher() -- Unregister public key algorithm
*
* Function unregisters an implementation of a public key cipher algorithm
*
* @alg: algorithm definition
*/
void crypto_unregister_akcipher(struct akcipher_alg *alg);
/**
* akcipher_register_instance() -- Unregister public key template instance
*
* Function registers an implementation of an asymmetric key algorithm
* created from a template
*
* @tmpl: the template from which the algorithm was created
* @inst: the template instance
*/
int akcipher_register_instance(struct crypto_template *tmpl,
struct akcipher_instance *inst);
#endif
@@ -0,0 +1,218 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
* Copyright (c) 2002 David S. Miller (davem@redhat.com)
* Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
*
* Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
* and Nettle, by Niels Möller.
*/
#ifndef _CRYPTO_INTERNAL_CIPHER_H
#define _CRYPTO_INTERNAL_CIPHER_H
#include <crypto/algapi.h>
struct crypto_cipher {
struct crypto_tfm base;
};
/**
* DOC: Single Block Cipher API
*
* The single block cipher API is used with the ciphers of type
* CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
*
* Using the single block cipher API calls, operations with the basic cipher
* primitive can be implemented. These cipher primitives exclude any block
* chaining operations including IV handling.
*
* The purpose of this single block cipher API is to support the implementation
* of templates or other concepts that only need to perform the cipher operation
* on one block at a time. Templates invoke the underlying cipher primitive
* block-wise and process either the input or the output data of these cipher
* operations.
*/
static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
{
return (struct crypto_cipher *)tfm;
}
/**
* crypto_alloc_cipher() - allocate single block cipher handle
* @alg_name: is the cra_name / name or cra_driver_name / driver name of the
* single block cipher
* @type: specifies the type of the cipher
* @mask: specifies the mask for the cipher
*
* Allocate a cipher handle for a single block cipher. The returned struct
* crypto_cipher is the cipher handle that is required for any subsequent API
* invocation for that single block cipher.
*
* Return: allocated cipher handle in case of success; IS_ERR() is true in case
* of an error, PTR_ERR() returns the error code.
*/
static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
u32 type, u32 mask)
{
type &= ~CRYPTO_ALG_TYPE_MASK;
type |= CRYPTO_ALG_TYPE_CIPHER;
mask |= CRYPTO_ALG_TYPE_MASK;
return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
}
static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
{
return &tfm->base;
}
/**
* crypto_free_cipher() - zeroize and free the single block cipher handle
* @tfm: cipher handle to be freed
*/
static inline void crypto_free_cipher(struct crypto_cipher *tfm)
{
crypto_free_tfm(crypto_cipher_tfm(tfm));
}
/**
* crypto_has_cipher() - Search for the availability of a single block cipher
* @alg_name: is the cra_name / name or cra_driver_name / driver name of the
* single block cipher
* @type: specifies the type of the cipher
* @mask: specifies the mask for the cipher
*
* Return: true when the single block cipher is known to the kernel crypto API;
* false otherwise
*/
static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
{
type &= ~CRYPTO_ALG_TYPE_MASK;
type |= CRYPTO_ALG_TYPE_CIPHER;
mask |= CRYPTO_ALG_TYPE_MASK;
return crypto_has_alg(alg_name, type, mask);
}
/**
* crypto_cipher_blocksize() - obtain block size for cipher
* @tfm: cipher handle
*
* The block size for the single block cipher referenced with the cipher handle
* tfm is returned. The caller may use that information to allocate appropriate
* memory for the data returned by the encryption or decryption operation
*
* Return: block size of cipher
*/
static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
{
return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
}
static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
{
return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
}
static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
{
return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
}
static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
u32 flags)
{
crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
}
static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
u32 flags)
{
crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
}
/**
* crypto_cipher_setkey() - set key for cipher
* @tfm: cipher handle
* @key: buffer holding the key
* @keylen: length of the key in bytes
*
* The caller provided key is set for the single block cipher referenced by the
* cipher handle.
*
* Note, the key length determines the cipher type. Many block ciphers implement
* different cipher modes depending on the key size, such as AES-128 vs AES-192
* vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
* is performed.
*
* Return: 0 if the setting of the key was successful; < 0 if an error occurred
*/
int crypto_cipher_setkey(struct crypto_cipher *tfm,
const u8 *key, unsigned int keylen);
/**
* crypto_cipher_encrypt_one() - encrypt one block of plaintext
* @tfm: cipher handle
* @dst: points to the buffer that will be filled with the ciphertext
* @src: buffer holding the plaintext to be encrypted
*
* Invoke the encryption operation of one block. The caller must ensure that
* the plaintext and ciphertext buffers are at least one block in size.
*/
void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
u8 *dst, const u8 *src);
/**
* crypto_cipher_decrypt_one() - decrypt one block of ciphertext
* @tfm: cipher handle
* @dst: points to the buffer that will be filled with the plaintext
* @src: buffer holding the ciphertext to be decrypted
*
* Invoke the decryption operation of one block. The caller must ensure that
* the plaintext and ciphertext buffers are at least one block in size.
*/
void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
u8 *dst, const u8 *src);
struct crypto_cipher_spawn {
struct crypto_spawn base;
};
static inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn,
struct crypto_instance *inst,
const char *name, u32 type, u32 mask)
{
type &= ~CRYPTO_ALG_TYPE_MASK;
type |= CRYPTO_ALG_TYPE_CIPHER;
mask |= CRYPTO_ALG_TYPE_MASK;
return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
}
static inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn)
{
crypto_drop_spawn(&spawn->base);
}
static inline struct crypto_alg *crypto_spawn_cipher_alg(
struct crypto_cipher_spawn *spawn)
{
return spawn->base.alg;
}
static inline struct crypto_cipher *crypto_spawn_cipher(
struct crypto_cipher_spawn *spawn)
{
u32 type = CRYPTO_ALG_TYPE_CIPHER;
u32 mask = CRYPTO_ALG_TYPE_MASK;
return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask));
}
static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
{
return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
}
#endif
+127
View File
@@ -0,0 +1,127 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* DES & Triple DES EDE key verification helpers
*/
#ifndef __CRYPTO_INTERNAL_DES_H
#define __CRYPTO_INTERNAL_DES_H
#include <linux/crypto.h>
#include <linux/fips.h>
#include <crypto/des.h>
#include <crypto/aead.h>
#include <crypto/skcipher.h>
/**
* crypto_des_verify_key - Check whether a DES key is weak
* @tfm: the crypto algo
* @key: the key buffer
*
* Returns -EINVAL if the key is weak and the crypto TFM does not permit weak
* keys. Otherwise, 0 is returned.
*
* It is the job of the caller to ensure that the size of the key equals
* DES_KEY_SIZE.
*/
static inline int crypto_des_verify_key(struct crypto_tfm *tfm, const u8 *key)
{
struct des_ctx tmp;
int err;
err = des_expand_key(&tmp, key, DES_KEY_SIZE);
if (err == -ENOKEY) {
if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
err = -EINVAL;
else
err = 0;
}
memzero_explicit(&tmp, sizeof(tmp));
return err;
}
/*
* RFC2451:
*
* For DES-EDE3, there is no known need to reject weak or
* complementation keys. Any weakness is obviated by the use of
* multiple keys.
*
* However, if the first two or last two independent 64-bit keys are
* equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
* same as DES. Implementers MUST reject keys that exhibit this
* property.
*
*/
static inline int des3_ede_verify_key(const u8 *key, unsigned int key_len,
bool check_weak)
{
int ret = fips_enabled ? -EINVAL : -ENOKEY;
u32 K[6];
memcpy(K, key, DES3_EDE_KEY_SIZE);
if ((!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
!((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
(fips_enabled || check_weak))
goto bad;
if ((!((K[0] ^ K[4]) | (K[1] ^ K[5]))) && fips_enabled)
goto bad;
ret = 0;
bad:
memzero_explicit(K, DES3_EDE_KEY_SIZE);
return ret;
}
/**
* crypto_des3_ede_verify_key - Check whether a DES3-EDE key is weak
* @tfm: the crypto algo
* @key: the key buffer
*
* Returns -EINVAL if the key is weak and the crypto TFM does not permit weak
* keys or when running in FIPS mode. Otherwise, 0 is returned. Note that some
* keys are rejected in FIPS mode even if weak keys are permitted by the TFM
* flags.
*
* It is the job of the caller to ensure that the size of the key equals
* DES3_EDE_KEY_SIZE.
*/
static inline int crypto_des3_ede_verify_key(struct crypto_tfm *tfm,
const u8 *key)
{
return des3_ede_verify_key(key, DES3_EDE_KEY_SIZE,
crypto_tfm_get_flags(tfm) &
CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
}
static inline int verify_skcipher_des_key(struct crypto_skcipher *tfm,
const u8 *key)
{
return crypto_des_verify_key(crypto_skcipher_tfm(tfm), key);
}
static inline int verify_skcipher_des3_key(struct crypto_skcipher *tfm,
const u8 *key)
{
return crypto_des3_ede_verify_key(crypto_skcipher_tfm(tfm), key);
}
static inline int verify_aead_des_key(struct crypto_aead *tfm, const u8 *key,
int keylen)
{
if (keylen != DES_KEY_SIZE)
return -EINVAL;
return crypto_des_verify_key(crypto_aead_tfm(tfm), key);
}
static inline int verify_aead_des3_key(struct crypto_aead *tfm, const u8 *key,
int keylen)
{
if (keylen != DES3_EDE_KEY_SIZE)
return -EINVAL;
return crypto_des3_ede_verify_key(crypto_aead_tfm(tfm), key);
}
#endif /* __CRYPTO_INTERNAL_DES_H */
+312
View File
@@ -0,0 +1,312 @@
/*
* Copyright (c) 2013, Kenneth MacKay
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _CRYPTO_ECC_H
#define _CRYPTO_ECC_H
#include <crypto/ecc_curve.h>
#include <linux/unaligned.h>
/* One digit is u64 qword. */
#define ECC_CURVE_NIST_P192_DIGITS 3
#define ECC_CURVE_NIST_P256_DIGITS 4
#define ECC_CURVE_NIST_P384_DIGITS 6
#define ECC_CURVE_NIST_P521_DIGITS 9
#define ECC_MAX_DIGITS DIV_ROUND_UP(521, 64) /* NIST P521 */
#define ECC_DIGITS_TO_BYTES_SHIFT 3
#define ECC_MAX_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT)
#define ECC_POINT_INIT(x, y, ndigits) (struct ecc_point) { x, y, ndigits }
/*
* The integers r and s making up the signature are expected to be
* formatted as two consecutive u64 arrays of size ECC_MAX_BYTES.
* The bytes within each u64 digit are in native endianness,
* but the order of the u64 digits themselves is little endian.
* This format allows direct use by internal vli_*() functions.
*/
struct ecdsa_raw_sig {
u64 r[ECC_MAX_DIGITS];
u64 s[ECC_MAX_DIGITS];
};
/**
* ecc_swap_digits() - Copy ndigits from big endian array to native array
* @in: Input array
* @out: Output array
* @ndigits: Number of digits to copy
*/
static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigits)
{
const __be64 *src = (__force __be64 *)in;
int i;
for (i = 0; i < ndigits; i++)
out[i] = get_unaligned_be64(&src[ndigits - 1 - i]);
}
/**
* ecc_digits_from_bytes() - Create ndigits-sized digits array from byte array
* @in: Input byte array
* @nbytes: Size of input byte array
* @out: Output digits array
* @ndigits: Number of digits to create from byte array
*
* The first byte in the input byte array is expected to hold the most
* significant bits of the large integer.
*/
void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
u64 *out, unsigned int ndigits);
/**
* ecc_is_key_valid() - Validate a given ECDH private key
*
* @curve_id: id representing the curve to use
* @ndigits: curve's number of digits
* @private_key: private key to be used for the given curve
* @private_key_len: private key length
*
* Returns: 0 if the key is acceptable, a negative value otherwise
*/
int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
const u64 *private_key, unsigned int private_key_len);
/**
* ecc_gen_privkey() - Generates an ECC private key.
* The private key is a random integer in the range 0 < random < n, where n is a
* prime that is the order of the cyclic subgroup generated by the distinguished
* point G.
* @curve_id: id representing the curve to use
* @ndigits: curve number of digits
* @private_key: buffer for storing the generated private key
*
* Returns: 0 if the private key was generated successfully, a negative value
* if an error occurred.
*/
int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits,
u64 *private_key);
/**
* ecc_make_pub_key() - Compute an ECC public key
*
* @curve_id: id representing the curve to use
* @ndigits: curve's number of digits
* @private_key: pregenerated private key for the given curve
* @public_key: buffer for storing the generated public key
*
* Returns: 0 if the public key was generated successfully, a negative value
* if an error occurred.
*/
int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
const u64 *private_key, u64 *public_key);
/**
* crypto_ecdh_shared_secret() - Compute a shared secret
*
* @curve_id: id representing the curve to use
* @ndigits: curve's number of digits
* @private_key: private key of part A
* @public_key: public key of counterpart B
* @secret: buffer for storing the calculated shared secret
*
* Note: It is recommended that you hash the result of crypto_ecdh_shared_secret
* before using it for symmetric encryption or HMAC.
*
* Returns: 0 if the shared secret was generated successfully, a negative value
* if an error occurred.
*/
int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
const u64 *private_key, const u64 *public_key,
u64 *secret);
/**
* ecc_is_pubkey_valid_partial() - Partial public key validation
*
* @curve: elliptic curve domain parameters
* @pk: public key as a point
*
* Valdiate public key according to SP800-56A section 5.6.2.3.4 ECC Partial
* Public-Key Validation Routine.
*
* Note: There is no check that the public key is in the correct elliptic curve
* subgroup.
*
* Return: 0 if validation is successful, -EINVAL if validation is failed.
*/
int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
struct ecc_point *pk);
/**
* ecc_is_pubkey_valid_full() - Full public key validation
*
* @curve: elliptic curve domain parameters
* @pk: public key as a point
*
* Valdiate public key according to SP800-56A section 5.6.2.3.3 ECC Full
* Public-Key Validation Routine.
*
* Return: 0 if validation is successful, -EINVAL if validation is failed.
*/
int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
struct ecc_point *pk);
/**
* vli_is_zero() - Determine is vli is zero
*
* @vli: vli to check.
* @ndigits: length of the @vli
*
* Returns: %true if vli == 0, %false otherwise.
*/
bool vli_is_zero(const u64 *vli, unsigned int ndigits);
/**
* vli_cmp() - compare left and right vlis
*
* @left: vli
* @right: vli
* @ndigits: length of both vlis
*
* Returns: sign of @left - @right, i.e. -1 if @left < @right,
* 0 if @left == @right, 1 if @left > @right.
*/
int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits);
/**
* vli_sub() - Subtracts right from left
*
* @result: where to write result
* @left: vli
* @right: vli
* @ndigits: length of all vlis
*
* Note: can modify in-place.
*
* Return: carry bit.
*/
u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
unsigned int ndigits);
/**
* vli_from_be64() - Load vli from big-endian u64 array
*
* @dest: destination vli
* @src: source array of u64 BE values
* @ndigits: length of both vli and array
*/
void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits);
/**
* vli_from_le64() - Load vli from little-endian u64 array
*
* @dest: destination vli
* @src: source array of u64 LE values
* @ndigits: length of both vli and array
*/
void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits);
/**
* vli_mod_inv() - Modular inversion
*
* @result: where to write vli number
* @input: vli value to operate on
* @mod: modulus
* @ndigits: length of all vlis
*/
void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
unsigned int ndigits);
/**
* vli_mod_mult_slow() - Modular multiplication
*
* @result: where to write result value
* @left: vli number to multiply with @right
* @right: vli number to multiply with @left
* @mod: modulus
* @ndigits: length of all vlis
*
* Note: Assumes that mod is big enough curve order.
*/
void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
const u64 *mod, unsigned int ndigits);
/**
* vli_num_bits() - Counts the number of bits required for vli.
*
* @vli: vli to check.
* @ndigits: Length of the @vli
*
* Return: The number of bits required to represent @vli.
*/
unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits);
/**
* ecc_alloc_point() - Allocate ECC point.
*
* @ndigits: Length of vlis in u64 qwords.
*
* Return: Pointer to the allocated point or NULL if allocation failed.
*/
struct ecc_point *ecc_alloc_point(unsigned int ndigits);
/**
* ecc_free_point() - Free ECC point.
*
* @p: The point to free.
*/
void ecc_free_point(struct ecc_point *p);
/**
* ecc_point_is_zero() - Check if point is zero.
*
* @point: Point to check for zero.
*
* Return: true if point is the point at infinity, false otherwise.
*/
bool ecc_point_is_zero(const struct ecc_point *point);
/**
* ecc_point_mult_shamir() - Add two points multiplied by scalars
*
* @result: resulting point
* @x: scalar to multiply with @p
* @p: point to multiply with @x
* @y: scalar to multiply with @q
* @q: point to multiply with @y
* @curve: curve
*
* Returns result = x * p + x * q over the curve.
* This works faster than two multiplications and addition.
*/
void ecc_point_mult_shamir(const struct ecc_point *result,
const u64 *x, const struct ecc_point *p,
const u64 *y, const struct ecc_point *q,
const struct ecc_curve *curve);
extern struct crypto_template ecdsa_x962_tmpl;
extern struct crypto_template ecdsa_p1363_tmpl;
#endif
@@ -0,0 +1,58 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Crypto engine API
*
* Copyright (c) 2016 Baolin Wang <baolin.wang@linaro.org>
* Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au>
*/
#ifndef _CRYPTO_INTERNAL_ENGINE_H
#define _CRYPTO_INTERNAL_ENGINE_H
#include <crypto/algapi.h>
#include <crypto/engine.h>
#include <linux/kthread.h>
#include <linux/spinlock_types.h>
#include <linux/types.h>
#define ENGINE_NAME_LEN 30
struct device;
/*
* struct crypto_engine - crypto hardware engine
* @name: the engine name
* @busy: request pump is busy
* @running: the engine is on working
* @retry_support: indication that the hardware allows re-execution
* of a failed backlog request
* crypto-engine, in head position to keep order
* @rt: whether this queue is set to run as a realtime task
* @list: link with the global crypto engine list
* @queue_lock: spinlock to synchronise access to request queue
* @queue: the crypto queue of the engine
* @kworker: kthread worker struct for request pump
* @pump_requests: work struct for scheduling work to the request pump
* @priv_data: the engine private data
* @cur_req: the current request which is on processing
*/
struct crypto_engine {
char name[ENGINE_NAME_LEN];
bool busy;
bool running;
bool retry_support;
bool rt;
struct list_head list;
spinlock_t queue_lock;
struct crypto_queue queue __guarded_by(&queue_lock);
struct device *dev;
struct kthread_worker *kworker;
struct kthread_work pump_requests;
void *priv_data;
struct crypto_async_request *cur_req;
};
#endif
@@ -0,0 +1,24 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* geniv: IV generation
*
* Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
*/
#ifndef _CRYPTO_INTERNAL_GENIV_H
#define _CRYPTO_INTERNAL_GENIV_H
#include <crypto/internal/aead.h>
#include <linux/types.h>
struct aead_geniv_ctx {
struct crypto_aead *child;
u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
};
struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl,
struct rtattr **tb);
int aead_init_geniv(struct crypto_aead *tfm);
void aead_exit_geniv(struct crypto_aead *tfm);
#endif /* _CRYPTO_INTERNAL_GENIV_H */
+405
View File
@@ -0,0 +1,405 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Hash algorithms.
*
* Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
*/
#ifndef _CRYPTO_INTERNAL_HASH_H
#define _CRYPTO_INTERNAL_HASH_H
#include <crypto/algapi.h>
#include <crypto/hash.h>
/* Set this bit to handle partial blocks in the API. */
#define CRYPTO_AHASH_ALG_BLOCK_ONLY 0x01000000
/* Set this bit if final requires at least one byte. */
#define CRYPTO_AHASH_ALG_FINAL_NONZERO 0x02000000
/* Set this bit if finup can deal with multiple blocks. */
#define CRYPTO_AHASH_ALG_FINUP_MAX 0x04000000
/* This bit is set by the Crypto API if export_core is not supported. */
#define CRYPTO_AHASH_ALG_NO_EXPORT_CORE 0x08000000
#define HASH_FBREQ_ON_STACK(name, req) \
char __##name##_req[sizeof(struct ahash_request) + \
MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \
struct ahash_request *name = ahash_fbreq_on_stack_init( \
__##name##_req, (req))
struct ahash_request;
struct scatterlist;
struct crypto_hash_walk {
const char *data;
unsigned int offset;
unsigned int flags;
struct page *pg;
unsigned int entrylen;
unsigned int total;
struct scatterlist *sg;
};
struct ahash_instance {
void (*free)(struct ahash_instance *inst);
union {
struct {
char head[offsetof(struct ahash_alg, halg.base)];
struct crypto_instance base;
} s;
struct ahash_alg alg;
};
};
struct shash_instance {
void (*free)(struct shash_instance *inst);
union {
struct {
char head[offsetof(struct shash_alg, base)];
struct crypto_instance base;
} s;
struct shash_alg alg;
};
};
struct crypto_ahash_spawn {
struct crypto_spawn base;
};
struct crypto_shash_spawn {
struct crypto_spawn base;
};
int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err);
int crypto_hash_walk_first(struct ahash_request *req,
struct crypto_hash_walk *walk);
static inline int crypto_hash_walk_last(struct crypto_hash_walk *walk)
{
return !(walk->entrylen | walk->total);
}
int crypto_register_ahash(struct ahash_alg *alg);
void crypto_unregister_ahash(struct ahash_alg *alg);
int crypto_register_ahashes(struct ahash_alg *algs, int count);
void crypto_unregister_ahashes(struct ahash_alg *algs, int count);
int ahash_register_instance(struct crypto_template *tmpl,
struct ahash_instance *inst);
void ahash_free_singlespawn_instance(struct ahash_instance *inst);
int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
unsigned int keylen);
static inline bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
{
return alg->setkey != shash_no_setkey;
}
bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg);
static inline bool crypto_shash_alg_needs_key(struct shash_alg *alg)
{
return crypto_shash_alg_has_setkey(alg) &&
!(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
}
static inline bool crypto_hash_alg_needs_key(struct hash_alg_common *alg)
{
return crypto_hash_alg_has_setkey(alg) &&
!(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
}
static inline bool crypto_hash_no_export_core(struct crypto_ahash *tfm)
{
return crypto_hash_alg_common(tfm)->base.cra_flags &
CRYPTO_AHASH_ALG_NO_EXPORT_CORE;
}
int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
struct crypto_instance *inst,
const char *name, u32 type, u32 mask);
static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn)
{
crypto_drop_spawn(&spawn->base);
}
static inline struct hash_alg_common *crypto_spawn_ahash_alg(
struct crypto_ahash_spawn *spawn)
{
return __crypto_hash_alg_common(spawn->base.alg);
}
int crypto_register_shash(struct shash_alg *alg);
void crypto_unregister_shash(struct shash_alg *alg);
int crypto_register_shashes(struct shash_alg *algs, int count);
void crypto_unregister_shashes(struct shash_alg *algs, int count);
int shash_register_instance(struct crypto_template *tmpl,
struct shash_instance *inst);
void shash_free_singlespawn_instance(struct shash_instance *inst);
int crypto_grab_shash(struct crypto_shash_spawn *spawn,
struct crypto_instance *inst,
const char *name, u32 type, u32 mask);
static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)
{
crypto_drop_spawn(&spawn->base);
}
static inline struct shash_alg *crypto_spawn_shash_alg(
struct crypto_shash_spawn *spawn)
{
return __crypto_shash_alg(spawn->base.alg);
}
int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
{
return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
}
static inline void *crypto_ahash_ctx_dma(struct crypto_ahash *tfm)
{
return crypto_tfm_ctx_dma(crypto_ahash_tfm(tfm));
}
static inline struct ahash_alg *__crypto_ahash_alg(struct crypto_alg *alg)
{
return container_of(__crypto_hash_alg_common(alg), struct ahash_alg,
halg);
}
static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
{
return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
halg);
}
static inline void crypto_ahash_set_statesize(struct crypto_ahash *tfm,
unsigned int size)
{
tfm->statesize = size;
}
static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm,
unsigned int reqsize)
{
tfm->reqsize = reqsize;
}
static inline bool crypto_ahash_tested(struct crypto_ahash *tfm)
{
struct crypto_tfm *tfm_base = crypto_ahash_tfm(tfm);
return tfm_base->__crt_alg->cra_flags & CRYPTO_ALG_TESTED;
}
static inline void crypto_ahash_set_reqsize_dma(struct crypto_ahash *ahash,
unsigned int reqsize)
{
reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
ahash->reqsize = reqsize;
}
static inline struct crypto_instance *ahash_crypto_instance(
struct ahash_instance *inst)
{
return &inst->s.base;
}
static inline struct ahash_instance *ahash_instance(
struct crypto_instance *inst)
{
return container_of(inst, struct ahash_instance, s.base);
}
static inline struct ahash_instance *ahash_alg_instance(
struct crypto_ahash *ahash)
{
return ahash_instance(crypto_tfm_alg_instance(&ahash->base));
}
static inline void *ahash_instance_ctx(struct ahash_instance *inst)
{
return crypto_instance_ctx(ahash_crypto_instance(inst));
}
static inline void *ahash_request_ctx_dma(struct ahash_request *req)
{
unsigned int align = crypto_dma_align();
if (align <= crypto_tfm_ctx_alignment())
align = 1;
return PTR_ALIGN(ahash_request_ctx(req), align);
}
static inline void ahash_request_complete(struct ahash_request *req, int err)
{
crypto_request_complete(&req->base, err);
}
static inline u32 ahash_request_flags(struct ahash_request *req)
{
return crypto_request_flags(&req->base) & ~CRYPTO_AHASH_REQ_PRIVATE;
}
static inline struct crypto_ahash *crypto_spawn_ahash(
struct crypto_ahash_spawn *spawn)
{
return crypto_spawn_tfm2(&spawn->base);
}
static inline int ahash_enqueue_request(struct crypto_queue *queue,
struct ahash_request *request)
{
return crypto_enqueue_request(queue, &request->base);
}
static inline struct ahash_request *ahash_dequeue_request(
struct crypto_queue *queue)
{
return ahash_request_cast(crypto_dequeue_request(queue));
}
static inline void *crypto_shash_ctx(struct crypto_shash *tfm)
{
return crypto_tfm_ctx(&tfm->base);
}
static inline struct crypto_instance *shash_crypto_instance(
struct shash_instance *inst)
{
return &inst->s.base;
}
static inline struct shash_instance *shash_instance(
struct crypto_instance *inst)
{
return container_of(inst, struct shash_instance, s.base);
}
static inline struct shash_instance *shash_alg_instance(
struct crypto_shash *shash)
{
return shash_instance(crypto_tfm_alg_instance(&shash->base));
}
static inline void *shash_instance_ctx(struct shash_instance *inst)
{
return crypto_instance_ctx(shash_crypto_instance(inst));
}
static inline struct crypto_shash *crypto_spawn_shash(
struct crypto_shash_spawn *spawn)
{
return crypto_spawn_tfm2(&spawn->base);
}
static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
{
return container_of(tfm, struct crypto_shash, base);
}
static inline bool ahash_request_isvirt(struct ahash_request *req)
{
return req->base.flags & CRYPTO_AHASH_REQ_VIRT;
}
static inline bool crypto_ahash_req_virt(struct crypto_ahash *tfm)
{
return crypto_tfm_req_virt(&tfm->base);
}
static inline struct crypto_ahash *crypto_ahash_fb(struct crypto_ahash *tfm)
{
return __crypto_ahash_cast(crypto_ahash_tfm(tfm)->fb);
}
static inline struct ahash_request *ahash_fbreq_on_stack_init(
char *buf, struct ahash_request *old)
{
struct crypto_ahash *tfm = crypto_ahash_reqtfm(old);
struct ahash_request *req = (void *)buf;
crypto_stack_request_init(&req->base,
crypto_ahash_tfm(crypto_ahash_fb(tfm)));
ahash_request_set_callback(req, ahash_request_flags(old), NULL, NULL);
req->base.flags &= ~CRYPTO_AHASH_REQ_PRIVATE;
req->base.flags |= old->base.flags & CRYPTO_AHASH_REQ_PRIVATE;
req->src = old->src;
req->result = old->result;
req->nbytes = old->nbytes;
return req;
}
/* Return the state size without partial block for block-only algorithms. */
static inline unsigned int crypto_shash_coresize(struct crypto_shash *tfm)
{
return crypto_shash_statesize(tfm) - crypto_shash_blocksize(tfm) - 1;
}
/* This can only be used if the request was never cloned. */
#define HASH_REQUEST_ZERO(name) \
memzero_explicit(__##name##_req, sizeof(__##name##_req))
/**
* crypto_ahash_export_core() - extract core state for message digest
* @req: reference to the ahash_request handle whose state is exported
* @out: output buffer of sufficient size that can hold the hash state
*
* Export the hash state without the partial block buffer.
*
* Context: Softirq or process context.
* Return: 0 if the export creation was successful; < 0 if an error occurred
*/
int crypto_ahash_export_core(struct ahash_request *req, void *out);
/**
* crypto_ahash_import_core() - import core state
* @req: reference to ahash_request handle the state is imported into
* @in: buffer holding the state
*
* Import the hash state without the partial block buffer.
*
* Context: Softirq or process context.
* Return: 0 if the import was successful; < 0 if an error occurred
*/
int crypto_ahash_import_core(struct ahash_request *req, const void *in);
/**
* crypto_shash_export_core() - extract core state for message digest
* @desc: reference to the operational state handle whose state is exported
* @out: output buffer of sufficient size that can hold the hash state
*
* Export the hash state without the partial block buffer.
*
* Context: Softirq or process context.
* Return: 0 if the export creation was successful; < 0 if an error occurred
*/
int crypto_shash_export_core(struct shash_desc *desc, void *out);
/**
* crypto_shash_import_core() - import core state
* @desc: reference to the operational state handle the state imported into
* @in: buffer holding the state
*
* Import the hash state without the partial block buffer.
*
* Context: Softirq or process context.
* Return: 0 if the import was successful; < 0 if an error occurred
*/
int crypto_shash_import_core(struct shash_desc *desc, const void *in);
#endif /* _CRYPTO_INTERNAL_HASH_H */
@@ -0,0 +1,71 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2021, Stephan Mueller <smueller@chronox.de>
*/
#ifndef _CRYPTO_KDF_SELFTEST_H
#define _CRYPTO_KDF_SELFTEST_H
#include <crypto/hash.h>
#include <linux/uio.h>
struct kdf_testvec {
unsigned char *key;
size_t keylen;
unsigned char *ikm;
size_t ikmlen;
struct kvec info;
unsigned char *expected;
size_t expectedlen;
};
static inline int
kdf_test(const struct kdf_testvec *test, const char *name,
int (*crypto_kdf_setkey)(struct crypto_shash *kmd,
const u8 *key, size_t keylen,
const u8 *ikm, size_t ikmlen),
int (*crypto_kdf_generate)(struct crypto_shash *kmd,
const struct kvec *info,
unsigned int info_nvec,
u8 *dst, unsigned int dlen))
{
struct crypto_shash *kmd;
int ret;
u8 *buf = kzalloc(test->expectedlen, GFP_KERNEL);
if (!buf)
return -ENOMEM;
kmd = crypto_alloc_shash(name, 0, 0);
if (IS_ERR(kmd)) {
pr_err("alg: kdf: could not allocate hash handle for %s\n",
name);
kfree(buf);
return -ENOMEM;
}
ret = crypto_kdf_setkey(kmd, test->key, test->keylen,
test->ikm, test->ikmlen);
if (ret) {
pr_err("alg: kdf: could not set key derivation key\n");
goto err;
}
ret = crypto_kdf_generate(kmd, &test->info, 1, buf, test->expectedlen);
if (ret) {
pr_err("alg: kdf: could not obtain key data\n");
goto err;
}
ret = memcmp(test->expected, buf, test->expectedlen);
if (ret)
ret = -EINVAL;
err:
crypto_free_shash(kmd);
kfree(buf);
return ret;
}
#endif /* _CRYPTO_KDF_SELFTEST_H */
+245
View File
@@ -0,0 +1,245 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Key-agreement Protocol Primitives (KPP)
*
* Copyright (c) 2016, Intel Corporation
* Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
*/
#ifndef _CRYPTO_KPP_INT_H
#define _CRYPTO_KPP_INT_H
#include <crypto/kpp.h>
#include <crypto/algapi.h>
/**
* struct kpp_instance - KPP template instance
* @free: Callback getting invoked upon instance destruction. Must be set.
* @s: Internal. Generic crypto core instance state properly layout
* to alias with @alg as needed.
* @alg: The &struct kpp_alg implementation provided by the instance.
*/
struct kpp_instance {
void (*free)(struct kpp_instance *inst);
union {
struct {
char head[offsetof(struct kpp_alg, base)];
struct crypto_instance base;
} s;
struct kpp_alg alg;
};
};
/**
* struct crypto_kpp_spawn - KPP algorithm spawn
* @base: Internal. Generic crypto core spawn state.
*
* Template instances can get a hold on some inner KPP algorithm by
* binding a &struct crypto_kpp_spawn via
* crypto_grab_kpp(). Transforms may subsequently get instantiated
* from the referenced inner &struct kpp_alg by means of
* crypto_spawn_kpp().
*/
struct crypto_kpp_spawn {
struct crypto_spawn base;
};
/*
* Transform internal helpers.
*/
static inline void *kpp_request_ctx(struct kpp_request *req)
{
return req->__ctx;
}
static inline void *kpp_request_ctx_dma(struct kpp_request *req)
{
unsigned int align = crypto_dma_align();
if (align <= crypto_tfm_ctx_alignment())
align = 1;
return PTR_ALIGN(kpp_request_ctx(req), align);
}
static inline void kpp_set_reqsize(struct crypto_kpp *kpp,
unsigned int reqsize)
{
kpp->reqsize = reqsize;
}
static inline void kpp_set_reqsize_dma(struct crypto_kpp *kpp,
unsigned int reqsize)
{
reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
kpp->reqsize = reqsize;
}
static inline void *kpp_tfm_ctx(struct crypto_kpp *tfm)
{
return crypto_tfm_ctx(&tfm->base);
}
static inline void *kpp_tfm_ctx_dma(struct crypto_kpp *tfm)
{
return crypto_tfm_ctx_dma(&tfm->base);
}
static inline void kpp_request_complete(struct kpp_request *req, int err)
{
crypto_request_complete(&req->base, err);
}
static inline const char *kpp_alg_name(struct crypto_kpp *tfm)
{
return crypto_kpp_tfm(tfm)->__crt_alg->cra_name;
}
/*
* Template instance internal helpers.
*/
/**
* kpp_crypto_instance() - Cast a &struct kpp_instance to the corresponding
* generic &struct crypto_instance.
* @inst: Pointer to the &struct kpp_instance to be cast.
* Return: A pointer to the &struct crypto_instance embedded in @inst.
*/
static inline struct crypto_instance *kpp_crypto_instance(
struct kpp_instance *inst)
{
return &inst->s.base;
}
/**
* kpp_instance() - Cast a generic &struct crypto_instance to the corresponding
* &struct kpp_instance.
* @inst: Pointer to the &struct crypto_instance to be cast.
* Return: A pointer to the &struct kpp_instance @inst is embedded in.
*/
static inline struct kpp_instance *kpp_instance(struct crypto_instance *inst)
{
return container_of(inst, struct kpp_instance, s.base);
}
/**
* kpp_alg_instance() - Get the &struct kpp_instance a given KPP transform has
* been instantiated from.
* @kpp: The KPP transform instantiated from some &struct kpp_instance.
* Return: The &struct kpp_instance associated with @kpp.
*/
static inline struct kpp_instance *kpp_alg_instance(struct crypto_kpp *kpp)
{
return kpp_instance(crypto_tfm_alg_instance(&kpp->base));
}
/**
* kpp_instance_ctx() - Get a pointer to a &struct kpp_instance's implementation
* specific context data.
* @inst: The &struct kpp_instance whose context data to access.
*
* A KPP template implementation may allocate extra memory beyond the
* end of a &struct kpp_instance instantiated from &crypto_template.create().
* This function provides a means to obtain a pointer to this area.
*
* Return: A pointer to the implementation specific context data.
*/
static inline void *kpp_instance_ctx(struct kpp_instance *inst)
{
return crypto_instance_ctx(kpp_crypto_instance(inst));
}
/*
* KPP algorithm (un)registration functions.
*/
/**
* crypto_register_kpp() -- Register key-agreement protocol primitives algorithm
*
* Function registers an implementation of a key-agreement protocol primitive
* algorithm
*
* @alg: algorithm definition
*
* Return: zero on success; error code in case of error
*/
int crypto_register_kpp(struct kpp_alg *alg);
/**
* crypto_unregister_kpp() -- Unregister key-agreement protocol primitive
* algorithm
*
* Function unregisters an implementation of a key-agreement protocol primitive
* algorithm
*
* @alg: algorithm definition
*/
void crypto_unregister_kpp(struct kpp_alg *alg);
/**
* kpp_register_instance() - Register a KPP template instance.
* @tmpl: The instantiating template.
* @inst: The KPP template instance to be registered.
* Return: %0 on success, negative error code otherwise.
*/
int kpp_register_instance(struct crypto_template *tmpl,
struct kpp_instance *inst);
/*
* KPP spawn related functions.
*/
/**
* crypto_grab_kpp() - Look up a KPP algorithm and bind a spawn to it.
* @spawn: The KPP spawn to bind.
* @inst: The template instance owning @spawn.
* @name: The KPP algorithm name to look up.
* @type: The type bitset to pass on to the lookup.
* @mask: The mask bismask to pass on to the lookup.
* Return: %0 on success, a negative error code otherwise.
*/
int crypto_grab_kpp(struct crypto_kpp_spawn *spawn,
struct crypto_instance *inst,
const char *name, u32 type, u32 mask);
/**
* crypto_drop_kpp() - Release a spawn previously bound via crypto_grab_kpp().
* @spawn: The spawn to release.
*/
static inline void crypto_drop_kpp(struct crypto_kpp_spawn *spawn)
{
crypto_drop_spawn(&spawn->base);
}
/**
* crypto_spawn_kpp_alg() - Get the algorithm a KPP spawn has been bound to.
* @spawn: The spawn to get the referenced &struct kpp_alg for.
*
* This function as well as the returned result are safe to use only
* after @spawn has been successfully bound via crypto_grab_kpp() and
* up to until the template instance owning @spawn has either been
* registered successfully or the spawn has been released again via
* crypto_drop_spawn().
*
* Return: A pointer to the &struct kpp_alg referenced from the spawn.
*/
static inline struct kpp_alg *crypto_spawn_kpp_alg(
struct crypto_kpp_spawn *spawn)
{
return container_of(spawn->base.alg, struct kpp_alg, base);
}
/**
* crypto_spawn_kpp() - Create a transform from a KPP spawn.
* @spawn: The spawn previously bound to some &struct kpp_alg via
* crypto_grab_kpp().
*
* Once a &struct crypto_kpp_spawn has been successfully bound to a
* &struct kpp_alg via crypto_grab_kpp(), transforms for the latter
* may get instantiated from the former by means of this function.
*
* Return: A pointer to the freshly created KPP transform on success
* or an ``ERR_PTR()`` otherwise.
*/
static inline struct crypto_kpp *crypto_spawn_kpp(
struct crypto_kpp_spawn *spawn)
{
return crypto_spawn_tfm2(&spawn->base);
}
#endif
@@ -0,0 +1,56 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Common values for the Poly1305 algorithm
*/
#ifndef _CRYPTO_INTERNAL_POLY1305_H
#define _CRYPTO_INTERNAL_POLY1305_H
#include <crypto/poly1305.h>
#include <linux/types.h>
/*
* Poly1305 core functions. These only accept whole blocks; the caller must
* handle any needed block buffering and padding. 'hibit' must be 1 for any
* full blocks, or 0 for the final block if it had to be padded. If 'nonce' is
* non-NULL, then it's added at the end to compute the Poly1305 MAC. Otherwise,
* only the ε-almost-∆-universal hash function (not the full MAC) is computed.
*/
void poly1305_core_setkey(struct poly1305_core_key *key,
const u8 raw_key[POLY1305_BLOCK_SIZE]);
static inline void poly1305_core_init(struct poly1305_state *state)
{
*state = (struct poly1305_state){};
}
void poly1305_core_blocks(struct poly1305_state *state,
const struct poly1305_core_key *key, const void *src,
unsigned int nblocks, u32 hibit);
void poly1305_core_emit(const struct poly1305_state *state, const u32 nonce[4],
void *dst);
static inline void
poly1305_block_init_generic(struct poly1305_block_state *desc,
const u8 raw_key[POLY1305_BLOCK_SIZE])
{
poly1305_core_init(&desc->h);
poly1305_core_setkey(&desc->core_r, raw_key);
}
static inline void poly1305_blocks_generic(struct poly1305_block_state *state,
const u8 *src, unsigned int len,
u32 padbit)
{
poly1305_core_blocks(&state->h, &state->core_r, src,
len / POLY1305_BLOCK_SIZE, padbit);
}
static inline void poly1305_emit_generic(const struct poly1305_state *state,
u8 digest[POLY1305_DIGEST_SIZE],
const u32 nonce[4])
{
poly1305_core_emit(state, nonce, digest);
}
#endif
+40
View File
@@ -0,0 +1,40 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* RNG: Random Number Generator algorithms under the crypto API
*
* Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
* Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
*/
#ifndef _CRYPTO_INTERNAL_RNG_H
#define _CRYPTO_INTERNAL_RNG_H
#include <crypto/algapi.h>
#include <crypto/rng.h>
int crypto_register_rng(struct rng_alg *alg);
void crypto_unregister_rng(struct rng_alg *alg);
int crypto_register_rngs(struct rng_alg *algs, int count);
void crypto_unregister_rngs(struct rng_alg *algs, int count);
#if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
int crypto_del_default_rng(void);
#else
static inline int crypto_del_default_rng(void)
{
return 0;
}
#endif
static inline void *crypto_rng_ctx(struct crypto_rng *tfm)
{
return crypto_tfm_ctx(&tfm->base);
}
static inline void crypto_rng_set_entropy(struct crypto_rng *tfm,
const u8 *data, unsigned int len)
{
crypto_rng_alg(tfm)->set_ent(tfm, data, len);
}
#endif
+86
View File
@@ -0,0 +1,86 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* RSA internal helpers
*
* Copyright (c) 2015, Intel Corporation
* Authors: Tadeusz Struk <tadeusz.struk@intel.com>
*/
#ifndef _RSA_HELPER_
#define _RSA_HELPER_
#include <linux/types.h>
#include <crypto/akcipher.h>
/**
* rsa_key - RSA key structure
* @n : RSA modulus raw byte stream
* @e : RSA public exponent raw byte stream
* @d : RSA private exponent raw byte stream
* @p : RSA prime factor p of n raw byte stream
* @q : RSA prime factor q of n raw byte stream
* @dp : RSA exponent d mod (p - 1) raw byte stream
* @dq : RSA exponent d mod (q - 1) raw byte stream
* @qinv : RSA CRT coefficient q^(-1) mod p raw byte stream
* @n_sz : length in bytes of RSA modulus n
* @e_sz : length in bytes of RSA public exponent
* @d_sz : length in bytes of RSA private exponent
* @p_sz : length in bytes of p field
* @q_sz : length in bytes of q field
* @dp_sz : length in bytes of dp field
* @dq_sz : length in bytes of dq field
* @qinv_sz : length in bytes of qinv field
*/
struct rsa_key {
const u8 *n;
const u8 *e;
const u8 *d;
const u8 *p;
const u8 *q;
const u8 *dp;
const u8 *dq;
const u8 *qinv;
size_t n_sz;
size_t e_sz;
size_t d_sz;
size_t p_sz;
size_t q_sz;
size_t dp_sz;
size_t dq_sz;
size_t qinv_sz;
};
int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
unsigned int key_len);
int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
unsigned int key_len);
#define RSA_PUB (true)
#define RSA_PRIV (false)
static inline int rsa_set_key(struct crypto_akcipher *child,
unsigned int *key_size, bool is_pubkey,
const void *key, unsigned int keylen)
{
int err;
*key_size = 0;
if (is_pubkey)
err = crypto_akcipher_set_pub_key(child, key, keylen);
else
err = crypto_akcipher_set_priv_key(child, key, keylen);
if (err)
return err;
/* Find out new modulus size from rsa implementation */
err = crypto_akcipher_maxsize(child);
if (err > PAGE_SIZE)
return -ENOTSUPP;
*key_size = err;
return 0;
}
extern struct crypto_template rsa_pkcs1pad_tmpl;
extern struct crypto_template rsassa_pkcs1_tmpl;
#endif
@@ -0,0 +1,109 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Synchronous Compression operations
*
* Copyright 2015 LG Electronics Inc.
* Copyright (c) 2016, Intel Corporation
* Author: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
*/
#ifndef _CRYPTO_SCOMP_INT_H
#define _CRYPTO_SCOMP_INT_H
#include <crypto/internal/acompress.h>
struct crypto_scomp {
struct crypto_tfm base;
};
/**
* struct scomp_alg - synchronous compression algorithm
*
* @compress: Function performs a compress operation
* @decompress: Function performs a de-compress operation
* @streams: Per-cpu memory for algorithm
* @calg: Cmonn algorithm data structure shared with acomp
* @COMP_ALG_COMMON: see struct comp_alg_common
*/
struct scomp_alg {
int (*compress)(struct crypto_scomp *tfm, const u8 *src,
unsigned int slen, u8 *dst, unsigned int *dlen,
void *ctx);
int (*decompress)(struct crypto_scomp *tfm, const u8 *src,
unsigned int slen, u8 *dst, unsigned int *dlen,
void *ctx);
struct crypto_acomp_streams streams;
union {
struct COMP_ALG_COMMON;
struct comp_alg_common calg;
};
};
static inline struct scomp_alg *__crypto_scomp_alg(struct crypto_alg *alg)
{
return container_of(alg, struct scomp_alg, base);
}
static inline struct crypto_scomp *__crypto_scomp_tfm(struct crypto_tfm *tfm)
{
return container_of(tfm, struct crypto_scomp, base);
}
static inline struct crypto_tfm *crypto_scomp_tfm(struct crypto_scomp *tfm)
{
return &tfm->base;
}
static inline void crypto_free_scomp(struct crypto_scomp *tfm)
{
crypto_destroy_tfm(tfm, crypto_scomp_tfm(tfm));
}
static inline struct scomp_alg *crypto_scomp_alg(struct crypto_scomp *tfm)
{
return __crypto_scomp_alg(crypto_scomp_tfm(tfm)->__crt_alg);
}
static inline int crypto_scomp_compress(struct crypto_scomp *tfm,
const u8 *src, unsigned int slen,
u8 *dst, unsigned int *dlen, void *ctx)
{
return crypto_scomp_alg(tfm)->compress(tfm, src, slen, dst, dlen, ctx);
}
static inline int crypto_scomp_decompress(struct crypto_scomp *tfm,
const u8 *src, unsigned int slen,
u8 *dst, unsigned int *dlen,
void *ctx)
{
return crypto_scomp_alg(tfm)->decompress(tfm, src, slen, dst, dlen,
ctx);
}
/**
* crypto_register_scomp() -- Register synchronous compression algorithm
*
* Function registers an implementation of a synchronous
* compression algorithm
*
* @alg: algorithm definition
*
* Return: zero on success; error code in case of error
*/
int crypto_register_scomp(struct scomp_alg *alg);
/**
* crypto_unregister_scomp() -- Unregister synchronous compression algorithm
*
* Function unregisters an implementation of a synchronous
* compression algorithm
*
* @alg: algorithm definition
*/
void crypto_unregister_scomp(struct scomp_alg *alg);
int crypto_register_scomps(struct scomp_alg *algs, int count);
void crypto_unregister_scomps(struct scomp_alg *algs, int count);
#endif
+97
View File
@@ -0,0 +1,97 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Public Key Signature Algorithm
*
* Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au>
*/
#ifndef _CRYPTO_INTERNAL_SIG_H
#define _CRYPTO_INTERNAL_SIG_H
#include <crypto/algapi.h>
#include <crypto/sig.h>
struct sig_instance {
void (*free)(struct sig_instance *inst);
union {
struct {
char head[offsetof(struct sig_alg, base)];
struct crypto_instance base;
};
struct sig_alg alg;
};
};
struct crypto_sig_spawn {
struct crypto_spawn base;
};
static inline void *crypto_sig_ctx(struct crypto_sig *tfm)
{
return crypto_tfm_ctx(&tfm->base);
}
/**
* crypto_register_sig() -- Register public key signature algorithm
*
* Function registers an implementation of a public key signature algorithm
*
* @alg: algorithm definition
*
* Return: zero on success; error code in case of error
*/
int crypto_register_sig(struct sig_alg *alg);
/**
* crypto_unregister_sig() -- Unregister public key signature algorithm
*
* Function unregisters an implementation of a public key signature algorithm
*
* @alg: algorithm definition
*/
void crypto_unregister_sig(struct sig_alg *alg);
int sig_register_instance(struct crypto_template *tmpl,
struct sig_instance *inst);
static inline struct sig_instance *sig_instance(struct crypto_instance *inst)
{
return container_of(&inst->alg, struct sig_instance, alg.base);
}
static inline struct sig_instance *sig_alg_instance(struct crypto_sig *tfm)
{
return sig_instance(crypto_tfm_alg_instance(&tfm->base));
}
static inline struct crypto_instance *sig_crypto_instance(struct sig_instance
*inst)
{
return container_of(&inst->alg.base, struct crypto_instance, alg);
}
static inline void *sig_instance_ctx(struct sig_instance *inst)
{
return crypto_instance_ctx(sig_crypto_instance(inst));
}
int crypto_grab_sig(struct crypto_sig_spawn *spawn,
struct crypto_instance *inst,
const char *name, u32 type, u32 mask);
static inline struct crypto_sig *crypto_spawn_sig(struct crypto_sig_spawn
*spawn)
{
return crypto_spawn_tfm2(&spawn->base);
}
static inline void crypto_drop_sig(struct crypto_sig_spawn *spawn)
{
crypto_drop_spawn(&spawn->base);
}
static inline struct sig_alg *crypto_spawn_sig_alg(struct crypto_sig_spawn
*spawn)
{
return container_of(spawn->base.alg, struct sig_alg, base);
}
#endif
@@ -0,0 +1,40 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Shared crypto simd helpers
*/
#ifndef _CRYPTO_INTERNAL_SIMD_H
#define _CRYPTO_INTERNAL_SIMD_H
#include <asm/simd.h>
#include <linux/percpu.h>
#include <linux/types.h>
struct simd_aead_alg;
struct aead_alg;
int simd_register_aeads_compat(struct aead_alg *algs, int count,
struct simd_aead_alg **simd_algs);
void simd_unregister_aeads(struct aead_alg *algs, int count,
struct simd_aead_alg **simd_algs);
/*
* crypto_simd_usable() - is it allowed at this time to use SIMD instructions or
* access the SIMD register file?
*
* This delegates to may_use_simd(), except that this also returns false if SIMD
* in crypto code has been temporarily disabled on this CPU by the crypto
* self-tests, in order to test the no-SIMD fallback code. This override is
* currently limited to configurations where the "full" self-tests are enabled,
* because it might be a bit too invasive to be part of the "fast" self-tests.
*/
#ifdef CONFIG_CRYPTO_SELFTESTS_FULL
DECLARE_PER_CPU(bool, crypto_simd_disabled_for_test);
#define crypto_simd_usable() \
(may_use_simd() && !this_cpu_read(crypto_simd_disabled_for_test))
#else
#define crypto_simd_usable() may_use_simd()
#endif
#endif /* _CRYPTO_INTERNAL_SIMD_H */
@@ -0,0 +1,315 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Symmetric key ciphers.
*
* Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
*/
#ifndef _CRYPTO_INTERNAL_SKCIPHER_H
#define _CRYPTO_INTERNAL_SKCIPHER_H
#include <crypto/algapi.h>
#include <crypto/internal/cipher.h>
#include <crypto/skcipher.h>
#include <linux/types.h>
/*
* Set this if your algorithm is sync but needs a reqsize larger
* than MAX_SYNC_SKCIPHER_REQSIZE.
*
* Reuse bit that is specific to hash algorithms.
*/
#define CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE CRYPTO_ALG_OPTIONAL_KEY
struct aead_request;
struct rtattr;
struct skcipher_instance {
void (*free)(struct skcipher_instance *inst);
union {
struct {
char head[offsetof(struct skcipher_alg, base)];
struct crypto_instance base;
} s;
struct skcipher_alg alg;
};
};
struct lskcipher_instance {
void (*free)(struct lskcipher_instance *inst);
union {
struct {
char head[offsetof(struct lskcipher_alg, co.base)];
struct crypto_instance base;
} s;
struct lskcipher_alg alg;
};
};
struct crypto_skcipher_spawn {
struct crypto_spawn base;
};
struct crypto_lskcipher_spawn {
struct crypto_spawn base;
};
struct skcipher_walk {
union {
/* Virtual address of the source. */
struct {
struct {
const void *const addr;
} virt;
} src;
/* Private field for the API, do not use. */
struct scatter_walk in;
};
union {
/* Virtual address of the destination. */
struct {
struct {
void *const addr;
} virt;
} dst;
/* Private field for the API, do not use. */
struct scatter_walk out;
};
unsigned int nbytes;
unsigned int total;
u8 *page;
u8 *buffer;
u8 *oiv;
void *iv;
unsigned int ivsize;
int flags;
unsigned int blocksize;
unsigned int stride;
unsigned int alignmask;
};
static inline struct crypto_instance *skcipher_crypto_instance(
struct skcipher_instance *inst)
{
return &inst->s.base;
}
static inline struct crypto_instance *lskcipher_crypto_instance(
struct lskcipher_instance *inst)
{
return &inst->s.base;
}
static inline struct skcipher_instance *skcipher_alg_instance(
struct crypto_skcipher *skcipher)
{
return container_of(crypto_skcipher_alg(skcipher),
struct skcipher_instance, alg);
}
static inline struct lskcipher_instance *lskcipher_alg_instance(
struct crypto_lskcipher *lskcipher)
{
return container_of(crypto_lskcipher_alg(lskcipher),
struct lskcipher_instance, alg);
}
static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
{
return crypto_instance_ctx(skcipher_crypto_instance(inst));
}
static inline void *lskcipher_instance_ctx(struct lskcipher_instance *inst)
{
return crypto_instance_ctx(lskcipher_crypto_instance(inst));
}
static inline void skcipher_request_complete(struct skcipher_request *req, int err)
{
crypto_request_complete(&req->base, err);
}
int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
struct crypto_instance *inst,
const char *name, u32 type, u32 mask);
int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn,
struct crypto_instance *inst,
const char *name, u32 type, u32 mask);
static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn)
{
crypto_drop_spawn(&spawn->base);
}
static inline void crypto_drop_lskcipher(struct crypto_lskcipher_spawn *spawn)
{
crypto_drop_spawn(&spawn->base);
}
static inline struct lskcipher_alg *crypto_lskcipher_spawn_alg(
struct crypto_lskcipher_spawn *spawn)
{
return container_of(spawn->base.alg, struct lskcipher_alg, co.base);
}
static inline struct skcipher_alg_common *crypto_spawn_skcipher_alg_common(
struct crypto_skcipher_spawn *spawn)
{
return container_of(spawn->base.alg, struct skcipher_alg_common, base);
}
static inline struct lskcipher_alg *crypto_spawn_lskcipher_alg(
struct crypto_lskcipher_spawn *spawn)
{
return crypto_lskcipher_spawn_alg(spawn);
}
static inline struct crypto_skcipher *crypto_spawn_skcipher(
struct crypto_skcipher_spawn *spawn)
{
return crypto_spawn_tfm2(&spawn->base);
}
static inline struct crypto_lskcipher *crypto_spawn_lskcipher(
struct crypto_lskcipher_spawn *spawn)
{
return crypto_spawn_tfm2(&spawn->base);
}
static inline void crypto_skcipher_set_reqsize(
struct crypto_skcipher *skcipher, unsigned int reqsize)
{
skcipher->reqsize = reqsize;
}
static inline void crypto_skcipher_set_reqsize_dma(
struct crypto_skcipher *skcipher, unsigned int reqsize)
{
reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
skcipher->reqsize = reqsize;
}
int crypto_register_skcipher(struct skcipher_alg *alg);
void crypto_unregister_skcipher(struct skcipher_alg *alg);
int crypto_register_skciphers(struct skcipher_alg *algs, int count);
void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
int skcipher_register_instance(struct crypto_template *tmpl,
struct skcipher_instance *inst);
int crypto_register_lskcipher(struct lskcipher_alg *alg);
void crypto_unregister_lskcipher(struct lskcipher_alg *alg);
int crypto_register_lskciphers(struct lskcipher_alg *algs, int count);
void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count);
int lskcipher_register_instance(struct crypto_template *tmpl,
struct lskcipher_instance *inst);
int skcipher_walk_done(struct skcipher_walk *walk, int res);
int skcipher_walk_virt(struct skcipher_walk *__restrict walk,
struct skcipher_request *__restrict req,
bool atomic);
int skcipher_walk_aead_encrypt(struct skcipher_walk *__restrict walk,
struct aead_request *__restrict req,
bool atomic);
int skcipher_walk_aead_decrypt(struct skcipher_walk *__restrict walk,
struct aead_request *__restrict req,
bool atomic);
static inline void skcipher_walk_abort(struct skcipher_walk *walk)
{
skcipher_walk_done(walk, -ECANCELED);
}
static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm)
{
return crypto_tfm_ctx(&tfm->base);
}
static inline void *crypto_lskcipher_ctx(struct crypto_lskcipher *tfm)
{
return crypto_tfm_ctx(&tfm->base);
}
static inline void *crypto_skcipher_ctx_dma(struct crypto_skcipher *tfm)
{
return crypto_tfm_ctx_dma(&tfm->base);
}
static inline bool crypto_skcipher_tested(struct crypto_skcipher *tfm)
{
struct crypto_tfm *tfm_base = crypto_skcipher_tfm(tfm);
return tfm_base->__crt_alg->cra_flags & CRYPTO_ALG_TESTED;
}
static inline void *skcipher_request_ctx(struct skcipher_request *req)
{
return req->__ctx;
}
static inline void *skcipher_request_ctx_dma(struct skcipher_request *req)
{
unsigned int align = crypto_dma_align();
if (align <= crypto_tfm_ctx_alignment())
align = 1;
return PTR_ALIGN(skcipher_request_ctx(req), align);
}
static inline u32 skcipher_request_flags(struct skcipher_request *req)
{
return req->base.flags;
}
/* Helpers for simple block cipher modes of operation */
struct skcipher_ctx_simple {
struct crypto_cipher *cipher; /* underlying block cipher */
};
static inline struct crypto_cipher *
skcipher_cipher_simple(struct crypto_skcipher *tfm)
{
struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
return ctx->cipher;
}
struct skcipher_instance *skcipher_alloc_instance_simple(
struct crypto_template *tmpl, struct rtattr **tb);
static inline struct crypto_alg *skcipher_ialg_simple(
struct skcipher_instance *inst)
{
struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
return crypto_spawn_cipher_alg(spawn);
}
static inline struct crypto_lskcipher *lskcipher_cipher_simple(
struct crypto_lskcipher *tfm)
{
struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
return *ctx;
}
struct lskcipher_instance *lskcipher_alloc_instance_simple(
struct crypto_template *tmpl, struct rtattr **tb);
static inline struct lskcipher_alg *lskcipher_ialg_simple(
struct lskcipher_instance *inst)
{
struct crypto_lskcipher_spawn *spawn = lskcipher_instance_ctx(inst);
return crypto_lskcipher_spawn_alg(spawn);
}
#endif /* _CRYPTO_INTERNAL_SKCIPHER_H */