|
| 1 | +# Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | +# |
| 4 | +# The Universal Permissive License (UPL), Version 1.0 |
| 5 | +# |
| 6 | +# Subject to the condition set forth below, permission is hereby granted to any |
| 7 | +# person obtaining a copy of this software, associated documentation and/or |
| 8 | +# data (collectively the "Software"), free of charge and under any and all |
| 9 | +# copyright rights in the Software, and any and all patent rights owned or |
| 10 | +# freely licensable by each licensor hereunder covering either (i) the |
| 11 | +# unmodified Software as contributed to or provided by such licensor, or (ii) |
| 12 | +# the Larger Works (as defined below), to deal in both |
| 13 | +# |
| 14 | +# (a) the Software, and |
| 15 | +# |
| 16 | +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 17 | +# one is included with the Software each a "Larger Work" to which the Software |
| 18 | +# is contributed by such licensors), |
| 19 | +# |
| 20 | +# without restriction, including without limitation the rights to copy, create |
| 21 | +# derivative works of, display, perform, and distribute the Software and make, |
| 22 | +# use, sell, offer for sale, import, export, have made, and have sold the |
| 23 | +# Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 24 | +# either these or other terms. |
| 25 | +# |
| 26 | +# This license is subject to the following condition: |
| 27 | +# |
| 28 | +# The above copyright notice and either this complete permission notice or at a |
| 29 | +# minimum a reference to the UPL must be included in all copies or substantial |
| 30 | +# portions of the Software. |
| 31 | +# |
| 32 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 33 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 34 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 35 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 36 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 37 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 38 | +# SOFTWARE. |
| 39 | + |
| 40 | +import hashlib |
| 41 | +import hmac |
| 42 | +import unittest |
| 43 | + |
| 44 | + |
| 45 | +class HashlibTest(unittest.TestCase): |
| 46 | + |
| 47 | + def test_messagedigest_update_after_digest(self): |
| 48 | + sha1 = hashlib.sha1() |
| 49 | + sha1.update(b'a') |
| 50 | + self.assertEqual('86f7e437faa5a7fce15d1ddcb9eaeaea377667b8', sha1.hexdigest()) |
| 51 | + sha1.update(b'b') |
| 52 | + self.assertEqual('da23614e02469a0d7c7bd1bdab5c9c474b1904dc', sha1.hexdigest()) |
| 53 | + |
| 54 | + def test_mac_update_after_digest(self): |
| 55 | + hm = hmac.new(b'key', digestmod=hashlib.sha256) |
| 56 | + hm.update(b'a') |
| 57 | + self.assertEqual('780c3db4ce3de5b9e55816fba98f590631d96c075271b26976238d5f4444219b', hm.hexdigest()) |
| 58 | + hm.update(b'b') |
| 59 | + self.assertEqual('5c1c0c948cbef5ad7d191e46e5901fa0395f85f694fa4633f665a1a637017b65', hm.hexdigest()) |
| 60 | + |
| 61 | + def test_messagedigest_buffer_length_in_constructor(self): |
| 62 | + sha1 = hashlib.sha1(self._get_buffer()) |
| 63 | + self.assertEqual('86f7e437faa5a7fce15d1ddcb9eaeaea377667b8', sha1.hexdigest()) |
| 64 | + |
| 65 | + def test_messagedigest_buffer_length_in_update(self): |
| 66 | + sha1 = hashlib.sha1() |
| 67 | + sha1.update(self._get_buffer()) |
| 68 | + self.assertEqual('86f7e437faa5a7fce15d1ddcb9eaeaea377667b8', sha1.hexdigest()) |
| 69 | + |
| 70 | + def test_mac_buffer_length_in_constructor(self): |
| 71 | + hm = hmac.new(b'key', self._get_buffer(), digestmod=hashlib.sha256) |
| 72 | + self.assertEqual('780c3db4ce3de5b9e55816fba98f590631d96c075271b26976238d5f4444219b', hm.hexdigest()) |
| 73 | + |
| 74 | + def test_mac_buffer_length_in_update(self): |
| 75 | + hm = hmac.new(b'key', digestmod=hashlib.sha256) |
| 76 | + hm.update(self._get_buffer()) |
| 77 | + self.assertEqual('780c3db4ce3de5b9e55816fba98f590631d96c075271b26976238d5f4444219b', hm.hexdigest()) |
| 78 | + |
| 79 | + def test_mac_key_length(self): |
| 80 | + hm = hmac.new(self._get_buffer(), b'data', digestmod=hashlib.sha256) |
| 81 | + self.assertEqual('c449f6626bf7f997cda786d07895f086c2fa18eab25b1c08c4de66a5d46a2a08', hm.hexdigest()) |
| 82 | + |
| 83 | + @staticmethod |
| 84 | + def _get_buffer(): |
| 85 | + ba = bytearray(b'ab') |
| 86 | + ba.remove(ord('b')) |
| 87 | + # The capacity of ba should now be different from its length. |
| 88 | + # Also, the rest of the buffer is not filled with binary zeroes - this is important for |
| 89 | + # test_mac_key_length because according to RFC 2104, the key is padded with zeroes anyway. |
| 90 | + # So providing a buffer with capacity > length, but with zero padding as the key argument to HMAC |
| 91 | + # would not trigger the bug. |
| 92 | + return ba |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + unittest.main() |
0 commit comments