1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT license.
3
4 // SEALNet
5 #include "seal/c/encryptor.h"
6 #include "seal/c/utilities.h"
7
8 // SEAL
9 #include "seal/encryptor.h"
10
11 using namespace std;
12 using namespace seal;
13 using namespace seal::c;
14
15 // Enables access to private members of seal::Encryptor.
16 using ph = struct seal::Encryptor::EncryptorPrivateHelper
17 {
18 static void encrypt_symmetric_internal(
19 Encryptor *encryptor, const Plaintext &plain, bool save_seed, Ciphertext &destination, MemoryPoolHandle pool)
20 {
21 encryptor->encrypt_internal(plain, false, save_seed, destination, pool);
22 }
23
24 static void encrypt_zero_symmetric_internal(
25 Encryptor *encryptor, parms_id_type parms_id, bool save_seed, Ciphertext &destination, MemoryPoolHandle pool)
26 {
27 encryptor->encrypt_zero_internal(parms_id, false, save_seed, destination, pool);
28 }
29
30 static void encrypt_zero_symmetric_internal(
31 Encryptor *encryptor, bool save_seed, Ciphertext &destination, MemoryPoolHandle pool)
32 {
33 encryptor->encrypt_zero_internal(encryptor->context_.first_parms_id(), false, save_seed, destination, pool);
34 }
35 };
36
Encryptor_Create(void * context,void * public_key,void * secret_key,void ** encryptor)37 SEAL_C_FUNC Encryptor_Create(void *context, void *public_key, void *secret_key, void **encryptor)
38 {
39 const SEALContext *ctx = FromVoid<SEALContext>(context);
40 IfNullRet(ctx, E_POINTER);
41 PublicKey *pkey = FromVoid<PublicKey>(public_key);
42 SecretKey *skey = FromVoid<SecretKey>(secret_key);
43 IfNullRet(encryptor, E_POINTER);
44 if (nullptr == pkey && nullptr == skey)
45 {
46 return E_POINTER;
47 }
48
49 try
50 {
51 Encryptor *enc;
52 if (nullptr != pkey)
53 {
54 enc = new Encryptor(*ctx, *pkey);
55 if (nullptr != skey)
56 {
57 enc->set_secret_key(*skey);
58 }
59 }
60 else
61 {
62 enc = new Encryptor(*ctx, *skey);
63 }
64 *encryptor = enc;
65 return S_OK;
66 }
67 catch (const invalid_argument &)
68 {
69 return E_INVALIDARG;
70 }
71 }
72
Encryptor_SetPublicKey(void * thisptr,void * public_key)73 SEAL_C_FUNC Encryptor_SetPublicKey(void *thisptr, void *public_key)
74 {
75 Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
76 IfNullRet(encryptor, E_POINTER);
77 PublicKey *pkey = FromVoid<PublicKey>(public_key);
78 IfNullRet(pkey, E_POINTER);
79
80 try
81 {
82 encryptor->set_public_key(*pkey);
83 return S_OK;
84 }
85 catch (const invalid_argument &)
86 {
87 return E_INVALIDARG;
88 }
89 }
90
Encryptor_SetSecretKey(void * thisptr,void * secret_key)91 SEAL_C_FUNC Encryptor_SetSecretKey(void *thisptr, void *secret_key)
92 {
93 Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
94 IfNullRet(encryptor, E_POINTER);
95 SecretKey *skey = FromVoid<SecretKey>(secret_key);
96 IfNullRet(skey, E_POINTER);
97
98 try
99 {
100 encryptor->set_secret_key(*skey);
101 return S_OK;
102 }
103 catch (const invalid_argument &)
104 {
105 return E_INVALIDARG;
106 }
107 }
108
Encryptor_Encrypt(void * thisptr,void * plaintext,void * destination,void * pool_handle)109 SEAL_C_FUNC Encryptor_Encrypt(void *thisptr, void *plaintext, void *destination, void *pool_handle)
110 {
111 Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
112 IfNullRet(encryptor, E_POINTER);
113 Plaintext *plain = FromVoid<Plaintext>(plaintext);
114 IfNullRet(plain, E_POINTER);
115 Ciphertext *cipher = FromVoid<Ciphertext>(destination);
116 IfNullRet(cipher, E_POINTER);
117 unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
118
119 try
120 {
121 encryptor->encrypt(*plain, *cipher, *pool);
122 return S_OK;
123 }
124 catch (const invalid_argument &)
125 {
126 return E_INVALIDARG;
127 }
128 catch (const logic_error &)
129 {
130 return COR_E_INVALIDOPERATION;
131 }
132 }
133
Encryptor_EncryptZero1(void * thisptr,uint64_t * parms_id,void * destination,void * pool_handle)134 SEAL_C_FUNC Encryptor_EncryptZero1(void *thisptr, uint64_t *parms_id, void *destination, void *pool_handle)
135 {
136 Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
137 IfNullRet(encryptor, E_POINTER);
138 IfNullRet(parms_id, E_POINTER);
139 Ciphertext *cipher = FromVoid<Ciphertext>(destination);
140 IfNullRet(cipher, E_POINTER);
141 unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
142
143 parms_id_type parms;
144 CopyParmsId(parms_id, parms);
145
146 try
147 {
148 encryptor->encrypt_zero(parms, *cipher, *pool);
149 return S_OK;
150 }
151 catch (const invalid_argument &)
152 {
153 return E_INVALIDARG;
154 }
155 catch (const logic_error &)
156 {
157 return COR_E_INVALIDOPERATION;
158 }
159 }
160
Encryptor_EncryptZero2(void * thisptr,void * destination,void * pool_handle)161 SEAL_C_FUNC Encryptor_EncryptZero2(void *thisptr, void *destination, void *pool_handle)
162 {
163 Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
164 IfNullRet(encryptor, E_POINTER);
165 Ciphertext *cipher = FromVoid<Ciphertext>(destination);
166 IfNullRet(cipher, E_POINTER);
167 unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
168
169 try
170 {
171 encryptor->encrypt_zero(*cipher, *pool);
172 return S_OK;
173 }
174 catch (const invalid_argument &)
175 {
176 return E_INVALIDARG;
177 }
178 catch (const logic_error &)
179 {
180 return COR_E_INVALIDOPERATION;
181 }
182 }
183
Encryptor_EncryptSymmetric(void * thisptr,void * plaintext,bool save_seed,void * destination,void * pool_handle)184 SEAL_C_FUNC Encryptor_EncryptSymmetric(
185 void *thisptr, void *plaintext, bool save_seed, void *destination, void *pool_handle)
186 {
187 Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
188 IfNullRet(encryptor, E_POINTER);
189 Plaintext *plain = FromVoid<Plaintext>(plaintext);
190 IfNullRet(plain, E_POINTER);
191 Ciphertext *cipher = FromVoid<Ciphertext>(destination);
192 IfNullRet(cipher, E_POINTER);
193 unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
194
195 try
196 {
197 ph::encrypt_symmetric_internal(encryptor, *plain, save_seed, *cipher, *pool);
198 return S_OK;
199 }
200 catch (const invalid_argument &)
201 {
202 return E_INVALIDARG;
203 }
204 catch (const logic_error &)
205 {
206 return COR_E_INVALIDOPERATION;
207 }
208 }
209
Encryptor_EncryptZeroSymmetric1(void * thisptr,uint64_t * parms_id,bool save_seed,void * destination,void * pool_handle)210 SEAL_C_FUNC Encryptor_EncryptZeroSymmetric1(
211 void *thisptr, uint64_t *parms_id, bool save_seed, void *destination, void *pool_handle)
212 {
213 Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
214 IfNullRet(encryptor, E_POINTER);
215 IfNullRet(parms_id, E_POINTER);
216 Ciphertext *cipher = FromVoid<Ciphertext>(destination);
217 IfNullRet(cipher, E_POINTER);
218 unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
219
220 parms_id_type parms;
221 CopyParmsId(parms_id, parms);
222
223 try
224 {
225 ph::encrypt_zero_symmetric_internal(encryptor, parms, save_seed, *cipher, *pool);
226 return S_OK;
227 }
228 catch (const invalid_argument &)
229 {
230 return E_INVALIDARG;
231 }
232 catch (const logic_error &)
233 {
234 return COR_E_INVALIDOPERATION;
235 }
236 }
237
Encryptor_EncryptZeroSymmetric2(void * thisptr,bool save_seed,void * destination,void * pool_handle)238 SEAL_C_FUNC Encryptor_EncryptZeroSymmetric2(void *thisptr, bool save_seed, void *destination, void *pool_handle)
239 {
240 Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
241 IfNullRet(encryptor, E_POINTER);
242 Ciphertext *cipher = FromVoid<Ciphertext>(destination);
243 IfNullRet(cipher, E_POINTER);
244 unique_ptr<MemoryPoolHandle> pool = MemHandleFromVoid(pool_handle);
245
246 try
247 {
248 ph::encrypt_zero_symmetric_internal(encryptor, save_seed, *cipher, *pool);
249 return S_OK;
250 }
251 catch (const invalid_argument &)
252 {
253 return E_INVALIDARG;
254 }
255 catch (const logic_error &)
256 {
257 return COR_E_INVALIDOPERATION;
258 }
259 }
260
Encryptor_Destroy(void * thisptr)261 SEAL_C_FUNC Encryptor_Destroy(void *thisptr)
262 {
263 Encryptor *encryptor = FromVoid<Encryptor>(thisptr);
264 IfNullRet(encryptor, E_POINTER);
265
266 delete encryptor;
267 return S_OK;
268 }
269