mpicoder.c 12 KB

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