This repository was archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprotocol.c
More file actions
executable file
·151 lines (112 loc) · 6.35 KB
/
protocol.c
File metadata and controls
executable file
·151 lines (112 loc) · 6.35 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
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "protocol.h"
void senderHello(field_t *restrict sendPacket, digit_t *restrict senderModExp, digit_t *restrict senderSecret) {
digit_t modExpResult[ENC_PRIVATE_KEY_DIGITS];
// Generate x
getRandomDigit(senderSecret);
// Calculate alpha^x mod p = generator^senderSecret mod prime
mpModExp(modExpResult, Enc_GeneratorDigits, senderSecret, Enc_PrimeDigits, ENC_PRIVATE_KEY_DIGITS);
sendPacket[0] = 0x00;
memcpy(sendPacket+1, modExpResult, ENC_PRIVATE_KEY_CHARS);
memcpy(senderModExp, modExpResult, ENC_PRIVATE_KEY_DIGITS*sizeof(digit_t));
}
int receiverHello(field_t *restrict sendPacket, digit_t *restrict receiverModExp, field_t *restrict receivedPacket, digit_t *restrict receiverSecret, digit_t *restrict senderModExp, unsigned char *restrict receiverPrivateExp) {
if (0x00 != receivedPacket[0])
return ENC_REJECT_PACKET_TAG;
unsigned char cSignature[ENC_ENCRYPTED_SIGNATURE_CHARS];
digit_t exponent[ENC_PRIVATE_KEY_DIGITS];
digit_t signature[ENC_SIGNATURE_DIGITS];
digit_t signatureMessage[2*ENC_PRIVATE_KEY_DIGITS];
field_t encryptedSignature[ENC_ENCRYPTED_SIGNATURE_CHARS];
uint8_t receiverCTRNonce[ENC_CTR_NONCE_CHARS];
uint8_t receiverAESKey[ENC_AES_KEY_CHARS];
// Generate y
getRandomDigit(receiverSecret);
// Calculate alpha^y mod p = alpha^receiverSecret mod prime
mpModExp(receiverModExp, Enc_GeneratorDigits, receiverSecret, Enc_PrimeDigits, ENC_PRIVATE_KEY_DIGITS);
// Concatenate alpha^y | alpha^x
memcpy(senderModExp, receivedPacket+1, ENC_PRIVATE_KEY_CHARS);
memcpy(signatureMessage, senderModExp, ENC_PRIVATE_KEY_DIGITS*sizeof(digit_t));
memcpy(signatureMessage+ENC_PRIVATE_KEY_DIGITS, receiverModExp, ENC_PRIVATE_KEY_DIGITS*sizeof(digit_t));
// Derive Keys
receiver_deriveKey(receiverAESKey, receiverCTRNonce, senderModExp);
// Create Signature
memset(signature, 0, sizeof(signature));
mpConvFromOctets(exponent, ENC_SIGNATURE_DIGITS, receiverPrivateExp, ENC_PRIVATE_KEY_CHARS);
_sign_crt(signature, signatureMessage, exponent, Enc_ReceiverPrimeOneDigits, Enc_ReceiverPrimeTwoDigits);
#ifndef __ENC_NO_ENCRYPTION_PRINTS__
printf("---| signature\n");
mpPrintNL(signature, ENC_SIGNATURE_DIGITS);
#endif
// Encrypt Signature
mpConvToOctets(signature, ENC_SIGNATURE_DIGITS, cSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
_encryptData(encryptedSignature, receiverAESKey, receiverCTRNonce, 0, cSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
#ifndef __ENC_NO_ENCRYPTION_PRINTS__
printf("---| encyptedSignature\n");
mpConvFromOctets(signature, ENC_ENCRYPTED_SIGNATURE_DIGITS, encryptedSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
mpPrintNL(signature, ENC_SIGNATURE_DIGITS);
#endif
sendPacket[0] = 0x01;
memcpy(sendPacket+1, receiverModExp, ENC_PRIVATE_KEY_DIGITS*sizeof(digit_t));
memcpy(sendPacket+ENC_PRIVATE_KEY_CHARS+1, encryptedSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
return ENC_ACCEPT_PACKET;
}
int senderAcknowledge(field_t *restrict sendPacket, field_t *restrict receivedPacket, digit_t *restrict senderSecret, digit_t *restrict receiverModExp, digit_t *restrict senderModExp, unsigned char *restrict senderPrivateExp) {
if (0x01 != receivedPacket[0])
return ENC_REJECT_PACKET_TAG;
unsigned char cSignature[ENC_ENCRYPTED_SIGNATURE_CHARS];
digit_t exponent[ENC_PRIVATE_KEY_DIGITS];
digit_t signature[ENC_SIGN_MODULUS_DIGITS];
digit_t signatureMessageDigits[2*ENC_PRIVATE_KEY_DIGITS];
field_t encryptedSignature[ENC_ENCRYPTED_SIGNATURE_CHARS];
uint8_t signatureMessage[2*ENC_PRIVATE_KEY_CHARS];
uint8_t senderCTRNonce[ENC_CTR_NONCE_CHARS];
uint8_t senderAESKey[ENC_AES_KEY_CHARS];
mpSetZero(signature, ENC_SIGN_MODULUS_DIGITS);
// Concatenate alpha^y | alpha^x
memcpy(receiverModExp, receivedPacket+1, ENC_PRIVATE_KEY_CHARS);
memcpy(signatureMessageDigits, senderModExp, ENC_PRIVATE_KEY_DIGITS*sizeof(digit_t));
memcpy(signatureMessageDigits+ENC_PRIVATE_KEY_DIGITS, receiverModExp, ENC_PRIVATE_KEY_DIGITS*sizeof(digit_t));
//deriveKey from receiverModExp
sender_deriveKey(senderAESKey, senderCTRNonce, receiverModExp);
// Decrypt signature
memcpy(encryptedSignature, receivedPacket+ENC_PRIVATE_KEY_CHARS+1, ENC_ENCRYPTED_SIGNATURE_CHARS);
#ifndef __ENC_NO_ENCRYPTION_PRINTS__
printf("---| encryptedSignature\n");
mpConvFromOctets(signature, ENC_ENCRYPTED_SIGNATURE_DIGITS, encryptedSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
mpPrintNL(signature, ENC_SIGNATURE_DIGITS);
#endif
_decryptData(cSignature, senderAESKey, senderCTRNonce, 0, (unsigned char *) encryptedSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
// Verify signature
mpConvFromOctets(signature, ENC_ENCRYPTED_SIGNATURE_DIGITS, cSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
#ifndef __ENC_NO_ENCRYPTION_PRINTS__
printf("---| signature\n");
mpPrintNL(signature, ENC_SIGN_MODULUS_DIGITS);
#endif
mpConvToOctets(signatureMessageDigits, ENC_PRIVATE_KEY_DIGITS*2, signatureMessage, ENC_PRIVATE_KEY_CHARS*2);
if (!_verify(signature, signatureMessage, Enc_PublicExpDigits, Enc_ReceiverModulusDigits))
return ENC_REJECT_PACKET_SIGNATURE;
// Concatenate alpha^x | alpha^y
memcpy(signatureMessageDigits, receiverModExp, ENC_PRIVATE_KEY_CHARS);
memcpy(signatureMessageDigits+ENC_PRIVATE_KEY_DIGITS, senderModExp, ENC_PRIVATE_KEY_CHARS);
// Create Signature
mpConvFromOctets(exponent, ENC_SIGNATURE_DIGITS, senderPrivateExp, ENC_PRIVATE_KEY_CHARS);
_sign_crt(signature, signatureMessageDigits, exponent, Enc_SenderPrimeOneDigits, Enc_SenderPrimeTwoDigits);
// Encrypt signature
memset(cSignature, 0, sizeof(cSignature));
mpConvToOctets(signature, ENC_SIGNATURE_DIGITS, cSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
_encryptData(encryptedSignature, senderAESKey, senderCTRNonce, 0, cSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
sendPacket[0] = 0x01;
mpConvFromOctets(signature, ENC_SIGNATURE_DIGITS, encryptedSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
memcpy(sendPacket+1, encryptedSignature, ENC_ENCRYPTED_SIGNATURE_CHARS);
return ENC_ACCEPT_PACKET;
}
int increaseCounter(uint32_t *counter) {
uint32_t nextValue = *counter + 1;
if (*counter > nextValue) {
*counter = nextValue;
return ENC_COUNTER_WRAPAROUND;
} else {
*counter = nextValue;
return ENC_ACCEPT_PACKET;
}
}