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