| 1 |
package tpm2test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
|
| 7 |
. "github.com/google/go-tpm/tpm2" |
| 8 |
"github.com/google/go-tpm/tpm2/transport/simulator" |
| 9 |
) |
| 10 |
|
| 11 |
func TestRSAEncryption(t *testing.T) { |
| 12 |
theTpm, err := simulator.OpenSimulator() |
| 13 |
if err != nil { |
| 14 |
t.Fatalf("could not connect to TPM simulator: %v", err) |
| 15 |
} |
| 16 |
t.Cleanup(func() { |
| 17 |
if err := theTpm.Close(); err != nil { |
| 18 |
t.Errorf("%v", err) |
| 19 |
} |
| 20 |
}) |
| 21 |
|
| 22 |
createPrimaryCmd := CreatePrimary{ |
| 23 |
PrimaryHandle: TPMRHOwner, |
| 24 |
InPublic: New2B(RSASRKTemplate), |
| 25 |
} |
| 26 |
createPrimaryRsp, err := createPrimaryCmd.Execute(theTpm) |
| 27 |
if err != nil { |
| 28 |
t.Fatalf("%v", err) |
| 29 |
} |
| 30 |
t.Cleanup(func() { |
| 31 |
flushContextCmd := FlushContext{FlushHandle: createPrimaryRsp.ObjectHandle} |
| 32 |
if _, err := flushContextCmd.Execute(theTpm); err != nil { |
| 33 |
t.Errorf("%v", err) |
| 34 |
} |
| 35 |
}) |
| 36 |
|
| 37 |
createCmd := Create{ |
| 38 |
ParentHandle: NamedHandle{ |
| 39 |
Handle: createPrimaryRsp.ObjectHandle, |
| 40 |
Name: createPrimaryRsp.Name, |
| 41 |
}, |
| 42 |
InPublic: New2B(TPMTPublic{ |
| 43 |
Type: TPMAlgRSA, |
| 44 |
NameAlg: TPMAlgSHA256, |
| 45 |
ObjectAttributes: TPMAObject{ |
| 46 |
FixedTPM: true, |
| 47 |
STClear: false, |
| 48 |
FixedParent: true, |
| 49 |
SensitiveDataOrigin: true, |
| 50 |
UserWithAuth: true, |
| 51 |
AdminWithPolicy: false, |
| 52 |
NoDA: true, |
| 53 |
EncryptedDuplication: false, |
| 54 |
Restricted: false, |
| 55 |
Decrypt: true, |
| 56 |
SignEncrypt: true, |
| 57 |
}, |
| 58 |
Parameters: NewTPMUPublicParms( |
| 59 |
TPMAlgRSA, |
| 60 |
&TPMSRSAParms{ |
| 61 |
KeyBits: 2048, |
| 62 |
}, |
| 63 |
), |
| 64 |
Unique: NewTPMUPublicID( |
| 65 |
TPMAlgRSA, |
| 66 |
&TPM2BPublicKeyRSA{ |
| 67 |
Buffer: make([]byte, 256), |
| 68 |
}, |
| 69 |
), |
| 70 |
}), |
| 71 |
} |
| 72 |
createRsp, err := createCmd.Execute(theTpm) |
| 73 |
if err != nil { |
| 74 |
t.Fatalf("%v", err) |
| 75 |
} |
| 76 |
|
| 77 |
loadCmd := Load{ |
| 78 |
ParentHandle: NamedHandle{ |
| 79 |
Handle: createPrimaryRsp.ObjectHandle, |
| 80 |
Name: createPrimaryRsp.Name, |
| 81 |
}, |
| 82 |
InPrivate: createRsp.OutPrivate, |
| 83 |
InPublic: createRsp.OutPublic, |
| 84 |
} |
| 85 |
loadRsp, err := loadCmd.Execute(theTpm) |
| 86 |
if err != nil { |
| 87 |
t.Fatalf("%v", err) |
| 88 |
} |
| 89 |
t.Cleanup(func() { |
| 90 |
flushContextCmd := FlushContext{FlushHandle: loadRsp.ObjectHandle} |
| 91 |
if _, err := flushContextCmd.Execute(theTpm); err != nil { |
| 92 |
t.Errorf("%v", err) |
| 93 |
} |
| 94 |
}) |
| 95 |
|
| 96 |
message := []byte("secret") |
| 97 |
|
| 98 |
encryptCmd := RSAEncrypt{ |
| 99 |
KeyHandle: loadRsp.ObjectHandle, |
| 100 |
Message: TPM2BPublicKeyRSA{Buffer: message}, |
| 101 |
InScheme: TPMTRSADecrypt{ |
| 102 |
Scheme: TPMAlgOAEP, |
| 103 |
Details: NewTPMUAsymScheme( |
| 104 |
TPMAlgOAEP, |
| 105 |
&TPMSEncSchemeOAEP{ |
| 106 |
HashAlg: TPMAlgSHA256, |
| 107 |
}, |
| 108 |
), |
| 109 |
}, |
| 110 |
} |
| 111 |
encryptRsp, err := encryptCmd.Execute(theTpm) |
| 112 |
if err != nil { |
| 113 |
t.Fatalf("%v", err) |
| 114 |
} |
| 115 |
|
| 116 |
decryptCmd := RSADecrypt{ |
| 117 |
KeyHandle: NamedHandle{ |
| 118 |
Handle: loadRsp.ObjectHandle, |
| 119 |
Name: loadRsp.Name, |
| 120 |
}, |
| 121 |
CipherText: TPM2BPublicKeyRSA{Buffer: encryptRsp.OutData.Buffer}, |
| 122 |
InScheme: TPMTRSADecrypt{ |
| 123 |
Scheme: TPMAlgOAEP, |
| 124 |
Details: NewTPMUAsymScheme( |
| 125 |
TPMAlgOAEP, |
| 126 |
&TPMSEncSchemeOAEP{ |
| 127 |
HashAlg: TPMAlgSHA256, |
| 128 |
}, |
| 129 |
), |
| 130 |
}, |
| 131 |
} |
| 132 |
decryptRsp, err := decryptCmd.Execute(theTpm) |
| 133 |
if err != nil { |
| 134 |
t.Fatalf("%v", err) |
| 135 |
} |
| 136 |
|
| 137 |
if !bytes.Equal(message, decryptRsp.Message.Buffer) { |
| 138 |
t.Errorf("want %x got %x", message, decryptRsp.Message.Buffer) |
| 139 |
} |
| 140 |
} |