Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 1 | /* mpicoder.c - Coder for the external representation of MPIs |
| 2 | * Copyright (C) 1998, 1999 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This file is part of GnuPG. |
| 5 | * |
| 6 | * GnuPG is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * GnuPG is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
| 19 | */ |
| 20 | |
David Howells | e104599 | 2012-09-24 17:11:27 +0100 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Christoph Hellwig | a1164a3 | 2015-08-28 09:27:15 +0200 | [diff] [blame] | 22 | #include <linux/count_zeros.h> |
Nicolai Stange | d755290 | 2016-03-22 13:12:39 +0100 | [diff] [blame] | 23 | #include <linux/byteorder/generic.h> |
Nicolai Stange | 90f864e | 2016-03-22 13:12:41 +0100 | [diff] [blame] | 24 | #include <linux/string.h> |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 25 | #include "mpi-internal.h" |
| 26 | |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 27 | #define MAX_EXTERN_MPI_BITS 16384 |
| 28 | |
David Howells | e104599 | 2012-09-24 17:11:27 +0100 | [diff] [blame] | 29 | /** |
| 30 | * mpi_read_raw_data - Read a raw byte stream as a positive integer |
| 31 | * @xbuffer: The data to read |
| 32 | * @nbytes: The amount of data to read |
| 33 | */ |
| 34 | MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes) |
| 35 | { |
| 36 | const uint8_t *buffer = xbuffer; |
| 37 | int i, j; |
| 38 | unsigned nbits, nlimbs; |
| 39 | mpi_limb_t a; |
| 40 | MPI val = NULL; |
| 41 | |
Chen Gang | 5402b80 | 2013-06-12 14:04:40 -0700 | [diff] [blame] | 42 | while (nbytes > 0 && buffer[0] == 0) { |
David Howells | e104599 | 2012-09-24 17:11:27 +0100 | [diff] [blame] | 43 | buffer++; |
| 44 | nbytes--; |
| 45 | } |
| 46 | |
| 47 | nbits = nbytes * 8; |
| 48 | if (nbits > MAX_EXTERN_MPI_BITS) { |
| 49 | pr_info("MPI: mpi too large (%u bits)\n", nbits); |
| 50 | return NULL; |
| 51 | } |
| 52 | if (nbytes > 0) |
Nicolai Stange | eef0df6 | 2016-05-26 13:05:32 +0200 | [diff] [blame] | 53 | nbits -= count_leading_zeros(buffer[0]) - (BITS_PER_LONG - 8); |
David Howells | e104599 | 2012-09-24 17:11:27 +0100 | [diff] [blame] | 54 | |
Andy Shevchenko | 0d2a1b2 | 2013-01-30 11:30:06 +0200 | [diff] [blame] | 55 | nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); |
David Howells | e104599 | 2012-09-24 17:11:27 +0100 | [diff] [blame] | 56 | val = mpi_alloc(nlimbs); |
| 57 | if (!val) |
| 58 | return NULL; |
| 59 | val->nbits = nbits; |
| 60 | val->sign = 0; |
| 61 | val->nlimbs = nlimbs; |
| 62 | |
| 63 | if (nbytes > 0) { |
| 64 | i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; |
| 65 | i %= BYTES_PER_MPI_LIMB; |
| 66 | for (j = nlimbs; j > 0; j--) { |
| 67 | a = 0; |
| 68 | for (; i < BYTES_PER_MPI_LIMB; i++) { |
| 69 | a <<= 8; |
| 70 | a |= *buffer++; |
| 71 | } |
| 72 | i = 0; |
| 73 | val->d[j - 1] = a; |
| 74 | } |
| 75 | } |
| 76 | return val; |
| 77 | } |
| 78 | EXPORT_SYMBOL_GPL(mpi_read_raw_data); |
| 79 | |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 80 | MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread) |
| 81 | { |
| 82 | const uint8_t *buffer = xbuffer; |
Nicolai Stange | 20b5b7f | 2016-05-26 23:19:55 +0200 | [diff] [blame^] | 83 | unsigned int nbits, nbytes; |
| 84 | MPI val; |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 85 | |
| 86 | if (*ret_nread < 2) |
Nicolai Stange | 03cdfaa | 2016-05-26 23:19:51 +0200 | [diff] [blame] | 87 | return ERR_PTR(-EINVAL); |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 88 | nbits = buffer[0] << 8 | buffer[1]; |
| 89 | |
| 90 | if (nbits > MAX_EXTERN_MPI_BITS) { |
| 91 | pr_info("MPI: mpi too large (%u bits)\n", nbits); |
Nicolai Stange | 03cdfaa | 2016-05-26 23:19:51 +0200 | [diff] [blame] | 92 | return ERR_PTR(-EINVAL); |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 93 | } |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 94 | |
Andy Shevchenko | 0d2a1b2 | 2013-01-30 11:30:06 +0200 | [diff] [blame] | 95 | nbytes = DIV_ROUND_UP(nbits, 8); |
Nicolai Stange | 7af791e | 2016-05-26 23:19:53 +0200 | [diff] [blame] | 96 | if (nbytes + 2 > *ret_nread) { |
Nicolai Stange | cdf24b4 | 2016-05-26 23:19:54 +0200 | [diff] [blame] | 97 | pr_info("MPI: mpi larger than buffer nbytes=%u ret_nread=%u\n", |
| 98 | nbytes, *ret_nread); |
Nicolai Stange | 7af791e | 2016-05-26 23:19:53 +0200 | [diff] [blame] | 99 | return ERR_PTR(-EINVAL); |
| 100 | } |
| 101 | |
Nicolai Stange | 20b5b7f | 2016-05-26 23:19:55 +0200 | [diff] [blame^] | 102 | val = mpi_read_raw_data(buffer + 2, nbytes); |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 103 | if (!val) |
Nicolai Stange | 03cdfaa | 2016-05-26 23:19:51 +0200 | [diff] [blame] | 104 | return ERR_PTR(-ENOMEM); |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 105 | |
Nicolai Stange | 7af791e | 2016-05-26 23:19:53 +0200 | [diff] [blame] | 106 | *ret_nread = nbytes + 2; |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 107 | return val; |
| 108 | } |
| 109 | EXPORT_SYMBOL_GPL(mpi_read_from_buffer); |
| 110 | |
Michal Marek | 3ee0cb5 | 2016-02-17 14:46:59 +0100 | [diff] [blame] | 111 | static int count_lzeros(MPI a) |
| 112 | { |
| 113 | mpi_limb_t alimb; |
| 114 | int i, lzeros = 0; |
| 115 | |
| 116 | for (i = a->nlimbs - 1; i >= 0; i--) { |
| 117 | alimb = a->d[i]; |
| 118 | if (alimb == 0) { |
| 119 | lzeros += sizeof(mpi_limb_t); |
| 120 | } else { |
| 121 | lzeros += count_leading_zeros(alimb) / 8; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | return lzeros; |
| 126 | } |
| 127 | |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 128 | /** |
| 129 | * mpi_read_buffer() - read MPI to a bufer provided by user (msb first) |
| 130 | * |
| 131 | * @a: a multi precision integer |
| 132 | * @buf: bufer to which the output will be written to. Needs to be at |
| 133 | * leaset mpi_get_size(a) long. |
| 134 | * @buf_len: size of the buf. |
Andrzej Zaborowski | 9cbe21d | 2015-11-13 12:01:32 +0100 | [diff] [blame] | 135 | * @nbytes: receives the actual length of the data written on success and |
| 136 | * the data to-be-written on -EOVERFLOW in case buf_len was too |
| 137 | * small. |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 138 | * @sign: if not NULL, it will be set to the sign of a. |
| 139 | * |
| 140 | * Return: 0 on success or error code in case of error |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 141 | */ |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 142 | int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, |
| 143 | int *sign) |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 144 | { |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 145 | uint8_t *p; |
Nicolai Stange | 90f864e | 2016-03-22 13:12:41 +0100 | [diff] [blame] | 146 | #if BYTES_PER_MPI_LIMB == 4 |
| 147 | __be32 alimb; |
| 148 | #elif BYTES_PER_MPI_LIMB == 8 |
| 149 | __be64 alimb; |
| 150 | #else |
| 151 | #error please implement for this limb size. |
| 152 | #endif |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 153 | unsigned int n = mpi_get_size(a); |
Michal Marek | 3ee0cb5 | 2016-02-17 14:46:59 +0100 | [diff] [blame] | 154 | int i, lzeros; |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 155 | |
Andrzej Zaborowski | 9cbe21d | 2015-11-13 12:01:32 +0100 | [diff] [blame] | 156 | if (!buf || !nbytes) |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 157 | return -EINVAL; |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 158 | |
| 159 | if (sign) |
| 160 | *sign = a->sign; |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 161 | |
Michal Marek | 3ee0cb5 | 2016-02-17 14:46:59 +0100 | [diff] [blame] | 162 | lzeros = count_lzeros(a); |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 163 | |
Andrzej Zaborowski | 9cbe21d | 2015-11-13 12:01:32 +0100 | [diff] [blame] | 164 | if (buf_len < n - lzeros) { |
| 165 | *nbytes = n - lzeros; |
| 166 | return -EOVERFLOW; |
| 167 | } |
| 168 | |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 169 | p = buf; |
Tadeusz Struk | 0f74fbf | 2015-08-24 07:52:14 -0700 | [diff] [blame] | 170 | *nbytes = n - lzeros; |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 171 | |
Nicolai Stange | f00fa24 | 2016-03-22 13:12:40 +0100 | [diff] [blame] | 172 | for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, |
| 173 | lzeros %= BYTES_PER_MPI_LIMB; |
| 174 | i >= 0; i--) { |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 175 | #if BYTES_PER_MPI_LIMB == 4 |
Nicolai Stange | 90f864e | 2016-03-22 13:12:41 +0100 | [diff] [blame] | 176 | alimb = cpu_to_be32(a->d[i]); |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 177 | #elif BYTES_PER_MPI_LIMB == 8 |
Nicolai Stange | 90f864e | 2016-03-22 13:12:41 +0100 | [diff] [blame] | 178 | alimb = cpu_to_be64(a->d[i]); |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 179 | #else |
| 180 | #error please implement for this limb size. |
| 181 | #endif |
Nicolai Stange | 462696f | 2016-03-22 13:12:42 +0100 | [diff] [blame] | 182 | memcpy(p, (u8 *)&alimb + lzeros, BYTES_PER_MPI_LIMB - lzeros); |
| 183 | p += BYTES_PER_MPI_LIMB - lzeros; |
| 184 | lzeros = 0; |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 185 | } |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | EXPORT_SYMBOL_GPL(mpi_read_buffer); |
| 189 | |
| 190 | /* |
| 191 | * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first). |
| 192 | * Caller must free the return string. |
| 193 | * This function does return a 0 byte buffer with nbytes set to zero if the |
| 194 | * value of A is zero. |
| 195 | * |
| 196 | * @a: a multi precision integer. |
| 197 | * @nbytes: receives the length of this buffer. |
| 198 | * @sign: if not NULL, it will be set to the sign of the a. |
| 199 | * |
| 200 | * Return: Pointer to MPI buffer or NULL on error |
| 201 | */ |
| 202 | void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign) |
| 203 | { |
Tadeusz Struk | 0f74fbf | 2015-08-24 07:52:14 -0700 | [diff] [blame] | 204 | uint8_t *buf; |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 205 | unsigned int n; |
| 206 | int ret; |
| 207 | |
| 208 | if (!nbytes) |
| 209 | return NULL; |
| 210 | |
| 211 | n = mpi_get_size(a); |
| 212 | |
| 213 | if (!n) |
| 214 | n++; |
| 215 | |
| 216 | buf = kmalloc(n, GFP_KERNEL); |
| 217 | |
| 218 | if (!buf) |
| 219 | return NULL; |
| 220 | |
| 221 | ret = mpi_read_buffer(a, buf, n, nbytes, sign); |
| 222 | |
| 223 | if (ret) { |
| 224 | kfree(buf); |
| 225 | return NULL; |
| 226 | } |
Tadeusz Struk | d37e296 | 2015-06-15 13:18:36 -0700 | [diff] [blame] | 227 | return buf; |
Dmitry Kasatkin | cdec9cb | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 228 | } |
| 229 | EXPORT_SYMBOL_GPL(mpi_get_buffer); |
| 230 | |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 231 | /** |
| 232 | * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first) |
| 233 | * |
| 234 | * This function works in the same way as the mpi_read_buffer, but it |
| 235 | * takes an sgl instead of u8 * buf. |
| 236 | * |
| 237 | * @a: a multi precision integer |
| 238 | * @sgl: scatterlist to write to. Needs to be at least |
| 239 | * mpi_get_size(a) long. |
| 240 | * @nbytes: in/out param - it has the be set to the maximum number of |
| 241 | * bytes that can be written to sgl. This has to be at least |
| 242 | * the size of the integer a. On return it receives the actual |
Andrzej Zaborowski | 9cbe21d | 2015-11-13 12:01:32 +0100 | [diff] [blame] | 243 | * length of the data written on success or the data that would |
| 244 | * be written if buffer was too small. |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 245 | * @sign: if not NULL, it will be set to the sign of a. |
| 246 | * |
| 247 | * Return: 0 on success or error code in case of error |
| 248 | */ |
| 249 | int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, |
| 250 | int *sign) |
| 251 | { |
| 252 | u8 *p, *p2; |
Nicolai Stange | d755290 | 2016-03-22 13:12:39 +0100 | [diff] [blame] | 253 | #if BYTES_PER_MPI_LIMB == 4 |
| 254 | __be32 alimb; |
| 255 | #elif BYTES_PER_MPI_LIMB == 8 |
| 256 | __be64 alimb; |
| 257 | #else |
| 258 | #error please implement for this limb size. |
| 259 | #endif |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 260 | unsigned int n = mpi_get_size(a); |
Michal Marek | 3ee0cb5 | 2016-02-17 14:46:59 +0100 | [diff] [blame] | 261 | int i, x, y = 0, lzeros, buf_len; |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 262 | |
Andrzej Zaborowski | 9cbe21d | 2015-11-13 12:01:32 +0100 | [diff] [blame] | 263 | if (!nbytes) |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 264 | return -EINVAL; |
| 265 | |
| 266 | if (sign) |
| 267 | *sign = a->sign; |
| 268 | |
Michal Marek | 3ee0cb5 | 2016-02-17 14:46:59 +0100 | [diff] [blame] | 269 | lzeros = count_lzeros(a); |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 270 | |
Andrzej Zaborowski | 9cbe21d | 2015-11-13 12:01:32 +0100 | [diff] [blame] | 271 | if (*nbytes < n - lzeros) { |
| 272 | *nbytes = n - lzeros; |
| 273 | return -EOVERFLOW; |
| 274 | } |
| 275 | |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 276 | *nbytes = n - lzeros; |
| 277 | buf_len = sgl->length; |
| 278 | p2 = sg_virt(sgl); |
| 279 | |
Nicolai Stange | f2d1362 | 2016-03-22 13:12:35 +0100 | [diff] [blame] | 280 | for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, |
| 281 | lzeros %= BYTES_PER_MPI_LIMB; |
| 282 | i >= 0; i--) { |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 283 | #if BYTES_PER_MPI_LIMB == 4 |
Nicolai Stange | d755290 | 2016-03-22 13:12:39 +0100 | [diff] [blame] | 284 | alimb = cpu_to_be32(a->d[i]); |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 285 | #elif BYTES_PER_MPI_LIMB == 8 |
Nicolai Stange | d755290 | 2016-03-22 13:12:39 +0100 | [diff] [blame] | 286 | alimb = cpu_to_be64(a->d[i]); |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 287 | #else |
| 288 | #error please implement for this limb size. |
| 289 | #endif |
Nicolai Stange | 654842e | 2016-03-22 13:12:36 +0100 | [diff] [blame] | 290 | if (lzeros) { |
Nicolai Stange | f2d1362 | 2016-03-22 13:12:35 +0100 | [diff] [blame] | 291 | y = lzeros; |
Nicolai Stange | 654842e | 2016-03-22 13:12:36 +0100 | [diff] [blame] | 292 | lzeros = 0; |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Nicolai Stange | d755290 | 2016-03-22 13:12:39 +0100 | [diff] [blame] | 295 | p = (u8 *)&alimb + y; |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 296 | |
| 297 | for (x = 0; x < sizeof(alimb) - y; x++) { |
| 298 | if (!buf_len) { |
| 299 | sgl = sg_next(sgl); |
| 300 | if (!sgl) |
| 301 | return -EINVAL; |
| 302 | buf_len = sgl->length; |
| 303 | p2 = sg_virt(sgl); |
| 304 | } |
| 305 | *p2++ = *p++; |
| 306 | buf_len--; |
| 307 | } |
| 308 | y = 0; |
| 309 | } |
| 310 | return 0; |
| 311 | } |
| 312 | EXPORT_SYMBOL_GPL(mpi_write_to_sgl); |
| 313 | |
| 314 | /* |
| 315 | * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with |
| 316 | * data from the sgl |
| 317 | * |
| 318 | * This function works in the same way as the mpi_read_raw_data, but it |
| 319 | * takes an sgl instead of void * buffer. i.e. it allocates |
| 320 | * a new MPI and reads the content of the sgl to the MPI. |
| 321 | * |
| 322 | * @sgl: scatterlist to read from |
Nicolai Stange | b698538 | 2016-03-22 13:12:43 +0100 | [diff] [blame] | 323 | * @nbytes: number of bytes to read |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 324 | * |
| 325 | * Return: Pointer to a new MPI or NULL on error |
| 326 | */ |
Nicolai Stange | b698538 | 2016-03-22 13:12:43 +0100 | [diff] [blame] | 327 | MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes) |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 328 | { |
| 329 | struct scatterlist *sg; |
| 330 | int x, i, j, z, lzeros, ents; |
Nicolai Stange | b698538 | 2016-03-22 13:12:43 +0100 | [diff] [blame] | 331 | unsigned int nbits, nlimbs; |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 332 | mpi_limb_t a; |
| 333 | MPI val = NULL; |
| 334 | |
| 335 | lzeros = 0; |
| 336 | ents = sg_nents(sgl); |
| 337 | |
| 338 | for_each_sg(sgl, sg, ents, i) { |
| 339 | const u8 *buff = sg_virt(sg); |
| 340 | int len = sg->length; |
| 341 | |
Stephan Mueller | 63349d0 | 2015-10-18 12:45:18 +0200 | [diff] [blame] | 342 | while (len && !*buff) { |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 343 | lzeros++; |
Stephan Mueller | 63349d0 | 2015-10-18 12:45:18 +0200 | [diff] [blame] | 344 | len--; |
| 345 | buff++; |
| 346 | } |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 347 | |
| 348 | if (len && *buff) |
| 349 | break; |
| 350 | |
| 351 | ents--; |
Nicolai Stange | ab1e912 | 2016-03-22 13:12:44 +0100 | [diff] [blame] | 352 | nbytes -= lzeros; |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 353 | lzeros = 0; |
| 354 | } |
| 355 | |
| 356 | sgl = sg; |
Nicolai Stange | ab1e912 | 2016-03-22 13:12:44 +0100 | [diff] [blame] | 357 | nbytes -= lzeros; |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 358 | nbits = nbytes * 8; |
| 359 | if (nbits > MAX_EXTERN_MPI_BITS) { |
| 360 | pr_info("MPI: mpi too large (%u bits)\n", nbits); |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | if (nbytes > 0) |
Nicolai Stange | 64c09b0 | 2016-03-22 13:17:27 +0100 | [diff] [blame] | 365 | nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros)) - |
| 366 | (BITS_PER_LONG - 8); |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 367 | |
| 368 | nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); |
| 369 | val = mpi_alloc(nlimbs); |
| 370 | if (!val) |
| 371 | return NULL; |
| 372 | |
| 373 | val->nbits = nbits; |
| 374 | val->sign = 0; |
| 375 | val->nlimbs = nlimbs; |
| 376 | |
| 377 | if (nbytes == 0) |
| 378 | return val; |
| 379 | |
| 380 | j = nlimbs - 1; |
| 381 | a = 0; |
Nicolai Stange | 85d541a | 2016-03-22 13:18:07 +0100 | [diff] [blame] | 382 | z = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; |
| 383 | z %= BYTES_PER_MPI_LIMB; |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 384 | |
| 385 | for_each_sg(sgl, sg, ents, i) { |
| 386 | const u8 *buffer = sg_virt(sg) + lzeros; |
| 387 | int len = sg->length - lzeros; |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 388 | |
Nicolai Stange | 85d541a | 2016-03-22 13:18:07 +0100 | [diff] [blame] | 389 | for (x = 0; x < len; x++) { |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 390 | a <<= 8; |
| 391 | a |= *buffer++; |
| 392 | if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) { |
| 393 | val->d[j--] = a; |
| 394 | a = 0; |
| 395 | } |
| 396 | } |
| 397 | z += x; |
Tadeusz Struk | 2d4d1ee | 2015-10-08 09:26:50 -0700 | [diff] [blame] | 398 | lzeros = 0; |
| 399 | } |
| 400 | return val; |
| 401 | } |
| 402 | EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl); |