1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package abi
import (
"bytes"
"encoding/hex"
"fmt"
"math/rand"
"runtime"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
spb "github.com/google/go-sev-guest/proto/sevsnp"
"github.com/google/uuid"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/wrapperspb"
)
var (
emptyReportV2 = `
version: 2
policy: 0xa0000
signature_algo: 1
report_data: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'
family_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
image_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
measurement: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
host_data: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
id_key_digest: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
author_key_digest: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
report_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
report_id_ma: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
chip_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
signature: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
`
emptyReportV3 = `
version: 3
policy: 0xa0000
signature_algo: 1
report_data: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'
family_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
image_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
measurement: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
host_data: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
id_key_digest: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
author_key_digest: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
report_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
report_id_ma: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
cpuid1eax_fms: 0
chip_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
signature: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
`
)
func TestMbz64(t *testing.T) {
tests := []struct {
data uint64
lo int
hi int
wantErr string
}{
{
data: uint64(0),
lo: 0,
hi: 63,
},
{
data: ^uint64(0) &^ (uint64(1<<31) | uint64(1<<32) | uint64(1<<33)),
lo: 31,
hi: 33,
},
{
data: ^uint64(0) &^ (uint64(1<<0x1f) | uint64(1<<0x20)),
lo: 0x1f,
hi: 0x21,
wantErr: "mbz range test[0x1f:0x21] not all zero",
},
{
data: ^uint64(0) &^ (uint64(1<<0x20) | uint64(1<<0x21)),
lo: 0x1f,
hi: 0x21,
wantErr: "mbz range test[0x1f:0x21] not all zero",
},
}
for _, tc := range tests {
err := mbz64(tc.data, "test", tc.hi, tc.lo)
if (tc.wantErr == "" && err != nil) || (tc.wantErr != "" && (err == nil || !strings.Contains(err.Error(), tc.wantErr))) {
t.Errorf("mbz64(0x%x, %d, %d) = %v, want %q", tc.data, tc.hi, tc.lo, err, tc.wantErr)
}
}
}
func TestReportMbz(t *testing.T) {
tests := []struct {
name string
report string
changeIndex int
changeValue byte
wantErr string
}{
{
name: "AuthorKeyEn reserved",
changeIndex: 0x49,
wantErr: "mbz range data[0x48:0x4C][0x5:0x1f] not all zero: cc00",
},
{
name: "pre-report data",
changeIndex: 0x4f,
wantErr: "mbz range [0x4c:0x50] not all zero: 000000cc",
},
{
name: "pre-chip id",
changeIndex: 0x18A,
wantErr: "mbz range [0x188:0x1a0] not all zero: 0000cc",
},
{
name: "current reserved",
changeIndex: 0x1EB,
wantErr: "mbz range [0x1eb:0x1ec] not all zero: cc",
},
{
name: "committed reserved",
changeIndex: 0x1EF,
wantErr: "mbz range [0x1ef:0x1f0] not all zero: cc",
},
{
name: "pre-signature reserved",
changeIndex: 0x1f9,
wantErr: "mbz range [0x1f8:0x2a0] not all zero: 00cc",
},
{
name: "post-ecdsa signature reserved",
changeIndex: signatureOffset + EcdsaP384Sha384SignatureSize + 2,
wantErr: "mbz range [0x330:0x4a0] not all zero: 0000cc",
},
{
name: "Guest policy bit 17",
changeIndex: policyOffset + 2, // Bits 16-23
changeValue: 0x1d, // Set bits 16, 18, 19, 20
wantErr: "policy[17] is reserved, must be 1, got 0",
},
{
name: "Guest policy bit 21",
changeIndex: policyOffset + 2, // Bits 16-23
changeValue: 0x22, // Set bits 17, 21
wantErr: "malformed guest policy: mbz range policy[0x15:0x3f] not all zero: 220000",
},
}
reportProto := &spb.Report{}
if err := prototext.Unmarshal([]byte(emptyReportV2), reportProto); err != nil {
t.Fatalf("test failure: %v", err)
}
for _, tc := range tests {
// Everything but the signature hase
raw, err := ReportToAbiBytes(reportProto)
if err != nil {
t.Fatalf("%s: test failure: ReportToAbiBytes(%v) errored unexpectedly: %v", tc.name, reportProto, err)
}
changeValue := byte(0xcc)
if tc.changeValue != 0 {
changeValue = tc.changeValue
}
raw[tc.changeIndex] = changeValue
if _, err := ReportToProto(raw); !strings.Contains(err.Error(), tc.wantErr) {
t.Errorf("%s: ReportToProto(%v) = _, %v. Want error %v", tc.name, reportProto, err, tc.wantErr)
}
}
reportProto = &spb.Report{}
if err := prototext.Unmarshal([]byte(emptyReportV3), reportProto); err != nil {
t.Fatalf("test failure: %v", err)
}
raw, err := ReportToAbiBytes(reportProto)
if err != nil {
t.Fatalf("%s: test failure: ReportToAbiBytes(reportV3) errored unexpectedly: %v", reportProto, err)
}
raw[0x188] = 0xcc
raw[0x189] = 0xcc
raw[0x18A] = 0xcc
if _, err := ReportToProto(raw); err != nil {
t.Errorf("ReportToProto(%v) = _, %v. Want nil", reportProto, err)
}
}
func TestSnpPolicySection(t *testing.T) {
entropySize := 128
entropy := make([]uint8, entropySize)
rand.Read(entropy)
for tc := 0; tc < entropySize/3; tc++ {
policy := SnpPolicy{
ABIMinor: entropy[tc*3],
ABIMajor: entropy[tc*3+1],
SMT: (entropy[tc*3+2] & 1) != 0,
MigrateMA: (entropy[tc*3+2] & 2) != 0,
Debug: (entropy[tc*3+2] & 4) != 0,
SingleSocket: (entropy[tc*3+2] & 8) != 0,
}
got, err := ParseSnpPolicy(SnpPolicyToBytes(policy))
if err != nil {
t.Errorf("ParseSnpPolicy(SnpPolicyToBytes(%v)) errored unexpectedly: %v", policy, err)
}
if got != policy {
t.Errorf("ParseSnpPolicy(SnpPolicyToBytes(%v)) = %v, want %v", policy, got, policy)
}
}
}
func TestSnpPlatformInfo(t *testing.T) {
tests := []struct {
input uint64
want SnpPlatformInfo
wantErr string
}{
{
input: 0,
},
{
input: 3,
want: SnpPlatformInfo{TSMEEnabled: true, SMTEnabled: true},
},
{
input: 21,
want: SnpPlatformInfo{
SMTEnabled: true,
ECCEnabled: true,
CiphertextHidingDRAMEnabled: true,
},
},
{
input: 42,
want: SnpPlatformInfo{
TSMEEnabled: true,
RAPLDisabled: true,
AliasCheckComplete: true,
},
},
{
input: 63,
want: SnpPlatformInfo{
TSMEEnabled: true,
SMTEnabled: true,
ECCEnabled: true,
RAPLDisabled: true,
CiphertextHidingDRAMEnabled: true,
AliasCheckComplete: true,
},
},
{
input: 64,
wantErr: "unrecognized platform info bit(s): 0x40",
},
}
for _, tc := range tests {
got, err := ParseSnpPlatformInfo(tc.input)
if (err != nil && (tc.wantErr == "" || !strings.Contains(err.Error(), tc.wantErr))) ||
(err == nil && tc.wantErr != "") {
t.Errorf("ParseSnpPlatformInfo(%x) errored unexpectedly. Got %v, want %v",
tc.input, err, tc.wantErr)
}
if err == nil && tc.want != got {
t.Errorf("ParseSnpPlatformInfo(%x) = %v, want %v", tc.input, got, tc.want)
}
}
}
func TestCpuid(t *testing.T) {
// GitHub actions may run on AARCH64
if runtime.GOARCH != "amd64" {
t.Skip()
}
a, b, c, d := cpuid(1)
if (a | b | c | d) == 0 {
t.Errorf("cpuid(1) = 0, 0, 0, 0")
}
}
type testCertTable struct {
table []byte
extraraw []byte
}
const extraGUID = "00000000-0000-c0de-0000-000000000000"
func testRawCertTable(t testing.TB) *testCertTable {
t.Helper()
headers := make([]CertTableHeaderEntry, 6) // ARK, ASK, VCEK, VLEK, extra, NULL
arkraw := []byte("ark")
askraw := []byte("ask")
vcekraw := []byte("vcek")
vlekraw := []byte("vlek")
extraraw := []byte("extra")
headers[0].GUID = uuid.MustParse(ArkGUID)
headers[0].Offset = uint32(len(headers) * CertTableEntrySize)
headers[0].Length = uint32(len(arkraw))
headers[1].GUID = uuid.MustParse(AskGUID)
headers[1].Offset = headers[0].Offset + headers[0].Length
headers[1].Length = uint32(len(askraw))
headers[2].GUID = uuid.MustParse(VcekGUID)
headers[2].Offset = headers[1].Offset + headers[1].Length
headers[2].Length = uint32(len(vcekraw))
headers[3].GUID = uuid.MustParse(VlekGUID)
headers[3].Offset = headers[2].Offset + headers[2].Length
headers[3].Length = uint32(len(vlekraw))
headers[4].GUID = uuid.MustParse(extraGUID)
headers[4].Offset = headers[3].Offset + headers[3].Length
headers[4].Length = uint32(len(extraraw))
result := &testCertTable{
table: make([]byte, headers[4].Offset+headers[4].Length),
extraraw: extraraw,
}
for i, cert := range [][]byte{arkraw, askraw, vcekraw, vlekraw, extraraw} {
if err := (&headers[i]).Write(result.table[i*CertTableEntrySize:]); err != nil {
t.Fatalf("could not write header %d: %v", i, err)
}
copy(result.table[headers[i].Offset:], cert)
}
return result
}
func testRawCertTableNoVcek(t testing.TB) *testCertTable {
t.Helper()
headers := make([]CertTableHeaderEntry, 2) // extra, NULL
extraraw := []byte("extra")
headers[0].GUID = uuid.MustParse(extraGUID)
headers[0].Offset = uint32(len(headers) * CertTableEntrySize)
headers[0].Length = uint32(len(extraraw))
result := &testCertTable{
table: make([]byte, headers[0].Offset+headers[0].Length),
extraraw: extraraw,
}
for i, cert := range [][]byte{extraraw} {
if err := (&headers[i]).Write(result.table[i*CertTableEntrySize:]); err != nil {
t.Fatalf("could not write header %d: %v", i, err)
}
copy(result.table[headers[i].Offset:], cert)
}
return result
}
func TestCertTableProto(t *testing.T) {
result := testRawCertTable(t)
c := new(CertTable)
if err := c.Unmarshal(result.table); err != nil {
t.Errorf("c.Unmarshal(%s) = %v, want nil", hex.Dump(result.table), err)
}
p := c.Proto()
if len(p.Extras) != 1 {
t.Fatalf("got cert table Extras length %d, want 1", len(p.Extras))
}
gotExtra, ok := p.Extras[extraGUID]
if !ok || !bytes.Equal(gotExtra, result.extraraw) {
t.Fatalf("Extras[%q] = %v, want %v", extraGUID, gotExtra, result.extraraw)
}
bs := c.Marshal()
if !bytes.Equal(bs, result.table) {
t.Errorf("c.Marshal() = %v, want %v", bs, result.table)
}
}
func TestSevProduct(t *testing.T) {
oldCpuid := cpuid
defer func() { cpuid = oldCpuid }()
tcs := []struct {
eax uint32
want *spb.SevProduct
}{
{
eax: 0x00a00f10,
want: &spb.SevProduct{
Name: spb.SevProduct_SEV_PRODUCT_MILAN,
MachineStepping: &wrapperspb.UInt32Value{Value: 0},
},
},
{
eax: 0x00a00f11,
want: &spb.SevProduct{
Name: spb.SevProduct_SEV_PRODUCT_MILAN,
MachineStepping: &wrapperspb.UInt32Value{Value: 1},
},
},
{
eax: 0x00a10f10,
want: &spb.SevProduct{
Name: spb.SevProduct_SEV_PRODUCT_GENOA,
MachineStepping: &wrapperspb.UInt32Value{Value: 0},
},
},
{
eax: 0x00a10f12,
want: &spb.SevProduct{
Name: spb.SevProduct_SEV_PRODUCT_GENOA,
MachineStepping: &wrapperspb.UInt32Value{Value: 2},
},
},
{
eax: 0x0b010f0,
want: &spb.SevProduct{
Name: spb.SevProduct_SEV_PRODUCT_UNKNOWN,
MachineStepping: &wrapperspb.UInt32Value{Value: 0},
},
},
{
eax: 0x00b00f21,
want: &spb.SevProduct{
Name: spb.SevProduct_SEV_PRODUCT_TURIN,
MachineStepping: &wrapperspb.UInt32Value{Value: 1},
},
},
}
for _, tc := range tcs {
t.Run(fmt.Sprintf("EAX_0x%x", tc.eax), func(t *testing.T) {
cpuid = func(uint32) (uint32, uint32, uint32, uint32) { return tc.eax, 0, 0, 0 }
got := SevProduct()
if diff := cmp.Diff(got, tc.want, protocmp.Transform()); diff != "" {
t.Errorf("SevProduct() = %+v, want %+v. Diff: %s", got, tc.want, diff)
}
got2 := SevProductFromCpuid1Eax(tc.eax)
if diff := cmp.Diff(got2, got, protocmp.Transform()); diff != "" {
t.Errorf("SevProductFromCpuid1Eax(0x%x) = %+v, want %+v. Diff: %s", tc.eax, got2, tc.want, diff)
}
})
}
}
func TestExtendedPlatformCertTableConservation(t *testing.T) {
// If VCEK is in the cert table, then the product info isn't added to the cert table.
table := testRawCertTable(t).table
oldt := new(CertTable)
_ = oldt.Unmarshal(table)
pold := oldt.Proto()
nextTable, err := ExtendedPlatformCertTable(table)
if err != nil {
t.Fatalf("ExtendedPlatformCertTable(%v) =_, %v. Want nil", table, err)
}
newt := new(CertTable)
if err := newt.Unmarshal(nextTable); err != nil {
t.Fatalf("ExtendedPlatformCertTable(_) _ %v, which could not be unmarshaled: %v", nextTable, err)
}
pnew := newt.Proto()
if len(pnew.Extras) != len(pold.Extras) {
t.Fatalf("ExtendedPlatformCertTable(_) table extras size is %d, want %d", len(pnew.Extras), len(pold.Extras))
}
}
func TestExtendedPlatformCertTable(t *testing.T) {
oldCpuid := cpuid
defer func() { cpuid = oldCpuid }()
table := testRawCertTableNoVcek(t).table
oldt := new(CertTable)
_ = oldt.Unmarshal(table)
pold := oldt.Proto()
tcs := []struct {
name string
pname spb.SevProduct_SevProductName
eax uint32
stepping uint32
}{
{name: "Genoa-B2", pname: spb.SevProduct_SEV_PRODUCT_GENOA, eax: 0x00a10f12, stepping: 2},
{name: "Milan-B1", pname: spb.SevProduct_SEV_PRODUCT_MILAN, eax: 0x00a00f11, stepping: 1},
{name: "Milan-B0", pname: spb.SevProduct_SEV_PRODUCT_MILAN, eax: 0x00a00f10, stepping: 0},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
cpuid = func(uint32) (uint32, uint32, uint32, uint32) { return tc.eax, 0, 0, 0 }
nextTable, err := ExtendedPlatformCertTable(table)
if err != nil {
t.Fatalf("ExtendedPlatformCertTable(%v) =_, %v. Want nil", table, err)
}
newt := new(CertTable)
if err := newt.Unmarshal(nextTable); err != nil {
t.Fatalf("ExtendedPlatformCertTable(_) _ %v, which could not be unmarshaled: %v", nextTable, err)
}
pnew := newt.Proto()
if len(pnew.Extras) != len(pold.Extras)+1 {
t.Fatalf("ExtendedPlatformCertTable(_) table extras size is %d, want %d", len(pnew.Extras), len(pold.Extras)+1)
}
blob, ok := pnew.Extras[ExtraPlatformInfoGUID]
if !ok {
t.Fatalf("ExtendedPlatfromCertTable(_) table %v extras missing ExtraPlatformInfoGUID", pnew)
}
info, err := ParseExtraPlatformInfo(blob)
if err != nil {
t.Fatalf("ParseExtraPlatformInfo(%v) = _, %v. Want nil", blob, err)
}
if info.Size != ExtraPlatformInfoV0Size {
t.Errorf("ExtraPltaformInfo Size %d is not %d", info.Size, ExtraPlatformInfoV0Size)
}
if info.Cpuid1Eax != tc.eax&CpuidProductMask {
t.Errorf("ExtraPlatformInfo Cpuid1Eax 0x%x is not 0x%x", info.Cpuid1Eax, tc.eax&CpuidProductMask)
}
got := SevProductFromCpuid1Eax(info.Cpuid1Eax)
want := &spb.SevProduct{Name: tc.pname, MachineStepping: &wrapperspb.UInt32Value{Value: tc.stepping}}
if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" {
t.Errorf("ExtraPlatformInfo Cpuid1Eax product %v is not %v: %s", got, want, diff)
}
})
}
}
|