mpiutil.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* mpiutil.ac - Utility functions for MPI
  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. /****************
  22. * Note: It was a bad idea to use the number of limbs to allocate
  23. * because on a alpha the limbs are large but we normally need
  24. * integers of n bits - So we should chnage this to bits (or bytes).
  25. *
  26. * But mpi_alloc is used in a lot of places :-)
  27. */
  28. MPI mpi_alloc(unsigned nlimbs)
  29. {
  30. MPI a;
  31. a = kmalloc(sizeof *a, GFP_KERNEL);
  32. if (!a)
  33. return a;
  34. if (nlimbs) {
  35. a->d = mpi_alloc_limb_space(nlimbs);
  36. if (!a->d) {
  37. kfree(a);
  38. return NULL;
  39. }
  40. } else {
  41. a->d = NULL;
  42. }
  43. a->alloced = nlimbs;
  44. a->nlimbs = 0;
  45. a->sign = 0;
  46. a->flags = 0;
  47. a->nbits = 0;
  48. return a;
  49. }
  50. EXPORT_SYMBOL_GPL(mpi_alloc);
  51. mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs)
  52. {
  53. size_t len = nlimbs * sizeof(mpi_limb_t);
  54. return kmalloc(len, GFP_KERNEL);
  55. }
  56. void mpi_free_limb_space(mpi_ptr_t a)
  57. {
  58. if (!a)
  59. return;
  60. kfree(a);
  61. }
  62. void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
  63. {
  64. mpi_free_limb_space(a->d);
  65. a->d = ap;
  66. a->alloced = nlimbs;
  67. }
  68. /****************
  69. * Resize the array of A to NLIMBS. the additional space is cleared
  70. * (set to 0) [done by m_realloc()]
  71. */
  72. int mpi_resize(MPI a, unsigned nlimbs)
  73. {
  74. void *p;
  75. if (nlimbs <= a->alloced)
  76. return 0; /* no need to do it */
  77. if (a->d) {
  78. p = kmalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
  79. if (!p)
  80. return -ENOMEM;
  81. memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
  82. kfree(a->d);
  83. a->d = p;
  84. } else {
  85. a->d = kzalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
  86. if (!a->d)
  87. return -ENOMEM;
  88. }
  89. a->alloced = nlimbs;
  90. return 0;
  91. }
  92. void mpi_clear(MPI a)
  93. {
  94. a->nlimbs = 0;
  95. a->nbits = 0;
  96. a->flags = 0;
  97. }
  98. void mpi_free(MPI a)
  99. {
  100. if (!a)
  101. return;
  102. if (a->flags & 4)
  103. kfree(a->d);
  104. else
  105. mpi_free_limb_space(a->d);
  106. if (a->flags & ~7)
  107. pr_info("invalid flag value in mpi\n");
  108. kfree(a);
  109. }
  110. EXPORT_SYMBOL_GPL(mpi_free);
  111. /****************
  112. * Note: This copy function should not interpret the MPI
  113. * but copy it transparently.
  114. */
  115. int mpi_copy(MPI *copied, const MPI a)
  116. {
  117. size_t i;
  118. MPI b;
  119. *copied = MPI_NULL;
  120. if (a) {
  121. b = mpi_alloc(a->nlimbs);
  122. if (!b)
  123. return -ENOMEM;
  124. b->nlimbs = a->nlimbs;
  125. b->sign = a->sign;
  126. b->flags = a->flags;
  127. b->nbits = a->nbits;
  128. for (i = 0; i < b->nlimbs; i++)
  129. b->d[i] = a->d[i];
  130. *copied = b;
  131. }
  132. return 0;
  133. }
  134. int mpi_set(MPI w, const MPI u)
  135. {
  136. mpi_ptr_t wp, up;
  137. mpi_size_t usize = u->nlimbs;
  138. int usign = u->sign;
  139. if (RESIZE_IF_NEEDED(w, (size_t) usize) < 0)
  140. return -ENOMEM;
  141. wp = w->d;
  142. up = u->d;
  143. MPN_COPY(wp, up, usize);
  144. w->nlimbs = usize;
  145. w->nbits = u->nbits;
  146. w->flags = u->flags;
  147. w->sign = usign;
  148. return 0;
  149. }
  150. int mpi_set_ui(MPI w, unsigned long u)
  151. {
  152. if (RESIZE_IF_NEEDED(w, 1) < 0)
  153. return -ENOMEM;
  154. w->d[0] = u;
  155. w->nlimbs = u ? 1 : 0;
  156. w->sign = 0;
  157. w->nbits = 0;
  158. w->flags = 0;
  159. return 0;
  160. }
  161. MPI mpi_alloc_set_ui(unsigned long u)
  162. {
  163. MPI w = mpi_alloc(1);
  164. if (!w)
  165. return w;
  166. w->d[0] = u;
  167. w->nlimbs = u ? 1 : 0;
  168. w->sign = 0;
  169. return w;
  170. }
  171. void mpi_swap(MPI a, MPI b)
  172. {
  173. struct gcry_mpi tmp;
  174. tmp = *a;
  175. *a = *b;
  176. *b = tmp;
  177. }