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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

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

95.15
/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
   return buf_size;
58
}
59

60
}  // namespace
61

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

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

71
      const size_t update_size = ffi_choose_update_size(*mode);
18✔
72

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

78
int botan_cipher_destroy(botan_cipher_t cipher) { return BOTAN_FFI_CHECKED_DELETE(cipher); }
18✔
79

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

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

88
int botan_cipher_output_length(botan_cipher_t cipher, size_t in_len, size_t* out_len) {
4✔
89
   if(out_len == nullptr)
4✔
90
      return BOTAN_FFI_ERROR_NULL_POINTER;
91

92
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *out_len = c.output_length(in_len); });
8✔
93
}
94

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

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

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

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

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

142
      using namespace Botan;
32✔
143
      Cipher_Mode& cipher = safe_get(cipher_obj);
32✔
144
      secure_vector<uint8_t>& mbuf = cipher_obj->buf();
32✔
145

146
      const bool final_input = (flags & BOTAN_CIPHER_UPDATE_FLAG_FINAL);
32✔
147

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

153
         try {
20✔
154
            cipher.finish(mbuf);
20✔
155
         } catch(Invalid_Authentication_Tag&) { return BOTAN_FFI_ERROR_BAD_MAC; }
×
156

157
         *output_written = mbuf.size();
20✔
158

159
         if(mbuf.size() <= output_size) {
20✔
160
            copy_mem(output, mbuf.data(), mbuf.size());
20✔
161
            mbuf.clear();
20✔
162
            return BOTAN_FFI_SUCCESS;
20✔
163
         }
164

165
         return -1;
166
      }
167

168
      if(input_size == 0) {
12✔
169
         // Currently must take entire buffer in this case
170
         *output_written = mbuf.size();
4✔
171
         if(output_size >= mbuf.size()) {
4✔
172
            copy_mem(output, mbuf.data(), mbuf.size());
4✔
173
            mbuf.clear();
4✔
174
            return BOTAN_FFI_SUCCESS;
4✔
175
         }
176

177
         return -1;
178
      }
179

180
      const size_t ud = cipher_obj->update_size();
8✔
181

182
      mbuf.resize(ud);
8✔
183
      size_t taken = 0, written = 0;
8✔
184

185
      while(input_size >= ud && output_size >= ud) {
138✔
186
         // FIXME we can use process here and avoid the copy
187
         copy_mem(mbuf.data(), input, ud);
122✔
188
         cipher.update(mbuf);
122✔
189

190
         input_size -= ud;
122✔
191
         copy_mem(output, mbuf.data(), ud);
122✔
192
         input += ud;
122✔
193
         taken += ud;
122✔
194

195
         output_size -= ud;
122✔
196
         output += ud;
122✔
197
         written += ud;
122✔
198
      }
199

200
      *output_written = written;
8✔
201
      *input_consumed = taken;
8✔
202

203
      return BOTAN_FFI_SUCCESS;
8✔
204
   });
32✔
205
}
206

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

217
int botan_cipher_valid_nonce_length(botan_cipher_t cipher, size_t nl) {
6✔
218
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { return c.valid_nonce_length(nl) ? 1 : 0; });
12✔
219
}
220

221
int botan_cipher_get_default_nonce_length(botan_cipher_t cipher, size_t* nl) {
6✔
222
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *nl = c.default_nonce_length(); });
12✔
223
}
224

225
int botan_cipher_get_update_granularity(botan_cipher_t cipher, size_t* ug) {
×
226
   return BOTAN_FFI_VISIT(cipher, [=](const auto& /*c*/) { *ug = cipher->update_size(); });
×
227
}
228

229
int botan_cipher_get_ideal_update_granularity(botan_cipher_t cipher, size_t* ug) {
×
230
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *ug = c.ideal_granularity(); });
×
231
}
232

233
int botan_cipher_get_tag_length(botan_cipher_t cipher, size_t* tl) {
6✔
234
   return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *tl = c.tag_size(); });
12✔
235
}
236

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