myl7/fss 1.1.0
Function secret sharing (FSS) primitives including distributed point/comparison function (DPF/DCF)
Loading...
Searching...
No Matches
sha256.cuh
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
8#pragma once
9#include <fss/hash.cuh>
10#include <cuda_runtime.h>
11#include <cuda/std/array>
12#include <cuda/std/span>
13#include <cuda/std/tuple>
14#include <cassert>
15#include <openssl/evp.h>
16#include <fss/util.cuh>
17
18namespace fss::hash {
19
25class Sha256 {
26private:
27 int4 key_;
28
29public:
35 explicit Sha256(int4 key) : key_(key) {}
36
43 __host__ __device__ cuda::std::array<int4, 2> Hash(cuda::std::span<const int4, 4> msg) {
44 cuda::std::array<int4, 2> out{};
45
46#ifdef __CUDA_ARCH__
47 assert(false && "Sha256 is not supported on device side");
48 __trap();
49#else
50 int4 buf[5] = {key_, msg[0], msg[1], msg[2], msg[3]};
51 auto buf_ptr = reinterpret_cast<const unsigned char *>(buf);
52 auto out_ptr = reinterpret_cast<unsigned char *>(out.data());
53 int ret = EVP_Digest(buf_ptr, 80, out_ptr, NULL, EVP_sha256(), NULL);
54 assert(ret == 1);
55#endif
56
57 return out;
58 }
59
69 __host__ __device__ cuda::std::array<int4, 4> Hash(cuda::std::tuple<int4, const int4> msg) {
70 cuda::std::array<int4, 4> out{};
71
72#ifdef __CUDA_ARCH__
73 assert(false && "Sha256 is not supported on device side");
74 __trap();
75#else
76 auto [a, b] = msg;
77 int4 buf[3] = {key_, fss::util::SetLsb(a, false), b};
78 auto buf_ptr = reinterpret_cast<const unsigned char *>(buf);
79 auto out_ptr = reinterpret_cast<unsigned char *>(out.data());
80 int ret = EVP_Digest(buf_ptr, 48, out_ptr, NULL, EVP_sha256(), NULL);
81 assert(ret == 1);
82
83 buf[1] = fss::util::SetLsb(a, true);
84 ret = EVP_Digest(buf_ptr, 48, out_ptr + 32, NULL, EVP_sha256(), NULL);
85 assert(ret == 1);
86#endif
87
88 return out;
89 }
90};
92
93} // namespace fss::hash
SHA-256 keyed hash.
Definition sha256.cuh:25
Sha256(int4 key)
Constructor.
Definition sha256.cuh:35
cuda::std::array< int4, 4 > Hash(cuda::std::tuple< int4, const int4 > msg)
XOR-collision-resistant hash.
Definition sha256.cuh:69
cuda::std::array< int4, 2 > Hash(cuda::std::span< const int4, 4 > msg)
Hash a 64B message with the key.
Definition sha256.cuh:43
Collision-resistant hash interface.
Definition hash.cuh:19
Collision-resistant and XOR-collision-resistant hash interface.
Definition hash.cuh:27