sara matheu
/
CurvasElipticas
Operaciones de generacion de claves, D-H, firma y validacion.
main.cpp@3:74a69ff114ba, 2015-02-14 (annotated)
- Committer:
- saranieves92
- Date:
- Sat Feb 14 18:10:15 2015 +0000
- Revision:
- 3:74a69ff114ba
- Parent:
- 2:94f5ffb2a52d
- Child:
- 5:4f619b9a7bb2
Disponible funcion SHA256 y conversion a uint32 para que pueda usarlo la funcion de firma
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:4875e39abd1e | 10 | Serial pc(USBTX, USBRX); // tx, rx |
saranieves92 | 1:4875e39abd1e | 11 | |
saranieves92 | 1:4875e39abd1e | 12 | void vli_print(uint32_t *p_vli, unsigned int p_size) |
saranieves92 | 1:4875e39abd1e | 13 | { |
saranieves92 | 1:4875e39abd1e | 14 | while(p_size) |
saranieves92 | 1:4875e39abd1e | 15 | { |
saranieves92 | 1:4875e39abd1e | 16 | printf("%08X ", (unsigned)p_vli[p_size - 1]); |
saranieves92 | 1:4875e39abd1e | 17 | --p_size; |
saranieves92 | 1:4875e39abd1e | 18 | } |
saranieves92 | 1:4875e39abd1e | 19 | } |
saranieves92 | 1:4875e39abd1e | 20 | |
saranieves92 | 1:4875e39abd1e | 21 | int main() { |
saranieves92 | 3:74a69ff114ba | 22 | clock_t t_ini, t_fin; |
saranieves92 | 3:74a69ff114ba | 23 | double secs; |
saranieves92 | 3:74a69ff114ba | 24 | |
saranieves92 | 3:74a69ff114ba | 25 | |
saranieves92 | 1:4875e39abd1e | 26 | /*GENERACION DE CLAVES*/ |
saranieves92 | 2:94f5ffb2a52d | 27 | pc.printf("Hola soy Alice, voy a generar mi clave publica coordenada x \n"); |
saranieves92 | 3:74a69ff114ba | 28 | uint32_t l_private1[NUM_ECC_DIGITS];//clave privada |
saranieves92 | 3:74a69ff114ba | 29 | uint32_t aleat[NUM_ECC_DIGITS]; //aleatorio |
saranieves92 | 3:74a69ff114ba | 30 | for (int i=0; i<NUM_ECC_DIGITS;i++){ |
saranieves92 | 3:74a69ff114ba | 31 | aleat[i]=rand()%10; //entero entre 0-9 |
saranieves92 | 3:74a69ff114ba | 32 | } |
saranieves92 | 3:74a69ff114ba | 33 | |
saranieves92 | 1:4875e39abd1e | 34 | EccPoint l_public1; |
saranieves92 | 3:74a69ff114ba | 35 | t_ini = clock(); |
saranieves92 | 3:74a69ff114ba | 36 | ecc_make_key(&l_public1, l_private1, aleat); //Calcula la publica |
saranieves92 | 3:74a69ff114ba | 37 | t_fin = clock(); |
saranieves92 | 1:4875e39abd1e | 38 | pc.printf("%08X ", (unsigned)l_public1.x); //imprime coordenada x de la clave publica |
saranieves92 | 2:94f5ffb2a52d | 39 | pc.printf("Hola soy Alice, voy a generar mi clave publica coordenada y \n"); |
saranieves92 | 1:4875e39abd1e | 40 | pc.printf("%08X ", (unsigned)l_public1.y); //imprime coordenada y de la clave publica |
saranieves92 | 1:4875e39abd1e | 41 | |
saranieves92 | 1:4875e39abd1e | 42 | uint32_t l_private2[NUM_ECC_DIGITS]; //aleatoria |
saranieves92 | 1:4875e39abd1e | 43 | EccPoint l_public2; |
saranieves92 | 3:74a69ff114ba | 44 | ecc_make_key(&l_public2, l_private2, aleat); //Calcula la publica |
saranieves92 | 2:94f5ffb2a52d | 45 | pc.printf("Hola soy Bob, voy a generar mi clave publica coordenada x \n"); |
saranieves92 | 1:4875e39abd1e | 46 | pc.printf("%08X ", (unsigned)l_public2.x); //imprime coordenada x de la clave publica |
saranieves92 | 2:94f5ffb2a52d | 47 | pc.printf("Hola soy Bob, voy a generar mi clave publica coordenada y \n"); |
saranieves92 | 1:4875e39abd1e | 48 | pc.printf("%08X ", (unsigned)l_public2.y); //imprime coordenada y de la clave publica |
saranieves92 | 1:4875e39abd1e | 49 | |
saranieves92 | 1:4875e39abd1e | 50 | /*D-H*/ |
saranieves92 | 1:4875e39abd1e | 51 | uint32_t l_shared1[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 52 | uint32_t l_shared2[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 53 | uint32_t l_random1[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 54 | uint32_t l_random2[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 55 | |
saranieves92 | 1:4875e39abd1e | 56 | //comprobamos que se ha generado bien la clave compartida y que tienen la misma |
saranieves92 | 1:4875e39abd1e | 57 | if(!ecdh_shared_secret(l_shared1, &l_public1, l_private2, l_random1)) |
saranieves92 | 1:4875e39abd1e | 58 | { |
saranieves92 | 1:4875e39abd1e | 59 | pc.printf("shared_secret() failed (1)\n"); |
saranieves92 | 1:4875e39abd1e | 60 | return 1; |
saranieves92 | 1:4875e39abd1e | 61 | } |
saranieves92 | 1:4875e39abd1e | 62 | |
saranieves92 | 1:4875e39abd1e | 63 | if(!ecdh_shared_secret(l_shared2, &l_public2, l_private1, l_random2)) |
saranieves92 | 1:4875e39abd1e | 64 | { |
saranieves92 | 1:4875e39abd1e | 65 | pc.printf("shared_secret() failed (2)\n"); |
saranieves92 | 1:4875e39abd1e | 66 | return 1; |
saranieves92 | 1:4875e39abd1e | 67 | } |
saranieves92 | 1:4875e39abd1e | 68 | |
saranieves92 | 1:4875e39abd1e | 69 | if(memcmp(l_shared1, l_shared2, sizeof(l_shared1)) != 0) |
saranieves92 | 1:4875e39abd1e | 70 | { |
saranieves92 | 1:4875e39abd1e | 71 | pc.printf("Shared secrets are not identical!\n"); |
saranieves92 | 1:4875e39abd1e | 72 | pc.printf("Shared secret 1 = "); |
saranieves92 | 1:4875e39abd1e | 73 | vli_print(l_shared1, NUM_ECC_DIGITS); |
saranieves92 | 1:4875e39abd1e | 74 | pc.printf("\n"); |
saranieves92 | 1:4875e39abd1e | 75 | pc.printf("Shared secret 2 = "); |
saranieves92 | 1:4875e39abd1e | 76 | vli_print(l_shared2, NUM_ECC_DIGITS); |
saranieves92 | 1:4875e39abd1e | 77 | pc.printf("\n"); |
saranieves92 | 1:4875e39abd1e | 78 | pc.printf("Private key 1 = "); |
saranieves92 | 1:4875e39abd1e | 79 | vli_print(l_private1, NUM_ECC_DIGITS); |
saranieves92 | 1:4875e39abd1e | 80 | pc.printf("\n"); |
saranieves92 | 1:4875e39abd1e | 81 | pc.printf("Private key 2 = "); |
saranieves92 | 1:4875e39abd1e | 82 | vli_print(l_private2, NUM_ECC_DIGITS); |
saranieves92 | 1:4875e39abd1e | 83 | pc.printf("\n"); |
saranieves92 | 1:4875e39abd1e | 84 | }else { |
saranieves92 | 1:4875e39abd1e | 85 | pc.printf("Shared secrets are identical!\n"); |
saranieves92 | 1:4875e39abd1e | 86 | } |
saranieves92 | 1:4875e39abd1e | 87 | |
saranieves92 | 1:4875e39abd1e | 88 | /*FIRMA y VERIFICACION*/ |
saranieves92 | 1:4875e39abd1e | 89 | |
saranieves92 | 3:74a69ff114ba | 90 | string mensaje = "Hola"; |
saranieves92 | 3:74a69ff114ba | 91 | |
saranieves92 | 3:74a69ff114ba | 92 | //SHA-256 y paso a uint32// |
saranieves92 | 3:74a69ff114ba | 93 | string output1 = sha256(mensaje); |
saranieves92 | 3:74a69ff114ba | 94 | uint32_t hash[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 95 | |
saranieves92 | 3:74a69ff114ba | 96 | int j=0; |
saranieves92 | 3:74a69ff114ba | 97 | while (j<NUM_ECC_DIGITS){ |
saranieves92 | 3:74a69ff114ba | 98 | string cortada=output1.substr(8*j, 7); |
saranieves92 | 3:74a69ff114ba | 99 | //pc.printf("corte: %s ", cortada); |
saranieves92 | 3:74a69ff114ba | 100 | const char* caracteres=cortada.c_str(); |
saranieves92 | 3:74a69ff114ba | 101 | uint32_t x = strtoul(caracteres, NULL, 16); |
saranieves92 | 3:74a69ff114ba | 102 | //pc.printf("hash: %08X ", (unsigned)x); |
saranieves92 | 3:74a69ff114ba | 103 | hash[j]=x; |
saranieves92 | 3:74a69ff114ba | 104 | j++; |
saranieves92 | 3:74a69ff114ba | 105 | } |
saranieves92 | 3:74a69ff114ba | 106 | ///--------/// |
saranieves92 | 3:74a69ff114ba | 107 | uint32_t aleatorio[NUM_ECC_DIGITS]; |
saranieves92 | 3:74a69ff114ba | 108 | for (int i=0; i<NUM_ECC_DIGITS;i++){ |
saranieves92 | 3:74a69ff114ba | 109 | aleatorio[i]=rand()%10; //entero entre 0-9 |
saranieves92 | 3:74a69ff114ba | 110 | } |
saranieves92 | 1:4875e39abd1e | 111 | uint32_t r[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 112 | uint32_t s[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 113 | |
saranieves92 | 3:74a69ff114ba | 114 | if(!ecdsa_sign(r, s, l_private1, aleatorio, hash)) |
saranieves92 | 1:4875e39abd1e | 115 | { |
saranieves92 | 1:4875e39abd1e | 116 | pc.printf("ecdsa_sign() failed\n"); |
saranieves92 | 1:4875e39abd1e | 117 | } |
saranieves92 | 1:4875e39abd1e | 118 | |
saranieves92 | 1:4875e39abd1e | 119 | if(!ecc_valid_public_key(&l_public1)) |
saranieves92 | 1:4875e39abd1e | 120 | { |
saranieves92 | 1:4875e39abd1e | 121 | pc.printf("Not a valid public key!\n"); |
saranieves92 | 1:4875e39abd1e | 122 | } |
saranieves92 | 1:4875e39abd1e | 123 | |
saranieves92 | 3:74a69ff114ba | 124 | if(!ecdsa_verify(&l_public1, hash, r, s)) |
saranieves92 | 1:4875e39abd1e | 125 | { |
saranieves92 | 1:4875e39abd1e | 126 | pc.printf("ecdsa_verify() failed\n"); |
saranieves92 | 1:4875e39abd1e | 127 | }else{ |
saranieves92 | 1:4875e39abd1e | 128 | pc.printf("ecdsa_verify() correcto \n"); |
saranieves92 | 1:4875e39abd1e | 129 | } |
saranieves92 | 3:74a69ff114ba | 130 | |
saranieves92 | 3:74a69ff114ba | 131 | |
saranieves92 | 3:74a69ff114ba | 132 | secs = (double)(t_fin - t_ini) / CLOCKS_PER_SEC; |
saranieves92 | 3:74a69ff114ba | 133 | pc.printf("%.16g milisegundos\n", secs * 1000.0); |
saranieves92 | 3:74a69ff114ba | 134 | |
saranieves92 | 1:4875e39abd1e | 135 | |
saranieves92 | 1:4875e39abd1e | 136 | } |