| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.crypto.cipher.internal.symmetric; |
| 21 |
|
|
| 22 |
|
import java.io.FilterInputStream; |
| 23 |
|
import java.io.FilterOutputStream; |
| 24 |
|
import java.io.InputStream; |
| 25 |
|
import java.io.OutputStream; |
| 26 |
|
import java.security.GeneralSecurityException; |
| 27 |
|
|
| 28 |
|
import javax.crypto.BadPaddingException; |
| 29 |
|
import javax.crypto.IllegalBlockSizeException; |
| 30 |
|
|
| 31 |
|
import org.bouncycastle.crypto.BlockCipher; |
| 32 |
|
import org.bouncycastle.crypto.BufferedBlockCipher; |
| 33 |
|
import org.bouncycastle.crypto.CipherParameters; |
| 34 |
|
import org.bouncycastle.crypto.DataLengthException; |
| 35 |
|
import org.bouncycastle.crypto.InvalidCipherTextException; |
| 36 |
|
import org.bouncycastle.crypto.io.CipherInputStream; |
| 37 |
|
import org.bouncycastle.crypto.io.CipherOutputStream; |
| 38 |
|
import org.xwiki.crypto.cipher.SymmetricCipher; |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
@version |
| 44 |
|
@since |
| 45 |
|
|
| |
|
| 73.9% |
Uncovered Elements: 18 (69) |
Complexity: 24 |
Complexity Density: 0.56 |
|
| 46 |
|
public class BcSymmetricCipher implements SymmetricCipher |
| 47 |
|
{ |
| 48 |
|
|
| 49 |
|
protected final BufferedBlockCipher cipher; |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
protected final boolean forEncryption; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
@param |
| 58 |
|
@param |
| 59 |
|
@param |
| 60 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 61 |
0 |
BcSymmetricCipher(BlockCipher cipher, boolean forEncryption, CipherParameters parameters)... |
| 62 |
|
{ |
| 63 |
0 |
this.cipher = new BufferedBlockCipher(cipher); |
| 64 |
0 |
this.forEncryption = forEncryption; |
| 65 |
0 |
cipher.init(forEncryption, parameters); |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
@param |
| 72 |
|
@param |
| 73 |
|
@param |
| 74 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 75 |
43 |
BcSymmetricCipher(BufferedBlockCipher cipher, boolean forEncryption, CipherParameters parameters)... |
| 76 |
|
{ |
| 77 |
43 |
this.cipher = cipher; |
| 78 |
43 |
this.forEncryption = forEncryption; |
| 79 |
43 |
cipher.init(forEncryption, parameters); |
| 80 |
|
} |
| 81 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 82 |
0 |
@Override... |
| 83 |
|
public String getAlgorithmName() |
| 84 |
|
{ |
| 85 |
0 |
return this.cipher.getUnderlyingCipher().getAlgorithmName(); |
| 86 |
|
} |
| 87 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 88 |
0 |
@Override... |
| 89 |
|
public int getInputBlockSize() |
| 90 |
|
{ |
| 91 |
0 |
return this.cipher.getBlockSize(); |
| 92 |
|
} |
| 93 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 94 |
57 |
@Override... |
| 95 |
|
public int getOutputBlockSize() |
| 96 |
|
{ |
| 97 |
57 |
return this.cipher.getBlockSize(); |
| 98 |
|
} |
| 99 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 100 |
14 |
@Override... |
| 101 |
|
public boolean isForEncryption() |
| 102 |
|
{ |
| 103 |
14 |
return this.forEncryption; |
| 104 |
|
} |
| 105 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 106 |
28 |
@Override... |
| 107 |
|
public FilterInputStream getInputStream(InputStream is) |
| 108 |
|
{ |
| 109 |
28 |
this.cipher.reset(); |
| 110 |
28 |
return new CipherInputStream(is, this.cipher); |
| 111 |
|
} |
| 112 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 113 |
28 |
@Override... |
| 114 |
|
public FilterOutputStream getOutputStream(OutputStream os) |
| 115 |
|
{ |
| 116 |
28 |
this.cipher.reset(); |
| 117 |
28 |
return new CipherOutputStream(os, this.cipher); |
| 118 |
|
} |
| 119 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 120 |
0 |
@Override... |
| 121 |
|
public byte[] update(byte[] input) |
| 122 |
|
{ |
| 123 |
0 |
if (input != null) { |
| 124 |
0 |
return update(input, 0, input.length); |
| 125 |
|
} else { |
| 126 |
0 |
return update(null, 0, 0); |
| 127 |
|
} |
| 128 |
|
} |
| 129 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 130 |
112 |
@Override... |
| 131 |
|
public byte[] update(byte[] input, int inputOffset, int inputLen) |
| 132 |
|
{ |
| 133 |
112 |
int len = this.cipher.getUpdateOutputSize(inputLen); |
| 134 |
|
|
| 135 |
112 |
if (input == null || len == 0) { |
| 136 |
28 |
this.cipher.processBytes(input, inputOffset, inputLen, null, 0); |
| 137 |
28 |
return null; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
84 |
byte[] out = new byte[len]; |
| 141 |
84 |
return shrinkBuffer(out, this.cipher.processBytes(input, inputOffset, inputLen, out, 0)); |
| 142 |
|
} |
| 143 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 144 |
28 |
@Override... |
| 145 |
|
public byte[] doFinal() throws GeneralSecurityException |
| 146 |
|
{ |
| 147 |
28 |
return doFinal(null, 0, 0); |
| 148 |
|
} |
| 149 |
|
|
| |
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 150 |
70 |
@Override... |
| 151 |
|
public byte[] doFinal(byte[] input) throws GeneralSecurityException |
| 152 |
|
{ |
| 153 |
70 |
if (input != null) { |
| 154 |
70 |
return doFinal(input, 0, input.length); |
| 155 |
|
} else { |
| 156 |
0 |
return doFinal(null, 0, 0); |
| 157 |
|
} |
| 158 |
|
} |
| 159 |
|
|
| |
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 5 |
Complexity Density: 0.56 |
|
| 160 |
98 |
@Override... |
| 161 |
|
public byte[] doFinal(byte[] input, int inputOffset, int inputLen) throws GeneralSecurityException |
| 162 |
|
{ |
| 163 |
98 |
byte[] out = new byte[this.cipher.getOutputSize(inputLen)]; |
| 164 |
98 |
int len = 0; |
| 165 |
|
|
| 166 |
98 |
if (input != null && inputLen > 0) { |
| 167 |
70 |
len = this.cipher.processBytes(input, inputOffset, inputLen, out, 0); |
| 168 |
|
} |
| 169 |
|
|
| 170 |
98 |
try { |
| 171 |
98 |
len += this.cipher.doFinal(out, len); |
| 172 |
|
} catch (DataLengthException e) { |
| 173 |
0 |
throw new IllegalBlockSizeException(e.getMessage()); |
| 174 |
|
} catch (InvalidCipherTextException e) { |
| 175 |
0 |
throw new BadPaddingException(e.getMessage()); |
| 176 |
|
} |
| 177 |
98 |
return shrinkBuffer(out, len); |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
@param |
| 184 |
|
@param |
| 185 |
|
@return |
| 186 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 187 |
182 |
private byte[] shrinkBuffer(byte[] buffer, int size)... |
| 188 |
|
{ |
| 189 |
182 |
if (size == 0) { |
| 190 |
7 |
return null; |
| 191 |
|
} |
| 192 |
|
|
| 193 |
175 |
if (size == buffer.length) { |
| 194 |
139 |
return buffer; |
| 195 |
|
} |
| 196 |
|
|
| 197 |
36 |
byte[] output = new byte[size]; |
| 198 |
36 |
System.arraycopy(buffer, 0, output, 0, size); |
| 199 |
36 |
return output; |
| 200 |
|
} |
| 201 |
|
} |