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