-
Notifications
You must be signed in to change notification settings - Fork 127
/
additional_authenticated_data_generator.cc
105 lines (91 loc) · 3.96 KB
/
additional_authenticated_data_generator.cc
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
/*
*
* Copyright 2019 Asylo authors
*
* 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.
*
*/
#include "asylo/identity/additional_authenticated_data_generator.h"
#include <cstdint>
#include <vector>
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "asylo/crypto/sha256_hash.h"
#include "asylo/crypto/util/byte_container_util.h"
#include "asylo/crypto/util/bytes.h"
#include "asylo/crypto/util/trivial_object_util.h"
#include "asylo/util/status.h"
#include "asylo/util/status_macros.h"
namespace asylo {
namespace {
constexpr uint8_t kGetPceInfoUuid[kAdditionalAuthenticatedDataUuidSize] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
constexpr uint8_t kGetPceInfoPurpose[kAdditionalAuthenticatedDataPurposeSize] =
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
constexpr uint8_t kPceSignReportUuid[kAdditionalAuthenticatedDataUuidSize] = {
0x41, 0x53, 0x59, 0x4c, 0x4f, 0x20, 0x53, 0x49,
0x47, 0x4e, 0x52, 0x45, 0x50, 0x4f, 0x52, 0x54};
constexpr uint8_t
kPceSignReportPurpose[kAdditionalAuthenticatedDataPurposeSize] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
constexpr uint8_t kEkepUuid[kAdditionalAuthenticatedDataUuidSize] = {
0x41, 0x53, 0x59, 0x4c, 0x4f, 0x20, 0x45, 0x4b,
0x45, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
constexpr uint8_t kEkepPurpose[kAdditionalAuthenticatedDataPurposeSize] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
} // namespace
AdditionalAuthenticatedDataGenerator::AdditionalAuthenticatedDataGenerator(
UnsafeBytes<kAdditionalAuthenticatedDataUuidSize> uuid,
UnsafeBytes<kAdditionalAuthenticatedDataPurposeSize> purpose)
: uuid_(uuid), purpose_(purpose) {}
std::unique_ptr<AdditionalAuthenticatedDataGenerator>
AdditionalAuthenticatedDataGenerator::CreateGetPceInfoAadGenerator() {
return absl::make_unique<AdditionalAuthenticatedDataGenerator>(
kGetPceInfoUuid, kGetPceInfoPurpose);
}
std::unique_ptr<AdditionalAuthenticatedDataGenerator>
AdditionalAuthenticatedDataGenerator::CreatePceSignReportAadGenerator() {
return absl::make_unique<AdditionalAuthenticatedDataGenerator>(
kPceSignReportUuid, kPceSignReportPurpose);
}
std::unique_ptr<AdditionalAuthenticatedDataGenerator>
AdditionalAuthenticatedDataGenerator::CreateEkepAadGenerator() {
return absl::make_unique<AdditionalAuthenticatedDataGenerator>(kEkepUuid,
kEkepPurpose);
}
StatusOr<UnsafeBytes<kAdditionalAuthenticatedDataSize>>
AdditionalAuthenticatedDataGenerator::Generate(ByteContainerView data) const {
Sha256Hash hasher;
hasher.Init();
hasher.Update(data);
std::vector<uint8_t> hash;
ASYLO_RETURN_IF_ERROR(hasher.CumulativeHash(&hash));
UnsafeBytes<kAdditionalAuthenticatedDataSize> aad;
if (aad.replace(0, hash) != hasher.DigestSize()) {
return absl::InternalError("Setting hash data failed");
}
if (aad.replace(hasher.DigestSize(), purpose_) !=
kAdditionalAuthenticatedDataPurposeSize) {
return absl::InternalError("Setting purpose data failed");
}
if (aad.replace(hasher.DigestSize() + kAdditionalAuthenticatedDataPurposeSize,
uuid_) != kAdditionalAuthenticatedDataUuidSize) {
return absl::InternalError("Setting UUID data failed");
}
return aad;
}
} // namespace asylo