Path: blob/master/dep/vixl/src/aarch64/pointer-auth-aarch64.cc
4261 views
// Copyright 2018, VIXL authors1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are met:5//6// * Redistributions of source code must retain the above copyright notice,7// this list of conditions and the following disclaimer.8// * Redistributions in binary form must reproduce the above copyright notice,9// this list of conditions and the following disclaimer in the documentation10// and/or other materials provided with the distribution.11// * Neither the name of ARM Limited nor the names of its contributors may be12// used to endorse or promote products derived from this software without13// specific prior written permission.14//15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE19// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR21// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER22// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,23// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE24// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2526#ifdef VIXL_INCLUDE_SIMULATOR_AARCH642728#include "utils-vixl.h"2930#include "simulator-aarch64.h"3132namespace vixl {33namespace aarch64 {3435// Randomly generated example keys for simulating only.36const Simulator::PACKey Simulator::kPACKeyIA = {0xc31718727de20f71,370xab9fd4e14b2fec51,380};39const Simulator::PACKey Simulator::kPACKeyIB = {0xeebb163b474e04c8,400x5267ac6fc280fb7c,411};42const Simulator::PACKey Simulator::kPACKeyDA = {0x5caef808deb8b1e2,430xd347cbc06b7b0f77,440};45const Simulator::PACKey Simulator::kPACKeyDB = {0xe06aa1a949ba8cc7,460xcfde69e3db6d0432,471};4849// The general PAC key isn't intended to be used with AuthPAC so we ensure the50// key number is invalid and asserts if used incorrectly.51const Simulator::PACKey Simulator::kPACKeyGA = {0xfcd98a44d564b3d5,520x6c56df1904bf0ddc,53-1};5455static uint64_t GetNibble(uint64_t in_data, int position) {56return (in_data >> position) & 0xf;57}5859static uint64_t ShuffleNibbles(uint64_t in_data) {60static int in_positions[16] =61{4, 36, 52, 40, 44, 0, 24, 12, 56, 60, 8, 32, 16, 28, 20, 48};62uint64_t out_data = 0;63for (int i = 0; i < 16; i++) {64out_data |= GetNibble(in_data, in_positions[i]) << (4 * i);65}66return out_data;67}6869static uint64_t SubstituteNibbles(uint64_t in_data) {70// Randomly chosen substitutes.71static uint64_t subs[16] =72{4, 7, 3, 9, 10, 14, 0, 1, 15, 2, 8, 6, 12, 5, 11, 13};73uint64_t out_data = 0;74for (int i = 0; i < 16; i++) {75int index = (in_data >> (4 * i)) & 0xf;76out_data |= subs[index] << (4 * i);77}78return out_data;79}8081// Rotate nibble to the left by the amount specified.82static uint64_t RotNibble(uint64_t in_cell, int amount) {83VIXL_ASSERT((amount >= 0) && (amount <= 3));8485in_cell &= 0xf;86uint64_t temp = (in_cell << 4) | in_cell;87return (temp >> (4 - amount)) & 0xf;88}8990static uint64_t BigShuffle(uint64_t in_data) {91uint64_t out_data = 0;92for (int i = 0; i < 4; i++) {93uint64_t n12 = GetNibble(in_data, 4 * (i + 12));94uint64_t n8 = GetNibble(in_data, 4 * (i + 8));95uint64_t n4 = GetNibble(in_data, 4 * (i + 4));96uint64_t n0 = GetNibble(in_data, 4 * (i + 0));9798uint64_t t0 = RotNibble(n8, 2) ^ RotNibble(n4, 1) ^ RotNibble(n0, 1);99uint64_t t1 = RotNibble(n12, 1) ^ RotNibble(n4, 2) ^ RotNibble(n0, 1);100uint64_t t2 = RotNibble(n12, 2) ^ RotNibble(n8, 1) ^ RotNibble(n0, 1);101uint64_t t3 = RotNibble(n12, 1) ^ RotNibble(n8, 1) ^ RotNibble(n4, 2);102103out_data |= t3 << (4 * (i + 0));104out_data |= t2 << (4 * (i + 4));105out_data |= t1 << (4 * (i + 8));106out_data |= t0 << (4 * (i + 12));107}108return out_data;109}110111// A simple, non-standard hash function invented for simulating. It mixes112// reasonably well, however it is unlikely to be cryptographically secure and113// may have a higher collision chance than other hashing algorithms.114uint64_t Simulator::ComputePAC(uint64_t data, uint64_t context, PACKey key) {115uint64_t working_value = data ^ key.high;116working_value = BigShuffle(working_value);117working_value = ShuffleNibbles(working_value);118working_value ^= key.low;119working_value = ShuffleNibbles(working_value);120working_value = BigShuffle(working_value);121working_value ^= context;122working_value = SubstituteNibbles(working_value);123working_value = BigShuffle(working_value);124working_value = SubstituteNibbles(working_value);125126return working_value;127}128129// The TTBR is selected by bit 63 or 55 depending on TBI for pointers without130// codes, but is always 55 once a PAC code is added to a pointer. For this131// reason, it must be calculated at the call site.132uint64_t Simulator::CalculatePACMask(uint64_t ptr, PointerType type, int ttbr) {133int bottom_pac_bit = GetBottomPACBit(ptr, ttbr);134int top_pac_bit = GetTopPACBit(ptr, type);135return ExtractUnsignedBitfield64(top_pac_bit,136bottom_pac_bit,1370xffffffffffffffff & ~kTTBRMask)138<< bottom_pac_bit;139}140141uint64_t Simulator::AuthPAC(uint64_t ptr,142uint64_t context,143PACKey key,144PointerType type) {145VIXL_ASSERT((key.number == 0) || (key.number == 1));146147uint64_t pac_mask = CalculatePACMask(ptr, type, (ptr >> 55) & 1);148uint64_t original_ptr =149((ptr & kTTBRMask) == 0) ? (ptr & ~pac_mask) : (ptr | pac_mask);150151uint64_t pac = ComputePAC(original_ptr, context, key);152153uint64_t error_code = 1 << key.number;154if ((pac & pac_mask) == (ptr & pac_mask)) {155return original_ptr;156} else {157int error_lsb = GetTopPACBit(ptr, type) - 2;158uint64_t error_mask = UINT64_C(0x3) << error_lsb;159return (original_ptr & ~error_mask) | (error_code << error_lsb);160}161}162163uint64_t Simulator::AddPAC(uint64_t ptr,164uint64_t context,165PACKey key,166PointerType type) {167int top_pac_bit = GetTopPACBit(ptr, type);168169// TODO: Properly handle the case where extension bits are bad and TBI is170// turned off, and also test me.171VIXL_ASSERT(HasTBI(ptr, type));172int ttbr = (ptr >> 55) & 1;173uint64_t pac_mask = CalculatePACMask(ptr, type, ttbr);174uint64_t ext_ptr = (ttbr == 0) ? (ptr & ~pac_mask) : (ptr | pac_mask);175176uint64_t pac = ComputePAC(ext_ptr, context, key);177178// If the pointer isn't all zeroes or all ones in the PAC bitfield, corrupt179// the resulting code.180if (((ptr & (pac_mask | kTTBRMask)) != 0x0) &&181((~ptr & (pac_mask | kTTBRMask)) != 0x0)) {182pac ^= UINT64_C(1) << (top_pac_bit - 1);183}184185uint64_t ttbr_shifted = static_cast<uint64_t>(ttbr) << 55;186return (pac & pac_mask) | ttbr_shifted | (ptr & ~pac_mask);187}188189uint64_t Simulator::StripPAC(uint64_t ptr, PointerType type) {190uint64_t pac_mask = CalculatePACMask(ptr, type, (ptr >> 55) & 1);191return ((ptr & kTTBRMask) == 0) ? (ptr & ~pac_mask) : (ptr | pac_mask);192}193} // namespace aarch64194} // namespace vixl195196#endif // VIXL_INCLUDE_SIMULATOR_AARCH64197198199