Skip to content

Conversation

@pbelevich
Copy link
Contributor

@pbelevich pbelevich commented Nov 14, 2020

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" or "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" or "ctr"

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

[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 14, 2020
ghstack-source-id: 8e814a1
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 16, 2020
ghstack-source-id: 55c9a76
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: 8c88890
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: ff69f4d
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: e8d2053
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: 2f481a1
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: f7af3bb
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: 3a26482
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: d0fc3f7
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: ebbc944
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: 0366942
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: b2ebdb4
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: 2d50967
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 18, 2020
ghstack-source-id: e04d026
Pull Request resolved: #83
[ghstack-poisoned]
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 19, 2020
ghstack-source-id: 07d63ea
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 19, 2020
ghstack-source-id: 3ee186d
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 19, 2020
ghstack-source-id: 1f1e995
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 19, 2020
ghstack-source-id: b32cd72
Pull Request resolved: #83
[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Nov 19, 2020
ghstack-source-id: dc20a35
Pull Request resolved: #83
@pbelevich pbelevich changed the title encrypt/decrypt torchcsprng.encrypt/torchcsprng.decrypt with AES128 ECB and CTR support Nov 19, 2020
@pbelevich pbelevich changed the title torchcsprng.encrypt/torchcsprng.decrypt with AES128 ECB and CTR support torchcsprng.encrypt/torchcsprng.decrypt with AES128 ECB/CTR support Nov 19, 2020
@pbelevich pbelevich requested a review from LaRiffle November 19, 2020 03:02
…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]
pbelevich added a commit that referenced this pull request Nov 19, 2020
ghstack-source-id: 25f08dc
Pull Request resolved: #83
…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]
pbelevich added a commit that referenced this pull request Nov 19, 2020
ghstack-source-id: 614b309
Pull Request resolved: #83
Copy link

@LaRiffle LaRiffle left a 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!

Comment on lines 134 to 141
// 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;

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

Copy link
Contributor Author

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) {

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)

Copy link
Contributor Author

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

if (gen->key().defined()) {
return gen->key().clone();
}
auto t = torch::empty({static_cast<signed long>(block_t_size)}, torch::kUInt8);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: rename t -> key

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed

return gen->key().clone();
}
auto t = torch::empty({static_cast<signed long>(block_t_size)}, torch::kUInt8);
using random_t = uint32_t;

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?

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?

Copy link
Contributor Author

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;

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]
pbelevich added a commit that referenced this pull request Nov 19, 2020
ghstack-source-id: bd3f791
Pull Request resolved: #83
@pbelevich
Copy link
Contributor Author

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 added a commit that referenced this pull request Nov 30, 2020
ghstack-source-id: c7cca30
Pull Request resolved: #83
@facebook-github-bot
Copy link
Contributor

@pbelevich merged this pull request in d313741.

@facebook-github-bot facebook-github-bot deleted the gh/pbelevich/45/head branch December 4, 2020 15:17
ts-alchemist659op added a commit to ts-alchemist659op/csprng that referenced this pull request Oct 20, 2025
…#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants