| 1 |
package crypto |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/big" |
| 6 |
) |
| 7 |
|
| 8 |
type RSAKeyParameters interface { |
| 9 |
Modulus() *big.Int |
| 10 |
Exponent() *big.Int |
| 11 |
Private() bool |
| 12 |
} |
| 13 |
|
| 14 |
func NewRSAKeyParameters(isPrivate bool, modulus, exponent *big.Int) RSAKeyParameters { |
| 15 |
return &rsaKeyParameters{ |
| 16 |
privateKey: isPrivate, |
| 17 |
modulus: modulus, |
| 18 |
exponent: exponent, |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
type rsaKeyParameters struct { |
| 23 |
privateKey bool |
| 24 |
modulus *big.Int |
| 25 |
exponent *big.Int |
| 26 |
} |
| 27 |
|
| 28 |
func (r *rsaKeyParameters) Modulus() *big.Int { return r.modulus } |
| 29 |
func (r *rsaKeyParameters) Exponent() *big.Int { return r.exponent } |
| 30 |
func (r *rsaKeyParameters) Private() bool { return r.privateKey } |
| 31 |
|
| 32 |
func NewRSAPrivateCrtKeyParameters( |
| 33 |
modulus, |
| 34 |
publicExponent, |
| 35 |
privateExponent, |
| 36 |
p, |
| 37 |
q, |
| 38 |
dP, |
| 39 |
dQ, |
| 40 |
qInv *big.Int) *RSAPrivateCrtKeyParameters { |
| 41 |
return &RSAPrivateCrtKeyParameters{ |
| 42 |
RSAKeyParameters: NewRSAKeyParameters(true, modulus, privateExponent), |
| 43 |
e: publicExponent, |
| 44 |
p: p, |
| 45 |
q: q, |
| 46 |
dP: dP, |
| 47 |
dQ: dQ, |
| 48 |
qInv: qInv, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
type RSAPrivateCrtKeyParameters struct { |
| 53 |
RSAKeyParameters |
| 54 |
e *big.Int |
| 55 |
p *big.Int |
| 56 |
q *big.Int |
| 57 |
dP *big.Int |
| 58 |
dQ *big.Int |
| 59 |
qInv *big.Int |
| 60 |
} |
| 61 |
|
| 62 |
func (r *RSAPrivateCrtKeyParameters) PublicExponent() *big.Int { return r.e } |
| 63 |
func (r *RSAPrivateCrtKeyParameters) P() *big.Int { return r.p } |
| 64 |
func (r *RSAPrivateCrtKeyParameters) Q() *big.Int { return r.q } |
| 65 |
func (r *RSAPrivateCrtKeyParameters) DP() *big.Int { return r.dP } |
| 66 |
func (r *RSAPrivateCrtKeyParameters) DQ() *big.Int { return r.dQ } |
| 67 |
func (r *RSAPrivateCrtKeyParameters) QInv() *big.Int { return r.qInv } |
| 68 |
|
| 69 |
type RSAEngine struct { |
| 70 |
*RSACoreEngine |
| 71 |
} |
| 72 |
|
| 73 |
func (r *RSAEngine) Init(forEncryption bool, key RSAKeyParameters) { |
| 74 |
if r.RSACoreEngine == nil { |
| 75 |
r.RSACoreEngine = newRsaCoreEngine() |
| 76 |
} |
| 77 |
r.RSACoreEngine.Init(forEncryption, key) |
| 78 |
} |
| 79 |
|
| 80 |
func (r *RSAEngine) ProcessBlock(in []byte, inOff, inLen int) []byte { |
| 81 |
if r.RSACoreEngine == nil { |
| 82 |
panic(fmt.Errorf("RAS engine not initialized")) |
| 83 |
} |
| 84 |
return r.RSACoreEngine.ConvertOutput(r.RSACoreEngine.ProcessBlock(r.RSACoreEngine.ConvertInput(in, inOff, inLen))) |
| 85 |
} |
| 86 |
|
| 87 |
func newRsaCoreEngine() *RSACoreEngine { |
| 88 |
return &RSACoreEngine{} |
| 89 |
} |
| 90 |
|
| 91 |
type RSACoreEngine struct { |
| 92 |
key RSAKeyParameters |
| 93 |
forEncryption bool |
| 94 |
} |
| 95 |
|
| 96 |
func (r *RSACoreEngine) Init(forEncryption bool, key RSAKeyParameters) { |
| 97 |
r.key = key |
| 98 |
r.forEncryption = forEncryption |
| 99 |
} |
| 100 |
|
| 101 |
func (r *RSACoreEngine) InputBlockSize() int { |
| 102 |
bitSize := r.key.Modulus().BitLen() |
| 103 |
if r.forEncryption { |
| 104 |
return (bitSize+7)/8 - 1 |
| 105 |
} else { |
| 106 |
return (bitSize + 7) / 8 |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func (r *RSACoreEngine) OutputBlockSize() int { |
| 111 |
bitSize := r.key.Modulus().BitLen() |
| 112 |
if r.forEncryption { |
| 113 |
return (bitSize + 7) / 8 |
| 114 |
} else { |
| 115 |
return (bitSize+7)/8 - 1 |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func (r *RSACoreEngine) ConvertInput(in []byte, inOff, inLen int) *big.Int { |
| 120 |
if inLen > (r.InputBlockSize() + 1) { |
| 121 |
panic(fmt.Errorf("input too large for RSA cipher.")) |
| 122 |
} else if inLen == (r.InputBlockSize()+1) && !r.forEncryption { |
| 123 |
panic(fmt.Errorf("input too large for RSA cipher.")) |
| 124 |
} |
| 125 |
|
| 126 |
var block []byte |
| 127 |
|
| 128 |
if inOff != 0 || inLen != len(in) { |
| 129 |
block = make([]byte, inLen) |
| 130 |
// System.arraycopy(in, inOff, block, 0, inLen); |
| 131 |
arrayCopy(in, inOff, block, 0, inLen) |
| 132 |
} else { |
| 133 |
block = in |
| 134 |
} |
| 135 |
res := new(big.Int) |
| 136 |
res = res.Abs(res.SetBytes(block)) |
| 137 |
if res.Cmp(r.key.Modulus()) >= 0 { |
| 138 |
panic(fmt.Errorf("input too large for RSA cipher.")) |
| 139 |
} |
| 140 |
return res |
| 141 |
} |
| 142 |
|
| 143 |
func (r *RSACoreEngine) ConvertOutput(result *big.Int) []byte { |
| 144 |
output := result.Bytes() |
| 145 |
if r.forEncryption { |
| 146 |
if output[0] == 0 && len(output) > r.OutputBlockSize() { |
| 147 |
|
| 148 |
// have ended up with an extra zero byte, copy down. |
| 149 |
tmp := make([]byte, len(output)-1) |
| 150 |
|
| 151 |
// System.arraycopy(output, 1, tmp, 0, tmp.length); |
| 152 |
arrayCopy(output, 1, tmp, 0, len(tmp)) |
| 153 |
|
| 154 |
return tmp |
| 155 |
} |
| 156 |
|
| 157 |
if len(output) < r.OutputBlockSize() { // have ended up with less bytes than normal, lengthen |
| 158 |
tmp := make([]byte, r.OutputBlockSize()) |
| 159 |
|
| 160 |
// System.arraycopy(output, 0, tmp, tmp.length - output.length, output.length); |
| 161 |
arrayCopy(output, 0, tmp, len(tmp)-len(output), len(output)) |
| 162 |
|
| 163 |
return tmp |
| 164 |
} |
| 165 |
|
| 166 |
} else { |
| 167 |
if output[0] == 0 { // have ended up with an extra zero byte, copy down. |
| 168 |
tmp := make([]byte, len(output)-1) |
| 169 |
|
| 170 |
// System.arraycopy(output, 1, tmp, 0, tmp.length); |
| 171 |
arrayCopy(output, 1, tmp, 0, len(tmp)) |
| 172 |
|
| 173 |
return tmp |
| 174 |
} |
| 175 |
} |
| 176 |
return output |
| 177 |
} |
| 178 |
|
| 179 |
func (r *RSACoreEngine) ProcessBlock(input *big.Int) *big.Int { |
| 180 |
if crtKey, ok := r.key.(*RSAPrivateCrtKeyParameters); ok { |
| 181 |
// |
| 182 |
// we have the extra factors, use the Chinese Remainder Theorem - the author |
| 183 |
// wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for |
| 184 |
// advice regarding the expression of this. |
| 185 |
// |
| 186 |
|
| 187 |
p := crtKey.P() |
| 188 |
q := crtKey.Q() |
| 189 |
dP := crtKey.DP() |
| 190 |
dQ := crtKey.DQ() |
| 191 |
qInv := crtKey.QInv() |
| 192 |
|
| 193 |
var mP, mQ, h, m *big.Int |
| 194 |
|
| 195 |
// mP = ((input mod p) ^ dP)) mod p |
| 196 |
mP = mP.Exp(new(big.Int).Rem(input, p), dP, p) |
| 197 |
|
| 198 |
// mQ = ((input mod q) ^ dQ)) mod q |
| 199 |
mQ = mQ.Exp(new(big.Int).Rem(input, q), dQ, q) |
| 200 |
|
| 201 |
// h = qInv * (mP - mQ) mod p |
| 202 |
h = h.Sub(mP, mQ) |
| 203 |
h = h.Mul(h, qInv) |
| 204 |
h = h.Mod(h, p) // mod (in Java) returns the positive residual |
| 205 |
|
| 206 |
// m = h * q + mQ |
| 207 |
m = h.Mul(h, q) |
| 208 |
m = m.Add(m, mQ) |
| 209 |
|
| 210 |
return m |
| 211 |
} else { |
| 212 |
return new(big.Int).Exp(input, r.key.Exponent(), r.key.Modulus()) |
| 213 |
} |
| 214 |
} |