Exemplo n.º 1
0
int main(int argc, char *argv[]) {

	NTL::ZZ p(NTL::INIT_VAL, argv[1]);
	NTL::ZZ q(NTL::INIT_VAL, argv[2]);

	rsa_keys_tuple keys;

	if (argc == 5) {
		keys = gen_keys(p, q, atoi(argv[4]));
	} else {
		keys = gen_keys(p, q);
	}


	rsa_key private_key, public_key;

	public_key = std::get<0>(keys);
	private_key = std::get<1>(keys);

	std::cout << "Public Key: ";
	print_rsa_key(public_key);

	std::cout << "Private Key: ";
	print_rsa_key(private_key);

	return 0;
}
Exemplo n.º 2
0
/* write_new_hid_file - generates a new HID and save to @filename.
 *
 *	In fact, all that is genereaged is a private key.
 *
 *	File is flushed before being closed to guarantee that
 *	it is written.
 *
 * RETURN
 *	returns zero on success; otherwise a negative number.
 */
static int write_new_hid_file(const char *filename)
{
	FILE *f;
	PPK_KEY *pkey;
	int fd, rc;

	fd = creat(filename, 0600);
	if (fd < 0) {
		perror("Couldn't create new HID file");
		return -1;
	}
	f = fdopen(fd, "w");
	assert(f);

	pkey = gen_keys();
	if (!pkey) {
		rc = -ENOMEM;
		goto close_f;
	}

	rc = write_prvpem(pkey, f);
	if (rc)
		goto pkey;

	assert(!fflush(f));

pkey:
	ppk_free_key(pkey);
close_f:
	fclose(f);
	return rc;
}
Exemplo n.º 3
0
char *test_distribution()
{
    int i = 0;
    int stats[3][BUCKETS] = { {0} };
    DArray *keys = DArray_create(0, NUM_KEYS);

    mu_assert(gen_keys(keys, NUM_KEYS) == 0,
            "Failed to generate random keys.");

    fill_distribution(stats[ALGO_FNV1A], keys, Hashmap_fnv1a_hash);
    fill_distribution(stats[ALGO_ADLER32], keys, Hashmap_adler32_hash);
    fill_distribution(stats[ALGO_DJB], keys, Hashmap_djb_hash);

    fprintf(stderr, "FNV\tA32\tDJB\n");

    for (i = 0; i < BUCKETS; i++) {
        fprintf(stderr, "%d\t%d\t%d\n",
                stats[ALGO_FNV1A][i],
                stats[ALGO_ADLER32][i], stats[ALGO_DJB][i]);
    }

    destroy_keys(keys);

    return NULL;
}
Exemplo n.º 4
0
void CRainDes::cipher(BYTE* key_ptr, BYTE* input, BYTE* output, BYTE mode)
{
	BYTE i;
	BYTE last[8], temp[4];

	memcpy (m_key, key_ptr, 8 );
	gen_keys();

	/* perform 'Initial Permutation' */
	do_ip(input, last, m_ip_table);
	 
	/* Generate 'Preoutput' */
	for (i=0; i<16; i++) 
	{		
		if (mode)
			function_rk(&last[4], temp, m_perm_keys[15-i]);
		else 
			function_rk(&last[4], temp, m_perm_keys[i]);

		/* XOR result with lower bits of last iteration */
		temp[0] ^= last[0]; 
		temp[1] ^= last[1];
		temp[2] ^= last[2];
		temp[3] ^= last[3];

		/* copy high 32 bits of previous iteration to low bits */
		last[0] = last[4];
		last[1] = last[5];
		last[2] = last[6];
		last[3] = last[7];

		/* copy result of XOR to high bits */
		last[4] = temp[0];
		last[5] = temp[1];
		last[6] = temp[2];
		last[7] = temp[3];
	}

	i       = last[4];
	last[4] = last[0];
	last[0] = i;
	i       = last[5];
	last[5] = last[1];
	last[1] = i;
	i       = last[6];
	last[6] = last[2];
	last[2] = i;
	i       = last[7];
	last[7] = last[3];
	last[3] = i;

	/* perform 'Inverse Initial Permutation' */
	do_ip(last, output, m_inv_ip_table);
}
Exemplo n.º 5
0
void main()
{
int pt[8]={0},i;
printf("Enter plain text binary bits:");
for(i=0;i<8;i++)
scanf("%d",&pt[i]);
gen_keys(); // Generating Keys key1 & key2
En_De(pt,0);
printf("\nCipher Text :");
for(i=0;i<8;i++)
printf("%d",ct[i]);
//Decrypting - - -
En_De(ct,1);
printf("\nPlain Text (After Decrypting):");
for(i=0;i<8;i++)
printf("%d",ct[i]);
}
Exemplo n.º 6
0
int main()
{
   EVP_PKEY      * priv_key   = 0;
   EVP_PKEY      * pub_key    = 0;
   char          * data       = "Hello World";
   int             data_len   = strlen(data);
   unsigned char * sig        = 0;
   int             sig_len    = 0;

   gen_keys( "priv.pem", "pub.pem" );

   priv_key   = load_private_key( "priv.pem" );
   pub_key    = load_public_key2( "pub.pem" );

   if (!priv_key || !pub_key)
   {
      printf("failed to load keys\n");
      return 1;
   }
   
   sig = sign_it( priv_key, EVP_sha1(), data, data_len, &sig_len );

   if (!sig)
   {
      printf("Failed to generate signature\n");
      return 1;
   }

   if ( verify_sig( pub_key, EVP_sha1(), data, data_len, sig, sig_len ) )
      printf("Signature matches!!\n");
   else
      printf("*** SIG FAILED ***\n");

   free(sig);
   EVP_PKEY_free (priv_key);
   EVP_PKEY_free (pub_key);

   return 0;
}
Exemplo n.º 7
0
static void print_keypair(bool pub, bool priv)
{
	secp256k1_context *ctx;
	struct seckey seckey;
	struct compressed_pubkey pubkey;
	char sechex[hex_str_size(sizeof(seckey))];
	char pubhex[hex_str_size(sizeof(pubkey))];

	assert(pub || priv);

	ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
	gen_keys(ctx, &seckey, &pubkey);

	hex_encode(&seckey, sizeof(seckey), sechex, sizeof(sechex));
	hex_encode(&pubkey, sizeof(pubkey), pubhex, sizeof(pubhex));

	if (pub && priv) {
		printf("%s:%s\n", sechex, pubhex);
	} else {
		printf("%s\n", (priv ? sechex : pubhex));
	}
}
Exemplo n.º 8
0
static bool create_onion(const secp256k1_pubkey pubkey[],
			 char *const msg[],
			 size_t num,
			 struct onion *onion)
{
	int i;
	struct seckey seckeys[MAX_HOPS];
	struct onion_pubkey pubkeys[MAX_HOPS];
	struct enckey enckeys[MAX_HOPS];
	struct hmackey hmackeys[MAX_HOPS];
	struct iv ivs[MAX_HOPS];
	struct iv pad_ivs[MAX_HOPS];
	HMAC_CTX padding_hmac[MAX_HOPS];
	struct hop padding[MAX_HOPS];
	size_t junk_hops;
	secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
	bool ok = false;

