summarylogtreecommitdiffstats
path: root/qca-botan2.patch
blob: 479e291514d88e806ad0004f3c56c4090d53b2bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
From 47163784d74232e3a844fc42897bffc7eff817b4 Mon Sep 17 00:00:00 2001
From: Antonio Rojas <arojas@archlinux.org>
Date: Wed, 11 Apr 2018 21:43:57 +0200
Subject: Add botan 2 support

Botan 1.10 will be EOL'd this year. This patch is a rough port to botan 2 API. Also:

- Removes support for botan < 1.10 to simplify code (1.10 was released 7 years ago)
- Ports the cmake module to use pkgconfig, since botan-config doesn't exist anymore in v2.
- To minimize ifdef'd code, botan2 unique_ptr's are release'd
- Removes -ansi C(XX)FLAGS, botan 2 requires C++11

Differential Revision: https://phabricator.kde.org/D11997
---
 CMakeLists.txt                  |  4 +-
 cmake/modules/FindBotan.cmake   | 22 +----------
 plugins/qca-botan/qca-botan.cpp | 87 ++++++++++++++++++++---------------------
 3 files changed, 46 insertions(+), 67 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7ef32ee..28b0169 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -131,8 +131,8 @@ if (CMAKE_COMPILER_IS_GNUCXX)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wcast-align")
      endif()
 
-     set ( CMAKE_C_FLAGS     "${CMAKE_C_FLAGS} -Wno-long-long -ansi -Wundef -Werror-implicit-function-declaration -Wchar-subscripts -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security -Wmissing-format-attribute -fno-common")
-     set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor -Wno-long-long -ansi -Wundef -Wchar-subscripts -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security -fno-check-new -fno-common")
+     set ( CMAKE_C_FLAGS     "${CMAKE_C_FLAGS} -Wno-long-long -Wundef -Werror-implicit-function-declaration -Wchar-subscripts -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security -Wmissing-format-attribute -fno-common")
+     set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor -Wno-long-long -Wundef -Wchar-subscripts -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security -fno-check-new -fno-common")
    endif (CMAKE_SYSTEM_NAME MATCHES Linux)
 endif (CMAKE_COMPILER_IS_GNUCXX)
 
diff --git a/cmake/modules/FindBotan.cmake b/cmake/modules/FindBotan.cmake
index 0c4e24b..ce92df5 100644
--- a/cmake/modules/FindBotan.cmake
+++ b/cmake/modules/FindBotan.cmake
@@ -11,30 +11,12 @@
 # Redistribution and use is allowed according to the terms of the BSD license.
 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
 
-# libgcrypt is moving to pkg-config, but earlier version don't have it
-
-#search in typical paths for libgcrypt-config
-FIND_PROGRAM(BOTANCONFIG_EXECUTABLE NAMES botan-config botan-config-1.10)
-mark_as_advanced(BOTANCONFIG_EXECUTABLE)
-
 #reset variables
 set(BOTAN_LIBRARIES)
 set(BOTAN_CFLAGS)
 
-# if botan-config has been found
-IF(BOTANCONFIG_EXECUTABLE)
-
-  EXEC_PROGRAM(${BOTANCONFIG_EXECUTABLE} ARGS --libs RETURN_VALUE _return_VALUE OUTPUT_VARIABLE BOTAN_LIBRARIES)
-
-  EXEC_PROGRAM(${BOTANCONFIG_EXECUTABLE} ARGS --cflags RETURN_VALUE _return_VALUE OUTPUT_VARIABLE BOTAN_CFLAGS)
-
-  IF(BOTAN_LIBRARIES)
-    SET(BOTAN_FOUND TRUE)
-  ENDIF(BOTAN_LIBRARIES)
-
-  MARK_AS_ADVANCED(BOTAN_CFLAGS BOTAN_LIBRARIES)
-
-ENDIF(BOTANCONFIG_EXECUTABLE)
+find_package(PkgConfig)
+pkg_search_module(BOTAN REQUIRED botan>=1.10 botan-1.10 botan-2)
 
 if (BOTAN_FOUND)
    if (NOT Botan_FIND_QUIETLY)
