Compare commits

...

5 Commits

Author SHA1 Message Date
a99d1d3784 docs only on master
All checks were successful
Test and Deploy / test (push) Successful in 11s
Test and Deploy / docs (push) Successful in 18s
2024-05-06 12:16:21 -07:00
7fae216ccf implement reallocarray on macOS
All checks were successful
Test and Deploy / test (push) Successful in 12s
Test and Deploy / docs (push) Successful in 18s
2024-05-06 11:17:34 -07:00
89a3585c7f add hamming_distance
All checks were successful
Test and Deploy / test (push) Successful in 13s
Test and Deploy / docs (push) Successful in 21s
2024-05-05 17:05:12 -07:00
c81c8bfa3c hamming distance
All checks were successful
Test and Deploy / test (push) Successful in 12s
Test and Deploy / docs (push) Successful in 17s
2024-05-04 11:35:54 -07:00
e622107f07 enhance hex_decode
All checks were successful
Test and Deploy / test (push) Successful in 12s
Test and Deploy / docs (push) Successful in 15s
2024-04-11 07:19:04 -07:00
9 changed files with 128 additions and 15 deletions

View File

@ -1,9 +1,6 @@
--- ---
name: Test and Deploy name: Test and Deploy
on: on: [push]
push:
branches:
- master
jobs: jobs:
test: test:
@ -29,6 +26,7 @@ jobs:
docs: docs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: gitea.ref == 'refs/heads/master'
timeout-minutes: 5 timeout-minutes: 5
steps: steps:
- name: Checkout repository - name: Checkout repository

3
.idea/misc.xml generated
View File

@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CMakePythonSetting">
<option name="pythonIntegrationState" value="YES" />
</component>
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> <component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project> </project>

View File

@ -2,6 +2,9 @@
Cryptographic tools Cryptographic tools
Any functions that return string representations of bytes in hexadecimal format will
always be formatted with no leading `0x` and lower case letters
## Functions ## Functions
### b64_encode ### b64_encode
@ -26,22 +29,25 @@ unsigned char *b64_decode(const char *s, size_t sz, size_t *decode_sz);
### hex_encode ### hex_encode
Encodes an array of bytes into a string in hex representation. For example, converts Encodes an array of bytes into a string in hex representation. For example, converts
`[0xDE, 0xAD, 0xBE, 0xEF]` into `"DEADBEEF"`. The returned string will always be in capital `[0xDE, 0xAD, 0xBE, 0xEF]` into `"deadbeef"`. User is responsible for freeing the returned string
letters with no leading `0x`. User is responsible for the freeing of the returned string
```c ```c
const char *hex_encode(const unsigned char *hex, size_t sz); const char *hex_encode(const unsigned char *hex, size_t sz);
// Example // Example
unsigned char h[4] = {
0xde, 0xad, 0xbe, 0xef
};
const char *s = hex_encode(h, 4); const char *s = hex_encode(h, 4);
assert(strcmp(s, "DEADBEEF") == 0); assert(strcmp(s, "deadbeef") == 0);
free(s); free(s);
``` ```
### hex_decode ### hex_decode
Decodes a string of characters representing hexadecimal bytes into an array of `unsigned char`. Decodes a string of characters representing hexadecimal bytes into an array of `unsigned char`.
For example, converts `"DEADBEEF"` into `[0xDE, 0xAD, 0xBE, 0xEF]`. User is reponsible for the Characters can be in upper or lower case, and can start with a leading `0x` or not.
For example, converts `"DEADBEEF"` into `[0xde, 0xad, 0xbe, 0xef]`. User is reponsible for the
freeing of the returned byte array freeing of the returned byte array
```c ```c
@ -83,3 +89,19 @@ responsible for freeing the returned array
```c ```c
const unsigned char *repeating_key_xor_s(const char* s, const char* key); const unsigned char *repeating_key_xor_s(const char* s, const char* key);
``` ```
### hamming_distance
Calculates the Hamming Distance (the number of differing bits) between two arrays of unsigned bytes
```c
unsigned int hamming_distance(unsigned char *a, unsigned char *b);
```
### hamming_distance_s
Calculates the Hamming Distance (the number of differing bits) between two strings
```c
unsigned int hamming_distance_s(const char *a, const char *b);
```

View File

@ -13,4 +13,7 @@ const char *hex_to_str(const unsigned char *hex, size_t sz);
const unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz); const unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz);
const unsigned char *repeating_key_xor_s(const char* s, const char* key); const unsigned char *repeating_key_xor_s(const char* s, const char* key);
unsigned int hamming_distance_s(const char *a, const char *b);
unsigned int hamming_distance(unsigned char *a, unsigned char *b, size_t sz);
#endif // LIBFLINT_CRYPTO_H #endif // LIBFLINT_CRYPTO_H

View File

