Esempio n. 1
0
 def encode_enum(self, obj):
     # empty enum
     if len(obj._cont) == 0:
         obj._msg = BER_TLV(self.get_name())
         return
     # get the maximum integer value to know the length on which to encode
     if isinstance(self._ENC_ENUM_LEN, (int, long)) \
     and self._ENC_ENUM_LEN % 8 == 0:
         # encoder forcing ENUMERATED length
         bit_len = abs(self._ENC_ENUM_LEN)
     else:
         # minimum byte length for ENUMERATED
         bit_len = 1 + len_bits(max(obj._cont.values()))
         miss_len = bit_len % 8
         if miss_len != 0:
             bit_len += 8 - miss_len
     # and get the identifier integer value from the string value
     V = Int('V', Pt=obj._cont[obj._val], Type='int%s'%bit_len,
             Repr=self._REPR_ENUM)
     if self._ENUM_BUILD_DICT:
         V.Dict = dict([(i[1], i[0]) for i in obj._cont.items()])
     #
     obj._msg = BER_TLV(obj.get_name())
     obj._msg.set(V)
     self.handle_tag_enc(obj)
Esempio n. 2
0
 def decode_int_val(self, obj):
     tlv = self.handle_tag_dec(obj)
     # map to an Int, given the length of the value part
     bit_len = tlv[1]()*8
     V = Int('V', Type='int%s'%bit_len, Repr=self._REPR_INT)
     V.map(str(tlv[2]))
     tlv.set(V)
     #
     obj._val = V()
