mpicoder.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * Return an allocated buffer with the MPI (msb first).
  70. * NBYTES receives the length of this buffer. Caller must free the
  71. * return string (This function does return a 0 byte buffer with NBYTES
  72. * set to zero if the value of A is zero. If sign is not NULL, it will
  73. * be set to the sign of the A.
  74. */
  75. void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
  76. {
  77. uint8_t *p, *buffer;
  78. mpi_limb_t alimb;
  79. int i;
  80. unsigned int n;
  81. if (sign)
  82. *sign = a->sign;
  83. *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
  84. if (!n)
  85. n++; /* avoid zero length allocation */
  86. p = buffer = kmalloc(n, GFP_KERNEL);
  87. if (!p)
  88. return NULL;
  89. for (i = a->nlimbs - 1; i >= 0; i--) {
  90. alimb = a->d[i];
  91. #if BYTES_PER_MPI_LIMB == 4
  92. *p++ = alimb >> 24;
  93. *p++ = alimb >> 16;
  94. *p++ = alimb >> 8;
  95. *p++ = alimb;
  96. #elif BYTES_PER_MPI_LIMB == 8
  97. *p++ = alimb >> 56;
  98. *p++ = alimb >> 48;
  99. *p++ = alimb >> 40;
  100. *p++ = alimb >> 32;
  101. *p++ = alimb >> 24;
  102. *p++ = alimb >> 16;
  103. *p++ = alimb >> 8;
  104. *p++ = alimb;
  105. #else
  106. #error please implement for this limb size.
  107. #endif
  108. }
  109. /* this is sub-optimal but we need to do the shift operation
  110. * because the caller has to free the returned buffer */
  111. for (p = buffer; !*p && *nbytes; p++, --*nbytes)
  112. ;
  113. if (p != buffer)
  114. memmove(buffer, p, *nbytes);
  115. return buffer;
  116. }
  117. EXPORT_SYMBOL_GPL(mpi_get_buffer);
  118. /****************
  119. * Use BUFFER to update MPI.
  120. */
  121. int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
  122. {
  123. const uint8_t *buffer = xbuffer, *p;
  124. mpi_limb_t alimb;
  125. int nlimbs;
  126. int i;
  127. nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
  128. if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
  129. return -ENOMEM;
  130. a->sign = sign;
  131. for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) {
  132. #if BYTES_PER_MPI_LIMB == 4
  133. alimb = (mpi_limb_t) *p--;
  134. alimb |= (mpi_limb_t) *p-- << 8;
  135. alimb |= (mpi_limb_t) *p-- << 16;
  136. alimb |= (mpi_limb_t) *p-- << 24;
  137. #elif BYTES_PER_MPI_LIMB == 8
  138. alimb = (mpi_limb_t) *p--;
  139. alimb |= (mpi_limb_t) *p-- << 8;
  140. alimb |= (mpi_limb_t) *p-- << 16;
  141. alimb |= (mpi_limb_t) *p-- << 24;
  142. alimb |= (mpi_limb_t) *p-- << 32;
  143. alimb |= (mpi_limb_t) *p-- << 40;
  144. alimb |= (mpi_limb_t) *p-- << 48;
  145. alimb |= (mpi_limb_t) *p-- << 56;
  146. #else
  147. #error please implement for this limb size.
  148. #endif
  149. a->d[i++] = alimb;
  150. }
  151. if (p >= buffer) {
  152. #if BYTES_PER_MPI_LIMB == 4
  153. alimb = *p--;
  154. if (p >= buffer)
  155. alimb |= (mpi_limb_t) *p-- << 8;
  156. if (p >= buffer)
  157. alimb |= (mpi_limb_t) *p-- << 16;
  158. if (p >= buffer)
  159. alimb |= (mpi_limb_t) *p-- << 24;
  160. #elif BYTES_PER_MPI_LIMB == 8
  161. alimb = (mpi_limb_t) *p--;
  162. if (p >= buffer)
  163. alimb |= (mpi_limb_t) *p-- << 8;
  164. if (p >= buffer)
  165. alimb |= (mpi_limb_t) *p-- << 16;
  166. if (p >= buffer)
  167. alimb |= (mpi_limb_t) *p-- << 24;
  168. if (p >= buffer)
  169. alimb |= (mpi_limb_t) *p-- << 32;
  170. if (p >= buffer)
  171. alimb |= (mpi_limb_t) *p-- << 40;
  172. if (p >= buffer)
  173. alimb |= (mpi_limb_t) *p-- << 48;
  174. if (p >= buffer)
  175. alimb |= (mpi_limb_t) *p-- << 56;
  176. #else
  177. #error please implement for this limb size.
  178. #endif
  179. a->d[i++] = alimb;
  180. }
  181. a->nlimbs = i;
  182. if (i != nlimbs) {
  183. pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
  184. nlimbs);
  185. BUG();
  186. }
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(mpi_set_buffer);