c,python: Use little-endina byte order for bigint arithmetics

This better matches what one would expect from the hex or base64
(upcoming) formats.
This commit is contained in:
2021-11-15 01:13:17 +01:00
parent cc473f4c24
commit bcae51ead9
7 changed files with 51 additions and 56 deletions

View File

@ -23,10 +23,13 @@ void bhash_fini(bhash_t *bh)
free(bh->bh_data); free(bh->bh_data);
} }
/*
* Treat the bhash as a bigint in `little-endian` notation and perform a 32-bit
* division of the whole hash. Return the modulo.
*/
uint32_t bhash_mod32(bhash_t *bh, uint32_t mod) uint32_t bhash_mod32(bhash_t *bh, uint32_t mod)
{ {
uint32_t *pi; uint8_t *pdata;
uint32_t *pend;
uint64_t n; uint64_t n;
uint32_t rv = 0; uint32_t rv = 0;
@ -36,15 +39,25 @@ uint32_t bhash_mod32(bhash_t *bh, uint32_t mod)
return BHASH_MOD32_ERR; return BHASH_MOD32_ERR;
} }
pend = (uint32_t *)(bh->bh_data + bh->bh_len); pdata = bh->bh_data + bh->bh_len;
for (pi = (uint32_t *)bh->bh_data; pi < pend; pi++) for (pdata = bh->bh_data + bh->bh_len - sizeof(uint32_t);
pdata >= bh->bh_data;
pdata -= sizeof(uint32_t))
{ {
n = (uint64_t)rv << (sizeof(uint32_t) * 8); n = (uint64_t)rv << (sizeof(uint32_t) * 8);
n |= htonl(*pi); n |= (uint32_t)pdata[0];
n |= (uint32_t)pdata[1] << 8;
n |= (uint32_t)pdata[2] << 16;
n |= (uint32_t)pdata[3] << 24;
*pi = ntohl(n / mod);
rv = n % mod; rv = n % mod;
n /= mod;
pdata[0] = n >> 0;
pdata[1] = n >> 8;
pdata[2] = n >> 16;
pdata[3] = n >> 24;
} }
bh->bh_bits_used += log(mod) / log(2); bh->bh_bits_used += log(mod) / log(2);

View File

@ -1,5 +1,8 @@
libcrypto_dep = dependency('libcrypto') libcrypto_dep = dependency('libcrypto')
PHOGEN_MAP_EXE = executable('phogen_map', 'phogen_map.c', dependencies: libcrypto_dep) PHOGEN_MAP_EXE = executable(
'phogen_map',
'phogen_map.c',
dependencies: [bhash_dep, dependency('libcrypto')])

View File

