{"id":21354914,"url":"https://github.com/salrashid123/tpmrand","last_synced_at":"2025-10-13T21:06:50.060Z","repository":{"id":172724408,"uuid":"649689053","full_name":"salrashid123/tpmrand","owner":"salrashid123","description":"TPM backed crypto/rand Reader","archived":false,"fork":false,"pushed_at":"2024-05-28T23:21:33.000Z","size":1338,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-29T08:09:57.535Z","etag":null,"topics":["cryptography","golang","random-generation","random-number-generators","trusted-platform-module"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/salrashid123.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-05T12:25:55.000Z","updated_at":"2024-05-31T00:38:21.072Z","dependencies_parsed_at":"2024-05-06T15:07:23.168Z","dependency_job_id":"fb3116ca-2ae4-4f8e-9ce3-257648f00c68","html_url":"https://github.com/salrashid123/tpmrand","commit_stats":null,"previous_names":["salrashid123/tpmrand"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/salrashid123/tpmrand","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salrashid123%2Ftpmrand","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salrashid123%2Ftpmrand/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salrashid123%2Ftpmrand/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salrashid123%2Ftpmrand/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/salrashid123","download_url":"https://codeload.github.com/salrashid123/tpmrand/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salrashid123%2Ftpmrand/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279017048,"owners_count":26085949,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","golang","random-generation","random-number-generators","trusted-platform-module"],"created_at":"2024-11-22T04:15:19.227Z","updated_at":"2025-10-13T21:06:50.052Z","avatar_url":"https://github.com/salrashid123.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## TPM backed crypto/rand Reader   \n\nA [crypto.rand](https://pkg.go.dev/crypto/rand) reader that uses a [Trusted Platform Module (TPM)](https://en.wikipedia.org/wiki/Trusted_Platform_Module) as the source of randomness.\n\nBasically, its just a source of randomness used to create RSA keys or just get bits for use anywhere else.  With `tpm2-tools`, its like this:\n\n```bash\n$ tpm2_getrandom --hex 32 \n   8c20c96c56d3ac200881ac86505020a0dafcfe0224fbc51b843e07625cc779fc\n```\n\nAs background, the default rand generator with golang uses the following sources by default in [rand.go](https://go.dev/src/crypto/rand/rand.go)\n\n\nThe implementation uses go-tpm's [tpm2.GetRandom](https://pkg.go.dev/github.com/google/go-tpm/tpm2#GetRandom) function as the source of randomness from the hardware.\n\n\nFrom there, the usage is simple:\n\n```golang\npackage main\n\nimport (\n\t\"github.com/google/go-tpm/tpmutil\"\n\t//\"github.com/cenkalti/backoff/v4\"\n\ttpmrand \"github.com/salrashid123/tpmrand\"\n)\n\nvar ()\n\nfunc main() {\n\n\trwc, err := tpmutil.OpenTPM(\"/dev/tpm0\")\n\tdefer rwc.Close()\n\n\trandomBytes := make([]byte, 32)\n\tr, err := tpmrand.NewTPMRand(\u0026tpmrand.Reader{\n\t\tTpmDevice: rwc,\n\t\t//Scheme:    backoff.NewConstantBackOff(time.Millisecond * 10),\n\t})\n\n\t// Rand read\n\t_, err = r.Read(randomBytes)\n\n\tfmt.Printf(\"Random String :%s\\n\", base64.StdEncoding.EncodeToString(randomBytes))\n\n\t// /// RSA keygen\n\tprivkey, err := rsa.GenerateKey(r, 2048)\n\n\tkeyPEM := pem.EncodeToMemory(\n\t\t\u0026pem.Block{\n\t\t\tType:  \"RSA PRIVATE KEY\",\n\t\t\tBytes: x509.MarshalPKCS1PrivateKey(privkey),\n\t\t},\n\t)\n\tfmt.Printf(\"RSA Key: \\n%s\\n\", keyPEM)\n}\n```\n\nfor a quick demo, use the simulator:\n\n```bash\ncd example/tpm/\n\n$ go run main.go --tpm-path=simulator\n\tRandom String :0e0078720751bdebc5551276b68601312f4b34ac808c5b5ab263c5a760bc9253\n\n\tRSA Key: \n\t-----BEGIN RSA PRIVATE KEY-----\n\tMIIEogIBAAKCAQEAtOSZC/BAzoB1/diSSffzfZbztXvfYViXmPrAhdKulOf/kx7S\n\toxf6sQ7PBdrS5611an3Eg8FfxuqWdFO7FM42xmWUi95av5TrdbDwUz792qOtGB0v\n\tq216wJPJg8GmJ1R9ckBdPJyI6fVm2OjwJWKZaYywmyyyCwQX/etclGBC2z+/kq3J\n\t/CJmZBsOHhoB/o3jd+9pEeIYVihtJvQcDMzUcXGSM8MZOb5csIo8FUEYIheM3Bds\n\tvNjxtG5WeNWDtO6HaUAs3yn1+b8ilAa5uSmNfSVlQl8N/Um4ezrdvZ3WGbh1Z97z\n\tFpahYt5D2eOy6roX5QK+jOInfA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97EGXkYMFkaPvWQudDX2\n\tXJm/AoGANVOlB3WM8vshEe/iGsxYlAVWTDQiDScLcrrnIZgauXTRmkWEiQNr0VIk\n\tHr4ZneTlLU6913NYscYXADgS+ns9Q+EACt7UhgCdLCTpWkjwA0TDRwQgeK+9BlYP\n\tL4drKWRgsXFxLTtDD+VflKYLPKXos0ZMWKHruOs1/VXs2y6/yOU=\n\t-----END RSA PRIVATE KEY-----\n\n```\n\n### Encrypted Session\n\n`tpmrand` also supports encrypted transports as described here:\n\n- [CPU to TPM Bus Protection Guidance](https://trustedcomputinggroup.org/wp-content/uploads/TCG_CPU_TPM_Bus_Protection_Guidance_Passive_Attack_Mitigation_8May23-3.pdf)\n- [Protecting Secrets At Tpm Interface](https://tpm2-software.github.io/2021/02/17/Protecting-secrets-at-TPM-interface.html)\n\nTransport encryption is disabled by default so to enable it, pass a known asymmetric key in first that you know to be on the TPM (eg an EK) as the `EncryptionHandle` and `EncryptionPub`.\n\nfor reference with `tpm2_tools`, you can find the test cases [here](https://github.com/tpm2-software/tpm2-tools/blob/master/test/integration/tests/getrandom.sh#L35)\n\nAs a demo, you see the TPM API calls [Using software TPM (swtpm) to trace API calls with TCPDump](https://github.com/salrashid123/tpm2/tree/master/simulator_swtpm_tcpdump)\n\n\n```bash\n## setup swtpm\nmkdir /tmp/myvtpm\nsudo swtpm socket --tpmstate dir=/tmp/myvtpm --tpm2 --server type=tcp,port=2321 --ctrl type=tcp,port=2322 --flags not-need-init,startup-clear\n## start traces\nsudo tcpdump -s0 -ilo -w encrypted.cap port 2321\n```\n\n\nWithout encryption:\n\n```bash\n$ go run main.go  --tpm-path=\"127.0.0.1:2321\"\n    Random String :2c0be4244301100bc77ae94655eb86a426c6685481993d71d28a74355602ec29\n\n```\n\nnote that the bytes returned is in the clear:\n\n![example/images/clear.png](example/images/clear.png)\n\n\nWith encryption:\n\nThen \n```bash\n$ go run main.go  --tpm-path=\"127.0.0.1:2321\"\n   Random String :b9302b856c03466ec90e65f8c9817becab7e3e0a7523fabd7169607b2de55d60\n```\n\n![example/images/encrypted.png](example/images/encrypted.png)\n\n---\n\nWhile you're here, some other references on TPMs and usage:\n\n* [Trusted Platform Module (TPM) recipes with tpm2_tools and go-tpm](https://github.com/salrashid123/tpm2)\n* [golang-jwt for Trusted Platform Module (TPM)](https://github.com/salrashid123/golang-jwt-tpm)\n* [golang-jwt for PKCS11](https://github.com/salrashid123/golang-jwt-pkcs11)\n* [TPM Remote Attestation protocol using go-tpm and gRPC](https://github.com/salrashid123/go_tpm_remote_attestation)\n* [crypto.Signer, implementations for Google Cloud KMS and Trusted Platform Modules](https://github.com/salrashid123/signer)\n\n---\n\n### Testing\n\nUsing [swtpm](https://github.com/stefanberger/swtpm)\n\n```bash\nrm -rf /tmp/myvtpm \u0026\u0026 mkdir /tmp/myvtpm\nswtpm_setup --tpmstate /tmp/myvtpm --tpm2 --create-ek-cert\nswtpm 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\n\n# then specify \"127.0.0.1:2321\"  as the TPM device path in the examples\n# and for tpm2_tools, export the following var\nexport TPM2TOOLS_TCTI=\"swtpm:port=2321\"\n\ngo test -v\n```\n\n### PKCS-11\n\n[ThalesIgnite crypto11.NewRandomReader()](https://pkg.go.dev/github.com/ThalesIgnite/crypto11#Context.NewRandomReader) is an alternative to this library but requires installing [tpm2-pkcs11](https://github.com/tpm2-software/tpm2-pkcs11) first on the library....and critically, i'm not sure if it supports TPM session encryption (it may)\n\nI've left some examples of using that library here for reference\n\n- [PKCS 11 Samples in Go using SoftHSM](https://github.com/salrashid123/go_pkcs11)\n- [TPM PKCS-11 setup](https://github.com/salrashid123/golang-jwt-pkcs11#tpm)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsalrashid123%2Ftpmrand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsalrashid123%2Ftpmrand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsalrashid123%2Ftpmrand/lists"}