示例#1
0
文件: ecc_dh.c 项目: rsalveti/zephyr
int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve)
{

	uECC_word_t _random[NUM_ECC_WORDS * 2];
	uECC_word_t _private[NUM_ECC_WORDS];
	uECC_word_t _public[NUM_ECC_WORDS * 2];
	uECC_word_t tries;

	for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
		/* Generating _private uniformly at random: */
		uECC_RNG_Function rng_function = uECC_get_rng();
		if (!rng_function ||
			!rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE)) {
        		return 0;
		}

		/* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
		uECC_vli_mmod(_private, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));

		/* Computing public-key from private: */
		if (EccPoint_compute_public_key(_public, _private, curve)) {

			/* Converting buffers to correct bit order: */
			uECC_vli_nativeToBytes(private_key,
					       BITS_TO_BYTES(curve->num_n_bits),
					       _private);
			uECC_vli_nativeToBytes(public_key,
					       curve->num_bytes,
					       _public);
			uECC_vli_nativeToBytes(public_key + curve->num_bytes,
 					       curve->num_bytes,
					       _public + curve->num_words);

			/* erasing temporary buffer that stored secret: */
			memset(_private, 0, NUM_ECC_BYTES);

      			return 1;
    		}
  	}
	return 0;
}
示例#2
0
文件: jsonx.c 项目: ymv/jsonx
ERL_NIF_TERM
make_decoder_resource_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]){
  unsigned records_cnt, ukeys_cnt, keys_cnt;

  enif_get_uint(env, argv[0], &records_cnt);
  enif_get_uint(env, argv[1], &ukeys_cnt);
  enif_get_uint(env, argv[2], &keys_cnt);
  PrivData* priv = (PrivData*)enif_priv_data(env);
  unsigned resource_sz = dec_resource_size(records_cnt, ukeys_cnt, keys_cnt);
  DecEntry *dec_entry = (DecEntry*)enif_alloc_resource(priv->decoder_RSTYPE, resource_sz);

  dec_entry->records_cnt = records_cnt;
  dec_entry->ukeys_cnt = ukeys_cnt;
  dec_entry->keys_cnt = keys_cnt;

  ERL_NIF_TERM list, head, tail;
  int i;
  ERL_NIF_TERM* ukeys = ukeys_base(dec_entry);
  list = argv[3]; //UKeys
  for(i = 0; i < ukeys_cnt; i++){
    if(!enif_get_list_cell(env, list, &head, &tail))
      goto error;
    ukeys[i] = head;
    list = tail;
  }

  unsigned* keys = keys_base(dec_entry, ukeys_cnt);
  list = argv[4]; //KeyNums
  for(i = 0; i < keys_cnt; i++){
    if(!enif_get_list_cell(env, list, &head, &tail))
      goto error;
    if(!enif_get_uint(env, head, &keys[i]))
      goto error;
    list = tail;
  }
 
  DecRecord* records = records_base(dec_entry, ukeys_cnt, keys_cnt);
  long  *bit_mask = bit_mask_base(dec_entry, ukeys_cnt, keys_cnt, records_cnt);
  size_t bit_mask_len = BITS_TO_WORDS(dec_entry->ukeys_cnt);
  memset((void *)bit_mask, 0, bit_mask_array_size(ukeys_cnt, records_cnt));
  const ERL_NIF_TERM *tuple;
  int arity, k;
  list = argv[5]; //Records
  for(i = 0; i < records_cnt; i++){
    if(!enif_get_list_cell(env, list, &head, &tail))
      goto error;
    if(!(enif_get_tuple(env, head, &arity, &tuple) && (arity == 3)))
      goto error;
    if(!enif_is_atom(env, tuple[0]))
      goto error;
    records[i].tag = tuple[0];
    if(!enif_get_uint(env, tuple[1], &records[i].keys_off))
      goto error;
    if(!enif_get_uint(env, tuple[2], &records[i].arity))
      goto error;
    list = tail;

    for(k = 0; k < records[i].arity; k++){
      int p = records[i].keys_off + k; //position in keys
      set_bit(keys[p], bit_mask + (i * bit_mask_len));
    }
  }

  ERL_NIF_TERM ret = enif_make_resource(env, (void *)dec_entry);
  enif_release_resource(dec_entry);
  return ret;
 error:
  enif_release_resource(dec_entry);
  return enif_make_badarg(env);
}