Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame^] | 2 | Copyright (C) 2006-2007 Werner Dittmann |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame^] | 5 | it under the terms of the GNU General Public License as published by |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 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 | |
| 25 | ZrtpPacketHello::ZrtpPacketHello() { |
| 26 | DEBUGOUT((fprintf(stdout, "Creating Hello packet without data\n"))); |
| 27 | } |
| 28 | |
| 29 | void 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 | |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame^] | 65 | setVersion((uint8_t*)zrtpVersion); |
| 66 | |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 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 | } |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame^] | 96 | *((uint32_t*)&helloHeader->flags) = htonl(lenField); |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | ZrtpPacketHello::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); |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame^] | 106 | uint32_t temp = ntohl(t); |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 107 | |
| 108 | nHash = (temp & (0xf << 16)) >> 16; |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 109 | nCipher = (temp & (0xf << 12)) >> 12; |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 110 | nAuth = (temp & (0xf << 8)) >> 8; |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 111 | nPubkey = (temp & (0xf << 4)) >> 4; |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 112 | nSas = temp & 0xf; |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 113 | |
| 114 | oHash = sizeof(Hello_t); |
| 115 | oCipher = oHash + (nHash * ZRTP_WORD_SIZE); |
| 116 | oAuth = oCipher + (nCipher * ZRTP_WORD_SIZE); |
| 117 | oPubkey = oAuth + (nAuth * ZRTP_WORD_SIZE); |
| 118 | oSas = oPubkey + (nPubkey * ZRTP_WORD_SIZE); |
| 119 | oHmac = oSas + (nSas * ZRTP_WORD_SIZE); // offset to HMAC |
| 120 | } |
| 121 | |
| 122 | ZrtpPacketHello::~ZrtpPacketHello() { |
| 123 | DEBUGOUT((fprintf(stdout, "Deleting Hello packet: alloc: %x\n", allocated))); |
| 124 | } |