Restructuring of noise impl.

This commit is contained in:
Mathias Hall-Andersen
2017-06-24 15:34:17 +02:00
parent 521e77fd54
commit 25190e4336
10 changed files with 420 additions and 128 deletions
+82
View File
@@ -0,0 +1,82 @@
package main
import (
"crypto/rand"
"sync"
)
/* Index=0 is reserved for unset indecies
*
*/
type IndexTable struct {
mutex sync.RWMutex
keypairs map[uint32]*KeyPair
handshakes map[uint32]*Handshake
}
func randUint32() (uint32, error) {
var buff [4]byte
_, err := rand.Read(buff[:])
id := uint32(buff[0])
id <<= 8
id |= uint32(buff[1])
id <<= 8
id |= uint32(buff[2])
id <<= 8
id |= uint32(buff[3])
return id, err
}
func (table *IndexTable) Init() {
table.mutex.Lock()
defer table.mutex.Unlock()
table.keypairs = make(map[uint32]*KeyPair)
table.handshakes = make(map[uint32]*Handshake)
}
func (table *IndexTable) NewIndex(handshake *Handshake) (uint32, error) {
table.mutex.Lock()
defer table.mutex.Unlock()
for {
// generate random index
id, err := randUint32()
if err != nil {
return id, err
}
if id == 0 {
continue
}
// check if index used
_, ok := table.keypairs[id]
if ok {
continue
}
_, ok = table.handshakes[id]
if ok {
continue
}
// update the index
delete(table.handshakes, handshake.localIndex)
handshake.localIndex = id
table.handshakes[id] = handshake
return id, nil
}
}
func (table *IndexTable) LookupKeyPair(id uint32) *KeyPair {
table.mutex.RLock()
defer table.mutex.RUnlock()
return table.keypairs[id]
}
func (table *IndexTable) LookupHandshake(id uint32) *Handshake {
table.mutex.RLock()
defer table.mutex.RUnlock()
return table.handshakes[id]
}