/* * TEA Total Copyright (c) Alex Holden 2000, 2001. * * This code is public domain; you can do whatever you want with it, though I * would prefer you to contribute any bug fixes back to me if possible. * This software comes with NO WARRANTY WHATSOEVER, not even the implied * warranties of merchantability and fitness for a particular purpose. * * tea-kgen.c: The tea-kgen applet. */ #include #include "teatotal.h" #include "tea-kgen.h" #ifdef BUILD_PPKEYS #include "readkey.h" #include "btea.h" #endif /* * Initialise the tea_kgen applet. */ void init_tea_kgen(teastate *state) { /* Check for the help flag */ if(getflag(state->ga, 'h', "help")) usage(state); #ifdef BUILD_PPKEYS /* Get the input key password */ state->inkeypass = getarg(state->ga, 'p', NULL); /* Get the output key password */ state->outkeypass = getarg(state->ga, 'k', NULL); /* Check if we should password protect the generated key */ state->protkey = getflag(state->ga, 'P', NULL); /* Check if we should convert the specified key file instead of * generating a new one */ state->convkey = getflag(state->ga, 'c', NULL); #endif /* The first unused argument is the name of the key file */ state->keyfile = getarg(state->ga, 0, NULL); /* Check if "-" (stdout) was specified */ if(state->keyfile && (state->keyfile[0] == '-') && !state->keyfile[1]) state->keyfile = NULL; } /* * The tea-kgen applet. */ void do_tea_kgen(teastate *state) { u32 *l, k[4]; u8 binkey[20]; int i, fd; char buf[KEY_LENGTH + 2], *p; /* Write the headers */ memcpy(buf, KEY_HEADER, (sizeof(KEY_HEADER) - 1)); l = (u32 *)binkey; *l = KEY_MAGIC; #ifdef BUILD_PPKEYS /* Read the specified input key, or generate a new one */ if(state->convkey) read_key(state); else generate_key(state); #else /* Generate a new key */ generate_key(state); #endif /* Load the key as big endian */ l = (u32 *)&binkey[4]; for(i = 0; i < 4; i++) #if BYTE_ORDER == LITTLE_ENDIAN l[i] = swapu32(state->k[i]); #else l[i] = state->k[i]; #endif #ifdef BUILD_PPKEYS /* If asked to encrypt the key */ if(state->protkey) { /* Read the password to encrypt the key with */ read_pass(state, k, 0, 1); /* Encrypt the key */ #if BYTE_ORDER == LITTLE_ENDIAN btea((u32 *)binkey, k, (20 / 4), 0, 1); #else btea((u32 *)binkey, k, (20 / 4), 0, 0); #endif /* Set the key type to non encrypted */ buf[sizeof(KEY_HEADER) - 1] = KEY_TEAENC; } else { /* Set the key type to non encrypted */ buf[sizeof(KEY_HEADER) - 1] = KEY_NOENC; } #else /* Set the key type to non encrypted */ buf[sizeof(KEY_HEADER) - 1] = KEY_NOENC; #endif /* Open the output key file */ fd = openwrite(state->keyfile); /* Convert the binary key data to a hex ascii string */ p = &buf[sizeof(KEY_HEADER)]; for(i = 0; i < 20; i++) { inttohex(binkey[i], p); p+= 2; } #ifdef DOS_LINE_ENDINGS *p++ = '\r'; #endif *p = '\n'; /* Write the string out to the key file (or stdout) */ #ifdef DOS_LINE_ENDINGS if(safe_write(fd, buf, (KEY_LENGTH + 2)) != (KEY_LENGTH + 2)) #else if(safe_write(fd, buf, (KEY_LENGTH + 1)) != (KEY_LENGTH + 1)) #endif die("Key write failed"); /* Close the key file, unless it is stdout */ if(state->keyfile) close(fd); }