fast_crypto_ops.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "crypto/common.h"
  15. #include "crypto/aes_wrap.h"
  16. #include "crypto/sha256.h"
  17. #include "crypto/crypto.h"
  18. #include "crypto/md5.h"
  19. #include "crypto/sha1.h"
  20. #include "crypto/aes.h"
  21. #include "crypto/dh_group5.h"
  22. #include "wps/wps.h"
  23. #include "wps/wps_i.h"
  24. #include "wpa2/eap_peer/eap.h"
  25. #include "wpa2/tls/tls.h"
  26. #include "wpa2/eap_peer/eap_methods.h"
  27. #include "wpa2/eap_peer/eap_i.h"
  28. #include "wpa2/eap_peer/eap_common.h"
  29. #include "esp_wifi_crypto_types.h"
  30. /*
  31. * The parameters is used to set the cyrpto callback function for station connect when in security mode,
  32. * every callback function can register as fast_xxx or normal one, i.e, fast_aes_wrap or aes_wrap, the
  33. * difference between them is the normal API is calculate by software, the fast one use the hardware
  34. * crypto in it, can be faster than the normal one, so the callback function register in default is which
  35. * we recommend, so as the API in WPS default and WPA2 default.
  36. */
  37. const wpa_crypto_funcs_t g_wifi_default_wpa_crypto_funcs = {
  38. .size = sizeof(wpa_crypto_funcs_t),
  39. .version = ESP_WIFI_CRYPTO_VERSION,
  40. .aes_wrap = (esp_aes_wrap_t)fast_aes_wrap,
  41. .aes_unwrap = (esp_aes_unwrap_t)fast_aes_unwrap,
  42. .hmac_sha256_vector = (esp_hmac_sha256_vector_t)fast_hmac_sha256_vector,
  43. .sha256_prf = (esp_sha256_prf_t)fast_sha256_prf,
  44. .hmac_md5 = (esp_hmac_md5_t)hmac_md5,
  45. .hamc_md5_vector = (esp_hmac_md5_vector_t)hmac_md5_vector,
  46. .hmac_sha1 = (esp_hmac_sha1_t)hmac_sha1,
  47. .hmac_sha1_vector = (esp_hmac_sha1_vector_t)hmac_sha1_vector,
  48. .sha1_prf = (esp_sha1_prf_t)sha1_prf,
  49. .sha1_vector = (esp_sha1_vector_t)sha1_vector,
  50. .pbkdf2_sha1 = (esp_pbkdf2_sha1_t)pbkdf2_sha1,
  51. .rc4_skip = (esp_rc4_skip_t)rc4_skip,
  52. .md5_vector = (esp_md5_vector_t)md5_vector,
  53. .aes_encrypt = (esp_aes_encrypt_t)aes_encrypt,
  54. .aes_encrypt_init = (esp_aes_encrypt_init_t)aes_encrypt_init,
  55. .aes_encrypt_deinit = (esp_aes_encrypt_deinit_t)aes_encrypt_deinit,
  56. .aes_decrypt = (esp_aes_decrypt_t)aes_decrypt,
  57. .aes_decrypt_init = (esp_aes_decrypt_init_t)aes_decrypt_init,
  58. .aes_decrypt_deinit = (esp_aes_decrypt_deinit_t)aes_decrypt_deinit
  59. };
  60. const wps_crypto_funcs_t g_wifi_default_wps_crypto_funcs = {
  61. .size = sizeof(wps_crypto_funcs_t),
  62. .version = ESP_WIFI_CRYPTO_VERSION,
  63. .aes_128_encrypt = (esp_aes_128_encrypt_t)fast_aes_128_cbc_encrypt,
  64. .aes_128_decrypt = (esp_aes_128_decrypt_t)fast_aes_128_cbc_decrypt,
  65. .crypto_mod_exp = (esp_crypto_mod_exp_t)fast_crypto_mod_exp,
  66. .hmac_sha256 = (esp_hmac_sha256_t)fast_hmac_sha256,
  67. .hmac_sha256_vector = (esp_hmac_sha256_vector_t)fast_hmac_sha256_vector,
  68. .sha256_vector = (esp_sha256_vector_t)fast_sha256_vector,
  69. .uuid_gen_mac_addr = (esp_uuid_gen_mac_addr_t)uuid_gen_mac_addr,
  70. .dh5_free = (esp_dh5_free_t)dh5_free,
  71. .wps_build_assoc_req_ie = (esp_wps_build_assoc_req_ie_t)wps_build_assoc_req_ie,
  72. .wps_build_assoc_resp_ie = (esp_wps_build_assoc_resp_ie_t)wps_build_assoc_resp_ie,
  73. .wps_build_probe_req_ie = (esp_wps_build_probe_req_ie_t)wps_build_probe_req_ie,
  74. .wps_build_public_key = (esp_wps_build_public_key_t)wps_build_public_key,
  75. .wps_enrollee_get_msg = (esp_wps_enrollee_get_msg_t)wps_enrollee_get_msg,
  76. .wps_enrollee_process_msg = (esp_wps_enrollee_process_msg_t)wps_enrollee_process_msg,
  77. .wps_generate_pin = (esp_wps_generate_pin_t)wps_generate_pin,
  78. .wps_is_selected_pin_registrar = (esp_wps_is_selected_pin_registrar_t)wps_is_selected_pin_registrar,
  79. .wps_is_selected_pbc_registrar = (esp_wps_is_selected_pbc_registrar_t)wps_is_selected_pbc_registrar,
  80. .eap_msg_alloc = (esp_eap_msg_alloc_t)eap_msg_alloc
  81. };
  82. /*
  83. * What should notice is that the cyrpto hash type function and crypto cipher type function can not register
  84. * as different, i.e, if you use fast_crypto_hash_init, you should use fast_crypto_hash_update and
  85. * fast_crypto_hash_finish for finish hash calculate, rather than call crypto_hash_update and
  86. * crypto_hash_finish, so do crypto_cipher.
  87. */
  88. const wpa2_crypto_funcs_t g_wifi_default_wpa2_crypto_funcs = {
  89. .size = sizeof(wpa2_crypto_funcs_t),
  90. .version = ESP_WIFI_CRYPTO_VERSION,
  91. .crypto_hash_init = (esp_crypto_hash_init_t)fast_crypto_hash_init,
  92. .crypto_hash_update = (esp_crypto_hash_update_t)fast_crypto_hash_update,
  93. .crypto_hash_finish = (esp_crypto_hash_finish_t)fast_crypto_hash_finish,
  94. .crypto_cipher_init = (esp_crypto_cipher_init_t)fast_crypto_cipher_init,
  95. .crypto_cipher_encrypt = (esp_crypto_cipher_encrypt_t)fast_crypto_cipher_encrypt,
  96. .crypto_cipher_decrypt = (esp_crypto_cipher_decrypt_t)fast_crypto_cipher_decrypt,
  97. .crypto_cipher_deinit = (esp_crypto_cipher_deinit_t)fast_crypto_cipher_deinit,
  98. .crypto_mod_exp = (esp_crypto_mod_exp_t)crypto_mod_exp,
  99. .sha256_vector = (esp_sha256_vector_t)fast_sha256_vector,
  100. .tls_init = (esp_tls_init_t)tls_init,
  101. .tls_deinit = (esp_tls_deinit_t)tls_deinit,
  102. .eap_peer_blob_init = (esp_eap_peer_blob_init_t)eap_peer_blob_init,
  103. .eap_peer_blob_deinit = (esp_eap_peer_blob_deinit_t)eap_peer_blob_deinit,
  104. .eap_peer_config_init = (esp_eap_peer_config_init_t)eap_peer_config_init,
  105. .eap_peer_config_deinit = (esp_eap_peer_config_deinit_t)eap_peer_config_deinit,
  106. .eap_peer_register_methods = (esp_eap_peer_register_methods_t)eap_peer_register_methods,
  107. .eap_peer_unregister_methods = (esp_eap_peer_unregister_methods_t)eap_peer_unregister_methods,
  108. .eap_deinit_prev_method = (esp_eap_deinit_prev_method_t)eap_deinit_prev_method,
  109. .eap_peer_get_eap_method = (esp_eap_peer_get_eap_method_t)eap_peer_get_eap_method,
  110. .eap_sm_abort = (esp_eap_sm_abort_t)eap_sm_abort,
  111. .eap_sm_build_nak = (esp_eap_sm_build_nak_t)eap_sm_build_nak,
  112. .eap_sm_build_identity_resp = (esp_eap_sm_build_identity_resp_t)eap_sm_build_identity_resp,
  113. .eap_msg_alloc = (esp_eap_msg_alloc_t)eap_msg_alloc
  114. };
  115. const mesh_crypto_funcs_t g_wifi_default_mesh_crypto_funcs = {
  116. .aes_128_encrypt = (esp_aes_128_encrypt_t)fast_aes_128_cbc_encrypt,
  117. .aes_128_decrypt = (esp_aes_128_decrypt_t)fast_aes_128_cbc_decrypt,
  118. };