1
0

rsatest.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include <bio.h>
  13. void
  14. main(void)
  15. {
  16. RSApriv *rsa;
  17. Biobuf b;
  18. int8_t *p;
  19. int n;
  20. mpint *clr, *enc, *clr2;
  21. uint8_t buf[4096];
  22. uint8_t *e;
  23. int64_t start;
  24. fmtinstall('B', mpconv);
  25. rsa = rsagen(1024, 16, 0);
  26. if(rsa == nil)
  27. sysfatal("rsagen");
  28. Binit(&b, 0, OREAD);
  29. clr = mpnew(0);
  30. clr2 = mpnew(0);
  31. enc = mpnew(0);
  32. strtomp("123456789abcdef123456789abcdef123456789abcdef123456789abcdef", nil, 16, clr);
  33. rsaencrypt(&rsa->pub, clr, enc);
  34. start = nsec();
  35. for(n = 0; n < 10; n++)
  36. rsadecrypt(rsa, enc, clr);
  37. print("%lld\n", nsec()-start);
  38. start = nsec();
  39. for(n = 0; n < 10; n++)
  40. mpexp(enc, rsa->dk, rsa->pub.n, clr2);
  41. print("%lld\n", nsec()-start);
  42. if(mpcmp(clr, clr2) != 0)
  43. print("%B != %B\n", clr, clr2);
  44. print("> ");
  45. while(p = Brdline(&b, '\n')){
  46. n = Blinelen(&b);
  47. letomp((uint8_t*)p, n, clr);
  48. print("clr %B\n", clr);
  49. rsaencrypt(&rsa->pub, clr, enc);
  50. print("enc %B\n", enc);
  51. rsadecrypt(rsa, enc, clr);
  52. print("clr %B\n", clr);
  53. n = mptole(clr, buf, sizeof(buf), nil);
  54. write(1, buf, n);
  55. print("> ");
  56. }
  57. }