/** * \brief Creates a new random number generator. * * \param state Points to the variable where to store the pointer to * the new RandState object. * * \return NOISE_ERROR_NONE on success. * \return NOISE_ERROR_INVALID_PARAM if \a state is NULL. * \return NOISE_ERROR_NO_MEMORY if there is insufficient memory to * allocate the new RandState object. * * \sa noise_randstate_free(), noise_randstate_generate() */ int noise_randstate_new(NoiseRandState **state) { /* Starting key for the random state before the first reseed. This is the SHA256 initialization vector, to introduce a little chaos into the starting state. */ static uint8_t const starting_key[32] = { 0x6A, 0x09, 0xE6, 0x67, 0xBB, 0x67, 0xAE, 0x85, 0x3C, 0x6E, 0xF3, 0x72, 0xA5, 0x4F, 0xF5, 0x3A, 0x51, 0x0E, 0x52, 0x7F, 0x9B, 0x05, 0x68, 0x8C, 0x1F, 0x83, 0xD9, 0xAB, 0x5B, 0xE0, 0xCD, 0x19 }; /* Validate the parameter */ if (!state) return NOISE_ERROR_INVALID_PARAM; /* Create the random number generator state */ *state = noise_new(NoiseRandState); if (!(*state)) return NOISE_ERROR_NO_MEMORY; /* Initialize the random number generator */ chacha_keysetup(&((*state)->chacha), starting_key, 256); noise_randstate_reseed((*state)); return NOISE_ERROR_NONE; }
/** * \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; }
NoiseHashState *noise_sha256_new(void) { NoiseSHA256State *state = noise_new(NoiseSHA256State); if (!state) return 0; state->parent.hash_id = NOISE_HASH_SHA256; state->parent.hash_len = 32; state->parent.block_len = 64; state->parent.reset = noise_sha256_reset; state->parent.update = noise_sha256_update; state->parent.finalize = noise_sha256_finalize; return &(state->parent); }
NoiseCipherState *noise_aesgcm_new(void) { NoiseAESGCMState *state = noise_new(NoiseAESGCMState); if (!state) return 0; state->parent.cipher_id = NOISE_CIPHER_AESGCM; state->parent.key_len = 32; state->parent.mac_len = 16; state->parent.create = noise_aesgcm_new; state->parent.init_key = noise_aesgcm_init_key; state->parent.encrypt = noise_aesgcm_encrypt; state->parent.decrypt = noise_aesgcm_decrypt; return &(state->parent); }
static gboolean marble_prepare (GstGeometricTransform * trans) { GstMarble *marble = GST_MARBLE_CAST (trans); gint i; if (!marble->noise) { marble->noise = noise_new (); } g_free (marble->sin_table); g_free (marble->cos_table); marble->sin_table = g_malloc0 (sizeof (gdouble) * 256); marble->cos_table = g_malloc0 (sizeof (gdouble) * 256); for (i = 0; i < 256; i++) { gdouble angle = (G_PI * 2 * i) / 256.0 * marble->turbulence; marble->sin_table[i] = -marble->yscale * sin (angle); marble->cos_table[i] = marble->yscale * cos (angle); } return TRUE; }
/** * @brief Loads the spfx stack. * * @return 0 on success. * * @todo Make spfx not hard-coded. */ int spfx_load (void) { int mem; uint32_t bufsize; char *buf; xmlNodePtr node; xmlDocPtr doc; /* Load and read the data. */ buf = ndata_read( SPFX_DATA_PATH, &bufsize ); doc = xmlParseMemory( buf, bufsize ); /* Check to see if document exists. */ node = doc->xmlChildrenNode; if (!xml_isNode(node,SPFX_XML_ID)) { ERR("Malformed '"SPFX_DATA_PATH"' file: missing root element '"SPFX_XML_ID"'"); return -1; } /* Check to see if is populated. */ node = node->xmlChildrenNode; /* first system node */ if (node == NULL) { ERR("Malformed '"SPFX_DATA_PATH"' file: does not contain elements"); return -1; } /* First pass, loads up ammunition. */ mem = 0; do { xml_onlyNodes(node); if (xml_isNode(node,SPFX_XML_TAG)) { spfx_neffects++; if (spfx_neffects > mem) { if (mem == 0) mem = SPFX_CHUNK_MIN; else mem *= 2; spfx_effects = realloc(spfx_effects, sizeof(SPFX_Base)*mem); } spfx_base_parse( &spfx_effects[spfx_neffects-1], node ); } else WARN("'"SPFX_DATA_PATH"' has unknown node '%s'.", node->name); } while (xml_nextNode(node)); /* Shrink back to minimum - shouldn't change ever. */ spfx_effects = realloc(spfx_effects, sizeof(SPFX_Base) * spfx_neffects); /* Clean up. */ xmlFreeDoc(doc); free(buf); /* * Now initialize force feedback. */ spfx_hapticInit(); shake_noise = noise_new( 1, NOISE_DEFAULT_HURST, NOISE_DEFAULT_LACUNARITY ); return 0; }