Skip to content

Commit

Permalink
include other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
teathsch committed Aug 11, 2011
1 parent c868ecf commit 0c27908
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 122 deletions.
4 changes: 2 additions & 2 deletions Makefile
@@ -1,6 +1,6 @@
#WALLEXTRA=-Wall -Wextra
WARNINGFLAGS=-Wall
LIBS = `pkg-config --libs libcurl gtkmm-2.4 sqlite3 libcrypto` -lboost_thread -lboost_program_options
CFLAGS = $(WALLEXTRA) `pkg-config --cflags libcurl gtkmm-2.4 sqlite3 libcrypto` -I./json_spirit_v4.03
CFLAGS = $(WARNINGFLAGS) `pkg-config --cflags libcurl gtkmm-2.4 sqlite3 libcrypto | sed -e 's/-I/-I/g'` -I./json_spirit_v4.03

all:
g++ $(CFLAGS) -c main.cpp -o main.o
Expand Down
23 changes: 16 additions & 7 deletions buffer.h
Expand Up @@ -15,8 +15,10 @@ template<size_t max_size> class buffer_t {

buffer_t(const char * tempdata, size_t templen) : data() , \
len(templen) , iter(0) {
if (len > max_size)
throw (const char *) ("Overflow.");
if (len > max_size) {
std::cout << "len: " << len << std::endl;
throw (const char *) ("Overflow in buffer()");
}
memcpy(data, tempdata, len);
}

Expand All @@ -27,7 +29,7 @@ template<size_t max_size> class buffer_t {
buffer_t & operator = (const char * rhs) {
len = strlen((char *) rhs);
if (len > max_size)
throw (const char *) ("Overflow.");
throw (const char *) ("Overflow in buffer::operator=.");
memcpy(data, (int8_t *) rhs, len);
iter = 0;
return *this;
Expand Down Expand Up @@ -63,8 +65,8 @@ template<size_t max_size> class buffer_t {
return temp;
}

const char * operator * () const {return (const char *) data; }
char * operator * () { return (char *) data; }
const char * operator * () const {return (const char *) data + iter; }
char * operator * () { return (char *) data + iter; }

const uint8_t & operator[](const size_t & idx) const {
if (idx > max_size)
Expand All @@ -82,6 +84,7 @@ template<size_t max_size> class buffer_t {

const size_t & length() const { return len; }
const size_t & iterator() const { return iter; }
size_t & iterator() { return iter; }

template<size_t bufsize>
friend std::ostream & operator << (std::ostream &,const buffer_t &);
Expand All @@ -108,8 +111,14 @@ template<size_t max_size> class buffer_t {
return *this;
}

// template<typename T> buffer_t & operator << (const buffer_t<max_size> & rhs) {
// this->len += rhs.len;
// }

template<typename T> buffer_t & operator << (const T & rhs) {

std::cout << "DID GOT HERE" << std::endl;

if (sizeof(rhs) > max_size)
throw (const char *) ("Too big.");

Expand All @@ -128,12 +137,12 @@ template<size_t max_size> class buffer_t {

void begin() { iter = 0; };

const bool operator < (const buffer_t & rhs) {
bool operator < (const buffer_t & rhs) {
return strncmp((char *) this->data, (char *) rhs.data, \
this->len < rhs.len ? this->len : rhs.len) < 0;
}

const bool operator ==(const buffer_t & rhs) {
bool operator ==(const buffer_t & rhs) {
return strncmp(this->data, rhs.data, \
this->len < rhs.len ? this->len : rhs.len) == 0;
}
Expand Down
20 changes: 10 additions & 10 deletions cryptopeer.h
Expand Up @@ -2,7 +2,7 @@
#define CRYPTOPEER_H

#include "myaes.h"
#include "curve25519.h"
#include "ecdhcrypto.h"
#include "buffer.h"

#include <deque>
Expand Down Expand Up @@ -88,8 +88,8 @@ template<size_t = 0> class crypto_user_t : public crypto_friend<> {
memset(temp_shared, 0, 32 );
memset(encrypted_nonce, 0, 256);

curve25519_keypairgen(ecdh_pvt, ecdh_pub);
curve25519(temp_shared, ecdh_pvt, their_pubkey);
generate_ec_keys(ecdh_pvt, ecdh_pub);
compute_ec_shared_secret(ecdh_pvt, their_pubkey, temp_shared);

for (size_t i = 0; i < 32; i++)
nonce[i] = rand() % 256;
Expand All @@ -110,7 +110,7 @@ template<size_t = 0> class crypto_user_t : public crypto_friend<> {

memset(decrypted_nonce, 0, 256);

curve25519(shared, ecdh_pvt, response);
compute_ec_shared_secret(ecdh_pvt, response, shared);

decrypted_nonce_len = aes_decrypt(shared, response + 32, response_len - 32, decrypted_nonce);

Expand Down Expand Up @@ -157,12 +157,12 @@ template<size_t = 0> class crypto_peer_t : public crypto_friend<> {
memset(encrypted_nonce, 0, 256);

// the first part of the intro is the pubkey
curve25519(temp_shared, my_pvtkey, intro);
compute_ec_shared_secret(my_pvtkey, intro, temp_shared);

aes_decrypt(temp_shared, intro + 32, intro_len - 32, decrypted_nonce);

curve25519_keypairgen(ecdh_pvt, ecdh_pub);
curve25519(shared, ecdh_pvt, intro);
generate_ec_keys(ecdh_pvt, ecdh_pub);
compute_ec_shared_secret(ecdh_pvt, intro, shared);

unsigned char nonce_xor_pubkey[32];

Expand Down Expand Up @@ -204,14 +204,14 @@ void do_print(std::string label, const unsigned char * buf, int len) {

int main() {

srand(time(NULL));
//srand(time(NULL));

unsigned char ecdh_pub[32];
unsigned char ecdh_pvt[32];
curve25519_keypairgen(ecdh_pvt, ecdh_pub);
generate_ec_keys(ecdh_pvt, ecdh_pub);

crypto_user user(ecdh_pub);
crypto_user user2(user);
//crypto_user user2(user);

unsigned char intro[256];
int intro_len = user.get_data(intro);
Expand Down
Binary file modified debian/voicedotbit_0.1_i386.deb
Binary file not shown.
57 changes: 34 additions & 23 deletions ecdh_aes.h
Expand Up @@ -2,12 +2,10 @@

// Create a general purpose cryptosystem by combining ecdh and aes

// FIXME: Need to derive the key from the shared secret using standard methods

#ifndef ECDH_AES_H
#define ECDH_AES_H

#include "curve25519.h"
#include "ecdhcrypto.h"
#include "myaes.h"

int encrypt_once(unsigned char * server_public, \
Expand All @@ -17,12 +15,22 @@ int encrypt_once(unsigned char * server_public, \
unsigned char client_public[32];
unsigned char shared[32];

curve25519_keypairgen(client_secret, client_public);
curve25519(shared, client_secret, server_public);
//std::cout << "ABOUT TO GEN KEYS AGAIN!" << std::endl << std::endl;

generate_ec_keys(client_secret, client_public);

//std::cout << "DONE GENERATING KEYS" << std::endl << std::endl;

// curve25519_keypairgen(client_secret, client_public);
// curve25519(shared, client_secret, server_public);
compute_ec_shared_secret(client_secret, server_public, shared);

//std::cout << "DONE COMPUTING SHARED SECRET" << std::endl << std::endl;

unsigned char ciphertext[1024];

int ci_len = aes_encrypt(shared, 32, cleartext, cleartext_len, ciphertext);
// int ci_len = aes_encrypt(shared, 32, cleartext, cleartext_len, ciphertext);
int ci_len = aes_encrypt(shared, cleartext, cleartext_len, ciphertext);

memcpy(packet, client_public, 32);
memcpy(packet + 32, ciphertext, ci_len);
Expand All @@ -33,28 +41,31 @@ int decrypt_once(unsigned char * server_private, unsigned char * packet, \
int packet_len, unsigned char * decrypted) {

unsigned char shared[32];
curve25519(shared, server_private, packet);
return aes_decrypt(shared, 32, packet + 32, packet_len - 32, decrypted);
// curve25519(shared, server_private, packet);
// compute_ec_shared_secret(shared, 32, packet + 32, packet_len - 32, decrypted);
compute_ec_shared_secret(server_private, packet, shared);
// return aes_decrypt(shared, 32, packet + 32, packet_len - 32, decrypted);
return aes_decrypt(shared, packet + 32, packet_len - 32, decrypted);
}

class cryptopeer {
public:
cryptopeer() : my_temp_pvt_ecdh_key(0) , \
my_temp_pub_ecdh_key(0) , \
their_temp_pub_ecdh_key(0) , \
their_perm_pub_ecdh_key(0) , \
our_aes_key(0) {
//class cryptopeer {
// public:
// cryptopeer() : my_temp_pvt_ecdh_key(0) ,
// my_temp_pub_ecdh_key(0) ,
// their_temp_pub_ecdh_key(0) ,
// their_perm_pub_ecdh_key(0) ,
// our_aes_key(0) {

}
// }

private:
// private:

char my_temp_pvt_ecdh_key[32];
char my_temp_pub_ecdh_key[32];
char their_temp_pub_ecdh_key[32];
char their_perm_pub_ecdh_key[32];
char our_aes_key;
// char my_temp_pvt_ecdh_key[32];
// char my_temp_pub_ecdh_key[32];
// char their_temp_pub_ecdh_key[32];
// char their_perm_pub_ecdh_key[32];
// char our_aes_key;

};
//};

#endif
8 changes: 6 additions & 2 deletions ecdhcrypto.h
@@ -1,3 +1,7 @@
// Copyright 2011 Heath Schultz et. al. under liberal licenses. See LICENSE for details.

// Wrap elliptic curve crypto functions

#ifndef ECDHCRYPTO_H
#define ECDHCRYPTO_H

Expand Down Expand Up @@ -122,7 +126,7 @@ template<typename UChar> EC_POINT * bn_to_pt(UChar * pub_key, EC_KEY * key,
return res;
}

template<typename UChar> int compute_ec_shared_secret(const UChar * my_pvt_key,
template<typename UChar> void compute_ec_shared_secret(const UChar * my_pvt_key,
const UChar * their_pub_key, UChar * shared_secret) {

EC_KEY * my_key = NULL;
Expand Down Expand Up @@ -188,7 +192,7 @@ bool do_verify(const UChar * pub_key, const UChar * msg, const int len,
return ECDSA_verify(0, dgst, 32, sig, siglen, key) == 1;
}

template<typename UChar> const bool ec_verify(const UChar * pub_key,
template<typename UChar> bool ec_verify(const UChar * pub_key,
const UChar * msg, int len, const UChar * sig, int siglen) {

// run both 2 and 3 to prevent a traffic analysis attack
Expand Down

0 comments on commit 0c27908

Please sign in to comment.