^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2011 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2011 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * <dmitry.kasatkin@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * File: sign.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * implements signature (RSA) verification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * pkcs decoding is based on LibTomCrypt code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/key.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <keys/user-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/mpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/digsig.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static struct crypto_shash *shash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static const char *pkcs_1_v1_5_decode_emsa(const unsigned char *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned long msglen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned long modulus_bitlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned long *outlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned long modulus_len, ps_len, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* test message size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if ((msglen > modulus_len) || (modulus_len < 11))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* separate encoded message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (msg[0] != 0x00 || msg[1] != 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) for (i = 2; i < modulus_len - 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (msg[i] != 0xFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* separator check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (msg[i] != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* There was no octet with hexadecimal value 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) to separate ps from m. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ps_len = i - 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *outlen = (msglen - (2 + ps_len + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return msg + 2 + ps_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * RSA Signature verification with public key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int digsig_verify_rsa(struct key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) const char *sig, int siglen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) const char *h, int hlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned long len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned long mlen, mblen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned nret, l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int head, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) unsigned char *out1 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) const char *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) MPI in = NULL, res = NULL, pkey[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) uint8_t *p, *datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) const uint8_t *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) const struct user_key_payload *ukp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct pubkey_hdr *pkh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) down_read(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ukp = user_key_payload_locked(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!ukp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* key was revoked before we acquired its semaphore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) err = -EKEYREVOKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (ukp->datalen < sizeof(*pkh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pkh = (struct pubkey_hdr *)ukp->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (pkh->version != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (pkh->algo != PUBKEY_ALGO_RSA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (pkh->nmpi != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) datap = pkh->mpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) endp = ukp->data + ukp->datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) for (i = 0; i < pkh->nmpi; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned int remaining = endp - datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pkey[i] = mpi_read_from_buffer(datap, &remaining);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (IS_ERR(pkey[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) err = PTR_ERR(pkey[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) datap += remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) mblen = mpi_get_nbits(pkey[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) mlen = DIV_ROUND_UP(mblen, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (mlen == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) out1 = kzalloc(mlen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (!out1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) nret = siglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) in = mpi_read_from_buffer(sig, &nret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (IS_ERR(in)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) err = PTR_ERR(in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) res = mpi_alloc(mpi_get_nlimbs(in) * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) err = mpi_powm(res, in, pkey[1], pkey[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (mpi_get_nlimbs(res) * BYTES_PER_MPI_LIMB > mlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) p = mpi_get_buffer(res, &l, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) len = mlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) head = len - l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) memset(out1, 0, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) memcpy(out1 + head, p, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) m = pkcs_1_v1_5_decode_emsa(out1, len, mblen, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!m || len != hlen || memcmp(m, h, hlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) mpi_free(in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) mpi_free(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) kfree(out1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) while (--i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) mpi_free(pkey[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) up_read(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * digsig_verify() - digital signature verification with public key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * @keyring: keyring to search key in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * @sig: digital signature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * @siglen: length of the signature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * @data: data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * @datalen: length of the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * Returns 0 on success, -EINVAL otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * Verifies data integrity against digital signature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * Currently only RSA is supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * Normally hash of the content is used as a data for this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int digsig_verify(struct key *keyring, const char *sig, int siglen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) const char *data, int datalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct signature_hdr *sh = (struct signature_hdr *)sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct shash_desc *desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) unsigned char hash[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) char name[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (siglen < sizeof(*sh) + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (sh->algo != PUBKEY_ALGO_RSA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return -ENOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) sprintf(name, "%llX", __be64_to_cpup((uint64_t *)sh->keyid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /* search in specific keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) key_ref_t kref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) kref = keyring_search(make_key_ref(keyring, 1UL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) &key_type_user, name, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (IS_ERR(kref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) key = ERR_CAST(kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) key = key_ref_to_ptr(kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) key = request_key(&key_type_user, name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (IS_ERR(key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) pr_err("key not found, id: %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return PTR_ERR(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(shash),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) desc->tfm = shash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) crypto_shash_init(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) crypto_shash_update(desc, data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) crypto_shash_update(desc, sig, sizeof(*sh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) crypto_shash_final(desc, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) kfree(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /* pass signature mpis address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) err = digsig_verify_rsa(key, sig + sizeof(*sh), siglen - sizeof(*sh),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) hash, sizeof(hash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) key_put(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return err ? -EINVAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) EXPORT_SYMBOL_GPL(digsig_verify);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int __init digsig_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) shash = crypto_alloc_shash("sha1", 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (IS_ERR(shash)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) pr_err("shash allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return PTR_ERR(shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static void __exit digsig_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) crypto_free_shash(shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) module_init(digsig_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) module_exit(digsig_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) MODULE_LICENSE("GPL");