34 lines
877 B
Go
34 lines
877 B
Go
package station
|
|
|
|
import (
|
|
"encoding/binary"
|
|
|
|
"gitea.neitzel.de/konrad/funknetz/internal/gcm64"
|
|
)
|
|
|
|
func counterAAD(counter uint32) []byte {
|
|
var b [4]byte
|
|
binary.BigEndian.PutUint32(b[:], counter)
|
|
return b[:]
|
|
}
|
|
|
|
func registrationAAD(clientID uint16) []byte {
|
|
b := counterAAD(0)
|
|
return appendU16(b, clientID)
|
|
}
|
|
|
|
func sealDatagram(key, prefix []byte, clientID uint16, counter uint32, aad, plain []byte) ([]byte, error) {
|
|
tag, ct, err := gcm64.Seal(key, gcm64.IV(prefix, clientID, counter), plain, aad)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
wire := make([]byte, 0, len(aad)+len(tag)+len(ct))
|
|
wire = append(wire, aad...)
|
|
wire = append(wire, tag...)
|
|
return append(wire, ct...), nil
|
|
}
|
|
|
|
func openDatagram(key, prefix []byte, clientID uint16, counter uint32, aad, tag, ct []byte) ([]byte, error) {
|
|
return gcm64.Open(key, gcm64.IV(prefix, clientID, counter), tag, ct, aad)
|
|
}
|