mpicoder.c 9.2 KB

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