summaryrefslogtreecommitdiffstats
path: root/struct.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2018-07-04 22:07:33 -0700
committerKaz Kylheku <kaz@kylheku.com>2018-07-04 22:07:33 -0700
commit44b66777caafeebf543c32c9373632c04f748353 (patch)
tree40e7f6f3a3b3435ef71865a48f072c3c732e3206 /struct.c
parent9127ea3ff84327cd32552b4a90d56b4bf114bb86 (diff)
downloadtxr-44b66777caafeebf543c32c9373632c04f748353.tar.gz
txr-44b66777caafeebf543c32c9373632c04f748353.tar.bz2
txr-44b66777caafeebf543c32c9373632c04f748353.zip
hashing: overhaul part 1.
Hashing of buffers and character strings is being replaced with a seedable hash, providing a tool against denial of service attacks against hash tables. This commit lays most of the groundwork: most of the internal interface changes, and a new hashing implementation. What is missing is the mechanisms to do the seeding. * hash.c (struct hash_ops): Hash operation now takes a seed argument of type ucnum. (struct hash): New member, seed. (hash_str_limit): Default value changed to INT_MAX. A short value opens the gateway to an obvious collision attack whereby strings sharing the same 128 character prefix are entered into the same hash table, which will defeat any seedings strategy. (randbox): New static array. Values come from the Kazlib hash module, but are not used in exactly the same way. (hash_c_str, hash_buf): Now take a seed argument, and are rewritten. (equal_hash): Takes a seed, and passes it to hash_c_str, hash_buf and to recursive self calls. (eql_hash_op): New static function. Adapts the eql_hash operation, which doesn't take a seed, to the new interface that calls for a seed. (obj_eq_hash_op): Take a seed; ignore it. (hash_hash_op): Take a seed, pass it down to equal_hash. (hash_eql_ops): Wire hash functiono pointer to eql_hash_op instead of eql_hash. (make_hash): For now, intialize the hash's seed to zero. (make_similar_hash): Copy original hash's seed. (gethash_c, gethash_e, remhash): Pass hash table's seed to the hashing function. (hash_equal): Pass a seed of zero to equal_hash for now; this function will soon acquire an optional parameter for the seed. * hash.h (equal_hash): Declaration updated. * lib.c (cobj_handle_hash_op): Take seed argument, pass down. * lib.h (cobj_ops): Hash operation now takes seed. (cobj_eq_hash_op, cobj_handle_hash_op): Declarations updated. * struct.c (struct_inst_hash): Take seed argument, pass down. * tests/009/json.expected: Updated, because the hash table included in this output is now printed in a different order.
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c6
1 files changed, 3 insertions, 3 deletions
diff --git a/struct.c b/struct.c
index f55c28f9..d1887f19 100644
--- a/struct.c
+++ b/struct.c
@@ -1492,16 +1492,16 @@ static val struct_inst_equal(val left, val right)
return t;
}
-static cnum struct_inst_hash(val obj, int *count)
+static cnum struct_inst_hash(val obj, int *count, ucnum seed)
{
struct struct_inst *si = coerce(struct struct_inst *, obj->co.handle);
struct struct_type *st = si->type;
- cnum nslots = st->nslots, sl, out = equal_hash(st->self, count);
+ cnum nslots = st->nslots, sl, out = equal_hash(st->self, count, seed);
check_init_lazy_struct(obj, si);
for (sl = 0; sl < nslots; sl++) {
- cnum hash = equal_hash(si->slot[sl], count);
+ cnum hash = equal_hash(si->slot[sl], count, seed);
out += hash;
out &= NUM_MAX;
}