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

randombit / botan / 19090777525

05 Nov 2025 01:22AM UTC coverage: 90.687% (+0.006%) from 90.681%
19090777525

push

github

web-flow
Merge pull request #5141 from randombit/jack/ffi-srp6-fixed-len

Change FFI SRP6 to use fixed length outputs for integer values

100611 of 110943 relevant lines covered (90.69%)

12819696.81 hits per line

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

91.67
/src/lib/ffi/ffi_srp6.cpp
1
/*
2
* (C) 2022 Rostyslav Khudolii
3
*     2023 Jack Lloyd
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_rng.h>
11
#include <botan/internal/ffi_util.h>
12

13
#if defined(BOTAN_HAS_SRP6)
14
   #include <botan/bigint.h>
15
   #include <botan/dl_group.h>
16
   #include <botan/rng.h>
17
   #include <botan/srp6.h>
18
   #include <botan/symkey.h>
19
#endif
20

21
extern "C" {
22

23
using namespace Botan_FFI;
24

25
#if defined(BOTAN_HAS_SRP6)
26
BOTAN_FFI_DECLARE_STRUCT(botan_srp6_server_session_struct, Botan::SRP6_Server_Session, 0x44F7425F);
2✔
27
#else
28
BOTAN_FFI_DECLARE_DUMMY_STRUCT(botan_srp6_server_session_struct, 0x44F7425F);
29
#endif
30

31
int botan_srp6_server_session_init(botan_srp6_server_session_t* srp6) {
2✔
32
#if defined(BOTAN_HAS_SRP6)
33
   return ffi_guard_thunk(
2✔
34
      __func__, [=]() -> int { return ffi_new_object(srp6, std::make_unique<Botan::SRP6_Server_Session>()); });
2✔
35
#else
36
   BOTAN_UNUSED(srp6);
37
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
38
#endif
39
}
40

41
int botan_srp6_server_session_destroy(botan_srp6_server_session_t srp6) {
2✔
42
   return BOTAN_FFI_CHECKED_DELETE(srp6);
2✔
43
}
44

45
int botan_srp6_group_size(const char* group_id, size_t* group_p_bytes) {
4✔
46
#if defined(BOTAN_HAS_SRP6)
47
   if(any_null_pointers(group_id, group_p_bytes)) {
4✔
48
      return BOTAN_FFI_ERROR_NULL_POINTER;
49
   }
50

51
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
52
      const auto group = Botan::DL_Group::from_name(group_id);
4✔
53
      *group_p_bytes = group.p_bytes();
4✔
54
      return BOTAN_FFI_SUCCESS;
4✔
55
   });
8✔
56
#else
57
   BOTAN_UNUSED(group_id, group_p_bytes);
58
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
59
#endif
60
}
61

62
int botan_srp6_server_session_step1(botan_srp6_server_session_t srp6,
3✔
63
                                    const uint8_t* verifier,
64
                                    size_t verifier_len,
65
                                    const char* group_id,
66
                                    const char* hash_id,
67
                                    botan_rng_t rng_obj,
68
                                    uint8_t b_pub[],
69
                                    size_t* b_pub_len) {
70
#if defined(BOTAN_HAS_SRP6)
71
   return BOTAN_FFI_VISIT(srp6, [=](auto& s) -> int {
11✔
72
      if(any_null_pointers(verifier, group_id, hash_id, rng_obj)) {
73
         return BOTAN_FFI_ERROR_NULL_POINTER;
74
      }
75
      try {
76
         const auto group = Botan::DL_Group::from_name(group_id);
77
         const auto rc = check_and_prepare_output_space(b_pub, b_pub_len, group.p_bytes());
78
         if(rc != BOTAN_FFI_SUCCESS) {
79
            return rc;
80
         }
81

82
         Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
83
         auto v_bn = Botan::BigInt::from_bytes(std::span{verifier, verifier_len});
84
         auto b_pub_bn = s.step1(v_bn, group, hash_id, group.exponent_bits(), rng);
85
         return write_vec_output(b_pub, b_pub_len, b_pub_bn.serialize(group.p_bytes()));
86
      } catch(Botan::Decoding_Error&) {
87
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
88
      } catch(Botan::Lookup_Error&) {
89
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
90
      }
91
   });
92
#else
93
   BOTAN_UNUSED(srp6, verifier, verifier_len, group_id, hash_id, rng_obj, b_pub, b_pub_len);
94
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
95
#endif
96
}
97

98
int botan_srp6_server_session_step2(
3✔
99
   botan_srp6_server_session_t srp6, const uint8_t a[], size_t a_len, uint8_t key[], size_t* key_len) {
100
#if defined(BOTAN_HAS_SRP6)
101
   return BOTAN_FFI_VISIT(srp6, [=](auto& s) -> int {
15✔
102
      if(!a) {
103
         return BOTAN_FFI_ERROR_NULL_POINTER;
104
      }
105
      try {
106
         Botan::BigInt a_bn = Botan::BigInt::from_bytes({a, a_len});
107
         auto key_sk = s.step2(a_bn);
108
         return write_vec_output(key, key_len, key_sk.bits_of());
109
      } catch(Botan::Decoding_Error&) {
110
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
111
      }
112
   });
113
#else
114
   BOTAN_UNUSED(srp6, a, a_len, key, key_len);
115
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
116
#endif
117
}
118

119
int botan_srp6_generate_verifier(const char* username,
3✔
120
                                 const char* password,
121
                                 const uint8_t salt[],
122
                                 size_t salt_len,
123
                                 const char* group_id,
124
                                 const char* hash_id,
125
                                 uint8_t verifier[],
126
                                 size_t* verifier_len) {
127
#if defined(BOTAN_HAS_SRP6)
128
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
129
      if(any_null_pointers(username, password, salt, group_id, hash_id)) {
6✔
130
         return BOTAN_FFI_ERROR_NULL_POINTER;
131
      }
132
      try {
3✔
133
         std::vector<uint8_t> salt_vec(salt, salt + salt_len);
3✔
134
         const auto group = Botan::DL_Group::from_name(group_id);
3✔
135
         const size_t p_bytes = group.p_bytes();
3✔
136
         auto verifier_bn = Botan::srp6_generate_verifier(username, password, salt_vec, group, hash_id);
3✔
137
         return write_vec_output(verifier, verifier_len, verifier_bn.serialize(p_bytes));
3✔
138
      } catch(Botan::Lookup_Error&) {
9✔
139
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
×
140
      }
×
141
   });
3✔
142
#else
143
   BOTAN_UNUSED(username, password, group_id, hash_id);
144
   BOTAN_UNUSED(salt, salt_len, verifier, verifier_len);
145
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
146
#endif
147
}
148

149
int botan_srp6_client_agree(const char* identity,
3✔
150
                            const char* password,
151
                            const char* group_id,
152
                            const char* hash_id,
153
                            const uint8_t salt[],
154
                            size_t salt_len,
155
                            const uint8_t b[],
156
                            size_t b_len,
157
                            botan_rng_t rng_obj,
158
                            uint8_t A[],
159
                            size_t* A_len,
160
                            uint8_t K[],
161
                            size_t* K_len) {
162
#if defined(BOTAN_HAS_SRP6)
163
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
164
      if(any_null_pointers(identity, password, salt, group_id, hash_id, b, rng_obj)) {
6✔
165
         return BOTAN_FFI_ERROR_NULL_POINTER;
166
      }
167
      try {
3✔
168
         std::vector<uint8_t> saltv(salt, salt + salt_len);
3✔
169
         Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
3✔
170
         auto b_bn = Botan::BigInt::from_bytes({b, b_len});
3✔
171
         const auto group = Botan::DL_Group::from_name(group_id);
3✔
172
         const size_t a_bits = group.exponent_bits();
3✔
173
         auto [A_bn, K_sk] = Botan::srp6_client_agree(identity, password, group, hash_id, saltv, b_bn, a_bits, rng);
3✔
174
         auto ret_a = write_vec_output(A, A_len, A_bn.serialize(group.p_bytes()));
3✔
175
         auto ret_k = write_vec_output(K, K_len, K_sk.bits_of());
3✔
176
         if(ret_a != BOTAN_FFI_SUCCESS) {
3✔
177
            return ret_a;
1✔
178
         }
179
         if(ret_k != BOTAN_FFI_SUCCESS) {
180
            return ret_k;
181
         }
182
         return BOTAN_FFI_SUCCESS;
183
      } catch(Botan::Lookup_Error&) {
9✔
184
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
×
185
      }
×
186
   });
3✔
187
#else
188
   BOTAN_UNUSED(identity, password, group_id, hash_id, rng_obj);
189
   BOTAN_UNUSED(salt, salt_len, b, b_len, A, A_len, K, K_len);
190
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
191
#endif
192
}
193
}
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