Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ffi_pk_op.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2015,2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6
7 #include <botan/ffi.h>
8 #include <botan/internal/ffi_util.h>
9 #include <botan/internal/ffi_pkey.h>
10 #include <botan/internal/ffi_rng.h>
11 #include <botan/pubkey.h>
12
13 extern "C" {
14
15 using namespace Botan_FFI;
16
17 BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_encrypt_struct, Botan::PK_Encryptor, 0x891F3FC3);
18 BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_decrypt_struct, Botan::PK_Decryptor, 0x912F3C37);
19 BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_sign_struct, Botan::PK_Signer, 0x1AF0C39F);
20 BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_verify_struct, Botan::PK_Verifier, 0x2B91F936);
21 BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_ka_struct, Botan::PK_Key_Agreement, 0x2939CAB1);
22
23 int botan_pk_op_encrypt_create(botan_pk_op_encrypt_t* op,
24 botan_pubkey_t key_obj,
25 const char* padding,
26 uint32_t flags)
27 {
28 if(op == nullptr)
29 return BOTAN_FFI_ERROR_NULL_POINTER;
30
31 if(flags != 0)
32 return BOTAN_FFI_ERROR_BAD_FLAG;
33
34 return ffi_guard_thunk(__func__, [=]() -> int {
35 *op = nullptr;
36
37 std::unique_ptr<Botan::PK_Encryptor> pk(new Botan::PK_Encryptor_EME(safe_get(key_obj), Botan::system_rng(), padding));
38 *op = new botan_pk_op_encrypt_struct(pk.release());
39 return BOTAN_FFI_SUCCESS;
40 });
41 }
42
43 int botan_pk_op_encrypt_destroy(botan_pk_op_encrypt_t op)
44 {
45 return BOTAN_FFI_CHECKED_DELETE(op);
46 }
47
48 int botan_pk_op_encrypt_output_length(botan_pk_op_encrypt_t op, size_t ptext_len, size_t* ctext_len)
49 {
50 if(ctext_len == nullptr)
51 return BOTAN_FFI_ERROR_NULL_POINTER;
52 return BOTAN_FFI_DO(Botan::PK_Encryptor, op, o, { *ctext_len = o.ciphertext_length(ptext_len); });
53 }
54
55 int botan_pk_op_encrypt(botan_pk_op_encrypt_t op,
56 botan_rng_t rng_obj,
57 uint8_t out[], size_t* out_len,
58 const uint8_t plaintext[], size_t plaintext_len)
59 {
60 return BOTAN_FFI_DO(Botan::PK_Encryptor, op, o, {
61 return write_vec_output(out, out_len, o.encrypt(plaintext, plaintext_len, safe_get(rng_obj)));
62 });
63 }
64
65 /*
66 * Public Key Decryption
67 */
68 int botan_pk_op_decrypt_create(botan_pk_op_decrypt_t* op,
69 botan_privkey_t key_obj,
70 const char* padding,
71 uint32_t flags)
72 {
73 if(op == nullptr)
74 return BOTAN_FFI_ERROR_NULL_POINTER;
75
76 if(flags != 0)
77 return BOTAN_FFI_ERROR_BAD_FLAG;
78
79 return ffi_guard_thunk(__func__, [=]() -> int {
80 *op = nullptr;
81
82 std::unique_ptr<Botan::PK_Decryptor> pk(new Botan::PK_Decryptor_EME(safe_get(key_obj), Botan::system_rng(), padding));
83 *op = new botan_pk_op_decrypt_struct(pk.release());
84 return BOTAN_FFI_SUCCESS;
85 });
86 }
87
88 int botan_pk_op_decrypt_destroy(botan_pk_op_decrypt_t op)
89 {
90 return BOTAN_FFI_CHECKED_DELETE(op);
91 }
92
93 int botan_pk_op_decrypt_output_length(botan_pk_op_decrypt_t op, size_t ctext_len, size_t* ptext_len)
94 {
95 if(ptext_len == nullptr)
96 return BOTAN_FFI_ERROR_NULL_POINTER;
97 return BOTAN_FFI_DO(Botan::PK_Decryptor, op, o, { *ptext_len = o.plaintext_length(ctext_len); });
98 }
99
100 int botan_pk_op_decrypt(botan_pk_op_decrypt_t op,
101 uint8_t out[], size_t* out_len,
102 const uint8_t ciphertext[], size_t ciphertext_len)
103 {
104 return BOTAN_FFI_DO(Botan::PK_Decryptor, op, o, {
105 return write_vec_output(out, out_len, o.decrypt(ciphertext, ciphertext_len));
106 });
107 }
108
109 /*
110 * Signature Generation
111 */
112 int botan_pk_op_sign_create(botan_pk_op_sign_t* op,
113 botan_privkey_t key_obj,
114 const char* hash,
115 uint32_t flags)
116 {
117 if(op == nullptr)
118 return BOTAN_FFI_ERROR_NULL_POINTER;
119
120 if(flags != 0)
121 return BOTAN_FFI_ERROR_BAD_FLAG;
122
123 return ffi_guard_thunk(__func__, [=]() -> int {
124 *op = nullptr;
125
126 std::unique_ptr<Botan::PK_Signer> pk(new Botan::PK_Signer(safe_get(key_obj), Botan::system_rng(), hash));
127 *op = new botan_pk_op_sign_struct(pk.release());
128 return BOTAN_FFI_SUCCESS;
129 });
130 }
131
132 int botan_pk_op_sign_destroy(botan_pk_op_sign_t op)
133 {
134 return BOTAN_FFI_CHECKED_DELETE(op);
135 }
136
137 int botan_pk_op_sign_output_length(botan_pk_op_sign_t op, size_t* sig_len)
138 {
139 if(sig_len == nullptr)
140 return BOTAN_FFI_ERROR_NULL_POINTER;
141
142 return BOTAN_FFI_DO(Botan::PK_Signer, op, o, { *sig_len = o.signature_length(); });
143 }
144
145 int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len)
146 {
147 return BOTAN_FFI_DO(Botan::PK_Signer, op, o, { o.update(in, in_len); });
148 }
149
150 int botan_pk_op_sign_finish(botan_pk_op_sign_t op, botan_rng_t rng_obj, uint8_t out[], size_t* out_len)
151 {
152 return BOTAN_FFI_DO(Botan::PK_Signer, op, o, {
153 return write_vec_output(out, out_len, o.signature(safe_get(rng_obj)));
154 });
155 }
156
157 int botan_pk_op_verify_create(botan_pk_op_verify_t* op,
158 botan_pubkey_t key_obj,
159 const char* hash,
160 uint32_t flags)
161 {
162 if(op == nullptr)
163 return BOTAN_FFI_ERROR_NULL_POINTER;
164
165 if(flags != 0)
166 return BOTAN_FFI_ERROR_BAD_FLAG;
167
168 return ffi_guard_thunk(__func__, [=]() -> int {
169 *op = nullptr;
170 std::unique_ptr<Botan::PK_Verifier> pk(new Botan::PK_Verifier(safe_get(key_obj), hash));
171 *op = new botan_pk_op_verify_struct(pk.release());
172 return BOTAN_FFI_SUCCESS;
173 });
174 }
175
176 int botan_pk_op_verify_destroy(botan_pk_op_verify_t op)
177 {
178 return BOTAN_FFI_CHECKED_DELETE(op);
179 }
180
181 int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len)
182 {
183 return BOTAN_FFI_DO(Botan::PK_Verifier, op, o, { o.update(in, in_len); });
184 }
185
186 int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len)
187 {
188 return BOTAN_FFI_RETURNING(Botan::PK_Verifier, op, o, {
189 const bool legit = o.check_signature(sig, sig_len);
190
191 if(legit)
192 return BOTAN_FFI_SUCCESS;
193 else
194 return BOTAN_FFI_INVALID_VERIFIER;
195 });
196 }
197
198 int botan_pk_op_key_agreement_create(botan_pk_op_ka_t* op,
199 botan_privkey_t key_obj,
200 const char* kdf,
201 uint32_t flags)
202 {
203 if(op == nullptr)
204 return BOTAN_FFI_ERROR_NULL_POINTER;
205
206 if(flags != 0)
207 return BOTAN_FFI_ERROR_BAD_FLAG;
208
209 return ffi_guard_thunk(__func__, [=]() -> int {
210 *op = nullptr;
211 std::unique_ptr<Botan::PK_Key_Agreement> pk(new Botan::PK_Key_Agreement(safe_get(key_obj), Botan::system_rng(), kdf));
212 *op = new botan_pk_op_ka_struct(pk.release());
213 return BOTAN_FFI_SUCCESS;
214 });
215 }
216
217 int botan_pk_op_key_agreement_destroy(botan_pk_op_ka_t op)
218 {
219 return BOTAN_FFI_CHECKED_DELETE(op);
220 }
221
222 int botan_pk_op_key_agreement_export_public(botan_privkey_t key,
223 uint8_t out[], size_t* out_len)
224 {
225 return BOTAN_FFI_DO(Botan::Private_Key, key, k, {
226 if(auto kak = dynamic_cast<const Botan::PK_Key_Agreement_Key*>(&k))
227 return write_vec_output(out, out_len, kak->public_value());
228 return BOTAN_FFI_ERROR_BAD_FLAG;
229 });
230 }
231
232 int botan_pk_op_key_agreement_size(botan_pk_op_ka_t op, size_t* out_len)
233 {
234 return BOTAN_FFI_DO(Botan::PK_Key_Agreement, op, o, {
235 if(out_len == nullptr)
236 return BOTAN_FFI_ERROR_NULL_POINTER;
237 *out_len = o.agreed_value_size();
238 });
239 }
240
241 int botan_pk_op_key_agreement(botan_pk_op_ka_t op,
242 uint8_t out[], size_t* out_len,
243 const uint8_t other_key[], size_t other_key_len,
244 const uint8_t salt[], size_t salt_len)
245 {
246 return BOTAN_FFI_DO(Botan::PK_Key_Agreement, op, o, {
247 auto k = o.derive_key(*out_len, other_key, other_key_len, salt, salt_len).bits_of();
248 return write_vec_output(out, out_len, k);
249 });
250 }
251
252 }
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
botan_pk_op_sign_destroy
int botan_pk_op_sign_destroy(botan_pk_op_sign_t op)
Definition: ffi_pk_op.cpp:132
Botan::OctetString::bits_of
secure_vector< uint8_t > bits_of() const
Definition: symkey.h:31
Botan::system_rng
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:279
botan_pk_op_sign_finish
int botan_pk_op_sign_finish(botan_pk_op_sign_t op, botan_rng_t rng_obj, uint8_t out[], size_t *out_len)
Definition: ffi_pk_op.cpp:150
botan_pk_op_key_agreement_destroy
int botan_pk_op_key_agreement_destroy(botan_pk_op_ka_t op)
Definition: ffi_pk_op.cpp:217
Botan::PK_Signer::signature_length
size_t signature_length() const
Definition: pubkey.cpp:270
Botan::Private_Key
Definition: pk_keys.h:181
botan_pk_op_encrypt_t
struct botan_pk_op_encrypt_struct * botan_pk_op_encrypt_t
Definition: ffi.h:1360
botan_pk_op_key_agreement_create
int botan_pk_op_key_agreement_create(botan_pk_op_ka_t *op, botan_privkey_t key_obj, const char *kdf, uint32_t flags)
Definition: ffi_pk_op.cpp:198
botan_pk_op_key_agreement
int botan_pk_op_key_agreement(botan_pk_op_ka_t op, uint8_t out[], size_t *out_len, const uint8_t other_key[], size_t other_key_len, const uint8_t salt[], size_t salt_len)
Definition: ffi_pk_op.cpp:241
Botan::PK_Key_Agreement::derive_key
SymmetricKey derive_key(size_t key_len, const uint8_t in[], size_t in_len, const uint8_t params[], size_t params_len) const
Definition: pubkey.cpp:218
botan_pk_op_sign_t
struct botan_pk_op_sign_struct * botan_pk_op_sign_t
Definition: ffi.h:1409
Botan::PKCS11::flags
Flags flags(Flag flags)
Definition: p11.h:858
BOTAN_FFI_DECLARE_STRUCT
BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_encrypt_struct, Botan::PK_Encryptor, 0x891F3FC3)
Botan_FFI::write_vec_output
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition: ffi_util.h:155
Botan::PK_Verifier::update
void update(uint8_t in)
Definition: pubkey.h:347
botan_pk_op_sign_update
int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len)
Definition: ffi_pk_op.cpp:145
botan_pk_op_key_agreement_export_public
int botan_pk_op_key_agreement_export_public(botan_privkey_t key, uint8_t out[], size_t *out_len)
Definition: ffi_pk_op.cpp:222
botan_pk_op_decrypt_create
int botan_pk_op_decrypt_create(botan_pk_op_decrypt_t *op, botan_privkey_t key_obj, const char *padding, uint32_t flags)
Definition: ffi_pk_op.cpp:68
botan_pk_op_verify_create
int botan_pk_op_verify_create(botan_pk_op_verify_t *op, botan_pubkey_t key_obj, const char *hash, uint32_t flags)
Definition: ffi_pk_op.cpp:157
botan_pk_op_encrypt_output_length
int botan_pk_op_encrypt_output_length(botan_pk_op_encrypt_t op, size_t ptext_len, size_t *ctext_len)
Definition: ffi_pk_op.cpp:48
botan_pk_op_decrypt_t
struct botan_pk_op_decrypt_struct * botan_pk_op_decrypt_t
Definition: ffi.h:1386
botan_pk_op_verify_finish
int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len)
Definition: ffi_pk_op.cpp:186
botan_pk_op_decrypt
int botan_pk_op_decrypt(botan_pk_op_decrypt_t op, uint8_t out[], size_t *out_len, const uint8_t ciphertext[], size_t ciphertext_len)
Definition: ffi_pk_op.cpp:100
Botan::PK_Decryptor_EME
Definition: pubkey.h:579
BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:76
Botan::PK_Signer::update
void update(uint8_t in)
Definition: pubkey.h:238
botan_pk_op_verify_update
int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len)
Definition: ffi_pk_op.cpp:181
salt_len
size_t salt_len
Definition: x509_obj.cpp:25
BOTAN_FFI_RETURNING
#define BOTAN_FFI_RETURNING(T, obj, param, block)
Definition: ffi_util.h:101
Botan_FFI
Definition: ffi.cpp:17
botan_pk_op_verify_t
struct botan_pk_op_verify_struct * botan_pk_op_verify_t
Definition: ffi.h:1433
Botan::PK_Verifier
Definition: pubkey.h:298
Botan::PK_Encryptor_EME
Definition: pubkey.h:529
BOTAN_FFI_CHECKED_DELETE
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:129
botan_privkey_t
struct botan_privkey_struct * botan_privkey_t
Definition: ffi.h:951
Botan::PK_Key_Agreement
Definition: pubkey.h:413
botan_pk_op_key_agreement_size
int botan_pk_op_key_agreement_size(botan_pk_op_ka_t op, size_t *out_len)
Definition: ffi_pk_op.cpp:232
Botan_FFI::safe_get
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:61
botan_pk_op_encrypt_create
int botan_pk_op_encrypt_create(botan_pk_op_encrypt_t *op, botan_pubkey_t key_obj, const char *padding, uint32_t flags)
Definition: ffi_pk_op.cpp:23
Botan::PK_Verifier::check_signature
bool check_signature(const uint8_t sig[], size_t length)
Definition: pubkey.cpp:336
botan_pk_op_decrypt_destroy
int botan_pk_op_decrypt_destroy(botan_pk_op_decrypt_t op)
Definition: ffi_pk_op.cpp:88
BOTAN_FFI_ERROR_BAD_FLAG
Definition: ffi.h:75
botan_pk_op_sign_create
int botan_pk_op_sign_create(botan_pk_op_sign_t *op, botan_privkey_t key_obj, const char *hash, uint32_t flags)
Definition: ffi_pk_op.cpp:112
botan_pk_op_ka_t
struct botan_pk_op_ka_struct * botan_pk_op_ka_t
Definition: ffi.h:1452
BOTAN_FFI_SUCCESS
Definition: ffi.h:62
botan_pk_op_decrypt_output_length
int botan_pk_op_decrypt_output_length(botan_pk_op_decrypt_t op, size_t ctext_len, size_t *ptext_len)
Definition: ffi_pk_op.cpp:93
botan_pk_op_encrypt_destroy
int botan_pk_op_encrypt_destroy(botan_pk_op_encrypt_t op)
Definition: ffi_pk_op.cpp:43
Botan::PK_Signer
Definition: pubkey.h:166
botan_pubkey_t
struct botan_pubkey_struct * botan_pubkey_t
Definition: ffi.h:1098
Botan::PK_Decryptor::plaintext_length
virtual size_t plaintext_length(size_t ctext_len) const =0
BOTAN_FFI_INVALID_VERIFIER
Definition: ffi.h:63
BOTAN_FFI_DO
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92
botan_pk_op_verify_destroy
int botan_pk_op_verify_destroy(botan_pk_op_verify_t op)
Definition: ffi_pk_op.cpp:176
Botan::PK_Decryptor
Definition: pubkey.h:84
Botan::PK_Encryptor::ciphertext_length
virtual size_t ciphertext_length(size_t ctext_len) const =0
hash
MechanismType hash
Definition: p11_mechanism.cpp:64
botan_rng_t
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:189
Botan::PK_Encryptor
Definition: pubkey.h:29
botan_pk_op_encrypt
int botan_pk_op_encrypt(botan_pk_op_encrypt_t op, botan_rng_t rng_obj, uint8_t out[], size_t *out_len, const uint8_t plaintext[], size_t plaintext_len)
Definition: ffi_pk_op.cpp:55
botan_pk_op_sign_output_length
int botan_pk_op_sign_output_length(botan_pk_op_sign_t op, size_t *sig_len)
Definition: ffi_pk_op.cpp:137