Esempio n. 3
0
 def set_private_manual(self, priv):
     if len(priv) < self.SIZE//8:
         priv = b'\0'*(self.SIZE//8 - len(priv)) + priv
     elif len(priv) > self.SIZE//8:
         priv = priv[-self.SIZE//8:]
     self.priv = Int('priv', Type=self.TYPE)
     self.priv.map(priv)
     if self.priv() >= self.MOD():
         self.priv < self.priv() % self.MOD()
     #
     if _with_gmpy:
         pub = int(pow(gmpy.mpz(self.GEN()),
                       gmpy.mpz(self.priv()),
                       gmpy.mpz(self.MOD())))
     else:
         pub = pow(self.GEN(), self.priv(), self.MOD())
     self.pub = Int('pub', Pt=pub, Type=self.TYPE)
Esempio n. 4
0
 def __init__(self, with_options=False, with_security=False, **kwargs):
     Layer3NASEMM.__init__(self, with_security)
     self.extend([
         Int('EMMCause', Pt=26, Type='uint8', Dict=EMMCause_dict),
         Type4_TLV('AUTS', T=0x30, V=14 * '\0')
     ])
     self.AUTS.V.Repr = 'hex'
     self._post_init(with_options, **kwargs)
Esempio n. 5
0
class TransTLV(Layer):
    constructorList = [
        Int(CallName='T', ReprName='Type', Type='uint16', Dict=TransAttType),
        Int(CallName='L', ReprName='Length', Type='uint16'),
        Str(CallName='V', ReprName='Value'),
    ]

    def __init__(self, T=0x4000, V=None):
        Layer.__init__(self,
                       CallName='TransTLV',
                       ReprName='Transform Attribute TLV')
        self.T.Pt = T
        self.L.Pt = self.V
        self.L.PtFunc = lambda V: len(V)
        self.V.Pt = V
        self.V.Len = self.L
        self.V.LenFunc = lambda L: int(L)
Esempio n. 6
0
 def __init__(self, with_options=True, **kwargs):
     Layer3.__init__(self)
     self.extend([ \
         Int('GMMCause', Pt=1, Type='uint8', Dict=GMMCause_dict),
         Type4_TLV('AUTS', T=0x30, V=14*'\0'),
         ])
     self.AUTS.V.Repr = 'hex'
     self._post_init(with_options, **kwargs)
Esempio n. 7
0
 def __init__(self, with_options=False, with_security=False, **kwargs):
     Layer3NASEMM.__init__(self, with_security)
     self.extend([
         Int('EMMCause', Pt=0, Type='uint8', Dict=EMMCause_dict),
         Type3_TV('T3442', T=0x5B, V='\0', Len=1),
         Type4_TLV('T3346', T=0x5F, V='\0') # timer with Length attribute but fixed length (1 byte)...
         ])
     self._post_init(with_options, **kwargs)
Esempio n. 8
0
class EMMHeader(Layer):
    constructorList = [
        Bit('SH', ReprName='Security Header Type', Pt=0, BitLen=4, 
            Dict=SecHdr_dict, Repr='hum'),
        Bit('PD', ReprName='Protocol Discriminator', Pt=7, BitLen=4,
            Dict=PD_dict, Repr='hum'),
        Int('Type', Type='uint8', Dict=EMM_dict, Repr='hum'),
        ]
Esempio n. 9
0
class Cfg(Layer):
    constructorList = [
        Int(CallName='T', ReprName='Type', Type='uint16', Dict=CfgAttType),
        Int(CallName='L', ReprName='Length', Type='uint16'),
        Str(CallName='V', ReprName='Value'),
    ]

    def __init__(self, T=1, V=None):
        Layer.__init__(self,
                       CallName='Cfg',
                       ReprName='Configuration Attribute')
        self.T.Pt = T
        self.V.Pt = V
        self.V.Len = self.L
        self.V.LenFunc = lambda L: int(L)
        self.L.Pt = self.V
        self.L.PtFunc = lambda V: len(V)
Esempio n. 10
0
class pay_CP(Layer):
    constructorList = [
        Int(CallName='Ptype', ReprName="Payload Type", Pt=47, Type='uint8', \
            Dict=PayloadType, Trans=True),
        Int(CallName='np', ReprName='Next Payload', Pt=0, Type='uint8', \
            Dict=PayloadType),
        Str(CallName='res', ReprName='Reserved', Pt='\x00', Len=1, Repr="hex"),
        Int(CallName='len', ReprName='Length', Type='uint16'),
        Int(CallName='cft', ReprName='Configuration Type', Type='uint8', Dict=CfgType),
        Str(CallName='res2', ReprName='Reserved2', Pt='\x00\x00\x00', Len=3, Repr="hex"),
        ]

    def __init__(self, cft=1):
        Layer.__init__(self, CallName='CP', ReprName='Configuration')
        self.cft.Pt = cft
        self.len.Pt = self.get_payload
        self.len.PtFunc = lambda pay: len(pay()) + 8
Esempio n. 11
0
 def __init__(self, with_options=False, with_security=False, **kwargs):
     Layer3NAS.__init__(self, with_security)
     self.extend([
         Int('ESMCause', Pt=0, Type='uint8', Dict=ESMCause_dict),
         Type4_TLV('ProtConfig', ReprName='Protocol Config Options',
                   T=0x27, V='\x80')
         ])
     self._post_init(with_options, **kwargs)
Esempio n. 12
0
class INIT_ACK(Layer):
    constructorList = [
        Int(CallName='type', ReprName='Chunk Type', Pt=2, Type='uint8', \
            Dict=chunk_dict),
        Bit(CallName='res', ReprName='Reserved', Pt=0, BitLen=5),
        Bit(CallName='U', ReprName='Unordered', Pt=0, BitLen=1),
        Bit(CallName='B', ReprName='Beginning', Pt=0, BitLen=1),
        Bit(CallName='E', ReprName='Ending', Pt=0, BitLen=1),
        Int(CallName='len', ReprName='Chunk Length', Type='uint16'),
        Int(CallName='init', ReprName='Initiate Tag', Type='uint32'),
        Int(CallName='a_rwnd', ReprName='Advertised Receiver Wundow Credit', \
            Type='uint32'),
        Int(CallName='outs', ReprName='Number of Outbound Streams', Type='uint16'),
        Int(CallName='ins', ReprName='Number of Inbound Streams', Type='uint16'),
        Int(CallName='itsn', ReprName='Initial Transmission Sequence Number', \
            Type='uint32'),
        ]

    def __init__(self, init=0x6D696368, a_rwnd=0x00100000, \
                 outs=10, ins=10, itsn=0x6D696368):
        Layer.__init__(self,
                       CallName='init-ack',
                       ReprName='SCTP INIT ACK chunk')
        self.len.Pt = self.get_payload
        self.len.PtFunc = lambda pay: len(pay()) + 16
        self.init.Pt = init
        self.a_rwnd.Pt = a_rwnd
        self.outs.Pt = outs
        self.ins.Pt = ins
        self.itsn.Pt = itsn
Esempio n. 13
0
class SOF(segment):
    constructorList = [
        Int('mark', Pt=0xFF, Type='uint8', Repr='hex'),
        Int('type', Pt=0xC0, Type='uint8', Repr='hum', Dict=Seg_dict),
        Int('len', Pt=0, Type='uint16'),
        Int('P', ReprName='Sample Precision', Pt=0, Type='uint8'),
        Int('Y', ReprName='Number of lines', Pt=0, Type='uint16'),
        Int('X', ReprName='Number of sample per line', Pt=0, Type='uint16'),
        Int('Nf', ReprName='Number of image components in frame', Pt=0, \
            Type='uint8'),
        Str('components', Pt=''),
        ]

    def __init__(self, **kwargs):
        Layer.__init__(self, **kwargs)
        self.components.Len = self.len
        self.components.LenFunc = lambda l: l() - 8
        self.len.Pt = self.components
        self.len.PtFunc = lambda p: len(p) + 8

    def map(self, s=''):
        Layer.map(self, s)
        if len(self.components) / 3.0 == self.Nf():
            cpn_s = self.components()
            self.remove(self.components)
            while len(cpn_s) > 0:
                self.append(SOFComponent())
                self[-1].map(cpn_s)
                cpn_s = cpn_s[3:]
Esempio n. 14
0
 def __init__(self, with_options=False, with_security=False, **kwargs):
     Layer3NASEMM.__init__(self, with_security)
     self.extend([
         Int('ContType', ReprName='Generic Container Type', Pt=1,
             Type='uint8', Dict=GeneContType_dict),
         Type6_LVE('GenericContainer', V='\0'),
         Type4_TLV('AddInfo', T=0x65, V='\0')
         ])
     self._post_init(with_options, **kwargs)
Esempio n. 15
0
 def __init__(self, with_options=False, with_security=False, **kwargs):
     Layer3NASEMM.__init__(self, with_security)
     self.extend([
         Int('EMMCause', Pt=0, Type='uint8', Dict=EMMCause_dict),
         Type6_TLVE('ESMContainer', T=0x78, V=3*'\0'),
         Type4_TLV('T3346', T=0x5F, V='\0'), # timer with Length attribute but fixed length (1 byte)...
         Type4_TLV('T3402', T=0x16, V='\0') # and again...
         ])
     self._post_init(with_options, **kwargs)
Esempio n. 16
0
class pay_N(Layer):
    constructorList = [
        Int(CallName='Ptype', ReprName="Payload Type", Pt=40, Type='uint8', \
            Dict=PayloadType, Trans=True),
        Int(CallName='np', ReprName='Next Payload', Pt=0, Type='uint8', \
            Dict=PayloadType),
        Str(CallName='res', ReprName='Reserved', Pt='\x00', Len=1, Repr="hex"),
        Int(CallName='len', ReprName='Length', Type='uint16'),
        Str(CallName='n', ReprName='Nonce', Repr="hex"),
        ]

    def __init__(self, n=None):
        Layer.__init__(self, CallName='N', ReprName='Nonce')
        self.n.Pt = n
        self.n.Len = self.len
        self.n.LenFunc = lambda len: int(len) - 4
        self.len.Pt = self.n
        self.len.PtFunc = lambda n: len(n) + 4
Esempio n. 17
0
class AT_RES(TLV):
    constructorList = [
        Int(CallName="T", ReprName="Type", Pt=3, Type="uint8", Dict=SIMAKAAttribute),
        Int(CallName="L", ReprName="Length", Type="uint8"),
        Int(CallName="resLen", ReprName="RES Bits Length", Type="uint16"),
        Str(CallName="res", ReprName="RES", Repr="hex"),
        ]
    
    def __init__(self, res=''):
        Layer.__init__(self, CallName='RES', ReprName='AT_RES')
        self.res.Pt = res
        self.res.PtFunc = lambda res: self.pad4( str(res), const=4 )
        self.res.Len = self.resLen
        self.res.LenFunc = lambda l: int(l)/8
        self.resLen.Pt = self.res
        self.resLen.PtFunc = lambda res: len(res.Pt)*8
        self.L.Pt = self.res
        self.L.PtFunc = lambda res: self.len4( len(res) + 4 )
Esempio n. 18
0
class PAD(Layer):
    constructorList = [
        Int(CallName='type', ReprName='Chunk Type', Pt=0x84, \
            Type='uint8', Dict=chunk_dict),
        Bit(CallName='res', ReprName='Reserved', Pt=0, BitLen=5),
        Bit(CallName='U', ReprName='Unordered', Pt=0, BitLen=1),
        Bit(CallName='B', ReprName='Beginning', Pt=0, BitLen=1),
        Bit(CallName='E', ReprName='Ending', Pt=0, BitLen=1),
        Int(CallName='len', ReprName='Chunk Length', Type='uint16'),
        Str(CallName='pad', ReprName='Padding Data', Repr='hex'),
        ]
    padding_bytes = '\0'

    def __init__(self, padlen=0):
        Layer.__init__(self, CallName='pad', ReprName='SCTP PAD chunk')
        self.pad.Pt = padlen * self.padding_bytes
        self.len.Pt = self.pad
        self.len.PtFunc = lambda pad: len(pad) + 4
Esempio n. 19
0
class UMA_IE_old(Layer):
    constructorList = [
        Int(CallName='T', ReprName='Tag', Type='uint8', \
            Dict=IEType),
        Int(CallName='L', ReprName='Length', Type='uint8'),
        Str(CallName='V', ReprName='Value'),
        ]

    def __init__(self, T=1, V=''):
        Layer.__init__(self, CallName='IE_old')
        # Tag
        self.T.Pt = T
        # Length / Value business
        self.V.Pt = V
        self.V.Len = self.L
        self.V.LenFunc = lambda L: int(L)
        self.L.Pt = self.V
        self.L.PtFunc = lambda V: len(V)
Esempio n. 20
0
class ABORT(Layer):
    constructorList = [
        Int(CallName='type', ReprName='Chunk Type', Pt=6, Type='uint8', \
            Dict=chunk_dict),
        Bit(CallName='res', ReprName='Reserved', Pt=0, BitLen=5),
        Bit(CallName='U', ReprName='Unordered', Pt=0, BitLen=1),
        Bit(CallName='B', ReprName='Beginning', Pt=0, BitLen=1),
        Bit(CallName='E', ReprName='Ending', Pt=0, BitLen=1),
        Bit(CallName='res', ReprName='Reserved', Pt=0, BitLen=7),
        Bit(CallName='T', ReprName='Transmit', BitLen=1),
        Int(CallName='len', ReprName='Chunk Length', Type='uint16'),
        ]

    def __init__(self, T=0):
        Layer.__init__(self, CallName='abort', ReprName='SCTP ABORT chunk')
        self.T.Pt = T
        self.len.Pt = self.get_payload
        self.len.PtFunc = lambda pay: len(pay()) + 4
Esempio n. 21
0
class SMS_SUBMIT_REPORT_RP_ACK(Layer):
    constructorList = [
        Bit('spare', Pt=0, BitLen=1),
        Bit('TP_UDHI', ReprName='TP User Data Header Indicator', Pt=0, BitLen=1),
        Bit('spare', Pt=0, BitLen=4),
        Bit('TP_MTI', ReprName='TP Message Type Indicator', Pt=0, BitLen=2, \
            Repr='hum', Dict=TP_MTI_SCtoMS_dict),
        TP_PI(),
        TP_SCTS(),
        TP_PID(),
        TP_DCS(),
        Int('TP_UDL', ReprName='User Data Length (in character)', Pt=0, \
            Type='uint8'),
        Str7b('TP_UD', Pt=''),
        ]
    
    def __init__(self, with_options=False, **kwargs):
        Layer.__init__(self, **kwargs)
        self.TP_UDL.Pt = self.TP_UD
        self.TP_UDL.PtFunc = self._get_udl
        self.TP_UD.Len = self.TP_UDL
        self.TP_UD.LenFunc = self._get_ud_len
        if not with_options:
            self.TP_PID.Trans = True
            self.TP_DCS.Trans = True
            self.TP_UDL.Trans = True
            self.TP_UD.Trans = True
    
    def _get_udl(self, _unused):
        if hasattr(self.TP_DCS, 'Charset') \
        and not self.TP_DCS.Charset.is_transparent():
            if self.TP_DCS.Charset() == 1:
                return len(self.TP_UD)
            elif self.TP_DCS.Charset() == 2:
                return len(self.TP_UD)//2
        return len(self.TP_UD.decode())
    
    def _get_ud_len(self, _unused):
        if hasattr(self.TP_DCS, 'Charset') \
        and not self.TP_DCS.Charset.is_transparent():
            if self.TP_DCS.Charset() == 1:
                return self.TP_UDL()
            elif self.TP_DCS.Charset() == 2:
                return self.TP_UDL()*2
        return int(ceil(self.TP_UDL()*7.0/8))
    
    def map(self, s=''):
        s_len = len(s)
        if s_len < 13:
            self.TP_UD.Trans = True
        if s_len < 12:
            self.TP_UDL.Trans = True
        if s_len < 11:
            self.TP_DCS.Trans = True
        if s_len < 10:
            self.TP_PID.Trans = True
        Layer.map(self, s)
Esempio n. 22
0
class RP_SMMA(Layer):
    '''
    MS -> Net
    '''
    constructorList = [
        Bit('spare', Pt=0, BitLen=5, Repr='hex'),
        Bit('Type', Pt=6, BitLen=3, Repr='hum', Dict=RPType_dict),
        Int('Ref', Type='uint8'),
        ]
Esempio n. 23
0
class AT_NEXT_REAUTH_ID(TLV):
    constructorList = [
        Int(CallName="T", ReprName="Type", Pt=133, Type="uint8", Dict=SIMAKAAttribute),
        Int(CallName="L", ReprName="Length", Type="uint8"),
        Int(CallName="reautLen", ReprName="Actual Re-auth Identity Length", Type="uint16"),
        Str(CallName="reaut", ReprName="Next Fast Re-authentication Username"),
        ]
    
    def __init__(self, reaut=''):
        Layer.__init__(self, CallName='reaut_ID', ReprName='AT_NEXT_REAUTH_ID')
        self.reaut.Pt = reaut
        self.reaut.PtFunc = lambda reau: self.pad4( str(reaut), const=4 )
        self.reaut.Len = self.reautLen
        self.reaut.LenFunc = lambda l: int(l)
        self.reautLen.Pt = self.reaut
        self.reautLen.PtFunc = lambda reaut: len(reaut.Pt)
        self.L.Pt = self.reaut
        self.L.PtFunc = lambda reaut: self.len4( len(reaut) + 4 )
Esempio n. 24
0
class AT_VERSION_LIST(TLV):
    constructorList = [
        Int(CallName="T", ReprName="Type", Pt=15, Type="uint8", Dict=SIMAKAAttribute),
        Int(CallName="L", ReprName="Length", Type="uint8"),
        Int(CallName="listLen", ReprName="Length of Version List", Type="uint16"),
        Str(CallName="list", ReprName="List of Supported Version", Repr="hex"),
        ]
    
    def __init__(self, list="\x00\x01"):
        Layer.__init__(self, CallName='VER_LIST', ReprName='AT_VERSION_LIST')
        self.list.Pt = list
        self.list.PtFunc = lambda list: self.pad4( str(list), const=4 )
        self.list.Len = self.listLen
        self.list.LenFunc = lambda l: int(l)
        self.listLen.Pt = self.list
        self.listLen.PtFunc = lambda list: len(list.Pt)
        self.L.Pt = self.list
        self.L.PtFunc = lambda list: self.len4( len(list) + 4 )
Esempio n. 25
0
class AT_NEXT_PSEUDONYM(TLV):
    constructorList = [
        Int(CallName="T", ReprName="Type", Pt=132, Type="uint8", Dict=SIMAKAAttribute),
        Int(CallName="L", ReprName="Length", Type="uint8"),
        Int(CallName="pseuLen", ReprName="Actual Pseudonym Length", Type="uint16"),
        Str(CallName="pseu", ReprName="Pseudonym"),
        ]
    
    def __init__(self, pseu=''):
        Layer.__init__(self, CallName='Pseudo', ReprName='AT_NEXT_PSEUDONYM')
        self.pseu.Pt = pseu
        self.pseu.PtFunc = lambda pseu: self.pad4( str(pseu), const=4 )
        self.pseu.Len = self.pseuLen
        self.pseu.LenFunc = lambda l: int(l)
        self.pseuLen.Pt = self.pseu
        self.pseuLen.PtFunc = lambda pseu: len(pseu.Pt)
        self.L.Pt = self.pseu
        self.L.PtFunc = lambda pseu: self.len4( len(pseu) + 4 )
Esempio n. 26
0
class pay_V(Layer):
    constructorList = [
        Int(CallName='Ptype', ReprName="Payload Type", Pt=43, Type='uint8', \
            Dict=PayloadType, Trans=True),
        Int(CallName='np', ReprName='Next Payload', Pt=0, Type='uint8', \
            Dict=PayloadType),
        Str(CallName='res', ReprName='Reserved', Pt='\x00', Len=1, Repr="hex"),
        Int(CallName='len', ReprName='Length', Type='uint16'),
        Str(CallName='vid', ReprName='Vendor ID'),
        ]

    def __init__(self, vid=None):
        Layer.__init__(self, CallName='V', ReprName='Vendor ID')
        self.vid.Pt = vid
        self.vid.Len = self.len
        self.vid.LenFunc = lambda len: int(len) - 4
        self.len.Pt = self.vid
        self.len.PtFunc = lambda vid: len(vid) + 4
Esempio n. 27
0
class AT_IDENTITY(TLV):
    constructorList = [
        Int(CallName="T", ReprName="Type", Pt=14, Type="uint8", Dict=SIMAKAAttribute),
        Int(CallName="L", ReprName="Length", Type="uint8"),
        Int(CallName="idLen", ReprName="Actual Identity Length", Type="uint16"),
        Str(CallName="id", ReprName="Identity"),
        ]
    
    def __init__(self, id=''):
        Layer.__init__(self, CallName='ID', ReprName='AT_IDENTITY')
        self.id.Pt = id
        self.id.PtFunc = lambda id: self.pad4( str(id), const=4 )
        self.id.Len = self.idLen
        self.id.LenFunc = lambda l: int(l)
        self.idLen.Pt = self.id
        self.idLen.PtFunc = lambda id: len(id.Pt)
        self.L.Pt = self.id
        self.L.PtFunc = lambda id: self.len4( len(id) + 4 )
Esempio n. 28
0
class Elf64_Phdr(Layer):
    constructorList = [
        Int('p_type', Pt=0, Type='uint64', Dict=p_type_dict),
        Int('p_offset', Pt=0, Type='uint64'),
        Int('p_vaddr', Pt=0, Type='uint64'),
        Int('p_paddr', Pt=0, Type='uint64'),
        Int('p_filesz', Pt=0, Type='uint64'),
        Int('p_memsz', Pt=0, Type='uint64'),
        Int('p_flags', Pt=0, Type='uint64', Dict=p_flags_dict),
        Int('p_align', Pt=0, Type='uint64'),
    ]
Esempio n. 29
0
class Adaptation(Layer):
    constructorList = [
        Int('len', Pt=0, Type='uint8', Repr='hum'),
        Str('adapt_data', ReprName='Raw adaption data', Pt='', Repr='hex')
    ]

    def __init__(self):
        Layer.__init__(self)
        self.adapt_data.Len = self.len
        self.adapt_data.LenFunc = lambda len: int(len) - 1
Esempio n. 30
0
class FORWARD_TSN(Layer):
    constructorList = [
        Int(CallName='type', ReprName='Chunk Type', Pt=0xC0, Type='uint8', \
            Dict=chunk_dict),
        Bit(CallName='res', ReprName='Reserved', Pt=0, BitLen=5),
        Bit(CallName='U', ReprName='Unordered', Pt=0, BitLen=1),
        Bit(CallName='B', ReprName='Beginning', Pt=0, BitLen=1),
        Bit(CallName='E', ReprName='Ending', Pt=0, BitLen=1),
        Int(CallName='len', ReprName='Chunk Length', Type='uint16'),
        Int(CallName='ctsn', ReprName='New Cumulative TSN', Type='uint32'),
        ]

    def __init__(self, ctsn=0):
        Layer.__init__(self,
                       CallName='fwdtsn',
                       ReprName='SCTP FORWARD_TSN chunk')
        self.ctsn.Pt = ctsn
        self.len.Pt = self.get_payload
        self.len.PtFunc = lambda pay: len(pay()) + 8
Esempio n. 31
0
class Header(Layer):
    constructorList = [
        Bit('TI', ReprName='Transaction Identifier Flag', Pt=0, BitLen=1, \
            Dict=TI_dict, Repr='hum'),
        Bit('TIO', ReprName='Transaction Identifier', Pt=0, BitLen=3, \
            Repr='hum'),
        Bit('PD', ReprName='Protocol Discriminator', Pt=9, BitLen=4, \
            Dict=PD_dict, Repr='hum'),
        Int('Type', Type='uint8', Dict=SMSCP_dict),
        ]
Esempio n. 32
0
 def set_private(self):
     self.priv = Int('priv', Pt=_rand.randint(3, self.MOD()-1),
                     Type=self.TYPE)
     if _with_gmpy:
         pub = int(pow(gmpy.mpz(self.GEN()),
                       gmpy.mpz(self.priv()),
                       gmpy.mpz(self.MOD())))
     else:
         pub = pow(self.GEN(), self.priv(), self.MOD())
     self.pub = Int('pub', Pt=pub, Type=self.TYPE)
Esempio n. 33
0
 def __init__(self, with_options=True, **kwargs):
     Layer3.__init__(self)
     self.extend([ \
         Int('GMMCause', Pt=1, Type='uint8', Dict=GMMCause_dict),
         Bit('spare', Pt=0, BitLen=4, Repr='hex'),
         Bit('ForceStdby', ReprName='Force to standby', Pt=0, BitLen=4,
             Repr='hum', Dict=ForceStdby_dict),
         Type4_TLV('T3302', T=0x2A, V='\0'),
         ])
     self._post_init(with_options, **kwargs)
Esempio n. 34
0
 def map(self, s=""):
     Layer.map(self, s)
     # map extensions
     if len(s) >= len(self) + 2:
         s = s[len(self) :]
         # add extensions length
         extl = Int("ext_length", Type="uint16")
         extl.map(s)
         self.append(extl)
         s = s[2:]
         l = extl()
         if len(s) > l:
             # truncate the buffer
             s = s[:l]
         elif len(s) < l:
             # error
             return
         # add extensions
         while s:
             self.append(Extension())
             self[-1].map(s)
             s = s[len(self[-1]) :]
Esempio n. 35
0
 def decode_enum_val(self, obj):
     tlv = self.handle_tag_dec(obj)
     # map to an Int, given the length of the value part
     bit_len = tlv[1]()*8
     if bit_len:
         V = Int('V', Type='int%s'%bit_len, Repr=self._REPR_ENUM)
         V.map(str(tlv[2]))
         if self._ENUM_BUILD_TYPE:
             V.Dict = dict([(i[1], i[0]) for i in obj._cont.items()])
         tlv.set(V)
         #
         val_int = V()
         enum_int = obj._cont.values()
         if val_int in enum_int:
             obj._val = obj._cont.keys()[enum_int.index(val_int)]
         elif obj._ext is not None:
             # if extended index value is unknown,
             # the decoded value cannot be retrieved
             # WNG: we put a dummy string here as value
             obj._val = '_ext_%i' % val_int
         elif self._SAFE:
             raise(ASN1_BER_DECODER('%s: invalid ENUMERATED identifier %s' \
                   % (obj.get_fullname(), val_int)))
Esempio n. 36
0
 def compute_shared_key(self, pub_peer):
     if len(pub_peer) < self.SIZE//8:
         pub_peer = b'\0'*(self.SIZE//8 - len(pub_peer)) + pub_peer
     elif len(pub_peer) > self.SIZE//8:
         pub_peer = pub_peer[-self.SIZE//8:]
     self.pub_peer = Int('pub_peer', Type=self.TYPE)
     self.pub_peer.map(pub_peer)
     #
     if _with_gmpy:
         shk = int(pow(gmpy.mpz(self.pub_peer()),
                       gmpy.mpz(self.priv()),
                       gmpy.mpz(self.MOD())))
     else:
         shk = pow(self.pub_peer(), self.priv(), self.MOD())
     self.shkey = Int('shkey', Pt=shk, Type=self.TYPE)
Esempio n. 37
0
 def __init__(self, mod=None, gen=None, size=None):
     if size and 0 < size <= 2**20:
         self.SIZE = size
     self.TYPE = 'uint{0}'.format(self.SIZE)
     self.MOD = Int('MOD', Pt=self.MOD, Type=self.TYPE)
     self.GEN = Int('GEN', Pt=self.GEN, Type=self.TYPE)
     #
     if mod:
         if len(mod) < self.SIZE//8:
             mod = b'\0'*(self.SIZE//8 - len(mod)) + mod
         elif len(mod) > self.SIZE//8:
             mod = mod[-self.SIZE//8:]
         self.MOD.map(mod)
     if gen:
         if len(gen) < self.SIZE//8:
             gen = b'\0'*(self.SIZE//8 - len(gen)) + gen
         elif len(gen) > self.SIZE//8:
             gen = gen[-self.SIZE//8:]
         self.GEN.map(gen)
     #
     self.priv = None
     self.pub = None
     self.pub_peer = None
     self.shkey = None
Esempio n. 38
0
class DH_Bytes(object):
    '''
    Diffie-Hellman computation.
    Uses Python bytes of size SIZE as input / output.
    
    Instance attributes:
    SIZE: size of Diffie-Hellman exchanged values
    TYPE: libmich Int.Type to be used, according to SIZE
    MOD: modulus
    GEN: generator
    priv: user's private value
    pub: user's public value, corresponding to the local priv one
    pub_peer: peer's public value, corresponding to the received one
    shkey: shared key computed from the pub_peer and priv ones
    
    1) Initialize DH modulus and generator
    dh = DH(mod, gen)
    
    2) Generate or set the user's private value, and compute the user's public
       value
    dh.set_private() # automatic generation from randint(3, MOD-1)
    dh.set_private_manual(priv) # manual setting with bytes
    
    3) Get the user's public value to be sent to peer
    pub = dh.get_public()
    
    4) Compute shared secret from the received peer's public value
    dh.compute_shared_key(pub_peer)
    
    5) Get the computed shared secret
    key = dh.get_shared_key()
    '''
    
    SIZE = 2048
    MOD = 3
    GEN = 2
    
    def __init__(self, mod=None, gen=None, size=None):
        if size and 0 < size <= 2**20:
            self.SIZE = size
        self.TYPE = 'uint{0}'.format(self.SIZE)
        self.MOD = Int('MOD', Pt=self.MOD, Type=self.TYPE)
        self.GEN = Int('GEN', Pt=self.GEN, Type=self.TYPE)
        #
        if mod:
            if len(mod) < self.SIZE//8:
                mod = b'\0'*(self.SIZE//8 - len(mod)) + mod
            elif len(mod) > self.SIZE//8:
                mod = mod[-self.SIZE//8:]
            self.MOD.map(mod)
        if gen:
            if len(gen) < self.SIZE//8:
                gen = b'\0'*(self.SIZE//8 - len(gen)) + gen
            elif len(gen) > self.SIZE//8:
                gen = gen[-self.SIZE//8:]
            self.GEN.map(gen)
        #
        self.priv = None
        self.pub = None
        self.pub_peer = None
        self.shkey = None
    
    def set_private(self):
        self.priv = Int('priv', Pt=_rand.randint(3, self.MOD()-1),
                        Type=self.TYPE)
        if _with_gmpy:
            pub = int(pow(gmpy.mpz(self.GEN()),
                          gmpy.mpz(self.priv()),
                          gmpy.mpz(self.MOD())))
        else:
            pub = pow(self.GEN(), self.priv(), self.MOD())
        self.pub = Int('pub', Pt=pub, Type=self.TYPE)
    
    def set_private_manual(self, priv):
        if len(priv) < self.SIZE//8:
            priv = b'\0'*(self.SIZE//8 - len(priv)) + priv
        elif len(priv) > self.SIZE//8:
            priv = priv[-self.SIZE//8:]
        self.priv = Int('priv', Type=self.TYPE)
        self.priv.map(priv)
        if self.priv() >= self.MOD():
            self.priv < self.priv() % self.MOD()
        #
        if _with_gmpy:
            pub = int(pow(gmpy.mpz(self.GEN()),
                          gmpy.mpz(self.priv()),
                          gmpy.mpz(self.MOD())))
        else:
            pub = pow(self.GEN(), self.priv(), self.MOD())
        self.pub = Int('pub', Pt=pub, Type=self.TYPE)
    
    def get_public(self):
        return bytes(self.pub)
    
    def compute_shared_key(self, pub_peer):
        if len(pub_peer) < self.SIZE//8:
            pub_peer = b'\0'*(self.SIZE//8 - len(pub_peer)) + pub_peer
        elif len(pub_peer) > self.SIZE//8:
            pub_peer = pub_peer[-self.SIZE//8:]
        self.pub_peer = Int('pub_peer', Type=self.TYPE)
        self.pub_peer.map(pub_peer)
        #
        if _with_gmpy:
            shk = int(pow(gmpy.mpz(self.pub_peer()),
                          gmpy.mpz(self.priv()),
                          gmpy.mpz(self.MOD())))
        else:
            shk = pow(self.pub_peer(), self.priv(), self.MOD())
        self.shkey = Int('shkey', Pt=shk, Type=self.TYPE)
    
    def get_shared_key(self):
        return bytes(self.shkey)
Esempio n. 39
0
 def __repr__(self):
     if self.Repr == 'hum' \
     and (self.Pt is not None or self.Val is not None):
         return self.interpret()
     else:
         return Int.__repr__(self)
Esempio n. 40
0
 def __init__(self, **kwargs):
     Int.__init__(self, CallName=split('\.', str(self.__class__))[-1][:-2], \
                  Type='uint8', **kwargs)