• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 24648292556

19 Apr 2026 10:53PM UTC coverage: 89.474% (+0.03%) from 89.442%
24648292556

push

github

web-flow
Merge pull request #5536 from randombit/jack/x509-misc

Various PKIX optimizations and bug fixes

106453 of 118977 relevant lines covered (89.47%)

11452293.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.43
/src/lib/ffi/ffi_ec.cpp
1
/*
2
* (C) 2025 Jack Lloyd
3
* (C) 2025,2026 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_rng.h>
14
#include <botan/internal/ffi_util.h>
15
#include <functional>
16

17
extern "C" {
18

19
using namespace Botan_FFI;
20

21
int botan_ec_group_destroy(botan_ec_group_t ec_group) {
18✔
22
   return BOTAN_FFI_CHECKED_DELETE(ec_group);
18✔
23
}
24

25
int botan_ec_group_supports_application_specific_group(int* out) {
3✔
26
   if(out == nullptr) {
3✔
27
      return BOTAN_FFI_ERROR_NULL_POINTER;
28
   }
29
   if(Botan::EC_Group::supports_application_specific_group()) {
3✔
30
      *out = 1;
3✔
31
   } else {
32
      *out = 0;
×
33
   }
34
   return BOTAN_FFI_SUCCESS;
35
}
36

37
int botan_ec_group_supports_named_group(const char* name, int* out) {
8✔
38
   return ffi_guard_thunk(__func__, [=]() -> int {
8✔
39
      if(name == nullptr || out == nullptr) {
8✔
40
         return BOTAN_FFI_ERROR_NULL_POINTER;
41
      }
42
      if(Botan::EC_Group::supports_named_group(name)) {
8✔
43
         *out = 1;
8✔
44
      } else {
45
         *out = 0;
×
46
      }
47
      return BOTAN_FFI_SUCCESS;
48
   });
8✔
49
}
50

51
int botan_ec_group_from_params(botan_ec_group_t* ec_group,
2✔
52
                               botan_asn1_oid_t oid,
53
                               botan_mp_t p,
54
                               botan_mp_t a,
55
                               botan_mp_t b,
56
                               botan_mp_t base_x,
57
                               botan_mp_t base_y,
58
                               botan_mp_t order) {
59
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
60
      if(ec_group == nullptr) {
2✔
61
         return BOTAN_FFI_ERROR_NULL_POINTER;
62
      }
63

64
      Botan::EC_Group group(
2✔
65
         safe_get(oid), safe_get(p), safe_get(a), safe_get(b), safe_get(base_x), safe_get(base_y), safe_get(order));
2✔
66

67
      auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
2✔
68
      return ffi_new_object(ec_group, std::move(group_ptr));
2✔
69
   });
2✔
70
}
71

72
int botan_ec_group_from_ber(botan_ec_group_t* ec_group, const uint8_t* ber, size_t ber_len) {
2✔
73
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
74
      if(ec_group == nullptr || ber == nullptr) {
2✔
75
         return BOTAN_FFI_ERROR_NULL_POINTER;
76
      }
77

78
      Botan::EC_Group group(ber, ber_len);
2✔
79

80
      auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
2✔
81
      return ffi_new_object(ec_group, std::move(group_ptr));
2✔
82
   });
2✔
83
}
84

85
int botan_ec_group_from_pem(botan_ec_group_t* ec_group, const char* pem) {
3✔
86
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
87
      if(ec_group == nullptr || pem == nullptr) {
3✔
88
         return BOTAN_FFI_ERROR_NULL_POINTER;
89
      }
90

91
      Botan::EC_Group group = Botan::EC_Group::from_PEM(pem);
3✔
92

93
      auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
3✔
94
      return ffi_new_object(ec_group, std::move(group_ptr));
3✔
95
   });
3✔
96
}
97

98
int botan_ec_group_from_oid(botan_ec_group_t* ec_group, botan_asn1_oid_t oid) {
3✔
99
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
100
      if(ec_group == nullptr) {
3✔
101
         return BOTAN_FFI_ERROR_NULL_POINTER;
102
      }
103

104
      Botan::EC_Group group = Botan::EC_Group::from_OID(safe_get(oid));
3✔
105

106
      auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
2✔
107
      return ffi_new_object(ec_group, std::move(group_ptr));
2✔
108
   });
2✔
109
}
110

111
int botan_ec_group_from_name(botan_ec_group_t* ec_group, const char* name) {
7✔
112
   return ffi_guard_thunk(__func__, [=]() -> int {
7✔
113
      if(ec_group == nullptr || name == nullptr) {
7✔
114
         return BOTAN_FFI_ERROR_NULL_POINTER;
115
      }
116

117
      Botan::EC_Group group = Botan::EC_Group::from_name(name);
7✔
118

119
      auto group_ptr = std::make_unique<Botan::EC_Group>(std::move(group));
6✔
120
      return ffi_new_object(ec_group, std::move(group_ptr));
6✔
121
   });
6✔
122
}
123

124
int botan_ec_group_unregister(botan_asn1_oid_t oid) {
8✔
125
   return BOTAN_FFI_VISIT(oid, [=](const auto& o) -> int { return Botan::EC_Group::unregister(o) ? 1 : 0; });
16✔
126
}
127

128
int botan_ec_group_view_der(botan_ec_group_t ec_group, botan_view_ctx ctx, botan_view_bin_fn view) {
2✔
129
   return BOTAN_FFI_VISIT(ec_group,
8✔
130
                          [=](const auto& g) -> int { return invoke_view_callback(view, ctx, g.DER_encode()); });
131
}
132

133
int botan_ec_group_view_pem(botan_ec_group_t ec_group, botan_view_ctx ctx, botan_view_str_fn view) {
2✔
134
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
6✔
135
      return invoke_view_callback(view, ctx, g.PEM_encode(Botan::EC_Group_Encoding::NamedCurve));
136
   });
137
}
138

139
int botan_ec_group_get_curve_oid(botan_asn1_oid_t* oid, botan_ec_group_t ec_group) {
4✔
140
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
8✔
141
      if(oid == nullptr) {
142
         return BOTAN_FFI_ERROR_NULL_POINTER;
143
      }
144
      auto oid_ptr = std::make_unique<Botan::OID>(g.get_curve_oid());
145
      return ffi_new_object(oid, std::move(oid_ptr));
146
   });
147
}
148

149
namespace {
150
int botan_ec_group_get_component(botan_mp_t* out,
28✔
151
                                 botan_ec_group_t ec_group,
152
                                 const std::function<const Botan::BigInt&(const Botan::EC_Group&)>& getter) {
153
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
112✔
154
      if(out == nullptr) {
155
         return BOTAN_FFI_ERROR_NULL_POINTER;
156
      }
157
      auto val = std::make_unique<Botan::BigInt>(getter(g));
158
      return ffi_new_object(out, std::move(val));
159
   });
160
}
161
}  // namespace
162

163
int botan_ec_group_get_p(botan_mp_t* p, botan_ec_group_t ec_group) {
6✔
164
   return botan_ec_group_get_component(p, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_p(); });
18✔
165
}
166

167
int botan_ec_group_get_a(botan_mp_t* a, botan_ec_group_t ec_group) {
4✔
168
   return botan_ec_group_get_component(a, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_a(); });
12✔
169
}
170

171
int botan_ec_group_get_b(botan_mp_t* b, botan_ec_group_t ec_group) {
4✔
172
   return botan_ec_group_get_component(b, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_b(); });
12✔
173
}
174

175
int botan_ec_group_get_g_x(botan_mp_t* g_x, botan_ec_group_t ec_group) {
4✔
176
   return botan_ec_group_get_component(
4✔
177
      g_x, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_g_x(); });
8✔
178
}
179

180
int botan_ec_group_get_g_y(botan_mp_t* g_y, botan_ec_group_t ec_group) {
4✔
181
   return botan_ec_group_get_component(
4✔
182
      g_y, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_g_y(); });
8✔
183
}
184

185
int botan_ec_group_get_order(botan_mp_t* order, botan_ec_group_t ec_group) {
6✔
186
   return botan_ec_group_get_component(
6✔
187
      order, ec_group, [](const auto& g) -> const Botan::BigInt& { return g.get_order(); });
12✔
188
}
189

190
int botan_ec_group_equal(botan_ec_group_t curve1_w, botan_ec_group_t curve2_w) {
13✔
191
   return BOTAN_FFI_VISIT(curve1_w, [=](const auto& curve1) -> int { return curve1 == safe_get(curve2_w); });
26✔
192
}
193

194
// ec scalars
195

196
int botan_ec_scalar_destroy(botan_ec_scalar_t ec_scalar) {
7✔
197
   return BOTAN_FFI_CHECKED_DELETE(ec_scalar);
7✔
198
}
199

200
int botan_ec_scalar_random(botan_ec_scalar_t* ec_scalar, botan_ec_group_t ec_group, botan_rng_t rng) {
1✔
201
   if(Botan::any_null_pointers(ec_scalar)) {
1✔
202
      return BOTAN_FFI_ERROR_NULL_POINTER;
203
   }
204
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
3✔
205
      return ffi_new_object(ec_scalar, std::make_unique<Botan::EC_Scalar>(Botan::EC_Scalar::random(g, safe_get(rng))));
206
   });
207
}
208

209
int botan_ec_scalar_from_mp(botan_ec_scalar_t* ec_scalar, botan_ec_group_t ec_group, botan_mp_t mp) {
4✔
210
   if(Botan::any_null_pointers(ec_scalar)) {
4✔
211
      return BOTAN_FFI_ERROR_NULL_POINTER;
212
   }
213
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
12✔
214
      return ffi_new_object(ec_scalar,
215
                            std::make_unique<Botan::EC_Scalar>(Botan::EC_Scalar::from_bigint(g, safe_get(mp))));
216
   });
217
}
218

219
// ec points
220

221
int botan_ec_point_destroy(botan_ec_point_t ec_point) {
22✔
222
   return BOTAN_FFI_CHECKED_DELETE(ec_point);
22✔
223
}
224

225
int botan_ec_point_identity(botan_ec_point_t* ec_point, botan_ec_group_t ec_group) {
2✔
226
   if(Botan::any_null_pointers(ec_point)) {
2✔
227
      return BOTAN_FFI_ERROR_NULL_POINTER;
228
   }
229
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
6✔
230
      return ffi_new_object(ec_point, std::make_unique<Botan::EC_AffinePoint>(Botan::EC_AffinePoint::identity(g)));
231
   });
232
}
233

234
int botan_ec_point_generator(botan_ec_point_t* ec_point, botan_ec_group_t ec_group) {
2✔
235
   if(Botan::any_null_pointers(ec_point)) {
2✔
236
      return BOTAN_FFI_ERROR_NULL_POINTER;
237
   }
238
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
6✔
239
      return ffi_new_object(ec_point, std::make_unique<Botan::EC_AffinePoint>(Botan::EC_AffinePoint::generator(g)));
240
   });
241
}
242

243
int botan_ec_point_from_xy(botan_ec_point_t* ec_point, botan_ec_group_t ec_group, botan_mp_t x, botan_mp_t y) {
1✔
244
   if(Botan::any_null_pointers(ec_point)) {
1✔
245
      return BOTAN_FFI_ERROR_NULL_POINTER;
246
   }
247
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
248
      std::optional<Botan::EC_AffinePoint> pt =
1✔
249
         Botan::EC_AffinePoint::from_bigint_xy(safe_get(ec_group), safe_get(x), safe_get(y));
1✔
250
      if(!pt.has_value()) {
1✔
251
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
252
      }
253

254
      return ffi_new_object(ec_point, std::make_unique<Botan::EC_AffinePoint>(pt.value()));
1✔
255
   });
2✔
256
}
257

258
int botan_ec_point_from_bytes(botan_ec_point_t* ec_point,
3✔
259
                              botan_ec_group_t ec_group,
260
                              const uint8_t* bytes,
261
                              size_t bytes_len) {
262
   if(Botan::any_null_pointers(ec_point, bytes)) {
3✔
263
      return BOTAN_FFI_ERROR_NULL_POINTER;
264
   }
265
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
9✔
266
      Botan::EC_AffinePoint pt(g, std::span{bytes, bytes_len});
267
      return ffi_new_object(ec_point, std::make_unique<Botan::EC_AffinePoint>(std::move(pt)));
268
   });
269
}
270

271
int botan_ec_point_view_x_bytes(botan_ec_point_t ec_point, botan_view_ctx ctx, botan_view_bin_fn view) {
1✔
272
   return BOTAN_FFI_VISIT(ec_point, [=](const auto& p) -> int {
4✔
273
      auto bytes = p.x_bytes();
274
      return invoke_view_callback(view, ctx, bytes);
275
   });
276
}
277

278
int botan_ec_point_view_y_bytes(botan_ec_point_t ec_point, botan_view_ctx ctx, botan_view_bin_fn view) {
1✔
279
   return BOTAN_FFI_VISIT(ec_point, [=](const auto& p) -> int {
4✔
280
      auto bytes = p.y_bytes();
281
      return invoke_view_callback(view, ctx, bytes);
282
   });
283
}
284

285
int botan_ec_point_view_xy_bytes(botan_ec_point_t ec_point, botan_view_ctx ctx, botan_view_bin_fn view) {
4✔
286
   return BOTAN_FFI_VISIT(ec_point, [=](const auto& p) -> int {
14✔
287
      auto bytes = p.xy_bytes();
288
      return invoke_view_callback(view, ctx, bytes);
289
   });
290
}
291

292
int botan_ec_point_view_uncompressed(botan_ec_point_t ec_point, botan_view_ctx ctx, botan_view_bin_fn view) {
2✔
293
   return BOTAN_FFI_VISIT(ec_point, [=](const auto& p) -> int {
8✔
294
      auto bytes = p.serialize_uncompressed();
295
      return invoke_view_callback(view, ctx, bytes);
296
   });
297
}
298

299
int botan_ec_point_view_compressed(botan_ec_point_t ec_point, botan_view_ctx ctx, botan_view_bin_fn view) {
2✔
300
   return BOTAN_FFI_VISIT(ec_point, [=](const auto& p) -> int {
8✔
301
      auto bytes = p.serialize_compressed();
302
      return invoke_view_callback(view, ctx, bytes);
303
   });
304
}
305

306
int botan_ec_point_is_identity(botan_ec_point_t ec_point) {
3✔
307
   return BOTAN_FFI_VISIT(ec_point, [=](const auto& p) -> int { return p.is_identity() ? 1 : 0; });
6✔
308
}
309

310
int botan_ec_point_equal(botan_ec_point_t x_w, botan_ec_point_t y_w) {
10✔
311
   return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w) ? 1 : 0; });
20✔
312
}
313

314
int botan_ec_point_mul(botan_ec_point_t* result,
6✔
315
                       botan_ec_point_t ec_point,
316
                       botan_ec_scalar_t ec_scalar,
317
                       botan_rng_t rng) {
318
   if(Botan::any_null_pointers(result)) {
6✔
319
      return BOTAN_FFI_ERROR_NULL_POINTER;
320
   }
321
   return BOTAN_FFI_VISIT(ec_point, [=](auto& pt) -> int {
18✔
322
      Botan::EC_AffinePoint res = pt.mul(safe_get(ec_scalar), safe_get(rng));
323
      return ffi_new_object(result, std::make_unique<Botan::EC_AffinePoint>(std::move(res)));
324
   });
325
}
326

327
int botan_ec_point_negate(botan_ec_point_t* result, botan_ec_point_t ec_point) {
2✔
328
   if(Botan::any_null_pointers(result)) {
2✔
329
      return BOTAN_FFI_ERROR_NULL_POINTER;
330
   }
331
   return BOTAN_FFI_VISIT(ec_point, [=](auto& pt) -> int {
6✔
332
      Botan::EC_AffinePoint res = pt.negate();
333
      return ffi_new_object(result, std::make_unique<Botan::EC_AffinePoint>(std::move(res)));
334
   });
335
}
336

337
int botan_ec_point_add(botan_ec_point_t* result, botan_ec_point_t x_w, botan_ec_point_t y_w) {
6✔
338
   if(Botan::any_null_pointers(result)) {
6✔
339
      return BOTAN_FFI_ERROR_NULL_POINTER;
340
   }
341
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) -> int {
18✔
342
      Botan::EC_AffinePoint res = x.add(safe_get(y_w));
343
      return ffi_new_object(result, std::make_unique<Botan::EC_AffinePoint>(std::move(res)));
344
   });
345
}
346
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc