Nitrokey/nitrokey-encryption-tool

GitHub: Nitrokey/nitrokey-encryption-tool

基于 PKCS#11 接口的命令行工具,利用硬件令牌上的 RSA 密钥实现混合加密,保护用户数据安全。

Stars: 33 | Forks: 4

# Nitrokey 加密工具 加密工具是一个命令行界面应用程序,它使用设备上的 RSA 密钥(通过 OpenSC 和 PKCS#11)来加密/解密 AES 密钥,进而对用户数据执行操作。 ## 系统要求 该工具的运行要求如下: - Linux(库路径已针对 Ubuntu 16.10 调整), - Python 3(含 PIP), - OpenSC v0.17(可能不适用于较低版本), - `requirements.txt` 中的 Python 库。 ### 安装说明 #### Python 要安装所需的 Python 3 库,请执行以下命令: ``` pip3 install -r requirements.txt --user ``` 这将在用户级目录中本地安装所需的包(与全局、操作系统级安装相对)。 也可以通过 `virtualenv`([指南](http://docs.python-guide.org/en/latest/dev/virtualenvs/#lower-level-virtualenv))或最新的 `pipenv`([指南](http://docs.python-guide.org/en/latest/dev/virtualenvs/#installing-pipenv))工具在独立环境中安装 Python 依赖项。 #### OpenSC OpenSC 编译手册(如果系统包存储库中没有提供,则需要编译)应可在项目主站点上找到。 Linux 版本请点击[这里](https://github.com/OpenSC/OpenSC/wiki/Compiling-and-Installing-on-Unix-flavors)。 ## 使用方法 ### 准备工作 在与该工具一起使用智能卡之前,请确保至少在一个插槽中生成了 RSA 密钥。 如果尚未生成,请先生成。该工具支持 2048 或 4096 位的 RSA 密钥。 请运行该工具以查看是否有可用的 PGP 密钥。 注意:通常在 OpenPGP 智能卡上只能使用 `Encryption key` 插槽(`key 0`): ``` # Inserted custom OpenPGP v2.2 Smart Card $ python3 encryption_tool.py Namespace(cmd='status', pin=None, token=0, verbose=False) Listing tokens (2): Token 0. "User PIN (OpenPGP card)" Please provide user PIN for the device (will not be echoed): Investigating device 0 Public RSA keys found: 2 Private RSA keys found: 2 Test key 0: Decrypting data with the device / done Encryption/decryption test result with key 0: success Test key 1: Decrypting data with the device | done Encryption/decryption test result with key 1: failure Token 1. "User PIN (sig) (OpenPGP card)" Investigating device 1 Public RSA keys found: 1 Private RSA keys found: 1 Test key 0: Decrypting data with the device \ done Encryption/decryption test result with key 0: failure Working configurations: Token 0 ("User PIN (OpenPGP card)"): key 0 ``` #### Nitrokey HSM (SmartCard-HSM) 该工具支持使用 Nitrokey HSM 生成密钥。 命令 `--create_keys` 将按如下方式执行: ``` python3 encryption_tool.py --token 0 create_keys "key label" ``` 这将创建一个 2048 位的 RSA 密钥对。 `--token` 开关允许选择要在哪个设备上工作。 要列出所有已连接的设备,请使用 `list_tokens` 命令: ``` # OpenPGP card inserted $ python3 encryption_tool.py list_tokens Namespace(cmd='list_tokens', pin=None, token=0) Listing tokens (2): 0. User PIN (OpenPGP card) 1. User PIN (sig) (OpenPGP card) ``` #### Nitrokey Pro/Storage (OpenPGP v2.1+) 遗憾的是,加密工具无法在 OpenPGP 卡上生成密钥。请按照以下代码片段使用 GPG 生成: ``` $ gpg --card-edit # 在 GPG 提示符内 > admin > generate # 请填写详细信息,如需要可以填写虚假信息 > (...) # 完成后可以使用以下命令验证密钥生成: $ gpg --card-status ``` ### 命令 可用命令显示在工具的帮助界面中: ``` $ python3 encryption_tool.py --help usage: encryption_tool.py [-h] [--token TOKEN] [--pin PIN] {decrypt,encrypt,list_keys,list_tokens,status,create_keys} ... Encrypt/decrypt or generate key through PKCS#11 positional arguments: {decrypt,encrypt,list_keys,list_tokens,status,create_keys} Action to be run by the tool. Each action has its own help (use --help). Description: decrypt Decrypt file with private key from the device encrypt Encrypt file with public key from the device list_keys List public keys saved on token (no PIN needed) list_tokens List available tokens status Show what RSA keys are available and perform simple encryption/decryption test. Default action. create_keys Create RSA keys for use in tool. Warning: it will not work with OpenSC in version 0.16. Only Smartcard-HSM is supported. optional arguments: -h, --help show this help message and exit --token TOKEN Token number to work on. Use list_tokens to see what tokens are available. (default: 0) --pin PIN User PIN. Will be asked when needed if not provided through switch. ``` 有关命令参数和详细的命令帮助,请在给定命令后添加 `--help` 开关,例如: ``` $ python3 encryption_tool.py decrypt --help usage: encryption_tool.py decrypt [-h] input output keyid positional arguments: input Input file output Output file keyid Key pair number to work on. Use list_keys to see what key pairs are available optional arguments: -h, --help show this help message and exit ``` ### 运行 在运行该工具之前,请确保 `scdaemon` 未运行(特别是在运行过 `gpg --card-status` 或 `--card-edit` 之后),并且 `pcscd` 进程已启动,如下所示: ``` sudo killall scdaemon pcscd sudo pcscd ``` 否则,该工具可能无法获取设备使用权,并将显示以下消息: ``` Traceback (most recent call last): File "encryption_tool.py", line 437, in with get_session(tokenID=args.token, skipPin=True) as session: File "encryption_tool.py", line 61, in get_session raise RuntimeError(error_message) RuntimeError: Cannot open token with ID 0. See if it is inserted or check if the Token ID is correct. Make sure pcscd (sudo pcscd) is running. Kill and run it again. ``` 简单地重新插入 USB 端口也可能解决问题。 ### 测试脚本 提供了一个名为 `encryption_tool_test.sh` 的简单测试脚本,用于测试整体的 RSA+AES 加密/解密操作。 请运行 `sudo bash encryption_tool_test.sh`。它将运行该工具,并在加密->解密操作后将其输出与输入文件(即脚本本身)进行比较。需要 `sudo` 来终止并重启 `pcscd` 和 `scdaemon` 服务。 它接受一个可选的文件路径作为唯一参数, ``` sudo bash encryption_tool_test.sh [file_path] ``` 在这种情况下,它将使用选定的文件作为数据源进行测试。 最后,除了 `diff` 之外,还会向用户展示输入和输出文件的 `wc -l` 和 `ls -l` 统计信息。 ## 测试环境 该工具已在以下环境测试通过: - Ubuntu 16.10, - docker 容器内(非必需), - OpenSC 0.17(从源码编译), 使用以下设备: - Nitrokey Pro/Storage (OpenPGP 2.1), - Nitrokey HSM (SmartCard-HSM), - 配合 Gemalto IDBRidge CT30 USB 智能卡读卡器的自定义 OpenPGP 2.2 卡。
标签:AES, CLI, Cloudformation, DNS 反向解析, DNS枚举, Nitrokey, OpenPGP, OpenSC, PKCS#11, Python, RSA, WiFi技术, 数据加密, 文件加密, 无后门, 智能卡, 混合加密, 硬件密钥, 网络安全, 网络安全工具, 蓝队防御, 隐私保护