Skip to content

Commit c08fed7

Browse files
dhowellsJames Morris
authored andcommitted
KEYS: Implement encrypt, decrypt and sign for software asymmetric key [ver #2]
Implement the encrypt, decrypt and sign operations for the software asymmetric key subtype. This mostly involves offloading the call to the crypto layer. Note that the decrypt and sign operations require a private key to be supplied. Encrypt (and also verify) will work with either a public or a private key. A public key can be supplied with an X.509 certificate and a private key can be supplied using a PKCS#8 blob: # j=`openssl pkcs8 -in ~/pkcs7/firmwarekey2.priv -topk8 -nocrypt -outform DER | keyctl padd asymmetric foo @s` # keyctl pkey_query $j - enc=pkcs1 key_size=4096 max_data_size=512 max_sig_size=512 max_enc_size=512 max_dec_size=512 encrypt=y decrypt=y sign=y verify=y # keyctl pkey_encrypt $j 0 data enc=pkcs1 >/tmp/enc # keyctl pkey_decrypt $j 0 /tmp/enc enc=pkcs1 >/tmp/dec # cmp data /tmp/dec # keyctl pkey_sign $j 0 data enc=pkcs1 hash=sha1 >/tmp/sig # keyctl pkey_verify $j 0 data /tmp/sig enc=pkcs1 hash=sha1 # Signed-off-by: David Howells <[email protected]> Tested-by: Marcel Holtmann <[email protected]> Reviewed-by: Marcel Holtmann <[email protected]> Reviewed-by: Denis Kenzior <[email protected]> Tested-by: Denis Kenzior <[email protected]> Signed-off-by: James Morris <[email protected]>
1 parent f7c4e06 commit c08fed7

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

crypto/asymmetric_keys/public_key.c

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ static int software_key_query(const struct kernel_pkey_params *params,
130130
info->max_sig_size = len;
131131
info->max_enc_size = len;
132132
info->max_dec_size = len;
133-
info->supported_ops = KEYCTL_SUPPORTS_VERIFY;
133+
info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
134+
KEYCTL_SUPPORTS_VERIFY);
135+
if (pkey->key_is_private)
136+
info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
137+
KEYCTL_SUPPORTS_SIGN);
134138
ret = 0;
135139

136140
error_free_tfm:
@@ -139,6 +143,81 @@ static int software_key_query(const struct kernel_pkey_params *params,
139143
return ret;
140144
}
141145

146+
/*
147+
* Do encryption, decryption and signing ops.
148+
*/
149+
static int software_key_eds_op(struct kernel_pkey_params *params,
150+
const void *in, void *out)
151+
{
152+
const struct public_key *pkey = params->key->payload.data[asym_crypto];
153+
struct akcipher_request *req;
154+
struct crypto_akcipher *tfm;
155+
struct crypto_wait cwait;
156+
struct scatterlist in_sg, out_sg;
157+
char alg_name[CRYPTO_MAX_ALG_NAME];
158+
int ret;
159+
160+
pr_devel("==>%s()\n", __func__);
161+
162+
ret = software_key_determine_akcipher(params->encoding,
163+
params->hash_algo,
164+
pkey, alg_name);
165+
if (ret < 0)
166+
return ret;
167+
168+
tfm = crypto_alloc_akcipher(alg_name, 0, 0);
169+
if (IS_ERR(tfm))
170+
return PTR_ERR(tfm);
171+
172+
req = akcipher_request_alloc(tfm, GFP_KERNEL);
173+
if (!req)
174+
goto error_free_tfm;
175+
176+
if (pkey->key_is_private)
177+
ret = crypto_akcipher_set_priv_key(tfm,
178+
pkey->key, pkey->keylen);
179+
else
180+
ret = crypto_akcipher_set_pub_key(tfm,
181+
pkey->key, pkey->keylen);
182+
if (ret)
183+
goto error_free_req;
184+
185+
sg_init_one(&in_sg, in, params->in_len);
186+
sg_init_one(&out_sg, out, params->out_len);
187+
akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
188+
params->out_len);
189+
crypto_init_wait(&cwait);
190+
akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
191+
CRYPTO_TFM_REQ_MAY_SLEEP,
192+
crypto_req_done, &cwait);
193+
194+
/* Perform the encryption calculation. */
195+
switch (params->op) {
196+
case kernel_pkey_encrypt:
197+
ret = crypto_akcipher_encrypt(req);
198+
break;
199+
case kernel_pkey_decrypt:
200+
ret = crypto_akcipher_decrypt(req);
201+
break;
202+
case kernel_pkey_sign:
203+
ret = crypto_akcipher_sign(req);
204+
break;
205+
default:
206+
BUG();
207+
}
208+
209+
ret = crypto_wait_req(ret, &cwait);
210+
if (ret == 0)
211+
ret = req->dst_len;
212+
213+
error_free_req:
214+
akcipher_request_free(req);
215+
error_free_tfm:
216+
crypto_free_akcipher(tfm);
217+
pr_devel("<==%s() = %d\n", __func__, ret);
218+
return ret;
219+
}
220+
142221
/*
143222
* Verify a signature using a public key.
144223
*/
@@ -242,6 +321,7 @@ struct asymmetric_key_subtype public_key_subtype = {
242321
.describe = public_key_describe,
243322
.destroy = public_key_destroy,
244323
.query = software_key_query,
324+
.eds_op = software_key_eds_op,
245325
.verify_signature = public_key_verify_signature_2,
246326
};
247327
EXPORT_SYMBOL_GPL(public_key_subtype);

0 commit comments

Comments
 (0)