mpi-mul.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* mpi-mul.c - MPI functions
  2. * Copyright (C) 1994, 1996, 1998, 2001, 2002,
  3. * 2003 Free Software Foundation, Inc.
  4. *
  5. * This file is part of Libgcrypt.
  6. *
  7. * Note: This code is heavily based on the GNU MP Library.
  8. * Actually it's the same code with only minor changes in the
  9. * way the data is stored; this is to support the abstraction
  10. * of an optional secure memory allocation which may be used
  11. * to avoid revealing of sensitive data due to paging etc.
  12. */
  13. #include <linux/export.h>
  14. #include "mpi-internal.h"
  15. int mpi_mul(MPI w, MPI u, MPI v)
  16. {
  17. mpi_size_t usize, vsize, wsize;
  18. mpi_ptr_t up, vp, wp;
  19. mpi_limb_t cy;
  20. int usign, vsign, sign_product;
  21. int assign_wp = 0;
  22. mpi_ptr_t tmp_limb = NULL;
  23. int err = 0;
  24. if (u->nlimbs < v->nlimbs) {
  25. /* Swap U and V. */
  26. usize = v->nlimbs;
  27. usign = v->sign;
  28. up = v->d;
  29. vsize = u->nlimbs;
  30. vsign = u->sign;
  31. vp = u->d;
  32. } else {
  33. usize = u->nlimbs;
  34. usign = u->sign;
  35. up = u->d;
  36. vsize = v->nlimbs;
  37. vsign = v->sign;
  38. vp = v->d;
  39. }
  40. sign_product = usign ^ vsign;
  41. wp = w->d;
  42. /* Ensure W has space enough to store the result. */
  43. wsize = usize + vsize;
  44. if (w->alloced < wsize) {
  45. if (wp == up || wp == vp) {
  46. wp = mpi_alloc_limb_space(wsize);
  47. if (!wp)
  48. return -ENOMEM;
  49. assign_wp = 1;
  50. } else {
  51. err = mpi_resize(w, wsize);
  52. if (err)
  53. return err;
  54. wp = w->d;
  55. }
  56. } else { /* Make U and V not overlap with W. */
  57. if (wp == up) {
  58. /* W and U are identical. Allocate temporary space for U. */
  59. up = tmp_limb = mpi_alloc_limb_space(usize);
  60. if (!up)
  61. return -ENOMEM;
  62. /* Is V identical too? Keep it identical with U. */
  63. if (wp == vp)
  64. vp = up;
  65. /* Copy to the temporary space. */
  66. MPN_COPY(up, wp, usize);
  67. } else if (wp == vp) {
  68. /* W and V are identical. Allocate temporary space for V. */
  69. vp = tmp_limb = mpi_alloc_limb_space(vsize);
  70. if (!vp)
  71. return -ENOMEM;
  72. /* Copy to the temporary space. */
  73. MPN_COPY(vp, wp, vsize);
  74. }
  75. }
  76. if (!vsize)
  77. wsize = 0;
  78. else {
  79. err = mpihelp_mul(wp, up, usize, vp, vsize, &cy);
  80. if (err) {
  81. if (assign_wp)
  82. mpi_free_limb_space(wp);
  83. goto free_tmp_limb;
  84. }
  85. wsize -= cy ? 0:1;
  86. }
  87. if (assign_wp)
  88. mpi_assign_limb_space(w, wp, wsize);
  89. w->nlimbs = wsize;
  90. w->sign = sign_product;
  91. free_tmp_limb:
  92. if (tmp_limb)
  93. mpi_free_limb_space(tmp_limb);
  94. return err;
  95. }
  96. EXPORT_SYMBOL_GPL(mpi_mul);
  97. int mpi_mulm(MPI w, MPI u, MPI v, MPI m)
  98. {
  99. return mpi_mul(w, u, v) ?:
  100. mpi_tdiv_r(w, w, m);
  101. }
  102. EXPORT_SYMBOL_GPL(mpi_mulm);