From 093e04a65036da41009b1bf1b04b4c634ec96587 Mon Sep 17 00:00:00 2001 From: DavidKorczynski Date: Wed, 8 Feb 2023 10:54:44 +0000 Subject: [PATCH] python-ecdsa: initial integration (#9637) Signed-off-by: David Korczynski Signed-off-by: David Korczynski --- projects/python-ecdsa/Dockerfile | 19 ++++++ projects/python-ecdsa/build.sh | 21 ++++++ projects/python-ecdsa/fuzz_ecdsa.py | 63 ++++++++++++++++++ projects/python-ecdsa/fuzz_eddsa.py | 60 +++++++++++++++++ projects/python-ecdsa/fuzz_keys.py | 99 +++++++++++++++++++++++++++++ projects/python-ecdsa/project.yaml | 10 +++ 6 files changed, 272 insertions(+) create mode 100644 projects/python-ecdsa/Dockerfile create mode 100644 projects/python-ecdsa/build.sh create mode 100644 projects/python-ecdsa/fuzz_ecdsa.py create mode 100644 projects/python-ecdsa/fuzz_eddsa.py create mode 100644 projects/python-ecdsa/fuzz_keys.py create mode 100644 projects/python-ecdsa/project.yaml diff --git a/projects/python-ecdsa/Dockerfile b/projects/python-ecdsa/Dockerfile new file mode 100644 index 000000000..51f32d642 --- /dev/null +++ b/projects/python-ecdsa/Dockerfile @@ -0,0 +1,19 @@ +#!/usr/bin/python3 +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +FROM gcr.io/oss-fuzz-base/base-builder-python +RUN pip3 install --upgrade pip +RUN git clone https://github.com/tlsfuzzer/python-ecdsa python-ecdsa +COPY *.sh *py $SRC/ +WORKDIR $SRC/python-ecdsa diff --git a/projects/python-ecdsa/build.sh b/projects/python-ecdsa/build.sh new file mode 100644 index 000000000..dd11fafc6 --- /dev/null +++ b/projects/python-ecdsa/build.sh @@ -0,0 +1,21 @@ +#!/bin/bash -eu +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +################################################################################ +pip3 install . +# Build fuzzers in $OUT. +for fuzzer in $(find $SRC -name 'fuzz_*.py'); do + compile_python_fuzzer $fuzzer +done diff --git a/projects/python-ecdsa/fuzz_ecdsa.py b/projects/python-ecdsa/fuzz_ecdsa.py new file mode 100644 index 000000000..8181d3809 --- /dev/null +++ b/projects/python-ecdsa/fuzz_ecdsa.py @@ -0,0 +1,63 @@ +#!/usr/bin/python3 +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import sys +import atheris +import hashlib +import binascii +import ecdsa + + +def target1(fdp): + d = fdp.ConsumeIntInRange( + 1, 9999999999999999999999999999999999999999999999999999999999) + k = fdp.ConsumeIntInRange( + 1, 9999999999999999999999999999999999999999999999999999999999) + msg = fdp.ConsumeIntInRange( + 1, 9999999999999999999999999999999999999999999999999999999999) + + Q = d * ecdsa.ecdsa.generator_192 + R = k * ecdsa.ecdsa.generator_192 + + pubk = ecdsa.ecdsa.Public_key(ecdsa.ecdsa.generator_192, + ecdsa.ecdsa.generator_192 * d) + privk = ecdsa.ecdsa.Private_key(pubk, d) + sig = privk.sign(msg, k) + pubk.verifies(msg, sig) + pubk.verifies(msg - 1, sig) + + +def target2(fdp): + ecdsa._sha3.shake_256(fdp.ConsumeBytes(sys.maxsize), + fdp.ConsumeIntInRange(1, 64)) + + +def TestOneInput(data): + fdp = atheris.FuzzedDataProvider(data) + targets = [ + target1, + target2, + ] + target = fdp.PickValueInList(targets) + target(fdp) + + +def main(): + atheris.instrument_all() + atheris.Setup(sys.argv, TestOneInput) + atheris.Fuzz() + + +if __name__ == "__main__": + main() diff --git a/projects/python-ecdsa/fuzz_eddsa.py b/projects/python-ecdsa/fuzz_eddsa.py new file mode 100644 index 000000000..ef2e58b7a --- /dev/null +++ b/projects/python-ecdsa/fuzz_eddsa.py @@ -0,0 +1,60 @@ +#!/usr/bin/python3 +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import sys +import atheris +import hashlib +import binascii + +import ecdsa + + +def target1(fdp): + a = ecdsa.ellipticcurve.PointEdwards(ecdsa.eddsa.curve_ed25519, + fdp.ConsumeIntInRange(0, 10), + fdp.ConsumeIntInRange(0, 10), + fdp.ConsumeIntInRange(0, 10), + fdp.ConsumeIntInRange(0, 10)) + z = a.double() + + +def target2(fdp): + try: + key = ecdsa.eddsa.PublicKey(ecdsa.eddsa.generator_ed25519, + fdp.ConsumeBytes(sys.maxsize)) + key.verify(fdp.ConsumeBytes(sys.maxsize), fdp.ConsumeBytes(sys.maxsize)) + except ValueError: + pass + except ecdsa.errors.MalformedPointError: + pass + + +def TestOneInput(data): + fdp = atheris.FuzzedDataProvider(data) + targets = [ + target1, + target2, + ] + target = fdp.PickValueInList(targets) + target(fdp) + + +def main(): + atheris.instrument_all() + atheris.Setup(sys.argv, TestOneInput) + atheris.Fuzz() + + +if __name__ == "__main__": + main() diff --git a/projects/python-ecdsa/fuzz_keys.py b/projects/python-ecdsa/fuzz_keys.py new file mode 100644 index 000000000..cd85635c4 --- /dev/null +++ b/projects/python-ecdsa/fuzz_keys.py @@ -0,0 +1,99 @@ +#!/usr/bin/python3 +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import sys +import atheris +import hashlib +import binascii + +import ecdsa +from ecdsa.keys import VerifyingKey + + +def target1(fdp): + try: + VerifyingKey.from_der(fdp.ConsumeBytes(sys.maxsize)) + except ecdsa.der.UnexpectedDER: + pass + + +def target2(fdp): + try: + VerifyingKey.from_pem(fdp.ConsumeBytes(sys.maxsize), hashlib.sha256) + except ecdsa.der.UnexpectedDER: + pass + except binascii.Error: + pass + + +def target3(fdp): + try: + VerifyingKey.from_public_key_recovery_with_digest( + fdp.ConsumeBytes(sys.maxsize), fdp.ConsumeBytes(sys.maxsize), + ecdsa.curves.Ed25519) + except ecdsa.der.UnexpectedDER: + pass + except ValueError: + pass + + +def target4(fdp): + try: + VerifyingKey.from_string(fdp.ConsumeUnicodeNoSurrogates(sys.maxsize), + ecdsa.curves.Ed25519) + except ecdsa.keys.MalformedPointError: + pass + except ecdsa.der.UnexpectedDER: + pass + except ValueError: + pass + + +def target5(fdp): + vk_str = fdp.ConsumeUnicodeNoSurrogates(sys.maxsize) + sig = fdp.ConsumeBytes(sys.maxsize) + data = fdp.ConsumeBytes(sys.maxsize) + try: + vk = VerifyingKey.from_pem(vk_str) + vk.verify(sig, data) + except ecdsa.keys.MalformedPointError: + pass + except ecdsa.der.UnexpectedDER: + pass + except ValueError: + pass + + +def TestOneInput(data): + fdp = atheris.FuzzedDataProvider(data) + targets = [ + target1, + target2, + target3, + target4, + target5, + ] + + target = fdp.PickValueInList(targets) + target(fdp) + + +def main(): + atheris.instrument_all() + atheris.Setup(sys.argv, TestOneInput) + atheris.Fuzz() + + +if __name__ == "__main__": + main() diff --git a/projects/python-ecdsa/project.yaml b/projects/python-ecdsa/project.yaml new file mode 100644 index 000000000..c8788a409 --- /dev/null +++ b/projects/python-ecdsa/project.yaml @@ -0,0 +1,10 @@ +homepage: https://github.com/tlsfuzzer/python-ecdsa +main_repo: https://github.com/tlsfuzzer/python-ecdsa +language: python +fuzzing_engines: +- libfuzzer +sanitizers: +- address +- undefined +vendor_ccs: +- david@adalogics.com