blockchainIDsystem/clientApp/ownrsa/rsa.go
clientApp/ownrsa/rsa.goRaw
1package ownrsa
2
3import (
4 "errors"
5 "fmt"
6 "math/big"
7 "math/rand"
8 "strconv"
9 "strings"
10 "time"
11)
12
13type RSAPublicKey struct {
14 E *big.Int `json:"e"`
15 N *big.Int `json:"n"`
16}
17type RSAPublicKeyString struct {
18 E string `json:"e"`
19 N string `json:"n"`
20}
21type RSAPrivateKey struct {
22 D *big.Int `json:"d"`
23 N *big.Int `json:"n"`
24}
25
26type RSA struct {
27 PubK RSAPublicKey
28 PrivK RSAPrivateKey
29}
30
31type PackRSA struct {
32 PubK string `json:"pubK"`
33 PrivK string `json:"privK"`
34 Date time.Time `json:"date"`
35 PubKSigned string `json:"pubKSigned"`
36 Verified bool `json:"verified"`
37 UnblindedSig string `json:"unblindedsig"`
38 BlockchainRef string `json:"blockchainref"`
39}
40
41const maxPrime = 500
42const minPrime = 100
43
44func GenerateKeyPair() RSA {
45
46 rand.Seed(time.Now().Unix())
47 p := randPrime(minPrime, maxPrime)
48 q := randPrime(minPrime, maxPrime)
49 fmt.Print("p:")
50 fmt.Println(p)
51 fmt.Print("q:")
52 fmt.Println(q)
53
54 n := p * q
55 phi := (p - 1) * (q - 1)
56 e := 65537
57 var pubK RSAPublicKey
58 pubK.E = big.NewInt(int64(e))
59 pubK.N = big.NewInt(int64(n))
60
61 d := new(big.Int).ModInverse(big.NewInt(int64(e)), big.NewInt(int64(phi)))
62
63 var privK RSAPrivateKey
64 privK.D = d
65 privK.N = big.NewInt(int64(n))
66
67 var rsa RSA
68 rsa.PubK = pubK
69 rsa.PrivK = privK
70 return rsa
71}
72func Encrypt(m string, pubK RSAPublicKey) []int {
73 var c []int
74 mBytes := []byte(m)
75 for _, byte := range mBytes {
76 c = append(c, EncryptInt(int(byte), pubK))
77 }
78 return c
79}
80func Decrypt(c []int, privK RSAPrivateKey) string {
81 var m string
82 var mBytes []byte
83 for _, indC := range c {
84 mBytes = append(mBytes, byte(DecryptInt(indC, privK)))
85 }
86 m = string(mBytes)
87 return m
88}
89
90func EncryptBigInt(bigint *big.Int, pubK RSAPublicKey) *big.Int {
91 Me := new(big.Int).Exp(bigint, pubK.E, nil)
92 c := new(big.Int).Mod(Me, pubK.N)
93 return c
94}
95func DecryptBigInt(bigint *big.Int, privK RSAPrivateKey) *big.Int {
96 Cd := new(big.Int).Exp(bigint, privK.D, nil)
97 m := new(big.Int).Mod(Cd, privK.N)
98 return m
99}
100
101func EncryptInt(char int, pubK RSAPublicKey) int {
102 charBig := big.NewInt(int64(char))
103 Me := charBig.Exp(charBig, pubK.E, nil)
104 c := Me.Mod(Me, pubK.N)
105 return int(c.Int64())
106}
107func DecryptInt(val int, privK RSAPrivateKey) int {
108 valBig := big.NewInt(int64(val))
109 Cd := valBig.Exp(valBig, privK.D, nil)
110 m := Cd.Mod(Cd, privK.N)
111 return int(m.Int64())
112}
113
114func Blind(m []int, r int, pubK RSAPublicKey) []int {
115 var mBlinded []int
116 rBigInt := big.NewInt(int64(r))
117 for i := 0; i < len(m); i++ {
118 mBigInt := big.NewInt(int64(m[i]))
119 rE := new(big.Int).Exp(rBigInt, pubK.E, nil)
120 mrE := new(big.Int).Mul(mBigInt, rE)
121 mrEmodN := new(big.Int).Mod(mrE, pubK.N)
122 mBlinded = append(mBlinded, int(mrEmodN.Int64()))
123 }
124 return mBlinded
125}
126
127func BlindSign(m []int, privK RSAPrivateKey) []int {
128 var r []int
129 for i := 0; i < len(m); i++ {
130 mBigInt := big.NewInt(int64(m[i]))
131 sigma := new(big.Int).Exp(mBigInt, privK.D, privK.N)
132 r = append(r, int(sigma.Int64()))
133 }
134 return r
135}
136func Unblind(blindsigned []int, r int, pubK RSAPublicKey) []int {
137 var mSigned []int
138 rBigInt := big.NewInt(int64(r))
139 for i := 0; i < len(blindsigned); i++ {
140 bsBigInt := big.NewInt(int64(blindsigned[i]))
141 //r1 := new(big.Int).Exp(rBigInt, big.NewInt(int64(-1)), nil)
142 r1 := new(big.Int).ModInverse(rBigInt, pubK.N)
143 bsr := new(big.Int).Mul(bsBigInt, r1)
144 sig := new(big.Int).Mod(bsr, pubK.N)
145 mSigned = append(mSigned, int(sig.Int64()))
146 }
147 return mSigned
148}
149func Verify(msg []int, mSigned []int, pubK RSAPublicKey) bool {
150 if len(msg) != len(mSigned) {
151 return false
152 }
153 var mSignedDecrypted []int
154 for _, ms := range mSigned {
155 msBig := big.NewInt(int64(ms))
156 //decrypt the mSigned with pubK
157 Cd := new(big.Int).Exp(msBig, pubK.E, nil)
158 m := new(big.Int).Mod(Cd, pubK.N)
159 mSignedDecrypted = append(mSignedDecrypted, int(m.Int64()))
160 }
161 fmt.Print("msg signed decrypted: ")
162 fmt.Println(mSignedDecrypted)
163 r := true
164 //check if the mSignedDecrypted == msg
165 for i := 0; i < len(msg); i++ {
166 if msg[i] != mSignedDecrypted[i] {
167 r = false
168 }
169 }
170 return r
171}
172
173func HomomorphicMultiplication(c1 int, c2 int, pubK RSAPublicKey) int {
174 c1BigInt := big.NewInt(int64(c1))
175 c2BigInt := big.NewInt(int64(c2))
176 c1c2 := new(big.Int).Mul(c1BigInt, c2BigInt)
177 n2 := new(big.Int).Mul(pubK.N, pubK.N)
178 d := new(big.Int).Mod(c1c2, n2)
179 r := int(d.Int64())
180 return r
181}
182
183func PubKStringToBigInt(kS RSAPublicKeyString) (RSAPublicKey, error) {
184 var k RSAPublicKey
185 var ok bool
186 k.E, ok = new(big.Int).SetString(kS.E, 10)
187 if !ok {
188 return k, errors.New("error parsing big int E")
189 }
190 k.N, ok = new(big.Int).SetString(kS.N, 10)
191 if !ok {
192 return k, errors.New("error parsing big int N")
193 }
194 return k, nil
195}
196
197func PackKey(k RSA) PackRSA {
198 var p PackRSA
199 p.PubK = k.PubK.E.String() + "," + k.PubK.N.String()
200 p.PrivK = k.PrivK.D.String() + "," + k.PrivK.N.String()
201 return p
202}
203
204func UnpackKey(p PackRSA) RSA {
205 var k RSA
206 var ok bool
207 k.PubK.E, ok = new(big.Int).SetString(strings.Split(p.PubK, ",")[0], 10)
208 k.PubK.N, ok = new(big.Int).SetString(strings.Split(p.PubK, ",")[1], 10)
209 k.PrivK.D, ok = new(big.Int).SetString(strings.Split(p.PrivK, ",")[0], 10)
210 k.PrivK.N, ok = new(big.Int).SetString(strings.Split(p.PrivK, ",")[1], 10)
211 if !ok {
212 fmt.Println("error on Unpacking Keys")
213 }
214 return k
215}
216
217func ArrayIntToString(a []int, delim string) string {
218 return strings.Trim(strings.Replace(fmt.Sprint(a), " ", delim, -1), "[]")
219}
220func StringToArrayInt(s string, delim string) []int {
221 var a []int
222 arrayString := strings.Split(s, delim)
223 for _, s := range arrayString {
224 i, err := strconv.Atoi(s)
225 if err != nil {
226 fmt.Println(err)
227 }
228 a = append(a, i)
229 }
230 return a
231}