Skip to content

Commit f02117e

Browse files
Merge pull request arduino-libraries#87 from andreagilardoni/secure-element-methods
implementing methods from SecureElement library
2 parents 4de05ab + 30bf80c commit f02117e

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/ECCX08.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ void ECCX08Class::end()
7373
#endif
7474
}
7575

76-
int ECCX08Class::serialNumber(byte sn[])
76+
int ECCX08Class::serialNumber(byte sn[], size_t len)
7777
{
78+
if(len < 12) {
79+
return 0;
80+
}
81+
7882
if (!read(0, 0, &sn[0], 4)) {
7983
return 0;
8084
}
@@ -252,6 +256,18 @@ int ECCX08Class::ecSign(int slot, const byte message[], byte signature[])
252256
return 1;
253257
}
254258

259+
int ECCX08Class::SHA256(const uint8_t *buffer, size_t size, uint8_t *digest)
260+
{
261+
beginSHA256();
262+
uint8_t * cursor = (uint8_t*)buffer;
263+
uint32_t bytes_read = 0;
264+
265+
for(; bytes_read + 64 < size; bytes_read += 64, cursor += 64) {
266+
updateSHA256(cursor);
267+
}
268+
return endSHA256(cursor, size - bytes_read, digest);
269+
}
270+
255271
int ECCX08Class::beginSHA256()
256272
{
257273
uint8_t status;

src/ECCX08.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <Arduino.h>
2424
#include <Wire.h>
25+
#include "utility/ECCX08DefaultTLSConfig.h"
2526

2627
class ECCX08Class
2728
{
@@ -33,7 +34,7 @@ class ECCX08Class
3334
int begin(uint8_t i2cAddress);
3435
void end();
3536

36-
int serialNumber(byte sn[]);
37+
int serialNumber(byte sn[], size_t length=12);
3738
String serialNumber();
3839

3940
long random(long max);
@@ -46,6 +47,7 @@ class ECCX08Class
4647
int ecdsaVerify(const byte message[], const byte signature[], const byte pubkey[]);
4748
int ecSign(int slot, const byte message[], byte signature[]);
4849

50+
int SHA256(const uint8_t *buffer, size_t size, uint8_t *digest);
4951
int beginSHA256();
5052
int updateSHA256(const byte data[]); // 64 bytes
5153
int endSHA256(byte result[]);
@@ -55,7 +57,7 @@ class ECCX08Class
5557
int writeSlot(int slot, const byte data[], int length);
5658

5759
int locked();
58-
int writeConfiguration(const byte data[]);
60+
int writeConfiguration(const byte data[]=ECCX08_DEFAULT_TLS_CONFIG);
5961
int readConfiguration(byte data[]);
6062
int lock();
6163

0 commit comments

Comments
 (0)