mpi-mpow.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* mpi-mpow.c - MPI functions
  2. * Copyright (C) 1998, 1999, 2000 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. #include "longlong.h"
  22. static int build_index(const MPI *exparray, int k, int i, int t)
  23. {
  24. int j, bitno;
  25. int index = 0;
  26. bitno = t - i;
  27. for (j = k - 1; j >= 0; j--) {
  28. index <<= 1;
  29. if (mpi_test_bit(exparray[j], bitno))
  30. index |= 1;
  31. }
  32. return index;
  33. }
  34. /****************
  35. * RES = (BASE[0] ^ EXP[0]) * (BASE[1] ^ EXP[1]) * ... * mod M
  36. */
  37. int mpi_mulpowm(MPI res, MPI *basearray, MPI *exparray, MPI m)
  38. {
  39. int rc = -ENOMEM;
  40. int k; /* number of elements */
  41. int t; /* bit size of largest exponent */
  42. int i, j, idx;
  43. MPI *G = NULL; /* table with precomputed values of size 2^k */
  44. MPI tmp = NULL;
  45. for (k = 0; basearray[k]; k++)
  46. ;
  47. if (!k) {
  48. pr_emerg("mpi_mulpowm: assert(k) failed\n");
  49. BUG();
  50. }
  51. for (t = 0, i = 0; (tmp = exparray[i]); i++) {
  52. j = mpi_get_nbits(tmp);
  53. if (j > t)
  54. t = j;
  55. }
  56. if (i != k) {
  57. pr_emerg("mpi_mulpowm: assert(i==k) failed\n");
  58. BUG();
  59. }
  60. if (!t) {
  61. pr_emerg("mpi_mulpowm: assert(t) failed\n");
  62. BUG();
  63. }
  64. if (k >= 10) {
  65. pr_emerg("mpi_mulpowm: assert(k<10) failed\n");
  66. BUG();
  67. }
  68. G = kzalloc((1 << k) * sizeof *G, GFP_KERNEL);
  69. if (!G)
  70. goto err_out;
  71. /* and calculate */
  72. tmp = mpi_alloc(mpi_get_nlimbs(m) + 1);
  73. if (!tmp)
  74. goto nomem;
  75. if (mpi_set_ui(res, 1) < 0)
  76. goto nomem;
  77. for (i = 1; i <= t; i++) {
  78. if (mpi_mulm(tmp, res, res, m) < 0)
  79. goto nomem;
  80. idx = build_index(exparray, k, i, t);
  81. if (!(idx >= 0 && idx < (1 << k))) {
  82. pr_emerg("mpi_mulpowm: assert(idx >= 0 && idx < (1<<k)) failed\n");
  83. BUG();
  84. }
  85. if (!G[idx]) {
  86. if (!idx) {
  87. G[0] = mpi_alloc_set_ui(1);
  88. if (!G[0])
  89. goto nomem;
  90. } else {
  91. for (j = 0; j < k; j++) {
  92. if ((idx & (1 << j))) {
  93. if (!G[idx]) {
  94. if (mpi_copy
  95. (&G[idx],
  96. basearray[j]) < 0)
  97. goto nomem;
  98. } else {
  99. if (mpi_mulm
  100. (G[idx], G[idx],
  101. basearray[j],
  102. m) < 0)
  103. goto nomem;
  104. }
  105. }
  106. }
  107. if (!G[idx]) {
  108. G[idx] = mpi_alloc(0);
  109. if (!G[idx])
  110. goto nomem;
  111. }
  112. }
  113. }
  114. if (mpi_mulm(res, tmp, G[idx], m) < 0)
  115. goto nomem;
  116. }
  117. rc = 0;
  118. nomem:
  119. /* cleanup */
  120. mpi_free(tmp);
  121. for (i = 0; i < (1 << k); i++)
  122. mpi_free(G[i]);
  123. kfree(G);
  124. err_out:
  125. return rc;
  126. }