added showtable
[anytun.git] / authAlgo.cpp
blob3b1967e12292ff3608298bcd03e69953b9a50d76
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "authAlgo.h"
32 #include "log.h"
33 #include "buffer.h"
34 #include "authTag.h"
35 #include "threadUtils.hpp"
37 #include <gcrypt.h>
39 //****** NullAuthAlgo ******
41 AuthTag NullAuthAlgo::calc(const Buffer& buf)
43 return AuthTag(0);
46 //****** Sha1AuthAlgo ******
48 Sha1AuthAlgo::Sha1AuthAlgo() : ctx_(NULL)
50 Lock lock(mutex_);
52 gcry_error_t err = gcry_md_open( &ctx_, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC );
53 if( err )
54 cLog.msg(Log::PRIO_CRIT) << "Sha1AuthAlgo::Sha1AuthAlgo: Failed to open message digest algo";
57 Sha1AuthAlgo::~Sha1AuthAlgo()
59 Lock lock(mutex_);
61 gcry_md_close( ctx_ );
62 cLog.msg(Log::PRIO_DEBUG) << "Sha1AuthAlgo::~Sha1AuthAlgo: closed hmac handler";
65 void Sha1AuthAlgo::setKey(Buffer key)
67 Lock lock(mutex_);
69 gcry_error_t err;
70 err = gcry_md_setkey( ctx_, key.getBuf(), key.getLength() );
71 if( err )
72 cLog.msg(Log::PRIO_ERR) << "Sha1AuthAlgo::setKey: Failed to set cipher key: " << gpg_strerror( err );
75 AuthTag Sha1AuthAlgo::calc(const Buffer& buf)
77 Lock lock(mutex_);
79 // gcry_error_t err;
80 AuthTag hmac(10); // 10byte
81 gcry_mpi_t tmp = gcry_mpi_new(160); // 20byte
83 gcry_md_write( ctx_, static_cast<Buffer>(buf).getBuf(), buf.getLength() );
84 gcry_md_final( ctx_ );
85 gcry_mpi_scan( &tmp, GCRYMPI_FMT_STD, gcry_md_read(ctx_, 0), 20, NULL );
86 gcry_mpi_clear_highbit( tmp, 81 ); // truncate hmac from 20byte to 10byte
87 gcry_mpi_print( GCRYMPI_FMT_STD, hmac, hmac.getLength(), NULL, tmp );
88 return hmac;