Operaciones de generacion de claves, D-H, firma y validacion.

Dependencies:   mbed CyaSSL

Committer:
saranieves92
Date:
Fri Feb 20 18:37:50 2015 +0000
Revision:
5:4f619b9a7bb2
Parent:
3:74a69ff114ba
intento de rsa

Who changed what in which revision?

UserRevisionLine numberNew contents of line
saranieves92 1:4875e39abd1e 1 #include <mbed.h>
saranieves92 3:74a69ff114ba 2 #include <time.h>
saranieves92 3:74a69ff114ba 3 #include <string>
saranieves92 3:74a69ff114ba 4 #include <stdlib.h>
saranieves92 3:74a69ff114ba 5 #include <cstdio>
saranieves92 3:74a69ff114ba 6 #include <cstdlib>
saranieves92 3:74a69ff114ba 7 #include <alloca.h>
saranieves92 3:74a69ff114ba 8 #include "sha256.h"
saranieves92 1:4875e39abd1e 9 #include "ecc.h"
saranieves92 5:4f619b9a7bb2 10 #include "ctc_rsa.h"
saranieves92 5:4f619b9a7bb2 11
saranieves92 1:4875e39abd1e 12 Serial pc(USBTX, USBRX); // tx, rx
saranieves92 1:4875e39abd1e 13
saranieves92 1:4875e39abd1e 14 void vli_print(uint32_t *p_vli, unsigned int p_size)
saranieves92 1:4875e39abd1e 15 {
saranieves92 1:4875e39abd1e 16 while(p_size)
saranieves92 1:4875e39abd1e 17 {
saranieves92 1:4875e39abd1e 18 printf("%08X ", (unsigned)p_vli[p_size - 1]);
saranieves92 1:4875e39abd1e 19 --p_size;
saranieves92 1:4875e39abd1e 20 }
saranieves92 1:4875e39abd1e 21 }
saranieves92 1:4875e39abd1e 22
saranieves92 5:4f619b9a7bb2 23 int rsa_test()
saranieves92 5:4f619b9a7bb2 24 {
saranieves92 5:4f619b9a7bb2 25 RsaKey genKey;
saranieves92 5:4f619b9a7bb2 26 InitRsaKey(&genKey, 0);
saranieves92 5:4f619b9a7bb2 27 for (int i=0; i<100;i++){
saranieves92 5:4f619b9a7bb2 28 pc.printf("%d ", genKey.p.dp[i]);
saranieves92 5:4f619b9a7bb2 29 }
saranieves92 5:4f619b9a7bb2 30 RsaKey* key;
saranieves92 5:4f619b9a7bb2 31 RNG* rng;
saranieves92 5:4f619b9a7bb2 32 InitRng(rng);
saranieves92 5:4f619b9a7bb2 33 MakeRsaKey(key, 512, 65537, rng);
saranieves92 5:4f619b9a7bb2 34 return 0;
saranieves92 5:4f619b9a7bb2 35 }
saranieves92 5:4f619b9a7bb2 36
saranieves92 5:4f619b9a7bb2 37
saranieves92 1:4875e39abd1e 38 int main() {
saranieves92 3:74a69ff114ba 39 clock_t t_ini, t_fin;
saranieves92 3:74a69ff114ba 40 double secs;
saranieves92 3:74a69ff114ba 41
saranieves92 3:74a69ff114ba 42
saranieves92 1:4875e39abd1e 43 /*GENERACION DE CLAVES*/
saranieves92 2:94f5ffb2a52d 44 pc.printf("Hola soy Alice, voy a generar mi clave publica coordenada x \n");
saranieves92 3:74a69ff114ba 45 uint32_t l_private1[NUM_ECC_DIGITS];//clave privada
saranieves92 3:74a69ff114ba 46 uint32_t aleat[NUM_ECC_DIGITS]; //aleatorio
saranieves92 3:74a69ff114ba 47 for (int i=0; i<NUM_ECC_DIGITS;i++){
saranieves92 3:74a69ff114ba 48 aleat[i]=rand()%10; //entero entre 0-9
saranieves92 3:74a69ff114ba 49 }
saranieves92 3:74a69ff114ba 50
saranieves92 1:4875e39abd1e 51 EccPoint l_public1;
saranieves92 3:74a69ff114ba 52 t_ini = clock();
saranieves92 3:74a69ff114ba 53 ecc_make_key(&l_public1, l_private1, aleat); //Calcula la publica
saranieves92 3:74a69ff114ba 54 t_fin = clock();
saranieves92 1:4875e39abd1e 55 pc.printf("%08X ", (unsigned)l_public1.x); //imprime coordenada x de la clave publica
saranieves92 2:94f5ffb2a52d 56 pc.printf("Hola soy Alice, voy a generar mi clave publica coordenada y \n");
saranieves92 1:4875e39abd1e 57 pc.printf("%08X ", (unsigned)l_public1.y); //imprime coordenada y de la clave publica
saranieves92 1:4875e39abd1e 58
saranieves92 1:4875e39abd1e 59 uint32_t l_private2[NUM_ECC_DIGITS]; //aleatoria
saranieves92 1:4875e39abd1e 60 EccPoint l_public2;
saranieves92 3:74a69ff114ba 61 ecc_make_key(&l_public2, l_private2, aleat); //Calcula la publica
saranieves92 2:94f5ffb2a52d 62 pc.printf("Hola soy Bob, voy a generar mi clave publica coordenada x \n");
saranieves92 1:4875e39abd1e 63 pc.printf("%08X ", (unsigned)l_public2.x); //imprime coordenada x de la clave publica
saranieves92 2:94f5ffb2a52d 64 pc.printf("Hola soy Bob, voy a generar mi clave publica coordenada y \n");
saranieves92 1:4875e39abd1e 65 pc.printf("%08X ", (unsigned)l_public2.y); //imprime coordenada y de la clave publica
saranieves92 1:4875e39abd1e 66
saranieves92 1:4875e39abd1e 67 /*D-H*/
saranieves92 1:4875e39abd1e 68 uint32_t l_shared1[NUM_ECC_DIGITS];
saranieves92 1:4875e39abd1e 69 uint32_t l_shared2[NUM_ECC_DIGITS];
saranieves92 1:4875e39abd1e 70 uint32_t l_random1[NUM_ECC_DIGITS];
saranieves92 1:4875e39abd1e 71 uint32_t l_random2[NUM_ECC_DIGITS];
saranieves92 1:4875e39abd1e 72
saranieves92 1:4875e39abd1e 73 //comprobamos que se ha generado bien la clave compartida y que tienen la misma
saranieves92 1:4875e39abd1e 74 if(!ecdh_shared_secret(l_shared1, &l_public1, l_private2, l_random1))
saranieves92 1:4875e39abd1e 75 {
saranieves92 1:4875e39abd1e 76 pc.printf("shared_secret() failed (1)\n");
saranieves92 1:4875e39abd1e 77 return 1;
saranieves92 1:4875e39abd1e 78 }
saranieves92 1:4875e39abd1e 79
saranieves92 1:4875e39abd1e 80 if(!ecdh_shared_secret(l_shared2, &l_public2, l_private1, l_random2))
saranieves92 1:4875e39abd1e 81 {
saranieves92 1:4875e39abd1e 82 pc.printf("shared_secret() failed (2)\n");
saranieves92 1:4875e39abd1e 83 return 1;
saranieves92 1:4875e39abd1e 84 }
saranieves92 1:4875e39abd1e 85
saranieves92 1:4875e39abd1e 86 if(memcmp(l_shared1, l_shared2, sizeof(l_shared1)) != 0)
saranieves92 1:4875e39abd1e 87 {
saranieves92 1:4875e39abd1e 88 pc.printf("Shared secrets are not identical!\n");
saranieves92 1:4875e39abd1e 89 pc.printf("Shared secret 1 = ");
saranieves92 1:4875e39abd1e 90 vli_print(l_shared1, NUM_ECC_DIGITS);
saranieves92 1:4875e39abd1e 91 pc.printf("\n");
saranieves92 1:4875e39abd1e 92 pc.printf("Shared secret 2 = ");
saranieves92 1:4875e39abd1e 93 vli_print(l_shared2, NUM_ECC_DIGITS);
saranieves92 1:4875e39abd1e 94 pc.printf("\n");
saranieves92 1:4875e39abd1e 95 pc.printf("Private key 1 = ");
saranieves92 1:4875e39abd1e 96 vli_print(l_private1, NUM_ECC_DIGITS);
saranieves92 1:4875e39abd1e 97 pc.printf("\n");
saranieves92 1:4875e39abd1e 98 pc.printf("Private key 2 = ");
saranieves92 1:4875e39abd1e 99 vli_print(l_private2, NUM_ECC_DIGITS);
saranieves92 1:4875e39abd1e 100 pc.printf("\n");
saranieves92 1:4875e39abd1e 101 }else {
saranieves92 1:4875e39abd1e 102 pc.printf("Shared secrets are identical!\n");
saranieves92 1:4875e39abd1e 103 }
saranieves92 1:4875e39abd1e 104
saranieves92 1:4875e39abd1e 105 /*FIRMA y VERIFICACION*/
saranieves92 1:4875e39abd1e 106
saranieves92 3:74a69ff114ba 107 string mensaje = "Hola";
saranieves92 3:74a69ff114ba 108
saranieves92 3:74a69ff114ba 109 //SHA-256 y paso a uint32//
saranieves92 3:74a69ff114ba 110 string output1 = sha256(mensaje);
saranieves92 3:74a69ff114ba 111 uint32_t hash[NUM_ECC_DIGITS];
saranieves92 1:4875e39abd1e 112
saranieves92 3:74a69ff114ba 113 int j=0;
saranieves92 3:74a69ff114ba 114 while (j<NUM_ECC_DIGITS){
saranieves92 3:74a69ff114ba 115 string cortada=output1.substr(8*j, 7);
saranieves92 3:74a69ff114ba 116 //pc.printf("corte: %s ", cortada);
saranieves92 3:74a69ff114ba 117 const char* caracteres=cortada.c_str();
saranieves92 3:74a69ff114ba 118 uint32_t x = strtoul(caracteres, NULL, 16);
saranieves92 3:74a69ff114ba 119 //pc.printf("hash: %08X ", (unsigned)x);
saranieves92 3:74a69ff114ba 120 hash[j]=x;
saranieves92 3:74a69ff114ba 121 j++;
saranieves92 3:74a69ff114ba 122 }
saranieves92 3:74a69ff114ba 123 ///--------///
saranieves92 3:74a69ff114ba 124 uint32_t aleatorio[NUM_ECC_DIGITS];
saranieves92 3:74a69ff114ba 125 for (int i=0; i<NUM_ECC_DIGITS;i++){
saranieves92 3:74a69ff114ba 126 aleatorio[i]=rand()%10; //entero entre 0-9
saranieves92 3:74a69ff114ba 127 }
saranieves92 1:4875e39abd1e 128 uint32_t r[NUM_ECC_DIGITS];
saranieves92 1:4875e39abd1e 129 uint32_t s[NUM_ECC_DIGITS];
saranieves92 1:4875e39abd1e 130
saranieves92 3:74a69ff114ba 131 if(!ecdsa_sign(r, s, l_private1, aleatorio, hash))
saranieves92 1:4875e39abd1e 132 {
saranieves92 1:4875e39abd1e 133 pc.printf("ecdsa_sign() failed\n");
saranieves92 1:4875e39abd1e 134 }
saranieves92 1:4875e39abd1e 135
saranieves92 1:4875e39abd1e 136 if(!ecc_valid_public_key(&l_public1))
saranieves92 1:4875e39abd1e 137 {
saranieves92 1:4875e39abd1e 138 pc.printf("Not a valid public key!\n");
saranieves92 1:4875e39abd1e 139 }
saranieves92 1:4875e39abd1e 140
saranieves92 3:74a69ff114ba 141 if(!ecdsa_verify(&l_public1, hash, r, s))
saranieves92 1:4875e39abd1e 142 {
saranieves92 1:4875e39abd1e 143 pc.printf("ecdsa_verify() failed\n");
saranieves92 1:4875e39abd1e 144 }else{
saranieves92 1:4875e39abd1e 145 pc.printf("ecdsa_verify() correcto \n");
saranieves92 1:4875e39abd1e 146 }
saranieves92 3:74a69ff114ba 147
saranieves92 3:74a69ff114ba 148
saranieves92 3:74a69ff114ba 149 secs = (double)(t_fin - t_ini) / CLOCKS_PER_SEC;
saranieves92 3:74a69ff114ba 150 pc.printf("%.16g milisegundos\n", secs * 1000.0);
saranieves92 3:74a69ff114ba 151
saranieves92 5:4f619b9a7bb2 152 rsa_test();
saranieves92 1:4875e39abd1e 153
saranieves92 1:4875e39abd1e 154 }
saranieves92 5:4f619b9a7bb2 155