On Thursday 13 September 2012 04:42:31 j...@jodybruchon.com wrote: > I wrote a pipeline utility that performs an XOR of the data streamed through > it > using a 32-bit key specified on the command line, intended mainly to serve as > a > sort of poor man's encryption. It also includes an option that generates a > random key to stdout. Being new to BusyBox development, I hope that I have > done > all of this correctly (libbb usage, etc.) My test build of BusyBox with > xorpipe > works as expected, and on x86-64, BusyBox 1.20.2 (built with "make defconfig" > in > both cases) increased in size by 176 bytes. If there is anything I could have > done better in the code, please let me know! I would like to make more > valuable > contributions to BusyBox in the future where I can. > > My rationale behind writing this simple utility is as follows: Moving data > through a fully encrypted tunnel like SSH is too slow for me, so I use > netcat/socat for moving large chunks of data between systems. I don't want the > data to be easily identified and reproduced if it is somehow intercepted, > though > it's also not a major problem if someone goes through the trouble of > decrypting > it; thus, simple XOR encryption is good enough for my purposes and is also > extremely fast. I'm sure someone else will find it useful, and given its > minuscule size, it is of negligible cost to include. > > Encrypting (with specified or generated key): > cat datafile | xorpipe c40da326 > datafile.new > xorpipe -g > datafile.key; cat datafile | xorpipe $(cat datafile.key) > > datafile.new > > Decrypting (with a previously generated key: > cat datafile.new | xorpipe $(cat datafile.key) > datafile > > Using xorpipe to obfuscate a generated compressed tar (my real-world purpose): > (on remote host) > socat -u tcp-listen:9999 - | xorpipe a8cf5d36 | lzop -dc | tar -x > (on local machine) > tar -c * | lzop | xorpipe a8cf5d36 | socat -u - tcp-connect:1.2.3.4:9999 > > Cheers, > Jody Lee Bruchon
Hi, is this a standard program? Some hints about libbb functions. +int xorpipe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int xorpipe_main(int argc, char **argv) +{ + uint32_t key = 0; + char *q = (char *)argv[1] + 7; + unsigned char *buf; + unsigned int i, ctr; + uint32_t *ibuf; + int bufsize = 65536; + Maybe use getopt32 ? + if(argc == 2 && (strcmp(argv[1], "-g") == 0)) + { + srand((unsigned int)time(NULL) ^ 0xdeadbeef); + i = 0; + while(i == 0) i = rand(); + printf("%x\n", i); + return 0; + } + + if((argc < 2) || (strlen(argv[1]) != 8)) bb_show_usage(); + + /* Convert 8 lowercase hex characters to a 32-bit integer key */ + for(ctr = 0; ctr < 8; ctr++) + { + if(!((*q < 0x3a && *q > 0x2f) || (*q < 0x67 && *q > 0x60))) + { bb_error_msg_and_die ? + fprintf(stderr, "Only characters 0-9, a-f are valid.\n"); + return 1; + } + if(*q < 0x3a) *q -= 0x30; /* 0-9 */ + if(*q > 0x60) *q -= 0x57; /* a-f */ + key += (*q << (ctr * 4)); + q--; + } + + if(key == 0) + { bb_error_msg_and_die ? + fprintf(stderr, "The key must be greater than zero.\n"); + exit(1); + } + + buf = xmalloc(bufsize*sizeof(char)); + i = 1; + while(i > 0) + { fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred. safe_read ? + i = fread(buf, 1, bufsize, stdin); + ibuf = (uint32_t *)buf; fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero). maybe ctr < i here ? + for(ctr = 1; ctr < bufsize; ctr += sizeof(int)) + { + *ibuf ^= key; + ibuf++; + } + if (i > 0) + { safe_write ? full_write ? + if(fwrite(buf, 1, i, stdout) < i) + { + bb_error_msg_and_die ? fprintf(stderr, "xorpipe: error writing to output\n"); + exit(1); + } + } + + } + return 0; +} Ciao, Tito _______________________________________________ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox