misc/hash-streaming.c
int hash_step(state_t *st, buffer_t block) {

    if (st->hash) {
        /* The current block we have is intended to be a hash. */

        assert(st->pending_hash.data != NULL);

        /* Concatenate the hash we have onto the pending hash and then hash the
         * result.
         */
        unsigned char amalgamated[st->pending_hash.length + block.length];
        memcpy(amalgamated, st->pending_hash.data, st->pending_hash.length);
        memcpy((void*)amalgamated + st->pending_hash.length, block.data, block.length);
        buffer_t buffer = {
            .data = amalgamated,
            .length = sizeof amalgamated,
        };
        buffer_t hash = st->hasher(buffer);

        /* If the hash did not match our current target, we have failed
         * authentication.
         */
        if (st->target_hash.length != hash.length ||
                memcmp(st->target_hash.data, hash.data, st->target_hash.length) != 0)
            return -1;

        /* Now that we validated the received block, stash it as the next target
         * hash.
         */
        st->target_hash = block;
        st->pending_hash.data = NULL;
        hash_tick(st);

    } else {
        /* The current block we have is intended to be data. */

        assert(st->pending_hash.data == NULL);

        /* Just hash the data and stash it for the next tick. */
        st->pending_hash = st->hasher(block);
        hash_tick(st);
    }

    return 0;
}
/* Take a step based on the block we just received. */
Take a step based on the block we just received.
Take a step based on the block we just received .
int hash_step(state_t *st, buffer_t block) {
    if (st->hash) {
        assert(st->pending_hash.data != NULL);
        unsigned char amalgamated[st->pending_hash.length + block.length];
        memcpy(amalgamated, st->pending_hash.data, st->pending_hash.length);
        memcpy((void*)amalgamated + st->pending_hash.length, block.data, block.length);
        buffer_t buffer = {
            .data = amalgamated,
            .length = sizeof amalgamated,
        };
        buffer_t hash = st->hasher(buffer);
        if (st->target_hash.length != hash.length ||
                memcmp(st->target_hash.data, hash.data, st->target_hash.length) != 0)
            return -1;
        st->target_hash = block;
        st->pending_hash.data = NULL;
        hash_tick(st);
    } else {
        assert(st->pending_hash.data == NULL);
        st->pending_hash = st->hasher(block);
        hash_tick(st);
    }
    return 0;
}
int hash_step ( state_t * st , buffer_t block ) { if ( st -> hash ) { assert ( st -> pending_hash . data != NULL ) ; unsigned char amalgamated [ st -> pending_hash . length + block . length ] ; memcpy ( amalgamated , st -> pending_hash . data , st -> pending_hash . length ) ; memcpy ( ( void * ) amalgamated + st -> pending_hash . length , block . data , block . length ) ; buffer_t buffer = { . data = amalgamated , . length = sizeof amalgamated , } ; buffer_t hash = st -> hasher ( buffer ) ; if ( st -> target_hash . length != hash . length || memcmp ( st -> target_hash . data , hash . data , st -> target_hash . length ) != 0 ) return -1 ; st -> target_hash = block ; st -> pending_hash . data = NULL ; hash_tick ( st ) ; } else { assert ( st -> pending_hash . data == NULL ) ; st -> pending_hash = st -> hasher ( block ) ; hash_tick ( st ) ; } return 0 ; }
Take a step based on the block we just received.
Take a step based on the block we just received .
/* The current block we have is intended to be a hash. */ /* Concatenate the hash we have onto the pending hash and then hash the
         * result.
         */ /* If the hash did not match our current target, we have failed
         * authentication.
         */ /* Now that we validated the received block, stash it as the next target
         * hash.
         */ /* The current block we have is intended to be data. */ /* Just hash the data and stash it for the next tick. */