• 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

93.33
/src/lib/ffi/ffi_cipher.cpp
1
/*
2
* (C) 2015,2017 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include <botan/ffi.h>
8

9
#include <botan/aead.h>
10
#include <botan/internal/ffi_util.h>
11

12
extern "C" {
13

14
using namespace Botan_FFI;
15

16
struct botan_cipher_struct final : public botan_struct<Botan::Cipher_Mode, 0xB4A2BF9C> {
17
   public:
18
      explicit botan_cipher_struct(std::unique_ptr<Botan::Cipher_Mode> x, size_t update_size) :
18✔
19
            botan_struct(std::move(x)), m_update_size(update_size) {
18✔
20
         m_buf.reserve(m_update_size);
18✔
21
      }
18✔
22

23
      Botan::secure_vector<uint8_t>& buf() { return m_buf; }
32✔
24

25
      size_t update_size() const { return m_update_size; }
8✔
26

27
   private:
28
      Botan::secure_vector<uint8_t> m_buf;
29
      size_t m_update_size;
30
};
31

32
namespace {
33

34
size_t ffi_choose_update_size(Botan::Cipher_Mode& mode) {
18✔
35
   const size_t update_granularity = mode.update_granularity();
18✔
36
   const size_t minimum_final_size = mode.minimum_final_size();
18✔
37

38
   /*
39
   * Return the minimum possible granularity given the FFI API constraints that
40
   * we require the returned size be > minimum final size.
41
   *
42
   * If the minimum final size is zero, or the update_granularity is
43
   * already greater, just use that.
44
   *
45
   * Otherwise scale the update_granularity to a sufficient size
46
   * to be greater than the minimum.
47
   */
48
   if(minimum_final_size == 0 || update_granularity > minimum_final_size) {
18✔
49
      BOTAN_ASSERT_NOMSG(update_granularity > 0);
9✔
50
      return update_granularity;
51
   }
52

53
   size_t buf_size = std::max(update_granularity, minimum_final_size + 1);
9✔
54
   if(buf_size % update_granularity != 0) {
9✔
55
      buf_size += update_granularity - (buf_size % update_granularity);
6✔
56
   }
57

58
   return buf_size;
59
}
60

61
}  // namespace
62

63
int botan_cipher_init(botan_cipher_t* cipher, const char* cipher_name, uint32_t flags) {
18✔
64
   return ffi_guard_thunk(__func__, [=]() -> int {
18✔
65
      const bool encrypt_p = ((flags & BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION) == BOTAN_CIPHER_INIT_FLAG_ENCRYPT);
18✔
66
      const Botan::Cipher_Dir dir = encrypt_p ? Botan::Cipher_Dir::Encryption : Botan::Cipher_Dir::Decryption;
18✔
67

68
      std::unique_ptr<Botan::Cipher_Mode> mode(Botan::Cipher_Mode::create(cipher_name, dir));
18✔
69
      if(!mode) {
18✔
70
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
71
      }
72

73
      const size_t update_size = ffi_choose_update_size(*mode);
18✔
74

75
      *cipher = new botan_cipher_struct(std::move(mode), update_size);
18✔
76
      return BOTAN_FFI_SUCCESS;
18✔
77
   });
36✔
78
}
79

80
int botan_cipher_destroy(botan_cipher_t cipher) { return BOTAN_FFI_CHECKED_DELETE(cipher); }
18✔
81

82
int botan_cipher_clear(botan_cipher_t cipher) {
6✔
83
   return BOTAN_FFI_VISIT(cipher, [](auto& c) { c.clear(); });
12✔
84
}
85

86
int botan_cipher_reset(botan_cipher_t cipher) {
2✔
87
   return BOTAN_FFI_VISIT(cipher, [](auto& c) { c.reset(); });
4✔
88
}
89

90
int botan_cipher_output_length(botan_cipher_t cipher, size_t in_len, size_t* out_len) {
4✔
91
   if(out_len == nullptr) {
4✔
92
      return BOTAN_FFI_ERROR_NULL_POINTER;
93
   }
94

95
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *out_len = c.output_length(in_len); });
8✔
96
}
97

98
int botan_cipher_query_keylen(botan_cipher_t cipher, size_t* out_minimum_keylength, size_t* out_maximum_keylength) {
7✔
99
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) {
14✔
100
      *out_minimum_keylength = c.key_spec().minimum_keylength();
101
      *out_maximum_keylength = c.key_spec().maximum_keylength();
102
   });
103
}
104

105
int botan_cipher_get_keyspec(botan_cipher_t cipher,
1✔
106
                             size_t* out_minimum_keylength,
107
                             size_t* out_maximum_keylength,
108
                             size_t* out_keylength_modulo) {
109
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) {
2✔
110
      if(out_minimum_keylength)
111
         *out_minimum_keylength = c.key_spec().minimum_keylength();
112
      if(out_maximum_keylength)
113
         *out_maximum_keylength = c.key_spec().maximum_keylength();
114
      if(out_keylength_modulo)
115
         *out_keylength_modulo = c.key_spec().keylength_multiple();
116
   });
117
}
118

119
int botan_cipher_set_key(botan_cipher_t cipher, const uint8_t* key, size_t key_len) {
27✔
120
   return BOTAN_FFI_VISIT(cipher, [=](auto& c) { c.set_key(key, key_len); });
54✔
121
}
122

