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

randombit / botan / 5134090420

31 May 2023 03:12PM UTC coverage: 91.721% (-0.3%) from 91.995%
5134090420

push

github

randombit
Merge GH #3565 Disable noisy/pointless pylint warnings

76048 of 82912 relevant lines covered (91.72%)

11755290.1 hits per line

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

89.13
/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

51
   return ffi_guard_thunk(__func__, [=]() -> int {
6✔
52
      Botan::DL_Group group(group_id);
3✔
53
      *group_p_bytes = group.p_bytes();
3✔
54
      return BOTAN_FFI_SUCCESS;
3✔
55
   });
3✔
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,
1✔
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 {
5✔
72
      if(!verifier || !group_id || !hash_id || !rng_obj) {
73
         return BOTAN_FFI_ERROR_NULL_POINTER;
74
      }
75
      try {
76
         Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
77
         auto v_bn = Botan::BigInt::decode(verifier, verifier_len);
78
         auto b_pub_bn = s.step1(v_bn, group_id, hash_id, rng);
79
         return write_vec_output(b_pub, b_pub_len, Botan::BigInt::encode(b_pub_bn));
80
      } catch(Botan::Decoding_Error&) {
81
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
82
      } catch(Botan::Lookup_Error&) {
83
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
84
      }
85
   });
86
#else
87
   BOTAN_UNUSED(srp6, verifier, verifier_len, group_id, hash_id, rng_obj, b_pub, b_pub_len);
88
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
89
#endif
90
}
91

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

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

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