Changeset b93be8b for core


Ignore:
Timestamp:
Nov 25, 2018 3:23:17 PM (18 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
a8bacd8
Parents:
9badfd0
Message:

Crypto: Define ElG length constants

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/crypto/ElGamalEngine.java

    r9badfd0 rb93be8b  
    5959
    6060    private static final BigInteger ELGPM1 = CryptoConstants.elgp.subtract(BigInteger.ONE);
     61    private static final int ELG_CLEARTEXT_LENGTH = 222;
     62    private static final int ELG_ENCRYPTED_LENGTH = 514;
     63    private static final int ELG_HALF_LENGTH = ELG_ENCRYPTED_LENGTH / 2;
    6164
    6265   
     
    114117     */
    115118    public byte[] encrypt(byte data[], PublicKey publicKey) {
    116         if ((data == null) || (data.length >= 223))
    117             throw new IllegalArgumentException("Data to encrypt must be < 223 bytes at the moment");
     119        if ((data == null) || (data.length > ELG_CLEARTEXT_LENGTH))
     120            throw new IllegalArgumentException("Data to encrypt must be <= 222 bytes");
    118121        if (publicKey == null) throw new IllegalArgumentException("Null public key specified");
    119122
     
    150153        byte[] ybytes = y.toByteArray();
    151154        byte[] dbytes = d.toByteArray();
    152         byte[] out = new byte[514];
    153         System.arraycopy(ybytes, 0, out, (ybytes.length < 257 ? 257 - ybytes.length : 0),
    154                          (ybytes.length > 257 ? 257 : ybytes.length));
    155         System.arraycopy(dbytes, 0, out, (dbytes.length < 257 ? 514 - dbytes.length : 257),
    156                          (dbytes.length > 257 ? 257 : dbytes.length));
     155        byte[] out = new byte[ELG_ENCRYPTED_LENGTH];
     156        System.arraycopy(ybytes, 0, out, (ybytes.length < ELG_HALF_LENGTH ? ELG_HALF_LENGTH - ybytes.length : 0),
     157                         (ybytes.length > ELG_HALF_LENGTH ? ELG_HALF_LENGTH : ybytes.length));
     158        System.arraycopy(dbytes, 0, out, (dbytes.length < ELG_HALF_LENGTH ? ELG_ENCRYPTED_LENGTH - dbytes.length : ELG_HALF_LENGTH),
     159                         (dbytes.length > ELG_HALF_LENGTH ? ELG_HALF_LENGTH : dbytes.length));
    157160        /*
    158161        StringBuilder buf = new StringBuilder(1024);
     
    191194     */
    192195    public byte[] decrypt(byte encrypted[], PrivateKey privateKey) {
    193         if ((encrypted == null) || (encrypted.length != 514))
    194             throw new IllegalArgumentException("Data to decrypt must be exactly 514 bytes");
     196        if ((encrypted == null) || (encrypted.length != ELG_ENCRYPTED_LENGTH))
     197            throw new IllegalArgumentException("Data to decrypt must be exactly ELG_ENCRYPTED_LENGTH bytes");
    195198        long start = _context.clock().now();
    196199
     
    198201        BigInteger y1p = ELGPM1.subtract(a);
    199202        // we use this buf first for Y, then for D, then for the hash
    200         byte[] buf = SimpleByteCache.acquire(257);
    201         System.arraycopy(encrypted, 0, buf, 0, 257);
     203        byte[] buf = SimpleByteCache.acquire(ELG_HALF_LENGTH);
     204        System.arraycopy(encrypted, 0, buf, 0, ELG_HALF_LENGTH);
    202205        NativeBigInteger y = new NativeBigInteger(1, buf);
    203206        BigInteger ya = y.modPowCT(y1p, CryptoConstants.elgp);
    204         System.arraycopy(encrypted, 257, buf, 0, 257);
     207        System.arraycopy(encrypted, ELG_HALF_LENGTH, buf, 0, ELG_HALF_LENGTH);
    205208        BigInteger d = new NativeBigInteger(1, buf);
    206209        BigInteger m = ya.multiply(d);
Note: See TracChangeset for help on using the changeset viewer.