Day 49 Layer 0 Encryption Engine Fleshout
December 23, 2021•554 words
What I did: Encryption, Data type definition and data organization
Time spent: 110.167
Comments:
For the command flow here's how the encryption of the CommandMidHeader is converted to EncryptedRawHeader.
Spread CommandMidHeader into DecryptedRawInnerHeader
Drop in Anti Replay UUID
Use 2SSL Session Derived Key and random nounce and encrypt DecryptedRawInnerHeader
Use 2SSL Session Derived Key and random Nounce and Encrypt DecryptedRawInnerHeader
Place SessionUUID, random Nounce, EncryptedRawInnerHeader, and HeaderTag into EncryptedRawHeader
Tada!!
Each layer and function needs to be as simple as possible. Therefore I'm having Crypto itself manage all the complixities of nounce, tag and data and key management itself.
Crypto.decrypt(key, nounce, tag, data) returns either null or the data.
Turns out the SessionID somehow isn't being passed. SessionSubsystem will need to pass that back. I'll need to retroactively update the layer hold on.
Alright wrote the beautiful encrypt() command. Now focusing on filling in the magic methods that I defined.
The first is this.detectNormalHeader(midHeader)
I'm looking at TypeScript theory to see if I can determine which subtype we're using???
Alright it's even easier than I could have hoped for.
type MidHeader = NormalMidHeader | CommandMidHeader;
type NormalMidHeader extends Uint8Array {length: 14 }
type CommandMidHeader extends Uint8Array {length: 88 }
Needing to learn how to do copying of arrays from X to Y.
The array runs from 0 to 159.
- Session UUID is 0 to 15,
- Header Nounce is 16 to 39
- Control Bytes is 40 to 43
- Anti Replay is 44 to 59
- Payload Key is 60 to 91
- Payload Nounce is 92 to 115
- SPacket Number is 116 to 125
- Unused is 126 to 127
- Payload Tag is 128 to 143
- Header Tag is 144 to 159
However the innerArray is 0 to 103
- Control Bytes is 0 to 3
- Anti Replay is 4 to 19
- Payload Key is 20 to 51
- Payload Nounce is 52 to 75
- SPacket Number is 76 to 85
- Unused is 86 to 87
- Payload Tag is 88 to 103
Insert PayloadCrypto. Working on the key, nounce and tag.
Finished insertPayloadCrypto
The innerArray for command is 0 to 103
- Control Bytes is 0 to 3
- Anti Replay is 4 to 19
- Command Data is 20 to 75
- SPacket Number is 76 to 85
- Command Data is 86 to 103
Meaning there is 74 bytes for command payload with 4 bytes for control, 16 bytes for Anti Replay and 10 for SPacket number.
The loops will look odd to you since I'm defining explicit endpoints for the loops. This prevents memory injection attacks or attribute modification from causing us to loop past the end of the array or cause other potentially undefined behavior.
I finished the spreadCommand function which beautifully uses the spreadNormal. This is like data structures in C++, in that parent classes can always access their data without needing to understand children's data or that children even exist. This is how memory is layed out.
Finished writing the useAntiReplayUUID(). Each function is mind numbingly simple but reads extremely well. You don't have array level interactions occuring at the same place that you're calling a string of functions. We are borrowing from C and having the calling function provide the memory we use.