blob: 17a82d27a4fb0609d92a584009668b07a02124ef [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
2 Copyright (C) 2006-2007 Werner Dittmann
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU 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
22#include <libzrtpcpp/ZrtpPacketHello.h>
23
24
25ZrtpPacketHello::ZrtpPacketHello() {
26 DEBUGOUT((fprintf(stdout, "Creating Hello packet without data\n")));
27}
28
29void ZrtpPacketHello::configureHello(ZrtpConfigure* config) {
30 // The NumSupported* data is in ZrtpTextData.h
31 nHash = config->getNumConfiguredAlgos(HashAlgorithm);
32 nCipher = config->getNumConfiguredAlgos(CipherAlgorithm);
33 nPubkey = config->getNumConfiguredAlgos(PubKeyAlgorithm);
34 nSas = config->getNumConfiguredAlgos(SasType);
35 nAuth = config->getNumConfiguredAlgos(AuthLength);
36
37 // length is fixed Header plus HMAC size (2*ZRTP_WORD_SIZE)
38 int32_t length = sizeof(HelloPacket_t) + (2 * ZRTP_WORD_SIZE);
39 length += nHash * ZRTP_WORD_SIZE;
40 length += nCipher * ZRTP_WORD_SIZE;
41 length += nPubkey * ZRTP_WORD_SIZE;
42 length += nSas * ZRTP_WORD_SIZE;
43 length += nAuth * ZRTP_WORD_SIZE;
44
45 // Don't change order of this sequence
46 oHash = sizeof(Hello_t);
47 oCipher = oHash + (nHash * ZRTP_WORD_SIZE);
48 oAuth = oCipher + (nCipher * ZRTP_WORD_SIZE);
49 oPubkey = oAuth + (nAuth * ZRTP_WORD_SIZE);
50 oSas = oPubkey + (nPubkey * ZRTP_WORD_SIZE);
51 oHmac = oSas + (nSas * ZRTP_WORD_SIZE); // offset to HMAC
52
53 void* allocated = &data;
54 memset(allocated, 0, sizeof(data));
55
56 zrtpHeader = (zrtpPacketHeader_t *)&((HelloPacket_t *)allocated)->hdr; // the standard header
57 helloHeader = (Hello_t *)&((HelloPacket_t *)allocated)->hello;
58
59 setZrtpId();
60
61 // minus 1 for CRC size
62 setLength(length / ZRTP_WORD_SIZE);
63 setMessageType((uint8_t*)HelloMsg);
64
65 setVersion((uint8_t*)zrtpVersion);
66
67 uint32_t lenField = nHash << 16;
68 for (int32_t i = 0; i < nHash; i++) {
69 AlgorithmEnum& hash = config->getAlgoAt(HashAlgorithm, i);
70 setHashType(i, (int8_t*)hash.getName());
71 }
72
73 lenField |= nCipher << 12;
74 for (int32_t i = 0; i < nCipher; i++) {
75 AlgorithmEnum& cipher = config->getAlgoAt(CipherAlgorithm, i);
76 setCipherType(i, (int8_t*)cipher.getName());
77 }
78
79 lenField |= nAuth << 8;
80 for (int32_t i = 0; i < nAuth; i++) {
81 AlgorithmEnum& length = config->getAlgoAt(AuthLength, i);
82 setAuthLen(i, (int8_t*)length.getName());
83 }
84
85 lenField |= nPubkey << 4;
86 for (int32_t i = 0; i < nPubkey; i++) {
87 AlgorithmEnum& pubKey = config->getAlgoAt(PubKeyAlgorithm, i);
88 setPubKeyType(i, (int8_t*)pubKey.getName());
89 }
90
91 lenField |= nSas;
92 for (int32_t i = 0; i < nSas; i++) {
93 AlgorithmEnum& sas = config->getAlgoAt(SasType, i);
94 setSasType(i, (int8_t*)sas.getName());
95 }
96 *((uint32_t*)&helloHeader->flags) = htonl(lenField);
97}
98
99ZrtpPacketHello::ZrtpPacketHello(uint8_t *data) {
100 DEBUGOUT((fprintf(stdout, "Creating Hello packet from data\n")));
101
102 zrtpHeader = (zrtpPacketHeader_t *)&((HelloPacket_t *)data)->hdr; // the standard header
103 helloHeader = (Hello_t *)&((HelloPacket_t *)data)->hello;
104
105 uint32_t t = *((uint32_t*)&helloHeader->flags);
106 uint32_t temp = ntohl(t);
107
108 nHash = (temp & (0xf << 16)) >> 16;
109 nHash &= 0x7; // restrict to max 7 algorithms
110 nCipher = (temp & (0xf << 12)) >> 12;
111 nCipher &= 0x7;
112 nAuth = (temp & (0xf << 8)) >> 8;
113 nAuth &= 0x7;
114 nPubkey = (temp & (0xf << 4)) >> 4;
115 nPubkey &= 0x7;
116 nSas = temp & 0xf;
117 nSas &= 0x7;
118
119 oHash = sizeof(Hello_t);
120 oCipher = oHash + (nHash * ZRTP_WORD_SIZE);
121 oAuth = oCipher + (nCipher * ZRTP_WORD_SIZE);
122 oPubkey = oAuth + (nAuth * ZRTP_WORD_SIZE);
123 oSas = oPubkey + (nPubkey * ZRTP_WORD_SIZE);
124 oHmac = oSas + (nSas * ZRTP_WORD_SIZE); // offset to HMAC
125}
126
127ZrtpPacketHello::~ZrtpPacketHello() {
128 DEBUGOUT((fprintf(stdout, "Deleting Hello packet: alloc: %x\n", allocated)));
129}