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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

97.62
/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) {
1✔
32
#if defined(BOTAN_HAS_SRP6)
33
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
34
      *srp6 = new botan_srp6_server_session_struct(std::make_unique<Botan::SRP6_Server_Session>());
1✔
35
      return BOTAN_FFI_SUCCESS;
1✔
36
   });
1✔
37
#else
38
   BOTAN_UNUSED(srp6);
39
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
40
#endif
41
}
42

43
int botan_srp6_server_session_destroy(botan_srp6_server_session_t srp6) { return BOTAN_FFI_CHECKED_DELETE(srp6); }
1✔
44

45
int botan_srp6_group_size(const char* group_id, size_t* group_p_bytes) {
3✔
46
#if defined(BOTAN_HAS_SRP6)
47
   if(group_id == nullptr || group_p_bytes == nullptr)
3✔
48
      return BOTAN_FFI_ERROR_NULL_POINTER;
49

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

61
int botan_srp6_server_session_step1(botan_srp6_server_session_t srp6,
1✔
62
                                    const uint8_t* verifier,
63
                                    size_t verifier_len,
64
                                    const char* group_id,
65
                                    const char* hash_id,
66
                                    botan_rng_t rng_obj,
67
                                    uint8_t b_pub[],
68
                                    size_t* b_pub_len) {
69
#if defined(BOTAN_HAS_SRP6)
70
   return BOTAN_FFI_VISIT(srp6, [=](auto& s) -> int {
5✔
71
      if(!verifier || !group_id || !hash_id || !rng_obj) {
72
         return BOTAN_FFI_ERROR_NULL_POINTER;
73
      }
74
      try {
75
         Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
76
         auto v_bn = Botan::BigInt::decode(verifier, verifier_len);
77
         auto b_pub_bn = s.step1(v_bn, group_id, hash_id, rng);
78
         return write_vec_output(b_pub, b_pub_len, Botan::BigInt::encode(b_pub_bn));
79
      } catch(Botan::Decoding_Error&) { return BOTAN_FFI_ERROR_BAD_PARAMETER; } catch(Botan::Lookup_Error&) {
80
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
81
      }
82
   });
83
#else
84
   BOTAN_UNUSED(srp6, verifier, verifier_len, group_id, hash_id, rng_obj, b_pub, b_pub_len);
85
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
86
#endif
87
}
88

89
int botan_srp6_server_session_step2(
1✔
90
   botan_srp6_server_session_t srp6, const uint8_t a[], size_t a_len, uint8_t key[], size_t* key_len) {
91
#if defined(BOTAN_HAS_SRP6)
92
   return BOTAN_FFI_VISIT(srp6, [=](auto& s) -> int {
6✔
93
      if(!a) {
94
         return BOTAN_FFI_ERROR_NULL_POINTER;
95
      }
96
      try {
97
         Botan::BigInt a_bn = Botan::BigInt::decode(a, a_len);
98
         auto key_sk = s.step2(a_bn);
99
         return write_vec_output(key, key_len, key_sk.bits_of());
100
      } catch(Botan::Decoding_Error&) { return BOTAN_FFI_ERROR_BAD_PARAMETER; }
101
   });
102
#else
103
   BOTAN_UNUSED(srp6, a, a_len, key, key_len);
104
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
105
#endif
106
}
107

108
int botan_srp6_generate_verifier(const char* username,
1✔
109
                                 const char* password,
110
                                 const uint8_t salt[],
111
                                 size_t salt_len,
112
                                 const char* group_id,
113
                                 const char* hash_id,
114
                                 uint8_t verifier[],
115
                                 size_t* verifier_len) {
116
#if defined(BOTAN_HAS_SRP6)
117
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
118
      if(!username || !password || !salt || !group_id || !hash_id) {
1✔
119
         return BOTAN_FFI_ERROR_NULL_POINTER;
120
      }
121
      try {
1✔
122
         std::vector<uint8_t> salt_vec(salt, salt + salt_len);
1✔
123
         auto verifier_bn = Botan::srp6_generate_verifier(username, password, salt_vec, group_id, hash_id);
1✔
124
         return write_vec_output(verifier, verifier_len, Botan::BigInt::encode(verifier_bn));
2✔
125
      } catch(Botan::Lookup_Error&) { return BOTAN_FFI_ERROR_BAD_PARAMETER; }
2✔
126
   });
1✔
127
#else
128
   BOTAN_UNUSED(username, password, group_id, hash_id);
129
   BOTAN_UNUSED(salt, salt_len, verifier, verifier_len);
130
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
131
#endif
132
}
133

134
int botan_srp6_client_agree(const char* identity,
1✔
135
                            const char* password,
136
                            const char* group_id,
137
                            const char* hash_id,
138
                            const uint8_t salt[],
139
                            size_t salt_len,
140
                            const uint8_t b[],
141
                            size_t b_len,
142
                            botan_rng_t rng_obj,
143
                            uint8_t A[],
144
                            size_t* A_len,
145
                            uint8_t K[],
146
                            size_t* K_len) {
147
#if defined(BOTAN_HAS_SRP6)
148
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
149
      if(!identity || !password || !salt || !group_id || !hash_id || !b || !rng_obj) {
1✔
150
         return BOTAN_FFI_ERROR_NULL_POINTER;
151
      }
152
      try {
1✔
153
         std::vector<uint8_t> saltv(salt, salt + salt_len);
1✔
154
         Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1✔
155
         auto b_bn = Botan::BigInt::decode(b, b_len);
1✔
156
         auto [A_bn, K_sk] = Botan::srp6_client_agree(identity, password, group_id, hash_id, saltv, b_bn, rng);
1✔
157
         auto ret_a = write_vec_output(A, A_len, Botan::BigInt::encode(A_bn));
1✔
158
         auto ret_k = write_vec_output(K, K_len, K_sk.bits_of());
1✔
159
         if(ret_a != BOTAN_FFI_SUCCESS) {
1✔
160
            return ret_a;
161
         }
162
         if(ret_k != BOTAN_FFI_SUCCESS) {
1✔
163
            return ret_k;
×
164
         }
165
         return BOTAN_FFI_SUCCESS;
166
      } catch(Botan::Lookup_Error&) { return BOTAN_FFI_ERROR_BAD_PARAMETER; }
3✔
167
   });
1✔
168
#else
169
   BOTAN_UNUSED(identity, password, group_id, hash_id, rng_obj);
170
   BOTAN_UNUSED(salt, salt_len, b, b_len, A, A_len, K, K_len);
171
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
172
#endif
173
}
174
}
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