1 // Copyright 2018, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of ARM Limited nor the names of its contributors may be
13 // used to endorse or promote products derived from this software without
14 // specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
28
29 #include "test-runner.h"
30
31 #include "aarch64/simulator-aarch64.h"
32
33 #define TEST(name) TEST_(AARCH64_POINTER_AUTH_##name)
34
35 namespace vixl {
36 namespace aarch64 {
37
38
TEST(compute_pac)39 TEST(compute_pac) {
40 Decoder decoder;
41 Simulator sim(&decoder);
42
43 uint64_t data1 = 0xfb623599da6e8127;
44 uint64_t data2 = 0x27979fadf7d53cb7;
45 uint64_t context = 0x477d469dec0b8762;
46 Simulator::PACKey key = {0x84be85ce9804e94b, 0xec2802d4e0a488e9, -1};
47
48 uint64_t pac1 = sim.ComputePAC(data1, context, key);
49 uint64_t pac2 = sim.ComputePAC(data2, context, key);
50
51 // NOTE: If the PAC implementation is changed, this may fail due to a hash
52 // collision.
53 VIXL_CHECK(pac1 != pac2);
54 }
55
TEST(add_and_auth_pac)56 TEST(add_and_auth_pac) {
57 Decoder decoder;
58 Simulator sim(&decoder);
59
60 uint64_t ptr = 0x0000000012345678;
61 uint64_t context = 0x477d469dec0b8762;
62 Simulator::PACKey key_a = {0x84be85ce9804e94b, 0xec2802d4e0a488e9, 0};
63 Simulator::PACKey key_b = {0xec1119e288704d13, 0xd7f6b76e1cea585e, 1};
64
65 uint64_t ptr_a =
66 sim.AddPAC(ptr, context, key_a, Simulator::kInstructionPointer);
67
68 // Attempt to authenticate the pointer with PAC using different keys.
69 uint64_t success =
70 sim.AuthPAC(ptr_a, context, key_a, Simulator::kInstructionPointer);
71 uint64_t fail =
72 sim.AuthPAC(ptr_a, context, key_b, Simulator::kInstructionPointer);
73
74 uint64_t pac_mask =
75 sim.CalculatePACMask(ptr, Simulator::kInstructionPointer, 0);
76
77 // NOTE: If the PAC implementation is changed, this may fail due to a hash
78 // collision.
79 VIXL_CHECK((ptr_a & pac_mask) != 0x0);
80 VIXL_CHECK(success == ptr);
81 VIXL_CHECK(fail != ptr);
82 }
83
TEST(add_and_strip_pac)84 TEST(add_and_strip_pac) {
85 Decoder decoder;
86 Simulator sim(&decoder);
87
88 uint64_t ptr = 0xff00000012345678;
89 uint64_t pac_mask =
90 sim.CalculatePACMask(ptr, Simulator::kInstructionPointer, 0);
91 uint64_t ptr_a = ptr | pac_mask;
92
93
94 VIXL_CHECK(sim.StripPAC(ptr_a, Simulator::kInstructionPointer) == ptr);
95 }
96 } // namespace aarch64
97 } // namespace vixl
98
99 #endif // VIXL_INCLUDE_SIMULATOR_AARCH64
100