sara matheu
/
CurvasElipticas
Operaciones de generacion de claves, D-H, firma y validacion.
main.cpp@1:4875e39abd1e, 2015-02-05 (annotated)
- Committer:
- saranieves92
- Date:
- Thu Feb 05 23:04:03 2015 +0000
- Revision:
- 1:4875e39abd1e
- Child:
- 2:94f5ffb2a52d
Prueba de generacion de claves, D-H, firma y validacion pasada correctamente
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
saranieves92 | 1:4875e39abd1e | 1 | #include <mbed.h> |
saranieves92 | 1:4875e39abd1e | 2 | #include "ecc.h" |
saranieves92 | 1:4875e39abd1e | 3 | Serial pc(USBTX, USBRX); // tx, rx |
saranieves92 | 1:4875e39abd1e | 4 | |
saranieves92 | 1:4875e39abd1e | 5 | void vli_print(uint32_t *p_vli, unsigned int p_size) |
saranieves92 | 1:4875e39abd1e | 6 | { |
saranieves92 | 1:4875e39abd1e | 7 | while(p_size) |
saranieves92 | 1:4875e39abd1e | 8 | { |
saranieves92 | 1:4875e39abd1e | 9 | printf("%08X ", (unsigned)p_vli[p_size - 1]); |
saranieves92 | 1:4875e39abd1e | 10 | --p_size; |
saranieves92 | 1:4875e39abd1e | 11 | } |
saranieves92 | 1:4875e39abd1e | 12 | } |
saranieves92 | 1:4875e39abd1e | 13 | |
saranieves92 | 1:4875e39abd1e | 14 | int main() { |
saranieves92 | 1:4875e39abd1e | 15 | |
saranieves92 | 1:4875e39abd1e | 16 | /*GENERACION DE CLAVES*/ |
saranieves92 | 1:4875e39abd1e | 17 | pc.printf("Hola Mundo1\n"); |
saranieves92 | 1:4875e39abd1e | 18 | uint32_t l_private1[NUM_ECC_DIGITS]; //aleatoria |
saranieves92 | 1:4875e39abd1e | 19 | EccPoint l_public1; |
saranieves92 | 1:4875e39abd1e | 20 | ecc_make_key(&l_public1, l_private1, l_private1); //Calcula la publica |
saranieves92 | 1:4875e39abd1e | 21 | pc.printf("%08X ", (unsigned)l_public1.x); //imprime coordenada x de la clave publica |
saranieves92 | 1:4875e39abd1e | 22 | pc.printf("Adios Mundo1\n"); |
saranieves92 | 1:4875e39abd1e | 23 | pc.printf("%08X ", (unsigned)l_public1.y); //imprime coordenada y de la clave publica |
saranieves92 | 1:4875e39abd1e | 24 | |
saranieves92 | 1:4875e39abd1e | 25 | uint32_t l_private2[NUM_ECC_DIGITS]; //aleatoria |
saranieves92 | 1:4875e39abd1e | 26 | EccPoint l_public2; |
saranieves92 | 1:4875e39abd1e | 27 | ecc_make_key(&l_public2, l_private2, l_private2); //Calcula la publica |
saranieves92 | 1:4875e39abd1e | 28 | pc.printf("%08X ", (unsigned)l_public2.x); //imprime coordenada x de la clave publica |
saranieves92 | 1:4875e39abd1e | 29 | pc.printf("Adios Mundo2\n"); |
saranieves92 | 1:4875e39abd1e | 30 | pc.printf("%08X ", (unsigned)l_public2.y); //imprime coordenada y de la clave publica |
saranieves92 | 1:4875e39abd1e | 31 | |
saranieves92 | 1:4875e39abd1e | 32 | /*D-H*/ |
saranieves92 | 1:4875e39abd1e | 33 | uint32_t l_shared1[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 34 | uint32_t l_shared2[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 35 | uint32_t l_random1[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 36 | uint32_t l_random2[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 37 | |
saranieves92 | 1:4875e39abd1e | 38 | //comprobamos que se ha generado bien la clave compartida y que tienen la misma |
saranieves92 | 1:4875e39abd1e | 39 | if(!ecdh_shared_secret(l_shared1, &l_public1, l_private2, l_random1)) |
saranieves92 | 1:4875e39abd1e | 40 | { |
saranieves92 | 1:4875e39abd1e | 41 | pc.printf("shared_secret() failed (1)\n"); |
saranieves92 | 1:4875e39abd1e | 42 | return 1; |
saranieves92 | 1:4875e39abd1e | 43 | } |
saranieves92 | 1:4875e39abd1e | 44 | |
saranieves92 | 1:4875e39abd1e | 45 | if(!ecdh_shared_secret(l_shared2, &l_public2, l_private1, l_random2)) |
saranieves92 | 1:4875e39abd1e | 46 | { |
saranieves92 | 1:4875e39abd1e | 47 | pc.printf("shared_secret() failed (2)\n"); |
saranieves92 | 1:4875e39abd1e | 48 | return 1; |
saranieves92 | 1:4875e39abd1e | 49 | } |
saranieves92 | 1:4875e39abd1e | 50 | |
saranieves92 | 1:4875e39abd1e | 51 | if(memcmp(l_shared1, l_shared2, sizeof(l_shared1)) != 0) |
saranieves92 | 1:4875e39abd1e | 52 | { |
saranieves92 | 1:4875e39abd1e | 53 | pc.printf("Shared secrets are not identical!\n"); |
saranieves92 | 1:4875e39abd1e | 54 | pc.printf("Shared secret 1 = "); |
saranieves92 | 1:4875e39abd1e | 55 | vli_print(l_shared1, NUM_ECC_DIGITS); |
saranieves92 | 1:4875e39abd1e | 56 | pc.printf("\n"); |
saranieves92 | 1:4875e39abd1e | 57 | pc.printf("Shared secret 2 = "); |
saranieves92 | 1:4875e39abd1e | 58 | vli_print(l_shared2, NUM_ECC_DIGITS); |
saranieves92 | 1:4875e39abd1e | 59 | pc.printf("\n"); |
saranieves92 | 1:4875e39abd1e | 60 | pc.printf("Private key 1 = "); |
saranieves92 | 1:4875e39abd1e | 61 | vli_print(l_private1, NUM_ECC_DIGITS); |
saranieves92 | 1:4875e39abd1e | 62 | pc.printf("\n"); |
saranieves92 | 1:4875e39abd1e | 63 | pc.printf("Private key 2 = "); |
saranieves92 | 1:4875e39abd1e | 64 | vli_print(l_private2, NUM_ECC_DIGITS); |
saranieves92 | 1:4875e39abd1e | 65 | pc.printf("\n"); |
saranieves92 | 1:4875e39abd1e | 66 | }else { |
saranieves92 | 1:4875e39abd1e | 67 | pc.printf("Shared secrets are identical!\n"); |
saranieves92 | 1:4875e39abd1e | 68 | } |
saranieves92 | 1:4875e39abd1e | 69 | |
saranieves92 | 1:4875e39abd1e | 70 | /*FIRMA y VERIFICACION*/ |
saranieves92 | 1:4875e39abd1e | 71 | |
saranieves92 | 1:4875e39abd1e | 72 | uint32_t l_hash[NUM_ECC_DIGITS]; //Se supone que es el hash del mensaje que queremos firmar |
saranieves92 | 1:4875e39abd1e | 73 | uint32_t l_random[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 74 | |
saranieves92 | 1:4875e39abd1e | 75 | uint32_t r[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 76 | uint32_t s[NUM_ECC_DIGITS]; |
saranieves92 | 1:4875e39abd1e | 77 | |
saranieves92 | 1:4875e39abd1e | 78 | if(!ecdsa_sign(r, s, l_private1, l_random, l_hash)) |
saranieves92 | 1:4875e39abd1e | 79 | { |
saranieves92 | 1:4875e39abd1e | 80 | pc.printf("ecdsa_sign() failed\n"); |
saranieves92 | 1:4875e39abd1e | 81 | } |
saranieves92 | 1:4875e39abd1e | 82 | |
saranieves92 | 1:4875e39abd1e | 83 | if(!ecc_valid_public_key(&l_public1)) |
saranieves92 | 1:4875e39abd1e | 84 | { |
saranieves92 | 1:4875e39abd1e | 85 | pc.printf("Not a valid public key!\n"); |
saranieves92 | 1:4875e39abd1e | 86 | } |
saranieves92 | 1:4875e39abd1e | 87 | |
saranieves92 | 1:4875e39abd1e | 88 | if(!ecdsa_verify(&l_public1, l_hash, r, s)) |
saranieves92 | 1:4875e39abd1e | 89 | { |
saranieves92 | 1:4875e39abd1e | 90 | pc.printf("ecdsa_verify() failed\n"); |
saranieves92 | 1:4875e39abd1e | 91 | }else{ |
saranieves92 | 1:4875e39abd1e | 92 | pc.printf("ecdsa_verify() correcto \n"); |
saranieves92 | 1:4875e39abd1e | 93 | } |
saranieves92 | 1:4875e39abd1e | 94 | |
saranieves92 | 1:4875e39abd1e | 95 | } |