rsatest.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "os.h"
  2. #include <mp.h>
  3. #include <libsec.h>
  4. #include <bio.h>
  5. void
  6. main(void)
  7. {
  8. RSApriv *rsa;
  9. Biobuf b;
  10. char *p;
  11. int n;
  12. mpint *clr, *enc, *clr2;
  13. uchar buf[4096];
  14. uchar *e;
  15. vlong start;
  16. fmtinstall('B', mpconv);
  17. rsa = rsagen(1024, 16, 0);
  18. if(rsa == nil)
  19. sysfatal("rsagen");
  20. Binit(&b, 0, OREAD);
  21. clr = mpnew(0);
  22. clr2 = mpnew(0);
  23. enc = mpnew(0);
  24. strtomp("123456789abcdef123456789abcdef123456789abcdef123456789abcdef", nil, 16, clr);
  25. rsaencrypt(&rsa->pub, clr, enc);
  26. start = nsec();
  27. for(n = 0; n < 10; n++)
  28. rsadecrypt(rsa, enc, clr);
  29. print("%lld\n", nsec()-start);
  30. start = nsec();
  31. for(n = 0; n < 10; n++)
  32. mpexp(enc, rsa->dk, rsa->pub.n, clr2);
  33. print("%lld\n", nsec()-start);
  34. if(mpcmp(clr, clr2) != 0)
  35. print("%B != %B\n", clr, clr2);
  36. print("> ");
  37. while(p = Brdline(&b, '\n')){
  38. n = Blinelen(&b);
  39. letomp((uchar*)p, n, clr);
  40. print("clr %B\n", clr);
  41. rsaencrypt(&rsa->pub, clr, enc);
  42. print("enc %B\n", enc);
  43. rsadecrypt(rsa, enc, clr);
  44. print("clr %B\n", clr);
  45. n = mptole(clr, buf, sizeof(buf), nil);
  46. write(1, buf, n);
  47. print("> ");
  48. }
  49. }