@@ -2,7 +2,6 @@ package otr3
2
2
3
3
import (
4
4
"crypto/hmac"
5
- "crypto/sha256"
6
5
"crypto/subtle"
7
6
"io"
8
7
"math/big"
@@ -13,10 +12,13 @@ type ake struct {
13
12
ourPublicValue * big.Int
14
13
theirPublicValue * big.Int
15
14
15
+ // TODO: why this number here?
16
16
r [16 ]byte
17
17
18
18
encryptedGx []byte
19
- hashedGx [sha256 .Size ]byte
19
+
20
+ // SIZE: this should always be version.hash2Length
21
+ xhashedGx []byte
20
22
21
23
revealKey akeKeys
22
24
sigKey akeKeys
@@ -40,7 +42,7 @@ func (c *Conversation) initAKE() {
40
42
}
41
43
42
44
func (c * Conversation ) calcAKEKeys (s * big.Int ) {
43
- c .ssid , c .ake .revealKey , c .ake .sigKey = calculateAKEKeys (s )
45
+ c .ssid , c .ake .revealKey , c .ake .sigKey = calculateAKEKeys (s , c . version )
44
46
}
45
47
46
48
func (c * Conversation ) setSecretExponent (val * big.Int ) {
@@ -53,9 +55,9 @@ func (c *Conversation) calcDHSharedSecret() *big.Int {
53
55
}
54
56
55
57
func (c * Conversation ) generateEncryptedSignature (key * akeKeys ) ([]byte , error ) {
56
- verifyData := appendAll (c .ake .ourPublicValue , c .ake .theirPublicValue , & c . ourKey .PublicKey , c .ake .keys .ourKeyID )
58
+ verifyData := appendAll (c .ake .ourPublicValue , c .ake .theirPublicValue , c . ourCurrentKey .PublicKey () , c .ake .keys .ourKeyID )
57
59
58
- mb := sumHMAC (key .m1 [:] , verifyData )
60
+ mb := sumHMAC (key .m1 , verifyData , c . version )
59
61
xb , err := c .calcXb (key , mb )
60
62
61
63
if err != nil {
@@ -64,15 +66,24 @@ func (c *Conversation) generateEncryptedSignature(key *akeKeys) ([]byte, error)
64
66
65
67
return appendData (nil , xb ), nil
66
68
}
67
- func appendAll (one , two * big.Int , publicKey * PublicKey , keyID uint32 ) []byte {
69
+ func appendAll (one , two * big.Int , publicKey PublicKey , keyID uint32 ) []byte {
68
70
return appendWord (append (appendMPI (appendMPI (nil , one ), two ), publicKey .serialize ()... ), keyID )
69
71
}
70
72
73
+ func fixedSize (s int , v []byte ) []byte {
74
+ if len (v ) < s {
75
+ vv := make ([]byte , s )
76
+ copy (vv , v )
77
+ return vv
78
+ }
79
+ return v
80
+ }
81
+
71
82
func (c * Conversation ) calcXb (key * akeKeys , mb []byte ) ([]byte , error ) {
72
- xb := c .ourKey .PublicKey .serialize ()
83
+ xb := c .ourCurrentKey .PublicKey () .serialize ()
73
84
xb = appendWord (xb , c .ake .keys .ourKeyID )
74
85
75
- sigb , err := c .ourKey .Sign (c .rand (), mb )
86
+ sigb , err := c .ourCurrentKey .Sign (c .rand (), mb )
76
87
if err == io .ErrUnexpectedEOF {
77
88
return nil , errShortRandomRead
78
89
}
@@ -82,7 +93,7 @@ func (c *Conversation) calcXb(key *akeKeys, mb []byte) ([]byte, error) {
82
93
}
83
94
84
95
// this error can't happen, since key.c is fixed to the correct size
85
- xb , _ = encrypt (key .c [:] , append (xb , sigb ... ))
96
+ xb , _ = encrypt (fixedSize ( c . version . keyLength (), key .c ) , append (xb , sigb ... ))
86
97
87
98
return xb , nil
88
99
}
@@ -93,6 +104,7 @@ func (c *Conversation) dhCommitMessage() ([]byte, error) {
93
104
c .initAKE ()
94
105
c .ake .keys .ourKeyID = 0
95
106
107
+ // TODO: where does this 40 come from?
96
108
x , err := c .randMPI (make ([]byte , 40 ))
97
109
if err != nil {
98
110
return nil , err
@@ -113,7 +125,7 @@ func (c *Conversation) dhCommitMessage() ([]byte, error) {
113
125
func (c * Conversation ) serializeDHCommit (public * big.Int ) []byte {
114
126
dhCommitMsg := dhCommit {
115
127
encryptedGx : c .ake .encryptedGx ,
116
- hashedGx : sha256 . Sum256 (appendMPI (nil , public )),
128
+ yhashedGx : c . version . hash2 (appendMPI (nil , public )),
117
129
}
118
130
return dhCommitMsg .serialize ()
119
131
}
@@ -123,6 +135,7 @@ func (c *Conversation) serializeDHCommit(public *big.Int) []byte {
123
135
func (c * Conversation ) dhKeyMessage () ([]byte , error ) {
124
136
c .initAKE ()
125
137
138
+ // TODO: where does this 40 come from?
126
139
y , err := c .randMPI (make ([]byte , 40 )[:])
127
140
128
141
if err != nil {
@@ -154,14 +167,14 @@ func (c *Conversation) revealSigMessage() ([]byte, error) {
154
167
return nil , err
155
168
}
156
169
157
- macSig := sumHMAC (c .ake .revealKey .m2 [:] , encryptedSig )
170
+ macSig := sumHMAC (c .ake .revealKey .m2 , encryptedSig , c . version )
158
171
revealSigMsg := revealSig {
159
172
r : c .ake .r ,
160
173
encryptedSig : encryptedSig ,
161
174
macSig : macSig ,
162
175
}
163
176
164
- return revealSigMsg .serialize (), nil
177
+ return revealSigMsg .serialize (c . version ), nil
165
178
}
166
179
167
180
// sigMessage = alice = y
@@ -174,13 +187,13 @@ func (c *Conversation) sigMessage() ([]byte, error) {
174
187
return nil , err
175
188
}
176
189
177
- macSig := sumHMAC (c .ake .sigKey .m2 [:] , encryptedSig )
190
+ macSig := sumHMAC (c .ake .sigKey .m2 , encryptedSig , c . version )
178
191
sigMsg := sig {
179
192
encryptedSig : encryptedSig ,
180
193
macSig : macSig ,
181
194
}
182
195
183
- return sigMsg .serialize (), nil
196
+ return sigMsg .serialize (c . version ), nil
184
197
}
185
198
186
199
// processDHCommit = alice = y
@@ -193,7 +206,7 @@ func (c *Conversation) processDHCommit(msg []byte) error {
193
206
}
194
207
195
208
c .ake .encryptedGx = dhCommitMsg .encryptedGx
196
- c .ake .hashedGx = dhCommitMsg .hashedGx
209
+ c .ake .xhashedGx = dhCommitMsg .yhashedGx
197
210
198
211
return err
199
212
}
@@ -225,7 +238,7 @@ func (c *Conversation) processDHKey(msg []byte) (isSame bool, err error) {
225
238
// Bob ---- Reveal Signature ----> Alice
226
239
func (c * Conversation ) processRevealSig (msg []byte ) (err error ) {
227
240
revealSigMsg := revealSig {}
228
- err = revealSigMsg .deserialize (msg )
241
+ err = revealSigMsg .deserialize (msg , c . version )
229
242
if err != nil {
230
243
return
231
244
}
@@ -239,7 +252,7 @@ func (c *Conversation) processRevealSig(msg []byte) (err error) {
239
252
return
240
253
}
241
254
242
- if err = checkDecryptedGx (decryptedGx , c .ake .hashedGx [:] ); err != nil {
255
+ if err = checkDecryptedGx (decryptedGx , c .ake .xhashedGx , c . version ); err != nil {
243
256
return
244
257
}
245
258
@@ -287,9 +300,10 @@ func (c *Conversation) checkedSignatureVerification(mb, sig []byte) error {
287
300
return nil
288
301
}
289
302
290
- func verifyEncryptedSignatureMAC (encryptedSig []byte , theirMAC []byte , keys * akeKeys ) error {
303
+ func verifyEncryptedSignatureMAC (encryptedSig []byte , theirMAC []byte , keys * akeKeys , v otrVersion ) error {
291
304
tomac := appendData (nil , encryptedSig )
292
- myMAC := sumHMAC (keys .m2 [:], tomac )[:20 ]
305
+
306
+ myMAC := sumHMAC (keys .m2 , tomac , v )[:v .truncateLength ()]
293
307
294
308
if len (myMAC ) != len (theirMAC ) || subtle .ConstantTimeCompare (myMAC , theirMAC ) == 0 {
295
309
return newOtrError ("bad signature MAC in encrypted signature" )
@@ -299,8 +313,9 @@ func verifyEncryptedSignatureMAC(encryptedSig []byte, theirMAC []byte, keys *ake
299
313
}
300
314
301
315
func (c * Conversation ) parseTheirKey (key []byte ) (sig []byte , keyID uint32 , err error ) {
302
- c .theirKey = & PublicKey {}
303
- rest , ok1 := c .theirKey .Parse (key )
316
+ var rest []byte
317
+ var ok1 bool
318
+ rest , ok1 , c .theirKey = ParsePublicKey (key )
304
319
sig , keyID , ok2 := extractWord (rest )
305
320
306
321
if ! ok1 || ! ok2 {
@@ -312,16 +327,16 @@ func (c *Conversation) parseTheirKey(key []byte) (sig []byte, keyID uint32, err
312
327
313
328
func (c * Conversation ) expectedMessageHMAC (keyID uint32 , keys * akeKeys ) []byte {
314
329
verifyData := appendAll (c .ake .theirPublicValue , c .ake .ourPublicValue , c .theirKey , keyID )
315
- return sumHMAC (keys .m1 [:] , verifyData )
330
+ return sumHMAC (keys .m1 , verifyData , c . version )
316
331
}
317
332
318
333
func (c * Conversation ) processEncryptedSig (encryptedSig []byte , theirMAC []byte , keys * akeKeys ) error {
319
- if err := verifyEncryptedSignatureMAC (encryptedSig , theirMAC , keys ); err != nil {
334
+ if err := verifyEncryptedSignatureMAC (encryptedSig , theirMAC , keys , c . version ); err != nil {
320
335
return err
321
336
}
322
337
323
338
decryptedSig := encryptedSig
324
- if err := decrypt (keys .c [:] , decryptedSig , encryptedSig ); err != nil {
339
+ if err := decrypt (fixedSize ( c . version . keyLength (), keys .c ) , decryptedSig , encryptedSig ); err != nil {
325
340
return err
326
341
}
327
342
@@ -353,14 +368,14 @@ func extractGx(decryptedGx []byte) (*big.Int, error) {
353
368
return gx , nil
354
369
}
355
370
356
- func sumHMAC (key , data []byte ) []byte {
357
- mac := hmac .New (sha256 . New , key )
371
+ func sumHMAC (key , data []byte , v otrVersion ) []byte {
372
+ mac := hmac .New (v . hash2Instance , key )
358
373
mac .Write (data )
359
374
return mac .Sum (nil )
360
375
}
361
376
362
- func checkDecryptedGx (decryptedGx , hashedGx []byte ) error {
363
- digest := sha256 . Sum256 (decryptedGx )
377
+ func checkDecryptedGx (decryptedGx , hashedGx []byte , v otrVersion ) error {
378
+ digest := v . hash2 (decryptedGx )
364
379
365
380
if subtle .ConstantTimeCompare (digest [:], hashedGx [:]) == 0 {
366
381
return newOtrError ("bad commit MAC in reveal signature message" )
0 commit comments