[4a4662]: script.module.cryptopy / lib / crypto / cipher / trolldoll.py  Maximize  Restore  History

Download this file

86 lines (70 with data), 3.4 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: iso-8859-1 -*-
""" crypto.cipher.trolldoll
Modification to Icedoll to take advantage of the better error extension
and provide a IV for randomization of the output and integrity checking.
IV is simply prepended to plaintext.
Integrity check is appended to the end of the plain text as
zeros with count of the blocks in the ciphertext.
Note !!!! auto IV uses python default random :-(
should not be 'too bad' (tm) for this applicaiton
ALso ... currently just IV .... in test ..
Copyright Š (c) 2002 by Paul A. Lambert
Read LICENSE.txt for license information.
"""
from crypto.cipher.icedoll import Icedoll
from crypto.errors import IntegrityCheckError
from random import Random # should change to crypto.random!!!
class Trolldoll(Icedoll):
""" Trolldoll encryption algorithm
based on Icedoll, which is based on Rijndael
Trolldoll adds an 'IV' and integrity checking to Icedoll
"""
def __init__(self,key=None,keySize=32,blockSize=32,tapRound=6,extraRounds=6,micSize=16,ivSize=16):
""" """
Icedoll.__init__(self,key=None,keySize=32,blockSize=32,tapRound=6,extraRounds=6)
self.name = 'TROLLDOLL'
self.micSize = micSize
self.ivSize = ivSize
self.r = Random() # for IV generation
import time
newSeed = time.ctime()+str(self.r) # seed with instance location
self.r.seed(newSeed) # to make unique
self.reset()
def reset(self):
Icedoll.reset(self)
self.hasIV = None
def _makeIV(self):
return self.ivSize*'a'
def _makeIC(self):
""" Make the integrity check """
return self.micSize*chr(0x00)
def _verifyIC(self,integrityCheck):
""" Verify the integrity check """
if self.micSize*chr(0x00) == integrityCheck :
return 1 # matches
else:
return 0 # fails
def encrypt(self, plainText, more=None):
""" """
if not(self.hasIV): # On first call to encrypt put in an IV
plainText = self._makeIV() + plainText # add the 'IV'
self.hasIV = 1
if more == None: # on last call to encrypt append integrity check
plainText = plainText + self._makeIC()
return Icedoll.encrypt(self, plainText, more=more)
def decrypt(self, cipherText, more=None):
""" Decrypt cipher text, Icedoll automatically removes
prepended random bits used as IV.
Note - typically IV is directly used as the first
cipher text. Here the IV is prepended to the plaintext
prior to encryption and removed on decryption.
"""
plainText = Icedoll.decrypt(self, cipherText, more=more)
if not(self.hasIV): # on first call to decrypt remove IV
plainText = plainText[self.ivSize:] # remove the IV
self.hasIV = 1
if more == None: # on last call to encrypt append integrity check
if not(self._verifyIC(plainText[-self.micSize:])) :
raise IntegrityCheckError, 'Trolldoll MIC Failure, bad key or modified data'
plainText = plainText[:-self.micSize] # trim off the integrity check
return plainText

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks