Botan 3.9.0
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 return ffi_new_object(ec_group, std::move(group_ptr));
68 });
69}
70
71int botan_ec_group_from_ber(botan_ec_group_t* ec_group, const uint8_t* ber, size_t ber_len) {
72 return ffi_guard_thunk(__func__, [=]() -> int {
73 if(ec_group == nullptr || ber == nullptr) {
75 }
76
77 Botan::EC_Group group(ber, ber_len);
78
79 auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
80 return ffi_new_object(ec_group, std::move(group_ptr));
81 });
82}
83
84int botan_ec_group_from_pem(botan_ec_group_t* ec_group, const char* pem) {
85 return ffi_guard_thunk(__func__, [=]() -> int {
86 if(ec_group == nullptr || pem == nullptr) {
88 }
89
91
92 auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
93 return ffi_new_object(ec_group, std::move(group_ptr));
94 });
95}
96
98 return ffi_guard_thunk(__func__, [=]() -> int {
99 if(ec_group == nullptr) {
101 }
102
104
105 auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
106 return ffi_new_object(ec_group, std::move(group_ptr));
107 });
108}
109
110int botan_ec_group_from_name(botan_ec_group_t* ec_group, const char* name) {
111 return ffi_guard_thunk(__func__, [=]() -> int {
112 if(ec_group == nullptr || name == nullptr) {
114 }
115
117
118 auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
119 return ffi_new_object(ec_group, std::move(group_ptr));
120 });
121}
122
124 return BOTAN_FFI_VISIT(ec_group,
125 [=](const auto& g) -> int { return invoke_view_callback(view, ctx, g.DER_encode()); });
126}
127
129 return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
130 return invoke_view_callback(view, ctx, g.PEM_encode(Botan::EC_Group_Encoding::NamedCurve));
131 });
132}
133
135 return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
136 if(oid == nullptr) {
138 }
139 auto oid_ptr = std::make_unique<Botan::OID>(g.get_curve_oid());
140 return ffi_new_object(oid, std::move(oid_ptr));
141 });
142}
143
144namespace {
145int botan_ec_group_get_component(botan_mp_t* out,
146 botan_ec_group_t ec_group,
147 const std::function<const Botan::BigInt&(const Botan::EC_Group&)>& getter) {
148 return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
149 if(out == nullptr) {
151 }
152 auto val = std::make_unique<Botan::BigInt>(getter(g));
153 return ffi_new_object(out, std::move(val));
154 });
155}
156} // namespace
157
159 return botan_ec_group_get_component(p, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_p(); });
160}
161
163 return botan_ec_group_get_component(a, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_a(); });
164}
165
167 return botan_ec_group_get_component(b, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_b(); });
168}
169
171 return botan_ec_group_get_component(
172 g_x, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_g_x(); });
173}
174
176 return botan_ec_group_get_component(
177 g_y, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_g_y(); });
178}
179
181 return botan_ec_group_get_component(
182 order, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_order(); });
183}
184
186 return BOTAN_FFI_VISIT(curve1_w, [=](const auto& curve1) -> int { return curve1 == safe_get(curve2_w); });
187}
188}
static EC_Group from_name(std::string_view name)
Definition ec_group.cpp:384
static EC_Group from_PEM(std::string_view pem)
Definition ec_group.cpp:427
static EC_Group from_OID(const OID &oid)
Definition ec_group.cpp:373
static bool supports_application_specific_group()
Definition ec_group.cpp:355
static bool supports_named_group(std::string_view name)
Definition ec_group.cpp:350
struct botan_asn1_oid_struct * botan_asn1_oid_t
Definition ffi.h:1121
struct botan_ec_group_struct * botan_ec_group_t
Definition ffi.h:1170
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
Definition ffi.h:161
struct botan_mp_struct * botan_mp_t
Definition ffi.h:921
void * botan_view_ctx
Definition ffi.h:152
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
Definition ffi.h:170
int botan_ec_group_get_g_x(botan_mp_t *g_x, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:170
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:128
int botan_ec_group_get_curve_oid(botan_asn1_oid_t *oid, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:134
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:175
int botan_ec_group_get_a(botan_mp_t *a, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:162
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:123
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:185
int botan_ec_group_get_order(botan_mp_t *order, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:180
int botan_ec_group_from_oid(botan_ec_group_t *ec_group, botan_asn1_oid_t oid)
Definition ffi_ec.cpp:97
int botan_ec_group_from_name(botan_ec_group_t *ec_group, const char *name)
Definition ffi_ec.cpp:110
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:166
int botan_ec_group_get_p(botan_mp_t *p, botan_ec_group_t ec_group)
Definition ffi_ec.cpp:158
int botan_ec_group_from_ber(botan_ec_group_t *ec_group, const uint8_t *ber, size_t ber_len)
Definition ffi_ec.cpp:71
int botan_ec_group_from_pem(botan_ec_group_t *ec_group, const char *pem)
Definition ffi_ec.cpp:84
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:185
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:187
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95