	if (num > MAX_HOPS)
		goto fail;

	/* FIXME: I think it would be safe to reuse a single disposable key
	 * here? */
	/* First generate all the keys. */
	for (i = 0; i < num; i++) {
		unsigned char secret[32];

		gen_keys(ctx, &seckeys[i], &pubkeys[i]);


		/* Make shared secret. */
		if (!secp256k1_ecdh(ctx, secret, &pubkey[i], seckeys[i].u.u8))
			goto fail;

		hmackeys[i] = hmackey_from_secret(memcheck(secret, 32));
		enckeys[i] = enckey_from_secret(secret);
		ivs_from_secret(secret, &ivs[i], &pad_ivs[i]);
	}

	/*
	 * Building the onion is a little tricky.
	 *
	 * First, there is the padding.  That's generated by previous nodes,
	 * and "decrypted" by the others.  So we have to generate that
	 * forwards.
	 */
	for (i = 0; i < num; i++) {
		if (i > 0) {
			/* Previous node decrypts padding before passing on. */
			aes_decrypt(padding, padding, sizeof(struct hop)*(i-1),
				    &enckeys[i-1], &ivs[i-1]);
			memmove(padding + 1, padding,
				sizeof(struct hop)*(i-1));
		}
		/* And generates more padding for next node. */
		add_padding(&padding[0], &enckeys[i-1], &pad_ivs[i-1]);
		HMAC_CTX_init(&padding_hmac[i]);
		HMAC_Init_ex(&padding_hmac[i],
			     hmackeys[i].k.u.u8, sizeof(hmackeys[i].k),
			     EVP_sha256(), NULL);
		HMAC_Update(&padding_hmac[i],
			    memcheck((unsigned char *)padding,
				     i * sizeof(struct hop)),
			    i * sizeof(struct hop));
	}

	/*
	 * Now the normal onion is generated backwards.
	 */

	/* Unused hops filled with random, so even recipient can't tell
	 * how many were used. */
	junk_hops = MAX_HOPS - num;
	random_bytes(onion->hop, junk_hops * sizeof(struct hop));

	for (i = num - 1; i >= 0; i--) {
		size_t other_hops, len;
		struct hop *myhop;

		other_hops = num - i - 1 + junk_hops;

		/* Our entry is at tail of onion. */
		myhop = onion->hop + other_hops;

		/* Now populate our hop. */
		myhop->pubkey = pubkeys[i];
		/* Set message. */
		assert(strlen(msg[i]) < MESSAGE_SIZE);
		memset(myhop->msg, 0, MESSAGE_SIZE);
		strcpy((char *)myhop->msg, msg[i]);

		/* Encrypt whole thing, including our message, but we
		 * aware it will be offset by the prepended padding. */
		if (!aes_encrypt_offset(i * sizeof(struct hop),
					onion, onion,
					other_hops * sizeof(struct hop)
					+ sizeof(myhop->msg),
					&enckeys[i], &ivs[i]))
			goto fail;

		/* HMAC covers entire thing except hmac itself. */
		len = (other_hops + 1)*sizeof(struct hop) - sizeof(myhop->hmac);
		HMAC_Update(&padding_hmac[i],
			    memcheck((unsigned char *)onion, len), len);
		HMAC_Final(&padding_hmac[i], myhop->hmac.u.u8, NULL);
	}

	ok = true;
fail:
	secp256k1_context_destroy(ctx);
	return ok;
}