google / go-tpm

Apache License 2.0
561 stars 162 forks source link

Windows and linux packages are different #373

Open Simkiw opened 1 month ago

Simkiw commented 1 month ago

Hi,

My code which builds correctly on linux does not on windows, as if the packages were not the same based on the OS it is being built upon. I noticed later that indeed, in the documentation, it is rendered according to the user preference (well according to the user's machine) 2024-09-26_17h47_15

E.g., on windows it's OpenTPM() and on linux it's OpenTPM(string)

I can use build constraints and provide the tag during the build. That would imply having 2 files, one targeting windows and another linux.

I understand that the reason why they have been seperated is that access to the TPM is different from one OS to another. It still made me wonder, is there a better way to have a code base that would work regardless of the OS ?

Cheers,

chrisfenner commented 1 month ago

Hi @Simkiw!

So unfortunately the way you talk to a TPM on Windows and on Linux is pretty fundamentally different in ways that it's not appropriate to try to hide from the user. On Linux you need to pass the path to the TPM device file (usually /dev/tpm0 or /dev/tpmrm0) and on Windows we rely on tbs.dll to provide access to the TPM. The behavior of the OSes under the hood is pretty different too.

I'd recommend checking out https://github.com/google/go-tpm/pull/369 which discussed some of the issues with trying to hide the difference between different TPM transports. If you use the new transport packages, you at least won't run into the issue where two different functions share the same name. But you will probably want a build constraint that builds your app for Windows (using transport/windowstpm.Open) and Linux (using transport/linuxtpm.Open) separately.