btstack_crypto.c (23b7e05eef4503ce54b4cef6a468bbaa344b5873) btstack_crypto.c (5db50aaad5b1b50d3f0645b052ed20baebdd39f3)
1/*
2 * Copyright (C) 2017 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 629 unchanged lines hidden (view full) ---

638 btstack_crypto_aes128_cmac_t * btstack_crypto_cmac;
639#ifdef ENABLE_ECC_P256
640 btstack_crypto_ecc_p256_t * btstack_crypto_ec_p192;
641#endif
642
643 // stack up and running?
644 if (hci_get_state() != HCI_STATE_WORKING) return;
645
1/*
2 * Copyright (C) 2017 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 629 unchanged lines hidden (view full) ---

638 btstack_crypto_aes128_cmac_t * btstack_crypto_cmac;
639#ifdef ENABLE_ECC_P256
640 btstack_crypto_ecc_p256_t * btstack_crypto_ec_p192;
641#endif
642
643 // stack up and running?
644 if (hci_get_state() != HCI_STATE_WORKING) return;
645
646 // already active?
647 if (btstack_crypto_wait_for_hci_result) return;
646 // try to do as much as possible
647 while (1){
648
648
649 // anything to do?
650 if (btstack_linked_list_empty(&btstack_crypto_operations)) return;
649 // anything to do?
650 if (btstack_linked_list_empty(&btstack_crypto_operations)) return;
651
651
652 // can send a command?
653 if (!hci_can_send_command_packet_now()) return;
652 // already active?
653 if (btstack_crypto_wait_for_hci_result) return;
654
654
655 btstack_crypto_t * btstack_crypto = (btstack_crypto_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations);
656 switch (btstack_crypto->operation){
657 case BTSTACK_CRYPTO_RANDOM:
658 btstack_crypto_wait_for_hci_result = 1;
659 hci_send_cmd(&hci_le_rand);
660 break;
661 case BTSTACK_CRYPTO_AES128:
662 btstack_crypto_aes128 = (btstack_crypto_aes128_t *) btstack_crypto;
655 // can send a command?
656 if (!hci_can_send_command_packet_now()) return;
657
658 // ok, find next task
659 btstack_crypto_t * btstack_crypto = (btstack_crypto_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations);
660 switch (btstack_crypto->operation){
661 case BTSTACK_CRYPTO_RANDOM:
662 btstack_crypto_wait_for_hci_result = 1;
663 hci_send_cmd(&hci_le_rand);
664 break;
665 case BTSTACK_CRYPTO_AES128:
666 btstack_crypto_aes128 = (btstack_crypto_aes128_t *) btstack_crypto;
663#ifdef USE_BTSTACK_AES128
667#ifdef USE_BTSTACK_AES128
664 btstack_aes128_calc(btstack_crypto_aes128->key, btstack_crypto_aes128->plaintext, btstack_crypto_aes128->ciphertext);
665 btstack_crypto_done(btstack_crypto);
668 btstack_aes128_calc(btstack_crypto_aes128->key, btstack_crypto_aes128->plaintext, btstack_crypto_aes128->ciphertext);
669 btstack_crypto_done(btstack_crypto);
666#else
670#else
667 btstack_crypto_aes128_start(btstack_crypto_aes128->key, btstack_crypto_aes128->plaintext);
671 btstack_crypto_aes128_start(btstack_crypto_aes128->key, btstack_crypto_aes128->plaintext);
668#endif
672#endif
669 break;
670 case BTSTACK_CRYPTO_CMAC_MESSAGE:
671 case BTSTACK_CRYPTO_CMAC_GENERATOR:
672 btstack_crypto_wait_for_hci_result = 1;
673 btstack_crypto_cmac = (btstack_crypto_aes128_cmac_t *) btstack_crypto;
674 if (btstack_crypto_cmac_state == CMAC_IDLE){
675 btstack_crypto_cmac_start(btstack_crypto_cmac);
676 } else {
677 btstack_crypto_cmac_handle_aes_engine_ready(btstack_crypto_cmac);
678 }
679 break;
673 break;
674 case BTSTACK_CRYPTO_CMAC_MESSAGE:
675 case BTSTACK_CRYPTO_CMAC_GENERATOR:
676 btstack_crypto_wait_for_hci_result = 1;
677 btstack_crypto_cmac = (btstack_crypto_aes128_cmac_t *) btstack_crypto;
678 if (btstack_crypto_cmac_state == CMAC_IDLE){
679 btstack_crypto_cmac_start(btstack_crypto_cmac);
680 } else {
681 btstack_crypto_cmac_handle_aes_engine_ready(btstack_crypto_cmac);
682 }
683 break;
680
684
681 case BTSTACK_CRYPTO_CCM_DIGEST_BLOCK:
682 case BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK:
683 case BTSTACK_CRYPTO_CCM_DECRYPT_BLOCK:
685 case BTSTACK_CRYPTO_CCM_DIGEST_BLOCK:
686 case BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK:
687 case BTSTACK_CRYPTO_CCM_DECRYPT_BLOCK:
684#ifdef USE_BTSTACK_AES128
688#ifdef USE_BTSTACK_AES128
685 UNUSED(btstack_crypto_ccm);
686 log_error("ccm not implemented for software aes128 yet");
689 UNUSED(btstack_crypto_ccm);
690 // NOTE: infinite output of this message
691 log_error("ccm not implemented for software aes128 yet");
687#else
692#else
688 btstack_crypto_ccm = (btstack_crypto_ccm_t *) btstack_crypto;
689 switch (btstack_crypto_ccm->state){
690 case CCM_CALCULATE_AAD_XN:
691 btstack_crypto_ccm_calc_aad_xn(btstack_crypto_ccm);
692 break;
693 case CCM_CALCULATE_X1:
694 btstack_crypto_ccm_calc_x1(btstack_crypto_ccm);
695 break;
696 case CCM_CALCULATE_S0:
697 btstack_crypto_ccm_calc_s0(btstack_crypto_ccm);
698 break;
699 case CCM_CALCULATE_SN:
700 btstack_crypto_ccm_calc_sn(btstack_crypto_ccm);
701 break;
702 case CCM_CALCULATE_XN:
703 btstack_crypto_ccm_calc_xn(btstack_crypto_ccm, btstack_crypto->operation == BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK ? btstack_crypto_ccm->input : btstack_crypto_ccm->output);
704 break;
705 default:
706 break;
707 }
693 btstack_crypto_ccm = (btstack_crypto_ccm_t *) btstack_crypto;
694 switch (btstack_crypto_ccm->state){
695 case CCM_CALCULATE_AAD_XN:
696 btstack_crypto_ccm_calc_aad_xn(btstack_crypto_ccm);
697 break;
698 case CCM_CALCULATE_X1:
699 btstack_crypto_ccm_calc_x1(btstack_crypto_ccm);
700 break;
701 case CCM_CALCULATE_S0:
702 btstack_crypto_ccm_calc_s0(btstack_crypto_ccm);
703 break;
704 case CCM_CALCULATE_SN:
705 btstack_crypto_ccm_calc_sn(btstack_crypto_ccm);
706 break;
707 case CCM_CALCULATE_XN:
708 btstack_crypto_ccm_calc_xn(btstack_crypto_ccm, btstack_crypto->operation == BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK ? btstack_crypto_ccm->input : btstack_crypto_ccm->output);
709 break;
710 default:
711 break;
712 }
708#endif
713#endif
709 break;
714 break;
710
711#ifdef ENABLE_ECC_P256
715
716#ifdef ENABLE_ECC_P256
712 case BTSTACK_CRYPTO_ECC_P256_GENERATE_KEY:
713 btstack_crypto_ec_p192 = (btstack_crypto_ecc_p256_t *) btstack_crypto;
714 switch (btstack_crypto_ecc_p256_key_generation_state){
715 case ECC_P256_KEY_GENERATION_DONE:
716 // done
717 btstack_crypto_log_ec_publickey(btstack_crypto_ecc_p256_public_key);
718 memcpy(btstack_crypto_ec_p192->public_key, btstack_crypto_ecc_p256_public_key, 64);
719 btstack_linked_list_pop(&btstack_crypto_operations);
720 (*btstack_crypto_ec_p192->btstack_crypto.context_callback.callback)(btstack_crypto_ec_p192->btstack_crypto.context_callback.context);
721 break;
722 case ECC_P256_KEY_GENERATION_IDLE:
717 case BTSTACK_CRYPTO_ECC_P256_GENERATE_KEY:
718 btstack_crypto_ec_p192 = (btstack_crypto_ecc_p256_t *) btstack_crypto;
719 switch (btstack_crypto_ecc_p256_key_generation_state){
720 case ECC_P256_KEY_GENERATION_DONE:
721 // done
722 btstack_crypto_log_ec_publickey(btstack_crypto_ecc_p256_public_key);
723 memcpy(btstack_crypto_ec_p192->public_key, btstack_crypto_ecc_p256_public_key, 64);
724 btstack_linked_list_pop(&btstack_crypto_operations);
725 (*btstack_crypto_ec_p192->btstack_crypto.context_callback.callback)(btstack_crypto_ec_p192->btstack_crypto.context_callback.context);
726 break;
727 case ECC_P256_KEY_GENERATION_IDLE:
723#ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION
728#ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION
724 log_info("start ecc random");
725 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_GENERATING_RANDOM;
726 btstack_crypto_ecc_p256_random_offset = 0;
727 btstack_crypto_wait_for_hci_result = 1;
728 hci_send_cmd(&hci_le_rand);
729 log_info("start ecc random");
730 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_GENERATING_RANDOM;
731 btstack_crypto_ecc_p256_random_offset = 0;
732 btstack_crypto_wait_for_hci_result = 1;
733 hci_send_cmd(&hci_le_rand);
729#else
734#else
730 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_W4_KEY;
731 btstack_crypto_wait_for_hci_result = 1;
732 hci_send_cmd(&hci_le_read_local_p256_public_key);
735 btstack_crypto_ecc_p256_key_generation_state = ECC_P256_KEY_GENERATION_W4_KEY;
736 btstack_crypto_wait_for_hci_result = 1;
737 hci_send_cmd(&hci_le_read_local_p256_public_key);
733#endif
738#endif
734 break;
739 break;
735#ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION
740#ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION
736 case ECC_P256_KEY_GENERATION_GENERATING_RANDOM:
737 log_info("more ecc random");
738 btstack_crypto_wait_for_hci_result = 1;
739 hci_send_cmd(&hci_le_rand);
740 break;
741 case ECC_P256_KEY_GENERATION_GENERATING_RANDOM:
742 log_info("more ecc random");
743 btstack_crypto_wait_for_hci_result = 1;
744 hci_send_cmd(&hci_le_rand);
745 break;
741#endif
746#endif
742 default:
743 break;
744 }
745 break;
746 case BTSTACK_CRYPTO_ECC_P256_CALCULATE_DHKEY:
747 btstack_crypto_ec_p192 = (btstack_crypto_ecc_p256_t *) btstack_crypto;
747 default:
748 break;
749 }
750 break;
751 case BTSTACK_CRYPTO_ECC_P256_CALCULATE_DHKEY:
752 btstack_crypto_ec_p192 = (btstack_crypto_ecc_p256_t *) btstack_crypto;
748#ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION
753#ifdef USE_SOFTWARE_ECC_P256_IMPLEMENTATION
749 btstack_crypto_ecc_p256_calculate_dhkey_software(btstack_crypto_ec_p192);
750 // done
751 btstack_linked_list_pop(&btstack_crypto_operations);
752 (*btstack_crypto_ec_p192->btstack_crypto.context_callback.callback)(btstack_crypto_ec_p192->btstack_crypto.context_callback.context);
754 btstack_crypto_ecc_p256_calculate_dhkey_software(btstack_crypto_ec_p192);
755 // done
756 btstack_linked_list_pop(&btstack_crypto_operations);
757 (*btstack_crypto_ec_p192->btstack_crypto.context_callback.callback)(btstack_crypto_ec_p192->btstack_crypto.context_callback.context);
753#else
758#else
754 btstack_crypto_wait_for_hci_result = 1;
755 hci_send_cmd(&hci_le_generate_dhkey, &btstack_crypto_ec_p192->public_key[0], &btstack_crypto_ec_p192->public_key[32]);
759 btstack_crypto_wait_for_hci_result = 1;
760 hci_send_cmd(&hci_le_generate_dhkey, &btstack_crypto_ec_p192->public_key[0], &btstack_crypto_ec_p192->public_key[32]);
756#endif
761#endif
757 break;
762 break;
758
759#endif /* ENABLE_ECC_P256 */
760
763
764#endif /* ENABLE_ECC_P256 */
765
761 default:
762 break;
766 default:
767 break;
768 }
763 }
764}
765
766static void btstack_crypto_handle_random_data(const uint8_t * data, uint16_t len){
767 btstack_crypto_random_t * btstack_crypto_random;
768 btstack_crypto_t * btstack_crypto = (btstack_crypto_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations);
769 uint16_t bytes_to_copy;
770 if (!btstack_crypto) return;

--- 425 unchanged lines hidden ---
769 }
770}
771
772static void btstack_crypto_handle_random_data(const uint8_t * data, uint16_t len){
773 btstack_crypto_random_t * btstack_crypto_random;
774 btstack_crypto_t * btstack_crypto = (btstack_crypto_t*) btstack_linked_list_get_first_item(&btstack_crypto_operations);
775 uint16_t bytes_to_copy;
776 if (!btstack_crypto) return;

--- 425 unchanged lines hidden ---