diff --git a/plugins/qca-botan/qca-botan.cpp b/plugins/qca-botan/qca-botan.cpp
index f387575..8822ab5 100644
--- a/plugins/qca-botan/qca-botan.cpp
+++ b/plugins/qca-botan/qca-botan.cpp
@@ -23,13 +23,18 @@
 
 #include <qstringlist.h>
 
-#include <botan/botan.h>
 #include <botan/hmac.h>
-#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,9,0)
-#include <botan/s2k.h>
-#endif
-#if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,8,0)
+#include <botan/version.h>
+#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(2,0,0)
+#include <botan/botan.h>
 #include <botan/algo_factory.h>
+#else
+#include <botan/auto_rng.h>
+#include <botan/block_cipher.h>
+#include <botan/filters.h>
+#include <botan/hash.h>
+#include <botan/pbkdf.h>
+#include <botan/stream_cipher.h>
 #endif
 
 #include <stdlib.h>
@@ -51,14 +56,8 @@ public:
     QCA::SecureArray nextBytes(int size)
     {
         QCA::SecureArray buf(size);
-#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,5,0)
-	Botan::Global_RNG::randomize( (Botan::byte*)buf.data(), buf.size(), Botan::SessionKey );
-#elif BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,7,6)
-	Botan::Global_RNG::randomize( (Botan::byte*)buf.data(), buf.size() );
-#else
 	Botan::AutoSeeded_RNG rng;
 	rng.randomize(reinterpret_cast<Botan::byte*>(buf.data()), buf.size());
-#endif
 	return buf;
     }
 };
@@ -70,7 +69,11 @@ class BotanHashContext : public QCA::HashContext
 public:
     BotanHashContext( const QString &hashName, QCA::Provider *p, const QString &type) : QCA::HashContext(p, type)
     {
+#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(2,0,0)
 	m_hashObj = Botan::get_hash(hashName.toStdString());
+#else
+	m_hashObj = Botan::HashFunction::create(hashName.toStdString()).release();
+#endif
     }
 
     ~BotanHashContext()
@@ -95,11 +98,7 @@ public:
 
     QCA::MemoryRegion final()
     {
-#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,9,0)
-        QCA::SecureArray a( m_hashObj->OUTPUT_LENGTH );
-#else
 	QCA::SecureArray a( m_hashObj->output_length() );
-#endif
 	m_hashObj->final( (Botan::byte *)a.data() );
 	return a;
     }
