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