mpicoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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]);
  49. else
  50. nbits = 0;
  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. int i, j;
  78. unsigned nbits, nbytes, nlimbs, nread = 0;
  79. mpi_limb_t a;
  80. MPI val = NULL;
  81. if (*ret_nread < 2)
  82. goto leave;
  83. nbits = buffer[0] << 8 | buffer[1];
  84. if (nbits > MAX_EXTERN_MPI_BITS) {
  85. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  86. goto leave;
  87. }
  88. buffer += 2;
  89. nread = 2;
  90. nbytes = DIV_ROUND_UP(nbits, 8);
  91. nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
  92. val = mpi_alloc(nlimbs);
  93. if (!val)
  94. return NULL;
  95. i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  96. i %= BYTES_PER_MPI_LIMB;
  97. val->nbits = nbits;
  98. j = val->nlimbs = nlimbs;
  99. val->sign = 0;
  100. for (; j > 0; j--) {
  101. a = 0;
  102. for (; i < BYTES_PER_MPI_LIMB; i++) {
  103. if (++nread > *ret_nread) {
  104. printk
  105. ("MPI: mpi larger than buffer nread=%d ret_nread=%d\n",
  106. nread, *ret_nread);
  107. goto leave;
  108. }
  109. a <<= 8;
  110. a |= *buffer++;
  111. }
  112. i = 0;
  113. val->d[j - 1] = a;
  114. }
  115. leave:
  116. *ret_nread = nread;
  117. return val;
  118. }
  119. EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
  120. static int count_lzeros(MPI a)
  121. {
  122. mpi_limb_t alimb;
  123. int i, lzeros = 0;
  124. for (i = a->nlimbs - 1; i >= 0; i--) {
  125. alimb = a->d[i];
  126. if (alimb == 0) {
  127. lzeros += sizeof(mpi_limb_t);
  128. } else {
  129. lzeros += count_leading_zeros(alimb) / 8;
  130. break;
  131. }
  132. }
  133. return lzeros;
  134. }
  135. /**
  136. * mpi_read_buffer() - read MPI to a bufer provided by user (msb first)
  137. *
  138. * @a: a multi precision integer
  139. * @buf: bufer to which the output will be written to. Needs to be at
  140. * leaset mpi_get_size(a) long.
  141. * @buf_len: size of the buf.
  142. * @nbytes: receives the actual length of the data written on success and
  143. * the data to-be-written on -EOVERFLOW in case buf_len was too
  144. * small.
  145. * @sign: if not NULL, it will be set to the sign of a.
  146. *
  147. * Return: 0 on success or error code in case of error
  148. */
  149. int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
  150. int *sign)
  151. {
  152. uint8_t *p;
  153. #if BYTES_PER_MPI_LIMB == 4
  154. __be32 alimb;
  155. #elif BYTES_PER_MPI_LIMB == 8
  156. __be64 alimb;
  157. #else
  158. #error please implement for this limb size.
  159. #endif
  160. unsigned int n = mpi_get_size(a);
  161. int i, lzeros;
  162. if (!buf || !nbytes)
  163. return -EINVAL;
  164. if (sign)
  165. *sign = a->sign;
  166. lzeros = count_lzeros(a);
  167. if (buf_len < n - lzeros) {
  168. *nbytes = n - lzeros;
  169. return -EOVERFLOW;
  170. }
  171. p = buf;
  172. *nbytes = n - lzeros;
  173. for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
  174. lzeros %= BYTES_PER_MPI_LIMB;
  175. i >= 0; i--) {
  176. #if BYTES_PER_MPI_LIMB == 4
  177. alimb = cpu_to_be32(a->d[i]);
  178. #elif BYTES_PER_MPI_LIMB == 8
  179. alimb = cpu_to_be64(a->d[i]);
  180. #else
  181. #error please implement for this limb size.
  182. #endif
  183. memcpy(p, (u8 *)&alimb + lzeros, BYTES_PER_MPI_LIMB - lzeros);
  184. p += BYTES_PER_MPI_LIMB - lzeros;
  185. lzeros = 0;
  186. }
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(mpi_read_buffer);
  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. {
  204. uint8_t *buf;
  205. unsigned int n;
  206. int ret;
  207. if (!nbytes)
  208. return NULL;
  209. n = mpi_get_size(a);
  210. if (!n)
  211. n++;
  212. buf = kmalloc(n, GFP_KERNEL);
  213. if (!buf)
  214. return NULL;
  215. ret = mpi_read_buffer(a, buf, n, nbytes, sign);
  216. if (ret) {
  217. kfree(buf);
  218. return NULL;
  219. }
  220. return buf;
  221. }
  222. EXPORT_SYMBOL_GPL(mpi_get_buffer);
  223. /****************
  224. * Use BUFFER to update MPI.
  225. */
  226. int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
  227. {
  228. const uint8_t *buffer = xbuffer, *p;
  229. mpi_limb_t alimb;
  230. int nlimbs;
  231. int i;
  232. nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
  233. if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
  234. return -ENOMEM;
  235. a->sign = sign;
  236. for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) {
  237. #if BYTES_PER_MPI_LIMB == 4
  238. alimb = (mpi_limb_t) *p--;
  239. alimb |= (mpi_limb_t) *p-- << 8;
  240. alimb |= (mpi_limb_t) *p-- << 16;
  241. alimb |= (mpi_limb_t) *p-- << 24;
  242. #elif BYTES_PER_MPI_LIMB == 8
  243. alimb = (mpi_limb_t) *p--;
  244. alimb |= (mpi_limb_t) *p-- << 8;
  245. alimb |= (mpi_limb_t) *p-- << 16;
  246. alimb |= (mpi_limb_t) *p-- << 24;
  247. alimb |= (mpi_limb_t) *p-- << 32;
  248. alimb |= (mpi_limb_t) *p-- << 40;
  249. alimb |= (mpi_limb_t) *p-- << 48;
  250. alimb |= (mpi_limb_t) *p-- << 56;
  251. #else
  252. #error please implement for this limb size.
  253. #endif
  254. a->d[i++] = alimb;
  255. }
  256. if (p >= buffer) {
  257. #if BYTES_PER_MPI_LIMB == 4
  258. alimb = *p--;
  259. if (p >= buffer)
  260. alimb |= (mpi_limb_t) *p-- << 8;
  261. if (p >= buffer)
  262. alimb |= (mpi_limb_t) *p-- << 16;
  263. if (p >= buffer)
  264. alimb |= (mpi_limb_t) *p-- << 24;
  265. #elif BYTES_PER_MPI_LIMB == 8
  266. alimb = (mpi_limb_t) *p--;
  267. if (p >= buffer)
  268. alimb |= (mpi_limb_t) *p-- << 8;
  269. if (p >= buffer)
  270. alimb |= (mpi_limb_t) *p-- << 16;
  271. if (p >= buffer)
  272. alimb |= (mpi_limb_t) *p-- << 24;
  273. if (p >= buffer)
  274. alimb |= (mpi_limb_t) *p-- << 32;
  275. if (p >= buffer)
  276. alimb |= (mpi_limb_t) *p-- << 40;
  277. if (p >= buffer)
  278. alimb |= (mpi_limb_t) *p-- << 48;
  279. if (p >= buffer)
  280. alimb |= (mpi_limb_t) *p-- << 56;
  281. #else
  282. #error please implement for this limb size.
  283. #endif
  284. a->d[i++] = alimb;
  285. }
  286. a->nlimbs = i;
  287. if (i != nlimbs) {
  288. pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
  289. nlimbs);
  290. BUG();
  291. }
  292. return 0;
  293. }
  294. EXPORT_SYMBOL_GPL(mpi_set_buffer);
  295. /**
  296. * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
  297. *
  298. * This function works in the same way as the mpi_read_buffer, but it
  299. * takes an sgl instead of u8 * buf.
  300. *
  301. * @a: a multi precision integer
  302. * @sgl: scatterlist to write to. Needs to be at least
  303. * mpi_get_size(a) long.
  304. * @nbytes: in/out param - it has the be set to the maximum number of
  305. * bytes that can be written to sgl. This has to be at least
  306. * the size of the integer a. On return it receives the actual
  307. * length of the data written on success or the data that would
  308. * be written if buffer was too small.
  309. * @sign: if not NULL, it will be set to the sign of a.
  310. *
  311. * Return: 0 on success or error code in case of error
  312. */
  313. int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
  314. int *sign)
  315. {
  316. u8 *p, *p2;
  317. #if BYTES_PER_MPI_LIMB == 4
  318. __be32 alimb;
  319. #elif BYTES_PER_MPI_LIMB == 8
  320. __be64 alimb;
  321. #else
  322. #error please implement for this limb size.
  323. #endif
  324. unsigned int n = mpi_get_size(a);
  325. int i, x, y = 0, lzeros, buf_len;
  326. if (!nbytes)
  327. return -EINVAL;
  328. if (sign)
  329. *sign = a->sign;
  330. lzeros = count_lzeros(a);
  331. if (*nbytes < n - lzeros) {
  332. *nbytes = n - lzeros;
  333. return -EOVERFLOW;
  334. }
  335. *nbytes = n - lzeros;
  336. buf_len = sgl->length;
  337. p2 = sg_virt(sgl);
  338. for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
  339. lzeros %= BYTES_PER_MPI_LIMB;
  340. i >= 0; i--) {
  341. #if BYTES_PER_MPI_LIMB == 4
  342. alimb = cpu_to_be32(a->d[i]);
  343. #elif BYTES_PER_MPI_LIMB == 8
  344. alimb = cpu_to_be64(a->d[i]);
  345. #else
  346. #error please implement for this limb size.
  347. #endif
  348. if (lzeros) {
  349. y = lzeros;
  350. lzeros = 0;
  351. }
  352. p = (u8 *)&alimb + y;
  353. for (x = 0; x < sizeof(alimb) - y; x++) {
  354. if (!buf_len) {
  355. sgl = sg_next(sgl);
  356. if (!sgl)
  357. return -EINVAL;
  358. buf_len = sgl->length;
  359. p2 = sg_virt(sgl);
  360. }
  361. *p2++ = *p++;
  362. buf_len--;
  363. }
  364. y = 0;
  365. }
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(mpi_write_to_sgl);
  369. /*
  370. * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
  371. * data from the sgl
  372. *
  373. * This function works in the same way as the mpi_read_raw_data, but it
  374. * takes an sgl instead of void * buffer. i.e. it allocates
  375. * a new MPI and reads the content of the sgl to the MPI.
  376. *
  377. * @sgl: scatterlist to read from
  378. * @nbytes: number of bytes to read
  379. *
  380. * Return: Pointer to a new MPI or NULL on error
  381. */
  382. MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes)
  383. {
  384. struct scatterlist *sg;
  385. int x, i, j, z, lzeros, ents;
  386. unsigned int nbits, nlimbs;
  387. mpi_limb_t a;
  388. MPI val = NULL;
  389. lzeros = 0;
  390. ents = sg_nents(sgl);
  391. for_each_sg(sgl, sg, ents, i) {
  392. const u8 *buff = sg_virt(sg);
  393. int len = sg->length;
  394. while (len && !*buff) {
  395. lzeros++;
  396. len--;
  397. buff++;
  398. }
  399. if (len && *buff)
  400. break;
  401. ents--;
  402. nbytes -= lzeros;
  403. lzeros = 0;
  404. }
  405. sgl = sg;
  406. nbytes -= lzeros;
  407. nbits = nbytes * 8;
  408. if (nbits > MAX_EXTERN_MPI_BITS) {
  409. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  410. return NULL;
  411. }
  412. if (nbytes > 0)
  413. nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros)) -
  414. (BITS_PER_LONG - 8);
  415. nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
  416. val = mpi_alloc(nlimbs);
  417. if (!val)
  418. return NULL;
  419. val->nbits = nbits;
  420. val->sign = 0;
  421. val->nlimbs = nlimbs;
  422. if (nbytes == 0)
  423. return val;
  424. j = nlimbs - 1;
  425. a = 0;
  426. z = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  427. z %= BYTES_PER_MPI_LIMB;
  428. for_each_sg(sgl, sg, ents, i) {
  429. const u8 *buffer = sg_virt(sg) + lzeros;
  430. int len = sg->length - lzeros;
  431. for (x = 0; x < len; x++) {
  432. a <<= 8;
  433. a |= *buffer++;
  434. if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) {
  435. val->d[j--] = a;
  436. a = 0;
  437. }
  438. }
  439. z += x;
  440. lzeros = 0;
  441. }
  442. return val;
  443. }
  444. EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);