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