rsagen.c 992 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <mp.h>
  4. #include <libsec.h>
  5. void
  6. usage(void)
  7. {
  8. fprint(2, "usage: auth/rsagen [-b bits] [-t 'attr=value attr=value ...']\n");
  9. exits("usage");
  10. }
  11. void
  12. main(int argc, char **argv)
  13. {
  14. char *s;
  15. int bits;
  16. char *tag;
  17. RSApriv *key;
  18. bits = 1024;
  19. tag = nil;
  20. key = nil;
  21. fmtinstall('B', mpfmt);
  22. ARGBEGIN{
  23. case 'b':
  24. bits = atoi(EARGF(usage()));
  25. if(bits == 0)
  26. usage();
  27. break;
  28. case 't':
  29. tag = EARGF(usage());
  30. break;
  31. default:
  32. usage();
  33. }ARGEND
  34. if(argc != 0)
  35. usage();
  36. do{
  37. if(key)
  38. rsaprivfree(key);
  39. key = rsagen(bits, 6, 0);
  40. }while(mpsignif(key->pub.n) != bits);
  41. s = smprint("key proto=rsa %s%ssize=%d ek=%B !dk=%B n=%B !p=%B !q=%B !kp=%B !kq=%B !c2=%B\n",
  42. tag ? tag : "", tag ? " " : "",
  43. mpsignif(key->pub.n), key->pub.ek,
  44. key->dk, key->pub.n, key->p, key->q,
  45. key->kp, key->kq, key->c2);
  46. if(s == nil)
  47. sysfatal("smprint: %r");
  48. if(write(1, s, strlen(s)) != strlen(s))
  49. sysfatal("write: %r");
  50. exits(nil);
  51. }