File: rsa.go

package info (click to toggle)
golang-github-mitch000001-go-hbci 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,468 kB
  • sloc: java: 1,092; makefile: 5
file content (214 lines) | stat: -rw-r--r-- 5,352 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package crypto

import (
	"fmt"
	"math/big"
)

type RSAKeyParameters interface {
	Modulus() *big.Int
	Exponent() *big.Int
	Private() bool
}

func NewRSAKeyParameters(isPrivate bool, modulus, exponent *big.Int) RSAKeyParameters {
	return &rsaKeyParameters{
		privateKey: isPrivate,
		modulus:    modulus,
		exponent:   exponent,
	}
}

type rsaKeyParameters struct {
	privateKey bool
	modulus    *big.Int
	exponent   *big.Int
}

func (r *rsaKeyParameters) Modulus() *big.Int  { return r.modulus }
func (r *rsaKeyParameters) Exponent() *big.Int { return r.exponent }
func (r *rsaKeyParameters) Private() bool      { return r.privateKey }

func NewRSAPrivateCrtKeyParameters(
	modulus,
	publicExponent,
	privateExponent,
	p,
	q,
	dP,
	dQ,
	qInv *big.Int) *RSAPrivateCrtKeyParameters {
	return &RSAPrivateCrtKeyParameters{
		RSAKeyParameters: NewRSAKeyParameters(true, modulus, privateExponent),
		e:                publicExponent,
		p:                p,
		q:                q,
		dP:               dP,
		dQ:               dQ,
		qInv:             qInv,
	}
}

type RSAPrivateCrtKeyParameters struct {
	RSAKeyParameters
	e    *big.Int
	p    *big.Int
	q    *big.Int
	dP   *big.Int
	dQ   *big.Int
	qInv *big.Int
}

func (r *RSAPrivateCrtKeyParameters) PublicExponent() *big.Int { return r.e }
func (r *RSAPrivateCrtKeyParameters) P() *big.Int              { return r.p }
func (r *RSAPrivateCrtKeyParameters) Q() *big.Int              { return r.q }
func (r *RSAPrivateCrtKeyParameters) DP() *big.Int             { return r.dP }
func (r *RSAPrivateCrtKeyParameters) DQ() *big.Int             { return r.dQ }
func (r *RSAPrivateCrtKeyParameters) QInv() *big.Int           { return r.qInv }

type RSAEngine struct {
	*RSACoreEngine
}

func (r *RSAEngine) Init(forEncryption bool, key RSAKeyParameters) {
	if r.RSACoreEngine == nil {
		r.RSACoreEngine = newRsaCoreEngine()
	}
	r.RSACoreEngine.Init(forEncryption, key)
}

func (r *RSAEngine) ProcessBlock(in []byte, inOff, inLen int) []byte {
	if r.RSACoreEngine == nil {
		panic(fmt.Errorf("RAS engine not initialized"))
	}
	return r.RSACoreEngine.ConvertOutput(r.RSACoreEngine.ProcessBlock(r.RSACoreEngine.ConvertInput(in, inOff, inLen)))
}

func newRsaCoreEngine() *RSACoreEngine {
	return &RSACoreEngine{}
}

type RSACoreEngine struct {
	key           RSAKeyParameters
	forEncryption bool
}

func (r *RSACoreEngine) Init(forEncryption bool, key RSAKeyParameters) {
	r.key = key
	r.forEncryption = forEncryption
}

func (r *RSACoreEngine) InputBlockSize() int {
	bitSize := r.key.Modulus().BitLen()
	if r.forEncryption {
		return (bitSize+7)/8 - 1
	} else {
		return (bitSize + 7) / 8
	}
}

func (r *RSACoreEngine) OutputBlockSize() int {
	bitSize := r.key.Modulus().BitLen()
	if r.forEncryption {
		return (bitSize + 7) / 8
	} else {
		return (bitSize+7)/8 - 1
	}
}

func (r *RSACoreEngine) ConvertInput(in []byte, inOff, inLen int) *big.Int {
	if inLen > (r.InputBlockSize() + 1) {
		panic(fmt.Errorf("input too large for RSA cipher."))
	} else if inLen == (r.InputBlockSize()+1) && !r.forEncryption {
		panic(fmt.Errorf("input too large for RSA cipher."))
	}

	var block []byte

	if inOff != 0 || inLen != len(in) {
		block = make([]byte, inLen)
		// System.arraycopy(in, inOff, block, 0, inLen);
		arrayCopy(in, inOff, block, 0, inLen)
	} else {
		block = in
	}
	res := new(big.Int)
	res = res.Abs(res.SetBytes(block))
	if res.Cmp(r.key.Modulus()) >= 0 {
		panic(fmt.Errorf("input too large for RSA cipher."))
	}
	return res
}

func (r *RSACoreEngine) ConvertOutput(result *big.Int) []byte {
	output := result.Bytes()
	if r.forEncryption {
		if output[0] == 0 && len(output) > r.OutputBlockSize() {

			// have ended up with an extra zero byte, copy down.
			tmp := make([]byte, len(output)-1)

			// System.arraycopy(output, 1, tmp, 0, tmp.length);
			arrayCopy(output, 1, tmp, 0, len(tmp))

			return tmp
		}

		if len(output) < r.OutputBlockSize() { // have ended up with less bytes than normal, lengthen
			tmp := make([]byte, r.OutputBlockSize())

			// System.arraycopy(output, 0, tmp, tmp.length - output.length, output.length);
			arrayCopy(output, 0, tmp, len(tmp)-len(output), len(output))

			return tmp
		}

	} else {
		if output[0] == 0 { // have ended up with an extra zero byte, copy down.
			tmp := make([]byte, len(output)-1)

			// System.arraycopy(output, 1, tmp, 0, tmp.length);
			arrayCopy(output, 1, tmp, 0, len(tmp))

			return tmp
		}
	}
	return output
}

func (r *RSACoreEngine) ProcessBlock(input *big.Int) *big.Int {
	if crtKey, ok := r.key.(*RSAPrivateCrtKeyParameters); ok {
		//
		// we have the extra factors, use the Chinese Remainder Theorem - the author
		// wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for
		// advice regarding the expression of this.
		//

		p := crtKey.P()
		q := crtKey.Q()
		dP := crtKey.DP()
		dQ := crtKey.DQ()
		qInv := crtKey.QInv()

		var mP, mQ, h, m *big.Int

		// mP = ((input mod p) ^ dP)) mod p
		mP = mP.Exp(new(big.Int).Rem(input, p), dP, p)

		// mQ = ((input mod q) ^ dQ)) mod q
		mQ = mQ.Exp(new(big.Int).Rem(input, q), dQ, q)

		// h = qInv * (mP - mQ) mod p
		h = h.Sub(mP, mQ)
		h = h.Mul(h, qInv)
		h = h.Mod(h, p) // mod (in Java) returns the positive residual

		// m = h * q + mQ
		m = h.Mul(h, q)
		m = m.Add(m, mQ)

		return m
	} else {
		return new(big.Int).Exp(input, r.key.Exponent(), r.key.Modulus())
	}
}