/rust/registry/src/index.crates.io-6f17d22bba15001f/ring-0.17.8/src/aead/block.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2018 Brian Smith. |
2 | | // |
3 | | // Permission to use, copy, modify, and/or distribute this software for any |
4 | | // purpose with or without fee is hereby granted, provided that the above |
5 | | // copyright notice and this permission notice appear in all copies. |
6 | | // |
7 | | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
8 | | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9 | | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
10 | | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
11 | | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
12 | | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
13 | | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
14 | | |
15 | | use crate::polyfill::ArrayFlatten; |
16 | | use core::ops::{BitXor, BitXorAssign}; |
17 | | |
18 | | #[repr(transparent)] |
19 | | #[derive(Copy, Clone)] |
20 | | pub struct Block([u8; 16]); |
21 | | |
22 | | pub const BLOCK_LEN: usize = 16; |
23 | | |
24 | | impl Block { |
25 | | #[inline] |
26 | 0 | pub fn zero() -> Self { |
27 | 0 | Self([0; 16]) |
28 | 0 | } |
29 | | |
30 | | #[inline] |
31 | 0 | pub fn overwrite_part_at(&mut self, index: usize, a: &[u8]) { |
32 | 0 | let mut tmp: [u8; BLOCK_LEN] = *self.as_ref(); |
33 | 0 | tmp[index..][..a.len()].copy_from_slice(a); |
34 | 0 | *self = Self::from(&tmp) |
35 | 0 | } |
36 | | |
37 | | #[inline] |
38 | 0 | pub fn zero_from(&mut self, index: usize) { |
39 | 0 | let mut tmp: [u8; BLOCK_LEN] = *self.as_ref(); |
40 | 0 | tmp[index..].fill(0); |
41 | 0 | *self = Self::from(&tmp) |
42 | 0 | } |
43 | | } |
44 | | |
45 | | impl BitXorAssign for Block { |
46 | | #[inline] |
47 | 0 | fn bitxor_assign(&mut self, a: Self) { |
48 | | // Relies heavily on optimizer to optimize this into word- or vector- |
49 | | // level XOR. |
50 | 0 | for (r, a) in self.0.iter_mut().zip(a.0.iter()) { |
51 | 0 | *r ^= *a; |
52 | 0 | } |
53 | 0 | } |
54 | | } |
55 | | |
56 | | impl BitXor for Block { |
57 | | type Output = Self; |
58 | | |
59 | | #[inline] |
60 | 0 | fn bitxor(self, a: Self) -> Self { |
61 | 0 | let mut r = self; |
62 | 0 | r.bitxor_assign(a); |
63 | 0 | r |
64 | 0 | } |
65 | | } |
66 | | |
67 | | impl<T> From<T> for Block |
68 | | where |
69 | | T: ArrayFlatten<Output = [u8; 16]>, |
70 | | { |
71 | | #[inline] |
72 | 0 | fn from(bytes: T) -> Self { |
73 | 0 | Self(bytes.array_flatten()) |
74 | 0 | } Unexecuted instantiation: <ring::aead::block::Block as core::convert::From<[[u8; 4]; 4]>>::from Unexecuted instantiation: <ring::aead::block::Block as core::convert::From<[[u8; 8]; 2]>>::from |
75 | | } |
76 | | |
77 | | impl From<&'_ [u8; BLOCK_LEN]> for Block { |
78 | | #[inline] |
79 | 0 | fn from(bytes: &[u8; BLOCK_LEN]) -> Self { |
80 | 0 | Self(*bytes) |
81 | 0 | } |
82 | | } |
83 | | |
84 | | impl AsRef<[u8; BLOCK_LEN]> for Block { |
85 | | #[inline] |
86 | 0 | fn as_ref(&self) -> &[u8; BLOCK_LEN] { |
87 | 0 | &self.0 |
88 | 0 | } |
89 | | } |