mpicoder.c 8.7 KB

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