├── doc ├── breadboard.jpg └── Hacking Logitech Unifying Slides.pdf ├── src ├── aes.hpp ├── main.c ├── unifying_const.c ├── unifying_error.c ├── unifying_error.h ├── aes.h ├── unifying_buffer.c ├── unifying_buffer.h ├── unifying_utils.c ├── unifying_state.c ├── unifying.h ├── unifying_const.h ├── unifying_utils.h ├── unifying_state.h ├── unifying_data.c ├── aes.c ├── unifying_data.h └── unifying.c ├── library.properties ├── Makefile ├── .gitignore ├── README.md ├── examples └── UnifyingKeyboard │ └── UnifyingKeyboard.ino └── LICENSE /doc/breadboard.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decrazyo/unifying/HEAD/doc/breadboard.jpg -------------------------------------------------------------------------------- /doc/Hacking Logitech Unifying Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decrazyo/unifying/HEAD/doc/Hacking Logitech Unifying Slides.pdf -------------------------------------------------------------------------------- /src/aes.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _AES_HPP_ 2 | #define _AES_HPP_ 3 | 4 | #ifndef __cplusplus 5 | #error Do not include the hpp header in a c project! 6 | #endif //__cplusplus 7 | 8 | extern "C" { 9 | #include "aes.h" 10 | } 11 | 12 | #endif //_AES_HPP_ 13 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | 2 | #ifndef ARDUINO 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int main(int argc, char const *argv[]) 13 | { 14 | return 0; 15 | } 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=unifying 2 | version=0.1 3 | author=decrazyo 4 | maintainer=decrazyo 5 | sentence=FOSS re-implementation of the Logitech Unifying protocol 6 | paragraph=A compiler and hardware agnostic implementation of the Logitech Unifying protocol. This is provided for the purpose of creating custom keyboards and mice. 7 | category=Communication 8 | url=https://github.com/decrazyo/unifying 9 | architectures=* 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | CC = gcc 3 | CFLAGS = -Wall 4 | LDFLAGS = 5 | 6 | NAME = main 7 | SRC = src/ 8 | BIN = bin/ 9 | 10 | TARGET := $(BIN)$(NAME) 11 | SOURCES := $(wildcard $(SRC)*.c) 12 | OBJECTS := $(SOURCES:$(SRC)%.c=$(BIN)%.o) 13 | 14 | .PHONY: all 15 | all: $(BIN) $(TARGET) 16 | 17 | .PHONY: docs 18 | docs: 19 | doxygen 20 | 21 | .PHONY: clean 22 | clean: 23 | rm -f $(BIN)* 24 | 25 | $(BIN): 26 | mkdir -p bin 27 | 28 | $(BIN)%.o: $(SRC)%.c 29 | $(CC) $(CFLAGS) -c $< -o $@ 30 | 31 | $(TARGET): $(OBJECTS) 32 | $(CC) $(LDFLAGS) $^ -o $@ 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | # Doxygen documentation 55 | doc/html 56 | doc/latex 57 | -------------------------------------------------------------------------------- /src/unifying_const.c: -------------------------------------------------------------------------------- 1 | 2 | #include "unifying_const.h" 3 | 4 | const uint8_t unifying_aes_key_bitmask[UNIFYING_AES_BLOCK_LEN] = { 5 | 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xAA, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xAA, 0xFF, 0xFF 6 | }; 7 | 8 | const uint8_t unifying_aes_key_index[UNIFYING_AES_BLOCK_LEN] = { 9 | 0x07, 0x01, 0x00, 0x03, 0x0A, 0x02, 0x09, 0x0E, 0x08, 0x06, 0x0C, 0x05, 0x0D, 0x0F, 0x04, 0x0B 10 | }; 11 | 12 | const uint8_t unifying_aes_nonce_prefix[UNIFYING_AES_NONCE_PREFIX_LEN] = { 13 | 0x04, 0x14, 0x1D, 0x1F, 0x27, 0x28, 0x0D 14 | }; 15 | 16 | const uint8_t unifying_aes_nonce_suffix[UNIFYING_AES_NONCE_SUFFIX_LEN] = { 17 | 0x0A, 0x0D, 0x13, 0x26, 0x0E 18 | }; 19 | 20 | const uint8_t unifying_channels[UNIFYING_CHANNELS_LEN] = { 21 | 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77 22 | }; 23 | 24 | const uint8_t unifying_pairing_channels[UNIFYING_PAIRING_CHANNELS_LEN] = { 25 | 5, 8, 17, 32, 35, 41, 44, 62, 65, 71, 74 26 | }; 27 | 28 | const uint8_t unifying_pairing_address[UNIFYING_ADDRESS_LEN] = { 29 | 0xBB, 0x0A, 0xDC, 0xA5, 0x75 // MSB first. 30 | }; 31 | -------------------------------------------------------------------------------- /src/unifying_error.c: -------------------------------------------------------------------------------- 1 | 2 | #include "unifying_error.h" 3 | 4 | const char* unifying_error_name[UNIFYING_ERROR_COUNT] = { 5 | "SUCCESS", 6 | "ERROR", 7 | "NAME_LENGTH_ERROR", 8 | "SET_ADDRESS_ERROR", 9 | "SET_CHANNEL_ERROR", 10 | "TRANSMIT_ERROR", 11 | "RECEIVE_ERROR", 12 | "PAYLOAD_LENGTH_ERROR", 13 | "CHECKSUM_ERROR", 14 | "PAIR_ERROR", 15 | "PAIR_STEP_ERROR", 16 | "PAIR_ID_ERROR", 17 | "ENCRYPTION_ERROR", 18 | "BUFFER_ERROR", 19 | "BUFFER_FULL_ERROR", 20 | "BUFFER_EMPTY_ERROR", 21 | "CREATE_ERROR", 22 | }; 23 | 24 | const char* unifying_error_message[UNIFYING_ERROR_COUNT] = { 25 | "Success", 26 | "Generic error", 27 | "Device name is too long", 28 | "Failed to set the radio address", 29 | "Failed to set the radio channel", 30 | "Failed to transmit a payload", 31 | "Failed to receive a payload", 32 | "Payload's length does not match its expected length", 33 | "Payload's computed checksum does not match its stated checksum", 34 | "Generic pairing error", 35 | "Received a pairing response with an unexpected step", 36 | "Received a pairing response with an ID that does not match the requested ID", 37 | "Encryption failed", 38 | "Generic buffer error", 39 | "Buffer was full when it was expected to not be full", 40 | "Buffer was empty when it was expected to not be empty", 41 | "Failed to create a dynamically allocated object", 42 | }; 43 | 44 | const char* unifying_get_error_name(enum unifying_error err) 45 | { 46 | return unifying_error_name[(size_t) err]; 47 | } 48 | 49 | const char* unifying_get_error_message(enum unifying_error err) 50 | { 51 | return unifying_error_message[(size_t) err]; 52 | } 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Logitech Unifying Protocol Implementation 3 | 4 | ![Arduino nano and nRF24L01+ on breadboard](https://raw.githubusercontent.com/decrazyo/unifying/main/doc/breadboard.jpg) 5 | 6 | This project is an attempt to re-implement the proprietary Logitech Unifying protocol as a free and open C library. 7 | The library is intended to be Arduino compatible while remaining compiler and hardware agnostic. 8 | The goal of this project is to enable people to create custom keyboards and mice that are compatible with Logitech Unifying receivers. 9 | 10 | ## Example 11 | The provided Arduino example is dependent on the RF24 library. 12 | https://github.com/nRF24/RF24 13 | 14 | ## TODO 15 | - [ ] Add proper HID++ response payloads 16 | - [ ] Add more examples 17 | - [ ] General code cleanup 18 | 19 | ## Done 20 | - [x] Timing-critical packet transmission 21 | - [x] Pairing with a receiver 22 | - [x] HID++ error response payloads 23 | - [x] Encrypted keystroke payloads 24 | - [x] Add documentation 25 | - [x] Add mouse payloads 26 | - [x] Add multimedia payloads 27 | - [x] Add wake up payloads 28 | 29 | ## See also 30 | [Hacking Logitech Unifying DC612 talk](https://www.youtube.com/watch?v=10lE96BBOF8) 31 | [nRF24 pseudo-promiscuous mode](http://travisgoodspeed.blogspot.com/2011/02/promiscuity-is-nrf24l01s-duty.html) 32 | [KeySweeper](https://github.com/samyk/keysweeper) 33 | [MouseJack](https://github.com/BastilleResearch/mousejack) 34 | [KeyJack](https://github.com/BastilleResearch/keyjack) 35 | [KeySniffer](https://github.com/BastilleResearch/keysniffer) 36 | [Of Mice And Keyboards](https://www.icaria.de/posts/2016/11/of-mice-and-keyboards/) 37 | [Logitech HID++ Specification](https://drive.google.com/folderview?id=0BxbRzx7vEV7eWmgwazJ3NUFfQ28) 38 | [Official Logitech Firmware](https://github.com/Logitech/fw_updates) 39 | -------------------------------------------------------------------------------- /src/unifying_error.h: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * \file unifying_error.h 4 | * \brief Collection of error values, names, and messages. 5 | * 6 | * \todo Add a preprocessor option to omit error names and messages to save space. 7 | * \todo make error names and messages static. 8 | */ 9 | 10 | #ifndef UNIFYING_ERROR_H 11 | #define UNIFYING_ERROR_H 12 | 13 | #include 14 | 15 | /*! 16 | * Error values returns by various functions. 17 | */ 18 | enum unifying_error { 19 | /// Success. 20 | UNIFYING_SUCCESS = 0, 21 | /// Generic error. 22 | UNIFYING_ERROR, 23 | /// Device name is too long. 24 | UNIFYING_NAME_LENGTH_ERROR, 25 | /// Failed to set the radio address. 26 | UNIFYING_SET_ADDRESS_ERROR, 27 | /// Failed to set the radio channel. 28 | UNIFYING_SET_CHANNEL_ERROR, 29 | /// Failed to transmit a payload. 30 | UNIFYING_TRANSMIT_ERROR, 31 | /// Failed to receive a payload. 32 | UNIFYING_RECEIVE_ERROR, 33 | /// Payload's length does not match its expected length. 34 | UNIFYING_PAYLOAD_LENGTH_ERROR, 35 | /// Payload's computed checksum does not match its stated checksum. 36 | UNIFYING_CHECKSUM_ERROR, 37 | /// Generic pairing error. 38 | UNIFYING_PAIR_ERROR, 39 | /// Received a pairing response with an unexpected step. 40 | UNIFYING_PAIR_STEP_ERROR, 41 | /// Received a pairing response with an ID that does not match the requested ID. 42 | UNIFYING_PAIR_ID_ERROR, 43 | /// Encryption failed. 44 | UNIFYING_ENCRYPTION_ERROR, 45 | /// Generic buffer error. 46 | UNIFYING_BUFFER_ERROR, 47 | /// Buffer was full when it was expected to not be full. 48 | UNIFYING_BUFFER_FULL_ERROR, 49 | /// Buffer was empty when it was expected to not be empty. 50 | UNIFYING_BUFFER_EMPTY_ERROR, 51 | /// Failed to create a dynamically allocated object. 52 | UNIFYING_CREATE_ERROR, 53 | /// The number of errors that have been defined 54 | UNIFYING_ERROR_COUNT, 55 | }; 56 | 57 | /*! 58 | * Names of error values. 59 | */ 60 | extern const char* unifying_error_name[UNIFYING_ERROR_COUNT]; 61 | 62 | /*! 63 | * Messages associated with error values. 64 | */ 65 | extern const char* unifying_error_message[UNIFYING_ERROR_COUNT]; 66 | 67 | #ifdef __cplusplus 68 | extern "C" { 69 | #endif 70 | 71 | 72 | /*! 73 | * Get the name of the supplied error. 74 | * 75 | * \param[in] err Error value. 76 | * 77 | * \return Error name. 78 | */ 79 | const char* unifying_get_error_name(enum unifying_error err); 80 | 81 | /*! 82 | * Get the message of the supplied error. 83 | * 84 | * \param[in] err Error value. 85 | * 86 | * \return Error message. 87 | */ 88 | const char* unifying_get_error_message(enum unifying_error err); 89 | 90 | #ifdef __cplusplus 91 | } 92 | #endif 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /src/aes.h: -------------------------------------------------------------------------------- 1 | #ifndef _AES_H_ 2 | #define _AES_H_ 3 | 4 | #include 5 | 6 | // #define the macros below to 1/0 to enable/disable the mode of operation. 7 | // 8 | // CBC enables AES encryption in CBC-mode of operation. 9 | // CTR enables encryption in counter-mode. 10 | // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously. 11 | 12 | // The #ifndef-guard allows it to be configured before #include'ing or at compile time. 13 | #ifndef CBC 14 | #define CBC 1 15 | #endif 16 | 17 | #ifndef ECB 18 | #define ECB 1 19 | #endif 20 | 21 | #ifndef CTR 22 | #define CTR 1 23 | #endif 24 | 25 | 26 | #define AES128 1 27 | //#define AES192 1 28 | //#define AES256 1 29 | 30 | #define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only 31 | 32 | #if defined(AES256) && (AES256 == 1) 33 | #define AES_KEYLEN 32 34 | #define AES_keyExpSize 240 35 | #elif defined(AES192) && (AES192 == 1) 36 | #define AES_KEYLEN 24 37 | #define AES_keyExpSize 208 38 | #else 39 | #define AES_KEYLEN 16 // Key length in bytes 40 | #define AES_keyExpSize 176 41 | #endif 42 | 43 | struct AES_ctx 44 | { 45 | uint8_t RoundKey[AES_keyExpSize]; 46 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 47 | uint8_t Iv[AES_BLOCKLEN]; 48 | #endif 49 | }; 50 | 51 | void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key); 52 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 53 | void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv); 54 | void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv); 55 | #endif 56 | 57 | #if defined(ECB) && (ECB == 1) 58 | // buffer size is exactly AES_BLOCKLEN bytes; 59 | // you need only AES_init_ctx as IV is not used in ECB 60 | // NB: ECB is considered insecure for most uses 61 | void AES_ECB_encrypt(struct AES_ctx* ctx, uint8_t* buf); 62 | void AES_ECB_decrypt(struct AES_ctx* ctx, uint8_t* buf); 63 | 64 | #endif // #if defined(ECB) && (ECB == !) 65 | 66 | 67 | #if defined(CBC) && (CBC == 1) 68 | // buffer size MUST be mutile of AES_BLOCKLEN; 69 | // Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme 70 | // NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv() 71 | // no IV should ever be reused with the same key 72 | void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 73 | void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 74 | 75 | #endif // #if defined(CBC) && (CBC == 1) 76 | 77 | 78 | #if defined(CTR) && (CTR == 1) 79 | 80 | // Same function for encrypting as for decrypting. 81 | // IV is incremented for every block, and used after encryption as XOR-compliment for output 82 | // Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme 83 | // NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv() 84 | // no IV should ever be reused with the same key 85 | void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 86 | 87 | #endif // #if defined(CTR) && (CTR == 1) 88 | 89 | 90 | #endif //_AES_H_ 91 | -------------------------------------------------------------------------------- /src/unifying_buffer.c: -------------------------------------------------------------------------------- 1 | 2 | #include "unifying_buffer.h" 3 | 4 | enum unifying_error unifying_ring_buffer_init(struct unifying_ring_buffer* ring_buffer, void** buffer, uint8_t size) 5 | { 6 | if(!size) 7 | { 8 | return UNIFYING_BUFFER_ERROR; 9 | } 10 | 11 | ring_buffer->buffer = buffer; 12 | ring_buffer->size = size; 13 | ring_buffer->count = 0; 14 | ring_buffer->front = 0; 15 | ring_buffer->back = size - 1; 16 | return UNIFYING_SUCCESS; 17 | } 18 | 19 | struct unifying_ring_buffer* unifying_ring_buffer_create(uint8_t size) 20 | { 21 | if(!size) 22 | { 23 | return NULL; 24 | } 25 | 26 | struct unifying_ring_buffer* ring_buffer = malloc(sizeof(struct unifying_ring_buffer)); 27 | 28 | if(!ring_buffer) 29 | { 30 | return NULL; 31 | } 32 | 33 | void** buffer = malloc(size * sizeof(void*)); 34 | 35 | if(!buffer) 36 | { 37 | free(ring_buffer); 38 | return NULL; 39 | } 40 | 41 | unifying_ring_buffer_init(ring_buffer, buffer, size); 42 | return ring_buffer; 43 | } 44 | 45 | void unifying_ring_buffer_destroy(struct unifying_ring_buffer* ring_buffer) 46 | { 47 | free(ring_buffer->buffer); 48 | free(ring_buffer); 49 | } 50 | 51 | enum unifying_error unifying_ring_buffer_push_front(struct unifying_ring_buffer* ring_buffer, void* entry) 52 | { 53 | if(unifying_ring_buffer_full(ring_buffer)) 54 | { 55 | return UNIFYING_BUFFER_FULL_ERROR; 56 | } 57 | 58 | if(ring_buffer->front) 59 | { 60 | ring_buffer->front -= 1; 61 | } 62 | else 63 | { 64 | ring_buffer->front = ring_buffer->size - 1; 65 | } 66 | 67 | ring_buffer->buffer[ring_buffer->front] = entry; 68 | ring_buffer->count += 1; 69 | return UNIFYING_SUCCESS; 70 | } 71 | 72 | enum unifying_error unifying_ring_buffer_push_back(struct unifying_ring_buffer* ring_buffer, void* entry) 73 | { 74 | if(unifying_ring_buffer_full(ring_buffer)) 75 | { 76 | return UNIFYING_BUFFER_FULL_ERROR; 77 | } 78 | 79 | if(ring_buffer->back >= ring_buffer->size - 1) 80 | { 81 | ring_buffer->back = 0; 82 | } 83 | else { 84 | ring_buffer->back += 1; 85 | } 86 | 87 | ring_buffer->buffer[ring_buffer->back] = entry; 88 | ring_buffer->count += 1; 89 | return UNIFYING_SUCCESS; 90 | } 91 | 92 | void* unifying_ring_buffer_pop_front(struct unifying_ring_buffer* ring_buffer) 93 | { 94 | if(unifying_ring_buffer_empty(ring_buffer)) 95 | { 96 | return NULL; 97 | } 98 | 99 | void* entry = ring_buffer->buffer[ring_buffer->front]; 100 | ring_buffer->count -= 1; 101 | 102 | if(ring_buffer->front >= ring_buffer->size - 1) 103 | { 104 | ring_buffer->front = 0; 105 | } 106 | else { 107 | ring_buffer->front += 1; 108 | } 109 | 110 | return entry; 111 | } 112 | 113 | void* unifying_ring_buffer_pop_back(struct unifying_ring_buffer* ring_buffer) 114 | { 115 | if(unifying_ring_buffer_empty(ring_buffer)) 116 | { 117 | return NULL; 118 | } 119 | 120 | void* entry = ring_buffer->buffer[ring_buffer->back]; 121 | ring_buffer->count -= 1; 122 | 123 | if(ring_buffer->back) 124 | { 125 | ring_buffer->back -= 1; 126 | } 127 | else 128 | { 129 | ring_buffer->back = ring_buffer->size - 1; 130 | } 131 | 132 | return entry; 133 | } 134 | 135 | void* unifying_ring_buffer_peek_front(struct unifying_ring_buffer* ring_buffer) 136 | { 137 | if(unifying_ring_buffer_empty(ring_buffer)) 138 | { 139 | return NULL; 140 | } 141 | 142 | return ring_buffer->buffer[ring_buffer->front]; 143 | } 144 | 145 | void* unifying_ring_buffer_peek_back(struct unifying_ring_buffer* ring_buffer) 146 | { 147 | if(unifying_ring_buffer_empty(ring_buffer)) 148 | { 149 | return NULL; 150 | } 151 | 152 | return ring_buffer->buffer[ring_buffer->back]; 153 | } 154 | 155 | bool unifying_ring_buffer_empty(const struct unifying_ring_buffer* ring_buffer) 156 | { 157 | return !ring_buffer->count; 158 | } 159 | 160 | bool unifying_ring_buffer_full(const struct unifying_ring_buffer* ring_buffer) 161 | { 162 | return ring_buffer->count >= ring_buffer->size; 163 | } 164 | -------------------------------------------------------------------------------- /src/unifying_buffer.h: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * \file unifying_buffer.h 4 | * \brief Simple ring buffer used to store Unifying payloads 5 | */ 6 | 7 | #ifndef UNIFYING_BUFFER_H 8 | #define UNIFYING_BUFFER_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "unifying_error.h" 16 | 17 | /*! 18 | * Ring buffer structure. 19 | * 20 | * Stores arbitrary data pointers in a fixed length buffer 21 | * as well as the metadata necessary to access that data. 22 | */ 23 | struct unifying_ring_buffer 24 | { 25 | /// Pointer to a fixed size array of data pointers. 26 | void** buffer; 27 | /// Number of pointers that `buffer` can hold. 28 | uint8_t size; 29 | /// Number of items stored in `buffer`. 30 | uint8_t count; 31 | /// Index of the first item in the buffer. 32 | uint8_t front; 33 | /// Index of the last item in the buffer. 34 | uint8_t back; 35 | }; 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /*! 42 | * Initialize a \ref unifying_ring_buffer "ring buffer" instance. 43 | * 44 | * \param[out] ring_buffer Pointer to a ring buffer to initialize. 45 | * \param[in] buffer Pointer to pointer buffer. 46 | * \param[in] size Size of the pointer buffer. 47 | * 48 | * \return \ref UNIFYING_BUFFER_ERROR if \p size is `0`. 49 | * \return \ref UNIFYING_SUCCESS otherwise. 50 | */ 51 | enum unifying_error unifying_ring_buffer_init(struct unifying_ring_buffer* ring_buffer, void** buffer, uint8_t size); 52 | 53 | /*! 54 | * Allocate and initialize a \ref unifying_ring_buffer "ring buffer" instance. 55 | * 56 | * Ring buffers created with this function should be freed with 57 | * unifying_ring_buffer_destroy() when they are no longer needed. 58 | * 59 | * \param[in] size Number of pointers the allocated buffer can store. 60 | * 61 | * \return `NULL` if \p size is `0` or if allocation fails. 62 | * \return \ref unifying_ring_buffer pointer otherwise. 63 | * 64 | * \see unifying_ring_buffer_destroy() 65 | */ 66 | struct unifying_ring_buffer* unifying_ring_buffer_create(uint8_t size); 67 | 68 | /*! 69 | * Free a dynamically allocated ring buffer instance. 70 | * 71 | * \param[in,out] ring_buffer Ring buffer to free. 72 | * 73 | * \see unifying_ring_buffer_create() 74 | */ 75 | void unifying_ring_buffer_destroy(struct unifying_ring_buffer* ring_buffer); 76 | 77 | /*! 78 | * Add a pointer to the front of a ring buffer. 79 | * 80 | * \param[in,out] ring_buffer Ring buffer to add a pointer to. 81 | * \param[in] entry Pointer to add to the ring buffer. 82 | * 83 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the buffer is full. 84 | * \return \ref UNIFYING_SUCCESS otherwise. 85 | */ 86 | enum unifying_error unifying_ring_buffer_push_front(struct unifying_ring_buffer* ring_buffer, void* entry); 87 | 88 | /*! 89 | * Add a pointer to the back of a ring buffer. 90 | * 91 | * \param[in,out] ring_buffer Ring buffer to add a pointer to. 92 | * \param[in] entry Pointer to add to the ring buffer. 93 | * 94 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the buffer is full. 95 | * \return \ref UNIFYING_SUCCESS otherwise. 96 | */ 97 | enum unifying_error unifying_ring_buffer_push_back(struct unifying_ring_buffer* ring_buffer, void* entry); 98 | 99 | /*! 100 | * Remove a pointer from the front of a ring buffer and return it. 101 | * 102 | * \param[in,out] ring_buffer Ring buffer to remove a pointer from. 103 | * 104 | * \return `NULL` if the buffer is empty. 105 | * \return `void*` to some data otherwise. 106 | */ 107 | void* unifying_ring_buffer_pop_front(struct unifying_ring_buffer* ring_buffer); 108 | 109 | /*! 110 | * Remove a pointer from the back of a ring buffer and return it. 111 | * 112 | * \param[in,out] ring_buffer Ring buffer to remove a pointer from. 113 | * 114 | * \return `NULL` if the buffer is empty. 115 | * \return `void*` to some data otherwise. 116 | */ 117 | void* unifying_ring_buffer_pop_back(struct unifying_ring_buffer* ring_buffer); 118 | 119 | /*! 120 | * Return the pointer at the front of the buffer but do not remove it from the buffer. 121 | * 122 | * \param[in] ring_buffer Ring buffer to get a pointer from. 123 | * 124 | * \return `NULL` if the buffer is empty. 125 | * \return `void*` to some data otherwise. 126 | */ 127 | void* unifying_ring_buffer_peek_front(struct unifying_ring_buffer* ring_buffer); 128 | 129 | /*! 130 | * Return the pointer at the back of the buffer but do not remove it from the buffer. 131 | * 132 | * \param[in] ring_buffer Ring buffer to get a pointer from. 133 | * 134 | * \return `NULL` if the buffer is empty. 135 | * \return `void*` to some data otherwise. 136 | */ 137 | void* unifying_ring_buffer_peek_back(struct unifying_ring_buffer* ring_buffer); 138 | 139 | /*! 140 | * Test if a ring buffer is empty. 141 | * 142 | * \param[in] ring_buffer Ring buffer to check. 143 | * 144 | * \return `true` if the ring buffer is empty. 145 | * \return `false` if the ring buffer is not empty. 146 | */ 147 | bool unifying_ring_buffer_empty(const struct unifying_ring_buffer* ring_buffer); 148 | 149 | /*! 150 | * Test if a ring buffer is full. 151 | * 152 | * \param[in] ring_buffer Ring buffer to check. 153 | * 154 | * \return `true` if the ring buffer is full. 155 | * \return `false` if the ring buffer is not full. 156 | */ 157 | bool unifying_ring_buffer_full(const struct unifying_ring_buffer* ring_buffer); 158 | 159 | #ifdef __cplusplus 160 | } 161 | #endif 162 | 163 | #endif 164 | -------------------------------------------------------------------------------- /src/unifying_utils.c: -------------------------------------------------------------------------------- 1 | 2 | #include "unifying_utils.h" 3 | 4 | void unifying_uint16_pack(uint8_t packed[2], uint16_t number) 5 | { 6 | packed[0] = (number >> 8) & 0xFF; 7 | packed[1] = (number >> 0) & 0xFF; 8 | } 9 | 10 | void unifying_uint16_unpack(uint16_t* number, const uint8_t packed[2]) 11 | { 12 | for(size_t i = 0; i < sizeof(*number); ++i) 13 | { 14 | *number <<= 8; 15 | *number |= packed[i]; 16 | } 17 | } 18 | 19 | void unifying_uint32_pack(uint8_t packed[4], uint32_t number) 20 | { 21 | packed[0] = (number >> 24) & 0xFF; 22 | packed[1] = (number >> 16) & 0xFF; 23 | packed[2] = (number >> 8) & 0xFF; 24 | packed[3] = (number >> 0) & 0xFF; 25 | } 26 | 27 | void unifying_uint32_unpack(uint32_t* number, const uint8_t packed[4]) 28 | { 29 | for(size_t i = 0; i < sizeof(*number); ++i) 30 | { 31 | *number <<= 8; 32 | *number |= packed[i]; 33 | } 34 | } 35 | 36 | int16_t unifying_int12_clamp(int16_t number) 37 | { 38 | if(number > 2047) 39 | { 40 | return 2047; 41 | } 42 | 43 | if(number < -2048) 44 | { 45 | return -2048; 46 | } 47 | 48 | return number; 49 | } 50 | 51 | uint8_t unifying_checksum(const uint8_t* buffer, uint8_t length) 52 | { 53 | uint8_t checksum = 0; 54 | for (uint8_t i = 0; i < length; i++) 55 | { 56 | checksum -= buffer[i]; 57 | } 58 | return checksum; 59 | } 60 | 61 | uint8_t unifying_checksum_verify(const uint8_t* buffer, uint8_t length) 62 | { 63 | return unifying_checksum(buffer, length - 1) != buffer[length - 1]; 64 | } 65 | 66 | uint8_t unifying_xnor(uint8_t first, uint8_t second) 67 | { 68 | return ~(first ^ second); 69 | } 70 | 71 | void unifying_encrypted_keystroke_plaintext_init(struct unifying_encrypted_keystroke_plaintext* unpacked, 72 | uint8_t modifiers, 73 | const uint8_t keys[UNIFYING_KEYS_LEN]) 74 | { 75 | memset(unpacked, 0, sizeof(struct unifying_encrypted_keystroke_plaintext)); 76 | unpacked->modifiers = modifiers; 77 | memcpy(unpacked->keys, keys, sizeof(unpacked->keys)); 78 | unpacked->flag = 0xC9; 79 | } 80 | 81 | void unifying_encrypted_keystroke_plaintext_pack(uint8_t packed[UNIFYING_AES_DATA_LEN], 82 | const struct unifying_encrypted_keystroke_plaintext* unpacked) 83 | { 84 | packed[0] = unpacked->modifiers; 85 | memcpy(&packed[1], unpacked->keys, sizeof(unpacked->keys)); 86 | packed[7] = unpacked->flag; 87 | } 88 | 89 | void unifying_encrypted_keystroke_iv_init(struct unifying_encrypted_keystroke_iv* unpacked, 90 | uint32_t counter) 91 | { 92 | memset(unpacked, 0, sizeof(struct unifying_encrypted_keystroke_iv)); 93 | memcpy(unpacked->prefix, unifying_aes_nonce_prefix, sizeof(unpacked->prefix)); 94 | unpacked->counter = counter; 95 | memcpy(unpacked->suffix, unifying_aes_nonce_suffix, sizeof(unpacked->suffix)); 96 | } 97 | 98 | void unifying_encrypted_keystroke_iv_pack(uint8_t packed[UNIFYING_AES_BLOCK_LEN], 99 | const struct unifying_encrypted_keystroke_iv* unpacked) 100 | { 101 | memcpy(&packed[0], unpacked->prefix, sizeof(unpacked->prefix)); 102 | unifying_uint32_pack(&packed[7], unpacked->counter); 103 | memcpy(&packed[11], unpacked->suffix, sizeof(unpacked->suffix)); 104 | } 105 | 106 | void unifying_proto_aes_key_init(struct unifying_proto_aes_key* unpacked, 107 | uint8_t base_address[UNIFYING_ADDRESS_LEN - 1], 108 | uint16_t device_product_id, 109 | uint16_t receiver_product_id, 110 | uint32_t device_crypto, 111 | uint32_t receiver_crypto) 112 | { 113 | memcpy(unpacked->base_address, base_address, sizeof(unpacked->base_address)); 114 | unpacked->device_product_id = device_product_id; 115 | unpacked->receiver_product_id = receiver_product_id; 116 | unpacked->device_crypto = device_crypto; 117 | unpacked->receiver_crypto = receiver_crypto; 118 | } 119 | 120 | void unifying_proto_aes_key_pack(uint8_t packed[UNIFYING_AES_BLOCK_LEN], 121 | const struct unifying_proto_aes_key* unpacked) 122 | { 123 | memcpy(&packed[0], unpacked->base_address, sizeof(unpacked->base_address)); 124 | unifying_uint16_pack(&packed[4], unpacked->device_product_id); 125 | unifying_uint16_pack(&packed[6], unpacked->receiver_product_id); 126 | unifying_uint32_pack(&packed[8], unpacked->device_crypto); 127 | unifying_uint32_pack(&packed[12], unpacked->receiver_crypto); 128 | } 129 | 130 | void unifying_deobfuscate_aes_key(uint8_t aes_key[UNIFYING_AES_BLOCK_LEN], 131 | const uint8_t proto_aes_key[UNIFYING_AES_BLOCK_LEN]) 132 | { 133 | for (uint8_t i = 0; i < UNIFYING_AES_BLOCK_LEN; i++) { 134 | aes_key[i] = unifying_xnor(proto_aes_key[unifying_aes_key_index[i]], unifying_aes_key_bitmask[i]); 135 | } 136 | } 137 | 138 | uint8_t unifying_next_channel(uint8_t channel) 139 | { 140 | // Convert the supplied channel into an index into an array of valid channels. 141 | // This is excessive but it should ensure that we always return a valid channel. 142 | uint8_t index = (channel - 2) / 3; 143 | 144 | if(index >= UNIFYING_CHANNELS_LEN) 145 | { 146 | index = 0; 147 | } 148 | 149 | return unifying_channels[index]; 150 | } 151 | 152 | void unifying_copy_reverse(uint8_t *reverse, const uint8_t *forward, uint8_t length) 153 | { 154 | for(uint8_t i = 0; i < length; i++) 155 | { 156 | reverse[i] = forward[length - i - 1]; 157 | } 158 | } 159 | 160 | void unifying_print_buffer(const uint8_t *buffer, uint8_t length) 161 | { 162 | printf("["); 163 | for(uint8_t i = 0; i < length; i++) 164 | { 165 | if(i) 166 | { 167 | printf(", "); 168 | } 169 | printf("0x%02X", buffer[i]); 170 | } 171 | printf("]\n"); 172 | } 173 | 174 | -------------------------------------------------------------------------------- /src/unifying_state.c: -------------------------------------------------------------------------------- 1 | 2 | #include "unifying_state.h" 3 | 4 | #if defined(UNIFYING_HARDWARE_AES) && (UNIFYING_HARDWARE_AES == 0) 5 | static uint8_t unifying_encrypt(uint8_t data[UNIFYING_AES_DATA_LEN], 6 | const uint8_t key[UNIFYING_AES_BLOCK_LEN], 7 | const uint8_t iv[UNIFYING_AES_BLOCK_LEN]) { 8 | struct AES_ctx ctx; 9 | AES_init_ctx_iv(&ctx, key, iv); 10 | AES_CTR_xcrypt_buffer(&ctx, data, UNIFYING_AES_DATA_LEN); 11 | return 0; 12 | } 13 | #endif 14 | 15 | enum unifying_error unifying_interface_init(struct unifying_interface* interface, 16 | uint8_t (*transmit_payload)(const uint8_t* payload, uint8_t length), 17 | uint8_t (*receive_payload)(uint8_t* payload, uint8_t length), 18 | bool (*payload_available)(), 19 | uint8_t (*payload_size)(), 20 | uint8_t (*set_address)(const uint8_t address[UNIFYING_ADDRESS_LEN]), 21 | uint8_t (*set_channel)(uint8_t channel), 22 | uint32_t (*time)(), 23 | uint8_t (*encrypt)(uint8_t data[UNIFYING_AES_DATA_LEN], 24 | const uint8_t key[UNIFYING_AES_BLOCK_LEN], 25 | const uint8_t iv[UNIFYING_AES_BLOCK_LEN])) 26 | { 27 | if(!encrypt) 28 | { 29 | #if defined(UNIFYING_HARDWARE_AES) && (UNIFYING_HARDWARE_AES == 0) 30 | interface->encrypt = unifying_encrypt; 31 | #else 32 | return UNIFYING_ERROR; 33 | #endif 34 | } 35 | else 36 | { 37 | interface->encrypt = encrypt; 38 | } 39 | 40 | interface->transmit_payload = transmit_payload; 41 | interface->receive_payload = receive_payload; 42 | interface->payload_available = payload_available; 43 | interface->payload_size = payload_size; 44 | interface->set_address = set_address; 45 | interface->set_channel = set_channel; 46 | interface->time = time; 47 | 48 | return UNIFYING_SUCCESS; 49 | } 50 | 51 | 52 | 53 | void unifying_state_init(struct unifying_state* state, 54 | const struct unifying_interface* interface, 55 | struct unifying_ring_buffer* transmit_buffer, 56 | struct unifying_ring_buffer* receive_buffer, 57 | uint8_t address[UNIFYING_ADDRESS_LEN], 58 | uint8_t aes_key[UNIFYING_AES_BLOCK_LEN], 59 | uint32_t aes_counter, 60 | uint16_t default_timeout, 61 | uint8_t channel) 62 | { 63 | state->transmit_buffer = transmit_buffer; 64 | state->receive_buffer = receive_buffer; 65 | state->interface = interface; 66 | state->address = address; 67 | state->aes_key = aes_key; 68 | state->aes_counter = aes_counter; 69 | state->default_timeout = default_timeout; 70 | state->timeout = default_timeout; 71 | state->previous_transmit = 0; 72 | state->next_transmit = 0; 73 | state->channel = channel; 74 | } 75 | 76 | void unifying_state_transmit_buffer_clear(struct unifying_state* state) 77 | { 78 | while(!unifying_ring_buffer_empty(state->transmit_buffer)) 79 | { 80 | unifying_transmit_entry_destroy(unifying_ring_buffer_pop_front(state->transmit_buffer)); 81 | } 82 | } 83 | 84 | void unifying_state_receive_buffer_clear(struct unifying_state* state) 85 | { 86 | while(!unifying_ring_buffer_empty(state->receive_buffer)) 87 | { 88 | unifying_receive_entry_destroy(unifying_ring_buffer_pop_front(state->receive_buffer)); 89 | } 90 | } 91 | 92 | void unifying_state_buffers_clear(struct unifying_state* state) 93 | { 94 | unifying_state_transmit_buffer_clear(state); 95 | unifying_state_receive_buffer_clear(state); 96 | } 97 | 98 | uint8_t unifying_state_channel_set(struct unifying_state* state, uint8_t channel) 99 | { 100 | uint8_t status = state->interface->set_channel(channel); 101 | 102 | if(!status) 103 | { 104 | // Success 105 | state->channel = channel; 106 | } 107 | 108 | return status; 109 | } 110 | 111 | uint8_t unifying_state_address_set(struct unifying_state* state, const uint8_t address[UNIFYING_ADDRESS_LEN]) 112 | { 113 | uint8_t status = state->interface->set_address(address); 114 | 115 | if(!status) 116 | { 117 | // Success 118 | memcpy(state->address, address, UNIFYING_ADDRESS_LEN); 119 | } 120 | 121 | return status; 122 | } 123 | 124 | void unifying_transmit_entry_init(struct unifying_transmit_entry* entry, 125 | uint8_t* payload, 126 | uint8_t length, 127 | uint8_t timeout) 128 | { 129 | entry->payload = payload; 130 | entry->length = length; 131 | entry->timeout = timeout; 132 | } 133 | 134 | struct unifying_transmit_entry* unifying_transmit_entry_create(uint8_t length, uint8_t timeout) 135 | { 136 | struct unifying_transmit_entry* entry = malloc(sizeof(struct unifying_transmit_entry)); 137 | 138 | if(!entry) 139 | { 140 | return NULL; 141 | } 142 | 143 | uint8_t* payload = malloc(length); 144 | 145 | if(!payload) 146 | { 147 | free(entry); 148 | return NULL; 149 | } 150 | 151 | unifying_transmit_entry_init(entry, payload, length, timeout); 152 | return entry; 153 | } 154 | 155 | void unifying_transmit_entry_destroy(struct unifying_transmit_entry* entry) 156 | { 157 | free(entry->payload); 158 | free(entry); 159 | } 160 | 161 | 162 | 163 | void unifying_receive_entry_init(struct unifying_receive_entry* entry, 164 | uint8_t* payload, 165 | uint8_t length) 166 | { 167 | entry->payload = payload; 168 | entry->length = length; 169 | } 170 | 171 | struct unifying_receive_entry* unifying_receive_entry_create(uint8_t length) 172 | { 173 | struct unifying_receive_entry* entry = malloc(sizeof(struct unifying_receive_entry)); 174 | 175 | if(!entry) 176 | { 177 | return NULL; 178 | } 179 | 180 | uint8_t* payload = malloc(length); 181 | 182 | if(!payload) 183 | { 184 | free(entry); 185 | return NULL; 186 | } 187 | 188 | unifying_receive_entry_init(entry, payload, length); 189 | return entry; 190 | } 191 | 192 | void unifying_receive_entry_destroy(struct unifying_receive_entry* entry) 193 | { 194 | free(entry->payload); 195 | free(entry); 196 | } 197 | -------------------------------------------------------------------------------- /examples/UnifyingKeyboard/UnifyingKeyboard.ino: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | #include "aes.hpp" 7 | 8 | #include "unifying.h" 9 | 10 | #define CE_PIN 7 11 | #define CSN_PIN 8 12 | #define INPUT_PIN 2 13 | #define HALT_PIN 3 14 | 15 | #define TRANSMIT_BUFFER_SIZE 8 16 | #define RECEIVE_BUFFER_SIZE 8 17 | #define SLOW_TIMEOUT 110 18 | 19 | int eeprom_address; 20 | uint8_t input_state; 21 | uint8_t address[UNIFYING_ADDRESS_LEN]; 22 | uint8_t aes_key[UNIFYING_AES_BLOCK_LEN]; 23 | 24 | struct unifying_interface interface; 25 | struct unifying_ring_buffer* transmit_buffer; 26 | struct unifying_ring_buffer* receive_buffer; 27 | struct unifying_state state; 28 | 29 | RF24 radio(CE_PIN, CSN_PIN); 30 | 31 | // The following functions are used by the Unifying library to interface with radio hardware. 32 | uint8_t transmit_payload(const uint8_t* payload, uint8_t length) { 33 | Serial.print("Transmit: "); 34 | unifying_print_buffer(payload, length); 35 | bool status = radio.write(payload, length); 36 | return (uint8_t) !status; 37 | } 38 | 39 | uint8_t receive_payload(uint8_t* payload, uint8_t length) { 40 | if(!radio.available()) { 41 | return 0; 42 | } 43 | 44 | uint8_t payload_size = radio.getDynamicPayloadSize(); 45 | radio.read(payload, length); 46 | 47 | Serial.print("Receive: "); 48 | unifying_print_buffer(payload, payload_size > length ? length : payload_size); 49 | 50 | return payload_size; 51 | } 52 | 53 | bool payload_available() { 54 | return radio.available(); 55 | } 56 | 57 | uint8_t payload_size() { 58 | return radio.getDynamicPayloadSize(); 59 | } 60 | 61 | uint8_t set_address(const uint8_t address[UNIFYING_ADDRESS_LEN]) { 62 | uint8_t temp[UNIFYING_ADDRESS_LEN]; 63 | unifying_copy_reverse(temp, address, UNIFYING_ADDRESS_LEN); 64 | Serial.print("Address: "); 65 | unifying_print_buffer(address, UNIFYING_ADDRESS_LEN); 66 | radio.openWritingPipe(temp); 67 | return 0; 68 | } 69 | 70 | uint8_t set_channel(uint8_t channel) { 71 | Serial.print("Channel: "); 72 | Serial.println(channel); 73 | radio.setChannel(channel); 74 | return 0; 75 | } 76 | 77 | // Check for key presses and send scancodes. 78 | void scan_keyboard_matrix() { 79 | uint8_t keys[UNIFYING_KEYS_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 80 | uint8_t modifiers = 0x00; 81 | 82 | // Check for key presses. 83 | if(input_state == HIGH && digitalRead(INPUT_PIN) == LOW) { 84 | Serial.println("Button pressed"); 85 | input_state = LOW; 86 | 87 | // Press keys. 88 | keys[5] = 0x04; // Scancode for 'a'. 89 | modifiers = 0x02; // Modifier flag for left shift. 90 | 91 | // This should type "A" for as long as the input button is held down. 92 | unifying_encrypted_keystroke(&state, keys, modifiers); 93 | } 94 | else if(input_state == LOW && digitalRead(INPUT_PIN) == HIGH) { 95 | Serial.println("Button released"); 96 | input_state = HIGH; 97 | 98 | // Release keys. 99 | keys[5] = 0x00; 100 | modifiers = 0x00; 101 | unifying_encrypted_keystroke(&state, keys, modifiers); 102 | unifying_set_timeout(&state, SLOW_TIMEOUT); 103 | } 104 | } 105 | 106 | void setup() { 107 | Serial.begin(115200); 108 | while(!Serial) {delay(100);} 109 | printf_begin(); 110 | 111 | // Configure a pin as an input. 112 | // This serves as our mock keyboard matrix. 113 | pinMode(INPUT_PIN, INPUT_PULLUP); 114 | input_state = HIGH; 115 | 116 | // Configure a pin as an interrupt. 117 | // This allows us to halt the microcontroller and analyze the output. 118 | pinMode(HALT_PIN, INPUT_PULLUP); 119 | attachInterrupt(digitalPinToInterrupt(HALT_PIN), halt, FALLING); 120 | 121 | // Initialize random() 122 | randomSeed(analogRead(0)); 123 | 124 | if(!radio.begin()) { 125 | Serial.println(F("Radio not responding")); 126 | while(true) {delay(100);} 127 | } 128 | 129 | // Assert defaults to document the correct settings. 130 | radio.powerUp(); 131 | radio.stopListening(); 132 | radio.setPALevel(RF24_PA_MAX, true); 133 | radio.setCRCLength(RF24_CRC_16); 134 | radio.setAddressWidth(sizeof(unifying_pairing_address)); 135 | radio.setAutoAck(true); 136 | 137 | // Assert non-default settings. 138 | radio.setDataRate(RF24_2MBPS); 139 | radio.enableDynamicPayloads(); 140 | radio.enableAckPayload(); 141 | radio.setRetries(15, 10); 142 | 143 | // Make sure we don't have any extraneous data in the RX FIFO. 144 | // The TX FIFO should have already been flushed by calling stopListening() 145 | radio.flush_rx(); 146 | 147 | unifying_interface_init(&interface, 148 | transmit_payload, 149 | receive_payload, 150 | payload_available, 151 | payload_size, 152 | set_address, 153 | set_channel, 154 | millis, 155 | NULL); 156 | 157 | transmit_buffer = unifying_ring_buffer_create(TRANSMIT_BUFFER_SIZE); 158 | receive_buffer = unifying_ring_buffer_create(RECEIVE_BUFFER_SIZE); 159 | 160 | // Load connection details to non-volatile memory. 161 | eeprom_address = 0; 162 | EEPROM.get(eeprom_address, address); 163 | eeprom_address += sizeof(address); 164 | EEPROM.get(eeprom_address, aes_key); 165 | 166 | uint32_t aes_counter = random(); 167 | 168 | unifying_state_init(&state, 169 | &interface, 170 | transmit_buffer, 171 | receive_buffer, 172 | address, 173 | aes_key, 174 | aes_counter, 175 | UNIFYING_DEFAULT_TIMEOUT_KEYBOARD, 176 | unifying_channels[0]); 177 | 178 | 179 | enum unifying_error err; 180 | 181 | // Try to connect to our receiver. 182 | err = unifying_connect(&state); 183 | 184 | if(err) { 185 | // If we can't connect to our receiver then try pairing to a new one. 186 | uint8_t id = random(); 187 | uint16_t product_id = 0x1025; 188 | uint16_t device_type = 0x0147; 189 | uint32_t crypto = random(); 190 | uint32_t serial = 0xA58094B6; 191 | uint16_t capabilities = 0x1E40; 192 | char name[] = "Hacked"; 193 | uint8_t name_length = strlen(name); 194 | 195 | err = unifying_pair(&state, 196 | id, 197 | product_id, 198 | device_type, 199 | crypto, 200 | serial, 201 | capabilities, 202 | name, 203 | name_length); 204 | 205 | if(!err) { 206 | // Pairing successful. 207 | // Save connection details to non-volatile memory. 208 | eeprom_address = 0; 209 | EEPROM.put(eeprom_address, address); 210 | eeprom_address += sizeof(address); 211 | EEPROM.put(eeprom_address, aes_key); 212 | } 213 | } 214 | 215 | Serial.println(unifying_get_error_name(err)); 216 | 217 | while(err) { 218 | // If pairing failed then resume trying to connect to our receiver. 219 | err = unifying_connect(&state); 220 | delay(100); 221 | } 222 | } 223 | 224 | void loop() { 225 | // Check if the user has pressed any keys. 226 | scan_keyboard_matrix(); 227 | 228 | // Transmit buffered data. 229 | unifying_tick(&state); 230 | } 231 | 232 | void halt() { 233 | while(true) {} 234 | } 235 | -------------------------------------------------------------------------------- /src/unifying.h: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * \file unifying.h 4 | * \brief High level functions for transmitting and receiving Unifying payloads. 5 | * 6 | * \todo create an initialization function for this library to set the RF channel and address. 7 | */ 8 | 9 | #ifndef UNIFYING_H 10 | #define UNIFYING_H 11 | 12 | #include 13 | #include 14 | 15 | #include "unifying_error.h" 16 | #include "unifying_data.h" 17 | #include "unifying_utils.h" 18 | #include "unifying_state.h" 19 | #include "unifying_buffer.h" 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | /*! 26 | * Transmit and receive Unifying payloads at regular intervals. 27 | * 28 | * \todo Define possible return values. 29 | * 30 | * Transmit a queued payload shortly before the current timeout has elapsed. 31 | * If an unhandled response payload is bufferd then a 32 | * \ref unifying_hidpp_1_0_short "HID++" payload will be queued for transmission. 33 | * If no payload is queued for transmission then a 34 | * \ref unifying_keep_alive_request "keep-alive" payload will be queued for transmission.. 35 | * 36 | * If a payload was received in response to the transmission then it will be queued for later handling. 37 | * 38 | * \note This function is expected to be called regularly by the user of this library. 39 | * 40 | * \param[in,out] state Unifying state information. 41 | * 42 | */ 43 | enum unifying_error unifying_tick(struct unifying_state* state); 44 | 45 | /*! 46 | * Repeatedly call unifying_tick() until a condition is met. 47 | * 48 | * \note If all \p exit_on_* parameters are `false` then this function will never return. 49 | * 50 | * \param[in,out] state Unifying state information. 51 | * \param[in] exit_on_error Return if unifying_tick() returns an error. 52 | * \param[in] exit_on_transmit Return if \ref unifying_state.transmit_buffer "state.transmit_buffer" 53 | * is empty, implying that all payloads have been transmitted. 54 | * \param[in] exit_on_receive Return if \ref unifying_state.receive_buffer "state.receive_buffer" 55 | * is not empty, implying that a payload has been received. 56 | * 57 | * \return Any error returned by unifying_tick() if \p exit_on_error is `true`. 58 | * \return \ref UNIFYING_SUCCESS otherwise. 59 | */ 60 | enum unifying_error unifying_loop(struct unifying_state* state, 61 | bool exit_on_error, 62 | bool exit_on_transmit, 63 | bool exit_on_receive); 64 | 65 | /*! 66 | * Pair with a Unifying receiver. 67 | * 68 | * \todo Define possible return values. 69 | * 70 | * \note Upon successfully pairing, this function will populate 71 | * \ref unifying_state.address "state.address" and 72 | * \ref unifying_state.aes_key "state.aes_key". 73 | * Those values are expected to be saved to non-volatile storage by the caller. 74 | * 75 | * \todo Document valid values for \p device_type 76 | * 77 | * \todo Document valid values for \p capabilities 78 | * 79 | * \param[in,out] state Unifying state information. 80 | * \param[in] id Random value used for verifying the early stage of the pairing process. 81 | * \param[in] product_id Product ID of your device. 82 | * This value becomes part of the device's AES encryption key. 83 | * For added security, this should be a cryptographically secure random number. 84 | * \param[in] device_type Values indicating the device type. 85 | * \param[in] crypto A cryptographically secure random number, used for AES encryption key generation. 86 | * \param[in] serial Serial number of your device. The exact value does not matter. 87 | * \param[in] capabilities HID++ capabilities. 88 | * \param[in] name Name of your device. 89 | * This name will appear in the Logitech Unifying desktop software. 90 | * This value does not need to be NULL terminated. 91 | * The name cannot be longer than \ref UNIFYING_MAX_NAME_LEN. 92 | * \param[in] name_length Length of the supplied name. 93 | * The name length does not include a NULL terminator. 94 | * The name cannot be longer than \ref UNIFYING_MAX_NAME_LEN. 95 | * 96 | */ 97 | enum unifying_error unifying_pair(struct unifying_state* state, 98 | uint8_t id, 99 | uint16_t product_id, 100 | uint16_t device_type, 101 | uint32_t crypto, 102 | uint32_t serial, 103 | uint16_t capabilities, 104 | const char* name, 105 | uint8_t name_length); 106 | 107 | /*! 108 | * Connect to a paired Unifying receiver. 109 | * 110 | * \todo Define possible return values. 111 | * 112 | * \param[in,out] state Unifying state information. 113 | * 114 | */ 115 | enum unifying_error unifying_connect(struct unifying_state* state); 116 | 117 | /*! 118 | * Queue a payload that sets the timeout for keep-alive packets. 119 | * 120 | * This can be useful for conserving power when the user isn't actively using the device. 121 | * 122 | * \param[in,out] state Unifying state information. 123 | * \param[in] timeout New packet timeout. 124 | * 125 | * \return \ref UNIFYING_CREATE_ERROR if dynamic memory allocation fails. 126 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the transmit buffer is full. 127 | * \return \ref UNIFYING_SUCCESS otherwise. 128 | */ 129 | enum unifying_error unifying_set_timeout(struct unifying_state* state, uint16_t timeout); 130 | 131 | /*! 132 | * Immediately transmit an encrypted keystroke payload. 133 | * 134 | * \note Sending 2 or more keyboard scancodes at once requires sending an intermediate payload 135 | * for each additional keyboard scancode. 136 | * Otherwise the Unifying receiver will reject the payload. 137 | * \code{.c} 138 | * // e.g. Pressing 'a', 'b', and 'c' keys at the same time. 139 | * uint8_t keys[UNIFYING_KEYS_LEN] = {0, 0, 0, 0, 0, 0}; 140 | * keys[5] = 0x04; // scancode for 'a' 141 | * // Press 'a'. 142 | * unifying_encrypted_keystroke(&state, &keys, 0); 143 | * keys[4] = 0x05; // scancode for 'b' 144 | * // Press 'a' and 'b'. 145 | * unifying_encrypted_keystroke(&state, &keys, 0); 146 | * keys[3] = 0x06; // scancode for 'c' 147 | * // Press 'a', 'b', and 'c'. 148 | * unifying_encrypted_keystroke(&state, &keys, 0); 149 | * \endcode 150 | * 151 | * \param[in,out] state Unifying state information. 152 | * \param[in] keys Pointer to a buffer of \ref UNIFYING_KEYS_LEN keyboard scancodes. 153 | * \param[in] modifiers Bitfield where each bit corresponds to a specific modifier key. 154 | * 155 | * \todo Define modifiers key bits. 156 | * 157 | * \return \ref UNIFYING_ENCRYPTION_ERROR if payload encryption fails. 158 | * \return \ref UNIFYING_CREATE_ERROR if dynamic memory allocation fails. 159 | * \return \ref UNIFYING_TRANSMIT_ERROR if payload transmission fails. 160 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the receive buffer is full and a response payload is available. 161 | * \return \ref UNIFYING_CREATE_ERROR if dynamic memory allocation fails. 162 | * \return \ref UNIFYING_PAYLOAD_LENGTH_ERROR if the response payload's length differs from its expected length. 163 | * This should never happen. 164 | * \return \ref UNIFYING_SUCCESS otherwise. 165 | */ 166 | enum unifying_error unifying_encrypted_keystroke(struct unifying_state* state, 167 | const uint8_t keys[UNIFYING_KEYS_LEN], 168 | uint8_t modifiers); 169 | 170 | /*! 171 | * Immediately transmit a multimedia keystroke payload. 172 | * 173 | * \param[in,out] state Unifying state information. 174 | * \param[in] keys Pointer to a buffer of \ref UNIFYING_KEYS_LEN keyboard scancodes. 175 | * 176 | */ 177 | enum unifying_error unifying_multimeia_keystroke(struct unifying_state* state, 178 | uint8_t keys[UNIFYING_MULTIMEDIA_KEYS_LEN]); 179 | 180 | /*! 181 | * Queue a mouse payload for transmission. 182 | * 183 | * \param[in,out] state Unifying state information. 184 | * \param[in] buttons Bitfield where each bit corresponds to a mouse button. 185 | * \param[in] move_y Y axis mouse movement. 186 | * \param[in] move_x X axis mouse movement. 187 | * \param[in] wheel_y Y axis scroll wheel movement. 188 | * \param[in] wheel_x X axis scroll wheel movement. 189 | * 190 | */ 191 | enum unifying_error unifying_mouse(struct unifying_state* state, 192 | uint8_t buttons, 193 | int16_t move_y, 194 | int16_t move_x, 195 | int8_t wheel_y, 196 | int8_t wheel_x); 197 | 198 | 199 | #ifdef __cplusplus 200 | } 201 | #endif 202 | 203 | #endif 204 | -------------------------------------------------------------------------------- /src/unifying_const.h: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * \file unifying_const.h 4 | * \brief Constants used by the Unifying protocol 5 | * 6 | * \todo Define HID++ 1.0 errors as an enum 7 | * 8 | * \todo Define mouse buttons as an enum 9 | * 10 | * \todo Add definitions for constants used in \ref unifying_data.c 11 | */ 12 | 13 | #ifndef UNIFYING_CONST_H 14 | #define UNIFYING_CONST_H 15 | 16 | #include 17 | 18 | #ifndef UNIFYING_TIMEOUT_COEFFICIENT 19 | /*! 20 | * Determines what percent of the timeout should elapse before transmitting a keep-alive packet. 21 | * 22 | * This can be re-defined by this library's user. 23 | * Defining this as a number greater than `1` will most like cause packets to timeout. 24 | */ 25 | #define UNIFYING_TIMEOUT_COEFFICIENT 0.875 26 | #endif 27 | 28 | /*! 29 | * Used to indicate that \ref unifying_transmit should not change the current timeout. 30 | */ 31 | #define UNIFYING_TIMEOUT_UNCHANGED 0 32 | 33 | /*! 34 | * Size of an AES-128 block in bytes. 35 | */ 36 | #define UNIFYING_AES_BLOCK_LEN 16 37 | 38 | /*! 39 | * Size of the AES nonce prefix in bytes. 40 | * \see unifying_aes_nonce_prefix 41 | */ 42 | #define UNIFYING_AES_NONCE_PREFIX_LEN 7 43 | 44 | /*! 45 | * Size of the AES nonce suffix in bytes. 46 | * \see unifying_aes_nonce_suffix 47 | */ 48 | #define UNIFYING_AES_NONCE_SUFFIX_LEN 5 49 | 50 | /*! 51 | * Size of encrypted data in bytes. 52 | */ 53 | #define UNIFYING_AES_DATA_LEN 8 54 | 55 | /*! 56 | * Number of RF channels that the Unifying protocol uses in normal operation. 57 | * /see unifying_channels 58 | */ 59 | 60 | #define UNIFYING_CHANNELS_LEN 25 61 | /*! 62 | * Number of RF channels that the Unifying protocol uses for pairing. 63 | * /see unifying_pairing_channels 64 | */ 65 | 66 | #define UNIFYING_PAIRING_CHANNELS_LEN 11 67 | 68 | /*! 69 | * Size of the RF address in bytes. 70 | * /see unifying_pairing_address 71 | */ 72 | #define UNIFYING_ADDRESS_LEN 5 73 | 74 | /*! 75 | * Maximum size of a Unifying payload in bytes. 76 | */ 77 | #define UNIFYING_MAX_PAYLOAD_LEN 22 78 | 79 | /*! 80 | * Maximum name length for a Unifying device. 81 | * 82 | * This length does not include a NULL terminator. 83 | */ 84 | #define UNIFYING_MAX_NAME_LEN 16 85 | 86 | /*! 87 | * Number keyboard scancodes that can be transmitted in a single encrypted payload. 88 | */ 89 | #define UNIFYING_KEYS_LEN 6 90 | 91 | /*! 92 | * Number keyboard scancodes that can be transmitted in a single encrypted payload. 93 | */ 94 | #define UNIFYING_MULTIMEDIA_KEYS_LEN 4 95 | 96 | /*! 97 | * Size of the first pairing request in bytes. 98 | */ 99 | #define UNIFYING_PAIR_REQUEST_1_LEN 22 100 | 101 | /*! 102 | * Size of the first pairing response in bytes. 103 | */ 104 | 105 | #define UNIFYING_PAIR_RESPONSE_1_LEN 22 106 | 107 | /*! 108 | * Size of the second pairing request in bytes. 109 | */ 110 | #define UNIFYING_PAIR_REQUEST_2_LEN 22 111 | 112 | /*! 113 | * Size of the second pairing response in bytes. 114 | */ 115 | #define UNIFYING_PAIR_RESPONSE_2_LEN 22 116 | 117 | /*! 118 | * Size of the third pairing request in bytes. 119 | */ 120 | #define UNIFYING_PAIR_REQUEST_3_LEN 22 121 | 122 | /*! 123 | * Size of the third pairing response in bytes. 124 | */ 125 | #define UNIFYING_PAIR_RESPONSE_3_LEN 10 126 | 127 | /*! 128 | * Size of the pairing complete request in bytes. 129 | * No pairing response is expected. 130 | */ 131 | #define UNIFYING_PAIR_COMPLETE_REQUEST_LEN 10 132 | 133 | /*! 134 | * Size of a large wake-up request in bytes. 135 | */ 136 | #define UNIFYING_LONG_WAKE_UP_REQUEST_LEN 22 137 | 138 | /*! 139 | * Size of a small wake-up request in bytes. 140 | */ 141 | #define UNIFYING_SHORT_WAKE_UP_REQUEST_LEN 10 142 | 143 | /*! 144 | * Size of a set timeout request in bytes. 145 | */ 146 | #define UNIFYING_SET_TIMEOUT_REQUEST_LEN 10 147 | 148 | /*! 149 | * Size of a keep-alive request in bytes. 150 | */ 151 | #define UNIFYING_KEEP_ALIVE_REQUEST_LEN 5 152 | 153 | /*! 154 | * Size of a small HID++ 1.0 payload in bytes. 155 | */ 156 | #define UNIFYING_HIDPP_1_0_SHORT_LEN 10 157 | 158 | /*! 159 | * Number of parameters that a small HID++ 1.0 payload can hold. 160 | */ 161 | #define UNIFYING_HIDPP_1_0_SHORT_PARAMS_LEN 4 162 | 163 | /*! 164 | * Size of a large HID++ 1.0 payload in bytes. 165 | */ 166 | #define UNIFYING_HIDPP_1_0_LONG_LEN 22 167 | 168 | /*! 169 | * Number of parameters that a small HID++ 1.0 payload can hold. 170 | */ 171 | #define UNIFYING_HIDPP_1_0_LONG_PARAMS_LEN 17 172 | 173 | /*! 174 | * HID++ 1.0 `SET_REGISTER` SubID. 175 | */ 176 | #define UNIFYING_HIDPP_1_0_SUB_ID_SET_REGISTER 0x80 177 | 178 | /*! 179 | * HID++ 1.0 `GET_REGISTER` SubID. 180 | */ 181 | #define UNIFYING_HIDPP_1_0_SUB_ID_GET_REGISTER 0x81 182 | 183 | /*! 184 | * HID++ 1.0 `SET_LONG_REGISTER` SubID. 185 | */ 186 | #define UNIFYING_HIDPP_1_0_SUB_ID_SET_LONG_REGISTER 0x82 187 | 188 | /*! 189 | * HID++ 1.0 `GET_LONG_REGISTER` SubID. 190 | */ 191 | #define UNIFYING_HIDPP_1_0_SUB_ID_GET_LONG_REGISTER 0x83 192 | 193 | /*! 194 | * HID++ 1.0 `ERROR_MSG` SubID. 195 | */ 196 | #define UNIFYING_HIDPP_1_0_SUB_ID_ERROR_MSG 0x8F 197 | 198 | /*! 199 | * HID++ 1.0 `SUCCESS` error code. 200 | */ 201 | #define UNIFYING_HIDPP_1_0_ERROR_SUCCESS 0x00 202 | 203 | /*! 204 | * HID++ 1.0 `INVALID_SUBID` error code. 205 | */ 206 | #define UNIFYING_HIDPP_1_0_ERROR_INVALID_SUBID 0x01 207 | 208 | /*! 209 | * HID++ 1.0 `INVALID_ADDRESS` error code. 210 | */ 211 | #define UNIFYING_HIDPP_1_0_ERROR_INVALID_ADDRESS 0x02 212 | 213 | /*! 214 | * HID++ 1.0 `INVALID_VALUE` error code. 215 | */ 216 | #define UNIFYING_HIDPP_1_0_ERROR_INVALID_VALUE 0x03 217 | 218 | /*! 219 | * HID++ 1.0 `CONNECT_FAIL` error code. 220 | */ 221 | #define UNIFYING_HIDPP_1_0_ERROR_CONNECT_FAIL 0x04 222 | 223 | /*! 224 | * HID++ 1.0 `TOO_MANY_DEVICES` error code. 225 | */ 226 | #define UNIFYING_HIDPP_1_0_ERROR_TOO_MANY_DEVICES 0x05 227 | 228 | /*! 229 | * HID++ 1.0 `ALREADY_EXISTS` error code. 230 | */ 231 | #define UNIFYING_HIDPP_1_0_ERROR_ALREADY_EXISTS 0x06 232 | 233 | /*! 234 | * HID++ 1.0 `BUSY` error code. 235 | */ 236 | #define UNIFYING_HIDPP_1_0_ERROR_BUSY 0x07 237 | 238 | /*! 239 | * HID++ 1.0 `UNKNOWN_DEVICE` error code. 240 | */ 241 | #define UNIFYING_HIDPP_1_0_ERROR_UNKNOWN_DEVICE 0x08 242 | 243 | /*! 244 | * HID++ 1.0 `RESOURCE_ERROR` error code. 245 | */ 246 | #define UNIFYING_HIDPP_1_0_ERROR_RESOURCE_ERROR 0x09 247 | 248 | /*! 249 | * HID++ 1.0 `REQUEST_UNAVAILABLE` error code. 250 | */ 251 | #define UNIFYING_HIDPP_1_0_ERROR_REQUEST_UNAVAILABLE 0x0A 252 | 253 | /*! 254 | * HID++ 1.0 `INVALID_PARAM_VALUE` error code. 255 | */ 256 | #define UNIFYING_HIDPP_1_0_ERROR_INVALID_PARAM_VALUE 0x0B 257 | 258 | /*! 259 | * HID++ 1.0 `WRONG_PIN_CODE` error code. 260 | */ 261 | #define UNIFYING_HIDPP_1_0_ERROR_WRONG_PIN_CODE 0x0C 262 | 263 | // These are the only supported default timeouts according to the HID++ 1.0 specification. 264 | /*! 265 | * Default HID++ timeout for keyboards. 266 | * \see UNIFYING_DEFAULT_TIMEOUT_MOUSE 267 | */ 268 | #define UNIFYING_DEFAULT_TIMEOUT_KEYBOARD 20 269 | 270 | /*! 271 | * Default HID++ timeout for mice. 272 | * \see UNIFYING_DEFAULT_TIMEOUT_KEYBOARD 273 | */ 274 | #define UNIFYING_DEFAULT_TIMEOUT_MOUSE 8 275 | 276 | /*! 277 | * Size of an encrypted keystroke request in bytes. 278 | */ 279 | #define UNIFYING_ENCRYPTED_KEYSTROKE_REQUEST_LEN 22 280 | 281 | /*! 282 | * Size of an unencrypted keystroke request in bytes. 283 | * \todo check if this is still supported. 284 | */ 285 | #define UNIFYING_UNENCRYPTED_KEYSTROKE_REQUEST_LEN 10 286 | 287 | /*! 288 | * Size of a multimedia keystroke request in bytes. 289 | */ 290 | #define UNIFYING_MULTIMEDIA_KEYSTROKE_REQUEST_LEN 10 291 | 292 | /*! 293 | * Size of a mouse request in bytes. 294 | */ 295 | #define UNIFYING_MOUSE_REQUEST_LEN 10 296 | 297 | /*! 298 | * Bitfield indicating the left mouse button. 299 | */ 300 | #define UNIFYING_MOUSE_BUTTON_LEFT 0x01 301 | 302 | /*! 303 | * Bitfield indicating the right mouse button. 304 | */ 305 | #define UNIFYING_MOUSE_BUTTON_RIGHT 0x02 306 | 307 | /*! 308 | * Bitfield indicating the middle mouse button. 309 | */ 310 | #define UNIFYING_MOUSE_BUTTON_MIDDLE 0x04 311 | 312 | /*! 313 | * Bitfield indicating the back mouse button. 314 | */ 315 | #define UNIFYING_MOUSE_BUTTON_BACK 0x08 316 | 317 | /*! 318 | * Bitfield indicating the forward mouse button. 319 | */ 320 | #define UNIFYING_MOUSE_BUTTON_FORWARD 0x10 321 | 322 | /*! 323 | * Bitmask for de-obfuscate an AES key. 324 | * 325 | * \see unifying_deobfuscate_aes_key() 326 | */ 327 | extern const uint8_t unifying_aes_key_bitmask[UNIFYING_AES_BLOCK_LEN]; 328 | 329 | /*! 330 | * AES key byte indices for de-obfuscate an AES key. 331 | * 332 | * The On-Line Encyclopedia of Integer Sequences returns no matches for this sequence nor any similar sequence. 333 | * It appears to be random. 334 | * 335 | * \see unifying_deobfuscate_aes_key() 336 | */ 337 | extern const uint8_t unifying_aes_key_index[UNIFYING_AES_BLOCK_LEN]; 338 | 339 | /*! 340 | * AES nonce that prefixes the AES counter in the AES initialization vector (IV). 341 | */ 342 | extern const uint8_t unifying_aes_nonce_prefix[UNIFYING_AES_NONCE_PREFIX_LEN]; 343 | 344 | /*! 345 | * AES nonce that suffixes the AES counter in the AES initialization vector (IV). 346 | */ 347 | extern const uint8_t unifying_aes_nonce_suffix[UNIFYING_AES_NONCE_SUFFIX_LEN]; 348 | 349 | /*! 350 | * All RF channels that a Unifying receiver listens on during normal operation. 351 | */ 352 | extern const uint8_t unifying_channels[UNIFYING_CHANNELS_LEN]; 353 | 354 | /*! 355 | * All RF channels that a Unifying receiver listens on during pairing. 356 | * 357 | */ 358 | extern const uint8_t unifying_pairing_channels[UNIFYING_PAIRING_CHANNELS_LEN]; 359 | 360 | /*! 361 | * Initial RF address for pairing. 362 | */ 363 | extern const uint8_t unifying_pairing_address[UNIFYING_ADDRESS_LEN]; 364 | 365 | #endif 366 | -------------------------------------------------------------------------------- /src/unifying_utils.h: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * \file unifying_utils.h 4 | * \brief Miscellaneous utility functions used by the Unifying protocol 5 | */ 6 | 7 | #ifndef UNIFYING_UTILS_H 8 | #define UNIFYING_UTILS_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "unifying_const.h" 16 | 17 | /*! 18 | * Plaintext keystroke data to be encrypted. 19 | */ 20 | struct unifying_encrypted_keystroke_plaintext 21 | { 22 | /// Bitfield where each bit corresponds to a specific modifier key. 23 | uint8_t modifiers; 24 | /// Buffer of \ref UNIFYING_KEYS_LEN keyboard scancodes 25 | uint8_t keys[UNIFYING_KEYS_LEN]; 26 | /// Constant value that a Unifying receiver uses to verify that decryption succeeded. 27 | uint8_t flag; 28 | }; 29 | 30 | /*! 31 | * Counter-mode AES encryption initialization vector (IV) data. 32 | */ 33 | struct unifying_encrypted_keystroke_iv 34 | { 35 | /*! 36 | * Constant AES nonce prefix. 37 | * \see unifying_aes_nonce_prefix 38 | */ 39 | uint8_t prefix[UNIFYING_AES_NONCE_PREFIX_LEN]; 40 | /// AES counter. 41 | uint32_t counter; 42 | /*! 43 | * Constant AES nonce suffix. 44 | * \see unifying_aes_nonce_suffix 45 | */ 46 | uint8_t suffix[UNIFYING_AES_NONCE_SUFFIX_LEN]; 47 | }; 48 | /*! 49 | * Obfuscated AES encryption key data. 50 | */ 51 | struct unifying_proto_aes_key 52 | { 53 | /*! 54 | * 4 most significant bytes of the RF address that the Unifying receiver sets for us during pairing. 55 | * This is also the serial number of the paired receiver. 56 | */ 57 | uint8_t base_address[UNIFYING_ADDRESS_LEN - 1]; 58 | /// The product ID of this device. This can be any value. 59 | uint16_t device_product_id; 60 | /// The product ID of the paired receiver. This appears to always be 0x8802. 61 | uint16_t receiver_product_id; 62 | /// 4 bytes of seemingly random data generated by this device. 63 | uint32_t device_crypto; 64 | /// 4 bytes of seemingly random data generated by the paired receiver. 65 | uint32_t receiver_crypto; 66 | }; 67 | 68 | #ifdef __cplusplus 69 | extern "C" { 70 | #endif 71 | 72 | /*! 73 | * Pack a 16-bit integer into a byte array. 74 | * 75 | * Bytes are packed into the array with the most significant byte first. 76 | * 77 | * \param[out] packed Pointer to byte array that is at least 2 bytes long. 78 | * \param[in] number A 16-bit integer to pack. 79 | */ 80 | void unifying_uint16_pack(uint8_t packed[2], uint16_t number); 81 | 82 | /*! 83 | * Unpack a byte array into a 16-bit integer. 84 | * 85 | * Array bytes are expected to have the most significant byte first. 86 | * 87 | * \param[out] number Pointer to a 16-bit integer to unpack into. 88 | * \param[in] packed Pointer to byte array that is at least 2 bytes long. 89 | */ 90 | void unifying_uint16_unpack(uint16_t* number, const uint8_t packed[2]); 91 | 92 | /*! 93 | * Pack a 32-bit integer into a byte array. 94 | * 95 | * Bytes are packed into the array with the most significant byte first. 96 | * 97 | * \param[out] packed Pointer to byte array that is at least 4 bytes long. 98 | * \param[in] number A 32-bit integer to pack. 99 | */ 100 | void unifying_uint32_pack(uint8_t packed[4], uint32_t number); 101 | 102 | /*! 103 | * Unpack a byte array into a 32-bit integer. 104 | * 105 | * Array bytes are expected to have the most significant byte first. 106 | * 107 | * \param[out] number Pointer to a 32-bit integer to unpack into. 108 | * \param[in] packed Pointer to byte array that is at least 4 bytes long. 109 | */ 110 | void unifying_uint32_unpack(uint32_t* number, const uint8_t packed[4]); 111 | 112 | /*! 113 | * Clamp a 16-bit integer to a 12-bit range. 114 | * 115 | * \param[in] number A 16-bit integer to clamp. 116 | * 117 | * \return A 16-bit integer with its value clamped to a 12-bit range. 118 | * 119 | * \see unifying_mouse_move_request_pack() 120 | */ 121 | int16_t unifying_int12_clamp(int16_t number); 122 | 123 | /*! 124 | * Compute the checksum of a byte array. 125 | * 126 | * \param[in] buffer Pointer to a byte array to compute the checksum for. 127 | * \param[in] length Length of the byte array. 128 | * 129 | * \return The checksum of \p length bytes in \p buffer. 130 | */ 131 | uint8_t unifying_checksum(const uint8_t* buffer, uint8_t length); 132 | 133 | /*! 134 | * Verify the checksum of a byte array. 135 | * 136 | * Compute the checksum of the first \p length `- 1` bytes of \p buffer and compare it to the last byte of \p buffer. 137 | * 138 | * \param[in] buffer Pointer to a byte array to verify. 139 | * \param[in] length Length of the byte array. 140 | * 141 | * \return `0` if the checksum is correct. 142 | * \return `1` if the checksum is incorrect. 143 | * 144 | * \see unifying_checksum() 145 | */ 146 | uint8_t unifying_checksum_verify(const uint8_t* buffer, uint8_t length); 147 | 148 | /*! 149 | * Perform a bitwise XNOR on 2 bytes and return the result. 150 | * 151 | * \param[in] first A byte. 152 | * \param[in] second Another byte. 153 | * 154 | * \return The bitwise XOR of \p first and \p second followed by a bitwise negation. 155 | * 156 | * \see unifying_deobfuscate_aes_key() 157 | */ 158 | uint8_t unifying_xnor(uint8_t first, uint8_t second); 159 | 160 | /*! 161 | * Initialize a \ref unifying_encrypted_keystroke_plaintext structure. 162 | * 163 | * \param[out] unpacked Pointer to a \ref unifying_encrypted_keystroke_plaintext to initialize. 164 | * \param[in] modifiers Bitfield where each bit corresponds to a specific modifier key. 165 | * \param[in] keys Pointer to a buffer of \ref UNIFYING_KEYS_LEN keyboard scancodes. 166 | * 167 | * \see unifying_encrypted_keystroke_plaintext 168 | */ 169 | void unifying_encrypted_keystroke_plaintext_init(struct unifying_encrypted_keystroke_plaintext* unpacked, 170 | uint8_t modifiers, 171 | const uint8_t keys[UNIFYING_KEYS_LEN]); 172 | 173 | /*! 174 | * Pack a \ref unifying_encrypted_keystroke_plaintext into a byte array. 175 | * 176 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_AES_DATA_LEN bytes long. 177 | * \param[in] unpacked A \ref unifying_encrypted_keystroke_plaintext to pack. 178 | */ 179 | void unifying_encrypted_keystroke_plaintext_pack(uint8_t packed[UNIFYING_AES_DATA_LEN], 180 | const struct unifying_encrypted_keystroke_plaintext* unpacked); 181 | 182 | /*! 183 | * Initialize a \ref unifying_encrypted_keystroke_iv structure. 184 | * 185 | * \param[out] unpacked Pointer to a \ref unifying_encrypted_keystroke_iv to initialize. 186 | * \param[in] counter AES counter. 187 | * 188 | * \see unifying_encrypted_keystroke_iv 189 | */ 190 | void unifying_encrypted_keystroke_iv_init(struct unifying_encrypted_keystroke_iv* unpacked, 191 | uint32_t counter); 192 | 193 | /*! 194 | * Pack a \ref unifying_encrypted_keystroke_iv into a byte array. 195 | * 196 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_AES_BLOCK_LEN bytes long. 197 | * \param[in] unpacked A \ref unifying_encrypted_keystroke_iv to pack. 198 | */ 199 | void unifying_encrypted_keystroke_iv_pack(uint8_t packed[UNIFYING_AES_BLOCK_LEN], 200 | const struct unifying_encrypted_keystroke_iv* unpacked); 201 | 202 | /*! 203 | * Initialize a \ref unifying_proto_aes_key structure. 204 | * 205 | * \param[out] unpacked Pointer to a \ref unifying_proto_aes_key to initialize. 206 | * \param[in] base_address Byte array containing the `4` most significant bytes the RF address. 207 | * Most significant byte first. 208 | * \param[in] device_product_id The product ID of this device. 209 | * \param[in] receiver_product_id The product ID of the paired receiver. 210 | * \param[in] device_crypto 4 bytes of random data generated by this device. 211 | * \param[in] receiver_crypto 4 bytes of random data generated by the paired receiver. 212 | * 213 | * \see unifying_proto_aes_key 214 | */ 215 | void unifying_proto_aes_key_init(struct unifying_proto_aes_key* unpacked, 216 | uint8_t base_address[UNIFYING_ADDRESS_LEN - 1], 217 | uint16_t device_product_id, 218 | uint16_t receiver_product_id, 219 | uint32_t device_crypto, 220 | uint32_t receiver_crypto); 221 | 222 | /*! 223 | * Pack a \ref unifying_proto_aes_key into a byte array. 224 | * 225 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_AES_BLOCK_LEN bytes long. 226 | * \param[in] unpacked Pointer to a \ref unifying_proto_aes_key to pack. 227 | */ 228 | void unifying_proto_aes_key_pack(uint8_t packed[UNIFYING_AES_BLOCK_LEN], 229 | const struct unifying_proto_aes_key* unpacked); 230 | 231 | /*! 232 | * De-obfuscate a Logitech Unifying AES key. 233 | * 234 | * \param[out] aes_key Buffer of at least \ref UNIFYING_AES_BLOCK_LEN bytes to store the de-obfuscate AES key. 235 | * \param[in] proto_aes_key At least \ref UNIFYING_AES_BLOCK_LEN bytes of obfuscated AES key data. 236 | * 237 | * \see unifying_proto_aes_key 238 | * \see unifying_proto_aes_key_init() 239 | * \see unifying_proto_aes_key_pack() 240 | * \see unifying_aes_key_bitmask 241 | * \see unifying_xnor() 242 | * \see unifying_aes_key_index 243 | */ 244 | void unifying_deobfuscate_aes_key(uint8_t aes_key[UNIFYING_AES_BLOCK_LEN], 245 | const uint8_t proto_aes_key[UNIFYING_AES_BLOCK_LEN]); 246 | 247 | /*! 248 | * Compute the next RF channel to use if a transmission fails. 249 | * 250 | * \param[in] current_channel The current RF channel. 251 | * 252 | * \return A new RF channel to use. 253 | * 254 | * \see unifying_channels 255 | */ 256 | uint8_t unifying_next_channel(uint8_t current_channel); 257 | 258 | /*! 259 | * Reverse the order of a byte array. 260 | * 261 | * This function is provided purely for the convenience of this library's user. 262 | * This library stores RF addresses with the most significant byte first 263 | * but nRF24 radios expect addresses to be specified with the least significant byte first. 264 | * Since this library's user is expected to write functions for interfacing with nRF hardware, 265 | * they may need a way to reverse the byte order of an RF addresses. 266 | * 267 | * \param[out] reverse A buffer that is at least \p length bytes long to contain the reverse of \p forward. 268 | * \param[in] forward A buffer of at least \p length bytes to reverse. 269 | * \param[in] length Size of \p reverse and \p forward. 270 | */ 271 | void unifying_copy_reverse(uint8_t *reverse, const uint8_t *forward, uint8_t length); 272 | 273 | /*! 274 | * Print a buffer to stdout. 275 | * 276 | * This function is provided for debugging purposes. 277 | * 278 | * \param[in] buffer A buffer of at least \p length bytes to print. 279 | * \param[in] length Size of \p buffer. 280 | */ 281 | void unifying_print_buffer(const uint8_t *buffer, uint8_t length); 282 | 283 | #ifdef __cplusplus 284 | } 285 | #endif 286 | 287 | #endif 288 | -------------------------------------------------------------------------------- /src/unifying_state.h: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * \file unifying_state.h 4 | * \brief Structures and functions for managing the Unifying protocol's state. 5 | * 6 | * \todo Add functions for allocating and freeing \ref unifying_state 7 | * 8 | * \todo Add a function for setting timeout state. 9 | */ 10 | 11 | #ifndef UNIFYING_STATE_H 12 | #define UNIFYING_STATE_H 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "unifying_const.h" 20 | #include "unifying_error.h" 21 | #include "unifying_buffer.h" 22 | 23 | /*! 24 | * Compile and use a software implementation of AES encryption by default. 25 | * 26 | * Defining this as anything other than `0` will disable the default implementation. 27 | * If the default implementation is disable then this library's user is expected to provide their own implementation. 28 | */ 29 | #ifndef UNIFYING_HARDWARE_AES 30 | #define UNIFYING_HARDWARE_AES 0 31 | #endif 32 | 33 | #if defined(UNIFYING_HARDWARE_AES) && (UNIFYING_HARDWARE_AES == 0) 34 | // https://github.com/kokke/tiny-AES-c 35 | #include "aes.h" 36 | #endif 37 | 38 | /*! 39 | * Functions for interfacing with hardware. 40 | * 41 | * These functions are expected to be defined by the user of this library. 42 | * 43 | * \note The functionality of payload_available and payload_size may be combined in the future. 44 | */ 45 | struct unifying_interface 46 | { 47 | /*! 48 | * Transmit an RF payload with an nRF24 compatible radio. 49 | * 50 | * \param[in] payload Payload data to transmit. 51 | * \param[in] length Length of the payload data. 52 | * 53 | * \return `0` if successful. 54 | * \return Anything else on failure. 55 | */ 56 | uint8_t (*transmit_payload)(const uint8_t* payload, uint8_t length); 57 | /*! 58 | * Receive an RF payload with an nRF24 compatible radio. 59 | * 60 | * \param[out] payload Buffer for returning the received payload. 61 | * \param[in] length Length of the payload buffer. 62 | * 63 | * \return The length of the received payload. 64 | * \return `0` if no payload is available. 65 | */ 66 | uint8_t (*receive_payload)(uint8_t* payload, uint8_t length); 67 | /*! 68 | * Indicate if an RF payload is available to be received. 69 | * 70 | * \return `true` if an RF payload is available. 71 | * \return `false` if no RF payload is available. 72 | */ 73 | bool (*payload_available)(); 74 | /*! 75 | * Return the size of the most recently received payload. 76 | * 77 | * \return Size of the most recently received payload. 78 | */ 79 | uint8_t (*payload_size)(); 80 | /*! 81 | * Set the address that the radio transmits and receives on. 82 | * 83 | * \param[in] address Address to set the radio to. 84 | * 85 | * \return `0` if successful. 86 | * \return Anything else on failure. 87 | */ 88 | uint8_t (*set_address)(const uint8_t address[UNIFYING_ADDRESS_LEN]); 89 | /*! 90 | * Set the channel that the radio transmits and receives on. 91 | * 92 | * \param[in] channel Channel to set the radio to. 93 | * 94 | * \return `0` if successful. 95 | * \return Anything else on failure. 96 | */ 97 | uint8_t (*set_channel)(uint8_t channel); 98 | /*! 99 | * Return the time in milliseconds since execution started. 100 | * 101 | * \todo Use millis() by default if we are running on Arduino. 102 | * 103 | * \return Time in milliseconds since execution started. 104 | */ 105 | uint32_t (*time)(); 106 | /*! 107 | * AES-128 encrypt the supplied data. 108 | * 109 | * This function is provided so that hardware-accelerated AES can be used if it is available. 110 | * 111 | * \todo Add a default implementation that leverages Tiny AES. 112 | * https://github.com/kokke/tiny-AES-c 113 | * 114 | * \param[in,out] data \ref UNIFYING_AES_DATA_LEN bytes of unencrypted data are supplied. 115 | * If encryption is successful then at least \ref UNIFYING_AES_DATA_LEN bytes 116 | * of encrypted data should be returned. 117 | * \param[in] key AES-128 encryption key. 118 | * \param[in] iv AES-128 initialization vector. 119 | * 120 | * \return `0` if successful. 121 | * \return Anything else on failure. 122 | */ 123 | uint8_t (*encrypt)(uint8_t data[UNIFYING_AES_DATA_LEN], 124 | const uint8_t key[UNIFYING_AES_BLOCK_LEN], 125 | const uint8_t iv[UNIFYING_AES_BLOCK_LEN]); 126 | }; 127 | 128 | /*! 129 | * State information that is required for the Unifying protocol to operate correctly. 130 | */ 131 | struct unifying_state 132 | { 133 | /// Functions for interfacing with hardware. 134 | const struct unifying_interface* interface; 135 | /// Buffer for payloads to be transmitted. 136 | struct unifying_ring_buffer* transmit_buffer; 137 | /// Buffer for received payloads to be handled. 138 | struct unifying_ring_buffer* receive_buffer; 139 | /// RF address. 140 | uint8_t *address; 141 | /// AES-128 encryption key. 142 | uint8_t *aes_key; 143 | /// AES counter. 144 | uint32_t aes_counter; 145 | /*! 146 | * Default timeout. 147 | * Transmitting some payloads will set `timeout` to this value. 148 | */ 149 | uint16_t default_timeout; 150 | /// Current timeout for keep-alive packets. 151 | uint16_t timeout; 152 | /// Time that the previous payload was transmitted. 153 | uint32_t previous_transmit; 154 | /// Time that the next payload should be transmitted, based on the current timeout. 155 | uint32_t next_transmit; 156 | /// Current RF channel. This is used to compute a new channel in the event of a transmission failure. 157 | uint8_t channel; 158 | }; 159 | 160 | /*! 161 | * Information stored in \ref unifying_state.transmit_buffer "state.transmit_buffer" 162 | */ 163 | struct unifying_transmit_entry 164 | { 165 | /// Array of bytes to transmit. 166 | uint8_t* payload; 167 | /// Size of `payload` in bytes. 168 | uint8_t length; 169 | /// New timeout value to set if `payload` is successfully transmitted. 170 | uint8_t timeout; 171 | }; 172 | 173 | /*! 174 | * Information stored in \ref unifying_state.receive_buffer "state.receive_buffer" 175 | */ 176 | struct unifying_receive_entry 177 | { 178 | /// Array of received bytes. 179 | uint8_t* payload; 180 | /// Size of `payload` in bytes. 181 | uint8_t length; 182 | }; 183 | 184 | #ifdef __cplusplus 185 | extern "C" { 186 | #endif 187 | 188 | /*! 189 | * Initialize a \ref unifying_interface structure. 190 | * 191 | * \param[out] interface A \ref unifying_interface to initialize. 192 | * \param[in] transmit_payload Function for transmitting RF payloads. 193 | * see \ref unifying_interface.transmit_payload for more details. 194 | * \param[in] receive_payload Function for receiving RF payloads. 195 | * see \ref unifying_interface.receive_payload for more details. 196 | * \param[in] payload_available Function for determining if an RF payload is available to be read. 197 | * see \ref unifying_interface.payload_available for more details. 198 | * \param[in] payload_size Function for determining the size of an available RF payload. 199 | * see \ref unifying_interface.payload_size for more details. 200 | * \param[in] set_address Function for setting the RF address of a radio. 201 | * see \ref unifying_interface.set_address for more details. 202 | * \param[in] set_channel Function for setting the RF channel of a radio. 203 | * see \ref unifying_interface.set_channel for more details. 204 | * \param[in] time Function getting the time since execution started in milliseconds. 205 | * see \ref unifying_interface.time for more details. 206 | * \param[in] encrypt Function for AES-128 encrypting data. 207 | * If \ref UNIFYING_HARDWARE_AES is `0` (default) then 208 | * a default implementation is provided for this function. 209 | * Specify `NULL` to use the default implementation. 210 | * see \ref unifying_interface.encrypt for more details. 211 | * 212 | * \return \ref UNIFYING_ERROR if \p encrypt is `NULL` and no default implementation is available. 213 | * \return \ref UNIFYING_SUCCESS otherwise. 214 | * 215 | * \see unifying_interface 216 | */ 217 | enum unifying_error unifying_interface_init(struct unifying_interface* interface, 218 | uint8_t (*transmit_payload)(const uint8_t* payload, uint8_t length), 219 | uint8_t (*receive_payload)(uint8_t* payload, uint8_t length), 220 | bool (*payload_available)(), 221 | uint8_t (*payload_size)(), 222 | uint8_t (*set_address)(const uint8_t address[UNIFYING_ADDRESS_LEN]), 223 | uint8_t (*set_channel)(uint8_t channel), 224 | uint32_t (*time)(), 225 | uint8_t (*encrypt)(uint8_t data[UNIFYING_AES_DATA_LEN], 226 | const uint8_t key[UNIFYING_AES_BLOCK_LEN], 227 | const uint8_t iv[UNIFYING_AES_BLOCK_LEN])); 228 | 229 | /*! 230 | * Initialize a \ref unifying_state structure. 231 | * 232 | * \param[out] state Pointer to a \ref unifying_state to initialize. 233 | * \param[in] interface Pointer to an initialized \ref unifying_interface 234 | * for accessing hardware features. 235 | * \param[in] transmit_buffer Pointer to an initialized \ref unifying_ring_buffer 236 | * for buffering payloads for transmission. 237 | * \param[in] receive_buffer Pointer to an initialized \ref unifying_ring_buffer 238 | * for buffering received payloads. 239 | * \param[in] address Byte array with space for at least \ref UNIFYING_ADDRESS_LEN bytes 240 | * to store an RF address. 241 | * \param[in] aes_key Byte array with space for at least \ref UNIFYING_AES_BLOCK_LEN bytes 242 | * to store an AES encryption key. 243 | * \param[in] aes_counter A random 32-bit integer for AES encryption. 244 | * \param[in] default_timeout Default timeout used by some payloads. 245 | * \param[in] channel RF channel to communicate on. 246 | * This should be a value from \ref unifying_channels. 247 | * 248 | * \todo Define valid values for `default_timeout`. 249 | * 250 | * \see unifying_state 251 | * \see unifying_channels 252 | */ 253 | void unifying_state_init(struct unifying_state* state, 254 | const struct unifying_interface* interface, 255 | struct unifying_ring_buffer* transmit_buffer, 256 | struct unifying_ring_buffer* receive_buffer, 257 | uint8_t address[UNIFYING_ADDRESS_LEN], 258 | uint8_t aes_key[UNIFYING_AES_BLOCK_LEN], 259 | uint32_t aes_counter, 260 | uint16_t default_timeout, 261 | uint8_t channel); 262 | 263 | /*! 264 | * Remove and free all items in \ref unifying_state.transmit_buffer "state.transmit_buffer". 265 | * 266 | * \param[in,out] state Unifying state information. 267 | */ 268 | void unifying_state_transmit_buffer_clear(struct unifying_state* state); 269 | 270 | /*! 271 | * Remove and free all items in \ref unifying_state.receive_buffer "state.receive_buffer". 272 | * 273 | * \param[in,out] state Unifying state information. 274 | */ 275 | void unifying_state_receive_buffer_clear(struct unifying_state* state); 276 | 277 | /*! 278 | * Remove and free all items in \ref unifying_state.transmit_buffer "state.transmit_buffer" 279 | * and \ref unifying_state.receive_buffer "state.receive_buffer". 280 | * 281 | * \param[in,out] state Unifying state information. 282 | */ 283 | void unifying_state_buffers_clear(struct unifying_state* state); 284 | 285 | /*! 286 | * Set the RF channel. 287 | * 288 | * Calls \ref unifying_interface.set_channel() "state.interface.set_channel()" to change the RF channel. 289 | * If that's successful then \ref unifying_state.timeout "state.timeout" will be updated as well. 290 | * 291 | * \param[in,out] state Unifying state information. 292 | * \param[in] channel New RF channel to use. 293 | * 294 | * \return The return value of \ref unifying_interface.set_channel() "state.interface.set_channel()". 295 | */ 296 | uint8_t unifying_state_channel_set(struct unifying_state* state, uint8_t channel); 297 | 298 | /*! 299 | * Set the RF address. 300 | * 301 | * Calls \ref unifying_interface.set_address() "state.interface.set_address()" to change the RF address. 302 | * If that's successful then \ref unifying_state.timeout "state.timeout" will be updated as well. 303 | * 304 | * \param[in,out] state Unifying state information. 305 | * \param[in] address New RF address to use. 306 | * 307 | * \return The return value of \ref unifying_interface.set_address() "state.interface.set_address()". 308 | */ 309 | uint8_t unifying_state_address_set(struct unifying_state* state, const uint8_t address[UNIFYING_ADDRESS_LEN]); 310 | 311 | /*! 312 | * Initialize a \ref unifying_transmit_entry structure. 313 | * 314 | * \param[out] entry Pointer to a \ref unifying_transmit_entry to initialize. 315 | * \param[in] payload Byte array of size `length`. 316 | * \param[in] length Size of `payload` in bytes. 317 | * \param[in] timeout A new timeout associated with `payload`. 318 | * 319 | * \see unifying_transmit_entry 320 | */ 321 | void unifying_transmit_entry_init(struct unifying_transmit_entry* entry, 322 | uint8_t* payload, 323 | uint8_t length, 324 | uint8_t timeout); 325 | 326 | /*! 327 | * Create and initialize a \ref unifying_transmit_entry structure. 328 | * 329 | * \param[in] length Size in bytes of a buffer to allocate. 330 | * \param[in] timeout A new timeout to be associated with a payload. 331 | * 332 | * \return `NULL` if allocation fails. 333 | * \return Pointer to new \ref unifying_transmit_entry instance otherwise. 334 | * 335 | * \see unifying_transmit_entry 336 | * \see unifying_transmit_entry_destroy() 337 | */ 338 | struct unifying_transmit_entry* unifying_transmit_entry_create(uint8_t length, uint8_t timeout); 339 | 340 | /*! 341 | * Free a \ref unifying_transmit_entry instance. 342 | * 343 | * \param[in,out] entry Pointer to a \ref unifying_transmit_entry to free. 344 | * 345 | * \see unifying_transmit_entry 346 | * \see unifying_transmit_entry_create() 347 | */ 348 | void unifying_transmit_entry_destroy(struct unifying_transmit_entry* entry); 349 | 350 | /*! 351 | * Initialize a \ref unifying_receive_entry structure. 352 | * 353 | * \param[out] entry Pointer to a \ref unifying_receive_entry to initialize. 354 | * \param[in] payload Byte array of size `length`. 355 | * \param[in] length Size of `payload` in bytes. 356 | * 357 | * \see unifying_receive_entry 358 | */ 359 | void unifying_receive_entry_init(struct unifying_receive_entry* entry, 360 | uint8_t* payload, 361 | uint8_t length); 362 | 363 | /*! 364 | * Create and initialize a \ref unifying_receive_entry structure. 365 | * 366 | * \param[in] length Size in bytes of a buffer to allocate. 367 | * 368 | * \return `NULL` if allocation fails. 369 | * \return Pointer to new \ref unifying_receive_entry instance otherwise. 370 | * 371 | * \see unifying_receive_entry 372 | * \see unifying_receive_entry_destroy() 373 | */ 374 | struct unifying_receive_entry* unifying_receive_entry_create(uint8_t length); 375 | 376 | /*! 377 | * Free a \ref unifying_receive_entry instance. 378 | * 379 | * \param[in,out] entry Pointer to a \ref unifying_receive_entry to free. 380 | * 381 | * \see unifying_receive_entry 382 | * \see unifying_receive_entry_create() 383 | */ 384 | void unifying_receive_entry_destroy(struct unifying_receive_entry* entry); 385 | 386 | 387 | #ifdef __cplusplus 388 | } 389 | #endif 390 | 391 | #endif 392 | -------------------------------------------------------------------------------- /src/unifying_data.c: -------------------------------------------------------------------------------- 1 | 2 | #include "unifying_data.h" 3 | 4 | void unifying_pair_request_1_init(struct unifying_pair_request_1* unpacked, 5 | uint8_t id, 6 | uint16_t timeout, 7 | uint16_t product_id, 8 | uint16_t device_type) 9 | { 10 | memset(unpacked, 0, sizeof(struct unifying_pair_request_1)); 11 | unpacked->id = id; 12 | unpacked->frame = 0x5F; 13 | unpacked->step = 0x01; 14 | unpacked->timeout = timeout; // 8 for mice, 20 for keyboards. 15 | unpacked->product_id = product_id; 16 | unpacked->protocol = 0x04; // Unifying protocol. 17 | unpacked->device_type = device_type; 18 | unpacked->unknown_20 = 0x01; 19 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_pair_request_1)); 20 | } 21 | 22 | void unifying_pair_request_1_pack(uint8_t packed[UNIFYING_PAIR_REQUEST_1_LEN], 23 | const struct unifying_pair_request_1* unpacked) 24 | { 25 | packed[0] = unpacked->id; 26 | packed[1] = unpacked->frame; 27 | packed[2] = unpacked->step; 28 | memcpy(&packed[3], unpacked->unknown_3_7, sizeof(unpacked->unknown_3_7)); 29 | packed[8] = unpacked->timeout; 30 | unifying_uint16_pack(&packed[9], unpacked->product_id); 31 | packed[11] = unpacked->protocol; 32 | packed[12] = unpacked->unknown_12; 33 | unifying_uint16_pack(&packed[13], unpacked->device_type); 34 | memcpy(&packed[15], unpacked->unknown_15_19, sizeof(unpacked->unknown_15_19)); 35 | packed[20] = unpacked->unknown_20; 36 | packed[21] = unpacked->checksum; 37 | } 38 | 39 | void unifying_pair_response_1_unpack(struct unifying_pair_response_1* unpacked, 40 | const uint8_t packed[UNIFYING_PAIR_RESPONSE_1_LEN]) 41 | { 42 | unpacked->id = packed[0]; 43 | unpacked->frame = packed[1]; 44 | unpacked->step = packed[2]; 45 | memcpy(unpacked->address, &packed[3], sizeof(unpacked->address)); 46 | unpacked->unknown_8 = packed[8]; 47 | unifying_uint16_unpack(&unpacked->product_id, &packed[9]); 48 | memcpy(unpacked->unknown_11_12, &packed[11], sizeof(unpacked->unknown_11_12)); 49 | unifying_uint16_unpack(&unpacked->device_type, &packed[13]); 50 | memcpy(unpacked->unknown_15_20, &packed[15], sizeof(unpacked->unknown_15_20)); 51 | unpacked->checksum = packed[21]; 52 | } 53 | 54 | 55 | 56 | void unifying_pair_request_2_init(struct unifying_pair_request_2* unpacked, 57 | uint32_t crypto, 58 | uint32_t serial, 59 | uint16_t capabilities) 60 | { 61 | memset(unpacked, 0, sizeof(struct unifying_pair_request_2)); 62 | unpacked->frame = 0x5F; 63 | unpacked->step = 0x02; 64 | unpacked->crypto = crypto; 65 | unpacked->serial = serial; 66 | unpacked->capabilities = capabilities; 67 | unpacked->unknown_13_20[15] = 0x01; 68 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_pair_request_2)); 69 | } 70 | 71 | void unifying_pair_request_2_pack(uint8_t packed[UNIFYING_PAIR_REQUEST_2_LEN], 72 | const struct unifying_pair_request_2* unpacked) 73 | { 74 | packed[0] = unpacked->unknown_0; 75 | packed[1] = unpacked->frame; 76 | packed[2] = unpacked->step; 77 | unifying_uint32_pack(&packed[3], unpacked->crypto); 78 | unifying_uint32_pack(&packed[7], unpacked->serial); 79 | unifying_uint16_pack(&packed[11], unpacked->capabilities); 80 | memcpy(&packed[13], unpacked->unknown_13_20, sizeof(unpacked->unknown_13_20)); 81 | packed[21] = unpacked->checksum; 82 | } 83 | 84 | void unifying_pair_response_2_unpack(struct unifying_pair_response_2* unpacked, 85 | const uint8_t packed[UNIFYING_PAIR_RESPONSE_2_LEN]) 86 | { 87 | unpacked->unknown_0 = packed[0]; 88 | unpacked->frame = packed[1]; 89 | unpacked->step = packed[2]; 90 | unifying_uint32_unpack(&unpacked->crypto, &packed[3]); 91 | unifying_uint32_unpack(&unpacked->serial, &packed[7]); 92 | unifying_uint16_unpack(&unpacked->capabilities, &packed[11]); 93 | memcpy(unpacked->unknown_13_20, &packed[13], sizeof(unpacked->unknown_13_20)); 94 | unpacked->checksum = packed[21]; 95 | } 96 | 97 | 98 | 99 | void unifying_pair_request_3_init(struct unifying_pair_request_3* unpacked, const char* name, uint8_t name_length) 100 | { 101 | memset(unpacked, 0, sizeof(struct unifying_pair_request_3)); 102 | unpacked->frame = 0x5F; 103 | unpacked->step = 0x03; 104 | unpacked->unknown_3 = 0x01; 105 | unpacked->name_length = name_length; 106 | memcpy(unpacked->name, name, name_length); 107 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_pair_request_3)); 108 | } 109 | 110 | void unifying_pair_request_3_pack(uint8_t packed[UNIFYING_PAIR_REQUEST_3_LEN], 111 | const struct unifying_pair_request_3* unpacked) 112 | { 113 | packed[0] = unpacked->unknown_0; 114 | packed[1] = unpacked->frame; 115 | packed[2] = unpacked->step; 116 | packed[3] = unpacked->unknown_3; 117 | packed[4] = unpacked->name_length; 118 | memcpy(&packed[5], unpacked->name, sizeof(unpacked->name)); 119 | packed[21] = unpacked->checksum; 120 | } 121 | 122 | void unifying_pair_response_3_unpack(struct unifying_pair_response_3* unpacked, 123 | const uint8_t packed[UNIFYING_PAIR_RESPONSE_3_LEN]) 124 | { 125 | unpacked->unknown_0 = packed[0]; 126 | unpacked->frame = packed[1]; 127 | unpacked->step = packed[2]; 128 | memcpy(unpacked->unknown_3_8, &packed[3], sizeof(unpacked->unknown_3_8)); 129 | unpacked->checksum = packed[9]; 130 | } 131 | 132 | void unifying_pair_complete_request_init(struct unifying_pair_complete_request* unpacked) 133 | { 134 | memset(unpacked, 0, sizeof(struct unifying_pair_complete_request)); 135 | unpacked->frame = 0x0F; 136 | unpacked->step = 0x06; 137 | unpacked->unknown_3 = 0x01; 138 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_pair_complete_request)); 139 | } 140 | 141 | void unifying_pair_complete_request_pack(uint8_t packed[UNIFYING_PAIR_COMPLETE_REQUEST_LEN], 142 | const struct unifying_pair_complete_request* unpacked) 143 | { 144 | packed[0] = unpacked->unknown_0; 145 | packed[1] = unpacked->frame; 146 | packed[2] = unpacked->step; 147 | packed[3] = unpacked->unknown_3; 148 | memcpy(&packed[4], unpacked->unknown_4_8, sizeof(unpacked->unknown_4_8)); 149 | packed[9] = unpacked->checksum; 150 | } 151 | 152 | 153 | 154 | void unifying_long_wake_up_request_init(struct unifying_long_wake_up_request* unpacked, uint8_t index) 155 | { 156 | memset(unpacked, 0, sizeof(struct unifying_long_wake_up_request)); 157 | unpacked->index = index; 158 | unpacked->frame = 0x51; 159 | unpacked->index_2 = index; 160 | unpacked->unknown_4 = 0x00; 161 | unpacked->unknown_5_7[0] = 0x01; 162 | unpacked->unknown_5_7[1] = 0x01; 163 | unpacked->unknown_5_7[2] = 0x01; 164 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_long_wake_up_request)); 165 | } 166 | 167 | void unifying_long_wake_up_request_pack(uint8_t packed[UNIFYING_LONG_WAKE_UP_REQUEST_LEN], 168 | const struct unifying_long_wake_up_request* unpacked) 169 | { 170 | packed[0] = unpacked->index; 171 | packed[1] = unpacked->frame; 172 | packed[2] = unpacked->index_2; 173 | packed[3] = unpacked->unknown_3; 174 | packed[4] = unpacked->unknown_4; 175 | memcpy(&packed[5], unpacked->unknown_5_7, sizeof(unpacked->unknown_5_7)); 176 | memcpy(&packed[8], unpacked->unknown_8_20, sizeof(unpacked->unknown_8_20)); 177 | packed[21] = unpacked->checksum; 178 | } 179 | 180 | 181 | 182 | void unifying_short_wake_up_request_init(struct unifying_short_wake_up_request* unpacked, uint8_t index) 183 | { 184 | memset(unpacked, 0, sizeof(struct unifying_short_wake_up_request)); 185 | unpacked->index = index; 186 | unpacked->frame = 0x50; 187 | unpacked->unknown_2 = 0x01; 188 | unpacked->unknown_3 = 0x4B; 189 | unpacked->unknown_4 = 0x01; 190 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_short_wake_up_request)); 191 | } 192 | 193 | void unifying_short_wake_up_request_pack(uint8_t packed[UNIFYING_SHORT_WAKE_UP_REQUEST_LEN], 194 | const struct unifying_short_wake_up_request* unpacked) 195 | { 196 | packed[0] = unpacked->index; 197 | packed[1] = unpacked->frame; 198 | packed[2] = unpacked->unknown_2; 199 | packed[3] = unpacked->unknown_3; 200 | packed[4] = unpacked->unknown_4; 201 | memcpy(&packed[5], unpacked->unknown_5_8, sizeof(unpacked->unknown_5_8)); 202 | packed[9] = unpacked->checksum; 203 | } 204 | 205 | 206 | 207 | void unifying_set_timeout_request_init(struct unifying_set_timeout_request* unpacked, uint16_t timeout) 208 | { 209 | memset(unpacked, 0, sizeof(struct unifying_set_timeout_request)); 210 | unpacked->frame = 0x4F; 211 | unpacked->timeout = timeout; 212 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_set_timeout_request)); 213 | } 214 | 215 | void unifying_set_timeout_request_pack(uint8_t packed[UNIFYING_SET_TIMEOUT_REQUEST_LEN], 216 | const struct unifying_set_timeout_request* unpacked) 217 | { 218 | packed[0] = unpacked->unknown_0; 219 | packed[1] = unpacked->frame; 220 | packed[2] = unpacked->unknown_2; 221 | unifying_uint16_pack(&packed[3], unpacked->timeout); 222 | memcpy(&packed[5], unpacked->unknown_5_8, sizeof(unpacked->unknown_5_8)); 223 | packed[9] = unpacked->checksum; 224 | } 225 | 226 | 227 | 228 | void unifying_keep_alive_request_init(struct unifying_keep_alive_request* unpacked, uint16_t timeout) 229 | { 230 | memset(unpacked, 0, sizeof(struct unifying_keep_alive_request)); 231 | unpacked->frame = 0x40; 232 | unpacked->timeout = timeout; 233 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_keep_alive_request)); 234 | } 235 | 236 | void unifying_keep_alive_request_pack(uint8_t packed[UNIFYING_KEEP_ALIVE_REQUEST_LEN], 237 | const struct unifying_keep_alive_request* unpacked) 238 | { 239 | packed[0] = unpacked->unknown_0; 240 | packed[1] = unpacked->frame; 241 | unifying_uint16_pack(&packed[2], unpacked->timeout); 242 | packed[4] = unpacked->checksum; 243 | } 244 | 245 | 246 | 247 | void unifying_hidpp_1_0_short_init(struct unifying_hidpp_1_0_short* unpacked, 248 | uint8_t index, 249 | uint8_t sub_id, 250 | uint8_t params[UNIFYING_HIDPP_1_0_SHORT_PARAMS_LEN]) 251 | { 252 | memset(unpacked, 0, sizeof(struct unifying_hidpp_1_0_short)); 253 | unpacked->report = 0x10; 254 | unpacked->index = index; 255 | unpacked->sub_id = sub_id; 256 | memcpy(unpacked->params, params, sizeof(unpacked->params)); 257 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_hidpp_1_0_short)); 258 | } 259 | 260 | void unifying_hidpp_1_0_short_pack(uint8_t packed[UNIFYING_HIDPP_1_0_SHORT_LEN], 261 | const struct unifying_hidpp_1_0_short* unpacked) 262 | { 263 | packed[0] = unpacked->unknown_0; 264 | packed[1] = unpacked->report; 265 | packed[2] = unpacked->index; 266 | packed[3] = unpacked->sub_id; 267 | memcpy(&packed[4], unpacked->params, sizeof(unpacked->params)); 268 | packed[8] = unpacked->unknown_8; 269 | packed[9] = unpacked->checksum; 270 | } 271 | 272 | void unifying_hidpp_1_0_short_unpack(struct unifying_hidpp_1_0_short* unpacked, 273 | const uint8_t packed[UNIFYING_HIDPP_1_0_SHORT_LEN]) 274 | { 275 | unpacked->unknown_0 = packed[0]; 276 | unpacked->report = packed[1]; 277 | unpacked->index = packed[2]; 278 | unpacked->sub_id = packed[3]; 279 | memcpy(unpacked->params, &packed[4], sizeof(unpacked->params)); 280 | unpacked->unknown_8 = packed[8]; 281 | unpacked->checksum = packed[9]; 282 | } 283 | 284 | 285 | 286 | void unifying_hidpp_1_0_long_init(struct unifying_hidpp_1_0_long* unpacked, 287 | uint8_t index, 288 | uint8_t sub_id, 289 | uint8_t params[UNIFYING_HIDPP_1_0_LONG_PARAMS_LEN]) 290 | { 291 | memset(unpacked, 0, sizeof(struct unifying_hidpp_1_0_long)); 292 | unpacked->report = 0x11; 293 | unpacked->index = index; 294 | unpacked->sub_id = sub_id; 295 | memcpy(unpacked->params, params, sizeof(unpacked->params)); 296 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_hidpp_1_0_long)); 297 | } 298 | 299 | void unifying_hidpp_1_0_long_pack(uint8_t packed[UNIFYING_HIDPP_1_0_LONG_LEN], 300 | const struct unifying_hidpp_1_0_long* unpacked) 301 | { 302 | packed[0] = unpacked->unknown_0; 303 | packed[1] = unpacked->report; 304 | packed[2] = unpacked->index; 305 | packed[3] = unpacked->sub_id; 306 | memcpy(&packed[4], unpacked->params, sizeof(unpacked->params)); 307 | packed[9] = unpacked->checksum; 308 | } 309 | 310 | void unifying_hidpp_1_0_long_unpack(struct unifying_hidpp_1_0_long* unpacked, 311 | const uint8_t packed[UNIFYING_HIDPP_1_0_LONG_LEN]) 312 | { 313 | unpacked->unknown_0 = packed[0]; 314 | unpacked->report = packed[1]; 315 | unpacked->index = packed[2]; 316 | unpacked->sub_id = packed[3]; 317 | memcpy(unpacked->params, &packed[4], sizeof(unpacked->params)); 318 | unpacked->checksum = packed[9]; 319 | } 320 | 321 | 322 | 323 | void unifying_encrypted_keystroke_request_init(struct unifying_encrypted_keystroke_request* unpacked, 324 | uint8_t ciphertext[UNIFYING_AES_DATA_LEN], 325 | uint32_t counter) 326 | { 327 | memset(unpacked, 0, sizeof(struct unifying_encrypted_keystroke_request)); 328 | unpacked->frame = 0xD3; 329 | memcpy(unpacked->ciphertext, ciphertext, sizeof(unpacked->ciphertext)); 330 | unpacked->counter = counter; 331 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_encrypted_keystroke_request)); 332 | } 333 | 334 | void unifying_encrypted_keystroke_request_pack(uint8_t packed[UNIFYING_ENCRYPTED_KEYSTROKE_REQUEST_LEN], 335 | const struct unifying_encrypted_keystroke_request* unpacked) 336 | { 337 | packed[0] = unpacked->unknown_0; 338 | packed[1] = unpacked->frame; 339 | memcpy(&packed[2], unpacked->ciphertext, sizeof(unpacked->ciphertext)); 340 | unifying_uint32_pack(&packed[10], unpacked->counter); 341 | memcpy(&packed[14], unpacked->unknown_14_20, sizeof(unpacked->unknown_14_20)); 342 | packed[21] = unpacked->checksum; 343 | } 344 | 345 | void unifying_multimeia_keystroke_request_init(struct unifying_multimeia_keystroke_request* unpacked, 346 | uint8_t keys[UNIFYING_MULTIMEDIA_KEYS_LEN]) 347 | { 348 | memset(unpacked, 0, sizeof(struct unifying_multimeia_keystroke_request)); 349 | unpacked->frame = 0xC3; 350 | memcpy(unpacked->keys, keys, sizeof(unpacked->keys)); 351 | unpacked->checksum = unifying_checksum((uint8_t*) unpacked, sizeof(struct unifying_multimeia_keystroke_request)); 352 | } 353 | 354 | void unifying_multimeia_keystroke_request_pack(uint8_t packed[UNIFYING_MULTIMEDIA_KEYSTROKE_REQUEST_LEN], 355 | const struct unifying_multimeia_keystroke_request* unpacked) 356 | { 357 | packed[0] = unpacked->unknown_0; 358 | packed[1] = unpacked->frame; 359 | memcpy(&packed[2], unpacked->keys, sizeof(unpacked->keys)); 360 | memcpy(&packed[6], unpacked->unknown_6_8, sizeof(unpacked->unknown_6_8)); 361 | packed[9] = unpacked->checksum; 362 | } 363 | 364 | void unifying_mouse_request_init(struct unifying_mouse_request* unpacked, 365 | uint8_t buttons, 366 | int16_t move_y, 367 | int16_t move_x, 368 | int8_t wheel_y, 369 | int8_t wheel_x) 370 | { 371 | memset(unpacked, 0, sizeof(struct unifying_mouse_request)); 372 | unpacked->frame = 0xC2; 373 | unpacked->buttons = buttons; 374 | unpacked->move_x = move_x; 375 | unpacked->move_y = move_y; 376 | unpacked->wheel_x = wheel_x; 377 | unpacked->wheel_y = wheel_y; 378 | } 379 | 380 | void unifying_mouse_request_pack(uint8_t packed[UNIFYING_MOUSE_REQUEST_LEN], 381 | const struct unifying_mouse_request* unpacked) 382 | { 383 | packed[0] = unpacked->unknown_0; 384 | packed[1] = unpacked->frame; 385 | packed[2] = unpacked->buttons; 386 | packed[3] = unpacked->unknown_3; 387 | 388 | // X and Y axis movement packing. 389 | // YY XY XX 390 | // | || | 391 | // | || '- bytes 4-11 of X axis movement 392 | // | || 393 | // | |'- byte 8-11 of Y axis movement 394 | // | | 395 | // | '- bytes 0-3 of X axis movement 396 | // | 397 | // '- bytes 0-7 of Y axis movement 398 | packed[4] = unpacked->move_y & 0xFF; 399 | packed[5] = ((unpacked->move_y >> 8) & 0x0F) | ((unpacked->move_x << 4) & 0xF0); 400 | packed[6] = (unpacked->move_x >> 4) & 0xFF; 401 | 402 | packed[7] = unpacked->wheel_y; 403 | packed[8] = unpacked->wheel_x; 404 | packed[9] = unifying_checksum(packed, UNIFYING_MOUSE_REQUEST_LEN - 1); 405 | } 406 | 407 | 408 | -------------------------------------------------------------------------------- /src/aes.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This is an implementation of the AES algorithm, specifically ECB, CTR and CBC mode. 4 | Block size can be chosen in aes.h - available choices are AES128, AES192, AES256. 5 | 6 | The implementation is verified against the test vectors in: 7 | National Institute of Standards and Technology Special Publication 800-38A 2001 ED 8 | 9 | ECB-AES128 10 | ---------- 11 | 12 | plain-text: 13 | 6bc1bee22e409f96e93d7e117393172a 14 | ae2d8a571e03ac9c9eb76fac45af8e51 15 | 30c81c46a35ce411e5fbc1191a0a52ef 16 | f69f2445df4f9b17ad2b417be66c3710 17 | 18 | key: 19 | 2b7e151628aed2a6abf7158809cf4f3c 20 | 21 | resulting cipher 22 | 3ad77bb40d7a3660a89ecaf32466ef97 23 | f5d3d58503b9699de785895a96fdbaaf 24 | 43b1cd7f598ece23881b00e3ed030688 25 | 7b0c785e27e8ad3f8223207104725dd4 26 | 27 | 28 | NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) 29 | You should pad the end of the string with zeros if this is not the case. 30 | For AES192/256 the key size is proportionally larger. 31 | 32 | */ 33 | 34 | 35 | /*****************************************************************************/ 36 | /* Includes: */ 37 | /*****************************************************************************/ 38 | #include 39 | #include // CBC mode, for memset 40 | #include "aes.h" 41 | 42 | /*****************************************************************************/ 43 | /* Defines: */ 44 | /*****************************************************************************/ 45 | // The number of columns comprising a state in AES. This is a constant in AES. Value=4 46 | #define Nb 4 47 | 48 | #if defined(AES256) && (AES256 == 1) 49 | #define Nk 8 50 | #define Nr 14 51 | #elif defined(AES192) && (AES192 == 1) 52 | #define Nk 6 53 | #define Nr 12 54 | #else 55 | #define Nk 4 // The number of 32 bit words in a key. 56 | #define Nr 10 // The number of rounds in AES Cipher. 57 | #endif 58 | 59 | // jcallan@github points out that declaring Multiply as a function 60 | // reduces code size considerably with the Keil ARM compiler. 61 | // See this link for more information: https://github.com/kokke/tiny-AES-C/pull/3 62 | #ifndef MULTIPLY_AS_A_FUNCTION 63 | #define MULTIPLY_AS_A_FUNCTION 0 64 | #endif 65 | 66 | 67 | 68 | 69 | /*****************************************************************************/ 70 | /* Private variables: */ 71 | /*****************************************************************************/ 72 | // state - array holding the intermediate results during decryption. 73 | typedef uint8_t state_t[4][4]; 74 | 75 | 76 | 77 | // The lookup-tables are marked const so they can be placed in read-only storage instead of RAM 78 | // The numbers below can be computed dynamically trading ROM for RAM - 79 | // This can be useful in (embedded) bootloader applications, where ROM is often limited. 80 | static const uint8_t sbox[256] = { 81 | //0 1 2 3 4 5 6 7 8 9 A B C D E F 82 | 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 83 | 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 84 | 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 85 | 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 86 | 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 87 | 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 88 | 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 89 | 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 90 | 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 91 | 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 92 | 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 93 | 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 94 | 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 95 | 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 96 | 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 97 | 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; 98 | 99 | static const uint8_t rsbox[256] = { 100 | 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 101 | 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 102 | 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, 103 | 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, 104 | 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 105 | 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, 106 | 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, 107 | 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 108 | 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, 109 | 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, 110 | 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 111 | 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, 112 | 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, 113 | 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 114 | 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 115 | 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; 116 | 117 | // The round constant word array, Rcon[i], contains the values given by 118 | // x to the power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8) 119 | static const uint8_t Rcon[11] = { 120 | 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; 121 | 122 | /* 123 | * Jordan Goulder points out in PR #12 (https://github.com/kokke/tiny-AES-C/pull/12), 124 | * that you can remove most of the elements in the Rcon array, because they are unused. 125 | * 126 | * From Wikipedia's article on the Rijndael key schedule @ https://en.wikipedia.org/wiki/Rijndael_key_schedule#Rcon 127 | * 128 | * "Only the first some of these constants are actually used – up to rcon[10] for AES-128 (as 11 round keys are needed), 129 | * up to rcon[8] for AES-192, up to rcon[7] for AES-256. rcon[0] is not used in AES algorithm." 130 | */ 131 | 132 | 133 | /*****************************************************************************/ 134 | /* Private functions: */ 135 | /*****************************************************************************/ 136 | /* 137 | static uint8_t getSBoxValue(uint8_t num) 138 | { 139 | return sbox[num]; 140 | } 141 | */ 142 | #define getSBoxValue(num) (sbox[(num)]) 143 | /* 144 | static uint8_t getSBoxInvert(uint8_t num) 145 | { 146 | return rsbox[num]; 147 | } 148 | */ 149 | #define getSBoxInvert(num) (rsbox[(num)]) 150 | 151 | // This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. 152 | static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key) 153 | { 154 | unsigned i, j, k; 155 | uint8_t tempa[4]; // Used for the column/row operations 156 | 157 | // The first round key is the key itself. 158 | for (i = 0; i < Nk; ++i) 159 | { 160 | RoundKey[(i * 4) + 0] = Key[(i * 4) + 0]; 161 | RoundKey[(i * 4) + 1] = Key[(i * 4) + 1]; 162 | RoundKey[(i * 4) + 2] = Key[(i * 4) + 2]; 163 | RoundKey[(i * 4) + 3] = Key[(i * 4) + 3]; 164 | } 165 | 166 | // All other round keys are found from the previous round keys. 167 | for (i = Nk; i < Nb * (Nr + 1); ++i) 168 | { 169 | { 170 | k = (i - 1) * 4; 171 | tempa[0]=RoundKey[k + 0]; 172 | tempa[1]=RoundKey[k + 1]; 173 | tempa[2]=RoundKey[k + 2]; 174 | tempa[3]=RoundKey[k + 3]; 175 | 176 | } 177 | 178 | if (i % Nk == 0) 179 | { 180 | // This function shifts the 4 bytes in a word to the left once. 181 | // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] 182 | 183 | // Function RotWord() 184 | { 185 | const uint8_t u8tmp = tempa[0]; 186 | tempa[0] = tempa[1]; 187 | tempa[1] = tempa[2]; 188 | tempa[2] = tempa[3]; 189 | tempa[3] = u8tmp; 190 | } 191 | 192 | // SubWord() is a function that takes a four-byte input word and 193 | // applies the S-box to each of the four bytes to produce an output word. 194 | 195 | // Function Subword() 196 | { 197 | tempa[0] = getSBoxValue(tempa[0]); 198 | tempa[1] = getSBoxValue(tempa[1]); 199 | tempa[2] = getSBoxValue(tempa[2]); 200 | tempa[3] = getSBoxValue(tempa[3]); 201 | } 202 | 203 | tempa[0] = tempa[0] ^ Rcon[i/Nk]; 204 | } 205 | #if defined(AES256) && (AES256 == 1) 206 | if (i % Nk == 4) 207 | { 208 | // Function Subword() 209 | { 210 | tempa[0] = getSBoxValue(tempa[0]); 211 | tempa[1] = getSBoxValue(tempa[1]); 212 | tempa[2] = getSBoxValue(tempa[2]); 213 | tempa[3] = getSBoxValue(tempa[3]); 214 | } 215 | } 216 | #endif 217 | j = i * 4; k=(i - Nk) * 4; 218 | RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0]; 219 | RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1]; 220 | RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2]; 221 | RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3]; 222 | } 223 | } 224 | 225 | void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key) 226 | { 227 | KeyExpansion(ctx->RoundKey, key); 228 | } 229 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 230 | void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv) 231 | { 232 | KeyExpansion(ctx->RoundKey, key); 233 | memcpy (ctx->Iv, iv, AES_BLOCKLEN); 234 | } 235 | void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv) 236 | { 237 | memcpy (ctx->Iv, iv, AES_BLOCKLEN); 238 | } 239 | #endif 240 | 241 | // This function adds the round key to state. 242 | // The round key is added to the state by an XOR function. 243 | static void AddRoundKey(uint8_t round,state_t* state,uint8_t* RoundKey) 244 | { 245 | uint8_t i,j; 246 | for (i = 0; i < 4; ++i) 247 | { 248 | for (j = 0; j < 4; ++j) 249 | { 250 | (*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j]; 251 | } 252 | } 253 | } 254 | 255 | // The SubBytes Function Substitutes the values in the 256 | // state matrix with values in an S-box. 257 | static void SubBytes(state_t* state) 258 | { 259 | uint8_t i, j; 260 | for (i = 0; i < 4; ++i) 261 | { 262 | for (j = 0; j < 4; ++j) 263 | { 264 | (*state)[j][i] = getSBoxValue((*state)[j][i]); 265 | } 266 | } 267 | } 268 | 269 | // The ShiftRows() function shifts the rows in the state to the left. 270 | // Each row is shifted with different offset. 271 | // Offset = Row number. So the first row is not shifted. 272 | static void ShiftRows(state_t* state) 273 | { 274 | uint8_t temp; 275 | 276 | // Rotate first row 1 columns to left 277 | temp = (*state)[0][1]; 278 | (*state)[0][1] = (*state)[1][1]; 279 | (*state)[1][1] = (*state)[2][1]; 280 | (*state)[2][1] = (*state)[3][1]; 281 | (*state)[3][1] = temp; 282 | 283 | // Rotate second row 2 columns to left 284 | temp = (*state)[0][2]; 285 | (*state)[0][2] = (*state)[2][2]; 286 | (*state)[2][2] = temp; 287 | 288 | temp = (*state)[1][2]; 289 | (*state)[1][2] = (*state)[3][2]; 290 | (*state)[3][2] = temp; 291 | 292 | // Rotate third row 3 columns to left 293 | temp = (*state)[0][3]; 294 | (*state)[0][3] = (*state)[3][3]; 295 | (*state)[3][3] = (*state)[2][3]; 296 | (*state)[2][3] = (*state)[1][3]; 297 | (*state)[1][3] = temp; 298 | } 299 | 300 | static uint8_t xtime(uint8_t x) 301 | { 302 | return ((x<<1) ^ (((x>>7) & 1) * 0x1b)); 303 | } 304 | 305 | // MixColumns function mixes the columns of the state matrix 306 | static void MixColumns(state_t* state) 307 | { 308 | uint8_t i; 309 | uint8_t Tmp, Tm, t; 310 | for (i = 0; i < 4; ++i) 311 | { 312 | t = (*state)[i][0]; 313 | Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ; 314 | Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ; 315 | Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ; 316 | Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ; 317 | Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ; 318 | } 319 | } 320 | 321 | // Multiply is used to multiply numbers in the field GF(2^8) 322 | // Note: The last call to xtime() is unneeded, but often ends up generating a smaller binary 323 | // The compiler seems to be able to vectorize the operation better this way. 324 | // See https://github.com/kokke/tiny-AES-c/pull/34 325 | #if MULTIPLY_AS_A_FUNCTION 326 | static uint8_t Multiply(uint8_t x, uint8_t y) 327 | { 328 | return (((y & 1) * x) ^ 329 | ((y>>1 & 1) * xtime(x)) ^ 330 | ((y>>2 & 1) * xtime(xtime(x))) ^ 331 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ 332 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); /* this last call to xtime() can be omitted */ 333 | } 334 | #else 335 | #define Multiply(x, y) \ 336 | ( ((y & 1) * x) ^ \ 337 | ((y>>1 & 1) * xtime(x)) ^ \ 338 | ((y>>2 & 1) * xtime(xtime(x))) ^ \ 339 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \ 340 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \ 341 | 342 | #endif 343 | 344 | #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1) 345 | // MixColumns function mixes the columns of the state matrix. 346 | // The method used to multiply may be difficult to understand for the inexperienced. 347 | // Please use the references to gain more information. 348 | static void InvMixColumns(state_t* state) 349 | { 350 | int i; 351 | uint8_t a, b, c, d; 352 | for (i = 0; i < 4; ++i) 353 | { 354 | a = (*state)[i][0]; 355 | b = (*state)[i][1]; 356 | c = (*state)[i][2]; 357 | d = (*state)[i][3]; 358 | 359 | (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09); 360 | (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d); 361 | (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b); 362 | (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e); 363 | } 364 | } 365 | 366 | 367 | // The SubBytes Function Substitutes the values in the 368 | // state matrix with values in an S-box. 369 | static void InvSubBytes(state_t* state) 370 | { 371 | uint8_t i, j; 372 | for (i = 0; i < 4; ++i) 373 | { 374 | for (j = 0; j < 4; ++j) 375 | { 376 | (*state)[j][i] = getSBoxInvert((*state)[j][i]); 377 | } 378 | } 379 | } 380 | 381 | static void InvShiftRows(state_t* state) 382 | { 383 | uint8_t temp; 384 | 385 | // Rotate first row 1 columns to right 386 | temp = (*state)[3][1]; 387 | (*state)[3][1] = (*state)[2][1]; 388 | (*state)[2][1] = (*state)[1][1]; 389 | (*state)[1][1] = (*state)[0][1]; 390 | (*state)[0][1] = temp; 391 | 392 | // Rotate second row 2 columns to right 393 | temp = (*state)[0][2]; 394 | (*state)[0][2] = (*state)[2][2]; 395 | (*state)[2][2] = temp; 396 | 397 | temp = (*state)[1][2]; 398 | (*state)[1][2] = (*state)[3][2]; 399 | (*state)[3][2] = temp; 400 | 401 | // Rotate third row 3 columns to right 402 | temp = (*state)[0][3]; 403 | (*state)[0][3] = (*state)[1][3]; 404 | (*state)[1][3] = (*state)[2][3]; 405 | (*state)[2][3] = (*state)[3][3]; 406 | (*state)[3][3] = temp; 407 | } 408 | #endif // #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1) 409 | 410 | // Cipher is the main function that encrypts the PlainText. 411 | static void Cipher(state_t* state, uint8_t* RoundKey) 412 | { 413 | uint8_t round = 0; 414 | 415 | // Add the First round key to the state before starting the rounds. 416 | AddRoundKey(0, state, RoundKey); 417 | 418 | // There will be Nr rounds. 419 | // The first Nr-1 rounds are identical. 420 | // These Nr-1 rounds are executed in the loop below. 421 | for (round = 1; round < Nr; ++round) 422 | { 423 | SubBytes(state); 424 | ShiftRows(state); 425 | MixColumns(state); 426 | AddRoundKey(round, state, RoundKey); 427 | } 428 | 429 | // The last round is given below. 430 | // The MixColumns function is not here in the last round. 431 | SubBytes(state); 432 | ShiftRows(state); 433 | AddRoundKey(Nr, state, RoundKey); 434 | } 435 | 436 | #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1) 437 | static void InvCipher(state_t* state,uint8_t* RoundKey) 438 | { 439 | uint8_t round = 0; 440 | 441 | // Add the First round key to the state before starting the rounds. 442 | AddRoundKey(Nr, state, RoundKey); 443 | 444 | // There will be Nr rounds. 445 | // The first Nr-1 rounds are identical. 446 | // These Nr-1 rounds are executed in the loop below. 447 | for (round = (Nr - 1); round > 0; --round) 448 | { 449 | InvShiftRows(state); 450 | InvSubBytes(state); 451 | AddRoundKey(round, state, RoundKey); 452 | InvMixColumns(state); 453 | } 454 | 455 | // The last round is given below. 456 | // The MixColumns function is not here in the last round. 457 | InvShiftRows(state); 458 | InvSubBytes(state); 459 | AddRoundKey(0, state, RoundKey); 460 | } 461 | #endif // #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1) 462 | 463 | /*****************************************************************************/ 464 | /* Public functions: */ 465 | /*****************************************************************************/ 466 | #if defined(ECB) && (ECB == 1) 467 | 468 | 469 | void AES_ECB_encrypt(struct AES_ctx *ctx, uint8_t* buf) 470 | { 471 | // The next function call encrypts the PlainText with the Key using AES algorithm. 472 | Cipher((state_t*)buf, ctx->RoundKey); 473 | } 474 | 475 | void AES_ECB_decrypt(struct AES_ctx* ctx, uint8_t* buf) 476 | { 477 | // The next function call decrypts the PlainText with the Key using AES algorithm. 478 | InvCipher((state_t*)buf, ctx->RoundKey); 479 | } 480 | 481 | 482 | #endif // #if defined(ECB) && (ECB == 1) 483 | 484 | 485 | 486 | 487 | 488 | #if defined(CBC) && (CBC == 1) 489 | 490 | 491 | static void XorWithIv(uint8_t* buf, uint8_t* Iv) 492 | { 493 | uint8_t i; 494 | for (i = 0; i < AES_BLOCKLEN; ++i) // The block in AES is always 128bit no matter the key size 495 | { 496 | buf[i] ^= Iv[i]; 497 | } 498 | } 499 | 500 | void AES_CBC_encrypt_buffer(struct AES_ctx *ctx,uint8_t* buf, uint32_t length) 501 | { 502 | uintptr_t i; 503 | uint8_t *Iv = ctx->Iv; 504 | for (i = 0; i < length; i += AES_BLOCKLEN) 505 | { 506 | XorWithIv(buf, Iv); 507 | Cipher((state_t*)buf, ctx->RoundKey); 508 | Iv = buf; 509 | buf += AES_BLOCKLEN; 510 | //printf("Step %d - %d", i/16, i); 511 | } 512 | /* store Iv in ctx for next call */ 513 | memcpy(ctx->Iv, Iv, AES_BLOCKLEN); 514 | } 515 | 516 | void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length) 517 | { 518 | uintptr_t i; 519 | uint8_t storeNextIv[AES_BLOCKLEN]; 520 | for (i = 0; i < length; i += AES_BLOCKLEN) 521 | { 522 | memcpy(storeNextIv, buf, AES_BLOCKLEN); 523 | InvCipher((state_t*)buf, ctx->RoundKey); 524 | XorWithIv(buf, ctx->Iv); 525 | memcpy(ctx->Iv, storeNextIv, AES_BLOCKLEN); 526 | buf += AES_BLOCKLEN; 527 | } 528 | 529 | } 530 | 531 | #endif // #if defined(CBC) && (CBC == 1) 532 | 533 | 534 | 535 | #if defined(CTR) && (CTR == 1) 536 | 537 | /* Symmetrical operation: same function for encrypting as for decrypting. Note any IV/nonce should never be reused with the same key */ 538 | void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length) 539 | { 540 | uint8_t buffer[AES_BLOCKLEN]; 541 | 542 | unsigned i; 543 | int bi; 544 | for (i = 0, bi = AES_BLOCKLEN; i < length; ++i, ++bi) 545 | { 546 | if (bi == AES_BLOCKLEN) /* we need to regen xor compliment in buffer */ 547 | { 548 | 549 | memcpy(buffer, ctx->Iv, AES_BLOCKLEN); 550 | Cipher((state_t*)buffer,ctx->RoundKey); 551 | 552 | /* Increment Iv and handle overflow */ 553 | for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi) 554 | { 555 | /* inc will owerflow */ 556 | if (ctx->Iv[bi] == 255) 557 | { 558 | ctx->Iv[bi] = 0; 559 | continue; 560 | } 561 | ctx->Iv[bi] += 1; 562 | break; 563 | } 564 | bi = 0; 565 | } 566 | 567 | buf[i] = (buf[i] ^ buffer[bi]); 568 | } 569 | } 570 | 571 | #endif // #if defined(CTR) && (CTR == 1) 572 | 573 | -------------------------------------------------------------------------------- /src/unifying_data.h: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * \file unifying_data.h 4 | * \brief Structures representing Unifying payload data. 5 | * 6 | * Unifying payloads are represented here as structs in an attempt to document payload structure in code. 7 | * Functions for initializing, packing, and unpacking these structs are also provided here. 8 | * There may be a small performance loss from converting between uint8_t arrays and structs. 9 | * That performance loss is deemed acceptable for the time being. 10 | * 11 | * \todo Document struct fields. 12 | * 13 | * \todo move checksum calculation from *_init functions to *_pack functions. 14 | */ 15 | 16 | #ifndef UNIFYING_DATA_H 17 | #define UNIFYING_DATA_H 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include "unifying_const.h" 24 | #include "unifying_utils.h" 25 | 26 | // NOTE: Consider replacing some of the uint8_t arrays with pointers. 27 | 28 | /*! 29 | * Pairing request payload number 1. 30 | * 31 | * This is the initial paring request that is sent to a Unifying receiver. 32 | */ 33 | struct unifying_pair_request_1 34 | { 35 | uint8_t id; 36 | uint8_t frame; 37 | uint8_t step; 38 | uint8_t unknown_3_7[5]; // Previously paired RF address or random data. 39 | uint8_t timeout; // Observed as 0x08. 40 | uint16_t product_id; 41 | uint8_t protocol; 42 | uint8_t unknown_12; 43 | uint16_t device_type; 44 | uint8_t unknown_15_19[5]; 45 | uint8_t unknown_20; // Observed as non-zero. 46 | uint8_t checksum; 47 | }; 48 | 49 | /*! 50 | * Pairing response payload number 1. 51 | * 52 | * This is the expected response to \ref unifying_pair_request_1. 53 | * 54 | * \see unifying_pair_request_1 55 | * \see unifying_keep_alive_request 56 | */ 57 | struct unifying_pair_response_1 58 | { 59 | uint8_t id; 60 | uint8_t frame; 61 | uint8_t step; 62 | uint8_t address[UNIFYING_ADDRESS_LEN]; 63 | uint8_t unknown_8; // Observed as 0x08. 64 | uint16_t product_id; 65 | uint8_t unknown_11_12[2]; 66 | uint16_t device_type; 67 | uint8_t unknown_15_20[6]; 68 | uint8_t checksum; 69 | }; 70 | 71 | /*! 72 | * Pairing request payload number 2. 73 | * 74 | * This paring request is sent after receiving a \ref unifying_pair_response_1 payload. 75 | * 76 | * \see unifying_pair_response_1 77 | */ 78 | struct unifying_pair_request_2 79 | { 80 | uint8_t unknown_0; 81 | uint8_t frame; 82 | uint8_t step; 83 | uint32_t crypto; 84 | uint32_t serial; 85 | uint16_t capabilities; 86 | uint8_t unknown_13_20[8]; 87 | uint8_t checksum; 88 | }; 89 | 90 | /*! 91 | * Pairing response payload number 2. 92 | * 93 | * This is the expected response to \ref unifying_pair_request_2. 94 | * 95 | * \see unifying_pair_request_2 96 | * \see unifying_keep_alive_request 97 | */ 98 | struct unifying_pair_response_2 99 | { 100 | uint8_t unknown_0; 101 | uint8_t frame; 102 | uint8_t step; 103 | uint32_t crypto; 104 | uint32_t serial; 105 | uint16_t capabilities; 106 | uint8_t unknown_13_20[8]; 107 | uint8_t checksum; 108 | }; 109 | 110 | /*! 111 | * Pairing request payload number 3. 112 | * 113 | * This paring request is sent after receiving a \ref unifying_pair_response_2 payload. 114 | * 115 | * * \see unifying_pair_response_2 116 | */ 117 | struct unifying_pair_request_3 118 | { 119 | uint8_t unknown_0; 120 | uint8_t frame; 121 | uint8_t step; 122 | // TODO: Test if this indicate the number of name packets. 123 | // Perhaps this would allow names to be longer than 16 bytes. 124 | uint8_t unknown_3; // Observed as 0x01. 125 | uint8_t name_length; // Name length does not include a null terminator. 126 | char name[UNIFYING_MAX_NAME_LEN]; // Name does not need to include a null terminator. 127 | uint8_t checksum; 128 | }; 129 | 130 | /*! 131 | * Pairing response payload number 3. 132 | * 133 | * This is the expected response to \ref unifying_pair_request_3. 134 | * 135 | * \see unifying_pair_request_2 136 | * \see unifying_keep_alive_request 137 | */ 138 | struct unifying_pair_response_3 139 | { 140 | uint8_t unknown_0; 141 | uint8_t frame; 142 | uint8_t step; 143 | uint8_t unknown_3_8[6]; // Observed to contain crypto data from request/response 2. 144 | uint8_t checksum; 145 | }; 146 | 147 | /*! 148 | * Pairing complete request payload. 149 | * 150 | * This paring request is sent after receiving a \ref unifying_pair_response_3 payload. 151 | * No response payload is expected after this. 152 | * 153 | * \see unifying_pair_response_3 154 | */ 155 | struct unifying_pair_complete_request 156 | { 157 | uint8_t unknown_0; 158 | uint8_t frame; 159 | uint8_t step; 160 | uint8_t unknown_3; // Observed as 0x01. 161 | uint8_t unknown_4_8[5]; 162 | uint8_t checksum; 163 | }; 164 | 165 | /*! 166 | * Long wake-up request payload. 167 | * 168 | * This is used to re-connect a device to a paired receiver 169 | * after being powered off or going to sleep. 170 | * \see unifying_long_wake_up_request 171 | */ 172 | struct unifying_long_wake_up_request 173 | { 174 | uint8_t index; 175 | uint8_t frame; 176 | uint8_t index_2; 177 | uint8_t unknown_3; 178 | uint8_t unknown_4; // Observed as 0x00. 179 | uint8_t unknown_5_7[3]; // All observed as 0x01. 180 | uint8_t unknown_8_20[13]; 181 | uint8_t checksum; 182 | }; 183 | 184 | /*! 185 | * Short wake-up request payload. 186 | * 187 | * This is used to re-connect a device to a paired receiver 188 | * after being powered off or going to sleep. 189 | * 190 | * \see unifying_long_wake_up_request 191 | */ 192 | struct unifying_short_wake_up_request 193 | { 194 | uint8_t index; 195 | uint8_t frame; 196 | uint8_t unknown_2; // Observed as 0x01. 197 | uint8_t unknown_3; // Observed as 0x4B. 198 | uint8_t unknown_4; // Observed as 0x01. 199 | uint8_t unknown_5_8[4]; 200 | uint8_t checksum; 201 | }; 202 | 203 | /*! 204 | * Set timeout request payload. 205 | * 206 | * Used to inform the paired receiver how often to expect keep-alive payloads. 207 | * Some other payloads implicitly set the timeout to a default value. 208 | * 209 | * \see unifying_keep_alive_request 210 | */ 211 | struct unifying_set_timeout_request 212 | { 213 | uint8_t unknown_0; 214 | uint8_t frame; 215 | uint8_t unknown_2; 216 | uint16_t timeout; 217 | uint8_t unknown_5_8[4]; 218 | uint8_t checksum; 219 | }; 220 | 221 | /*! 222 | * Keep-alive request payload. 223 | * 224 | * Used to inform the paired receiver that this device is still active. 225 | * Various response payloads will sometimes be received after transmitting a keep-alive. 226 | * 227 | * \see unifying_pair_response_1 228 | * \see unifying_pair_response_2 229 | * \see unifying_pair_response_3 230 | * \see unifying_hidpp_1_0_short 231 | * \see unifying_hidpp_1_0_long 232 | */ 233 | struct unifying_keep_alive_request 234 | { 235 | uint8_t unknown_0; 236 | uint8_t frame; 237 | uint16_t timeout; 238 | uint8_t checksum; 239 | }; 240 | 241 | /*! 242 | * Short HID++ 1.0 payload. 243 | * 244 | * This is used for HID++ 1.0 requests and responses. 245 | * 246 | * \see unifying_keep_alive_request 247 | * \see unifying_hidpp_1_0_long 248 | */ 249 | struct unifying_hidpp_1_0_short 250 | { 251 | uint8_t unknown_0; 252 | uint8_t report; 253 | uint8_t index; 254 | uint8_t sub_id; 255 | uint8_t params[UNIFYING_HIDPP_1_0_SHORT_PARAMS_LEN]; 256 | uint8_t unknown_8; 257 | uint8_t checksum; 258 | }; 259 | 260 | /*! 261 | * Long HID++ 1.0 payload. 262 | * 263 | * This is used for HID++ 1.0 requests and responses. 264 | * 265 | * \see unifying_keep_alive_request 266 | * \see unifying_hidpp_1_0_short 267 | */ 268 | struct unifying_hidpp_1_0_long 269 | { 270 | uint8_t unknown_0; 271 | uint8_t report; 272 | uint8_t index; 273 | uint8_t sub_id; 274 | uint8_t params[UNIFYING_HIDPP_1_0_LONG_PARAMS_LEN]; 275 | uint8_t checksum; 276 | }; 277 | 278 | /*! 279 | * Encrypted keystroke request payload. 280 | * 281 | * Used to transmit keyboard scancodes to a Unifying receiver. 282 | */ 283 | struct unifying_encrypted_keystroke_request 284 | { 285 | uint8_t unknown_0; 286 | uint8_t frame; 287 | uint8_t ciphertext[UNIFYING_AES_DATA_LEN]; 288 | uint32_t counter; 289 | uint8_t unknown_14_20[7]; 290 | uint8_t checksum; 291 | }; 292 | 293 | /*! 294 | * Multimedia keystroke request payload. 295 | */ 296 | struct unifying_multimeia_keystroke_request 297 | { 298 | uint8_t unknown_0; 299 | uint8_t frame; 300 | uint8_t keys[UNIFYING_MULTIMEDIA_KEYS_LEN]; 301 | uint8_t unknown_6_8[3]; 302 | uint8_t checksum; 303 | }; 304 | 305 | /*! 306 | * Mouse request payload. 307 | * 308 | * Used to transmit mouse movement, clicking, and scrolling to a Unifying receiver. 309 | */ 310 | struct unifying_mouse_request 311 | { 312 | uint8_t unknown_0; 313 | uint8_t frame; 314 | uint8_t buttons; 315 | uint8_t unknown_3; 316 | int16_t move_y; 317 | int16_t move_x; 318 | int8_t wheel_y; 319 | int8_t wheel_x; 320 | uint8_t checksum; 321 | }; 322 | 323 | #ifdef __cplusplus 324 | extern "C" { 325 | #endif 326 | 327 | /*! 328 | * Initialize a \ref unifying_pair_request_1 structure. 329 | * 330 | * \param[out] unpacked Pointer to a \ref unifying_pair_request_1 to initialize. 331 | * \param[in] id Random value used for verifying the early stage of the pairing process. 332 | * \param[in] timeout Default device timeout. 333 | * \param[in] product_id The product ID of this device. 334 | * \param[in] device_type Values indicating the device type. 335 | * 336 | * \see unifying_pair_request_1 337 | * \see UNIFYING_DEFAULT_TIMEOUT_KEYBOARD 338 | * \see UNIFYING_DEFAULT_TIMEOUT_MOUSE 339 | */ 340 | void unifying_pair_request_1_init(struct unifying_pair_request_1* unpacked, 341 | uint8_t id, 342 | uint16_t timeout, 343 | uint16_t product_id, 344 | uint16_t device_type); 345 | 346 | /*! 347 | * Pack a \ref unifying_pair_request_1 into a byte array. 348 | * 349 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_PAIR_REQUEST_1_LEN bytes long. 350 | * \param[in] unpacked Pointer to a \ref unifying_pair_request_1 to pack. 351 | */ 352 | void unifying_pair_request_1_pack(uint8_t packed[UNIFYING_PAIR_REQUEST_1_LEN], 353 | const struct unifying_pair_request_1* unpacked); 354 | 355 | /*! 356 | * Unpack a byte array into a \ref unifying_pair_response_1. 357 | * 358 | * \param[out] unpacked Pointer to a \ref unifying_pair_response_1 to unpack into. 359 | * \param[in] packed Pointer to byte array that is at least \ref UNIFYING_PAIR_RESPONSE_1_LEN bytes long. 360 | */ 361 | void unifying_pair_response_1_unpack(struct unifying_pair_response_1* unpacked, 362 | const uint8_t packed[UNIFYING_PAIR_RESPONSE_1_LEN]); 363 | 364 | /*! 365 | * Initialize a \ref unifying_pair_request_2 structure. 366 | * 367 | * \param[out] unpacked Pointer to a \ref unifying_pair_request_2 to initialize. 368 | * \param[in] crypto Cryptographically secure random number. 369 | * \param[in] serial Serial number of this device. 370 | * \param[in] capabilities HID++ capabilities. 371 | * 372 | * \see unifying_pair_request_2 373 | */ 374 | void unifying_pair_request_2_init(struct unifying_pair_request_2* unpacked, 375 | uint32_t crypto, 376 | uint32_t serial, 377 | uint16_t capabilities); 378 | 379 | /*! 380 | * Pack a \ref unifying_pair_request_2 into a byte array. 381 | * 382 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_PAIR_RESPONSE_2_LEN bytes long. 383 | * \param[in] unpacked Pointer to a \ref unifying_pair_request_2 to pack. 384 | */ 385 | void unifying_pair_request_2_pack(uint8_t packed[UNIFYING_PAIR_REQUEST_2_LEN], 386 | const struct unifying_pair_request_2* unpacked); 387 | 388 | /*! 389 | * Unpack a byte array into a \ref unifying_pair_response_2. 390 | * 391 | * \param[out] unpacked Pointer to a \ref unifying_pair_response_2 to unpack into. 392 | * \param[in] packed Pointer to byte array that is at least \ref UNIFYING_PAIR_RESPONSE_2_LEN bytes long. 393 | */ 394 | void unifying_pair_response_2_unpack(struct unifying_pair_response_2* unpacked, 395 | const uint8_t packed[UNIFYING_PAIR_RESPONSE_2_LEN]); 396 | 397 | /*! 398 | * Initialize a \ref unifying_pair_request_3 structure. 399 | * 400 | * \param[out] unpacked Pointer to a \ref unifying_pair_request_3 to initialize. 401 | * \param[in] name Name of your device. 402 | * \param[in] name_length Length of \p name. 403 | * 404 | * \see unifying_pair_request_3 405 | */ 406 | void unifying_pair_request_3_init(struct unifying_pair_request_3* unpacked, const char* name, uint8_t name_length); 407 | 408 | /*! 409 | * Pack a \ref unifying_pair_request_3 into a byte array. 410 | * 411 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_PAIR_REQUEST_3_LEN bytes long. 412 | * \param[in] unpacked Pointer to a \ref unifying_pair_request_3 to pack. 413 | */ 414 | void unifying_pair_request_3_pack(uint8_t packed[UNIFYING_PAIR_REQUEST_3_LEN], 415 | const struct unifying_pair_request_3* unpacked); 416 | 417 | /*! 418 | * Unpack a byte array into a \ref unifying_pair_response_3. 419 | * 420 | * \param[out] unpacked Pointer to a \ref unifying_pair_response_3 to unpack into. 421 | * \param[in] packed Pointer to byte array that is at least \ref UNIFYING_PAIR_RESPONSE_3_LEN bytes long. 422 | */ 423 | void unifying_pair_response_3_unpack(struct unifying_pair_response_3* unpacked, 424 | const uint8_t packed[UNIFYING_PAIR_RESPONSE_3_LEN]); 425 | 426 | /*! 427 | * Initialize a \ref unifying_pair_complete_request structure. 428 | * 429 | * \param[out] unpacked Pointer to a \ref unifying_pair_complete_request to initialize. 430 | * 431 | * \see unifying_pair_complete_request 432 | */ 433 | void unifying_pair_complete_request_init(struct unifying_pair_complete_request* unpacked); 434 | 435 | /*! 436 | * Pack a \ref unifying_pair_complete_request into a byte array. 437 | * 438 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_PAIR_COMPLETE_REQUEST_LEN bytes long. 439 | * \param[in] unpacked Pointer to a \ref unifying_pair_complete_request to pack. 440 | */ 441 | void unifying_pair_complete_request_pack(uint8_t packed[UNIFYING_PAIR_COMPLETE_REQUEST_LEN], 442 | const struct unifying_pair_complete_request* unpacked); 443 | 444 | /*! 445 | * Initialize a \ref unifying_long_wake_up_request structure. 446 | * 447 | * \param[out] unpacked Pointer to a \ref unifying_long_wake_up_request to initialize. 448 | * \param[in] index Least significant byte of the RF address for this device. 449 | * 450 | * \see unifying_long_wake_up_request 451 | */ 452 | void unifying_long_wake_up_request_init(struct unifying_long_wake_up_request* unpacked, uint8_t index); 453 | 454 | /*! 455 | * Pack a \ref unifying_long_wake_up_request into a byte array. 456 | * 457 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_LONG_WAKE_UP_REQUEST_LEN bytes long. 458 | * \param[in] unpacked Pointer to a \ref unifying_long_wake_up_request to pack. 459 | */ 460 | void unifying_long_wake_up_request_pack(uint8_t packed[UNIFYING_LONG_WAKE_UP_REQUEST_LEN], 461 | const struct unifying_long_wake_up_request* unpacked); 462 | 463 | /*! 464 | * Initialize a \ref unifying_short_wake_up_request structure. 465 | * 466 | * \param[out] unpacked Pointer to a \ref unifying_short_wake_up_request to initialize. 467 | * \param[in] index Least significant byte of the RF address for this device. 468 | * 469 | * \see unifying_short_wake_up_request 470 | */ 471 | void unifying_short_wake_up_request_init(struct unifying_short_wake_up_request* unpacked, uint8_t index); 472 | 473 | /*! 474 | * Pack a \ref unifying_short_wake_up_request into a byte array. 475 | * 476 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_SHORT_WAKE_UP_REQUEST_LEN bytes long. 477 | * \param[in] unpacked Pointer to a \ref unifying_short_wake_up_request to pack. 478 | */ 479 | void unifying_short_wake_up_request_pack(uint8_t packed[UNIFYING_SHORT_WAKE_UP_REQUEST_LEN], 480 | const struct unifying_short_wake_up_request* unpacked); 481 | 482 | /*! 483 | * Initialize a \ref unifying_set_timeout_request structure. 484 | * 485 | * \param[out] unpacked Pointer to a \ref unifying_set_timeout_request to initialize. 486 | * \param[in] timeout Timeout for keep-alive packets. 487 | * 488 | * \see unifying_set_timeout_request 489 | */ 490 | void unifying_set_timeout_request_init(struct unifying_set_timeout_request* unpacked, uint16_t timeout); 491 | 492 | /*! 493 | * Pack a \ref unifying_set_timeout_request into a byte array. 494 | * 495 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_SET_TIMEOUT_REQUEST_LEN bytes long. 496 | * \param[in] unpacked Pointer to a \ref unifying_set_timeout_request to pack. 497 | */ 498 | void unifying_set_timeout_request_pack(uint8_t packed[UNIFYING_SET_TIMEOUT_REQUEST_LEN], 499 | const struct unifying_set_timeout_request* unpacked); 500 | 501 | /*! 502 | * Initialize a \ref unifying_keep_alive_request structure. 503 | * 504 | * \param[out] unpacked Pointer to a \ref unifying_keep_alive_request to initialize. 505 | * \param[in] timeout Timeout for keep-alive packets. 506 | * 507 | * \see unifying_keep_alive_request 508 | */ 509 | void unifying_keep_alive_request_init(struct unifying_keep_alive_request* unpacked, uint16_t timeout); 510 | 511 | /*! 512 | * Pack a \ref unifying_keep_alive_request into a byte array. 513 | * 514 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_KEEP_ALIVE_REQUEST_LEN bytes long. 515 | * \param[in] unpacked Pointer to a \ref unifying_keep_alive_request to pack. 516 | */ 517 | void unifying_keep_alive_request_pack(uint8_t packed[UNIFYING_KEEP_ALIVE_REQUEST_LEN], 518 | const struct unifying_keep_alive_request* unpacked); 519 | 520 | /*! 521 | * Initialize a \ref unifying_hidpp_1_0_short structure. 522 | * 523 | * \param[out] unpacked Pointer to a \ref unifying_hidpp_1_0_short to initialize. 524 | * \param[in] index Least significant byte of the RF address for this device. 525 | * Or 0xFF if the payload originates from a receiver. 526 | * \param[in] sub_id HID++ 1.0 SubID. 527 | * \param[in] params Array of at least \ref UNIFYING_HIDPP_1_0_SHORT_PARAMS_LEN bytes 528 | * containing HID++ parameters. 529 | * 530 | * \see unifying_hidpp_1_0_short 531 | */ 532 | void unifying_hidpp_1_0_short_init(struct unifying_hidpp_1_0_short* unpacked, 533 | uint8_t index, 534 | uint8_t sub_id, 535 | uint8_t params[UNIFYING_HIDPP_1_0_SHORT_PARAMS_LEN]); 536 | 537 | /*! 538 | * Pack a \ref unifying_hidpp_1_0_short into a byte array. 539 | * 540 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_HIDPP_1_0_SHORT_LEN bytes long. 541 | * \param[in] unpacked Pointer to a \ref unifying_hidpp_1_0_short to pack. 542 | */ 543 | void unifying_hidpp_1_0_short_pack(uint8_t packed[UNIFYING_HIDPP_1_0_SHORT_LEN], 544 | const struct unifying_hidpp_1_0_short* unpacked); 545 | 546 | /*! 547 | * Unpack a byte array into a \ref unifying_hidpp_1_0_short. 548 | * 549 | * \param[out] unpacked Pointer to a \ref unifying_hidpp_1_0_short to unpack into. 550 | * \param[in] packed Pointer to byte array that is at least \ref UNIFYING_HIDPP_1_0_SHORT_LEN bytes long. 551 | */ 552 | void unifying_hidpp_1_0_short_unpack(struct unifying_hidpp_1_0_short* unpacked, 553 | const uint8_t packed[UNIFYING_HIDPP_1_0_SHORT_LEN]); 554 | 555 | /*! 556 | * Initialize a \ref unifying_hidpp_1_0_long structure. 557 | * 558 | * \param[out] unpacked Pointer to a \ref unifying_hidpp_1_0_long to initialize. 559 | * \param[in] index Least significant byte of the RF address for this device. 560 | * Or 0xFF if the payload originates from a receiver. 561 | * \param[in] sub_id HID++ 1.0 SubID. 562 | * \param[in] params Array of at least \ref UNIFYING_HIDPP_1_0_LONG_PARAMS_LEN bytes 563 | * containing HID++ parameters. 564 | * 565 | * \see unifying_hidpp_1_0_long 566 | */ 567 | void unifying_hidpp_1_0_long_init(struct unifying_hidpp_1_0_long* unpacked, 568 | uint8_t index, 569 | uint8_t sub_id, 570 | uint8_t params[UNIFYING_HIDPP_1_0_LONG_PARAMS_LEN]); 571 | 572 | /*! 573 | * Pack a \ref unifying_hidpp_1_0_long into a byte array. 574 | * 575 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_HIDPP_1_0_LONG_LEN bytes long. 576 | * \param[in] unpacked Pointer to a \ref unifying_hidpp_1_0_long to pack. 577 | */ 578 | void unifying_hidpp_1_0_long_pack(uint8_t packed[UNIFYING_HIDPP_1_0_LONG_LEN], 579 | const struct unifying_hidpp_1_0_long* unpacked); 580 | 581 | /*! 582 | * Unpack a byte array into a \ref unifying_hidpp_1_0_long. 583 | * 584 | * \param[out] unpacked Pointer to a \ref unifying_hidpp_1_0_long to unpack into. 585 | * \param[in] packed Pointer to byte array that is at least \ref UNIFYING_HIDPP_1_0_LONG_LEN bytes long. 586 | */ 587 | void unifying_hidpp_1_0_long_unpack(struct unifying_hidpp_1_0_long* unpacked, 588 | const uint8_t packed[UNIFYING_HIDPP_1_0_LONG_LEN]); 589 | 590 | /*! 591 | * Initialize a \ref unifying_encrypted_keystroke_request structure. 592 | * 593 | * \param[out] unpacked Pointer to a \ref unifying_encrypted_keystroke_request to initialize. 594 | * \param[in] ciphertext Array of at least \ref UNIFYING_AES_DATA_LEN bytes of encrypted keystroke data. 595 | * \param[in] counter AES counter. 596 | * 597 | * \see unifying_encrypted_keystroke_request 598 | */ 599 | void unifying_encrypted_keystroke_request_init(struct unifying_encrypted_keystroke_request* unpacked, 600 | uint8_t ciphertext[UNIFYING_AES_DATA_LEN], 601 | uint32_t counter); 602 | 603 | /*! 604 | * Pack a \ref unifying_encrypted_keystroke_request into a byte array. 605 | * 606 | * \param[out] packed Pointer to byte array that is at least 607 | * \ref UNIFYING_ENCRYPTED_KEYSTROKE_REQUEST_LEN bytes long. 608 | * \param[in] unpacked Pointer to a \ref unifying_encrypted_keystroke_request to pack. 609 | */ 610 | void unifying_encrypted_keystroke_request_pack(uint8_t packed[UNIFYING_ENCRYPTED_KEYSTROKE_REQUEST_LEN], 611 | const struct unifying_encrypted_keystroke_request* unpacked); 612 | 613 | /*! 614 | * Initialize a \ref unifying_multimeia_keystroke_request structure. 615 | * 616 | * \param[out] unpacked Pointer to a \ref unifying_multimeia_keystroke_request to initialize. 617 | * \param[in] keys Array of at least \ref UNIFYING_AES_DATA_LEN bytes of multimedia keystroke data. 618 | * 619 | * \see unifying_multimeia_keystroke_request 620 | */ 621 | void unifying_multimeia_keystroke_request_init(struct unifying_multimeia_keystroke_request* unpacked, 622 | uint8_t keys[UNIFYING_MULTIMEDIA_KEYS_LEN]); 623 | 624 | /*! 625 | * Pack a \ref unifying_multimeia_keystroke_request into a byte array. 626 | * 627 | * \param[out] packed Pointer to byte array that is at least 628 | * \ref UNIFYING_MULTIMEDIA_KEYSTROKE_REQUEST_LEN bytes long. 629 | * \param[in] unpacked Pointer to a \ref unifying_multimeia_keystroke_request to pack. 630 | */ 631 | void unifying_multimeia_keystroke_request_pack(uint8_t packed[UNIFYING_MULTIMEDIA_KEYSTROKE_REQUEST_LEN], 632 | const struct unifying_multimeia_keystroke_request* unpacked); 633 | 634 | /*! 635 | * Initialize a \ref unifying_mouse_request structure. 636 | * 637 | * \param[out] unpacked Pointer to a \ref unifying_mouse_request to initialize. 638 | * \param[in] buttons Bitfield where each bit corresponds to a mouse button. 639 | * \param[in] move_y Y axis mouse movement. 640 | * \param[in] move_x X axis mouse movement. 641 | * \param[in] wheel_y Y axis scroll wheel movement. 642 | * \param[in] wheel_x X axis scroll wheel movement. 643 | * 644 | * \note X and Y movement is packed as a pair of big-endian signed 12-bit integers. 645 | * The X and Y movement data is expected to have already been clamped to a signed 12-bit range 646 | * with unifying_clamp_int12() prior to calling this function. 647 | * 648 | * \see unifying_mouse_request 649 | */ 650 | void unifying_mouse_request_init(struct unifying_mouse_request* unpacked, 651 | uint8_t buttons, 652 | int16_t move_y, 653 | int16_t move_x, 654 | int8_t wheel_y, 655 | int8_t wheel_x); 656 | 657 | /*! 658 | * Pack a \ref unifying_mouse_request into a byte array. 659 | * 660 | * \param[out] packed Pointer to byte array that is at least \ref UNIFYING_MOUSE_REQUEST_LEN bytes long. 661 | * \param[in] unpacked Pointer to a \ref unifying_mouse_request to pack. 662 | */ 663 | void unifying_mouse_request_pack(uint8_t packed[UNIFYING_MOUSE_REQUEST_LEN], 664 | const struct unifying_mouse_request* unpacked); 665 | 666 | 667 | #ifdef __cplusplus 668 | } 669 | #endif 670 | 671 | #endif 672 | -------------------------------------------------------------------------------- /src/unifying.c: -------------------------------------------------------------------------------- 1 | 2 | #include "unifying.h" 3 | 4 | /*! 5 | * Immediately transmit a payload. 6 | * 7 | * If transmission fails then a new RF channel will be selected and the timeout will not be updated. 8 | * 9 | * \param[in,out] state Unifying state information. 10 | * \param[out] payload Pointer to a payload to transmit. 11 | * \param[in] length Length of the payload to transmit. 12 | * \param[in] timeout New timeout for keep alive packets. 13 | * Specifying \ref UNIFYING_TIMEOUT_UNCHANGED will leave the timeout unchanged. 14 | * 15 | * \return \ref UNIFYING_TRANSMIT_ERROR if transmission failed. 16 | * \return \ref UNIFYING_SUCCESS otherwise. 17 | */ 18 | static enum unifying_error unifying_transmit(struct unifying_state* state, 19 | uint8_t* payload, 20 | uint8_t length, 21 | uint16_t timeout) 22 | { 23 | 24 | uint8_t err = state->interface->transmit_payload(payload, length); 25 | 26 | if(err) 27 | { 28 | // Transmission failed. 29 | // Switch to a new channel. 30 | unifying_state_channel_set(state, unifying_next_channel(state->channel)); 31 | return UNIFYING_TRANSMIT_ERROR; 32 | } 33 | 34 | // Transmission succeeded. 35 | // Adjust the timeout and determine when the next packet should be sent. 36 | if(timeout) 37 | { 38 | state->timeout = timeout; 39 | } 40 | 41 | state->previous_transmit = state->interface->time(); 42 | state->next_transmit = state->previous_transmit + state->timeout * UNIFYING_TIMEOUT_COEFFICIENT; 43 | 44 | return UNIFYING_SUCCESS; 45 | } 46 | 47 | /*! 48 | * Queue a received payload in a buffer. 49 | * 50 | * \param[in,out] state Unifying state information. 51 | * 52 | * \return \ref UNIFYING_RECEIVE_ERROR if no payload is available. 53 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the receive buffer is full. 54 | * \return \ref UNIFYING_CREATE_ERROR if dynamic memory allocation fails. 55 | * \return \ref UNIFYING_PAYLOAD_LENGTH_ERROR if the payload's length differs from its expected length. 56 | * This should never happen. 57 | * \return \ref UNIFYING_SUCCESS otherwise. 58 | */ 59 | static enum unifying_error unifying_receive(struct unifying_state* state) 60 | { 61 | // Check if we have received an ACK payload. 62 | if(!state->interface->payload_available()) { 63 | return UNIFYING_RECEIVE_ERROR; 64 | } 65 | 66 | if(unifying_ring_buffer_full(state->receive_buffer)) 67 | { 68 | // We don't have room to store the payload. 69 | // Maybe we will later. 70 | // The payload can just hang out in the radio's RX FIFO in the meantime. 71 | return UNIFYING_BUFFER_FULL_ERROR; 72 | } 73 | 74 | uint8_t length = state->interface->payload_size(); 75 | struct unifying_receive_entry* receive_entry; 76 | receive_entry = unifying_receive_entry_create(length); 77 | 78 | if(!receive_entry) 79 | { 80 | // Memory allocation failed. 81 | return UNIFYING_CREATE_ERROR; 82 | } 83 | 84 | // Buffer the received payload for now. 85 | // It will be handled later. 86 | length = state->interface->receive_payload(receive_entry->payload, receive_entry->length); 87 | 88 | if(length != receive_entry->length) 89 | { 90 | // Somehow we received a payload of a different size than was stated earlier. 91 | // This should never happen. 92 | unifying_receive_entry_destroy(receive_entry); 93 | return UNIFYING_PAYLOAD_LENGTH_ERROR; 94 | } 95 | 96 | if(unifying_ring_buffer_push_back(state->receive_buffer, receive_entry)) 97 | { 98 | // The buffer didn't have enough space even though we checked it earlier. 99 | // This should never happen. 100 | unifying_receive_entry_destroy(receive_entry); 101 | return UNIFYING_BUFFER_FULL_ERROR; 102 | } 103 | 104 | return UNIFYING_SUCCESS; 105 | } 106 | 107 | /*! 108 | * Dequeue a received payload and perform basic verification. 109 | * 110 | * \param[in,out] state Unifying state information. 111 | * \param[out] receive_entry Pointer to an entry pointer. Used to return the dequeued payload. 112 | * The pointer is only guaranteed to be valid 113 | * if this function returns \ref UNIFYING_SUCCESS. 114 | * \param[in] length Expected length of the received payload. 115 | * Supplying 0 as the payload length will skip the length check. 116 | * 117 | * \return \ref UNIFYING_BUFFER_EMPTY_ERROR if \ref unifying_state.receive_buffer "state.receive_buffer" 118 | * is empty. 119 | * \return \ref UNIFYING_CHECKSUM_ERROR if the received payload's computed checksum 120 | * does not match its stated checksum. 121 | * \return \ref UNIFYING_PAYLOAD_LENGTH_ERROR if the payload's length differs from \p length 122 | * \return \ref UNIFYING_SUCCESS otherwise. 123 | */ 124 | static enum unifying_error unifying_response(struct unifying_state* state, 125 | struct unifying_receive_entry** receive_entry, 126 | uint8_t length) 127 | { 128 | *receive_entry = unifying_ring_buffer_pop_front(state->receive_buffer); 129 | 130 | if(!(*receive_entry)) 131 | { 132 | return UNIFYING_BUFFER_EMPTY_ERROR; 133 | } 134 | 135 | if(unifying_checksum_verify((*receive_entry)->payload, (*receive_entry)->length)) 136 | { 137 | unifying_receive_entry_destroy(*receive_entry); 138 | return UNIFYING_CHECKSUM_ERROR; 139 | } 140 | 141 | if(length && (*receive_entry)->length != length) 142 | { 143 | unifying_receive_entry_destroy(*receive_entry); 144 | return UNIFYING_PAYLOAD_LENGTH_ERROR; 145 | } 146 | 147 | return UNIFYING_SUCCESS; 148 | } 149 | 150 | /*! 151 | * Dequeue a response payload and queue a HID++ payload for transmission. 152 | * 153 | * \todo Refactor this code to be more readable 154 | * 155 | * \todo Implement proper HID++ responses instead of responding to everything with errors. 156 | * - https://drive.google.com/drive/folders/0BxbRzx7vEV7eWmgwazJ3NUFfQ28?resourcekey=0-dQ-Lx1FORQl0KAdOHQaE1A 157 | * - https://docs.google.com/document/d/0BxbRzx7vEV7eNDBheWY0UHM5dEU/edit?resourcekey=0-SPDGsNiO52FX6E-mJIXYXQ#! 158 | * - https://drive.google.com/file/d/0BxbRzx7vEV7eU3VfMnRuRXktZ3M/view?resourcekey=0-06JzoS5yy_4Asod95f4Ecw 159 | * 160 | * \param[in,out] state Unifying state information. 161 | * 162 | * \return \ref UNIFYING_PAYLOAD_LENGTH_ERROR if the received payload is too short. 163 | * \return \ref UNIFYING_CREATE_ERROR if dynamic memory allocation fails. 164 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the transmit buffer is full. 165 | * \return \ref UNIFYING_SUCCESS otherwise. 166 | */ 167 | static enum unifying_error unifying_hidpp_1_0(struct unifying_state* state) 168 | { 169 | enum unifying_error err; 170 | struct unifying_receive_entry* receive_entry; 171 | struct unifying_transmit_entry* transmit_entry; 172 | struct unifying_hidpp_1_0_short hidpp_1_0_short; 173 | 174 | err = unifying_response(state, &receive_entry, 0); 175 | 176 | if(err) 177 | { 178 | return err; 179 | } 180 | 181 | if(receive_entry->length < 4) 182 | { 183 | unifying_receive_entry_destroy(receive_entry); 184 | return UNIFYING_PAYLOAD_LENGTH_ERROR; 185 | } 186 | 187 | transmit_entry = unifying_transmit_entry_create(UNIFYING_HIDPP_1_0_SHORT_LEN, state->default_timeout); 188 | 189 | if(!transmit_entry) 190 | { 191 | unifying_receive_entry_destroy(receive_entry); 192 | return UNIFYING_CREATE_ERROR; 193 | } 194 | 195 | receive_entry->payload[5] = UNIFYING_HIDPP_1_0_ERROR_INVALID_SUBID; 196 | receive_entry->payload[6] = 0x00; 197 | unifying_hidpp_1_0_short_init(&hidpp_1_0_short, 198 | receive_entry->payload[2], // index 199 | UNIFYING_HIDPP_1_0_SUB_ID_ERROR_MSG, 200 | &receive_entry->payload[3]); 201 | hidpp_1_0_short.report = 0x50; 202 | unifying_receive_entry_destroy(receive_entry); 203 | unifying_hidpp_1_0_short_pack(transmit_entry->payload, &hidpp_1_0_short); 204 | 205 | err = unifying_ring_buffer_push_back(state->transmit_buffer, transmit_entry); 206 | 207 | if(err) 208 | { 209 | unifying_transmit_entry_destroy(transmit_entry); 210 | } 211 | 212 | return err; 213 | } 214 | 215 | /*! 216 | * Queue a payload for step 1 of the pairing process. 217 | * 218 | * \param[in,out] state Unifying state information. 219 | * \param[in] id Random value used for verifying the early stage of the pairing process. 220 | * \param[in] product_id Product ID of your device. 221 | * This value becomes part of the device's AES encryption key. 222 | * For added security, this should be a cryptographically secure random number. 223 | * \param[in] device_type Values indicating the device type. 224 | * Valid values and their meaning are not yet documented. 225 | * 226 | * \return \ref UNIFYING_CREATE_ERROR if dynamic memory allocation fails. 227 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the transmit buffer is full. 228 | * \return \ref UNIFYING_SUCCESS otherwise. 229 | */ 230 | static enum unifying_error unifying_pair_step_1(struct unifying_state* state, 231 | uint8_t id, 232 | uint16_t product_id, 233 | uint16_t device_type) 234 | { 235 | enum unifying_error err; 236 | struct unifying_transmit_entry* transmit_entry; 237 | struct unifying_pair_request_1 pair_request; 238 | 239 | transmit_entry = unifying_transmit_entry_create(UNIFYING_PAIR_REQUEST_1_LEN, state->default_timeout); 240 | 241 | if(!transmit_entry) 242 | { 243 | return UNIFYING_CREATE_ERROR; 244 | } 245 | 246 | unifying_pair_request_1_init(&pair_request, id, state->timeout, product_id, device_type); 247 | unifying_pair_request_1_pack(transmit_entry->payload, &pair_request); 248 | 249 | err = unifying_ring_buffer_push_back(state->transmit_buffer, transmit_entry); 250 | 251 | if(err) 252 | { 253 | unifying_transmit_entry_destroy(transmit_entry); 254 | } 255 | 256 | return err; 257 | } 258 | 259 | /*! 260 | * Queue a payload for step 2 of the pairing process. 261 | * 262 | * \param[in,out] state Unifying state information. 263 | * \param[in] crypto A cryptographically secure random number, used for AES encryption key generation. 264 | * \param[in] serial Serial number of your device. The exact value does not matter. 265 | * \param[in] capabilities HID++ capabilities. 266 | * 267 | * \return \ref UNIFYING_CREATE_ERROR if dynamic memory allocation fails. 268 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the transmit buffer is full. 269 | * \return \ref UNIFYING_SUCCESS otherwise. 270 | */ 271 | static enum unifying_error unifying_pair_step_2(struct unifying_state* state, 272 | uint32_t crypto, 273 | uint32_t serial, 274 | uint16_t capabilities) 275 | { 276 | enum unifying_error err; 277 | struct unifying_transmit_entry* transmit_entry; 278 | struct unifying_pair_request_2 pair_request; 279 | 280 | transmit_entry = unifying_transmit_entry_create(UNIFYING_PAIR_REQUEST_2_LEN, state->default_timeout); 281 | 282 | if(!transmit_entry) 283 | { 284 | return UNIFYING_CREATE_ERROR; 285 | } 286 | 287 | unifying_pair_request_2_init(&pair_request, crypto, serial, capabilities); 288 | unifying_pair_request_2_pack(transmit_entry->payload, &pair_request); 289 | 290 | err = unifying_ring_buffer_push_back(state->transmit_buffer, transmit_entry); 291 | 292 | if(err) 293 | { 294 | unifying_transmit_entry_destroy(transmit_entry); 295 | } 296 | 297 | return err; 298 | } 299 | 300 | /*! 301 | * Queue a payload for step 3 of the pairing process. 302 | * 303 | * \param[in,out] state Unifying state information. 304 | * \param[in] name Name of your device. 305 | * This name will appear in the Logitech Unifying desktop software. 306 | * This value does not need to be NULL terminated. 307 | * The name cannot be longer than \ref UNIFYING_MAX_NAME_LEN. 308 | * \param[in] name_length Length of the supplied name. 309 | * The name length does not include a NULL terminator. 310 | * The name cannot be longer than \ref UNIFYING_MAX_NAME_LEN. 311 | * 312 | * \return \ref UNIFYING_CREATE_ERROR if dynamic memory allocation fails. 313 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the transmit buffer is full. 314 | * \return \ref UNIFYING_SUCCESS otherwise. 315 | */ 316 | static enum unifying_error unifying_pair_step_3(struct unifying_state* state, 317 | const char* name, 318 | uint8_t name_length) 319 | { 320 | enum unifying_error err; 321 | struct unifying_transmit_entry* transmit_entry; 322 | struct unifying_pair_request_3 pair_request; 323 | 324 | transmit_entry = unifying_transmit_entry_create(UNIFYING_PAIR_REQUEST_3_LEN, state->default_timeout); 325 | 326 | if(!transmit_entry) 327 | { 328 | return UNIFYING_CREATE_ERROR; 329 | } 330 | 331 | unifying_pair_request_3_init(&pair_request, name, name_length); 332 | unifying_pair_request_3_pack(transmit_entry->payload, &pair_request); 333 | 334 | err = unifying_ring_buffer_push_back(state->transmit_buffer, transmit_entry); 335 | 336 | if(err) 337 | { 338 | unifying_transmit_entry_destroy(transmit_entry); 339 | } 340 | 341 | return err; 342 | } 343 | 344 | /*! 345 | * Queue a payload to complete the pairing process. 346 | * 347 | * \param[in,out] state Unifying state information. 348 | * 349 | * \return \ref UNIFYING_CREATE_ERROR if dynamic memory allocation fails. 350 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the transmit buffer is full. 351 | * \return \ref UNIFYING_SUCCESS otherwise. 352 | */ 353 | static enum unifying_error unifying_pair_complete(struct unifying_state* state) 354 | { 355 | enum unifying_error err; 356 | struct unifying_transmit_entry* transmit_entry; 357 | struct unifying_pair_complete_request pair_request; 358 | 359 | transmit_entry = unifying_transmit_entry_create(UNIFYING_PAIR_COMPLETE_REQUEST_LEN, state->default_timeout); 360 | 361 | if(!transmit_entry) 362 | { 363 | return UNIFYING_CREATE_ERROR; 364 | } 365 | 366 | unifying_pair_complete_request_init(&pair_request); 367 | unifying_pair_complete_request_pack(transmit_entry->payload, &pair_request); 368 | 369 | err = unifying_ring_buffer_push_back(state->transmit_buffer, transmit_entry); 370 | 371 | if(err) 372 | { 373 | unifying_transmit_entry_destroy(transmit_entry); 374 | } 375 | 376 | return err; 377 | } 378 | 379 | /*! 380 | * Queue a keep-alive payload. 381 | * 382 | * \todo remove \p timeout and use \ref unifying_state.timeout "state.timeout" instead. 383 | * 384 | * \param[in,out] state Unifying state information. 385 | * \param[in] timeout Current packet timeout. 386 | * 387 | * \return \ref UNIFYING_CREATE_ERROR if dynamic memory allocation fails. 388 | * \return \ref UNIFYING_BUFFER_FULL_ERROR if the transmit buffer is full. 389 | * \return \ref UNIFYING_SUCCESS otherwise. 390 | */ 391 | static enum unifying_error unifying_keep_alive(struct unifying_state* state, uint16_t timeout) 392 | { 393 | enum unifying_error err; 394 | struct unifying_transmit_entry* transmit_entry; 395 | struct unifying_keep_alive_request keep_alive_request; 396 | 397 | transmit_entry = unifying_transmit_entry_create(UNIFYING_KEEP_ALIVE_REQUEST_LEN, 0); 398 | 399 | if(!transmit_entry) 400 | { 401 | return UNIFYING_CREATE_ERROR; 402 | } 403 | 404 | unifying_keep_alive_request_init(&keep_alive_request, timeout); 405 | unifying_keep_alive_request_pack(transmit_entry->payload, &keep_alive_request); 406 | 407 | err = unifying_ring_buffer_push_back(state->transmit_buffer, transmit_entry); 408 | 409 | if(err) 410 | { 411 | unifying_transmit_entry_destroy(transmit_entry); 412 | } 413 | 414 | return err; 415 | } 416 | 417 | enum unifying_error unifying_tick(struct unifying_state* state) 418 | { 419 | uint32_t current_time = state->interface->time(); 420 | 421 | // Handle edge case where next_transmit has overflowed but the current_time hasn't. 422 | if(state->previous_transmit > state->next_transmit && current_time > state->previous_transmit) 423 | { 424 | return UNIFYING_SUCCESS; 425 | } 426 | 427 | if((current_time >= state->next_transmit) || 428 | // Handle edge case where current_time has overflowed but the next_transmit hasn't. 429 | (state->previous_transmit > current_time && state->next_transmit > current_time)) 430 | { 431 | 432 | if(!unifying_ring_buffer_empty(state->receive_buffer)) 433 | { 434 | // We have received a payload that hasn't been handled yet. 435 | // It should be a HID++ query so we'll queue a HID++ response. 436 | // TODO: Consider handling HID++ queries outside of the transmit interval. 437 | unifying_hidpp_1_0(state); 438 | } 439 | else if(unifying_ring_buffer_empty(state->transmit_buffer)) 440 | { 441 | // No payloads are queued for transmission so we'll queue a keep alive packet. 442 | unifying_keep_alive(state, state->timeout); 443 | } 444 | 445 | // Get a payload and transmit it 446 | struct unifying_transmit_entry* transmit_entry; 447 | transmit_entry = unifying_ring_buffer_peek_front(state->transmit_buffer); 448 | 449 | if(!transmit_entry) 450 | { 451 | // The buffer appears to be empty even though we checked and queued data if it was empty. 452 | // This should never happen. 453 | return UNIFYING_BUFFER_EMPTY_ERROR; 454 | } 455 | 456 | enum unifying_error err; 457 | err = unifying_transmit(state, 458 | transmit_entry->payload, 459 | transmit_entry->length, 460 | transmit_entry->timeout); 461 | 462 | if(err) 463 | { 464 | // Transmission failed. 465 | // Keep the payload queued for re-transmission. 466 | return err; 467 | } 468 | 469 | // Dequeue and destroy the transmit entry since we won't need it anymore. 470 | unifying_transmit_entry_destroy(unifying_ring_buffer_pop_front(state->transmit_buffer)); 471 | 472 | if(state->interface->payload_available()) { 473 | return unifying_receive(state); 474 | } 475 | } 476 | 477 | return UNIFYING_SUCCESS; 478 | } 479 | 480 | enum unifying_error unifying_loop(struct unifying_state* state, 481 | bool exit_on_error, 482 | bool exit_on_transmit, 483 | bool exit_on_receive) 484 | { 485 | enum unifying_error err = UNIFYING_SUCCESS; 486 | 487 | while(true) 488 | { 489 | if(exit_on_error && err) 490 | { 491 | // Some error occurred. 492 | break; 493 | } 494 | 495 | if(exit_on_transmit && unifying_ring_buffer_empty(state->transmit_buffer)) 496 | { 497 | // Transmit buffer empty. 498 | break; 499 | } 500 | 501 | if(exit_on_receive && !unifying_ring_buffer_empty(state->receive_buffer)) 502 | { 503 | // Payload received. 504 | break; 505 | } 506 | 507 | err = unifying_tick(state); 508 | } 509 | 510 | return err; 511 | } 512 | 513 | enum unifying_error unifying_pair(struct unifying_state* state, 514 | uint8_t id, 515 | uint16_t product_id, 516 | uint16_t device_type, 517 | uint32_t crypto, 518 | uint32_t serial, 519 | uint16_t capabilities, 520 | const char* name, 521 | uint8_t name_length) 522 | { 523 | enum unifying_error err = UNIFYING_SUCCESS; 524 | struct unifying_receive_entry* receive_entry; 525 | 526 | // Unifying appears to only support 16 character names. 527 | if(name_length > UNIFYING_MAX_NAME_LEN) 528 | { 529 | return UNIFYING_NAME_LENGTH_ERROR; 530 | } 531 | 532 | // Pairing begins on a predetermined address. 533 | if(state->interface->set_address(unifying_pairing_address)) 534 | { 535 | return UNIFYING_SET_ADDRESS_ERROR; 536 | } 537 | 538 | // We want total control of the buffers so we'll clear them before pairing. 539 | unifying_state_buffers_clear(state); 540 | 541 | // Queue a pairing packet for transmission. 542 | unifying_pair_step_1(state, id, product_id, device_type); 543 | 544 | // We don't know which channel the receiver is listening on. 545 | // Try to pair on each channel until one works. 546 | for(int i = 0; i < UNIFYING_CHANNELS_LEN; i++) 547 | { 548 | // Transmit the initial paring request. 549 | err = unifying_loop(state, true, true, false); 550 | 551 | // If transmission fails then we'll try again on another channel. 552 | if(!err) 553 | { 554 | // Success. 555 | // Continue pairing. 556 | break; 557 | } 558 | } 559 | 560 | // We may have already received a payload from a previous pairing attempt. 561 | // That payload is invalid so we'll just ignore it. 562 | unifying_state_buffers_clear(state); 563 | 564 | if(err) 565 | { 566 | return err; 567 | } 568 | 569 | 570 | // Send a keep alive payload so we can receive a response. 571 | err = unifying_loop(state, true, false, true); 572 | 573 | if(err) 574 | { 575 | // If transmission failed then a keep alive payload is still buffered. 576 | unifying_state_buffers_clear(state); 577 | return err; 578 | } 579 | 580 | err = unifying_response(state, &receive_entry, UNIFYING_PAIR_RESPONSE_1_LEN); 581 | 582 | if(err) 583 | { 584 | return err; 585 | } 586 | 587 | // Unpack the response. 588 | struct unifying_pair_response_1 pair_response_1; 589 | unifying_pair_response_1_unpack(&pair_response_1, receive_entry->payload); 590 | unifying_receive_entry_destroy(receive_entry); 591 | 592 | // Check that we got the correct response to our pairing request. 593 | if(pair_response_1.step != 1) 594 | { 595 | return UNIFYING_PAIR_STEP_ERROR; 596 | } 597 | 598 | // Check that the response was intended for us. 599 | if(id != pair_response_1.id) 600 | { 601 | return UNIFYING_PAIR_ID_ERROR; 602 | } 603 | 604 | // We've received a new address for all future communication with the receiver. 605 | if(unifying_state_address_set(state, pair_response_1.address)) 606 | { 607 | return UNIFYING_SET_ADDRESS_ERROR; 608 | } 609 | 610 | err = unifying_pair_step_2(state, crypto, serial, capabilities); 611 | 612 | if(err) 613 | { 614 | return err; 615 | } 616 | 617 | // Transmit the next paring request. 618 | err = unifying_loop(state, true, true, false); 619 | 620 | if(err) 621 | { 622 | // If transmission failed then a pairing request is still buffered. 623 | unifying_state_buffers_clear(state); 624 | return err; 625 | } 626 | 627 | // Send a keep alive payload so we can receive a response. 628 | err = unifying_loop(state, true, false, true); 629 | 630 | if(err) 631 | { 632 | // If transmission failed then a keep alive payload is still buffered. 633 | unifying_state_buffers_clear(state); 634 | return err; 635 | } 636 | 637 | err = unifying_response(state, &receive_entry, UNIFYING_PAIR_RESPONSE_2_LEN); 638 | 639 | if(err) 640 | { 641 | return err; 642 | } 643 | 644 | // Unpack the response. 645 | struct unifying_pair_response_2 pair_response_2; 646 | unifying_pair_response_2_unpack(&pair_response_2, receive_entry->payload); 647 | unifying_receive_entry_destroy(receive_entry); 648 | 649 | // Check that we got the correct response to our pairing request. 650 | if(pair_response_2.step != 2) 651 | { 652 | return UNIFYING_PAIR_STEP_ERROR; 653 | } 654 | 655 | err = unifying_pair_step_3(state, name, name_length); 656 | 657 | if(err) 658 | { 659 | return err; 660 | } 661 | 662 | // Transmit the next paring request. 663 | err = unifying_loop(state, true, true, false); 664 | 665 | if(err) 666 | { 667 | // If transmission failed then a pairing request is still buffered. 668 | unifying_state_buffers_clear(state); 669 | return err; 670 | } 671 | 672 | // Send a keep alive payload so we can receive a response. 673 | err = unifying_loop(state, true, false, true); 674 | 675 | if(err) 676 | { 677 | // If transmission failed then a keep alive payload is still buffered. 678 | unifying_state_buffers_clear(state); 679 | return err; 680 | } 681 | 682 | err = unifying_response(state, &receive_entry, UNIFYING_PAIR_RESPONSE_3_LEN); 683 | 684 | if(err) 685 | { 686 | return err; 687 | } 688 | 689 | // Unpack the response. 690 | struct unifying_pair_response_3 pair_response_3; 691 | unifying_pair_response_3_unpack(&pair_response_3, receive_entry->payload); 692 | unifying_receive_entry_destroy(receive_entry); 693 | 694 | // Check that we got the correct response to our pairing request. 695 | if(pair_response_3.step != 6) 696 | { 697 | return UNIFYING_PAIR_STEP_ERROR; 698 | } 699 | 700 | err = unifying_pair_complete(state); 701 | 702 | if(err) 703 | { 704 | return err; 705 | } 706 | 707 | // Transmit the next paring request. 708 | err = unifying_loop(state, true, true, false); 709 | 710 | if(err) 711 | { 712 | // If transmission failed then a pairing request is still buffered. 713 | unifying_state_buffers_clear(state); 714 | return err; 715 | } 716 | 717 | // We've received all the information that we need to create an AES key. 718 | // We need to deobfuscate it. 719 | struct unifying_proto_aes_key proto_aes_key; 720 | unifying_proto_aes_key_init(&proto_aes_key, 721 | pair_response_1.address, 722 | product_id, 723 | pair_response_1.product_id, 724 | crypto, 725 | pair_response_2.crypto); 726 | uint8_t aes_buffer[UNIFYING_AES_BLOCK_LEN]; 727 | unifying_proto_aes_key_pack(aes_buffer, &proto_aes_key); 728 | unifying_deobfuscate_aes_key(state->aes_key, aes_buffer); 729 | 730 | return UNIFYING_SUCCESS; 731 | } 732 | 733 | enum unifying_error unifying_connect(struct unifying_state* state) 734 | { 735 | enum unifying_error err; 736 | struct unifying_transmit_entry* transmit_entry; 737 | struct unifying_short_wake_up_request request; 738 | 739 | transmit_entry = unifying_transmit_entry_create(UNIFYING_SHORT_WAKE_UP_REQUEST_LEN, state->default_timeout); 740 | 741 | if(!transmit_entry) 742 | { 743 | return UNIFYING_CREATE_ERROR; 744 | } 745 | 746 | unifying_short_wake_up_request_init(&request, state->address[4]); 747 | unifying_short_wake_up_request_pack(transmit_entry->payload, &request); 748 | 749 | err = unifying_ring_buffer_push_back(state->transmit_buffer, transmit_entry); 750 | 751 | if(err) 752 | { 753 | unifying_transmit_entry_destroy(transmit_entry); 754 | return err; 755 | } 756 | 757 | // We don't know which channel the receiver is listening on. 758 | // Try to connect on each channel until one works. 759 | for(int i = 0; i < UNIFYING_CHANNELS_LEN; i++) 760 | { 761 | // Transmit the initial paring request. 762 | err = unifying_loop(state, true, true, false); 763 | 764 | // If transmission fails then we'll try again on another channel. 765 | if(!err) 766 | { 767 | // Success. 768 | // Continue pairing. 769 | break; 770 | } 771 | } 772 | 773 | return err; 774 | } 775 | 776 | enum unifying_error unifying_set_timeout(struct unifying_state* state, uint16_t timeout) 777 | { 778 | enum unifying_error err; 779 | struct unifying_transmit_entry* transmit_entry; 780 | struct unifying_set_timeout_request timeout_request; 781 | 782 | transmit_entry = unifying_transmit_entry_create(UNIFYING_SET_TIMEOUT_REQUEST_LEN, timeout); 783 | 784 | if(!transmit_entry) 785 | { 786 | return UNIFYING_CREATE_ERROR; 787 | } 788 | 789 | unifying_set_timeout_request_init(&timeout_request, timeout); 790 | unifying_set_timeout_request_pack(transmit_entry->payload, &timeout_request); 791 | 792 | err = unifying_ring_buffer_push_back(state->transmit_buffer, transmit_entry); 793 | 794 | if(err) 795 | { 796 | unifying_transmit_entry_destroy(transmit_entry); 797 | } 798 | 799 | return err; 800 | } 801 | 802 | enum unifying_error unifying_encrypted_keystroke(struct unifying_state* state, 803 | const uint8_t keys[UNIFYING_KEYS_LEN], 804 | uint8_t modifiers) 805 | { 806 | enum unifying_error err; 807 | uint8_t aes_buffer[UNIFYING_AES_DATA_LEN]; 808 | uint8_t aes_iv[UNIFYING_AES_BLOCK_LEN]; 809 | uint8_t payload[UNIFYING_ENCRYPTED_KEYSTROKE_REQUEST_LEN]; 810 | 811 | struct unifying_encrypted_keystroke_plaintext plaintext; 812 | struct unifying_encrypted_keystroke_iv iv; 813 | struct unifying_encrypted_keystroke_request request; 814 | 815 | unifying_encrypted_keystroke_plaintext_init(&plaintext, modifiers, keys); 816 | unifying_encrypted_keystroke_plaintext_pack(aes_buffer, &plaintext); 817 | 818 | unifying_encrypted_keystroke_iv_init(&iv, state->aes_counter); 819 | unifying_encrypted_keystroke_iv_pack(aes_iv, &iv); 820 | 821 | if(state->interface->encrypt(aes_buffer, state->aes_key, aes_iv)) { 822 | return UNIFYING_ENCRYPTION_ERROR; 823 | } 824 | 825 | unifying_encrypted_keystroke_request_init(&request, aes_buffer, state->aes_counter); 826 | unifying_encrypted_keystroke_request_pack(payload, &request); 827 | 828 | err = unifying_transmit(state, payload, UNIFYING_ENCRYPTED_KEYSTROKE_REQUEST_LEN, state->default_timeout); 829 | 830 | if(err) 831 | { 832 | return err; 833 | } 834 | 835 | state->aes_counter++; 836 | 837 | if(state->interface->payload_available()) { 838 | return unifying_receive(state); 839 | } 840 | 841 | return UNIFYING_SUCCESS; 842 | } 843 | 844 | enum unifying_error unifying_multimeia_keystroke(struct unifying_state* state, 845 | uint8_t keys[UNIFYING_MULTIMEDIA_KEYS_LEN]) 846 | { 847 | enum unifying_error err; 848 | uint8_t payload[UNIFYING_MULTIMEDIA_KEYSTROKE_REQUEST_LEN]; 849 | struct unifying_multimeia_keystroke_request request; 850 | 851 | unifying_multimeia_keystroke_request_init(&request, keys); 852 | unifying_multimeia_keystroke_request_pack(payload, &request); 853 | 854 | err = unifying_transmit(state, payload, UNIFYING_MULTIMEDIA_KEYSTROKE_REQUEST_LEN, state->default_timeout); 855 | 856 | if(err) 857 | { 858 | return err; 859 | } 860 | 861 | if(state->interface->payload_available()) { 862 | return unifying_receive(state); 863 | } 864 | 865 | return UNIFYING_SUCCESS; 866 | } 867 | 868 | // enum unifying_error unifying_mouse(struct unifying_state* state, 869 | // uint8_t buttons, 870 | // int16_t move_y, 871 | // int16_t move_x, 872 | // int8_t wheel_y, 873 | // int8_t wheel_x) 874 | // { 875 | // enum unifying_error err; 876 | // uint8_t payload[UNIFYING_MOUSE_REQUEST_LEN]; 877 | // struct unifying_mouse_request request; 878 | 879 | // move_x = unifying_int12_clamp(move_x); 880 | // move_y = unifying_int12_clamp(move_y); 881 | 882 | // unifying_mouse_request_init(&request, buttons, move_y, move_x, wheel_y, wheel_x); 883 | // unifying_mouse_request_pack(payload, &request); 884 | 885 | // err = unifying_transmit(state, payload, UNIFYING_MOUSE_REQUEST_LEN, state->default_timeout); 886 | 887 | // if(err) 888 | // { 889 | // return err; 890 | // } 891 | 892 | // if(state->interface->payload_available()) { 893 | // return unifying_receive(state); 894 | // } 895 | 896 | // return UNIFYING_SUCCESS; 897 | // } 898 | 899 | enum unifying_error unifying_mouse(struct unifying_state* state, 900 | uint8_t buttons, 901 | int16_t move_y, 902 | int16_t move_x, 903 | int8_t wheel_y, 904 | int8_t wheel_x) 905 | { 906 | enum unifying_error err; 907 | struct unifying_transmit_entry* transmit_entry; 908 | struct unifying_mouse_request request; 909 | 910 | move_y = unifying_int12_clamp(move_y); 911 | move_x = unifying_int12_clamp(move_x); 912 | 913 | transmit_entry = unifying_transmit_entry_create(UNIFYING_MOUSE_REQUEST_LEN, state->default_timeout); 914 | 915 | if(!transmit_entry) 916 | { 917 | return UNIFYING_CREATE_ERROR; 918 | } 919 | 920 | unifying_mouse_request_init(&request, buttons, move_y, move_x, wheel_y, wheel_x); 921 | unifying_mouse_request_pack(transmit_entry->payload, &request); 922 | 923 | err = unifying_ring_buffer_push_back(state->transmit_buffer, transmit_entry); 924 | 925 | if(err) 926 | { 927 | unifying_transmit_entry_destroy(transmit_entry); 928 | } 929 | 930 | return err; 931 | } 932 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------