xref: /freebsd/contrib/llvm-project/llvm/lib/Support/SipHash.cpp (revision 770cf0a5f02dc8983a89c6568d741fbc25baa999)
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.data(), In.size(), K, 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.data(), In.size(), K, Out);
36 }
37 
38 /// Compute an ABI-stable 16-bit hash of the given string.
39 uint16_t llvm::getPointerAuthStableSipHash(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(arrayRefFromStringRef(Str), K, RawHashBytes);
45   uint64_t RawHash = endian::read64le(RawHashBytes);
46 
47   // Produce a non-zero 16-bit discriminator.
48   uint16_t Discriminator = (RawHash % 0xFFFF) + 1;
49   LLVM_DEBUG(
50       dbgs() << "ptrauth stable hash discriminator: " << utostr(Discriminator)
51              << " (0x"
52              << utohexstr(Discriminator, /*Lowercase=*/false, /*Width=*/4)
53              << ")"
54              << " of: " << Str << "\n");
55   return Discriminator;
56 }
57