Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

101 rinda
3.4 KiB

  1. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # https://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Functions for PKCS#1 version 2 encryption and signing
  15. This module implements certain functionality from PKCS#1 version 2. Main
  16. documentation is RFC 2437: https://tools.ietf.org/html/rfc2437
  17. """
  18. from rsa import (
  19. common,
  20. pkcs1,
  21. transform,
  22. )
  23. def mgf1(seed: bytes, length: int, hasher: str = "SHA-1") -> bytes:
  24. """
  25. MGF1 is a Mask Generation Function based on a hash function.
  26. A mask generation function takes an octet string of variable length and a
  27. desired output length as input, and outputs an octet string of the desired
  28. length. The plaintext-awareness of RSAES-OAEP relies on the random nature of
  29. the output of the mask generation function, which in turn relies on the
  30. random nature of the underlying hash.
  31. :param bytes seed: seed from which mask is generated, an octet string
  32. :param int length: intended length in octets of the mask, at most 2^32(hLen)
  33. :param str hasher: hash function (hLen denotes the length in octets of the hash
  34. function output)
  35. :return: mask, an octet string of length `length`
  36. :rtype: bytes
  37. :raise OverflowError: when `length` is too large for the specified `hasher`
  38. :raise ValueError: when specified `hasher` is invalid
  39. """
  40. try:
  41. hash_length = pkcs1.HASH_METHODS[hasher]().digest_size
  42. except KeyError as ex:
  43. raise ValueError(
  44. "Invalid `hasher` specified. Please select one of: {hash_list}".format(
  45. hash_list=", ".join(sorted(pkcs1.HASH_METHODS.keys()))
  46. )
  47. ) from ex
  48. # If l > 2^32(hLen), output "mask too long" and stop.
  49. if length > (2 ** 32 * hash_length):
  50. raise OverflowError(
  51. "Desired length should be at most 2**32 times the hasher's output "
  52. "length ({hash_length} for {hasher} function)".format(
  53. hash_length=hash_length,
  54. hasher=hasher,
  55. )
  56. )
  57. # Looping `counter` from 0 to ceil(l / hLen)-1, build `output` based on the
  58. # hashes formed by (`seed` + C), being `C` an octet string of length 4
  59. # generated by converting `counter` with the primitive I2OSP
  60. output = b"".join(
  61. pkcs1.compute_hash(
  62. seed + transform.int2bytes(counter, fill_size=4),
  63. method_name=hasher,
  64. )
  65. for counter in range(common.ceil_div(length, hash_length) + 1)
  66. )
  67. # Output the leading `length` octets of `output` as the octet string mask.
  68. return output[:length]
  69. __all__ = [
  70. "mgf1",
  71. ]
  72. if __name__ == "__main__":
  73. print("Running doctests 1000x or until failure")
  74. import doctest
  75. for count in range(1000):
  76. (failures, tests) = doctest.testmod()
  77. if failures:
  78. break
  79. if count % 100 == 0 and count:
  80. print("%i times" % count)
  81. print("Doctests done")