mpiutil.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <linux/export.h>
  21. #include "mpi-internal.h"
  22. /****************
  23. * Note: It was a bad idea to use the number of limbs to allocate
  24. * because on a alpha the limbs are large but we normally need
  25. * integers of n bits - So we should change this to bits (or bytes).
  26. *
  27. * But mpi_alloc is used in a lot of places :-)
  28. */
  29. MPI mpi_alloc(unsigned nlimbs)
  30. {
  31. MPI a;
  32. a = kmalloc_obj(*a);
  33. if (!a)
  34. return a;
  35. if (nlimbs) {
  36. a->d = mpi_alloc_limb_space(nlimbs);
  37. if (!a->d) {
  38. kfree(a);
  39. return NULL;
  40. }
  41. } else {
  42. a->d = NULL;
  43. }
  44. a->alloced = nlimbs;
  45. a->nlimbs = 0;
  46. a->sign = 0;
  47. a->flags = 0;
  48. a->nbits = 0;
  49. return a;
  50. }
  51. EXPORT_SYMBOL_GPL(mpi_alloc);
  52. mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs)
  53. {
  54. size_t len = nlimbs * sizeof(mpi_limb_t);
  55. if (!len)
  56. return NULL;
  57. return kmalloc(len, GFP_KERNEL);
  58. }
  59. void mpi_free_limb_space(mpi_ptr_t a)
  60. {
  61. if (!a)
  62. return;
  63. kfree_sensitive(a);
  64. }
  65. void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
  66. {
  67. mpi_free_limb_space(a->d);
  68. a->d = ap;
  69. a->alloced = nlimbs;
  70. }
  71. /****************
  72. * Resize the array of A to NLIMBS. the additional space is cleared
  73. * (set to 0) [done by m_realloc()]
  74. */
  75. int mpi_resize(MPI a, unsigned nlimbs)
  76. {
  77. void *p;
  78. if (nlimbs <= a->alloced)
  79. return 0; /* no need to do it */
  80. if (a->d) {
  81. p = kzalloc_objs(mpi_limb_t, nlimbs);
  82. if (!p)
  83. return -ENOMEM;
  84. memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
  85. kfree_sensitive(a->d);
  86. a->d = p;
  87. } else {
  88. a->d = kzalloc_objs(mpi_limb_t, nlimbs);
  89. if (!a->d)
  90. return -ENOMEM;
  91. }
  92. a->alloced = nlimbs;
  93. return 0;
  94. }
  95. void mpi_free(MPI a)
  96. {
  97. if (!a)
  98. return;
  99. if (a->flags & 4)
  100. kfree_sensitive(a->d);
  101. else
  102. mpi_free_limb_space(a->d);
  103. if (a->flags & ~7)
  104. pr_info("invalid flag value in mpi\n");
  105. kfree(a);
  106. }
  107. EXPORT_SYMBOL_GPL(mpi_free);
  108. /****************
  109. * Note: This copy function should not interpret the MPI
  110. * but copy it transparently.
  111. */
  112. MPI mpi_copy(MPI a)
  113. {
  114. int i;
  115. MPI b;
  116. if (a) {
  117. b = mpi_alloc(a->nlimbs);
  118. if (!b)
  119. return NULL;
  120. b->nlimbs = a->nlimbs;
  121. b->sign = a->sign;
  122. b->flags = a->flags;
  123. b->flags &= ~(16|32); /* Reset the immutable and constant flags. */
  124. for (i = 0; i < b->nlimbs; i++)
  125. b->d[i] = a->d[i];
  126. } else
  127. b = NULL;
  128. return b;
  129. }
  130. MODULE_DESCRIPTION("Multiprecision maths library");
  131. MODULE_LICENSE("GPL");