crypto/rsa-pkcs1pad.c
Source file repositories/reference/linux-study-clean/crypto/rsa-pkcs1pad.c
File Facts
- System
- Linux kernel
- Corpus path
crypto/rsa-pkcs1pad.c- Extension
.c- Size
- 9127 bytes
- Lines
- 380
- Domain
- Kernel Services
- Bucket
- crypto
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
crypto/algapi.hcrypto/akcipher.hcrypto/internal/akcipher.hcrypto/internal/rsa.hlinux/err.hlinux/init.hlinux/kernel.hlinux/module.hlinux/random.hlinux/scatterlist.h
Detected Declarations
struct pkcs1pad_ctxstruct pkcs1pad_inst_ctxstruct pkcs1pad_requestfunction pkcs1pad_set_pub_keyfunction pkcs1pad_set_priv_keyfunction pkcs1pad_get_max_sizefunction pkcs1pad_sg_set_buffunction pkcs1pad_encrypt_completefunction pkcs1pad_encrypt_complete_cbfunction pkcs1pad_encryptfunction pkcs1pad_decrypt_completefunction pkcs1pad_decrypt_complete_cbfunction pkcs1pad_decryptfunction pkcs1pad_init_tfmfunction pkcs1pad_exit_tfmfunction pkcs1pad_freefunction pkcs1pad_create
Annotated Snippet
struct pkcs1pad_ctx {
struct crypto_akcipher *child;
unsigned int key_size;
};
struct pkcs1pad_inst_ctx {
struct crypto_akcipher_spawn spawn;
};
struct pkcs1pad_request {
struct scatterlist in_sg[2], out_sg[1];
uint8_t *in_buf, *out_buf;
struct akcipher_request child_req;
};
static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,
unsigned int keylen)
{
struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
return rsa_set_key(ctx->child, &ctx->key_size, RSA_PUB, key, keylen);
}
static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
unsigned int keylen)
{
struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
return rsa_set_key(ctx->child, &ctx->key_size, RSA_PRIV, key, keylen);
}
static unsigned int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
{
struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
/*
* The maximum destination buffer size for the encrypt operation
* will be the same as for RSA, even though it's smaller for
* decrypt.
*/
return ctx->key_size;
}
static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
struct scatterlist *next)
{
int nsegs = next ? 2 : 1;
sg_init_table(sg, nsegs);
sg_set_buf(sg, buf, len);
if (next)
sg_chain(sg, nsegs, next);
}
static int pkcs1pad_encrypt_complete(struct akcipher_request *req, int err)
{
struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
unsigned int pad_len;
unsigned int len;
u8 *out_buf;
if (err)
goto out;
len = req_ctx->child_req.dst_len;
pad_len = ctx->key_size - len;
/* Four billion to one */
if (likely(!pad_len))
goto out;
out_buf = kzalloc(ctx->key_size, GFP_ATOMIC);
err = -ENOMEM;
if (!out_buf)
goto out;
sg_copy_to_buffer(req->dst, sg_nents_for_len(req->dst, len),
out_buf + pad_len, len);
sg_copy_from_buffer(req->dst,
sg_nents_for_len(req->dst, ctx->key_size),
out_buf, ctx->key_size);
kfree_sensitive(out_buf);
out:
req->dst_len = ctx->key_size;
Annotation
- Immediate include surface: `crypto/algapi.h`, `crypto/akcipher.h`, `crypto/internal/akcipher.h`, `crypto/internal/rsa.h`, `linux/err.h`, `linux/init.h`, `linux/kernel.h`, `linux/module.h`.
- Detected declarations: `struct pkcs1pad_ctx`, `struct pkcs1pad_inst_ctx`, `struct pkcs1pad_request`, `function pkcs1pad_set_pub_key`, `function pkcs1pad_set_priv_key`, `function pkcs1pad_get_max_size`, `function pkcs1pad_sg_set_buf`, `function pkcs1pad_encrypt_complete`, `function pkcs1pad_encrypt_complete_cb`, `function pkcs1pad_encrypt`.
- Atlas domain: Kernel Services / crypto.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.