| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
import Crypto.Util.number |
| 32 |
from Crypto.Util.number import ceil_div, bytes_to_long, long_to_bytes |
| 33 |
from Crypto.Util.asn1 import DerSequence, DerNull, DerOctetString, DerObjectId |
| 34 |
|
| 35 |
class PKCS115_SigScheme: |
| 36 |
"""A signature object for ``RSASSA-PKCS1-v1_5``. |
| 37 |
Do not instantiate directly. |
| 38 |
Use :func:`Crypto.Signature.pkcs1_15.new`. |
| 39 |
""" |
| 40 |
|
| 41 |
def __init__(self, rsa_key): |
| 42 |
"""Initialize this PKCS#1 v1.5 signature scheme object. |
| 43 |
|
| 44 |
:Parameters: |
| 45 |
rsa_key : an RSA key object |
| 46 |
Creation of signatures is only possible if this is a *private* |
| 47 |
RSA key. Verification of signatures is always possible. |
| 48 |
""" |
| 49 |
self._key = rsa_key |
| 50 |
|
| 51 |
def can_sign(self): |
| 52 |
"""Return ``True`` if this object can be used to sign messages.""" |
| 53 |
return self._key.has_private() |
| 54 |
|
| 55 |
def sign(self, msg_hash): |
| 56 |
"""Create the PKCS#1 v1.5 signature of a message. |
| 57 |
|
| 58 |
This function is also called ``RSASSA-PKCS1-V1_5-SIGN`` and |
| 59 |
it is specified in |
| 60 |
`section 8.2.1 of RFC8017 <https://tools.ietf.org/html/rfc8017#page-36>`_. |
| 61 |
|
| 62 |
:parameter msg_hash: |
| 63 |
This is an object from the :mod:`Crypto.Hash` package. |
| 64 |
It has been used to digest the message to sign. |
| 65 |
:type msg_hash: hash object |
| 66 |
|
| 67 |
:return: the signature encoded as a *byte string*. |
| 68 |
:raise ValueError: if the RSA key is not long enough for the given hash algorithm. |
| 69 |
:raise TypeError: if the RSA key has no private half. |
| 70 |
""" |
| 71 |
|
| 72 |
|
| 73 |
modBits = Crypto.Util.number.size(self._key.n) |
| 74 |
k = ceil_div(modBits,8) |
| 75 |
|
| 76 |
|
| 77 |
em = _EMSA_PKCS1_V1_5_ENCODE(msg_hash, k) |
| 78 |
|
| 79 |
em_int = bytes_to_long(em) |
| 80 |
|
| 81 |
signature = self._key._decrypt_to_bytes(em_int) |
| 82 |
|
| 83 |
if em_int != pow(bytes_to_long(signature), self._key.e, self._key.n): |
| 84 |
raise ValueError("Fault detected in RSA private key operation") |
| 85 |
return signature |
| 86 |
|
| 87 |
def verify(self, msg_hash, signature): |
| 88 |
"""Check if the PKCS#1 v1.5 signature over a message is valid. |
| 89 |
|
| 90 |
This function is also called ``RSASSA-PKCS1-V1_5-VERIFY`` and |
| 91 |
it is specified in |
| 92 |
`section 8.2.2 of RFC8037 <https://tools.ietf.org/html/rfc8017#page-37>`_. |
| 93 |
|
| 94 |
:parameter msg_hash: |
| 95 |
The hash that was carried out over the message. This is an object |
| 96 |
belonging to the :mod:`Crypto.Hash` module. |
| 97 |
:type parameter: hash object |
| 98 |
|
| 99 |
:parameter signature: |
| 100 |
The signature that needs to be validated. |
| 101 |
:type signature: byte string |
| 102 |
|
| 103 |
:raise ValueError: if the signature is not valid. |
| 104 |
""" |
| 105 |
|
| 106 |
|
| 107 |
modBits = Crypto.Util.number.size(self._key.n) |
| 108 |
k = ceil_div(modBits, 8) |
| 109 |
|
| 110 |
|
| 111 |
if len(signature) != k: |
| 112 |
raise ValueError("Invalid signature") |
| 113 |
|
| 114 |
signature_int = bytes_to_long(signature) |
| 115 |
|
| 116 |
em_int = self._key._encrypt(signature_int) |
| 117 |
|
| 118 |
em1 = long_to_bytes(em_int, k) |
| 119 |
|
| 120 |
try: |
| 121 |
possible_em1 = [ _EMSA_PKCS1_V1_5_ENCODE(msg_hash, k, True) ] |
| 122 |
|
| 123 |
|
| 124 |
try: |
| 125 |
algorithm_is_md = msg_hash.oid.startswith('1.2.840.113549.2.') |
| 126 |
except AttributeError: |
| 127 |
algorithm_is_md = False |
| 128 |
if not algorithm_is_md: |
| 129 |
possible_em1.append(_EMSA_PKCS1_V1_5_ENCODE(msg_hash, k, False)) |
| 130 |
except ValueError: |
| 131 |
raise ValueError("Invalid signature") |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if em1 not in possible_em1: |
| 138 |
raise ValueError("Invalid signature") |
| 139 |
pass |
| 140 |
|
| 141 |
|
| 142 |
def _EMSA_PKCS1_V1_5_ENCODE(msg_hash, emLen, with_hash_parameters=True): |
| 143 |
""" |
| 144 |
Implement the ``EMSA-PKCS1-V1_5-ENCODE`` function, as defined |
| 145 |
in PKCS#1 v2.1 (RFC3447, 9.2). |
| 146 |
|
| 147 |
``_EMSA-PKCS1-V1_5-ENCODE`` actually accepts the message ``M`` as input, |
| 148 |
and hash it internally. Here, we expect that the message has already |
| 149 |
been hashed instead. |
| 150 |
|
| 151 |
:Parameters: |
| 152 |
msg_hash : hash object |
| 153 |
The hash object that holds the digest of the message being signed. |
| 154 |
emLen : int |
| 155 |
The length the final encoding must have, in bytes. |
| 156 |
with_hash_parameters : bool |
| 157 |
If True (default), include NULL parameters for the hash |
| 158 |
algorithm in the ``digestAlgorithm`` SEQUENCE. |
| 159 |
|
| 160 |
:attention: the early standard (RFC2313) stated that ``DigestInfo`` |
| 161 |
had to be BER-encoded. This means that old signatures |
| 162 |
might have length tags in indefinite form, which |
| 163 |
is not supported in DER. Such encoding cannot be |
| 164 |
reproduced by this function. |
| 165 |
|
| 166 |
:Return: An ``emLen`` byte long string that encodes the hash. |
| 167 |
""" |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
digestAlgo = DerSequence([ DerObjectId(msg_hash.oid).encode() ]) |
| 193 |
|
| 194 |
if with_hash_parameters: |
| 195 |
digestAlgo.append(DerNull().encode()) |
| 196 |
|
| 197 |
digest = DerOctetString(msg_hash.digest()) |
| 198 |
digestInfo = DerSequence([ |
| 199 |
digestAlgo.encode(), |
| 200 |
digest.encode() |
| 201 |
]).encode() |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
if emLen<len(digestInfo)+11: |
| 206 |
raise TypeError("DigestInfo is too long for this RSA key (%d bytes)." % len(digestInfo)) |
| 207 |
PS = b'\xFF' * (emLen - len(digestInfo) - 3) |
| 208 |
return b'\x00\x01' + PS + b'\x00' + digestInfo |
| 209 |
|
| 210 |
def new(rsa_key): |
| 211 |
"""Create a signature object for creating |
| 212 |
or verifying PKCS#1 v1.5 signatures. |
| 213 |
|
| 214 |
:parameter rsa_key: |
| 215 |
The RSA key to use for signing or verifying the message. |
| 216 |
This is a :class:`Crypto.PublicKey.RSA` object. |
| 217 |
Signing is only possible when ``rsa_key`` is a **private** RSA key. |
| 218 |
:type rsa_key: RSA object |
| 219 |
|
| 220 |
:return: a :class:`PKCS115_SigScheme` signature object |
| 221 |
""" |
| 222 |
return PKCS115_SigScheme(rsa_key) |
| 223 |
|