|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2022 IMProject Development Team. All rights reserved. |
| 4 | + * Authors: Igor Misic <igy1000mb@gmail.com> |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in |
| 14 | + * the documentation and/or other materials provided with the |
| 15 | + * distribution. |
| 16 | + * 3. Neither the name IMProject nor the names of its contributors may be |
| 17 | + * used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 26 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 27 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 28 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 30 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + * POSSIBILITY OF SUCH DAMAGE. |
| 32 | + * |
| 33 | + ****************************************************************************/ |
| 34 | + |
| 35 | +#ifndef BOOTLOADER_INC_SECURITY_H_ |
| 36 | +#define BOOTLOADER_INC_SECURITY_H_ |
| 37 | + |
| 38 | +#include <stdint.h> |
| 39 | +#include <stdbool.h> |
| 40 | +#include <assert.h> |
| 41 | +#include <string.h> |
| 42 | + |
| 43 | +typedef enum securityAlgorithm_ENUM { |
| 44 | + BLAKE2B = 0, |
| 45 | + SHA256 |
| 46 | +} securityAlgorithm_E; |
| 47 | + |
| 48 | +#define MAC_SIZE 16U |
| 49 | +#define NONCE_SIZE 24U |
| 50 | +#define DATA_SIZE 256U |
| 51 | +#define SECURE_PACKET_SIZE 296U // PACKET_SIZE = MAC_SIZE + NONCE_SIZE + DATA_SIZE |
| 52 | + |
| 53 | +#define SERVER_SECURITY_DATA_SIZE 175U |
| 54 | + |
| 55 | +#define SIGNATURE_ALGORITHM BLAKE2B //!< Selected algorithm for calculating public key signature |
| 56 | + |
| 57 | +#define PRESHARED_KEY_SIZE 32U //!< Size for preshared key binary |
| 58 | +#define PRESHARED_KEY_SIZE_BASE64_STR 45U //!< Size for preshared key string in base64 format including null-terminator |
| 59 | +#define PRESHARED_KEY_BASE64_STR (const char*)("cHJlc2hhcmVkX2tleV9pbl8zMl9ieXRlc19mb3JtYXQ=") |
| 60 | + |
| 61 | +static_assert(strlen((const char*)PRESHARED_KEY_BASE64_STR) == (PRESHARED_KEY_SIZE_BASE64_STR - 1U), "PRESHARED_KEY_BASE64_STR is wrong size"); |
| 62 | + |
| 63 | +bool Security_setServerSecurityDataJson(char* buffer, uint16_t buffer_size); |
| 64 | +bool Security_getClientSecurityDataJson(char* buffer, uint16_t buffer_size); |
| 65 | +bool Security_isSecured(void); |
| 66 | +bool Security_decrypt( |
| 67 | + const uint8_t mac[MAC_SIZE], |
| 68 | + const uint8_t nonce[NONCE_SIZE], |
| 69 | + const uint8_t* cipher_data, |
| 70 | + uint8_t* plain_data, |
| 71 | + const uint16_t data_length); |
| 72 | +void Security_wipeKeys(void); |
| 73 | + |
| 74 | +#endif /* BOOTLOADER_INC_SECURITY_H_ */ |
0 commit comments