mpicoder.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "mpi-internal.h"
  21. #define MAX_EXTERN_MPI_BITS 16384
  22. MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
  23. {
  24. const uint8_t *buffer = xbuffer;
  25. int i, j;
  26. unsigned nbits, nbytes, nlimbs, nread = 0;
  27. mpi_limb_t a;
  28. MPI val = NULL;
  29. if (*ret_nread < 2)
  30. goto leave;
  31. nbits = buffer[0] << 8 | buffer[1];
  32. if (nbits > MAX_EXTERN_MPI_BITS) {
  33. pr_info("MPI: mpi too large (%u bits)\n", nbits);
  34. goto leave;
  35. }
  36. buffer += 2;
  37. nread = 2;
  38. nbytes = (nbits + 7) / 8;
  39. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  40. val = mpi_alloc(nlimbs);
  41. if (!val)
  42. return NULL;
  43. i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  44. i %= BYTES_PER_MPI_LIMB;
  45. val->nbits = nbits;
  46. j = val->nlimbs = nlimbs;
  47. val->sign = 0;
  48. for (; j > 0; j--) {
  49. a = 0;
  50. for (; i < BYTES_PER_MPI_LIMB; i++) {
  51. if (++nread > *ret_nread) {
  52. printk
  53. ("MPI: mpi larger than buffer nread=%d ret_nread=%d\n",
  54. nread, *ret_nread);
  55. goto leave;
  56. }
  57. a <<= 8;
  58. a |= *buffer++;
  59. }
  60. i = 0;
  61. val->d[j - 1] = a;
  62. }
  63. leave:
  64. *ret_nread = nread;
  65. return val;
  66. }
  67. EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
  68. /****************
  69. * Make an mpi from a character string.
  70. */
  71. int mpi_fromstr(MPI val, const char *str)
  72. {
  73. int hexmode = 0, sign = 0, prepend_zero = 0, i, j, c, c1, c2;
  74. unsigned nbits, nbytes, nlimbs;
  75. mpi_limb_t a;
  76. if (*str == '-') {
  77. sign = 1;
  78. str++;
  79. }
  80. if (*str == '0' && str[1] == 'x')
  81. hexmode = 1;
  82. else
  83. return -EINVAL; /* other bases are not yet supported */
  84. str += 2;
  85. nbits = strlen(str) * 4;
  86. if (nbits % 8)
  87. prepend_zero = 1;
  88. nbytes = (nbits + 7) / 8;
  89. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  90. if (val->alloced < nlimbs)
  91. if (!mpi_resize(val, nlimbs))
  92. return -ENOMEM;
  93. i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
  94. i %= BYTES_PER_MPI_LIMB;
  95. j = val->nlimbs = nlimbs;
  96. val->sign = sign;
  97. for (; j > 0; j--) {
  98. a = 0;
  99. for (; i < BYTES_PER_MPI_LIMB; i++) {
  100. if (prepend_zero) {
  101. c1 = '0';
  102. prepend_zero = 0;
  103. } else
  104. c1 = *str++;
  105. assert(c1);
  106. c2 = *str++;
  107. assert(c2);
  108. if (c1 >= '0' && c1 <= '9')
  109. c = c1 - '0';
  110. else if (c1 >= 'a' && c1 <= 'f')
  111. c = c1 - 'a' + 10;
  112. else if (c1 >= 'A' && c1 <= 'F')
  113. c = c1 - 'A' + 10;
  114. else {
  115. mpi_clear(val);
  116. return 1;
  117. }
  118. c <<= 4;
  119. if (c2 >= '0' && c2 <= '9')
  120. c |= c2 - '0';
  121. else if (c2 >= 'a' && c2 <= 'f')
  122. c |= c2 - 'a' + 10;
  123. else if (c2 >= 'A' && c2 <= 'F')
  124. c |= c2 - 'A' + 10;
  125. else {
  126. mpi_clear(val);
  127. return 1;
  128. }
  129. a <<= 8;
  130. a |= c;
  131. }
  132. i = 0;
  133. val->d[j - 1] = a;
  134. }
  135. return 0;
  136. }
  137. EXPORT_SYMBOL_GPL(mpi_fromstr);
  138. /****************
  139. * Return an allocated buffer with the MPI (msb first).
  140. * NBYTES receives the length of this buffer. Caller must free the
  141. * return string (This function does return a 0 byte buffer with NBYTES
  142. * set to zero if the value of A is zero. If sign is not NULL, it will
  143. * be set to the sign of the A.
  144. */
  145. void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
  146. {
  147. uint8_t *p, *buffer;
  148. mpi_limb_t alimb;
  149. int i;
  150. unsigned int n;
  151. if (sign)
  152. *sign = a->sign;
  153. *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
  154. if (!n)
  155. n++; /* avoid zero length allocation */
  156. p = buffer = kmalloc(n, GFP_KERNEL);
  157. if (!p)
  158. return NULL;
  159. for (i = a->nlimbs - 1; i >= 0; i--) {
  160. alimb = a->d[i];
  161. #if BYTES_PER_MPI_LIMB == 4
  162. *p++ = alimb >> 24;
  163. *p++ = alimb >> 16;
  164. *p++ = alimb >> 8;
  165. *p++ = alimb;
  166. #elif BYTES_PER_MPI_LIMB == 8
  167. *p++ = alimb >> 56;
  168. *p++ = alimb >> 48;
  169. *p++ = alimb >> 40;
  170. *p++ = alimb >> 32;
  171. *p++ = alimb >> 24;
  172. *p++ = alimb >> 16;
  173. *p++ = alimb >> 8;
  174. *p++ = alimb;
  175. #else
  176. #error please implement for this limb size.
  177. #endif
  178. }
  179. /* this is sub-optimal but we need to do the shift operation
  180. * because the caller has to free the returned buffer */
  181. for (p = buffer; !*p && *nbytes; p++, --*nbytes)
  182. ;
  183. if (p != buffer)
  184. memmove(buffer, p, *nbytes);
  185. return buffer;
  186. }
  187. EXPORT_SYMBOL_GPL(mpi_get_buffer);
  188. /****************
  189. * Use BUFFER to update MPI.
  190. */
  191. int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
  192. {
  193. const uint8_t *buffer = xbuffer, *p;
  194. mpi_limb_t alimb;
  195. int nlimbs;
  196. int i;
  197. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  198. if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
  199. return -ENOMEM;
  200. a->sign = sign;
  201. for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) {
  202. #if BYTES_PER_MPI_LIMB == 4
  203. alimb = (mpi_limb_t) *p--;
  204. alimb |= (mpi_limb_t) *p-- << 8;
  205. alimb |= (mpi_limb_t) *p-- << 16;
  206. alimb |= (mpi_limb_t) *p-- << 24;
  207. #elif BYTES_PER_MPI_LIMB == 8
  208. alimb = (mpi_limb_t) *p--;
  209. alimb |= (mpi_limb_t) *p-- << 8;
  210. alimb |= (mpi_limb_t) *p-- << 16;
  211. alimb |= (mpi_limb_t) *p-- << 24;
  212. alimb |= (mpi_limb_t) *p-- << 32;
  213. alimb |= (mpi_limb_t) *p-- << 40;
  214. alimb |= (mpi_limb_t) *p-- << 48;
  215. alimb |= (mpi_limb_t) *p-- << 56;
  216. #else
  217. #error please implement for this limb size.
  218. #endif
  219. a->d[i++] = alimb;
  220. }
  221. if (p >= buffer) {
  222. #if BYTES_PER_MPI_LIMB == 4
  223. alimb = *p--;
  224. if (p >= buffer)
  225. alimb |= (mpi_limb_t) *p-- << 8;
  226. if (p >= buffer)
  227. alimb |= (mpi_limb_t) *p-- << 16;
  228. if (p >= buffer)
  229. alimb |= (mpi_limb_t) *p-- << 24;
  230. #elif BYTES_PER_MPI_LIMB == 8
  231. alimb = (mpi_limb_t) *p--;
  232. if (p >= buffer)
  233. alimb |= (mpi_limb_t) *p-- << 8;
  234. if (p >= buffer)
  235. alimb |= (mpi_limb_t) *p-- << 16;
  236. if (p >= buffer)
  237. alimb |= (mpi_limb_t) *p-- << 24;
  238. if (p >= buffer)
  239. alimb |= (mpi_limb_t) *p-- << 32;
  240. if (p >= buffer)
  241. alimb |= (mpi_limb_t) *p-- << 40;
  242. if (p >= buffer)
  243. alimb |= (mpi_limb_t) *p-- << 48;
  244. if (p >= buffer)
  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. a->nlimbs = i;
  252. if (i != nlimbs) {
  253. pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
  254. nlimbs);
  255. BUG();
  256. }
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(mpi_set_buffer);