mpicoder.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 bufer provided by user (msb first)
  210. *
  211. * @a: a multi precision integer
  212. * @buf: bufer to which the output will be written to. Needs to be at
  213. * leaset 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 = miter.length;
  440. for (x = 0; x < len; x++) {
  441. a <<= 8;
  442. a |= *buff++;
  443. if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) {
  444. val->d[j--] = a;
  445. a = 0;
  446. }
  447. }
  448. z += x;
  449. }
  450. return val;
  451. }
  452. EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);
  453. /* Perform a two's complement operation on buffer P of size N bytes. */
  454. static void twocompl(unsigned char *p, unsigned int n)
  455. {
  456. int i;
  457. for (i = n-1; i >= 0 && !p[i]; i--)
  458. ;
  459. if (i >= 0) {
  460. if ((p[i] & 0x01))
  461. p[i] = (((p[i] ^ 0xfe) | 0x01) & 0xff);
  462. else if ((p[i] & 0x02))
  463. p[i] = (((p[i] ^ 0xfc) | 0x02) & 0xfe);
  464. else if ((p[i] & 0x04))
  465. p[i] = (((p[i] ^ 0xf8) | 0x04) & 0xfc);
  466. else if ((p[i] & 0x08))
  467. p[i] = (((p[i] ^ 0xf0) | 0x08) & 0xf8);
  468. else if ((p[i] & 0x10))
  469. p[i] = (((p[i] ^ 0xe0) | 0x10) & 0xf0);
  470. else if ((p[i] & 0x20))
  471. p[i] = (((p[i] ^ 0xc0) | 0x20) & 0xe0);
  472. else if ((p[i] & 0x40))
  473. p[i] = (((p[i] ^ 0x80) | 0x40) & 0xc0);
  474. else
  475. p[i] = 0x80;
  476. for (i--; i >= 0; i--)
  477. p[i] ^= 0xff;
  478. }
  479. }
  480. int mpi_print(enum gcry_mpi_format format, unsigned char *buffer,
  481. size_t buflen, size_t *nwritten, MPI a)
  482. {
  483. unsigned int nbits = mpi_get_nbits(a);
  484. size_t len;
  485. size_t dummy_nwritten;
  486. int negative;
  487. if (!nwritten)
  488. nwritten = &dummy_nwritten;
  489. /* Libgcrypt does no always care to set clear the sign if the value
  490. * is 0. For printing this is a bit of a surprise, in particular
  491. * because if some of the formats don't support negative numbers but
  492. * should be able to print a zero. Thus we need this extra test
  493. * for a negative number.
  494. */
  495. if (a->sign && mpi_cmp_ui(a, 0))
  496. negative = 1;
  497. else
  498. negative = 0;
  499. len = buflen;
  500. *nwritten = 0;
  501. if (format == GCRYMPI_FMT_STD) {
  502. unsigned char *tmp;
  503. int extra = 0;
  504. unsigned int n;
  505. tmp = mpi_get_buffer(a, &n, NULL);
  506. if (!tmp)
  507. return -EINVAL;
  508. if (negative) {
  509. twocompl(tmp, n);
  510. if (!(*tmp & 0x80)) {
  511. /* Need to extend the sign. */
  512. n++;
  513. extra = 2;
  514. }
  515. } else if (n && (*tmp & 0x80)) {
  516. /* Positive but the high bit of the returned buffer is set.
  517. * Thus we need to print an extra leading 0x00 so that the
  518. * output is interpreted as a positive number.
  519. */
  520. n++;
  521. extra = 1;
  522. }
  523. if (buffer && n > len) {
  524. /* The provided buffer is too short. */
  525. kfree(tmp);
  526. return -E2BIG;
  527. }
  528. if (buffer) {
  529. unsigned char *s = buffer;
  530. if (extra == 1)
  531. *s++ = 0;
  532. else if (extra)
  533. *s++ = 0xff;
  534. memcpy(s, tmp, n-!!extra);
  535. }
  536. kfree(tmp);
  537. *nwritten = n;
  538. return 0;
  539. } else if (format == GCRYMPI_FMT_USG) {
  540. unsigned int n = (nbits + 7)/8;
  541. /* Note: We ignore the sign for this format. */
  542. /* FIXME: for performance reasons we should put this into
  543. * mpi_aprint because we can then use the buffer directly.
  544. */
  545. if (buffer && n > len)
  546. return -E2BIG;
  547. if (buffer) {
  548. unsigned char *tmp;
  549. tmp = mpi_get_buffer(a, &n, NULL);
  550. if (!tmp)
  551. return -EINVAL;
  552. memcpy(buffer, tmp, n);
  553. kfree(tmp);
  554. }
  555. *nwritten = n;
  556. return 0;
  557. } else if (format == GCRYMPI_FMT_PGP) {
  558. unsigned int n = (nbits + 7)/8;
  559. /* The PGP format can only handle unsigned integers. */
  560. if (negative)
  561. return -EINVAL;
  562. if (buffer && n+2 > len)
  563. return -E2BIG;
  564. if (buffer) {
  565. unsigned char *tmp;
  566. unsigned char *s = buffer;
  567. s[0] = nbits >> 8;
  568. s[1] = nbits;
  569. tmp = mpi_get_buffer(a, &n, NULL);
  570. if (!tmp)
  571. return -EINVAL;
  572. memcpy(s+2, tmp, n);
  573. kfree(tmp);
  574. }
  575. *nwritten = n+2;
  576. return 0;
  577. } else if (format == GCRYMPI_FMT_SSH) {
  578. unsigned char *tmp;
  579. int extra = 0;
  580. unsigned int n;
  581. tmp = mpi_get_buffer(a, &n, NULL);
  582. if (!tmp)
  583. return -EINVAL;
  584. if (negative) {
  585. twocompl(tmp, n);
  586. if (!(*tmp & 0x80)) {
  587. /* Need to extend the sign. */
  588. n++;
  589. extra = 2;
  590. }
  591. } else if (n && (*tmp & 0x80)) {
  592. n++;
  593. extra = 1;
  594. }
  595. if (buffer && n+4 > len) {
  596. kfree(tmp);
  597. return -E2BIG;
  598. }
  599. if (buffer) {
  600. unsigned char *s = buffer;
  601. *s++ = n >> 24;
  602. *s++ = n >> 16;
  603. *s++ = n >> 8;
  604. *s++ = n;
  605. if (extra == 1)
  606. *s++ = 0;
  607. else if (extra)
  608. *s++ = 0xff;
  609. memcpy(s, tmp, n-!!extra);
  610. }
  611. kfree(tmp);
  612. *nwritten = 4+n;
  613. return 0;
  614. } else if (format == GCRYMPI_FMT_HEX) {
  615. unsigned char *tmp;
  616. int i;
  617. int extra = 0;
  618. unsigned int n = 0;
  619. tmp = mpi_get_buffer(a, &n, NULL);
  620. if (!tmp)
  621. return -EINVAL;
  622. if (!n || (*tmp & 0x80))
  623. extra = 2;
  624. if (buffer && 2*n + extra + negative + 1 > len) {
  625. kfree(tmp);
  626. return -E2BIG;
  627. }
  628. if (buffer) {
  629. unsigned char *s = buffer;
  630. if (negative)
  631. *s++ = '-';
  632. if (extra) {
  633. *s++ = '0';
  634. *s++ = '0';
  635. }
  636. for (i = 0; i < n; i++) {
  637. unsigned int c = tmp[i];
  638. *s++ = (c >> 4) < 10 ? '0'+(c>>4) : 'A'+(c>>4)-10;
  639. c &= 15;
  640. *s++ = c < 10 ? '0'+c : 'A'+c-10;
  641. }
  642. *s++ = 0;
  643. *nwritten = s - buffer;
  644. } else {
  645. *nwritten = 2*n + extra + negative + 1;
  646. }
  647. kfree(tmp);
  648. return 0;
  649. } else
  650. return -EINVAL;
  651. }
  652. EXPORT_SYMBOL_GPL(mpi_print);