mirror of
https://gitlab.kit.edu/kit/scc/sys/mail/exim-encrypt-dlfunc.git
synced 2025-12-06 09:33:56 +01:00
Wrote decrypt tool for sodium_crypto_box_seal plus matching tests.
Lots of code cleanups.
This commit is contained in:
66
src/common.c
66
src/common.c
@ -3,31 +3,83 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <sodium.h>
|
||||
#include "common.h"
|
||||
|
||||
//#define MIN_KEY_SIZE (crypto_box_SECRETKEYBYTES < crypto_box_PUBLICKEYBYTES ? crypto_box_SECRETKEYBYTES : crypto_box_PUBLICKEYBYTES)
|
||||
//#define MAX_KEY_SIZE (crypto_box_SECRETKEYBYTES > crypto_box_PUBLICKEYBYTES ? crypto_box_SECRETKEYBYTES : crypto_box_PUBLICKEYBYTES)
|
||||
|
||||
char *read_first_line(const char *filename) {
|
||||
FILE *stream;
|
||||
char *cipherstring;
|
||||
size_t len = 0;
|
||||
ssize_t nread;
|
||||
bool input_is_stdin = false;
|
||||
|
||||
// open file
|
||||
stream = fopen(filename, "r");
|
||||
if (stream == NULL) {
|
||||
perror("Error opening file");
|
||||
exit(EXIT_FAILURE);
|
||||
// open file (use stdin for '-')
|
||||
if (!strncmp(filename, "-", 1)) {
|
||||
stream = stdin;
|
||||
input_is_stdin = true;
|
||||
} else {
|
||||
stream = fopen(filename, "r");
|
||||
if (stream == NULL) {
|
||||
fprintf(stderr, "[ERROR] Error opening file %s\n\n", filename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
nread = getline(&cipherstring, &len, stream);
|
||||
|
||||
if (nread == -1) {
|
||||
if (nread == -1 && !feof(stream)) {
|
||||
perror("getline: ");
|
||||
}
|
||||
fclose(stream);
|
||||
if (input_is_stdin == false) {
|
||||
fclose(stream);
|
||||
}
|
||||
|
||||
// remove trailing newline
|
||||
cipherstring[strcspn(cipherstring, "\r\n")] = 0;
|
||||
|
||||
return cipherstring;
|
||||
}
|
||||
|
||||
char *read_password_file(const char *filename, size_t keysize, size_t *length) {
|
||||
FILE *stream;
|
||||
char *contents;
|
||||
ssize_t nread;
|
||||
bool input_is_stdin = false;
|
||||
|
||||
contents = malloc(keysize + 1);
|
||||
sodium_memzero(contents, keysize + 1);
|
||||
|
||||
// open file (use stdin for '-')
|
||||
if (!strncmp(filename, "-", 1)) {
|
||||
stream = stdin;
|
||||
input_is_stdin = true;
|
||||
} else {
|
||||
stream = fopen(filename, "r");
|
||||
if (stream == NULL) {
|
||||
fprintf(stderr, "[ERROR] Error opening file %s\n\n", filename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
nread = fread(contents, sizeof(char), keysize, stream);
|
||||
|
||||
if (nread < 0) {
|
||||
fprintf(stderr, "[ERROR] reading from %s failed\n\n", filename);
|
||||
exit(EXIT_FAILURE);
|
||||
} else {
|
||||
*length = (size_t) nread;
|
||||
}
|
||||
|
||||
if (input_is_stdin == false) {
|
||||
fclose(stream);
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
|
||||
int base64_decode_string(const char *input, unsigned char **outstring, size_t *outlen) {
|
||||
size_t input_len = strlen(input);
|
||||
size_t outmaxlen = input_len / 4 * 3;
|
||||
|
||||
Reference in New Issue
Block a user