crypto tool: added encryption

This commit is contained in:
schneider 2011-08-03 08:58:27 +02:00
parent 0d67b62050
commit 5c6f9e2f76
2 changed files with 35 additions and 54 deletions

View File

@ -136,8 +136,10 @@ int main(int argc, char *argv[]) {
if (verbose) if (verbose)
fprintf(stderr,"byte count=%d word count=%d\n", bytes, words); fprintf(stderr,"byte count=%d word count=%d\n", bytes, words);
if( sign )
buf=malloc(bytes); buf=malloc(bytes+sizeof(uint32_t)*4);
else
buf=malloc(bytes);
if(!buf){ if(!buf){
fprintf(stderr,"Error: malloc() failed.\n"); fprintf(stderr,"Error: malloc() failed.\n");
@ -147,69 +149,48 @@ int main(int argc, char *argv[]) {
if (verbose) if (verbose)
fprintf(stderr,"Key: %08x %08x %08x %08x\n",k[0],k[1],k[2],k[3]); fprintf(stderr,"Key: %08x %08x %08x %08x\n",k[0],k[1],k[2],k[3]);
memset(buf, 0, bytes);
if( fread(buf,sizeof(*buf),filesize,fp) != filesize ){
fprintf(stderr, "Error: read failed\n");
exit(253);
};
if( encrypt ){
if (verbose)
fprintf(stderr,"Encrypting: ");
xxtea_encode_words((uint32_t*)buf, words, k);
if(verbose) fprintf(stderr,".\n");
}
if( sign ){ if( sign ){
if (verbose) if (verbose)
fprintf(stderr,"Signing: "); fprintf(stderr,"Signing: ");
memset(buf, 0, bytes);
int cnt = fread(buf,sizeof(*buf),filesize,fp);
cnt = 0;
uint32_t mac[4]; uint32_t mac[4];
xxtea_cbcmac(mac, (uint32_t*)buf, words, k); xxtea_cbcmac(mac, (uint32_t*)buf, words, k);
if (verbose) if (verbose)
fprintf(stderr,"MAC: %08x %08x %08x %08x\n",mac[0],mac[1],mac[2],mac[3]); fprintf(stderr,"MAC: %08x %08x %08x %08x\n",mac[0],mac[1],mac[2],mac[3]);
memcpy(buf+bytes, mac, sizeof(*mac)*4);
if(!outfile) // in-place crypting... bytes += sizeof(*mac)*4;
if( fseek(fp, 0L, SEEK_SET) != 0){
fprintf(stderr, "Error: Seek failed\n");
exit(253);
}
if (fwrite(buf,sizeof(*buf),bytes,ofp) != bytes){
fprintf(stderr, "Error: file write failed\n");
exit(253);
}
if (fwrite(mac,sizeof(*mac),4,ofp) != 4){
fprintf(stderr, "Error: mac write failed\n");
exit(253);
}
if(verbose) fprintf(stderr,".\n"); if(verbose) fprintf(stderr,".\n");
} }
if( encrypt ){ if(!outfile) // in-place crypting...
int cnt, block=0; if (fseek(fp,0,SEEK_SET) != 0){
if (verbose) fprintf(stderr, "Error: Seek failed\n");
fprintf(stderr,"Encrypting: "); exit(253);
}
do{ if (fwrite(buf,sizeof(*buf),bytes,ofp) != bytes){
cnt=fread(buf,sizeof(*buf),block,fp); // XXX: deal with non-block-sized? fprintf(stderr, "Error: CRC write failed\n");
exit(253);
if(cnt<0){
fprintf(stderr, "Error: read failed\n");
exit(253);
};
if(verbose)
fprintf(stderr,"cnt=%d:",cnt);
/* if(cnt%sizeof(*buf)!=0){
fprintf(stderr,"Whoops. needs padding: cnt=%d, mod=%d\n",cnt,cnt%sizeof(*buf));
}; */
//btea(buf, decrypt?-cnt:cnt, k);
if(!outfile) // in-place crypting...
if (fseek(fp,-cnt*sizeof(*buf),SEEK_CUR) != 0){
fprintf(stderr, "Error: Seek failed\n");
exit(253);
}
if (fwrite(buf,sizeof(*buf),cnt,ofp) != cnt){
fprintf(stderr, "Error: CRC write failed\n");
exit(253);
}
if(verbose) fprintf(stderr,".\n");
}while(cnt==block);
} }
if( fseek(ofp, 0L, SEEK_SET) != 0){
fprintf(stderr, "Error: Seek failed\n");
exit(253);
}
if(verbose) if(verbose)
fprintf(stderr,"done\n"); fprintf(stderr,"done\n");

View File

@ -40,7 +40,7 @@ void xxtea_cbcmac(uint32_t mac[4], uint32_t *data,
void xxtea_encode_words(uint32_t *v, int n, uint32_t const k[4]) void xxtea_encode_words(uint32_t *v, int n, uint32_t const k[4])
{ {
if(k[0] == 0 && k[1] == 0 && k[2] == 0 && k[3] == 0) return; //if(k[0] == 0 && k[1] == 0 && k[2] == 0 && k[3] == 0) return;
uint32_t y, z, sum; uint32_t y, z, sum;
unsigned p, rounds, e; unsigned p, rounds, e;
@ -63,7 +63,7 @@ void xxtea_encode_words(uint32_t *v, int n, uint32_t const k[4])
void xxtea_decode_words(uint32_t *v, int n, uint32_t const k[4]) void xxtea_decode_words(uint32_t *v, int n, uint32_t const k[4])
{ {
if(k[0] == 0 && k[1] == 0 && k[2] == 0 && k[3] == 0) return; //if(k[0] == 0 && k[1] == 0 && k[2] == 0 && k[3] == 0) return;
uint32_t y, z, sum; uint32_t y, z, sum;
unsigned p, rounds, e; unsigned p, rounds, e;
htonlp(v ,n); htonlp(v ,n);