@ -3,6 +3,8 @@
#if defined(__APPLE__) || defined(__MACH__) #if defined(__APPLE__) || defined(__MACH__)
#include <time.h>
typedef struct { typedef struct {
double total_user_time; double total_user_time;
double total_kernel_time; double total_kernel_time;
@ -18,6 +20,7 @@ typedef struct {
ProcessData *new_ProcessData(); ProcessData *new_ProcessData();
int update_process(pid_t pid, ProcessData *proc); int update_process(pid_t pid, ProcessData *proc);
void *reallocarray(void *optr, size_t nmemb, size_t size);
#endif /* defined(__APPLE__) || defined(__MACH__) */ #endif /* defined(__APPLE__) || defined(__MACH__) */
#endif /* LIBFLINT_MACOS_H */ #endif /* LIBFLINT_MACOS_H */

View File

@ -204,16 +204,22 @@ unsigned char *b64_decode(const char *s, size_t sz, size_t *decode_sz) {
unsigned char *hex_decode(const char *orig, size_t *sz) { unsigned char *hex_decode(const char *orig, size_t *sz) {
size_t buf_sz = strlen(orig) + 1; size_t buf_sz = strlen(orig) + 1;
char *sptr = orig;
if (strncmp(orig, "0x", 2) == 0) {
buf_sz -= 2;
sptr += 2;
}
if (strlen(orig) % 2 != 0) { if (strlen(orig) % 2 != 0) {
buf_sz += 1; buf_sz += 1;
} }
char *buf = malloc(sizeof(char) * buf_sz); char *buf = malloc(sizeof(char) * buf_sz);
if (strlen(orig) % 2 != 0) { if (strlen(sptr) % 2 != 0) {
strcpy(buf + 1, orig); strcpy(buf+ 1, sptr);
buf[0] = '0'; buf[0] = '0';
} else { } else {
strcpy(buf, orig); strcpy(buf, sptr);
} }
buf[buf_sz - 1] = '\0'; buf[buf_sz - 1] = '\0';
@ -265,3 +271,29 @@ const unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, cons
const unsigned char *repeating_key_xor_s(const char* s, const char* key) { const unsigned char *repeating_key_xor_s(const char* s, const char* key) {
return repeating_key_xor((unsigned char*)s, strlen(s), (unsigned char*)key, strlen(key)); return repeating_key_xor((unsigned char*)s, strlen(s), (unsigned char*)key, strlen(key));
} }
unsigned int hamming_distance_s(const char *a, const char *b) {
size_t sz = strlen(a);
if (sz != strlen(b)) {
return -1;
}
return hamming_distance((unsigned char *)a, (unsigned char *)b, sz);
}
unsigned int hamming_distance(unsigned char *a, unsigned char *b, size_t sz) {
unsigned int hamming = 0;
for (size_t i = 0; i < sz; ++i) {
if (a[i] == b[i]) {
continue;
}
unsigned char c = a[i] ^ b[i];
unsigned int count = 0;
for (; c; count++) {
c &= c - 1;
}
hamming += count;
}
return hamming;
}

View File

@ -1,6 +1,7 @@
#include <libproc.h> #include <libproc.h>
#include <time.h> #include <time.h>
#include <mach/mach_time.h> #include <mach/mach_time.h>
#include <sys/errno.h>
#include <stdlib.h> #include <stdlib.h>
#include "lfmacos.h" #include "lfmacos.h"
@ -43,3 +44,39 @@ int update_process(pid_t pid, ProcessData *proc) {
return 0; return 0;
} }
/* reallocarray is reimplemented here for macOS because Apple doesn't expose
* their implementation. This is taken straight from the OpenBSD source as
* shown in the below copyright notice
*/
/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */
/*
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* OPENBSD ORIGINAL: lib/libc/stdlib/reallocarray.c */
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
void *reallocarray(void *optr, size_t nmemb, size_t size)
{
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
nmemb > 0 && SIZE_MAX / nmemb < size) {
errno = ENOMEM;
return NULL;
}
return realloc(optr, size * nmemb);
}

View File

@ -5,6 +5,10 @@
#include <bsd/stdlib.h> #include <bsd/stdlib.h>
#endif #endif
#if defined(__APPLE__) || defined(__MACH__)
#include "lfmacos.h"
#endif
#include "lfvector.h" #include "lfvector.h"
#define VEC_INIT_CAP 2 #define VEC_INIT_CAP 2
@ -32,11 +36,7 @@ int vec_init_with_capacity(Vector *vec, void (*destroy)(void *data), size_t capa
static int vec_grow(Vector *const vec) { static int vec_grow(Vector *const vec) {
vec->capacity *= 2; vec->capacity *= 2;
#ifdef __OpenBSD__
vec->elements = reallocarray(vec->elements, vec->capacity, sizeof(void *)); vec->elements = reallocarray(vec->elements, vec->capacity, sizeof(void *));
#else
vec->elements = reallocf(vec->elements, sizeof(void *) * vec->capacity);
#endif
if (vec->elements == NULL) { if (vec->elements == NULL) {
return -1; return -1;

View File

@ -349,6 +349,13 @@ void test_crypto() {
} }
free(s); free(s);
// leading 0x
s = hex_decode("0xf00f5", &s_sz);
for (size_t i = 0; i < 3; ++i) {
assert(s[i] == h2[i]);
}
free(s);
s = hex_encode(h, 4); s = hex_encode(h, 4);
assert(strcmp(s, "deadbeef") == 0); assert(strcmp(s, "deadbeef") == 0);
free(s); free(s);
@ -367,6 +374,14 @@ void test_crypto() {
free(enc); free(enc);
free(s); free(s);
unsigned char ua[2] = { 0x2, 0xF };
unsigned char ub[2] = { 0x4, 0xE };
unsigned int hamming = hamming_distance(ua, ub, 2);
assert(hamming == 3);
hamming = hamming_distance_s("this is a test", "wokka wokka!!!");
assert(hamming == 37);
printf("Passes all crypto tests\n"); printf("Passes all crypto tests\n");
} }