| 1 | package Crypt::Dining; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use vars qw($VERSION $PORT $PACKETSZ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | # use Data::Dumper; | ||||||||||
| 7 | 3 | ▶ | use IO::Socket::INET; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Crypt::Random qw(makerandom_octet); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Net::Address::IPv4::Local; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | $VERSION = '1.01'; | |||||||||
| 12 | 3 | $PORT = 17355; | |||||||||
| 13 | 3 | $PACKETSZ = 1024; | |||||||||
| 14 | |||||||||||
| 15 | 10 | ▶ | sub debug { | ||||||||
|
Subroutine
debug: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||
| 16 | 10 | my ($self, @msg) = @_; | |||||||||
| 17 | 10 | ▶ | if ($self->{Debug}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 0 | print "[$$] ", @msg, "\n"; | |||||||||
| 19 | } | ||||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=11 SCAR=24.2
Pod
undocumented
| |||||||||||
| 23 | 6 | my $class = shift; | |||||||||
| 24 | 0 | ▶ | my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | |||||||||||
| 26 | 6 | ▶ | unless ($self->{LocalPort}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 2 | $self->{LocalPort} = $PORT; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 6 | ▶ | unless ($self->{LocalAddr}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 2 | $self->{LocalAddr} = Net::Address::IPv4::Local->public; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 6 | my $this = $self->{LocalAddr} . ":" . $self->{LocalPort}; | |||||||||
| 35 | |||||||||||
| 36 | 6 | my @peers = @{ $self->{Peers} }; | |||||||||
| 37 | 6 | ▶ | unless (@peers) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 2 | die "No peers given - protocol will be a no-op"; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 4 | foreach (0..$#peers) { | |||||||||
| 42 | 4 | ▶ | $peers[$_] .= ":$PORT" unless $peers[$_] =~ /:/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | } | ||||||||||
| 44 | 0 | @peers = sort { $a cmp $b } @peers; | |||||||||
| 45 | |||||||||||
| 46 | 4 | my $next = $peers[0]; | |||||||||
| 47 | 4 | my $prev = $peers[-1]; | |||||||||
| 48 | 4 | PEER: foreach (@peers) { | |||||||||
| 49 | 4 | ▶ | if ($this lt $_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 2 | $next = $_; | |||||||||
| 51 | 2 | last PEER; | |||||||||
| 52 | } | ||||||||||
| 53 | 2 | $prev = $_; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | # print "Peers are " . Dumper(\@peers); | ||||||||||
| 57 | # print "Prev is $prev\n"; | ||||||||||
| 58 | # print "This is $this\n"; | ||||||||||
| 59 | # print "Next is $next\n"; | ||||||||||
| 60 | |||||||||||
| 61 | 4 | $self->{Peers} = \@peers; | |||||||||
| 62 | |||||||||||
| 63 | 4 | ▶ | $prev =~ m/(.*):(.*)/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | or die "No address:port in $prev"; | ||||||||||
| 65 | 4 | $self->{PrevAddr} = $1; | |||||||||
| 66 | 4 | $self->{PrevPort} = $2; | |||||||||
| 67 | |||||||||||
| 68 | 4 | ▶ | $next =~ m/(.*):(.*)/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | or die "No address:port in $next"; | ||||||||||
| 70 | 4 | $self->{NextAddr} = $1; | |||||||||
| 71 | 4 | $self->{NextPort} = $2; | |||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | 4 | return bless $self, $class; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 10 | ▶ | sub socket_udp { | ||||||||
|
Subroutine
socket_udp: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 78 | 10 | my ($self) = @_; | |||||||||
| 79 | 10 | ▶ | unless ($self->{SocketUdp}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 2 | $self->debug( | |||||||||
| 81 | "Creating socket $self->{LocalAddr}:$self->{LocalPort}" | ||||||||||
| 82 | ); | ||||||||||
| 83 | $self->{SocketUdp} = new IO::Socket::INET( | ||||||||||
| 84 | Proto => "udp", | ||||||||||
| 85 | LocalAddr => $self->{LocalAddr}, | ||||||||||
| 86 | LocalPort => $self->{LocalPort}, | ||||||||||
| 87 | 2 | ▶ | ReuseAddr => 1, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | # Listen => 5, | ||||||||||
| 89 | ) | ||||||||||
| 90 | or die "socket: $self->{LocalAddr}:$self->{LocalPort}: $!"; | ||||||||||
| 91 | } | ||||||||||
| 92 | 10 | return $self->{SocketUdp}; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 2 | ▶ | sub listen_prev { | ||||||||
|
Subroutine
listen_prev: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 96 | 2 | my ($self) = @_; | |||||||||
| 97 | 2 | $self->socket_udp(); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 2 | ▶ | sub send_next { | ||||||||
|
Subroutine
send_next: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 101 | 2 | my ($self, $data) = @_; | |||||||||
| 102 | 2 | my $socket = $self->socket_udp(); | |||||||||
| 103 | 2 | $self->debug("Send coin to $self->{NextAddr}:$self->{NextPort}"); | |||||||||
| 104 | 2 | my $addr = pack_sockaddr_in($self->{NextPort}, inet_aton($self->{NextAddr})); | |||||||||
| 105 | 2 | return $socket->send($data, 0, $addr); | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 2 | ▶ | sub send_all { | ||||||||
|
Subroutine
send_all: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 109 | 2 | my ($self, $data) = @_; | |||||||||
| 110 | 2 | my $socket = $self->socket_udp(); | |||||||||
| 111 | 2 | foreach (@{ $self->{Peers} }) { | |||||||||
| 112 | 2 | $self->debug("Send hand to $_"); | |||||||||
| 113 | 2 | ▶ | m/(.*):(.*)/ or die "Invalid peer: $_: No host:port"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 2 | my ($host, $port) = ($1, $2); | |||||||||
| 115 | 2 | my $addr = pack_sockaddr_in($port, inet_aton($host)); | |||||||||
| 116 | 2 | $socket->send($data, 0, $addr); | |||||||||
| 117 | } | ||||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 2 | ▶ | sub recv_prev { | ||||||||
|
Subroutine
recv_prev: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 121 | 2 | my ($self) = @_; | |||||||||
| 122 | 2 | my $socket = $self->socket_udp(); | |||||||||
| 123 | |||||||||||
| 124 | 2 | my $data; | |||||||||
| 125 | 2 | my $addr = $socket->recv($data, $PACKETSZ + 4); | |||||||||
| 126 | 2 | $self->debug("Got " . length($data) . " bytes from prev"); | |||||||||
| 127 | 2 | my ($port, $iaddr) = unpack_sockaddr_in($addr); | |||||||||
| 128 | 2 | my $aaddr = inet_ntoa($iaddr); | |||||||||
| 129 | die "Unexpected packet from $aaddr:$port" | ||||||||||
| 130 | 2 | ▶ | unless $aaddr eq $self->{PrevAddr}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | 2 | return $data; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | 2 | ▶ | sub recv_all { | ||||||||
|
Subroutine
recv_all: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 135 | 2 | my ($self) = @_; | |||||||||
| 136 | 2 | my $socket = $self->socket_udp(); | |||||||||
| 137 | |||||||||||
| 138 | 2 | my %data; | |||||||||
| 139 | 2 | foreach (@{ $self->{Peers} }) { | |||||||||
| 140 | 2 | my $data; | |||||||||
| 141 | 2 | my $addr = $socket->recv($data, $PACKETSZ + 4); | |||||||||
| 142 | 2 | $data{$_} = $data; | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 2 | return %data; | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | 2 | ▶ | sub round { | ||||||||
|
Subroutine
round: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||
| 149 | 2 | my ($self, $message) = @_; | |||||||||
| 150 | |||||||||||
| 151 | 2 | my $random = makerandom_octet( | |||||||||
| 152 | Length => $PACKETSZ, | ||||||||||
| 153 | Strength => 0, | ||||||||||
| 154 | ); | ||||||||||
| 155 | # print "Random is $random\n"; | ||||||||||
| 156 | |||||||||||
| 157 | 2 | $self->listen_prev(); | |||||||||
| 158 | 2 | sleep 1; | |||||||||
| 159 | |||||||||||
| 160 | 2 | $self->send_next("coin" . $random); | |||||||||
| 161 | |||||||||||
| 162 | 2 | my $packet = $self->recv_prev(); | |||||||||
| 163 | 2 | ▶ | unless (substr($packet, 0, 4) eq 'coin') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | 0 | die "Didn't get a coin packet: got " . substr($packet, 0, 4); | |||||||||
| 165 | } | ||||||||||
| 166 | 2 | ▶ | die "Bad length for received coin data" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | unless length $packet eq $PACKETSZ + 4; | ||||||||||
| 168 | 2 | my $store = substr($packet, 4) ^ $random; | |||||||||
| 169 | 2 | ▶ | $store ^= $message if $message; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | |||||||||||
| 171 | 2 | $self->debug("===="); | |||||||||
| 172 | # sleep 1; | ||||||||||
| 173 | |||||||||||
| 174 | # return; | ||||||||||
| 175 | |||||||||||
| 176 | 2 | $self->send_all("hand" . $store); | |||||||||
| 177 | |||||||||||
| 178 | 2 | my %answers = $self->recv_all(); | |||||||||
| 179 | 2 | my $answer = $store; | |||||||||
| 180 | 2 | foreach (keys %answers) { | |||||||||
| 181 | 2 | $packet = $answers{$_}; | |||||||||
| 182 | 2 | ▶ | unless (substr($packet, 0, 4) eq 'hand') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 183 | 0 | die "Didn't get a hand packet from $_: got " . | |||||||||
| 184 | substr($packet, 0, 4); | ||||||||||
| 185 | } | ||||||||||
| 186 | 2 | ▶ | die "Bad length for received hand data" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 187 | unless length $packet eq $PACKETSZ + 4; | ||||||||||
| 188 | 2 | $answer ^= substr($packet, 4); | |||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | 2 | return $answer; | |||||||||
| 192 | } | ||||||||||
| 193 | |||||||||||
| 194 | =head1 NAME | ||||||||||
| 195 | |||||||||||
| 196 | Crypt::Dining - The Dining Cryptographers' Protocol | ||||||||||
| 197 | |||||||||||
| 198 | =head1 SYNOPSIS | ||||||||||
| 199 | |||||||||||
| 200 | my $dc = new Crypt::Dining( | ||||||||||
| 201 | LocalAddr => '123.45.6.7', | ||||||||||
| 202 | Peers => [ '123.45.6.8', ... ], | ||||||||||
| 203 | ); | ||||||||||
| 204 | my $answer = $dc->round; | ||||||||||
| 205 | my $answer = $dc->round("hello"); | ||||||||||
| 206 | |||||||||||
| 207 | =head1 DESCRIPTION | ||||||||||
| 208 | |||||||||||
| 209 | The dining cryptographers' protocol is documented in Bruce | ||||||||||
| 210 | Schneier's book as a kind of "cryptographic ouija board". It works | ||||||||||
| 211 | as follows: | ||||||||||
| 212 | |||||||||||
| 213 | A number of cryptographers are dining at a circular table. At the end | ||||||||||
| 214 | of the meal, the waiter is summoned and asked for the bill. He replies, | ||||||||||
| 215 | "Thank you, sir. The bill has been paid." The cryptographers now have | ||||||||||
| 216 | the problem of working out whether someone at the table paid the bill, | ||||||||||
| 217 | or whether the NSA has paid it as some sort of veiled threat. The | ||||||||||
| 218 | protocol proceeds. | ||||||||||
| 219 | |||||||||||
| 220 | Each cryptographer flips a coin, and shows the result ONLY to the | ||||||||||
| 221 | participant on his RIGHT. Each cryptographer then compares his coin | ||||||||||
| 222 | with that on his LEFT, and raises his hand if they show different | ||||||||||
| 223 | faces. If any participant paid the bill, he "cheats" and does the | ||||||||||
| 224 | opposite, that is, he raises his hand if the coins show the same | ||||||||||
| 225 | face. Now, the hands are counted. An odd number means that someone | ||||||||||
| 226 | at the table paid the bill. An even number means that the NSA paid. | ||||||||||
| 227 | |||||||||||
| 228 | =head1 ASSUMPTIONS AND IMPLEMENTATION | ||||||||||
| 229 | |||||||||||
| 230 | At most one person "cheats" at any time, otherwise the message is | ||||||||||
| 231 | scrambled. Detecting scrambling is only possible with multi-bit | ||||||||||
| 232 | messages containing a checksum. | ||||||||||
| 233 | |||||||||||
| 234 | The comparison operator described above is the XOR operator on | ||||||||||
| 235 | single-bit values. If the protocol is performed with multi-bit | ||||||||||
| 236 | messages, then the XOR is still used. | ||||||||||
| 237 | |||||||||||
| 238 | =head1 WIKIPEDIA DESCRIPTION | ||||||||||
| 239 | |||||||||||
| 240 | The following description is copied from | ||||||||||
| 241 | L<http://en.wikipedia.org/wiki/Dining_cryptographers_protocol> and | ||||||||||
| 242 | is redistributed under the GNU Free Documentation License. It is | ||||||||||
| 243 | a very slightly different protocol to that implemented here, but the | ||||||||||
| 244 | result is the same. | ||||||||||
| 245 | |||||||||||
| 246 | The dining cryptographers protocol is a method of anonymous | ||||||||||
| 247 | communication. It offers untraceability of both the sender and the | ||||||||||
| 248 | recipient. | ||||||||||
| 249 | |||||||||||
| 250 | The method is as follows: two or more cryptographers arrange | ||||||||||
| 251 | themselves around a circular dinner table, with menus hiding the | ||||||||||
| 252 | interaction of each pair of adjacent cryptographers from the rest. | ||||||||||
| 253 | Each adjacent pair picks a random number in private. Then each | ||||||||||
| 254 | cryptographer announces publicly the difference between the number | ||||||||||
| 255 | on his right and the number on his left, adding a message if he | ||||||||||
| 256 | wants to transmit one. All cryptographers then add up the publicly | ||||||||||
| 257 | announced numbers. If the sum is 0, no one sent a message. If the | ||||||||||
| 258 | sum is a valid message, one cryptographer transmitted a message. If | ||||||||||
| 259 | the sum is invalid, more than one cryptographer tried to transmit a | ||||||||||
| 260 | message; they wait a random time and try again. | ||||||||||
| 261 | |||||||||||
| 262 | =head1 BUGS | ||||||||||
| 263 | |||||||||||
| 264 | If the send_*() and recv_*() methods are overridden to use TCP sockets | ||||||||||
| 265 | with very large messages, deadlock may occur around the ring unless | ||||||||||
| 266 | something intelligent is done with select(). | ||||||||||
| 267 | |||||||||||
| 268 | =head1 SEE ALSO | ||||||||||
| 269 | |||||||||||
| 270 | L<http://en.wikipedia.org/wiki/Dining_cryptographers_protocol>, | ||||||||||
| 271 | L<Crypt::Chimera> - another cryptographic curiosity. | ||||||||||
| 272 | |||||||||||
| 273 | =head1 COPYRIGHT | ||||||||||
| 274 | |||||||||||
| 275 | Copyright (c) 2005 Shevek. All rights reserved. | ||||||||||
| 276 | |||||||||||
| 277 | This program is free software; you can redistribute it and/or modify | ||||||||||
| 278 | it under the same terms as Perl itself. | ||||||||||
| 279 | |||||||||||
| 280 | =cut | ||||||||||
| 281 | |||||||||||
| 282 | 3 | 1; | |||||||||