예제 #1
0
파일: _pyecc.c 프로젝트: stef/PyECC
passed a buffer, a ECC_KeyPair PyCObject and a \
ECC_State PyCObject \
\n\
";
static PyObject *py_decrypt(PyObject *self, PyObject *args, PyObject *kwargs)
{
    PyObject *temp_state, *temp_keypair;
    ECC_State state;
    ECC_KeyPair keypair;
    ECC_Data encrypted;
    char *data;
    int datalen;

    if (!PyArg_ParseTuple(args, "s#OO", &data, &datalen, &temp_keypair,
            &temp_state)) {
        return NULL;
    }

    state = (ECC_State)(PyCObject_AsVoidPtr(temp_state));
    keypair = (ECC_KeyPair)(PyCObject_AsVoidPtr(temp_keypair));
    
    encrypted = ecc_new_data();
    encrypted->data = data;
    encrypted->datalen = datalen;

    ECC_Data result = ecc_decrypt(encrypted, keypair, state);

    if ( (result == NULL) || (result->data == NULL) )
        Py_RETURN_NONE;

    return PyString_FromStringAndSize((char *)(result->data), result->datalen);
}
예제 #2
0
파일: bob.c 프로젝트: codeguru1/ecc
/*---------------------------------------------------------------------------*/
static void
abc_recv ( struct abc_conn *c )
{
	mst_t *rcv_msg;
	uint8_t decrypted[MSG_LEN];
	int j, i;
        rcv_msg = packetbuf_dataptr();
	printf("\nmessage received\n");
	for(i = 0; i < rcv_msg->len; i++)
	{
		printf ( "%u:", rcv_msg->dat[i]);
	}
	printf("\n");
        ENERGEST_OFF ( ENERGEST_TYPE_CPU );
        ENERGEST_OFF ( ENERGEST_TYPE_LPM );
        ENERGEST_OFF ( ENERGEST_TYPE_TRANSMIT );
        ENERGEST_OFF ( ENERGEST_TYPE_LISTEN );
        ENERGEST_ON ( ENERGEST_TYPE_CPU );
        ENERGEST_ON ( ENERGEST_TYPE_LPM );
        ENERGEST_ON ( ENERGEST_TYPE_TRANSMIT );
        ENERGEST_ON ( ENERGEST_TYPE_LISTEN );

        last.lpm = energest_type_time ( ENERGEST_TYPE_LPM );
        last.transmit = energest_type_time ( ENERGEST_TYPE_TRANSMIT );
        last.listen = energest_type_time ( ENERGEST_TYPE_LISTEN );
        last.cpu = energest_type_time ( ENERGEST_TYPE_CPU );
	
	j = ecc_decrypt(decrypted, MSG_LEN, rcv_msg->dat, rcv_msg->len, prKey_alice);
	
        diff.cpu = energest_type_time ( ENERGEST_TYPE_CPU ) - last.cpu;
        diff.lpm = energest_type_time ( ENERGEST_TYPE_LPM ) - last.lpm;
        diff.transmit = energest_type_time ( ENERGEST_TYPE_TRANSMIT ) - last.transmit;
        diff.listen = energest_type_time ( ENERGEST_TYPE_LISTEN ) - last.listen;
        if ( j > 0 ) {
                printf ( "\nDecrypted data\n" );
                for ( i =0; i < MSG_LEN; i++ ) {
                        printf ( "%u:", decrypted[i] );
                }
                printf ( "\n" );

                printf ( "Clock ticks recorded by\nCPU = %ld \nLPM = %ld \nTRANSMITTER = %ld\nRECEIVER = %ld\n",diff.cpu, diff.lpm, diff.transmit, diff.listen );
        }
        else
	{
		printf("couldn't decrypt\n");
	}
}
예제 #3
0
int main(int argc, char *argv[])
{
	/* { fix start } */
	ltc_mp = ltm_desc;
	/* { fix end } */
    int sockfd = 0;

    struct sockaddr_in serv_addr;

    if(argc != 2)
    {
        printf("\n Usage: %s <ip of server> \n",argv[0]);
        return 1;
    }

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Error : Could not create socket \n");
        return 1;
    }

    memset(&serv_addr, '0', sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(5002);

    if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
    {
        printf("\n inet_pton error occured\n");
        return 1;
    }

    if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
       printf("\n Error : Connect Failed \n");
       return 1;
    }

    initEncrypt();
    ecc_key encryptKey;
    loadKey(&encryptKey, "bpublic.key");
    ecc_key decryptKey;
    loadKey(&decryptKey, "aprivate.key");

	/* { userString start } */
	printf("Enter message to send\n");
	unsigned char message[256];
	fgets((char*)message,256,stdin);
	/* { userString end } */

	/* { sendNonceA start } */
	int nonceA = randomNumber();
	printf("nonceA = %i\n",nonceA);
	printf("Encrypting nonceA with bpub\n");
	unsigned char nonceA_enc[2048];
	unsigned long outLength = 2048;
	ecc_encrypt((unsigned char*)&nonceA, sizeof(int), nonceA_enc, &outLength,&encryptKey);
	printf("Sending nonceA\n");
	write(sockfd, nonceA_enc, outLength);
	/* { sendNonceA end } */

	/* { resiveSessionKey start } */
	unsigned char recvBuff[1024];
	unsigned long msgLength;
	msgLength = recv(sockfd, recvBuff, sizeof(recvBuff),0);
	struct SessionKey sKey;
	unsigned long inLength = sizeof(struct SessionKey);
	ecc_decrypt(recvBuff,msgLength,(unsigned char*)&sKey,&inLength,&decryptKey);
	printf("Received sKey, nonceA = %i, key = %i\n", sKey.nonceA, sKey.key);
	/* { resiveSessionKey end } */

	/* { resendKey start } */
	my_aes_setup(sKey.key);
	sKey.nonceA ++;
	printf("Sending nonceA = %i encrypted with AES\n", sKey.nonceA);
	outLength = 2048;
	aes_encrypt((unsigned char*)&sKey.nonceA,sizeof(int),nonceA_enc, &outLength);
	write(sockfd, nonceA_enc, outLength);
	/* { resendKey end } */

	/* { sendMessage start } */
	printf("Sending message encrypted with AES\n");
	printf("%s", message);
	outLength = 2048;
	unsigned char message_enc[2048];
	aes_encrypt(message, strlen((char*)message), message_enc, &outLength);
	write(sockfd, message_enc, outLength);
	/* { sendMessage end } */
	
	return -1;

}