Botan 3.9.0
Crypto and TLS for C&
ffi_ec.cpp File Reference
#include <botan/ffi.h>
#include <botan/internal/ffi_ec.h>
#include <botan/internal/ffi_mp.h>
#include <botan/internal/ffi_oid.h>
#include <botan/internal/ffi_util.h>
#include <functional>

Go to the source code of this file.

Functions

int botan_ec_group_destroy (botan_ec_group_t ec_group)
int botan_ec_group_equal (botan_ec_group_t curve1_w, botan_ec_group_t curve2_w)
int botan_ec_group_from_ber (botan_ec_group_t *ec_group, const uint8_t *ber, size_t ber_len)
int botan_ec_group_from_name (botan_ec_group_t *ec_group, const char *name)
int botan_ec_group_from_oid (botan_ec_group_t *ec_group, botan_asn1_oid_t oid)
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)
int botan_ec_group_from_pem (botan_ec_group_t *ec_group, const char *pem)
int botan_ec_group_get_a (botan_mp_t *a, botan_ec_group_t ec_group)
int botan_ec_group_get_b (botan_mp_t *b, botan_ec_group_t ec_group)
int botan_ec_group_get_curve_oid (botan_asn1_oid_t *oid, botan_ec_group_t ec_group)
int botan_ec_group_get_g_x (botan_mp_t *g_x, botan_ec_group_t ec_group)
int botan_ec_group_get_g_y (botan_mp_t *g_y, botan_ec_group_t ec_group)
int botan_ec_group_get_order (botan_mp_t *order, botan_ec_group_t ec_group)
int botan_ec_group_get_p (botan_mp_t *p, botan_ec_group_t ec_group)
int botan_ec_group_supports_application_specific_group (int *out)
int botan_ec_group_supports_named_group (const char *name, int *out)
int botan_ec_group_view_der (botan_ec_group_t ec_group, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_ec_group_view_pem (botan_ec_group_t ec_group, botan_view_ctx ctx, botan_view_str_fn view)

Function Documentation

◆ botan_ec_group_destroy()

int botan_ec_group_destroy ( botan_ec_group_t ec_group)
Returns
negative number on error, or zero on success

Definition at line 20 of file ffi_ec.cpp.

20 {
21 return BOTAN_FFI_CHECKED_DELETE(ec_group);
22}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:185

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_ec_group_equal()

int botan_ec_group_equal ( botan_ec_group_t curve1,
botan_ec_group_t curve2 )
Returns
0 if curve1 != curve2
1 if curve1 == curve2
negative number on error

Definition at line 185 of file ffi_ec.cpp.

185 {
186 return BOTAN_FFI_VISIT(curve1_w, [=](const auto& curve1) -> int { return curve1 == safe_get(curve2_w); });
187}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_ec_group_from_ber()

int botan_ec_group_from_ber ( botan_ec_group_t * ec_group,
const uint8_t * ber,
size_t ber_len )

Decode a BER encoded ECC domain parameter set

Parameters
EC Groupthe new object will be placed here
berencoding
ber_lensize of the encoding in bytes
Returns
negative number on error, or zero on success

Definition at line 71 of file ffi_ec.cpp.

71 {
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}
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
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

References BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_ec_group_from_name()

int botan_ec_group_from_name ( botan_ec_group_t * ec_group,
const char * name )

Initialize an EC Group from a common group name (eg "secp256r1")

Parameters
EC Groupthe new object will be placed here
namea known group name
Returns
negative number on error, or zero on success

Definition at line 110 of file ffi_ec.cpp.

110 {
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}
static EC_Group from_name(std::string_view name)
Definition ec_group.cpp:384

References BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan::EC_Group::from_name().

◆ botan_ec_group_from_oid()

int botan_ec_group_from_oid ( botan_ec_group_t * ec_group,
botan_asn1_oid_t oid )

Initialize an EC Group from a group named by an object identifier

Parameters
EC Groupthe new object will be placed here
oida known OID
Returns
negative number on error, or zero on success

Definition at line 97 of file ffi_ec.cpp.

97 {
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}
static EC_Group from_OID(const OID &oid)
Definition ec_group.cpp:373

References BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan::EC_Group::from_OID(), and Botan_FFI::safe_get().

◆ botan_ec_group_from_params()

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 )

Create a new EC Group from parameters

Warning
use only elliptic curve parameters that you trust
Parameters
EC Groupthe new object will be placed here
pthe elliptic curve prime (at most 521 bits)
athe elliptic curve a param
bthe elliptic curve b param
base_xthe x coordinate of the group generator
base_ythe y coordinate of the group generator
orderthe order of the group
Returns
negative number on error, or zero on success

Definition at line 50 of file ffi_ec.cpp.

57 {
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}

References BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_ec_group_from_pem()

int botan_ec_group_from_pem ( botan_ec_group_t * ec_group,
const char * pem )

