def _simpleDES (key, data): assert len (key) in [8, 16, 24] assert len (data) in [8, 16, 24] assert not len (data) % len (key) cipher = M2Crypto.EVP.Cipher (alg='des_ecb', op=0, iv=8*'00', key=_unhexlify(key), salt=None, padding = 0) return cipher.update (_unhexlify(data))
def _search_string_to_bytes_list(searchstr, sflag=0, radix=16): """ # TODO: Document This yields so in the future i can support wildcards """ # TODO: Implement radix? if isinstance(searchstr, str): searchstr = searchstr.encode('utf8') searchstr = searchstr.lstrip() match = b'' for c in [b'"', b"'"]: if searchstr.startswith(c): searchstr = searchstr[1:] match = c break # Decode a search string representing a string (ex: '"Hello\nworld!"') if match: i = searchstr.rfind(match) if i not in (-1, 0) and searchstr[i - 1] != '\\': searchstr = searchstr[:i] # TODO: Actual string parsing yield searchstr # Decode a search string representing hex bytes else: for b in _whitespaces: searchstr = searchstr.replace(b, b'') yield _unhexlify(searchstr)
def generateResponse (nonce, key=16*'00', ourNonce = None, debug=False): """ Given a nonce picked by the verifier computes the response of the prover. nonce -- the nonce as given by the verifier. our_nonce -- the nonce of the prover (in case he choses one). debug -- output the intermediate steps. """ nt = _decipher(nonce, key) nt2 = nt[1:]+nt[:1] nr = ourNonce or os.urandom(8) d1 = _decipher (nr, key) buff = int (_hexlify (d1), 16) ^ int (_hexlify (nt2), 16) buff = buff & 0xffffffffffffff00 >> 8 | buff & 0x00000000000000ff << 56 d2 = _decipher (_unhexlify(hex (buff)[2:-1]), key) if debug: print 'nt =', _hexlify (nt) print 'nt2 =', _hexlify (nt2) print 'D1 =', _hexlify (d1) print 'Buff =', hex(buff)[2:-1] print 'D2 =', _hexlify(d2) print 'D1 || D2 =', _hexlify (d1) + _hexlify (d2) return (d1 + d2, nr)
def row2feature(row, id_field, geometry_field): feature = {'type': 'Feature', 'properties': _copy(row)} geometry = feature['properties'].pop(geometry_field) feature['geometry'] = _loadshape(_unhexlify(geometry)) feature['id'] = feature['properties'].pop(id_field) return feature
def decipherSendMode (data, key): assert not len(data) % len(key) iv = 8*'00' res = [] while len(data): iv = _hexlify(_simpleDES(crc.mergeList(key), xorit(iv, data[0:8]))) res.append(iv) data = data[8:] return _unhexlify (''.join(res))
def row2feature(row, id_field, geometry_field): """ Convert a database row dict to a feature dict. """ feature = {'type': 'Feature', 'properties': _copy(row)} geometry = feature['properties'].pop(geometry_field) feature['geometry'] = _loadshape(_unhexlify(geometry)) feature['id'] = feature['properties'].pop(id_field) return feature
def verifyResponse (resp, nr, key = 16*'00'): """ Verifies the resonse given by the verifier for mutual authentication. resp -- the response as given by the verifier. nonce -- the nonce as chosen by the prover. """ _resp = _unhexlify (resp) nr2 = nr[1:]+nr[:1] return _decipher (_resp, key) == nr2
def _decipher (data, key = 16*'00', iv=8*'00', pad=None): _key = _unhexlify (key) _iv = _unhexlify (iv) pbuf = cStringIO.StringIO () cbuf = cStringIO.StringIO (data) if pad is None: _pad = 0 else: _pad = 1 des_box = M2Crypto.EVP.Cipher (alg='des_ede_cbc', op=0, key=_key, iv=_iv, padding = _pad) plaintext = _cipher_filter(des_box, cbuf, pbuf) pbuf.close () cbuf.close () des_box = None return plaintext
def _to_bytes(value, length, dummy, _unhexlify=_unhexlify): """An implementation of int.to_bytes for python 2.x.""" fmt = '%%0%dx' % (2 * length,) return _unhexlify(fmt % value)
def _to_bytes(value, length, endianess, unhexlify=_unhexlify): fmt = '%%0%ix' % (2 * length) return _unhexlify(fmt % value)
def _to_bytes(value, dummy0, dummy1, _unhexlify=_unhexlify): """An implementation of int.to_bytes for python 2.x.""" return _unhexlify('%040x' % value)
isDes -- for the special case of TDES where k1 = k2 (DES). """ if isDES: sessionKey = ourNonce[:4] + nonce[:4] assert len(sessionKey) is 8 return sessionKey else: sessionKey = ourNonce[:4] + nonce[:4] + ourNonce[4:] + nonce[4:] assert len(sessionKey) is 16 return sessionKey def isDES (key): """ Returns True if a given 3DES key is used to actually do DES. """ h = len (key) / 2 return key[:h] == key[h:] if __name__ == '__main__': nonce = 0x6e7577944adffc0c _nonce = _unhexlify (hex (nonce)[2:]) ourNonce = 0x1122334455667788 _ourNonce = _unhexlify (hex (ourNonce)[2:]) response, nr = generateResponse (_nonce, ourNonce = _ourNonce, debug=True) print 'Response = ', _hexlify (response) print 'Nonce = ', _hexlify (nr) print 'Verification ok?', verifyResponse ('AD6CC16025CCFB7B', nr) print 'Session key = ', _hexlify (deriveSessionKey (_nonce, ourNonce = _ourNonce, isDES=True))