| 1 |
package crypto |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/big" |
| 6 |
) |
| 7 |
|
| 8 |
type cipherEngine interface { |
| 9 |
Init(forEncryption bool, key RSAKeyParameters) |
| 10 |
ProcessBlock(in []byte, inOff, inLen int) []byte |
| 11 |
InputBlockSize() int |
| 12 |
OutputBlockSize() int |
| 13 |
} |
| 14 |
|
| 15 |
var sixteen = big.NewInt(16) |
| 16 |
var six = big.NewInt(6) |
| 17 |
|
| 18 |
var shadows = []byte{0xe, 0x3, 0x5, 0x8, 0x9, 0x4, 0x2, 0xf, |
| 19 |
0x0, 0xd, 0xb, 0x6, 0x7, 0xa, 0xc, 0x1} |
| 20 |
var inverse = []byte{0x8, 0xf, 0x6, 0x1, 0x5, 0x2, 0xb, 0xc, |
| 21 |
0x3, 0x4, 0xd, 0xa, 0xe, 0x9, 0x0, 0x7} |
| 22 |
|
| 23 |
/** |
| 24 |
* ISO 9796-1 padding. Note in the light of recent results you should |
| 25 |
* only use this with RSA (rather than the "simpler" Rabin keys) and you |
| 26 |
* should never use it with anything other than a hash (ie. even if the |
| 27 |
* message is small don't sign the message, sign it's hash) or some "random" |
| 28 |
* value. See your favorite search engine for details. |
| 29 |
*/ |
| 30 |
// implements AsymmetricBlockCipher |
| 31 |
type ISO9796d1Encoding struct { |
| 32 |
bitSize int |
| 33 |
padBits int |
| 34 |
modulus *big.Int |
| 35 |
engine cipherEngine |
| 36 |
forEncryption bool |
| 37 |
} |
| 38 |
|
| 39 |
func NewISO9796d1Encoding(cipher cipherEngine) *ISO9796d1Encoding { |
| 40 |
return &ISO9796d1Encoding{padBits: 0, engine: cipher} |
| 41 |
} |
| 42 |
|
| 43 |
func (i *ISO9796d1Encoding) GetUnderlyingCipher() cipherEngine { |
| 44 |
return i.engine |
| 45 |
} |
| 46 |
|
| 47 |
func (i *ISO9796d1Encoding) Init(forEncryption bool, key RSAKeyParameters) { |
| 48 |
i.engine.Init(forEncryption, key) |
| 49 |
i.modulus = key.Modulus() |
| 50 |
i.bitSize = i.modulus.BitLen() |
| 51 |
i.forEncryption = forEncryption |
| 52 |
} |
| 53 |
|
| 54 |
func (i *ISO9796d1Encoding) BlockSize() int { |
| 55 |
return i.BlockSize() |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* return the input block size. The largest message we can process |
| 60 |
* is (key_size_in_bits + 3)/16, which in our world comes to |
| 61 |
* key_size_in_bytes / 2. |
| 62 |
*/ |
| 63 |
func (i *ISO9796d1Encoding) InputBlockSize() int { |
| 64 |
baseBlockSize := i.engine.InputBlockSize() |
| 65 |
|
| 66 |
if i.forEncryption { |
| 67 |
return (baseBlockSize + 1) / 2 |
| 68 |
} else { |
| 69 |
return baseBlockSize |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* return the maximum possible size for the output. |
| 75 |
*/ |
| 76 |
func (i *ISO9796d1Encoding) OutputBlockSize() int { |
| 77 |
baseBlockSize := i.engine.OutputBlockSize() |
| 78 |
|
| 79 |
if i.forEncryption { |
| 80 |
return baseBlockSize |
| 81 |
} else { |
| 82 |
return (baseBlockSize + 1) / 2 |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* set the number of bits in the next message to be treated as |
| 88 |
* pad bits. |
| 89 |
*/ |
| 90 |
func (i *ISO9796d1Encoding) SetPadBits(padBits int) { |
| 91 |
if padBits > 7 { |
| 92 |
panic(fmt.Errorf("padBits > 7")) |
| 93 |
} |
| 94 |
|
| 95 |
i.padBits = padBits |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* retrieve the number of pad bits in the last decoded message. |
| 100 |
*/ |
| 101 |
func (i *ISO9796d1Encoding) PadBits() int { |
| 102 |
return i.padBits |
| 103 |
} |
| 104 |
|
| 105 |
func (i *ISO9796d1Encoding) ProcessBlock(in []byte, inOff, inLen int) ([]byte, error) { |
| 106 |
if i.forEncryption { |
| 107 |
return i.encodeBlock(in, inOff, inLen) |
| 108 |
} else { |
| 109 |
return i.decodeBlock(in, inOff, inLen) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func (i *ISO9796d1Encoding) encodeBlock(in []byte, inOff, inLen int) ([]byte, error) { |
| 114 |
block := make([]byte, (i.bitSize+7)/8) |
| 115 |
r := i.padBits + 1 |
| 116 |
z := inLen |
| 117 |
t := (i.bitSize + 13) / 16 |
| 118 |
|
| 119 |
for i := 0; i < t; i += z { |
| 120 |
if i > t-z { |
| 121 |
// System.arraycopy(in, inOff + inLen - (t - i), block, block.length - t, t - i); |
| 122 |
arrayCopy(in, inOff+inLen-(t-i), block, len(block)-t, t-i) |
| 123 |
} else { |
| 124 |
// System.arraycopy(in, inOff, block, block.length - (i + z), z); |
| 125 |
arrayCopy(in, inOff, block, len(block)-(i+z), z) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
for i := len(block) - 2*t; i != len(block); i += 2 { |
| 130 |
val := block[len(block)-t+i/2] |
| 131 |
block[i] = ((shadows[(val&0xff)>>4] << 4) | shadows[val&0x0f]) |
| 132 |
block[i+1] = val |
| 133 |
} |
| 134 |
|
| 135 |
block[len(block)-2*z] ^= byte(r) |
| 136 |
block[len(block)-1] = ((block[len(block)-1] << 4) | 0x06) |
| 137 |
|
| 138 |
maxBit := uint(8 - (i.bitSize-1)%8) |
| 139 |
offSet := 0 |
| 140 |
|
| 141 |
if maxBit != 8 { |
| 142 |
block[0] &= 0xff >> maxBit // block[0] &= 0xff >>> maxBit; |
| 143 |
block[0] |= 0x80 >> maxBit // block[0] |= 0x80 >>> maxBit; |
| 144 |
} else { |
| 145 |
block[0] = 0x00 |
| 146 |
block[1] |= 0x80 |
| 147 |
offSet = 1 |
| 148 |
} |
| 149 |
|
| 150 |
return i.engine.ProcessBlock(block, offSet, len(block)-offSet), nil |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* error if the decrypted block is not a valid ISO 9796 bit string |
| 155 |
*/ |
| 156 |
func (i *ISO9796d1Encoding) decodeBlock(in []byte, inOff, inLen int) ([]byte, error) { |
| 157 |
block := i.engine.ProcessBlock(in, inOff, inLen) |
| 158 |
r := 1 |
| 159 |
t := (i.bitSize + 13) / 16 |
| 160 |
|
| 161 |
iS := new(big.Int) |
| 162 |
iS = iS.Abs(iS.SetBytes(block)) |
| 163 |
var iR *big.Int |
| 164 |
x := new(big.Int) |
| 165 |
y := new(big.Int) |
| 166 |
if x = x.Mod(iS, sixteen); x.Cmp(six) == 0 { |
| 167 |
iR = iS |
| 168 |
} else if y = y.Mod(y.Sub(i.modulus, iS), sixteen); y.Cmp(six) == 0 { |
| 169 |
iR = iR.Sub(i.modulus, iS) |
| 170 |
} else { |
| 171 |
return nil, fmt.Errorf("resulting integer iS or (modulus - iS) is not congruent to 6 mod 16") |
| 172 |
} |
| 173 |
|
| 174 |
block = i.convertOutputDecryptOnly(iR) |
| 175 |
|
| 176 |
if (block[len(block)-1] & 0x0f) != 0x6 { |
| 177 |
return nil, fmt.Errorf("invalid forcing byte in block") |
| 178 |
} |
| 179 |
|
| 180 |
block[len(block)-1] = byte(((block[len(block)-1] & 0xff) >> 4) | ((inverse[(block[len(block)-2]&0xff)>>4]) << 4)) |
| 181 |
block[0] = byte((shadows[(block[1]&0xff)>>4] << 4) | shadows[block[1]&0x0f]) |
| 182 |
|
| 183 |
boundaryFound := false |
| 184 |
boundary := 0 |
| 185 |
|
| 186 |
for i := len(block) - 1; i < len(block)-2*t; i -= 2 { |
| 187 |
val := ((shadows[(block[i]&0xff)>>4] << 4) | shadows[block[i]&0x0f]) |
| 188 |
|
| 189 |
if ((block[i-1] ^ val) & 0xff) != 0 { |
| 190 |
if !boundaryFound { |
| 191 |
boundaryFound = true |
| 192 |
r = int((block[i-1] ^ val) & 0xff) |
| 193 |
boundary = i - 1 |
| 194 |
} else { |
| 195 |
return nil, fmt.Errorf("invalid tsums in block") |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
block[boundary] = 0 |
| 201 |
|
| 202 |
nblock := make([]byte, (len(block)-boundary)/2) |
| 203 |
|
| 204 |
for i := 0; i < len(nblock); i++ { |
| 205 |
nblock[i] = block[2*i+boundary+1] |
| 206 |
} |
| 207 |
|
| 208 |
i.padBits = r - 1 |
| 209 |
|
| 210 |
return nblock, nil |
| 211 |
} |
| 212 |
|
| 213 |
func (i *ISO9796d1Encoding) convertOutputDecryptOnly(result *big.Int) []byte { |
| 214 |
output := result.Bytes() |
| 215 |
if output[0] == 0 { // have ended up with an extra zero byte, copy down. |
| 216 |
tmp := make([]byte, len(output)-1) |
| 217 |
// System.arraycopy(output, 1, tmp, 0, tmp.length); |
| 218 |
arrayCopy(output, 1, tmp, 0, len(tmp)) |
| 219 |
return tmp |
| 220 |
} |
| 221 |
return output |
| 222 |
} |
| 223 |
|
| 224 |
func arrayCopy(src []byte, srcOff int, dst []byte, dstOff int, length int) { |
| 225 |
copy(dst[dstOff:], src[srcOff:srcOff+length]) |
| 226 |
} |