Initialize an EC Group from the PEM/ASN.1 encoding

Parameters
EC Groupthe new object will be placed here
PEMencoding
Returns
negative number on error, or zero on success

Definition at line 84 of file ffi_ec.cpp.

84 {
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}
static EC_Group from_PEM(std::string_view pem)
Definition ec_group.cpp:427

References BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan::EC_Group::from_PEM().

◆ botan_ec_group_get_a()

int botan_ec_group_get_a ( botan_mp_t * a,
botan_ec_group_t ec_group )

Get the a parameter of the elliptic curve equation

Definition at line 162 of file ffi_ec.cpp.

162 {
163 return botan_ec_group_get_component(a, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_a(); });
164}

◆ botan_ec_group_get_b()

int botan_ec_group_get_b ( botan_mp_t * b,
botan_ec_group_t ec_group )

Get the b parameter of the elliptic curve equation

Definition at line 166 of file ffi_ec.cpp.

166 {
167 return botan_ec_group_get_component(b, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_b(); });
168}

◆ botan_ec_group_get_curve_oid()

int botan_ec_group_get_curve_oid ( botan_asn1_oid_t * oid,
botan_ec_group_t ec_group )

Get the curve OID of an EC Group

Definition at line 134 of file ffi_ec.cpp.

134 {
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}

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, and Botan_FFI::ffi_new_object().

◆ botan_ec_group_get_g_x()

int botan_ec_group_get_g_x ( botan_mp_t * g_x,
botan_ec_group_t ec_group )

Get the x coordinate of the base point

Definition at line 170 of file ffi_ec.cpp.

170 {
171 return botan_ec_group_get_component(
172 g_x, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_g_x(); });
173}

◆ botan_ec_group_get_g_y()

int botan_ec_group_get_g_y ( botan_mp_t * g_y,
botan_ec_group_t ec_group )

Get the y coordinate of the base point

Definition at line 175 of file ffi_ec.cpp.

175 {
176 return botan_ec_group_get_component(
177 g_y, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_g_y(); });
178}

◆ botan_ec_group_get_order()

int botan_ec_group_get_order ( botan_mp_t * order,
botan_ec_group_t ec_group )

Get the order of the base point

Definition at line 180 of file ffi_ec.cpp.

180 {
181 return botan_ec_group_get_component(
182 order, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_order(); });
183}

◆ botan_ec_group_get_p()

int botan_ec_group_get_p ( botan_mp_t * p,
botan_ec_group_t ec_group )

Get the prime modulus of the field

Definition at line 158 of file ffi_ec.cpp.

158 {
159 return botan_ec_group_get_component(p, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_p(); });
160}

◆ botan_ec_group_supports_application_specific_group()

int botan_ec_group_supports_application_specific_group ( int * out)

Checks if in this build configuration it is possible to register an application specific elliptic curve and sets

Parameters
outto 1 if so, 0 otherwise
Returns
0 on success, a negative value on failure

Definition at line 24 of file ffi_ec.cpp.

24 {
25 if(out == nullptr) {
27 }
29 *out = 1;
30 } else {
31 *out = 0;
32 }
33 return BOTAN_FFI_SUCCESS;
34}
static bool supports_application_specific_group()
Definition ec_group.cpp:355
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and Botan::EC_Group::supports_application_specific_group().

◆ botan_ec_group_supports_named_group()

int botan_ec_group_supports_named_group ( const char * name,
int * out )

Checks if in this build configuration botan_ec_group_from_name(group_ptr, name) will succeed and sets

Parameters
outto 1 if so, 0 otherwise.
Returns
negative number on error, or zero on success

Definition at line 36 of file ffi_ec.cpp.

36 {
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}
static bool supports_named_group(std::string_view name)
Definition ec_group.cpp:350

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, Botan_FFI::ffi_guard_thunk(), and Botan::EC_Group::supports_named_group().

◆ botan_ec_group_view_der()

int botan_ec_group_view_der ( botan_ec_group_t ec_group,
botan_view_ctx ctx,
botan_view_bin_fn view )

View an EC Group in DER encoding

Definition at line 123 of file ffi_ec.cpp.

123 {
124 return BOTAN_FFI_VISIT(ec_group,
125 [=](const auto& g) -> int { return invoke_view_callback(view, ctx, g.DER_encode()); });
126}
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:187

References BOTAN_FFI_VISIT, and Botan_FFI::invoke_view_callback().

◆ botan_ec_group_view_pem()

int botan_ec_group_view_pem ( botan_ec_group_t ec_group,
botan_view_ctx ctx,
botan_view_str_fn view )

View an EC Group in PEM encoding

Definition at line 128 of file ffi_ec.cpp.

128 {
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}

References BOTAN_FFI_VISIT, Botan_FFI::invoke_view_callback(), and Botan::NamedCurve.