This repository has been archived by the owner on Aug 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Anon implementation: starting to prototype in python
- Loading branch information
Henry Corrigan-Gibbs
committed
Apr 1, 2010
1 parent
cff6bf5
commit 11072fd
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import logging, random | ||
from logging import debug, info | ||
import asyncore, socket, cPickle | ||
import M2Crypto.RSA | ||
|
||
class anon_node(): | ||
def __init__(self, id, total_nodes, socket_n, key_len, round_id): | ||
info("Node started (id=%d, total_nodes=%d, socket_n=%d, key_len=%d, round_id=%d)" | ||
% (id, total_nodes, socket_n, key_len, round_id)) | ||
|
||
self.id = id | ||
self.downstream = (id - 1) % total_nodes | ||
self.upstream = (id + 1) % total_nodes | ||
|
||
self.total_nodes = total_nodes | ||
self.socket_n = socket_n | ||
self.key_len = key_len | ||
self.round_id = round_id | ||
|
||
# | ||
# Protocol phases | ||
# | ||
|
||
def run_phase1(self): | ||
self.generate_keys() | ||
|
||
def phase1_msg(self): | ||
return "Test from #%d" % (id) | ||
return cPickle.dumps( | ||
(self.id, | ||
self.round_id, | ||
self.key_from_file(1), | ||
self.key_from_file(2))) | ||
|
||
|
||
# | ||
# Network stuff | ||
# | ||
|
||
def set_up_connections(self): | ||
# Server for i-1, client to i+1 | ||
|
||
|
||
|
||
def port_for_node(self, node_id): | ||
return self.socket_n + node_id | ||
|
||
# | ||
# Key and file IO | ||
# | ||
def key_from_file(self, key_number): | ||
str = "" | ||
f = open(self.key_filename(key_number), 'r') | ||
for line in f: | ||
str += line | ||
return str | ||
|
||
def generate_keys(self): | ||
self.key1 = self.random_key() | ||
self.key2 = self.random_key() | ||
self.save_pub_key(self.key1, 1) | ||
self.save_pub_key(self.key2, 2) | ||
|
||
def save_pub_key(self, rsa_key, key_number): | ||
rsa_key.save_pub_key(self.key_filename(key_number)) | ||
|
||
def key_filename(self, key_number): | ||
return "/tmp/anon_node_%d_%d.pem" % (self.id, key_number) | ||
|
||
def random_key(self): | ||
info("Generating keypair, please wait...") | ||
return M2Crypto.RSA.gen_key(self.key_len, 65537) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import logging | ||
from logging import debug, info | ||
import node_set | ||
import sys | ||
|
||
def __main__(argv): | ||
min_nodes = 3 | ||
min_socket_start = 5000 | ||
min_key_len = 1024 | ||
|
||
if(len(argv) < 3): | ||
raise ValueError, "Usage: %s n_nodes socket_start key_len" % (argv[0]) | ||
|
||
n_nodes = int(argv[1]) | ||
socket_start = int(argv[2]) | ||
key_len = int(argv[3]) | ||
|
||
if(n_nodes < min_nodes): | ||
raise ValueError, "Cannot run protocol with less than %d nodes" % (min_nodes) | ||
if(key_len < min_key_len): | ||
raise ValueError, "Key length must be larger than ", min_key_len | ||
if(socket_start < min_socket_start): | ||
raise ValueError, "Socket number must be larger than ", min_socket_start | ||
|
||
logger = logging.getLogger() | ||
logger.setLevel(logging.DEBUG) | ||
|
||
info("== Anonymous Protocol ==") | ||
info("Run as %s" % (argv[0])) | ||
info("n_nodes = %d" % (n_nodes)) | ||
info("socket_start = %d" % (socket_start)) | ||
info("key_len = %d" % (key_len)) | ||
|
||
node_set.node_set(n_nodes, socket_start, key_len) | ||
|
||
|
||
|
||
__main__(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from logging import debug, info | ||
import anon_node, random | ||
|
||
class node_set(): | ||
def __init__(self, n_nodes, socket_start, key_len): | ||
self.n_nodes = n_nodes | ||
self.socket_start = socket_start | ||
self.key_len = key_len | ||
self.round_id = random.randint(1, 10000) | ||
self.create_nodes() | ||
|
||
def create_nodes(self): | ||
for i in xrange(0, self.n_nodes): | ||
anon_node.anon_node(i, self.n_nodes, | ||
self.socket_start+i, self.key_len, self.round_id) |