Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Commit 99789fc

Browse files
committed
Extract SCALAR_SIZE and PRD_SCALAR_SIZE constants
1 parent 55a3e8b commit 99789fc

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

crates/playready/src/binary_format/device.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
use binrw::{BinRead, BinWrite};
44

5+
pub const PRD_SCALAR_SIZE: usize = 96;
6+
57
#[derive(BinRead, BinWrite, Debug, Clone)]
68
#[brw(big)]
79
pub struct DeviceV2 {
810
pub group_certificate_length: u32,
911
#[br(count = group_certificate_length)]
1012
pub group_certificate: Vec<u8>,
11-
pub encryption_key: [u8; 96],
12-
pub signing_key: [u8; 96],
13+
pub encryption_key: [u8; PRD_SCALAR_SIZE],
14+
pub signing_key: [u8; PRD_SCALAR_SIZE],
1315
}
1416

1517
#[derive(BinRead, BinWrite, Debug, Clone)]
1618
#[brw(big)]
1719
pub struct DeviceV3 {
18-
pub group_key: [u8; 96],
19-
pub encryption_key: [u8; 96],
20-
pub signing_key: [u8; 96],
20+
pub group_key: [u8; PRD_SCALAR_SIZE],
21+
pub encryption_key: [u8; PRD_SCALAR_SIZE],
22+
pub signing_key: [u8; PRD_SCALAR_SIZE],
2123
pub group_certificate_length: u32,
2224
#[br(count = group_certificate_length)]
2325
pub group_certificate: Vec<u8>,

crates/playready/src/crypto/ecc_p256.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub type PublicKey = elastic_elgamal::PublicKey<Generic<NistP256>>;
1414
pub type SecretKey = elastic_elgamal::SecretKey<Generic<NistP256>>;
1515
pub type Keypair = elastic_elgamal::Keypair<Generic<NistP256>>;
1616

17+
pub const SCALAR_SIZE: usize = 32;
1718
pub const SIGNATURE_SIZE: usize = 64;
1819

1920
pub trait ToUntaggedBytes {

crates/playready/src/device.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Creating and parsing devices.
22
33
use crate::{
4-
binary_format::{self},
4+
binary_format::{self, device::PRD_SCALAR_SIZE},
55
certificate::CertificateChain,
6-
crypto::ecc_p256::{FromBytes, Keypair, ToUntaggedBytes},
6+
crypto::ecc_p256::{FromBytes, Keypair, ToUntaggedBytes, SCALAR_SIZE},
77
};
88
use binrw::{BinRead, BinWrite};
99
use p256::ecdsa::SigningKey;
@@ -50,7 +50,7 @@ impl Device {
5050
};
5151

5252
let group_key = match group_key {
53-
Some(group_key) => Some(SigningKey::from_slice(&group_key[..32])?),
53+
Some(group_key) => Some(SigningKey::from_slice(&group_key[..SCALAR_SIZE])?),
5454
None => None,
5555
};
5656

@@ -64,8 +64,8 @@ impl Device {
6464
binary_format::device::DeviceInner::V3(v3) => &v3.signing_key,
6565
};
6666

67-
let encryption_key = Keypair::from_bytes(&encryption_key[..32])?;
68-
let signing_key = SigningKey::from_slice(&signing_key[..32])?;
67+
let encryption_key = Keypair::from_bytes(&encryption_key[..SCALAR_SIZE])?;
68+
let signing_key = SigningKey::from_slice(&signing_key[..SCALAR_SIZE])?;
6969

7070
let group_certificate = match device.inner {
7171
binary_format::device::DeviceInner::V2(v2) => v2.group_certificate,
@@ -202,7 +202,7 @@ impl Device {
202202
bytes.clear();
203203
file.read_to_end(&mut bytes)?;
204204

205-
let group_key = SigningKey::from_slice(bytes.get(..32).ok_or(
205+
let group_key = SigningKey::from_slice(bytes.get(..SCALAR_SIZE).ok_or(
206206
crate::Error::SliceOutOfBoundsError("group_key", bytes.len()),
207207
)?)?;
208208

@@ -211,21 +211,21 @@ impl Device {
211211

212212
/// Serializes and writes device to file specified by path.
213213
pub fn write_to_file(&self, path: impl AsRef<Path>) -> Result<(), crate::Error> {
214-
let mut group_key = [0u8; 96];
215-
let mut encryption_key = [0u8; 96];
216-
let mut signing_key = [0u8; 96];
214+
let mut group_key = [0u8; PRD_SCALAR_SIZE];
215+
let mut encryption_key = [0u8; PRD_SCALAR_SIZE];
216+
let mut signing_key = [0u8; PRD_SCALAR_SIZE];
217217

218-
group_key[..32].copy_from_slice(
218+
group_key[..SCALAR_SIZE].copy_from_slice(
219219
&self
220220
.group_key
221221
.as_ref()
222222
.ok_or(crate::Error::GroupKeyMissingError)?
223223
.to_bytes(),
224224
);
225225

226-
encryption_key[..32]
226+
encryption_key[..SCALAR_SIZE]
227227
.copy_from_slice(&self.encryption_key.secret().expose_scalar().to_repr());
228-
signing_key[..32].copy_from_slice(&self.signing_key.to_bytes());
228+
signing_key[..SCALAR_SIZE].copy_from_slice(&self.signing_key.to_bytes());
229229

230230
let group_certificate = self.group_certificate().to_vec();
231231
let group_certificate_length = u32::try_from(group_certificate.len()).unwrap();

0 commit comments

Comments
 (0)