1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT license.
3
4 // SEALNet
5 #include "seal/c/decryptor.h"
6 #include "seal/c/utilities.h"
7
8 // SEAL
9 #include "seal/decryptor.h"
10
11 using namespace std;
12 using namespace seal;
13 using namespace seal::c;
14
Decryptor_Create(void * context,void * secret_key,void ** decryptor)15 SEAL_C_FUNC Decryptor_Create(void *context, void *secret_key, void **decryptor)
16 {
17 const SEALContext *ctx = FromVoid<SEALContext>(context);
18 IfNullRet(ctx, E_POINTER);
19 SecretKey *secretKey = FromVoid<SecretKey>(secret_key);
20 IfNullRet(secretKey, E_POINTER);
21 IfNullRet(decryptor, E_POINTER);
22
23 try
24 {
25 Decryptor *decr = new Decryptor(*ctx, *secretKey);
26 *decryptor = decr;
27 return S_OK;
28 }
29 catch (const invalid_argument &)
30 {
31 return E_INVALIDARG;
32 }
33 }
34
Decryptor_Destroy(void * thisptr)35 SEAL_C_FUNC Decryptor_Destroy(void *thisptr)
36 {
37 Decryptor *decryptor = FromVoid<Decryptor>(thisptr);
38 IfNullRet(decryptor, E_POINTER);
39
40 delete decryptor;
41 return S_OK;
42 }
43
Decryptor_Decrypt(void * thisptr,void * encrypted,void * destination)44 SEAL_C_FUNC Decryptor_Decrypt(void *thisptr, void *encrypted, void *destination)
45 {
46 Decryptor *decryptor = FromVoid<Decryptor>(thisptr);
47 IfNullRet(decryptor, E_POINTER);
48 Ciphertext *encryptedptr = FromVoid<Ciphertext>(encrypted);
49 IfNullRet(encryptedptr, E_POINTER);
50 Plaintext *destinationptr = FromVoid<Plaintext>(destination);
51 IfNullRet(destinationptr, E_POINTER);
52
53 try
54 {
55 decryptor->decrypt(*encryptedptr, *destinationptr);
56 return S_OK;
57 }
58 catch (const invalid_argument &)
59 {
60 return E_INVALIDARG;
61 }
62 }
63
Decryptor_InvariantNoiseBudget(void * thisptr,void * encrypted,int * invariant_noise_budget)64 SEAL_C_FUNC Decryptor_InvariantNoiseBudget(void *thisptr, void *encrypted, int *invariant_noise_budget)
65 {
66 Decryptor *decryptor = FromVoid<Decryptor>(thisptr);
67 IfNullRet(decryptor, E_POINTER);
68 Ciphertext *encryptedptr = FromVoid<Ciphertext>(encrypted);
69 IfNullRet(encryptedptr, E_POINTER);
70 IfNullRet(invariant_noise_budget, E_POINTER);
71
72 try
73 {
74 *invariant_noise_budget = decryptor->invariant_noise_budget(*encryptedptr);
75 return S_OK;
76 }
77 catch (const invalid_argument &)
78 {
79 return E_INVALIDARG;
80 }
81 }
82