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

ocb3: fix nonce and tag size bounds #595

Merged
merged 3 commits into from
Mar 27, 2024
Merged

ocb3: fix nonce and tag size bounds #595

merged 3 commits into from
Mar 27, 2024

Conversation

newpavlov
Copy link
Member

Also adds compile_fail tests to check that the sizes are correctly enforced at compile time.

The first commit intentionally does not include the fix to demonstrate that NonZero bounds are necessary.

ocb3/README.md Outdated Show resolved Hide resolved
TagSize: ArrayLength<u8> + NonZero + IsLessOrEqual<U16>,
GrEq<NonceSize, U6>: NonZero,
LeEq<NonceSize, U15>: NonZero,
LeEq<TagSize, U16>: NonZero,
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that these bounds cause quite ugly compilation errors:

error[E0599]: the function or associated item `new` exists for struct `Ocb3<Aes128, UInt<UInt<..., ...>, ...>, ...>`, but its trait bounds were not satisfied
 --> ocb3/src/lib.rs:100:40
  |
9 | let cipher = Ocb3::<Aes128, U12, U20>::new(&key);
  |                                        ^^^ function or associated item cannot be called on `Ocb3<Aes128, UInt<UInt<..., ...>, ...>, ...>` due to unsatisfied trait bounds
  |
  = note: the full type name has been written to '/tmp/rustdoctestWBN6Uv/rust_out.long-type-4996721529537771483.txt'
  = note: consider using `--verbose` to print the full type name to the console
  = note: the following trait bounds were not satisfied:
          `&Ocb3<Aes128, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, B0>>: InnerInit`
          which is required by `&Ocb3<Aes128, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, B0>>: KeyInit`
          `&Ocb3<Aes128, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, B0>>: InnerUser`
          which is required by `&Ocb3<Aes128, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, B0>>: KeyInit`
          `&mut Ocb3<Aes128, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, B0>>: InnerInit`
          which is required by `&mut Ocb3<Aes128, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, B0>>: KeyInit`
          `&mut Ocb3<Aes128, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, B0>>: InnerUser`
          which is required by `&mut Ocb3<Aes128, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, B0>>: KeyInit`

error[E0277]: the trait bound `B0: aes::cipher::typenum::NonZero` is not satisfied
   --> ocb3/src/lib.rs:100:14
    |
9   | let cipher = Ocb3::<Aes128, U12, U20>::new(&key);
    |              ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `aes::cipher::typenum::NonZero` is not implemented for `B0`
    |
    = help: the following other types implement trait `aes::cipher::typenum::NonZero`:
              B1
              PInt<U>
              NInt<U>
              UInt<U, B>
note: required by a bound in `Ocb3`
   --> /home/newpavlov/projects/rust/crypto/AEADs/ocb3/src/lib.rs:108:25
    |
102 | pub struct Ocb3<Cipher, NonceSize = U12, TagSize = U16>
    |            ---- required by a bound in this struct
...
108 |     LeEq<TagSize, U16>: NonZero,
    |                         ^^^^^^^ required by this bound in `Ocb3`

Copy link
Member

Choose a reason for hiding this comment

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

Ok, that might be a good reason to use sealed traits for now

@newpavlov newpavlov merged commit 01fcf4c into master Mar 27, 2024
12 checks passed
@newpavlov newpavlov deleted the ocb3/fix_bounds branch March 27, 2024 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants