Compare commits
5 Commits
0318221f3f
...
a99d1d3784
Author | SHA1 | Date | |
---|---|---|---|
a99d1d3784 | |||
7fae216ccf | |||
89a3585c7f | |||
c81c8bfa3c | |||
e622107f07 |
@ -1,9 +1,6 @@
|
||||
---
|
||||
name: Test and Deploy
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
@ -29,6 +26,7 @@ jobs:
|
||||
|
||||
docs:
|
||||
runs-on: ubuntu-latest
|
||||
if: gitea.ref == 'refs/heads/master'
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
|
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
@ -1,4 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakePythonSetting">
|
||||
<option name="pythonIntegrationState" value="YES" />
|
||||
</component>
|
||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
@ -2,6 +2,9 @@
|
||||
|
||||
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
|
||||
|
||||
### b64_encode
|
||||
@ -26,22 +29,25 @@ unsigned char *b64_decode(const char *s, size_t sz, size_t *decode_sz);
|
||||
### hex_encode
|
||||
|
||||
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
|
||||
letters with no leading `0x`. User is responsible for the freeing of the returned string
|
||||
`[0xDE, 0xAD, 0xBE, 0xEF]` into `"deadbeef"`. User is responsible for freeing the returned string
|
||||
|
||||
```c
|
||||
const char *hex_encode(const unsigned char *hex, size_t sz);
|
||||
|
||||
// Example
|
||||
unsigned char h[4] = {
|
||||
0xde, 0xad, 0xbe, 0xef
|
||||
};
|
||||
const char *s = hex_encode(h, 4);
|
||||
assert(strcmp(s, "DEADBEEF") == 0);
|
||||
assert(strcmp(s, "deadbeef") == 0);
|
||||
free(s);
|
||||
```
|
||||
|
||||
### hex_decode
|
||||
|
||||
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
|
||||
|
||||
```c
|
||||
@ -83,3 +89,19 @@ responsible for freeing the returned array
|
||||
```c
|
||||
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);
|
||||
```
|
||||
|
@ -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_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
|
||||
|
@ -3,6 +3,8 @@
|
||||
|
||||
#if defined(__APPLE__) || defined(__MACH__)
|
||||
|
||||
#include <time.h>
|
||||
|
||||
typedef struct {
|
||||
double total_user_time;
|
||||
double total_kernel_time;
|
||||
@ -18,6 +20,7 @@ typedef struct {
|
||||
|
||||
ProcessData *new_ProcessData();
|
||||
int update_process(pid_t pid, ProcessData *proc);
|
||||
void *reallocarray(void *optr, size_t nmemb, size_t size);
|
||||
|
||||
#endif /* defined(__APPLE__) || defined(__MACH__) */
|
||||
#endif /* LIBFLINT_MACOS_H */
|
||||
|
38
src/crypto.c
38
src/crypto.c
@ -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) {
|
||||
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) {
|
||||
buf_sz += 1;
|
||||
}
|
||||
|
||||
char *buf = malloc(sizeof(char) * buf_sz);
|
||||
if (strlen(orig) % 2 != 0) {
|
||||
strcpy(buf + 1, orig);
|
||||
if (strlen(sptr) % 2 != 0) {
|
||||
strcpy(buf+ 1, sptr);
|
||||
buf[0] = '0';
|
||||
} else {
|
||||
strcpy(buf, orig);
|
||||
strcpy(buf, sptr);
|
||||
}
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
37
src/macos.c
37
src/macos.c
@ -1,6 +1,7 @@
|
||||
#include <libproc.h>
|
||||
#include <time.h>
|
||||
#include <mach/mach_time.h>
|
||||
#include <sys/errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lfmacos.h"
|
||||
@ -43,3 +44,39 @@ int update_process(pid_t pid, ProcessData *proc) {
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -5,6 +5,10 @@
|
||||
#include <bsd/stdlib.h>
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__) || defined(__MACH__)
|
||||
#include "lfmacos.h"
|
||||
#endif
|
||||
|
||||
#include "lfvector.h"
|
||||
|
||||
#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) {
|
||||
vec->capacity *= 2;
|
||||
|
||||
#ifdef __OpenBSD__
|
||||
vec->elements = reallocarray(vec->elements, vec->capacity, sizeof(void *));
|
||||
#else
|
||||
vec->elements = reallocf(vec->elements, sizeof(void *) * vec->capacity);
|
||||
#endif
|
||||
|
||||
if (vec->elements == NULL) {
|
||||
return -1;
|
||||
|
@ -349,6 +349,13 @@ void test_crypto() {
|
||||
}
|
||||
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);
|
||||
assert(strcmp(s, "deadbeef") == 0);
|
||||
free(s);
|
||||
@ -367,6 +374,14 @@ void test_crypto() {
|
||||
free(enc);
|
||||
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");
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user