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);
}
/*
* 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 *pi;
uint32_t *pend;
uint8_t *pdata;
uint64_t n;
uint32_t rv = 0;
@ -36,15 +39,25 @@ uint32_t bhash_mod32(bhash_t *bh, uint32_t mod)
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 |= 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;
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);

View File

@ -1,5 +1,8 @@
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 "bhash.h"
/* List of vowels */
#define PHOGEN_VOWELS "aeiou"
@ -78,11 +80,11 @@ struct
char *output;
} phogen_test_table[] =
{
{ "passgeny", "herang xiasem zitend qibele" },
{ "phonetic", "lineum foneum zybale mangur" },
{ "generator", "latole elitab ackina exprou" },
{ "password", "nulize nomere fonici crednt" },
{ "duck", "catabb rompor cricin prunsi" },
{ "passgeny", "vatome iddeda pashis artarc" },
{ "phonetic", "joredl qibini kninet ouslis" },
{ "generator", "vormul zilyth disedl chryso" },
{ "password", "wompot ficara uplort lancon" },
{ "duck", "inerea iricin ovusia irrisi" },
};
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
* 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;
@ -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;
}
nsel = bigint_mod32(in, insz, nsize);
nsel = bhash_mod32(bh, nsize);
out[ii] = phogen_freq_list[ni][nsel].fe_letter;
@ -523,6 +498,7 @@ bool phogen_test(void)
SHA256_CTX sha256_ctx;
int ii;
int ij;
bhash_t bh;
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_Final(sha256, &sha256_ctx);
bhash_init(&bh, sha256, sizeof(sha256));
/* 4 words, each 6 characters long and 1 more character for spaces */
char buf[(6 + 1) * 4];
char *pbuf = buf;
for (ij = 0; ij < 4; ij++)
{
phogen(pbuf, 7, sha256, sizeof(sha256));
phogen(pbuf, 7, &bh);
pbuf += strlen(pbuf);
*pbuf++ = ' ';
}
*(--pbuf) = '\0';
bhash_fini(&bh);
if (strcmp(buf, phogen_test_table[ii].output) != 0)
{
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
"""
self.bhash = int.from_bytes(buf, byteorder='big')
self.bhash = int.from_bytes(buf, byteorder='little')
self.bits_avail = len(buf) * 8
self.bits_used = 0.0

View File

@ -12,11 +12,11 @@ import argparse
PHO_GEN_VOWELS = "aeiou"
PHO_GEN_TEST = [
[ b"passgeny", "herang xiasem zitend qibele" ],
[ b"phonetic", "lineum foneum zybale mangur" ],
[ b"generator", "latole elitab ackina exprou" ],
[ b"password", "nulize nomere fonici crednt" ],
[ b"duck", "catabb rompor cricin prunsi" ] ]
[ b"passgeny", "vatome iddeda pashis artarc" ],
[ b"phonetic", "joredl qibini kninet ouslis" ],
[ b"generator", "vormul zilyth disedl chryso" ],
[ b"password", "wompot ficara uplort lancon" ],
[ b"duck", "inerea iricin ovusia irrisi" ] ]
PYTHON_HEADER = """#
@ -57,7 +57,7 @@ class HashInt:
self.bits_used = 0
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
# Divide the HashInt by `idiv` and return the modulo

View File

@ -8,8 +8,7 @@ bh = passgeny.bhash.Bhash()
bh.from_bytes(TEST_STRING)
# Scan the string in reverse order (::-1) as bhash should use big endian arithmetics
for c in TEST_STRING[::-1]:
for c in TEST_STRING:
hc = bh.modulo(256)
if 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
PHO_GEN_TEST = [
[ b"passgeny", "herang xiasem zitend qibele" ],
[ b"phonetic", "lineum foneum zybale mangur" ],
[ b"generator", "latole elitab ackina exprou" ],
[ b"password", "nulize nomere fonici crednt" ],
[ b"duck", "catabb rompor cricin prunsi" ] ]
[ b"passgeny", "vatome iddeda pashis artarc" ],
[ b"phonetic", "joredl qibini kninet ouslis" ],
[ b"generator", "vormul zilyth disedl chryso" ],
[ b"password", "wompot ficara uplort lancon" ],
[ b"duck", "inerea iricin ovusia irrisi" ] ]
for pt in PHO_GEN_TEST:
bh = bhash.Bhash()