-
Notifications
You must be signed in to change notification settings - Fork 31
torchcsprng.encrypt/torchcsprng.decrypt with AES128 ECB/CTR support #83
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
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
…R support" This PR introduces two new methods to `torchcsprng` module: * `torchcsprng.encrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)` > - `input` tensor can be any CPU or CUDA tensor of any dtype and size in bytes(zero-padding is used to make its size in bytes divisible by block size in bytes) > - `output` tensor can have any dtype and the same device as `input` tensor and the size in bytes rounded to the block size in bytes(16 bytes for AES 128) > - `key` tensor can be any CPU or CUDA tensor of any dtype and size in bytes equal to 16 for AES 128 > - `cipher` currently can be only one supported value `"aes128"` > - `mode` currently can be either [`"ecb"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(ECB)) or [`"ctr"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) * `torchcsprng.decrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)` > - `input` tensor can be any CPU or CUDA tensor of any dtype with size in bytes divisible by the block size in bytes(16 bytes for AES 128) > - `output` tensor can have any dtype but the same device as `input` tensor and the same size in bytes as `input` tensor > - `key` tensor can be any CPU or CUDA tensor of any dtype and size in bytes equal to 16 for AES 128 > - `cipher` currently can be only one supported value `"aes128"` > - `mode` currently can be either [`"ecb"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(ECB)) or [`"ctr"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) Also this PR unifies encryption/decryption with existing random number generation which uses AES128 in CTR mode Fixes #77 [ghstack-poisoned]
…R support" This PR introduces two new methods to `torchcsprng` module: * `torchcsprng.encrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)` > - `input` tensor can be any CPU or CUDA tensor of any dtype and size in bytes(zero-padding is used to make its size in bytes divisible by block size in bytes) > - `output` tensor can have any dtype and the same device as `input` tensor and the size in bytes rounded to the block size in bytes(16 bytes for AES 128) > - `key` tensor can be any CPU or CUDA tensor of any dtype and size in bytes equal to 16 for AES 128 > - `cipher` currently can be only one supported value `"aes128"` > - `mode` currently can be either [`"ecb"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(ECB)) or [`"ctr"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) * `torchcsprng.decrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)` > - `input` tensor can be any CPU or CUDA tensor of any dtype with size in bytes divisible by the block size in bytes(16 bytes for AES 128) > - `output` tensor can have any dtype but the same device as `input` tensor and the same size in bytes as `input` tensor > - `key` tensor can be any CPU or CUDA tensor of any dtype and size in bytes equal to 16 for AES 128 > - `cipher` currently can be only one supported value `"aes128"` > - `mode` currently can be either [`"ecb"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(ECB)) or [`"ctr"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) Also this PR unifies encryption/decryption with existing random number generation which uses AES128 in CTR mode Fixes #77 [ghstack-poisoned]
LaRiffle
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is perfect for me!
torchcsprng/csrc/block_cipher.h
Outdated
| // TORCH_CHECK((input_numel * input_type_size + block_size - 1) / block_size * block_size == output_numel * output_type_size, "wrong size"); | ||
|
|
||
| // const auto size_in_bytes = input_numel * input_type_size; | ||
| // const auto size_in_bytes = output_numel * output_type_size; | ||
|
|
||
| if (device.type() == at::kCPU) { | ||
| // const auto total = (size_in_bytes + block_size - 1) / block_size; | ||
| // const auto total = (size_in_bytes + block_size / N - 1) / block_size * N; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably remove these commented lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
| } | ||
|
|
||
| template<int block_size, typename cipher_t> | ||
| void block_cipher(Tensor input, Tensor output, cipher_t cipher) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should make sure that 2 functions are not named block_cipher ? (see l.127 above)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's ok, since they are overloaded functions of the same functionality
torchcsprng/csrc/csprng.h
Outdated
| if (gen->key().defined()) { | ||
| return gen->key().clone(); | ||
| } | ||
| auto t = torch::empty({static_cast<signed long>(block_t_size)}, torch::kUInt8); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: rename t -> key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed
torchcsprng/csrc/csprng.h
Outdated
| return gen->key().clone(); | ||
| } | ||
| auto t = torch::empty({static_cast<signed long>(block_t_size)}, torch::kUInt8); | ||
| using random_t = uint32_t; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can fix using uint32_t if it's ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gen->random(); will always output 32 bits?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced with typename std::result_of<decltype(&RNG::random)(RNG)>::type;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!
…R support" This PR introduces two new methods to `torchcsprng` module: * `torchcsprng.encrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)` > - `input` tensor can be any CPU or CUDA tensor of any dtype and size in bytes(zero-padding is used to make its size in bytes divisible by block size in bytes) > - `output` tensor can have any dtype and the same device as `input` tensor and the size in bytes rounded to the block size in bytes(16 bytes for AES 128) > - `key` tensor can have any dtype and the same device as `input` tensor and size in bytes equal to 16 for AES 128 > - `cipher` currently can be only one supported value `"aes128"` > - `mode` currently can be either [`"ecb"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(ECB)) or [`"ctr"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) * `torchcsprng.decrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)` > - `input` tensor can be any CPU or CUDA tensor of any dtype with size in bytes divisible by the block size in bytes(16 bytes for AES 128) > - `output` tensor can have any dtype but the same device as `input` tensor and the same size in bytes as `input` tensor > - `key` tensor can have any dtype and the same device as `input` tensor and size in bytes equal to 16 for AES 128 > - `cipher` currently can be only one supported value `"aes128"` > - `mode` currently can be either [`"ecb"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(ECB)) or [`"ctr"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) Also this PR unifies encryption/decryption with existing random number generation which uses AES128 in CTR mode Fixes #77 Differential Revision: [D25080624](https://our.internmc.facebook.com/intern/diff/D25080624) [ghstack-poisoned]
|
blocked by pytorch/pytorch#48257 |
…R support" This PR introduces two new methods to `torchcsprng` module: * `torchcsprng.encrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)` > - `input` tensor can be any CPU or CUDA tensor of any dtype and size in bytes(zero-padding is used to make its size in bytes divisible by block size in bytes) > - `output` tensor can have any dtype and the same device as `input` tensor and the size in bytes rounded to the block size in bytes(16 bytes for AES 128) > - `key` tensor can have any dtype and the same device as `input` tensor and size in bytes equal to 16 for AES 128 > - `cipher` currently can be only one supported value `"aes128"` > - `mode` currently can be either [`"ecb"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(ECB)) or [`"ctr"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) * `torchcsprng.decrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)` > - `input` tensor can be any CPU or CUDA tensor of any dtype with size in bytes divisible by the block size in bytes(16 bytes for AES 128) > - `output` tensor can have any dtype but the same device as `input` tensor and the same size in bytes as `input` tensor > - `key` tensor can have any dtype and the same device as `input` tensor and size in bytes equal to 16 for AES 128 > - `cipher` currently can be only one supported value `"aes128"` > - `mode` currently can be either [`"ecb"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(ECB)) or [`"ctr"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) Also this PR unifies encryption/decryption with existing random number generation which uses AES128 in CTR mode Fixes #77 Differential Revision: [D25080624](https://our.internmc.facebook.com/intern/diff/D25080624) [ghstack-poisoned]
|
@pbelevich merged this pull request in d313741. |
…#83) Summary: Pull Request resolved: meta-pytorch/csprng#83 This PR introduces two new methods to `torchcsprng` module: * `torchcsprng.encrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)` > - `input` tensor can be any CPU or CUDA tensor of any dtype and size in bytes(zero-padding is used to make its size in bytes divisible by block size in bytes) > - `output` tensor can have any dtype and the same device as `input` tensor and the size in bytes rounded to the block size in bytes(16 bytes for AES 128) > - `key` tensor can be any CPU or CUDA tensor of any dtype and size in bytes equal to 16 for AES 128 > - `cipher` currently can be only one supported value `"aes128"` > - `mode` currently can be either [`"ecb"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(ECB)) or [`"ctr"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) * `torchcsprng.decrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)` > - `input` tensor can be any CPU or CUDA tensor of any dtype with size in bytes divisible by the block size in bytes(16 bytes for AES 128) > - `output` tensor can have any dtype but the same device as `input` tensor and the same size in bytes as `input` tensor > - `key` tensor can be any CPU or CUDA tensor of any dtype and size in bytes equal to 16 for AES 128 > - `cipher` currently can be only one supported value `"aes128"` > - `mode` currently can be either [`"ecb"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(ECB)) or [`"ctr"`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) Also this PR unifies encryption/decryption with existing random number generation which uses AES128 in CTR mode Fixes #77 Test Plan: Imported from OSS Reviewed By: malfet Differential Revision: D25080624 Pulled By: pbelevich fbshipit-source-id: 00cb7765a02c0320e223dea9fefcb8e57e331f3f
This PR introduces two new methods to
torchcsprngmodule:torchcsprng.encrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)torchcsprng.decrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)Also this PR unifies encryption/decryption with existing random number generation which uses AES128 in CTR mode
Fixes #77
Stack from ghstack:
Differential Revision: D25080624