egdecrypt.c 976 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "os.h"
  10. #include <mp.h>
  11. #include <libsec.h>
  12. mpint*
  13. egdecrypt(EGpriv *priv, mpint *in, mpint *out)
  14. {
  15. EGpub *pub = &priv->pub;
  16. mpint *gamma, *delta;
  17. mpint *p = pub->p;
  18. int plen = mpsignif(p)+1;
  19. int shift = ((plen+Dbits-1)/Dbits)*Dbits;
  20. if(out == nil)
  21. out = mpnew(0);
  22. gamma = mpnew(0);
  23. delta = mpnew(0);
  24. mpright(in, shift, gamma);
  25. mpleft(gamma, shift, delta);
  26. mpsub(in, delta, delta);
  27. mpexp(gamma, priv->secret, p, out);
  28. mpinvert(out, p, gamma);
  29. mpmul(gamma, delta, out);
  30. mpmod(out, p, out);
  31. mpfree(gamma);
  32. mpfree(delta);
  33. return out;
  34. }