Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encrypt the original SACC file before saving it to disk #58

Closed
7 tasks done
arthurmloureiro opened this issue Oct 4, 2024 · 1 comment · Fixed by #62
Closed
7 tasks done

Encrypt the original SACC file before saving it to disk #58

arthurmloureiro opened this issue Oct 4, 2024 · 1 comment · Fixed by #62
Assignees
Labels
data-vector blinding Issues related to data-vector blinding enhancement

Comments

@arthurmloureiro
Copy link
Contributor

arthurmloureiro commented Oct 4, 2024

To avoid accidental unblinding, we should encrypt the original sacc file before saving it. We should also save the encryption key in a text file and provide the user with a smokescreen function to decrypt (unblind) the original SACC.

To Do:

  • Implement encryption/decryption functions for SACC files
  • Save the key in a re-usable way
  • Implement unit tests
  • Update documentation
  • Add example of how to decrypt from the command line to the documentation.
  • Update environment.yaml with new dependencies
  • Update pyproject.toml with the new dependencies

Tagging @jablazek and @jessmuir for comments :)

@arthurmloureiro arthurmloureiro added enhancement data-vector blinding Issues related to data-vector blinding labels Oct 4, 2024
@arthurmloureiro arthurmloureiro self-assigned this Oct 4, 2024
arthurmloureiro added a commit that referenced this issue Oct 18, 2024
Related to #58

Add encryption and decryption functionalities for SACC files.

* **Encryption and Decryption**:
  - Add `generate_encryption_key`, `encrypt_data`, and `decrypt_data` methods in `src/smokescreen/datavector.py`.
  - Modify `save_concealed_datavector` method to encrypt the SACC file before saving.
  - Add `decrypt_sacc_file` function in `src/smokescreen/datavector.py`.

* **Main Function**:
  - Update `main` function in `src/smokescreen/__main__.py` to handle encryption and decryption.
  - Add `decrypt`, `encrypted_file_path`, and `encryption_key_path` arguments to the `main` function.

* **Tests**:
  - Add tests for `generate_encryption_key`, `encrypt_data`, and `decrypt_data` methods in `tests/test_datavector.py`.
  - Add tests for the modified `save_concealed_datavector` method.
  - Add tests for `decrypt_sacc_file` function.

* **Documentation**:
  - Add a section in `docs/source/usage.rst` to document the encryption and decryption functionalities.

* **Dependencies**:
  - Add `cryptography` as a dependency in `pyproject.toml` and `environment.yml`.

Needs testing if the Fernet lib actually works with sacc!

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/LSSTDESC/Smokescreen/issues/58?shareId=XXXX-XXXX-XXXX-XXXX).
@arthurmloureiro arthurmloureiro linked a pull request Oct 22, 2024 that will close this issue
@arthurmloureiro arthurmloureiro added this to the Smokescreen v1.5 milestone Nov 12, 2024
@arthurmloureiro
Copy link
Contributor Author

arthurmloureiro commented Nov 12, 2024

We can use the cryptography python package for that.

The follwing is a rough sketch that worked on a notebook:

import pylab as plt
import sacc
from cryptography.fernet import Fernet

# example sacc to encrypt
path_to_sacc = "../examples/cosmic_shear/cosmicshear_sacc.fits"

# Generate a key and save it securely
key = Fernet.generate_key()
cipher_suite = Fernet(key)

# Read and encrypt the FITS file data
with open(path_to_sacc, 'rb') as file:
    file_data = file.read()
encrypted_data = cipher_suite.encrypt(file_data)

# Save the encrypted data to a new file
with open('encrypted_file.fits', 'wb') as file:
    file.write(encrypted_data)

The resulting fits file cannot be read by SACC in any way until it is decrypted properly using:

# decrypting the file and testing if it got corrupted or not:
# Load the key and initialize the cipher
cipher_suite = Fernet(key)

# Read the encrypted file and decrypt it
with open('encrypted_file.fits', 'rb') as file:
    encrypted_data = file.read()
decrypted_data = cipher_suite.decrypt(encrypted_data)

# Save the decrypted data back to a FITS file
with open('decrypted_file.fits', 'wb') as file:
    file.write(decrypted_data)

We need to find a safe way to store the decryption key though.

@arthurmloureiro arthurmloureiro removed a link to a pull request Nov 12, 2024
@arthurmloureiro arthurmloureiro linked a pull request Nov 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data-vector blinding Issues related to data-vector blinding enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant