Skip to content

Instantly share code, notes, and snippets.

@salrashid123
salrashid123 / tpm_kdf_hmac.md
Last active April 24, 2025 10:54
KDF using TPM based HMAC

Snippet which uses NIST SP 800-108 KDF with Countermode basically, this is an adaptation of github.com/hashicorp/vault/sdk/helper/kdf#CounterMode.

but with the HMAC operation using the TPM.


the sample below uses a swtpm where the hmac key is saved as a PEM encoded file.

First embed the key:

@salrashid123
salrashid123 / envoy_ekm.md
Last active April 4, 2025 23:08
Envoy EKM : extract the EKM value for a TLS connection with envoy

The following diff extracts the EKM value for a given TLS connection and then surfaces that to LUA (which emits the EKM as a header to the backend)

the LUA config will log the EKM in trace logs

# envoy -c envoy_server.yaml -l trace
[2025-04-04 08:36:29.396][3334775][info][lua] [source/extensions/filters/common/lua/lua.cc:26] script log: >>>>>>>>>>>> EKM: XGurfnlqXyjXphhJrrCmHRoKXAwC7CjrD7vixHdqOIo=

which has the same derived EKM value as a sample client app (eg, golang)

@salrashid123
salrashid123 / openssl_ekm.md
Last active April 3, 2025 11:31
Openssl server and client which prints the EKM
@salrashid123
salrashid123 / openssl_server_tpm.md
Last active March 18, 2025 11:23
openssl server with TPM based private key

TLS with TPM based private key

Requires openssl-tpm2 provider

# export OPENSSL_MODULES=/usr/lib/x86_64-linux-gnu/ossl-modules/
# 
# cat /etc/ssl/openssl.cnf
# [openssl_init]
@salrashid123
salrashid123 / istio_gcp.md
Last active March 8, 2025 13:34
Istio with GCP OIDC Authorization

Simple demo of istio authorization rules using GCP OIDC tokens

Basically, this will allow inbound OIDC authentication and authorization for a service using a google issued id_token

Setup

First install minikube (i'm using kvm2 but you can use anything)

@salrashid123
salrashid123 / attestationkeyauth.go
Last active March 5, 2025 15:18
GCE Attestation Key based authentication
package main
/*
Authenticate to GCP using the GCP embedded vTPM AttestationKey
this specific implementation acquires a JWTAccessToken with scopes
https://github.com/salrashid123/gcp-vtpm-ek-ak/tree/main?tab=readme-ov-file#sign-jwt-with-tpm
1. first create a gce instance with confidentialcompute and vtpm enabled
@salrashid123
salrashid123 / hmacsha256.c
Created February 20, 2025 13:47
hmacsha256 with openssl
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/hmac.h>
@salrashid123
salrashid123 / psk.md
Created February 13, 2025 11:49
TLS PSK with opennsl C
@salrashid123
salrashid123 / csek_versioning.md
Last active January 24, 2025 14:34
GCS CSEK and object versioning

GCS CSEK with Object versioning

The following will upload a file into a bucket with object versioning.

The file will have a CSEK

Then encrypt it with another CSEK and recall the first version using its original CSEK

### create two cseks
@salrashid123
salrashid123 / pcrgen.go
Last active December 12, 2024 22:03
TPM Policy command parameter generation for PCR
package main
import (
"encoding/binary"
"encoding/hex"
"flag"
"io"
"log"
"net"
"os"