Permalink
Browse files

Start C API work

  • Loading branch information...
bwalex committed Jul 3, 2011
1 parent adacd75 commit 05bf0a1b64b3c06fcee887d96281626c0a9fc9c3
Showing with 429 additions and 130 deletions.
  1. +6 −6 crypto-dev.c
  2. +10 −10 hdr.c
  3. +16 −15 io.c
  4. +29 −26 main.c
  5. +140 −0 tc-play-api.c
  6. +64 −0 tc-play-api.h
  7. +153 −69 tc-play.c
  8. +11 −4 tc-play.h
View
@@ -188,7 +188,7 @@ tc_cipher_chain_populate_keys(struct tc_cipher_chain *cipher_chain,
dummy_chain = dummy_chain->next) {
dummy_chain->key = alloc_safe_mem(dummy_chain->cipher->klen);
if (dummy_chain->key == NULL) {
- fprintf(stderr, "tc_decrypt: Could not allocate key "
+ tc_log(1, "tc_decrypt: Could not allocate key "
"memory\n");
return ENOMEM;
}
@@ -231,7 +231,7 @@ tc_encrypt(struct tc_cipher_chain *cipher_chain, unsigned char *key,
cipher_chain = cipher_chain->next) {
cipher_id = get_cryptodev_cipher_id(cipher_chain->cipher);
if (cipher_id < 0) {
- fprintf(stderr, "Cipher %s not found\n",
+ tc_log(1, "Cipher %s not found\n",
cipher_chain->cipher->name);
return ENOENT;
}
@@ -283,7 +283,7 @@ tc_decrypt(struct tc_cipher_chain *cipher_chain, unsigned char *key,
cipher_chain = cipher_chain->prev) {
cipher_id = get_cryptodev_cipher_id(cipher_chain->cipher);
if (cipher_id < 0) {
- fprintf(stderr, "Cipher %s not found\n",
+ tc_log(1, "Cipher %s not found\n",
cipher_chain->cipher->name);
return ENOENT;
}
@@ -344,15 +344,15 @@ apply_keyfiles(unsigned char *pass, size_t pass_memsz, const char *keyfiles[],
uint32_t crc;
if (pass_memsz < MAX_PASSSZ) {
- fprintf(stderr, "Not enough memory for password manipluation\n");
+ tc_log(1, "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");
+ tc_log(1, "Error allocating memory for keyfile pool\n");
return ENOMEM;
}
@@ -367,7 +367,7 @@ apply_keyfiles(unsigned char *pass, size_t pass_memsz, const char *keyfiles[],
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",
+ tc_log(1, "Error reading keyfile %s content\n",
keyfiles[k]);
free_safe_mem(kpool);
return EIO;
View
20 hdr.c
@@ -51,7 +51,7 @@ decrypt_hdr(struct tchdr_enc *ehdr, struct tc_cipher_chain *cipher_chain,
int error;
if ((dhdr = alloc_safe_mem(sizeof(struct tchdr_dec))) == NULL) {
- fprintf(stderr, "Error allocating safe tchdr_dec memory\n");
+ tc_log(1, "Error allocating safe tchdr_dec memory\n");
return NULL;
}
@@ -60,7 +60,7 @@ decrypt_hdr(struct tchdr_enc *ehdr, struct tc_cipher_chain *cipher_chain,
error = tc_decrypt(cipher_chain, key, iv, ehdr->enc,
sizeof(struct tchdr_dec), (unsigned char *)dhdr);
if (error) {
- fprintf(stderr, "Header decryption failed\n");
+ tc_log(1, "Header decryption failed\n");
free_safe_mem(dhdr);
return NULL;
}
@@ -105,7 +105,7 @@ verify_hdr(struct tchdr_dec *hdr)
case 1:
case 2:
/* Unsupported header version */
- fprintf(stderr, "Header version %d unsupported\n", hdr->tc_ver);
+ tc_log(1, "Header version %d unsupported\n", hdr->tc_ver);
return 0;
case 3:
@@ -129,22 +129,22 @@ create_hdr(unsigned char *pass, int passlen, struct pbkdf_prf_algo *prf_algo,
int error;
if ((dhdr = (struct tchdr_dec *)alloc_safe_mem(sizeof(*dhdr))) == NULL) {
- fprintf(stderr, "could not allocate safe dhdr memory\n");
+ tc_log(1, "could not allocate safe dhdr memory\n");
return NULL;
}
if ((ehdr = (struct tchdr_enc *)alloc_safe_mem(sizeof(*ehdr))) == NULL) {
- fprintf(stderr, "could not allocate safe ehdr memory\n");
+ tc_log(1, "could not allocate safe ehdr memory\n");
return NULL;
}
if ((key = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
- fprintf(stderr, "could not allocate safe key memory\n");
+ tc_log(1, "could not allocate safe key memory\n");
return NULL;
}
if ((error = get_random(ehdr->salt, sizeof(ehdr->salt))) != 0) {
- fprintf(stderr, "could not get salt\n");
+ tc_log(1, "could not get salt\n");
return NULL;
}
@@ -153,14 +153,14 @@ create_hdr(unsigned char *pass, int passlen, struct pbkdf_prf_algo *prf_algo,
prf_algo->iteration_count,
prf_algo->name, MAX_KEYSZ, key);
if (error) {
- fprintf(stderr, "could not derive key\n");
+ tc_log(1, "could not derive key\n");
return NULL;
}
memset(dhdr, 0, sizeof(*dhdr));
if ((error = get_random(dhdr->keys, sizeof(dhdr->keys))) != 0) {
- fprintf(stderr, "could not get key random bits\n");
+ tc_log(1, "could not get key random bits\n");
return NULL;
}
@@ -193,7 +193,7 @@ create_hdr(unsigned char *pass, int passlen, struct pbkdf_prf_algo *prf_algo,
error = tc_encrypt(cipher_chain, key, iv, (unsigned char *)dhdr,
sizeof(struct tchdr_dec), ehdr->enc);
if (error) {
- fprintf(stderr, "Header encryption failed\n");
+ tc_log(1, "Header encryption failed\n");
free_safe_mem(dhdr);
return NULL;
}
View
31 io.c
@@ -47,22 +47,22 @@ read_to_safe_mem(const char *file, off_t offset, size_t *sz)
int fd;
if ((fd = open(file, O_RDONLY)) < 0) {
- fprintf(stderr, "Error opening file %s\n", file);
+ tc_log(1, "Error opening file %s\n", file);
return NULL;
}
if ((mem = alloc_safe_mem(*sz)) == NULL) {
- fprintf(stderr, "Error allocating memory\n");
+ tc_log(1, "Error allocating memory\n");
goto out;
}
if ((lseek(fd, offset, SEEK_SET) < 0)) {
- fprintf(stderr, "Error seeking on file %s\n", file);
+ tc_log(1, "Error seeking on file %s\n", file);
goto m_err;
}
if ((r = read(fd, mem, *sz)) <= 0) {
- fprintf(stderr, "Error reading from file %s\n", file);
+ tc_log(1, "Error reading from file %s\n", file);
goto m_err;
}
@@ -88,13 +88,13 @@ get_random(unsigned char *buf, size_t len)
if ((fd = open("/dev/random", O_RDONLY)) < 0) {
- fprintf(stderr, "Error opening /dev/random\n");
+ tc_log(1, "Error opening /dev/random\n");
return -1;
}
while (rd < len) {
if ((r = read(fd, buf+rd, len-rd)) < 0) {
- fprintf(stderr, "Error reading from /dev/random\n");
+ tc_log(1, "Error reading from /dev/random\n");
close(fd);
return -1;
}
@@ -106,6 +106,7 @@ get_random(unsigned char *buf, size_t len)
return 0;
}
+/* XXX: improve secure_erase performance! */
int
secure_erase(const char *dev, size_t bytes, size_t blksz)
{
@@ -115,24 +116,24 @@ secure_erase(const char *dev, size_t bytes, size_t blksz)
ssize_t r, w;
if (blksz > MAX_BLKSZ) {
- fprintf(stderr, "blksz > MAX_BLKSZ\n");
+ tc_log(1, "blksz > MAX_BLKSZ\n");
return -1;
}
if ((fd_rand = open("/dev/urandom", O_RDONLY)) < 0) {
- fprintf(stderr, "Error opening /dev/urandom\n");
+ tc_log(1, "Error opening /dev/urandom\n");
return -1;
}
if ((fd = open(dev, O_WRONLY)) < 0) {
close(fd_rand);
- fprintf(stderr, "Error opening %s\n", dev);
+ tc_log(1, "Error opening %s\n", dev);
return -1;
}
while (erased < bytes) {
if ((r = read(fd_rand, buf, blksz)) < 0) {
- fprintf(stderr, "Error reading from /dev/urandom\n");
+ tc_log(1, "Error reading from /dev/urandom\n");
close(fd);
close(fd_rand);
return -1;
@@ -142,7 +143,7 @@ secure_erase(const char *dev, size_t bytes, size_t blksz)
continue;
if ((w = write(fd, buf, blksz)) < 0) {
- fprintf(stderr, "Error writing to %s\n", dev);
+ tc_log(1, "Error writing to %s\n", dev);
close(fd);
close(fd_rand);
return -1;
@@ -164,7 +165,7 @@ get_disk_info(const char *dev, size_t *blocks, size_t *bsize)
int fd;
if ((fd = open(dev, O_RDONLY)) < 0) {
- fprintf(stderr, "Error opening %s\n", dev);
+ tc_log(1, "Error opening %s\n", dev);
return -1;
}
@@ -189,18 +190,18 @@ write_mem(const char *dev, off_t offset, size_t blksz, void *mem, size_t bytes)
int fd;
if ((fd = open(dev, O_WRONLY)) < 0) {
- fprintf(stderr, "Error opening device %s\n", dev);
+ tc_log(1, "Error opening device %s\n", dev);
return -1;
}
if ((lseek(fd, offset, SEEK_SET) < 0)) {
- fprintf(stderr, "Error seeking on device %s\n", dev);
+ tc_log(1, "Error seeking on device %s\n", dev);
close(fd);
return -1;
}
if ((w = write(fd, mem, bytes)) <= 0) {
- fprintf(stderr, "Error writing to device %s\n", dev);
+ tc_log(1, "Error writing to device %s\n", dev);
close(fd);
return -1;
}
Oops, something went wrong.

0 comments on commit 05bf0a1

Please sign in to comment.