myl7/fss 1.1.0
Function secret sharing (FSS) primitives including distributed point/comparison function (DPF/DCF)
Loading...
Searching...
No Matches
aes128_mmo.cuh
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
8#pragma once
9#include <fss/prg.cuh>
10#include <cuda_runtime.h>
11#include <cuda/std/array>
12#include <cuda/std/span>
13#include <cassert>
14#include <openssl/evp.h>
15#include <openssl/aes.h>
16#include <fss/util.cuh>
17
18namespace fss::prg {
19
27template <int mul>
28class Aes128Mmo {
29private:
30 cuda::std::array<EVP_CIPHER_CTX *, mul> ctxs_;
31
32public:
40 Aes128Mmo(cuda::std::span<EVP_CIPHER_CTX *, mul> ctxs) {
41 for (int i = 0; i < mul; ++i) ctxs_[i] = ctxs[i];
42 }
43
49 static cuda::std::array<EVP_CIPHER_CTX *, mul> CreateCtxs(const unsigned char *keys[mul]) {
50 int ret;
51 cuda::std::array<EVP_CIPHER_CTX *, mul> ctxs;
52
53 for (int i = 0; i < mul; ++i) {
54 ctxs[i] = EVP_CIPHER_CTX_new();
55 assert(ctxs[i] != NULL);
56
57 ret = EVP_EncryptInit_ex2(ctxs[i], EVP_aes_128_ecb(), keys[i], NULL, NULL);
58 assert(ret == 1);
59
60 ret = EVP_CIPHER_CTX_set_padding(ctxs[i], 0);
61 assert(ret == 1);
62 }
63 return ctxs;
64 }
65
66 static void FreeCtxs(cuda::std::span<EVP_CIPHER_CTX *, mul> ctxs) {
67 for (auto ctx : ctxs) {
68 EVP_CIPHER_CTX_free(ctx);
69 }
70 }
71
72 __host__ __device__ cuda::std::array<int4, mul> Gen(int4 seed) {
73 cuda::std::array<int4, mul> out{};
74
75#ifdef __CUDA_ARCH__
76 assert(false && "Aes128Mmo is not supported on device side");
77 __trap();
78#else
79 for (int i = 0; i < mul; ++i) {
80 auto out_ptr = reinterpret_cast<unsigned char *>(&out[i]);
81 auto seed_ptr = reinterpret_cast<const unsigned char *>(&seed);
82 int cipher_len = 0;
83 // Ctx does not change after block encryption because we use ECB, no padding, and AES_BLOCK_SIZE input size.
84 int ret = EVP_EncryptUpdate(ctxs_[i], out_ptr, &cipher_len, seed_ptr, AES_BLOCK_SIZE);
85 assert(ret == 1);
86 assert(cipher_len == AES_BLOCK_SIZE);
87
88 out[i] = fss::util::Xor(out[i], seed);
89 }
90#endif
91
92 return out;
93 }
94};
95static_assert(Prgable<Aes128Mmo<2>, 2> && Prgable<Aes128Mmo<4>, 4>);
96
97} // namespace fss::prg
AES-128 with Matyas-Meyer-Oseas and pre-initialized cipher contexts as a PRG.
Definition aes128_mmo.cuh:28
static cuda::std::array< EVP_CIPHER_CTX *, mul > CreateCtxs(const unsigned char *keys[mul])
Create cipher contexts.
Definition aes128_mmo.cuh:49
Aes128Mmo(cuda::std::span< EVP_CIPHER_CTX *, mul > ctxs)
Constructor.
Definition aes128_mmo.cuh:40
Pseudorandom generator (PRG) interface.
Definition prg.cuh:21