OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2007 Georgia Tech Research Corporation |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 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, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Author: Kashif Nadeem <kshfnadeem@gmail.com> |
| 19 * Matthieu Coudron <matthieu.coudron@lip6.fr> |
| 20 * |
| 21 */ |
| 22 |
| 23 #include <stdint.h> |
| 24 #include "ns3/mptcp-crypto.h" |
| 25 #include "ns3/log.h" |
| 26 #include "ns3/node.h" |
| 27 #include "ns3/buffer.h" |
| 28 #include "ns3/assert.h" |
| 29 #include <cstddef>· |
| 30 |
| 31 #ifdef HAVE_CRYPTO |
| 32 #include <gcrypt.h> |
| 33 #else |
| 34 #include <functional> |
| 35 #include <iostream> |
| 36 #include <string> |
| 37 #endif |
| 38 |
| 39 NS_LOG_COMPONENT_DEFINE ("MpTcpCrypto"); |
| 40 |
| 41 namespace ns3 { |
| 42 |
| 43 /* https://www.gnupg.org/documentation/manuals/gcrypt/Working-with-hash-algorith
ms.html#Working-with-hash-algorithms */ |
| 44 void |
| 45 GenerateTokenForKey( mptcp_crypto_alg_t ns_alg, uint64_t key, uint32_t& token, u
int64_t& idsn) |
| 46 { |
| 47 NS_LOG_LOGIC("Generating token/key from key=" << key); |
| 48 #ifdef HAVE_CRYPTO |
| 49 gcry_md_algos gcry_algo = GCRY_MD_SHA1; |
| 50 static const int KEY_SIZE_IN_BYTES = sizeof(key); |
| 51 |
| 52 /* converts the key into a buffer */ |
| 53 Buffer keyBuff; |
| 54 keyBuff.AddAtStart(KEY_SIZE_IN_BYTES); |
| 55 Buffer::Iterator it = keyBuff.Begin(); |
| 56 it.WriteHtonU64(key); |
| 57 int hash_length = gcry_md_get_algo_dlen( gcry_algo ); |
| 58 unsigned char digest[ 20 ]; |
| 59 Buffer digestBuf; /* to store the generated hash */ |
| 60 digestBuf.AddAtStart(hash_length); |
| 61 /* |
| 62 * gcry_md_hash_buffer (int algo, void *digest, const void *buffer, size_t len
gth); |
| 63 * gcry_md_hash_buffer is a shortcut function to calculate a message digest of
a buffer. |
| 64 * This function does not require a context and immediately returns the messag
e digest |
| 65 * of the length bytes at buffer. digest must be allocated by the caller, |
| 66 * large enough to hold the message digest yielded by the the specified algori
thm algo. |
| 67 * This required size may be obtained by using the function gcry_md_get_algo_d
len. |
| 68 */ |
| 69 |
| 70 gcry_md_hash_buffer( GCRY_MD_SHA1, digest, keyBuff.PeekData(), KEY_SIZE_IN_BYT
ES ); |
| 71 Buffer::Iterator it_digest = digestBuf.Begin(); |
| 72 it_digest.Write( digest , hash_length ); // strlen( (const char*)digest) |
| 73 it_digest = digestBuf.Begin(); |
| 74 token = it_digest.ReadNtohU32(); |
| 75 it_digest.Next( 8 ); |
| 76 idsn = it_digest.ReadNtohU64(); |
| 77 #else |
| 78 /* the cryptographic library is not available so we rely on a ns3 specific imp
lementation |
| 79 * that does not comply with the standard. |
| 80 * In the following, the idsn = the key (could be 0) and the token a truncated
key |
| 81 */ |
| 82 |
| 83 idsn = key; |
| 84 token = (uint32_t)key; |
| 85 #endif // HAVE_CRYPTO |
| 86 } |
| 87 |
| 88 } // end of 'ns3' |
OLD | NEW |