| 1 |
// Copyright (c) 2018, Google LLC All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package tpm2 |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"crypto" |
| 20 |
"crypto/ecdsa" |
| 21 |
"crypto/elliptic" |
| 22 |
"crypto/rand" |
| 23 |
"encoding/hex" |
| 24 |
"reflect" |
| 25 |
"testing" |
| 26 |
|
| 27 |
"github.com/google/go-tpm/tpmutil" |
| 28 |
) |
| 29 |
|
| 30 |
var ( |
| 31 |
pcrSelection0 = PCRSelection{Hash: AlgSHA1, PCRs: []int{0}} |
| 32 |
pcrSelection1 = PCRSelection{Hash: AlgSHA1, PCRs: []int{1}} |
| 33 |
pcrSelection7 = PCRSelection{Hash: AlgSHA1, PCRs: []int{7}} |
| 34 |
defaultPassword = "\x01\x02\x03\x04" |
| 35 |
) |
| 36 |
|
| 37 |
func TestEncodeDecodeCreationData(t *testing.T) { |
| 38 |
parentQualified := tpmutil.Handle(101) |
| 39 |
cd := CreationData{ |
| 40 |
PCRSelection: PCRSelection{Hash: AlgSHA1, PCRs: []int{7}}, |
| 41 |
PCRDigest: []byte{1, 2, 3}, |
| 42 |
Locality: 32, |
| 43 |
ParentNameAlg: AlgSHA1, |
| 44 |
ParentName: Name{ |
| 45 |
Digest: &HashValue{ |
| 46 |
Alg: AlgSHA1, |
| 47 |
Value: make([]byte, crypto.SHA1.Size()), |
| 48 |
}, |
| 49 |
}, |
| 50 |
ParentQualifiedName: Name{ |
| 51 |
Handle: &parentQualified, |
| 52 |
}, |
| 53 |
OutsideInfo: []byte{7, 8, 9}, |
| 54 |
} |
| 55 |
|
| 56 |
encoded, err := cd.EncodeCreationData() |
| 57 |
if err != nil { |
| 58 |
t.Fatalf("error encoding CreationData: %v", err) |
| 59 |
} |
| 60 |
decoded, err := DecodeCreationData(encoded) |
| 61 |
if err != nil { |
| 62 |
t.Fatalf("error decoding CreationData: %v", err) |
| 63 |
} |
| 64 |
|
| 65 |
if !reflect.DeepEqual(*decoded, cd) { |
| 66 |
t.Errorf("got decoded value:\n%v\nwant:\n%v", decoded, cd) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestDecodeReadPCRs(t *testing.T) { |
| 71 |
testRespBytes, err := hex.DecodeString("800100000032000000000000001400000001000403800000000000010014427d27fe15f8f69736e02b6007b8f6ea674c0745") |
| 72 |
if err != nil { |
| 73 |
t.Fatal(err) |
| 74 |
} |
| 75 |
if _, err = decodeReadPCRs(testRespBytes[10:]); err != nil { |
| 76 |
t.Fatal(err) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestSingleEncodeDecodeTPMLSelection(t *testing.T) { |
| 81 |
buf, err := encodeTPMLPCRSelection(pcrSelection7) |
| 82 |
if err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
got, err := decodeOneTPMLPCRSelection(bytes.NewBuffer(buf)) |
| 86 |
if err != nil { |
| 87 |
t.Fatal(err) |
| 88 |
} |
| 89 |
|
| 90 |
if !reflect.DeepEqual(got, pcrSelection7) { |
| 91 |
t.Errorf("after decoding: %+v, before encoding: %+v", got, pcrSelection7) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func TestMultiArgsEncodeDecodeTPMLSelection(t *testing.T) { |
| 96 |
multipcrselection := []PCRSelection{pcrSelection0, pcrSelection1, pcrSelection7} |
| 97 |
buf, err := encodeTPMLPCRSelection(multipcrselection...) |
| 98 |
if err != nil { |
| 99 |
t.Fatal(err) |
| 100 |
} |
| 101 |
got, err := decodeTPMLPCRSelection(bytes.NewBuffer(buf)) |
| 102 |
if err != nil { |
| 103 |
t.Fatal(err) |
| 104 |
} |
| 105 |
|
| 106 |
if !reflect.DeepEqual(got, multipcrselection) { |
| 107 |
t.Errorf("after decoding: %+v, before encoding %+v", got, multipcrselection) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func TestDecodeReadClock(t *testing.T) { |
| 112 |
testRespBytes, err := hex.DecodeString("8001000000230000000000000001011380d00000001d1f57f84d000000530000000001") |
| 113 |
if err != nil { |
| 114 |
t.Fatal(err) |
| 115 |
} |
| 116 |
|
| 117 |
if _, _, err = decodeReadClock(testRespBytes[10:]); err != nil { |
| 118 |
t.Fatal(err) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
func TestDecodeGetCapability(t *testing.T) { |
| 123 |
testRespBytes, err := hex.DecodeString("80010000001300000000000000000100000000") |
| 124 |
if err != nil { |
| 125 |
t.Fatal(err) |
| 126 |
} |
| 127 |
|
| 128 |
handles, moreData, err := decodeGetCapability(testRespBytes[10:]) |
| 129 |
if err != nil { |
| 130 |
t.Fatal(err) |
| 131 |
} |
| 132 |
if len(handles) != 0 || moreData { |
| 133 |
t.Fatalf("got: (%v, %v), want: (%v, %v)", handles, moreData, 0, false) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func TestEncodeLoad(t *testing.T) { |
| 138 |
testCmdBytes, err := hex.DecodeString("8002000000b300000157800000000000000d40000009000001000401020" + |
| 139 |
"304005a0014450ecdce5f1ce202e4f8db15e2bde9a1241f85f30010faf6" + |
| 140 |
"2244fedc13fe0abb526e64b10b2de030b6f02be278e23365ef663febe7e" + |
| 141 |
"b4ddae935ca627ce4c40af9f5244dafbc7f47ceb84de87e72a75c7f1032" + |
| 142 |
"d3e7faddde0036000800040000001200140debb4cc9d2158cf7051a19ca" + |
| 143 |
"24b31e35d53b64d001000140b0758c7e4ce32c9d249151e91b72e35a6372fed") |
| 144 |
if err != nil { |
| 145 |
t.Fatal(err) |
| 146 |
} |
| 147 |
privateBlob := testCmdBytes[33:123] |
| 148 |
publicBlob := testCmdBytes[125:] |
| 149 |
auth := AuthCommand{Session: HandlePasswordSession, Attributes: AttrContinueSession, Auth: []byte(defaultPassword)} |
| 150 |
cmdBytes, err := encodeLoad(tpmutil.Handle(0x80000000), auth, publicBlob, privateBlob) |
| 151 |
if err != nil { |
| 152 |
t.Fatalf("encodeLoad failed %s", err) |
| 153 |
} |
| 154 |
if !bytes.Equal(cmdBytes, testCmdBytes[10:]) { |
| 155 |
t.Fatalf("got:\n%v\nwant:\n%v", cmdBytes, testCmdBytes[10:]) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func TestDecodeLoad(t *testing.T) { |
| 160 |
testRespBytes, err := hex.DecodeString("80020000002f000000008000000100000018001600049bc5e230c250b7d984d757f6450f575a5a896ad00000010000") |
| 161 |
if err != nil { |
| 162 |
t.Fatal(err) |
| 163 |
} |
| 164 |
|
| 165 |
if _, _, err = decodeLoad(testRespBytes[10:]); err != nil { |
| 166 |
t.Fatal(err) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func TestEncodeCreate(t *testing.T) { |
| 171 |
testCmdBytes, err := hex.DecodeString("80020000004d00000131400000010000000940000009000001000000090004010203040001ff001a000100040003007200000006008000430010040000010001000000045445535400000001000403800000") |
| 172 |
if err != nil { |
| 173 |
t.Fatal(err) |
| 174 |
} |
| 175 |
params := Public{ |
| 176 |
Type: AlgRSA, |
| 177 |
NameAlg: AlgSHA1, |
| 178 |
Attributes: FlagStorageDefault, |
| 179 |
RSAParameters: &RSAParams{ |
| 180 |
Symmetric: &SymScheme{ |
| 181 |
Alg: AlgAES, |
| 182 |
KeyBits: 128, |
| 183 |
Mode: AlgCFB, |
| 184 |
}, |
| 185 |
KeyBits: 1024, |
| 186 |
ExponentRaw: defaultRSAExponent, |
| 187 |
}, |
| 188 |
} |
| 189 |
auth := AuthCommand{Session: HandlePasswordSession, Attributes: AttrContinueSession} |
| 190 |
cmdBytes, err := encodeCreate(HandleOwner, pcrSelection7, auth, defaultPassword, []byte{255} /*sensitiveData*/, params, []byte("TEST") /*OutsideInfo*/) |
| 191 |
if err != nil { |
| 192 |
t.Fatal(err) |
| 193 |
} |
| 194 |
if !bytes.Equal(cmdBytes, testCmdBytes[10:]) { |
| 195 |
t.Fatalf("got: %v, want: %v", cmdBytes, testCmdBytes[10:]) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func TestDecodeCreatePrimary(t *testing.T) { |
| 200 |
testRespBytes, err := hex.DecodeString("80020000013c000000008000000000000125009a00010" + |
| 201 |
"00400030072000000060080004300100400000100010080afe42d93b037f25f5f4" + |
| 202 |
"a92bd65d61b417b51041f057e08670da98bb4720df166d8c0e12cd651196e0e577" + |
| 203 |
"828e65f0e9b0a0da4181bc6553e35970f8b4a6c1790c6132359c62f45952a6e377" + |
| 204 |
"9256de208b996bf2d216fdcfbddd4bdcb0e0cf9fd454caa9604d867e7d7901353d" + |
| 205 |
"1ccd23e16c7a53788f57b602449b0ecaf0590fb003100000001000403800000001" + |
| 206 |
"4bbf70aea75095f280ea3b835afda4a195279ab2c0100100004400000010004400" + |
| 207 |
"00001000000141a1ea8de55d7410287405c3b54057d578d76444a8021400000010" + |
| 208 |
"020e74aa1a8f272b604d6c0cf55b271211a130c011a12b0ba632cc1448c4de8371" + |
| 209 |
"3001600043adbc7b1296c49aac7c154371fd99aeb6e58a9f50000010000") |
| 210 |
if err != nil { |
| 211 |
t.Fatal(err) |
| 212 |
} |
| 213 |
|
| 214 |
if _, _, _, _, _, _, err = decodeCreatePrimary(testRespBytes[10:]); err != nil { |
| 215 |
t.Fatal(err) |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func TestEncodePolicyPCR(t *testing.T) { |
| 220 |
testCmdBytes, err := hex.DecodeString("80010000001a0000017f03000000000000000001000403800000") |
| 221 |
if err != nil { |
| 222 |
t.Fatal(err) |
| 223 |
} |
| 224 |
cmdBytes, err := encodePolicyPCR(tpmutil.Handle(0x03000000), []byte(nil), pcrSelection7) |
| 225 |
if err != nil { |
| 226 |
t.Fatal(err) |
| 227 |
} |
| 228 |
if !bytes.Equal(cmdBytes, testCmdBytes[10:]) { |
| 229 |
t.Fatalf("got: %v, want: %v", cmdBytes, testCmdBytes[10:]) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
func TestDecodeStartAuthSession(t *testing.T) { |
| 234 |
testRespBytes, err := hex.DecodeString("800100000020000000000300000000106cf0c90c419ce1a96d5205eb870ec527") |
| 235 |
if err != nil { |
| 236 |
t.Fatal(err) |
| 237 |
} |
| 238 |
|
| 239 |
if _, _, err = decodeStartAuthSession(testRespBytes[10:]); err != nil { |
| 240 |
t.Fatal(err) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func TestDecodeCreate(t *testing.T) { |
| 245 |
testRespBytes, err := hex.DecodeString("8002000001ba00000000000001a70076001405f2c6b6035d4" + |
| 246 |
"fab43fdc2ed0b6544de59ebd07100100e88a20eb9f58f0f13474a8ab6135144f7c" + |
| 247 |
"49b80f0f1c2f4900458e2c573c94e7d81e413a06031c634890ccf47e6d02762366" + |
| 248 |
"aedaa902f7e369950b6397e5a5884a0e888ab42fbc38b2d703d265bb539d3d8567" + |
| 249 |
"f766c7aac4046327c6a6b009800010004000400720000001000140004040000010" + |
| 250 |
"0010080e1189c2d7b301ecc75e2ab3a5f07484d6399fd5601e95af66d567a5ff40" + |
| 251 |
"78dd5edd0f38c6a7002370ba8e65eb8700aa5b0b41ddc33ba48543dc00cc855b3e" + |
| 252 |
"efa62985b75e720f62dcf2ac48d8aeb022610dea42bb9091cd304e3d13f6e85e95" + |
| 253 |
"63c2744591bccee343da9d8d0b183ed6409314ce19e990d644e115d78a51b225b0" + |
| 254 |
"055000000010004038000000014bbf70aea75095f280ea3b835afda4a195279ab2" + |
| 255 |
"c010004001600043adbc7b1296c49aac7c154371fd99aeb6e58a9f500160004cfc" + |
| 256 |
"b68f91fb12789154c722d4dbb528420ca211a0000001409987adb82d9864dbbdf5" + |
| 257 |
"15545798e3fe3e55a418021400000010020e504b9a055eb465316328cfa9d9cbb2" + |
| 258 |
"0706db0160457fa3dfe7e7aca34a334370000010000") |
| 259 |
if err != nil { |
| 260 |
t.Fatal(err) |
| 261 |
} |
| 262 |
|
| 263 |
if _, _, _, _, _, err = decodeCreate(testRespBytes[10:]); err != nil { |
| 264 |
t.Fatal(err) |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
func TestEncodeUnseal(t *testing.T) { |
| 269 |
testCmdBytes, err := hex.DecodeString("80020000001f0000015e800000010000000d40000009000001000401020304") |
| 270 |
if err != nil { |
| 271 |
t.Fatal(err) |
| 272 |
} |
| 273 |
cmdBytes, err := encodeUnseal(HandlePasswordSession, tpmutil.Handle(0x80000001), defaultPassword) |
| 274 |
if err != nil { |
| 275 |
t.Fatal(err) |
| 276 |
} |
| 277 |
if !bytes.Equal(cmdBytes, testCmdBytes[10:]) { |
| 278 |
t.Fatalf("got: %v, want: %v", cmdBytes, testCmdBytes[10:]) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
func TestDecodeUnseal(t *testing.T) { |
| 283 |
testRespBytes, err := hex.DecodeString("800200000035000000000000001200100102030405060708090a0b0c0d0e0f100010ea78d080f9f77d9d85e1f80350247ecb010000") |
| 284 |
if err != nil { |
| 285 |
t.Fatal(err) |
| 286 |
} |
| 287 |
|
| 288 |
if _, err = decodeUnseal(testRespBytes[10:]); err != nil { |
| 289 |
t.Fatal(err) |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
func TestEncodeQuote(t *testing.T) { |
| 294 |
testCmdBytes, err := hex.DecodeString("80020000003d00000158800000010000000d4000000900000100040102030400100102030405060708090a0b0c0d0e0f10001000000001000403800000") |
| 295 |
if err != nil { |
| 296 |
t.Fatal(err) |
| 297 |
} |
| 298 |
toQuote := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10} |
| 299 |
cmdBytes, err := encodeQuote(tpmutil.Handle(0x80000001), defaultPassword, toQuote, pcrSelection7, 0x0010) |
| 300 |
if err != nil { |
| 301 |
t.Fatal(err) |
| 302 |
} |
| 303 |
if !bytes.Equal(cmdBytes, testCmdBytes[10:]) { |
| 304 |
t.Fatalf("got: %v, want: %v", cmdBytes, testCmdBytes[10:]) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
func TestDecodeQuote(t *testing.T) { |
| 309 |
testRespBytes, err := hex.DecodeString("80020000010400000000000000f10069ff544347801800160004" + |
| 310 |
"7705bde86e3780577632421d34e5db4759667c8900100102030405060708090a0b" + |
| 311 |
"0c0d0e0f1000000000000fe8f99cf4968c1d6e516100eb40a3278641a1c6000000" + |
| 312 |
"010004038000000014ae2edb7e23d7e8f58daa87af87775993a426722500140004" + |
| 313 |
"00804e49bb73712bc6acca4778005741b586ee6da2c98fe4dd1a3babdd9dd58c2d" + |
| 314 |
"6fed9441a5bfb3c07ae0c7a5f2aff3d46b97429cff515caa12726fec6021b439c9" + |
| 315 |
"856ebdd2f006b9159b5bfcbb8ca16c6a8f4a5953669d6af769593c00249e240f50" + |
| 316 |
"09735b03abff38917de1c43bfdcc7a488fa6474c1011d3f399939e033930bb0000" + |
| 317 |
"010000") |
| 318 |
if err != nil { |
| 319 |
t.Fatal(err) |
| 320 |
} |
| 321 |
|
| 322 |
if _, _, err = decodeQuote(testRespBytes[10:]); err != nil { |
| 323 |
t.Fatal(err) |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
func TestDecodeReadPublic(t *testing.T) { |
| 328 |
testRespBytes, err := hex.DecodeString("80010000016e00000000011a0001000b000300720000000600800043" + |
| 329 |
"00100800000100010100c02b360837e3bfcb42c509eeafc89561cd3b68b0e257d77" + |
| 330 |
"488d99515f4135149adb64a419aea5f1d254819123b4a9e7df8c9f0c7ae11f128d6" + |
| 331 |
"8fef78c318cf62cee8aef48236027d0e6c8c63c4eec24b35c939017156a18b4a3f7" + |
| 332 |
"d0279e2ee79bfe9fa7680228490ad0bde089220ed59644b7a27667ddcca899e87bd" + |
| 333 |
"564fb96114884ad4534e2c4b4d838a3403b8f50508a2c18d0c405b5837b05349905" + |
| 334 |
"11112d1b1b961061ae9b24f01ad5cbae911e91fd7ee02507bd6b86df96ece3c9d47" + |
| 335 |
"f312ec0b2855cd203605fbab5c887d0f912674e17e8e76c50b0053da2b616746365" + |
| 336 |
"c49bc58ac80d1bac7f19b410feee62a048ccbfafd006af04988901d0852a0f30022" + |
| 337 |
"000bcc5923a0993903ea7754f3243ad11ab20c84e30c82a0bc0a443049e5f452782" + |
| 338 |
"00022000bcc514224b2eda95f3ef72174e551ecb5f5370d1886b06a68e54581bef5592bbe") |
| 339 |
if err != nil { |
| 340 |
t.Fatal(err) |
| 341 |
} |
| 342 |
|
| 343 |
if _, _, _, err = decodeReadPublic(testRespBytes[10:]); err != nil { |
| 344 |
t.Fatal(err) |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
func TestEncodeEvictControl(t *testing.T) { |
| 349 |
testCmdBytes, err := hex.DecodeString("8002000000230000012040000001810003e800000009400000090000010000810003e8") |
| 350 |
if err != nil { |
| 351 |
t.Fatal(err) |
| 352 |
} |
| 353 |
cmdBytes, err := encodeEvictControl("", tpmutil.Handle(0x40000001), tpmutil.Handle(0x810003e8), tpmutil.Handle(0x810003e8)) |
| 354 |
if err != nil { |
| 355 |
t.Fatal(err) |
| 356 |
} |
| 357 |
if !bytes.Equal(cmdBytes, testCmdBytes[10:]) { |
| 358 |
t.Fatalf("got: %v, want: %v", cmdBytes, testCmdBytes[10:]) |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
func TestEncodeAuthArea(t *testing.T) { |
| 363 |
pwAuth, err := encodeAuthArea(AuthCommand{Session: HandlePasswordSession, Attributes: AttrContinueSession, Auth: []byte(defaultPassword)}) |
| 364 |
if err != nil { |
| 365 |
t.Fatal(err) |
| 366 |
} |
| 367 |
want := []byte{0, 0, 0, 0xd, 0x40, 0, 0, 9, 0, 0, 1, 0, 4, 1, 2, 3, 4} |
| 368 |
if !bytes.Equal(want, pwAuth) { |
| 369 |
t.Fatalf("got: %v, want: %v", pwAuth, want) |
| 370 |
} |
| 371 |
|
| 372 |
pwAuth, err = encodeAuthArea(AuthCommand{Session: HandlePasswordSession, Attributes: AttrContinueSession, Auth: []byte("")}) |
| 373 |
if err != nil { |
| 374 |
t.Fatal(err) |
| 375 |
} |
| 376 |
want = []byte{0, 0, 0, 0x9, 0x40, 0, 0, 9, 0, 0, 1, 0, 0} |
| 377 |
if !bytes.Equal(want, pwAuth) { |
| 378 |
t.Fatalf("got: %v, want: %v", pwAuth, want) |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
func TestEncodeSensitiveArea(t *testing.T) { |
| 383 |
s, err := encodeSensitiveArea(tpmsSensitiveCreate{UserAuth: []byte(defaultPassword)}) |
| 384 |
if err != nil { |
| 385 |
t.Fatal(err) |
| 386 |
} |
| 387 |
want := []byte{0, 8, 0, 4, 1, 2, 3, 4, 0, 0} |
| 388 |
if !bytes.Equal(want, s) { |
| 389 |
t.Fatalf("got: %v, want: %v", s, want) |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
func TestEncodeTPMLPCRSelection(t *testing.T) { |
| 394 |
s, err := encodeTPMLPCRSelection(pcrSelection7) |
| 395 |
if err != nil { |
| 396 |
t.Fatal(err) |
| 397 |
} |
| 398 |
want := []byte{0, 0, 0, 1, 0, 4, 3, 0x80, 0, 0} |
| 399 |
if !bytes.Equal(want, s) { |
| 400 |
t.Fatalf("got: %v, want: %v", s, want) |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
func TestECCParamsEncodeDecode(t *testing.T) { |
| 405 |
pk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 406 |
if err != nil { |
| 407 |
t.Fatal(err) |
| 408 |
} |
| 409 |
params := &ECCParams{ |
| 410 |
Sign: &SigScheme{ |
| 411 |
Alg: AlgECDSA, |
| 412 |
Hash: AlgSHA1, |
| 413 |
}, |
| 414 |
CurveID: CurveNISTP256, |
| 415 |
Point: ECPoint{XRaw: pk.PublicKey.X.Bytes(), YRaw: pk.PublicKey.Y.Bytes()}, |
| 416 |
} |
| 417 |
|
| 418 |
buf, err := params.encode() |
| 419 |
if err != nil { |
| 420 |
t.Fatalf("encode: %v", err) |
| 421 |
} |
| 422 |
got, err := decodeECCParams(bytes.NewBuffer(buf)) |
| 423 |
if err != nil { |
| 424 |
t.Fatalf("decodeECCParams: %v", err) |
| 425 |
} |
| 426 |
|
| 427 |
if params.Point.X().Cmp(pk.PublicKey.X) != 0 { |
| 428 |
t.Fatalf("the deserialized X(big.Int) from ECCParams didn't match the X(Int) generated by ecdsa. got: %+v\nwant: %+v", params.Point.X(), pk.PublicKey.X) |
| 429 |
} |
| 430 |
if params.Point.Y().Cmp(pk.PublicKey.Y) != 0 { |
| 431 |
t.Fatalf("the deserialized Y(big.Int) from ECCParams didn't match the Y(Int) generated by ecdsa. got: %+v\nwant: %+v", params.Point.Y(), pk.PublicKey.Y) |
| 432 |
} |
| 433 |
if params.Point.X().Cmp(got.Point.X()) != 0 { |
| 434 |
t.Fatalf("the deserialized X(big.Int) from ECCParams after encode/decode didn't match the X(big.Int) before. got: %+v\nwant: %+v", got.Point.X(), params.Point.X()) |
| 435 |
} |
| 436 |
if params.Point.Y().Cmp(got.Point.Y()) != 0 { |
| 437 |
t.Fatalf("the deserialized Y(big.Int) from ECCParams after encode/decode didn't match the Y(big.Int) before. got: %+v\nwant: %+v", got.Point.Y(), params.Point.Y()) |
| 438 |
} |
| 439 |
|
| 440 |
if !reflect.DeepEqual(got, params) { |
| 441 |
t.Fatalf("got: %+v\nwant: %+v", got, params) |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
func TestSignEncode(t *testing.T) { |
| 446 |
// The expected output blobs (testCmdBytes) were recorded while successfully running Sign against a real TPM device. |
| 447 |
t.Run("AlgNull", func(t *testing.T) { |
| 448 |
testCmdBytes, err := hex.DecodeString("800000010000000d40000009000001000401020304000301020300108024400000070000") |
| 449 |
if err != nil { |
| 450 |
t.Fatal(err) |
| 451 |
} |
| 452 |
digest := []byte{0x01, 0x02, 0x03} |
| 453 |
cmdBytes, err := encodeSign(HandlePasswordSession, tpmutil.Handle(0x80000001), defaultPassword, digest, nil, nil) |
| 454 |
if err != nil { |
| 455 |
t.Fatal(err) |
| 456 |
} |
| 457 |
if !bytes.Equal(cmdBytes, testCmdBytes) { |
| 458 |
t.Fatalf("encoded Sign message did not match golden value. got: %s, want: %s", hex.EncodeToString(cmdBytes), hex.EncodeToString(testCmdBytes)) |
| 459 |
} |
| 460 |
}) |
| 461 |
|
| 462 |
t.Run("AlgSHA256", func(t *testing.T) { |
| 463 |
testCmdBytes, err := hex.DecodeString("800000010000000d4000000900000100040102030400030102030014000b8024400000070000") |
| 464 |
if err != nil { |
| 465 |
t.Fatal(err) |
| 466 |
} |
| 467 |
digest := []byte{0x01, 0x02, 0x03} |
| 468 |
cmdBytes, err := encodeSign(HandlePasswordSession, tpmutil.Handle(0x80000001), defaultPassword, digest, &SigScheme{ |
| 469 |
Alg: AlgRSASSA, |
| 470 |
Hash: AlgSHA256, |
| 471 |
}, nil) |
| 472 |
if err != nil { |
| 473 |
t.Fatal(err) |
| 474 |
} |
| 475 |
if !bytes.Equal(cmdBytes, testCmdBytes) { |
| 476 |
t.Fatalf("encoded Sign message did not match golden value. got: %s, want: %s", hex.EncodeToString(cmdBytes), hex.EncodeToString(testCmdBytes)) |
| 477 |
} |
| 478 |
}) |
| 479 |
} |
| 480 |
|
| 481 |
func TestTPMLDigestEncode(t *testing.T) { |
| 482 |
hashA := []byte{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 255} |
| 483 |
hashB := []byte{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 255} |
| 484 |
|
| 485 |
hl := TPMLDigest{Digests: []tpmutil.U16Bytes{ |
| 486 |
hashA, hashB}, |
| 487 |
} |
| 488 |
|
| 489 |
want := []byte{0, 0, 0, 2, 0, 20, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 255, |
| 490 |
0, 20, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 255} |
| 491 |
|
| 492 |
got, err := hl.Encode() |
| 493 |
if err != nil { |
| 494 |
t.Errorf("hl.Encode() failed: %v", err) |
| 495 |
} |
| 496 |
if !bytes.Equal(want, got) { |
| 497 |
t.Errorf("TPMLDigest{%v}.Encode() = %v, want: %v", hl, got, want) |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
func TestTPMLDigestDecode(t *testing.T) { |
| 502 |
hashA := []byte{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 255} |
| 503 |
hashB := []byte{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 255} |
| 504 |
|
| 505 |
want := &TPMLDigest{Digests: []tpmutil.U16Bytes{ |
| 506 |
hashA, hashB}, |
| 507 |
} |
| 508 |
|
| 509 |
b := []byte{0, 0, 0, 2, 0, 20, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 255, |
| 510 |
0, 20, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 255} |
| 511 |
|
| 512 |
got, err := DecodeTPMLDigest(b) |
| 513 |
if err != nil { |
| 514 |
t.Errorf("DecodeTPMDigest(b) failed: %v", err) |
| 515 |
} |
| 516 |
|
| 517 |
if len(got.Digests) != len(want.Digests) || |
| 518 |
!bytes.Equal(got.Digests[0], want.Digests[0]) || |
| 519 |
!bytes.Equal(got.Digests[1], want.Digests[1]) { |
| 520 |
t.Errorf("Digests are not the same.\n Have:\n %v\n - Want:\n %v", got.Digests, want.Digests) |
| 521 |
} |
| 522 |
} |