1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Public Key Signature Algorithm
5 * Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au>
10 #include <linux/crypto.h>
13 * struct crypto_sig - user-instantiated objects which encapsulate
14 * algorithms and core processing logic
16 * @base: Common crypto API algorithm data structure
19 struct crypto_tfm base
;
23 * struct sig_alg - generic public key signature algorithm
25 * @sign: Function performs a sign operation as defined by public key
26 * algorithm. Optional.
27 * @verify: Function performs a complete verify operation as defined by
28 * public key algorithm, returning verification status. Optional.
29 * @set_pub_key: Function invokes the algorithm specific set public key
30 * function, which knows how to decode and interpret
31 * the BER encoded public key and parameters. Mandatory.
32 * @set_priv_key: Function invokes the algorithm specific set private key
33 * function, which knows how to decode and interpret
34 * the BER encoded private key and parameters. Optional.
35 * @key_size: Function returns key size. Mandatory.
36 * @digest_size: Function returns maximum digest size. Optional.
37 * @max_size: Function returns maximum signature size. Optional.
38 * @init: Initialize the cryptographic transformation object.
39 * This function is used to initialize the cryptographic
40 * transformation object. This function is called only once at
41 * the instantiation time, right after the transformation context
42 * was allocated. In case the cryptographic hardware has some
43 * special requirements which need to be handled by software, this
44 * function shall check for the precise requirement of the
45 * transformation and put any software fallbacks in place.
46 * @exit: Deinitialize the cryptographic transformation object. This is a
47 * counterpart to @init, used to remove various changes set in
50 * @base: Common crypto API algorithm data structure
53 int (*sign
)(struct crypto_sig
*tfm
,
54 const void *src
, unsigned int slen
,
55 void *dst
, unsigned int dlen
);
56 int (*verify
)(struct crypto_sig
*tfm
,
57 const void *src
, unsigned int slen
,
58 const void *digest
, unsigned int dlen
);
59 int (*set_pub_key
)(struct crypto_sig
*tfm
,
60 const void *key
, unsigned int keylen
);
61 int (*set_priv_key
)(struct crypto_sig
*tfm
,
62 const void *key
, unsigned int keylen
);
63 unsigned int (*key_size
)(struct crypto_sig
*tfm
);
64 unsigned int (*digest_size
)(struct crypto_sig
*tfm
);
65 unsigned int (*max_size
)(struct crypto_sig
*tfm
);
66 int (*init
)(struct crypto_sig
*tfm
);
67 void (*exit
)(struct crypto_sig
*tfm
);
69 struct crypto_alg base
;
73 * DOC: Generic Public Key Signature API
75 * The Public Key Signature API is used with the algorithms of type
76 * CRYPTO_ALG_TYPE_SIG (listed as type "sig" in /proc/crypto)
80 * crypto_alloc_sig() - allocate signature tfm handle
81 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
82 * signing algorithm e.g. "ecdsa"
83 * @type: specifies the type of the algorithm
84 * @mask: specifies the mask for the algorithm
86 * Allocate a handle for public key signature algorithm. The returned struct
87 * crypto_sig is the handle that is required for any subsequent
88 * API invocation for signature operations.
90 * Return: allocated handle in case of success; IS_ERR() is true in case
91 * of an error, PTR_ERR() returns the error code.
93 struct crypto_sig
*crypto_alloc_sig(const char *alg_name
, u32 type
, u32 mask
);
95 static inline struct crypto_tfm
*crypto_sig_tfm(struct crypto_sig
*tfm
)
100 static inline struct crypto_sig
*__crypto_sig_tfm(struct crypto_tfm
*tfm
)
102 return container_of(tfm
, struct crypto_sig
, base
);
105 static inline struct sig_alg
*__crypto_sig_alg(struct crypto_alg
*alg
)
107 return container_of(alg
, struct sig_alg
, base
);
110 static inline struct sig_alg
*crypto_sig_alg(struct crypto_sig
*tfm
)
112 return __crypto_sig_alg(crypto_sig_tfm(tfm
)->__crt_alg
);
116 * crypto_free_sig() - free signature tfm handle
118 * @tfm: signature tfm handle allocated with crypto_alloc_sig()
120 * If @tfm is a NULL or error pointer, this function does nothing.
122 static inline void crypto_free_sig(struct crypto_sig
*tfm
)
124 crypto_destroy_tfm(tfm
, crypto_sig_tfm(tfm
));
128 * crypto_sig_keysize() - Get key size
130 * Function returns the key size in bytes.
131 * Function assumes that the key is already set in the transformation. If this
132 * function is called without a setkey or with a failed setkey, you may end up
133 * in a NULL dereference.
135 * @tfm: signature tfm handle allocated with crypto_alloc_sig()
137 static inline unsigned int crypto_sig_keysize(struct crypto_sig
*tfm
)
139 struct sig_alg
*alg
= crypto_sig_alg(tfm
);
141 return alg
->key_size(tfm
);
145 * crypto_sig_digestsize() - Get maximum digest size
147 * Function returns the maximum digest size in bytes.
148 * Function assumes that the key is already set in the transformation. If this
149 * function is called without a setkey or with a failed setkey, you may end up
150 * in a NULL dereference.
152 * @tfm: signature tfm handle allocated with crypto_alloc_sig()
154 static inline unsigned int crypto_sig_digestsize(struct crypto_sig
*tfm
)
156 struct sig_alg
*alg
= crypto_sig_alg(tfm
);
158 return alg
->digest_size(tfm
);
162 * crypto_sig_maxsize() - Get maximum signature size
164 * Function returns the maximum signature size in bytes.
165 * Function assumes that the key is already set in the transformation. If this
166 * function is called without a setkey or with a failed setkey, you may end up
167 * in a NULL dereference.
169 * @tfm: signature tfm handle allocated with crypto_alloc_sig()
171 static inline unsigned int crypto_sig_maxsize(struct crypto_sig
*tfm
)
173 struct sig_alg
*alg
= crypto_sig_alg(tfm
);
175 return alg
->max_size(tfm
);
179 * crypto_sig_sign() - Invoke signing operation
181 * Function invokes the specific signing operation for a given algorithm
183 * @tfm: signature tfm handle allocated with crypto_alloc_sig()
184 * @src: source buffer
185 * @slen: source length
186 * @dst: destination obuffer
187 * @dlen: destination length
189 * Return: zero on success; error code in case of error
191 static inline int crypto_sig_sign(struct crypto_sig
*tfm
,
192 const void *src
, unsigned int slen
,
193 void *dst
, unsigned int dlen
)
195 struct sig_alg
*alg
= crypto_sig_alg(tfm
);
197 return alg
->sign(tfm
, src
, slen
, dst
, dlen
);
201 * crypto_sig_verify() - Invoke signature verification
203 * Function invokes the specific signature verification operation
204 * for a given algorithm.
206 * @tfm: signature tfm handle allocated with crypto_alloc_sig()
207 * @src: source buffer
208 * @slen: source length
210 * @dlen: digest length
212 * Return: zero on verification success; error code in case of error.
214 static inline int crypto_sig_verify(struct crypto_sig
*tfm
,
215 const void *src
, unsigned int slen
,
216 const void *digest
, unsigned int dlen
)
218 struct sig_alg
*alg
= crypto_sig_alg(tfm
);
220 return alg
->verify(tfm
, src
, slen
, digest
, dlen
);
224 * crypto_sig_set_pubkey() - Invoke set public key operation
226 * Function invokes the algorithm specific set key function, which knows
227 * how to decode and interpret the encoded key and parameters
230 * @key: BER encoded public key, algo OID, paramlen, BER encoded
232 * @keylen: length of the key (not including other data)
234 * Return: zero on success; error code in case of error
236 static inline int crypto_sig_set_pubkey(struct crypto_sig
*tfm
,
237 const void *key
, unsigned int keylen
)
239 struct sig_alg
*alg
= crypto_sig_alg(tfm
);
241 return alg
->set_pub_key(tfm
, key
, keylen
);
245 * crypto_sig_set_privkey() - Invoke set private key operation
247 * Function invokes the algorithm specific set key function, which knows
248 * how to decode and interpret the encoded key and parameters
251 * @key: BER encoded private key, algo OID, paramlen, BER encoded
253 * @keylen: length of the key (not including other data)
255 * Return: zero on success; error code in case of error
257 static inline int crypto_sig_set_privkey(struct crypto_sig
*tfm
,
258 const void *key
, unsigned int keylen
)
260 struct sig_alg
*alg
= crypto_sig_alg(tfm
);
262 return alg
->set_priv_key(tfm
, key
, keylen
);