-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffie.go
279 lines (227 loc) · 9.03 KB
/
diffie.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Implementation of Diffie Hellman for mutual authentication and key establishment.
package main
import (
"bytes"
"bufio"
"crypto"
"crypto/sha256"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"io/ioutil"
"fmt"
"math/big"
"net"
"os"
)
const (
dhNonceLen = 32 // according to RFC 2409 nonce should be between 8 and 256 bytes
dhExponentLen = 64 // 512 bit number should provide 256-bit security
rsaLen = 384 // Length of RSA encrptions and signatures assuming 3072-bit key
)
// Prime and generator for DH Group 14 NOTE: The Group 15 prime is too large for RSA to handle (when sending partial keys)
const (
generator = 2
prime = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF"
)
var g *big.Int
var p *big.Int
var publicKey *rsa.PublicKey
var privateKey *rsa.PrivateKey
var nonce []byte
var secretExp big.Int
var sessionKey []byte
// prepDHValues prepares values needed for the Diffie Hellman exchange,
// parsing them into the desired format. Specifically, the prime and generator
// are converted into big Ints in preparation for doing big math, and the RSA
// keys are extracted from the provided files.
func prepDHValues() {
// 1. parse prime and generator into big Ints
g = big.NewInt(generator)
bytes, _ := hex.DecodeString(prime)
p = new(big.Int)
p.SetBytes(bytes)
// 2. Parse public and private key files into RSA key format
var err error
privBytes := parseKeyFile(*privKeyFile)
privateKey, err = x509.ParsePKCS1PrivateKey(privBytes)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to parse private key: " + err.Error())
os.Exit(1)
}
if privateKey.N.BitLen() != 3072 {
fmt.Fprintln(os.Stderr, "Private key provided is not 3072-bit.")
os.Exit(1)
}
pubBytes := parseKeyFile(*pubKeyFile)
parsed, err := x509.ParsePKIXPublicKey(pubBytes)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to parse public key: " + err.Error())
os.Exit(1)
}
var typeOK bool
publicKey, typeOK = parsed.(*rsa.PublicKey)
if !typeOK {
fmt.Fprintln(os.Stderr, "Public key is of wrong type. Please make sure you use RSA keys.")
os.Exit(1)
}
if publicKey.N.BitLen() != 3072 {
fmt.Fprintln(os.Stderr, "Public key provided is not 3072-bit.")
os.Exit(1)
}
}
// computeSessionKey computes (g^bmodp)^amodp, where g^bmodp is the partial
// key received from the peer user, and a is our secret exponent. The result is
// saved direrctly in the global byte slice sessionKey.
func computeSessionKey(peerPartialKey *big.Int) {
key := new(big.Int)
key.Exp(peerPartialKey, &secretExp, p)
sessionKey = key.Bytes()
}
// computePartialKey computes g^amodp and returns the result as a byte slice.
func computePartialKey() []byte {
secretExp = generateExponent()
key := new(big.Int)
key.Exp(g, &secretExp, p)
return key.Bytes()
}
// parseKeyFile reads the content of the specified file and attempts to
// decode it into a PEM block and extract the key bytes.
func parseKeyFile(keyFile string) []byte {
data, err := ioutil.ReadFile(keyFile)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading key file: " + err.Error())
os.Exit(1)
}
if len(data) == 0 {
fmt.Fprintln(os.Stderr, "Key file is empty.")
os.Exit(1)
}
keyBlock, _ := pem.Decode(data)
if keyBlock == nil {
fmt.Fprintln(os.Stderr, "Could not extract PEM block.")
os.Exit(1)
}
if x509.IsEncryptedPEMBlock(keyBlock) {
fmt.Fprintln(os.Stderr, "Could not extract key. PEM file is encrypted with a passcode.")
os.Exit(1)
}
return keyBlock.Bytes
}
// generateExponent generates a random secret exponent to be used in the Diffie
// Hellman key exchange.
func generateExponent() big.Int {
bytes := genRandomBytes(dhExponentLen)
exponent := new(big.Int)
exponent.SetBytes(bytes)
return *exponent
}
// checkNonce verifies that the received value matches the recorded nonce. If
// that is not the case, authentication has failed, and we exit immediately.
func checkNonce(received []byte) {
if !bytes.Equal(received, nonce) {
fmt.Fprintln(os.Stderr, "Challenge response was incorrect!")
os.Exit(1)
}
}
// parseDHMessage takes a cipertext message which is expected to contain a nonce
// response and partial key. It checks that the nonce response matches the one
// that was sent, and uses the peer partial key to compute the session key.
func parseDHMessage(message *[]byte) {
plaintext := verifyAndDecrypt(message)
nonceResponse := plaintext[:dhNonceLen]
checkNonce(nonceResponse)
peerPartialKey := plaintext[dhNonceLen:]
peerPartialKeyInt := new(big.Int)
peerPartialKeyInt.SetBytes(peerPartialKey)
computeSessionKey(peerPartialKeyInt)
}
// encryptAndSign uses the peer's public key to encrypt using RSA-OAEP and our
// private key to sign using RSASSA-PKCS1-V1_5-SIGN. It returns the appended
// ciphertext and signature.
func encryptAndSign(plaintext *[]byte) []byte {
rng := rand.Reader
ciphertext, err := rsa.EncryptOAEP(sha256.New(), rng, publicKey, *plaintext, []byte("auth"))
if err != nil {
fmt.Fprintf(os.Stderr, "Error from encryption: %s\n", err)
os.Exit(1)
}
hashed := sha256.Sum256(ciphertext)
signature, err := rsa.SignPKCS1v15(rng, privateKey, crypto.SHA256, hashed[:])
if err != nil {
fmt.Fprintf(os.Stderr, "Error from signing: %s\n", err)
os.Exit(1)
}
return append(ciphertext[:], signature[:]...)
}
// verifyAndDecrypt splits the given message into cipertext and signature. It
// verifies the signature and if all is good, returns the decrypted message.
func verifyAndDecrypt(message *[]byte) []byte {
rng := rand.Reader
// first half is the message second half is the signature
ciphertext := (*message)[:rsaLen]
signature := (*message)[rsaLen:]
plaintext, err := rsa.DecryptOAEP(sha256.New(), rng, privateKey, ciphertext, []byte("auth"))
if err != nil {
fmt.Fprintf(os.Stderr, "Error from decryption: %s\n", err)
os.Exit(1)
}
hashed := sha256.Sum256(ciphertext)
err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hashed[:], signature)
if err != nil {
fmt.Fprintf(os.Stderr, "Error from verification: %s\n", err)
os.Exit(1)
}
return plaintext
}
// mutualAuth prompts an exchange of messages as defined by the Diffie
// Hellman algorithm. Both sides are authenticated to each other, and
// they establish a shared secret symmetric key to use for future communication
func keyExchange(conn net.Conn) {
reader := bufio.NewReader(conn)
if *mode == "server" {
// Step 1: Receive nonce challenge from peer
nonceChallenge := make([]byte, dhNonceLen)
receiveIncoming(reader, &nonceChallenge)
if *debug {
fmt.Printf("Received nonce challenge: %x\n", nonceChallenge[:])
}
// Step 2: Encrypt and sign the received challenge and our partial key
partialKey := computePartialKey()
message := append(nonceChallenge[:], partialKey[:]...)
encrypted := encryptAndSign(&message)
// generate and send our own nonce challenge along with the encypted message
nonce = genRandomBytes(dhNonceLen)
nonceAndEncrypted := append(nonce[:], encrypted[:]...)
sendOutgoing(conn, nonceAndEncrypted)
// Step 3: Receive response containing nonce to check and peer partial key
responseLen := 2 * rsaLen
response := make([]byte, responseLen)
receiveIncoming(reader, &response)
parseDHMessage(&response)
} else { // client mode
// Step 1: Generate and send a nonce challenge
nonce = genRandomBytes(dhNonceLen)
sendOutgoing(conn, nonce)
if *debug {
fmt.Printf("Sent nonce challenge: %x\n", nonce)
}
// Step 2: Receive challenge from peer + response containing nonce to check and peer partial key
responseLen := (2 * rsaLen) + dhNonceLen
response := make([]byte, responseLen)
receiveIncoming(reader, &response)
nonceChallenge := response[:dhNonceLen]
ciphertext := response[dhNonceLen:]
partialKey := computePartialKey()
parseDHMessage(&ciphertext)
// Step 3: Encrypt and sign the received challenge and our partial key, send
message := append(nonceChallenge[:], partialKey[:]...)
encrypted := encryptAndSign(&message)
sendOutgoing(conn, encrypted)
}
if *debug {
fmt.Printf("SESSION KEY: %x\n", sessionKey)
}
}