├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── examples ├── Example01_TemperatureSensor │ ├── Example01_TemperatureSensor.ino │ ├── my_accessory.c │ └── wifi_info.h ├── Example02_Switch │ ├── Example02_Switch.ino │ ├── my_accessory.c │ └── wifi_info.h ├── Example03_StatelessProgrammableSwitch │ ├── ESPButton.h │ ├── Example03_StatelessProgrammableSwitch.ino │ ├── my_accessory.c │ └── wifi_info.h ├── Example04_MultipleAccessories │ ├── Example04_MultipleAccessories.ino │ ├── my_accessory.c │ └── wifi_info.h ├── Example05_WS2812_Neopixel │ ├── Example05_WS2812_Neopixel.ino │ ├── my_accessory.c │ └── wifi_info.h ├── Example06_makesmart_lock │ ├── Example06_makesmart_lock.ino │ ├── my_accessory.c │ └── wifi_info.h └── legacy │ ├── simple_led │ ├── ButtonDebounce.h │ ├── ButtonHandler.h │ ├── simple_led.ino │ └── simple_led_accessory.c │ └── simplest_led │ ├── simplest_led.ino │ └── simplest_led_accessory.c ├── extras ├── ESP8266WiFi_nossl_noleak │ └── src │ │ ├── ESP8266WiFi.cpp │ │ ├── ESP8266WiFi.h │ │ ├── ESP8266WiFiAP.cpp │ │ ├── ESP8266WiFiAP.h │ │ ├── ESP8266WiFiGeneric.cpp │ │ ├── ESP8266WiFiGeneric.h │ │ ├── ESP8266WiFiMulti.cpp │ │ ├── ESP8266WiFiMulti.h │ │ ├── ESP8266WiFiSTA.cpp │ │ ├── ESP8266WiFiSTA.h │ │ ├── ESP8266WiFiScan.cpp │ │ ├── ESP8266WiFiScan.h │ │ ├── ESP8266WiFiType.h │ │ ├── WiFiClient.cpp │ │ ├── WiFiClient.h │ │ ├── WiFiServer.cpp │ │ ├── WiFiServer.h │ │ ├── WiFiUdp.cpp │ │ ├── WiFiUdp.h │ │ └── include │ │ ├── ClientContext.h │ │ ├── DataSource.h │ │ ├── SSLContext.h │ │ ├── UdpContext.h │ │ ├── WiFiState.h │ │ ├── slist.h │ │ ├── ssl.h │ │ └── wl_definitions.h ├── example_serial_output.txt ├── example_serial_output_v1.1.0.txt ├── preview.jpg └── qq_group_qrcode.png ├── library.json ├── library.properties ├── src ├── accessories.c ├── arduino_homekit_server.cpp ├── arduino_homekit_server.h ├── base64.c ├── base64.h ├── cJSON.c ├── cJSON.h ├── cQueue.c ├── cQueue.h ├── constants.h ├── crypto.c ├── crypto.h ├── esp_xpgm.h ├── homekit │ ├── characteristics.h │ ├── homekit.h │ ├── tlv.h │ └── types.h ├── homekit_debug.c ├── homekit_debug.h ├── http_parser.c ├── http_parser.h ├── json.c ├── json.h ├── pairing.h ├── port.c ├── port.h ├── query_params.c ├── query_params.h ├── storage.c ├── storage.h ├── tlv.c ├── types.c ├── user_settings.h ├── watchdog.c ├── watchdog.h ├── wolfcrypt │ └── src │ │ ├── chacha.c │ │ ├── chacha20_poly1305.c │ │ ├── curve25519.c │ │ ├── ed25519.c │ │ ├── error.c │ │ ├── fe_low_mem.c │ │ ├── fe_operations.c │ │ ├── ge_low_mem.c │ │ ├── ge_operations.c │ │ ├── hash.c │ │ ├── hmac.c │ │ ├── integer.c │ │ ├── misc.c │ │ ├── poly1305.c │ │ ├── random.c │ │ ├── sha.c │ │ ├── sha256.c │ │ ├── sha512.c │ │ ├── srp.c │ │ └── wolfmath.c └── wolfssl │ └── wolfcrypt │ ├── aes.h │ ├── arc4.h │ ├── asn.h │ ├── asn_public.h │ ├── blake2-impl.h │ ├── blake2-int.h │ ├── blake2.h │ ├── camellia.h │ ├── chacha.h │ ├── chacha20_poly1305.h │ ├── cmac.h │ ├── coding.h │ ├── compress.h │ ├── cpuid.h │ ├── curve25519.h │ ├── des3.h │ ├── dh.h │ ├── dsa.h │ ├── ecc.h │ ├── ed25519.h │ ├── error-crypt.h │ ├── fe_operations.h │ ├── fips_test.h │ ├── ge_operations.h │ ├── hash.h │ ├── hc128.h │ ├── hmac.h │ ├── idea.h │ ├── integer.h │ ├── logging.h │ ├── md2.h │ ├── md4.h │ ├── md5.h │ ├── mem_track.h │ ├── memory.h │ ├── misc.h │ ├── mpi_class.h │ ├── mpi_superclass.h │ ├── pkcs12.h │ ├── pkcs7.h │ ├── poly1305.h │ ├── pwdbased.h │ ├── rabbit.h │ ├── random.h │ ├── ripemd.h │ ├── rsa.h │ ├── settings.h │ ├── sha.h │ ├── sha256.h │ ├── sha3.h │ ├── sha512.h │ ├── signature.h │ ├── sp.h │ ├── srp.h │ ├── tfm.h │ ├── types.h │ ├── visibility.h │ ├── wc_encrypt.h │ ├── wc_port.h │ ├── wolfevent.h │ └── wolfmath.h └── translations ├── README-ptbr.md ├── README-ru.md └── README_cn.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # Others 35 | .DS_STORE 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Mixiaoxiao(谜小小) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /examples/Example01_TemperatureSensor/Example01_TemperatureSensor.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * temperature_sensor.ino 3 | * 4 | * This example shows how to: 5 | * 1. define a temperature sensor accessory and its characteristics (in my_accessory.c). 6 | * 2. report the sensor value to HomeKit (just random value here, you need to change it to your real sensor value). 7 | * 8 | * Created on: 2020-05-15 9 | * Author: Mixiaoxiao (Wang Bin) 10 | * 11 | * Note: 12 | * 13 | * You are recommended to read the Apple's HAP doc before using this library. 14 | * https://developer.apple.com/support/homekit-accessory-protocol/ 15 | * 16 | * This HomeKit library is mostly written in C, 17 | * you can define your accessory/service/characteristic in a .c file, 18 | * since the library provides convenient Macro (C only, CPP can not compile) to do this. 19 | * But it is possible to do this in .cpp or .ino (just not so conveniently), do it yourself if you like. 20 | * Check out homekit/characteristics.h and use the Macro provided to define your accessory. 21 | * 22 | * Generally, the Arduino libraries (e.g. sensors, ws2812) are written in cpp, 23 | * you can include and use them in a .ino or a .cpp file (but can NOT in .c). 24 | * A .ino is a .cpp indeed. 25 | * 26 | * You can define some variables in a .c file, e.g. int my_value = 1;, 27 | * and you can access this variable in a .ino or a .cpp by writing extern "C" int my_value;. 28 | * 29 | * So, if you want use this HomeKit library and other Arduino Libraries together, 30 | * 1. define your HomeKit accessory/service/characteristic in a .c file 31 | * 2. in your .ino, include some Arduino Libraries and you can use them normally 32 | * write extern "C" homekit_characteristic_t xxxx; to access the characteristic defined in your .c file 33 | * write your logic code (eg. read sensors) and 34 | * report your data by writing your_characteristic.value.xxxx_value = some_data; homekit_characteristic_notify(..., ...) 35 | * done. 36 | */ 37 | 38 | #include 39 | #include 40 | #include "wifi_info.h" 41 | //include the Arduino library for your real sensor here, e.g. 42 | 43 | #define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); 44 | 45 | void setup() { 46 | Serial.begin(115200); 47 | wifi_connect(); // in wifi_info.h 48 | my_homekit_setup(); 49 | } 50 | 51 | void loop() { 52 | my_homekit_loop(); 53 | delay(10); 54 | } 55 | 56 | //============================== 57 | // Homekit setup and loop 58 | //============================== 59 | 60 | // access your homekit characteristics defined in my_accessory.c 61 | extern "C" homekit_server_config_t config; 62 | extern "C" homekit_characteristic_t cha_current_temperature; 63 | 64 | static uint32_t next_heap_millis = 0; 65 | static uint32_t next_report_millis = 0; 66 | 67 | void my_homekit_setup() { 68 | arduino_homekit_setup(&config); 69 | } 70 | 71 | void my_homekit_loop() { 72 | arduino_homekit_loop(); 73 | const uint32_t t = millis(); 74 | if (t > next_report_millis) { 75 | // report sensor values every 10 seconds 76 | next_report_millis = t + 10 * 1000; 77 | my_homekit_report(); 78 | } 79 | if (t > next_heap_millis) { 80 | // show heap info every 5 seconds 81 | next_heap_millis = t + 5 * 1000; 82 | LOG_D("Free heap: %d, HomeKit clients: %d", 83 | ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); 84 | 85 | } 86 | } 87 | 88 | void my_homekit_report() { 89 | float temperature_value = random_value(10, 30); // FIXME, read your real sensor here. 90 | cha_current_temperature.value.float_value = temperature_value; 91 | LOG_D("Current temperature: %.1f", temperature_value); 92 | homekit_characteristic_notify(&cha_current_temperature, cha_current_temperature.value); 93 | } 94 | 95 | int random_value(int min, int max) { 96 | return min + random(max - min); 97 | } 98 | -------------------------------------------------------------------------------- /examples/Example01_TemperatureSensor/my_accessory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * my_accessory.c 3 | * Define the accessory in C language using the Macro in characteristics.h 4 | * 5 | * Created on: 2020-05-15 6 | * Author: Mixiaoxiao (Wang Bin) 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | // Called to identify this accessory. See HAP section 6.7.6 Identify Routine 13 | // Generally this is called when paired successfully or click the "Identify Accessory" button in Home APP. 14 | void my_accessory_identify(homekit_value_t _value) { 15 | printf("accessory identify\n"); 16 | } 17 | 18 | // For TEMPERATURE_SENSOR, 19 | // the required characteristics are: CURRENT_TEMPERATURE 20 | // the optional characteristics are: NAME, STATUS_ACTIVE, STATUS_FAULT, STATUS_TAMPERED, STATUS_LOW_BATTERY 21 | // See HAP section 8.41 and characteristics.h 22 | 23 | // (required) format: float; HAP section 9.35; min 0, max 100, step 0.1, unit celsius 24 | homekit_characteristic_t cha_current_temperature = HOMEKIT_CHARACTERISTIC_(CURRENT_TEMPERATURE, 0); 25 | 26 | // (optional) format: string; HAP section 9.62; max length 64 27 | homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "Temperature Sensor"); 28 | 29 | 30 | // (optional) format: bool; HAP section 9.96 31 | // homekit_characteristic_t cha_status_active = HOMEKIT_CHARACTERISTIC_(STATUS_ACTIVE, true); 32 | 33 | // (optional) format: uint8; HAP section 9.97; 0 "No Fault", 1 "General Fault" 34 | // homekit_characteristic_t cha_status_fault = HOMEKIT_CHARACTERISTIC_(STATUS_FAULT, 0); 35 | 36 | // (optional) format: uint8; HAP section 9.100; 0 "Accessory is not tampered", 1 "Accessory is tampered with" 37 | // homekit_characteristic_t cha_status_tampered = HOMEKIT_CHARACTERISTIC_(STATUS_TAMPERED, 0); 38 | 39 | // (optional) format: uint8; HAP section 9.99; 0 "Battery level is normal", 1 "Battery level is low" 40 | // homekit_characteristic_t cha_status_low_battery = HOMEKIT_CHARACTERISTIC_(STATUS_LOW_BATTERY, 0); 41 | 42 | // example for humidity 43 | // homekit_characteristic_t cha_humidity = HOMEKIT_CHARACTERISTIC_(CURRENT_RELATIVE_HUMIDITY, 0); 44 | 45 | homekit_accessory_t *accessories[] = { 46 | HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_sensor, .services=(homekit_service_t*[]) { 47 | HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) { 48 | HOMEKIT_CHARACTERISTIC(NAME, "Temperature Sensor"), 49 | HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"), 50 | HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"), 51 | HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"), 52 | HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"), 53 | HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify), 54 | NULL 55 | }), 56 | HOMEKIT_SERVICE(TEMPERATURE_SENSOR, .primary=true, .characteristics=(homekit_characteristic_t*[]) { 57 | &cha_current_temperature, 58 | &cha_name,//optional 59 | //&cha_status_active,//optional 60 | //&cha_status_fault,//optional 61 | //&cha_status_tampered,//optional 62 | //&cha_status_low_battery,//optional 63 | NULL 64 | }), 65 | // Add this HOMEKIT_SERVICE if you has a HUMIDITY_SENSOR together 66 | /* 67 | HOMEKIT_SERVICE(HUMIDITY_SENSOR, .characteristics=(homekit_characteristic_t*[]) { 68 | HOMEKIT_CHARACTERISTIC(NAME, "Humidity Sensor"), 69 | &cha_humidity, 70 | NULL 71 | }),*/ 72 | NULL 73 | }), 74 | NULL 75 | }; 76 | 77 | homekit_server_config_t config = { 78 | .accessories = accessories, 79 | .password = "111-11-111" 80 | }; 81 | 82 | 83 | -------------------------------------------------------------------------------- /examples/Example01_TemperatureSensor/wifi_info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * wifi_info.h 3 | * 4 | * Created on: 2020-05-15 5 | * Author: Mixiaoxiao (Wang Bin) 6 | */ 7 | 8 | #ifndef WIFI_INFO_H_ 9 | #define WIFI_INFO_H_ 10 | 11 | #if defined(ESP8266) 12 | #include 13 | #elif defined(ESP32) 14 | #include 15 | #endif 16 | 17 | const char *ssid = "your-ssid"; 18 | const char *password = "your-password"; 19 | 20 | void wifi_connect() { 21 | WiFi.persistent(false); 22 | WiFi.mode(WIFI_STA); 23 | WiFi.setAutoReconnect(true); 24 | WiFi.begin(ssid, password); 25 | Serial.println("WiFi connecting..."); 26 | while (!WiFi.isConnected()) { 27 | delay(100); 28 | Serial.print("."); 29 | } 30 | Serial.print("\n"); 31 | Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str()); 32 | } 33 | 34 | #endif /* WIFI_INFO_H_ */ 35 | -------------------------------------------------------------------------------- /examples/Example02_Switch/Example02_Switch.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * switch.ino 3 | * 4 | * Created on: 2020-05-15 5 | * Author: Mixiaoxiao (Wang Bin) 6 | * 7 | * HAP section 8.38 Switch 8 | * An accessory contains a switch. 9 | * 10 | * This example shows how to: 11 | * 1. define a switch accessory and its characteristics (in my_accessory.c). 12 | * 2. get the switch-event sent from iOS Home APP. 13 | * 3. report the switch value to HomeKit. 14 | * 15 | * You should: 16 | * 1. read and use the Example01_TemperatureSensor with detailed comments 17 | * to know the basic concept and usage of this library before other examples。 18 | * 2. erase the full flash or call homekit_storage_reset() in setup() 19 | * to remove the previous HomeKit pairing storage and 20 | * enable the pairing with the new accessory of this new HomeKit example. 21 | */ 22 | 23 | #include 24 | #include 25 | #include "wifi_info.h" 26 | 27 | #define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); 28 | 29 | void setup() { 30 | Serial.begin(115200); 31 | wifi_connect(); // in wifi_info.h 32 | //homekit_storage_reset(); // to remove the previous HomeKit pairing storage when you first run this new HomeKit example 33 | my_homekit_setup(); 34 | } 35 | 36 | void loop() { 37 | my_homekit_loop(); 38 | delay(10); 39 | } 40 | 41 | //============================== 42 | // HomeKit setup and loop 43 | //============================== 44 | 45 | // access your HomeKit characteristics defined in my_accessory.c 46 | extern "C" homekit_server_config_t config; 47 | extern "C" homekit_characteristic_t cha_switch_on; 48 | 49 | static uint32_t next_heap_millis = 0; 50 | 51 | #define PIN_SWITCH 2 52 | 53 | //Called when the switch value is changed by iOS Home APP 54 | void cha_switch_on_setter(const homekit_value_t value) { 55 | bool on = value.bool_value; 56 | cha_switch_on.value.bool_value = on; //sync the value 57 | LOG_D("Switch: %s", on ? "ON" : "OFF"); 58 | digitalWrite(PIN_SWITCH, on ? LOW : HIGH); 59 | } 60 | 61 | void my_homekit_setup() { 62 | pinMode(PIN_SWITCH, OUTPUT); 63 | digitalWrite(PIN_SWITCH, HIGH); 64 | 65 | //Add the .setter function to get the switch-event sent from iOS Home APP. 66 | //The .setter should be added before arduino_homekit_setup. 67 | //HomeKit sever uses the .setter_ex internally, see homekit_accessories_init function. 68 | //Maybe this is a legacy design issue in the original esp-homekit library, 69 | //and I have no reason to modify this "feature". 70 | cha_switch_on.setter = cha_switch_on_setter; 71 | arduino_homekit_setup(&config); 72 | 73 | //report the switch value to HomeKit if it is changed (e.g. by a physical button) 74 | //bool switch_is_on = true/false; 75 | //cha_switch_on.value.bool_value = switch_is_on; 76 | //homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value); 77 | } 78 | 79 | void my_homekit_loop() { 80 | arduino_homekit_loop(); 81 | const uint32_t t = millis(); 82 | if (t > next_heap_millis) { 83 | // show heap info every 5 seconds 84 | next_heap_millis = t + 5 * 1000; 85 | LOG_D("Free heap: %d, HomeKit clients: %d", 86 | ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /examples/Example02_Switch/my_accessory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * my_accessory.c 3 | * Define the accessory in C language using the Macro in characteristics.h 4 | * 5 | * Created on: 2020-05-15 6 | * Author: Mixiaoxiao (Wang Bin) 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | void my_accessory_identify(homekit_value_t _value) { 13 | printf("accessory identify\n"); 14 | } 15 | 16 | // Switch (HAP section 8.38) 17 | // required: ON 18 | // optional: NAME 19 | 20 | // format: bool; HAP section 9.70; write the .setter function to get the switch-event sent from iOS Home APP. 21 | homekit_characteristic_t cha_switch_on = HOMEKIT_CHARACTERISTIC_(ON, false); 22 | 23 | // format: string; HAP section 9.62; max length 64 24 | homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "Switch"); 25 | 26 | homekit_accessory_t *accessories[] = { 27 | HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_switch, .services=(homekit_service_t*[]) { 28 | HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) { 29 | HOMEKIT_CHARACTERISTIC(NAME, "Switch"), 30 | HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"), 31 | HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"), 32 | HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"), 33 | HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"), 34 | HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify), 35 | NULL 36 | }), 37 | HOMEKIT_SERVICE(SWITCH, .primary=true, .characteristics=(homekit_characteristic_t*[]){ 38 | &cha_switch_on, 39 | &cha_name, 40 | NULL 41 | }), 42 | NULL 43 | }), 44 | NULL 45 | }; 46 | 47 | homekit_server_config_t config = { 48 | .accessories = accessories, 49 | .password = "111-11-111" 50 | }; 51 | 52 | 53 | -------------------------------------------------------------------------------- /examples/Example02_Switch/wifi_info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * wifi_info.h 3 | * 4 | * Created on: 2020-05-15 5 | * Author: Mixiaoxiao (Wang Bin) 6 | */ 7 | 8 | #ifndef WIFI_INFO_H_ 9 | #define WIFI_INFO_H_ 10 | 11 | #if defined(ESP8266) 12 | #include 13 | #elif defined(ESP32) 14 | #include 15 | #endif 16 | 17 | const char *ssid = "your-ssid"; 18 | const char *password = "your-password"; 19 | 20 | void wifi_connect() { 21 | WiFi.persistent(false); 22 | WiFi.mode(WIFI_STA); 23 | WiFi.setAutoReconnect(true); 24 | WiFi.begin(ssid, password); 25 | Serial.println("WiFi connecting..."); 26 | while (!WiFi.isConnected()) { 27 | delay(100); 28 | Serial.print("."); 29 | } 30 | Serial.print("\n"); 31 | Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str()); 32 | } 33 | 34 | #endif /* WIFI_INFO_H_ */ 35 | -------------------------------------------------------------------------------- /examples/Example03_StatelessProgrammableSwitch/Example03_StatelessProgrammableSwitch.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * stateless_programmable_switch.ino 3 | * 4 | * Created on: 2020-05-16 5 | * Author: Mixiaoxiao (Wang Bin) 6 | * 7 | * 8 | * HAP section 8.37 Stateless Programmable Switch 9 | * An accessory contains a stateless programmable switch, also called a button. 10 | * Used to report Programmable Switch Event to HomeKit. 11 | * 12 | * Programmable Switch Event: (HAP section 9.75) 13 | * 0 "Single Press" 14 | * 1 "Double Press" 15 | * 2 "Long Press" 16 | * 17 | * Note: 18 | * Reading this "Programmable Switch Event" should always return "null". 19 | * The value must only be reported in the events ("ev") property. 20 | * 21 | * You should: 22 | * 1. read and use the Example01_TemperatureSensor with detailed comments 23 | * to know the basic concept and usage of this library before other examples。 24 | * 2. erase the full flash or call homekit_storage_reset() in setup() 25 | * to remove the previous HomeKit pairing storage and 26 | * enable the pairing with the new accessory of this new HomeKit example. 27 | */ 28 | 29 | #include 30 | #include 31 | #include "wifi_info.h" 32 | #include "ESPButton.h" 33 | 34 | #define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); 35 | 36 | void setup() { 37 | Serial.begin(115200); 38 | wifi_connect(); // in wifi_info.h 39 | //homekit_storage_reset(); // to remove the previous HomeKit pairing storage when you first run this new HomeKit example 40 | my_homekit_setup(); 41 | } 42 | 43 | void loop() { 44 | my_homekit_loop(); 45 | delay(10); 46 | } 47 | 48 | //============================== 49 | // HomeKit setup and loop 50 | //============================== 51 | 52 | extern "C" homekit_server_config_t config; 53 | extern "C" homekit_characteristic_t cha_programmable_switch_event; 54 | 55 | #define PIN_BUTTON 0 // Use the Flash-Button of NodeMCU 56 | 57 | #define HOMEKIT_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS 0 58 | #define HOMEKIT_PROGRAMMABLE_SWITCH_EVENT_DOUBLE_PRESS 1 59 | #define HOMEKIT_PROGRAMMABLE_SWITCH_EVENT_LONG_PRESS 2 60 | 61 | // Called when the value is read by iOS Home APP 62 | homekit_value_t cha_programmable_switch_event_getter() { 63 | // Should always return "null" for reading, see HAP section 9.75 64 | return HOMEKIT_NULL_CPP(); 65 | } 66 | 67 | void my_homekit_setup() { 68 | pinMode(PIN_BUTTON, INPUT_PULLUP); 69 | ESPButton.add(0, PIN_BUTTON, LOW, true, true); 70 | ESPButton.setCallback([&](uint8_t id, ESPButtonEvent event) { 71 | // Only one button is added, no need to check the id. 72 | LOG_D("Button Event: %s", ESPButton.getButtonEventDescription(event)); 73 | uint8_t cha_value = 0; 74 | if (event == ESPBUTTONEVENT_SINGLECLICK) { 75 | cha_value = HOMEKIT_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS; 76 | } else if (event == ESPBUTTONEVENT_DOUBLECLICK) { 77 | cha_value = HOMEKIT_PROGRAMMABLE_SWITCH_EVENT_DOUBLE_PRESS; 78 | } else if (event == ESPBUTTONEVENT_LONGCLICK) { 79 | cha_value = HOMEKIT_PROGRAMMABLE_SWITCH_EVENT_LONG_PRESS; 80 | } 81 | cha_programmable_switch_event.value.uint8_value = cha_value; 82 | homekit_characteristic_notify(&cha_programmable_switch_event, 83 | cha_programmable_switch_event.value); 84 | }); 85 | ESPButton.begin(); 86 | 87 | cha_programmable_switch_event.getter = cha_programmable_switch_event_getter; 88 | arduino_homekit_setup(&config); 89 | } 90 | 91 | static uint32_t next_heap_millis = 0; 92 | 93 | void my_homekit_loop() { 94 | ESPButton.loop(); 95 | arduino_homekit_loop(); 96 | const uint32_t t = millis(); 97 | if (t > next_heap_millis) { 98 | // Show heap info every 5 seconds 99 | next_heap_millis = t + 5 * 1000; 100 | LOG_D("Free heap: %d, HomeKit clients: %d", 101 | ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); 102 | 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /examples/Example03_StatelessProgrammableSwitch/my_accessory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * my_accessory.c 3 | * Define the accessory in C language using the Macro in characteristics.h 4 | * 5 | * Created on: 2020-05-16 6 | * Author: Mixiaoxiao (Wang Bin) 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | void my_accessory_identify(homekit_value_t _value) { 13 | printf("accessory identify\n"); 14 | } 15 | 16 | // Stateless Programmable Switch (HAP section 8.37) 17 | // required: PROGRAMMABLE_SWITCH_EVENT 18 | // optional: NAME, SERVICE_LABEL_INDEX 19 | 20 | // format: uint8; HAP section 9.75; write the .getter function and always return "null" for reading 21 | homekit_characteristic_t cha_programmable_switch_event = HOMEKIT_CHARACTERISTIC_(PROGRAMMABLE_SWITCH_EVENT, 0); 22 | 23 | homekit_accessory_t *accessories[] = { 24 | HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_switch, .services=(homekit_service_t*[]) { 25 | HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) { 26 | HOMEKIT_CHARACTERISTIC(NAME, "Stateless Programmable Switch"), 27 | HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"), 28 | HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"), 29 | HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"), 30 | HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"), 31 | HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify), 32 | NULL 33 | }), 34 | HOMEKIT_SERVICE(STATELESS_PROGRAMMABLE_SWITCH, .primary=true, .characteristics=(homekit_characteristic_t*[]){ 35 | HOMEKIT_CHARACTERISTIC(NAME, "Stateless Programmable Switch"), 36 | &cha_programmable_switch_event, 37 | NULL 38 | }), 39 | NULL 40 | }), 41 | NULL 42 | }; 43 | 44 | homekit_server_config_t config = { 45 | .accessories = accessories, 46 | .password = "111-11-111" 47 | }; 48 | 49 | 50 | -------------------------------------------------------------------------------- /examples/Example03_StatelessProgrammableSwitch/wifi_info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * wifi_info.h 3 | * 4 | * Created on: 2020-05-15 5 | * Author: Mixiaoxiao (Wang Bin) 6 | */ 7 | 8 | #ifndef WIFI_INFO_H_ 9 | #define WIFI_INFO_H_ 10 | 11 | #if defined(ESP8266) 12 | #include 13 | #elif defined(ESP32) 14 | #include 15 | #endif 16 | 17 | const char *ssid = "your-ssid"; 18 | const char *password = "your-password"; 19 | 20 | void wifi_connect() { 21 | WiFi.persistent(false); 22 | WiFi.mode(WIFI_STA); 23 | WiFi.setAutoReconnect(true); 24 | WiFi.begin(ssid, password); 25 | Serial.println("WiFi connecting..."); 26 | while (!WiFi.isConnected()) { 27 | delay(100); 28 | Serial.print("."); 29 | } 30 | Serial.print("\n"); 31 | Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str()); 32 | } 33 | 34 | #endif /* WIFI_INFO_H_ */ 35 | -------------------------------------------------------------------------------- /examples/Example04_MultipleAccessories/Example04_MultipleAccessories.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * multiple_accessories.ino 3 | * 4 | * Created on: 2020-05-16 5 | * Author: Mixiaoxiao (Wang Bin) 6 | * 7 | * 8 | * This example is a bridge (aka a gateway) which contains multiple accessories. 9 | * 10 | * This example includes 6 sensors: 11 | * 1. Temperature Sensor (HAP section 8.41) 12 | * 2. Humidity Sensor (HAP section 8.20) 13 | * 3. Light Sensor (HAP section 8.24) 14 | * 4. Contact Sensor (HAP section 8.9) 15 | * 5. Motion Sensor (HAP section 8.28) 16 | * 6. Occupancy Sensor (HAP section 8.29) 17 | * 18 | * You should: 19 | * 1. read and use the Example01_TemperatureSensor with detailed comments 20 | * to know the basic concept and usage of this library before other examples。 21 | * 2. erase the full flash or call homekit_storage_reset() in setup() 22 | * to remove the previous HomeKit pairing storage and 23 | * enable the pairing with the new accessory of this new HomeKit example. 24 | */ 25 | 26 | #include 27 | #include 28 | #include "wifi_info.h" 29 | 30 | #define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); 31 | 32 | void setup() { 33 | Serial.begin(115200); 34 | wifi_connect(); // in wifi_info.h 35 | //homekit_storage_reset(); // to remove the previous HomeKit pairing storage when you first run this new HomeKit example 36 | my_homekit_setup(); 37 | } 38 | 39 | void loop() { 40 | my_homekit_loop(); 41 | delay(10); 42 | } 43 | 44 | //============================== 45 | // HomeKit setup and loop 46 | //============================== 47 | 48 | extern "C" homekit_server_config_t config; 49 | extern "C" homekit_characteristic_t cha_temperature; 50 | extern "C" homekit_characteristic_t cha_humidity; 51 | extern "C" homekit_characteristic_t cha_light; 52 | extern "C" homekit_characteristic_t cha_contact; 53 | extern "C" homekit_characteristic_t cha_motion; 54 | extern "C" homekit_characteristic_t cha_occupancy; 55 | 56 | #define HOMEKIT_CONTACT_SENSOR_DETECTED 0 57 | #define HOMEKIT_CONTACT_SENSOR_NOT_DETECTED 1 58 | 59 | #define HOMEKIT_OCCUPANCY_DETECTED 0 60 | #define HOMEKIT_OCCUPANCY_NOT_DETECTED 1 61 | 62 | // Called when the value is read by iOS Home APP 63 | homekit_value_t cha_programmable_switch_event_getter() { 64 | // Should always return "null" for reading, see HAP section 9.75 65 | return HOMEKIT_NULL_CPP(); 66 | } 67 | 68 | void my_homekit_setup() { 69 | arduino_homekit_setup(&config); 70 | } 71 | 72 | static uint32_t next_heap_millis = 0; 73 | static uint32_t next_report_millis = 0; 74 | 75 | void my_homekit_loop() { 76 | arduino_homekit_loop(); 77 | const uint32_t t = millis(); 78 | if (t > next_report_millis) { 79 | // report sensor values every 10 seconds 80 | next_report_millis = t + 10 * 1000; 81 | my_homekit_report(); 82 | } 83 | if (t > next_heap_millis) { 84 | // Show heap info every 5 seconds 85 | next_heap_millis = t + 5 * 1000; 86 | LOG_D("Free heap: %d, HomeKit clients: %d", 87 | ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); 88 | 89 | } 90 | } 91 | 92 | void my_homekit_report() { 93 | // FIXME, read your real sensors here. 94 | float t = random_value(10, 30); 95 | float h = random_value(30, 70); 96 | float l = random_value(1, 10000); 97 | uint8_t c = random_value(0, 10) < 5 ? 98 | HOMEKIT_CONTACT_SENSOR_DETECTED : HOMEKIT_CONTACT_SENSOR_NOT_DETECTED; 99 | bool m = random_value(0, 10) < 5; 100 | uint8_t o = random_value(0, 10) < 5 ? 101 | HOMEKIT_OCCUPANCY_DETECTED : HOMEKIT_OCCUPANCY_NOT_DETECTED; 102 | 103 | cha_temperature.value.float_value = t; 104 | homekit_characteristic_notify(&cha_temperature, cha_temperature.value); 105 | 106 | cha_humidity.value.float_value = h; 107 | homekit_characteristic_notify(&cha_humidity, cha_humidity.value); 108 | 109 | cha_light.value.float_value = l; 110 | homekit_characteristic_notify(&cha_light, cha_light.value); 111 | 112 | cha_contact.value.uint8_value = c; 113 | homekit_characteristic_notify(&cha_contact, cha_contact.value); 114 | 115 | cha_motion.value.bool_value = m; 116 | homekit_characteristic_notify(&cha_motion, cha_motion.value); 117 | 118 | cha_occupancy.value.uint8_value = o; 119 | homekit_characteristic_notify(&cha_occupancy, cha_occupancy.value); 120 | 121 | LOG_D("t %.1f, h %.1f, l %.1f, c %u, m %u, o %u", t, h, l, c, (uint8_t)m, o); 122 | } 123 | 124 | int random_value(int min, int max) { 125 | return min + random(max - min); 126 | } 127 | -------------------------------------------------------------------------------- /examples/Example04_MultipleAccessories/wifi_info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * wifi_info.h 3 | * 4 | * Created on: 2020-05-15 5 | * Author: Mixiaoxiao (Wang Bin) 6 | */ 7 | 8 | #ifndef WIFI_INFO_H_ 9 | #define WIFI_INFO_H_ 10 | 11 | #if defined(ESP8266) 12 | #include 13 | #elif defined(ESP32) 14 | #include 15 | #endif 16 | 17 | const char *ssid = "your-ssid"; 18 | const char *password = "your-password"; 19 | 20 | void wifi_connect() { 21 | WiFi.persistent(false); 22 | WiFi.mode(WIFI_STA); 23 | WiFi.setAutoReconnect(true); 24 | WiFi.begin(ssid, password); 25 | Serial.println("WiFi connecting..."); 26 | while (!WiFi.isConnected()) { 27 | delay(100); 28 | Serial.print("."); 29 | } 30 | Serial.print("\n"); 31 | Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str()); 32 | } 33 | 34 | #endif /* WIFI_INFO_H_ */ 35 | -------------------------------------------------------------------------------- /examples/Example05_WS2812_Neopixel/my_accessory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * my_accessory.c 3 | * Define the accessory in C language using the Macro in characteristics.h 4 | * 5 | * Created on: 2020-05-15 6 | * Author: Mixiaoxiao (Wang Bin) 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | void my_accessory_identify(homekit_value_t _value) { 13 | printf("accessory identify\n"); 14 | } 15 | 16 | homekit_characteristic_t cha_on = HOMEKIT_CHARACTERISTIC_(ON, false); 17 | homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "ESP8266 Lamp"); 18 | homekit_characteristic_t cha_bright = HOMEKIT_CHARACTERISTIC_(BRIGHTNESS, 50); 19 | homekit_characteristic_t cha_sat = HOMEKIT_CHARACTERISTIC_(SATURATION, (float) 0); 20 | homekit_characteristic_t cha_hue = HOMEKIT_CHARACTERISTIC_(HUE, (float) 180); 21 | 22 | homekit_accessory_t *accessories[] = { 23 | HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_lightbulb, .services=(homekit_service_t*[]) { 24 | HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) { 25 | HOMEKIT_CHARACTERISTIC(NAME, "ESP8266 Lamp"), 26 | HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"), 27 | HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"), 28 | HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"), 29 | HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"), 30 | HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify), 31 | NULL 32 | }), 33 | HOMEKIT_SERVICE(LIGHTBULB, .primary=true, .characteristics=(homekit_characteristic_t*[]) { 34 | &cha_on, 35 | &cha_name, 36 | &cha_bright, 37 | &cha_sat, 38 | &cha_hue, 39 | NULL 40 | }), 41 | NULL 42 | }), 43 | NULL 44 | }; 45 | 46 | homekit_server_config_t accessory_config = { 47 | .accessories = accessories, 48 | .password = "111-11-111" 49 | }; -------------------------------------------------------------------------------- /examples/Example05_WS2812_Neopixel/wifi_info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * wifi_info.h 3 | * 4 | * Created on: 2020-05-15 5 | * Author: Mixiaoxiao (Wang Bin) 6 | */ 7 | 8 | #ifndef WIFI_INFO_H_ 9 | #define WIFI_INFO_H_ 10 | 11 | #if defined(ESP8266) 12 | #include 13 | #elif defined(ESP32) 14 | #include 15 | #endif 16 | 17 | const char *ssid = "your-ssid"; 18 | const char *password = "your-password"; 19 | 20 | void wifi_connect() { 21 | WiFi.persistent(false); 22 | WiFi.mode(WIFI_STA); 23 | WiFi.setAutoReconnect(true); 24 | WiFi.begin(ssid, password); 25 | Serial.println("WiFi connecting..."); 26 | while (!WiFi.isConnected()) { 27 | delay(100); 28 | Serial.print("."); 29 | } 30 | Serial.print("\n"); 31 | Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str()); 32 | } 33 | 34 | #endif /* WIFI_INFO_H_ */ 35 | -------------------------------------------------------------------------------- /examples/Example06_makesmart_lock/Example06_makesmart_lock.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * makesmart_lock.ino 3 | * 4 | * Created on: 2021-02-05 5 | * Author: cooper @ makesmart.net 6 | * Thank you for this great library! 7 | * 8 | * This example shows how to: 9 | * 1. define a lock accessory and its characteristics in my_accessory.c 10 | * 2. get the target-state sent from iOS Home APP. 11 | * 3. report the current-state value to HomeKit. 12 | * 13 | * you can use both: 14 | * void open_lock(){} 15 | * and 16 | * void close_lock(){} 17 | * 18 | * at the end of this file to let the lock-mechanism do whatever you want. 19 | * 20 | * 21 | * Pairing Code: 123-45-678 22 | * 23 | * 24 | * You should: 25 | * 1. read and use the Example01_TemperatureSensor with detailed comments 26 | * to know the basic concept and usage of this library before other examples。 27 | * 2. erase the full flash or call homekit_storage_reset() in setup() 28 | * to remove the previous HomeKit pairing storage and 29 | * enable the pairing with the new accessory of this new HomeKit example. 30 | * 31 | */ 32 | 33 | #include 34 | #include 35 | #include "wifi_info.h" 36 | 37 | #define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); 38 | 39 | void setup() { 40 | Serial.begin(115200); 41 | wifi_connect(); 42 | homekit_storage_reset(); 43 | my_homekit_setup(); 44 | } 45 | 46 | void loop() { 47 | my_homekit_loop(); 48 | delay(10); 49 | } 50 | 51 | //============================== 52 | // HomeKit setup and loop 53 | //============================== 54 | 55 | // access lock-mechanism HomeKit characteristics defined in my_accessory.c 56 | extern "C" homekit_server_config_t config; 57 | 58 | extern "C" homekit_characteristic_t cha_lock_current_state; 59 | extern "C" homekit_characteristic_t cha_lock_target_state; 60 | 61 | static uint32_t next_heap_millis = 0; 62 | 63 | 64 | // called when the lock-mechanism target-set is changed by iOS Home APP 65 | void set_lock(const homekit_value_t value) { 66 | 67 | uint8_t state = value.int_value; 68 | cha_lock_current_state.value.int_value = state; 69 | 70 | if(state == 0){ 71 | // lock-mechanism was unsecured by iOS Home APP 72 | open_lock(); 73 | } 74 | if(state == 1){ 75 | // lock-mechanism was secured by iOS Home APP 76 | close_lock(); 77 | } 78 | 79 | //report the lock-mechanism current-sate to HomeKit 80 | homekit_characteristic_notify(&cha_lock_current_state, cha_lock_current_state.value); 81 | 82 | } 83 | 84 | void my_homekit_setup() { 85 | 86 | cha_lock_target_state.setter = set_lock; 87 | arduino_homekit_setup(&config); 88 | 89 | 90 | } 91 | 92 | 93 | void my_homekit_loop() { 94 | arduino_homekit_loop(); 95 | const uint32_t t = millis(); 96 | if (t > next_heap_millis) { 97 | // show heap info every 30 seconds 98 | next_heap_millis = t + 30 * 1000; 99 | LOG_D("Free heap: %d, HomeKit clients: %d", 100 | ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); 101 | 102 | } 103 | } 104 | 105 | 106 | 107 | /* use this functions to let your lock mechanism do whatever yoi want */ 108 | void open_lock(){ 109 | Serial.println("unsecure"); 110 | // add your code here eg switch a relay or whatever 111 | } 112 | 113 | void close_lock(){ 114 | Serial.println("secure"); 115 | // add your code here eg switch a relay or whatever 116 | } 117 | -------------------------------------------------------------------------------- /examples/Example06_makesmart_lock/my_accessory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * my_accessory.c 3 | * Define the accessory in C language using the Macro in characteristics.h 4 | * 5 | * Created on: 2021-02-05 6 | * Author: cooper @ makesmart.net 7 | * Thank you for this great library! 8 | * 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | void my_accessory_identify(homekit_value_t _value) { 15 | printf("accessory identify\n"); 16 | } 17 | 18 | 19 | // characteristics of the lock-mechanism 20 | // format: uint8_t // 0 is unsecured // 1 is secured 21 | homekit_characteristic_t cha_lock_current_state = HOMEKIT_CHARACTERISTIC_(LOCK_CURRENT_STATE, 1); 22 | homekit_characteristic_t cha_lock_target_state = HOMEKIT_CHARACTERISTIC_(LOCK_TARGET_STATE, 1); 23 | 24 | homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "Lock"); 25 | 26 | homekit_accessory_t *accessories[] = { 27 | HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_door_lock, .services=(homekit_service_t*[]) { 28 | HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) { 29 | HOMEKIT_CHARACTERISTIC(NAME, "Lock"), 30 | HOMEKIT_CHARACTERISTIC(MANUFACTURER, "makesmart Community"), 31 | HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "1111111"), 32 | HOMEKIT_CHARACTERISTIC(MODEL, "makesmart Lock"), 33 | HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"), 34 | HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify), 35 | NULL 36 | }), 37 | HOMEKIT_SERVICE(LOCK_MECHANISM, .primary=true, .characteristics=(homekit_characteristic_t*[]){ 38 | &cha_lock_current_state, 39 | &cha_lock_target_state, 40 | &cha_name, 41 | NULL 42 | }), 43 | NULL 44 | }), 45 | NULL 46 | }; 47 | 48 | homekit_server_config_t config = { 49 | .accessories = accessories, 50 | .password = "123-45-678" 51 | }; 52 | -------------------------------------------------------------------------------- /examples/Example06_makesmart_lock/wifi_info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * wifi_info.h 3 | * 4 | * Created on: 2020-05-15 5 | * Author: Mixiaoxiao (Wang Bin) 6 | */ 7 | 8 | #ifndef WIFI_INFO_H_ 9 | #define WIFI_INFO_H_ 10 | 11 | #if defined(ESP8266) 12 | #include 13 | #elif defined(ESP32) 14 | #include 15 | #endif 16 | 17 | const char *ssid = "your-ssid"; 18 | const char *password = "your-password"; 19 | 20 | void wifi_connect() { 21 | WiFi.persistent(false); 22 | WiFi.mode(WIFI_STA); 23 | WiFi.setAutoReconnect(true); 24 | WiFi.begin(ssid, password); 25 | Serial.println("WiFi connecting..."); 26 | while (!WiFi.isConnected()) { 27 | delay(100); 28 | Serial.print("."); 29 | } 30 | Serial.print("\n"); 31 | Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str()); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /examples/legacy/simple_led/ButtonDebounce.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef BUTTON_DEBOUNCE_H_ 3 | #define BUTTON_DEBOUNCE_H_ 4 | 5 | #include 6 | #include 7 | #include //attachInterrupt可以传function 8 | 9 | //Ref debounce_time = 35 , dbTime=25 10 | //https://github.com/evert-arias/EasyButton/blob/master/src/EasyButton.h 11 | //https://github.com/JChristensen/JC_Button/blob/master/src/JC_Button.h 12 | 13 | 14 | /** 15 | * ButtonDebounce 16 | * WangBin 2019.12.26 17 | * 18 | * Example: 19 | * ButtonDebounce flashButton(D3, INPUT_PULLUP, LOW); 20 | * flashButton.setCallback(keyCallback); 21 | * flashButton.setInterrupt(interrupt_callback); // mark "IRAM_ATTR" on ESP32 22 | * call flashButton.update() in interrupt_callback 23 | * use flashButton.checkIsDown() to check the debounced state 24 | */ 25 | 26 | 27 | class ButtonDebounce { 28 | 29 | private: 30 | 31 | std::function callback; 32 | uint32_t lastchange_ms = 0; 33 | uint32_t debounce_ms = 35; 34 | bool laststate_is_down = false; // true is down; 35 | int pin_down_digital = LOW; 36 | int pin; 37 | 38 | bool readIsDown() { 39 | return pin_down_digital == digitalRead(pin); 40 | } 41 | 42 | public: 43 | //IRAM_ATTR 44 | ButtonDebounce(uint8_t pin, uint8_t pin_mode, uint8_t pin_down_digital, 45 | uint32_t debounce_ms = 35) : //冒号后赋值和this赋值等价 46 | pin(pin), pin_down_digital(pin_down_digital), debounce_ms(debounce_ms) { 47 | // callback(callback), 48 | //this->pin = pin; 49 | //this->pin_down_digital = pin_down_digital; 50 | //this->debounce_ms = debounce_ms; 51 | pinMode(pin, pin_mode); 52 | // if (attach_interrupt) { 53 | // using namespace std::placeholders; 54 | //// std::function ttt = &ButtonDebounce::buttonInterruptFunc; 55 | // std::function tvctt = std::bind(&ButtonDebounce::buttonInterruptFunc, this); 56 | //// std::function tvctt = &ButtonDebounce::buttonInterruptFunc; // @suppress("Invalid arguments") 57 | //// void (*ff)(void); 58 | //// ff = tvctt; 59 | //// ff = [](void) { 60 | //// ButtonDebounce::update(); 61 | //// }; 62 | // attachInterrupt(digitalPinToInterrupt(pin), 63 | // tvctt, CHANGE); 64 | // } 65 | 66 | } 67 | 68 | void update(bool down) { 69 | const uint32_t t = millis(); 70 | if (t - lastchange_ms < debounce_ms) { 71 | lastchange_ms = t; 72 | //debounce 73 | //Serial.println(F("[ButtonDebounce] debounce")); 74 | } else { 75 | if (laststate_is_down == down) { 76 | //same state 77 | //Serial.println(F("[ButtonDebounce] same state")); 78 | } else { //state changed, up->down or down->up 79 | lastchange_ms = t; 80 | laststate_is_down = down; 81 | if (callback) { 82 | callback(down); 83 | } 84 | } 85 | } 86 | } 87 | 88 | void update() { 89 | bool down = readIsDown(); 90 | update(down); 91 | } 92 | 93 | //返回当前debouce后的按钮状态 94 | bool checkIsDown(){ 95 | return laststate_is_down; 96 | } 97 | 98 | void setCallback(std::function callback) { 99 | this->callback = callback; 100 | } 101 | //interrupt_function 需标记为 IRAM_ATTR 102 | void setInterrupt(std::function interrupt_function) { 103 | if (interrupt_function) { 104 | attachInterrupt(digitalPinToInterrupt(pin), interrupt_function, CHANGE); 105 | } 106 | } 107 | 108 | }; 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /examples/legacy/simple_led/ButtonHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef BUTTONHANDLER_H_ 3 | #define BUTTONHANDLER_H_ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | enum button_event { 10 | BUTTON_EVENT_SINGLECLICK = 0, 11 | BUTTON_EVENT_DOUBLECLICK, 12 | BUTTON_EVENT_LONGCLICK 13 | }; 14 | /** 15 | * ButtonHandler 16 | * WangBin 2019.12.26 17 | * Example: 18 | * ButtonHandler handler; 19 | * handler.setCallback( 20 | [](const button_event event) { 21 | if (event == BUTTON_EVENT_SINGLECLICK) { 22 | ... 23 | } else if (event == BUTTON_EVENT_DOUBLECLICK) { 24 | ... 25 | } else if (event == BUTTON_EVENT_LONGCLICK) { 26 | ... 27 | } 28 | } 29 | ); 30 | handler.setIsDownFunction( 31 | [](void) { 32 | //return bool: the button is down or not 33 | return flashButton.checkIsDown(); 34 | }); 35 | 36 | //when the button state changed, call ~.handle(down or not) 37 | handler.handleChange(down); 38 | 39 | //call ~.loop() in Arduino's loop 40 | */ 41 | class ButtonHandler { 42 | 43 | private: 44 | std::function callback; 45 | std::function is_down_function; 46 | 47 | bool longclicked = false; //用来保证一次按下只能触发一次长按 48 | bool down_handled = false; //标志着是否已经处理了该次down按下事件(比如已经触发了长按) 49 | //down->up,在规定时间内再次down就是双击,否则超时就是单击 50 | bool wait_doubleclick = false; //标志着是否等待着双击事件 51 | uint32_t down_time = 0; //ms 52 | uint32_t up_time = 0; 53 | 54 | uint32_t longclick_threshold = 5000; 55 | uint32_t doubleclick_threshold = 200; //按下释放后在此时间间隔内又按下认为是双击 56 | 57 | bool longclick_enable = true; 58 | bool doubleclick_enable = true; 59 | 60 | public: 61 | ButtonHandler(uint32_t longclick_duration = 5000, uint32_t doubleclick_duration = 200) : 62 | longclick_threshold(longclick_duration), 63 | doubleclick_threshold(doubleclick_duration) { 64 | } 65 | 66 | void setCallback(std::function callback) { 67 | this->callback = callback; 68 | } 69 | 70 | void setIsDownFunction(std::function is_down_function) { 71 | this->is_down_function = is_down_function; 72 | } 73 | 74 | void setLongClickEnable(bool enable) { 75 | longclick_enable = enable; 76 | } 77 | void setDoubleClickEnable(bool enable) { 78 | doubleclick_enable = enable; 79 | } 80 | //当pin点评改变时调用 81 | void handleChange(bool down) { 82 | //单击:按下释放且释放一段时间内没有第二次按下 83 | //双击:按下释放且释放一段时间内执行第二次按下时触发 84 | //长按:按下一段时间内未释放 85 | if (down) { //down 86 | if (wait_doubleclick && doubleclick_enable) { 87 | //规定时间内第二次down了,认为是双击 88 | //亲测,一般情况下我的双击up->第二次down的间隔是80~100左右 89 | //Serial.println(String("doubleclick->duration=") + (millis() - up_time)); 90 | down_handled = true; 91 | //key2DoDoubleClick(); 92 | if (callback) { 93 | callback(BUTTON_EVENT_DOUBLECLICK); 94 | } 95 | } else { 96 | //第一次按下 97 | down_handled = false; 98 | } 99 | down_time = millis(); 100 | longclicked = false; 101 | wait_doubleclick = false; 102 | } else { //up 103 | if (!down_handled) { 104 | if (doubleclick_enable) { 105 | //在loop中延时等待第二次按下 106 | up_time = millis(); 107 | wait_doubleclick = true; 108 | } else { 109 | down_handled = true; 110 | if (callback) { 111 | callback(BUTTON_EVENT_SINGLECLICK); 112 | } 113 | } 114 | } 115 | } 116 | } 117 | 118 | void loop() { 119 | bool down = is_down_function(); 120 | if (down) { 121 | if (longclick_enable) { 122 | if (!longclicked && !down_handled) { 123 | if (millis() - down_time > longclick_threshold) { 124 | //key2DoLongClick(); 125 | longclicked = true; 126 | down_handled = true; 127 | if (callback) { 128 | callback(BUTTON_EVENT_LONGCLICK); 129 | } 130 | } 131 | } 132 | } 133 | } else { //up 134 | longclicked = false; 135 | if (wait_doubleclick && doubleclick_enable) { 136 | if (millis() - up_time > doubleclick_threshold) { 137 | wait_doubleclick = false; 138 | down_handled = true; 139 | //key2DoClick(); 140 | if (callback) { 141 | callback(BUTTON_EVENT_SINGLECLICK); 142 | } 143 | } 144 | } 145 | 146 | } 147 | } 148 | }; 149 | 150 | #endif /* BUTTONHANDLER_H_ */ 151 | 152 | -------------------------------------------------------------------------------- /examples/legacy/simple_led/simple_led.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * simple_led.ino 3 | * 4 | * This accessory contains a builtin-led on NodeMCU and a "virtual" occupancy sensor. 5 | * Setup code: 111-11-111 6 | * The Flash-Button(D3, GPIO0) on NodeMCU: 7 | * single-click: turn on/off the builtin-led (D4, GPIO2) 8 | * double-click: toggle the occupancy sensor state 9 | * long-click: reset the homekit server (remove the saved pairing) 10 | * 11 | * Created on: 2020-02-08 12 | * Author: Mixiaoxiao (Wang Bin) 13 | */ 14 | 15 | #include 16 | #include 17 | 18 | #include 19 | #include "ButtonDebounce.h" 20 | #include "ButtonHandler.h" 21 | 22 | //D0 16 //led 23 | //D3 0 //flash button 24 | //D4 2 //led 25 | 26 | #define PIN_LED 16//D0 27 | 28 | const char *ssid = "your-ssid"; 29 | const char *password = "your-password"; 30 | 31 | #define SIMPLE_INFO(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); 32 | 33 | void blink_led(int interval, int count) { 34 | for (int i = 0; i < count; i++) { 35 | builtinledSetStatus(true); 36 | delay(interval); 37 | builtinledSetStatus(false); 38 | delay(interval); 39 | } 40 | } 41 | 42 | void setup() { 43 | Serial.begin(115200); 44 | Serial.setRxBufferSize(32); 45 | Serial.setDebugOutput(false); 46 | 47 | pinMode(PIN_LED, OUTPUT); 48 | WiFi.mode(WIFI_STA); 49 | WiFi.persistent(false); 50 | WiFi.disconnect(false); 51 | WiFi.setAutoReconnect(true); 52 | WiFi.begin(ssid, password); 53 | 54 | SIMPLE_INFO(""); 55 | SIMPLE_INFO("SketchSize: %d", ESP.getSketchSize()); 56 | SIMPLE_INFO("FreeSketchSpace: %d", ESP.getFreeSketchSpace()); 57 | SIMPLE_INFO("FlashChipSize: %d", ESP.getFlashChipSize()); 58 | SIMPLE_INFO("FlashChipRealSize: %d", ESP.getFlashChipRealSize()); 59 | SIMPLE_INFO("FlashChipSpeed: %d", ESP.getFlashChipSpeed()); 60 | SIMPLE_INFO("SdkVersion: %s", ESP.getSdkVersion()); 61 | SIMPLE_INFO("FullVersion: %s", ESP.getFullVersion().c_str()); 62 | SIMPLE_INFO("CpuFreq: %dMHz", ESP.getCpuFreqMHz()); 63 | SIMPLE_INFO("FreeHeap: %d", ESP.getFreeHeap()); 64 | SIMPLE_INFO("ResetInfo: %s", ESP.getResetInfo().c_str()); 65 | SIMPLE_INFO("ResetReason: %s", ESP.getResetReason().c_str()); 66 | INFO_HEAP(); 67 | homekit_setup(); 68 | INFO_HEAP(); 69 | blink_led(200, 3); 70 | } 71 | 72 | void loop() { 73 | homekit_loop(); 74 | } 75 | 76 | void builtinledSetStatus(bool on) { 77 | digitalWrite(PIN_LED, on ? LOW : HIGH); 78 | } 79 | 80 | //============================== 81 | // Homekit setup and loop 82 | //============================== 83 | 84 | extern "C" homekit_server_config_t config; 85 | extern "C" homekit_characteristic_t name; 86 | extern "C" void occupancy_toggle(); 87 | extern "C" void led_toggle(); 88 | extern "C" void accessory_init(); 89 | 90 | ButtonDebounce btn(0, INPUT_PULLUP, LOW); 91 | ButtonHandler btnHandler; 92 | 93 | void IRAM_ATTR btnInterrupt() { 94 | btn.update(); 95 | } 96 | 97 | void homekit_setup() { 98 | accessory_init(); 99 | uint8_t mac[WL_MAC_ADDR_LENGTH]; 100 | WiFi.macAddress(mac); 101 | int name_len = snprintf(NULL, 0, "%s_%02X%02X%02X", 102 | name.value.string_value, mac[3], mac[4], mac[5]); 103 | char *name_value = (char*) malloc(name_len + 1); 104 | snprintf(name_value, name_len + 1, "%s_%02X%02X%02X", 105 | name.value.string_value, mac[3], mac[4], mac[5]); 106 | name.value = HOMEKIT_STRING_CPP(name_value); 107 | 108 | arduino_homekit_setup(&config); 109 | 110 | btn.setCallback(std::bind(&ButtonHandler::handleChange, &btnHandler, 111 | std::placeholders::_1)); 112 | btn.setInterrupt(btnInterrupt); 113 | btnHandler.setIsDownFunction(std::bind(&ButtonDebounce::checkIsDown, &btn)); 114 | btnHandler.setCallback([](button_event e) { 115 | if (e == BUTTON_EVENT_SINGLECLICK) { 116 | SIMPLE_INFO("Button Event: SINGLECLICK"); 117 | led_toggle(); 118 | } else if (e == BUTTON_EVENT_DOUBLECLICK) { 119 | SIMPLE_INFO("Button Event: DOUBLECLICK"); 120 | occupancy_toggle(); 121 | } else if (e == BUTTON_EVENT_LONGCLICK) { 122 | SIMPLE_INFO("Button Event: LONGCLICK"); 123 | SIMPLE_INFO("Rebooting..."); 124 | homekit_storage_reset(); 125 | ESP.restart(); // or system_restart(); 126 | } 127 | }); 128 | } 129 | 130 | void homekit_loop() { 131 | btnHandler.loop(); 132 | arduino_homekit_loop(); 133 | static uint32_t next_heap_millis = 0; 134 | uint32_t time = millis(); 135 | if (time > next_heap_millis) { 136 | SIMPLE_INFO("heap: %d, sockets: %d", 137 | ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); 138 | next_heap_millis = time + 5000; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /examples/legacy/simple_led/simple_led_accessory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * simple_led_accessory.c 3 | * Define the accessory in pure C language using the Macro in characteristics.h 4 | * 5 | * Created on: 2020-02-08 6 | * Author: Mixiaoxiao (Wang Bin) 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | //const char * buildTime = __DATE__ " " __TIME__ " GMT"; 17 | 18 | #define ACCESSORY_NAME ("ESP8266_LED") 19 | #define ACCESSORY_SN ("SN_0123456") //SERIAL_NUMBER 20 | #define ACCESSORY_MANUFACTURER ("Arduino Homekit") 21 | #define ACCESSORY_MODEL ("ESP8266") 22 | 23 | #define PIN_LED 2//D4 24 | 25 | int led_bri = 100; //[0, 100] 26 | bool led_power = false; //true or flase 27 | 28 | homekit_value_t led_on_get() { 29 | return HOMEKIT_BOOL(led_power); 30 | } 31 | 32 | void led_on_set(homekit_value_t value) { 33 | if (value.format != homekit_format_bool) { 34 | printf("Invalid on-value format: %d\n", value.format); 35 | return; 36 | } 37 | led_power = value.bool_value; 38 | if (led_power) { 39 | if (led_bri < 1) { 40 | led_bri = 100; 41 | } 42 | } 43 | led_update(); 44 | } 45 | 46 | homekit_value_t light_bri_get() { 47 | return HOMEKIT_INT(led_bri); 48 | } 49 | 50 | homekit_characteristic_t name = HOMEKIT_CHARACTERISTIC_(NAME, ACCESSORY_NAME); 51 | homekit_characteristic_t serial_number = HOMEKIT_CHARACTERISTIC_(SERIAL_NUMBER, ACCESSORY_SN); 52 | homekit_characteristic_t occupancy_detected = HOMEKIT_CHARACTERISTIC_(OCCUPANCY_DETECTED, 0); 53 | homekit_characteristic_t led_on = HOMEKIT_CHARACTERISTIC_(ON, false, 54 | //.callback=HOMEKIT_CHARACTERISTIC_CALLBACK(switch_on_callback), 55 | .getter=led_on_get, 56 | .setter=led_on_set 57 | ); 58 | 59 | void occupancy_toggle() { 60 | const uint8_t state = occupancy_detected.value.uint8_value; 61 | occupancy_detected.value = HOMEKIT_UINT8((!state) ? 1 : 0); 62 | homekit_characteristic_notify(&occupancy_detected, occupancy_detected.value); 63 | } 64 | 65 | void led_update() { 66 | if (led_power) { 67 | int pwm = PWMRANGE - (int) (led_bri * 1.0 * PWMRANGE / 100.0 + 0.5f); 68 | analogWrite(PIN_LED, pwm); 69 | printf("ON %3d (pwm: %4d of %d)\n", led_bri, pwm, PWMRANGE); 70 | } else { 71 | printf("OFF\n"); 72 | digitalWrite(PIN_LED, HIGH); 73 | } 74 | } 75 | 76 | void led_bri_set(homekit_value_t value) { 77 | if (value.format != homekit_format_int) { 78 | return; 79 | } 80 | led_bri = value.int_value; 81 | led_update(); 82 | } 83 | 84 | void led_toggle() { 85 | led_on.value.bool_value = !led_on.value.bool_value; 86 | led_on.setter(led_on.value); 87 | homekit_characteristic_notify(&led_on, led_on.value); 88 | } 89 | 90 | void accessory_identify(homekit_value_t _value) { 91 | printf("accessory identify\n"); 92 | for (int j = 0; j < 3; j++) { 93 | led_power = true; 94 | led_update(); 95 | delay(100); 96 | led_power = false; 97 | led_update(); 98 | delay(100); 99 | } 100 | } 101 | 102 | homekit_accessory_t *accessories[] = 103 | { 104 | HOMEKIT_ACCESSORY( 105 | .id = 1, 106 | .category = homekit_accessory_category_lightbulb, 107 | .services=(homekit_service_t*[]){ 108 | HOMEKIT_SERVICE(ACCESSORY_INFORMATION, 109 | .characteristics=(homekit_characteristic_t*[]){ 110 | &name, 111 | HOMEKIT_CHARACTERISTIC(MANUFACTURER, ACCESSORY_MANUFACTURER), 112 | &serial_number, 113 | HOMEKIT_CHARACTERISTIC(MODEL, ACCESSORY_MODEL), 114 | HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.0.9"), 115 | HOMEKIT_CHARACTERISTIC(IDENTIFY, accessory_identify), 116 | NULL 117 | }), 118 | HOMEKIT_SERVICE(LIGHTBULB, .primary=true, 119 | .characteristics=(homekit_characteristic_t*[]){ 120 | HOMEKIT_CHARACTERISTIC(NAME, "Led"), 121 | &led_on, 122 | HOMEKIT_CHARACTERISTIC(BRIGHTNESS, 100, .getter=light_bri_get, .setter=led_bri_set), 123 | NULL 124 | }), 125 | HOMEKIT_SERVICE(OCCUPANCY_SENSOR, .primary=false, .characteristics=(homekit_characteristic_t*[]) { 126 | HOMEKIT_CHARACTERISTIC(NAME, "Occupancy"), 127 | &occupancy_detected, 128 | NULL 129 | }), 130 | NULL 131 | }), 132 | NULL 133 | }; 134 | 135 | homekit_server_config_t config = { 136 | .accessories = accessories, 137 | .password = "111-11-111", 138 | //.on_event = on_homekit_event, 139 | .setupId = "ABCD" 140 | }; 141 | 142 | void accessory_init() { 143 | pinMode(PIN_LED, OUTPUT); 144 | led_update(); 145 | } 146 | 147 | -------------------------------------------------------------------------------- /examples/legacy/simplest_led/simplest_led.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * simple_led.ino 3 | * 4 | * This accessory contains a builtin-led on ESP8266 5 | * Setup code: 111-11-111 6 | * The Flash-Button(D3, GPIO0) on NodeMCU: 7 | * 8 | * Created on: 2020-02-08 9 | * Author: Mixiaoxiao (Wang Bin) 10 | * Edited on: 2020-03-01 11 | * Edited by: euler271 (Jonas Linn) 12 | */ 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | 19 | #define PL(s) Serial.println(s) 20 | #define P(s) Serial.print(s) 21 | 22 | //D0 16 //led 23 | //D3 0 //flash button 24 | //D4 2 //led 25 | 26 | #define PIN_LED 16//D0 27 | 28 | const char *ssid = "your-ssid"; 29 | const char *password = "your-password"; 30 | 31 | void blink_led(int interval, int count) { 32 | for (int i = 0; i < count; i++) { 33 | builtinledSetStatus(true); 34 | delay(interval); 35 | builtinledSetStatus(false); 36 | delay(interval); 37 | } 38 | } 39 | 40 | void setup() { 41 | Serial.begin(115200); 42 | Serial.setRxBufferSize(32); 43 | Serial.setDebugOutput(false); 44 | 45 | pinMode(PIN_LED, OUTPUT); 46 | WiFi.mode(WIFI_STA); 47 | WiFi.persistent(false); 48 | WiFi.disconnect(false); 49 | WiFi.setAutoReconnect(true); 50 | WiFi.begin(ssid, password); 51 | 52 | printf("\n"); 53 | printf("SketchSize: %d B\n", ESP.getSketchSize()); 54 | printf("FreeSketchSpace: %d B\n", ESP.getFreeSketchSpace()); 55 | printf("FlashChipSize: %d B\n", ESP.getFlashChipSize()); 56 | printf("FlashChipRealSize: %d B\n", ESP.getFlashChipRealSize()); 57 | printf("FlashChipSpeed: %d\n", ESP.getFlashChipSpeed()); 58 | printf("SdkVersion: %s\n", ESP.getSdkVersion()); 59 | printf("FullVersion: %s\n", ESP.getFullVersion().c_str()); 60 | printf("CpuFreq: %dMHz\n", ESP.getCpuFreqMHz()); 61 | printf("FreeHeap: %d B\n", ESP.getFreeHeap()); 62 | printf("ResetInfo: %s\n", ESP.getResetInfo().c_str()); 63 | printf("ResetReason: %s\n", ESP.getResetReason().c_str()); 64 | DEBUG_HEAP(); 65 | homekit_setup(); 66 | DEBUG_HEAP(); 67 | blink_led(200, 3); 68 | } 69 | 70 | void loop() { 71 | homekit_loop(); 72 | delay(5); 73 | } 74 | 75 | void builtinledSetStatus(bool on) { 76 | digitalWrite(PIN_LED, on ? LOW : HIGH); 77 | } 78 | 79 | //============================== 80 | // Homekit setup and loop 81 | //============================== 82 | 83 | extern "C" homekit_server_config_t config; 84 | extern "C" homekit_characteristic_t name; 85 | extern "C" void led_toggle(); 86 | extern "C" void accessory_init(); 87 | 88 | uint32_t next_heap_millis = 0; 89 | 90 | void homekit_setup() { 91 | accessory_init(); 92 | uint8_t mac[WL_MAC_ADDR_LENGTH]; 93 | WiFi.macAddress(mac); 94 | int name_len = snprintf(NULL, 0, "%s_%02X%02X%02X", name.value.string_value, mac[3], mac[4], mac[5]); 95 | char *name_value = (char*)malloc(name_len + 1); 96 | snprintf(name_value, name_len + 1, "%s_%02X%02X%02X", name.value.string_value, mac[3], mac[4], mac[5]); 97 | name.value = HOMEKIT_STRING_CPP(name_value); 98 | 99 | arduino_homekit_setup(&config); 100 | 101 | } 102 | 103 | void homekit_loop() { 104 | arduino_homekit_loop(); 105 | uint32_t time = millis(); 106 | if (time > next_heap_millis) { 107 | INFO("heap: %d, sockets: %d", ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); 108 | next_heap_millis = time + 5000; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /examples/legacy/simplest_led/simplest_led_accessory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * simple_led_accessory.c 3 | * Define the accessory in pure C language using the Macro in characteristics.h 4 | * 5 | * Created on: 2020-02-08 6 | * Author: Mixiaoxiao (Wang Bin) 7 | * Edited on: 2020-03-01 8 | * Edited by: euler271 (Jonas Linn) 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | //const char * buildTime = __DATE__ " " __TIME__ " GMT"; 19 | 20 | #define ACCESSORY_NAME ("ESP8266_LED") 21 | #define ACCESSORY_SN ("SN_0123456") //SERIAL_NUMBER 22 | #define ACCESSORY_MANUFACTURER ("Arduino Homekit") 23 | #define ACCESSORY_MODEL ("ESP8266") 24 | 25 | #define PIN_LED 2//D4 26 | 27 | bool led_power = false; //true or flase 28 | 29 | homekit_value_t led_on_get() { 30 | return HOMEKIT_BOOL(led_power); 31 | } 32 | 33 | void led_on_set(homekit_value_t value) { 34 | if (value.format != homekit_format_bool) { 35 | printf("Invalid on-value format: %d\n", value.format); 36 | return; 37 | } 38 | led_power = value.bool_value; 39 | led_update(); 40 | } 41 | 42 | homekit_characteristic_t name = HOMEKIT_CHARACTERISTIC_(NAME, ACCESSORY_NAME); 43 | homekit_characteristic_t serial_number = HOMEKIT_CHARACTERISTIC_(SERIAL_NUMBER, ACCESSORY_SN); 44 | homekit_characteristic_t led_on = HOMEKIT_CHARACTERISTIC_(ON, false, .getter=led_on_get, .setter=led_on_set); 45 | 46 | void led_update() { 47 | if (led_power) { 48 | printf("ON\n"); 49 | digitalWrite(PIN_LED, LOW); 50 | } else { 51 | printf("OFF\n"); 52 | digitalWrite(PIN_LED, HIGH); 53 | } 54 | } 55 | 56 | void led_toggle() { 57 | led_on.value.bool_value = !led_on.value.bool_value; 58 | led_on.setter(led_on.value); 59 | homekit_characteristic_notify(&led_on, led_on.value); 60 | } 61 | 62 | void accessory_identify(homekit_value_t _value) { 63 | printf("accessory identify\n"); 64 | for (int j = 0; j < 3; j++) { 65 | led_power = true; 66 | led_update(); 67 | delay(100); 68 | led_power = false; 69 | led_update(); 70 | delay(100); 71 | } 72 | } 73 | 74 | homekit_accessory_t *accessories[] = 75 | { 76 | HOMEKIT_ACCESSORY( 77 | .id = 1, 78 | .category = homekit_accessory_category_lightbulb, 79 | .services=(homekit_service_t*[]){ 80 | HOMEKIT_SERVICE(ACCESSORY_INFORMATION, 81 | .characteristics=(homekit_characteristic_t*[]){ 82 | &name, 83 | HOMEKIT_CHARACTERISTIC(MANUFACTURER, ACCESSORY_MANUFACTURER), 84 | &serial_number, 85 | HOMEKIT_CHARACTERISTIC(MODEL, ACCESSORY_MODEL), 86 | HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.0.1"), 87 | HOMEKIT_CHARACTERISTIC(IDENTIFY, accessory_identify), 88 | NULL 89 | }), 90 | HOMEKIT_SERVICE(LIGHTBULB, .primary=true, 91 | .characteristics=(homekit_characteristic_t*[]){ 92 | HOMEKIT_CHARACTERISTIC(NAME, "Led"), 93 | &led_on, 94 | NULL 95 | }), 96 | NULL 97 | }), 98 | NULL 99 | }; 100 | 101 | homekit_server_config_t config = { 102 | .accessories = accessories, 103 | .password = "111-11-111", 104 | //.on_event = on_homekit_event, 105 | .setupId = "ABCD" 106 | }; 107 | 108 | void accessory_init() { 109 | pinMode(PIN_LED, OUTPUT); 110 | led_update(); 111 | } 112 | -------------------------------------------------------------------------------- /extras/ESP8266WiFi_nossl_noleak/src/ESP8266WiFi.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ESP8266WiFi.cpp - WiFi library for esp8266 3 | 4 | Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. 5 | This file is part of the esp8266 core for Arduino environment. 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library; if not, write to the Free Software 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 | 21 | Reworked on 28 Dec 2015 by Markus Sattler 22 | 23 | */ 24 | 25 | #include "ESP8266WiFi.h" 26 | 27 | extern "C" { 28 | #include "c_types.h" 29 | #include "ets_sys.h" 30 | #include "os_type.h" 31 | #include "osapi.h" 32 | #include "mem.h" 33 | #include "user_interface.h" 34 | #include "smartconfig.h" 35 | #include "lwip/opt.h" 36 | #include "lwip/err.h" 37 | #include "lwip/dns.h" 38 | } 39 | 40 | #include "debug.h" 41 | 42 | // ----------------------------------------------------------------------------------------------------------------------- 43 | // ---------------------------------------------------------- Debug ------------------------------------------------------ 44 | // ----------------------------------------------------------------------------------------------------------------------- 45 | 46 | 47 | /** 48 | * Output WiFi settings to an object derived from Print interface (like Serial). 49 | * @param p Print interface 50 | */ 51 | void ESP8266WiFiClass::printDiag(Print& p) { 52 | const char* const modes[] = { "NULL", "STA", "AP", "STA+AP" }; 53 | p.print(F("Mode: ")); 54 | p.println(modes[wifi_get_opmode()]); 55 | 56 | const char* const phymodes[] = { "", "B", "G", "N" }; 57 | p.print(F("PHY mode: ")); 58 | p.println(phymodes[(int) wifi_get_phy_mode()]); 59 | 60 | p.print(F("Channel: ")); 61 | p.println(wifi_get_channel()); 62 | 63 | p.print(F("AP id: ")); 64 | p.println(wifi_station_get_current_ap_id()); 65 | 66 | p.print(F("Status: ")); 67 | p.println(wifi_station_get_connect_status()); 68 | 69 | p.print(F("Auto connect: ")); 70 | p.println(wifi_station_get_auto_connect()); 71 | 72 | struct station_config conf; 73 | wifi_station_get_config(&conf); 74 | 75 | char ssid[33]; //ssid can be up to 32chars, => plus null term 76 | memcpy(ssid, conf.ssid, sizeof(conf.ssid)); 77 | ssid[32] = 0; //nullterm in case of 32 char ssid 78 | p.printf_P(PSTR("SSID (%d): %s\n"), strlen(ssid), ssid); 79 | 80 | char passphrase[65]; 81 | memcpy(passphrase, conf.password, sizeof(conf.password)); 82 | passphrase[64] = 0; 83 | p.printf_P(PSTR("Passphrase (%d): %s\n"), strlen(passphrase), passphrase); 84 | 85 | p.print(F("BSSID set: ")); 86 | p.println(conf.bssid_set); 87 | 88 | } 89 | 90 | ESP8266WiFiClass WiFi; 91 | -------------------------------------------------------------------------------- /extras/ESP8266WiFi_nossl_noleak/src/ESP8266WiFi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ESP8266WiFi.h - esp8266 Wifi support. 3 | Based on WiFi.h from Arduino WiFi shield library. 4 | Copyright (c) 2011-2014 Arduino. All right reserved. 5 | Modified by Ivan Grokhotkov, December 2014 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library; if not, write to the Free Software 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 | */ 21 | 22 | #ifndef WiFi_h 23 | #define WiFi_h 24 | 25 | #include 26 | 27 | extern "C" { 28 | #include "include/wl_definitions.h" 29 | } 30 | 31 | #include "IPAddress.h" 32 | 33 | #include "ESP8266WiFiType.h" 34 | #include "ESP8266WiFiSTA.h" 35 | #include "ESP8266WiFiAP.h" 36 | #include "ESP8266WiFiScan.h" 37 | #include "ESP8266WiFiGeneric.h" 38 | 39 | #include "WiFiClient.h" 40 | #include "WiFiServer.h" 41 | //#include "WiFiServerSecure.h" 42 | //#include "WiFiClientSecure.h" 43 | //#include "BearSSLHelpers.h" 44 | //#include "CertStoreBearSSL.h" 45 | 46 | #ifdef DEBUG_ESP_WIFI 47 | #ifdef DEBUG_ESP_PORT 48 | #define DEBUG_WIFI(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ##__VA_ARGS__ ) 49 | #endif 50 | #endif 51 | 52 | #ifndef DEBUG_WIFI 53 | #define DEBUG_WIFI(...) do { (void)0; } while (0) 54 | #endif 55 | 56 | 57 | class ESP8266WiFiClass : public ESP8266WiFiGenericClass, public ESP8266WiFiSTAClass, public ESP8266WiFiScanClass, public ESP8266WiFiAPClass { 58 | public: 59 | 60 | // workaround same function name with different signature 61 | using ESP8266WiFiGenericClass::channel; 62 | 63 | using ESP8266WiFiSTAClass::SSID; 64 | using ESP8266WiFiSTAClass::RSSI; 65 | using ESP8266WiFiSTAClass::BSSID; 66 | using ESP8266WiFiSTAClass::BSSIDstr; 67 | 68 | using ESP8266WiFiScanClass::SSID; 69 | using ESP8266WiFiScanClass::encryptionType; 70 | using ESP8266WiFiScanClass::RSSI; 71 | using ESP8266WiFiScanClass::BSSID; 72 | using ESP8266WiFiScanClass::BSSIDstr; 73 | using ESP8266WiFiScanClass::channel; 74 | using ESP8266WiFiScanClass::isHidden; 75 | 76 | // ---------------------------------------------------------------------------------------------- 77 | // ------------------------------------------- Debug -------------------------------------------- 78 | // ---------------------------------------------------------------------------------------------- 79 | 80 | public: 81 | 82 | void printDiag(Print& dest); 83 | 84 | friend class WiFiClient; 85 | friend class WiFiServer; 86 | 87 | }; 88 | 89 | extern ESP8266WiFiClass WiFi; 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /extras/ESP8266WiFi_nossl_noleak/src/ESP8266WiFiAP.h: -------------------------------------------------------------------------------- 1 | /* 2 | ESP8266WiFiAP.h - esp8266 Wifi support. 3 | Based on WiFi.h from Arduino WiFi shield library. 4 | Copyright (c) 2011-2014 Arduino. All right reserved. 5 | Modified by Ivan Grokhotkov, December 2014 6 | Reworked by Markus Sattler, December 2015 7 | 8 | This library is free software; you can redistribute it and/or 9 | modify it under the terms of the GNU Lesser General Public 10 | License as published by the Free Software Foundation; either 11 | version 2.1 of the License, or (at your option) any later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | Lesser General Public License for more details. 17 | 18 | You should have received a copy of the GNU Lesser General Public 19 | License along with this library; if not, write to the Free Software 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 | */ 22 | 23 | #ifndef ESP8266WIFIAP_H_ 24 | #define ESP8266WIFIAP_H_ 25 | 26 | 27 | #include "ESP8266WiFiType.h" 28 | #include "ESP8266WiFiGeneric.h" 29 | 30 | 31 | class ESP8266WiFiAPClass { 32 | 33 | // ---------------------------------------------------------------------------------------------- 34 | // ----------------------------------------- AP function ---------------------------------------- 35 | // ---------------------------------------------------------------------------------------------- 36 | 37 | public: 38 | 39 | bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4); 40 | bool softAP(const String& ssid,const String& passphrase = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4); 41 | bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); 42 | bool softAPdisconnect(bool wifioff = false); 43 | 44 | uint8_t softAPgetStationNum(); 45 | 46 | IPAddress softAPIP(); 47 | 48 | uint8_t* softAPmacAddress(uint8_t* mac); 49 | String softAPmacAddress(void); 50 | 51 | String softAPSSID() const; 52 | String softAPPSK() const; 53 | 54 | protected: 55 | 56 | }; 57 | 58 | #endif /* ESP8266WIFIAP_H_*/ 59 | -------------------------------------------------------------------------------- /extras/ESP8266WiFi_nossl_noleak/src/ESP8266WiFiMulti.h: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @file ESP8266WiFiMulti.h 4 | * @date 16.05.2015 5 | * @author Markus Sattler 6 | * 7 | * Copyright (c) 2015 Markus Sattler. All rights reserved. 8 | * This file is part of the esp8266 core for Arduino environment. 9 | * 10 | * This library is free software; you can redistribute it and/or 11 | * modify it under the terms of the GNU Lesser General Public 12 | * License as published by the Free Software Foundation; either 13 | * version 2.1 of the License, or (at your option) any later version. 14 | * 15 | * This library is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | * Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public 21 | * License along with this library; if not, write to the Free Software 22 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 | * 24 | */ 25 | 26 | 27 | #ifndef WIFICLIENTMULTI_H_ 28 | #define WIFICLIENTMULTI_H_ 29 | 30 | #include "ESP8266WiFi.h" 31 | #include 32 | 33 | #ifdef DEBUG_ESP_WIFI 34 | #ifdef DEBUG_ESP_PORT 35 | #define DEBUG_WIFI_MULTI(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ##__VA_ARGS__ ) 36 | #endif 37 | #endif 38 | 39 | #ifndef DEBUG_WIFI_MULTI 40 | #define DEBUG_WIFI_MULTI(...) do { (void)0; } while (0) 41 | #endif 42 | 43 | struct WifiAPEntry { 44 | char * ssid; 45 | char * passphrase; 46 | }; 47 | 48 | typedef std::vector WifiAPlist; 49 | 50 | class ESP8266WiFiMulti { 51 | public: 52 | ESP8266WiFiMulti(); 53 | ~ESP8266WiFiMulti(); 54 | 55 | bool addAP(const char* ssid, const char *passphrase = NULL); 56 | bool existsAP(const char* ssid, const char *passphrase = NULL); 57 | 58 | wl_status_t run(void); 59 | 60 | void cleanAPlist(void); 61 | 62 | private: 63 | WifiAPlist APlist; 64 | bool APlistAdd(const char* ssid, const char *passphrase = NULL); 65 | bool APlistExists(const char* ssid, const char *passphrase = NULL); 66 | void APlistClean(void); 67 | 68 | }; 69 | 70 | #endif /* WIFICLIENTMULTI_H_ */ 71 | -------------------------------------------------------------------------------- /extras/ESP8266WiFi_nossl_noleak/src/ESP8266WiFiScan.h: -------------------------------------------------------------------------------- 1 | /* 2 | ESP8266WiFiScan.h - esp8266 Wifi support. 3 | Based on WiFi.h from Ardiono WiFi shield library. 4 | Copyright (c) 2011-2014 Arduino. All right reserved. 5 | Modified by Ivan Grokhotkov, December 2014 6 | Reworked by Markus Sattler, December 2015 7 | 8 | This library is free software; you can redistribute it and/or 9 | modify it under the terms of the GNU Lesser General Public 10 | License as published by the Free Software Foundation; either 11 | version 2.1 of the License, or (at your option) any later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | Lesser General Public License for more details. 17 | 18 | You should have received a copy of the GNU Lesser General Public 19 | License along with this library; if not, write to the Free Software 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 | */ 22 | 23 | #ifndef ESP8266WIFISCAN_H_ 24 | #define ESP8266WIFISCAN_H_ 25 | 26 | #include "ESP8266WiFiType.h" 27 | #include "ESP8266WiFiGeneric.h" 28 | 29 | class ESP8266WiFiScanClass { 30 | 31 | // ---------------------------------------------------------------------------------------------- 32 | // ----------------------------------------- scan function -------------------------------------- 33 | // ---------------------------------------------------------------------------------------------- 34 | 35 | public: 36 | 37 | int8_t scanNetworks(bool async = false, bool show_hidden = false, uint8 channel = 0, uint8* ssid = NULL); 38 | void scanNetworksAsync(std::function onComplete, bool show_hidden = false); 39 | 40 | int8_t scanComplete(); 41 | void scanDelete(); 42 | 43 | // scan result 44 | bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden); 45 | 46 | String SSID(uint8_t networkItem); 47 | uint8_t encryptionType(uint8_t networkItem); 48 | int32_t RSSI(uint8_t networkItem); 49 | uint8_t * BSSID(uint8_t networkItem); 50 | String BSSIDstr(uint8_t networkItem); 51 | int32_t channel(uint8_t networkItem); 52 | bool isHidden(uint8_t networkItem); 53 | 54 | protected: 55 | 56 | static bool _scanAsync; 57 | static bool _scanStarted; 58 | static bool _scanComplete; 59 | 60 | static size_t _scanCount; 61 | static void* _scanResult; 62 | 63 | static std::function _onComplete; 64 | 65 | static void _scanDone(void* result, int status); 66 | static void * _getScanInfoByIndex(int i); 67 | 68 | }; 69 | 70 | 71 | #endif /* ESP8266WIFISCAN_H_ */ 72 | -------------------------------------------------------------------------------- /extras/ESP8266WiFi_nossl_noleak/src/WiFiServer.h: -------------------------------------------------------------------------------- 1 | /* 2 | WiFiServer.h - Library for Arduino Wifi shield. 3 | Copyright (c) 2011-2014 Arduino LLC. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | Modified by Ivan Grokhotkov, December 2014 - esp8266 support 20 | */ 21 | 22 | #ifndef wifiserver_h 23 | #define wifiserver_h 24 | 25 | extern "C" { 26 | #include "include/wl_definitions.h" 27 | 28 | struct tcp_pcb; 29 | } 30 | 31 | #include "Server.h" 32 | #include "IPAddress.h" 33 | 34 | class ClientContext; 35 | class WiFiClient; 36 | 37 | class WiFiServer : public Server { 38 | // Secure server needs access to all the private entries here 39 | protected: 40 | uint16_t _port; 41 | IPAddress _addr; 42 | tcp_pcb* _pcb; 43 | 44 | ClientContext* _unclaimed; 45 | ClientContext* _discarded; 46 | enum { _ndDefault, _ndFalse, _ndTrue } _noDelay = _ndDefault; 47 | 48 | public: 49 | WiFiServer(const IPAddress& addr, uint16_t port); 50 | WiFiServer(uint16_t port); 51 | virtual ~WiFiServer() {} 52 | WiFiClient available(uint8_t* status = NULL); 53 | bool hasClient(); 54 | void begin(); 55 | void begin(uint16_t port); 56 | void setNoDelay(bool nodelay); 57 | bool getNoDelay(); 58 | virtual size_t write(uint8_t); 59 | virtual size_t write(const uint8_t *buf, size_t size); 60 | uint8_t status(); 61 | void close(); 62 | void stop(); 63 | 64 | using Print::write; 65 | using ClientType = WiFiClient; 66 | 67 | protected: 68 | long _accept(tcp_pcb* newpcb, long err); 69 | void _discard(ClientContext* client); 70 | 71 | static long _s_accept(void *arg, tcp_pcb* newpcb, long err); 72 | static void _s_discard(void* server, ClientContext* ctx); 73 | }; 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /extras/ESP8266WiFi_nossl_noleak/src/include/WiFiState.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef WIFISTATE_H_ 3 | #define WIFISTATE_H_ 4 | 5 | #include 6 | #include 7 | 8 | struct WiFiState 9 | { 10 | uint32_t crc; 11 | struct 12 | { 13 | station_config fwconfig; 14 | ip_info ip; 15 | ip_addr_t dns[2]; 16 | ip_addr_t ntp[2]; 17 | WiFiMode_t mode; 18 | uint8_t channel; 19 | bool persistent; 20 | } state; 21 | }; 22 | 23 | #endif // WIFISTATE_H_ 24 | -------------------------------------------------------------------------------- /extras/ESP8266WiFi_nossl_noleak/src/include/slist.h: -------------------------------------------------------------------------------- 1 | #ifndef SLIST_H 2 | #define SLIST_H 3 | 4 | template 5 | class SList { 6 | public: 7 | SList() : _next(0) { } 8 | 9 | protected: 10 | 11 | static void _add(T* self) { 12 | T* tmp = _s_first; 13 | _s_first = self; 14 | self->_next = tmp; 15 | } 16 | 17 | static void _remove(T* self) { 18 | if (_s_first == self) { 19 | _s_first = self->_next; 20 | self->_next = 0; 21 | return; 22 | } 23 | 24 | for (T* prev = _s_first; prev->_next; prev = prev->_next) { 25 | if (prev->_next == self) { 26 | prev->_next = self->_next; 27 | self->_next = 0; 28 | return; 29 | } 30 | } 31 | } 32 | 33 | static T* _s_first; 34 | T* _next; 35 | }; 36 | 37 | 38 | #endif //SLIST_H 39 | -------------------------------------------------------------------------------- /extras/ESP8266WiFi_nossl_noleak/src/include/wl_definitions.h: -------------------------------------------------------------------------------- 1 | /* 2 | wl_definitions.h - Library for Arduino Wifi shield. 3 | Copyright (c) 2011-2014 Arduino. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | /* 20 | * wl_definitions.h 21 | * 22 | * Created on: Mar 6, 2011 23 | * Author: dlafauci 24 | */ 25 | 26 | #ifndef WL_DEFINITIONS_H_ 27 | #define WL_DEFINITIONS_H_ 28 | 29 | // Maximum size of a SSID 30 | #define WL_SSID_MAX_LENGTH 32 31 | // Length of passphrase. Valid lengths are 8-63. 32 | #define WL_WPA_KEY_MAX_LENGTH 63 33 | // Length of key in bytes. Valid values are 5 and 13. 34 | #define WL_WEP_KEY_MAX_LENGTH 13 35 | // Size of a MAC-address or BSSID 36 | #define WL_MAC_ADDR_LENGTH 6 37 | // Size of a MAC-address or BSSID 38 | #define WL_IPV4_LENGTH 4 39 | // Maximum size of a SSID list 40 | #define WL_NETWORKS_LIST_MAXNUM 10 41 | // Maxmium number of socket 42 | #define MAX_SOCK_NUM 4 43 | // Socket not available constant 44 | #define SOCK_NOT_AVAIL 255 45 | // Default state value for Wifi state field 46 | #define NA_STATE -1 47 | //Maximum number of attempts to establish wifi connection 48 | #define WL_MAX_ATTEMPT_CONNECTION 10 49 | 50 | typedef enum { 51 | WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library 52 | WL_IDLE_STATUS = 0, 53 | WL_NO_SSID_AVAIL = 1, 54 | WL_SCAN_COMPLETED = 2, 55 | WL_CONNECTED = 3, 56 | WL_CONNECT_FAILED = 4, 57 | WL_CONNECTION_LOST = 5, 58 | WL_DISCONNECTED = 6 59 | } wl_status_t; 60 | 61 | /* Encryption modes */ 62 | enum wl_enc_type { /* Values map to 802.11 encryption suites... */ 63 | ENC_TYPE_WEP = 5, 64 | ENC_TYPE_TKIP = 2, 65 | ENC_TYPE_CCMP = 4, 66 | /* ... except these two, 7 and 8 are reserved in 802.11-2007 */ 67 | ENC_TYPE_NONE = 7, 68 | ENC_TYPE_AUTO = 8 69 | }; 70 | 71 | #if !defined(LWIP_INTERNAL) && !defined(__LWIP_TCP_H__) 72 | enum wl_tcp_state { 73 | CLOSED = 0, 74 | LISTEN = 1, 75 | SYN_SENT = 2, 76 | SYN_RCVD = 3, 77 | ESTABLISHED = 4, 78 | FIN_WAIT_1 = 5, 79 | FIN_WAIT_2 = 6, 80 | CLOSE_WAIT = 7, 81 | CLOSING = 8, 82 | LAST_ACK = 9, 83 | TIME_WAIT = 10 84 | }; 85 | #endif 86 | 87 | #endif /* WL_DEFINITIONS_H_ */ 88 | -------------------------------------------------------------------------------- /extras/preview.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mixiaoxiao/Arduino-HomeKit-ESP8266/8a8e1a065005e9252d728b24f96f6d0b29993f67/extras/preview.jpg -------------------------------------------------------------------------------- /extras/qq_group_qrcode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mixiaoxiao/Arduino-HomeKit-ESP8266/8a8e1a065005e9252d728b24f96f6d0b29993f67/extras/qq_group_qrcode.png -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"HomeKit-ESP8266", 3 | "description":"Apple HomeKit accessory server library for the ESP8266 Arduino core.", 4 | "keywords":"homekit,esp8266,esp32,apple homekit", 5 | "authors": 6 | { 7 | "name": "Mixiaoxiao (Wang Bin)", 8 | "maintainer": true 9 | }, 10 | "repository": 11 | { 12 | "type": "git", 13 | "url": "https://github.com/Mixiaoxiao/Arduino-HomeKit-ESP8266.git" 14 | }, 15 | "version": "1.2.0", 16 | "license": "MIT", 17 | "frameworks": "arduino", 18 | "platforms": "espressif8266", 19 | "build": { 20 | "libCompatMode": 2 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=HomeKit-ESP8266 2 | version=1.2.0 3 | author=Mixiaoxiao 4 | maintainer=Mixiaoxiao 5 | sentence=Native Apple HomeKit accessory implementation for the ESP8266 Arduino core. 6 | paragraph=Native Apple HomeKit Accessory Implementation for the ESP8266 Arduino core. 7 | category=Communication 8 | url=https://github.com/Mixiaoxiao/Arduino-HomeKit-ESP8266.git 9 | architectures=esp8266 10 | -------------------------------------------------------------------------------- /src/base64.c: -------------------------------------------------------------------------------- 1 | #include "base64.h" 2 | 3 | static unsigned char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 4 | 5 | unsigned char base64_encode_char(unsigned char c) { 6 | return base64_chars[c]; 7 | } 8 | 9 | unsigned char base64_decode_char(unsigned char c) { 10 | if (c >= 'A' && c <= 'Z') 11 | return c - 'A'; 12 | if (c >= 'a' && c <= 'z') 13 | return c - 'a' + 26; 14 | if (c >= '0' && c <= '9') 15 | return c - '0' + 52; 16 | if (c == '+') 17 | return 62; 18 | if (c == '/') 19 | return 63; 20 | if (c == '=') 21 | return 64; 22 | 23 | return 0; 24 | } 25 | 26 | size_t base64_encoded_size(const unsigned char *data, size_t size) { 27 | return (size + 2)/3*4; 28 | } 29 | 30 | size_t base64_decoded_size(const unsigned char *encoded_data, size_t encoded_size) { 31 | size_t size = (encoded_size + 3)/4*3; 32 | if (encoded_data[encoded_size-1] == '=') 33 | size--; 34 | if (encoded_data[encoded_size-2] == '=') 35 | size--; 36 | return size; 37 | } 38 | 39 | int base64_encode_(const unsigned char* data, size_t size, unsigned char *encoded_data) { 40 | size_t i=0, j=0, size1=size - size%3; 41 | for (; i>2); 43 | encoded_data[j+1] = base64_encode_char(((data[i+0]&0x3)<<4) + (data[i+1]>>4)); 44 | encoded_data[j+2] = base64_encode_char(((data[i+1]&0xF)<<2) + (data[i+2]>>6)); 45 | encoded_data[j+3] = base64_encode_char(data[i+2]&0x3F); 46 | } 47 | 48 | if (size % 3 == 1) { 49 | encoded_data[j+0] = base64_encode_char(data[i+0]>>2); 50 | encoded_data[j+1] = base64_encode_char((data[i+0]&0x3)<<4); 51 | encoded_data[j+2] = encoded_data[j+3] = '='; 52 | j += 4; 53 | } else if (size % 3 == 2) { 54 | encoded_data[j+0] = base64_encode_char(data[i+0]>>2); 55 | encoded_data[j+1] = base64_encode_char(((data[i+0]&0x3)<<4) + (data[i+1]>>4)); 56 | encoded_data[j+2] = base64_encode_char(((data[i+1]&0xF)<<2)); 57 | encoded_data[j+3] = '='; 58 | j += 4; 59 | } 60 | 61 | return j; 62 | } 63 | 64 | int base64_decode_(const unsigned char* encoded_data, size_t encoded_size, unsigned char *data) { 65 | if (encoded_size % 4 != 0) 66 | return -1; 67 | 68 | size_t i=0, j=0; 69 | for (; i>4); 75 | if (block[2] == 64) 76 | break; 77 | 78 | data[j++] = ((block[1]&0xF)<<4) + (block[2]>>2); 79 | if (block[3] == 64) 80 | break; 81 | 82 | data[j++] = ((block[2]&0x3)<<6) + block[3]; 83 | } 84 | 85 | return j; 86 | } 87 | 88 | -------------------------------------------------------------------------------- /src/base64.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #include 8 | 9 | 10 | size_t base64_encoded_size(const unsigned char* data, size_t size); 11 | size_t base64_decoded_size(const unsigned char* encoded_data, size_t encoded_size); 12 | //multiple definition of `base64_decode'; 13 | int base64_encode_(const unsigned char* data, size_t data_size, unsigned char *encoded_data); 14 | int base64_decode_(const unsigned char* encoded_data, size_t encoded_size, unsigned char *data); 15 | #ifdef __cplusplus 16 | } 17 | #endif 18 | -------------------------------------------------------------------------------- /src/constants.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define DEVICE_ID_SIZE 36 4 | #define ACCESSORY_ID_SIZE 17 5 | -------------------------------------------------------------------------------- /src/crypto.h: -------------------------------------------------------------------------------- 1 | #ifndef __CRYPTO_H__ 2 | #define __CRYPTO_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | //wolfssl-3.13.0-stable 9 | 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | //#include "wolfcrypt/hmac.h" 17 | //#include "wolfcrypt/ed25519.h" 18 | //#include "wolfcrypt/curve25519.h" 19 | //#include "wolfcrypt/sha512.h" 20 | //#include "wolfcrypt/chacha20_poly1305.h" 21 | //#include "wolfcrypt/srp.h" 22 | //#include "wolfcrypt/error-crypt.h" 23 | 24 | typedef unsigned char byte; 25 | 26 | #define HKDF_HASH_SIZE 32 // CHACHA20_POLY1305_AEAD_KEYSIZE 27 | 28 | int crypto_hkdf( 29 | const byte *key, size_t key_size, 30 | const byte *salt, size_t salt_size, 31 | const byte *info, size_t info_size, 32 | byte *output, size_t *output_size 33 | ); 34 | 35 | // SRP 36 | struct _Srp; 37 | typedef struct _Srp Srp; 38 | 39 | 40 | Srp *crypto_srp_new(); 41 | void crypto_srp_free(Srp *srp); 42 | 43 | int crypto_srp_init(Srp *srp, const char *username, const char *password); 44 | 45 | int crypto_srp_get_salt(Srp *srp, byte *buffer, size_t *buffer_length); 46 | int crypto_srp_get_public_key(Srp *srp, byte *buffer, size_t *buffer_length); 47 | 48 | int crypto_srp_compute_key( 49 | Srp *srp, 50 | const byte *client_public_key, size_t client_public_key_size, 51 | const byte *server_public_key, size_t server_public_key_size 52 | ); 53 | int crypto_srp_verify(Srp *srp, const byte *proof, size_t proof_size); 54 | int crypto_srp_get_proof(Srp *srp, byte *proof, size_t *proof_size); 55 | 56 | int crypto_srp_hkdf( 57 | Srp *srp, 58 | const byte *salt, size_t salt_size, 59 | const byte *info, size_t info_size, 60 | byte *output, size_t *output_size 61 | ); 62 | 63 | int crypto_chacha20poly1305_encrypt( 64 | const byte *key, const byte *nonce, const byte *aad, size_t aad_size, 65 | const byte *message, size_t message_size, 66 | byte *encrypted, size_t *encrypted_size 67 | ); 68 | int crypto_chacha20poly1305_decrypt( 69 | const byte *key, const byte *nonce, const byte *aad, size_t aad_size, 70 | const byte *message, size_t message_size, 71 | byte *decrypted, size_t *descrypted_size 72 | ); 73 | 74 | // ED25519 75 | int crypto_ed25519_init(ed25519_key *key); 76 | ed25519_key *crypto_ed25519_new(); 77 | int crypto_ed25519_generate(ed25519_key *key); 78 | void crypto_ed25519_free(ed25519_key *key); 79 | 80 | int crypto_ed25519_import_key( 81 | ed25519_key *key, 82 | const byte *data, size_t size 83 | ); 84 | int crypto_ed25519_export_key( 85 | const ed25519_key *key, 86 | byte *buffer, size_t *size 87 | ); 88 | 89 | int crypto_ed25519_import_public_key( 90 | ed25519_key *key, 91 | const byte *data, size_t size 92 | ); 93 | int crypto_ed25519_export_public_key( 94 | const ed25519_key *key, 95 | byte *buffer, size_t *size 96 | ); 97 | 98 | int crypto_ed25519_sign( 99 | const ed25519_key *key, 100 | const byte *message, size_t message_size, 101 | byte *signature, size_t *signature_size 102 | ); 103 | int crypto_ed25519_verify( 104 | const ed25519_key *key, 105 | const byte *message, size_t message_size, 106 | const byte *signature, size_t signature_size 107 | ); 108 | 109 | 110 | // CURVE25519 111 | int crypto_curve25519_init(curve25519_key *key); 112 | int crypto_curve25519_done(curve25519_key *key); 113 | int crypto_curve25519_generate(curve25519_key *key); 114 | int crypto_curve25519_import_public( 115 | curve25519_key *key, 116 | const byte *data, size_t size 117 | ); 118 | int crypto_curve25519_export_public( 119 | const curve25519_key *key, 120 | byte *buffer, size_t *size 121 | ); 122 | int crypto_curve25519_shared_secret( 123 | const curve25519_key *private_key, 124 | const curve25519_key *public_key, 125 | byte *buffer, size_t *size 126 | ); 127 | 128 | #ifdef __cplusplus 129 | } 130 | #endif 131 | 132 | #endif // __CRYPTO_H__ 133 | -------------------------------------------------------------------------------- /src/esp_xpgm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * xpgm.h 3 | * 4 | * Created on: 2020-03-08 5 | * Author: Wang Bin 6 | */ 7 | 8 | #ifndef ESP_XPGM_H_ 9 | #define ESP_XPGM_H_ 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | //see "osapi.h" 20 | /* 21 | This function works only after Serial.setDebugOutput(true); ? 22 | otherwise Serial prints nothing. 23 | #define esp_printf(fmt, ...) do { \ 24 | static const char flash_str[] ICACHE_RODATA_ATTR STORE_ATTR = fmt; \ 25 | os_printf_plus(flash_str, ##__VA_ARGS__); \ 26 | } while(0)*/ 27 | 28 | #define XPGM_PRINTF(fmt, ...) printf_P(PSTR(fmt) , ##__VA_ARGS__); 29 | 30 | #define XPGM_VAR(v0, v1) v0##v1 31 | 32 | // pgm_ptr --> ram_ptr 33 | #define XPGM_CPY(ram_ptr, pgm_ptr, size) memcpy_P(ram_ptr, pgm_ptr, size) 34 | 35 | #define XPGM_BUFFCPY(buff_type, buff_name, pgm_ptr, size) size_t XPGM_VAR(buff_name, _size) = size; \ 36 | buff_type buff_name[XPGM_VAR(buff_name, _size)]; \ 37 | XPGM_CPY(buff_name, pgm_ptr, XPGM_VAR(buff_name, _size)); 38 | 39 | #define XPGM_BUFFCPY_ARRAY(buff_type, buff_name, pgm_array) XPGM_BUFFCPY(buff_type, buff_name, pgm_array, sizeof(pgm_array)) 40 | 41 | #define XPGM_BUFFCPY_STRING(buff_type, buff_name, pgm_string) XPGM_BUFFCPY(buff_type, buff_name, pgm_string, strlen_P(pgm_string) + 1) 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* ESP_XPGM_H_ */ 48 | -------------------------------------------------------------------------------- /src/homekit/homekit.h: -------------------------------------------------------------------------------- 1 | #ifndef __HOMEKIT_H__ 2 | #define __HOMEKIT_H__ 3 | 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | typedef void *homekit_client_id_t; 11 | 12 | 13 | typedef enum { 14 | HOMEKIT_EVENT_SERVER_INITIALIZED, 15 | // Just accepted client connection 16 | HOMEKIT_EVENT_CLIENT_CONNECTED, 17 | // Pairing verification completed and secure session is established 18 | HOMEKIT_EVENT_CLIENT_VERIFIED, 19 | HOMEKIT_EVENT_CLIENT_DISCONNECTED, 20 | HOMEKIT_EVENT_PAIRING_ADDED, 21 | HOMEKIT_EVENT_PAIRING_REMOVED, 22 | } homekit_event_t; 23 | 24 | 25 | typedef struct { 26 | // Pointer to an array of homekit_accessory_t pointers. 27 | // Array should be terminated by a NULL pointer. 28 | homekit_accessory_t **accessories; 29 | 30 | homekit_accessory_category_t category; 31 | 32 | // Used for Bonjour 33 | // Current configuration number. Required. 34 | // Must update when an accessory, service, or characteristic is added or removed on the accessory server. 35 | // Accessories must increment the config number after a firmware update. 36 | // This must have a range of 1-65535 and wrap to 1 when it overflows. 37 | // This value must persist across reboots, power cycles, etc. 38 | uint16_t config_number; 39 | 40 | // Password in format "111-23-456". 41 | // If password is not specified, a random password 42 | // will be used. In that case, a password_callback 43 | // field must contain pointer to a function that should 44 | // somehow communicate password to the user (e.g. display 45 | // it on a screen if accessory has one). 46 | char *password; 47 | void (*password_callback)(const char *password); 48 | 49 | // Setup ID in format "XXXX" (where X is digit or latin capital letter) 50 | // Used for pairing using QR code 51 | char *setupId; 52 | 53 | // Callback for "POST /resource" to get snapshot image from camera 54 | void (*on_resource)(const char *body, size_t body_size); 55 | 56 | void (*on_event)(homekit_event_t event); 57 | } homekit_server_config_t; 58 | 59 | // Get pairing URI 60 | int homekit_get_setup_uri(const homekit_server_config_t *config, 61 | char *buffer, size_t buffer_size); 62 | 63 | // Initialize HomeKit accessory server 64 | void homekit_server_init(homekit_server_config_t *config); 65 | 66 | // Reset HomeKit accessory server, removing all pairings 67 | void homekit_server_reset(); 68 | 69 | int homekit_get_accessory_id(char *buffer, size_t size); 70 | bool homekit_is_paired(); 71 | 72 | // Client related stuff 73 | homekit_client_id_t homekit_get_client_id(); 74 | 75 | bool homekit_client_is_admin(); 76 | int homekit_client_send(unsigned char *data, size_t size); 77 | 78 | #ifdef __cplusplus 79 | } 80 | #endif 81 | 82 | #endif // __HOMEKIT_H__ 83 | -------------------------------------------------------------------------------- /src/homekit/tlv.h: -------------------------------------------------------------------------------- 1 | #ifndef __TLV_H__ 2 | #define __TLV_H__ 3 | 4 | #define __need_size_t 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | typedef unsigned char byte; 15 | 16 | typedef struct _tlv { 17 | struct _tlv *next; 18 | byte type; 19 | byte *value; 20 | size_t size; 21 | } tlv_t; 22 | 23 | 24 | typedef struct { 25 | tlv_t *head; 26 | } tlv_values_t; 27 | 28 | 29 | tlv_values_t *tlv_new(); 30 | 31 | void tlv_free(tlv_values_t *values); 32 | 33 | int tlv_add_value(tlv_values_t *values, byte type, const byte *value, size_t size); 34 | int tlv_add_string_value(tlv_values_t *values, byte type, const char *value); 35 | int tlv_add_integer_value(tlv_values_t *values, byte type, size_t size, int value); 36 | int tlv_add_tlv_value(tlv_values_t *values, byte type, tlv_values_t *value); 37 | 38 | tlv_t *tlv_get_value(const tlv_values_t *values, byte type); 39 | int tlv_get_integer_value(const tlv_values_t *values, byte type, int def); 40 | tlv_values_t *tlv_get_tlv_value(const tlv_values_t *values, byte type); 41 | 42 | int tlv_format(const tlv_values_t *values, byte *buffer, size_t *size); 43 | 44 | int tlv_parse(const byte *buffer, size_t length, tlv_values_t *values); 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif // __TLV_H__ 51 | -------------------------------------------------------------------------------- /src/homekit_debug.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "homekit_debug.h" 5 | 6 | 7 | char *binary_to_string(const byte *data, size_t size) { 8 | int i; 9 | 10 | size_t buffer_size = 1; // 1 char for eos 11 | for (i=0; i= 32 && data[i] < 128) ? 1 : 4); 13 | 14 | char *buffer = malloc(buffer_size); 15 | 16 | int pos = 0; 17 | for (i=0; i= 128) { 22 | size_t printed = snprintf(&buffer[pos], buffer_size - pos, "\\x%02X", data[i]); 23 | if (printed > 0) { 24 | pos += printed; 25 | } 26 | } else { 27 | buffer[pos++] = data[i]; 28 | } 29 | } 30 | buffer[pos] = 0; 31 | 32 | return buffer; 33 | } 34 | 35 | 36 | void print_binary(const char *prompt, const byte *data, size_t size) { 37 | #ifdef HOMEKIT_DEBUG 38 | char *buffer = binary_to_string(data, size); 39 | printf("%s (%d bytes): \"%s\"\n", prompt, (int)size, buffer); 40 | free(buffer); 41 | #endif 42 | } 43 | -------------------------------------------------------------------------------- /src/homekit_debug.h: -------------------------------------------------------------------------------- 1 | #ifndef __HOMEKIT_DEBUG_H__ 2 | #define __HOMEKIT_DEBUG_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | #include 10 | #include "Arduino.h" 11 | #include 12 | #include 13 | 14 | typedef unsigned char byte; 15 | 16 | #define HOMEKIT_NO_LOG 0 17 | #define HOMEKIT_LOG_ERROR 1 18 | #define HOMEKIT_LOG_INFO 2 19 | #define HOMEKIT_LOG_DEBUG 3 20 | 21 | #ifndef HOMEKIT_LOG_LEVEL 22 | #define HOMEKIT_LOG_LEVEL HOMEKIT_LOG_INFO 23 | #endif 24 | 25 | #define HOMEKIT_PRINTF XPGM_PRINTF 26 | 27 | #if HOMEKIT_LOG_LEVEL >= HOMEKIT_LOG_DEBUG 28 | 29 | #define DEBUG(message, ...) HOMEKIT_PRINTF(">>> %s: " message "\n", __func__, ##__VA_ARGS__) 30 | static uint32_t start_time = 0; 31 | #define DEBUG_TIME_BEGIN() start_time=millis(); 32 | #define DEBUG_TIME_END(func_name) HOMEKIT_PRINTF("### [%7d] %s took %6dms\n", millis(), func_name, (millis() - start_time)); 33 | #define DEBUG_HEAP() DEBUG("Free heap: %d", system_get_free_heap_size()); 34 | 35 | #else 36 | 37 | #define DEBUG(message, ...) 38 | #define DEBUG_TIME_BEGIN() 39 | #define DEBUG_TIME_END(func_name) 40 | #define DEBUG_HEAP() 41 | 42 | #endif 43 | 44 | #if HOMEKIT_LOG_LEVEL >= HOMEKIT_LOG_ERROR 45 | 46 | #define ERROR(message, ...) HOMEKIT_PRINTF("!!! [%7d] HomeKit: " message "\n", millis(), ##__VA_ARGS__) 47 | 48 | #else 49 | 50 | #define ERROR(message, ...) 51 | 52 | #endif 53 | 54 | #if HOMEKIT_LOG_LEVEL >= HOMEKIT_LOG_INFO 55 | 56 | #define INFO(message, ...) HOMEKIT_PRINTF(">>> [%7d] HomeKit: " message "\n", millis(), ##__VA_ARGS__) 57 | #define INFO_HEAP() INFO("Free heap: %d", system_get_free_heap_size()); 58 | 59 | #else 60 | 61 | #define INFO(message, ...) 62 | #define INFO_HEAP() 63 | 64 | #endif 65 | 66 | char *binary_to_string(const byte *data, size_t size); 67 | void print_binary(const char *prompt, const byte *data, size_t size); 68 | 69 | #ifdef __cplusplus 70 | } 71 | #endif 72 | 73 | #endif // __HOMEKIT_DEBUG_H__ 74 | -------------------------------------------------------------------------------- /src/json.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #include 8 | 9 | struct json_stream; 10 | typedef struct json_stream json_stream; 11 | 12 | typedef void (*json_flush_callback)(uint8_t *buffer, size_t size, void *context); 13 | 14 | json_stream *json_new(size_t buffer_size, json_flush_callback on_flush, void *context); 15 | void json_free(json_stream *json); 16 | 17 | void json_flush(json_stream *json); 18 | 19 | void json_object_start(json_stream *json); 20 | void json_object_end(json_stream *json); 21 | 22 | void json_array_start(json_stream *json); 23 | void json_array_end(json_stream *json); 24 | 25 | void json_integer(json_stream *json, int x); 26 | void json_uint8(json_stream *json, uint8_t x); 27 | void json_uint16(json_stream *json, uint16_t x); 28 | void json_uint32(json_stream *json, uint32_t x); 29 | void json_uint64(json_stream *json, uint64_t x); 30 | void json_float(json_stream *json, float x); 31 | void json_string(json_stream *json, const char *x); 32 | void json_boolean(json_stream *json, bool x); 33 | void json_null(json_stream *json); 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | -------------------------------------------------------------------------------- /src/pairing.h: -------------------------------------------------------------------------------- 1 | #ifndef __PAIRING_H__ 2 | #define __PAIRING_H__ 3 | 4 | #include "constants.h" 5 | #include "crypto.h" 6 | 7 | 8 | typedef enum { 9 | pairing_permissions_admin = (1 << 0), 10 | } pairing_permissions_t; 11 | 12 | typedef struct { 13 | int id; 14 | char device_id[DEVICE_ID_SIZE + 1]; 15 | ed25519_key device_key; 16 | pairing_permissions_t permissions; 17 | } pairing_t; 18 | 19 | #endif // __PAIRING_H__ 20 | -------------------------------------------------------------------------------- /src/port.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifdef ESP_OPEN_RTOS 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "mdnsresponder.h" 10 | 11 | #ifndef MDNS_TTL 12 | #define MDNS_TTL 4500 13 | #endif 14 | 15 | uint32_t homekit_random() { 16 | return hwrand(); 17 | } 18 | 19 | void homekit_random_fill(uint8_t *data, size_t size) { 20 | hwrand_fill(data, size); 21 | } 22 | 23 | void homekit_system_restart() { 24 | sdk_system_restart(); 25 | } 26 | 27 | void homekit_overclock_start() { 28 | sdk_system_overclock(); 29 | } 30 | 31 | void homekit_overclock_end() { 32 | sdk_system_restoreclock(); 33 | } 34 | 35 | static char mdns_instance_name[65] = {0}; 36 | static char mdns_txt_rec[128] = {0}; 37 | static int mdns_port = 80; 38 | 39 | void homekit_mdns_init() { 40 | mdns_init(); 41 | } 42 | 43 | void homekit_mdns_configure_init(const char *instance_name, int port) { 44 | strncpy(mdns_instance_name, instance_name, sizeof(mdns_instance_name)); 45 | mdns_txt_rec[0] = 0; 46 | mdns_port = port; 47 | } 48 | 49 | void homekit_mdns_add_txt(const char *key, const char *format, ...) { 50 | va_list arg_ptr; 51 | va_start(arg_ptr, format); 52 | 53 | char value[128]; 54 | int value_len = vsnprintf(value, sizeof(value), format, arg_ptr); 55 | 56 | va_end(arg_ptr); 57 | 58 | if (value_len && value_len < sizeof(value)-1) { 59 | char buffer[128]; 60 | int buffer_len = snprintf(buffer, sizeof(buffer), "%s=%s", key, value); 61 | 62 | if (buffer_len < sizeof(buffer)-1) 63 | mdns_TXT_append(mdns_txt_rec, sizeof(mdns_txt_rec), buffer, buffer_len); 64 | } 65 | } 66 | 67 | void homekit_mdns_configure_finalize() { 68 | mdns_clear(); 69 | mdns_add_facility(mdns_instance_name, "_hap", mdns_txt_rec, mdns_TCP, mdns_port, MDNS_TTL); 70 | 71 | printf("mDNS announcement: Name=%s %s Port=%d TTL=%d\n", 72 | mdns_instance_name, mdns_txt_rec, mdns_port, MDNS_TTL); 73 | } 74 | 75 | #endif 76 | 77 | //#ifdef ESP_IDF 78 | 79 | #ifdef ARDUINO_ARCH_ESP8266 80 | 81 | #include 82 | #include 83 | 84 | uint32_t homekit_random() { 85 | return os_random(); 86 | // return esp_random(); 87 | } 88 | 89 | void homekit_random_fill(uint8_t *data, size_t size) { 90 | // uint32_t x; 91 | // for (int i=0; i= sizeof(x)) ? sizeof(x) : size-i); 94 | // } 95 | os_get_random(data, size); 96 | } 97 | 98 | void homekit_system_restart() { 99 | system_restart(); 100 | } 101 | 102 | void homekit_overclock_start() { 103 | //ets_update_cpu_frequency(ticks_per_us); 104 | } 105 | 106 | void homekit_overclock_end() { 107 | //ets_update_cpu_frequency(ticks_per_us); 108 | } 109 | /* 110 | void homekit_mdns_init() { 111 | mdns_init(); 112 | } 113 | 114 | void homekit_mdns_configure_init(const char *instance_name, int port) { 115 | mdns_hostname_set(instance_name); 116 | mdns_instance_name_set(instance_name); 117 | mdns_service_add(instance_name, "_hap", "_tcp", port, NULL, 0); 118 | } 119 | 120 | void homekit_mdns_add_txt(const char *key, const char *format, ...) { 121 | va_list arg_ptr; 122 | va_start(arg_ptr, format); 123 | 124 | char value[128]; 125 | int value_len = vsnprintf(value, sizeof(value), format, arg_ptr); 126 | 127 | va_end(arg_ptr); 128 | 129 | if (value_len && value_len < sizeof(value)-1) { 130 | mdns_service_txt_item_set("_hap", "_tcp", key, value); 131 | } 132 | } 133 | 134 | void homekit_mdns_configure_finalize() { 135 | /* 136 | printf("mDNS announcement: Name=%s %s Port=%d TTL=%d\n", 137 | name->value.string_value, txt_rec, PORT, 0); 138 | */ 139 | //}*/ 140 | 141 | #endif 142 | -------------------------------------------------------------------------------- /src/port.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #include 8 | 9 | uint32_t homekit_random(); 10 | void homekit_random_fill(uint8_t *data, size_t size); 11 | 12 | void homekit_system_restart(); 13 | void homekit_overclock_start(); 14 | void homekit_overclock_end(); 15 | 16 | #ifdef ESP_OPEN_RTOS 17 | #include 18 | #define ESP_OK 0 19 | #endif 20 | 21 | //#ifdef ESP_IDF 22 | //#include 23 | //#include 24 | 25 | #ifdef ARDUINO_ARCH_ESP8266 26 | #include "Arduino.h" 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | 33 | #define ESP_OK SPI_FLASH_RESULT_OK //0 34 | 35 | #define SPI_FLASH_SECTOR_SIZE SPI_FLASH_SEC_SIZE 36 | #define spiflash_read(addr, buffer, size) (spi_flash_read((addr), (buffer), (size)) == ESP_OK) 37 | #define spiflash_write(addr, data, size) (spi_flash_write((addr), (data), (size)) == ESP_OK) 38 | #define spiflash_erase_sector(addr) (spi_flash_erase_sector((addr) / SPI_FLASH_SECTOR_SIZE) == ESP_OK) 39 | #endif 40 | 41 | 42 | #ifdef ESP_IDF 43 | #define SERVER_TASK_STACK 12288 44 | #else 45 | #define SERVER_TASK_STACK 2048 46 | #endif 47 | 48 | 49 | //void homekit_mdns_init(); 50 | //void homekit_mdns_configure_init(const char *instance_name, int port); 51 | //void homekit_mdns_add_txt(const char *key, const char *format, ...); 52 | //void homekit_mdns_configure_finalize(); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | -------------------------------------------------------------------------------- /src/query_params.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "query_params.h" 4 | 5 | 6 | query_param_t *query_params_parse(const char *s) { 7 | query_param_t *params = NULL; 8 | 9 | int i = 0; 10 | while (1) { 11 | int pos = i; 12 | while (s[i] && s[i] != '=' && s[i] != '&' && s[i] != '#') i++; 13 | if (i == pos) { 14 | i++; 15 | continue; 16 | } 17 | 18 | query_param_t *param = malloc(sizeof(query_param_t)); 19 | param->name = strndup(s+pos, i-pos); 20 | param->value = NULL; 21 | param->next = params; 22 | params = param; 23 | 24 | if (s[i] == '=') { 25 | i++; 26 | pos = i; 27 | while (s[i] && s[i] != '&' && s[i] != '#') i++; 28 | if (i != pos) { 29 | param->value = strndup(s+pos, i-pos); 30 | } 31 | } 32 | 33 | if (!s[i] || s[i] == '#') 34 | break; 35 | } 36 | 37 | return params; 38 | } 39 | 40 | 41 | query_param_t *query_params_find(query_param_t *params, const char *name) { 42 | while (params) { 43 | if (!strcmp(params->name, name)) 44 | return params; 45 | params = params->next; 46 | } 47 | 48 | return NULL; 49 | } 50 | 51 | 52 | void query_params_free(query_param_t *params) { 53 | while (params) { 54 | query_param_t *next = params->next; 55 | if (params->name) 56 | free(params->name); 57 | if (params->value) 58 | free(params->value); 59 | free(params); 60 | 61 | params = next; 62 | } 63 | } 64 | 65 | -------------------------------------------------------------------------------- /src/query_params.h: -------------------------------------------------------------------------------- 1 | #ifndef __HOMEKIT_QUERY_PARAMS__ 2 | #define __HOMEKIT_QUERY_PARAMS__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | typedef struct _query_param { 9 | char *name; 10 | char *value; 11 | 12 | struct _query_param *next; 13 | } query_param_t; 14 | 15 | query_param_t *query_params_parse(const char *s); 16 | query_param_t *query_params_find(query_param_t *params, const char *name); 17 | void query_params_free(query_param_t *params); 18 | 19 | #ifdef __cplusplus 20 | } 21 | #endif 22 | 23 | #endif // __HOMEKIT_QUERY_PARAMS__ 24 | -------------------------------------------------------------------------------- /src/storage.h: -------------------------------------------------------------------------------- 1 | #ifndef __STORAGE_H__ 2 | #define __STORAGE_H__ 3 | 4 | //#include "EEPROM.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | #include "pairing.h" 11 | 12 | int homekit_storage_reset(); 13 | 14 | int homekit_storage_init(); 15 | 16 | void homekit_storage_save_accessory_id(const char *accessory_id); 17 | int homekit_storage_load_accessory_id(char *data); 18 | 19 | void homekit_storage_save_accessory_key(const ed25519_key *key); 20 | int homekit_storage_load_accessory_key(ed25519_key *key); 21 | 22 | bool homekit_storage_can_add_pairing(); 23 | int homekit_storage_add_pairing(const char *device_id, const ed25519_key *device_key, byte permissions); 24 | int homekit_storage_update_pairing(const char *device_id, byte permissions); 25 | int homekit_storage_remove_pairing(const char *device_id); 26 | int homekit_storage_find_pairing(const char *device_id, pairing_t *pairing); 27 | 28 | typedef struct { 29 | int idx; 30 | } pairing_iterator_t; 31 | 32 | 33 | void homekit_storage_pairing_iterator_init(pairing_iterator_t *it); 34 | int homekit_storage_next_pairing(pairing_iterator_t *it, pairing_t *pairing); 35 | void homekit_storage_pairing_iterator_done(pairing_iterator_t *iterator); 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | #endif // __STORAGE_H__ 41 | -------------------------------------------------------------------------------- /src/types.c: -------------------------------------------------------------------------------- 1 | #include "homekit/types.h" 2 | 3 | //========================= 4 | // Compat for cplusplus 5 | //========================= 6 | 7 | homekit_value_t HOMEKIT_DEFAULT_CPP() { 8 | homekit_value_t homekit_value; 9 | //homekit_value.is_null = false;//该值为默认,不用设置 10 | return homekit_value; 11 | } 12 | 13 | homekit_value_t HOMEKIT_NULL_CPP() { 14 | homekit_value_t homekit_value; 15 | homekit_value.is_null = true; 16 | return homekit_value; 17 | } 18 | 19 | homekit_value_t HOMEKIT_BOOL_CPP(bool value) { 20 | homekit_value_t homekit_value = HOMEKIT_DEFAULT_CPP(); 21 | homekit_value.format = homekit_format_bool; 22 | homekit_value.bool_value = value; 23 | return homekit_value; 24 | } 25 | 26 | homekit_value_t HOMEKIT_INT_CPP(int value) { 27 | homekit_value_t homekit_value = HOMEKIT_DEFAULT_CPP(); 28 | homekit_value.format = homekit_format_int; 29 | homekit_value.int_value = value; 30 | return homekit_value; 31 | } 32 | 33 | homekit_value_t HOMEKIT_UINT8_CPP(uint8_t value) { 34 | homekit_value_t homekit_value = HOMEKIT_DEFAULT_CPP(); 35 | homekit_value.format = homekit_format_uint8; 36 | homekit_value.uint8_value = value; 37 | return homekit_value; 38 | } 39 | 40 | homekit_value_t HOMEKIT_UINT16_CPP(uint16_t value) { 41 | homekit_value_t homekit_value = HOMEKIT_DEFAULT_CPP(); 42 | homekit_value.format = homekit_format_uint16; 43 | homekit_value.uint16_value = value; 44 | return homekit_value; 45 | } 46 | 47 | homekit_value_t HOMEKIT_UINT32_CPP(uint32_t value) { 48 | homekit_value_t homekit_value = HOMEKIT_DEFAULT_CPP(); 49 | homekit_value.format = homekit_format_uint32; 50 | homekit_value.uint32_value = value; 51 | return homekit_value; 52 | } 53 | 54 | homekit_value_t HOMEKIT_UINT64_CPP(uint64_t value) { 55 | homekit_value_t homekit_value = HOMEKIT_DEFAULT_CPP(); 56 | homekit_value.format = homekit_format_uint64; 57 | homekit_value.uint64_value = value; 58 | return homekit_value; 59 | } 60 | 61 | homekit_value_t HOMEKIT_FLOAT_CPP(float value) { 62 | homekit_value_t homekit_value = HOMEKIT_DEFAULT_CPP(); 63 | homekit_value.format = homekit_format_float; 64 | homekit_value.float_value = value; 65 | return homekit_value; 66 | } 67 | 68 | homekit_value_t HOMEKIT_STRING_CPP(char *value) { 69 | homekit_value_t homekit_value = HOMEKIT_DEFAULT_CPP(); 70 | homekit_value.format = homekit_format_string; 71 | homekit_value.string_value = value; 72 | return homekit_value; 73 | } 74 | 75 | homekit_value_t HOMEKIT_TLV_CPP(tlv_values_t *value) { 76 | homekit_value_t homekit_value = HOMEKIT_DEFAULT_CPP(); 77 | homekit_value.format = homekit_format_tlv; 78 | homekit_value.tlv_values = value; 79 | return homekit_value; 80 | } 81 | 82 | homekit_value_t HOMEKIT_DATA_CPP(uint8_t *value, size_t size) { 83 | homekit_value_t homekit_value = HOMEKIT_DEFAULT_CPP(); 84 | homekit_value.format = homekit_format_data; 85 | homekit_value.data_value = value; 86 | homekit_value.data_size = size; 87 | return homekit_value; 88 | } 89 | 90 | -------------------------------------------------------------------------------- /src/user_settings.h: -------------------------------------------------------------------------------- 1 | #ifndef wolfcrypt_user_settings_h 2 | #define wolfcrypt_user_settings_h 3 | 4 | #define WOLFSSL_USER_SETTINGS 5 | 6 | //#define ARDUINO_HOMEKIT_LOWROM 7 | 8 | //skip ed25519_verify, see crypto_ed25519_verify in crypto.c 9 | //Pair Verify Step 2/2: skip=35ms, not-skip=794ms 10 | //#define ARDUINO_HOMEKIT_SKIP_ED25519_VERIFY 11 | 12 | #include "stdint.h" 13 | #include "stddef.h" 14 | #include "stdlib.h" 15 | #include "osapi.h" 16 | #include "homekit_debug.h" 17 | 18 | static inline int hwrand_generate_block(uint8_t *buf, size_t len) { 19 | os_get_random(buf, len); 20 | return 0; 21 | } 22 | 23 | #define WC_NO_HARDEN 24 | #define NO_WOLFSSL_DIR 25 | #define SINGLE_THREADED 26 | #define WOLFSSL_LWIP 27 | #define NO_INLINE 28 | 29 | #define NO_WOLFSSL_MEMORY 30 | #define MP_LOW_MEM 31 | 32 | #define CUSTOM_RAND_GENERATE_BLOCK hwrand_generate_block 33 | 34 | //========== 35 | // added 36 | //========== 37 | #define WOLFCRYPT_ONLY 38 | 39 | #define NO_ASN 40 | #define NO_AES 41 | #define NO_RC4 42 | #define NO_RSA 43 | #define NO_SHA256 44 | #define NO_DH 45 | #define NO_DSA 46 | #define NO_CODING 47 | #define NO_SHA 48 | #define NO_MD5 49 | 50 | #define HAVE_CURVE25519 51 | #define HAVE_CHACHA 52 | #define HAVE_POLY1305 53 | #define HAVE_ED25519 54 | #define WOLFSSL_SHA512 55 | #define WOLFCRYPT_HAVE_SRP 56 | #define HAVE_HKDF 57 | 58 | #define WC_NO_HASHDRBG 59 | 60 | #define WOLFSSL_BASE64_ENCODE 61 | 62 | //see integer.c 63 | //default winsize=5(MP_LOW_MEM), but ram(heap) is not sufficient! 64 | //winsize of {2,3,4,5} are same performance 65 | //lower winsize, lower ram required 66 | #define ESP_INTEGER_WINSIZE 2 67 | //winsize=3 & mp_exptmod_fast : ram(heap) is not sufficient 68 | //force use s_mp_exptmod (lower memory), and smiller performance with mp_exptmod_fast 69 | #define ESP_FORCE_S_MP_EXPTMOD 70 | //winsize = 5 & mp_exptmod_fast 最快,Pair Verify Step 2/2 = 10s左右 71 | //winsize = 6 heap不够 72 | 73 | 74 | #define MP_16BIT //faster than 32bit in ESP8266 75 | 76 | #if defined(ARDUINO_HOMEKIT_LOWROM) 77 | 78 | #define CURVE25519_SMALL 79 | #define ED25519_SMALL //关联ED25519,关闭这个之后编译dram会超(stack mem太大) 80 | //`.bss' is not within region `dram0_0_seg' 81 | 82 | #else 83 | // crypto.c crypto_ed25519_verify 84 | // No sufficient ram to run ge_double_scalarmult_vartime in ge_operations.c 85 | // Run the ge_double_scalarmult_vartime in ge_low_mem 86 | #define ESP_GE_DOUBLE_SCALARMULT_VARTIME_LOWMEM 87 | 88 | #endif 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /src/watchdog.c: -------------------------------------------------------------------------------- 1 | // Base on esp_hw_wdt in github 2 | /* 3 | * Copyright (c) 2014-2017 Cesanta Software Limited 4 | * All rights reserved 5 | * Changed: 6 | * 2017 Alexander Epstine (a@epstine.com) 7 | */ 8 | 9 | #include "watchdog.h" 10 | #include "Arduino.h" 11 | #include "stdint.h" 12 | #include "user_interface.h" 13 | #include "homekit_debug.h" 14 | 15 | #define REG_WDT_BASE 0x60000900 16 | 17 | #define WDT_CTL (REG_WDT_BASE + 0x0) 18 | #define WDT_CTL_ENABLE (BIT(0)) 19 | #define WDT_RESET (REG_WDT_BASE + 0x14) 20 | #define WDT_RESET_VALUE 0x73 21 | 22 | void esp_hw_wdt_enable() { 23 | SET_PERI_REG_MASK(WDT_CTL, WDT_CTL_ENABLE); 24 | } 25 | 26 | void esp_hw_wdt_disable() { 27 | CLEAR_PERI_REG_MASK(WDT_CTL, WDT_CTL_ENABLE); 28 | } 29 | 30 | void esp_hw_wdt_feed() { 31 | WRITE_PERI_REG(WDT_RESET, WDT_RESET_VALUE); 32 | } 33 | 34 | void watchdog_disable_all() { 35 | system_soft_wdt_stop(); 36 | esp_hw_wdt_disable(); 37 | } 38 | 39 | void watchdog_enable_all() { 40 | esp_hw_wdt_enable(); 41 | system_soft_wdt_restart(); 42 | } 43 | 44 | #ifdef HOMEKIT_DEBUG 45 | 46 | static uint32_t wdt_checkpoint; 47 | 48 | void watchdog_check_begin() { 49 | wdt_checkpoint = millis(); 50 | } 51 | 52 | void watchdog_check_end(const char *message) { 53 | uint32_t d = millis() - wdt_checkpoint; 54 | printf("[WatchDog] Function %s took: %dms\n", message, d); 55 | } 56 | 57 | #else 58 | 59 | void watchdog_check_begin() { 60 | } 61 | void watchdog_check_end(const char *message) { 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /src/watchdog.h: -------------------------------------------------------------------------------- 1 | #ifndef WATCHDOG_H_ 2 | #define WATCHDOG_H_ 3 | 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | void watchdog_disable_all(); 10 | 11 | void watchdog_enable_all(); 12 | 13 | void watchdog_check_begin(); 14 | 15 | void watchdog_check_end(const char* message); 16 | 17 | #ifdef __cplusplus 18 | } 19 | #endif 20 | 21 | 22 | #endif /* WATCHDOG_H_ */ 23 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/arc4.h: -------------------------------------------------------------------------------- 1 | /* arc4.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | 24 | #ifndef WOLF_CRYPT_ARC4_H 25 | #define WOLF_CRYPT_ARC4_H 26 | 27 | #include 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | #ifdef WOLFSSL_ASYNC_CRYPT 34 | #include 35 | #endif 36 | 37 | enum { 38 | ARC4_ENC_TYPE = 4, /* cipher unique type */ 39 | ARC4_STATE_SIZE = 256 40 | }; 41 | 42 | /* ARC4 encryption and decryption */ 43 | typedef struct Arc4 { 44 | byte x; 45 | byte y; 46 | byte state[ARC4_STATE_SIZE]; 47 | #ifdef WOLFSSL_ASYNC_CRYPT 48 | WC_ASYNC_DEV asyncDev; 49 | #endif 50 | void* heap; 51 | } Arc4; 52 | 53 | WOLFSSL_API int wc_Arc4Process(Arc4*, byte*, const byte*, word32); 54 | WOLFSSL_API int wc_Arc4SetKey(Arc4*, const byte*, word32); 55 | 56 | WOLFSSL_API int wc_Arc4Init(Arc4*, void*, int); 57 | WOLFSSL_API void wc_Arc4Free(Arc4*); 58 | 59 | #ifdef __cplusplus 60 | } /* extern "C" */ 61 | #endif 62 | 63 | 64 | #endif /* WOLF_CRYPT_ARC4_H */ 65 | 66 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/blake2.h: -------------------------------------------------------------------------------- 1 | /* blake2.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | 24 | 25 | #ifndef WOLF_CRYPT_BLAKE2_H 26 | #define WOLF_CRYPT_BLAKE2_H 27 | 28 | #include 29 | 30 | #ifdef HAVE_BLAKE2 31 | 32 | #include 33 | 34 | /* call old functions if using fips for the sake of hmac @wc_fips */ 35 | #ifdef HAVE_FIPS 36 | /* Since hmac can call blake functions provide original calls */ 37 | #define wc_InitBlake2b InitBlake2b 38 | #define wc_Blake2bUpdate Blake2bUpdate 39 | #define wc_Blake2bFinal Blake2bFinal 40 | #endif 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | /* in bytes, variable digest size up to 512 bits (64 bytes) */ 47 | enum { 48 | BLAKE2B_ID = 7, /* hash type unique */ 49 | BLAKE2B_256 = 32 /* 256 bit type, SSL default */ 50 | }; 51 | 52 | 53 | /* BLAKE2b digest */ 54 | typedef struct Blake2b { 55 | blake2b_state S[1]; /* our state */ 56 | word32 digestSz; /* digest size used on init */ 57 | } Blake2b; 58 | 59 | 60 | WOLFSSL_API int wc_InitBlake2b(Blake2b*, word32); 61 | WOLFSSL_API int wc_Blake2bUpdate(Blake2b*, const byte*, word32); 62 | WOLFSSL_API int wc_Blake2bFinal(Blake2b*, byte*, word32); 63 | 64 | 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif /* HAVE_BLAKE2 */ 71 | #endif /* WOLF_CRYPT_BLAKE2_H */ 72 | 73 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/camellia.h: -------------------------------------------------------------------------------- 1 | /* camellia.h ver 1.2.0 2 | * 3 | * Copyright (c) 2006,2007 4 | * NTT (Nippon Telegraph and Telephone Corporation) . All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer as 11 | * the first lines of this file unmodified. 12 | * 2. Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY NTT ``AS IS'' AND ANY EXPRESS OR 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL NTT BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* camellia.h 29 | * 30 | * Copyright (C) 2006-2017 wolfSSL Inc. 31 | * 32 | * This file is part of wolfSSL. 33 | * 34 | * wolfSSL is free software; you can redistribute it and/or modify 35 | * it under the terms of the GNU General Public License as published by 36 | * the Free Software Foundation; either version 2 of the License, or 37 | * (at your option) any later version. 38 | * 39 | * wolfSSL is distributed in the hope that it will be useful, 40 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 41 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 42 | * GNU General Public License for more details. 43 | * 44 | * You should have received a copy of the GNU General Public License 45 | * along with this program; if not, write to the Free Software 46 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 47 | */ 48 | 49 | 50 | #ifndef WOLF_CRYPT_CAMELLIA_H 51 | #define WOLF_CRYPT_CAMELLIA_H 52 | 53 | #include 54 | 55 | #ifdef HAVE_CAMELLIA 56 | 57 | #ifdef __cplusplus 58 | extern "C" { 59 | #endif 60 | 61 | enum { 62 | CAMELLIA_BLOCK_SIZE = 16 63 | }; 64 | 65 | #define CAMELLIA_TABLE_BYTE_LEN 272 66 | #define CAMELLIA_TABLE_WORD_LEN (CAMELLIA_TABLE_BYTE_LEN / sizeof(word32)) 67 | 68 | typedef word32 KEY_TABLE_TYPE[CAMELLIA_TABLE_WORD_LEN]; 69 | 70 | typedef struct Camellia { 71 | word32 keySz; 72 | KEY_TABLE_TYPE key; 73 | word32 reg[CAMELLIA_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ 74 | word32 tmp[CAMELLIA_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ 75 | } Camellia; 76 | 77 | 78 | WOLFSSL_API int wc_CamelliaSetKey(Camellia* cam, 79 | const byte* key, word32 len, const byte* iv); 80 | WOLFSSL_API int wc_CamelliaSetIV(Camellia* cam, const byte* iv); 81 | WOLFSSL_API int wc_CamelliaEncryptDirect(Camellia* cam, byte* out, 82 | const byte* in); 83 | WOLFSSL_API int wc_CamelliaDecryptDirect(Camellia* cam, byte* out, 84 | const byte* in); 85 | WOLFSSL_API int wc_CamelliaCbcEncrypt(Camellia* cam, 86 | byte* out, const byte* in, word32 sz); 87 | WOLFSSL_API int wc_CamelliaCbcDecrypt(Camellia* cam, 88 | byte* out, const byte* in, word32 sz); 89 | 90 | 91 | #ifdef __cplusplus 92 | } /* extern "C" */ 93 | #endif 94 | 95 | #endif /* HAVE_CAMELLIA */ 96 | #endif /* WOLF_CRYPT_CAMELLIA_H */ 97 | 98 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/chacha.h: -------------------------------------------------------------------------------- 1 | /* chacha.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_CHACHA_H 24 | #define WOLF_CRYPT_CHACHA_H 25 | 26 | #include 27 | 28 | #ifdef HAVE_CHACHA 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | /* Size of the IV */ 35 | #define CHACHA_IV_WORDS 3 36 | #define CHACHA_IV_BYTES (CHACHA_IV_WORDS * sizeof(word32)) 37 | 38 | /* Size of ChaCha chunks */ 39 | #define CHACHA_CHUNK_WORDS 16 40 | #define CHACHA_CHUNK_BYTES (CHACHA_CHUNK_WORDS * sizeof(word32)) 41 | 42 | enum { 43 | CHACHA_ENC_TYPE = 7 /* cipher unique type */ 44 | }; 45 | 46 | typedef struct ChaCha { 47 | word32 X[CHACHA_CHUNK_WORDS]; /* state of cipher */ 48 | } ChaCha; 49 | 50 | /** 51 | * IV(nonce) changes with each record 52 | * counter is for what value the block counter should start ... usually 0 53 | */ 54 | WOLFSSL_API int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter); 55 | 56 | WOLFSSL_API int wc_Chacha_Process(ChaCha* ctx, byte* cipher, const byte* plain, 57 | word32 msglen); 58 | WOLFSSL_API int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz); 59 | 60 | #ifdef __cplusplus 61 | } /* extern "C" */ 62 | #endif 63 | 64 | #endif /* HAVE_CHACHA */ 65 | #endif /* WOLF_CRYPT_CHACHA_H */ 66 | 67 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/chacha20_poly1305.h: -------------------------------------------------------------------------------- 1 | /* chacha20_poly1305.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | /* This implementation of the ChaCha20-Poly1305 AEAD is based on "ChaCha20 24 | * and Poly1305 for IETF protocols" (draft-irtf-cfrg-chacha20-poly1305-10): 25 | * https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 26 | */ 27 | 28 | #ifndef WOLF_CRYPT_CHACHA20_POLY1305_H 29 | #define WOLF_CRYPT_CHACHA20_POLY1305_H 30 | 31 | #include 32 | 33 | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #define CHACHA20_POLY1305_AEAD_KEYSIZE 32 40 | #define CHACHA20_POLY1305_AEAD_IV_SIZE 12 41 | #define CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE 16 42 | 43 | enum { 44 | CHACHA20_POLY_1305_ENC_TYPE = 8 /* cipher unique type */ 45 | }; 46 | 47 | /* 48 | * The IV for this implementation is 96 bits to give the most flexibility. 49 | * 50 | * Some protocols may have unique per-invocation inputs that are not 51 | * 96-bit in length. For example, IPsec may specify a 64-bit nonce. In 52 | * such a case, it is up to the protocol document to define how to 53 | * transform the protocol nonce into a 96-bit nonce, for example by 54 | * concatenating a constant value. 55 | */ 56 | 57 | WOLFSSL_API 58 | int wc_ChaCha20Poly1305_Encrypt( 59 | const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE], 60 | const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE], 61 | const byte* inAAD, const word32 inAADLen, 62 | const byte* inPlaintext, const word32 inPlaintextLen, 63 | byte* outCiphertext, 64 | byte outAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE]); 65 | 66 | WOLFSSL_API 67 | int wc_ChaCha20Poly1305_Decrypt( 68 | const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE], 69 | const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE], 70 | const byte* inAAD, const word32 inAADLen, 71 | const byte* inCiphertext, const word32 inCiphertextLen, 72 | const byte inAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE], 73 | byte* outPlaintext); 74 | 75 | #ifdef __cplusplus 76 | } /* extern "C" */ 77 | #endif 78 | 79 | #endif /* HAVE_CHACHA && HAVE_POLY1305 */ 80 | #endif /* WOLF_CRYPT_CHACHA20_POLY1305_H */ 81 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/cmac.h: -------------------------------------------------------------------------------- 1 | /* cmac.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_CMAC_H 24 | #define WOLF_CRYPT_CMAC_H 25 | 26 | #include 27 | #include 28 | 29 | #if !defined(NO_AES) && defined(WOLFSSL_CMAC) 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | typedef struct Cmac { 36 | Aes aes; 37 | byte buffer[AES_BLOCK_SIZE]; /* partially stored block */ 38 | byte digest[AES_BLOCK_SIZE]; /* running digest */ 39 | byte k1[AES_BLOCK_SIZE]; 40 | byte k2[AES_BLOCK_SIZE]; 41 | word32 bufferSz; 42 | word32 totalSz; 43 | } Cmac; 44 | 45 | 46 | typedef enum CmacType { 47 | WC_CMAC_AES = 1 48 | } CmacType; 49 | 50 | 51 | WOLFSSL_API 52 | int wc_InitCmac(Cmac* cmac, 53 | const byte* key, word32 keySz, 54 | int type, void* unused); 55 | WOLFSSL_API 56 | int wc_CmacUpdate(Cmac* cmac, 57 | const byte* in, word32 inSz); 58 | WOLFSSL_API 59 | int wc_CmacFinal(Cmac* cmac, 60 | byte* out, word32* outSz); 61 | 62 | WOLFSSL_API 63 | int wc_AesCmacGenerate(byte* out, word32* outSz, 64 | const byte* in, word32 inSz, 65 | const byte* key, word32 keySz); 66 | 67 | WOLFSSL_API 68 | int wc_AesCmacVerify(const byte* check, word32 checkSz, 69 | const byte* in, word32 inSz, 70 | const byte* key, word32 keySz); 71 | 72 | #ifdef __cplusplus 73 | } /* extern "C" */ 74 | #endif 75 | 76 | 77 | #endif /* NO_AES && WOLFSSL_CMAC */ 78 | #endif /* WOLF_CRYPT_CMAC_H */ 79 | 80 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/coding.h: -------------------------------------------------------------------------------- 1 | /* coding.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | 24 | #ifndef WOLF_CRYPT_CODING_H 25 | #define WOLF_CRYPT_CODING_H 26 | 27 | #include 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | 34 | WOLFSSL_API int Base64_Decode(const byte* in, word32 inLen, byte* out, 35 | word32* outLen); 36 | 37 | #if defined(OPENSSL_EXTRA) || defined(SESSION_CERTS) || defined(WOLFSSL_KEY_GEN) \ 38 | || defined(WOLFSSL_CERT_GEN) || defined(HAVE_WEBSERVER) || !defined(NO_DSA) 39 | #ifndef WOLFSSL_BASE64_ENCODE 40 | #define WOLFSSL_BASE64_ENCODE 41 | #endif 42 | #endif 43 | 44 | 45 | #ifdef WOLFSSL_BASE64_ENCODE 46 | enum Escaped { 47 | WC_STD_ENC = 0, /* normal \n line ending encoding */ 48 | WC_ESC_NL_ENC, /* use escape sequence encoding */ 49 | WC_NO_NL_ENC /* no encoding at all */ 50 | }; /* Encoding types */ 51 | 52 | /* encode isn't */ 53 | WOLFSSL_API 54 | int Base64_Encode(const byte* in, word32 inLen, byte* out, 55 | word32* outLen); 56 | WOLFSSL_API 57 | int Base64_EncodeEsc(const byte* in, word32 inLen, byte* out, 58 | word32* outLen); 59 | WOLFSSL_API 60 | int Base64_Encode_NoNl(const byte* in, word32 inLen, byte* out, 61 | word32* outLen); 62 | #endif 63 | 64 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(HAVE_FIPS) \ 65 | || defined(HAVE_ECC_CDH) 66 | WOLFSSL_API 67 | int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen); 68 | WOLFSSL_API 69 | int Base16_Encode(const byte* in, word32 inLen, byte* out, word32* outLen); 70 | #endif 71 | 72 | 73 | #ifdef __cplusplus 74 | } /* extern "C" */ 75 | #endif 76 | 77 | #endif /* WOLF_CRYPT_CODING_H */ 78 | 79 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/compress.h: -------------------------------------------------------------------------------- 1 | /* compress.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_COMPRESS_H 24 | #define WOLF_CRYPT_COMPRESS_H 25 | 26 | #include 27 | 28 | #ifdef HAVE_LIBZ 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | 35 | #define COMPRESS_FIXED 1 36 | 37 | 38 | WOLFSSL_API int wc_Compress(byte*, word32, const byte*, word32, word32); 39 | WOLFSSL_API int wc_DeCompress(byte*, word32, const byte*, word32); 40 | 41 | 42 | #ifdef __cplusplus 43 | } /* extern "C" */ 44 | #endif 45 | 46 | 47 | #endif /* HAVE_LIBZ */ 48 | #endif /* WOLF_CRYPT_COMPRESS_H */ 49 | 50 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/cpuid.h: -------------------------------------------------------------------------------- 1 | /* cpuid.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | 24 | #ifndef WOLF_CRYPT_CPUID_H 25 | #define WOLF_CRYPT_CPUID_H 26 | 27 | 28 | #include 29 | 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | #if defined(WOLFSSL_X86_64_BUILD) || defined(USE_INTEL_SPEEDUP) || \ 36 | defined(WOLFSSL_AESNI) 37 | #define CPUID_AVX1 0x0001 38 | #define CPUID_AVX2 0x0002 39 | #define CPUID_RDRAND 0x0004 40 | #define CPUID_RDSEED 0x0008 41 | #define CPUID_BMI2 0x0010 /* MULX, RORX */ 42 | #define CPUID_AESNI 0x0020 43 | #define CPUID_ADX 0x0040 /* ADCX, ADOX */ 44 | 45 | #define IS_INTEL_AVX1(f) ((f) & CPUID_AVX1) 46 | #define IS_INTEL_AVX2(f) ((f) & CPUID_AVX2) 47 | #define IS_INTEL_RDRAND(f) ((f) & CPUID_RDRAND) 48 | #define IS_INTEL_RDSEED(f) ((f) & CPUID_RDSEED) 49 | #define IS_INTEL_BMI2(f) ((f) & CPUID_BMI2) 50 | #define IS_INTEL_AESNI(f) ((f) & CPUID_AESNI) 51 | #define IS_INTEL_ADX(f) ((f) & CPUID_ADX) 52 | 53 | void cpuid_set_flags(void); 54 | word32 cpuid_get_flags(void); 55 | #endif 56 | 57 | #ifdef __cplusplus 58 | } /* extern "C" */ 59 | #endif 60 | 61 | 62 | #endif /* WOLF_CRYPT_CPUID_H */ 63 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/des3.h: -------------------------------------------------------------------------------- 1 | /* des3.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_DES3_H 24 | #define WOLF_CRYPT_DES3_H 25 | 26 | #include 27 | 28 | #ifndef NO_DES3 29 | 30 | #ifdef HAVE_FIPS 31 | /* included for fips @wc_fips */ 32 | #include 33 | #endif 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #ifndef HAVE_FIPS /* to avoid redefinition of macros */ 40 | 41 | #ifdef WOLFSSL_ASYNC_CRYPT 42 | #include 43 | #endif 44 | 45 | enum { 46 | DES_ENC_TYPE = 2, /* cipher unique type */ 47 | DES3_ENC_TYPE = 3, /* cipher unique type */ 48 | DES_BLOCK_SIZE = 8, 49 | DES_KS_SIZE = 32, 50 | 51 | DES_ENCRYPTION = 0, 52 | DES_DECRYPTION = 1 53 | }; 54 | 55 | #define DES_IVLEN 8 56 | #define DES_KEYLEN 8 57 | #define DES3_IVLEN 8 58 | #define DES3_KEYLEN 24 59 | 60 | 61 | #if defined(STM32_CRYPTO) 62 | enum { 63 | DES_CBC = 0, 64 | DES_ECB = 1 65 | }; 66 | #endif 67 | 68 | 69 | /* DES encryption and decryption */ 70 | typedef struct Des { 71 | word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ 72 | word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */ 73 | word32 key[DES_KS_SIZE]; 74 | } Des; 75 | 76 | 77 | /* DES3 encryption and decryption */ 78 | typedef struct Des3 { 79 | word32 key[3][DES_KS_SIZE]; 80 | word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ 81 | word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */ 82 | #ifdef WOLFSSL_ASYNC_CRYPT 83 | const byte* key_raw; 84 | const byte* iv_raw; 85 | WC_ASYNC_DEV asyncDev; 86 | #endif 87 | void* heap; 88 | } Des3; 89 | #endif /* HAVE_FIPS */ 90 | 91 | 92 | WOLFSSL_API int wc_Des_SetKey(Des* des, const byte* key, 93 | const byte* iv, int dir); 94 | WOLFSSL_API void wc_Des_SetIV(Des* des, const byte* iv); 95 | WOLFSSL_API int wc_Des_CbcEncrypt(Des* des, byte* out, 96 | const byte* in, word32 sz); 97 | WOLFSSL_API int wc_Des_CbcDecrypt(Des* des, byte* out, 98 | const byte* in, word32 sz); 99 | WOLFSSL_API int wc_Des_EcbEncrypt(Des* des, byte* out, 100 | const byte* in, word32 sz); 101 | WOLFSSL_API int wc_Des3_EcbEncrypt(Des3* des, byte* out, 102 | const byte* in, word32 sz); 103 | 104 | /* ECB decrypt same process as encrypt but with decrypt key */ 105 | #define wc_Des_EcbDecrypt wc_Des_EcbEncrypt 106 | #define wc_Des3_EcbDecrypt wc_Des3_EcbEncrypt 107 | 108 | WOLFSSL_API int wc_Des3_SetKey(Des3* des, const byte* key, 109 | const byte* iv,int dir); 110 | WOLFSSL_API int wc_Des3_SetIV(Des3* des, const byte* iv); 111 | WOLFSSL_API int wc_Des3_CbcEncrypt(Des3* des, byte* out, 112 | const byte* in,word32 sz); 113 | WOLFSSL_API int wc_Des3_CbcDecrypt(Des3* des, byte* out, 114 | const byte* in,word32 sz); 115 | 116 | /* These are only required when using either: 117 | static memory (WOLFSSL_STATIC_MEMORY) or asynchronous (WOLFSSL_ASYNC_CRYPT) */ 118 | WOLFSSL_API int wc_Des3Init(Des3*, void*, int); 119 | WOLFSSL_API void wc_Des3Free(Des3*); 120 | 121 | #ifdef __cplusplus 122 | } /* extern "C" */ 123 | #endif 124 | 125 | #endif /* NO_DES3 */ 126 | #endif /* WOLF_CRYPT_DES3_H */ 127 | 128 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/dh.h: -------------------------------------------------------------------------------- 1 | /* dh.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_DH_H 24 | #define WOLF_CRYPT_DH_H 25 | 26 | #include 27 | 28 | #ifndef NO_DH 29 | 30 | #include 31 | #include 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | #ifdef WOLFSSL_ASYNC_CRYPT 38 | #include 39 | #endif 40 | typedef struct DhParams { 41 | const byte* p; 42 | word32 p_len; 43 | const byte* g; 44 | word32 g_len; 45 | } DhParams; 46 | 47 | /* Diffie-Hellman Key */ 48 | typedef struct DhKey { 49 | mp_int p, g; /* group parameters */ 50 | void* heap; 51 | #ifdef WOLFSSL_ASYNC_CRYPT 52 | WC_ASYNC_DEV asyncDev; 53 | #endif 54 | } DhKey; 55 | 56 | 57 | #ifdef HAVE_FFDHE_2048 58 | WOLFSSL_API const DhParams* wc_Dh_ffdhe2048_Get(void); 59 | #endif 60 | #ifdef HAVE_FFDHE_3072 61 | WOLFSSL_API const DhParams* wc_Dh_ffdhe3072_Get(void); 62 | #endif 63 | #ifdef HAVE_FFDHE_4096 64 | WOLFSSL_API const DhParams* wc_Dh_ffdhe4096_Get(void); 65 | #endif 66 | #ifdef HAVE_FFDHE_6144 67 | WOLFSSL_API const DhParams* wc_Dh_ffdhe6144_Get(void); 68 | #endif 69 | #ifdef HAVE_FFDHE_8192 70 | WOLFSSL_API const DhParams* wc_Dh_ffdhe8192_Get(void); 71 | #endif 72 | 73 | WOLFSSL_API int wc_InitDhKey(DhKey* key); 74 | WOLFSSL_API int wc_InitDhKey_ex(DhKey* key, void* heap, int devId); 75 | WOLFSSL_API void wc_FreeDhKey(DhKey* key); 76 | 77 | WOLFSSL_API int wc_DhGenerateKeyPair(DhKey* key, WC_RNG* rng, byte* priv, 78 | word32* privSz, byte* pub, word32* pubSz); 79 | WOLFSSL_API int wc_DhAgree(DhKey* key, byte* agree, word32* agreeSz, 80 | const byte* priv, word32 privSz, const byte* otherPub, 81 | word32 pubSz); 82 | 83 | WOLFSSL_API int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, 84 | word32); 85 | WOLFSSL_API int wc_DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g, 86 | word32 gSz); 87 | WOLFSSL_API int wc_DhParamsLoad(const byte* input, word32 inSz, byte* p, 88 | word32* pInOutSz, byte* g, word32* gInOutSz); 89 | WOLFSSL_API int wc_DhCheckPubKey(DhKey* key, const byte* pub, word32 pubSz); 90 | 91 | #ifdef __cplusplus 92 | } /* extern "C" */ 93 | #endif 94 | 95 | #endif /* NO_DH */ 96 | #endif /* WOLF_CRYPT_DH_H */ 97 | 98 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/dsa.h: -------------------------------------------------------------------------------- 1 | /* dsa.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_DSA_H 24 | #define WOLF_CRYPT_DSA_H 25 | 26 | #include 27 | 28 | #ifndef NO_DSA 29 | 30 | #include 31 | #include 32 | 33 | /* for DSA reverse compatibility */ 34 | #define InitDsaKey wc_InitDsaKey 35 | #define FreeDsaKey wc_FreeDsaKey 36 | #define DsaSign wc_DsaSign 37 | #define DsaVerify wc_DsaVerify 38 | #define DsaPublicKeyDecode wc_DsaPublicKeyDecode 39 | #define DsaPrivateKeyDecode wc_DsaPrivateKeyDecode 40 | #define DsaKeyToDer wc_DsaKeyToDer 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | 47 | enum { 48 | DSA_PUBLIC = 0, 49 | DSA_PRIVATE = 1 50 | }; 51 | 52 | /* DSA */ 53 | typedef struct DsaKey { 54 | mp_int p, q, g, y, x; 55 | int type; /* public or private */ 56 | void* heap; /* memory hint */ 57 | } DsaKey; 58 | 59 | WOLFSSL_API int wc_InitDsaKey(DsaKey* key); 60 | WOLFSSL_API int wc_InitDsaKey_h(DsaKey* key, void* h); 61 | WOLFSSL_API void wc_FreeDsaKey(DsaKey* key); 62 | WOLFSSL_API int wc_DsaSign(const byte* digest, byte* out, 63 | DsaKey* key, WC_RNG* rng); 64 | WOLFSSL_API int wc_DsaVerify(const byte* digest, const byte* sig, 65 | DsaKey* key, int* answer); 66 | WOLFSSL_API int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, 67 | DsaKey*, word32); 68 | WOLFSSL_API int wc_DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, 69 | DsaKey*, word32); 70 | WOLFSSL_API int wc_DsaKeyToDer(DsaKey* key, byte* output, word32 inLen); 71 | 72 | #ifdef WOLFSSL_KEY_GEN 73 | WOLFSSL_API int wc_MakeDsaKey(WC_RNG *rng, DsaKey *dsa); 74 | WOLFSSL_API int wc_MakeDsaParameters(WC_RNG *rng, int modulus_size, DsaKey *dsa); 75 | #endif 76 | 77 | #ifdef __cplusplus 78 | } /* extern "C" */ 79 | #endif 80 | 81 | #endif /* NO_DSA */ 82 | #endif /* WOLF_CRYPT_DSA_H */ 83 | 84 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/ed25519.h: -------------------------------------------------------------------------------- 1 | /* ed25519.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_ED25519_H 24 | #define WOLF_CRYPT_ED25519_H 25 | 26 | #include 27 | 28 | #ifdef HAVE_ED25519 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #ifdef WOLFSSL_ASYNC_CRYPT 36 | #include 37 | #endif 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | 44 | /* info about EdDSA curve specifically ed25519, defined as an elliptic curve 45 | over GF(p) */ 46 | /* 47 | 32, key size 48 | "ED25519", curve name 49 | "2^255-19", prime number 50 | "SHA512", hash function 51 | "-121665/121666", value of d 52 | */ 53 | 54 | #define ED25519_KEY_SIZE 32 /* private key only */ 55 | #define ED25519_SIG_SIZE 64 56 | 57 | #define ED25519_PUB_KEY_SIZE 32 /* compressed */ 58 | /* both private and public key */ 59 | #define ED25519_PRV_KEY_SIZE (ED25519_PUB_KEY_SIZE+ED25519_KEY_SIZE) 60 | 61 | 62 | #ifndef WC_ED25519KEY_TYPE_DEFINED 63 | typedef struct ed25519_key ed25519_key; 64 | #define WC_ED25519KEY_TYPE_DEFINED 65 | #endif 66 | 67 | /* An ED25519 Key */ 68 | struct ed25519_key { 69 | byte p[ED25519_PUB_KEY_SIZE]; /* compressed public key */ 70 | byte k[ED25519_PRV_KEY_SIZE]; /* private key : 32 secret -- 32 public */ 71 | #ifdef FREESCALE_LTC_ECC 72 | /* uncompressed point coordinates */ 73 | byte pointX[ED25519_KEY_SIZE]; /* recovered X coordinate */ 74 | byte pointY[ED25519_KEY_SIZE]; /* Y coordinate is the public key with The most significant bit of the final octet always zero. */ 75 | #endif 76 | #ifdef WOLFSSL_ASYNC_CRYPT 77 | WC_ASYNC_DEV asyncDev; 78 | #endif 79 | }; 80 | 81 | 82 | WOLFSSL_API 83 | int wc_ed25519_make_key(WC_RNG* rng, int keysize, ed25519_key* key); 84 | WOLFSSL_API 85 | int wc_ed25519_sign_msg(const byte* in, word32 inlen, byte* out, 86 | word32 *outlen, ed25519_key* key); 87 | WOLFSSL_API 88 | int wc_ed25519_verify_msg(const byte* sig, word32 siglen, const byte* msg, 89 | word32 msglen, int* stat, ed25519_key* key); 90 | WOLFSSL_API 91 | int wc_ed25519_init(ed25519_key* key); 92 | WOLFSSL_API 93 | void wc_ed25519_free(ed25519_key* key); 94 | WOLFSSL_API 95 | int wc_ed25519_import_public(const byte* in, word32 inLen, ed25519_key* key); 96 | WOLFSSL_API 97 | int wc_ed25519_import_private_only(const byte* priv, word32 privSz, 98 | ed25519_key* key); 99 | WOLFSSL_API 100 | int wc_ed25519_import_private_key(const byte* priv, word32 privSz, 101 | const byte* pub, word32 pubSz, ed25519_key* key); 102 | WOLFSSL_API 103 | int wc_ed25519_export_public(ed25519_key*, byte* out, word32* outLen); 104 | WOLFSSL_API 105 | int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen); 106 | WOLFSSL_API 107 | int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen); 108 | WOLFSSL_API 109 | int wc_ed25519_export_key(ed25519_key* key, 110 | byte* priv, word32 *privSz, 111 | byte* pub, word32 *pubSz); 112 | 113 | int wc_ed25519_check_key(ed25519_key* key); 114 | 115 | /* size helper */ 116 | WOLFSSL_API 117 | int wc_ed25519_size(ed25519_key* key); 118 | WOLFSSL_API 119 | int wc_ed25519_priv_size(ed25519_key* key); 120 | WOLFSSL_API 121 | int wc_ed25519_pub_size(ed25519_key* key); 122 | WOLFSSL_API 123 | int wc_ed25519_sig_size(ed25519_key* key); 124 | 125 | #ifdef __cplusplus 126 | } /* extern "C" */ 127 | #endif 128 | 129 | #endif /* HAVE_ED25519 */ 130 | #endif /* WOLF_CRYPT_ED25519_H */ 131 | 132 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/fips_test.h: -------------------------------------------------------------------------------- 1 | /* fips_test.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | 24 | #ifndef WOLF_CRYPT_FIPS_TEST_H 25 | #define WOLF_CRYPT_FIPS_TEST_H 26 | 27 | #include 28 | 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | /* Known Answer Test string inputs are hex, internal */ 35 | CYASSL_LOCAL int DoKnownAnswerTests(char*, int); 36 | 37 | 38 | /* FIPS failure callback */ 39 | typedef void(*wolfCrypt_fips_cb)(int ok, int err, const char* hash); 40 | 41 | /* Public set function */ 42 | CYASSL_API int wolfCrypt_SetCb_fips(wolfCrypt_fips_cb cbf); 43 | 44 | /* Public get status functions */ 45 | CYASSL_API int wolfCrypt_GetStatus_fips(void); 46 | CYASSL_API const char* wolfCrypt_GetCoreHash_fips(void); 47 | 48 | #ifdef HAVE_FORCE_FIPS_FAILURE 49 | /* Public function to force failure mode for operational testing */ 50 | CYASSL_API int wolfCrypt_SetStatus_fips(int); 51 | #endif 52 | 53 | 54 | #ifdef __cplusplus 55 | } /* extern "C" */ 56 | #endif 57 | 58 | #endif /* WOLF_CRYPT_FIPS_TEST_H */ 59 | 60 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/ge_operations.h: -------------------------------------------------------------------------------- 1 | /* ge_operations.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | /* Based On Daniel J Bernstein's ed25519 Public Domain ref10 work. */ 24 | 25 | #ifndef WOLF_CRYPT_GE_OPERATIONS_H 26 | #define WOLF_CRYPT_GE_OPERATIONS_H 27 | 28 | #include 29 | 30 | #ifdef HAVE_ED25519 31 | 32 | #include 33 | 34 | /* 35 | ge means group element. 36 | 37 | Here the group is the set of pairs (x,y) of field elements (see fe.h) 38 | satisfying -x^2 + y^2 = 1 + d x^2y^2 39 | where d = -121665/121666. 40 | 41 | Representations: 42 | ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z 43 | ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT 44 | ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T 45 | ge_precomp (Duif): (y+x,y-x,2dxy) 46 | */ 47 | 48 | #ifdef ED25519_SMALL 49 | typedef byte ge[F25519_SIZE]; 50 | #elif defined(CURVED25519_X64) 51 | typedef int64_t ge[4]; 52 | #elif defined(CURVED25519_128BIT) 53 | typedef int64_t ge[5]; 54 | #else 55 | typedef int32_t ge[10]; 56 | #endif 57 | 58 | typedef struct { 59 | ge X; 60 | ge Y; 61 | ge Z; 62 | } ge_p2; 63 | 64 | typedef struct { 65 | ge X; 66 | ge Y; 67 | ge Z; 68 | ge T; 69 | } ge_p3; 70 | 71 | 72 | WOLFSSL_LOCAL int ge_compress_key(byte* out, const byte* xIn, const byte* yIn, 73 | word32 keySz); 74 | WOLFSSL_LOCAL int ge_frombytes_negate_vartime(ge_p3 *,const unsigned char *); 75 | 76 | WOLFSSL_LOCAL int ge_double_scalarmult_vartime(ge_p2 *,const unsigned char *, 77 | const ge_p3 *,const unsigned char *); 78 | // for ESP8266 79 | WOLFSSL_LOCAL int ge_double_scalarmult_vartime_lowmem(ge_p2 *,const unsigned char *, 80 | const ge_p3 *,const unsigned char *); 81 | WOLFSSL_LOCAL void ge_scalarmult_base(ge_p3 *,const unsigned char *); 82 | WOLFSSL_LOCAL void sc_reduce(byte* s); 83 | WOLFSSL_LOCAL void sc_muladd(byte* s, const byte* a, const byte* b, 84 | const byte* c); 85 | WOLFSSL_LOCAL void ge_tobytes(unsigned char *,const ge_p2 *); 86 | WOLFSSL_LOCAL void ge_p3_tobytes(unsigned char *,const ge_p3 *); 87 | 88 | 89 | #ifndef ED25519_SMALL 90 | typedef struct { 91 | ge X; 92 | ge Y; 93 | ge Z; 94 | ge T; 95 | } ge_p1p1; 96 | 97 | typedef struct { 98 | ge yplusx; 99 | ge yminusx; 100 | ge xy2d; 101 | } ge_precomp; 102 | 103 | typedef struct { 104 | ge YplusX; 105 | ge YminusX; 106 | ge Z; 107 | ge T2d; 108 | } ge_cached; 109 | 110 | #endif /* !ED25519_SMALL */ 111 | 112 | #endif /* HAVE_ED25519 */ 113 | 114 | #endif /* WOLF_CRYPT_GE_OPERATIONS_H */ 115 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/hc128.h: -------------------------------------------------------------------------------- 1 | /* hc128.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_HC128_H 24 | #define WOLF_CRYPT_HC128_H 25 | 26 | #include 27 | 28 | #ifndef NO_HC128 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | enum { 35 | HC128_ENC_TYPE = 6 /* cipher unique type */ 36 | }; 37 | 38 | /* HC-128 stream cipher */ 39 | typedef struct HC128 { 40 | word32 T[1024]; /* P[i] = T[i]; Q[i] = T[1024 + i ]; */ 41 | word32 X[16]; 42 | word32 Y[16]; 43 | word32 counter1024; /* counter1024 = i mod 1024 at the ith step */ 44 | word32 key[8]; 45 | word32 iv[8]; 46 | #ifdef XSTREAM_ALIGN 47 | void* heap; /* heap hint, currently XMALLOC only used with aligning */ 48 | #endif 49 | } HC128; 50 | 51 | 52 | WOLFSSL_API int wc_Hc128_Process(HC128*, byte*, const byte*, word32); 53 | WOLFSSL_API int wc_Hc128_SetKey(HC128*, const byte* key, const byte* iv); 54 | 55 | WOLFSSL_LOCAL int wc_Hc128_SetHeap(HC128* ctx, void* heap); 56 | 57 | #ifdef __cplusplus 58 | } /* extern "C" */ 59 | #endif 60 | 61 | #endif /* HAVE_HC128 */ 62 | #endif /* WOLF_CRYPT_HC128_H */ 63 | 64 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/idea.h: -------------------------------------------------------------------------------- 1 | /* idea.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_IDEA_H 24 | #define WOLF_CRYPT_IDEA_H 25 | 26 | #include 27 | 28 | #ifdef HAVE_IDEA 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | enum { 35 | IDEA_MODULO = 0x10001, /* 2^16+1 */ 36 | IDEA_2EXP16 = 0x10000, /* 2^16 */ 37 | IDEA_MASK = 0xFFFF, /* 16 bits set to one */ 38 | IDEA_ROUNDS = 8, /* number of rounds for IDEA */ 39 | IDEA_SK_NUM = (6*IDEA_ROUNDS + 4), /* number of subkeys */ 40 | IDEA_KEY_SIZE = 16, /* size of key in bytes */ 41 | IDEA_BLOCK_SIZE = 8, /* size of IDEA blocks in bytes */ 42 | IDEA_IV_SIZE = 8, /* size of IDEA IV in bytes */ 43 | IDEA_ENCRYPTION = 0, 44 | IDEA_DECRYPTION = 1 45 | }; 46 | 47 | /* IDEA encryption and decryption */ 48 | typedef struct Idea { 49 | word32 reg[IDEA_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ 50 | word32 tmp[IDEA_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ 51 | word16 skey[IDEA_SK_NUM]; /* 832 bits expanded key */ 52 | } Idea; 53 | 54 | WOLFSSL_API int wc_IdeaSetKey(Idea *idea, const byte* key, word16 keySz, 55 | const byte *iv, int dir); 56 | WOLFSSL_API int wc_IdeaSetIV(Idea *idea, const byte* iv); 57 | WOLFSSL_API int wc_IdeaCipher(Idea *idea, byte* out, const byte* in); 58 | WOLFSSL_API int wc_IdeaCbcEncrypt(Idea *idea, byte* out, 59 | const byte* in, word32 len); 60 | WOLFSSL_API int wc_IdeaCbcDecrypt(Idea *idea, byte* out, 61 | const byte* in, word32 len); 62 | #ifdef __cplusplus 63 | } /* extern "C" */ 64 | #endif 65 | 66 | #endif /* HAVE_IDEA */ 67 | #endif /* WOLF_CRYPT_IDEA_H */ 68 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/logging.h: -------------------------------------------------------------------------------- 1 | /* logging.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | /* submitted by eof */ 24 | 25 | 26 | #ifndef WOLFSSL_LOGGING_H 27 | #define WOLFSSL_LOGGING_H 28 | 29 | #include 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | 36 | enum CYA_Log_Levels { 37 | ERROR_LOG = 0, 38 | INFO_LOG, 39 | ENTER_LOG, 40 | LEAVE_LOG, 41 | OTHER_LOG 42 | }; 43 | 44 | typedef void (*wolfSSL_Logging_cb)(const int logLevel, 45 | const char *const logMessage); 46 | 47 | WOLFSSL_API int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb log_function); 48 | 49 | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) 50 | WOLFSSL_LOCAL int wc_LoggingInit(void); 51 | WOLFSSL_LOCAL int wc_LoggingCleanup(void); 52 | WOLFSSL_LOCAL int wc_AddErrorNode(int error, int line, char* buf, 53 | char* file); 54 | WOLFSSL_LOCAL int wc_PeekErrorNode(int index, const char **file, 55 | const char **reason, int *line); 56 | WOLFSSL_LOCAL void wc_RemoveErrorNode(int index); 57 | WOLFSSL_LOCAL void wc_ClearErrorNodes(void); 58 | WOLFSSL_API int wc_SetLoggingHeap(void* h); 59 | #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) 60 | WOLFSSL_API void wc_ERR_print_errors_fp(FILE* fp); 61 | #endif 62 | #endif /* defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) */ 63 | 64 | #ifdef DEBUG_WOLFSSL 65 | #if defined(_WIN32) 66 | #if defined(INTIME_RTOS) 67 | #define __func__ NULL 68 | #else 69 | #define __func__ __FUNCTION__ 70 | #endif 71 | #endif 72 | 73 | /* a is prepended to m and b is appended, creating a log msg a + m + b */ 74 | #define WOLFSSL_LOG_CAT(a, m, b) #a " " m " " #b 75 | 76 | void WOLFSSL_ENTER(const char* msg); 77 | void WOLFSSL_LEAVE(const char* msg, int ret); 78 | #define WOLFSSL_STUB(m) \ 79 | WOLFSSL_MSG(WOLFSSL_LOG_CAT(wolfSSL Stub, m, not implemented)) 80 | 81 | void WOLFSSL_MSG(const char* msg); 82 | void WOLFSSL_BUFFER(const byte* buffer, word32 length); 83 | 84 | #else /* DEBUG_WOLFSSL */ 85 | 86 | #define WOLFSSL_ENTER(m) 87 | #define WOLFSSL_LEAVE(m, r) 88 | #define WOLFSSL_STUB(m) 89 | 90 | #define WOLFSSL_MSG(m) 91 | #define WOLFSSL_BUFFER(b, l) 92 | 93 | #endif /* DEBUG_WOLFSSL */ 94 | 95 | #if (defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX)) || defined(WOLFSSL_HAPROXY) 96 | #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) 97 | void WOLFSSL_ERROR_LINE(int err, const char* func, unsigned int line, 98 | const char* file, void* ctx); 99 | #define WOLFSSL_ERROR(x) WOLFSSL_ERROR_LINE((x), __func__, __LINE__, __FILE__,NULL) 100 | #else 101 | void WOLFSSL_ERROR(int); 102 | #endif 103 | #else 104 | #define WOLFSSL_ERROR(e) 105 | #endif 106 | 107 | #ifdef __cplusplus 108 | } 109 | #endif 110 | #endif /* WOLFSSL_LOGGING_H */ 111 | 112 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/md2.h: -------------------------------------------------------------------------------- 1 | /* md2.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_MD2_H 24 | #define WOLF_CRYPT_MD2_H 25 | 26 | #include 27 | 28 | #ifdef WOLFSSL_MD2 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | /* in bytes */ 35 | enum { 36 | MD2 = 6, /* hash type unique */ 37 | MD2_BLOCK_SIZE = 16, 38 | MD2_DIGEST_SIZE = 16, 39 | MD2_PAD_SIZE = 16, 40 | MD2_X_SIZE = 48 41 | }; 42 | 43 | 44 | /* Md2 digest */ 45 | typedef struct Md2 { 46 | word32 count; /* bytes % PAD_SIZE */ 47 | byte X[MD2_X_SIZE]; 48 | byte C[MD2_BLOCK_SIZE]; 49 | byte buffer[MD2_BLOCK_SIZE]; 50 | } Md2; 51 | 52 | 53 | WOLFSSL_API void wc_InitMd2(Md2*); 54 | WOLFSSL_API void wc_Md2Update(Md2*, const byte*, word32); 55 | WOLFSSL_API void wc_Md2Final(Md2*, byte*); 56 | WOLFSSL_API int wc_Md2Hash(const byte*, word32, byte*); 57 | 58 | 59 | #ifdef __cplusplus 60 | } /* extern "C" */ 61 | #endif 62 | 63 | #endif /* WOLFSSL_MD2 */ 64 | #endif /* WOLF_CRYPT_MD2_H */ 65 | 66 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/md4.h: -------------------------------------------------------------------------------- 1 | /* md4.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_MD4_H 24 | #define WOLF_CRYPT_MD4_H 25 | 26 | #include 27 | 28 | #ifndef NO_MD4 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | /* in bytes */ 35 | enum { 36 | MD4_BLOCK_SIZE = 64, 37 | MD4_DIGEST_SIZE = 16, 38 | MD4_PAD_SIZE = 56 39 | }; 40 | 41 | 42 | /* MD4 digest */ 43 | typedef struct Md4 { 44 | word32 buffLen; /* in bytes */ 45 | word32 loLen; /* length in bytes */ 46 | word32 hiLen; /* length in bytes */ 47 | word32 digest[MD4_DIGEST_SIZE / sizeof(word32)]; 48 | word32 buffer[MD4_BLOCK_SIZE / sizeof(word32)]; 49 | } Md4; 50 | 51 | 52 | WOLFSSL_API void wc_InitMd4(Md4*); 53 | WOLFSSL_API void wc_Md4Update(Md4*, const byte*, word32); 54 | WOLFSSL_API void wc_Md4Final(Md4*, byte*); 55 | 56 | 57 | #ifdef __cplusplus 58 | } /* extern "C" */ 59 | #endif 60 | 61 | #endif /* NO_MD4 */ 62 | #endif /* WOLF_CRYPT_MD4_H */ 63 | 64 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/md5.h: -------------------------------------------------------------------------------- 1 | /* md5.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_MD5_H 24 | #define WOLF_CRYPT_MD5_H 25 | 26 | #include 27 | 28 | #ifndef NO_MD5 29 | 30 | #ifdef HAVE_FIPS 31 | #define wc_InitMd5 InitMd5 32 | #define wc_Md5Update Md5Update 33 | #define wc_Md5Final Md5Final 34 | #define wc_Md5Hash Md5Hash 35 | #endif 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | #ifndef NO_OLD_WC_NAMES 42 | #define Md5 wc_Md5 43 | #define MD5 WC_MD5 44 | #define MD5_BLOCK_SIZE WC_MD5_BLOCK_SIZE 45 | #define MD5_DIGEST_SIZE WC_MD5_DIGEST_SIZE 46 | #define WC_MD5_PAD_SIZE WC_MD5_PAD_SIZE 47 | #endif 48 | 49 | /* in bytes */ 50 | enum { 51 | WC_MD5 = 0, /* hash type unique */ 52 | WC_MD5_BLOCK_SIZE = 64, 53 | WC_MD5_DIGEST_SIZE = 16, 54 | WC_MD5_PAD_SIZE = 56 55 | }; 56 | 57 | #ifdef WOLFSSL_MICROCHIP_PIC32MZ 58 | #include 59 | #endif 60 | #ifdef WOLFSSL_ASYNC_CRYPT 61 | #include 62 | #endif 63 | 64 | #ifdef WOLFSSL_TI_HASH 65 | #include "wolfssl/wolfcrypt/port/ti/ti-hash.h" 66 | #else 67 | 68 | /* MD5 digest */ 69 | typedef struct wc_Md5 { 70 | word32 buffLen; /* in bytes */ 71 | word32 loLen; /* length in bytes */ 72 | word32 hiLen; /* length in bytes */ 73 | word32 buffer[WC_MD5_BLOCK_SIZE / sizeof(word32)]; 74 | #ifdef WOLFSSL_PIC32MZ_HASH 75 | word32 digest[PIC32_DIGEST_SIZE / sizeof(word32)]; 76 | #else 77 | word32 digest[WC_MD5_DIGEST_SIZE / sizeof(word32)]; 78 | #endif 79 | void* heap; 80 | #ifdef WOLFSSL_PIC32MZ_HASH 81 | hashUpdCache cache; /* cache for updates */ 82 | #endif 83 | #if defined(STM32_HASH) && defined(WOLFSSL_STM32_CUBEMX) 84 | HASH_HandleTypeDef hashHandle; 85 | #endif 86 | #ifdef WOLFSSL_ASYNC_CRYPT 87 | WC_ASYNC_DEV asyncDev; 88 | #endif /* WOLFSSL_ASYNC_CRYPT */ 89 | } wc_Md5; 90 | 91 | #endif /* WOLFSSL_TI_HASH */ 92 | 93 | WOLFSSL_API int wc_InitMd5(wc_Md5*); 94 | WOLFSSL_API int wc_InitMd5_ex(wc_Md5*, void*, int); 95 | WOLFSSL_API int wc_Md5Update(wc_Md5*, const byte*, word32); 96 | WOLFSSL_API int wc_Md5Final(wc_Md5*, byte*); 97 | WOLFSSL_API void wc_Md5Free(wc_Md5*); 98 | 99 | WOLFSSL_API int wc_Md5GetHash(wc_Md5*, byte*); 100 | WOLFSSL_API int wc_Md5Copy(wc_Md5*, wc_Md5*); 101 | 102 | #ifdef WOLFSSL_PIC32MZ_HASH 103 | WOLFSSL_API void wc_Md5SizeSet(wc_Md5* md5, word32 len); 104 | #endif 105 | 106 | #ifdef __cplusplus 107 | } /* extern "C" */ 108 | #endif 109 | 110 | #endif /* NO_MD5 */ 111 | #endif /* WOLF_CRYPT_MD5_H */ 112 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/misc.h: -------------------------------------------------------------------------------- 1 | /* misc.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | 24 | #ifndef WOLF_CRYPT_MISC_H 25 | #define WOLF_CRYPT_MISC_H 26 | 27 | 28 | #include 29 | 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | 36 | #ifdef NO_INLINE 37 | WOLFSSL_LOCAL 38 | word32 rotlFixed(word32, word32); 39 | WOLFSSL_LOCAL 40 | word32 rotrFixed(word32, word32); 41 | 42 | WOLFSSL_LOCAL 43 | word32 ByteReverseWord32(word32); 44 | WOLFSSL_LOCAL 45 | void ByteReverseWords(word32*, const word32*, word32); 46 | 47 | WOLFSSL_LOCAL 48 | void XorWords(wolfssl_word*, const wolfssl_word*, word32); 49 | WOLFSSL_LOCAL 50 | void xorbuf(void*, const void*, word32); 51 | 52 | WOLFSSL_LOCAL 53 | void ForceZero(const void*, word32); 54 | 55 | WOLFSSL_LOCAL 56 | int ConstantCompare(const byte*, const byte*, int); 57 | 58 | #ifdef WORD64_AVAILABLE 59 | WOLFSSL_LOCAL 60 | word64 rotlFixed64(word64, word64); 61 | WOLFSSL_LOCAL 62 | word64 rotrFixed64(word64, word64); 63 | 64 | WOLFSSL_LOCAL 65 | word64 ByteReverseWord64(word64); 66 | WOLFSSL_LOCAL 67 | void ByteReverseWords64(word64*, const word64*, word32); 68 | #endif /* WORD64_AVAILABLE */ 69 | 70 | #ifndef WOLFSSL_HAVE_MIN 71 | #if defined(HAVE_FIPS) && !defined(min) /* so ifdef check passes */ 72 | #define min min 73 | #endif 74 | WOLFSSL_LOCAL word32 min(word32 a, word32 b); 75 | #endif 76 | 77 | #ifndef WOLFSSL_HAVE_MAX 78 | #if defined(HAVE_FIPS) && !defined(max) /* so ifdef check passes */ 79 | #define max max 80 | #endif 81 | WOLFSSL_LOCAL word32 max(word32 a, word32 b); 82 | #endif /* WOLFSSL_HAVE_MAX */ 83 | 84 | 85 | void c32to24(word32 in, word24 out); 86 | void c16toa(word16 u16, byte* c); 87 | void c32toa(word32 u32, byte* c); 88 | void c24to32(const word24 u24, word32* u32); 89 | void ato16(const byte* c, word16* u16); 90 | void ato24(const byte* c, word32* u24); 91 | void ato32(const byte* c, word32* u32); 92 | word32 btoi(byte b); 93 | 94 | #endif /* NO_INLINE */ 95 | 96 | 97 | #ifdef __cplusplus 98 | } /* extern "C" */ 99 | #endif 100 | 101 | 102 | #endif /* WOLF_CRYPT_MISC_H */ 103 | 104 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/mpi_superclass.h: -------------------------------------------------------------------------------- 1 | /* mpi_superclass.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | 24 | /* super class file for PK algos */ 25 | 26 | /* default ... include all MPI */ 27 | #define LTM_ALL 28 | 29 | /* RSA only (does not support DH/DSA/ECC) */ 30 | /* #define SC_RSA_1 */ 31 | 32 | /* For reference.... On an Athlon64 optimizing for speed... 33 | 34 | LTM's mpi.o with all functions [striped] is 142KiB in size. 35 | 36 | */ 37 | 38 | /* Works for RSA only, mpi.o is 68KiB */ 39 | #ifdef SC_RSA_1 40 | #define BN_MP_SHRINK_C 41 | #define BN_MP_LCM_C 42 | #define BN_MP_PRIME_RANDOM_EX_C 43 | #define BN_MP_INVMOD_C 44 | #define BN_MP_GCD_C 45 | #define BN_MP_MOD_C 46 | #define BN_MP_MULMOD_C 47 | #define BN_MP_ADDMOD_C 48 | #define BN_MP_EXPTMOD_C 49 | #define BN_MP_SET_INT_C 50 | #define BN_MP_INIT_MULTI_C 51 | #define BN_MP_CLEAR_MULTI_C 52 | #define BN_MP_UNSIGNED_BIN_SIZE_C 53 | #define BN_MP_TO_UNSIGNED_BIN_C 54 | #define BN_MP_MOD_D_C 55 | #define BN_MP_PRIME_RABIN_MILLER_TRIALS_C 56 | #define BN_REVERSE_C 57 | #define BN_PRIME_TAB_C 58 | 59 | /* other modifiers */ 60 | #define BN_MP_DIV_SMALL /* Slower division, not critical */ 61 | 62 | /* here we are on the last pass so we turn things off. The functions classes are still there 63 | * but we remove them specifically from the build. This also invokes tweaks in functions 64 | * like removing support for even moduli, etc... 65 | */ 66 | #ifdef LTM_LAST 67 | #undef BN_MP_TOOM_MUL_C 68 | #undef BN_MP_TOOM_SQR_C 69 | #undef BN_MP_KARATSUBA_MUL_C 70 | #undef BN_MP_KARATSUBA_SQR_C 71 | #undef BN_MP_REDUCE_C 72 | #undef BN_MP_REDUCE_SETUP_C 73 | #undef BN_MP_DR_IS_MODULUS_C 74 | #undef BN_MP_DR_SETUP_C 75 | #undef BN_MP_DR_REDUCE_C 76 | #undef BN_MP_REDUCE_IS_2K_C 77 | #undef BN_MP_REDUCE_2K_SETUP_C 78 | #undef BN_MP_REDUCE_2K_C 79 | #undef BN_S_MP_EXPTMOD_C 80 | #undef BN_MP_DIV_3_C 81 | #undef BN_S_MP_MUL_HIGH_DIGS_C 82 | #undef BN_FAST_S_MP_MUL_HIGH_DIGS_C 83 | #undef BN_FAST_MP_INVMOD_C 84 | 85 | /* To safely undefine these you have to make sure your RSA key won't exceed the Comba threshold 86 | * which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines] 87 | * which means roughly speaking you can handle up to 2536-bit RSA keys with these defined without 88 | * trouble. 89 | */ 90 | #undef BN_S_MP_MUL_DIGS_C 91 | #undef BN_S_MP_SQR_C 92 | #undef BN_MP_MONTGOMERY_REDUCE_C 93 | #endif 94 | 95 | #endif 96 | 97 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/pkcs12.h: -------------------------------------------------------------------------------- 1 | /* pkcs12.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_PKCS12_H 24 | #define WOLF_CRYPT_PKCS12_H 25 | 26 | #include 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | #ifndef WOLFSSL_TYPES_DEFINED /* do not redeclare from ssl.h */ 33 | typedef struct WC_PKCS12 WC_PKCS12; 34 | #endif 35 | 36 | typedef struct WC_DerCertList { /* dereferenced in ssl.c */ 37 | byte* buffer; 38 | word32 bufferSz; 39 | struct WC_DerCertList* next; 40 | } WC_DerCertList; 41 | 42 | 43 | 44 | WOLFSSL_API WC_PKCS12* wc_PKCS12_new(void); 45 | WOLFSSL_API void wc_PKCS12_free(WC_PKCS12* pkcs12); 46 | WOLFSSL_API int wc_d2i_PKCS12(const byte* der, word32 derSz, WC_PKCS12* pkcs12); 47 | WOLFSSL_API int wc_PKCS12_parse(WC_PKCS12* pkcs12, const char* psw, 48 | byte** pkey, word32* pkeySz, byte** cert, word32* certSz, 49 | WC_DerCertList** ca); 50 | 51 | WOLFSSL_LOCAL int wc_PKCS12_SetHeap(WC_PKCS12* pkcs12, void* heap); 52 | WOLFSSL_LOCAL void* wc_PKCS12_GetHeap(WC_PKCS12* pkcs12); 53 | 54 | 55 | #ifdef __cplusplus 56 | } /* extern "C" */ 57 | #endif 58 | 59 | #endif /* WOLF_CRYPT_PKCS12_H */ 60 | 61 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/poly1305.h: -------------------------------------------------------------------------------- 1 | /* poly1305.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_POLY1305_H 24 | #define WOLF_CRYPT_POLY1305_H 25 | 26 | #include 27 | 28 | #ifdef HAVE_POLY1305 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | /* auto detect between 32bit / 64bit */ 35 | #if defined(__SIZEOF_INT128__) && defined(__LP64__) 36 | #define WC_HAS_SIZEOF_INT128_64BIT 37 | #endif 38 | 39 | #if defined(_MSC_VER) && defined(_M_X64) 40 | #define WC_HAS_MSVC_64BIT 41 | #endif 42 | 43 | #if (defined(__GNUC__) && defined(__LP64__) && \ 44 | ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4)))) 45 | #define WC_HAS_GCC_4_4_64BIT 46 | #endif 47 | 48 | #ifdef USE_INTEL_SPEEDUP 49 | #elif (defined(WC_HAS_SIZEOF_INT128_64BIT) || defined(WC_HAS_MSVC_64BIT) || \ 50 | defined(WC_HAS_GCC_4_4_64BIT)) 51 | #define POLY130564 52 | #else 53 | #define POLY130532 54 | #endif 55 | 56 | enum { 57 | POLY1305 = 7, 58 | POLY1305_BLOCK_SIZE = 16, 59 | POLY1305_DIGEST_SIZE = 16, 60 | }; 61 | 62 | #define WC_POLY1305_PAD_SZ 16 63 | #define WC_POLY1305_MAC_SZ 16 64 | 65 | /* Poly1305 state */ 66 | typedef struct Poly1305 { 67 | #ifdef USE_INTEL_SPEEDUP 68 | word64 r[3]; 69 | word64 h[3]; 70 | word64 pad[2]; 71 | word64 hh[14]; 72 | word32 r0[8]; 73 | word32 r1[8]; 74 | word32 r2[8]; 75 | word32 r3[8]; 76 | word32 r4[8]; 77 | word32* rp[4]; 78 | word64 hibit[4]; 79 | size_t leftover; 80 | unsigned char buffer[4*POLY1305_BLOCK_SIZE]; 81 | unsigned char finished; 82 | unsigned char started; 83 | #else 84 | #if defined(POLY130564) 85 | word64 r[3]; 86 | word64 h[3]; 87 | word64 pad[2]; 88 | #else 89 | word32 r[5]; 90 | word32 h[5]; 91 | word32 pad[4]; 92 | #endif 93 | size_t leftover; 94 | unsigned char buffer[POLY1305_BLOCK_SIZE]; 95 | unsigned char finished; 96 | #endif 97 | } Poly1305; 98 | 99 | /* does init */ 100 | 101 | WOLFSSL_API int wc_Poly1305SetKey(Poly1305* poly1305, const byte* key, 102 | word32 kySz); 103 | WOLFSSL_API int wc_Poly1305Update(Poly1305* poly1305, const byte*, word32); 104 | WOLFSSL_API int wc_Poly1305Final(Poly1305* poly1305, byte* tag); 105 | WOLFSSL_API int wc_Poly1305_MAC(Poly1305* ctx, byte* additional, word32 addSz, 106 | byte* input, word32 sz, byte* tag, word32 tagSz); 107 | #ifdef __cplusplus 108 | } /* extern "C" */ 109 | #endif 110 | 111 | #endif /* HAVE_POLY1305 */ 112 | #endif /* WOLF_CRYPT_POLY1305_H */ 113 | 114 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/pwdbased.h: -------------------------------------------------------------------------------- 1 | /* pwdbased.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_PWDBASED_H 24 | #define WOLF_CRYPT_PWDBASED_H 25 | 26 | #include 27 | 28 | #ifndef NO_PWDBASED 29 | 30 | #ifndef NO_MD5 31 | #include /* for hash type */ 32 | #endif 33 | 34 | #include 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | /* 41 | * hashType renamed to typeH to avoid shadowing global declaration here: 42 | * wolfssl/wolfcrypt/asn.h line 173 in enum Oid_Types 43 | */ 44 | WOLFSSL_API int wc_PBKDF1(byte* output, const byte* passwd, int pLen, 45 | const byte* salt, int sLen, int iterations, int kLen, 46 | int typeH); 47 | WOLFSSL_API int wc_PBKDF2(byte* output, const byte* passwd, int pLen, 48 | const byte* salt, int sLen, int iterations, int kLen, 49 | int typeH); 50 | WOLFSSL_API int wc_PKCS12_PBKDF(byte* output, const byte* passwd, int pLen, 51 | const byte* salt, int sLen, int iterations, 52 | int kLen, int typeH, int purpose); 53 | WOLFSSL_API int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd,int passLen, 54 | const byte* salt, int saltLen, int iterations, int kLen, 55 | int hashType, int id, void* heap); 56 | 57 | #ifdef HAVE_SCRYPT 58 | WOLFSSL_API int wc_scrypt(byte* output, const byte* passwd, int passLen, 59 | const byte* salt, int saltLen, int cost, 60 | int blockSize, int parallel, int dkLen); 61 | #endif 62 | 63 | /* helper functions */ 64 | WOLFSSL_LOCAL int GetDigestSize(int typeH); 65 | WOLFSSL_LOCAL int GetPKCS12HashSizes(int typeH, word32* v, word32* u); 66 | WOLFSSL_LOCAL int DoPKCS12Hash(int typeH, byte* buffer, word32 totalLen, 67 | byte* Ai, word32 u, int iterations); 68 | 69 | 70 | #ifdef __cplusplus 71 | } /* extern "C" */ 72 | #endif 73 | 74 | #endif /* NO_PWDBASED */ 75 | #endif /* WOLF_CRYPT_PWDBASED_H */ 76 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/rabbit.h: -------------------------------------------------------------------------------- 1 | /* rabbit.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_RABBIT_H 24 | #define WOLF_CRYPT_RABBIT_H 25 | 26 | #include 27 | 28 | #ifndef NO_RABBIT 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | 35 | enum { 36 | RABBIT_ENC_TYPE = 5 /* cipher unique type */ 37 | }; 38 | 39 | 40 | /* Rabbit Context */ 41 | typedef struct RabbitCtx { 42 | word32 x[8]; 43 | word32 c[8]; 44 | word32 carry; 45 | } RabbitCtx; 46 | 47 | 48 | /* Rabbit stream cipher */ 49 | typedef struct Rabbit { 50 | RabbitCtx masterCtx; 51 | RabbitCtx workCtx; 52 | #ifdef XSTREAM_ALIGN 53 | void* heap; /* heap hint, currently XMALLOC only used with aligning */ 54 | #endif 55 | } Rabbit; 56 | 57 | 58 | WOLFSSL_API int wc_RabbitProcess(Rabbit*, byte*, const byte*, word32); 59 | WOLFSSL_API int wc_RabbitSetKey(Rabbit*, const byte* key, const byte* iv); 60 | 61 | WOLFSSL_LOCAL int wc_Rabbit_SetHeap(Rabbit* ctx, void* heap); 62 | 63 | #ifdef __cplusplus 64 | } /* extern "C" */ 65 | #endif 66 | 67 | #endif /* NO_RABBIT */ 68 | #endif /* WOLF_CRYPT_RABBIT_H */ 69 | 70 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/ripemd.h: -------------------------------------------------------------------------------- 1 | /* ripemd.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_RIPEMD_H 24 | #define WOLF_CRYPT_RIPEMD_H 25 | 26 | #include 27 | 28 | #ifdef WOLFSSL_RIPEMD 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | 35 | /* in bytes */ 36 | enum { 37 | RIPEMD = 3, /* hash type unique */ 38 | RIPEMD_BLOCK_SIZE = 64, 39 | RIPEMD_DIGEST_SIZE = 20, 40 | RIPEMD_PAD_SIZE = 56 41 | }; 42 | 43 | 44 | /* RipeMd 160 digest */ 45 | typedef struct RipeMd { 46 | word32 buffLen; /* in bytes */ 47 | word32 loLen; /* length in bytes */ 48 | word32 hiLen; /* length in bytes */ 49 | word32 digest[RIPEMD_DIGEST_SIZE / sizeof(word32)]; 50 | word32 buffer[RIPEMD_BLOCK_SIZE / sizeof(word32)]; 51 | } RipeMd; 52 | 53 | 54 | WOLFSSL_API int wc_InitRipeMd(RipeMd*); 55 | WOLFSSL_API int wc_RipeMdUpdate(RipeMd*, const byte*, word32); 56 | WOLFSSL_API int wc_RipeMdFinal(RipeMd*, byte*); 57 | 58 | 59 | #ifdef __cplusplus 60 | } /* extern "C" */ 61 | #endif 62 | 63 | #endif /* WOLFSSL_RIPEMD */ 64 | #endif /* WOLF_CRYPT_RIPEMD_H */ 65 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/sha.h: -------------------------------------------------------------------------------- 1 | /* sha.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_SHA_H 24 | #define WOLF_CRYPT_SHA_H 25 | 26 | #include 27 | 28 | #ifndef NO_SHA 29 | 30 | #ifdef HAVE_FIPS 31 | #define wc_Sha Sha 32 | #define WC_SHA SHA 33 | #define WC_SHA_BLOCK_SIZE SHA_BLOCK_SIZE 34 | #define WC_SHA_DIGEST_SIZE SHA_DIGEST_SIZE 35 | #define WC_SHA_PAD_SIZE SHA_PAD_SIZE 36 | 37 | /* for fips @wc_fips */ 38 | #include 39 | #endif 40 | 41 | #ifdef FREESCALE_LTC_SHA 42 | #include "fsl_ltc.h" 43 | #endif 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | #ifndef HAVE_FIPS /* avoid redefining structs */ 50 | 51 | #ifdef WOLFSSL_MICROCHIP_PIC32MZ 52 | #include 53 | #endif 54 | #ifdef WOLFSSL_ASYNC_CRYPT 55 | #include 56 | #endif 57 | 58 | #ifndef NO_OLD_WC_NAMES 59 | #define Sha wc_Sha 60 | #define SHA WC_SHA 61 | #define SHA_BLOCK_SIZE WC_SHA_BLOCK_SIZE 62 | #define SHA_DIGEST_SIZE WC_SHA_DIGEST_SIZE 63 | #define SHA_PAD_SIZE WC_SHA_PAD_SIZE 64 | #endif 65 | 66 | /* in bytes */ 67 | enum { 68 | WC_SHA = 1, /* hash type unique */ 69 | WC_SHA_BLOCK_SIZE = 64, 70 | WC_SHA_DIGEST_SIZE = 20, 71 | WC_SHA_PAD_SIZE = 56 72 | }; 73 | 74 | 75 | #ifndef WOLFSSL_TI_HASH 76 | /* Sha digest */ 77 | typedef struct wc_Sha { 78 | #ifdef FREESCALE_LTC_SHA 79 | ltc_hash_ctx_t ctx; 80 | #else 81 | word32 buffLen; /* in bytes */ 82 | word32 loLen; /* length in bytes */ 83 | word32 hiLen; /* length in bytes */ 84 | word32 buffer[WC_SHA_BLOCK_SIZE / sizeof(word32)]; 85 | #ifdef WOLFSSL_PIC32MZ_HASH 86 | word32 digest[PIC32_DIGEST_SIZE / sizeof(word32)]; 87 | #else 88 | word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)]; 89 | #endif 90 | void* heap; 91 | #ifdef WOLFSSL_PIC32MZ_HASH 92 | hashUpdCache cache; /* cache for updates */ 93 | #endif 94 | #if defined(STM32_HASH) && defined(WOLFSSL_STM32_CUBEMX) 95 | HASH_HandleTypeDef hashHandle; 96 | #endif 97 | #ifdef WOLFSSL_ASYNC_CRYPT 98 | WC_ASYNC_DEV asyncDev; 99 | #endif /* WOLFSSL_ASYNC_CRYPT */ 100 | #endif /* FREESCALE_LTC_SHA */ 101 | } wc_Sha; 102 | 103 | #else 104 | #include "wolfssl/wolfcrypt/port/ti/ti-hash.h" 105 | #endif /* WOLFSSL_TI_HASH */ 106 | 107 | 108 | #endif /* HAVE_FIPS */ 109 | 110 | WOLFSSL_API int wc_InitSha(wc_Sha*); 111 | WOLFSSL_API int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId); 112 | WOLFSSL_API int wc_ShaUpdate(wc_Sha*, const byte*, word32); 113 | WOLFSSL_API int wc_ShaFinal(wc_Sha*, byte*); 114 | WOLFSSL_API void wc_ShaFree(wc_Sha*); 115 | 116 | WOLFSSL_API int wc_ShaGetHash(wc_Sha*, byte*); 117 | WOLFSSL_API int wc_ShaCopy(wc_Sha*, wc_Sha*); 118 | 119 | #ifdef WOLFSSL_PIC32MZ_HASH 120 | WOLFSSL_API void wc_ShaSizeSet(wc_Sha* sha, word32 len); 121 | #endif 122 | 123 | #ifdef __cplusplus 124 | } /* extern "C" */ 125 | #endif 126 | 127 | #endif /* NO_SHA */ 128 | #endif /* WOLF_CRYPT_SHA_H */ 129 | 130 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/sha3.h: -------------------------------------------------------------------------------- 1 | /* sha3.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_SHA3_H 24 | #define WOLF_CRYPT_SHA3_H 25 | 26 | #include 27 | 28 | #ifdef WOLFSSL_SHA3 29 | 30 | #ifdef HAVE_FIPS 31 | /* for fips @wc_fips */ 32 | #include 33 | #endif 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #ifndef HAVE_FIPS /* avoid redefinition of structs */ 40 | 41 | #ifdef WOLFSSL_ASYNC_CRYPT 42 | #include 43 | #endif 44 | 45 | /* in bytes */ 46 | enum { 47 | SHA3_224 = 10, /* hash type unique */ 48 | SHA3_224_DIGEST_SIZE = 28, 49 | SHA3_224_COUNT = 18, 50 | 51 | SHA3_256 = 11, /* hash type unique */ 52 | SHA3_256_DIGEST_SIZE = 32, 53 | SHA3_256_COUNT = 17, 54 | 55 | SHA3_384 = 12, /* hash type unique */ 56 | SHA3_384_DIGEST_SIZE = 48, 57 | SHA3_384_COUNT = 13, 58 | 59 | SHA3_512 = 13, /* hash type unique */ 60 | SHA3_512_DIGEST_SIZE = 64, 61 | SHA3_512_COUNT = 9 62 | }; 63 | 64 | 65 | #ifdef WOLFSSL_XILINX_CRYPT 66 | #include "wolfssl/wolfcrypt/port/xilinx/xil-sha3.h" 67 | #else 68 | /* Sha3 digest */ 69 | typedef struct Sha3 { 70 | /* State data that is processed for each block. */ 71 | word64 s[25]; 72 | /* Unprocessed message data. */ 73 | byte t[200]; 74 | /* Index into unprocessed data to place next message byte. */ 75 | byte i; 76 | 77 | void* heap; 78 | 79 | #ifdef WOLFSSL_ASYNC_CRYPT 80 | WC_ASYNC_DEV asyncDev; 81 | #endif /* WOLFSSL_ASYNC_CRYPT */ 82 | } Sha3; 83 | #endif 84 | #endif /* HAVE_FIPS */ 85 | 86 | WOLFSSL_API int wc_InitSha3_224(Sha3*, void*, int); 87 | WOLFSSL_API int wc_Sha3_224_Update(Sha3*, const byte*, word32); 88 | WOLFSSL_API int wc_Sha3_224_Final(Sha3*, byte*); 89 | WOLFSSL_API void wc_Sha3_224_Free(Sha3*); 90 | WOLFSSL_API int wc_Sha3_224_GetHash(Sha3*, byte*); 91 | WOLFSSL_API int wc_Sha3_224_Copy(Sha3* src, Sha3* dst); 92 | 93 | WOLFSSL_API int wc_InitSha3_256(Sha3*, void*, int); 94 | WOLFSSL_API int wc_Sha3_256_Update(Sha3*, const byte*, word32); 95 | WOLFSSL_API int wc_Sha3_256_Final(Sha3*, byte*); 96 | WOLFSSL_API void wc_Sha3_256_Free(Sha3*); 97 | WOLFSSL_API int wc_Sha3_256_GetHash(Sha3*, byte*); 98 | WOLFSSL_API int wc_Sha3_256_Copy(Sha3* src, Sha3* dst); 99 | 100 | WOLFSSL_API int wc_InitSha3_384(Sha3*, void*, int); 101 | WOLFSSL_API int wc_Sha3_384_Update(Sha3*, const byte*, word32); 102 | WOLFSSL_API int wc_Sha3_384_Final(Sha3*, byte*); 103 | WOLFSSL_API void wc_Sha3_384_Free(Sha3*); 104 | WOLFSSL_API int wc_Sha3_384_GetHash(Sha3*, byte*); 105 | WOLFSSL_API int wc_Sha3_384_Copy(Sha3* src, Sha3* dst); 106 | 107 | WOLFSSL_API int wc_InitSha3_512(Sha3*, void*, int); 108 | WOLFSSL_API int wc_Sha3_512_Update(Sha3*, const byte*, word32); 109 | WOLFSSL_API int wc_Sha3_512_Final(Sha3*, byte*); 110 | WOLFSSL_API void wc_Sha3_512_Free(Sha3*); 111 | WOLFSSL_API int wc_Sha3_512_GetHash(Sha3*, byte*); 112 | WOLFSSL_API int wc_Sha3_512_Copy(Sha3* src, Sha3* dst); 113 | 114 | #ifdef __cplusplus 115 | } /* extern "C" */ 116 | #endif 117 | 118 | #endif /* WOLFSSL_SHA3 */ 119 | #endif /* WOLF_CRYPT_SHA3_H */ 120 | 121 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/signature.h: -------------------------------------------------------------------------------- 1 | /* signature.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_SIGNATURE_H 24 | #define WOLF_CRYPT_SIGNATURE_H 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | enum wc_SignatureType { 35 | WC_SIGNATURE_TYPE_NONE = 0, 36 | WC_SIGNATURE_TYPE_ECC = 1, 37 | WC_SIGNATURE_TYPE_RSA = 2, 38 | WC_SIGNATURE_TYPE_RSA_W_ENC = 3, /* Adds DER header via wc_EncodeSignature */ 39 | }; 40 | 41 | WOLFSSL_API int wc_SignatureGetSize(enum wc_SignatureType sig_type, 42 | const void* key, word32 key_len); 43 | 44 | WOLFSSL_API int wc_SignatureVerify( 45 | enum wc_HashType hash_type, enum wc_SignatureType sig_type, 46 | const byte* data, word32 data_len, 47 | const byte* sig, word32 sig_len, 48 | const void* key, word32 key_len); 49 | 50 | WOLFSSL_API int wc_SignatureGenerate( 51 | enum wc_HashType hash_type, enum wc_SignatureType sig_type, 52 | const byte* data, word32 data_len, 53 | byte* sig, word32 *sig_len, 54 | const void* key, word32 key_len, 55 | WC_RNG* rng); 56 | 57 | #ifdef __cplusplus 58 | } /* extern "C" */ 59 | #endif 60 | 61 | #endif /* WOLF_CRYPT_SIGNATURE_H */ 62 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/sp.h: -------------------------------------------------------------------------------- 1 | /* sp.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | #ifndef WOLF_CRYPT_SP_H 24 | #define WOLF_CRYPT_SP_H 25 | 26 | #include 27 | 28 | #if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ 29 | defined(WOLFSSL_HAVE_SP_ECC) 30 | 31 | #include 32 | 33 | #include 34 | #include 35 | 36 | #if defined(NO_64BIT) || !defined(HAVE___UINT128_T) 37 | #define SP_WORD_SIZE 32 38 | #else 39 | #define SP_WORD_SIZE 64 40 | #endif 41 | 42 | #if !defined(WOLFSSL_X86_64_BUILD) || !defined(USE_INTEL_SPEEDUP) 43 | #if SP_WORD_SIZE == 32 44 | typedef int32_t sp_digit; 45 | #elif SP_WORD_SIZE == 64 46 | typedef int64_t sp_digit; 47 | typedef long int128_t __attribute__ ((mode(TI))); 48 | #else 49 | #error Word size not defined 50 | #endif 51 | #else 52 | #if SP_WORD_SIZE == 32 53 | typedef uint32_t sp_digit; 54 | #elif SP_WORD_SIZE == 64 55 | typedef uint64_t sp_digit; 56 | typedef unsigned long uint128_t __attribute__ ((mode(TI))); 57 | typedef long int128_t __attribute__ ((mode(TI))); 58 | #else 59 | #error Word size not defined 60 | #endif 61 | #endif 62 | 63 | #if defined(_MSC_VER) 64 | #define SP_NOINLINE __declspec(noinline) 65 | #elif defined(__GNUC__) 66 | #define SP_NOINLINE __attribute__((noinline)) 67 | #else 68 | #define 5P_NOINLINE 69 | #endif 70 | 71 | #ifdef __cplusplus 72 | extern "C" { 73 | #endif 74 | 75 | #ifdef WOLFSSL_HAVE_SP_RSA 76 | 77 | WOLFSSL_LOCAL int sp_RsaPublic_2048(const byte* in, word32 inLen, 78 | mp_int* em, mp_int* mm, byte* out, word32* outLen); 79 | WOLFSSL_LOCAL int sp_RsaPrivate_2048(const byte* in, word32 inLen, 80 | mp_int* dm, mp_int* pm, mp_int* qm, mp_int* dpm, mp_int* dqm, mp_int* qim, 81 | mp_int* mm, byte* out, word32* outLen); 82 | 83 | WOLFSSL_LOCAL int sp_RsaPublic_3072(const byte* in, word32 inLen, 84 | mp_int* em, mp_int* mm, byte* out, word32* outLen); 85 | WOLFSSL_LOCAL int sp_RsaPrivate_3072(const byte* in, word32 inLen, 86 | mp_int* dm, mp_int* pm, mp_int* qm, mp_int* dpm, mp_int* dqm, mp_int* qim, 87 | mp_int* mm, byte* out, word32* outLen); 88 | 89 | #endif /* WOLFSSL_HAVE_SP_RSA */ 90 | 91 | #ifdef WOLFSSL_HAVE_SP_DH 92 | 93 | WOLFSSL_LOCAL int sp_DhExp_2048(mp_int* base, const byte* exp, word32 expLen, 94 | mp_int* mod, byte* out, word32* outLen); 95 | WOLFSSL_LOCAL int sp_DhExp_3072(mp_int* base, const byte* exp, word32 expLen, 96 | mp_int* mod, byte* out, word32* outLen); 97 | 98 | #endif /* WOLFSSL_HAVE_SP_DH */ 99 | 100 | #ifdef WOLFSSL_HAVE_SP_ECC 101 | 102 | int sp_ecc_mulmod_256(mp_int* km, ecc_point* gm, ecc_point* rm, int map, 103 | void* heap); 104 | int sp_ecc_mulmod_base_256(mp_int* km, ecc_point* rm, int map, void* heap); 105 | 106 | int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap); 107 | int sp_ecc_secret_gen_256(mp_int* priv, ecc_point* pub, byte* out, 108 | word32* outlen, void* heap); 109 | int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, mp_int* priv, 110 | mp_int* rm, mp_int* sm, void* heap); 111 | int sp_ecc_verify_256(const byte* hash, word32 hashLen, mp_int* pX, mp_int* pY, 112 | mp_int* pZ, mp_int* r, mp_int* sm, int* res, void* heap); 113 | 114 | #endif /*ifdef WOLFSSL_HAVE_SP_ECC */ 115 | 116 | 117 | #ifdef __cplusplus 118 | } /* extern "C" */ 119 | #endif 120 | 121 | #endif /* WOLFSSL_HAVE_SP_RSA || WOLFSSL_HAVE_SP_DH || WOLFSSL_HAVE_SP_ECC */ 122 | 123 | #endif /* WOLF_CRYPT_SP_H */ 124 | 125 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/visibility.h: -------------------------------------------------------------------------------- 1 | /* visibility.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | /* Visibility control macros */ 24 | 25 | #ifndef WOLF_CRYPT_VISIBILITY_H 26 | #define WOLF_CRYPT_VISIBILITY_H 27 | 28 | 29 | /* for compatibility and so that fips is using same name of macro @wc_fips */ 30 | #ifdef HAVE_FIPS 31 | #include 32 | #define WOLFSSL_API CYASSL_API 33 | #define WOLFSSL_LOCAL CYASSL_LOCAL 34 | #else 35 | 36 | /* WOLFSSL_API is used for the public API symbols. 37 | It either imports or exports (or does nothing for static builds) 38 | 39 | WOLFSSL_LOCAL is used for non-API symbols (private). 40 | */ 41 | 42 | #if defined(BUILDING_WOLFSSL) 43 | #if defined(HAVE_VISIBILITY) && HAVE_VISIBILITY 44 | #define WOLFSSL_API __attribute__ ((visibility("default"))) 45 | #define WOLFSSL_LOCAL __attribute__ ((visibility("hidden"))) 46 | #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550) 47 | #define WOLFSSL_API __global 48 | #define WOLFSSL_LOCAL __hidden 49 | #elif defined(_MSC_VER) || defined(__MINGW32__) 50 | #if defined(WOLFSSL_DLL) 51 | #define WOLFSSL_API __declspec(dllexport) 52 | #else 53 | #define WOLFSSL_API 54 | #endif 55 | #define WOLFSSL_LOCAL 56 | #else 57 | #define WOLFSSL_API 58 | #define WOLFSSL_LOCAL 59 | #endif /* HAVE_VISIBILITY */ 60 | #else /* BUILDING_WOLFSSL */ 61 | #if defined(_MSC_VER) || defined(__MINGW32__) 62 | #if defined(WOLFSSL_DLL) 63 | #define WOLFSSL_API __declspec(dllimport) 64 | #else 65 | #define WOLFSSL_API 66 | #endif 67 | #define WOLFSSL_LOCAL 68 | #else 69 | #define WOLFSSL_API 70 | #define WOLFSSL_LOCAL 71 | #endif 72 | #endif /* BUILDING_WOLFSSL */ 73 | 74 | #endif /* HAVE_FIPS */ 75 | #endif /* WOLF_CRYPT_VISIBILITY_H */ 76 | 77 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/wc_encrypt.h: -------------------------------------------------------------------------------- 1 | /* wc_encrypt.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | 23 | 24 | #ifndef WOLF_CRYPT_ENCRYPT_H 25 | #define WOLF_CRYPT_ENCRYPT_H 26 | 27 | #include 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | #ifndef NO_AES 34 | WOLFSSL_API int wc_AesCbcEncryptWithKey(byte* out, const byte* in, word32 inSz, 35 | const byte* key, word32 keySz, 36 | const byte* iv); 37 | WOLFSSL_API int wc_AesCbcDecryptWithKey(byte* out, const byte* in, word32 inSz, 38 | const byte* key, word32 keySz, 39 | const byte* iv); 40 | #endif /* !NO_AES */ 41 | 42 | 43 | #ifndef NO_DES3 44 | WOLFSSL_API int wc_Des_CbcDecryptWithKey(byte* out, 45 | const byte* in, word32 sz, 46 | const byte* key, const byte* iv); 47 | WOLFSSL_API int wc_Des_CbcEncryptWithKey(byte* out, 48 | const byte* in, word32 sz, 49 | const byte* key, const byte* iv); 50 | WOLFSSL_API int wc_Des3_CbcEncryptWithKey(byte* out, 51 | const byte* in, word32 sz, 52 | const byte* key, const byte* iv); 53 | WOLFSSL_API int wc_Des3_CbcDecryptWithKey(byte* out, 54 | const byte* in, word32 sz, 55 | const byte* key, const byte* iv); 56 | #endif /* !NO_DES3 */ 57 | 58 | #ifdef __cplusplus 59 | } /* extern "C" */ 60 | #endif 61 | 62 | #endif /* WOLF_CRYPT_ENCRYPT_H */ 63 | 64 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/wolfevent.h: -------------------------------------------------------------------------------- 1 | /* wolfevent.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | #ifndef _WOLF_EVENT_H_ 23 | #define _WOLF_EVENT_H_ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | #ifndef SINGLE_THREADED 30 | #include 31 | #endif 32 | #ifdef HAVE_CAVIUM 33 | #include 34 | #endif 35 | 36 | #ifndef WOLFSSL_WOLFSSL_TYPE_DEFINED 37 | #define WOLFSSL_WOLFSSL_TYPE_DEFINED 38 | typedef struct WOLFSSL WOLFSSL; 39 | #endif 40 | typedef struct WOLF_EVENT WOLF_EVENT; 41 | #ifndef WOLFSSL_WOLFSSL_CTX_TYPE_DEFINED 42 | #define WOLFSSL_WOLFSSL_CTX_TYPE_DEFINED 43 | typedef struct WOLFSSL_CTX WOLFSSL_CTX; 44 | #endif 45 | 46 | typedef unsigned short WOLF_EVENT_FLAG; 47 | 48 | typedef enum WOLF_EVENT_TYPE { 49 | WOLF_EVENT_TYPE_NONE, 50 | #ifdef WOLFSSL_ASYNC_CRYPT 51 | WOLF_EVENT_TYPE_ASYNC_WOLFSSL, /* context is WOLFSSL* */ 52 | WOLF_EVENT_TYPE_ASYNC_WOLFCRYPT, /* context is WC_ASYNC_DEV */ 53 | WOLF_EVENT_TYPE_ASYNC_FIRST = WOLF_EVENT_TYPE_ASYNC_WOLFSSL, 54 | WOLF_EVENT_TYPE_ASYNC_LAST = WOLF_EVENT_TYPE_ASYNC_WOLFCRYPT, 55 | #endif /* WOLFSSL_ASYNC_CRYPT */ 56 | } WOLF_EVENT_TYPE; 57 | 58 | typedef enum WOLF_EVENT_STATE { 59 | WOLF_EVENT_STATE_READY, 60 | WOLF_EVENT_STATE_PENDING, 61 | WOLF_EVENT_STATE_DONE, 62 | } WOLF_EVENT_STATE; 63 | 64 | struct WOLF_EVENT { 65 | /* double linked list */ 66 | WOLF_EVENT* next; 67 | WOLF_EVENT* prev; 68 | 69 | void* context; 70 | union { 71 | void* ptr; 72 | #ifdef WOLFSSL_ASYNC_CRYPT 73 | struct WC_ASYNC_DEV* async; 74 | #endif 75 | } dev; 76 | #ifdef HAVE_CAVIUM 77 | CavReqId reqId; 78 | #endif 79 | #ifndef WC_NO_ASYNC_THREADING 80 | pthread_t threadId; 81 | #endif 82 | int ret; /* Async return code */ 83 | unsigned int flags; 84 | WOLF_EVENT_TYPE type; 85 | WOLF_EVENT_STATE state; 86 | }; 87 | 88 | enum WOLF_POLL_FLAGS { 89 | WOLF_POLL_FLAG_CHECK_HW = 0x01, 90 | }; 91 | 92 | typedef struct { 93 | WOLF_EVENT* head; /* head of queue */ 94 | WOLF_EVENT* tail; /* tail of queue */ 95 | #ifndef SINGLE_THREADED 96 | wolfSSL_Mutex lock; /* queue lock */ 97 | #endif 98 | int count; 99 | } WOLF_EVENT_QUEUE; 100 | 101 | 102 | #ifdef HAVE_WOLF_EVENT 103 | 104 | /* Event */ 105 | WOLFSSL_API int wolfEvent_Init(WOLF_EVENT* event, WOLF_EVENT_TYPE type, void* context); 106 | WOLFSSL_API int wolfEvent_Poll(WOLF_EVENT* event, WOLF_EVENT_FLAG flags); 107 | 108 | /* Event Queue */ 109 | WOLFSSL_API int wolfEventQueue_Init(WOLF_EVENT_QUEUE* queue); 110 | WOLFSSL_API int wolfEventQueue_Push(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event); 111 | WOLFSSL_API int wolfEventQueue_Pop(WOLF_EVENT_QUEUE* queue, WOLF_EVENT** event); 112 | WOLFSSL_API int wolfEventQueue_Poll(WOLF_EVENT_QUEUE* queue, void* context_filter, 113 | WOLF_EVENT** events, int maxEvents, WOLF_EVENT_FLAG flags, int* eventCount); 114 | WOLFSSL_API int wolfEventQueue_Count(WOLF_EVENT_QUEUE* queue); 115 | WOLFSSL_API void wolfEventQueue_Free(WOLF_EVENT_QUEUE* queue); 116 | 117 | /* the queue mutex must be locked prior to calling these */ 118 | WOLFSSL_API int wolfEventQueue_Add(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event); 119 | WOLFSSL_API int wolfEventQueue_Remove(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event); 120 | 121 | 122 | #endif /* HAVE_WOLF_EVENT */ 123 | 124 | 125 | #ifdef __cplusplus 126 | } /* extern "C" */ 127 | #endif 128 | 129 | #endif /* _WOLF_EVENT_H_ */ 130 | -------------------------------------------------------------------------------- /src/wolfssl/wolfcrypt/wolfmath.h: -------------------------------------------------------------------------------- 1 | /* wolfmath.h 2 | * 3 | * Copyright (C) 2006-2017 wolfSSL Inc. 4 | * 5 | * This file is part of wolfSSL. 6 | * 7 | * wolfSSL is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * wolfSSL is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 | */ 21 | 22 | #if defined(HAVE_WOLF_BIGINT) && !defined(WOLF_BIGINT_DEFINED) 23 | /* raw big integer */ 24 | typedef struct WC_BIGINT { 25 | byte* buf; 26 | word32 len; 27 | void* heap; 28 | } WC_BIGINT; 29 | 30 | #define WOLF_BIGINT_DEFINED 31 | #endif 32 | 33 | 34 | /* only define functions if mp_int has been declared */ 35 | #ifdef MP_INT_DEFINED 36 | 37 | #ifndef __WOLFMATH_H__ 38 | #define __WOLFMATH_H__ 39 | 40 | /* timing resistance array */ 41 | #if !defined(WC_NO_CACHE_RESISTANT) && \ 42 | ((defined(HAVE_ECC) && defined(ECC_TIMING_RESISTANT)) || \ 43 | (defined(USE_FAST_MATH) && defined(TFM_TIMING_RESISTANT))) 44 | 45 | extern const wolfssl_word wc_off_on_addr[2]; 46 | #endif 47 | 48 | /* common math functions */ 49 | int get_digit_count(mp_int* a); 50 | mp_digit get_digit(mp_int* a, int n); 51 | int get_rand_digit(WC_RNG* rng, mp_digit* d); 52 | int mp_rand(mp_int* a, int digits, WC_RNG* rng); 53 | 54 | 55 | #ifdef HAVE_WOLF_BIGINT 56 | void wc_bigint_init(WC_BIGINT* a); 57 | int wc_bigint_alloc(WC_BIGINT* a, word32 sz); 58 | int wc_bigint_from_unsigned_bin(WC_BIGINT* a, const byte* in, word32 inlen); 59 | int wc_bigint_to_unsigned_bin(WC_BIGINT* a, byte* out, word32* outlen); 60 | void wc_bigint_zero(WC_BIGINT* a); 61 | void wc_bigint_free(WC_BIGINT* a); 62 | 63 | int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst); 64 | int wc_bigint_to_mp(WC_BIGINT* src, mp_int* dst); 65 | #endif /* HAVE_WOLF_BIGINT */ 66 | 67 | #endif /* __WOLFMATH_H__ */ 68 | 69 | #endif /* MP_INT_DEFINED */ 70 | -------------------------------------------------------------------------------- /translations/README_cn.md: -------------------------------------------------------------------------------- 1 | # Arduino HomeKit ESP8266 2 | 3 | 4 | ## Apple HomeKit accessory server library for ESP8266 Arduino 5 | 6 | 本库是适用于[ESP8266 Arduino core](https://github.com/esp8266/Arduino) 的原生 Apple HomeKit 协议实现,无需额外的homeassistant、homebridge等服务器配置。可制作使用ESP8266芯片的独立运行的非商用的HomeKit配件。 7 | 8 | 本库的开发主要来自适用于[ESP-OPEN-RTOS](https://github.com/SuperHouse/esp-open-rtos) 的 [esp-homekit](https://github.com/maximkulkin/esp-homekit) 库,进行修改适配后适用于Arduino环境,大大降低上手难度与开发成本。另外,强烈推荐使用sloeber(for Eclipse), PlatformIO等较为现代化的IDE来敲代码。 9 | 10 | 适用于ESP32 Arduino的版本请移步[Arduino-HomeKit-ESP32](https://github.com/Mixiaoxiao/Arduino-HomeKit-ESP32). 运行在ESP32上的HomeKit性能是ESP8266的近10倍。 11 | 12 | 其他技术相关说明含有大量专用名词,不适合翻译,请参见原始的English版本。 13 | 14 | 15 | ## QQ群:686409089 16 | 17 | 更多对于ESP8266/ESP32开发以及HomeKit库的使用和交流,欢迎大家加群讨论: 18 | 19 | ![QQGroup](https://raw.github.com/Mixiaoxiao/Arduino-HomeKit-ESP8266/master/extras/qq_group_qrcode.png) 20 | --------------------------------------------------------------------------------