|
13 | 13 | from pyelliptic import OpenSSL
|
14 | 14 | from pyelliptic import arithmetic as a
|
15 | 15 |
|
16 |
| -from bmconfigparser import config |
17 | 16 |
|
18 | 17 | __all__ = ['encrypt', 'makeCryptor', 'pointMult', 'privToPub', 'sign', 'verify']
|
19 | 18 |
|
@@ -64,43 +63,44 @@ def decryptFast(msg, cryptor):
|
64 | 63 | return cryptor.decrypt(msg)
|
65 | 64 |
|
66 | 65 |
|
67 |
| -def sign(msg, hexPrivkey): |
| 66 | +def _choose_digest_alg(name): |
68 | 67 | """
|
69 |
| - Signs with hex private key using SHA1 or SHA256 depending on |
70 |
| - "digestalg" setting |
| 68 | + Choose openssl digest constant by name raises ValueError if not appropriate |
71 | 69 | """
|
72 |
| - digestAlg = config.safeGet( |
73 |
| - 'bitmessagesettings', 'digestalg', 'sha256') |
74 |
| - if digestAlg == "sha1": |
| 70 | + if name not in ("sha1", "sha256"): |
| 71 | + raise ValueError("Unknown digest algorithm %s" % name) |
| 72 | + return ( |
75 | 73 | # SHA1, this will eventually be deprecated
|
76 |
| - return makeCryptor(hexPrivkey).sign( |
77 |
| - msg, digest_alg=OpenSSL.digest_ecdsa_sha1) |
78 |
| - elif digestAlg == "sha256": |
79 |
| - # SHA256. Eventually this will become the default |
80 |
| - return makeCryptor(hexPrivkey).sign(msg, digest_alg=OpenSSL.EVP_sha256) |
81 |
| - else: |
82 |
| - raise ValueError("Unknown digest algorithm %s" % digestAlg) |
| 74 | + OpenSSL.digest_ecdsa_sha1 if name == "sha1" else OpenSSL.EVP_sha256) |
| 75 | + |
83 | 76 |
|
| 77 | +def sign(msg, hexPrivkey, digestAlg="sha256"): |
| 78 | + """ |
| 79 | + Signs with hex private key using SHA1 or SHA256 depending on |
| 80 | + *digestAlg* keyword. |
| 81 | + """ |
| 82 | + return makeCryptor(hexPrivkey).sign( |
| 83 | + msg, digest_alg=_choose_digest_alg(digestAlg)) |
84 | 84 |
|
85 |
| -def verify(msg, sig, hexPubkey): |
| 85 | + |
| 86 | +def verify(msg, sig, hexPubkey, digestAlg=None): |
86 | 87 | """Verifies with hex public key using SHA1 or SHA256"""
|
87 | 88 | # As mentioned above, we must upgrade gracefully to use SHA256. So
|
88 | 89 | # let us check the signature using both SHA1 and SHA256 and if one
|
89 | 90 | # of them passes then we will be satisfied. Eventually this can
|
90 | 91 | # be simplified and we'll only check with SHA256.
|
91 |
| - try: |
| 92 | + if digestAlg is None: |
92 | 93 | # old SHA1 algorithm.
|
93 |
| - sigVerifyPassed = makePubCryptor(hexPubkey).verify( |
94 |
| - sig, msg, digest_alg=OpenSSL.digest_ecdsa_sha1) |
95 |
| - except: |
96 |
| - sigVerifyPassed = False |
97 |
| - if sigVerifyPassed: |
98 |
| - # The signature check passed using SHA1 |
99 |
| - return True |
100 |
| - # The signature check using SHA1 failed. Let us try it with SHA256. |
| 94 | + sigVerifyPassed = verify(msg, sig, hexPubkey, "sha1") |
| 95 | + if sigVerifyPassed: |
| 96 | + # The signature check passed using SHA1 |
| 97 | + return True |
| 98 | + # The signature check using SHA1 failed. Let us try it with SHA256. |
| 99 | + return verify(msg, sig, hexPubkey, "sha256") |
| 100 | + |
101 | 101 | try:
|
102 | 102 | return makePubCryptor(hexPubkey).verify(
|
103 |
| - sig, msg, digest_alg=OpenSSL.EVP_sha256) |
| 103 | + sig, msg, digest_alg=_choose_digest_alg(digestAlg)) |
104 | 104 | except:
|
105 | 105 | return False
|
106 | 106 |
|
|
0 commit comments