123
int botan_cipher_start(botan_cipher_t cipher_obj, const uint8_t* nonce, size_t nonce_len) {
29✔
124
   return ffi_guard_thunk(__func__, [=]() -> int {
29✔
125
      Botan::Cipher_Mode& cipher = safe_get(cipher_obj);
29✔
126
      cipher.start(nonce, nonce_len);
29✔
127
      return BOTAN_FFI_SUCCESS;
29✔
128
   });
29✔
129
}
130

131
int botan_cipher_update(botan_cipher_t cipher_obj,
32✔
132
                        uint32_t flags,
133
                        uint8_t output_ptr[],
134
                        size_t orig_output_size,
135
                        size_t* output_written,
136
                        const uint8_t input_ptr[],
137
                        size_t orig_input_size,
138
                        size_t* input_consumed) {
139
   return ffi_guard_thunk(__func__, [=]() -> int {
32✔
140
      size_t input_size = orig_input_size;
32✔
141
      size_t output_size = orig_output_size;
32✔
142
      const uint8_t* input = input_ptr;
32✔
143
      uint8_t* output = output_ptr;
32✔
144

145
      using namespace Botan;
32✔
146
      Cipher_Mode& cipher = safe_get(cipher_obj);
32✔
147
      secure_vector<uint8_t>& mbuf = cipher_obj->buf();
32✔
148

149
      const bool final_input = (flags & BOTAN_CIPHER_UPDATE_FLAG_FINAL);
32✔
150

151
      if(final_input) {
32✔
152
         mbuf.assign(input, input + input_size);
20✔
153
         *input_consumed = input_size;
28✔
154
         *output_written = 0;
52✔
155

156
         try {
20✔
157
            cipher.finish(mbuf);
20✔
158
         } catch(Invalid_Authentication_Tag&) {
×
159
            return BOTAN_FFI_ERROR_BAD_MAC;
×
160
         }
×
161

162
         *output_written = mbuf.size();
20✔
163

164
         if(mbuf.size() <= output_size) {
20✔
165
            copy_mem(output, mbuf.data(), mbuf.size());
20✔
166
            mbuf.clear();
20✔
167
            return BOTAN_FFI_SUCCESS;
20✔
168
         }
169

170
         return -1;
171
      }
172

173
      if(input_size == 0) {
12✔
174
         // Currently must take entire buffer in this case
175
         *output_written = mbuf.size();
4✔
176
         if(output_size >= mbuf.size()) {
4✔
177
            copy_mem(output, mbuf.data(), mbuf.size());
4✔
178
            mbuf.clear();
4✔
179
            return BOTAN_FFI_SUCCESS;
4✔
180
         }
181

182
         return -1;
183
      }
184

185
      const size_t ud = cipher_obj->update_size();
8✔
186

187
      mbuf.resize(ud);
8✔
188
      size_t taken = 0, written = 0;
8✔
189

190
      while(input_size >= ud && output_size >= ud) {
138✔
191
         // FIXME we can use process here and avoid the copy
192
         copy_mem(mbuf.data(), input, ud);
122✔
193
         cipher.update(mbuf);
122✔
194

195
         input_size -= ud;
122✔
196
         copy_mem(output, mbuf.data(), ud);
122✔
197
         input += ud;
122✔
198
         taken += ud;
122✔
199

200
         output_size -= ud;
122✔
201
         output += ud;
122✔
202
         written += ud;
122✔
203
      }
204

205
      *output_written = written;
8✔
206
      *input_consumed = taken;
8✔
207

208
      return BOTAN_FFI_SUCCESS;
8✔
209
   });
32✔
210
}
211

212
int botan_cipher_set_associated_data(botan_cipher_t cipher, const uint8_t* ad, size_t ad_len) {
4✔
213
   return BOTAN_FFI_VISIT(cipher, [=](auto& c) {
8✔
214
      if(Botan::AEAD_Mode* aead = dynamic_cast<Botan::AEAD_Mode*>(&c)) {
215
         aead->set_associated_data(ad, ad_len);
216
         return BOTAN_FFI_SUCCESS;
217
      }
218
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
219
   });
220
}
221

222
int botan_cipher_valid_nonce_length(botan_cipher_t cipher, size_t nl) {
6✔
223
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { return c.valid_nonce_length(nl) ? 1 : 0; });
12✔
224
}
225

226
int botan_cipher_get_default_nonce_length(botan_cipher_t cipher, size_t* nl) {
6✔
227
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *nl = c.default_nonce_length(); });
12✔
228
}
229

230
int botan_cipher_get_update_granularity(botan_cipher_t cipher, size_t* ug) {
×
231
   return BOTAN_FFI_VISIT(cipher, [=](const auto& /*c*/) { *ug = cipher->update_size(); });
×
232
}
233

234
int botan_cipher_get_ideal_update_granularity(botan_cipher_t cipher, size_t* ug) {
×
235
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *ug = c.ideal_granularity(); });
×
236
}
237

238
int botan_cipher_get_tag_length(botan_cipher_t cipher, size_t* tl) {
6✔
239
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *tl = c.tag_size(); });
12✔
240
}
241

242
int botan_cipher_name(botan_cipher_t cipher, char* name, size_t* name_len) {
6✔
243
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { return write_str_output(name, name_len, c.name()); });
14✔
244
}
245
}
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

© 2025 Coveralls, Inc