Hui Peng | d885d45 | 2023-12-14 22:21:13 -0800 | [diff] [blame] | 1 | # Copyright 2023 Google LLC |
| 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 | |
| 15 | import click |
| 16 | from bumble.colors import color |
| 17 | from bumble.hci import Address |
| 18 | from bumble.helpers import generate_irk, verify_rpa_with_irk |
| 19 | |
| 20 | |
| 21 | @click.group() |
| 22 | def cli(): |
| 23 | ''' |
| 24 | This is a tool for generating IRK, RPA, |
| 25 | and verifying IRK/RPA pairs |
| 26 | ''' |
| 27 | |
| 28 | |
| 29 | @click.command() |
| 30 | def gen_irk() -> None: |
| 31 | print(generate_irk().hex()) |
| 32 | |
| 33 | |
| 34 | @click.command() |
| 35 | @click.argument("irk", type=str) |
| 36 | def gen_rpa(irk: str) -> None: |
| 37 | irk_bytes = bytes.fromhex(irk) |
| 38 | rpa = Address.generate_private_address(irk_bytes) |
| 39 | print(rpa.to_string(with_type_qualifier=False)) |
| 40 | |
| 41 | |
| 42 | @click.command() |
| 43 | @click.argument("irk", type=str) |
| 44 | @click.argument("rpa", type=str) |
| 45 | def verify_rpa(irk: str, rpa: str) -> None: |
| 46 | address = Address(rpa) |
| 47 | irk_bytes = bytes.fromhex(irk) |
| 48 | if verify_rpa_with_irk(address, irk_bytes): |
| 49 | print(color("Verified", "green")) |
| 50 | else: |
| 51 | print(color("Not Verified", "red")) |
| 52 | |
| 53 | |
| 54 | def main(): |
| 55 | cli.add_command(gen_irk) |
| 56 | cli.add_command(gen_rpa) |
| 57 | cli.add_command(verify_rpa) |
| 58 | cli() |
| 59 | |
| 60 | |
| 61 | # ----------------------------------------------------------------------------- |
| 62 | if __name__ == '__main__': |
| 63 | main() |