convkeys2.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <u.h>
  10. #include <libc.h>
  11. #include <mp.h>
  12. #include <libsec.h>
  13. #include <authsrv.h>
  14. #include <bio.h>
  15. #include "authcmdlib.h"
  16. char authkey[DESKEYLEN];
  17. int verb;
  18. int usepass;
  19. int convert(char*, char*, char*, int);
  20. int dofcrypt(int, char*, char*, int);
  21. void usage(void);
  22. void randombytes(uint8_t*, int);
  23. void
  24. main(int argc, char *argv[])
  25. {
  26. Dir *d;
  27. char *p, *np, *file, key[DESKEYLEN];
  28. int fd, len;
  29. ARGBEGIN{
  30. case 'v':
  31. verb = 1;
  32. break;
  33. case 'p':
  34. usepass = 1;
  35. break;
  36. default:
  37. usage();
  38. }ARGEND
  39. if(argc != 1)
  40. usage();
  41. file = argv[0];
  42. /* get original key */
  43. if(usepass){
  44. print("enter password file is encoded with\n");
  45. getpass(authkey, nil, 0, 1);
  46. } else
  47. getauthkey(authkey);
  48. print("enter password to reencode with\n");
  49. getpass(key, nil, 0, 1);
  50. fd = open(file, ORDWR);
  51. if(fd < 0)
  52. error("can't open %s: %r\n", file);
  53. d = dirfstat(fd);
  54. if(d == nil)
  55. error("can't stat %s: %r\n", file);
  56. len = d->length;
  57. p = malloc(len);
  58. if(!p)
  59. error("out of memory");
  60. np = malloc((len/OKEYDBLEN)*KEYDBLEN + KEYDBOFF);
  61. if(!np)
  62. error("out of memory");
  63. if(read(fd, p, len) != len)
  64. error("can't read key file: %r\n");
  65. len = convert(p, np, key, len);
  66. if(verb)
  67. exits(0);
  68. if(pwrite(fd, np, len, 0) != len)
  69. error("can't write key file: %r\n");
  70. close(fd);
  71. exits(0);
  72. }
  73. void
  74. oldCBCencrypt(char *key7, char *p, int len)
  75. {
  76. uint8_t ivec[8];
  77. uint8_t key[8];
  78. DESstate s;
  79. memset(ivec, 0, 8);
  80. des56to64((uint8_t*)key7, key);
  81. setupDESstate(&s, key, ivec);
  82. desCBCencrypt((uint8_t*)p, len, &s);
  83. }
  84. int
  85. convert(char *p, char *np, char *key, int len)
  86. {
  87. int i, off, noff;
  88. if(len % OKEYDBLEN)
  89. fprint(2, "convkeys2: file odd length; not converting %d bytes\n",
  90. len % KEYDBLEN);
  91. len /= OKEYDBLEN;
  92. for(i = 0; i < len; i ++){
  93. off = i*OKEYDBLEN;
  94. noff = KEYDBOFF+i*(KEYDBLEN);
  95. decrypt(authkey, &p[off], OKEYDBLEN);
  96. memmove(&np[noff], &p[off], OKEYDBLEN);
  97. memset(&np[noff-SECRETLEN], 0, SECRETLEN);
  98. if(verb)
  99. print("%s\n", &p[off]);
  100. }
  101. randombytes((uint8_t*)np, KEYDBOFF);
  102. len = (len*KEYDBLEN) + KEYDBOFF;
  103. oldCBCencrypt(key, np, len);
  104. return len;
  105. }
  106. void
  107. usage(void)
  108. {
  109. fprint(2, "usage: convkeys2 keyfile\n");
  110. exits("usage");
  111. }
  112. void
  113. randombytes(uint8_t *p, int len)
  114. {
  115. int i, fd;
  116. fd = open("/dev/random", OREAD);
  117. if(fd < 0){
  118. fprint(2, "convkeys2: can't open /dev/random, using rand()\n");
  119. srand(time(0));
  120. for(i = 0; i < len; i++)
  121. p[i] = rand();
  122. return;
  123. }
  124. read(fd, p, len);
  125. close(fd);
  126. }