|
1 |
| -# Keycard implementation |
| 1 | +# Keycard Implementation |
2 | 2 |
|
3 |
| -## Notes |
| 3 | +## Introduction |
4 | 4 |
|
5 |
| -Understanding Keycard Capabilities: |
6 |
| -1. Key Storage: |
7 |
| -- Keycard is designed to securely store private keys, typically used for digital signatures and authenticating transactions, especially in blockchain contexts. |
| 5 | +This document outlines the process of integrating Keycard into a Java-based application for symmetrically encrypting and decrypting data, particularly focusing on file-based applications with IPFS (InterPlanetary File System) storage. |
8 | 6 |
|
9 |
| -3. Signing Operations: |
10 |
| -- Keycard can sign data using the stored private keys. This is commonly used for signing transactions in cryptocurrency applications. Instead we are using a file based application where instead of blocks of cryptocurrency being signed it is blocks of IPFS storage. |
11 |
| -- The idea would be akin to keeping private public keys for Ethereum / ERC-20. Something which is well proven and battle tested for keycard through Status. |
12 |
| -- Instead we just decrypt the data using the private keycard key. ***Though this will be different to the core of the IPFSS program as the private key is not revealed to the interface itself.*** |
| 7 | +## Developers: Criteria for Commits |
13 | 8 |
|
14 |
| -5. Encryption/Decryption: |
15 |
| -- Standard file encryption/decryption is not a primary function of Keycard. It's more oriented towards signing and transaction authentication. |
16 |
| -- Though this signing and transaction authentication can be expanded upon. Since cryptocurrency transactions are just private key signings this shouldnt be too hard to implement. |
| 9 | +Contributors should follow these guidelines to ensure consistency and security in the codebase. |
17 | 10 |
|
18 |
| -## From Stack Overflow about something similar: |
| 11 | +### Encryption Process: |
19 | 12 |
|
| 13 | +#### User Passphrase Input: |
| 14 | +- Ensure secure input handling. |
| 15 | +- Use secure GUI prompts or console inputs that don't echo the passphrase. |
20 | 16 |
|
21 |
| -> If it is available, how does it work. Will the Smart Card take a stream of encrypted bytes and then spit out a stream of unencrypted bytes? |
22 |
| -> |
23 |
| ->No, that's generally not what happens. In general smart card simply supply an RSA operation that performs raw RSA, RSA PKCS#1 or RSA OAEP decryption. The result of this operation is a relatively small amount of bytes; e.g. in the case of RSA PKCS#1 about 11 bytes less then the key size (which is the size of the modulus for RSA). If a raw RSA operation is provided then the unpadding should be performed by the off card entity. |
24 |
| -> |
25 |
| ->So what is used is a hybrid cryptosystem. Such a system uses a random, symmetric data key to encrypt the plaintext. This random data key - usually an AES key - is then encrypted by the RSA public key. The encrypted data key is stored together with the ciphertext using a container format such as CMS or Open PGP. |
26 |
| -> |
27 |
| ->Upon decryption the AES data key is first decrypted with the private key on the smart card. This for instance requires a PIN code to be entered to gain access to the private key. Once the data key is decrypted it can be used to decrypt the rest of the data. Using authenticated encryption (such as GCM) should of course be preferred. |
28 |
| -> |
29 |
| ->So the smart card only "accelerates" the private key operation. I put that between quotes as in general a mainstream CPU will be much faster than the speed of the cryptographic co-processor and the communication overhead provided by the card. The AES operations are performed off-card, and they bear the brunt of the work for any files above, say, a few KiB. |
| 17 | +#### Signing Passphrase with Keycard: |
| 18 | +- Keycards typically sign a hash of data rather than the passphrase directly. |
| 19 | +- Use the Keycard to securely generate or sign a hash of the passphrase for encryption. |
30 | 20 |
|
| 21 | +#### Key Generation and Management: |
| 22 | +- Generate a symmetric key based on the signed data, ensuring cryptographic security. |
| 23 | +- Securely handle the key in memory and clear it immediately after use. |
31 | 24 |
|
32 |
| -## HOW TO! Note from Satunix. |
33 |
| -## Data flow of symmetrically encrypting and decrypting data using JAVA Keycard |
| 25 | +#### Integration with GPG: |
| 26 | +- Pass the symmetric key to GPG securely for encryption. |
| 27 | +- Avoid using command-line arguments for key passing. |
34 | 28 |
|
35 |
| -1. Encryption: |
36 |
| -- User enters passphrase |
37 |
| -- Sign passphrase with keycard |
38 |
| -- Unique key is generated |
39 |
| -- Key used as string data and input into gpg |
40 |
| -- Remove any pointers, process logs, or files required for the data flow to prevent lifting or malware extraction. |
41 |
| -- gpg encrypts the data using that unique string |
42 |
| -- encrypted file.gpg is passed off to IPFS. |
| 29 | +#### Security Measures: |
| 30 | +- Implement measures to prevent memory dumping. |
| 31 | +- Ensure secure deletion of temporary files or logs containing sensitive data. |
| 32 | +- Use secure file handling libraries in Java. |
43 | 33 |
|
44 |
| -2. Decryption: |
45 |
| -- IPFS retrieves file via CID |
46 |
| -- Enter passphrase prompt, user enters passphrase |
47 |
| -- Sign passphrase with keycard |
48 |
| -- Unique key is generated |
49 |
| -- Key used as string to decrypt the file |
50 |
| -- Remove any pointers, process logs, or files with sensitive information. |
51 |
| -- encrypted <file.gpg> becomes <file> |
| 34 | +#### Uploading to IPFS: |
| 35 | +- Maintain the integrity and confidentiality of data during the IPFS upload process. |
| 36 | +- Securely handle IPFS interactions and manage CIDs appropriately. |
52 | 37 |
|
53 |
| -## Keycard Implementation for Encryption and Decryption |
| 38 | +### Decryption Process: |
54 | 39 |
|
55 |
| -### Utilize Keycard for Signing: |
| 40 | +#### Retrieve File from IPFS: |
| 41 | +- Ensure secure retrieval of the encrypted file from IPFS using the CID. |
56 | 42 |
|
57 |
| -- The Keycard will be used for signing data blocks, similar to how it signs cryptocurrency transactions. |
58 |
| -- For file-based applications, each block of data (or the entire file) can be signed using the Keycard's private key. |
| 43 | +#### Passphrase Handling: |
| 44 | +- Manage passphrase input securely, similar to the encryption process. |
59 | 45 |
|
60 |
| -### Generating and Storing Keys: |
| 46 | +#### Keycard Interaction: |
| 47 | +- Use the Keycard to generate or retrieve the decryption key securely. |
61 | 48 |
|
62 |
| -- Use commands like `GENERATE KEY` or `LOAD KEY` to create or load RSA keys onto the Keycard. |
63 |
| -- The generated keys can be used for signing operations. Ensure these keys are securely stored and managed on the Keycard. |
| 49 | +#### Decrypting with GPG: |
| 50 | +- Decrypt the file using GPG with the symmetric key derived from the Keycard-signed data. |
| 51 | +- Ensure secure handling of key material during decryption. |
64 | 52 |
|
65 |
| -### File Encryption and Decryption: |
| 53 | +#### Post-Decryption Security: |
| 54 | +- Manage decrypted data securely. |
| 55 | +- Clean up any sensitive remnants in memory or temporary storage. |
66 | 56 |
|
67 |
| -#### Encryption: |
68 |
| -- Encrypt files using standard encryption tools, like OpenSSL or GPG, with a public key. This public key can be the one associated with the Keycard's private key, or another keypair depending on your security design. |
69 |
| - |
70 |
| -#### Decryption: |
71 |
| -- Decryption is typically performed using the corresponding private key. However, as Keycard does not directly expose private keys for security reasons, it cannot directly decrypt data in the conventional sense. Instead, consider using Keycard for validating the signature of the encrypted data to ensure its integrity and authenticity. |
72 |
| -- Alternative Approach: If direct decryption with Keycard's private key is necessary, you may need to explore if `EXPORT KEY` can be used securely, though this might pose a security risk as exporting private keys is generally discouraged. |
73 |
| - |
74 |
| -### Signing Encrypted Files: |
75 |
| - |
76 |
| -- After encrypting a file, use the `SIGN` command to sign the encrypted file with the Keycard's private key. |
77 |
| -- This signature can be used to verify the file's integrity and authenticity upon decryption. |
| 57 | +#### Error Handling and Logging: |
| 58 | +- Implement robust error handling for Keycard interactions, GPG operations, and IPFS retrieval. |
| 59 | +- Avoid storing sensitive information in logs. |
78 | 60 |
|
79 |
| -### Verifying Signatures: |
| 61 | +### General Considerations: |
80 | 62 |
|
81 |
| -- When a file is decrypted using a standard decryption tool, use Keycard’s public key to verify the signature. This ensures that the file was indeed encrypted and signed by the owner of the Keycard. |
| 63 | +#### Cryptographic Best Practices: |
| 64 | +- Adhere to best practices for key generation, data signing, and symmetric encryption. |
82 | 65 |
|
83 |
| -### Workflow Integration: |
| 66 | +#### Code Security: |
| 67 | +- Ensure the Java code handling cryptographic operations is secure against common vulnerabilities. |
84 | 68 |
|
85 |
| -- Implement a workflow in your application where every file to be uploaded to IPFS is first encrypted, then signed using the Keycard. |
86 |
| -- For downloading, after retrieving the file from IPFS, the application should first verify the signature using Keycard's public key, then decrypt it using the corresponding private key (not stored on Keycard). |
| 69 | +#### User Feedback: |
| 70 | +- Provide clear and user-friendly feedback for operations, especially for errors or successful operations. |
87 | 71 |
|
88 |
| -### Security Protocols: |
| 72 | +#### Documentation and Testing: |
| 73 | +- Document the process clearly, including prerequisites and configurations. |
| 74 | +- Thoroughly test the application for reliability and security. |
89 | 75 |
|
90 |
| -- Ensure all interactions with Keycard, like PIN verification (`VERIFY PIN`), opening secure channels (`OPEN SECURE CHANNEL`), and mutual authentication (`MUTUALLY AUTHENTICATE`), are handled securely in your application. |
| 76 | +## Understanding Keycard Capabilities |
91 | 77 |
|
92 |
| -### Conclusion |
| 78 | +### Key Storage: |
| 79 | +- Keycard is designed for secure storage of private keys, commonly used in digital signatures and transaction authentication. |
93 | 80 |
|
94 |
| -While Keycard provides robust capabilities for signing and key management, its direct use in standard file encryption/decryption workflows is limited due to its design as a secure element for transaction signing and authentication. Your implementation can leverage Keycard for signing encrypted files, adding a layer of security by ensuring data integrity and origin verification. However, for conventional encryption and decryption of files, reliance on external tools and methodologies that complement Keycard's capabilities would be necessary. |
| 81 | +### Signing Operations: |
| 82 | +- Keycard can sign data blocks using stored private keys, adaptable for file-based applications and IPFS storage. |
95 | 83 |
|
| 84 | +### Encryption/Decryption: |
| 85 | +- While not a primary function, Keycard's signing capabilities can be adapted for encryption/decryption processes in file-based applications. |
96 | 86 |
|
| 87 | +## Implementation Notes |
97 | 88 |
|
| 89 | +### Data Flow for Encryption and Decryption Using Java Keycard |
98 | 90 |
|
99 |
| -## Further Reading for Developers |
100 |
| - |
101 |
| -Most promising and recomended: **The Official Go API** |
102 |
| - |
103 |
| -- [Keycard for Go applications](https://github.com/status-im/keycard-go/) |
| 91 | +#### Encryption: |
| 92 | +- User enters a passphrase. |
| 93 | +- Sign the passphrase with the Keycard. |
| 94 | +- Generate a unique key based on the signed passphrase. |
| 95 | +- Use the key as string data for GPG encryption. |
| 96 | +- Remove sensitive process logs or files to prevent data exposure. |
| 97 | +- Encrypt the data with GPG and pass the encrypted file to IPFS. |
104 | 98 |
|
105 |
| -Might be the easiest to implement, just have to make sure your can encrypt and decrypt files using the keycard CLI. |
| 99 | +#### Decryption: |
| 100 | +- Retrieve the file from IPFS using its CID. |
| 101 | +- User re-enters the passphrase. |
| 102 | +- Sign the passphrase with the Keycard to regenerate the decryption key. |
| 103 | +- Use the key to decrypt the file with GPG. |
| 104 | +- Ensure removal of sensitive logs or temporary files. |
106 | 105 |
|
107 |
| -- [Keycard for CLI (can pass sysargs with Go to keycard CLI)](https://github.com/status-im/keycard-cli) |
| 106 | +## Further Reading for Developers |
108 | 107 |
|
109 |
| -Could use APDU, whatever tf that means. |
| 108 | +### Recommended Resources: |
110 | 109 |
|
111 |
| -- [ Keycard APDU API ](https://keycard.tech/docs/apdu/)https://keycard.tech/docs/apdu/ |
| 110 | +- [Official Go API for Keycard](https://github.com/status-im/keycard-go/) |
| 111 | +- [Keycard CLI](https://github.com/status-im/keycard-cli) |
| 112 | +- [Keycard APDU API](https://keycard.tech/docs/apdu/) |
| 113 | +- [Localhost Web3 Application for Keycard](https://keycard.tech/docs/web3.html) |
112 | 114 |
|
113 |
| -Or we could use the WEB3 Application and push it through a localhost web UI (One of my favs for the future) |
| 115 | +Contributors are encouraged to explore these resources for a deeper understanding of Keycard's capabilities and integration methods. |
114 | 116 |
|
115 |
| -- [Localhost web3 application](https://keycard.tech/docs/web3.html)https://keycard.tech/docs/web3.html |
| 117 | +## Conclusion |
116 | 118 |
|
| 119 | +By leveraging Keycard's unique capabilities for signing and transaction authentication, developers can create a secure and efficient system for encrypting and decrypting data, particularly for applications involving IPFS storage. |
117 | 120 |
|
0 commit comments