Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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