-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoESP.ino
More file actions
138 lines (110 loc) · 3.66 KB
/
Copy pathCryptoESP.ino
File metadata and controls
138 lines (110 loc) · 3.66 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <Arduino.h>
#include <mbedtls/config.h>
#include <mbedtls/platform.h>
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/rsa.h>
#include <mbedtls/sha256.h>
#include <mbedtls/sha512.h>
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_rsa_context rsa;
void printPEM(const char *label, mbedtls_mpi *mpi);
void printRSAKey(mbedtls_rsa_context *key);
void setup() {
Serial.begin(115200);
Serial.println("Bienvenue dans l'interpréteur de commandes ESP32!");
}
void loop() {
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
executeCommand(command);
}
}
void executeCommand(String command) {
command.trim(); // Supprimer les espaces de début et de fin
if (command.equals("generate_rsa_key")) {
generateRSAKey();
} else if (command.startsWith("sha256 ")) {
String input = command.substring(7);
hashSHA256(input);
} else if (command.startsWith("sha512 ")) {
String input = command.substring(7);
hashSHA512(input);
} else {
Serial.println("Commande non reconnue. Les commandes disponibles sont :");
Serial.println("- generate_rsa_key");
Serial.println("- sha256 [chaine]");
Serial.println("- sha512 [chaine]");
}
}
void generateRSAKey() {
mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctr_drbg);
const char *pers = "rsa_gen";
mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers, strlen(pers));
mbedtls_rsa_gen_key(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, 2048, 65537);
Serial.println("Paire de clés RSA 4096 bits générée avec succès!");
printRSAKey(&rsa);
mbedtls_rsa_free(&rsa);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
}
void printRSAKey(mbedtls_rsa_context *key) {
Serial.println("----- Clé RSA N -----");
printPEM("N = ", &key->N);
Serial.println("----- Clé RSA E -----");
printPEM("E = ", &key->E);
Serial.println("----- Clé RSA P -----");
printPEM("P = ", &key->P);
Serial.println("----- Clé RSA Q -----");
printPEM("Q = ", &key->Q);
Serial.println("----- Clé RSA D -----");
printPEM("D = ", &key->D);
Serial.println("----- Clé RSA DP -----");
printPEM("DP = ", &key->DP);
Serial.println("----- Clé RSA DQ -----");
printPEM("DQ = ", &key->DQ);
Serial.println("----- Clé RSA QP -----");
printPEM("QP = ", &key->QP);
}
void hashSHA256(String input) {
mbedtls_sha256_context sha256;
mbedtls_sha256_init(&sha256);
mbedtls_sha256_starts(&sha256, 0);
mbedtls_sha256_update(&sha256, (const unsigned char *)input.c_str(), input.length());
unsigned char hash[32];
mbedtls_sha256_finish(&sha256, hash);
printHash(hash, 32);
mbedtls_sha256_free(&sha256);
}
void hashSHA512(String input) {
mbedtls_sha512_context sha512;
mbedtls_sha512_init(&sha512);
mbedtls_sha512_starts(&sha512, 0);
mbedtls_sha512_update(&sha512, (const unsigned char *)input.c_str(), input.length());
unsigned char hash[64];
mbedtls_sha512_finish(&sha512, hash);
printHash(hash, 64);
mbedtls_sha512_free(&sha512);
}
void printHash(unsigned char *hash, int length) {
Serial.print("Hash : ");
for (int i = 0; i < length; i++) {
Serial.print(String(hash[i], HEX));
}
Serial.println();
}
void printPEM(const char *label, mbedtls_mpi *mpi) {
char buf[4096]; // Augmenter la taille du tampon
size_t len = 0;
int ret = mbedtls_mpi_write_string(mpi, 16, buf, sizeof(buf), &len);
if (ret != 0) {
Serial.println("Erreur lors de l'écriture de l'entier MPI dans la chaîne PEM");
return;
}
Serial.print(label);
Serial.println(buf);
}