Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

digsig: remove unnecessary memory allocation and copying

In existing use case, copying of the decoded data is unnecessary in
pkcs_1_v1_5_decode_emsa. It is just enough to get pointer to the message.
Removing copying and extra buffer allocation.

Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
Signed-off-by: James Morris <james.l.morris@oracle.com>

authored by

Dmitry Kasatkin and committed by
James Morris
26d43845 5a73fcfa

+14 -27
+14 -27
lib/digsig.c
··· 30 30 31 31 static struct crypto_shash *shash; 32 32 33 - static int pkcs_1_v1_5_decode_emsa(const unsigned char *msg, 34 - unsigned long msglen, 35 - unsigned long modulus_bitlen, 36 - unsigned char *out, 37 - unsigned long *outlen) 33 + static const char *pkcs_1_v1_5_decode_emsa(const unsigned char *msg, 34 + unsigned long msglen, 35 + unsigned long modulus_bitlen, 36 + unsigned long *outlen) 38 37 { 39 38 unsigned long modulus_len, ps_len, i; 40 39 ··· 41 42 42 43 /* test message size */ 43 44 if ((msglen > modulus_len) || (modulus_len < 11)) 44 - return -EINVAL; 45 + return NULL; 45 46 46 47 /* separate encoded message */ 47 - if ((msg[0] != 0x00) || (msg[1] != (unsigned char)1)) 48 - return -EINVAL; 48 + if (msg[0] != 0x00 || msg[1] != 0x01) 49 + return NULL; 49 50 50 51 for (i = 2; i < modulus_len - 1; i++) 51 52 if (msg[i] != 0xFF) ··· 55 56 if (msg[i] != 0) 56 57 /* There was no octet with hexadecimal value 0x00 57 58 to separate ps from m. */ 58 - return -EINVAL; 59 + return NULL; 59 60 60 61 ps_len = i - 2; 61 62 62 - if (*outlen < (msglen - (2 + ps_len + 1))) { 63 - *outlen = msglen - (2 + ps_len + 1); 64 - return -EOVERFLOW; 65 - } 66 - 67 63 *outlen = (msglen - (2 + ps_len + 1)); 68 - memcpy(out, &msg[2 + ps_len + 1], *outlen); 69 64 70 - return 0; 65 + return msg + 2 + ps_len + 1; 71 66 } 72 67 73 68 /* ··· 76 83 unsigned long mlen, mblen; 77 84 unsigned nret, l; 78 85 int head, i; 79 - unsigned char *out1 = NULL, *out2 = NULL; 86 + unsigned char *out1 = NULL; 87 + const char *m; 80 88 MPI in = NULL, res = NULL, pkey[2]; 81 89 uint8_t *p, *datap, *endp; 82 90 struct user_key_payload *ukp; ··· 114 120 } 115 121 116 122 mblen = mpi_get_nbits(pkey[0]); 117 - mlen = (mblen + 7)/8; 123 + mlen = DIV_ROUND_UP(mblen, 8); 118 124 119 125 if (mlen == 0) 120 126 goto err; 121 127 122 128 out1 = kzalloc(mlen, GFP_KERNEL); 123 129 if (!out1) 124 - goto err; 125 - 126 - out2 = kzalloc(mlen, GFP_KERNEL); 127 - if (!out2) 128 130 goto err; 129 131 130 132 nret = siglen; ··· 152 162 memset(out1, 0, head); 153 163 memcpy(out1 + head, p, l); 154 164 155 - err = pkcs_1_v1_5_decode_emsa(out1, len, mblen, out2, &len); 156 - if (err) 157 - goto err; 165 + m = pkcs_1_v1_5_decode_emsa(out1, len, mblen, &len); 158 166 159 - if (len != hlen || memcmp(out2, h, hlen)) 167 + if (!m || len != hlen || memcmp(m, h, hlen)) 160 168 err = -EINVAL; 161 169 162 170 err: 163 171 mpi_free(in); 164 172 mpi_free(res); 165 173 kfree(out1); 166 - kfree(out2); 167 174 while (--i >= 0) 168 175 mpi_free(pkey[i]); 169 176 err1: