17.6 Stream Ciphers

Module: sage.crypto.stream_cipher

Stream Ciphers

Class: LFSRCipher

class LFSRCipher
LFSRCipher( self, parent, poly, IS)

Create a linear feedback shift register (LFSR) cipher.

Input:

parent
- parent
poly
- connection polynomial
IS
- initial state

sage: FF = FiniteField(2)
sage: P.<x> = PolynomialRing(FF)
sage: E = LFSRCryptosystem(FF)
sage: E
LFSR cryptosystem over Finite Field of size 2
sage: IS = [ FF(a) for a in [0,1,1,1,0,1,1] ]
sage: g = x^7 + x + 1
sage: e = E((g,IS))
sage: B = BinaryStrings()
sage: m = B.encoding("THECATINTHEHAT")
sage: e(m)
001000110111101011101010101000110000000011010001010101110000101111001001000
0011111100100100011001101101000001111
sage: FF = FiniteField(2)
sage: P.<x> = PolynomialRing(FF)
sage: LFSR = LFSRCryptosystem(FF)
sage: e = LFSR((x^2+x+1,[FF(0),FF(1)]))
sage: B = e.domain()
sage: m = B.encoding("The cat in the hat.")
sage: e(m)
001110011101111010111110010011011101010110111010000110011001011010110010000
000111001011010101111000001011101001111111011000001011101011110101111010000
11
sage: m == e(e(m))
True

TESTS:

sage: FF = FiniteField(2)
sage: P.<x> = PolynomialRing(FF)
sage: E = LFSRCryptosystem(FF)
sage: E == loads(dumps(E))
True

Functions: connection_polynomial,$ \,$ initial_state

connection_polynomial( self)

The connection polynomial defining the LFSR of the cipher.

sage: k = GF(2)
sage: P.<x> = PolynomialRing( k )
sage: LFSR = LFSRCryptosystem( k )
sage: e = LFSR((x^2+x+1,[k(0), k(1)]))
sage: e.connection_polynomial()
x^2 + x + 1

initial_state( self)

The initial state of the LFSR cipher.

sage: k = GF(2)
sage: P.<x> = PolynomialRing( k )
sage: LFSR = LFSRCryptosystem( k )
sage: e = LFSR((x^2+x+1,[k(0), k(1)]))
sage: e.initial_state()
[0, 1]

Special Functions: __call__,$ \,$ __init__

__call__( self, M, [mode=ECB])

Generate key stream from the binary string M.

Input:

M
- a StringMonoidElement
mode
- ignored (default: 'ECB')

sage: k = GF(2)
sage: P.<x> = PolynomialRing( k )
sage: LFSR = LFSRCryptosystem( k )
sage: e = LFSR((x^2+x+1,[k(0), k(1)]))
sage: B = e.domain()
sage: m = B.encoding('The cat in the hat.')
sage: e(m)
001110011101111010111110010011011101010110111010000110011001011010110010000
000111001011010101111000001011101001111111011000001011101011110101111010000
11

Class: ShrinkingGeneratorCipher

class ShrinkingGeneratorCipher
ShrinkingGeneratorCipher( self, parent, e1, e2)

Create a shrinking generator cipher.

Input:

parent
- parent
poly
- connection polynomial
IS
- initial state

       sage: FF = FiniteField(2)
       sage: P.<x> = PolynomialRing(FF)
       sage: LFSR = LFSRCryptosystem(FF)
       sage: IS_1 = [ FF(a) for a in [0,1,0,1,0,0,0] ]
       sage: e1 = LFSR((x^7 + x + 1,IS_1))
       sage: IS_2 = [ FF(a) for a in [0,0,1,0,0,0,1,0,1] ]
       sage: e2 = LFSR((x^9 + x^3 + 1,IS_2))
       sage: E = ShrinkingGeneratorCryptosystem()
       sage: e = E((e1,e2))
sage: e
((x^7 + x + 1, [0, 1, 0, 1, 0, 0, 0]), (x^9 + x^3 + 1, [0, 0, 1, 0, 0, 0,
1, 0, 1]))

Functions: decimating_cipher,$ \,$ keystream_cipher

decimating_cipher( self)

The LFSR cipher generating the decimating key stream.

       sage: FF = FiniteField(2)
       sage: P.<x> = PolynomialRing(FF)
       sage: LFSR = LFSRCryptosystem(FF)
       sage: IS_1 = [ FF(a) for a in [0,1,0,1,0,0,0] ]
       sage: e1 = LFSR((x^7 + x + 1,IS_1))
       sage: IS_2 = [ FF(a) for a in [0,0,1,0,0,0,1,0,1] ]
       sage: e2 = LFSR((x^9 + x^3 + 1,IS_2))
       sage: E = ShrinkingGeneratorCryptosystem()
       sage: e = E((e1,e2))
sage: e.decimating_cipher()
       (x^9 + x^3 + 1, [0, 0, 1, 0, 0, 0, 1, 0, 1])

keystream_cipher( self)

The LFSR cipher generating the output key stream.

       sage: FF = FiniteField(2)
       sage: P.<x> = PolynomialRing(FF)
       sage: LFSR = LFSRCryptosystem(FF)
       sage: IS_1 = [ FF(a) for a in [0,1,0,1,0,0,0] ]
       sage: e1 = LFSR((x^7 + x + 1,IS_1))
       sage: IS_2 = [ FF(a) for a in [0,0,1,0,0,0,1,0,1] ]
       sage: e2 = LFSR((x^9 + x^3 + 1,IS_2))
       sage: E = ShrinkingGeneratorCryptosystem()
       sage: e = E((e1,e2))
sage: e.keystream_cipher()
       (x^7 + x + 1, [0, 1, 0, 1, 0, 0, 0])

Special Functions: __call__,$ \,$ __init__

__call__( self, M, [mode=ECB])

Input:

M
- a StringMonoidElement
mode
- ignored (default: 'ECB')

sage: FF = FiniteField(2)   
sage: P.<x> = PolynomialRing(FF)    
sage: LFSR = LFSRCryptosystem(FF)   
sage: IS_1 = [ FF(a) for a in [0,1,0,1,0,0,0] ]     
sage: e1 = LFSR((x^7 + x + 1,IS_1)) 
sage: IS_2 = [ FF(a) for a in [0,0,1,0,0,0,1,0,1] ] 
sage: e2 = LFSR((x^9 + x^3 + 1,IS_2))       
sage: E = ShrinkingGeneratorCryptosystem()  
sage: e = E((e1,e2))
sage: B = BinaryStrings()   
sage: m = B.encoding("THECATINTHEHAT")      
sage: c = e(m)
sage: c.decoding()
'\xac\xfa\xfd\xc6\xa7\xe5\x16\x8f\xa2Q\xb8\xb7\xbe\xab'
sage: e(e(m)) == m
True
sage: m.decoding()
'THECATINTHEHAT'

See About this document... for information on suggesting changes.