I met some problems in testing ECDH , the project is the example in E:\Source\NRF52832\nRF5_SDK_11.0.0_89a8197\examples\ble_peripheral\ble_app_uart\pca10040\s132\arm5_no_packs.
1.the same private key,the different functions I called created the different public keys. First,I generated a pair of key by the function of "ecc_p256_keypair_gen", then, I used the private key created by "ecc_p256_keypair_gen" to create a public key by calling the function of "ecc_p256_public_key_compute", however the result is not the same as the public key I got bufore. The attached data shows the details.
2.failed in ECDH . Now, I have 2 pairs of keys generated by "ecc_p256_keypair_gen", if ECDH works well, sKA and pKB should get the same secret as sKB and pKA does. But the result makes me confused: I got two different secret data,why?
The following data shows the result I got.
sKA=63 2B 88 D9 2C B8 D7 71 30 1E E4 1D 59 CE E1 2D
pKA=FA 55 67 36 D7 EE D6 D0 F8 DA 10 D8 DC C3 48 11 3D DC E1 77 3C D7 B6 29 ED B9 97 87 9E F7 BC D8
uECC_compute_public_key
uECC_compute_public_key complete: 1
pK1=21 71 8C EA 94 B9 B1 27 DC C0 A9 66 B6 B0 F3 C1 DA 71 71 EB DC 10 DF 91 7F 49 00 B4 46 36 4C 6E
sKB=6A A8 F0 FA 65 7B 50 E9 3C 57 16 00 22 51 03 38
pKB=CB B4 8C 3A C7 CB 0D 0A D8 43 01 C6 09 2B 1F 96 C6 05 49 7A B8 31 E8 43 7A 66 0F FA D5 57 2D 19
uECC_shared_secret
uECC_shared_secret complete: 1
SecretA=9A 14 92 C3 61 84 94 E5 8A 4F F1 8A F5 D3 3E F3 0A 93 04 1C 8F C3 9C 8B 32 34 4F 80 8C 87 70 FE
uECC_shared_secret
uECC_shared_secret complete: 1
secretB=FD 4B 38 A4 8A 06 8C 59 E4 73 6A 15 0A F4 3C A9 3E 80 61 AB EF 8B 81 19 62 90 61 32 0A 83 DA C8
My Codes:
static uint8_t privateKeyA[16],publicKeyA[32],publicKey[32];
static uint8_t privateKeyB[16],publicKeyB[32];
static uint8_t secretA[32],secretB[32];
printf("\r\n");
ecc_init();
while (ecc_p256_keypair_gen(privateKeyA,publicKeyA)!=NRF_SUCCESS);
printf("sKA=");
printarray(privateKeyA,16);
printf("pKA=");
printarray(publicKeyA,32);
ecc_p256_public_key_compute(privateKeyA,publicKey);
printf("pK1=");
printarray(publicKey,32);
while (ecc_p256_keypair_gen(privateKeyB,publicKeyB)!=NRF_SUCCESS);
printf("sKB=");
printarray(privateKeyB,16);
printf("pKB=");
printarray(publicKeyB,32);
ecc_p256_shared_secret_compute(privateKeyA,publicKeyB,secretA);
printf("secretA=");
printarray(secretA,32);
ecc_p256_shared_secret_compute(privateKeyB,publicKeyA,secretB);
printf("secretB=");
printarray(secretB,32);