Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Tests/Cipher/CryptoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ public function testGenerateKey()
// Assert that the key is the correct type.
$this->assertInstanceOf(Key::class, $key);

// Assert the public key is the expected length
$this->assertSame(DefuseKey::KEY_BYTE_SIZE, Binary::strlen($key->getPublic()));
// Assert the key has no public part, as the raw key bytes must not be exposed through getPublic()
$this->assertSame('', $key->getPublic());

// Assert the private key is a usable key
$this->assertInstanceOf(DefuseKey::class, DefuseKey::loadFromAsciiSafeString($key->getPrivate()));

// Assert the key is of the correct type.
$this->assertSame('crypto', $key->getType());
Expand Down
7 changes: 4 additions & 3 deletions docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ $key = new Key('crypto', $secret, '');
$crypt = new Crypt(new Crypto(), $key);
```

`Crypto::generateKey()` currently puts the raw key bytes into the key object's *public* field. That
field is meaningless for a symmetric algorithm, so treat `getPublic()` on a `crypto` key as secret
material and never log, transmit or store it.
The algorithm is symmetric, so a `crypto` key has no public part: since 4.1 `Crypto::generateKey()`
sets it to an empty string. Earlier releases put the raw key bytes into the *public* field; treat
`getPublic()` of keys generated by those releases as secret material and never log, transmit or
store it. Only the private part is needed to encrypt and decrypt.

## Handling failures

Expand Down
4 changes: 2 additions & 2 deletions src/Cipher/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public function generateKey(array $options = [])
throw new InvalidKeyException('Cannot safely create a key', $ex->getCode(), $ex);
}

// Create the new encryption key object.
return new Key('crypto', $public->saveToAsciiSafeString(), $public->getRawBytes());
// Create the new encryption key object. The cipher is symmetric, so there is no public part.
return new Key('crypto', $public->saveToAsciiSafeString(), '');
}

/**
Expand Down
Loading