void mm_hash_update(MM_Hash *table, char *key, int length, void *data) { MM_Bucket *b, p; unsigned int hash; hash = hash_hash(key, length) % MM_HASH_SIZE; for(b = table->buckets[ hash ]; b; b = b->next) { if (hash != b->hash) continue; if (length != b->length) continue; if (memcmp(key, b->key, length)) continue; if (table->dtor) table->dtor(b->data); b->data = data; } if(!b) { if ((b = (MM_Bucket *) mm_malloc( table->mm, sizeof(MM_Bucket))) == NULL) return; if ((b->key = (char *) mm_malloc( table->mm, length + 1)) == NULL) { free(b); return; } memcpy(b->key, key, length); b->key[length] = 0; b->length = length; b->hash = hash; b->data = data; b->next = table->buckets[ hash ]; table->buckets[ hash ] = b; } table->nElements++; }
/* Removes the key from the hash table. Does not signal an error if the key * was not present. */ void hash_remove (struct hashtable *tablestruct, uintptr_t key) { uintptr_t hash; struct hashentry *entry, *tmpentry; hash = hash_hash (key) & (tablestruct->size - 1); entry = tablestruct->table[hash]; /* If this is the first entry then it needs special handling. */ if (entry && entry->key == key) { tmpentry = entry->next; free (entry); tablestruct->table[hash] = tmpentry; } else { while (entry) { if (entry->next && entry->next->key == key) { tmpentry = entry->next; entry->next = entry->next->next; free (tmpentry); break; } entry = entry->next; } } }
/* * A new sign in group 'groupname' is added. If the group is not present, * create it. Otherwise reference the group. */ static signgroup_T * sign_group_ref(char_u *groupname) { hash_T hash; hashitem_T *hi; signgroup_T *group; hash = hash_hash(groupname); hi = hash_lookup(&sg_table, groupname, hash); if (HASHITEM_EMPTY(hi)) { // new group group = (signgroup_T *)alloc( (unsigned)(sizeof(signgroup_T) + STRLEN(groupname))); if (group == NULL) return NULL; STRCPY(group->sg_name, groupname); group->refcount = 1; group->next_sign_id = 1; hash_add_item(&sg_table, hi, group->sg_name, hash); } else { // existing group group = HI2SG(hi); group->refcount++; } return group; }
void mm_hash_delete(MM_Hash *table, char *key, int length) { MM_Bucket *b; MM_Bucket *prev = NULL; unsigned int hash; hash = hash_hash(key, length) % MM_HASH_SIZE; for (b = table->buckets[ hash ]; b; b = b->next) { if (hash != b->hash || length != b->length || memcmp(key, b->key, length)) { prev = b; continue; } /* unlink */ if (prev) { prev->next = b->next; } else { table->buckets[hash] = b->next; } if (table->dtor) table->dtor(b->data); mm_free(table->mm, b->key); mm_free(table->mm, b); break; } }
/* * Hash generic data. */ static hash_t hash_data(const char *data, size_t len, hash_t key) { hash_t hash = HASH(0x8E93668B31ACE316ull, 0xE9270DEF701B0ECFull); size_t i; for (i = 0; i + sizeof(hash_t) <= len; i += sizeof(hash_t)) { hash_t chunk = *(hash_t *)(data + i); hash = hash_join(i, hash, hash_hash(chunk, key)); } char buf[sizeof(hash_t)] = {0}; memcpy(buf, data+i, len-i); const char *buf1 = buf; // Suppress warning. hash_t chunk = *(hash_t *)buf1; hash = hash_join(i, hash, hash_hash(chunk, key)); return hash; }
/// Add item with key "key" to hashtable "ht". /// /// @param ht /// @param key /// /// @returns FAIL when out of memory or the key is already present. int hash_add(hashtab_T *ht, char_u *key) { hash_T hash = hash_hash(key); hashitem_T *hi = hash_lookup(ht, key, hash); if (!HASHITEM_EMPTY(hi)) { EMSG2(_(e_intern2), "hash_add()"); return FAIL; } return hash_add_item(ht, hi, key, hash); }
HASH_INT hash_tands(HASH_INT triad, hash_table_t *theTable) { hash_cell_t *theElement= theTable->table+hash_hash(triad,theTable); int isScratch; if(theElement->triad == 0)/*No such a triad -- install it and return:*/ return theElement->triad=triad; if(theElement->next == 0){ if( hash_cmp(triad, theElement->triad, theTable) ) return theElement->triad; isScratch=0;/*theElement points somewhere into the table*/ }else{ #ifdef DEBUG HASH_INT maxCNew=0; #endif isScratch=1;/*theElement points somewhere into the scratch*/ for(;;){ if( hash_cmp(triad, theElement->triad, theTable) ) return theElement->triad; if(theElement->next == 0) break; theElement= theTable->scratch+theElement->next; #ifdef DEBUG maxCNew++; #endif }/*for(;;)*/ #ifdef DEBUG nC++; avC+=maxCNew; if(maxCNew>maxC) maxC=maxCNew; #endif }/*else*/ /*Install a new element in scratch:*/ if(theTable->free == theTable->max){/*(re)allocate the scratch array:*/ if(theTable->max == 0) hash_alloc(theTable); else{ if(isScratch){/*theElement points somewhere into the scratch*/ /*Store the position:*/ HASH_INT n=theElement-theTable->scratch; hash_realloc(theTable); /*The scratch was changed, restore the position:*/ theElement=theTable->scratch+n; }else/*theElement has no relation to the scratch, just realloc the scratch:*/ hash_realloc(theTable); }/*else*/ }/*if(theTable->free == theTable->max)*/ /*Install the triad and return:*/ theTable->scratch[theTable->free].triad=triad; theElement->next=theTable->free; theTable->scratch[theTable->free].next=0; theTable->free++; return triad; }/*hash_tands*/
/* In this file because it uses the hash */ void cryptonite_decaf_ed448_convert_private_key_to_x448 ( uint8_t x[CRYPTONITE_DECAF_X448_PRIVATE_BYTES], const uint8_t ed[CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES] ) { /* pass the private key through hash_hash function */ /* and keep the first CRYPTONITE_DECAF_X448_PRIVATE_BYTES bytes */ hash_hash( x, CRYPTONITE_DECAF_X448_PRIVATE_BYTES, ed, CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES ); }
void * hash_lookup (struct hashtable *tablestruct, uintptr_t key) { uintptr_t hash; struct hashentry *entry; hash = hash_hash (key) & (tablestruct->size - 1); for (entry = tablestruct->table[hash]; entry != NULL; entry = entry->next) { if (entry->key == key) { return entry->value; } } return NULL; }
/*Returns address of a triad, or 0*/ HASH_INT hash_check(HASH_INT triad, hash_table_t *theTable) { HASH_INT theIndex=hash_hash(triad,theTable); hash_cell_t *theElement= theTable->table+theIndex; if(theElement->triad != 0)for(;;){ if( hash_cmp(triad, theElement->triad, theTable) ) return theElement->triad; if(theElement->next == 0) break; theElement= theTable->scratch+theElement->next; }/*if(theElement->triad != 0)for(;;)*/ return 0; }/*hash_check*/
void *mm_hash_find(MM_Hash *table, const void *key, int length) { MM_Bucket *b; unsigned int hash = hash_hash((const char *)key, length) % MM_HASH_SIZE; for (b = table->buckets[ hash ]; b; b = b->next) { if (hash != b->hash) continue; if (length != b->length) continue; if (memcmp(key, b->key, length)) continue; return b->data; } return NULL; }
/* Add the key to the hash table. Assumes the key is not already present. */ int hash_insert (struct hashtable *tablestruct, uintptr_t key, void *value) { uintptr_t hash; struct hashentry *entry; hash = hash_hash (key) & (tablestruct->size - 1); //printf ("bucket is %d\n", hash); entry = malloc (sizeof (struct hashentry)); if (!entry) { return -1; } entry->key = key; entry->value = value; entry->next = tablestruct->table[hash]; tablestruct->table[hash] = entry; return 0; }
void cryptonite_decaf_ed448_derive_public_key ( uint8_t pubkey[CRYPTONITE_DECAF_EDDSA_448_PUBLIC_BYTES], const uint8_t privkey[CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES] ) { /* only this much used for keygen */ uint8_t secret_scalar_ser[CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES]; hash_hash( secret_scalar_ser, sizeof(secret_scalar_ser), privkey, CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES ); clamp(secret_scalar_ser); API_NS(scalar_t) secret_scalar; API_NS(scalar_decode_long)(secret_scalar, secret_scalar_ser, sizeof(secret_scalar_ser)); /* Since we are going to mul_by_cofactor during encoding, divide by it here. * However, the EdDSA base point is not the same as the decaf base point if * the sigma isogeny is in use: the EdDSA base point is on Etwist_d/(1-d) and * the decaf base point is on Etwist_d, and when converted it effectively * picks up a factor of 2 from the isogenies. So we might start at 2 instead of 1. */ for (unsigned int c = EDDSA_BASE_POINT_RATIO; c < COFACTOR; c <<= 1) { API_NS(scalar_halve)(secret_scalar,secret_scalar); } API_NS(point_t) p; API_NS(precomputed_scalarmul)(p,API_NS(precomputed_base),secret_scalar); API_NS(point_mul_by_cofactor_and_encode_like_eddsa)(pubkey, p); /* Cleanup */ API_NS(scalar_destroy)(secret_scalar); API_NS(point_destroy)(p); cryptonite_decaf_bzero(secret_scalar_ser, sizeof(secret_scalar_ser)); }
/// Find item for given "key" in hashtable "ht". /// /// @param key The key of the looked-for item. Must not be NULL. /// /// @return Pointer to the hash item corresponding to the given key. /// If not found, then return pointer to the empty item that would be /// used for that key. /// WARNING: Returned pointer becomes invalid as soon as the hash table /// is changed in any way. hashitem_T *hash_find(hashtab_T *ht, const char_u *key) { return hash_lookup(ht, (const char *)key, STRLEN(key), hash_hash(key)); }
/** * Gets the index of the bucket a key should fall under * given the number of buckets and key * * @param key Key to check * @param num_buckets Number of buckets to get index relative to * @return Index of bucket */ static int bucket_index(const char *key, int num_buckets) { return hash_hash(key) % num_buckets; }
void cryptonite_decaf_ed448_sign ( uint8_t signature[CRYPTONITE_DECAF_EDDSA_448_SIGNATURE_BYTES], const uint8_t privkey[CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES], const uint8_t pubkey[CRYPTONITE_DECAF_EDDSA_448_PUBLIC_BYTES], const uint8_t *message, size_t message_len, uint8_t prehashed, const uint8_t *context, uint8_t context_len ) { API_NS(scalar_t) secret_scalar; hash_ctx_t hash; { /* Schedule the secret key */ struct { uint8_t secret_scalar_ser[CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES]; uint8_t seed[CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES]; } __attribute__((packed)) expanded; hash_hash( (uint8_t *)&expanded, sizeof(expanded), privkey, CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES ); clamp(expanded.secret_scalar_ser); API_NS(scalar_decode_long)(secret_scalar, expanded.secret_scalar_ser, sizeof(expanded.secret_scalar_ser)); /* Hash to create the nonce */ hash_init_with_dom(hash,prehashed,0,context,context_len); hash_update(hash,expanded.seed,sizeof(expanded.seed)); hash_update(hash,message,message_len); cryptonite_decaf_bzero(&expanded, sizeof(expanded)); } /* Decode the nonce */ API_NS(scalar_t) nonce_scalar; { uint8_t nonce[2*CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES]; hash_final(hash,nonce,sizeof(nonce)); API_NS(scalar_decode_long)(nonce_scalar, nonce, sizeof(nonce)); cryptonite_decaf_bzero(nonce, sizeof(nonce)); } uint8_t nonce_point[CRYPTONITE_DECAF_EDDSA_448_PUBLIC_BYTES] = {0}; { /* Scalarmul to create the nonce-point */ API_NS(scalar_t) nonce_scalar_2; API_NS(scalar_halve)(nonce_scalar_2,nonce_scalar); for (unsigned int c = 2*EDDSA_BASE_POINT_RATIO; c < COFACTOR; c <<= 1) { API_NS(scalar_halve)(nonce_scalar_2,nonce_scalar_2); } API_NS(point_t) p; API_NS(precomputed_scalarmul)(p,API_NS(precomputed_base),nonce_scalar_2); API_NS(point_mul_by_cofactor_and_encode_like_eddsa)(nonce_point, p); API_NS(point_destroy)(p); API_NS(scalar_destroy)(nonce_scalar_2); } API_NS(scalar_t) challenge_scalar; { /* Compute the challenge */ hash_init_with_dom(hash,prehashed,0,context,context_len); hash_update(hash,nonce_point,sizeof(nonce_point)); hash_update(hash,pubkey,CRYPTONITE_DECAF_EDDSA_448_PUBLIC_BYTES); hash_update(hash,message,message_len); uint8_t challenge[2*CRYPTONITE_DECAF_EDDSA_448_PRIVATE_BYTES]; hash_final(hash,challenge,sizeof(challenge)); hash_destroy(hash); API_NS(scalar_decode_long)(challenge_scalar,challenge,sizeof(challenge)); cryptonite_decaf_bzero(challenge,sizeof(challenge)); } API_NS(scalar_mul)(challenge_scalar,challenge_scalar,secret_scalar); API_NS(scalar_add)(challenge_scalar,challenge_scalar,nonce_scalar); cryptonite_decaf_bzero(signature,CRYPTONITE_DECAF_EDDSA_448_SIGNATURE_BYTES); memcpy(signature,nonce_point,sizeof(nonce_point)); API_NS(scalar_encode)(&signature[CRYPTONITE_DECAF_EDDSA_448_PUBLIC_BYTES],challenge_scalar); API_NS(scalar_destroy)(secret_scalar); API_NS(scalar_destroy)(nonce_scalar); API_NS(scalar_destroy)(challenge_scalar); }
uint64_t hash_pos(hash_t *hash, uint8_t *buf, uint64_t n) { return hash_hash(buf, n) % hash->n; }
/* * Find "key" in hashtable "ht". "key" must not be NULL. * Always returns a pointer to a hashitem. If the item was not found then * HASHITEM_EMPTY() is TRUE. The pointer is then the place where the key * would be added. * WARNING: The returned pointer becomes invalid when the hashtable is changed * (adding, setting or removing an item)! */ hashitem_T * hash_find(hashtab_T *ht, char_u *key) { return hash_lookup(ht, key, hash_hash(key)); }