Coverage Summary for Class: MGF1BytesGeneratorExt (org.ethereum.crypto)

Class Class, % Method, % Line, %
MGF1BytesGeneratorExt 0% (0/1) 0% (0/5) 0% (0/35)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) 5  * 6  * This program is free software: you can redistribute it and/or modify 7  * it under the terms of the GNU Lesser General Public License as published by 8  * the Free Software Foundation, either version 3 of the License, or 9  * (at your option) any later version. 10  * 11  * This program is distributed in the hope that it will be useful, 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14  * GNU Lesser General Public License for more details. 15  * 16  * You should have received a copy of the GNU Lesser General Public License 17  * along with this program. If not, see <http://www.gnu.org/licenses/>. 18  */ 19  20 package org.ethereum.crypto; 21  22 import org.bouncycastle.crypto.DataLengthException; 23 import org.bouncycastle.crypto.DerivationFunction; 24 import org.bouncycastle.crypto.DerivationParameters; 25 import org.bouncycastle.crypto.Digest; 26 import org.bouncycastle.crypto.params.MGFParameters; 27  28 /** 29  * This class is borrowed from spongycastle project 30  * The only change made is addition of 'counterStart' parameter to 31  * conform to Crypto++ capabilities 32  */ 33 public class MGF1BytesGeneratorExt implements DerivationFunction { 34  private Digest digest; 35  private byte[] seed; 36  private int hLen; 37  private int counterStart; 38  39  public MGF1BytesGeneratorExt(Digest digest, int counterStart) { 40  this.digest = digest; 41  this.hLen = digest.getDigestSize(); 42  this.counterStart = counterStart; 43  } 44  45  public void init(DerivationParameters param) { 46  if(!(param instanceof MGFParameters)) { 47  throw new IllegalArgumentException("MGF parameters required for MGF1Generator"); 48  } else { 49  MGFParameters p = (MGFParameters)param; 50  this.seed = p.getSeed(); 51  } 52  } 53  54  public Digest getDigest() { 55  return this.digest; 56  } 57  58  private void itoosp(int i, byte[] sp) { 59  sp[0] = (byte)(i >>> 24); 60  sp[1] = (byte)(i >>> 16); 61  sp[2] = (byte)(i >>> 8); 62  sp[3] = (byte)(i >>> 0); 63  } 64  65  public int generateBytes(byte[] out, int outOff, int len) throws DataLengthException, IllegalArgumentException { 66  if(out.length - len < outOff) { 67  throw new DataLengthException("output buffer too small"); 68  } else { 69  byte[] hashBuf = new byte[this.hLen]; 70  byte[] c = new byte[4]; 71  int counter = 0; 72  int hashCounter = counterStart; 73  this.digest.reset(); 74  if(len > this.hLen) { 75  do { 76  this.itoosp(hashCounter++, c); 77  this.digest.update(this.seed, 0, this.seed.length); 78  this.digest.update(c, 0, c.length); 79  this.digest.doFinal(hashBuf, 0); 80  System.arraycopy(hashBuf, 0, out, outOff + counter * this.hLen, this.hLen); 81  ++counter; 82  } while(counter < len / this.hLen); 83  } 84  85  if(counter * this.hLen < len) { 86  this.itoosp(hashCounter, c); 87  this.digest.update(this.seed, 0, this.seed.length); 88  this.digest.update(c, 0, c.length); 89  this.digest.doFinal(hashBuf, 0); 90  System.arraycopy(hashBuf, 0, out, outOff + counter * this.hLen, len - counter * this.hLen); 91  } 92  93  return len; 94  } 95  } 96 }