Skip to content

Commit b324289

Browse files
authored
Merge branch 'master' into fix/compile_issue
2 parents 7c4188a + f4307ef commit b324289

10 files changed

Lines changed: 89 additions & 81 deletions

File tree

‎Makefile‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
# This is a draft of a Makefile
22
# it contains stuff like -g and -std=c11 and -fsanitize=address that are not
33
# useful for production binaries
4-
5-
# for some reason -Os gives me larger library
6-
CFLAGS= -g -fPIC -Os -Wall -Werror -std=c99 -fsanitize=address
4+
CFLAGS= -g -fPIC -Os -Wall -Werror -std=c99 -fsanitize=address,undefined
75

86
.PHONY: all clean test test_strobe
97

108
all: disco.so
119

1210
# make a library
1311
disco.so: disco_symmetric.o disco_asymmetric.o tweetstrobe.o tweetX25519.o randombytes.o
14-
$(CC) -shared disco_symmetric.o disco_asymmetric.o tweetstrobe.o tweetX25519.o randombytes.o -o disco.so
12+
$(CC) $(CFLAGS) -shared disco_symmetric.o disco_asymmetric.o tweetstrobe.o tweetX25519.o randombytes.o -o disco.so
1513

1614
# Disco protocol
1715
disco_asymmetric.o: lib/disco_asymmetric.c lib/disco_asymmetric.h
@@ -35,8 +33,8 @@ tweetstrobe.o: lib/tweetstrobe.c lib/tweetstrobe.h lib/keccak_f.c.inc
3533

3634
# test is probably how you should compile your own program
3735
test: tests/test_disco.c disco_asymmetric.o tweetstrobe.o tweetX25519.o randombytes.o disco_symmetric.o
38-
$(CC) $(CFLAGS) -g tests/test_disco.c -c -o test_disco.o -I lib
39-
$(CC) $(CFLAGS) -g test_disco.o disco_asymmetric.o disco_symmetric.o tweetstrobe.o tweetX25519.o randombytes.o -o test
36+
$(CC) $(CFLAGS) tests/test_disco.c -c -o test_disco.o -I lib
37+
$(CC) $(CFLAGS) test_disco.o disco_asymmetric.o disco_symmetric.o tweetstrobe.o tweetX25519.o randombytes.o -o test
4038
./test
4139

4240
# test our implementation of tweetstrobe

‎README.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int main() {
2727
NULL, NULL, NULL);
2828

2929
// process the first handshake message → e, es, s, ss
30-
u8 in[500];
30+
uint8_t in[500];
3131
size_t in_len
3232
bool ret = disco_ReadMessage(&hs_server, out, out_len, in, &in_len, NULL, NULL);
3333
if (!ret) {
@@ -41,7 +41,7 @@ int main() {
4141
strobe_s s_write;
4242
strobe_s s_read;
4343
size_t out_len;
44-
ret = disco_WriteMessage(&hs_server, (u8 *)"second payload", 15,
44+
ret = disco_WriteMessage(&hs_server, (uint8_t *)"second payload", 15,
4545
out, &out_len, &s_read, &s_write);
4646
if (!ret) {
4747
abort();
@@ -86,10 +86,10 @@ int main() {
8686
NULL, &server_keypair, NULL);
8787

8888
// write the first handshake message → e, es, s, ss
89-
u8 out[500];
89+
uint8_t out[500];
9090
size_t out_len;
9191
bool ret =
92-
disco_WriteMessage(&hs_client, (u8*)"hey!", 5, out, &out_len, NULL, NULL);
92+
disco_WriteMessage(&hs_client, (uint8_t*)"hey!", 5, out, &out_len, NULL, NULL);
9393
if (!ret) {
9494
abort();
9595
}
@@ -110,8 +110,8 @@ int main() {
110110
assert(c_write.initialized && c_read.initialized);
111111

112112
// send a post-handshake message
113-
u8 plaintext[] = "just a simple message";
114-
u8* ciphertext = (u8*)malloc(sizeof(plaintext) + 16);
113+
uint8_t plaintext[] = "just a simple message";
114+
uint8_t* ciphertext = (uint8_t*)malloc(sizeof(plaintext) + 16);
115115
memcpy(ciphertext, plaintext, sizeof(plaintext));
116116

117117
disco_EncryptInPlace(&c_write, ciphertext, sizeof(plaintext),

‎docs/index.md‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int main() {
2727
NULL, NULL, NULL);
2828

2929
// process the first handshake message → e, es, s, ss
30-
u8 in[500];
30+
uint8_t in[500];
3131
size_t in_len
3232
bool ret = disco_ReadMessage(&hs_server, out, out_len, in, &in_len, NULL, NULL);
3333
if (!ret) {
@@ -41,7 +41,7 @@ int main() {
4141
strobe_s s_write;
4242
strobe_s s_read;
4343
size_t out_len;
44-
ret = disco_WriteMessage(&hs_server, (u8 *)"second payload", 15,
44+
ret = disco_WriteMessage(&hs_server, (uint8_t *)"second payload", 15,
4545
out, &out_len, &s_read, &s_write);
4646
if (!ret) {
4747
abort();
@@ -86,10 +86,10 @@ int main() {
8686
NULL, &server_keypair, NULL);
8787

8888
// write the first handshake message → e, es, s, ss
89-
u8 out[500];
89+
uint8_t out[500];
9090
size_t out_len;
9191
bool ret =
92-
disco_WriteMessage(&hs_client, (u8*)"hey!", 5, out, &out_len, NULL, NULL);
92+
disco_WriteMessage(&hs_client, (uint8_t*)"hey!", 5, out, &out_len, NULL, NULL);
9393
if (!ret) {
9494
abort();
9595
}
@@ -110,8 +110,8 @@ int main() {
110110
assert(c_write.initialized && c_read.initialized);
111111

112112
// send a post-handshake message
113-
u8 plaintext[] = "just a simple message";
114-
u8* ciphertext = (u8*)malloc(sizeof(plaintext) + 16);
113+
uint8_t plaintext[] = "just a simple message";
114+
uint8_t* ciphertext = (uint8_t*)malloc(sizeof(plaintext) + 16);
115115
memcpy(ciphertext, plaintext, sizeof(plaintext));
116116

117117
disco_EncryptInPlace(&c_write, ciphertext, sizeof(plaintext),
@@ -134,6 +134,12 @@ int main() {
134134
}
135135
```
136136

137+
## Testing and Production
138+
139+
**EmbeddedDisco is not stable yet, it is not ready for production**.
140+
141+
Do not use optimizations for testing, as the code contains asserts to pick common mistakes.
142+
137143
## Need help?
138144

139145
This library is still heavily experimental. Its goal is to support as many platforms as possible. If you need help making it work for a specific platform please post an issue. If you have feedback, or suggestions, please post an issue as well.

‎examples/golang_interop/Makefile‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ all:
66
@echo " then ./client server_address server_port server_public_key"
77
@echo "- make server"
88

9-
client:
9+
client: client.c
1010
cd ../.. && make && cp *.o $(curdir) && cd $(curdir)
11-
$(CC) client.c -fsanitize=address -I ../../lib -c -o client.o
12-
$(CC) -fsanitize=address client.o tweetdisco.o tweetstrobe.o tweet25519.o randombytes.o -o client
11+
$(CC) client.c -fsanitize=address,undefined -g -I ../../lib -c -o client.o
12+
$(CC) -fsanitize=address,undefined -g client.o disco_asymmetric.o tweetstrobe.o tweetX25519.o randombytes.o -o client
1313

1414
server:
1515
go run server.go
1616

1717
clean:
1818
rm *.o
19-
rm client
19+
rm client

‎examples/golang_interop/client.c‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ int main(int argc, char const *argv[]) {
7474

7575
// generate the first handshake message → e, es
7676
uint8_t out[500];
77-
ssize_t out_len =
78-
disco_WriteMessage(&hs_client, NULL, 0, out + 2, NULL, NULL);
79-
if (out_len < 0) {
77+
size_t out_len;
78+
bool ret =
79+
disco_WriteMessage(&hs_client, NULL, 0, out + 2, &out_len, NULL, NULL);
80+
if (!ret) {
8081
printf("can't generate first handshake message\n");
8182
return 1;
8283
}
@@ -132,9 +133,10 @@ int main(int argc, char const *argv[]) {
132133
printf("\nwe don't suppor this yet\n");
133134
return 1;
134135
}
135-
ssize_t payload_len =
136-
disco_ReadMessage(&hs_client, in + 2, in_len, payload, &c_write, &c_read);
137-
if (payload_len < 0) {
136+
size_t payload_len;
137+
ret = disco_ReadMessage(&hs_client, in + 2, in_len, payload, &payload_len,
138+
&c_write, &c_read);
139+
if (!ret) {
138140
printf("can't read handshake message\n");
139141
abort();
140142
}
@@ -147,18 +149,24 @@ int main(int argc, char const *argv[]) {
147149
// loop: receive a line from the terminal and send it to the server
148150
while (true) {
149151
// get line
150-
unsigned char *buffer = NULL;
151-
size_t size;
152-
getline(&buffer, &size, stdin);
152+
char *buffer = NULL;
153+
size_t size = 0;
154+
ssize_t nread = getline(&buffer, &size, stdin);
155+
if (nread < 0) {
156+
printf("error reading the line\n");
157+
return 1;
158+
}
153159
// encrypt
154-
out_len = sizeof(buffer) + 16; // plaintext + tag
155-
uint8_t *ct_and_mac = (uint8_t *)malloc(out_len + 2);
156-
memcpy(ct_and_mac + 2, buffer, sizeof(buffer));
157-
disco_EncryptInPlace(&c_write, ct_and_mac + 2, sizeof(buffer), out_len + 2);
160+
printf("sizeof(buffer)=%lu\n", nread);
161+
out_len = nread + 16; // plaintext + tag
162+
uint8_t *ct_and_mac = (uint8_t *)malloc(out_len + 2); // +2 (length)
163+
memcpy(ct_and_mac + 2, (uint8_t *)buffer, nread);
164+
disco_EncryptInPlace(&c_write, ct_and_mac + 2, nread, out_len);
158165

159166
// length framing
160167
ct_and_mac[0] = (out_len >> 8) & 0xFF;
161168
ct_and_mac[1] = out_len & 0xFF;
169+
printf("debug: %d, %d\n", ct_and_mac[0], ct_and_mac[1]);
162170

163171
// send
164172
sent = send(sock, ct_and_mac, out_len + 2, 0);
@@ -170,6 +178,7 @@ int main(int argc, char const *argv[]) {
170178

171179
// free
172180
free(buffer);
181+
free(ct_and_mac);
173182
}
174183

175184
//

‎lib/devurandom.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <fcntl.h>
22
#include <unistd.h>
3+
#include <stdint.h>
34

45
static int fd = -1;
56

6-
void randombytes(unsigned char *x, unsigned long long xlen) {
7+
void randombytes(uint8_t *x, uint64_t xlen) {
78
int i;
89

910
if (fd == -1) {

‎lib/disco_asymmetric.c‎

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,7 @@
3232
// ======
3333
// Used for key exchanges.
3434

35-
/**
36-
* disco_generateKeyPair can be used to generate a X25519 keypair. This is
37-
* useful for creating long-term keypairs for a peer.
38-
* @kp an initialized keyPair struct. It is over-written by the function.
39-
*/
40-
inline void disco_generateKeyPair(keyPair *kp) {
41-
crypto_box_keypair(kp->pub, kp->priv);
42-
kp->isSet = true; // TODO: is this useful? If it is, should we use a magic
43-
// number here in case it's not initialized to false?
44-
}
45-
46-
inline void DH(keyPair mine, keyPair theirs, uint8_t *output) {
35+
static inline void DH(keyPair mine, keyPair theirs, uint8_t *output) {
4736
crypto_scalarmult(output, mine.priv, theirs.pub);
4837
}
4938

@@ -53,33 +42,37 @@ inline void DH(keyPair mine, keyPair theirs, uint8_t *output) {
5342
// Refer to the Disco specification to understand the meaning of these
5443
// functions.
5544

56-
inline void initializeSymmetric(symmetricState *ss, const char *protocol_name,
57-
size_t protocol_name_len) {
45+
static inline void initializeSymmetric(symmetricState *ss,
46+
const char *protocol_name,
47+
size_t protocol_name_len) {
5848
strobe_init(&(ss->strobe), protocol_name, protocol_name_len);
5949
}
6050

61-
inline void mixKey(symmetricState *ss, uint8_t *input_key_material) {
51+
static inline void mixKey(symmetricState *ss, uint8_t *input_key_material) {
6252
strobe_operate(&(ss->strobe), TYPE_AD, input_key_material, 32, false);
6353
ss->isKeyed = true;
6454
}
6555

66-
inline void mixHash(symmetricState *ss, uint8_t *data, size_t data_len) {
56+
static inline void mixHash(symmetricState *ss, uint8_t *data, size_t data_len) {
6757
strobe_operate(&(ss->strobe), TYPE_AD, data, data_len, false);
6858
}
6959

70-
inline void mixKeyAndHash(symmetricState *ss, uint8_t *input_key_material) {
60+
/*
61+
static inline void mixKeyAndHash(symmetricState *ss,
62+
uint8_t *input_key_material) {
7163
strobe_operate(&(ss->strobe), TYPE_AD, input_key_material, 32, false);
7264
}
7365
74-
inline void getHandshakeHash(symmetricState *ss, uint8_t *result) {
66+
static inline void getHandshakeHash(symmetricState *ss, uint8_t *result) {
7567
strobe_operate(&(ss->strobe), TYPE_PRF, result, 32, false);
7668
}
69+
*/
7770

7871
// note that this function modifies the plaintext in place, and requires the
7972
// plaintext buffer to have 16 more bytes of capacity for the authentication tag
8073
// if the symmetric state is keyed
81-
inline void encryptAndHash(symmetricState *ss, uint8_t *plaintext,
82-
size_t plaintext_len) {
74+
static inline void encryptAndHash(symmetricState *ss, uint8_t *plaintext,
75+
size_t plaintext_len) {
8376
if (!ss->isKeyed) {
8477
strobe_operate(&(ss->strobe), TYPE_CLR, plaintext, plaintext_len, false);
8578
} else {
@@ -91,8 +84,8 @@ inline void encryptAndHash(symmetricState *ss, uint8_t *plaintext,
9184

9285
// note that the decryption occurs in place, and the the result is
9386
// `ciphertext_len-16` in case the symmetric state is keyed.
94-
inline bool decryptAndHash(symmetricState *ss, uint8_t *ciphertext,
95-
size_t ciphertext_len) {
87+
static inline bool decryptAndHash(symmetricState *ss, uint8_t *ciphertext,
88+
size_t ciphertext_len) {
9689
if (!ss->isKeyed) {
9790
return strobe_operate(&(ss->strobe), TYPE_CLR | FLAG_I, ciphertext,
9891
ciphertext_len, false);
@@ -200,11 +193,9 @@ void disco_Initialize(handshakeState *hs, const char *handshake_pattern,
200193

201194
// handshake_pattern is of the following form:
202195
// "protocol_name \x00 pre-message patterns \x00 message patterns"
203-
printf("debug1:%s\n", handshake_pattern);
204196
initializeSymmetric(&(hs->symmetric_state), handshake_pattern,
205197
strlen(handshake_pattern));
206198
handshake_pattern = handshake_pattern + strlen(handshake_pattern) + 1;
207-
printf("debug2:%s\n", handshake_pattern);
208199

209200
hs->symmetric_state.isKeyed = false;
210201

‎lib/disco_asymmetric.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define __DISCO_H__
1313

1414
#include "tweetstrobe.h"
15+
#include "tweetX25519.h"
1516

1617
#include <stdlib.h>
1718
#include <stdint.h>
@@ -80,8 +81,12 @@ typedef struct handshakeState_ {
8081
// Public API
8182
// ==========
8283

83-
// used to generate long-term key pairs
84-
void disco_generateKeyPair(keyPair *kp);
84+
// used to generate long-term key pairs for a peer
85+
inline void disco_generateKeyPair(keyPair *kp) {
86+
crypto_box_keypair(kp->pub, kp->priv);
87+
kp->isSet = true; // TODO: is this useful? If it is, should we use a magic
88+
// number here in case it's not initialized to false?
89+
}
8590

8691
// used to initialized your handshakeState with a handshake pattern
8792
void disco_Initialize(handshakeState *hs, const char *handshake_pattern,

0 commit comments

Comments
 (0)