Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit of current state

  • Loading branch information...
commit fff721857f4cc85714c8a61ef55567ef8acab459 0 parents
@bwalex authored
Showing with 992 additions and 0 deletions.
  1. +107 −0 crc32.c
  2. +2 −0  crc32.h
  3. +773 −0 tc-play.c
  4. +110 −0 tc-play.h
107 crc32.c
@@ -0,0 +1,107 @@
+/*-
+ * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
+ * code or tables extracted from it, as desired without restriction.
+ *
+ * First, the polynomial itself and its table of feedback terms. The
+ * polynomial is
+ * X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
+ *
+ * Note that we take it "backwards" and put the highest-order term in
+ * the lowest-order bit. The X^32 term is "implied"; the LSB is the
+ * X^31 term, etc. The X^0 term (usually shown as "+1") results in
+ * the MSB being 1
+ *
+ * Note that the usual hardware shift register implementation, which
+ * is what we're using (we're merely optimizing it by doing eight-bit
+ * chunks at a time) shifts bits into the lowest-order term. In our
+ * implementation, that means shifting towards the right. Why do we
+ * do it this way? Because the calculated CRC must be transmitted in
+ * order from highest-order term to lowest-order term. UARTs transmit
+ * characters in order from LSB to MSB. By storing the CRC this way
+ * we hand it to the UART in the order low-byte to high-byte; the UART
+ * sends each low-bit to hight-bit; and the result is transmission bit
+ * by bit from highest- to lowest-order term without requiring any bit
+ * shuffling on our part. Reception works similarly
+ *
+ * The feedback terms table consists of 256, 32-bit entries. Notes
+ *
+ * The table can be generated at runtime if desired; code to do so
+ * is shown later. It might not be obvious, but the feedback
+ * terms simply represent the results of eight shift/xor opera
+ * tions for all combinations of data and CRC register values
+ *
+ * The values must be right-shifted by eight bits by the "updcrc
+ * logic; the shift must be unsigned (bring in zeroes). On some
+ * hardware you could probably optimize the shift in assembler by
+ * using byte-swap instructions
+ * polynomial $edb88320
+ */
+
+#include <stdlib.h>
+
+uint32_t crc32_tab[] = {
+ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
+ 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
+ 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
+ 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
+ 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
+ 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
+ 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
+ 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
+ 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
+ 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+ 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
+ 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
+ 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
+ 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
+ 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
+ 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
+ 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
+ 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
+ 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
+ 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
+ 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
+ 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
+ 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
+ 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
+ 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
+ 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
+ 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
+ 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
+ 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+ 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
+ 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
+ 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
+ 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
+ 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
+ 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
+ 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
+ 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
+ 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
+ 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
+ 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
+ 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
+};
+
+uint32_t
+crc32(const void *buf, size_t size)
+{
+ const uint8_t *p;
+ uint32_t crc;
+
+ p = buf;
+ crc = ~0U;
+
+ while (size--)
+ crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
+
+ return crc ^ ~0U;
+}
+
+uint32_t
+crc32_intermediate(uint32_t crc, uint8_t d)
+{
+ return crc32_tab[(crc ^ d) & 0xFF] ^ (crc >> 8);
+}
2  crc32.h
@@ -0,0 +1,2 @@
+uint32_t crc32(const void *buf, size_t size);
+uint32_t crc32_intermediate(uint32_t crc, uint8_t d);
773 tc-play.c
@@ -0,0 +1,773 @@
+/*
+ * Copyright (c) 2011 The DragonFly Project. All rights reserved.
+ *
+ * This code is derived from software contributed to The DragonFly Project
+ * by Alex Hornung <ahornung@gmail.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+#include <sys/endian.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+#include <err.h>
+#include <uuid.h>
+#include <termios.h>
+#include <libdevmapper.h>
+#include <openssl/evp.h>
+
+#include "crc32.h"
+#include "tc-play.h"
+
+/* XXX TODO:
+ * - LRW-benbi support? needs further work in dm-crypt and even opencrypto
+ * - secure buffer review (i.e: is everything that needs it using secure mem?)
+ * - mlockall? (at least MCL_FUTURE, which is the only one we support)
+ */
+
+#if 0
+/* Volume times:
+ * return wxDateTime ((time_t) (volumeTime / 1000ULL / 1000 / 10 - 134774ULL * 24 * 3600));
+ */
+#define VOLTIME_TO_TIME(volumeTime) \
+ ((time_t)(volumeTime / 1000ULL / 1000 / 10 - 134774ULL * 24 * 3600))
+
+/*
+Hi, dm-params:
+0 261632 crypt aes-xts-plain64 0 256 /dev/loop0 256
+
+Hi, dm-params: 0 261632 crypt aes-xts-plain64 0 256 /dev/loop0 256
+ 256 = off_mk_scope / sec_sz!
+ 261632 = sz_mk_scope / sec_sz!
+
+ aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8
+ iv off---^ block off--^
+
+Volume "/home/alex/tc-play/tctest.container" has been mounted.
+*/
+
+#endif
+
+/* Version of tc-play */
+#define MAJ_VER 0
+#define MIN_VER 2
+
+/* Comment out to disable debug info */
+#define DEBUG 1
+
+/* Endianess macros */
+#define BE_TO_HOST(n, v) v = be ## n ## toh(v)
+#define LE_TO_HOST(n, v) v = le ## n ## toh(v)
+
+
+/* Supported algorithms */
+struct pbkdf_prf_algo pbkdf_prf_algos[] = {
+ { "RIPEMD160", 2000, 1000 },
+ { "SHA512", 1000, 1000 },
+ { "whirlpool", 1000, 1000 },
+ { NULL, 0, 0 }
+};
+
+struct tc_crypto_algo tc_crypto_algos[] = {
+ { "AES-128-XTS", "aes-xts-plain", 32, 8 },
+ { "AES-256-XTS", "aes-xts-plain", 64, 8 },
+ { NULL, NULL, 0, 0 }
+};
+
+int
+hex2key(char *hex, size_t key_len, unsigned char *key)
+{
+ char hex_buf[3];
+ size_t key_idx;
+ hex_buf[2] = 0;
+ for (key_idx = 0; key_idx < key_len; ++key_idx) {
+ hex_buf[0] = *hex++;
+ hex_buf[1] = *hex++;
+ key[key_idx] = (unsigned char)strtoul(hex_buf, NULL, 16);
+ }
+ hex_buf[0] = 0;
+ hex_buf[1] = 0;
+
+ return 0;
+}
+
+#ifdef DEBUG
+void
+print_hex(unsigned char *buf, off_t start, size_t len)
+{
+ size_t i;
+
+ for (i = start; i < start+len; i++)
+ printf("%02x", buf[i]);
+
+ printf("\n");
+}
+#endif
+
+void *
+alloc_safe_mem(size_t req_sz)
+{
+ struct safe_mem_hdr *hdr;
+ struct safe_mem_tail *tail;
+ size_t alloc_sz;
+ void *mem, *user_mem;
+
+ alloc_sz = req_sz + sizeof(*hdr) + sizeof(*tail);
+ if ((mem = malloc(alloc_sz)) == NULL)
+ return NULL;
+
+ if (mlock(mem, alloc_sz) < 0) {
+ free(mem);
+ return NULL;
+ }
+
+ memset(mem, 0, alloc_sz);
+
+ hdr = (struct safe_mem_hdr *)mem;
+ tail = (struct safe_mem_tail *)(mem + alloc_sz - sizeof(*tail));
+ user_mem = mem + sizeof(*hdr);
+
+ strcpy(hdr->sig, "SAFEMEM");
+ strcpy(tail->sig, "SAFEMEM");
+ hdr->alloc_sz = alloc_sz;
+
+ return user_mem;
+}
+
+void
+free_safe_mem(void *mem)
+{
+ struct safe_mem_hdr *hdr;
+ struct safe_mem_tail *tail;
+
+ mem -= sizeof(*hdr);
+ hdr = (struct safe_mem_hdr *)mem;
+ tail = (struct safe_mem_tail *)(mem + hdr->alloc_sz - sizeof(*tail));
+
+ /* Integrity checks */
+ if ((memcmp(hdr->sig, "SAFEMEM\0", 8) != 0) ||
+ (memcmp(tail->sig, "SAFEMEM\0", 8) != 0)) {
+ fprintf(stderr, "BUG: safe_mem buffer under- or overflow!!!\n");
+ exit(1);
+ }
+
+ memset(mem, 0, hdr->alloc_sz);
+
+ free(mem);
+}
+
+void *
+read_to_safe_mem(const char *file, off_t offset, size_t *sz)
+{
+ void *mem = NULL;
+ int fd;
+
+ if ((fd = open(file, O_RDONLY)) < 0) {
+ fprintf(stderr, "Error opening file %s\n", file);
+ return NULL;
+ }
+
+ if ((mem = alloc_safe_mem(*sz)) == NULL) {
+ fprintf(stderr, "Error allocating memory\n");
+ goto out;
+ }
+
+ if ((lseek(fd, offset, SEEK_SET) < 0)) {
+ fprintf(stderr, "Error seeking on file %s\n", file);
+ goto m_err;
+ }
+
+ if ((*sz = read(fd, mem, *sz)) <= 0) {
+ fprintf(stderr, "Error reading from file %s\n", file);
+ goto m_err;
+ }
+
+out:
+ close(fd);
+ return mem;
+ /* NOT REACHED */
+
+m_err:
+ free_safe_mem(mem);
+ close(fd);
+ return NULL;
+}
+
+int
+tc_encrypt(const char *cipher_name, unsigned char *key, unsigned char *iv,
+ unsigned char *in, int in_len, unsigned char *out)
+{
+ const EVP_CIPHER *evp;
+ EVP_CIPHER_CTX ctx;
+ int outl, tmplen;
+
+ evp = EVP_get_cipherbyname(cipher_name);
+ if (evp == NULL) {
+ printf("Cipher %s not found\n", cipher_name);
+ return ENOENT;
+ }
+
+ EVP_CIPHER_CTX_init(&ctx);
+ EVP_EncryptInit(&ctx, evp, key, iv);
+ EVP_EncryptUpdate(&ctx, out, &outl, in, in_len);
+ EVP_EncryptFinal(&ctx, out + outl, &tmplen);
+
+ return 0;
+}
+
+int
+tc_decrypt(const char *cipher_name, unsigned char *key, unsigned char *iv,
+ unsigned char *in, int in_len, unsigned char *out)
+{
+ const EVP_CIPHER *evp;
+ EVP_CIPHER_CTX ctx;
+ int outl, tmplen;
+
+ evp = EVP_get_cipherbyname(cipher_name);
+ if (evp == NULL) {
+ printf("Cipher %s not found\n", cipher_name);
+ return ENOENT;
+ }
+
+ EVP_CIPHER_CTX_init(&ctx);
+ EVP_DecryptInit(&ctx, evp, key, iv);
+ EVP_DecryptUpdate(&ctx, out, &outl, in, in_len);
+ EVP_DecryptFinal(&ctx, out + outl, &tmplen);
+
+ return 0;
+}
+
+int
+pbkdf2(const char *pass, int passlen, const unsigned char *salt, int saltlen,
+ int iter, const char *hash_name, int keylen, unsigned char *out)
+{
+ const EVP_MD *md;
+ int r;
+
+ md = EVP_get_digestbyname(hash_name);
+ if (md == NULL) {
+ printf("Hash %s not found\n", hash_name);
+ return ENOENT;
+ }
+ r = PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, md,
+ keylen, out);
+
+ if (r == 0) {
+ printf("Error in PBKDF2\n");
+ return EINVAL;
+ }
+
+ return 0;
+}
+
+int
+read_passphrase(char *pass, size_t passlen)
+{
+ struct termios termios_old, termios_new;
+ ssize_t n;
+ int fd, r = 0, cfd = 0;
+
+ if ((fd = open("/dev/tty", O_RDONLY)) == -1) {
+ fd = STDIN_FILENO;
+ cfd = 1;
+ }
+
+ printf("Passphrase: ");
+ fflush(stdout);
+
+ memset(pass, 0, passlen);
+
+ tcgetattr(fd, &termios_old);
+ memcpy(&termios_new, &termios_old, sizeof(termios_new));
+ termios_new.c_lflag &= ~ECHO;
+ tcsetattr(fd, TCSAFLUSH, &termios_new);
+
+ n = read(fd, pass, passlen-1);
+ if (n > 0) {
+ pass[n-1] = '\0'; /* Strip trailing \n */
+ } else {
+ r = EIO;
+ }
+
+ if (cfd)
+ close(fd);
+
+ tcsetattr(fd, TCSAFLUSH, &termios_old);
+ putchar('\n');
+
+ return r;
+}
+
+struct tchdr_dec *
+decrypt_hdr(struct tchdr_enc *ehdr, char *algo, unsigned char *key)
+{
+ struct tchdr_dec *dhdr;
+ unsigned char iv[128];
+ int error;
+
+ if ((dhdr = alloc_safe_mem(sizeof(struct tchdr_dec))) == NULL) {
+ fprintf(stderr, "Error allocating safe tchdr_dec memory\n");
+ return NULL;
+ }
+
+ memset(iv, 0, sizeof(iv));
+
+ error = tc_decrypt(algo, key, iv, ehdr->enc, sizeof(struct tchdr_dec),
+ (unsigned char *)dhdr);
+ if (error) {
+ fprintf(stderr, "Header decryption failed");
+ free_safe_mem(dhdr);
+ return NULL;
+ }
+
+ BE_TO_HOST(16, dhdr->tc_ver);
+ LE_TO_HOST(16, dhdr->tc_min_ver);
+ BE_TO_HOST(32, dhdr->crc_keys);
+ BE_TO_HOST(64, dhdr->vol_ctime);
+ BE_TO_HOST(64, dhdr->hdr_ctime);
+ BE_TO_HOST(64, dhdr->sz_hidvol);
+ BE_TO_HOST(64, dhdr->sz_vol);
+ BE_TO_HOST(64, dhdr->off_mk_scope);
+ BE_TO_HOST(64, dhdr->sz_mk_scope);
+ BE_TO_HOST(32, dhdr->flags);
+ BE_TO_HOST(32, dhdr->sec_sz);
+ BE_TO_HOST(32, dhdr->crc_dhdr);
+
+ return dhdr;
+}
+
+int
+verify_hdr(struct tchdr_dec *hdr)
+{
+ uint32_t crc;
+
+ if (memcmp(hdr->tc_str, TC_SIG, sizeof(hdr->tc_str)) != 0) {
+#ifdef DEBUG
+ fprintf(stderr, "Signature mismatch\n");
+#endif
+ return 0;
+ }
+
+ crc = crc32((void *)&hdr->keys, 256);
+ if (crc != hdr->crc_keys) {
+#ifdef DEBUG
+ fprintf(stderr, "CRC32 mismatch (crc_keys)\n");
+#endif
+ return 0;
+ }
+
+ return 1;
+}
+
+int
+apply_keyfiles(unsigned char *pass, size_t pass_memsz, const char *keyfiles[],
+ int nkeyfiles)
+{
+ int pl, k;
+ unsigned char *kpool;
+ unsigned char *kdata;
+ int kpool_idx;
+ size_t i, kdata_sz;
+ uint32_t crc;
+
+ if (pass_memsz < MAX_PASSSZ) {
+ fprintf(stderr, "Not enough memory for password manipluation\n");
+ return ENOMEM;
+ }
+
+ pl = strlen(pass);
+ memset(pass+pl, 0, MAX_PASSSZ-pl);
+
+ if ((kpool = alloc_safe_mem(KPOOL_SZ)) == NULL) {
+ fprintf(stderr, "Error allocating memory for keyfile pool\n");
+ return ENOMEM;
+ }
+
+ memset(kpool, 0, KPOOL_SZ);
+
+ for (k = 0; k < nkeyfiles; k++) {
+#ifdef DEBUG
+ printf("Loading keyfile %s into kpool\n", keyfiles[k]);
+#endif
+ kpool_idx = 0;
+ crc = ~0U;
+ kdata_sz = MAX_KFILE_SZ;
+
+ if ((kdata = read_to_safe_mem(keyfiles[k], 0, &kdata_sz)) == NULL) {
+ fprintf(stderr, "Error reading keyfile %s content\n",
+ keyfiles[k]);
+ free_safe_mem(kpool);
+ return EIO;
+ }
+
+ for (i = 0; i < kdata_sz; i++) {
+ crc = crc32_intermediate(crc, kdata[i]);
+
+ kpool[kpool_idx++] += (unsigned char)(crc >> 24);
+ kpool[kpool_idx++] += (unsigned char)(crc >> 16);
+ kpool[kpool_idx++] += (unsigned char)(crc >> 8);
+ kpool[kpool_idx++] += (unsigned char)(crc);
+
+ /* Wrap around */
+ if (kpool_idx == KPOOL_SZ)
+ kpool_idx = 0;
+ }
+
+ free_safe_mem(kdata);
+ }
+
+#ifdef DEBUG
+ printf("Applying kpool to passphrase\n");
+#endif
+ /* Apply keyfile pool to passphrase */
+ for (i = 0; i < KPOOL_SZ; i++)
+ pass[i] += kpool[i];
+
+ free_safe_mem(kpool);
+
+ return 0;
+}
+
+void
+print_info(struct tcplay_info *info)
+{
+ printf("PBKDF2 PRF:\t\t%s\n", info->pbkdf_prf->name);
+ printf("PBKDF2 iterations:\t%d\n", info->pbkdf_prf->iteration_count);
+ printf("Cipher:\t\t\t%s\n", info->cipher->name);
+ printf("Key Length:\t\t%d bits\n", info->cipher->klen*8);
+ printf("CRC Key Data:\t\t%#x\n", info->hdr->crc_keys);
+}
+
+struct tcplay_info *
+new_info(const char *dev, struct tc_crypto_algo *cipher,
+ struct pbkdf_prf_algo *prf, struct tchdr_dec *hdr, off_t start)
+{
+ struct tcplay_info *info;
+ size_t i;
+
+ if ((info = (struct tcplay_info *)alloc_safe_mem(sizeof(*info))) == NULL) {
+ fprintf(stderr, "could not allocate safe info memory");
+ return NULL;
+ }
+
+ info->dev = dev;
+ info->cipher = cipher;
+ info->pbkdf_prf = prf;
+ info->start = start;
+ info->hdr = hdr;
+ info->size = hdr->sz_mk_scope / hdr->sec_sz; /* volume size */
+ info->skip = hdr->off_mk_scope / hdr->sec_sz; /* iv skip */
+ info->offset = hdr->off_mk_scope / hdr->sec_sz; /* block offset */
+
+ for (i = 0; i < cipher->klen; i++) {
+ sprintf(&info->key[i*2], "%02x", hdr->keys[i]);
+ }
+
+ return info;
+}
+
+int
+dm_setup(const char *mapname, struct tcplay_info *info)
+{
+ struct dm_task *dmt = NULL;
+ struct dm_info dmi;
+ char *params = NULL;
+ char *uu;
+ uint32_t status;
+ int ret = 0;
+
+ if ((params = alloc_safe_mem(512)) == NULL) {
+ fprintf(stderr, "could not allocate safe parameters memory");
+ return ENOMEM;
+
+ }
+
+ /* aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8 */
+ /* iv off---^ block off--^ */
+ snprintf(params, 512, "%s %s %"PRIu64 " %s %"PRIu64,
+ info->cipher->dm_crypt_str, info->key,
+ info->skip, info->dev, info->offset);
+#ifdef DEBUG
+ printf("Params: %s\n", params);
+#endif
+ if ((dmt = dm_task_create(DM_DEVICE_CREATE)) == NULL) {
+ fprintf(stderr, "dm_task_create failed\n");
+ ret = -1;
+ goto out;
+ }
+
+ if ((dm_task_set_name(dmt, mapname)) == 0) {
+ fprintf(stderr, "dm_task_set_name failed\n");
+ ret = -1;
+ goto out;
+ }
+
+ uuid_create(&info->uuid, &status);
+ if (status != uuid_s_ok) {
+ fprintf(stderr, "uuid_create failed\n");
+ ret = -1;
+ goto out;
+ }
+
+ uuid_to_string(&info->uuid, &uu, &status);
+ if (uu == NULL) {
+ fprintf(stderr, "uuid_to_string failed\n");
+ ret = -1;
+ goto out;
+ }
+
+ if ((dm_task_set_uuid(dmt, uu)) == 0) {
+ free(uu);
+ fprintf(stderr, "dm_task_set_uuid failed\n");
+ ret = -1;
+ goto out;
+ }
+ free(uu);
+
+ if ((dm_task_add_target(dmt, info->start, info->size, "crypt", params)) == 0) {
+ fprintf(stderr, "dm_task_add_target failed\n");
+ ret = -1;
+ goto out;
+ }
+
+ if ((dm_task_run(dmt)) == 0) {
+ fprintf(stderr, "dm_task_task_run failed\n");
+ ret = -1;
+ goto out;
+ }
+
+ if ((dm_task_get_info(dmt, &dmi)) == 0) {
+ fprintf(stderr, "dm_task_get info failed\n");
+ /* XXX: probably do more than just erroring out... */
+ ret = -1;
+ goto out;
+ }
+
+out:
+ free_safe_mem(params);
+ if (dmt)
+ dm_task_destroy(dmt);
+
+ return ret;
+}
+
+static void
+usage(void)
+{
+ fprintf(stderr,
+ "Usage: tc-play <command> [options]\n"
+ "Valid commands and its arguments are:\n"
+ " -i\n"
+ "\t Gives information about the TC volume specified by -d\n"
+ " -m <mapping name>\n"
+ "\t Creates a dm-crypt mapping for the device specified by -d\n"
+ "Valid options and its arguments are:\n"
+ " -d <device path>\n"
+ "\t specifies the path to the volume to operate on (e.g. /dev/da0s1)\n"
+ " -s <disk path>\n"
+ "\t specifies that the disk (e.g. /dev/da0) is using system encryption\n"
+ );
+
+ exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ const char *dev = NULL, *sys_dev = NULL, *map_name = NULL;
+ const char *keyfiles[MAX_KEYFILES];
+ char *pass;
+ unsigned char *key;
+ struct tchdr_enc *ehdr;
+ struct tchdr_dec *dhdr;
+ struct tcplay_info *info;
+ int i, j, found;
+ int nkeyfiles;
+ int ch, error;
+ int sflag = 0, iflag = 0, mflag = 0;
+ size_t sz;
+
+ OpenSSL_add_all_algorithms();
+
+ nkeyfiles = 0;
+
+ while ((ch = getopt(argc, argv, "d:ik:m:s:v")) != -1) {
+ switch(ch) {
+ case 'd':
+ dev = optarg;
+ break;
+ case 'i':
+ iflag = 1;
+ break;
+ case 'k':
+ keyfiles[nkeyfiles++] = optarg;
+ break;
+ case 'm':
+ mflag = 1;
+ map_name = optarg;
+ break;
+ case 's':
+ sflag = 1;
+ sys_dev = optarg;
+ break;
+ case 'v':
+ printf("tc-play v%d.%d\n", MAJ_VER, MIN_VER);
+ exit(0);
+ /* NOT REACHED */
+ case 'h':
+ case '?':
+ default:
+ usage();
+ /* NOT REACHED */
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ /* Check arguments */
+ if (!((mflag || iflag) && dev != NULL) ||
+ (mflag && iflag) ||
+ (sflag && (sys_dev == NULL)) ||
+ (mflag && (map_name == NULL))) {
+ usage();
+ /* NOT REACHED */
+ }
+
+ sz = HDRSZ;
+ ehdr = (struct tchdr_enc *)read_to_safe_mem((sflag) ? sys_dev : dev,
+ (sflag) ? HDR_OFFSET_SYS : 0, &sz);
+ if (ehdr == NULL) {
+ err(1, "read hdr_enc: %s", dev);
+ }
+
+ if ((pass = alloc_safe_mem(MAX_PASSSZ)) == NULL) {
+ err(1, "could not allocate safe passphrase memory");
+ }
+
+ if ((key = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
+ err(1, "could not allocate safe key memory");
+ }
+
+ if ((error = read_passphrase(pass, MAX_PASSSZ))) {
+ err(1, "could not read passphrase");
+ }
+
+ if (nkeyfiles > 0) {
+ /* Apply keyfiles to 'pass' */
+ if ((error = apply_keyfiles(pass, MAX_PASSSZ, keyfiles,
+ nkeyfiles))) {
+ err(1, "could not apply keyfiles");
+ }
+ }
+
+ /* Start search for correct algorithm combination */
+ found = 0;
+ for (i = 0; !found && pbkdf_prf_algos[i].name != NULL; i++) {
+#ifdef DEBUG
+ printf("\nTrying PRF algo %s (%d)\n", pbkdf_prf_algos[i].name,
+ ((sflag) ? pbkdf_prf_algos[i].sysenc_iteration_count :
+ pbkdf_prf_algos[i].iteration_count));
+ printf("Salt: ");
+ print_hex(ehdr->salt, 0, sizeof(ehdr->salt));
+#endif
+ error = pbkdf2(pass, (nkeyfiles > 0)?MAX_PASSSZ:strlen(pass),
+ ehdr->salt, sizeof(ehdr->salt),
+ ((sflag) ? pbkdf_prf_algos[i].sysenc_iteration_count :
+ pbkdf_prf_algos[i].iteration_count),
+ pbkdf_prf_algos[i].name, MAX_KEYSZ, key);
+
+ if (error)
+ continue;
+
+#if 0
+ printf("Derived Key: ");
+ print_hex(key, 0, MAX_KEYSZ);
+#endif
+
+ for (j = 0; !found && tc_crypto_algos[j].name != NULL; j++) {
+#ifdef DEBUG
+ printf("\nTrying cipher %s\n", tc_crypto_algos[j].name);
+#endif
+
+ dhdr = decrypt_hdr(ehdr, tc_crypto_algos[j].name, key);
+ if (dhdr == NULL) {
+ continue;
+ }
+
+ if (verify_hdr(dhdr)) {
+#ifdef DEBUG
+ printf("All happy!\n");
+ printf("tc_str: %.4s, tc_ver: %zd, tc_min_ver: %zd, "
+ "crc_keys: %d, sz_vol: %"PRIu64", "
+ "off_mk_scope: %"PRIu64", sz_mk_scope: %"PRIu64", "
+ "flags: %d, sec_sz: %d crc_dhdr: %d\n",
+ dhdr->tc_str, dhdr->tc_ver, dhdr->tc_min_ver,
+ dhdr->crc_keys, dhdr->sz_vol, dhdr->off_mk_scope,
+ dhdr->sz_mk_scope, dhdr->flags, dhdr->sec_sz,
+ dhdr->crc_dhdr);
+#endif
+ found = 1;
+ }
+ }
+ }
+
+ free_safe_mem(key);
+
+ if (!found) {
+ fprintf(stderr, "Incorrect password or not a TrueCrypt volume\n");
+ free_safe_mem(pass);
+ return 1;
+ }
+
+ if ((info = new_info(dev, &tc_crypto_algos[j-1], &pbkdf_prf_algos[i-1],
+ dhdr, 0)) == NULL) {
+ err(1, "could not allocate safe info memory");
+ }
+
+ if (iflag) {
+ print_info(info);
+ } else if (mflag) {
+ if ((error = dm_setup(map_name, info)) != 0) {
+ err(1, "could not set up dm-crypt mapping");
+ }
+
+ printf("All ok!");
+ }
+
+ free_safe_mem(pass);
+ free_safe_mem(info);
+
+ return 0;
+}
110 tc-play.h
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2011 The DragonFly Project. All rights reserved.
+ *
+ * This code is derived from software contributed to The DragonFly Project
+ * by Alex Hornung <ahornung@gmail.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#define MAX_BLKSZ 4096
+#define MAX_KEYSZ 128
+#define HDRSZ 512
+#define HDR_OFFSET_SYS 31744 /* 512 * (63 -1) */
+#define TC_SIG "TRUE"
+#define MAX_PASSSZ 64
+#define KPOOL_SZ 64
+#define MAX_KFILE_SZ 1048576
+#define MAX_KEYFILES 256
+
+struct pbkdf_prf_algo {
+ char *name;
+ int iteration_count;
+ int sysenc_iteration_count;
+};
+
+struct tc_crypto_algo {
+ char *name;
+ char *dm_crypt_str;
+ int klen;
+ int ivlen;
+};
+
+struct tchdr_enc {
+ unsigned char salt[64]; /* Salt for PBKDF */
+ unsigned char enc[448]; /* Encrypted part of the header */
+} __attribute__((__packed__));
+
+struct tchdr_dec {
+ char tc_str[4]; /* ASCII string "TRUE" */
+ uint16_t tc_ver; /* Volume header format version */
+ uint16_t tc_min_ver;
+ uint32_t crc_keys; /* CRC32 of the key section */
+ uint64_t vol_ctime; /* Volume creation time */
+ uint64_t hdr_ctime; /* Header creation time */
+ uint64_t sz_hidvol; /* Size of hidden volume (set to zero
+ in non-hidden volumes) */
+ uint64_t sz_vol; /* Size of volume */
+ uint64_t off_mk_scope; /* Byte offset of the start of the
+ master key scope */
+ uint64_t sz_mk_scope; /* Size of the encrypted area within
+ the master key scope */
+ uint32_t flags; /* Flag bits
+ (bit 0: system encryption;
+ bit 1: non-system in-place-encrypted volume;
+ bits 2–31 are reserved) */
+ uint32_t sec_sz; /* Sector size (in bytes) */
+ unsigned char unused3[120];
+ uint32_t crc_dhdr; /* CRC32 of dec. header (except keys) */
+ unsigned char keys[256];
+} __attribute__((__packed__));
+
+struct tcplay_info {
+ const char *dev;
+ struct tchdr_dec *hdr;
+ struct tc_crypto_algo *cipher;
+ struct pbkdf_prf_algo *pbkdf_prf;
+ char key[MAX_KEYSZ*2];
+ off_t start; /* Logical volume offset in table */
+ size_t size; /* Volume size */
+
+ off_t skip; /* IV offset */
+ off_t offset; /* Block offset */
+
+ /* Populated by dm_setup */
+ uuid_t uuid;
+};
+
+struct safe_mem_hdr {
+ struct safe_mem_tail *tail;
+ size_t alloc_sz;
+ char sig[8]; /* SAFEMEM */
+};
+
+struct safe_mem_tail {
+ char sig[8]; /* SAFEMEM */
+};
Please sign in to comment.
Something went wrong with that request. Please try again.