2 * Copyright (C) 2018 René Korthaus
3 * Copyright (C) 2018 Konstantinos Kolelis
4 * Copyright (C) 2018 Tobias Hommel
5 * Rohde & Schwarz Cybersecurity GmbH
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "botan_crypter.h"
28 #include <botan/build.h>
30 #if defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_MODE_CBC)
32 #include <botan/ffi.h>
34 typedef struct private_botan_crypter_t private_botan_crypter_t
;
37 * Private data of botan_crypter_t
39 struct private_botan_crypter_t
{
42 * Public part of this class
44 botan_crypter_t
public;
54 const char* cipher_name
;
58 * Do the actual en/decryption
60 static bool crypt(private_botan_crypter_t
*this, chunk_t data
, chunk_t iv
,
61 chunk_t
*dst
, uint32_t init_flag
)
63 botan_cipher_t cipher
;
64 size_t output_written
= 0;
65 size_t input_consumed
= 0;
72 *dst
= chunk_alloc(data
.len
);
80 if (botan_cipher_init(&cipher
, this->cipher_name
, init_flag
))
85 if (!botan_cipher_set_key(cipher
, this->key
.ptr
, this->key
.len
) &&
86 !botan_cipher_start(cipher
, iv
.ptr
, iv
.len
) &&
87 !botan_cipher_update(cipher
, BOTAN_CIPHER_UPDATE_FLAG_FINAL
, out
,
88 data
.len
, &output_written
, in
, data
.len
,
90 (output_written
== input_consumed
))
95 botan_cipher_destroy(cipher
);
99 METHOD(crypter_t
, decrypt
, bool,
100 private_botan_crypter_t
*this, chunk_t data
, chunk_t iv
, chunk_t
*dst
)
102 return crypt(this, data
, iv
, dst
, BOTAN_CIPHER_INIT_FLAG_DECRYPT
);
106 METHOD(crypter_t
, encrypt
, bool,
107 private_botan_crypter_t
*this, chunk_t data
, chunk_t iv
, chunk_t
*dst
)
109 return crypt(this, data
, iv
, dst
, BOTAN_CIPHER_INIT_FLAG_ENCRYPT
);
112 METHOD(crypter_t
, get_block_size
, size_t,
113 private_botan_crypter_t
*this)
115 return AES_BLOCK_SIZE
;
118 METHOD(crypter_t
, get_iv_size
, size_t,
119 private_botan_crypter_t
*this)
121 return AES_BLOCK_SIZE
;
124 METHOD(crypter_t
, get_key_size
, size_t,
125 private_botan_crypter_t
*this)
127 return this->key
.len
;
130 METHOD(crypter_t
, set_key
, bool,
131 private_botan_crypter_t
*this, chunk_t key
)
133 memcpy(this->key
.ptr
, key
.ptr
, min(key
.len
, this->key
.len
));
137 METHOD(crypter_t
, destroy
, void,
138 private_botan_crypter_t
*this)
140 chunk_clear(&this->key
);
145 * Described in header
147 botan_crypter_t
*botan_crypter_create(encryption_algorithm_t algo
,
150 private_botan_crypter_t
*this;
157 .get_block_size
= _get_block_size
,
158 .get_iv_size
= _get_iv_size
,
159 .get_key_size
= _get_key_size
,
173 this->cipher_name
= "AES-128/CBC/NoPadding";
177 this->cipher_name
= "AES-192/CBC/NoPadding";
181 this->cipher_name
= "AES-256/CBC/NoPadding";
193 this->key
= chunk_alloc(key_size
);
194 return &this->public;