mpiutil.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. /* Constants allocated right away at startup. */
  22. static MPI constants[MPI_NUMBER_OF_CONSTANTS];
  23. /* Initialize the MPI subsystem. This is called early and allows to
  24. * do some initialization without taking care of threading issues.
  25. */
  26. static int __init mpi_init(void)
  27. {
  28. int idx;
  29. unsigned long value;
  30. for (idx = 0; idx < MPI_NUMBER_OF_CONSTANTS; idx++) {
  31. switch (idx) {
  32. case MPI_C_ZERO:
  33. value = 0;
  34. break;
  35. case MPI_C_ONE:
  36. value = 1;
  37. break;
  38. case MPI_C_TWO:
  39. value = 2;
  40. break;
  41. case MPI_C_THREE:
  42. value = 3;
  43. break;
  44. case MPI_C_FOUR:
  45. value = 4;
  46. break;
  47. case MPI_C_EIGHT:
  48. value = 8;
  49. break;
  50. default:
  51. pr_err("MPI: invalid mpi_const selector %d\n", idx);
  52. return -EFAULT;
  53. }
  54. constants[idx] = mpi_alloc_set_ui(value);
  55. constants[idx]->flags = (16|32);
  56. }
  57. return 0;
  58. }
  59. postcore_initcall(mpi_init);
  60. /* Return a constant MPI descripbed by NO which is one of the
  61. * MPI_C_xxx macros. There is no need to copy this returned value; it
  62. * may be used directly.
  63. */
  64. MPI mpi_const(enum gcry_mpi_constants no)
  65. {
  66. if ((int)no < 0 || no > MPI_NUMBER_OF_CONSTANTS)
  67. pr_err("MPI: invalid mpi_const selector %d\n", no);
  68. if (!constants[no])
  69. pr_err("MPI: MPI subsystem not initialized\n");
  70. return constants[no];
  71. }
  72. EXPORT_SYMBOL_GPL(mpi_const);
  73. /****************
  74. * Note: It was a bad idea to use the number of limbs to allocate
  75. * because on a alpha the limbs are large but we normally need
  76. * integers of n bits - So we should chnage this to bits (or bytes).
  77. *
  78. * But mpi_alloc is used in a lot of places :-)
  79. */
  80. MPI mpi_alloc(unsigned nlimbs)
  81. {
  82. MPI a;
  83. a = kmalloc(sizeof *a, GFP_KERNEL);
  84. if (!a)
  85. return a;
  86. if (nlimbs) {
  87. a->d = mpi_alloc_limb_space(nlimbs);
  88. if (!a->d) {
  89. kfree(a);
  90. return NULL;
  91. }
  92. } else {
  93. a->d = NULL;
  94. }
  95. a->alloced = nlimbs;
  96. a->nlimbs = 0;
  97. a->sign = 0;
  98. a->flags = 0;
  99. a->nbits = 0;
  100. return a;
  101. }
  102. EXPORT_SYMBOL_GPL(mpi_alloc);
  103. mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs)
  104. {
  105. size_t len = nlimbs * sizeof(mpi_limb_t);
  106. if (!len)
  107. return NULL;
  108. return kmalloc(len, GFP_KERNEL);
  109. }
  110. void mpi_free_limb_space(mpi_ptr_t a)
  111. {
  112. if (!a)
  113. return;
  114. kfree_sensitive(a);
  115. }
  116. void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
  117. {
  118. mpi_free_limb_space(a->d);
  119. a->d = ap;
  120. a->alloced = nlimbs;
  121. }
  122. /****************
  123. * Resize the array of A to NLIMBS. the additional space is cleared
  124. * (set to 0) [done by m_realloc()]
  125. */
  126. int mpi_resize(MPI a, unsigned nlimbs)
  127. {
  128. void *p;
  129. if (nlimbs <= a->alloced)
  130. return 0; /* no need to do it */
  131. if (a->d) {
  132. p = kcalloc(nlimbs, sizeof(mpi_limb_t), GFP_KERNEL);
  133. if (!p)
  134. return -ENOMEM;
  135. memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
  136. kfree_sensitive(a->d);
  137. a->d = p;
  138. } else {
  139. a->d = kcalloc(nlimbs, sizeof(mpi_limb_t), GFP_KERNEL);
  140. if (!a->d)
  141. return -ENOMEM;
  142. }
  143. a->alloced = nlimbs;
  144. return 0;
  145. }
  146. void mpi_clear(MPI a)
  147. {
  148. if (!a)
  149. return;
  150. a->nlimbs = 0;
  151. a->flags = 0;
  152. }
  153. EXPORT_SYMBOL_GPL(mpi_clear);
  154. void mpi_free(MPI a)
  155. {
  156. if (!a)
  157. return;
  158. if (a->flags & 4)
  159. kfree_sensitive(a->d);
  160. else
  161. mpi_free_limb_space(a->d);
  162. if (a->flags & ~7)
  163. pr_info("invalid flag value in mpi\n");
  164. kfree(a);
  165. }
  166. EXPORT_SYMBOL_GPL(mpi_free);
  167. /****************
  168. * Note: This copy function should not interpret the MPI
  169. * but copy it transparently.
  170. */
  171. MPI mpi_copy(MPI a)
  172. {
  173. int i;
  174. MPI b;
  175. if (a) {
  176. b = mpi_alloc(a->nlimbs);
  177. b->nlimbs = a->nlimbs;
  178. b->sign = a->sign;
  179. b->flags = a->flags;
  180. b->flags &= ~(16|32); /* Reset the immutable and constant flags. */
  181. for (i = 0; i < b->nlimbs; i++)
  182. b->d[i] = a->d[i];
  183. } else
  184. b = NULL;
  185. return b;
  186. }
  187. /****************
  188. * This function allocates an MPI which is optimized to hold
  189. * a value as large as the one given in the argument and allocates it
  190. * with the same flags as A.
  191. */
  192. MPI mpi_alloc_like(MPI a)
  193. {
  194. MPI b;
  195. if (a) {
  196. b = mpi_alloc(a->nlimbs);
  197. b->nlimbs = 0;
  198. b->sign = 0;
  199. b->flags = a->flags;
  200. } else
  201. b = NULL;
  202. return b;
  203. }
  204. /* Set U into W and release U. If W is NULL only U will be released. */
  205. void mpi_snatch(MPI w, MPI u)
  206. {
  207. if (w) {
  208. mpi_assign_limb_space(w, u->d, u->alloced);
  209. w->nlimbs = u->nlimbs;
  210. w->sign = u->sign;
  211. w->flags = u->flags;
  212. u->alloced = 0;
  213. u->nlimbs = 0;
  214. u->d = NULL;
  215. }
  216. mpi_free(u);
  217. }
  218. MPI mpi_set(MPI w, MPI u)
  219. {
  220. mpi_ptr_t wp, up;
  221. mpi_size_t usize = u->nlimbs;
  222. int usign = u->sign;
  223. if (!w)
  224. w = mpi_alloc(mpi_get_nlimbs(u));
  225. RESIZE_IF_NEEDED(w, usize);
  226. wp = w->d;
  227. up = u->d;
  228. MPN_COPY(wp, up, usize);
  229. w->nlimbs = usize;
  230. w->flags = u->flags;
  231. w->flags &= ~(16|32); /* Reset the immutable and constant flags. */
  232. w->sign = usign;
  233. return w;
  234. }
  235. EXPORT_SYMBOL_GPL(mpi_set);
  236. MPI mpi_set_ui(MPI w, unsigned long u)
  237. {
  238. if (!w)
  239. w = mpi_alloc(1);
  240. /* FIXME: If U is 0 we have no need to resize and thus possible
  241. * allocating the the limbs.
  242. */
  243. RESIZE_IF_NEEDED(w, 1);
  244. w->d[0] = u;
  245. w->nlimbs = u ? 1 : 0;
  246. w->sign = 0;
  247. w->flags = 0;
  248. return w;
  249. }
  250. EXPORT_SYMBOL_GPL(mpi_set_ui);
  251. MPI mpi_alloc_set_ui(unsigned long u)
  252. {
  253. MPI w = mpi_alloc(1);
  254. w->d[0] = u;
  255. w->nlimbs = u ? 1 : 0;
  256. w->sign = 0;
  257. return w;
  258. }
  259. /****************
  260. * Swap the value of A and B, when SWAP is 1.
  261. * Leave the value when SWAP is 0.
  262. * This implementation should be constant-time regardless of SWAP.
  263. */
  264. void mpi_swap_cond(MPI a, MPI b, unsigned long swap)
  265. {
  266. mpi_size_t i;
  267. mpi_size_t nlimbs;
  268. mpi_limb_t mask = ((mpi_limb_t)0) - swap;
  269. mpi_limb_t x;
  270. if (a->alloced > b->alloced)
  271. nlimbs = b->alloced;
  272. else
  273. nlimbs = a->alloced;
  274. if (a->nlimbs > nlimbs || b->nlimbs > nlimbs)
  275. return;
  276. for (i = 0; i < nlimbs; i++) {
  277. x = mask & (a->d[i] ^ b->d[i]);
  278. a->d[i] = a->d[i] ^ x;
  279. b->d[i] = b->d[i] ^ x;
  280. }
  281. x = mask & (a->nlimbs ^ b->nlimbs);
  282. a->nlimbs = a->nlimbs ^ x;
  283. b->nlimbs = b->nlimbs ^ x;
  284. x = mask & (a->sign ^ b->sign);
  285. a->sign = a->sign ^ x;
  286. b->sign = b->sign ^ x;
  287. }
  288. MODULE_DESCRIPTION("Multiprecision maths library");
  289. MODULE_LICENSE("GPL");