Botan 3.8.1
Crypto and TLS for C&
ffi_ec.cpp
Go to the documentation of this file.
1/*
2* (C) 2025 Jack Lloyd
3* (C) 2025 Dominik Schricker
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/ffi.h>
9
10#include <botan/internal/ffi_ec.h>
11#include <botan/internal/ffi_mp.h>
12#include <botan/internal/ffi_oid.h>
13#include <botan/internal/ffi_util.h>
14#include <functional>
15
16extern "C" {
17
18using namespace Botan_FFI;
19
21 return BOTAN_FFI_CHECKED_DELETE(ec_group);
22}
23
25 if(out == nullptr) {
27 }
29 *out = 1;
30 } else {
31 *out = 0;
32 }
33 return BOTAN_FFI_SUCCESS;
34}
35
36int botan_ec_group_supports_named_group(const char* name, int* out) {
37 return ffi_guard_thunk(__func__, [=]() -> int {
38 if(name == nullptr || out == nullptr) {
40 }
42 *out = 1;
43 } else {
44 *out = 0;
45 }
46 return BOTAN_FFI_SUCCESS;
47 });
48}
49
52 botan_mp_t p,
53 botan_mp_t a,
54 botan_mp_t b,
55 botan_mp_t base_x,
56 botan_mp_t base_y,
57 botan_mp_t order) {
58 return ffi_guard_thunk(__func__, [=]() -> int {
59 if(ec_group == nullptr) {
61 }
62
63 Botan::EC_Group group(
64 safe_get(oid), safe_get(p), safe_get(a), safe_get(b), safe_get(base_x), safe_get(base_y), safe_get(order));
65
66 auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
67 *ec_group = new botan_ec_group_struct(std::move(group_ptr));
68
69 return BOTAN_FFI_SUCCESS;
70 });
71}
72
73int botan_ec_group_from_ber(botan_ec_group_t* ec_group, const uint8_t* ber, size_t ber_len) {
74 return ffi_guard_thunk(__func__, [=]() -> int {
75 if(ec_group == nullptr || ber == nullptr) {
77 }
78
79 Botan::EC_Group group(ber, ber_len);
80
81 auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
82 *ec_group = new botan_ec_group_struct(std::move(group_ptr));
83
84 return BOTAN_FFI_SUCCESS;
85 });
86}
87
88int botan_ec_group_from_pem(botan_ec_group_t* ec_group, const char* pem) {
89 return ffi_guard_thunk(__func__, [=]() -> int {
90 if(ec_group == nullptr || pem == nullptr) {
92 }
93
95
96 auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
97 *ec_group = new botan_ec_group_struct(std::move(group_ptr));
98
99 return BOTAN_FFI_SUCCESS;
100 });
101}
102
104 return ffi_guard_thunk(__func__, [=]() -> int {
105 if(ec_group == nullptr) {
107 }
108
110
111 auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
112 *ec_group = new botan_ec_group_struct(std::move(group_ptr));
113
114 return BOTAN_FFI_SUCCESS;
115 });
116}
117
118int botan_ec_group_from_name(botan_ec_group_t* ec_group, const char* name) {
119 return ffi_guard_thunk(__func__, [=]() -> int {
120 if(ec_group == nullptr || name == nullptr) {
122 }
123
125
126 auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
127 *ec_group = new botan_ec_group_struct(std::move(group_ptr));
128
129 return BOTAN_FFI_SUCCESS;
130 });
131}
132
134 return BOTAN_FFI_VISIT(ec_group,
135 [=](const auto& g) -> int { return invoke_view_callback(view, ctx, g.DER_encode()); });
136}
137
139 return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
140 return invoke_view_callback(view, ctx, g.PEM_encode(Botan::EC_Group_Encoding::NamedCurve));
141 });
142}
143
145 return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
146 if(oid == nullptr) {
148 }
149 auto oid_ptr = std::make_unique<Botan::OID>(g.get_curve_oid());
150 *oid = new botan_asn1_oid_struct(std::move(oid_ptr));
151
152 return BOTAN_FFI_SUCCESS;
153 });
154}
155
156namespace {
157int botan_ec_group_get_component(botan_mp_t* out,
158 botan_ec_group_t ec_group,
159 const std::function<const Botan::BigInt&(const Botan::EC_Group&)>& getter) {
160 return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
161 if(out == nullptr) {
163 }
164 auto val = std::make_unique<Botan::BigInt>(getter(g));
165 *out = new botan_mp_struct(std::move(val));
166 return BOTAN_FFI_SUCCESS;
167 });
168}
169} // namespace
170
172 return botan_ec_group_get_component(p, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_p(); });
173}
174
176 return botan_ec_group_get_component(a, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_a(); });
177}
178
180 return botan_ec_group_get_component(b, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_b(); });
181}
182
184 return botan_ec_group_get_component(
185 g_x, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_g_x(); });
186}
187
189 return botan_ec_group_get_component(
190 g_y, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_g_y(); });
191}
192
194 return botan_ec_group_get_component(
195 order, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_order(); });
196}
197
199 return BOTAN_FFI_VISIT(curve1_w, [=](const auto& curve1) -> int { return curve1 == safe_get(curve2_w); });
200}
201}
static EC_Group from_name(std::string_view name)
Definition ec_group.cpp:371
static EC_Group from_PEM(std::string_view pem)
Definition ec_group.cpp:414
static EC_Group from_OID(const OID &oid)
Definition ec_group.cpp:360
static bool supports_application_specific_group()
Definition ec_group.cpp:351
static bool supports_named_group(std::string_view name)
Definition ec_group.cpp:346
struct botan_asn1_oid_struct * botan_asn1_oid_t
Definition ffi.h:1117
struct botan_ec_group_struct * botan_ec_group_t
Definition ffi.h:1166
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
Definition ffi.h:159
struct botan_mp_struct * botan_mp_t
Definition ffi.h:919
void * botan_view_ctx
Definition ffi.h:150
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:130
@ BOTAN_FFI_SUCCESS
Definition ffi.h:113
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
Definition ffi.h:168
int botan_ec_group_get_g_x(botan_mp_t *g_x, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:183
int botan_ec_group_view_pem(botan_ec_group_t ec_group, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_ec.cpp:138
int botan_ec_group_get_curve_oid(botan_asn1_oid_t *oid, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:144
int botan_ec_group_from_params(botan_ec_group_t *ec_group, botan_asn1_oid_t oid, botan_mp_t p, botan_mp_t a, botan_mp_t b, botan_mp_t base_x, botan_mp_t base_y, botan_mp_t order)
Definition ffi_ec.cpp:50
int botan_ec_group_get_g_y(botan_mp_t *g_y, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:188
int botan_ec_group_get_a(botan_mp_t *a, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:175
int botan_ec_group_view_der(botan_ec_group_t ec_group, botan_view_ctx ctx, botan_view_bin_fn view)
Definition ffi_ec.cpp:133
int botan_ec_group_supports_named_group(const char *name, int *out)
Definition ffi_ec.cpp:36
int botan_ec_group_equal(botan_ec_group_t curve1_w, botan_ec_group_t curve2_w)
Definition ffi_ec.cpp:198
int botan_ec_group_get_order(botan_mp_t *order, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:193
int botan_ec_group_from_oid(botan_ec_group_t *ec_group, botan_asn1_oid_t oid)
Definition ffi_ec.cpp:103
int botan_ec_group_from_name(botan_ec_group_t *ec_group, const char *name)
Definition ffi_ec.cpp:118
int botan_ec_group_supports_application_specific_group(int *out)
Definition ffi_ec.cpp:24
int botan_ec_group_destroy(botan_ec_group_t ec_group)
Definition ffi_ec.cpp:20
int botan_ec_group_get_b(botan_mp_t *b, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:179
int botan_ec_group_get_p(botan_mp_t *p, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:171
int botan_ec_group_from_ber(botan_ec_group_t *ec_group, const uint8_t *ber, size_t ber_len)
Definition ffi_ec.cpp:73
int botan_ec_group_from_pem(botan_ec_group_t *ec_group, const char *pem)
Definition ffi_ec.cpp:88
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:145
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:164
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:166
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:67
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:83