General cleanup: comments and test cases.

Also removed unused function.
This commit is contained in:
Érico Rolim 2020-10-04 00:01:58 -03:00
parent 7803af405c
commit c10edd5180
4 changed files with 41 additions and 11 deletions

View File

@ -1,3 +1,21 @@
/*
* The functions in this file are responsible for most of the special
* functionality of the purr/meow utilities, which is encrypted pastes
* that the server can't access. They can be configured with the RANDOMIZE_IV,
* ENCODE_BASE_64 and DECODE_BASE_64 macros from purr.h.
*
* RANDOMIZE_IV: IV used for AES encryption is randomized or all zero.
*
* ENCODE_BASE_64: the binary data generated by the encryption process is
* encoded as base64.
*
* DECODE_BASE_64: the data returned by the server is decoded, assuming its
* encoding is base64.
*
* Having all these macros defined is the default mode of operation, and what's
* expected by the other PurritoBin clients.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
@ -14,8 +32,7 @@
#include "mmap_file.h"
/*
* This function takes an mmap_file struct, and creates an encrypted buffer from it.
* The created file is passed to an atexit function so it can be deleted automatically.
* This function takes an mmap_file struct, and returns an encrypted mmap_file from it.
* Args:
* file: mmap_file for the input file
* keyp: will receive the newly generated random key
@ -64,7 +81,7 @@ struct mmap_file encrypt_mmap(struct mmap_file file, uint8_t **keyp, uint8_t **i
}
memcpy(rv.data, file.data, file.size);
// PKCS#5 padding
// PKCS7 padding
memset(rv.data + file.size, padding, padding);
br_aes_big_cbcenc_keys br = { 0 };
@ -101,6 +118,13 @@ struct mmap_file encrypt_mmap(struct mmap_file file, uint8_t **keyp, uint8_t **i
return rv;
}
/*
* This function takes an mmap_file struct, and returns a decrypted buffer from it.
* Args:
* file: mmap_file for the input file
* keyp: the key used to encrypt the paste
* ivp: the iv used to encrypt the paste
*/
struct mmap_file decrypt_mmap(struct mmap_file file, const uint8_t *key, const uint8_t *iv)
{
struct mmap_file rv = {.size = file.size, .prot = PROT_MEM, .flags = MAP_MEM};

View File

@ -5,7 +5,8 @@
#include "purr.h"
/*
* Set print to true to print value, otherwise returns dynamic string with the number
* Set print to true to print value to stdout,
* otherwise returns dynamically allocated string with the number
*/
char *print_hex(const uint8_t *buf, int len, bool print)
{
@ -52,6 +53,11 @@ static uint8_t assemble_u8(const char *cs)
return rv;
}
/*
* Decode string containing hexadecimal numbers as printed by print_hex().
* len should be half the length of the input string, since it's the final length
* of the output array.
*/
int decode_hex(const char *s, uint8_t *output, int len)
{
for (int i = 0; i < len; i++) {

View File

@ -11,13 +11,7 @@
char *redirect_link = NULL;
static struct gemini_link_node *gimme_node(void)
{
struct gemini_link_node *node = calloc(1, sizeof *node);
return node;
}
static bool is_terminator(char c)
static bool is_terminator(int c)
{
return c == '\n' || c == '\r' || c == 0;
}

View File

@ -50,6 +50,12 @@ int main()
const char *expected = "1202124ca8";
const char *result = print_hex(buf, sizeof buf, false);
rv = compare_strings(expected, result, "print_hex") ? 1 : rv;
const char *hex = "0403124574";
uint8_t buf_expected[] = {0x04, 0x03, 0x12, 0x45, 0x74};
uint8_t buf_result[sizeof buf_expected];
int err = decode_hex(hex, buf_result, sizeof buf_result);
rv = compare_arrays(buf_expected, buf_result, sizeof buf_result, "decode_hex") ? 1 : rv;
assert(err == 0);
}
{