-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryptor.cpp
61 lines (52 loc) · 1.5 KB
/
encryptor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdexcept>
#include <memory>
#include <seal/encryptor.h>
#include <seal-c/encryptor.h>
#include "shared_context.hpp"
#include "public_key.hpp"
#include "encryptor.hpp"
#include "plaintext.hpp"
#include "ciphertext.hpp"
#include "wrap.hpp"
namespace seal_c {
bool
encrypt (seal::Encryptor& encryptor, const seal::Plaintext& plaintext, seal::Ciphertext& dest) noexcept
{
// TODO: don't just use a boolean, also write string error message to some buffer
try {
encryptor.encrypt (plaintext, dest);
return true;
} catch (std::invalid_argument) {
return false;
}
}
} // namespace seal_c
SEALEncryptorRef
SEAL_Encryptor_construct (SEALSharedContextRef context, SEALPublicKeyRef public_key)
{
using seal_c::wrap::unwrap;
auto ctx = unwrap (context)->get_context ();
auto p = std::make_unique <seal::Encryptor> (ctx, *unwrap (public_key));
return seal_c::wrap::wrap (p.release ());
}
SEALCiphertextRef
SEAL_Encryptor_encrypt_new (SEALEncryptorRef encryptor, SEALPlaintextRef plaintext, SEALBoolean *success)
{
using seal_c::wrap::unwrap;
using seal_c::wrap::wrap;
if (!success)
throw new std::logic_error ("success must not be a nullptr");
auto result = std::make_unique <seal::Ciphertext> ();
if (seal_c::encrypt (*unwrap (encryptor), *unwrap (plaintext), *result)) {
*success = 1;
return wrap (result.release ());
} else {
*success = 0;
return wrap<seal::Ciphertext*> (nullptr);
}
}
void
SEAL_Encryptor_destroy (SEALEncryptorRef encryptor)
{
delete seal_c::wrap::unwrap (encryptor);
}