mpicoder.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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_SCAN_BYTES (16*1024*1024)
  27. #define MAX_EXTERN_MPI_BITS 16384
  28. /**
  29. * mpi_read_raw_data - Read a raw byte stream as a positive integer
  30. * @xbuffer: The data to read
  31. * @nbytes: The amount of data to read
  32. */
  33. MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes)
  34. {
  35. const uint8_t *buffer = xbuffer;
  36. int i, j;
  37. unsigned nbits, nlimbs;
  38. mpi_limb_t a;
  39. MPI val = NULL;
  40. while (nbytes > 0 && buffer[0] == 0) {
  41. buffer++;
  42. nbytes--;
  43. }
  44. nbits = nbytes * 8;
  45. if (nbits > MAX_EXTERN_MPI_BITS) {
  46. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  47. return NULL;
  48. }
  49. if (nbytes > 0)
  50. nbits -= count_leading_zeros(buffer[0]) - (BITS_PER_LONG - 8);
  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. unsigned int nbits, nbytes;
  78. MPI val;
  79. if (*ret_nread < 2)
  80. return ERR_PTR(-EINVAL);
  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. return ERR_PTR(-EINVAL);
  85. }
  86. nbytes = DIV_ROUND_UP(nbits, 8);
  87. if (nbytes + 2 > *ret_nread) {
  88. pr_info("MPI: mpi larger than buffer nbytes=%u ret_nread=%u\n",
  89. nbytes, *ret_nread);
  90. return ERR_PTR(-EINVAL);
  91. }
  92. val = mpi_read_raw_data(buffer + 2, nbytes);
  93. if (!val)
  94. return ERR_PTR(-ENOMEM);
  95. *ret_nread = nbytes + 2;
  96. return val;
  97. }
  98. EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
  99. /****************
  100. * Fill the mpi VAL from the hex string in STR.
  101. */
  102. int mpi_fromstr(MPI val, const char *str)
  103. {
  104. int sign = 0;
  105. int prepend_zero = 0;
  106. int i, j, c, c1, c2;
  107. unsigned int nbits, nbytes, nlimbs;
  108. mpi_limb_t a;
  109. if (*str == '-') {
  110. sign = 1;
  111. str++;
  112. }
  113. /* Skip optional hex prefix. */
  114. if (*str == '0' && str[1] == 'x')
  115. str += 2;
  116. nbits = strlen(str);
  117. if (nbits > MAX_EXTERN_SCAN_BYTES) {
  118. mpi_clear(val);
  119. return -EINVAL;
  120. }
  121. nbits *= 4;
  122. if ((nbits % 8))
  123. prepend_zero = 1;
  124. nbytes = (nbits+7) / 8;
  125. nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
  126. if (val->alloced < nlimbs)
  127. mpi_resize(val, nlimbs);
  128. i = BYTES_PER_MPI_LIMB - (nbytes % BYTES_PER_MPI_LIMB);
  129. i %= BYTES_PER_MPI_LIMB;
  130. j = val->nlimbs = nlimbs;
  131. val->sign = sign;
  132. for (; j > 0; j--) {
  133. a = 0;
  134. for (; i < BYTES_PER_MPI_LIMB; i++) {
  135. if (prepend_zero) {
  136. c1 = '0';
  137. prepend_zero = 0;
  138. } else
  139. c1 = *str++;
  140. if (!c1) {
  141. mpi_clear(val);
  142. return -EINVAL;
  143. }
  144. c2 = *str++;
  145. if (!c2) {
  146. mpi_clear(val);
  147. return -EINVAL;
  148. }
  149. if (c1 >= '0' && c1 <= '9')
  150. c = c1 - '0';
  151. else if (c1 >= 'a' && c1 <= 'f')
  152. c = c1 - 'a' + 10;
  153. else if (c1 >= 'A' && c1 <= 'F')
  154. c = c1 - 'A' + 10;
  155. else {
  156. mpi_clear(val);
  157. return -EINVAL;
  158. }
  159. c <<= 4;
  160. if (c2 >= '0' && c2 <= '9')
  161. c |= c2 - '0';
  162. else if (c2 >= 'a' && c2 <= 'f')
  163. c |= c2 - 'a' + 10;
  164. else if (c2 >= 'A' && c2 <= 'F')
  165. c |= c2 - 'A' + 10;
  166. else {
  167. mpi_clear(val);
  168. return -EINVAL;
  169. }
  170. a <<= 8;
  171. a |= c;
  172. }
  173. i = 0;
  174. val->d[j-1] = a;
  175. }
  176. return 0;
  177. }
  178. EXPORT_SYMBOL_GPL(mpi_fromstr);
  179. MPI mpi_scanval(const char *string)
  180. {
  181. MPI a;
  182. a = mpi_alloc(0);
  183. if (!a)
  184. return NULL;
  185. if (mpi_fromstr(a, string)) {
  186. mpi_free(a);
  187. return NULL;
  188. }
  189. mpi_normalize(a);
  190. return a;
  191. }
  192. EXPORT_SYMBOL_GPL(mpi_scanval);
  193. static int count_lzeros(MPI a)
  194. {
  195. mpi_limb_t alimb;
  196. int i, lzeros = 0;
  197. for (i = a->nlimbs - 1; i >= 0; i--) {
  198. alimb = a->d[i];
  199. if (alimb == 0) {
  200. lzeros += sizeof(mpi_limb_t);
  201. } else {
  202. lzeros += count_leading_zeros(alimb) / 8;
  203. break;
  204. }
  205. }
  206. return lzeros;
  207. }
  208. /**
  209. * mpi_read_buffer() - read MPI to a buffer provided by user (msb first)
  210. *
  211. * @a: a multi precision integer
  212. * @buf: buffer to which the output will be written to. Needs to be at
  213. * least mpi_get_size(a) long.
  214. * @buf_len: size of the buf.
  215. * @nbytes: receives the actual length of the data written on success and
  216. * the data to-be-written on -EOVERFLOW in case buf_len was too
  217. * small.
  218. * @sign: if not NULL, it will be set to the sign of a.
  219. *
  220. * Return: 0 on success or error code in case of error
  221. */
  222. int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
  223. int *sign)
  224. {
  225. uint8_t *p;
  226. #if BYTES_PER_MPI_LIMB == 4
  227. __be32 alimb;
  228. #elif BYTES_PER_MPI_LIMB == 8
  229. __be64 alimb;
  230. #else
  231. #error please implement for this limb size.
  232. #endif
  233. unsigned int n = mpi_get_size(a);
  234. int i, lzeros;
  235. if (!buf || !nbytes)
  236. return -EINVAL;
  237. if (sign)
  238. *sign = a->sign;
  239. lzeros = count_lzeros(a);
  240. if (buf_len < n - lzeros) {
  241. *nbytes = n - lzeros;
  242. return -EOVERFLOW;
  243. }
  244. p = buf;
  245. *nbytes = n - lzeros;
  246. for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
  247. lzeros %= BYTES_PER_MPI_LIMB;
  248. i >= 0; i--) {
  249. #if BYTES_PER_MPI_LIMB == 4
  250. alimb = cpu_to_be32(a->d[i]);
  251. #elif BYTES_PER_MPI_LIMB == 8
  252. alimb = cpu_to_be64(a->d[i]);
  253. #else
  254. #error please implement for this limb size.
  255. #endif
  256. memcpy(p, (u8 *)&alimb + lzeros, BYTES_PER_MPI_LIMB - lzeros);
  257. p += BYTES_PER_MPI_LIMB - lzeros;
  258. lzeros = 0;
  259. }
  260. return 0;
  261. }
  262. EXPORT_SYMBOL_GPL(mpi_read_buffer);
  263. /*
  264. * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first).
  265. * Caller must free the return string.
  266. * This function does return a 0 byte buffer with nbytes set to zero if the
  267. * value of A is zero.
  268. *
  269. * @a: a multi precision integer.
  270. * @nbytes: receives the length of this buffer.
  271. * @sign: if not NULL, it will be set to the sign of the a.
  272. *
  273. * Return: Pointer to MPI buffer or NULL on error
  274. */
  275. void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
  276. {
  277. uint8_t *buf;
  278. unsigned int n;
  279. int ret;
  280. if (!nbytes)
  281. return NULL;
  282. n = mpi_get_size(a);
  283. if (!n)
  284. n++;
  285. buf = kmalloc(n, GFP_KERNEL);
  286. if (!buf)
  287. return NULL;
  288. ret = mpi_read_buffer(a, buf, n, nbytes, sign);
  289. if (ret) {
  290. kfree(buf);
  291. return NULL;
  292. }
  293. return buf;
  294. }
  295. EXPORT_SYMBOL_GPL(mpi_get_buffer);
  296. /**
  297. * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
  298. *
  299. * This function works in the same way as the mpi_read_buffer, but it
  300. * takes an sgl instead of u8 * buf.
  301. *
  302. * @a: a multi precision integer
  303. * @sgl: scatterlist to write to. Needs to be at least
  304. * mpi_get_size(a) long.
  305. * @nbytes: the number of bytes to write. Leading bytes will be
  306. * filled with zero.
  307. * @sign: if not NULL, it will be set to the sign of a.
  308. *
  309. * Return: 0 on success or error code in case of error
  310. */
  311. int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned nbytes,
  312. int *sign)
  313. {
  314. u8 *p, *p2;
  315. #if BYTES_PER_MPI_LIMB == 4
  316. __be32 alimb;
  317. #elif BYTES_PER_MPI_LIMB == 8
  318. __be64 alimb;
  319. #else
  320. #error please implement for this limb size.
  321. #endif
  322. unsigned int n = mpi_get_size(a);
  323. struct sg_mapping_iter miter;
  324. int i, x, buf_len;
  325. int nents;
  326. if (sign)
  327. *sign = a->sign;
  328. if (nbytes < n)
  329. return -EOVERFLOW;
  330. nents = sg_nents_for_len(sgl, nbytes);
  331. if (nents < 0)
  332. return -EINVAL;
  333. sg_miter_start(&miter, sgl, nents, SG_MITER_ATOMIC | SG_MITER_TO_SG);
  334. sg_miter_next(&miter);
  335. buf_len = miter.length;
  336. p2 = miter.addr;
  337. while (nbytes > n) {
  338. i = min_t(unsigned, nbytes - n, buf_len);
  339. memset(p2, 0, i);
  340. p2 += i;
  341. nbytes -= i;
  342. buf_len -= i;
  343. if (!buf_len) {
  344. sg_miter_next(&miter);
  345. buf_len = miter.length;
  346. p2 = miter.addr;
  347. }
  348. }
  349. for (i = a->nlimbs - 1; i >= 0; i--) {
  350. #if BYTES_PER_MPI_LIMB == 4
  351. alimb = a->d[i] ? cpu_to_be32(a->d[i]) : 0;
  352. #elif BYTES_PER_MPI_LIMB == 8
  353. alimb = a->d[i] ? cpu_to_be64(a->d[i]) : 0;
  354. #else
  355. #error please implement for this limb size.
  356. #endif
  357. p = (u8 *)&alimb;
  358. for (x = 0; x < sizeof(alimb); x++) {
  359. *p2++ = *p++;
  360. if (!--buf_len) {
  361. sg_miter_next(&miter);
  362. buf_len = miter.length;
  363. p2 = miter.addr;
  364. }
  365. }
  366. }
  367. sg_miter_stop(&miter);
  368. return 0;
  369. }
  370. EXPORT_SYMBOL_GPL(mpi_write_to_sgl);
  371. /*
  372. * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
  373. * data from the sgl
  374. *
  375. * This function works in the same way as the mpi_read_raw_data, but it
  376. * takes an sgl instead of void * buffer. i.e. it allocates
  377. * a new MPI and reads the content of the sgl to the MPI.
  378. *
  379. * @sgl: scatterlist to read from
  380. * @nbytes: number of bytes to read
  381. *
  382. * Return: Pointer to a new MPI or NULL on error
  383. */
  384. MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes)
  385. {
  386. struct sg_mapping_iter miter;
  387. unsigned int nbits, nlimbs;
  388. int x, j, z, lzeros, ents;
  389. unsigned int len;
  390. const u8 *buff;
  391. mpi_limb_t a;
  392. MPI val = NULL;
  393. ents = sg_nents_for_len(sgl, nbytes);
  394. if (ents < 0)
  395. return NULL;
  396. sg_miter_start(&miter, sgl, ents, SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  397. lzeros = 0;
  398. len = 0;
  399. while (nbytes > 0) {
  400. while (len && !*buff) {
  401. lzeros++;
  402. len--;
  403. buff++;
  404. }
  405. if (len && *buff)
  406. break;
  407. sg_miter_next(&miter);
  408. buff = miter.addr;
  409. len = miter.length;
  410. nbytes -= lzeros;
  411. lzeros = 0;
  412. }
  413. miter.consumed = lzeros;
  414. nbytes -= lzeros;
  415. nbits = nbytes * 8;
  416. if (nbits > MAX_EXTERN_MPI_BITS) {
  417. sg_miter_stop(&miter);
  418. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  419. return NULL;
  420. }
  421. if (nbytes > 0)
  422. nbits -= count_leading_zeros(*buff) - (BITS_PER_LONG - 8);
  423. sg_miter_stop(&miter);
  424. nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
  425. val = mpi_alloc(nlimbs);
  426. if (!val)
  427. return NULL;
  428. val->nbits = nbits;
  429. val->sign = 0;
  430. val->nlimbs = nlimbs;
  431. if (nbytes == 0)
  432. return val;
  433. j = nlimbs - 1;
  434. a = 0;
  435. z = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  436. z %= BYTES_PER_MPI_LIMB;
  437. while (sg_miter_next(&miter)) {
  438. buff = miter.addr;
  439. len = min_t(unsigned, miter.length, nbytes);
  440. nbytes -= len;
  441. for (x = 0; x < len; x++) {
  442. a <<= 8;
  443. a |= *buff++;
  444. if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) {
  445. val->d[j--] = a;
  446. a = 0;
  447. }
  448. }
  449. z += x;
  450. }
  451. return val;
  452. }
  453. EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);
  454. /* Perform a two's complement operation on buffer P of size N bytes. */
  455. static void twocompl(unsigned char *p, unsigned int n)
  456. {
  457. int i;
  458. for (i = n-1; i >= 0 && !p[i]; i--)
  459. ;
  460. if (i >= 0) {
  461. if ((p[i] & 0x01))
  462. p[i] = (((p[i] ^ 0xfe) | 0x01) & 0xff);
  463. else if ((p[i] & 0x02))
  464. p[i] = (((p[i] ^ 0xfc) | 0x02) & 0xfe);
  465. else if ((p[i] & 0x04))
  466. p[i] = (((p[i] ^ 0xf8) | 0x04) & 0xfc);
  467. else if ((p[i] & 0x08))
  468. p[i] = (((p[i] ^ 0xf0) | 0x08) & 0xf8);
  469. else if ((p[i] & 0x10))
  470. p[i] = (((p[i] ^ 0xe0) | 0x10) & 0xf0);
  471. else if ((p[i] & 0x20))
  472. p[i] = (((p[i] ^ 0xc0) | 0x20) & 0xe0);
  473. else if ((p[i] & 0x40))
  474. p[i] = (((p[i] ^ 0x80) | 0x40) & 0xc0);
  475. else
  476. p[i] = 0x80;
  477. for (i--; i >= 0; i--)
  478. p[i] ^= 0xff;
  479. }
  480. }
  481. int mpi_print(enum gcry_mpi_format format, unsigned char *buffer,
  482. size_t buflen, size_t *nwritten, MPI a)
  483. {
  484. unsigned int nbits = mpi_get_nbits(a);
  485. size_t len;
  486. size_t dummy_nwritten;
  487. int negative;
  488. if (!nwritten)
  489. nwritten = &dummy_nwritten;
  490. /* Libgcrypt does no always care to set clear the sign if the value
  491. * is 0. For printing this is a bit of a surprise, in particular
  492. * because if some of the formats don't support negative numbers but
  493. * should be able to print a zero. Thus we need this extra test
  494. * for a negative number.
  495. */
  496. if (a->sign && mpi_cmp_ui(a, 0))
  497. negative = 1;
  498. else
  499. negative = 0;
  500. len = buflen;
  501. *nwritten = 0;
  502. if (format == GCRYMPI_FMT_STD) {
  503. unsigned char *tmp;
  504. int extra = 0;
  505. unsigned int n;
  506. tmp = mpi_get_buffer(a, &n, NULL);
  507. if (!tmp)
  508. return -EINVAL;
  509. if (negative) {
  510. twocompl(tmp, n);
  511. if (!(*tmp & 0x80)) {
  512. /* Need to extend the sign. */
  513. n++;
  514. extra = 2;
  515. }
  516. } else if (n && (*tmp & 0x80)) {
  517. /* Positive but the high bit of the returned buffer is set.
  518. * Thus we need to print an extra leading 0x00 so that the
  519. * output is interpreted as a positive number.
  520. */
  521. n++;
  522. extra = 1;
  523. }
  524. if (buffer && n > len) {
  525. /* The provided buffer is too short. */
  526. kfree(tmp);
  527. return -E2BIG;
  528. }
  529. if (buffer) {
  530. unsigned char *s = buffer;
  531. if (extra == 1)
  532. *s++ = 0;
  533. else if (extra)
  534. *s++ = 0xff;
  535. memcpy(s, tmp, n-!!extra);
  536. }
  537. kfree(tmp);
  538. *nwritten = n;
  539. return 0;
  540. } else if (format == GCRYMPI_FMT_USG) {
  541. unsigned int n = (nbits + 7)/8;
  542. /* Note: We ignore the sign for this format. */
  543. /* FIXME: for performance reasons we should put this into
  544. * mpi_aprint because we can then use the buffer directly.
  545. */
  546. if (buffer && n > len)
  547. return -E2BIG;
  548. if (buffer) {
  549. unsigned char *tmp;
  550. tmp = mpi_get_buffer(a, &n, NULL);
  551. if (!tmp)
  552. return -EINVAL;
  553. memcpy(buffer, tmp, n);
  554. kfree(tmp);
  555. }
  556. *nwritten = n;
  557. return 0;
  558. } else if (format == GCRYMPI_FMT_PGP) {
  559. unsigned int n = (nbits + 7)/8;
  560. /* The PGP format can only handle unsigned integers. */
  561. if (negative)
  562. return -EINVAL;
  563. if (buffer && n+2 > len)
  564. return -E2BIG;
  565. if (buffer) {
  566. unsigned char *tmp;
  567. unsigned char *s = buffer;
  568. s[0] = nbits >> 8;
  569. s[1] = nbits;
  570. tmp = mpi_get_buffer(a, &n, NULL);
  571. if (!tmp)
  572. return -EINVAL;
  573. memcpy(s+2, tmp, n);
  574. kfree(tmp);
  575. }
  576. *nwritten = n+2;
  577. return 0;
  578. } else if (format == GCRYMPI_FMT_SSH) {
  579. unsigned char *tmp;
  580. int extra = 0;
  581. unsigned int n;
  582. tmp = mpi_get_buffer(a, &n, NULL);
  583. if (!tmp)
  584. return -EINVAL;
  585. if (negative) {
  586. twocompl(tmp, n);
  587. if (!(*tmp & 0x80)) {
  588. /* Need to extend the sign. */
  589. n++;
  590. extra = 2;
  591. }
  592. } else if (n && (*tmp & 0x80)) {
  593. n++;
  594. extra = 1;
  595. }
  596. if (buffer && n+4 > len) {
  597. kfree(tmp);
  598. return -E2BIG;
  599. }
  600. if (buffer) {
  601. unsigned char *s = buffer;
  602. *s++ = n >> 24;
  603. *s++ = n >> 16;
  604. *s++ = n >> 8;
  605. *s++ = n;
  606. if (extra == 1)
  607. *s++ = 0;
  608. else if (extra)
  609. *s++ = 0xff;
  610. memcpy(s, tmp, n-!!extra);
  611. }
  612. kfree(tmp);
  613. *nwritten = 4+n;
  614. return 0;
  615. } else if (format == GCRYMPI_FMT_HEX) {
  616. unsigned char *tmp;
  617. int i;
  618. int extra = 0;
  619. unsigned int n = 0;
  620. tmp = mpi_get_buffer(a, &n, NULL);
  621. if (!tmp)
  622. return -EINVAL;
  623. if (!n || (*tmp & 0x80))
  624. extra = 2;
  625. if (buffer && 2*n + extra + negative + 1 > len) {
  626. kfree(tmp);
  627. return -E2BIG;
  628. }
  629. if (buffer) {
  630. unsigned char *s = buffer;
  631. if (negative)
  632. *s++ = '-';
  633. if (extra) {
  634. *s++ = '0';
  635. *s++ = '0';
  636. }
  637. for (i = 0; i < n; i++) {
  638. unsigned int c = tmp[i];
  639. *s++ = (c >> 4) < 10 ? '0'+(c>>4) : 'A'+(c>>4)-10;
  640. c &= 15;
  641. *s++ = c < 10 ? '0'+c : 'A'+c-10;
  642. }
  643. *s++ = 0;
  644. *nwritten = s - buffer;
  645. } else {
  646. *nwritten = 2*n + extra + negative + 1;
  647. }
  648. kfree(tmp);
  649. return 0;
  650. } else
  651. return -EINVAL;
  652. }
  653. EXPORT_SYMBOL_GPL(mpi_print);