@ -18,6 +18,8 @@
#include <openssl/sha.h> #include <openssl/sha.h>
#include "bhash.h"
/* List of vowels */ /* List of vowels */
#define PHOGEN_VOWELS "aeiou" #define PHOGEN_VOWELS "aeiou"
@ -78,11 +80,11 @@ struct
char *output; char *output;
} phogen_test_table[] = } phogen_test_table[] =
{ {
{ "passgeny", "herang xiasem zitend qibele" }, { "passgeny", "vatome iddeda pashis artarc" },
{ "phonetic", "lineum foneum zybale mangur" }, { "phonetic", "joredl qibini kninet ouslis" },
{ "generator", "latole elitab ackina exprou" }, { "generator", "vormul zilyth disedl chryso" },
{ "password", "nulize nomere fonici crednt" }, { "password", "wompot ficara uplort lancon" },
{ "duck", "catabb rompor cricin prunsi" }, { "duck", "inerea iricin ovusia irrisi" },
}; };
const char phogen_python_header[] = const char phogen_python_header[] =
@ -452,38 +454,11 @@ void phogen_rstrip(char *str, char *what)
} }
} }
/*
* Take the buffer in `bigin` and treat is as a big-endian big number.
* Perform a division using the 32-bit divisor in `base` and return the
* 32-bit modulo.
*/
uint32_t bigint_mod32(void *bigint, size_t bigintsz, uint32_t base)
{
uint32_t *pi;
uint64_t n;
uint32_t mod = 0;
for (pi = (uint32_t *)bigint;
pi < (uint32_t *)(bigint + bigintsz);
pi++)
{
n = mod;
n <<= sizeof(uint32_t) * 8;
n |= htonl(*pi);
*pi = ntohl(n / base);
mod = n % base;
}
return mod;
}
/* /*
* Take input buffer `in` and generate its phonetic representation * Take input buffer `in` and generate its phonetic representation
* and store it to out. * and store it to out.
*/ */
void phogen(char *out, size_t outsz, void *in, size_t insz) void phogen(char *out, size_t outsz, bhash_t *bh)
{ {
int ii; int ii;
@ -502,7 +477,7 @@ void phogen(char *out, size_t outsz, void *in, size_t insz)
{ {
if (phogen_freq_list[ni][nsize].fe_letter == '\0') break; if (phogen_freq_list[ni][nsize].fe_letter == '\0') break;
} }
nsel = bigint_mod32(in, insz, nsize); nsel = bhash_mod32(bh, nsize);
out[ii] = phogen_freq_list[ni][nsel].fe_letter; out[ii] = phogen_freq_list[ni][nsel].fe_letter;
@ -523,6 +498,7 @@ bool phogen_test(void)
SHA256_CTX sha256_ctx; SHA256_CTX sha256_ctx;
int ii; int ii;
int ij; int ij;
bhash_t bh;
bool retval = true; bool retval = true;
@ -532,17 +508,21 @@ bool phogen_test(void)
SHA256_Update(&sha256_ctx, phogen_test_table[ii].input, strlen(phogen_test_table[ii].input)); SHA256_Update(&sha256_ctx, phogen_test_table[ii].input, strlen(phogen_test_table[ii].input));
SHA256_Final(sha256, &sha256_ctx); SHA256_Final(sha256, &sha256_ctx);
bhash_init(&bh, sha256, sizeof(sha256));
/* 4 words, each 6 characters long and 1 more character for spaces */ /* 4 words, each 6 characters long and 1 more character for spaces */
char buf[(6 + 1) * 4]; char buf[(6 + 1) * 4];
char *pbuf = buf; char *pbuf = buf;
for (ij = 0; ij < 4; ij++) for (ij = 0; ij < 4; ij++)
{ {
phogen(pbuf, 7, sha256, sizeof(sha256)); phogen(pbuf, 7, &bh);
pbuf += strlen(pbuf); pbuf += strlen(pbuf);
*pbuf++ = ' '; *pbuf++ = ' ';
} }
*(--pbuf) = '\0'; *(--pbuf) = '\0';
bhash_fini(&bh);
if (strcmp(buf, phogen_test_table[ii].output) != 0) if (strcmp(buf, phogen_test_table[ii].output) != 0)
{ {
fprintf(stderr, "Error, test failed: %s != %s\n", buf, phogen_test_table[ii].output); fprintf(stderr, "Error, test failed: %s != %s\n", buf, phogen_test_table[ii].output);

View File

@ -21,7 +21,7 @@ class Bhash:
""" """
Initialize a Bhash from a bytes object Initialize a Bhash from a bytes object
""" """
self.bhash = int.from_bytes(buf, byteorder='big') self.bhash = int.from_bytes(buf, byteorder='little')
self.bits_avail = len(buf) * 8 self.bits_avail = len(buf) * 8
self.bits_used = 0.0 self.bits_used = 0.0

View File

@ -12,11 +12,11 @@ import argparse
PHO_GEN_VOWELS = "aeiou" PHO_GEN_VOWELS = "aeiou"
PHO_GEN_TEST = [ PHO_GEN_TEST = [
[ b"passgeny", "herang xiasem zitend qibele" ], [ b"passgeny", "vatome iddeda pashis artarc" ],
[ b"phonetic", "lineum foneum zybale mangur" ], [ b"phonetic", "joredl qibini kninet ouslis" ],
[ b"generator", "latole elitab ackina exprou" ], [ b"generator", "vormul zilyth disedl chryso" ],
[ b"password", "nulize nomere fonici crednt" ], [ b"password", "wompot ficara uplort lancon" ],
[ b"duck", "catabb rompor cricin prunsi" ] ] [ b"duck", "inerea iricin ovusia irrisi" ] ]
PYTHON_HEADER = """# PYTHON_HEADER = """#
@ -57,7 +57,7 @@ class HashInt:
self.bits_used = 0 self.bits_used = 0
def from_bytes(self, buf): def from_bytes(self, buf):
self.hash = int.from_bytes(buf, byteorder='big') self.hash = int.from_bytes(buf, byteorder='little')
self.bits = len(buf) * 8 self.bits = len(buf) * 8
# Divide the HashInt by `idiv` and return the modulo # Divide the HashInt by `idiv` and return the modulo

View File

@ -8,8 +8,7 @@ bh = passgeny.bhash.Bhash()
bh.from_bytes(TEST_STRING) bh.from_bytes(TEST_STRING)
# Scan the string in reverse order (::-1) as bhash should use big endian arithmetics for c in TEST_STRING:
for c in TEST_STRING[::-1]:
hc = bh.modulo(256) hc = bh.modulo(256)
if hc != c: if hc != c:
raise Exception("Returned value is {}, expected {}".format(chr(hc), c)) raise Exception("Returned value is {}, expected {}".format(chr(hc), c))

View File

@ -5,11 +5,11 @@ from passgeny import phogen
from passgeny import bhash from passgeny import bhash
PHO_GEN_TEST = [ PHO_GEN_TEST = [
[ b"passgeny", "herang xiasem zitend qibele" ], [ b"passgeny", "vatome iddeda pashis artarc" ],
[ b"phonetic", "lineum foneum zybale mangur" ], [ b"phonetic", "joredl qibini kninet ouslis" ],
[ b"generator", "latole elitab ackina exprou" ], [ b"generator", "vormul zilyth disedl chryso" ],
[ b"password", "nulize nomere fonici crednt" ], [ b"password", "wompot ficara uplort lancon" ],
[ b"duck", "catabb rompor cricin prunsi" ] ] [ b"duck", "inerea iricin ovusia irrisi" ] ]
for pt in PHO_GEN_TEST: for pt in PHO_GEN_TEST:
bh = bhash.Bhash() bh = bhash.Bhash()