Exemplo n.º 1
0
/* Measure the performance of a hashing primitive */
static void perf_hash(int id)
{
    NoiseHashState *hash;
    uint8_t data[BLOCK_SIZE];
    timestamp_t start, end;
    int count;
    double elapsed;

    if (noise_hashstate_new_by_id(&hash, id) != NOISE_ERROR_NONE)
        return;

    memset(data, 0xAA, sizeof(data));
    noise_hashstate_reset(hash);
    start = current_timestamp();
    for (count = 0; count < (MB_COUNT * BLOCKS_PER_MB); ++count)
        noise_hashstate_update(hash, data, sizeof(data));
    end = current_timestamp();

    elapsed = elapsed_to_seconds(start, end) / (double)MB_COUNT;
    printf("%-20s%8.2f          %8.2f\n",
           noise_id_to_name(NOISE_HASH_CATEGORY, id),
           1.0 / elapsed, units / elapsed);

    noise_hashstate_free(hash);
}
Exemplo n.º 2
0
/**
 * \brief Creates a new SymmetricState object.
 *
 * \param state Points to the variable where to store the pointer to
 * the new SymmetricState object.
 * \param name The name of the Noise protocol to use.  This string
 * must be NUL-terminated.
 * \param id The protocol identifier as a set of algorithm identifiers.
 *
 * This is the internal implementation of noise_symmetricstate_new_by_id()
 * and noise_symmetricstate_new_by_name().
 */
static int noise_symmetricstate_new
    (NoiseSymmetricState **state, const char *name, const NoiseProtocolId *id)
{
    NoiseSymmetricState *new_state;
    size_t name_len;
    size_t hash_len;
    int err;

    /* Construct a state object and initialize it */
    new_state = noise_new(NoiseSymmetricState);
    if (!new_state)
        return NOISE_ERROR_NO_MEMORY;
    new_state->id = *id;
    err = noise_cipherstate_new_by_id(&(new_state->cipher), id->cipher_id);
    if (err != NOISE_ERROR_NONE) {
        noise_symmetricstate_free(new_state);
        return err;
    }
    err = noise_hashstate_new_by_id(&(new_state->hash), id->hash_id);
    if (err != NOISE_ERROR_NONE) {
        noise_symmetricstate_free(new_state);
        return err;
    }

    /* Check the hash length against the maximum, just in case someone
       adds a new hash algorithm in the future with longer output.
       If this happens, modify NOISE_MAX_HASHLEN in internal.h accordingly */
    hash_len = noise_hashstate_get_hash_length(new_state->hash);
    if (hash_len > NOISE_MAX_HASHLEN) {
        noise_symmetricstate_free(new_state);
        return NOISE_ERROR_INVALID_LENGTH;
    }

    /* The key length must also be less than or equal to the hash length */
    if (noise_cipherstate_get_key_length(new_state->cipher) > hash_len) {
        noise_symmetricstate_free(new_state);
        return NOISE_ERROR_INVALID_LENGTH;
    }

    /* Initialize the chaining key "ck" and the handshake hash "h" from
       the protocol name.  If the name is too long, hash it down first */
    name_len = strlen(name);
    if (name_len <= hash_len) {
        memcpy(new_state->h, name, name_len);
        memset(new_state->h + name_len, 0, hash_len - name_len);
    } else {
        noise_hashstate_hash_one
            (new_state->hash, (const uint8_t *)name, name_len,
             new_state->h, hash_len);
    }
    memcpy(new_state->ck, new_state->h, hash_len);

    /* Ready to go */
    *state = new_state;
    return NOISE_ERROR_NONE;
}