blob: 8c59233f83b311b36945ec664b8f33f618ac4385 [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/ZrtpPacketDHPart.h>
23
24ZrtpPacketDHPart::ZrtpPacketDHPart() {
25 DEBUGOUT((fprintf(stdout, "Creating DHPart packet without data and pkt type\n")));
26 initialize();
27}
28
29ZrtpPacketDHPart::ZrtpPacketDHPart(const char* pkt) {
30 DEBUGOUT((fprintf(stdout, "Creating DHPart packet without data\n")));
31 initialize();
32 setPubKeyType(pkt);
33}
34
35void ZrtpPacketDHPart::initialize() {
36
37 void* allocated = &data;
38 memset(allocated, 0, sizeof(data));
39
40 zrtpHeader = (zrtpPacketHeader_t *)&((DHPartPacket_t *)allocated)->hdr; // the standard header
41 DHPartHeader = (DHPart_t *)&((DHPartPacket_t *)allocated)->dhPart;
42 pv = ((uint8_t*)allocated) + sizeof(DHPartPacket_t); // point to the public key value
43
44 setZrtpId();
45}
46
47// The fixed numbers below are taken from ZRTP specification, chap 5.1.5
48void ZrtpPacketDHPart::setPubKeyType(const char* pkt) {
49 // Well - the algo type is only 4 char thus cast to int32 and compare
50 if (*(int32_t*)pkt == *(int32_t*)dh2k) {
51 dhLength = 256;
52 }
53 else if (*(int32_t*)pkt == *(int32_t*)dh3k) {
54 dhLength = 384;
55 }
56 else if (*(int32_t*)pkt == *(int32_t*)ec25) {
57 dhLength = 64;
58 }
59 else if (*(int32_t*)pkt == *(int32_t*)ec38) {
60 dhLength = 96;
61 }
62 else
63 return;
64
65 int length = sizeof(DHPartPacket_t) + dhLength + (2 * ZRTP_WORD_SIZE); // HMAC field is 2*ZRTP_WORD_SIZE
66 setLength(length / ZRTP_WORD_SIZE);
67}
68
69ZrtpPacketDHPart::ZrtpPacketDHPart(uint8_t *data) {
70 DEBUGOUT((fprintf(stdout, "Creating DHPart packet from data\n")));
71
72 zrtpHeader = (zrtpPacketHeader_t *)&((DHPartPacket_t *)data)->hdr; // the standard header
73 DHPartHeader = (DHPart_t *)&((DHPartPacket_t *)data)->dhPart;
74
75 int16_t len = getLength();
76 DEBUGOUT((fprintf(stdout, "DHPart length: %d\n", len)));
77 if (len == 85) {
78 dhLength = 256;
79 }
80 else if (len == 117) {
81 dhLength = 384;
82 }
83 else if (len == 37) {
84 dhLength = 64;
85 }
86 else if (len == 45) {
87 dhLength = 96;
88 }
89 else {
90 pv = NULL;
91 return;
92 }
93 pv = data + sizeof(DHPartPacket_t); // point to the public key value
94}
95
96ZrtpPacketDHPart::~ZrtpPacketDHPart() {
97 DEBUGOUT((fprintf(stdout, "Deleting DHPart packet: alloc: %x\n", allocated)));
98}