-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add encryption sigil with pre-obfuscation layer #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Implements ChaChaPolySigil that applies pre-obfuscation before sending data to CPU encryption routines. This ensures raw plaintext is never passed directly to encryption functions. Key improvements: - XORObfuscator and ShuffleMaskObfuscator for pre-encryption transforms - Nonce is now properly embedded in ciphertext, not stored separately in headers (production-ready, not demo-style) - Trix crypto integration with EncryptPayload/DecryptPayload methods - Comprehensive test coverage following Good/Bad/Ugly pattern
📝 WalkthroughSummary by CodeRabbitRelease Notes
✏️ Tip: You can customize this high-level summary in your review settings. WalkthroughA new cryptographic layer is introduced across two packages. The enchantrix package receives ChaChaPolySigil, a ChaCha20-Poly1305 encryption implementation with pluggable pre-obfuscation strategies (XOR-based and shuffle-mask variants). The trix package integrates this via encryption/decryption methods, configuration handling, state tracking through headers, and a convenience constructor for encrypted containers. Changes
Sequence DiagramssequenceDiagram
participant Client
participant Trix
participant CryptoConfig
participant ChaChaPolySigil
participant PreObfuscator
participant RNG as RNG<br/>(nonce)
participant ChaCha as ChaCha20-<br/>Poly1305
Client->>Trix: EncryptPayload(config)
Trix->>Trix: Validate not encrypted
Trix->>Trix: Select obfuscator<br/>(from config)
Trix->>ChaChaPolySigil: NewChaChaPolySigilWithObfuscator<br/>(key, obfuscator)
ChaChaPolySigil->>ChaChaPolySigil: Validate 32-byte key
Trix->>ChaChaPolySigil: In(payload)
ChaChaPolySigil->>PreObfuscator: Generate nonce via RNG
ChaChaPolySigil->>PreObfuscator: Obfuscate(payload, nonce)
PreObfuscator-->>ChaChaPolySigil: Obfuscated data
ChaChaPolySigil->>ChaCha: Encrypt obfuscated data<br/>with nonce
ChaCha-->>ChaChaPolySigil: Ciphertext
ChaChaPolySigil->>ChaChaPolySigil: Prepend nonce to ciphertext
ChaChaPolySigil-->>Trix: Encrypted payload (nonce + ct)
Trix->>Trix: Update header<br/>(encrypted, algorithm,<br/>obfuscator, timestamp)
Trix-->>Client: Success
sequenceDiagram
participant Client
participant Trix
participant CryptoConfig
participant ChaChaPolySigil
participant PreObfuscator
participant ChaCha as ChaCha20-<br/>Poly1305
Client->>Trix: DecryptPayload(config)
Trix->>Trix: Validate encrypted state<br/>from header
Trix->>Trix: Read obfuscator from header<br/>(default XOR if missing)
Trix->>ChaChaPolySigil: NewChaChaPolySigilWithObfuscator<br/>(key, obfuscator)
ChaChaPolySigil->>ChaChaPolySigil: Validate 32-byte key
Trix->>ChaChaPolySigil: Out(ciphertext)
ChaChaPolySigil->>ChaChaPolySigil: Extract nonce from<br/>ciphertext prefix
ChaChaPolySigil->>ChaCha: Decrypt (ct, nonce)
ChaCha-->>ChaChaPolySigil: Plaintext
ChaChaPolySigil->>PreObfuscator: Deobfuscate(plaintext, nonce)
PreObfuscator-->>ChaChaPolySigil: Original payload
ChaChaPolySigil-->>Trix: Decrypted payload
Trix->>Trix: Clear encrypted flag<br/>in header
Trix-->>Client: Success, payload restored
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (4)
🧰 Additional context used🧬 Code graph analysis (2)pkg/enchantrix/crypto_sigil_test.go (1)
pkg/trix/crypto_test.go (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
🔇 Additional comments (36)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @Snider, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a robust and secure encryption layer to the system, primarily focusing on the implementation of a ChaChaPolySigil using the ChaCha20-Poly1305 algorithm. A key security enhancement is the addition of pre-obfuscation techniques, such as XOR and shuffle-masking, which ensure that raw plaintext is never directly exposed to the core encryption routines. Furthermore, the design prioritizes security best practices by embedding the encryption nonce directly within the ciphertext, moving away from less secure "demo-style" separate nonce storage. This new cryptographic functionality is seamlessly integrated into the Trix data container, providing methods for encrypting and decrypting payloads with appropriate metadata handling. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a robust encryption layer with a pre-obfuscation step, which is a great security enhancement. The implementation correctly uses ChaCha20-Poly1305 and embeds the nonce within the ciphertext, following modern best practices. The code is well-structured and accompanied by an excellent and comprehensive test suite. I've identified a couple of areas for improvement: one is a performance issue in the ShuffleMaskObfuscator due to hashing within a loop, and the other is a small code simplification in the decryption logic to improve readability. Overall, this is a high-quality contribution.
| // Fisher-Yates shuffle with deterministic randomness | ||
| for i := length - 1; i > 0; i-- { | ||
| h.Reset() | ||
| h.Write(seed) | ||
| var iBytes [8]byte | ||
| binary.BigEndian.PutUint64(iBytes[:], uint64(i)) | ||
| h.Write(iBytes[:]) | ||
| jBytes := h.Sum(nil) | ||
| j := int(binary.BigEndian.Uint64(jBytes[:8]) % uint64(i+1)) | ||
| perm[i], perm[j] = perm[j], perm[i] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of generatePermutation re-calculates a SHA256 hash on every iteration of the loop. This is inefficient and can be a performance bottleneck for large inputs. Additionally, using the modulo operator % to determine the swap index j can introduce a slight statistical bias.
A more performant approach would be to generate a single stream of bytes upfront (similar to deriveKeyStream) and use that to source the values for the shuffle. This avoids expensive hash computations inside the loop.
For example, you could generate a stream of bytes and then use it like this:
// ... after generating seed ...
// Generate a stream of bytes to use for shuffling
streamLen := (length - 1) * 8 // 8 bytes per swap
keyStream := deriveStreamFromSeed(seed, streamLen) // A new helper function
// Fisher-Yates shuffle with deterministic randomness from the stream
for i := length - 1; i > 0; i-- {
byteOffset := (length - 1 - i) * 8
jBytes := keyStream[byteOffset : byteOffset+8]
j := int(binary.BigEndian.Uint64(jBytes) % uint64(i+1))
perm[i], perm[j] = perm[j], perm[i]
}I also recommend refactoring the stream generation logic from XORObfuscator.deriveKeyStream and ShuffleMaskObfuscator.deriveMask into a shared helper function to promote code reuse.
| var obfuscator enchantrix.PreObfuscator | ||
| if obfType, ok := t.Header[HeaderKeyObfuscator].(string); ok { | ||
| switch obfType { | ||
| case ObfuscatorShuffleMask: | ||
| obfuscator = &enchantrix.ShuffleMaskObfuscator{} | ||
| default: | ||
| obfuscator = &enchantrix.XORObfuscator{} | ||
| } | ||
| } else { | ||
| obfuscator = &enchantrix.XORObfuscator{} | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic to determine which obfuscator to use during decryption can be simplified. The else block is redundant because the default case in the switch statement already handles the same logic (assigning XORObfuscator).
You can make this more concise by reading the header value and using a single switch with a default case to handle both missing values and unknown values.
var obfuscator enchantrix.PreObfuscator
obfType, _ := t.Header[HeaderKeyObfuscator].(string)
switch obfType {
case ObfuscatorShuffleMask:
obfuscator = &enchantrix.ShuffleMaskObfuscator{}
default:
// Default to XORObfuscator if the header is missing, has the wrong type, or an unknown value.
obfuscator = &enchantrix.XORObfuscator{}
}
Implements ChaChaPolySigil that applies pre-obfuscation before sending
data to CPU encryption routines. This ensures raw plaintext is never
passed directly to encryption functions.
Key improvements:
in headers (production-ready, not demo-style)