1//===--- SipHash.h - An ABI-stable string SipHash ---------------*- C++ -*-===//
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// An implementation of SipHash, a hash function optimized for speed on
10// short inputs. Based on the SipHash reference implementation.
11//
12// Also provides one specific wrapper on top of SipHash-2-4-64 to compute
13// compute ABI-stable ptrauth discriminators.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_SUPPORT_SIPHASH_H
18#define LLVM_SUPPORT_SIPHASH_H
19
20#include "llvm/Support/Compiler.h"
21#include <cstdint>
22
23namespace llvm {
24
25template <typename T> class ArrayRef;
26class StringRef;
27
28/// Computes a SipHash-2-4 64-bit result.
29LLVM_ABI void getSipHash_2_4_64(ArrayRef<uint8_t> In, const uint8_t (&K)[16],
30 uint8_t (&Out)[8]);
31
32/// Computes a SipHash-2-4 128-bit result.
33LLVM_ABI void getSipHash_2_4_128(ArrayRef<uint8_t> In, const uint8_t (&K)[16],
34 uint8_t (&Out)[16]);
35
36/// Compute a stable non-zero 16-bit hash of the given string.
37///
38/// The exact algorithm is the little-endian interpretation of the
39/// non-doubled (i.e. 64-bit) result of applying a SipHash-2-4 using
40/// a specific seed value which can be found in the source.
41/// This 64-bit result is truncated to a non-zero 16-bit value.
42///
43/// We use a 16-bit discriminator because ARM64 can efficiently load
44/// a 16-bit immediate into the high bits of a register without disturbing
45/// the remainder of the value, which serves as a nice blend operation.
46/// 16 bits is also sufficiently compact to not inflate a loader relocation.
47/// We disallow zero to guarantee a different discriminator from the places
48/// in the ABI that use a constant zero.
49LLVM_ABI uint16_t getPointerAuthStableSipHash(StringRef S);
50
51} // end namespace llvm
52
53#endif
54