Plan 9 from Bell Labs’s /usr/web/sources/contrib/rsc/cryptfs/crypt.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include	"u.h"
#include	"libc.h"
#include	"dat.h"
#include	"fns.h"
#include	<mp.h>
#include	<libsec.h>

AESstate *aes;

void
readln(char *prompt, char *line, int len, int raw)
{
	char *p;
	int fdin, fdout, ctl, n, nr;

	fdin = open("/dev/cons", OREAD);
	fdout = open("/dev/cons", OWRITE);
	fprint(fdout, "%s", prompt);
	if(raw){
		ctl = open("/dev/consctl", OWRITE);
		if(ctl < 0){
			fprint(2, "couldn't set raw mode");
			exits("readln");
		}
		write(ctl, "rawon", 5);
	} else
		ctl = -1;
	nr = 0;
	p = line;
	for(;;){
		n = read(fdin, p, 1);
		if(n < 0){
			close(ctl);
			close(fdin);
			close(fdout);
			fprint(2, "can't read cons");
			exits("readln");
		}
		if(*p == 0x7f)
			exits(0);
		if(n == 0 || *p == '\n' || *p == '\r'){
			*p = '\0';
			if(raw){
				write(ctl, "rawoff", 6);
				write(fdout, "\n", 1);
			}
			close(ctl);
			close(fdin);
			close(fdout);
			return;
		}
		if(*p == '\b'){
			if(nr > 0){
				nr--;
				p--;
			}
		}else{
			nr++;
			p++;
		}
		if(nr == len){
			fprint(fdout, "line too long; try again\n");
			nr = 0;
			p = line;
		}
	}
}

void
initkey(void)
{
	char k[128];
	uchar dig[20], ivec[20];
	static AESstate a;
	DigestState *s;

	readln("cryptkfs key: ", k, sizeof k, 1);
	sha1((uchar*)k, strlen(k), dig, nil);
	s = sha1((uchar*)k, strlen(k), nil, nil);
	sha1((uchar*)k, strlen(k), ivec, s);
	setupAESstate(&a, dig, 16, ivec);
	aes = &a;
}

void
bufencrypt(void *a)
{
	AESstate s;

	if(aes == nil)
		initkey();

	s = *aes;
	aesCBCencrypt(a, RBUFSIZE, &s);
}

void
bufdecrypt(void *a)
{
	AESstate s;

	if(aes == nil)
		initkey();
	s = *aes;
	aesCBCdecrypt(a, RBUFSIZE, &s);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.