mpicoder.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <asm-generic/bitops/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 = (nbytes + BYTES_PER_MPI_LIMB - 1) / 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 = (nbits + 7) / 8;
  89. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / 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. * Return an allocated buffer with the MPI (msb first).
  120. * NBYTES receives the length of this buffer. Caller must free the
  121. * return string (This function does return a 0 byte buffer with NBYTES
  122. * set to zero if the value of A is zero. If sign is not NULL, it will
  123. * be set to the sign of the A.
  124. */
  125. void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
  126. {
  127. uint8_t *p, *buffer;
  128. mpi_limb_t alimb;
  129. int i;
  130. unsigned int n;
  131. if (sign)
  132. *sign = a->sign;
  133. *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
  134. if (!n)
  135. n++; /* avoid zero length allocation */
  136. p = buffer = kmalloc(n, GFP_KERNEL);
  137. if (!p)
  138. return NULL;
  139. for (i = a->nlimbs - 1; i >= 0; i--) {
  140. alimb = a->d[i];
  141. #if BYTES_PER_MPI_LIMB == 4
  142. *p++ = alimb >> 24;
  143. *p++ = alimb >> 16;
  144. *p++ = alimb >> 8;
  145. *p++ = alimb;
  146. #elif BYTES_PER_MPI_LIMB == 8
  147. *p++ = alimb >> 56;
  148. *p++ = alimb >> 48;
  149. *p++ = alimb >> 40;
  150. *p++ = alimb >> 32;
  151. *p++ = alimb >> 24;
  152. *p++ = alimb >> 16;
  153. *p++ = alimb >> 8;
  154. *p++ = alimb;
  155. #else
  156. #error please implement for this limb size.
  157. #endif
  158. }
  159. /* this is sub-optimal but we need to do the shift operation
  160. * because the caller has to free the returned buffer */
  161. for (p = buffer; !*p && *nbytes; p++, --*nbytes)
  162. ;
  163. if (p != buffer)
  164. memmove(buffer, p, *nbytes);
  165. return buffer;
  166. }
  167. EXPORT_SYMBOL_GPL(mpi_get_buffer);
  168. /****************
  169. * Use BUFFER to update MPI.
  170. */
  171. int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
  172. {
  173. const uint8_t *buffer = xbuffer, *p;
  174. mpi_limb_t alimb;
  175. int nlimbs;
  176. int i;
  177. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  178. if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
  179. return -ENOMEM;
  180. a->sign = sign;
  181. for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) {
  182. #if BYTES_PER_MPI_LIMB == 4
  183. alimb = (mpi_limb_t) *p--;
  184. alimb |= (mpi_limb_t) *p-- << 8;
  185. alimb |= (mpi_limb_t) *p-- << 16;
  186. alimb |= (mpi_limb_t) *p-- << 24;
  187. #elif BYTES_PER_MPI_LIMB == 8
  188. alimb = (mpi_limb_t) *p--;
  189. alimb |= (mpi_limb_t) *p-- << 8;
  190. alimb |= (mpi_limb_t) *p-- << 16;
  191. alimb |= (mpi_limb_t) *p-- << 24;
  192. alimb |= (mpi_limb_t) *p-- << 32;
  193. alimb |= (mpi_limb_t) *p-- << 40;
  194. alimb |= (mpi_limb_t) *p-- << 48;
  195. alimb |= (mpi_limb_t) *p-- << 56;
  196. #else
  197. #error please implement for this limb size.
  198. #endif
  199. a->d[i++] = alimb;
  200. }
  201. if (p >= buffer) {
  202. #if BYTES_PER_MPI_LIMB == 4
  203. alimb = *p--;
  204. if (p >= buffer)
  205. alimb |= (mpi_limb_t) *p-- << 8;
  206. if (p >= buffer)
  207. alimb |= (mpi_limb_t) *p-- << 16;
  208. if (p >= buffer)
  209. alimb |= (mpi_limb_t) *p-- << 24;
  210. #elif BYTES_PER_MPI_LIMB == 8
  211. alimb = (mpi_limb_t) *p--;
  212. if (p >= buffer)
  213. alimb |= (mpi_limb_t) *p-- << 8;
  214. if (p >= buffer)
  215. alimb |= (mpi_limb_t) *p-- << 16;
  216. if (p >= buffer)
  217. alimb |= (mpi_limb_t) *p-- << 24;
  218. if (p >= buffer)
  219. alimb |= (mpi_limb_t) *p-- << 32;
  220. if (p >= buffer)
  221. alimb |= (mpi_limb_t) *p-- << 40;
  222. if (p >= buffer)
  223. alimb |= (mpi_limb_t) *p-- << 48;
  224. if (p >= buffer)
  225. alimb |= (mpi_limb_t) *p-- << 56;
  226. #else
  227. #error please implement for this limb size.
  228. #endif
  229. a->d[i++] = alimb;
  230. }
  231. a->nlimbs = i;
  232. if (i != nlimbs) {
  233. pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
  234. nlimbs);
  235. BUG();
  236. }
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(mpi_set_buffer);