Skip to content

Commit

Permalink
Remove dependency on openssl.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tekki committed Nov 8, 2019
1 parent a693b34 commit 82d2791
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 61 deletions.
4 changes: 3 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Not Released: Version 0.03
2019-11-08: Version 0.10

* Define constants, flat data structure.

* Remove dependency on openssl.

2019-11-06: Version 0.02

* Makefile for simple compilation.
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NAME

libquickxor - C implementation of the QuickXor hash
libquickxor - C implementation of the QuickXorHash

# SYNOPSIS

Expand All @@ -22,19 +22,22 @@ libquickxor - C implementation of the QuickXor hash

# DESCRIPTION

The C implementation of the QuickXor hash.
libquickxor is the C implementation of the QuickXorHash.

The QuickXor hash is the digest used by Microsoft on Office 365 OneDrive for Business and Sharepoint.
The QuickXorHash is the digest used by Microsoft on Office 365 OneDrive for Business and Sharepoint.
It was published by Microsoft in 2016 in form of a C\# script. The explanation describes it as a
"quick, simple non-cryptographic hash algorithm that works by XORing the bytes in a circular-shifting fashion".

# INSTALLATION

Requires `openssl`. For full installation additionally `automake` and `libtool`.
Download the latest version from Github.

wget https://github.com/Tekki/quickxor-c/archive/v0.10.tar.gz -O quickxor-0.10.tar.gz
tar xvzf quickxor-0.10.tar.gz
cd quickxor-c-0.10

A full installation requires `automake` and `libtool`.

wget https://github.com/Tekki/quickxor-c/archive/v0.02.tar.gz -O quickxor-0.02.tar.gz
tar xvzf quickxor-0.02.tar.gz
cd quickxor-c-0.02
autoreconf --install
./configure
make
Expand Down
11 changes: 7 additions & 4 deletions README.md.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NAME

libquickxor - C implementation of the QuickXor hash
libquickxor - C implementation of the QuickXorHash

# SYNOPSIS

Expand All @@ -22,19 +22,22 @@ libquickxor - C implementation of the QuickXor hash

# DESCRIPTION

The C implementation of the QuickXor hash.
libquickxor is the C implementation of the QuickXorHash.

The QuickXor hash is the digest used by Microsoft on Office 365 OneDrive for Business and Sharepoint.
The QuickXorHash is the digest used by Microsoft on Office 365 OneDrive for Business and Sharepoint.
It was published by Microsoft in 2016 in form of a C\# script. The explanation describes it as a
"quick, simple non-cryptographic hash algorithm that works by XORing the bytes in a circular-shifting fashion".

# INSTALLATION

Requires `openssl`. For full installation additionally `automake` and `libtool`.
Download the latest version from Github.

wget https://github.com/Tekki/quickxor-c/archive/v@PACKAGE_VERSION@.tar.gz -O @PACKAGE_TARNAME@-@PACKAGE_VERSION@.tar.gz
tar xvzf @PACKAGE_TARNAME@-@PACKAGE_VERSION@.tar.gz
cd quickxor-c-@PACKAGE_VERSION@

A full installation requires `automake` and `libtool`.

autoreconf --install
./configure
make
Expand Down
6 changes: 3 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# prelude
AC_PREREQ([2.69])
AC_INIT([QuickXor],[0.02],[tekki@tekki.ch])
AC_INIT([QuickXor], [0.10], [tekki@tekki.ch])
AM_PROG_AR

# unique source file --- primitive safety check
Expand All @@ -21,13 +21,13 @@ LT_INIT

# checks for libraries

PKG_CHECK_MODULES([REQUIRED], [openssl >= 1.1])
# PKG_CHECK_MODULES([REQUIRED], [])
PKG_CHECK_MODULES([TEST], [check >= 0.10.0])
AM_PROG_CC_C_O

# checks for header files
AC_HEADER_STDC
AC_CHECK_HEADERS([stdlib.h string.h openssl/hmac.h])
AC_CHECK_HEADERS([stdlib.h string.h])

# checks for typedefs, structures, and compiler characteristics

Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## Process this file with automake to produce Makefile.in

lib_LTLIBRARIES = libquickxor.la
libquickxor_la_SOURCES = quickxor.c quickxor.h
libquickxor_la_SOURCES = quickxor.c quickxor.h base64.h
include_HEADERS = quickxor.h

bin_PROGRAMS = quickxorhash
quickxorhash_SOURCES = quickxor_bin.c
quickxorhash_LDADD = libquickxor.la @REQUIRED_LIBS@
quickxorhash_LDADD = libquickxor.la
18 changes: 9 additions & 9 deletions src/Makefile.simple
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
CC = gcc
CFLAGS = -g -Wall
LDFLAGS = -lssl -lcrypto
LDFLAGS =

quickxorhash: quickxor_bin.o quickxor.o
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
TARGET = quickxorhash

OBJECTS := $(patsubst %.c,%.o,$(wildcard *.c))

quickxor_bin.o: quickxor_bin.c
$(CC) -c $^ -o $@
$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@

quickxor.o: quickxor.c
$(CC) -c $^ -o $@
$(OBJECTS): %.o: %.c
$(CC) $(CFLAGS) -c $^ -o $@

clean:
rm -f *.o
rm -f quickxorhash
rm -f $(TARGET) $(OBJECTS)
85 changes: 85 additions & 0 deletions src/base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Base64 encoding function
*
* source: https://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#C
*
*/

#ifndef BASE64_H
#define BASE64_H

#include <stdint.h>
#include <string.h>

