Ejemplo n.º 1
0
void knock_server (pkc_state * s)
{
    register uint16_t it;                               /* Iterator */
    int sockfd, n;                                      /* Server Socket */
    struct sockaddr_in srv_addr;                        /* Server sockaddr struct */
    uint32_t srv_len = sizeof (struct sockaddr_in);     /* Srv_addr length */
    char buffer [LINE_LENGTH];                          /* Packet Data */
    
#ifdef RSA_ENCRYPTION                                   /* Data for RSA Encryption */
    struct RsaPubKey pkc_pubkey;                        /* RSA Public Key Struct */
    RSA_Data packet_encrypt;                            /* Encrypted Data */
    pkc_pubkey.e = RSA_PUBKEY_E;                        /* RSA Public Key E component*/
    pkc_pubkey.n = RSA_PUBKEY_N;                        /* RSA Public Key N component*/
#endif
    
    if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        fatal_error (s, "Opening server socket.");
    }
    
    memset ((char *)&srv_addr, '\0', sizeof (srv_addr));
    srv_addr.sin_family = AF_INET;
    
    if (inet_aton (s->srv_address, &srv_addr.sin_addr) == 0) /* Assign IP Address */
    {
        fatal_error (s, "Invalid server address.");
    }
    
    /* Send Port Knocks to Server */
    for (it = 0; it < SEQUENCE_SIZE; it++)
    {
        memset (buffer, '\0', LINE_LENGTH * sizeof (char));
        srv_addr.sin_port = htons (s->sequence[it]); /* Assign Port from Sequence*/
    
#ifdef RSA_ENCRYPTION
        rsa_encipher ((RSA_Data)s->sequence[it], &packet_encrypt, pkc_pubkey);
        sprintf (buffer, "%lu", packet_encrypt);
#endif /* RSA_ENCRYPTION */

#ifdef DEBUG
        fprintf (stderr, "Sending Knock #: %i, Port: %i\n", it, s->sequence[it]);
#endif
    
        if ((n = sendto (sockfd, buffer, strlen (buffer), 0, 
                         (const struct sockaddr *)&srv_addr, srv_len)) < 0)
        {
            fatal_error (s, "Sending knock to server.");
        }
        sleep (1); /* Delay for avoiding packet loss */
    }
    close (sockfd);
}
Ejemplo n.º 2
0
static void test_rsa() {
	Huge rsatmp;
	Huge rsaptx;
	Huge rsactx;

	int i = 0;

	RsaPubKey rsapubkey;
	RsaPriKey rsaprikey;

	fprintf(stdout, "Enciphering with RSA\n");

	rsapubkey.e = 17;
	rsapubkey.n = 209;
	rsaprikey.d = 53;
	rsaprikey.n = 209;
	
	fprintf(stdout, "d=%lu, e=%lu, n=%lu\n",
			rsaprikey.d, rsapubkey.e, rsapubkey.n);

	for (i = 0; i < 128; i++) {
		rsatmp = i;
		rsa_encipher(rsatmp, &rsactx, rsapubkey);
		rsa_decipher(rsactx, &rsaptx, rsaprikey);

		if (rsatmp == rsaptx) {
			fprintf(stdout, "rsatmp=%5lu, rsactx=%5lu, rsaptx=%5lu (OK)\n",
					rsatmp, rsactx, rsaptx);
		} else {
			fprintf(stdout, "rsatmp=%5lu, rsactx=%5lu, rsaptx=%5lu (ERROR)\n",
					rsatmp, rsactx, rsaptx);
		}
	}

	return;
}
Ejemplo n.º 3
0
int main(int argc, char **argv) {

unsigned char      destmp[8],
                   desptx[8],
                   desctx[8],
                   deskey[8];

Huge               rsatmp,
                   rsaptx,
                   rsactx;

RsaPubKey          rsapubkey;
RsaPriKey          rsaprikey;

int                i;

/*****************************************************************************
*                                                                            *
*  Encipher some data using DES.                                             *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Enciphering with DES\n");

destmp[0] = 0xa9;
destmp[1] = 0x10;
destmp[2] = 0x11;
destmp[3] = 0x38;
destmp[4] = 0x93;
destmp[5] = 0xca;
destmp[6] = 0xb4;
destmp[7] = 0xa1;

deskey[0] = 0x01;
deskey[1] = 0x1f;
deskey[2] = 0x01;
deskey[3] = 0x1f;
deskey[4] = 0x01;
deskey[5] = 0x0e;
deskey[6] = 0x01;
deskey[7] = 0x0e;

fprintf(stdout, "Before enciphering\n");

fprintf(stdout, "destmp: %02x %02x %02x %02x %02x %02x %02x %02x\n",
   destmp[0], destmp[1], destmp[2], destmp[3], destmp[4], destmp[5],
   destmp[6], destmp[7]);

fprintf(stdout, "deskey: %02x %02x %02x %02x %02x %02x %02x %02x\n",
   deskey[0], deskey[1], deskey[2], deskey[3], deskey[4], deskey[5],
   deskey[6], deskey[7]);

des_encipher(destmp, desctx, deskey);

fprintf(stdout, "After enciphering\n");

fprintf(stdout, "destmp: %02x %02x %02x %02x %02x %02x %02x %02x\n",
   destmp[0], destmp[1], destmp[2], destmp[3], destmp[4], destmp[5],
   destmp[6], destmp[7]);

fprintf(stdout, "desctx: %02x %02x %02x %02x %02x %02x %02x %02x\n",
   desctx[0], desctx[1], desctx[2], desctx[3], desctx[4], desctx[5],
   desctx[6], desctx[7]);

fprintf(stdout, "Before deciphering\n");

fprintf(stdout, "desctx: %02x %02x %02x %02x %02x %02x %02x %02x\n",
   desctx[0], desctx[1], desctx[2], desctx[3], desctx[4], desctx[5],
   desctx[6], desctx[7]);

fprintf(stdout, "deskey: %02x %02x %02x %02x %02x %02x %02x %02x\n",
   deskey[0], deskey[1], deskey[2], deskey[3], deskey[4], deskey[5],
   deskey[6], deskey[7]);

des_decipher(desctx, desptx, deskey);

fprintf(stdout, "After deciphering\n");

fprintf(stdout, "desctx: %02x %02x %02x %02x %02x %02x %02x %02x\n",
   desctx[0], desctx[1], desctx[2], desctx[3], desctx[4], desctx[5],
   desctx[6], desctx[7]);

fprintf(stdout, "desptx: %02x %02x %02x %02x %02x %02x %02x %02x\n",
   desptx[0], desptx[1], desptx[2], desptx[3], desptx[4], desptx[5],
   desptx[6], desptx[7]);

/*****************************************************************************
*                                                                            *
*  Encipher some data using RSA.                                             *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Enciphering with RSA\n");

rsapubkey.e = 17;
rsapubkey.n = 209;
rsaprikey.d = 53;
rsaprikey.n = 209;

fprintf(stdout, "d=%d, e=%d, n=%d\n", rsaprikey.d, rsapubkey.e, rsapubkey.n);

for (i = 0; i < 128; i++) {

   rsatmp = i;
   rsa_encipher(rsatmp, &rsactx, rsapubkey);
   rsa_decipher(rsactx, &rsaptx, rsaprikey);

   if (rsatmp == rsaptx)
      fprintf(stdout, "rsatmp=%5d, rsactx=%5d, rsaptx=%5d (OK)\n", rsatmp,
         rsactx, rsaptx);
   else
      fprintf(stdout, "rsatmp=%5d, rsactx=%5d, rsaptx=%5d (ERROR)\n", rsatmp,
         rsactx, rsaptx);

}

fprintf(stdout,"Crypto Demo finished");
return 0;

}