blob: 6a22983b5c4ee9229785bfda1b018f747df07a84 [file] [log] [blame]
Alexandre Lision907ed2e2014-02-04 10:33:09 -05001/*
2 Copyright (C) 2006-2013 Werner Dittmann
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18/*
19 * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
20 */
21
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050022#include <crypto/aesCFB.h>
23#include <crypto/twoCFB.h>
Alexandre Lision51140e12013-12-02 10:54:09 -050024#include <libzrtpcpp/ZrtpConfigure.h>
25#include <libzrtpcpp/ZrtpTextData.h>
26
27AlgorithmEnum::AlgorithmEnum(const AlgoTypes type, const char* name,
28 int32_t klen, const char* ra, encrypt_t en,
29 decrypt_t de, SrtpAlgorithms alId):
30 algoType(type) , algoName(name), keyLen(klen), readable(ra), encrypt(en),
31 decrypt(de), algoId(alId) {
32}
33
34AlgorithmEnum::~AlgorithmEnum()
35{
36}
37
38const char* AlgorithmEnum::getName() {
39 return algoName.c_str();
40}
41
42const char* AlgorithmEnum::getReadable() {
43 return readable.c_str();
44}
45
46int AlgorithmEnum::getKeylen() {
47 return keyLen;
48}
49
50SrtpAlgorithms AlgorithmEnum::getAlgoId() {
51 return algoId;
52}
53
54encrypt_t AlgorithmEnum::getEncrypt() {
55 return encrypt;
56}
57
58decrypt_t AlgorithmEnum::getDecrypt() {
59 return decrypt;
60}
61
62AlgoTypes AlgorithmEnum::getAlgoType() {
63 return algoType;
64}
65
66bool AlgorithmEnum::isValid() {
67 return (algoType != Invalid);
68}
69
70static AlgorithmEnum invalidAlgo(Invalid, "", 0, "", NULL, NULL, None);
71
72
73EnumBase::EnumBase(AlgoTypes a) : algoType(a) {
74}
75
76EnumBase::~EnumBase() {}
77
78void EnumBase::insert(const char* name) {
79 if (!name)
80 return;
81 AlgorithmEnum* e = new AlgorithmEnum(algoType, name, 0, "", NULL, NULL, None);
82 algos.push_back(e);
83}
84
85void EnumBase::insert(const char* name, int32_t klen, const char* ra,
86 encrypt_t enc, decrypt_t dec, SrtpAlgorithms alId) {
87 if (!name)
88 return;
89 AlgorithmEnum* e = new AlgorithmEnum(algoType, name, klen, ra, enc, dec, alId);
90 algos.push_back(e);
91}
92
93int EnumBase::getSize() {
94 return algos.size();
95}
96
97AlgoTypes EnumBase::getAlgoType() {
98 return algoType;
99}
100
101AlgorithmEnum& EnumBase::getByName(const char* name) {
102 std::vector<AlgorithmEnum* >::iterator b = algos.begin();
103 std::vector<AlgorithmEnum* >::iterator e = algos.end();
104
105 for (; b != e; b++) {
106 if (strncmp((*b)->getName(), name, 4) == 0) {
107 return *(*b);
108 }
109 }
110 return invalidAlgo;
111}
112
113AlgorithmEnum& EnumBase::getByOrdinal(int ord) {
114 std::vector<AlgorithmEnum* >::iterator b = algos.begin();
115 std::vector<AlgorithmEnum* >::iterator e = algos.end();
116
117 for (int i = 0; b != e; ++b) {
118 if (i == ord) {
119 return *(*b);
120 }
121 i++;
122 }
123 return invalidAlgo;
124}
125
126int EnumBase::getOrdinal(AlgorithmEnum& algo) {
127 std::vector<AlgorithmEnum* >::iterator b = algos.begin();
128 std::vector<AlgorithmEnum* >::iterator e = algos.end();
129
130 for (int i = 0; b != e; ++b) {
131 if (strncmp((*b)->getName(), algo.getName(), 4) == 0) {
132 return i;
133 }
134 i++;
135 }
136 return -1;
137}
138
139std::list<std::string>* EnumBase::getAllNames() {
140 std::vector<AlgorithmEnum* >::iterator b = algos.begin();
141 std::vector<AlgorithmEnum* >::iterator e = algos.end();
142
143 std::list<std::string>* strg = new std::list<std::string>();
144
145 for (; b != e; b++) {
146 std::string s((*b)->getName());
147 strg->push_back(s);
148 }
149 return strg;
150}
151
152
153/**
154 * Set up the enumeration list for available hash algorithms
155 */
156HashEnum::HashEnum() : EnumBase(HashAlgorithm) {
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500157 insert(s256, 0, "SHA-256", NULL, NULL, None);
158 insert(s384, 0, "SHA-384", NULL, NULL, None);
Alexandre Lision907ed2e2014-02-04 10:33:09 -0500159 insert(skn2, 0, "Skein-256", NULL, NULL, None);
160 insert(skn3, 0, "Skein-384", NULL, NULL, None);
Alexandre Lision51140e12013-12-02 10:54:09 -0500161}
162
163HashEnum::~HashEnum() {}
164
165/**
166 * Set up the enumeration list for available symmetric cipher algorithms
167 */
168SymCipherEnum::SymCipherEnum() : EnumBase(CipherAlgorithm) {
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500169 insert(aes3, 32, "AES-256", aesCfbEncrypt, aesCfbDecrypt, Aes);
170 insert(aes1, 16, "AES-128", aesCfbEncrypt, aesCfbDecrypt, Aes);
171 insert(two3, 32, "Twofish-256", twoCfbEncrypt, twoCfbDecrypt, TwoFish);
172 insert(two1, 16, "TwoFish-128", twoCfbEncrypt, twoCfbDecrypt, TwoFish);
Alexandre Lision51140e12013-12-02 10:54:09 -0500173}
174
175SymCipherEnum::~SymCipherEnum() {}
176
177/**
178 * Set up the enumeration list for available public key algorithms
179 */
180PubKeyEnum::PubKeyEnum() : EnumBase(PubKeyAlgorithm) {
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500181 insert(dh2k, 0, "DH-2048", NULL, NULL, None);
Alexandre Lision907ed2e2014-02-04 10:33:09 -0500182 insert(ec25, 0, "NIST ECDH-256", NULL, NULL, None);
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500183 insert(dh3k, 0, "DH-3072", NULL, NULL, None);
Alexandre Lision907ed2e2014-02-04 10:33:09 -0500184 insert(ec38, 0, "NIST ECDH-384", NULL, NULL, None);
185 insert(mult, 0, "Multi-stream", NULL, NULL, None);
186#ifdef SUPPORT_NON_NIST
187 insert(e255, 0, "ECDH-255", NULL, NULL, None);
188 insert(e414, 0, "ECDH-414", NULL, NULL, None);
189#endif
Alexandre Lision51140e12013-12-02 10:54:09 -0500190}
191
192PubKeyEnum::~PubKeyEnum() {}
193
194/**
195 * Set up the enumeration list for available SAS algorithms
196 */
197SasTypeEnum::SasTypeEnum() : EnumBase(SasType) {
198 insert(b32);
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500199 insert(b256);
Alexandre Lision51140e12013-12-02 10:54:09 -0500200}
201
202SasTypeEnum::~SasTypeEnum() {}
203
204/**
205 * Set up the enumeration list for available SRTP authentications
206 */
207AuthLengthEnum::AuthLengthEnum() : EnumBase(AuthLength) {
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500208 insert(hs32, 32, "HMAC-SHA1 32 bit", NULL, NULL, Sha1);
209 insert(hs80, 80, "HMAC-SHA1 80 bit", NULL, NULL, Sha1);
210 insert(sk32, 32, "Skein-MAC 32 bit", NULL, NULL, Skein);
211 insert(sk64, 64, "Skein-MAC 64 bit", NULL, NULL, Skein);
Alexandre Lision51140e12013-12-02 10:54:09 -0500212}
213
214AuthLengthEnum::~AuthLengthEnum() {}
215
216/*
217 * Here the global accessible enumerations for all implemented algorithms.
218 */
219HashEnum zrtpHashes;
220SymCipherEnum zrtpSymCiphers;
221PubKeyEnum zrtpPubKeys;
222SasTypeEnum zrtpSasTypes;
223AuthLengthEnum zrtpAuthLengths;
224
225/*
226 * The public methods are mainly a facade to the private methods.
227 */
Alexandre Lision907ed2e2014-02-04 10:33:09 -0500228ZrtpConfigure::ZrtpConfigure(): enableTrustedMitM(false), enableSasSignature(false), enableParanoidMode(false),
229selectionPolicy(Standard){}
Alexandre Lision51140e12013-12-02 10:54:09 -0500230
231ZrtpConfigure::~ZrtpConfigure() {}
232
233void ZrtpConfigure::setStandardConfig() {
234 clear();
235
236 addAlgo(HashAlgorithm, zrtpHashes.getByName(s384));
237 addAlgo(HashAlgorithm, zrtpHashes.getByName(s256));
238
239 addAlgo(CipherAlgorithm, zrtpSymCiphers.getByName(two3));
240 addAlgo(CipherAlgorithm, zrtpSymCiphers.getByName(aes3));
241 addAlgo(CipherAlgorithm, zrtpSymCiphers.getByName(two1));
242 addAlgo(CipherAlgorithm, zrtpSymCiphers.getByName(aes1));
243
244 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(ec25));
245 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(dh3k));
246 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(ec38));
247 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(dh2k));
248 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(mult));
249
250 addAlgo(SasType, zrtpSasTypes.getByName(b32));
251
252 addAlgo(AuthLength, zrtpAuthLengths.getByName(sk32));
253 addAlgo(AuthLength, zrtpAuthLengths.getByName(sk64));
254 addAlgo(AuthLength, zrtpAuthLengths.getByName(hs32));
255 addAlgo(AuthLength, zrtpAuthLengths.getByName(hs80));
256}
257
258void ZrtpConfigure::setMandatoryOnly() {
259 clear();
260
261 addAlgo(HashAlgorithm, zrtpHashes.getByName(s256));
262
263 addAlgo(CipherAlgorithm, zrtpSymCiphers.getByName(aes1));
264
265 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(dh3k));
266 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(mult));
267
268 addAlgo(SasType, zrtpSasTypes.getByName(b32));
269
270 addAlgo(AuthLength, zrtpAuthLengths.getByName(hs32));
271 addAlgo(AuthLength, zrtpAuthLengths.getByName(hs80));
272
273}
274
275void ZrtpConfigure::clear() {
276 hashes.clear();
277 symCiphers.clear();
278 publicKeyAlgos.clear();
279 sasTypes.clear();
280 authLengths.clear();
281}
282
283int32_t ZrtpConfigure::addAlgo(AlgoTypes algoType, AlgorithmEnum& algo) {
284
285 return addAlgo(getEnum(algoType), algo);
286}
287
288int32_t ZrtpConfigure::addAlgoAt(AlgoTypes algoType, AlgorithmEnum& algo, int32_t index) {
289
290 return addAlgoAt(getEnum(algoType), algo, index);
291}
292
293AlgorithmEnum& ZrtpConfigure::getAlgoAt(AlgoTypes algoType, int32_t index) {
294
295 return getAlgoAt(getEnum(algoType), index);
296}
297
298int32_t ZrtpConfigure::removeAlgo(AlgoTypes algoType, AlgorithmEnum& algo) {
299
300 return removeAlgo(getEnum(algoType), algo);
301}
302
303int32_t ZrtpConfigure::getNumConfiguredAlgos(AlgoTypes algoType) {
304
305 return getNumConfiguredAlgos(getEnum(algoType));
306}
307
308bool ZrtpConfigure::containsAlgo(AlgoTypes algoType, AlgorithmEnum& algo) {
309
310 return containsAlgo(getEnum(algoType), algo);
311}
312
313void ZrtpConfigure::printConfiguredAlgos(AlgoTypes algoType) {
314
315 printConfiguredAlgos(getEnum(algoType));
316}
317
318/*
319 * The next methods are the private methods that implement the real
320 * details.
321 */
322AlgorithmEnum& ZrtpConfigure::getAlgoAt(std::vector<AlgorithmEnum* >& a, int32_t index) {
323
324 if (index >= (int)a.size())
325 return invalidAlgo;
326
327 std::vector<AlgorithmEnum* >::iterator b = a.begin();
328 std::vector<AlgorithmEnum* >::iterator e = a.end();
329
330 for (int i = 0; b != e; ++b) {
331 if (i == index) {
332 return *(*b);
333 }
334 i++;
335 }
336 return invalidAlgo;
337}
338
339int32_t ZrtpConfigure::addAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo) {
340 int size = (int)a.size();
341 if (size >= maxNoOfAlgos)
342 return -1;
343
344 if (!algo.isValid())
345 return -1;
346
347 if (containsAlgo(a, algo))
348 return (maxNoOfAlgos - size);
349
350 a.push_back(&algo);
351 return (maxNoOfAlgos - (int)a.size());
352}
353
354int32_t ZrtpConfigure::addAlgoAt(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo, int32_t index) {
355 if (index >= maxNoOfAlgos)
356 return -1;
357
358 int size = (int)a.size();
359
360 if (!algo.isValid())
361 return -1;
362
363// a[index] = &algo;
364
365 if (index >= size) {
366 a.push_back(&algo);
367 return maxNoOfAlgos - (int)a.size();
368 }
369 std::vector<AlgorithmEnum* >::iterator b = a.begin();
370 std::vector<AlgorithmEnum* >::iterator e = a.end();
371
372 for (int i = 0; b != e; ++b) {
373 if (i == index) {
374 a.insert(b, &algo);
375 break;
376 }
377 i++;
378 }
379 return (maxNoOfAlgos - (int)a.size());
380}
381
382int32_t ZrtpConfigure::removeAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo) {
383
384 if ((int)a.size() == 0 || !algo.isValid())
385 return maxNoOfAlgos;
386
387 std::vector<AlgorithmEnum* >::iterator b = a.begin();
388 std::vector<AlgorithmEnum* >::iterator e = a.end();
389
390 for (; b != e; ++b) {
391 if (strcmp((*b)->getName(), algo.getName()) == 0) {
392 a.erase(b);
393 break;
394 }
395 }
396 return (maxNoOfAlgos - (int)a.size());
397}
398
399int32_t ZrtpConfigure::getNumConfiguredAlgos(std::vector<AlgorithmEnum* >& a) {
400 return (int32_t)a.size();
401}
402
403bool ZrtpConfigure::containsAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo) {
404
405 if ((int)a.size() == 0 || !algo.isValid())
406 return false;
407
408 std::vector<AlgorithmEnum* >::iterator b = a.begin();
409 std::vector<AlgorithmEnum* >::iterator e = a.end();
410
411 for (; b != e; ++b) {
412 if (strcmp((*b)->getName(), algo.getName()) == 0) {
413 return true;
414 }
415 }
416 return false;
417}
418
419void ZrtpConfigure::printConfiguredAlgos(std::vector<AlgorithmEnum* >& a) {
420
421 std::vector<AlgorithmEnum* >::iterator b = a.begin();
422 std::vector<AlgorithmEnum* >::iterator e = a.end();
423
424 for (; b != e; ++b) {
425 printf("print configured: name: %s\n", (*b)->getName());
426 }
427}
428
429std::vector<AlgorithmEnum* >& ZrtpConfigure::getEnum(AlgoTypes algoType) {
430
431 switch(algoType) {
432 case HashAlgorithm:
433 return hashes;
434 break;
435
436 case CipherAlgorithm:
437 return symCiphers;
438 break;
439
440 case PubKeyAlgorithm:
441 return publicKeyAlgos;
442 break;
443
444 case SasType:
445 return sasTypes;
446 break;
447
448 case AuthLength:
449 return authLengths;
450 break;
451
452 default:
453 break;
454 }
455 return hashes;
456}
457
458void ZrtpConfigure::setTrustedMitM(bool yesNo) {
459 enableTrustedMitM = yesNo;
460}
461
462bool ZrtpConfigure::isTrustedMitM() {
463 return enableTrustedMitM;
464}
465
466void ZrtpConfigure::setSasSignature(bool yesNo) {
467 enableSasSignature = yesNo;
468}
469
470bool ZrtpConfigure::isSasSignature() {
471 return enableSasSignature;
472}
473
474void ZrtpConfigure::setParanoidMode(bool yesNo) {
475 enableParanoidMode = yesNo;
476}
477
478bool ZrtpConfigure::isParanoidMode() {
479 return enableParanoidMode;
480}
481
482#if 0
483ZrtpConfigure config;
484
485main() {
486 printf("Start\n");
487 printf("size: %d\n", zrtpHashes.getSize());
488 AlgorithmEnum e = zrtpHashes.getByName("S256");
489 printf("algo name: %s\n", e.getName());
490 printf("algo type: %d\n", e.getAlgoType());
491
492 std::list<std::string>* names = zrtpHashes.getAllNames();
493 printf("size of name list: %d\n", names->size());
494 printf("first name: %s\n", names->front().c_str());
495 printf("last name: %s\n", names->back().c_str());
496
497 printf("free slots: %d (expected 6)\n", config.addAlgo(HashAlgorithm, e));
498
499 AlgorithmEnum e1(HashAlgorithm, "SHA384");
500 printf("free slots: %d (expected 5)\n", config.addAlgoAt(HashAlgorithm, e1, 0));
501 AlgorithmEnum e2 = config.getAlgoAt(HashAlgorithm, 0);
502 printf("algo name: %s (expected SHA384)\n", e2.getName());
503 printf("Num of configured algos: %d (expected 2)\n", config.getNumConfiguredAlgos(HashAlgorithm));
504 config.printConfiguredAlgos(HashAlgorithm);
505 printf("free slots: %d (expected 6)\n", config.removeAlgo(HashAlgorithm, e2));
506 e2 = config.getAlgoAt(HashAlgorithm, 0);
507 printf("algo name: %s (expected SHA256)\n", e2.getName());
508
509 printf("clearing config\n");
510 config.clear();
511 printf("size: %d\n", zrtpHashes.getSize());
512 e = zrtpHashes.getByName("S256");
513 printf("algo name: %s\n", e.getName());
514 printf("algo type: %d\n", e.getAlgoType());
515
516}
517
518#endif
519/** EMACS **
520 * Local variables:
521 * mode: c++
522 * c-default-style: ellemtel
523 * c-basic-offset: 4
524 * End:
525 */