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

randombit / botan / 23491727390

24 Mar 2026 01:24PM UTC coverage: 89.486% (+0.005%) from 89.481%
23491727390

Pull #5404

github

web-flow
Merge fbf5c9bfe into db4897d1b
Pull Request #5404: Add EC scalars and points to FFI

105522 of 117920 relevant lines covered (89.49%)

11840536.39 hits per line

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

98.46
/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) {
16✔
22
   return BOTAN_FFI_CHECKED_DELETE(ec_group);
16✔
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) {
11✔
191
   return BOTAN_FFI_VISIT(curve1_w, [=](const auto& curve1) -> int { return curve1 == safe_get(curve2_w); });
22✔
192
}
193

194
// ec scalars
195

196
int botan_ec_scalar_destroy(botan_ec_scalar_t ec_scalar) {
8✔
197
   return BOTAN_FFI_CHECKED_DELETE(ec_scalar);
8✔
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_one(botan_ec_scalar_t* ec_scalar, botan_ec_group_t ec_group) {
2✔
210
   if(Botan::any_null_pointers(ec_scalar)) {
2✔
211
      return BOTAN_FFI_ERROR_NULL_POINTER;
212
   }
213
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
6✔
214
      return ffi_new_object(ec_scalar, std::make_unique<Botan::EC_Scalar>(Botan::EC_Scalar::one(g)));
215
   });
216
}
217

218
int botan_ec_scalar_from_mp(botan_ec_scalar_t* ec_scalar, botan_ec_group_t ec_group, botan_mp_t mp) {
3✔
219
   if(Botan::any_null_pointers(ec_scalar)) {
3✔
220
      return BOTAN_FFI_ERROR_NULL_POINTER;
221
   }
222
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
9✔
223
      return ffi_new_object(ec_scalar,
224
                            std::make_unique<Botan::EC_Scalar>(Botan::EC_Scalar::from_bigint(g, safe_get(mp))));
225
   });
226
}
227

228
// ec points
229

230
int botan_ec_point_destroy(botan_ec_point_t ec_point) {
22✔
231
   return BOTAN_FFI_CHECKED_DELETE(ec_point);
22✔
232
}
233

234
int botan_ec_point_identity(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::identity(g)));
240
   });
241
}
242

243
int botan_ec_point_generator(botan_ec_point_t* ec_point, botan_ec_group_t ec_group) {
2✔
244
   if(Botan::any_null_pointers(ec_point)) {
2✔
245
      return BOTAN_FFI_ERROR_NULL_POINTER;
246
   }
247
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
6✔
248
      return ffi_new_object(ec_point, std::make_unique<Botan::EC_AffinePoint>(Botan::EC_AffinePoint::generator(g)));
249
   });
250
}
251

252
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✔
253
   if(Botan::any_null_pointers(ec_point)) {
1✔
254
      return BOTAN_FFI_ERROR_NULL_POINTER;
255
   }
256
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
257
      std::optional<Botan::EC_AffinePoint> pt =
1✔
258
         Botan::EC_AffinePoint::from_bigint_xy(safe_get(ec_group), safe_get(x), safe_get(y));
1✔
259
      if(!pt.has_value()) {
1✔
260
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
261
      }
262

263
      return ffi_new_object(ec_point, std::make_unique<Botan::EC_AffinePoint>(pt.value()));
1✔
264
   });
2✔
265
}
266

267
int botan_ec_point_from_bytes(botan_ec_point_t* ec_point,
3✔
268
                              botan_ec_group_t ec_group,
269
                              const uint8_t* bytes,
270
                              size_t bytes_len) {
271
   if(Botan::any_null_pointers(ec_point, bytes)) {
3✔
272
      return BOTAN_FFI_ERROR_NULL_POINTER;
273
   }
274
   return BOTAN_FFI_VISIT(ec_group, [=](const auto& g) -> int {
9✔
275
      Botan::EC_AffinePoint pt(g, std::span{bytes, bytes_len});
276
      return ffi_new_object(ec_point, std::make_unique<Botan::EC_AffinePoint>(std::move(pt)));
277
   });
278
}
279

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

287
int botan_ec_point_view_y_bytes(botan_ec_point_t ec_point, botan_view_ctx ctx, botan_view_bin_fn view) {
1✔
288
   return BOTAN_FFI_VISIT(ec_point, [=](const auto& p) -> int {
4✔
289
      auto bytes = p.y_bytes();
290
      return invoke_view_callback(view, ctx, bytes);
291
   });
292
}
293

294
int botan_ec_point_view_xy_bytes(botan_ec_point_t ec_point, botan_view_ctx ctx, botan_view_bin_fn view) {
4✔
295
   return BOTAN_FFI_VISIT(ec_point, [=](const auto& p) -> int {
14✔
296
      auto bytes = p.xy_bytes();
297
      return invoke_view_callback(view, ctx, bytes);
298
   });
299
}
300

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

308
int botan_ec_point_view_compressed(botan_ec_point_t ec_point, botan_view_ctx ctx, botan_view_bin_fn view) {
2✔
309
   return BOTAN_FFI_VISIT(ec_point, [=](const auto& p) -> int {
8✔
310
      auto bytes = p.serialize_compressed();
311
      return invoke_view_callback(view, ctx, bytes);
312
   });
313
}
314

315
int botan_ec_point_is_identity(botan_ec_point_t ec_point) {
3✔
316
   return BOTAN_FFI_VISIT(ec_point, [=](const auto& p) -> int { return p.is_identity() ? 1 : 0; });
6✔
317
}
318

319
int botan_ec_point_equal(botan_ec_point_t x_w, botan_ec_point_t y_w) {
10✔
320
   return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w) ? 1 : 0; });
20✔
321
}
322

323
int botan_ec_point_mul(botan_ec_point_t* result,
6✔
324
                       botan_ec_point_t ec_point,
325
                       botan_ec_scalar_t ec_scalar,
326
                       botan_rng_t rng) {
327
   if(Botan::any_null_pointers(result)) {
6✔
328
      return BOTAN_FFI_ERROR_NULL_POINTER;
329
   }
330
   return BOTAN_FFI_VISIT(ec_point, [=](auto& pt) -> int {
18✔
331
      Botan::EC_AffinePoint res = pt.mul(safe_get(ec_scalar), safe_get(rng));
332
      return ffi_new_object(result, std::make_unique<Botan::EC_AffinePoint>(std::move(res)));
333
   });
334
}
335

336
int botan_ec_point_negate(botan_ec_point_t* result, botan_ec_point_t ec_point) {
2✔
337
   if(Botan::any_null_pointers(result)) {
2✔
338
      return BOTAN_FFI_ERROR_NULL_POINTER;
339
   }
340
   return BOTAN_FFI_VISIT(ec_point, [=](auto& pt) -> int {
6✔
341
      Botan::EC_AffinePoint res = pt.negate();
342
      return ffi_new_object(result, std::make_unique<Botan::EC_AffinePoint>(std::move(res)));
343
   });
344
}
345

346
int botan_ec_point_add(botan_ec_point_t* result, botan_ec_point_t x_w, botan_ec_point_t y_w) {
6✔
347
   if(Botan::any_null_pointers(result)) {
6✔
348
      return BOTAN_FFI_ERROR_NULL_POINTER;
349
   }
350
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) -> int {
18✔
351
      Botan::EC_AffinePoint res = x.add(safe_get(y_w));
352
      return ffi_new_object(result, std::make_unique<Botan::EC_AffinePoint>(std::move(res)));
353
   });
354
}
355
}
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