Skip to content

Commit

Permalink
リリース用
Browse files Browse the repository at this point in the history
テスト追加
Maven Central対応など
  • Loading branch information
okomeki committed Mar 28, 2022
1 parent c9d0e58 commit abea363
Show file tree
Hide file tree
Showing 15 changed files with 676 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ Apache 2.0 License としたいです。
<dependency>
<groupId>net.siisise</groupId>
<artifactId>softlib-crypto</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.1</version>
<scope>test</scope>
<type>jar</type>
</dependency>
~~~
バージョンは 1.0.1-SNAPSHOT です。
バージョンは 1.0.1 です。


6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.siisise</groupId>
<artifactId>softlib-crypto</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>SoftLibCrypt</name>
<description>Block Stream Digest Crypt for Java</description>
Expand Down Expand Up @@ -92,13 +92,13 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>[5.8.1,)</version>
<version>[5.8.2,)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.siisise</groupId>
<artifactId>softlib</artifactId>
<version>1.1.6-SNAPSHOT</version>
<version>1.1.6</version>
<type>jar</type>
</dependency>
</dependencies>
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/net/siisise/security/HKDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@
*/
public class HKDF {

private MessageDigest sha;
private HMAC hmac;

public HKDF(MessageDigest sha) {
this.sha = sha;
hmac = new HMAC(sha);
}

public HKDF(HMAC mac) {
hmac = mac;
}

/**
*
* @param salt 塩 (HMAC鍵) null可
* @param ikm 秘密鍵
* @param info 付加 null可
* @param info 付加 null可 saltっぽいもの
* @param length リクエスト鍵長 (HMACの255倍まで)
* @return
*/
Expand All @@ -43,33 +47,33 @@ byte[] extract( byte[] salt, byte[] ikm) {
if (salt == null) {
salt = new byte[0];
}
HMAC mac = new HMAC(sha, salt);
return mac.doFinal(ikm);
hmac.init(salt);
return hmac.doFinal(ikm);
}

/**
* 鍵長になるまで繰り返し.
*
* @param prk 中間鍵
* @param info 付加
* @param length 鍵長
* @return
* @param prk PRK 中間鍵
* @param info 付加 saltっぽいもの
* @param length L 鍵長 byte
* @return OKM output keying maerial (of L octets)
*/
private byte[] expand(byte[] prk, byte[] info, int length) {
int l = sha.getDigestLength();
int l = hmac.getMacLength();
int n = ((length + l - 1) / l);
if (info == null) {
info = new byte[0];
}
PacketS pt = new PacketS();
byte[] t = new byte[0];
HMAC mac = new HMAC(sha, prk);
hmac.init(prk);
byte[] d = new byte[1];
for (int i = 1; i <= n; i++) {
mac.update(t);
mac.update(info);
hmac.update(t);
hmac.update(info);
d[0] = (byte) i;
t = mac.doFinal(d);
t = hmac.doFinal(d);
pt.write(t);
}
byte[] r = new byte[length];
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/siisise/security/block/DES.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DES extends OneBlock {
* DESのブロック長.
* 64bit っぽい56bit
*
* @return
* @return 64
*/
@Override
public int getBlockLength() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/siisise/security/mac/HMAC.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private void setMD(MessageDigest md) {
}

/**
*
* HMACのバイト長.
* @return バイト長
*/
@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/siisise/security/mode/BlockMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public void init(byte[] key) {
block.init(key);
}

/**
* 初期化
*
* @param params 外側が後ろ
*/
@Override
public void init(byte[]... params) {
block.init(params);
Expand Down
53 changes: 42 additions & 11 deletions src/test/java/net/siisise/security/HKDFTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,53 @@ private byte[] toHex(String src) {
}

@Test
public void testSomeMethod() {
byte[] s = new byte[1];
public void testCase1() {
System.out.println("Test Case 1");
MessageDigest md = new SHA256();
HKDF hk = new HKDF(md);
byte[] ikm = toHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
byte[] ikm = toHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
byte[] salt = toHex("000102030405060708090a0b0c");
byte[] info = toHex("f0f1f2f3f4f5f6f7f8f9");
byte[] ep = toHex("077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5");
byte[] er = toHex("3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865");
byte[] eprk = toHex("077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5");
byte[] eokm = toHex("3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865");
byte[] prk = hk.extract(salt, ikm);
assertArrayEquals(ep,prk);
assertArrayEquals(eprk,prk);

byte[] r = hk.hkdf(salt, ikm, info, 42);
assertArrayEquals(er,r);
// TODO review the generated test code and remove the default call to fail.
// fail("The test case is a prototype.");
byte[] okm = hk.hkdf(salt, ikm, info, 42);
assertArrayEquals(eokm,okm);
}

@Test
public void testCase2() {
System.out.println("Test Case 2");
MessageDigest md = new SHA256();
HKDF hk = new HKDF(md);
byte[] ikm = toHex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f");
byte[] salt = toHex("606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf");
byte[] info = toHex("b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
byte[] eprk = toHex("06a6b88c5853361a06104c9ceb35b45cef760014904671014a193f40c15fc244");
byte[] eokm = toHex("b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c59045a99cac7827271cb41c65e590e09da3275600c2f09b8367793a9aca3db71cc30c58179ec3e87c14c01d5c1f3434f1d87");
byte[] prk = hk.extract(salt, ikm);
assertArrayEquals(eprk,prk);

byte[] okm = hk.hkdf(salt, ikm, info, 82);
assertArrayEquals(eokm,okm);
}

@Test
public void testCase3() {
System.out.println("Test Case 3");
MessageDigest md = new SHA256();
HKDF hk = new HKDF(md);
byte[] ikm = toHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
byte[] salt = toHex("");
byte[] info = toHex("");
byte[] eprk = toHex("19ef24a32c717b167f33a91d6f648bdf96596776afdb6377ac434c1c293ccb04");
byte[] eokm = toHex("8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8");
byte[] prk = hk.extract(salt, ikm);
assertArrayEquals(eprk,prk);

byte[] okm = hk.hkdf(salt, ikm, info, 42);
assertArrayEquals(eokm,okm);
}

}
28 changes: 28 additions & 0 deletions src/test/java/net/siisise/security/block/AESTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ public void testGetBlockLength() {
// fail("The test case is a prototype.");
}

@Test
public void testSbox() {
System.out.println("FIPS 197 Appendix B - Cipher Example");
byte[] key = { 0x2b, 0x7e, 0x15, 0x16, 0x28, (byte)0xae, (byte)0xd2, (byte)0xa6,
(byte)0xab, (byte)0xf7, 0x15, (byte)0x88, 0x09, (byte)0xcf, 0x4f, 0x3c };
byte[] iv = new byte[16];
Block aes = new CBC(new AES());
aes.init(key,iv);
// for ( int i = 0; i < 24; i++ ) {
// System.out.println("w["+i+"] " + Integer.toHexString(aes.w[i]) );
// }
// for ( int i = 0; i < aes.rcon.length; i++) {
// System.out.println(Integer.toHexString(aes.rcon[i]));
// }

byte[] input = {
0x32,0x43,(byte)0xf6,(byte)0xa8,(byte)0x88,0x5a,0x30,(byte)0x8d,
0x31,0x31,(byte)0x98,(byte)0xa2,(byte)0xe0,0x37,0x07,0x34};
byte[] output = aes.encrypt(input, 0);
aes.init(key,iv);
byte[] x = aes.decrypt(output, 0);
for ( int i = 0; i < x.length; i++) {
// System.out.println("in:" + input[i] + " out:" + x[i]);
assertArrayEquals(x, input);
}

}

@Test
public void testFips197AppendixB() {
System.out.println("FIPS 197 Appendix B - Cipher Example");
Expand Down
95 changes: 95 additions & 0 deletions src/test/java/net/siisise/security/block/DESTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package net.siisise.security.block;

import net.siisise.io.FileIO;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

/**
* https://www.ipa.go.jp/files/000013698.pdf
* [23] NIST SP 800-17 MOVS https://csrc.nist.gov/publications/detail/sp/800-17/archive/1998-02-01
*/
public class DESTest {

public DESTest() {
}

/**
* Test of getBlockLength method, of class DES.
*/
@Test
public void testGetBlockLength() {
System.out.println("getBlockLength");
DES des = new DES();
int expResult = 64;
int result = des.getBlockLength();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
// fail("The test case is a prototype.");
}

/**
* Test of encrypt method, of class DES.
*/
@Test
public void testEncrypt() {
System.out.println("encrypt");
byte[] src = null;
int offset = 0;
DES des = new DES();
byte[] expResult = null;
// byte[] result = des.encrypt(src, offset);

System.out.println("NIST 800-17 Appendix A Sample Round Outputs for the DES");
des = new DES();
byte[] key = new byte[] {0x10,0x31,0x6e,0x02,(byte)0x8c,(byte)0x8f,0x3b,0x4a};
src = new byte[8];
des.init(key);
byte[] x = des.encrypt(new byte[8],0);
FileIO.dump(x);

System.out.println("NIST END -------------");

byte[] in8 = {0x41,0x7a, 0x61, 0x74, 0x68,0x6f, 0x74, 0x68 };
// x = des.ip(in8,0);

// HMACTest.dump(in8);
// HMACTest.dump(x);
// byte[] ex = {(byte)0xff,(byte)0x4a,0x68,0x25, 0x00,(byte)0xfe,(byte)0xb2,0x22};
// HMACTest.dump(ex);

// assertArrayEquals(ex, x);

// byte[] x2 = des.ip_1(x);
// HMACTest.dump(x2);

// assertArrayEquals(in8, x2);

des.init(new byte[] {0,3,6,9,0,3,6,9});
x = des.encrypt(in8, 0);
// HMACTest.dump(in8);
// HMACTest.dump(x);
byte[] x2 = des.decrypt(x, 0);
// HMACTest.dump(ex);
assertArrayEquals(in8, x2);

// TODO review the generated test code and remove the default call to fail.
// fail("The test case is a prototype.");
}

/**
* Test of decrypt method, of class DES.
*/
@Test
public void testDecrypt() {
System.out.println("decrypt");
byte[] src = null;
int offset = 0;
DES instance = new DES();
byte[] expResult = null;
// byte[] result = des.decrypt(src, offset);
// assertArrayEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
// fail("The test case is a prototype.");
}

}
Loading

0 comments on commit abea363

Please sign in to comment.