-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScpCrypto.h
More file actions
99 lines (79 loc) · 2.39 KB
/
Copy pathScpCrypto.h
File metadata and controls
99 lines (79 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
secure-control-protocol
This is the header file for the ScpCrypto class.
SPDX-License-Identifier: GPL-3.0-or-later
Copyright (C) 2018 - 2020 Benjamin Schilling
*/
#ifndef ScpCrypto_h
#define ScpCrypto_h
// Arduino Libraries
#include "Arduino.h"
// ESP8266 Libraries
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <Crypto.h>
// SCP Libraries
#include "ScpDebug.h"
#include "ScpDecode.h"
// Defines for ChaCha20 Poly1305
#define MAC_LENGTH 16
#define KEY_LENGTH 32
#define NONCE_LENGTH 12
// Defines for the HMAC
#define HMAC_LENGTH 64
#define SUCCESS 0
#define ERROR 1
const int SEED_PIN = A0;
using namespace experimental::crypto;
class ScpCrypto
{
public:
/**
* @brief Construct a new Scp Crypto object
*
*/
ScpCrypto();
/**
* @brief Uses ChaCha20_Poly1305 to decrypt the message and verify
* the message authentication code afterwards
*
* @param base64Payload The base64 encoded encrypted payload
* @param payloadLength The length of the encrypted payload
* @param key The key
* @param base64Nonce The base64 encoded nonce
* @param base64Mac The base64 encoded message authentication code
*
* @returns String The decrypted message on success, empty string on error
*/
String decodeAndDecrypt(String base64Payload, int payloadLength,
String key, String base64Nonce, String base64Mac);
/**
* @brief Uses ChaCha20_Poly1305 to encrypt the message and generate
* the message authentication code
*
* @param payload The plaintext payload
* @param key The key
* @param base64Nonce The base64 encoded nonce after the encryption
* @param base64Mac The base64 encoded message authentication code after the encryption
*
* @returns String The encrypted and encoded message on success, empty string on error
*/
String encryptAndEncode(String payload, int *payloadLength,
String key, String *base64Nonce, String *base64Mac);
/**
* @brief Generates a SHA512 HMAC for the given payload
*
* @param payload The payload to hash
* @param key The key for the HMAC as byte array
* @param keyLength The length of the key
*
* @returns The resulting HMAC as HEX String
*/
String generateHMAC(String payload, uint8_t *key, size_t keyLength);
String getNVCN();
bool checkNVCN(String nvcn);
private:
ScpDebug scpDebug;
long unsigned int currentNvcn;
};
#endif