def run(self):
     """run when our device driver thread is started"""
     enc = SettingsBase.get_setting(self, "encryption")
     if enc:
         enc_key = '%016x' % 0x1234
         kSize = len(enc_key)
         aes = AES(enc_key, keySize=kSize, padding=padWithPadLen())
         
     self.sd = socket(AF_INET, SOCK_DGRAM)
     self.sd.bind((self.ip, self.port))
     self.sd.setblocking(0)
     
     while self.__stopevent.isSet()==False:
         while self.__stopevent.isSet()==False:
             try:
                 buf, addr = self.sd.recvfrom(255)
                 break
             except:
                 time.sleep(0.01)
         try:
             data=str(buf)
         except:
             data='data error'
         if enc:
             data=aes.decrypt(data)
         
         self.property_set(self.ch_name, Sample(0, str(data), self.ch_unit))
             
             
     self.__stopevent.clear()
Пример #2
0
    def __init__(self,
                 key=None,
                 padding=padWithPadLen(),
                 keySize=16,
                 blockSize=16,
                 tapRound=6,
                 extraRounds=6):
        """ key, keysize, blockSize same as Rijndael, tapROund is feedback tap, """
        self.tapRound = tapRound  # <------- !!! change from Rijndael !!!
        self.extraRounds = extraRounds  # <------- !!! change from Rijndael !!!
        self.name = 'ICEDOLL'
        self.keySize = keySize
        self.strength = keySize
        self.blockSize = blockSize  # blockSize is in bytes
        self.padding = padding  # change default to noPadding() to get normal ECB behavior

        assert (keySize % 4 == 0 and NrTable[4].has_key(
            keySize / 4)), 'key size must be 16,20,24,29 or 32 bytes'
        assert (blockSize % 4 == 0 and NrTable.has_key(
            blockSize / 4)), 'block size must be 16,20,24,29 or 32 bytes'

        self.Nb = self.blockSize / 4  # Nb is number of columns of 32 bit words
        self.Nk = keySize / 4  # Nk is the key length in 32-bit words
        self.Nr = NrTable[self.Nb][
            self.Nk] + extraRounds  # <------- !!! change from Rijndael !!!

        if key != None:
            self.setKey(key)
Пример #3
0
    def __init__(self, key = None, padding = padWithPadLen(), keySize=16):
        """ Initialize AES, keySize is in bytes """
        if  not (keySize == 16 or keySize == 24 or keySize == 32) :
            raise BadKeySizeError, 'Illegal AES key size, must be 16, 24, or 32 bytes'

        Rijndael.__init__( self, key, padding=padding, keySize=keySize, blockSize=16 )

        self.name       = 'AES'
Пример #4
0
    def __init__(self, key = None, padding = padWithPadLen(), keySize=16):
        """ Initialize AES, keySize is in bytes """
        if  not (keySize == 16 or keySize == 24 or keySize == 32) :
            raise BadKeySizeError, 'Illegal AES key size, must be 16, 24, or 32 bytes'

        Rijndael.__init__( self, key, padding=padding, keySize=keySize, blockSize=16 )

        self.name       = 'AES'
Пример #5
0
 def __init__(self, blockCipherInstance, padding = padWithPadLen()):
     """ CBC algorithms are created by initializing with a BlockCipher instance """
     self.baseCipher = blockCipherInstance
     self.name       = self.baseCipher.name + '_CBC'
     self.blockSize  = self.baseCipher.blockSize
     self.keySize    = self.baseCipher.keySize
     self.padding    = padding
     self.baseCipher.padding = noPadding()   # baseCipher should NOT pad!!
     self.r          = Random()            # for IV generation, currently uses
                                           # mediocre standard distro version     <----------------
     import time
     newSeed = time.ctime()+str(self.r)    # seed with instance location
     self.r.seed(newSeed)                  # to make unique
     self.reset()
Пример #6
0
 def __init__(self, blockCipherInstance, padding=padWithPadLen()):
     """ CBC algorithms are created by initializing with a BlockCipher instance """
     self.baseCipher = blockCipherInstance
     self.name = self.baseCipher.name + '_CBC'
     self.blockSize = self.baseCipher.blockSize
     self.keySize = self.baseCipher.keySize
     self.padding = padding
     self.baseCipher.padding = noPadding()  # baseCipher should NOT pad!!
     self.r = Random()  # for IV generation, currently uses
     # mediocre standard distro version     <----------------
     import time
     newSeed = time.ctime() + str(self.r)  # seed with instance location
     self.r.seed(newSeed)  # to make unique
     self.reset()
