Skip to content

Commit

Permalink
[1/rename] rename everything from "djb" to just "curve25519"
Browse files Browse the repository at this point in the history
- [NON RUST/RENAME] rename "DJB_TYPE" to "CURVE_25519_TYPE" in java and swift
- [1+CRYPTO CRATE] make swift/build-ffi.sh --generate-ffi work
- [1+CURVE] add docs and the Keyed trait to `curve*.rs`
  • Loading branch information
cosmicexplorer committed Jun 27, 2022
1 parent 2a46a5b commit 19e11fe
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 104 deletions.
4 changes: 2 additions & 2 deletions java/shared/java/org/signal/libsignal/protocol/ecc/Curve.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Copyright (C) 2013-2016 Open Whisper Systems
* Copyright (C) 2013-2022 Open Whisper Systems
*
* Licensed according to the LICENSE file in this repository.
*/
package org.signal.libsignal.protocol.ecc;
import org.signal.libsignal.protocol.InvalidKeyException;

public class Curve {
public static final int DJB_TYPE = 0x05;
public static final int CURVE_25519_TYPE = 0x05;

public static ECKeyPair generateKeyPair() {
ECPrivateKey privateKey = ECPrivateKey.generate();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2013-2016 Open Whisper Systems
* Copyright (C) 2013-2022 Open Whisper Systems
*
* Licensed according to the LICENSE file in this repository.
*/
Expand All @@ -26,7 +26,7 @@ public ECPublicKey(byte[] serialized) {

static public ECPublicKey fromPublicKeyBytes(byte[] key) {
byte[] with_type = new byte[33];
with_type[0] = 0x05;
with_type[0] = Curve.CURVE_25519_TYPE;
System.arraycopy(key, 0, with_type, 1, 32);
return new ECPublicKey(Native.ECPublicKey_Deserialize(with_type, 0));
}
Expand Down
2 changes: 1 addition & 1 deletion node/ts/test/PublicAPITest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ describe('SignalClient', () => {

assert.throws(() => {
SignalClient.PrivateKey.deserialize(invalid_key);
}, 'bad key length <33> for key with type <Djb>');
}, 'bad key length <33> for key with type <Curve25519>');

assert.throws(() => {
SignalClient.PublicKey.deserialize(invalid_key);
Expand Down
2 changes: 1 addition & 1 deletion rust/bridge/shared/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn ECPrivateKey_Sign(key: &PrivateKey, message: &[u8]) -> Result<Vec<u8>> {

#[bridge_fn_buffer(ffi = "privatekey_agree", node = "PrivateKey_Agree")]
fn ECPrivateKey_Agree(private_key: &PrivateKey, public_key: &PublicKey) -> Result<Vec<u8>> {
Ok(private_key.calculate_agreement(public_key)?.into_vec())
Ok(private_key.calculate_agreement(public_key)?.to_vec())
}

#[bridge_fn_buffer(ffi = "identitykeypair_serialize")]
Expand Down
4 changes: 3 additions & 1 deletion rust/crypto/src/aes_ctr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use aes::{Aes256, NewBlockCipher};
pub struct Aes256Ctr32(aes::Aes256Ctr);

impl Aes256Ctr32 {
pub const NONCE_SIZE: usize = aes::BLOCK_SIZE - 4;
pub const CTR_NONCE_SIZE: usize = aes::BLOCK_SIZE - 4;

const NONCE_SIZE: usize = Self::CTR_NONCE_SIZE;

pub fn new(aes256: Aes256, nonce: &[u8], init_ctr: u32) -> Result<Self> {
if nonce.len() != Self::NONCE_SIZE {
Expand Down
17 changes: 12 additions & 5 deletions rust/crypto/src/aes_gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ use ghash::universal_hash::{NewUniversalHash, UniversalHash};
use ghash::GHash;
use subtle::ConstantTimeEq;

pub const TAG_SIZE: usize = 16;
pub const NONCE_SIZE: usize = 12;
pub const GCM_TAG_SIZE: usize = 16;
pub const GCM_NONCE_SIZE: usize = 12;

use GCM_NONCE_SIZE as NONCE_SIZE;
use GCM_TAG_SIZE as TAG_SIZE;

#[derive(Clone)]
struct GcmGhash {
Expand Down Expand Up @@ -126,8 +129,12 @@ pub struct Aes256GcmEncryption {
}

impl Aes256GcmEncryption {
pub const TAG_SIZE: usize = TAG_SIZE;
pub const NONCE_SIZE: usize = NONCE_SIZE;
pub const GCM_ENCRYPTION_TAG_SIZE: usize = TAG_SIZE;
pub const GCM_ENCRYPTION_NONCE_SIZE: usize = NONCE_SIZE;

const TAG_SIZE: usize = Self::GCM_ENCRYPTION_TAG_SIZE;
#[allow(dead_code)]
const NONCE_SIZE: usize = Self::GCM_ENCRYPTION_NONCE_SIZE;

pub fn new(key: &[u8], nonce: &[u8], associated_data: &[u8]) -> Result<Self> {
let (ctr, ghash) = setup_gcm(key, nonce, associated_data)?;
Expand All @@ -140,7 +147,7 @@ impl Aes256GcmEncryption {
Ok(())
}

pub fn compute_tag(self) -> Result<[u8; TAG_SIZE]> {
pub fn compute_tag(self) -> Result<[u8; Self::TAG_SIZE]> {
self.ghash.finalize()
}
}
Expand Down
Loading

0 comments on commit 19e11fe

Please sign in to comment.