go
Copilot instructions433 starsChanged 6 months agoBSD-3-ClauseView on GitHub
Use when implementing, modifying, or adding crypto algorithms to the Microsoft build of Go crypto backend system. Covers the end-to-end workflow: editing Go source in the submodule, backend architecture patterns, and extracting changes into patches with git go-patch. See eng/doc/DeveloperGuide.md for the full development workflow, including how to set up the repository, build, test, and work with git go-patch. The Microsoft build of Go replaces upstream BoringCrypto (crypto/internal/boring) with a pluggable backend system (github.com/microsoft/go/cryptobackend). The backend dispatches crypto operations to…
The file
Show its rules and text
We split this copilot-instructions.md into the rules it gives an agent
16 rules. About 0 are common, 6 are distinctive, and 10 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
10
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 3
Use when implementing, modifying, or adding crypto algorithms to the Microsoft build of Go crypto backend system.
- Very specific: line 21
All backend files use //go:build goexperiment.systemcrypto.
- Very specific: line 22
Prefer filename suffixes (_linux.go, _windows.go, _darwin.go) over build tag OS constraints.
- Very specific: line 30
Always import the backend package as boring:
- Very specific: line 76
If an operation is not supported on a platform (e.g., Darwin doesn't support DSA), the platform backend should also panic with …
- Distinctive: line 153
When an operation isn't supported by all backends (e.g., deterministic signing), reconstruct the upstream Go key from the seed:
- Very specific: line 182
When GODEBUG=fips140=only is set, non-FIPS-approved algorithms must return errors:
- Very specific: line 196
go/src/go/build/deps_test.go: Update if adding new package dependencies. - Very specific: line 200
Add the new algorithm's platform support status to eng/_util/cmd/updatecryptodocs/docs.go, then regenerate the doc:
- Distinctive: line 216
If the native crypto library (OpenSSL, CNG, CryptoKit) provides a function for an operation like key comparison, implement it in the …
- Very specific: line 220
When modifying existing upstream code, preserve the original variable names.
- Very specific: line 224
Prefer func FIPSApprovedHash(h hash.Hash) bool over adding a method to hash types.
- Very specific: line 229
0002-Add-crypto-backends.patch: All crypto backend integration code — backend shims, algorithm integrations, test modifications, build … - Distinctive: line 234
If your crypto change requires a new or updated backend module dependency:
- Distinctive: line 237
Commit them as a separate commit
- Distinctive: line 240
Run git go-patch extract to regenerate both patches
≈ “Common” is an estimate: at least 1 in 5 of the 1,644 Copilot instructions files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
crypto-backend
Use when implementing, modifying, or adding crypto algorithms to the Microsoft build of Go crypto backend system. Covers the end-to-end workflow: editing Go source in the submodule, backend architecture patterns, and extracting changes into patches with git go-patch.
Development workflow
See eng/doc/DeveloperGuide.md for the full development workflow, including how to set up the repository, build, test, and work with git go-patch.
Architecture overview
The Microsoft build of Go replaces upstream BoringCrypto (crypto/internal/boring) with a pluggable backend system (github.com/microsoft/go/cryptobackend). The backend dispatches crypto operations to platform-native libraries:
- Linux: OpenSSL via
github.com/microsoft/go-crypto-openssl - Windows: CNG via
github.com/microsoft/go-crypto-winnative - macOS: CryptoKit/CommonCrypto via
github.com/microsoft/go-crypto-darwin
The backend is gated by the goexperiment.systemcrypto build tag. When disabled, nobackend.go provides panic stubs so the code compiles but never executes.
Build tag conventions
- All backend files use
//go:build goexperiment.systemcrypto. Thenobackend.gouses//go:build !goexperiment.systemcrypto. - Prefer filename suffixes (
_linux.go,_windows.go,_darwin.go) over build tag OS constraints.- Do:
foo_linux.gowith//go:build goexperiment.systemcrypto - Don't:
foo.gowith//go:build goexperiment.systemcrypto && linux
- Do:
- The
boring.go/notboring.gofile pair in crypto packages usesgoexperiment.systemcrypto/!goexperiment.systemcryptobuild tags respectively. - Legacy build tags (
goexperiment.opensslcrypto,goexperiment.cngcrypto,goexperiment.darwincrypto) are still emitted for source compatibility but must not be used in new code.
Import alias convention
Always import the backend package as boring:
import (
boring "github.com/microsoft/go/cryptobackend"
"github.com/microsoft/go/cryptobackend/bbig"
)This preserves compatibility with upstream BoringCrypto code paths and the bcache caching infrastructure.
How to add a new crypto algorithm
1. Add backend shims (all four files)
For each platform backend file, add:
- A
Supports*()function that delegates to the platform library - Type aliases for key types
- Shim functions for each operation
// backend_linux.go
func SupportsMLKEM768() bool { return openssl.SupportsMLKEM768() }
type DecapsulationKeyMLKEM768 = openssl.DecapsulationKeyMLKEM768
func GenerateKeyMLKEM768() (DecapsulationKeyMLKEM768, error) {
return openssl.GenerateKeyMLKEM768()
}For nobackend.go, add matching panic stubs:
func SupportsMLKEM768() bool { panic("cryptobackend: not available") }
type DecapsulationKeyMLKEM768 struct{}
func GenerateKeyMLKEM768() (DecapsulationKeyMLKEM768, error) {
panic("cryptobackend: not available")
}nobackend.go stub rules:
- Functions: panic with
"cryptobackend: not available" - Types: empty structs (
struct{}orstruct{ _ int }) - Methods on stub types: panic
Supports*(): must panic (not return false) — they should never be called whenEnabledisfalse- Exception:
SupportsChaCha20Poly1305()returnsfalse(for FIPS-only mode checks)
If an operation is not supported on a platform (e.g., Darwin doesn't support DSA), the platform backend should also panic with "cryptobackend: not available" and the Supports* function should return false.
2. Integrate into the crypto package
Add a backend key field to the key struct. Be conscious of allocations — avoid unnecessary pointer indirection when the backend type can be stored by value, but note that some backend pointers' lifetimes may be tied to system-provided objects, so this must be a deliberate decision per type:
// Prefer value types when the backend type allows it
type DecapsulationKey768 struct {
key *mlkem.DecapsulationKey768
boringKey boring.DecapsulationKeyMLKEM768
}@qmuntal on PR #2342: "Can we keep boring by value instead of by reference? Same as k."
3. Create the gate function
The gate checks all prerequisites before dispatching to the backend:
// For a single capability:
func supportsBoringMLKEM768() bool {
return boring.Enabled && rand.IsDefaultReader(rand.Reader) && boring.SupportsMLKEM768()
}
// For algorithms with parameter sets:
func useBoringMLDSA(params Parameters) (bparams boring.MLDSAParameters, ok bool) {
if !boring.Enabled || !rand.IsDefaultReader(rand.Reader) {
return boring.MLDSAParameters{}, false
}
bparams, ok = boringMLDSAParameters(params)
if !ok || !boring.SupportsMLDSA(bparams) {
return boring.MLDSAParameters{}, false
}
return bparams, true
}Always check capability, not just Enabled:
// CORRECT
if boring.Enabled && boring.SupportsCurve(c.Params().Name) { ... }
// WRONG — backend may not support all parameter variations
if boring.Enabled { ... }4. Dispatch at the top of every public function
func GenerateKey768() (*DecapsulationKey768, error) {
if supportsBoringMLKEM768() {
key, err := boring.GenerateKeyMLKEM768()
if err != nil {
return nil, err
}
return &DecapsulationKey768{boringKey: key}, nil
}
// ... existing Go implementation
}For methods, check which key is populated:
func (dk *DecapsulationKey768) Bytes() []byte {
if dk.key == nil {
return dk.boringKey.Bytes()
}
return dk.key.Bytes()
}5. Handle unsupported operations with fallback
When an operation isn't supported by all backends (e.g., deterministic signing), reconstruct the upstream Go key from the seed:
func (sk *PrivateKey) goKey() (*mldsa.PrivateKey, error) {
if sk.boringKey.Bytes() == nil {
return &sk.k, nil
}
// Cache the reconstructed key so subsequent calls reuse it.
if sk.k != (mldsa.PrivateKey{}) {
return &sk.k, nil
}
seed := sk.boringKey.Bytes()
var err error
switch sk.boringKey.Parameters().String() {
case "ML-DSA-44":
sk.k, err = mldsa.NewPrivateKey44(seed)
// ...
}
if err != nil {
return nil, err
}
return &sk.k, nil
}Cache the result — reconstructing keys is expensive. Set sk.k on first call so subsequent calls reuse it. @qmuntal: "Calling NewPrivate[44,65,87] is a bit slow. We could set sk.k the first time goKey is used so that the next call reuses the same upstream key."
6. FIPS 140-only mode
When GODEBUG=fips140=only is set, non-FIPS-approved algorithms must return errors:
func NewChaCha20Poly1305(key []byte) (cipher.AEAD, error) {
if fips140only.Enforced() {
return nil, errors.New("chacha20poly1305: use of ChaCha20Poly1305 is not allowed in FIPS 140-only mode")
}
return openssl.NewChaCha20Poly1305(key)
}7. Update tests
- Allocation tests: Adjust expected allocation counts, don't skip. @qmuntal: "Don't skip allocation tests. It's better to adjust the allocation expectations when systemcrypto is enabled. This way we catch regressions."
go/src/go/build/deps_test.go: Update if adding new package dependencies.
8. Update documentation
- Add the new algorithm's platform support status to
eng/_util/cmd/updatecryptodocs/docs.go, then regenerate the doc:
cd eng/_util && go run ./cmd/updatecryptodocsThis regenerates eng/doc/CrossPlatformCryptography.md from the structured data in docs.go. Do not edit CrossPlatformCryptography.md by hand — it is auto-generated.
@qmuntal on the CSHAKE PR: "LGTM, I'm only missing an update to CrossPlatformCryptography.md."
Design principles from @qmuntal
Push operations into backends, not the integration layer
"As a general rule (which has exceptions), we shouldn't implement in this layer operations that upstream implements in
internal/fips140. For example, OpenSSL supports EVP_PKEY_eq. If CNG/CryptoKit don't provide similar helpers, we can always fallback to comparing the bytes in the backend."
If the native crypto library (OpenSSL, CNG, CryptoKit) provides a function for an operation like key comparison, implement it in the backend shim rather than reimplementing it in the crypto package integration code.
Keep original variable names
When modifying existing upstream code, preserve the original variable names. @qmuntal on PR #2149: "Keep the original variable name, h."
Use top-level functions over methods for approval checks
Prefer func FIPSApprovedHash(h hash.Hash) bool over adding a method to hash types. @qmuntal: "This might be slightly better, because then we don't add new methods to the hash objects returned by sha256.New (and friends)."
Which patch gets which changes
0001-Vendor-external-dependencies.patch:vendor/directory,go.mod,go.sum,modules.txt,deps_ignore.go— all module/vendor changes go here exclusively0002-Add-crypto-backends.patch: All crypto backend integration code — backend shims, algorithm integrations, test modifications, build system changes for systemcrypto- Other patches: See their commit messages. Never duplicate changes across patches.
Vendor changes workflow
If your crypto change requires a new or updated backend module dependency:
- Make the vendor changes (
go.mod,go.sum,vendor/) in the submodule - Commit them as a separate commit
- Squash that commit into the
0001-Vendor-external-dependencies.patchcommit duringgit go-patch rebase - Squash the crypto code changes into the
0002-Add-crypto-backends.patchcommit - Run
git go-patch extractto regenerate both patches
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