Пример #7
0
    def __init__(self, key = None, padding = padWithPadLen(), keySize=16, blockSize=16 ):
        self.name       = 'RIJNDAEL'
        self.keySize    = keySize
        self.strength   = keySize*8
        self.blockSize  = blockSize  # blockSize is in bytes
        self.padding    = padding    # change default to noPadding() to get normal ECB behavior

        assert( keySize%4==0 and NrTable[4].has_key(keySize/4)),'key size must be 16,20,24,29 or 32 bytes'
        assert( blockSize%4==0 and NrTable.has_key(blockSize/4)), 'block size must be 16,20,24,29 or 32 bytes'

        self.Nb = self.blockSize/4          # Nb is number of columns of 32 bit words
        self.Nk = keySize/4                 # Nk is the key length in 32-bit words
        self.Nr = NrTable[self.Nb][self.Nk] # The number of rounds (Nr) is a function of
                                            # the block (Nb) and key (Nk) sizes.
        if key != None:
            self.setKey(key)
Пример #8
0
    def __init__(self,key=None,padding=padWithPadLen(),keySize=16,blockSize=16,tapRound=6,extraRounds=6):
        """ key, keysize, blockSize same as Rijndael, tapROund is feedback tap, """
        self.tapRound    = tapRound     # <------- !!! change from Rijndael !!!
        self.extraRounds = extraRounds  # <------- !!! change from Rijndael !!!
        self.name        = 'ICEDOLL'
        self.keySize     = keySize
        self.strength    = keySize
        self.blockSize   = blockSize  # blockSize is in bytes
        self.padding     = padding    # change default to noPadding() to get normal ECB behavior

        assert( keySize%4==0 and NrTable[4].has_key(keySize/4)),'key size must be 16,20,24,29 or 32 bytes'
        assert( blockSize%4==0 and NrTable.has_key(blockSize/4)), 'block size must be 16,20,24,29 or 32 bytes'

        self.Nb = self.blockSize/4          # Nb is number of columns of 32 bit words
        self.Nk = keySize/4                 # Nk is the key length in 32-bit words
        self.Nr = NrTable[self.Nb][self.Nk]+extraRounds # <------- !!! change from Rijndael !!!

        if key != None:
            self.setKey(key)
Пример #9
0
    def __init__(self,
                 key=None,
                 padding=padWithPadLen(),
                 keySize=16,
                 blockSize=16):
        self.name = 'RIJNDAEL'
        self.keySize = keySize
        self.strength = keySize * 8
        self.blockSize = blockSize  # blockSize is in bytes
        self.padding = padding  # change default to noPadding() to get normal ECB behavior

        assert (keySize % 4 == 0 and NrTable[4].has_key(
            keySize / 4)), 'key size must be 16,20,24,29 or 32 bytes'
        assert (blockSize % 4 == 0 and NrTable.has_key(
            blockSize / 4)), 'block size must be 16,20,24,29 or 32 bytes'

        self.Nb = self.blockSize / 4  # Nb is number of columns of 32 bit words
        self.Nk = keySize / 4  # Nk is the key length in 32-bit words
        self.Nr = NrTable[self.Nb][
            self.Nk]  # The number of rounds (Nr) is a function of
        # the block (Nb) and key (Nk) sizes.
        if key != None:
            self.setKey(key)
Пример #10
0
def AES(key):
    from crypto.cipher.base import padWithPadLen
    from crypto.cipher.rijndael import Rijndael
    return Rijndael(key, keySize=32, blockSize=16, padding=padWithPadLen())
Пример #11
0
def AES(key):
    return Rijndael(key, keySize=32, blockSize=16, padding=padWithPadLen())
