forked from cloudflare/circl
/
big_keys.go
57 lines (50 loc) · 1.35 KB
/
big_keys.go
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
package keys
import (
"crypto/rsa"
"math/big"
)
// BigPublicKey is the same as an rsa.PublicKey struct, except the public
// key is represented as a big integer as opposed to an int. For the partially
// blind scheme, this is required since the public key will typically be
// any value in the RSA group.
type BigPublicKey struct {
N *big.Int
E *big.Int
}
// Size returns the size of the public key.
func (pub *BigPublicKey) Size() int {
return (pub.N.BitLen() + 7) / 8
}
// Marshal encodes the public key exponent (e).
func (pub *BigPublicKey) Marshal() []byte {
buf := make([]byte, (pub.E.BitLen()+7)/8)
pub.E.FillBytes(buf)
return buf
}
// NewBigPublicKey creates a BigPublicKey from a rsa.PublicKey.
func NewBigPublicKey(pk *rsa.PublicKey) *BigPublicKey {
return &BigPublicKey{
N: pk.N,
E: new(big.Int).SetInt64(int64(pk.E)),
}
}
// CustomPublicKey is similar to rsa.PrivateKey, containing information needed
// for a private key used in the partially blind signature protocol.
type BigPrivateKey struct {
Pk *BigPublicKey
D *big.Int
P *big.Int
Q *big.Int
}
// NewBigPrivateKey creates a BigPrivateKey from a rsa.PrivateKey.
func NewBigPrivateKey(sk *rsa.PrivateKey) *BigPrivateKey {
return &BigPrivateKey{
Pk: &BigPublicKey{
N: sk.N,
E: new(big.Int).SetInt64(int64(sk.PublicKey.E)),
},
D: sk.D,
P: sk.Primes[0],
Q: sk.Primes[1],
}
}