Skip to content

Commit 9bd5b56

Browse files
committed
Merge pull request #25 from spark/feature/secure-random-seed
Secure random seed from Spark Cloud
2 parents 6dd0504 + a69c03a commit 9bd5b56

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ ssllib = $(ssllibdir)/libtropicssl.a
1818
objects = src/handshake.o \
1919
src/coap.o \
2020
src/spark_protocol.o \
21-
src/events.o
21+
src/events.o \
22+
src/functions.o
2223

2324
testobjects = tests/ConstructorFixture.o \
2425
tests/TestHandshake.o \

src/build.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ TARGET_SRC_PATH = src
1010
INCLUDE_DIRS += $(TARGET_SRC_PATH)
1111

1212
# C source files included in this build.
13-
CSRC +=
13+
CSRC += $(TARGET_SRC_PATH)/functions.c
1414

1515
# C++ source files included in this build.
1616
CPPSRC += $(TARGET_SRC_PATH)/coap.cpp

src/functions.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#include "functions.h"
3+
#include <stdlib.h>
4+
5+
/**
6+
* Handle the cryptographically secure random seed from the cloud by using
7+
* it to seed the stdlib PRNG.
8+
* @param seed A random value from a cryptographically secure random number generator.
9+
*
10+
* This function has weak linkage, so that user code may re-define this function and
11+
* handle the random number in some other way. For example, to combine with local
12+
* entropy sources.
13+
*/
14+
__attribute__((weak)) void random_seed_from_cloud(unsigned int seed){
15+
srand(seed);
16+
}
17+

src/functions.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* File: functions.h
3+
* Author: mat
4+
*
5+
* Created on 04 September 2014, 00:10
6+
*/
7+
8+
#ifndef FUNCTIONS_H
9+
#define FUNCTIONS_H
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
/**
16+
* Handle the cryptographically secure random seed from the cloud.
17+
* @param seed A random value. This is typically used to seed a pseudo-random generator.
18+
*/
19+
extern void random_seed_from_cloud(unsigned int seed);
20+
21+
22+
23+
#ifdef __cplusplus
24+
}
25+
#endif
26+
27+
#endif /* FUNCTIONS_H */
28+

src/spark_protocol.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
#include "spark_protocol.h"
2626
#include "handshake.h"
27+
#include "functions.h"
2728
#include <string.h>
2829
#include <stdlib.h>
2930

@@ -1233,6 +1234,10 @@ int SparkProtocol::set_key(const unsigned char *signed_encrypted_credentials)
12331234
_message_id = *(credentials + 32) << 8 | *(credentials + 33);
12341235
_token = *(credentials + 34);
12351236

1237+
unsigned int seed;
1238+
memcpy(&seed, credentials + 35, 4);
1239+
random_seed_from_cloud(seed);
1240+
12361241
return 0;
12371242
}
12381243
else return 2;
@@ -1265,6 +1270,6 @@ inline void SparkProtocol::coded_ack(unsigned char *buf,
12651270
buf[4] = token;
12661271

12671272
memset(buf + 5, 11, 11); // PKCS #7 padding
1268-
1273+
12691274
encrypt(buf, 16);
12701275
}

0 commit comments

Comments
 (0)