@@ -115,10 +114,10 @@ class BotanHMACContext : public QCA::MACContext
 public:
     BotanHMACContext( const QString &hashName, QCA::Provider *p, const QString &type) : QCA::MACContext(p, type)
     {
-#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,8,0)
-	m_hashObj = new Botan::HMAC(hashName.toStdString());
-#else
+#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(2,0,0)
 	m_hashObj = new Botan::HMAC(Botan::global_state().algorithm_factory().make_hash_function(hashName.toStdString()));
+#else
+	m_hashObj = new Botan::HMAC(Botan::HashFunction::create_or_throw(hashName.toStdString()).release());
 #endif
 	if (0 == m_hashObj) {
 	    std::cout << "null context object" << std::endl;
@@ -161,11 +160,7 @@ public:
 
     void final( QCA::MemoryRegion *out)
     {
-#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,9,0)
-	QCA::SecureArray sa( m_hashObj->OUTPUT_LENGTH, 0 );
-#else
 	QCA::SecureArray sa( m_hashObj->output_length(), 0 );
-#endif
 	m_hashObj->final( (Botan::byte *)sa.data() );
 	*out = sa;
     }
@@ -197,15 +192,8 @@ public:
     QCA::SymmetricKey makeKey(const QCA::SecureArray &secret, const QCA::InitializationVector &salt,
 			      unsigned int keyLength, unsigned int iterationCount)
     {
-#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,9,0)
-	m_s2k->set_iterations(iterationCount);
-	m_s2k->change_salt((const Botan::byte*)salt.data(), salt.size());
-	std::string secretString(secret.data(), secret.size() );
-	Botan::OctetString key = m_s2k->derive_key(keyLength, secretString);
-#else
 	std::string secretString(secret.data(), secret.size() );
 	Botan::OctetString key = m_s2k->derive_key(keyLength, secretString, (const Botan::byte*)salt.data(), salt.size(), iterationCount);
-#endif
         QCA::SecureArray retval(QByteArray((const char*)key.begin(), key.length()));
 	return QCA::SymmetricKey(retval);
     }
@@ -222,15 +210,6 @@ public:
 		std::string secretString(secret.data(), secret.size() );
 
 		*iterationCount = 0;
-#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,9,0)
-		m_s2k->set_iterations(1);
-		m_s2k->change_salt((const Botan::byte*)salt.data(), salt.size());
-		timer.start();
-		while (timer.elapsed() < msecInterval) {
-			key = m_s2k->derive_key(keyLength, secretString);
-			++(*iterationCount);
-		}
-#else
 		timer.start();
 		while (timer.elapsed() < msecInterval) {
 			key = m_s2k->derive_key(keyLength,
@@ -240,7 +219,6 @@ public:
 									1);
 			++(*iterationCount);
 		}
-#endif
 		return makeKey(secret, salt, keyLength, *iterationCount);
 	}
 
@@ -304,7 +282,14 @@ public:
 
     int blockSize() const
     {
+#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(2,0,0)
 	return Botan::block_size_of(m_algoName);
+#else
+	if(const std::unique_ptr<Botan::BlockCipher> bc = Botan::BlockCipher::create(m_algoName))
+	    return bc->block_size();
+        
+	throw Botan::Algorithm_Not_Found(m_algoName);
+#endif
     }
 
     QCA::AuthTag tag() const
@@ -337,23 +322,31 @@ public:
 
     QCA::KeyLength keyLength() const
     {
-#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,9,0)
-	return QCA::KeyLength( Botan::min_keylength_of(m_algoName),
-			       Botan::max_keylength_of(m_algoName),
-			       Botan::keylength_multiple_of(m_algoName) );
-#else
+#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(2,0,0)
         Botan::Algorithm_Factory &af = Botan::global_state().algorithm_factory();
+#endif
         Botan::Key_Length_Specification kls(0);
+#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(2,0,0)
         if(const Botan::BlockCipher *bc = af.prototype_block_cipher(m_algoName))
+#else
+        if(const std::unique_ptr<Botan::BlockCipher> bc = Botan::BlockCipher::create(m_algoName))
+#endif
             kls = bc->key_spec();
+#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(2,0,0)
         else if(const Botan::StreamCipher *sc = af.prototype_stream_cipher(m_algoName))
+#else
+        else if(const std::unique_ptr<Botan::StreamCipher> sc = Botan::StreamCipher::create(m_algoName))
+#endif
             kls = sc->key_spec();
+#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(2,0,0)
         else if(const Botan::MessageAuthenticationCode *mac = af.prototype_mac(m_algoName))
+#else
+        else if(const std::unique_ptr<Botan::MessageAuthenticationCode> mac = Botan::MessageAuthenticationCode::create(m_algoName))
+#endif
             kls = mac->key_spec();
         return QCA::KeyLength( kls.minimum_keylength(),
                                kls.maximum_keylength(),
                                kls.keylength_multiple() );
-#endif
     }
 
 
@@ -379,7 +372,9 @@ class botanProvider : public QCA::Provider
 public:
     void init()
     {
+#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(2,0,0)
 	m_init = new Botan::LibraryInitializer;
+#endif
     }
 
     ~botanProvider()
@@ -538,7 +533,9 @@ public:
 	    return 0;
     }
 private:
+#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(2,0,0)
     Botan::LibraryInitializer *m_init;
+#endif
 
 };
 
-- 
cgit v0.11.2