basic functionalty
This commit is contained in:
343
modules/Crypto/Hash/CMAC.py
Normal file
343
modules/Crypto/Hash/CMAC.py
Normal file
@@ -0,0 +1,343 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Hash/CMAC.py - Implements the CMAC algorithm
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""CMAC (Cipher-based Message Authentication Code) algorithm
|
||||
|
||||
CMAC is a MAC defined in `NIST SP 800-38B`_ and in RFC4493_ (for AES only)
|
||||
and constructed using a block cipher. It was originally known as `OMAC1`_.
|
||||
|
||||
The algorithm is sometimes named *X-CMAC* where *X* is the name
|
||||
of the cipher (e.g. AES-CMAC).
|
||||
|
||||
This is an example showing how to *create* an AES-CMAC:
|
||||
|
||||
>>> from Crypto.Hash import CMAC
|
||||
>>> from Crypto.Cipher import AES
|
||||
>>>
|
||||
>>> secret = b'Sixteen byte key'
|
||||
>>> cobj = CMAC.new(secret, ciphermod=AES)
|
||||
>>> cobj.update(b'Hello')
|
||||
>>> print cobj.hexdigest()
|
||||
|
||||
And this is an example showing how to *check* an AES-CMAC:
|
||||
|
||||
>>> from Crypto.Hash import CMAC
|
||||
>>> from Crypto.Cipher import AES
|
||||
>>>
|
||||
>>> # We have received a message 'msg' together
|
||||
>>> # with its MAC 'mac'
|
||||
>>>
|
||||
>>> secret = b'Sixteen byte key'
|
||||
>>> cobj = CMAC.new(secret, ciphermod=AES)
|
||||
>>> cobj.update(msg)
|
||||
>>> try:
|
||||
>>> cobj.verify(mac)
|
||||
>>> print "The message '%s' is authentic" % msg
|
||||
>>> except ValueError:
|
||||
>>> print "The message or the key is wrong"
|
||||
|
||||
.. _`NIST SP 800-38B`: http://csrc.nist.gov/publications/nistpubs/800-38B/SP_800-38B.pdf
|
||||
.. _RFC4493: http://www.ietf.org/rfc/rfc4493.txt
|
||||
.. _OMAC1: http://www.nuee.nagoya-u.ac.jp/labs/tiwata/omac/omac.html
|
||||
"""
|
||||
|
||||
__all__ = ['new', 'digest_size', 'CMAC' ]
|
||||
|
||||
import sys
|
||||
if sys.version_info[0] == 2 and sys.version_info[1] == 1:
|
||||
from Crypto.Util.py21compat import *
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
from binascii import unhexlify
|
||||
|
||||
from Crypto.Util.strxor import strxor
|
||||
from Crypto.Util.number import long_to_bytes, bytes_to_long
|
||||
|
||||
#: The size of the authentication tag produced by the MAC.
|
||||
digest_size = None
|
||||
|
||||
def _shift_bytes(bs, xor_lsb=0):
|
||||
num = (bytes_to_long(bs)<<1) ^ xor_lsb
|
||||
return long_to_bytes(num, len(bs))[-len(bs):]
|
||||
|
||||
class _SmoothMAC(object):
|
||||
"""Turn a MAC that only operates on aligned blocks of data
|
||||
into a MAC with granularity of 1 byte."""
|
||||
|
||||
def __init__(self, block_size, msg=b(""), min_digest=0):
|
||||
self._bs = block_size
|
||||
#: Data waiting to be MAC-ed
|
||||
self._buffer = []
|
||||
self._buffer_len = 0
|
||||
#: Data received via update()
|
||||
self._total_len = 0
|
||||
#: Minimum amount of bytes required by the final digest step
|
||||
self._min_digest = min_digest
|
||||
#: Block MAC object
|
||||
self._mac = None
|
||||
#: Cached digest
|
||||
self._tag = None
|
||||
if msg:
|
||||
self.update(msg)
|
||||
|
||||
def can_reduce(self):
|
||||
return (self._mac is not None)
|
||||
|
||||
def get_len(self):
|
||||
return self._total_len
|
||||
|
||||
def zero_pad(self):
|
||||
if self._buffer_len & (self._bs-1):
|
||||
npad = self._bs - self._buffer_len & (self._bs-1)
|
||||
self._buffer.append(bchr(0)*npad)
|
||||
self._buffer_len += npad
|
||||
|
||||
def update(self, data):
|
||||
# Optimization (try not to copy data if possible)
|
||||
if self._buffer_len==0 and self.can_reduce() and\
|
||||
self._min_digest==0 and len(data)%self._bs==0:
|
||||
self._update(data)
|
||||
self._total_len += len(data)
|
||||
return
|
||||
|
||||
self._buffer.append(data)
|
||||
self._buffer_len += len(data)
|
||||
self._total_len += len(data)
|
||||
|
||||
# Feed data into MAC
|
||||
blocks, rem = divmod(self._buffer_len, self._bs)
|
||||
if rem<self._min_digest:
|
||||
blocks -= 1
|
||||
if blocks>0 and self.can_reduce():
|
||||
aligned_data = blocks*self._bs
|
||||
buf = b("").join(self._buffer)
|
||||
self._update(buf[:aligned_data])
|
||||
self._buffer = [ buf[aligned_data:] ]
|
||||
self._buffer_len -= aligned_data
|
||||
|
||||
def _deep_copy(self, target):
|
||||
# Copy everything by self._mac, since we don't know how to
|
||||
target._buffer = self._buffer[:]
|
||||
for m in [ '_bs', '_buffer_len', '_total_len', '_min_digest', '_tag' ]:
|
||||
setattr(target, m, getattr(self, m))
|
||||
|
||||
def _update(self, data_block):
|
||||
"""Delegate to the implementation the update
|
||||
of the MAC state given some new *block aligned* data."""
|
||||
raise NotImplementedError("_update() must be still implemented")
|
||||
|
||||
def _digest(self, left_data):
|
||||
"""Delegate to the implementation the computation
|
||||
of the final MAC given the current MAC state
|
||||
and the last piece of data (not block aligned)."""
|
||||
raise NotImplementedError("_digest() must be still implemented")
|
||||
|
||||
def digest(self):
|
||||
if self._tag:
|
||||
return self._tag
|
||||
if self._buffer_len>0:
|
||||
self.update(b(""))
|
||||
left_data = b("").join(self._buffer)
|
||||
self._tag = self._digest(left_data)
|
||||
return self._tag
|
||||
|
||||
class CMAC(_SmoothMAC):
|
||||
"""Class that implements CMAC"""
|
||||
|
||||
#: The size of the authentication tag produced by the MAC.
|
||||
digest_size = None
|
||||
|
||||
def __init__(self, key, msg = None, ciphermod = None):
|
||||
"""Create a new CMAC object.
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
secret key for the CMAC object.
|
||||
The key must be valid for the underlying cipher algorithm.
|
||||
For instance, it must be 16 bytes long for AES-128.
|
||||
msg : byte string
|
||||
The very first chunk of the message to authenticate.
|
||||
It is equivalent to an early call to `update`. Optional.
|
||||
ciphermod : module
|
||||
A cipher module from `Crypto.Cipher`.
|
||||
The cipher's block size must be 64 or 128 bits.
|
||||
It is recommended to use `Crypto.Cipher.AES`.
|
||||
"""
|
||||
|
||||
if ciphermod is None:
|
||||
raise TypeError("ciphermod must be specified (try AES)")
|
||||
|
||||
_SmoothMAC.__init__(self, ciphermod.block_size, msg, 1)
|
||||
|
||||
self._key = key
|
||||
self._factory = ciphermod
|
||||
|
||||
# Section 5.3 of NIST SP 800 38B
|
||||
if ciphermod.block_size==8:
|
||||
const_Rb = 0x1B
|
||||
elif ciphermod.block_size==16:
|
||||
const_Rb = 0x87
|
||||
else:
|
||||
raise TypeError("CMAC requires a cipher with a block size of 8 or 16 bytes, not %d" %
|
||||
(ciphermod.block_size,))
|
||||
self.digest_size = ciphermod.block_size
|
||||
|
||||
# Compute sub-keys
|
||||
cipher = ciphermod.new(key, ciphermod.MODE_ECB)
|
||||
l = cipher.encrypt(bchr(0)*ciphermod.block_size)
|
||||
if bord(l[0]) & 0x80:
|
||||
self._k1 = _shift_bytes(l, const_Rb)
|
||||
else:
|
||||
self._k1 = _shift_bytes(l)
|
||||
if bord(self._k1[0]) & 0x80:
|
||||
self._k2 = _shift_bytes(self._k1, const_Rb)
|
||||
else:
|
||||
self._k2 = _shift_bytes(self._k1)
|
||||
|
||||
# Initialize CBC cipher with zero IV
|
||||
self._IV = bchr(0)*ciphermod.block_size
|
||||
self._mac = ciphermod.new(key, ciphermod.MODE_CBC, self._IV)
|
||||
|
||||
def update(self, msg):
|
||||
"""Continue authentication of a message by consuming the next chunk of data.
|
||||
|
||||
Repeated calls are equivalent to a single call with the concatenation
|
||||
of all the arguments. In other words:
|
||||
|
||||
>>> m.update(a); m.update(b)
|
||||
|
||||
is equivalent to:
|
||||
|
||||
>>> m.update(a+b)
|
||||
|
||||
:Parameters:
|
||||
msg : byte string
|
||||
The next chunk of the message being authenticated
|
||||
"""
|
||||
|
||||
_SmoothMAC.update(self, msg)
|
||||
|
||||
def _update(self, data_block):
|
||||
self._IV = self._mac.encrypt(data_block)[-self._mac.block_size:]
|
||||
|
||||
def copy(self):
|
||||
"""Return a copy ("clone") of the MAC object.
|
||||
|
||||
The copy will have the same internal state as the original MAC
|
||||
object.
|
||||
This can be used to efficiently compute the MAC of strings that
|
||||
share a common initial substring.
|
||||
|
||||
:Returns: A `CMAC` object
|
||||
"""
|
||||
obj = CMAC(self._key, ciphermod=self._factory)
|
||||
|
||||
_SmoothMAC._deep_copy(self, obj)
|
||||
obj._mac = self._factory.new(self._key, self._factory.MODE_CBC, self._IV)
|
||||
for m in [ '_tag', '_k1', '_k2', '_IV']:
|
||||
setattr(obj, m, getattr(self, m))
|
||||
return obj
|
||||
|
||||
def digest(self):
|
||||
"""Return the **binary** (non-printable) MAC of the message that has
|
||||
been authenticated so far.
|
||||
|
||||
This method does not change the state of the MAC object.
|
||||
You can continue updating the object after calling this function.
|
||||
|
||||
:Return: A byte string of `digest_size` bytes. It may contain non-ASCII
|
||||
characters, including null bytes.
|
||||
"""
|
||||
return _SmoothMAC.digest(self)
|
||||
|
||||
def _digest(self, last_data):
|
||||
if len(last_data)==self._bs:
|
||||
last_block = strxor(last_data, self._k1)
|
||||
else:
|
||||
last_block = strxor(last_data+bchr(128)+
|
||||
bchr(0)*(self._bs-1-len(last_data)), self._k2)
|
||||
tag = self._mac.encrypt(last_block)
|
||||
return tag
|
||||
|
||||
def hexdigest(self):
|
||||
"""Return the **printable** MAC of the message that has been
|
||||
authenticated so far.
|
||||
|
||||
This method does not change the state of the MAC object.
|
||||
|
||||
:Return: A string of 2* `digest_size` bytes. It contains only
|
||||
hexadecimal ASCII digits.
|
||||
"""
|
||||
return "".join(["%02x" % bord(x)
|
||||
for x in tuple(self.digest())])
|
||||
|
||||
def verify(self, mac_tag):
|
||||
"""Verify that a given **binary** MAC (computed by another party) is valid.
|
||||
|
||||
:Parameters:
|
||||
mac_tag : byte string
|
||||
The expected MAC of the message.
|
||||
:Raises ValueError:
|
||||
if the MAC does not match. It means that the message
|
||||
has been tampered with or that the MAC key is incorrect.
|
||||
"""
|
||||
|
||||
mac = self.digest()
|
||||
res = 0
|
||||
# Constant-time comparison
|
||||
for x,y in zip(mac, mac_tag):
|
||||
res |= bord(x) ^ bord(y)
|
||||
if res or len(mac_tag)!=self.digest_size:
|
||||
raise ValueError("MAC check failed")
|
||||
|
||||
def hexverify(self, hex_mac_tag):
|
||||
"""Verify that a given **printable** MAC (computed by another party) is valid.
|
||||
|
||||
:Parameters:
|
||||
hex_mac_tag : string
|
||||
The expected MAC of the message, as a hexadecimal string.
|
||||
:Raises ValueError:
|
||||
if the MAC does not match. It means that the message
|
||||
has been tampered with or that the MAC key is incorrect.
|
||||
"""
|
||||
|
||||
self.verify(unhexlify(tobytes(hex_mac_tag)))
|
||||
|
||||
def new(key, msg = None, ciphermod = None):
|
||||
"""Create a new CMAC object.
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
secret key for the CMAC object.
|
||||
The key must be valid for the underlying cipher algorithm.
|
||||
For instance, it must be 16 bytes long for AES-128.
|
||||
msg : byte string
|
||||
The very first chunk of the message to authenticate.
|
||||
It is equivalent to an early call to `CMAC.update`. Optional.
|
||||
ciphermod : module
|
||||
A cipher module from `Crypto.Cipher`.
|
||||
The cipher's block size must be 64 or 128 bits.
|
||||
Default is `Crypto.Cipher.AES`.
|
||||
|
||||
:Returns: A `CMAC` object
|
||||
"""
|
||||
return CMAC(key, msg, ciphermod)
|
263
modules/Crypto/Hash/HMAC.py
Normal file
263
modules/Crypto/Hash/HMAC.py
Normal file
@@ -0,0 +1,263 @@
|
||||
# HMAC.py - Implements the HMAC algorithm as described by RFC 2104.
|
||||
#
|
||||
# ===================================================================
|
||||
# Portions Copyright (c) 2001, 2002, 2003 Python Software Foundation;
|
||||
# All Rights Reserved
|
||||
#
|
||||
# This file contains code from the Python 2.2 hmac.py module (the
|
||||
# "Original Code"), with modifications made after it was incorporated
|
||||
# into PyCrypto (the "Modifications").
|
||||
#
|
||||
# To the best of our knowledge, the Python Software Foundation is the
|
||||
# copyright holder of the Original Code, and has licensed it under the
|
||||
# Python 2.2 license. See the file LEGAL/copy/LICENSE.python-2.2 for
|
||||
# details.
|
||||
#
|
||||
# The Modifications to this file are dedicated to the public domain.
|
||||
# To the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever. No rights are
|
||||
# reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
|
||||
"""HMAC (Hash-based Message Authentication Code) algorithm
|
||||
|
||||
HMAC is a MAC defined in RFC2104_ and FIPS-198_ and constructed using
|
||||
a cryptograpic hash algorithm.
|
||||
It is usually named *HMAC-X*, where *X* is the hash algorithm; for
|
||||
instance *HMAC-SHA1* or *HMAC-MD5*.
|
||||
|
||||
The strength of an HMAC depends on:
|
||||
|
||||
- the strength of the hash algorithm
|
||||
- the length and entropy of the secret key
|
||||
|
||||
This is an example showing how to *create* a MAC:
|
||||
|
||||
>>> from Crypto.Hash import HMAC
|
||||
>>>
|
||||
>>> secret = b'Swordfish'
|
||||
>>> h = HMAC.new(secret)
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
This is an example showing how to *check* a MAC:
|
||||
|
||||
>>> from Crypto.Hash import HMAC
|
||||
>>>
|
||||
>>> # We have received a message 'msg' together
|
||||
>>> # with its MAC 'mac'
|
||||
>>>
|
||||
>>> secret = b'Swordfish'
|
||||
>>> h = HMAC.new(secret)
|
||||
>>> h.update(msg)
|
||||
>>> try:
|
||||
>>> h.verify(mac)
|
||||
>>> print "The message '%s' is authentic" % msg
|
||||
>>> except ValueError:
|
||||
>>> print "The message or the key is wrong"
|
||||
|
||||
.. _RFC2104: http://www.ietf.org/rfc/rfc2104.txt
|
||||
.. _FIPS-198: http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf
|
||||
"""
|
||||
|
||||
# This is just a copy of the Python 2.2 HMAC module, modified to work when
|
||||
# used on versions of Python before 2.2.
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'HMAC' ]
|
||||
|
||||
from binascii import unhexlify
|
||||
|
||||
from Crypto.Util.strxor import strxor_c
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
#: The size of the authentication tag produced by the MAC.
|
||||
#: It matches the digest size on the underlying
|
||||
#: hashing module used.
|
||||
digest_size = None
|
||||
|
||||
class HMAC:
|
||||
"""Class that implements HMAC"""
|
||||
|
||||
#: The size of the authentication tag produced by the MAC.
|
||||
#: It matches the digest size on the underlying
|
||||
#: hashing module used.
|
||||
digest_size = None
|
||||
|
||||
def __init__(self, key, msg = None, digestmod = None):
|
||||
"""Create a new HMAC object.
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
secret key for the MAC object.
|
||||
It must be long enough to match the expected security level of the
|
||||
MAC. However, there is no benefit in using keys longer than the
|
||||
`digest_size` of the underlying hash algorithm.
|
||||
msg : byte string
|
||||
The very first chunk of the message to authenticate.
|
||||
It is equivalent to an early call to `update()`. Optional.
|
||||
:Parameter digestmod:
|
||||
The hash algorithm the HMAC is based on.
|
||||
Default is `Crypto.Hash.MD5`.
|
||||
:Type digestmod:
|
||||
A hash module or object instantiated from `Crypto.Hash`
|
||||
"""
|
||||
if digestmod is None:
|
||||
import MD5
|
||||
digestmod = MD5
|
||||
|
||||
self.digestmod = digestmod
|
||||
self.outer = digestmod.new()
|
||||
self.inner = digestmod.new()
|
||||
try:
|
||||
self.digest_size = digestmod.digest_size
|
||||
except AttributeError:
|
||||
self.digest_size = len(self.outer.digest())
|
||||
|
||||
try:
|
||||
# The block size is 128 bytes for SHA384 and SHA512 and 64 bytes
|
||||
# for the others hash function
|
||||
blocksize = digestmod.block_size
|
||||
except AttributeError:
|
||||
blocksize = 64
|
||||
|
||||
ipad = 0x36
|
||||
opad = 0x5C
|
||||
|
||||
if len(key) > blocksize:
|
||||
key = digestmod.new(key).digest()
|
||||
|
||||
key = key + bchr(0) * (blocksize - len(key))
|
||||
self.outer.update(strxor_c(key, opad))
|
||||
self.inner.update(strxor_c(key, ipad))
|
||||
if (msg):
|
||||
self.update(msg)
|
||||
|
||||
def update(self, msg):
|
||||
"""Continue authentication of a message by consuming the next chunk of data.
|
||||
|
||||
Repeated calls are equivalent to a single call with the concatenation
|
||||
of all the arguments. In other words:
|
||||
|
||||
>>> m.update(a); m.update(b)
|
||||
|
||||
is equivalent to:
|
||||
|
||||
>>> m.update(a+b)
|
||||
|
||||
:Parameters:
|
||||
msg : byte string
|
||||
The next chunk of the message being authenticated
|
||||
"""
|
||||
|
||||
self.inner.update(msg)
|
||||
|
||||
def copy(self):
|
||||
"""Return a copy ("clone") of the MAC object.
|
||||
|
||||
The copy will have the same internal state as the original MAC
|
||||
object.
|
||||
This can be used to efficiently compute the MAC of strings that
|
||||
share a common initial substring.
|
||||
|
||||
:Returns: An `HMAC` object
|
||||
"""
|
||||
other = HMAC(b(""))
|
||||
other.digestmod = self.digestmod
|
||||
other.inner = self.inner.copy()
|
||||
other.outer = self.outer.copy()
|
||||
return other
|
||||
|
||||
def digest(self):
|
||||
"""Return the **binary** (non-printable) MAC of the message that has
|
||||
been authenticated so far.
|
||||
|
||||
This method does not change the state of the MAC object.
|
||||
You can continue updating the object after calling this function.
|
||||
|
||||
:Return: A byte string of `digest_size` bytes. It may contain non-ASCII
|
||||
characters, including null bytes.
|
||||
"""
|
||||
|
||||
h = self.outer.copy()
|
||||
h.update(self.inner.digest())
|
||||
return h.digest()
|
||||
|
||||
def verify(self, mac_tag):
|
||||
"""Verify that a given **binary** MAC (computed by another party) is valid.
|
||||
|
||||
:Parameters:
|
||||
mac_tag : byte string
|
||||
The expected MAC of the message.
|
||||
:Raises ValueError:
|
||||
if the MAC does not match. It means that the message
|
||||
has been tampered with or that the MAC key is incorrect.
|
||||
"""
|
||||
|
||||
mac = self.digest()
|
||||
res = 0
|
||||
# Constant-time comparison
|
||||
for x,y in zip(mac, mac_tag):
|
||||
res |= bord(x) ^ bord(y)
|
||||
if res or len(mac_tag)!=self.digest_size:
|
||||
raise ValueError("MAC check failed")
|
||||
|
||||
def hexdigest(self):
|
||||
"""Return the **printable** MAC of the message that has been
|
||||
authenticated so far.
|
||||
|
||||
This method does not change the state of the MAC object.
|
||||
|
||||
:Return: A string of 2* `digest_size` bytes. It contains only
|
||||
hexadecimal ASCII digits.
|
||||
"""
|
||||
return "".join(["%02x" % bord(x)
|
||||
for x in tuple(self.digest())])
|
||||
|
||||
def hexverify(self, hex_mac_tag):
|
||||
"""Verify that a given **printable** MAC (computed by another party) is valid.
|
||||
|
||||
:Parameters:
|
||||
hex_mac_tag : string
|
||||
The expected MAC of the message, as a hexadecimal string.
|
||||
:Raises ValueError:
|
||||
if the MAC does not match. It means that the message
|
||||
has been tampered with or that the MAC key is incorrect.
|
||||
"""
|
||||
|
||||
self.verify(unhexlify(tobytes(hex_mac_tag)))
|
||||
|
||||
def new(key, msg = None, digestmod = None):
|
||||
"""Create a new HMAC object.
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
key for the MAC object.
|
||||
It must be long enough to match the expected security level of the
|
||||
MAC. However, there is no benefit in using keys longer than the
|
||||
`digest_size` of the underlying hash algorithm.
|
||||
msg : byte string
|
||||
The very first chunk of the message to authenticate.
|
||||
It is equivalent to an early call to `HMAC.update()`.
|
||||
Optional.
|
||||
:Parameter digestmod:
|
||||
The hash to use to implement the HMAC. Default is `Crypto.Hash.MD5`.
|
||||
:Type digestmod:
|
||||
A hash module or instantiated object from `Crypto.Hash`
|
||||
:Returns: An `HMAC` object
|
||||
"""
|
||||
return HMAC(key, msg, digestmod)
|
||||
|
92
modules/Crypto/Hash/MD5.py
Normal file
92
modules/Crypto/Hash/MD5.py
Normal file
@@ -0,0 +1,92 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""MD5 cryptographic hash algorithm.
|
||||
|
||||
MD5 is specified in RFC1321_ and produces the 128 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import MD5
|
||||
>>>
|
||||
>>> h = MD5.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
MD5 stand for Message Digest version 5, and it was invented by Rivest in 1991.
|
||||
|
||||
This algorithm is insecure. Do not use it for new designs.
|
||||
|
||||
.. _RFC1321: http://tools.ietf.org/html/rfc1321
|
||||
"""
|
||||
|
||||
from __future__ import nested_scopes
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'block_size', 'digest_size']
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
if sys.version_info[0] == 2 and sys.version_info[1] == 1:
|
||||
from Crypto.Util.py21compat import *
|
||||
|
||||
def __make_constructor():
|
||||
try:
|
||||
# The md5 module is deprecated in Python 2.6, so use hashlib when possible.
|
||||
from hashlib import md5 as _hash_new
|
||||
except ImportError:
|
||||
from md5 import new as _hash_new
|
||||
|
||||
h = _hash_new()
|
||||
if hasattr(h, 'new') and hasattr(h, 'name') and hasattr(h, 'digest_size') and hasattr(h, 'block_size'):
|
||||
# The module from stdlib has the API that we need. Just use it.
|
||||
return _hash_new
|
||||
else:
|
||||
# Wrap the hash object in something that gives us the expected API.
|
||||
_copy_sentinel = object()
|
||||
class _MD5(object):
|
||||
digest_size = 16
|
||||
block_size = 64
|
||||
name = "md5"
|
||||
def __init__(self, *args):
|
||||
if args and args[0] is _copy_sentinel:
|
||||
self._h = args[1]
|
||||
else:
|
||||
self._h = _hash_new(*args)
|
||||
def copy(self):
|
||||
return _MD5(_copy_sentinel, self._h.copy())
|
||||
def update(self, *args):
|
||||
f = self.update = self._h.update
|
||||
f(*args)
|
||||
def digest(self):
|
||||
f = self.digest = self._h.digest
|
||||
return f()
|
||||
def hexdigest(self):
|
||||
f = self.hexdigest = self._h.hexdigest
|
||||
return f()
|
||||
_MD5.new = _MD5
|
||||
return _MD5
|
||||
|
||||
new = __make_constructor()
|
||||
del __make_constructor
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = new().digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = new().block_size
|
26
modules/Crypto/Hash/RIPEMD.py
Normal file
26
modules/Crypto/Hash/RIPEMD.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
# This file exists for backward compatibility with old code that refers to
|
||||
# Crypto.Hash.RIPEMD
|
||||
|
||||
"""Deprecated alias for `Crypto.Hash.RIPEMD160`"""
|
||||
|
||||
from Crypto.Hash.RIPEMD160 import new, block_size, digest_size
|
24
modules/Crypto/Hash/SHA.py
Normal file
24
modules/Crypto/Hash/SHA.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
# This file exists for backward compatibility with old code that refers to
|
||||
# Crypto.Hash.SHA
|
||||
|
||||
from Crypto.Hash.SHA1 import __doc__, new, block_size, digest_size
|
92
modules/Crypto/Hash/SHA1.py
Normal file
92
modules/Crypto/Hash/SHA1.py
Normal file
@@ -0,0 +1,92 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""SHA-1 cryptographic hash algorithm.
|
||||
|
||||
SHA-1_ produces the 160 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import SHA1
|
||||
>>>
|
||||
>>> h = SHA1.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
*SHA* stands for Secure Hash Algorithm.
|
||||
|
||||
This algorithm is not considered secure. Do not use it for new designs.
|
||||
|
||||
.. _SHA-1: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
"""
|
||||
|
||||
from __future__ import nested_scopes
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'block_size', 'digest_size']
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
if sys.version_info[0] == 2 and sys.version_info[1] == 1:
|
||||
from Crypto.Util.py21compat import *
|
||||
|
||||
def __make_constructor():
|
||||
try:
|
||||
# The sha module is deprecated in Python 2.6, so use hashlib when possible.
|
||||
from hashlib import sha1 as _hash_new
|
||||
except ImportError:
|
||||
from sha import new as _hash_new
|
||||
|
||||
h = _hash_new()
|
||||
if hasattr(h, 'new') and hasattr(h, 'name') and hasattr(h, 'digest_size') and hasattr(h, 'block_size'):
|
||||
# The module from stdlib has the API that we need. Just use it.
|
||||
return _hash_new
|
||||
else:
|
||||
# Wrap the hash object in something that gives us the expected API.
|
||||
_copy_sentinel = object()
|
||||
class _SHA1(object):
|
||||
digest_size = 20
|
||||
block_size = 64
|
||||
name = "sha1"
|
||||
def __init__(self, *args):
|
||||
if args and args[0] is _copy_sentinel:
|
||||
self._h = args[1]
|
||||
else:
|
||||
self._h = _hash_new(*args)
|
||||
def copy(self):
|
||||
return _SHA1(_copy_sentinel, self._h.copy())
|
||||
def update(self, *args):
|
||||
f = self.update = self._h.update
|
||||
f(*args)
|
||||
def digest(self):
|
||||
f = self.digest = self._h.digest
|
||||
return f()
|
||||
def hexdigest(self):
|
||||
f = self.hexdigest = self._h.hexdigest
|
||||
return f()
|
||||
_SHA1.new = _SHA1
|
||||
return _SHA1
|
||||
|
||||
new = __make_constructor()
|
||||
del __make_constructor
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = new().digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = new().block_size
|
176
modules/Crypto/Hash/__init__.py
Normal file
176
modules/Crypto/Hash/__init__.py
Normal file
@@ -0,0 +1,176 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Hashing algorithms
|
||||
|
||||
Hash functions take arbitrary binary strings as input, and produce a random-like output
|
||||
of fixed size that is dependent on the input; it should be practically infeasible
|
||||
to derive the original input data given only the hash function's
|
||||
output. In other words, the hash function is *one-way*.
|
||||
|
||||
It should also not be practically feasible to find a second piece of data
|
||||
(a *second pre-image*) whose hash is the same as the original message
|
||||
(*weak collision resistance*).
|
||||
|
||||
Finally, it should not be feasible to find two arbitrary messages with the
|
||||
same hash (*strong collision resistance*).
|
||||
|
||||
The output of the hash function is called the *digest* of the input message.
|
||||
In general, the security of a hash function is related to the length of the
|
||||
digest. If the digest is *n* bits long, its security level is roughly comparable
|
||||
to the the one offered by an *n/2* bit encryption algorithm.
|
||||
|
||||
Hash functions can be used simply as a integrity check, or, in
|
||||
association with a public-key algorithm, can be used to implement
|
||||
digital signatures.
|
||||
|
||||
The hashing modules here all support the interface described in `PEP
|
||||
247`_ , "API for Cryptographic Hash Functions".
|
||||
|
||||
.. _`PEP 247` : http://www.python.org/dev/peps/pep-0247/
|
||||
|
||||
:undocumented: _MD2, _MD4, _RIPEMD160, _SHA224, _SHA256, _SHA384, _SHA512
|
||||
"""
|
||||
|
||||
__all__ = ['HMAC', 'MD2', 'MD4', 'MD5', 'RIPEMD160', 'SHA1',
|
||||
'SHA224', 'SHA256', 'SHA384', 'SHA512', 'CMAC']
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
import sys
|
||||
if sys.version_info[0] == 2 and sys.version_info[1] == 1:
|
||||
from Crypto.Util.py21compat import *
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
def new(algo, *args):
|
||||
"""Initialize a new hash object.
|
||||
|
||||
The first argument to this function may be an algorithm name or another
|
||||
hash object.
|
||||
|
||||
This function has significant overhead. It's recommended that you instead
|
||||
import and use the individual hash modules directly.
|
||||
"""
|
||||
|
||||
# Try just invoking algo.new()
|
||||
# We do this first so that this is the fastest.
|
||||
try:
|
||||
new_func = algo.new
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
return new_func(*args)
|
||||
|
||||
# Try getting the algorithm name.
|
||||
if isinstance(algo, str):
|
||||
name = algo
|
||||
else:
|
||||
try:
|
||||
name = algo.name
|
||||
except AttributeError:
|
||||
raise ValueError("unsupported hash type %r" % (algo,))
|
||||
|
||||
# Got the name. Let's see if we have a PyCrypto implementation.
|
||||
try:
|
||||
new_func = _new_funcs[name]
|
||||
except KeyError:
|
||||
# No PyCrypto implementation. Try hashlib.
|
||||
try:
|
||||
import hashlib
|
||||
except ImportError:
|
||||
# There is no hashlib.
|
||||
raise ValueError("unsupported hash type %s" % (name,))
|
||||
return hashlib.new(name, *args)
|
||||
else:
|
||||
# We have a PyCrypto implementation. Instantiate it.
|
||||
return new_func(*args)
|
||||
|
||||
# This dict originally gets the following _*_new methods, but its members get
|
||||
# replaced with the real new() methods of the various hash modules as they are
|
||||
# used. We do it without locks to improve performance, which is safe in
|
||||
# CPython because dict access is atomic in CPython. This might break PyPI.
|
||||
_new_funcs = {}
|
||||
|
||||
def _md2_new(*args):
|
||||
from Crypto.Hash import MD2
|
||||
_new_funcs['MD2'] = _new_funcs['md2'] = MD2.new
|
||||
return MD2.new(*args)
|
||||
_new_funcs['MD2'] = _new_funcs['md2'] = _md2_new
|
||||
del _md2_new
|
||||
|
||||
def _md4_new(*args):
|
||||
from Crypto.Hash import MD4
|
||||
_new_funcs['MD4'] = _new_funcs['md4'] = MD4.new
|
||||
return MD4.new(*args)
|
||||
_new_funcs['MD4'] = _new_funcs['md4'] = _md4_new
|
||||
del _md4_new
|
||||
|
||||
def _md5_new(*args):
|
||||
from Crypto.Hash import MD5
|
||||
_new_funcs['MD5'] = _new_funcs['md5'] = MD5.new
|
||||
return MD5.new(*args)
|
||||
_new_funcs['MD5'] = _new_funcs['md5'] = _md5_new
|
||||
del _md5_new
|
||||
|
||||
def _ripemd160_new(*args):
|
||||
from Crypto.Hash import RIPEMD160
|
||||
_new_funcs['RIPEMD160'] = _new_funcs['ripemd160'] = \
|
||||
_new_funcs['RIPEMD'] = _new_funcs['ripemd'] = RIPEMD160.new
|
||||
return RIPEMD160.new(*args)
|
||||
_new_funcs['RIPEMD160'] = _new_funcs['ripemd160'] = \
|
||||
_new_funcs['RIPEMD'] = _new_funcs['ripemd'] = _ripemd160_new
|
||||
del _ripemd160_new
|
||||
|
||||
def _sha1_new(*args):
|
||||
from Crypto.Hash import SHA1
|
||||
_new_funcs['SHA1'] = _new_funcs['sha1'] = \
|
||||
_new_funcs['SHA'] = _new_funcs['sha'] = SHA1.new
|
||||
return SHA1.new(*args)
|
||||
_new_funcs['SHA1'] = _new_funcs['sha1'] = \
|
||||
_new_funcs['SHA'] = _new_funcs['sha'] = _sha1_new
|
||||
del _sha1_new
|
||||
|
||||
def _sha224_new(*args):
|
||||
from Crypto.Hash import SHA224
|
||||
_new_funcs['SHA224'] = _new_funcs['sha224'] = SHA224.new
|
||||
return SHA224.new(*args)
|
||||
_new_funcs['SHA224'] = _new_funcs['sha224'] = _sha224_new
|
||||
del _sha224_new
|
||||
|
||||
def _sha256_new(*args):
|
||||
from Crypto.Hash import SHA256
|
||||
_new_funcs['SHA256'] = _new_funcs['sha256'] = SHA256.new
|
||||
return SHA256.new(*args)
|
||||
_new_funcs['SHA256'] = _new_funcs['sha256'] = _sha256_new
|
||||
del _sha256_new
|
||||
|
||||
def _sha384_new(*args):
|
||||
from Crypto.Hash import SHA384
|
||||
_new_funcs['SHA384'] = _new_funcs['sha384'] = SHA384.new
|
||||
return SHA384.new(*args)
|
||||
_new_funcs['SHA384'] = _new_funcs['sha384'] = _sha384_new
|
||||
del _sha384_new
|
||||
|
||||
def _sha512_new(*args):
|
||||
from Crypto.Hash import SHA512
|
||||
_new_funcs['SHA512'] = _new_funcs['sha512'] = SHA512.new
|
||||
return SHA512.new(*args)
|
||||
_new_funcs['SHA512'] = _new_funcs['sha512'] = _sha512_new
|
||||
del _sha512_new
|
Reference in New Issue
Block a user