18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/slab.h>
26 #include <crypto/sha.h>
33 static int pkcs_1_v1_5_decode_emsa(
const unsigned char *
msg,
35 unsigned long modulus_bitlen,
39 unsigned long modulus_len, ps_len,
i;
41 modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
44 if ((msglen > modulus_len) || (modulus_len < 11))
48 if ((msg[0] != 0x00) || (msg[1] != (
unsigned char)1))
51 for (i = 2; i < modulus_len - 1; i++)
63 if (*outlen < (msglen - (2 + ps_len + 1))) {
64 *outlen = msglen - (2 + ps_len + 1);
68 *outlen = (msglen - (2 + ps_len + 1));
69 memcpy(out, &msg[2 + ps_len + 1], *outlen);
77 static int digsig_verify_rsa(
struct key *
key,
78 const char *
sig,
int siglen,
79 const char *
h,
int hlen)
83 unsigned long mlen, mblen;
86 unsigned char *out1 =
NULL, *out2 =
NULL;
93 ukp = key->payload.
data;
95 if (ukp->
datalen <
sizeof(*pkh))
114 for (i = 0; i < pkh->
nmpi; i++) {
115 unsigned int remaining = endp - datap;
123 mlen = (mblen + 7)/8;
145 err =
mpi_powm(res, in, pkey[1], pkey[0]);
163 memcpy(out1 + head, p, l);
165 err = pkcs_1_v1_5_decode_emsa(out1, len, mblen, out2, &len);
169 if (len != hlen ||
memcmp(out2, h, hlen))
209 if (siglen <
sizeof(*sh) + 2)
223 key = ERR_PTR(PTR_ERR(kref));
225 key = key_ref_to_ptr(kref);
230 pr_err(
"key not found, id: %s\n", name);
234 desc = kzalloc(
sizeof(*desc) + crypto_shash_descsize(shash),
242 crypto_shash_init(desc);
250 err = digsig_verify_rsa(key, sig +
sizeof(*sh), siglen -
sizeof(*sh),
260 static int __init digsig_init(
void)
264 pr_err(
"shash allocation failed\n");
265 return PTR_ERR(shash);
272 static void __exit digsig_cleanup(
void)
274 crypto_free_shash(shash);