Пример #12
0
 def __init__(self, key=None, padding=padWithPadLen(), keySize=16):
     CBC.__init__(self, AES(key, noPadding(), keySize), padding)
     self.name = 'AES_CBC'
 def TX(self):
     try:
         if self.rand:
             self.random_data_generator()
         else:
             self.bin_file_data_extractor()
         
         SAMPLES=int(self.samples_textctrl.GetValue())
         self.progress_bar.SetRange(SAMPLES)
         
         #Check Sample Rate mode and calculate Sleep in seconds
         sample_rate=self.sample_rate_textctrl.GetValue()
         if sample_rate in ['0','']:
             sample_rate=1
             self.sample_rate_textctrl.SetValue('1')
         else:
             sample_rate=float(sample_rate)
         choice=self.sample_rate_rb.GetStringSelection()
         if choice=='[Hz]':
             SLEEP=1.0/float(sample_rate)
         else:
             SLEEP=float(sample_rate)
         
         choice=self.addressing_type_rb.GetStringSelection()
         if choice.startswith('XBee'):
             #PAN Change
             #self.ser.Open()
             PAN = self.pan_id_textctrl.GetValue()
             frame_data="0801"+hexlify("ID")+PAN
             length = self.length_calc(frame_data)
             check_sum = self.check_sum_calc(frame_data)
             command="7E" + length + frame_data + check_sum
             self.ser.write(unhexlify(command))
             #response=hexlify(self.ser.readall())
             
             #Encryption Disable (if not set)
             if self.encryption_enabled:
                 AT_cmd=hexlify('KY')
                 AT_par=self.encryption_key
                 if len(AT_par) % 2:
                     AT_par='0'+AT_par
                 frame_data="0801"+AT_cmd+AT_par #AT command send
                 length = self.length_calc(frame_data)   
                 check_sum = self.check_sum_calc(frame_data)
                 command="7E" + length + frame_data + check_sum
                 self.ser.write(unhexlify(command))
                 #response=(hexlify(self.ser.readall()))
                 
                 AT_cmd=hexlify('EE')
                 AT_par='01'
                 frame_data="0801"+AT_cmd+AT_par #AT command send
                 length = self.length_calc(frame_data)   
                 check_sum = self.check_sum_calc(frame_data)
                 command="7E" + length + frame_data + check_sum
                 self.ser.write(unhexlify(command))
                 #response=(hexlify(self.ser.readall()))
             else:
                 AT_cmd=hexlify('EE')
                 AT_par='00'
                 frame_data="0801"+AT_cmd+AT_par #AT command send
                 length = self.length_calc(frame_data)   
                 check_sum = self.check_sum_calc(frame_data)
                 command="7E" + length + frame_data + check_sum
                 self.ser.write(unhexlify(command))
                 #response=(hexlify(self.ser.readall()))
         else:
             if self.encryption_enabled: #UDP AES Encryption calrulation here!
                 key = '%016x' % int(self.encryption_key, 16)
                 kSize = len(key)
                 
                 alg = AES(key, keySize=kSize, padding=padWithPadLen())
                 
                 for i in range(0, SAMPLES):
                     self.data_arr[i]=alg.encrypt(self.data_arr[i])
                 
         '''
         XBee S2     - we will use Radios Series2 (ZigBee) Module with explicit addressing (64bit & 16bit) data Transmission
         XBee short  - we will use Radios Series1 (XBee) Module with short addressing (16bit) data Transmission
         XBee long   - we will use Radios Series1 (XBee) Module with long addressing (64bit) data Transmission
         UDP         - we will use standard UDP data Transmission (no Digi Modules involved here)
         '''
         if choice=="XBee S2":
             #self.ser.Open()
             self.S2_TX(SLEEP, SAMPLES)
             #self.ser.Close()
         elif choice=="XBee short":
             #self.ser.Open()
             self.S1_TX_short(SLEEP, SAMPLES)
             #self.ser.Close()
         elif choice=="XBee long":
             #self.ser.Open()
             self.S1_TX_long(SLEEP, SAMPLES)
             #self.ser.Close()
         elif choice=="UDP":
             #self.udp.Open()
             self.UDP_TX(SLEEP, SAMPLES)
             #self.udp.Close()
             
     except KeyboardInterrupt:
         return
from crypto.cipher.aes  import AES
from crypto.cipher.base import padWithPadLen
from binascii           import a2b_hex, b2a_hex, hexlify, unhexlify

key = '%016x' % 0x1234
kSize = len(key)

data = 'Data goes here! Is that a surprize?'

alg = AES(key, keySize=kSize, padding=padWithPadLen())

enc_data=alg.encrypt(data)
dec_data=alg.decrypt(enc_data)
Пример #15
0
 def __init__(self, key=None, padding=padWithPadLen(), keySize=16):
     CBC.__init__( self, AES(key, noPadding(), keySize), padding)
     self.name       = 'AES_CBC'
Пример #16
0
def AES(key):
    return Rijndael(key, keySize=32, blockSize=16, padding=padWithPadLen())
Пример #17
0
def AES(key):
    from crypto.cipher.base import padWithPadLen
    from crypto.cipher.rijndael import Rijndael
    return Rijndael(key, keySize=32, blockSize=16, padding=padWithPadLen())