{"id":17686543,"url":"https://github.com/pheki/xts-mode","last_synced_at":"2025-05-07T09:47:04.651Z","repository":{"id":43663561,"uuid":"260125962","full_name":"pheki/xts-mode","owner":"pheki","description":"XTS block mode implementation in rust","archived":false,"fork":false,"pushed_at":"2024-07-26T03:43:14.000Z","size":179,"stargazers_count":12,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T05:37:19.675Z","etag":null,"topics":["cryptography","hacktoberfest","rust","xts"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pheki.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-30T05:57:17.000Z","updated_at":"2025-02-07T08:06:15.000Z","dependencies_parsed_at":"2023-12-08T05:27:35.445Z","dependency_job_id":"3829378e-e039-4de9-b9bb-35e552a26462","html_url":"https://github.com/pheki/xts-mode","commit_stats":{"total_commits":40,"total_committers":6,"mean_commits":6.666666666666667,"dds":"0.15000000000000002","last_synced_commit":"d0a433d564d28e58af77d82041cc9097625a8c5d"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pheki%2Fxts-mode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pheki%2Fxts-mode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pheki%2Fxts-mode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pheki%2Fxts-mode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pheki","download_url":"https://codeload.github.com/pheki/xts-mode/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252854337,"owners_count":21814669,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","hacktoberfest","rust","xts"],"created_at":"2024-10-24T10:45:11.492Z","updated_at":"2025-05-07T09:47:04.631Z","avatar_url":"https://github.com/pheki.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# xts-mode\n\n[XTS block mode](https://en.wikipedia.org/wiki/Disk_encryption_theory#XEX-based_tweaked-codebook_mode_with_ciphertext_stealing_(XTS)) implementation in Rust.\n\nCurrently this implementation supports only ciphers with 128-bit (16-byte) block size (distinct from key size). Note that AES-256 uses 128-bit blocks, so it works with this crate. If you require other cipher block sizes, please open an issue.\n\n## Examples:\n\nEncrypting and decrypting multiple sectors at a time:\n```rust\nuse aes::{Aes128, cipher::KeyInit, cipher::generic_array::GenericArray};\nuse xts_mode::{Xts128, get_tweak_default};\n\n// Load the encryption key\nlet key = [1; 32];\nlet plaintext = [5; 0x400];\n\n// Load the data to be encrypted\nlet mut buffer = plaintext.to_owned();\n\nlet cipher_1 = Aes128::new(GenericArray::from_slice(\u0026key[..16]));\nlet cipher_2 = Aes128::new(GenericArray::from_slice(\u0026key[16..]));\n\nlet xts = Xts128::\u003cAes128\u003e::new(cipher_1, cipher_2);\n\nlet sector_size = 0x200;\nlet first_sector_index = 0;\n\n// Encrypt data in the buffer\nxts.encrypt_area(\u0026mut buffer, sector_size, first_sector_index, get_tweak_default);\n\n// Decrypt data in the buffer\nxts.decrypt_area(\u0026mut buffer, sector_size, first_sector_index, get_tweak_default);\n\nassert_eq!(\u0026buffer[..], \u0026plaintext[..]);\n```\n\nAES-256 works too:\n```rust\nuse aes::{Aes256, cipher::KeyInit, cipher::generic_array::GenericArray};\nuse xts_mode::{Xts128, get_tweak_default};\n\n// Load the encryption key\nlet key = [1; 64];\nlet plaintext = [5; 0x400];\n\n// Load the data to be encrypted\nlet mut buffer = plaintext.to_owned();\n\nlet cipher_1 = Aes256::new(GenericArray::from_slice(\u0026key[..32]));\nlet cipher_2 = Aes256::new(GenericArray::from_slice(\u0026key[32..]));\n\nlet xts = Xts128::\u003cAes256\u003e::new(cipher_1, cipher_2);\n\nlet sector_size = 0x200;\nlet first_sector_index = 0;\n\nxts.encrypt_area(\u0026mut buffer, sector_size, first_sector_index, get_tweak_default);\n\nxts.decrypt_area(\u0026mut buffer, sector_size, first_sector_index, get_tweak_default);\n\nassert_eq!(\u0026buffer[..], \u0026plaintext[..]);\n```\n\nEncrypting and decrypting a single sector:\n```rust\nuse aes::{Aes128, cipher::KeyInit, cipher::generic_array::GenericArray};\nuse xts_mode::{Xts128, get_tweak_default};\n\n// Load the encryption key\nlet key = [1; 32];\nlet plaintext = [5; 0x200];\n\n// Load the data to be encrypted\nlet mut buffer = plaintext.to_owned();\n\nlet cipher_1 = Aes128::new(GenericArray::from_slice(\u0026key[..16]));\nlet cipher_2 = Aes128::new(GenericArray::from_slice(\u0026key[16..]));\n\nlet xts = Xts128::\u003cAes128\u003e::new(cipher_1, cipher_2);\n\nlet tweak = get_tweak_default(0); // 0 is the sector index\n\n// Encrypt data in the buffer\nxts.encrypt_sector(\u0026mut buffer, tweak);\n\n// Decrypt data in the buffer\nxts.decrypt_sector(\u0026mut buffer, tweak);\n\nassert_eq!(\u0026buffer[..], \u0026plaintext[..]);\n```\n\nDecrypting a [NCA](https://switchbrew.org/wiki/NCA_Format) (nintendo content archive) header:\n```rust\nuse aes::{Aes128, cipher::KeyInit, cipher::generic_array::GenericArray};\nuse xts_mode::{Xts128, get_tweak_default};\n\npub fn get_nintendo_tweak(sector_index: u128) -\u003e [u8; 0x10] {\n    sector_index.to_be_bytes()\n}\n\n// Load the header key\nlet header_key = \u0026[0; 0x20];\n\n// Read into buffer header to be decrypted\nlet mut buffer = vec![0; 0xC00];\n\nlet cipher_1 = Aes128::new(GenericArray::from_slice(\u0026header_key[..0x10]));\nlet cipher_2 = Aes128::new(GenericArray::from_slice(\u0026header_key[0x10..]));\n\nlet mut xts = Xts128::new(cipher_1, cipher_2);\n\n// Decrypt the first 0x400 bytes of the header in 0x200 sections\nxts.decrypt_area(\u0026mut buffer[0..0x400], 0x200, 0, get_nintendo_tweak);\n\nlet magic = \u0026buffer[0x200..0x204];\nassert_eq!(magic, b\"NCA3\"); // In older NCA versions the section index used in header encryption was different\n\n// Decrypt the rest of the header\nxts.decrypt_area(\u0026mut buffer[0x400..0xC00], 0x200, 2, get_nintendo_tweak);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpheki%2Fxts-mode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpheki%2Fxts-mode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpheki%2Fxts-mode/lists"}