Wrong ciphertext length in c libgcrypt - c

So, i am trying to encrypt and decrypt a string, using libgcrypt library (version 1.8.7) on arch and at this moment i have tried 2 modes: CBC and GCM (not sure about GCM, so let's solve the CBC first), but the same problem appears.
I padd the string and then, encrypt it block by block. Sometimes, this happens chaotically by the way, the gcry_cipher_encrypt function returns wrong amount of bytes (5, 7, 11...), but if i understood correctly, the output should be 16 bytes (128 bits). The same thing happens with decryption, that i do the exact same way, block by block. I'm using the same GCRY handler with through out the encryption or decryption process and it feels like i'm really missing something... Here is an example, only encryption in CBC mode, to make it easier to find the problem.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gcrypt.h>
// Define cipher details
#define GCRY_CIPHER GCRY_CIPHER_AES256
#define GCRY_C_MODE GCRY_CIPHER_MODE_CBC
char * encrypt_block(gcry_cipher_hd_t handler, unsigned char * key, unsigned char * input) {
size_t key_length = 32;
size_t blk_length = 16;
// Encryption result variable
unsigned char * enc = (char *) calloc(16, sizeof(char));
// Error variable
gcry_error_t err = 0;
// Set key
err = gcry_cipher_setkey(handler, key, key_length);
if (err) {
printf("Couldn't set the key!\n%s\n%s\n", gcry_strsource(err), gcry_strerror(err));
exit(-1);
}
// Start encryption process
err = gcry_cipher_encrypt(handler, enc, blk_length, input, blk_length);
if (err) {
printf("Couldn't encrypt!\n%s\n%s\n", gcry_strsource(err), gcry_strerror(err));
exit(-1);
}
if (strlen(enc) != 16) {
printf("\n\nCORRUPTED BLOCK!\n\n");
}
// Printing the block result
printf("\nENC BLOCK:\t%d\t", strlen(enc));
for (unsigned short int i = 0; i < strlen(enc); ++i) {
printf("%X ", enc[i]);
}
printf("\n");
return enc;
}
int main() {
// Creating basic variables
unsigned char * input = (char *) calloc(2048, sizeof(char));
unsigned char * key = (char *) calloc(32, sizeof(char));
unsigned char * iv = (char *) calloc(16, sizeof(char));
// Taking user input
printf("Input (2048 max): ");
scanf(" %[^\n]", input);
printf("Key (32 max): ");
scanf(" %[^\n]", key);
printf("RAW DATA:\n\tinput: %d\t%s\n\tkey: %d\t%s\n\n", strlen(input), input, strlen(key), key);
// Create GCRY handler
gcry_cipher_hd_t handler;
gcry_error_t err = 0;
// Initialize cipher handler
err = gcry_cipher_open(&handler, GCRY_CIPHER, GCRY_C_MODE, 0);
if (err) {
printf("Couldn't initialize the cipher!\n%s\n%s\n", gcry_strsource(err), gcry_strerror(err));
exit(-1);
}
// Add padding to the input
if ((strlen(input) % 16) != 0) {
for (unsigned short int i = 0; i < (((strlen(input) / 16) * 16) - strlen(input)); ++i) {
strcat(input, "X");
}
}
// Add padding to the key
if (strlen(key) < 32) {
for (unsigned short int i = strlen(key); i < 32; ++i) {
key[i] = 0x0058;
}
}
// Generate random IV
char charset[] = "abcdefghijklmnopqrstuvwxyz0123456789";
unsigned short int iv_size = 16;
for (unsigned short int i = 0; i < iv_size; ++i) {
unsigned short int index = rand() % (unsigned short int) (sizeof charset - 1);
iv[i] = charset[index];
}
// Set the IV
err = gcry_cipher_setiv(handler, iv, 16);
if (err) {
printf("Couldn't set the IV!\n%s\n%s\n", gcry_strsource(err), gcry_strerror(err));
exit(-1);
}
printf("ENC DATA:\n\tinput: %d\t%s\n\tkey: %d\t%s\n\tiv: %d\t%s\n\n", strlen(input), input, strlen(key), key, strlen(iv), iv);
// Create encryption variables
unsigned char * input_buffer = (char *) calloc(16, sizeof(char));
unsigned char * enc_buffer = (char *) calloc(16, sizeof(char));
unsigned char * out = (char *) calloc(strlen(input), sizeof(char));
// Start encryption process block by block
for (unsigned short int i = 0; i < (strlen(input) / 16); ++i) {
// Create a new block
for (unsigned short int j = 0; j < 16; ++j) {
input_buffer[j] = input[(i * 16) + j];
}
printf("\nENC INPUT:\t%d\t%s\n", strlen(input_buffer), input_buffer);
// Check if this is a final round
if (i == ((strlen(input) / 16) - 1)) {
err = gcry_cipher_final(handler);
}
// Start encrypting the block
enc_buffer = encrypt_block(handler, key, input_buffer);
// Adding up the block to the out result
strcat(out, enc_buffer);
memset(input_buffer, 0, 16);
memset(enc_buffer, 0, 16);
}
// Print the encryption result
printf("\n\nENC RESULT:\n\t%d\n\t", strlen(out));
for (unsigned short int i = 0; i < strlen(out); ++i) {
printf("%X ", out[i]);
}
printf("\n");
gcry_cipher_close(handler);
}
Output:
Input (2048 max): This string is made for testing the program
Key (32 max): hey my password
RAW DATA:
input: 43 This string is made for testing the program
key: 15 hey my password
ENC DATA:
input: 48 This string is made for testing the programXXXXX
key: 32 hey my passwordXXXXXXXXXXXXXXXXX
iv: 16 t8jhfhkm7bo5ohxw
ENC INPUT: 16 This string is m
ENC BLOCK: 16 2 BF AA A0 1 7C A8 77 DA 4A 5A 72 29 EB FA F6
ENC INPUT: 16 ade for testing
ENC BLOCK: 16 41 BA CE 61 8A E3 F4 89 8A 46 50 2 47 5 11 A4
ENC INPUT: 16 the programXXXXX
CORRUPTED BLOCK!
ENC BLOCK: 12 AE D6 92 D2 5A AF 85 CB 57 2 1B 93
ENC RESULT:
44
2 BF AA A0 1 7C A8 77 DA 4A 5A 72 29 EB FA F6 41 BA CE 61 8A E3 F4 89 8A 46 50 2 47 5 11 A4 AE D6 92 D2 5A AF 85 CB 57 2 1B 93
I am really sorry for this mess, it's just me going crazy at this point, it seems like the solution is so simple, but i just can't get it.

strlen does not tell you anything about your output buffer's length. Your output buffer is, in fact, always the same length. There's no need to test its length because libgcrypt has no way of modifying its length.
If you want to understand why strlen is returning "chaotic" values, you need to understand what strlen is intended to do. strlen is intended to operate on a C-style (null-terminated) string, not on arbitrary bytes. Strings in C are stored as arrays of characters ending with a '\0' (0x00) character. This is the null terminator. This is how the length of C-strings can be determined.
// example implementation to explicate the concept
size_t strlen(const char *s) {
size_t i = 0;
while (s[i] != '\0')
++i;
return i;
}
When you apply strlen to arbitrary bytes, the results are nonsensical. It is perfectly possible for your binary ciphertext to contain the byte 0x00 anywhere. It could appear at the beginning or anywhere in the middle. It could appear several times. Or it could never appear, in which case you would get a fatal segmentation fault. Wherever 0x00 happens to first appear in your ciphertext, that will be where strlen assumes it ends. The behavior appears "chaotic" because encryption produces random-seeming data, so the distribution of 0x00 within that data is also random-seeming.
PS: You don't need to reset the key every time you encrypt a block.

Related

How do I convert a byte stream to unsigned int 8 in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I am streaming a video in python via WebSocket, which is a raw bytes stream and appears like this:
b'\x00\x00\x00\x01A\x9a \x02\x04\xe1{=z\xf8FMS\xe6\\\x9eMubH\xa7R.1\xd7]F\xea3}\xa9b\x9f\x14n\x12| ....'
Now, I am passing these bytes to a C function (via ctypes) where I am trying to convert this to a uint8_t [] array (This is needed in order to decode it using a FFmpeg library). Here's my code so far:
This is how I am passing bytes to C:
import ctypes
dll = ctypes.CDLL("decode_video.so")
data = bytearray(b'\x00\x00\x00\x01A\x9a \x02\x04\xe1-{=z\xf8FM....')
b_array = ctypes.c_char * len(data)
dll.conversion_test(b_array.from_buffer(data), len(data))
decode_video.c
void conversion_test(unsigned char* buf, int bufSize) {
char temp[3];
uint8_t vals[bufSize];
// Iterate over the values
for (int i = 0; i < bufSize; i++) {
// Copy two characters into the temporary string
temp[0] = buf[i * 2];
temp[1] = buf[i * 2 + 1];
temp[2] = 0;
vals[i] = strtol(temp, NULL, 16);
}
for(int i=0; i<bufSize; i++){
printf("%02x ", vals[i] & 0xff);
}
}
Aside from this, I am simultaneously dumping the stream to a file. In C, I have another function that reads from this file and stores in a uint8_t buffer.
Streaming code in python:
f = open("video.h264", "wb")
def on_message(ws, message):
# The first 14 characters have irrelevant info to decode the video
f.write(message[14:])
ws = websocket.WebSocketApp("wss://somewebsite.com/archives",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
header=[protocol_str]
)
ws.binaryType = 'arraybuffer'
ws.run_forever(dispatcher=rel)
Reading from the raw file in C:
#include "libavcodec/avcodec.h"
#define INBUF_SIZE 4096
uint8_t *data;
uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
f = fopen(input_name, "rb");
data_size = fread(inbuf, 1, INBUF_SIZE, f);
// Printing bytes in hex to debug
for(size_t i=0; i<data_size; i++){
printf("%02x ", inbuf[i] & 0xff);
}
However the contents of this inbuf and output of the vals buffer are not the same. Basically, I am unsure of my method of passing bytes to C and its corresponding coversion to uint8_t.
Update:
I tried printing the hex values of vals and here's what it looks like:
00 00 0a 00 00 00 00 00 00 00 00 00 00 00 01 00 00 ...
While the output of inbuf looks like this:
00 00 00 01 67 42 00 1e e2 90 14 07 b6 02 dc ...
Fix:
As suggested by #n. m. and #dreamlax, a simple identity mapping works between unsigned char and uint8_t. Now vals and inbuf output the same values!
void conversion_test(unsigned char* buf, int bufSize) {
uint8_t vals[bufSize];
for(int i = 0; i < bufSize; i++)
vals[i] = (uint8_t)buf[i];
}
(OR) A simple type-casting
uint8_t *vals = (uint8_t *)ubuf;

How can I write hexadecimal data in the file so that additional data is not written (like hexeditors)?

I want to make something like a small hex editor for my project.
so i wrote a function like this(to replace the original code with the new code):
int replace(FILE *binaryFile, long offset, unsigned char *replaced, int length) {
if (binaryFile != NULL) {
for (int i = 0; i < length; i++) {
fseek(binaryFile, offset + i, SEEK_SET);
fwrite(&replaced[i], sizeof(replaced), 1, binaryFile);
}
fclose(binaryFile);
return 1;
} else return -1;
}
So I wrote this code to test the function and sent it to address 0x0:
unsigned char code[] = "\x1E\xFF\x2F\xE1";
and i got this hexadecimal result:
1e ff 2f e1 00 10 2b 35 ff fe 07 00
But I don't need data after E1 (00 10 2b 35 ff fe 07 00)
How can I write the function so that only the data sent to the function is stored?
sizeof(replaced) is wrong. replaced is a unsigned char *, so that's not the size you want.
You probably want sizeof(unsigned char) or sizeof(*replaced).
Currently, you end up writing eight times too much.
Note that you could also write in a single step:
if (binaryFile != NULL)
{
fseek(binaryFile, offset, SEEK_SET);
fwrite(replaced, sizeof(unsigned char), length, binaryFile);
}

Printing variable number of bytes using format strings with printf

Goal: Print variable number of bytes using a single format specifier.
Environment: x86-64 Ubuntu 20.04.3 LTS running in VM on an x86-64 host machine.
Example:
Let %kmagic be the format specifier I am looking for which prints k bytes by popping them from the stack and additing them to the output. Then, for %rsp pointing to a region in memory holding bytes 0xde 0xad 0xbe 0xef, I want printf("Next 4 bytes on the stack: %4magic") to print Next 4 bytes on the stack: deadbeef.
What I tried so far:
%khhx, which unfortunately just results in k-1 blank spaces followed by two hex-characters (one byte of data).
%kx, which I expected to print k/2 bytes interpreted as one number. This only prints 8 hex-characters (4 bytes) prepended by k - 8 blank spaces.
The number of non-blank characters printed matches the length of the format specifiers, i.e. the expected length of %hhx is 2, which is also the number of non-blank characters printed. The same holds for %x, which one expects to print 8 characters.
Question:
Is it possible to get the desired behavior? If so, how?
Is it possible to get the desired behavior? If so, how?
There does not exist printf format specifier to do what you want.
Is it possible
Write your own printf implementation that supports what you want. Use implementation-specific tools to create your own printf format specifier. You can take inspiration from linux kernel printk %*phN format speciifer.
It is not possible to using standard printf. You need to write your own function and customize the printf function.
http://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html
Example (simple dump):
int printdump (FILE *stream, const struct printf_info *info, const void *const *args)
{
const unsigned char *ptr = *(const unsigned char **)args[0];
size_t size = *(size_t*)args[1];
for(size_t i = 1; i <= size; i++)
{
fprintf(stream, "%02X%c", ptr[i-1], i % 8 ? ' ' : '\n');
}
return 1;
}
int printdumpargs (const struct printf_info *info, size_t n, int *argtypes)
{
if (n == 2)
argtypes[0] = PA_POINTER;
argtypes[1] = PA_INT;
return 2;
}
int main(void)
{
double x[4] = {456543645.6786e45, 456543654, 1e345, -345.56e67};
register_printf_function ('Y', printdump, printdumpargs);
printf("%Y\n", &x, sizeof(x));
}
As I see it is depreciated now (probably no one was using it)
https://godbolt.org/z/qKs6e1d9q
Output:
30 18 CB 5A EF 10 13 4B
00 00 00 A6 4D 36 BB 41
00 00 00 00 00 00 F0 7F
C4 5D ED 48 9C 05 60 CE
There is no standard conversion specifier for your purpose, but you can achieve your goal in C99 using an ancillary function and dynamic array:
#include <stdio.h>
char *dump_bytes(char *buf, const void *p, size_t count) {
const unsigned char *src = p;
char *dest = buf;
while (count --> 0) {
dest += sprintf(dest, "%.2X", *src++);
if (count)
*dest++ = ' ';
}
*dest = '\0'; // return an empty sting for an empty memory chunk
return buf;
}
int main() {
long n = 0x12345;
printf("n is at address %p with contents: %s\n",
(void *)&n,
dump_bytes((char[3 * sizeof(n)]){""}, &n, sizeof(n)));
return 0;
}
Output: n is at address 0x7fff523f57d8 with contents: 45 23 01 00 00 00 00 00
You can use a macro for simpler invocation:
#define DUMPBYTES(p, n) dump_bytes((char[3 * (n)]){""}, p, n)
int main() {
char *p = malloc(5);
printf("allocated 5 bytes at address %p with contents: %s\n",
p, DUMPBYTES(p, 5));
free(p);
return 0;
}

scanf hexadecimal a long byte array

I have a file with the following format:
0 b71b3a8de0c18abd2e56ec5f4efc4af2ba084604
1 4bec20891a68887eef982e9cda5d02ca8e6d4f57
The first value is an integer, and the second integer is a 20-byte value encoded in hexadecimal. I want to be able read in both values using a fscanf loop like so:
FILE *file = fopen("file.txt", "r");
int id;
char hash[20];
while(fscanf(has_chunks, "%i %40x\n", &id, c_hash) == 2){
// Do Stuff
}
However, this clearly doesn't work, as %40x expects an unsigned int pointer, but this is not large enough to hold the value. I know I can do multiple formatters, like %x%x%x, but this doesn't seem elegant. Is there a better way I can do this using fscanf?
b7 1b 3a 8d e0 c1 8a bd 2e 56 ec 5f 4e fc 4a f2 ba 08 46 04
Each pair of characters is in the range between 0 to 0xff. This fits in one byte, or unsigned char. Hash functions normally expect unsigned char as well.
Use the following conversion:
int i, id;
unsigned int v;
unsigned char hash[20];
char buf[41];
while(fscanf(file, "%d %s\n", &id, buf) == 2)
{
for(i = 0; i < 20; i++)
{
if(sscanf(buf + i * 2, "%2x", &v) != 1) break;
hash[i] = (unsigned char)v;
}
}

C read X bytes from a file, padding if needed

I am trying to read in an input file 64 bits at a time, then do some calculations on those 64 bits, the problem is I need to convert the ascii text to hexadecimal characters. I have searched around but none of the answers posted seem to work for my situation.
Here is what I have:
int main(int argc, int * argv)
{
char buffer[9];
FILE *f;
unsigned long long test;
if(f = fopen("input2.txt", "r"))
{
while( fread(buffer, 8, 1, f) != 0) //while not EOF read 8 bytes at a time
{
buffer[8] = '\0';
test = strtoull(buffer, NULL, 16); //interpret as hex
printf("%llu\n", test);
printf("%s\n", buffer);
}
fclose(f);
}
}
For an input like this:
"testing string to hex conversion"
I get results like this:
0
testing
0
string t
0
o hex co
0 nversion
Where I would expect:
74 65 73 74 69 6e 67 20 <- "testing" in hex
testing
73 74 72 69 6e 67 20 74 <- "string t" in hex
string t
6f 20 68 65 78 20 63 6f <- "o hex co" in hex
o hex co
6e 76 65 72 73 69 6f 6e <- "nversion" in hex
nversion
Can anyone see where I misstepped?
strtoull converts a number represented by a string into an unsigned long long. Your input to this function (eg. string "testing") makes no sense as it has to be a number.
printf("%llu\n", strtoull("123")); // prints 123
To get the result you want, you have to print each character of the string like this:
for(int i=0; i<8; i++)
printf( "%02X ", (unsigned char) buffer[i]);
The function strtoull (with 16) converts HEX string to number, not ASCII char to HEX string.
To print a char in HEX form, you should do something like printf("%02x ",buffer[0]);
strtoull() Converts a string that is in hex format (e.g. 0xFFAABBEE) to it's integer format.
What you really need is a function to convert a string to a hex string, like this:
char *strToHex(const char *input)
{
char *output = calloc(1, strlen(input) * 3 + 1);
char *o = output;
int i = 0;
for (; input[i] != '\0'; o += 3, i++)
{
sprintf(o, "%.2X ", input[i]);
}
// don't forget to free output!
return output;
}
Consider using limits.h as well.
I went a bit overboard but perhaps some of this fits:
Edit: [
Ehrmf. Perhaps a define of BITS_ULL is more fitting to your quest.
I.e. something in the direction of:
#define BITS_ULL (sizeof(unsigned long long) * CHAR_BIT)
#define BYTE_ULL (sizeof(unsigned long long))
And then read BYTE_ULL bytes, but make shure to check size of read bytes and not if it is -1 as latter could be a smash. I'm a bit unsure what you mean by "calculations" on read bits.
You could read BYTE_ULL bytes and cast to unsigned long long by address of buffer[0], or bit shift taking byte order into consideration. Or former and sort bytes with char pointer.
Also note that I have used len instead of null terminated / C string.
Oh, this is lots of fun :) - I'm learning, and this kind of hacking is heaven.
]
#include <stdio.h>
#include <limits.h> /* BITS */
#include <ctype.h> /* isprint() */
#define CHUNK_BITS 62
#define CHUNK_CHAR (CHUNK_BITS / CHAR_BIT)
#define HEX_WIDTH 2
/* print len hex values of s, separate every sep byte with space,
* but do not add trailing space. */
void prnt_cshex(const char *s, int len, int sep)
{
const unsigned char *p = (const unsigned char*)s;
int i;
for (i = 1; i <= len; ++p, ++i)
fprintf(stdout,
"%02x"
"%s",
*p,
(i < len && !((i)%sep) ? " " : ""));
}
/* Print len bytes of s, print dot if !isprint() */
void prnt_csbytes(const char *s, int len)
{
int i = 0;
for (i = 0; i < len; ++s, ++i)
fprintf(stdout,
"%c",
(isprint(*s) ? *s : '.'));
}
/* Pass file as first argument, if none, use default "input.txt" */
int main(int argc, char *argv[])
{
const char *fn = "input.txt";
FILE *fh;
char buffer[CHUNK_CHAR];
const char *p = &buffer[0];
size_t k;
if (argc > 1)
fn = argv[1];
if ((fh = fopen(fn, "rb")) == NULL) {
fprintf(stderr, " * Unable to open \"%s\"\n", fn);
goto fail_1;
}
fprintf(stdout,
"Processing \"%s\"\n"
"Chunks of %d bytes of %d bits = %d bits\n",
fn,
CHUNK_CHAR, CHAR_BIT, CHUNK_CHAR * CHAR_BIT);
if (CHUNK_BITS != CHUNK_CHAR * CHAR_BIT) {
fprintf(stdout,
"%d bits chunk requested. Won't fit, trunkated to\n"
"%d * %d = %d\n"
"%d bits short.\n\n",
CHUNK_BITS,
CHUNK_CHAR, CHAR_BIT, CHUNK_BITS / CHAR_BIT * CHAR_BIT,
CHUNK_BITS - CHUNK_CHAR * CHAR_BIT);
}
while ((k = fread(buffer, 1, CHUNK_CHAR, fh)) == CHUNK_CHAR) {
prnt_cshex(p, CHUNK_CHAR, HEX_WIDTH); /* Print as hex */
printf(" ");
prnt_csbytes(p, CHUNK_CHAR); /* Print as text */
putchar('\n');
}
if (!feof(fh)) {
fprintf(stderr, " * Never reached EOF;\n");
goto fail_close;
}
/* If input file does not fit in to CHUNK, report this */
if (k > 0) {
printf("%d byte tail: '", k);
prnt_csbytes(p, k);
printf("'\n");
}
fclose(fh);
return 0;
fail_close:
fclose(fh);
fail_1:
return 1;
}
You can try doing getchar() 8 times (each value returned by getchar is 1 byte = 8 bits) and then using atoh or something - I'm not even sure if atoh exists, but barring that do something like atoi followed by itoh.. or write your own function to convert it.

Resources