Skip to content

Commit

Permalink
Implement base32 encoders and decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
vladistan committed Jan 5, 2019
1 parent 50d2222 commit 94b9ff2
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
143 changes: 143 additions & 0 deletions RunAllTests.cpp
Expand Up @@ -12,6 +12,7 @@ extern "C" {
#include "sim_types.h"
#include "mock_data.h"
#include "client.h"
#include "crypto.h"
}


Expand Down Expand Up @@ -143,7 +144,147 @@ TEST(Misc, Bcvh) {
LONGS_EQUAL(0x1, n)
}

TEST_GROUP(Encoders) {
void setup() {}

void teardown() { mock().clear(); }
};

TEST(Encoders, EncSmallByteVal) {
_BYTE src[] = {0x2};
_BYTE dst[4];

bzero(dst, sizeof(src));
encByte(src[0], dst);

MEMCMP_EQUAL(dst, "02", 2);
}

TEST(Encoders, EncByteValeMoreThanA) {
_BYTE src[] = {0xC};
_BYTE dst[4];

bzero(dst, sizeof(src));
encByte(src[0], dst);

MEMCMP_EQUAL(dst, "0c", 2);
}

TEST(Encoders, EncByteValueEdgeCase) {
_BYTE src[] = {0xA};
_BYTE dst[4];

bzero(dst, sizeof(src));
encByte(src[0], dst);

MEMCMP_EQUAL(dst, "0a", 2);
}

TEST(Encoders, EncByteValueTwoDigits) {
_BYTE src[] = {0x10};
_BYTE dst[4];

bzero(dst, sizeof(src));
encByte(src[0], dst);

MEMCMP_EQUAL(dst, "10", 2);
}

TEST(Encoders, EncByteValueTwoDigitsEdge) {
_BYTE src[] = {0xff};
_BYTE dst[4];

bzero(dst, sizeof(src));
encByte(src[0], dst);

MEMCMP_EQUAL(dst, "ff", 2);
}

TEST(Encoders, EncLonger) {
_BYTE src[] = {0xf3, 0x10, 0x94, 0x84, 0x11, 0x00, 0x01, 0x55, 0xea, 0xa0};
_BYTE dst[30];


bzero(dst, sizeof(dst));

bcvh(src, 10, dst, 21);

MEMCMP_EQUAL(dst, "f310948411000155eaa0", 21);

}

TEST(Encoders, EncIP) {

_BYTE res[9] = {0x30, 0x61, 0x32, 0x66, 0x37, 0x32, 0x31, 0x36, 0};
_BYTE src[4] = {10, 47, 114, 22};
_BYTE dst[9];

memset(dst, 0x34, 9);

bcvh(src, 4, dst, 9);

MEMCMP_EQUAL(dst, res, 9);

}

TEST(Encoders, EncByteValueTwoDigitsEdgeM1) {
_BYTE src[] = {0xfe};
_BYTE dst[4];

bzero(dst, sizeof(src));
encByte(src[0], dst);

MEMCMP_EQUAL(dst, "fe", 2);
}

TEST_GROUP(Crypto) {
void setup() {
}

void teardown() {}
};


TEST(Crypto, get_key) {

char key[128];
int a3;
const char *enc_key = "CAYPFE6MG2DJT4EB5RIZLIAYFJAUGL3L";

bzero(key, sizeof(key));
get_sign_key(key, sizeof(key), a3);

STRCMP_EQUAL(enc_key, (const char *) key);

}

TEST_GROUP(B32codecs) {
void setup() {}

void teardown() { mock().clear(); }
};

TEST(B32codecs, simpleDec) {
const char *src = "JBSWY3DPEBUG65Z7";
const char *exp = "Hello how?";
char dst[1024];

b32dec(src, dst);

STRCMP_EQUAL(exp, dst);

}

TEST(B32codecs, properDec) {

const char *src = "JBSWY3DPEBUG65Z7";
const char *exp = "Hello how?";
int len = 0;
const char *rv = decode_b32(src, &len);

LONGS_EQUAL(11, len);
STRCMP_EQUAL(exp, rv);
}
TEST_GROUP(PartialFunctions) {
void setup() {
// const char *otp = "197548";
Expand Down Expand Up @@ -351,3 +492,5 @@ TEST(Transmit, SendIsCalled) {
int main(int ac, char **av) {
return CommandLineTestRunner::RunAllTests(ac, av);
}


81 changes: 81 additions & 0 deletions b32.c
@@ -1 +1,82 @@
#include <string.h>
#include <stdlib.h>
#include "crypto.h"

static unsigned char shift_right(unsigned char byte, char offset) {
if (offset > 0)
return byte >> offset;
else
return byte << -offset;
}

static unsigned char shift_left(unsigned char byte, char offset) {
return shift_right(byte, -offset);
}

static int get_offset(int block) {
return (8 - 5 - (5 * block) % 8);
}

static int get_octet(int block) {
return (block * 5) / 8;
}

static int decode_char(unsigned char c) {
char retval = -1;

if (c >= 'A' && c <= 'Z')
retval = c - 'A';
if (c >= '2' && c <= '7')
retval = c - '2' + 26;

return retval;
}

static int decode_sequence(const unsigned char *coded, unsigned char *plain) {

plain[0] = 0;
for (int block = 0; block < 8; block++) {
int offset = get_offset(block);
int octet = get_octet(block);

int c = decode_char(coded[block]);
if (c < 0) // invalid char, stop here
return octet;

plain[octet] |= shift_left(c, offset);
if (offset < 0) { // does this block overflows to next octet?
plain[octet + 1] = shift_left(c, 8 + offset);
}
}
return 5;
}

size_t base32_decode(const unsigned char *coded, unsigned char *plain) {
size_t written = 0;
for (size_t i = 0, j = 0;; i += 8, j += 5) {
int n = decode_sequence(&coded[i], &plain[j]);
written += n;
if (n < 5)
return written;
}
}

void b32dec(const char *src, char dst[1024]) {

base32_decode(src, dst);

}

const char *decode_b32(const char *src, int *pInt) {

int ln = strlen(src);
ln = 5 * ((ln + 7) >> 3) + 1;

char *dst = malloc(ln + 8);
*pInt = ln;

base32_decode(src, dst);

return dst;
}

9 changes: 9 additions & 0 deletions crypto.c
@@ -1,2 +1,11 @@
#include <string.h>
#include "crypto.h"


__int64 get_sign_key(char *addr, __int64 len, __int64 a3) {

const char *enc_key = "CAYPFE6MG2DJT4EB5RIZLIAYFJAUGL3L";

strcpy(addr, enc_key);
return 0LL;
}
11 changes: 11 additions & 0 deletions crypto.h
@@ -1 +1,12 @@
#ifndef _CRYPTO_H_INCLUDED
#define _CRYPTO_H_INCLUDED

#include "sim_types.h"

__int64 get_sign_key(char *addr, __int64 len, __int64 a3);

void b32dec(const char *src, char dst[1024]);
const char *decode_b32(const char *src, int *pInt);
#endif


1 change: 1 addition & 0 deletions stubs.h
Expand Up @@ -12,5 +12,6 @@ bool enc_ki(void *, long long int len);
bool dispatch_server_command(void *ptr, char *alias_3);
void set_loc_data(_BYTE *addr, const char *otp);

void encByte(_BYTE src, _BYTE *dst);

#endif

0 comments on commit 94b9ff2

Please sign in to comment.