int B64_encode(const void* data_buf, size_t dataLength, char* result, size_t resultSize) {
const char base64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const uint8_t* data = (const uint8_t*)data_buf;
size_t resultIndex = 0;
size_t x;
uint32_t n = 0;
int padCount = dataLength % 3;
uint8_t n0, n1, n2, n3;

/* increment over the length of the string, three characters at a time */
for (x = 0; x < dataLength; x += 3) {
/* these three 8-bit (ASCII) characters become one 24-bit number */
n = ((uint32_t)data[x]) << 16; // parenthesis needed, compiler depending on flags can do the
// shifting before conversion to uint32_t, resulting to 0

if ((x + 1) < dataLength)
n += ((uint32_t)data[x + 1]) << 8; // parenthesis needed, compiler depending on flags can do
// the shifting before conversion to uint32_t, resulting
// to 0

if ((x + 2) < dataLength) n += data[x + 2];

/* this 24-bit number gets separated into four 6-bit numbers */
n0 = (uint8_t)(n >> 18) & 63;
n1 = (uint8_t)(n >> 12) & 63;
n2 = (uint8_t)(n >> 6) & 63;
n3 = (uint8_t)n & 63;

/*
* if we have one byte available, then its encoding is spread
* out over two characters
*/
if (resultIndex >= resultSize) return 1; /* indicate failure: buffer too small */
result[resultIndex++] = base64chars[n0];
if (resultIndex >= resultSize) return 1; /* indicate failure: buffer too small */
result[resultIndex++] = base64chars[n1];

/*
* if we have only two bytes available, then their encoding is
* spread out over three chars
*/
if ((x + 1) < dataLength) {
if (resultIndex >= resultSize) return 1; /* indicate failure: buffer too small */
result[resultIndex++] = base64chars[n2];
}

/*
* if we have all three bytes available, then their encoding is spread
* out over four characters
*/
if ((x + 2) < dataLength) {
if (resultIndex >= resultSize) return 1; /* indicate failure: buffer too small */
result[resultIndex++] = base64chars[n3];
}
}

/*
* create and add padding that is required if we did not have a multiple of 3
* number of characters available
*/
if (padCount > 0) {
for (; padCount < 3; padCount++) {
if (resultIndex >= resultSize) return 1; /* indicate failure: buffer too small */
result[resultIndex++] = '=';
}
}
if (resultIndex >= resultSize) return 1; /* indicate failure: buffer too small */
result[resultIndex] = 0;
return 0; /* indicate success */
}

#endif /* BASE64_H */
36 changes: 11 additions & 25 deletions src/quickxor.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* quickxor - QuickXor hash library
* libquickxor - QuickXorHash Library
*
* © 2019 by Tekki (Rolf Stöckli)
*
Expand All @@ -21,6 +21,7 @@

#include <openssl/hmac.h>
#include <string.h>
#include "base64.h"
#include "quickxor.h"

QX* QX_new() {
Expand Down Expand Up @@ -60,10 +61,10 @@ void QX_add(QX* pqx, uint8_t* addData, size_t addSize) {
xoredByte ^= addData[j];
}

pqx->data[vectorArrayIndex] ^= (uint64_t)xoredByte << vectorOffset;
pqx->data[vectorArrayIndex] ^= ((uint64_t)xoredByte) << vectorOffset;

if (vectorOffset > bitsInVectorCell - 8) {
pqx->data[nextCell] ^= (uint64_t)xoredByte >> (bitsInVectorCell - vectorOffset);
pqx->data[nextCell] ^= ((uint64_t)xoredByte) >> (bitsInVectorCell - vectorOffset);
}

vectorOffset += pqx->kShift;
Expand Down Expand Up @@ -108,41 +109,26 @@ uint8_t* QX_digest(QX* pqx) {
}

char* QX_b64digest(QX* pqx) {
BIO* mem = NULL;
BIO* b64 = NULL;
char* hash = NULL;
uint8_t* digest = NULL;
size_t hashSize;
char* hash = NULL;

digest = QX_digest(pqx);
mem = BIO_new(BIO_s_mem());
b64 = BIO_new(BIO_f_base64());
hash = calloc(2, pqx->kWidthInBytes);
digest = QX_digest(pqx);
hashSize = 2 * pqx->kWidthInBytes;
hash = calloc(1, hashSize);

if (digest && mem && b64 && hash) {
b64 = BIO_push(b64, mem);
BIO_write(b64, digest, pqx->kWidthInBytes);
BIO_flush(b64);

BIO_read(mem, hash, pqx->kWidthInBytes * 2);
hash[strcspn(hash, "\r\n")] = '\0';
if (digest && hash) {
B64_encode(digest, pqx->kWidthInBytes, hash, hashSize);
}

free(digest);
digest = NULL;
if (b64) {
BIO_free_all(b64);
b64 = NULL;
} else {
BIO_free_all(mem);
}
mem = NULL;

return hash;
}

void QX_free(QX* pqx) {
if (pqx) {
/* free(pqx->data); */
free(pqx);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
TESTS = t_quickxor
check_PROGRAMS = t_quickxor
t_quickxor_SOURCES = t_quickxor.c $(top_builddir)/src/quickxor.h
t_quickxor_LDADD = $(top_builddir)/src/libquickxor.la @REQUIRED_LIBS@ @TEST_LIBS@
t_quickxor_LDADD = $(top_builddir)/src/libquickxor.la @TEST_LIBS@
t_quickxor_CFLAGS = @TEST_CFLAGS@
Loading

0 comments on commit 82d2791

Please sign in to comment.