The following is a quoted description of the protocol as described by Nash Kaminski. See https://kaminski.io Full credit to Mr. Kaminski for his work in explaining the details of creating a CSRMesh packet.
"The 128 bit network key used in CSRMesh networks is derived by concatenating the ASCII representation of the PIN with a null byte and the string ’MCP’, computing the SHA256 hash of the string, reversing the order of the bytes in the resulting hash, and taking the first 16 bytes as the key."
"Packets sent to CSRMesh devices require authentication as well as encryption. All multibyte types are represented in little endian format. To form a valid packet, the sequence/nonce value, constant 0x0080, and 10 null bytes are concatenated together to form a 128 bit initialization vector (IV). This IV, as well as the network key derived earlier is then used to initialize AES-128 in OFB mode. The arbitrary length data payload is then encrypted using this AES-OFB instance to form the encrypted payload. Next, a message authentication code is computed using HMAC-SHA256, using the network key as the secret, of the following data: 8 null bytes, sequence number, constant 0x80 and encrypted payload. The order of the bytes in the resulting hash are then reversed and the hash truncated to 8 bytes. The final output packet can then be built by contatenating the sequence/nonce value, constant 0x80, encrypted payload, truncated HMAC, and the constant 0xff."
|(require csrmesh)||package: csrmesh|
The sequence number of a packet is not only essential to the packet encryption but is also used by the mesh network in packet transmission. The mesh "remembers" recently transmitted packet sequence numbers and will drop packets with the same sequence number. An application using this library should ensure non-duplication of packet sequence numbers by either a global incrementing counter, a random generation of the next sequence number or similar scheme.
(define : Pin (string->Pin "1234")) (define net-key : NetworkKey (network-key )) (define my-payload : Payload ( (bytes 1 2 3))) (define next-seq-no : SeqNo (seqno (random 1000000))) (define csr-pkt : Packet (build-packet-with-key net-key next-seq-no my-payload))