-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More or less straightforward, if involved implementation. The main class is SymmetricKey which manages input validation and delegates actual key generation to JNI code. JNI returns "null" on errors, just like other methods, because it's easier to deal with Java exceptions from Java. Note the cast from jbyte* to uint8_t*. It is necessary because Java bytes are always signed, but uint8_t is unsigned. Later we cast them back so this does not cause any problems. We add more constructors to KeyGenerationException because now we need to have some messages there as there are two places where keys are generated. Do not export new constructors for KeyGenerationException. There is no reason for the users to construct instances of these exceptions. Historically we had constructors for other exceptions, but that's a mistake. We can't hide them without breaking compatibility, but we can avoid making the same mistake again.
- Loading branch information
Showing
6 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/wrappers/themis/java/com/cossacklabs/themis/SymmetricKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2019 Cossack Labs Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.cossacklabs.themis; | ||
|
||
/** | ||
* Symmetric encryption key. | ||
* | ||
* These keys are used with Secure Cell cryptosystem. | ||
*/ | ||
public class SymmetricKey extends KeyBytes { | ||
|
||
/** | ||
* Generates a new symmetric key. | ||
*/ | ||
public SymmetricKey() { | ||
super(newSymmetricKey()); | ||
} | ||
|
||
/** | ||
* Creates a symmetric key from byte array. | ||
* | ||
* @param key byte array | ||
* | ||
* @throws NullArgumentException if `key` is null. | ||
* @throws InvalidArgumentException if `key` is empty. | ||
*/ | ||
public SymmetricKey(byte[] key) { | ||
super(key); | ||
} | ||
|
||
private static native byte[] generateSymmetricKey(); | ||
|
||
private static byte[] newSymmetricKey() { | ||
byte[] key = generateSymmetricKey(); | ||
if (key == null || key.length == 0) { | ||
throw new KeyGenerationException("failed to generate symmetric key"); | ||
} | ||
return key; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
tests/themis/wrappers/android/com/cossacklabs/themis/test/SymmetricKeyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2019 Cossack Labs Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.cossacklabs.themis.test; | ||
|
||
import com.cossacklabs.themis.SymmetricKey; | ||
import com.cossacklabs.themis.InvalidArgumentException; | ||
import com.cossacklabs.themis.NullArgumentException; | ||
|
||
import static org.junit.Assert.*; | ||
import org.junit.Test; | ||
|
||
public class SymmetricKeyTest { | ||
|
||
private static final int defaultLength = 32; | ||
|
||
@Test | ||
public void generateNewKey() { | ||
SymmetricKey key = new SymmetricKey(); | ||
|
||
byte[] keyBytes = key.toByteArray(); | ||
assertNotNull(keyBytes); | ||
assertEquals(keyBytes.length, defaultLength); | ||
} | ||
|
||
@Test | ||
public void restoreKeyFromBytes() { | ||
byte[] buffer = { 3, 14, 15, 92, 6 }; | ||
SymmetricKey key = new SymmetricKey(buffer); | ||
assertArrayEquals(buffer, key.toByteArray()); | ||
} | ||
|
||
@Test(expected = NullArgumentException.class) | ||
public void restoreKeyFromNull() { | ||
SymmetricKey key = new SymmetricKey(null); | ||
} | ||
|
||
@Test(expected = InvalidArgumentException.class) | ||
public void restoreKeyFromEmpty() { | ||
SymmetricKey key = new SymmetricKey(new byte[0]); | ||
} | ||
} |