Skip to content

Instantly share code, notes, and snippets.

@salrashid123
salrashid123 / cosign_id_token.md
Created March 1, 2026 16:01
Cosign SignBlob with GCP Identity Token from TPM

Cosign SignBlob with GCP Identity Token from TPM

Snippet which uses a TPM based service account key to acquire an identity_token used to sign-blob using cosin

Normally, if you want to use cosign and a TPM, you would use the built in pkcs11 capability as described here:

However, this snippet encodes the service account private key into a TPM and then making it issue an id_token directly using:

@salrashid123
salrashid123 / decode_sigstore.md
Last active March 2, 2026 01:46
Decoding ENCRYPTED SIGSTORE PRIVATE KEY cosign key format

Decoding ENCRYPTED SIGSTORE PRIVATE KEY cosign key format

basically, -----BEGIN ENCRYPTED SIGSTORE PRIVATE KEY----- is not ans1 encoded but a JSON struct which looks like the follwoing

You need to decode/decrypt the EC key thats embedded inside it. The following keypair in the code does not have passphrase

also see cosign signature-specification

@salrashid123
salrashid123 / sign_verify_tpm.md
Created February 28, 2026 16:05
Sign and Verify TPM based JWT using `github.com/lestrrat-go/jwx`

Sign and Verify TPM based JWT using github.com/lestrrat-go/jwx

Using a crypto.Signer from github.com/salrashid123/tpmsigner

Following using a swtpm

rm -rf /tmp/myvtpm && mkdir /tmp/myvtpm
swtpm_setup --tpmstate /tmp/myvtpm --tpm2 --create-ek-cert
@salrashid123
salrashid123 / gcp_python_mtls.md
Created February 22, 2026 11:49
GCP Python Workload Federation TPM based mTLS
@salrashid123
salrashid123 / publickey_tpm.go
Created February 3, 2026 15:47
Create Public PEM key from TPM templates (rsa_ek, ecc_ek, h2, rsa_srk, ecc_srk)
package main
/*
rm -rf /tmp/myvtpm && mkdir /tmp/myvtpm && swtpm_setup --tpmstate /tmp/myvtpm --tpm2 --create-ek-cert && swtpm socket --tpmstate dir=/tmp/myvtpm --tpm2 --server type=tcp,port=2321 --ctrl type=tcp,port=2322 --flags not-need-init,startup-clear --log level=2
export TPM2TOOLS_TCTI="swtpm:port=2321"
export TPMB="127.0.0.1:2321"
go run main.go --parentKeyType=ecc_srk --tpm-path=127.0.0.1:2321
@salrashid123
salrashid123 / jwtTPM.md
Last active January 22, 2026 11:10
Issue a TPM based JWT using `github.com/lestrrat-go/jwx/v3/jwt`

Issue a JWT using github.com/lestrrat-go/jwx/v3/jwt

basically, you just have to pass in a crypto.Signer that represents the TPM-based key

there are several crypto.Signers around, i'm just using my own from

https://github.com/salrashid123/tpmsigner

also see

@salrashid123
salrashid123 / mlkem_generate_externally.md
Last active March 6, 2026 19:01
Generate MLKEM key using Trusted Platfrom Module as random number generator

Generate MLKEM key using Trusted Platfrom Module as random number generator

the following snippet generates an MLKEM key using a variety of sources and then writes the keys to file as PEM format

  • A) generate key internally in code
  • B) generate key externally using default crypto/rand source
  • C) generate a key externally using a TPM as the rand source ("github.com/salrashid123/tpmrand")
  • D) generate key externally using a given hex string statically

also see

package main
import (
"crypto/rand"
"encoding/base64"
"flag"
"io"
"log"
"net"
"slices"
@salrashid123
salrashid123 / tls_message_signer.md
Last active October 2, 2025 01:30
TLS with Restricted TPM Signing key and crypto.MessageSigner
@salrashid123
salrashid123 / server.go
Last active October 2, 2025 02:03
server code for crypto.messagesigner patch for TLS (https://github.com/golang/go/issues/75656)
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"flag"
"fmt"
"io"