Added keys and encrypt/decrypt.
parent
c423636d77
commit
b55905b164
|
@ -20,7 +20,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#define MAX_BTLE_MSG_SIZE 128
|
#define MAX_BTLE_MSG_SIZE 1024
|
||||||
|
|
||||||
int btle_open(void** dev);
|
int btle_open(void** dev);
|
||||||
int btle_send(const unsigned char* buf, int len, void* context);
|
int btle_send(const unsigned char* buf, int len, void* context);
|
||||||
|
|
|
@ -35,7 +35,10 @@ int main(int argc, char** argv)
|
||||||
void* devCtx = NULL;
|
void* devCtx = NULL;
|
||||||
byte peerSalt[EXCHANGE_SALT_SZ];
|
byte peerSalt[EXCHANGE_SALT_SZ];
|
||||||
byte buffer[MAX_BTLE_MSG_SIZE];
|
byte buffer[MAX_BTLE_MSG_SIZE];
|
||||||
size_t bufLen;
|
word32 bufferSz;
|
||||||
|
byte plain[MAX_BTLE_MSG_SIZE];
|
||||||
|
word32 plainSz;
|
||||||
|
ecc_key myKey, peerKey;
|
||||||
|
|
||||||
wolfSSL_Init();
|
wolfSSL_Init();
|
||||||
|
|
||||||
|
@ -43,6 +46,11 @@ int main(int argc, char** argv)
|
||||||
wolfSSL_Debugging_ON();
|
wolfSSL_Debugging_ON();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* make my session key */
|
||||||
|
wc_ecc_init(&myKey);
|
||||||
|
wc_ecc_init(&peerKey);
|
||||||
|
wc_ecc_make_key(&rng, 32, &myKey);
|
||||||
|
|
||||||
/* open BTLE */
|
/* open BTLE */
|
||||||
ret = btle_open(&devCtx);
|
ret = btle_open(&devCtx);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
|
@ -55,12 +63,24 @@ int main(int argc, char** argv)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
cliCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
|
cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
|
||||||
if (cliCtx == NULL) {
|
if (cliCtx == NULL) {
|
||||||
printf("wc_ecc_ctx_new failed!\n");
|
printf("wc_ecc_ctx_new failed!\n");
|
||||||
ret = -1; goto cleanup;
|
ret = -1; goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* exchange public keys */
|
||||||
|
/* send my public key */
|
||||||
|
/* export my public key */
|
||||||
|
bufferSz = sizeof(buffer);
|
||||||
|
wc_ecc_export_x963(&myKey, buffer, &bufferSz);
|
||||||
|
ret = btle_send(buffer, bufferSz, devCtx);
|
||||||
|
|
||||||
|
/* Get peer key */
|
||||||
|
ret = btle_recv(buffer, sizeof(buffer), devCtx);
|
||||||
|
bufferSz = ret;
|
||||||
|
ret = wc_ecc_import_x963(buffer, bufferSz, &peerKey);
|
||||||
|
|
||||||
/* Collect Message to send and get echo */
|
/* Collect Message to send and get echo */
|
||||||
while (1) {
|
while (1) {
|
||||||
/* get my salt */
|
/* get my salt */
|
||||||
|
@ -78,17 +98,29 @@ int main(int argc, char** argv)
|
||||||
wc_ecc_ctx_set_peer_salt(cliCtx, peerSalt);
|
wc_ecc_ctx_set_peer_salt(cliCtx, peerSalt);
|
||||||
|
|
||||||
/* get message to send */
|
/* get message to send */
|
||||||
fgets((char*)buffer, sizeof(buffer), stdin);
|
bufferSz = sizeof(buffer);
|
||||||
|
fgets((char*)buffer, bufferSz, stdin);
|
||||||
|
bufferSz = strlen((char*)buffer);
|
||||||
|
|
||||||
bufLen = strlen((char*)buffer);
|
/* Encrypt message */
|
||||||
|
bufferSz = sizeof(buffer);
|
||||||
|
ret = wc_ecc_encrypt(&myKey, &peerKey, plain, plainSz, buffer, &bufferSz, cliCtx);
|
||||||
|
|
||||||
/* send message */
|
/* Send message */
|
||||||
btle_send(buffer, bufLen, devCtx);
|
btle_send(buffer, bufferSz, devCtx);
|
||||||
|
|
||||||
/* get response (echo) */
|
/* get message until null termination found */
|
||||||
btle_recv(buffer, bufLen, devCtx);
|
bufferSz = sizeof(bufferSz);
|
||||||
|
ret = btle_recv(buffer, bufferSz, devCtx);
|
||||||
|
|
||||||
if (strstr((char*)buffer, "EXIT"))
|
/* decrypt message */
|
||||||
|
bufferSz = ret;
|
||||||
|
ret = wc_ecc_decrypt(&myKey, &peerKey, buffer, bufferSz, plain, &plainSz, cliCtx);
|
||||||
|
|
||||||
|
printf("Recv %d: %s\n", plainSz, plain);
|
||||||
|
|
||||||
|
/* check for exit flag */
|
||||||
|
if (strstr((char*)plain, "EXIT"))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* reset context (reset my salt) */
|
/* reset context (reset my salt) */
|
||||||
|
|
|
@ -35,7 +35,10 @@ int main(int argc, char** argv)
|
||||||
void* devCtx = NULL;
|
void* devCtx = NULL;
|
||||||
byte peerSalt[EXCHANGE_SALT_SZ];
|
byte peerSalt[EXCHANGE_SALT_SZ];
|
||||||
byte buffer[MAX_BTLE_MSG_SIZE];
|
byte buffer[MAX_BTLE_MSG_SIZE];
|
||||||
size_t bufLen;
|
word32 bufferSz;
|
||||||
|
byte plain[MAX_BTLE_MSG_SIZE];
|
||||||
|
word32 plainSz;
|
||||||
|
ecc_key myKey, peerKey;
|
||||||
|
|
||||||
wolfSSL_Init();
|
wolfSSL_Init();
|
||||||
|
|
||||||
|
@ -43,6 +46,11 @@ int main(int argc, char** argv)
|
||||||
wolfSSL_Debugging_ON();
|
wolfSSL_Debugging_ON();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* make my session key */
|
||||||
|
wc_ecc_init(&myKey);
|
||||||
|
wc_ecc_init(&peerKey);
|
||||||
|
wc_ecc_make_key(&rng, 32, &myKey);
|
||||||
|
|
||||||
/* open BTLE */
|
/* open BTLE */
|
||||||
ret = btle_open(&devCtx);
|
ret = btle_open(&devCtx);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
|
@ -61,6 +69,18 @@ int main(int argc, char** argv)
|
||||||
ret = -1; goto cleanup;
|
ret = -1; goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* exchange public keys */
|
||||||
|
/* Get peer key */
|
||||||
|
ret = btle_recv(buffer, sizeof(buffer), devCtx);
|
||||||
|
bufferSz = ret;
|
||||||
|
ret = wc_ecc_import_x963(buffer, bufferSz, &peerKey);
|
||||||
|
|
||||||
|
/* send my public key */
|
||||||
|
/* export my public key */
|
||||||
|
bufferSz = sizeof(buffer);
|
||||||
|
wc_ecc_export_x963(&myKey, buffer, &bufferSz);
|
||||||
|
ret = btle_send(buffer, bufferSz, devCtx);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
mySalt = wc_ecc_ctx_get_own_salt(srvCtx);
|
mySalt = wc_ecc_ctx_get_own_salt(srvCtx);
|
||||||
if (mySalt == NULL) {
|
if (mySalt == NULL) {
|
||||||
|
@ -75,13 +95,24 @@ int main(int argc, char** argv)
|
||||||
ret = btle_send(mySalt, EXCHANGE_SALT_SZ, devCtx);
|
ret = btle_send(mySalt, EXCHANGE_SALT_SZ, devCtx);
|
||||||
|
|
||||||
/* get message until null termination found */
|
/* get message until null termination found */
|
||||||
btle_recv(buffer, sizeof(buffer), devCtx);
|
bufferSz = sizeof(bufferSz);
|
||||||
|
ret = btle_recv(buffer, bufferSz, devCtx);
|
||||||
|
|
||||||
bufLen = strlen((char*)buffer);
|
/* decrypt message */
|
||||||
|
bufferSz = ret;
|
||||||
|
ret = wc_ecc_decrypt(&myKey, &peerKey, buffer, bufferSz, plain, &plainSz, srvCtx);
|
||||||
|
|
||||||
btle_send(buffer, bufLen, devCtx);
|
printf("Recv %d: %s\n", plainSz, plain);
|
||||||
|
|
||||||
if (strstr((char*)buffer, "EXIT"))
|
/* Encrypt message */
|
||||||
|
bufferSz = sizeof(buffer);
|
||||||
|
ret = wc_ecc_encrypt(&myKey, &peerKey, plain, plainSz, buffer, &bufferSz, srvCtx);
|
||||||
|
|
||||||
|
/* Send message */
|
||||||
|
btle_send(buffer, bufferSz, devCtx);
|
||||||
|
|
||||||
|
/* check for exit flag */
|
||||||
|
if (strstr((char*)plain, "EXIT"))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* reset context (reset my salt) */
|
/* reset context (reset my salt) */
|
||||||
|
|
Loading…
Reference in New Issue