mpicoder.c 9.0 KB

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