cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

HOTP/TOTP implementation

Open Ayrx opened this issue 12 years ago • 10 comments

As per @public's comment on #585, this is a todo list for a HOTP/TOTP implementation.

  • [x] Test vectors
  • [x] HOTP implementation, documentation and test.
  • [x] TOTP implementation, documentation and test.

As far as the meat of the implementation goes, I have previously written a working HOTP and TOTP implementation located here https://github.com/Ayrx/py-otp. I can reuse most of the code from that and modify it to a more generalized and appropriate API once the interface is decided.

Ayrx avatar Feb 09 '14 12:02 Ayrx

Here is my suggested interface for the two algorithms.

otp = HOTP(secret, backend, length=6)
hotp = otp.generate(counter)
otp.verify(hotp, counter)

otp = TOTP(secret, backend, length=6, algo="SHA1", time_step=30)
totp = otp.generate(time)
otp.verify(totp, time)

I think both the length and time_step parameters should have sensible defaults. Opinions?

Ayrx avatar Feb 09 '14 14:02 Ayrx

I am -1 on defaults. algo should be algorithm and also a HashAlgorithm implementation. backend is traditionally last in the arguments.

public avatar Feb 09 '14 14:02 public

I think these should live in cryptography.oath?

public avatar Feb 09 '14 14:02 public

As @public already noted we try to avoid defaults (recommendations evolve over time and having defaults becomes a real support burden). algo should also be algorithm and it will take an instance of an object conforming to HashAlgorithm (defined in cryptography.hazmat.primitives.interfaces. The actual hashes are defined in cryptography.hazmat.primitives.hashes).

So that makes it look more like this:

otp = HOTP(secret, length, backend)
hotp = otp.generate(counter)
otp.verify(hotp, counter)

otp = TOTP(secret, length, algorithm, time_step, backend)
totp = otp.generate(time)
otp.verify(totp, time)

We can list normative advice (e.g. time_step size, length, algorithm) in the docs.

Also, as discussed in IRC I'd prefer this to live in cryptography.hazmat.oath. My personal opinion is that the need to track both key and counter value (and ensure that counter is strictly increasing) makes it hazmat-y. Others are free to disagree. :smiley:

reaperhulk avatar Feb 09 '14 16:02 reaperhulk

So I have concerns which are not exactly specific to HOTP/TOTP but it's as good of a soap box as any. i do not want to see cryptography become a "everything and the kitchen sink" library. As far as I know HOTP/TOTP do not need anything backend specific and can be completely implemented using the standard library's hashlib. Is there a benefit to using our backends? Is there anything that we'd want to use the backends for at all? (Storing secrets in a HSM?).

In the end I'm not super fussed about if we get a HOTP/TOTP implementation or not, I just want to make sure we don't just start adding anything that's cool or useful. Once something is added to cryptography then that thing is tied to our release schedule. Making it so people can't upgrade (or not upgrade!) things independently if they are using more than one piece of our library. We need to draw the line somewhere and say "No", this may or may not mean saying no to HOTP/TOTP.

dstufft avatar Feb 09 '14 17:02 dstufft

My question around the whole kitchen sink objection would be responded to by discussing the existing Fernet implementation in the code. From a simple look at it, it also doesn't really depend on anything specific about these implementations of cryptography primatives. Could HOTP/TOTP be considered a recipe that people would be interested in too?

harleyk avatar Feb 09 '14 23:02 harleyk

@juliankrause I think there is concern about making HOTP/TOTP a recipe because there are a few vectors that the user can potentially shoot himself in the foot with including things like counter reuse.

I see @dstufft's concern about bloat in the library. I agree with what Paul said on the mailing list though. HOTP and TOTP are well defined and commonly used protocols that is simple enough to write and won't require much maintenance after. Of course, being the one that suggested the feature in the first place, I might be a little biased. :)

Ayrx avatar Feb 10 '14 03:02 Ayrx

So today I got some input from a few Python programmers who were interested, potential consumers of cryptography. Their attitude was approximately: "Is this a subtle thing, that's cryptographic, and possibly easy to get wrong, that I should have a definitive good implementation of if I want to use it? Then yes, it should be in your library". I was very moved by @dstufft's quip "The cryptographic standard library" earlier, and I think these are probably within scope.

On Sun, Feb 9, 2014 at 7:56 PM, Ayrx [email protected] wrote:

@juliankrause https://github.com/juliankrause I think there is concern about making HOTP/TOTP a recipe because there are a few vectors that the user can potentially shoot himself in the foot with including things like counter reuse.

I see @dstufft https://github.com/dstufft's concern about bloat in the library. I agree with what Paul said on the mailing list though. HOTP and TOTP are well defined and commonly used protocols that is simple enough to write and won't require much maintenance after. Of course, being the one that suggested the feature in the first place, I might be a little biased. :)

— Reply to this email directly or view it on GitHubhttps://github.com/pyca/cryptography/issues/588#issuecomment-34599239 .

"I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire) "The people's good is the highest law." -- Cicero GPG Key fingerprint: 125F 5C67 DFE9 4084

alex avatar Feb 10 '14 03:02 alex

Maybe that should be our tagline :D

dstufft avatar Feb 10 '14 04:02 dstufft

In general I am +1 on adding pretty much anything under the cryptography namespace. Particularly if it potentially brings a new contributor with it. The question of how we package that is separate AIUI. (Or are subpackage installs not a thing that works anymore?)

public avatar Feb 10 '14 08:02 public