mpicoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. /**
  119. * mpi_read_buffer() - read MPI to a bufer provided by user (msb first)
  120. *
  121. * @a: a multi precision integer
  122. * @buf: bufer to which the output will be written to. Needs to be at
  123. * leaset mpi_get_size(a) long.
  124. * @buf_len: size of the buf.
  125. * @nbytes: receives the actual length of the data written.
  126. * @sign: if not NULL, it will be set to the sign of a.
  127. *
  128. * Return: 0 on success or error code in case of error
  129. */
  130. int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
  131. int *sign)
  132. {
  133. uint8_t *p;
  134. mpi_limb_t alimb;
  135. unsigned int n = mpi_get_size(a);
  136. int i, lzeros = 0;
  137. if (buf_len < n || !buf || !nbytes)
  138. return -EINVAL;
  139. if (sign)
  140. *sign = a->sign;
  141. p = (void *)&a->d[a->nlimbs] - 1;
  142. for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) {
  143. if (!*p)
  144. lzeros++;
  145. else
  146. break;
  147. }
  148. p = buf;
  149. *nbytes = n - lzeros;
  150. for (i = a->nlimbs - 1; i >= 0; i--) {
  151. alimb = a->d[i];
  152. #if BYTES_PER_MPI_LIMB == 4
  153. *p++ = alimb >> 24;
  154. *p++ = alimb >> 16;
  155. *p++ = alimb >> 8;
  156. *p++ = alimb;
  157. #elif BYTES_PER_MPI_LIMB == 8
  158. *p++ = alimb >> 56;
  159. *p++ = alimb >> 48;
  160. *p++ = alimb >> 40;
  161. *p++ = alimb >> 32;
  162. *p++ = alimb >> 24;
  163. *p++ = alimb >> 16;
  164. *p++ = alimb >> 8;
  165. *p++ = alimb;
  166. #else
  167. #error please implement for this limb size.
  168. #endif
  169. if (lzeros > 0) {
  170. if (lzeros >= sizeof(alimb)) {
  171. p -= sizeof(alimb);
  172. } else {
  173. mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
  174. mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
  175. + lzeros;
  176. *limb1 = *limb2;
  177. p -= lzeros;
  178. }
  179. lzeros -= sizeof(alimb);
  180. }
  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. * Use BUFFER to update MPI.
  220. */
  221. int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
  222. {
  223. const uint8_t *buffer = xbuffer, *p;
  224. mpi_limb_t alimb;
  225. int nlimbs;
  226. int i;
  227. nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
  228. if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
  229. return -ENOMEM;
  230. a->sign = sign;
  231. for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) {
  232. #if BYTES_PER_MPI_LIMB == 4
  233. alimb = (mpi_limb_t) *p--;
  234. alimb |= (mpi_limb_t) *p-- << 8;
  235. alimb |= (mpi_limb_t) *p-- << 16;
  236. alimb |= (mpi_limb_t) *p-- << 24;
  237. #elif BYTES_PER_MPI_LIMB == 8
  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. alimb |= (mpi_limb_t) *p-- << 32;
  243. alimb |= (mpi_limb_t) *p-- << 40;
  244. alimb |= (mpi_limb_t) *p-- << 48;
  245. alimb |= (mpi_limb_t) *p-- << 56;
  246. #else
  247. #error please implement for this limb size.
  248. #endif
  249. a->d[i++] = alimb;
  250. }
  251. if (p >= buffer) {
  252. #if BYTES_PER_MPI_LIMB == 4
  253. alimb = *p--;
  254. if (p >= buffer)
  255. alimb |= (mpi_limb_t) *p-- << 8;
  256. if (p >= buffer)
  257. alimb |= (mpi_limb_t) *p-- << 16;
  258. if (p >= buffer)
  259. alimb |= (mpi_limb_t) *p-- << 24;
  260. #elif BYTES_PER_MPI_LIMB == 8
  261. alimb = (mpi_limb_t) *p--;
  262. if (p >= buffer)
  263. alimb |= (mpi_limb_t) *p-- << 8;
  264. if (p >= buffer)
  265. alimb |= (mpi_limb_t) *p-- << 16;
  266. if (p >= buffer)
  267. alimb |= (mpi_limb_t) *p-- << 24;
  268. if (p >= buffer)
  269. alimb |= (mpi_limb_t) *p-- << 32;
  270. if (p >= buffer)
  271. alimb |= (mpi_limb_t) *p-- << 40;
  272. if (p >= buffer)
  273. alimb |= (mpi_limb_t) *p-- << 48;
  274. if (p >= buffer)
  275. alimb |= (mpi_limb_t) *p-- << 56;
  276. #else
  277. #error please implement for this limb size.
  278. #endif
  279. a->d[i++] = alimb;
  280. }
  281. a->nlimbs = i;
  282. if (i != nlimbs) {
  283. pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
  284. nlimbs);
  285. BUG();
  286. }
  287. return 0;
  288. }
  289. EXPORT_SYMBOL_GPL(mpi_set_buffer);
  290. /**
  291. * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
  292. *
  293. * This function works in the same way as the mpi_read_buffer, but it
  294. * takes an sgl instead of u8 * buf.
  295. *
  296. * @a: a multi precision integer
  297. * @sgl: scatterlist to write to. Needs to be at least
  298. * mpi_get_size(a) long.
  299. * @nbytes: in/out param - it has the be set to the maximum number of
  300. * bytes that can be written to sgl. This has to be at least
  301. * the size of the integer a. On return it receives the actual
  302. * length of the data written.
  303. * @sign: if not NULL, it will be set to the sign of a.
  304. *
  305. * Return: 0 on success or error code in case of error
  306. */
  307. int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
  308. int *sign)
  309. {
  310. u8 *p, *p2;
  311. mpi_limb_t alimb, alimb2;
  312. unsigned int n = mpi_get_size(a);
  313. int i, x, y = 0, lzeros = 0, buf_len;
  314. if (!nbytes || *nbytes < n)
  315. return -EINVAL;
  316. if (sign)
  317. *sign = a->sign;
  318. p = (void *)&a->d[a->nlimbs] - 1;
  319. for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) {
  320. if (!*p)
  321. lzeros++;
  322. else
  323. break;
  324. }
  325. *nbytes = n - lzeros;
  326. buf_len = sgl->length;
  327. p2 = sg_virt(sgl);
  328. for (i = a->nlimbs - 1; i >= 0; i--) {
  329. alimb = a->d[i];
  330. p = (u8 *)&alimb2;
  331. #if BYTES_PER_MPI_LIMB == 4
  332. *p++ = alimb >> 24;
  333. *p++ = alimb >> 16;
  334. *p++ = alimb >> 8;
  335. *p++ = alimb;
  336. #elif BYTES_PER_MPI_LIMB == 8
  337. *p++ = alimb >> 56;
  338. *p++ = alimb >> 48;
  339. *p++ = alimb >> 40;
  340. *p++ = alimb >> 32;
  341. *p++ = alimb >> 24;
  342. *p++ = alimb >> 16;
  343. *p++ = alimb >> 8;
  344. *p++ = alimb;
  345. #else
  346. #error please implement for this limb size.
  347. #endif
  348. if (lzeros > 0) {
  349. if (lzeros >= sizeof(alimb)) {
  350. p -= sizeof(alimb);
  351. continue;
  352. } else {
  353. mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
  354. mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
  355. + lzeros;
  356. *limb1 = *limb2;
  357. p -= lzeros;
  358. y = lzeros;
  359. }
  360. lzeros -= sizeof(alimb);
  361. }
  362. p = p - (sizeof(alimb) - y);
  363. for (x = 0; x < sizeof(alimb) - y; x++) {
  364. if (!buf_len) {
  365. sgl = sg_next(sgl);
  366. if (!sgl)
  367. return -EINVAL;
  368. buf_len = sgl->length;
  369. p2 = sg_virt(sgl);
  370. }
  371. *p2++ = *p++;
  372. buf_len--;
  373. }
  374. y = 0;
  375. }
  376. return 0;
  377. }
  378. EXPORT_SYMBOL_GPL(mpi_write_to_sgl);
  379. /*
  380. * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
  381. * data from the sgl
  382. *
  383. * This function works in the same way as the mpi_read_raw_data, but it
  384. * takes an sgl instead of void * buffer. i.e. it allocates
  385. * a new MPI and reads the content of the sgl to the MPI.
  386. *
  387. * @sgl: scatterlist to read from
  388. * @len: number of bytes to read
  389. *
  390. * Return: Pointer to a new MPI or NULL on error
  391. */
  392. MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len)
  393. {
  394. struct scatterlist *sg;
  395. int x, i, j, z, lzeros, ents;
  396. unsigned int nbits, nlimbs, nbytes;
  397. mpi_limb_t a;
  398. MPI val = NULL;
  399. lzeros = 0;
  400. ents = sg_nents(sgl);
  401. for_each_sg(sgl, sg, ents, i) {
  402. const u8 *buff = sg_virt(sg);
  403. int len = sg->length;
  404. while (len && !*buff) {
  405. lzeros++;
  406. len--;
  407. buff++;
  408. }
  409. if (len && *buff)
  410. break;
  411. ents--;
  412. lzeros = 0;
  413. }
  414. sgl = sg;
  415. if (!ents)
  416. nbytes = 0;
  417. else
  418. nbytes = len - lzeros;
  419. nbits = nbytes * 8;
  420. if (nbits > MAX_EXTERN_MPI_BITS) {
  421. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  422. return NULL;
  423. }
  424. if (nbytes > 0)
  425. nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros));
  426. else
  427. nbits = 0;
  428. nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
  429. val = mpi_alloc(nlimbs);
  430. if (!val)
  431. return NULL;
  432. val->nbits = nbits;
  433. val->sign = 0;
  434. val->nlimbs = nlimbs;
  435. if (nbytes == 0)
  436. return val;
  437. j = nlimbs - 1;
  438. a = 0;
  439. z = 0;
  440. x = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  441. x %= BYTES_PER_MPI_LIMB;
  442. for_each_sg(sgl, sg, ents, i) {
  443. const u8 *buffer = sg_virt(sg) + lzeros;
  444. int len = sg->length - lzeros;
  445. int buf_shift = x;
  446. if (sg_is_last(sg) && (len % BYTES_PER_MPI_LIMB))
  447. len += BYTES_PER_MPI_LIMB - (len % BYTES_PER_MPI_LIMB);
  448. for (; x < len + buf_shift; x++) {
  449. a <<= 8;
  450. a |= *buffer++;
  451. if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) {
  452. val->d[j--] = a;
  453. a = 0;
  454. }
  455. }
  456. z += x;
  457. x = 0;
  458. lzeros = 0;
  459. }
  460. return val;
  461. }
  462. EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);