aboutsummaryrefslogtreecommitdiffstats
path: root/cryptography
diff options
context:
space:
mode:
authorDavid Reid <dreid@dreid.org>2014-02-25 10:51:52 -0800
committerDavid Reid <dreid@dreid.org>2014-02-25 10:51:52 -0800
commiteaf4595794516c7c3a4284dce1c9acc8faa122fa (patch)
tree09c77db79276261c2fb9ca01ae5b4a8e8a36029b /cryptography
parenta62d2f5abb953ae761b30b7f09038864602bdc2b (diff)
parentc18dc45eb241d58ec0e81d04ec3f961ad7562ad0 (diff)
downloadcryptography-eaf4595794516c7c3a4284dce1c9acc8faa122fa.tar.gz
cryptography-eaf4595794516c7c3a4284dce1c9acc8faa122fa.tar.bz2
cryptography-eaf4595794516c7c3a4284dce1c9acc8faa122fa.zip
Merge pull request #637 from Ayrx/totp-impl
TOTP Implementation
Diffstat (limited to 'cryptography')
-rw-r--r--cryptography/hazmat/primitives/twofactor/hotp.py15
-rw-r--r--cryptography/hazmat/primitives/twofactor/totp.py33
2 files changed, 43 insertions, 5 deletions
diff --git a/cryptography/hazmat/primitives/twofactor/hotp.py b/cryptography/hazmat/primitives/twofactor/hotp.py
index 80e28820..e806c7ef 100644
--- a/cryptography/hazmat/primitives/twofactor/hotp.py
+++ b/cryptography/hazmat/primitives/twofactor/hotp.py
@@ -17,13 +17,13 @@ import struct
import six
-from cryptography.exceptions import InvalidToken
+from cryptography.exceptions import InvalidToken, UnsupportedAlgorithm
from cryptography.hazmat.primitives import constant_time, hmac
-from cryptography.hazmat.primitives.hashes import SHA1
+from cryptography.hazmat.primitives.hashes import SHA1, SHA256, SHA512
class HOTP(object):
- def __init__(self, key, length, backend):
+ def __init__(self, key, length, algorithm, backend):
if len(key) < 16:
raise ValueError("Key length has to be at least 128 bits.")
@@ -31,8 +31,13 @@ class HOTP(object):
if length < 6 or length > 8:
raise ValueError("Length of HOTP has to be between 6 to 8.")
+ if not isinstance(algorithm, (SHA1, SHA256, SHA512)):
+ raise UnsupportedAlgorithm(
+ "Algorithm must be SHA1, SHA256 or SHA512")
+
self._key = key
self._length = length
+ self._algorithm = algorithm
self._backend = backend
def generate(self, counter):
@@ -45,11 +50,11 @@ class HOTP(object):
raise InvalidToken("Supplied HOTP value does not match")
def _dynamic_truncate(self, counter):
- ctx = hmac.HMAC(self._key, SHA1(), self._backend)
+ ctx = hmac.HMAC(self._key, self._algorithm, self._backend)
ctx.update(struct.pack(">Q", counter))
hmac_value = ctx.finalize()
- offset_bits = six.indexbytes(hmac_value, 19) & 0b1111
+ offset_bits = six.indexbytes(hmac_value, len(hmac_value) - 1) & 0b1111
offset = int(offset_bits)
p = hmac_value[offset:offset + 4]
diff --git a/cryptography/hazmat/primitives/twofactor/totp.py b/cryptography/hazmat/primitives/twofactor/totp.py
new file mode 100644
index 00000000..be84b477
--- /dev/null
+++ b/cryptography/hazmat/primitives/twofactor/totp.py
@@ -0,0 +1,33 @@
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from __future__ import absolute_import, division, print_function
+
+from cryptography.exceptions import InvalidToken
+from cryptography.hazmat.primitives import constant_time
+from cryptography.hazmat.primitives.twofactor.hotp import HOTP
+
+
+class TOTP(object):
+ def __init__(self, key, length, algorithm, time_step, backend):
+
+ self._time_step = time_step
+ self._hotp = HOTP(key, length, algorithm, backend)
+
+ def generate(self, time):
+ counter = int(time / self._time_step)
+ return self._hotp.generate(counter)
+
+ def verify(self, totp, time):
+ if not constant_time.bytes_eq(self.generate(time), totp):
+ raise InvalidToken("Supplied TOTP value does not match")