|
6 | 6 | package device
|
7 | 7 |
|
8 | 8 | import (
|
| 9 | + "encoding/binary" |
9 | 10 | "errors"
|
10 | 11 | "fmt"
|
11 | 12 | "sync"
|
@@ -115,6 +116,53 @@ type MessageCookieReply struct {
|
115 | 116 | Cookie [blake2s.Size128 + poly1305.TagSize]byte
|
116 | 117 | }
|
117 | 118 |
|
| 119 | +var errMessageTooShort = errors.New("message too short") |
| 120 | + |
| 121 | +func (msg *MessageInitiation) unmarshal(b []byte) error { |
| 122 | + if len(b) < MessageInitiationSize { |
| 123 | + return errMessageTooShort |
| 124 | + } |
| 125 | + |
| 126 | + msg.Type = binary.LittleEndian.Uint32(b) |
| 127 | + msg.Sender = binary.LittleEndian.Uint32(b[4:]) |
| 128 | + copy(msg.Ephemeral[:], b[8:]) |
| 129 | + copy(msg.Static[:], b[8+len(msg.Ephemeral):]) |
| 130 | + copy(msg.Timestamp[:], b[8+len(msg.Ephemeral)+len(msg.Static):]) |
| 131 | + copy(msg.MAC1[:], b[8+len(msg.Ephemeral)+len(msg.Static)+len(msg.Timestamp):]) |
| 132 | + copy(msg.MAC2[:], b[8+len(msg.Ephemeral)+len(msg.Static)+len(msg.Timestamp)+len(msg.MAC1):]) |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func (msg *MessageResponse) unmarshal(b []byte) error { |
| 138 | + if len(b) < MessageResponseSize { |
| 139 | + return errMessageTooShort |
| 140 | + } |
| 141 | + |
| 142 | + msg.Type = binary.LittleEndian.Uint32(b) |
| 143 | + msg.Sender = binary.LittleEndian.Uint32(b[4:]) |
| 144 | + msg.Receiver = binary.LittleEndian.Uint32(b[8:]) |
| 145 | + copy(msg.Ephemeral[:], b[12:]) |
| 146 | + copy(msg.Empty[:], b[12+len(msg.Ephemeral):]) |
| 147 | + copy(msg.MAC1[:], b[12+len(msg.Ephemeral)+len(msg.Empty):]) |
| 148 | + copy(msg.MAC2[:], b[12+len(msg.Ephemeral)+len(msg.Empty)+len(msg.MAC1):]) |
| 149 | + |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func (msg *MessageCookieReply) unmarshal(b []byte) error { |
| 154 | + if len(b) < MessageCookieReplySize { |
| 155 | + return errMessageTooShort |
| 156 | + } |
| 157 | + |
| 158 | + msg.Type = binary.LittleEndian.Uint32(b) |
| 159 | + msg.Receiver = binary.LittleEndian.Uint32(b[4:]) |
| 160 | + copy(msg.Nonce[:], b[8:]) |
| 161 | + copy(msg.Cookie[:], b[8+len(msg.Nonce):]) |
| 162 | + |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
118 | 166 | type Handshake struct {
|
119 | 167 | state handshakeState
|
120 | 168 | mutex sync.RWMutex
|
|
0 commit comments