mpiutil.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. if (!len)
  55. return NULL;
  56. return kmalloc(len, GFP_KERNEL);
  57. }
  58. void mpi_free_limb_space(mpi_ptr_t a)
  59. {
  60. if (!a)
  61. return;
  62. kfree(a);
  63. }
  64. void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
  65. {
  66. mpi_free_limb_space(a->d);
  67. a->d = ap;
  68. a->alloced = nlimbs;
  69. }
  70. /****************
  71. * Resize the array of A to NLIMBS. the additional space is cleared
  72. * (set to 0) [done by m_realloc()]
  73. */
  74. int mpi_resize(MPI a, unsigned nlimbs)
  75. {
  76. void *p;
  77. if (nlimbs <= a->alloced)
  78. return 0; /* no need to do it */
  79. if (a->d) {
  80. p = kmalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
  81. if (!p)
  82. return -ENOMEM;
  83. memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
  84. kfree(a->d);
  85. a->d = p;
  86. } else {
  87. a->d = kzalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
  88. if (!a->d)
  89. return -ENOMEM;
  90. }
  91. a->alloced = nlimbs;
  92. return 0;
  93. }
  94. void mpi_clear(MPI a)
  95. {
  96. a->nlimbs = 0;
  97. a->nbits = 0;
  98. a->flags = 0;
  99. }
  100. void mpi_free(MPI a)
  101. {
  102. if (!a)
  103. return;
  104. if (a->flags & 4)
  105. kfree(a->d);
  106. else
  107. mpi_free_limb_space(a->d);
  108. if (a->flags & ~7)
  109. pr_info("invalid flag value in mpi\n");
  110. kfree(a);
  111. }
  112. EXPORT_SYMBOL_GPL(mpi_free);
  113. /****************
  114. * Note: This copy function should not interpret the MPI
  115. * but copy it transparently.
  116. */
  117. int mpi_copy(MPI *copied, const MPI a)
  118. {
  119. size_t i;
  120. MPI b;
  121. *copied = NULL;
  122. if (a) {
  123. b = mpi_alloc(a->nlimbs);
  124. if (!b)
  125. return -ENOMEM;
  126. b->nlimbs = a->nlimbs;
  127. b->sign = a->sign;
  128. b->flags = a->flags;
  129. b->nbits = a->nbits;
  130. for (i = 0; i < b->nlimbs; i++)
  131. b->d[i] = a->d[i];
  132. *copied = b;
  133. }
  134. return 0;
  135. }
  136. int mpi_set(MPI w, const MPI u)
  137. {
  138. mpi_ptr_t wp, up;
  139. mpi_size_t usize = u->nlimbs;
  140. int usign = u->sign;
  141. if (RESIZE_IF_NEEDED(w, (size_t) usize) < 0)
  142. return -ENOMEM;
  143. wp = w->d;
  144. up = u->d;
  145. MPN_COPY(wp, up, usize);
  146. w->nlimbs = usize;
  147. w->nbits = u->nbits;
  148. w->flags = u->flags;
  149. w->sign = usign;
  150. return 0;
  151. }
  152. int mpi_set_ui(MPI w, unsigned long u)
  153. {
  154. if (RESIZE_IF_NEEDED(w, 1) < 0)
  155. return -ENOMEM;
  156. w->d[0] = u;
  157. w->nlimbs = u ? 1 : 0;
  158. w->sign = 0;
  159. w->nbits = 0;
  160. w->flags = 0;
  161. return 0;
  162. }
  163. MPI mpi_alloc_set_ui(unsigned long u)
  164. {
  165. MPI w = mpi_alloc(1);
  166. if (!w)
  167. return w;
  168. w->d[0] = u;
  169. w->nlimbs = u ? 1 : 0;
  170. w->sign = 0;
  171. return w;
  172. }
  173. void mpi_swap(MPI a, MPI b)
  174. {
  175. struct gcry_mpi tmp;
  176. tmp = *a;
  177. *a = *b;
  178. *b = tmp;
  179. }