|
| 1 | +package org.bouncycastle.pqc.crypto.mayo; |
| 2 | + |
| 3 | +import org.bouncycastle.crypto.BlockCipher; |
| 4 | +import org.bouncycastle.crypto.engines.AESEngine; |
| 5 | +import org.bouncycastle.crypto.modes.CTRModeCipher; |
| 6 | +import org.bouncycastle.crypto.modes.SICBlockCipher; |
| 7 | +import org.bouncycastle.crypto.params.KeyParameter; |
| 8 | +import org.bouncycastle.crypto.params.ParametersWithIV; |
| 9 | +import org.bouncycastle.util.Arrays; |
| 10 | +import org.bouncycastle.util.Pack; |
| 11 | + |
| 12 | +public class MayoEngine |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Expands P1 and P2 using AES_128_CTR as a PRF and then unpacks the resulting bytes |
| 16 | + * into an array of 64-bit limbs. |
| 17 | + * |
| 18 | + * @param p Mayo parameters |
| 19 | + * @param P The output long array which will hold the unpacked limbs. |
| 20 | + * Its length should be at least ((P1_bytes + P2_bytes) / 8) limbs. |
| 21 | + * @param seed_pk The seed (used as the key) for the PRF. |
| 22 | + * @return The number of bytes produced, i.e., P1_bytes + P2_bytes. |
| 23 | + */ |
| 24 | + public static int expandP1P2(MayoParameters p, long[] P, byte[] seed_pk) |
| 25 | + { |
| 26 | + // Compute total number of bytes to generate: P1_bytes + P2_bytes. |
| 27 | + int outLen = p.getP1Bytes() + p.getP2Bytes(); |
| 28 | + // Temporary byte array to hold the PRF output. |
| 29 | + byte[] temp = new byte[outLen]; |
| 30 | + |
| 31 | + // Call AES_128_CTR (our previously defined function using BouncyCastle) |
| 32 | + // to fill temp with outLen pseudorandom bytes using seed_pk as key. |
| 33 | + AES_128_CTR(temp, outLen, seed_pk, p.getPkSeedBytes()); |
| 34 | + |
| 35 | + // The number of vectors is the total limbs divided by mVecLimbs. |
| 36 | + int numVectors = (p.getP1Limbs() + p.getP2Limbs()) / p.getMVecLimbs(); |
| 37 | + |
| 38 | + // Unpack the byte array 'temp' into the long array 'P' |
| 39 | + // using our previously defined unpackMVecs method. |
| 40 | + Utils.unpackMVecs(temp, P, numVectors, p.getM()); |
| 41 | + |
| 42 | + // Return the number of output bytes produced. |
| 43 | + return outLen; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * AES_128_CTR generates outputByteLen bytes using AES-128 in CTR mode. |
| 48 | + * The key (of length keyLen) is used to expand the AES key. |
| 49 | + * A 16-byte IV (all zeros) is used. |
| 50 | + * |
| 51 | + * @param output the output buffer which will be filled with the keystream |
| 52 | + * @param outputByteLen the number of bytes to produce |
| 53 | + * @param key the AES key (should be 16 bytes for AES-128) |
| 54 | + * @param keyLen the length of the key (unused here but kept for similarity) |
| 55 | + * @return the number of output bytes produced (i.e. outputByteLen) |
| 56 | + */ |
| 57 | + public static int AES_128_CTR(byte[] output, int outputByteLen, byte[] key, int keyLen) |
| 58 | + { |
| 59 | + // Create a 16-byte IV (all zeros) |
| 60 | + byte[] iv = new byte[16]; // automatically zero-initialized |
| 61 | + |
| 62 | + // Set up AES engine in CTR (SIC) mode. |
| 63 | + BlockCipher aesEngine = AESEngine.newInstance(); |
| 64 | + // SICBlockCipher implements CTR mode for AES. |
| 65 | + CTRModeCipher ctrCipher = SICBlockCipher.newInstance(aesEngine); |
| 66 | + // Wrap the key with the IV. |
| 67 | + ParametersWithIV params = new ParametersWithIV(new KeyParameter(Arrays.copyOf(key, keyLen)), iv); |
| 68 | + ctrCipher.init(true, params); |
| 69 | + |
| 70 | + // CTR mode is a stream cipher: encrypting zero bytes produces the keystream. |
| 71 | + int blockSize = ctrCipher.getBlockSize(); // typically 16 bytes |
| 72 | + byte[] zeroBlock = new byte[blockSize]; // block of zeros |
| 73 | + byte[] blockOut = new byte[blockSize]; |
| 74 | + |
| 75 | + int offset = 0; |
| 76 | + // Process full blocks |
| 77 | + while (offset + blockSize <= outputByteLen) |
| 78 | + { |
| 79 | + ctrCipher.processBlock(zeroBlock, 0, blockOut, 0); |
| 80 | + System.arraycopy(blockOut, 0, output, offset, blockSize); |
| 81 | + offset += blockSize; |
| 82 | + } |
| 83 | + // Process any remaining partial block. |
| 84 | + if (offset < outputByteLen) |
| 85 | + { |
| 86 | + ctrCipher.processBlock(zeroBlock, 0, blockOut, 0); |
| 87 | + int remaining = outputByteLen - offset; |
| 88 | + System.arraycopy(blockOut, 0, output, offset, remaining); |
| 89 | + } |
| 90 | + return outputByteLen; |
| 91 | + } |
| 92 | + |
| 93 | + public static final int MAYO_OK = 0; |
| 94 | + public static final int PK_SEED_BYTES_MAX = 16; // Adjust as needed |
| 95 | + public static final int O_BYTES_MAX = 312; // Adjust as needed |
| 96 | + |
| 97 | + /** |
| 98 | + * Expands the secret key. |
| 99 | + * |
| 100 | + * @param p the MayoParameters instance. |
| 101 | + * @param csk the input secret key seed (byte array). |
| 102 | + * @param sk the Sk object that holds the expanded secret key components. |
| 103 | + * @return MAYO_OK on success. |
| 104 | + */ |
| 105 | +// public static int mayoExpandSk(MayoParameters p, byte[] csk, MayoPrivateKeyParameter sk) |
| 106 | +// { |
| 107 | +// int ret = MAYO_OK; |
| 108 | +// int totalS = PK_SEED_BYTES_MAX + O_BYTES_MAX; |
| 109 | +// byte[] S = new byte[totalS]; |
| 110 | +// |
| 111 | +// // sk.p is the long[] array, sk.O is the byte[] array. |
| 112 | +// |
| 113 | +// long[] P = new long[p.getPkSeedBytes() >> 3]; |
| 114 | +// Pack.littleEndianToLong(sk.getP(), 0, P); |
| 115 | +// byte[] O = sk.getO(); |
| 116 | +// |
| 117 | +// int param_o = p.getO(); |
| 118 | +// int param_v = p.getV(); |
| 119 | +// int param_O_bytes = p.getOBytes(); |
| 120 | +// int param_pk_seed_bytes = p.getPkSeedBytes(); |
| 121 | +// int param_sk_seed_bytes = p.getSkSeedBytes(); |
| 122 | +// |
| 123 | +// // In C, seed_sk = csk and seed_pk = S (the beginning of S) |
| 124 | +// byte[] seed_sk = csk; |
| 125 | +// byte[] seed_pk = S; // first param_pk_seed_bytes of S |
| 126 | +// |
| 127 | +// // Generate S = seed_pk || (additional bytes), using SHAKE256. |
| 128 | +// // Output length is param_pk_seed_bytes + param_O_bytes. |
| 129 | +// Utils.shake256(S, param_pk_seed_bytes + param_O_bytes, seed_sk, param_sk_seed_bytes); |
| 130 | +// |
| 131 | +// // Decode the portion of S after the first param_pk_seed_bytes into O. |
| 132 | +// // (In C, this is: decode(S + param_pk_seed_bytes, O, param_v * param_o)) |
| 133 | +// Utils.decode(S, param_pk_seed_bytes, O, param_v * param_o); |
| 134 | +// |
| 135 | +// // Expand P1 and P2 into the long array P using seed_pk. |
| 136 | +// MayoEngine.expandP1P2(p, P, seed_pk); |
| 137 | +// |
| 138 | +// // Let P2 start at offset = PARAM_P1_limbs(p) |
| 139 | +// int p1Limbs = p.getP1Limbs(); |
| 140 | +// int offsetP2 = p1Limbs; |
| 141 | +// |
| 142 | +// // Compute L_i = (P1 + P1^t)*O + P2. |
| 143 | +// // Here, we assume that P1P1tTimesO writes into the portion of P starting at offsetP2. |
| 144 | +// P1P1tTimesO(p, P, O, P, offsetP2); |
| 145 | +// |
| 146 | +// // Securely clear sensitive temporary data. |
| 147 | +// java.util.Arrays.fill(S, (byte)0); |
| 148 | +// return ret; |
| 149 | +// } |
| 150 | + |
| 151 | + /** |
| 152 | + * Multiplies and accumulates the product (P1 + P1^t)*O into the accumulator. |
| 153 | + * This version writes into the 'acc' array starting at the specified offset. |
| 154 | + * |
| 155 | + * @param p the MayoParameters. |
| 156 | + * @param P1 the P1 vector as a long[] array. |
| 157 | + * @param O the O array (each byte represents a GF(16) element). |
| 158 | + * @param acc the accumulator array where results are XORed in. |
| 159 | + * @param accOffset the starting index in acc. |
| 160 | + */ |
| 161 | + public static void P1P1tTimesO(MayoParameters p, long[] P1, byte[] O, long[] acc, int accOffset) |
| 162 | + { |
| 163 | + int paramO = p.getO(); |
| 164 | + int paramV = p.getV(); |
| 165 | + int mVecLimbs = p.getMVecLimbs(); |
| 166 | + int bsMatEntriesUsed = 0; |
| 167 | + for (int r = 0; r < paramV; r++) |
| 168 | + { |
| 169 | + for (int c = r; c < paramV; c++) |
| 170 | + { |
| 171 | + if (c == r) |
| 172 | + { |
| 173 | + bsMatEntriesUsed++; |
| 174 | + continue; |
| 175 | + } |
| 176 | + for (int k = 0; k < paramO; k++) |
| 177 | + { |
| 178 | + // Multiply the m-vector at P1 for the current matrix entry, |
| 179 | + // and accumulate into acc for row r. |
| 180 | + GF16Utils.mVecMulAdd(mVecLimbs, P1, bsMatEntriesUsed * mVecLimbs, |
| 181 | + O[c * paramO + k] & 0xFF, acc, accOffset + (r * paramO + k) * mVecLimbs); |
| 182 | + // Similarly, accumulate into acc for row c. |
| 183 | + GF16Utils.mVecMulAdd(mVecLimbs, P1, bsMatEntriesUsed * mVecLimbs, |
| 184 | + O[r * paramO + k] & 0xFF, acc, accOffset + (c * paramO + k) * mVecLimbs); |
| 185 | + } |
| 186 | + bsMatEntriesUsed++; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments