blob: 1a89e163cb1ed0222e42780631f781ba38d46c0f [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05002 Copyright (C) 2006-2013 Werner Dittmann
Alexandre Lision51140e12013-12-02 10:54:09 -05003
4 This program is free software: you can redistribute it and/or modify
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05005 it under the terms of the GNU Lesser General Public License as published by
Alexandre Lision51140e12013-12-02 10:54:09 -05006 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 }
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050062 else if (*(int32_t*)pkt == *(int32_t*)e255) {
63 dhLength = 32;
64 }
65 else if (*(int32_t*)pkt == *(int32_t*)e414) {
66 dhLength = 104;
67 }
Alexandre Lision51140e12013-12-02 10:54:09 -050068 else
69 return;
70
71 int length = sizeof(DHPartPacket_t) + dhLength + (2 * ZRTP_WORD_SIZE); // HMAC field is 2*ZRTP_WORD_SIZE
72 setLength(length / ZRTP_WORD_SIZE);
73}
74
75ZrtpPacketDHPart::ZrtpPacketDHPart(uint8_t *data) {
76 DEBUGOUT((fprintf(stdout, "Creating DHPart packet from data\n")));
77
78 zrtpHeader = (zrtpPacketHeader_t *)&((DHPartPacket_t *)data)->hdr; // the standard header
79 DHPartHeader = (DHPart_t *)&((DHPartPacket_t *)data)->dhPart;
80
81 int16_t len = getLength();
82 DEBUGOUT((fprintf(stdout, "DHPart length: %d\n", len)));
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050083 if (len == 85) { // Dh2k
Alexandre Lision51140e12013-12-02 10:54:09 -050084 dhLength = 256;
85 }
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050086 else if (len == 117) { // Dh3k
Alexandre Lision51140e12013-12-02 10:54:09 -050087 dhLength = 384;
88 }
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050089 else if (len == 37) { // EC256
Alexandre Lision51140e12013-12-02 10:54:09 -050090 dhLength = 64;
91 }
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050092 else if (len == 45) { // EC384
Alexandre Lision51140e12013-12-02 10:54:09 -050093 dhLength = 96;
94 }
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050095 else if (len == 29) { // E255
96 dhLength = 32;
97 }
98 else if (len == 47) { // E414
99 dhLength = 104;
100 }
Alexandre Lision51140e12013-12-02 10:54:09 -0500101 else {
102 pv = NULL;
103 return;
104 }
105 pv = data + sizeof(DHPartPacket_t); // point to the public key value
106}
107
108ZrtpPacketDHPart::~ZrtpPacketDHPart() {
109 DEBUGOUT((fprintf(stdout, "Deleting DHPart packet: alloc: %x\n", allocated)));
110}