blib/lib/Crypt/Nash.pm

filter: Statementstmt 100% 54 / 54 Branchbran 57.1% 8 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 92.3% 72 / 78 CC 10 SCAR 12.5
CC
10
Cov
91%
new
6.3
_DEBUG
2.1
_tick
2.0
CRAP 3.5
↑ Summary
1 package Crypt::Nash;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $DEBUG = 0;
5 2 our $VERSION = 0.1;
6  
7 =head1 NAME
8  
9 Crypt::Nash - Implementation of Nash Cryptosystem
10  
11 =head1 DESCRIPTION
12  
13 This is a self-synchronizing cipher feedback stream cipher
14 proposed by John Nash in 1950, just recently declassified.
15  
16 =head1 NOTES
17  
18 -- Nash doesn't say anything about the initial state of the bits
19 in the system; here we allow an initial state as part of the key
20 It would be reasonable and interesting to consider other
21 possibilities, such as having a fixed initial state (all zeros),
22 or running the system with "0"'s as input for a while to arrive
23 at an initial state, or ... ??
24  
25 -- We implement the example given in his note. There is one arrow
26 missing a label; we assume here the missing label is a "+".
27 We also choose an arbitrary starting state as part of the key.
28  
29 -- There are many interesting open questions about this system;
30 here are some as ``food for thought'':
31 (a) Are there ``weak keys''? (Keys that shouldn't be used?)
32 (b) If the system receives periodic input, it will exhibit
33 periodic output. (E.g. input 001001001001001...)
34 What can be said about the periodicities?
35 (c) How do different guesses about what Nash intended
36 for the starting state affect security?
37 (d) How long can a given bit circulate internally?
38 (e) Can you figure out the permutations and bit-flips if you are allowed
39 to specify inputs to the system, and to reset it to
40 the initial state whenever you like? (Effectively, a
41 chosen ciphertext attack)
42 (f) Is the output of the system balanced (equal number of 0's and 1's)
43 or unbalanced (biased somehow)?
44  
45 =head1 METHODS
46  
47 =cut
48  
49
50 =head2 new <n>, <red permutation>, <red bits>, <blue permutation>, <blue bits>, <initial permutation>
51  
52 =over 4
53  
54 =item n - number of state bits (not counting D, P entry point, or output bit)
55  
56 =item red permutation - specifies the red permutation: redp[i] says where bit i comes from, in the red permutation
57  
58 =item red bits - 1 = complement, 0 = no complement
59  
60 =item blue permutation - blue permutation
61  
62 =item blue bits - same as for redbits
63  
64 =item initial permuatation - initial state P[0...n] and P[n+1]=output bit. P[0] is entry point
65  
66 =back
67  
68 =cut
69 2 ▶sub new {
Subroutine new: called CC=6 SCAR=18.3
Pod documented
70 2 my $class = shift;
71 2 my $n = shift;
72 2 my $red_p = shift;
73 2 my $red_bits = shift;
74 2 my $blue_p = shift;
75 2 my $blue_bits = shift;
76 2 my $initial_p = shift;
77  
78 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶ die "Red p is not $n+2" unless $n+2 == scalar(@$red_p);
Branch: 1 branch
BranchTrueFalseTotal
unless $n + 2 == scalar @$red_p 0 2 2
80 2 ▶ die "Red b is not $n+2" unless $n+2 == scalar(@$red_bits);
Branch: 1 branch
BranchTrueFalseTotal
unless $n + 2 == scalar @$red_bits 0 2 2
81 2 ▶ die "Blue p is not $n+2" unless $n+2 == scalar(@$blue_p);
Branch: 1 branch
BranchTrueFalseTotal
unless $n + 2 == scalar @$blue_p 0 2 2
82 2 ▶ die "Blue b is not $n+2" unless $n+2 == scalar(@$blue_bits);
Branch: 1 branch
BranchTrueFalseTotal
unless $n + 2 == scalar @$blue_bits 0 2 2
83 2 ▶ die "Init p is not $n+2" unless $n+2 == scalar(@$initial_p);
Branch: 1 branch
BranchTrueFalseTotal
unless $n + 2 == scalar @$initial_p 0 2 2
84
85 2 return bless {
86 n => $n,
87 red_p => $red_p,
88 red_bits => $red_bits,
89 blue_p => $blue_p,
90 blue_bits => $blue_bits,
91 p => $initial_p,
92 }, $class;
93 }
94  
95 # advance state for one tick, with input ciphertext bit c.
96 28 ▶sub _tick {
Subroutine _tick: called CC=2 SCAR=6.9
97 28 my $self = shift;
98 28 my $c = shift;
99 28 ▶ if (0==$c) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $c) { } 6 22 28
100 # use blue permutation
101 # copy P[bluep[[i]] to P[i], complementing if bluebits[i]==1 (a "-" label on the blue arrow)
102 6 $self->{p} = [ map { $self->{p}->[$self->{blue_p}->[$_]] ^ $self->{blue_bits}->[$_] } (0..$self->{n}+1) ];
103 } else {
104 # use red permutation
105 # copy P[redp[[i]] to P[i], complementing if redbits[i]==1 (a "-" label on the red arrow)
106 22 $self->{p} = [ map { $self->{p}->[$self->{red_p}->[$_]] ^ $self->{red_bits}->[$_] } (0..$self->{n}+1) ];
107 }
108 # entry point of P gets new bit
109 28 $self->{p}->[0] = $c;
110 28 _DEBUG("State: ".$c." ".join("", @{$self->{p}}));
111 }
112  
113 =head2 encrypt <bit stream>
114  
115 Encrypt bitstring, return ciphertext string
116  
117 =cut
118 1 ▶sub encrypt {
Subroutine encrypt: called CC=2 SCAR=6.9
Pod documented
119 1 my $self = shift;
120 1 my $bs = shift;
121 1 my $cs = [];
122 1 _DEBUG("Encrypt: encrypting string bs = ".join("", @$bs));
123 1 foreach my $b (@$bs) {
124 14 my $c = $b ^ $self->{p}->[-1];
125 14 push @$cs, $c;
126 14 $self->_tick($c);
127 }
128 1 _DEBUG("Encrypt: ciphertext string cs = ".join("", @$cs));
129 1 return $cs;
130 }
131  
132 =head2 decrypt
133  
134 Decrypt bitstring, return ciphertext string
135  
136 =cut
137 1 ▶sub decrypt {
Subroutine decrypt: called CC=2 SCAR=6.9
Pod documented
138 1 my $self = shift;
139 1 my $cs = shift;
140 1 my $bs = [];
141 1 _DEBUG("Decrypt: decrypting string cs = ".join("", @$cs));
142 1 foreach my $c (@$cs) {
143 14 my $b = $self->{p}->[-1] ^ $c;
144 14 $self->_tick($c);
145 14 push @$bs, $b;
146 }
147 1 _DEBUG("Decrypt: plaintext string bs = ".join("", @$bs));
148 1 return $bs;
149
150 }
151  
152  
153 32 ▶sub _DEBUG {
Subroutine _DEBUG: called CC=2 SCAR=7.2
154 32 my $mess = shift;
155 32 ▶ warn $mess if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Crypt::Nash::DEBUG 0 32 32
156 }
157  
158 =head1 AUTHOR
159  
160 Python Implementation by Ronald L. Rivest (2/17/2012)
161  
162 Available here http://courses.csail.mit.edu/6.857/2012/files/nash.py
163  
164 Perl port by Simon Wistow
165  
166 =head1 LICENSE
167  
168 Distributed under the same terms as Perl itself
169  
170 =cut
171  
172 2 1;
↑ Summary