gyurix
gyurix

Reputation: 1086

Java Cryptonight hash algorithm - expanded AES key length?

I am working on a cryptonight algorithm based Java cryptocurrency miner application.

I am trying to implement the hashing function based on this document: https://cryptonote.org/cns/cns008.txt

My code:

public byte[] mine(String hash) {
    byte[] out = hash.getBytes();
    out = doKeccak(out);

    //Make key
    byte[] key = new byte[32];
    System.arraycopy(out, 0, key, 0, 32);

    //Make blocks
    byte[][] blocks = new byte[8][];
    for (int i = 0; i < 8; ++i)
        System.arraycopy(out, 64 + 16 * i, blocks[i], 0, 16);

    byte[][] keys = new byte[11][];
    keys[0]=key;
    for (int i = 0; i < 10; ++i) {
        keys[i+1]=new byte[32];
        Rijndael.expandKey(keys[i], keys[i+1], 0, 32, 32);
    }

    //byte[] pad = new byte[2097152];

    //Encrypt blocks
    for (int bid = 0; bid < 8; ++bid) {
        for (int i = 0; i < 10; ++i) {
            blocks[bid] = AES.encrypt(blocks[i], keys[i+1]);
        }
    }

    return Utils.byteToHex(out);
}

The usage of the Rijndael.expandKey method looks like that: public static void expandKey(byte[] key, byte[] out, int offset, int keySize, int expKeySize)

What number should I enter to the expanded key size? I can't see that in the documentation, that's why I am asking it here.

Upvotes: 3

Views: 590

Answers (1)

alexey28
alexey28

Reputation: 5230

Cryptonight uses modified AES algorithm. In standard algorithm you expand 256 bit key into 15 keys of 128 bit each. For cryptonight you need only 10 keys of 128 bit each. So the output length should be 1280 bit or 160 Bytes

The good article that explains key schedule with Java code samples (include case for Cryptonight modified AES algorithm):

http://cryptoexplained.tech/hash-algorithms/aes-key-schedule

And the code source:

https://github.com/cryptoexplained/hash-algorithms/blob/master/src/main/java/tech/cryptoexplained/hash/aes/Aes.java

Upvotes: 1

Related Questions