mpicoder.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. #include <linux/bitops.h>
  21. #include <linux/byteorder/generic.h>
  22. #include <linux/count_zeros.h>
  23. #include <linux/export.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/string.h>
  26. #include "mpi-internal.h"
  27. #define MAX_EXTERN_MPI_BITS 16384
  28. /**
  29. * mpi_read_raw_data - Read a raw byte stream as a positive integer
  30. * @xbuffer: The data to read
  31. * @nbytes: The amount of data to read
  32. */
  33. MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes)
  34. {
  35. const uint8_t *buffer = xbuffer;
  36. int i, j;
  37. unsigned nbits, nlimbs;
  38. mpi_limb_t a;
  39. MPI val = NULL;
  40. while (nbytes > 0 && buffer[0] == 0) {
  41. buffer++;
  42. nbytes--;
  43. }
  44. nbits = nbytes * 8;
  45. if (nbits > MAX_EXTERN_MPI_BITS) {
  46. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  47. return NULL;
  48. }
  49. if (nbytes > 0)
  50. nbits -= count_leading_zeros(buffer[0]) - (BITS_PER_LONG - 8);
  51. nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
  52. val = mpi_alloc(nlimbs);
  53. if (!val)
  54. return NULL;
  55. val->nbits = nbits;
  56. val->sign = 0;
  57. val->nlimbs = nlimbs;
  58. if (nbytes > 0) {
  59. i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  60. i %= BYTES_PER_MPI_LIMB;
  61. for (j = nlimbs; j > 0; j--) {
  62. a = 0;
  63. for (; i < BYTES_PER_MPI_LIMB; i++) {
  64. a <<= 8;
  65. a |= *buffer++;
  66. }
  67. i = 0;
  68. val->d[j - 1] = a;
  69. }
  70. }
  71. return val;
  72. }
  73. EXPORT_SYMBOL_GPL(mpi_read_raw_data);
  74. MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
  75. {
  76. const uint8_t *buffer = xbuffer;
  77. unsigned int nbits, nbytes;
  78. MPI val;
  79. if (*ret_nread < 2)
  80. return ERR_PTR(-EINVAL);
  81. nbits = buffer[0] << 8 | buffer[1];
  82. if (nbits > MAX_EXTERN_MPI_BITS) {
  83. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  84. return ERR_PTR(-EINVAL);
  85. }
  86. nbytes = DIV_ROUND_UP(nbits, 8);
  87. if (nbytes + 2 > *ret_nread) {
  88. pr_info("MPI: mpi larger than buffer nbytes=%u ret_nread=%u\n",
  89. nbytes, *ret_nread);
  90. return ERR_PTR(-EINVAL);
  91. }
  92. val = mpi_read_raw_data(buffer + 2, nbytes);
  93. if (!val)
  94. return ERR_PTR(-ENOMEM);
  95. *ret_nread = nbytes + 2;
  96. return val;
  97. }
  98. EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
  99. static int count_lzeros(MPI a)
  100. {
  101. mpi_limb_t alimb;
  102. int i, lzeros = 0;
  103. for (i = a->nlimbs - 1; i >= 0; i--) {
  104. alimb = a->d[i];
  105. if (alimb == 0) {
  106. lzeros += sizeof(mpi_limb_t);
  107. } else {
  108. lzeros += count_leading_zeros(alimb) / 8;
  109. break;
  110. }
  111. }
  112. return lzeros;
  113. }
  114. /**
  115. * mpi_read_buffer() - read MPI to a buffer provided by user (msb first)
  116. *
  117. * @a: a multi precision integer
  118. * @buf: buffer to which the output will be written to. Needs to be at
  119. * least mpi_get_size(a) long.
  120. * @buf_len: size of the buf.
  121. * @nbytes: receives the actual length of the data written on success and
  122. * the data to-be-written on -EOVERFLOW in case buf_len was too
  123. * small.
  124. * @sign: if not NULL, it will be set to the sign of a.
  125. *
  126. * Return: 0 on success or error code in case of error
  127. */
  128. int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
  129. int *sign)
  130. {
  131. uint8_t *p;
  132. #if BYTES_PER_MPI_LIMB == 4
  133. __be32 alimb;
  134. #elif BYTES_PER_MPI_LIMB == 8
  135. __be64 alimb;
  136. #else
  137. #error please implement for this limb size.
  138. #endif
  139. unsigned int n = mpi_get_size(a);
  140. int i, lzeros;
  141. if (!buf || !nbytes)
  142. return -EINVAL;
  143. if (sign)
  144. *sign = a->sign;
  145. lzeros = count_lzeros(a);
  146. if (buf_len < n - lzeros) {
  147. *nbytes = n - lzeros;
  148. return -EOVERFLOW;
  149. }
  150. p = buf;
  151. *nbytes = n - lzeros;
  152. for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
  153. lzeros %= BYTES_PER_MPI_LIMB;
  154. i >= 0; i--) {
  155. #if BYTES_PER_MPI_LIMB == 4
  156. alimb = cpu_to_be32(a->d[i]);
  157. #elif BYTES_PER_MPI_LIMB == 8
  158. alimb = cpu_to_be64(a->d[i]);
  159. #else
  160. #error please implement for this limb size.
  161. #endif
  162. memcpy(p, (u8 *)&alimb + lzeros, BYTES_PER_MPI_LIMB - lzeros);
  163. p += BYTES_PER_MPI_LIMB - lzeros;
  164. lzeros = 0;
  165. }
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(mpi_read_buffer);
  169. /*
  170. * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first).
  171. * Caller must free the return string.
  172. * This function does return a 0 byte buffer with nbytes set to zero if the
  173. * value of A is zero.
  174. *
  175. * @a: a multi precision integer.
  176. * @nbytes: receives the length of this buffer.
  177. * @sign: if not NULL, it will be set to the sign of the a.
  178. *
  179. * Return: Pointer to MPI buffer or NULL on error
  180. */
  181. void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
  182. {
  183. uint8_t *buf;
  184. unsigned int n;
  185. int ret;
  186. if (!nbytes)
  187. return NULL;
  188. n = mpi_get_size(a);
  189. if (!n)
  190. n++;
  191. buf = kmalloc(n, GFP_KERNEL);
  192. if (!buf)
  193. return NULL;
  194. ret = mpi_read_buffer(a, buf, n, nbytes, sign);
  195. if (ret) {
  196. kfree(buf);
  197. return NULL;
  198. }
  199. return buf;
  200. }
  201. EXPORT_SYMBOL_GPL(mpi_get_buffer);
  202. /**
  203. * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
  204. *
  205. * This function works in the same way as the mpi_read_buffer, but it
  206. * takes an sgl instead of u8 * buf.
  207. *
  208. * @a: a multi precision integer
  209. * @sgl: scatterlist to write to. Needs to be at least
  210. * mpi_get_size(a) long.
  211. * @nbytes: the number of bytes to write. Leading bytes will be
  212. * filled with zero.
  213. * @sign: if not NULL, it will be set to the sign of a.
  214. *
  215. * Return: 0 on success or error code in case of error
  216. */
  217. int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned nbytes,
  218. int *sign)
  219. {
  220. u8 *p, *p2;
  221. #if BYTES_PER_MPI_LIMB == 4
  222. __be32 alimb;
  223. #elif BYTES_PER_MPI_LIMB == 8
  224. __be64 alimb;
  225. #else
  226. #error please implement for this limb size.
  227. #endif
  228. unsigned int n = mpi_get_size(a);
  229. struct sg_mapping_iter miter;
  230. int i, x, buf_len;
  231. int nents;
  232. if (sign)
  233. *sign = a->sign;
  234. if (nbytes < n)
  235. return -EOVERFLOW;
  236. nents = sg_nents_for_len(sgl, nbytes);
  237. if (nents < 0)
  238. return -EINVAL;
  239. sg_miter_start(&miter, sgl, nents, SG_MITER_ATOMIC | SG_MITER_TO_SG);
  240. sg_miter_next(&miter);
  241. buf_len = miter.length;
  242. p2 = miter.addr;
  243. while (nbytes > n) {
  244. i = min_t(unsigned, nbytes - n, buf_len);
  245. memset(p2, 0, i);
  246. p2 += i;
  247. nbytes -= i;
  248. buf_len -= i;
  249. if (!buf_len) {
  250. sg_miter_next(&miter);
  251. buf_len = miter.length;
  252. p2 = miter.addr;
  253. }
  254. }
  255. for (i = a->nlimbs - 1; i >= 0; i--) {
  256. #if BYTES_PER_MPI_LIMB == 4
  257. alimb = a->d[i] ? cpu_to_be32(a->d[i]) : 0;
  258. #elif BYTES_PER_MPI_LIMB == 8
  259. alimb = a->d[i] ? cpu_to_be64(a->d[i]) : 0;
  260. #else
  261. #error please implement for this limb size.
  262. #endif
  263. p = (u8 *)&alimb;
  264. for (x = 0; x < sizeof(alimb); x++) {
  265. *p2++ = *p++;
  266. if (!--buf_len) {
  267. sg_miter_next(&miter);
  268. buf_len = miter.length;
  269. p2 = miter.addr;
  270. }
  271. }
  272. }
  273. sg_miter_stop(&miter);
  274. return 0;
  275. }
  276. EXPORT_SYMBOL_GPL(mpi_write_to_sgl);
  277. /*
  278. * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
  279. * data from the sgl
  280. *
  281. * This function works in the same way as the mpi_read_raw_data, but it
  282. * takes an sgl instead of void * buffer. i.e. it allocates
  283. * a new MPI and reads the content of the sgl to the MPI.
  284. *
  285. * @sgl: scatterlist to read from
  286. * @nbytes: number of bytes to read
  287. *
  288. * Return: Pointer to a new MPI or NULL on error
  289. */
  290. MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes)
  291. {
  292. struct sg_mapping_iter miter;
  293. unsigned int nbits, nlimbs;
  294. int x, j, z, lzeros, ents;
  295. unsigned int len;
  296. const u8 *buff;
  297. mpi_limb_t a;
  298. MPI val = NULL;
  299. ents = sg_nents_for_len(sgl, nbytes);
  300. if (ents < 0)
  301. return NULL;
  302. sg_miter_start(&miter, sgl, ents, SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  303. lzeros = 0;
  304. len = 0;
  305. while (nbytes > 0) {
  306. while (len && !*buff) {
  307. lzeros++;
  308. len--;
  309. buff++;
  310. }
  311. if (len && *buff)
  312. break;
  313. sg_miter_next(&miter);
  314. buff = miter.addr;
  315. len = miter.length;
  316. nbytes -= lzeros;
  317. lzeros = 0;
  318. }
  319. miter.consumed = lzeros;
  320. nbytes -= lzeros;
  321. nbits = nbytes * 8;
  322. if (nbits > MAX_EXTERN_MPI_BITS) {
  323. sg_miter_stop(&miter);
  324. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  325. return NULL;
  326. }
  327. if (nbytes > 0)
  328. nbits -= count_leading_zeros(*buff) - (BITS_PER_LONG - 8);
  329. sg_miter_stop(&miter);
  330. nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
  331. val = mpi_alloc(nlimbs);
  332. if (!val)
  333. return NULL;
  334. val->nbits = nbits;
  335. val->sign = 0;
  336. val->nlimbs = nlimbs;
  337. if (nbytes == 0)
  338. return val;
  339. j = nlimbs - 1;
  340. a = 0;
  341. z = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  342. z %= BYTES_PER_MPI_LIMB;
  343. while (sg_miter_next(&miter)) {
  344. buff = miter.addr;
  345. len = min(miter.length, nbytes);
  346. nbytes -= len;
  347. for (x = 0; x < len; x++) {
  348. a <<= 8;
  349. a |= *buff++;
  350. if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) {
  351. val->d[j--] = a;
  352. a = 0;
  353. }
  354. }
  355. z += x;
  356. }
  357. return val;
  358. }
  359. EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);