| 1 | //===--- SipHash.cpp - An ABI-stable string hash --------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements an ABI-stable string hash based on SipHash, used to |
| 10 | // compute ptrauth discriminators. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/SipHash.h" |
| 15 | #include "siphash/SipHash.h" |
| 16 | #include "llvm/ADT/ArrayRef.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/Support/Endian.h" |
| 21 | #include <cstdint> |
| 22 | |
| 23 | using namespace llvm; |
| 24 | using namespace support; |
| 25 | |
| 26 | #define DEBUG_TYPE "llvm-siphash" |
| 27 | |
| 28 | void llvm::getSipHash_2_4_64(ArrayRef<uint8_t> In, const uint8_t (&K)[16], |
| 29 | uint8_t (&Out)[8]) { |
| 30 | siphash<2, 4>(in: In.data(), inlen: In.size(), k: K, out&: Out); |
| 31 | } |
| 32 | |
| 33 | void llvm::getSipHash_2_4_128(ArrayRef<uint8_t> In, const uint8_t (&K)[16], |
| 34 | uint8_t (&Out)[16]) { |
| 35 | siphash<2, 4>(in: In.data(), inlen: In.size(), k: K, out&: Out); |
| 36 | } |
| 37 | |
| 38 | /// Compute an ABI-stable 64-bit hash of the given string. |
| 39 | uint64_t llvm::getStableSipHash(StringRef Str) { |
| 40 | static const uint8_t K[16] = {0xb5, 0xd4, 0xc9, 0xeb, 0x79, 0x10, 0x4a, 0x79, |
| 41 | 0x6f, 0xec, 0x8b, 0x1b, 0x42, 0x87, 0x81, 0xd4}; |
| 42 | |
| 43 | uint8_t RawHashBytes[8]; |
| 44 | getSipHash_2_4_64(In: arrayRefFromStringRef(Input: Str), K, Out&: RawHashBytes); |
| 45 | return endian::read64le(P: RawHashBytes); |
| 46 | } |
| 47 | |
| 48 | /// Compute an ABI-stable 16-bit hash of the given string. |
| 49 | uint16_t llvm::getPointerAuthStableSipHash(StringRef Str) { |
| 50 | uint64_t RawHash = getStableSipHash(Str); |
| 51 | |
| 52 | // Produce a non-zero 16-bit discriminator. |
| 53 | uint16_t Discriminator = (RawHash % 0xFFFF) + 1; |
| 54 | LLVM_DEBUG( |
| 55 | dbgs() << "ptrauth stable hash discriminator: " << utostr(Discriminator) |
| 56 | << " (0x" |
| 57 | << utohexstr(Discriminator, /*Lowercase=*/false, /*Width=*/4) |
| 58 | << ")" |
| 59 | << " of: " << Str << "\n" ); |
| 60 | return Discriminator; |
| 61 | } |
| 62 | |