Skip to content

Commit

Permalink
TKSS-1030: Add tests on closing a native crypto instance twice
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshajiang committed Jan 9, 2025
1 parent db1d71d commit fba6178
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024, THL A29 Limited, a Tencent company. All rights reserved.
* Copyright (C) 2024, 2025, THL A29 Limited, a Tencent company. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -161,6 +161,14 @@ public void testSM2KeyPairGenUseClosedRef() {
sm2KeyPairGen::genKeyPair);
}

@Test
public void testCloseSM2KeyPairGenTwice() {
NativeSM2KeyPairGen sm2KeyPairGen = new NativeSM2KeyPairGen();
sm2KeyPairGen.genKeyPair();
sm2KeyPairGen.close();
sm2KeyPairGen.close();
}

@Test
public void testSM2Cipher() throws Exception {
try (NativeSM2KeyPairGen sm2KeyPairGen = new NativeSM2KeyPairGen()) {
Expand Down Expand Up @@ -264,6 +272,23 @@ public void testSM2CipherUseClosedRef() {
}
}

@Test
public void testCloseSM2CipherTwice() throws Exception {
try (NativeSM2KeyPairGen sm2KeyPairGen = new NativeSM2KeyPairGen()) {
byte[] keyPair = sm2KeyPairGen.genKeyPair();

NativeSM2Cipher sm2Encrypter = new NativeSM2Cipher(keyPair);
byte[] ciphertext = sm2Encrypter.encrypt(MESSAGE);
sm2Encrypter.close();
sm2Encrypter.close();

NativeSM2Cipher sm2Decrypter = new NativeSM2Cipher(keyPair);
sm2Decrypter.decrypt(ciphertext);
sm2Decrypter.close();
sm2Decrypter.close();
}
}

@Test
public void testSM2Signature() throws Exception {
try (NativeSM2KeyPairGen sm2KeyPairGen = new NativeSM2KeyPairGen()) {
Expand Down Expand Up @@ -383,6 +408,25 @@ public void testSM2SignatureUseClosedRef() {
}
}

@Test
public void testCloseSM2SignatureTwice() throws Exception {
try (NativeSM2KeyPairGen sm2KeyPairGen = new NativeSM2KeyPairGen()) {
byte[] keyPair = sm2KeyPairGen.genKeyPair();
byte[] priKey = copy(keyPair, 0, SM2_PRIKEY_LEN);
byte[] pubKey = copy(keyPair, SM2_PRIKEY_LEN, SM2_PUBKEY_LEN);

NativeSM2Signature sm2Signer = new NativeSM2Signature(priKey, pubKey, ID, true);
byte[] sign = sm2Signer.sign(MESSAGE);
sm2Signer.close();
sm2Signer.close();

NativeSM2Signature sm2Verifier = new NativeSM2Signature(pubKey, ID, false);
sm2Verifier.verify(MESSAGE, sign);
sm2Verifier.close();
sm2Signer.close();
}
}

@Test
public void testSM2SignatureTwice() throws Exception {
try (NativeSM2KeyPairGen sm2KeyPairGen = new NativeSM2KeyPairGen()) {
Expand Down Expand Up @@ -496,4 +540,30 @@ public void testSM2KeyAgreementUseClosedRef() {
true, 32));
}
}

@Test
public void testCloseSM2KeyAgreementTwice() {
try (NativeSM2KeyPairGen sm2KeyPairGen = new NativeSM2KeyPairGen()) {
byte[] keyPair = sm2KeyPairGen.genKeyPair();
byte[] priKey = copy(keyPair, 0, SM2_PRIKEY_LEN);
byte[] pubKey = copy(keyPair, SM2_PRIKEY_LEN, SM2_PUBKEY_LEN);

byte[] eKeyPair = sm2KeyPairGen.genKeyPair();
byte[] ePriKey = copy(eKeyPair, 0, SM2_PRIKEY_LEN);

byte[] peerKeyPair = sm2KeyPairGen.genKeyPair();
byte[] peerPubKey = copy(peerKeyPair, SM2_PRIKEY_LEN, SM2_PUBKEY_LEN);

byte[] peerEKeyPair = sm2KeyPairGen.genKeyPair();
byte[] peerEPubKey = copy(peerEKeyPair, SM2_PRIKEY_LEN, SM2_PUBKEY_LEN);

NativeSM2KeyAgreement sm2KeyAgreement = new NativeSM2KeyAgreement();
sm2KeyAgreement.deriveKey(
priKey, pubKey, ePriKey, ID,
peerPubKey, peerEPubKey, PEER_ID,
true, 32);
sm2KeyAgreement.close();
sm2KeyAgreement.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024, THL A29 Limited, a Tencent company. All rights reserved.
* Copyright (C) 2024, 2025, THL A29 Limited, a Tencent company. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -137,4 +137,12 @@ public void testUseClosedRef() {
IllegalStateException.class,
() -> sm3hmac.doFinal(MESSAGE));
}

@Test
public void testCloseTwice() {
NativeSM3HMac sm3hmac = new NativeSM3HMac(KEY);
sm3hmac.doFinal(MESSAGE);
sm3hmac.close();
sm3hmac.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024, THL A29 Limited, a Tencent company. All rights reserved.
* Copyright (C) 2024, 2025, THL A29 Limited, a Tencent company. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -133,4 +133,12 @@ public void testUseClosedRef() {
IllegalStateException.class,
() -> sm3.doFinal(MESSAGE_SHORT));
}

@Test
public void testCloseTwice() {
NativeSM3 sm3 = new NativeSM3();
sm3.doFinal(MESSAGE_LONG);
sm3.close();
sm3.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024, THL A29 Limited, a Tencent company. All rights reserved.
* Copyright (C) 2024, 2025, THL A29 Limited, a Tencent company. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -170,6 +170,23 @@ public void testCBCPaddingWithNullData() {
}
}

@Test
public void testUseClosedCBCRef() {
SM4CBC sm4 = new SM4CBC(true, false, KEY, IV);
sm4.close();

Assertions.assertThrows(
IllegalStateException.class,
() -> sm4.doFinal(MESSAGE_32));
}

@Test
public void testCloseCBCTwice() {
SM4CBC sm4 = new SM4CBC(true, false, KEY, IV);
sm4.close();
sm4.close();
}

@Test
public void testCTR() {
try(SM4CTR encrypter = new SM4CTR(true, KEY, IV)) {
Expand Down Expand Up @@ -248,6 +265,23 @@ public void testCTRWithNullData() {
}
}

@Test
public void testUseClosedCTRRef() {
SM4CTR sm4 = new SM4CTR(true, KEY, IV);
sm4.close();

Assertions.assertThrows(
IllegalStateException.class,
() -> sm4.doFinal(MESSAGE_32));
}

@Test
public void testCloseCTRTwice() {
SM4CTR sm4 = new SM4CTR(true, KEY, IV);
sm4.close();
sm4.close();
}

@Test
public void testECBNoPadding() {
try(SM4ECB encrypter = new SM4ECB(true, false, KEY)) {
Expand Down Expand Up @@ -341,6 +375,23 @@ public void testECBPaddingWithNullData() {
}
}

@Test
public void testUseClosedECBRef() {
SM4ECB sm4 = new SM4ECB(true, false, KEY);
sm4.close();

Assertions.assertThrows(
IllegalStateException.class,
() -> sm4.doFinal(MESSAGE_32));
}

@Test
public void testCloseECBTwice() {
SM4ECB sm4 = new SM4ECB(true, false, KEY);
sm4.close();
sm4.close();
}

@Test
public void testGCM() {
try(SM4GCM encrypter = new SM4GCM(true, KEY, GCM_IV)) {
Expand Down Expand Up @@ -452,6 +503,23 @@ public void testGCMWithNullData() {
}
}

@Test
public void testUseClosedGCMRef() {
SM4GCM sm4 = new SM4GCM(false, KEY, GCM_IV);
sm4.close();

Assertions.assertThrows(
IllegalStateException.class,
() -> sm4.doFinal(MESSAGE_32));
}

@Test
public void testCloseGCMTwice() {
SM4GCM sm4 = new SM4GCM(false, KEY, GCM_IV);
sm4.close();
sm4.close();
}

@Test
public void testKey() {
Assertions.assertThrows(
Expand All @@ -477,14 +545,4 @@ public void testIV() {
IllegalStateException.class,
()-> new SM4GCM(true, KEY, IV).close());
}

@Test
public void testUseClosedRef() {
SM4ECB sm4 = new SM4ECB(true, false, KEY);
sm4.close();

Assertions.assertThrows(
IllegalStateException.class,
() -> sm4.doFinal(MESSAGE_32));
}
}

0 comments on commit fba6178

Please sign in to comment.