-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbcrypt.go
92 lines (81 loc) · 3.82 KB
/
tbcrypt.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
MIT License
Copyright (c) 2017 Simon Schmidt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
This package implements a Time-Based encryption system. A Message is encrypted
using the public key of the recipient and a "Time Ticket" which is a short-lived
public key generated by a "Time-Authority". The recipient can decrypt the message
if, and only if the "Time-Authority" owning the private key of the "Time Ticket"
"signs" the private key of the recipient. As the "Time-Authority" usually revokes
the "Time Ticket" destroying the associated private key, signatures will become
impossible after a "Time Ticket" has been revoked.
This cryptosystem is especially useful for defending people against omnipotent
power, for example oppressive government or "law enforcement". For example:
The UK Law-enforcement seizes computer(s)/laptop(s) from animal rights activists
and wrings their decryption keys and passwords from them. They're unable to
decrypt (most of) the recipient's messages, since the Time-Tickets of those
messages have already expired, an no one owns the associated private keys any
more. Even if the "Law-enforcement" seizes the Time-Authority, it could not gain
access to these messages anymore, since the wanted keys simply don't exist at
this point.
*/
package tbcrypt
import "golang.org/x/crypto/sha3"
import "golang.org/x/crypto/bn256"
import "math/big"
import "io"
import "errors"
/*
Signs a public key. This step is done by the Time-Authority. Note that the Signature
can still be used after the Time-Ticked has expired. The signature should be
communicated using a secure channel only (such as the Noise protocol), to avoid
anyone capturing the signature for later decryption of messages.
*/
func TA_Sign(r io.Reader, pub *bn256.G2, taPriv *big.Int) (S1 *bn256.G2,S2 []byte,E error) {
o,O,e := bn256.RandomG2(r)
if e!=nil { return nil,nil,e }
sdck := new(bn256.G2).ScalarMult(pub,taPriv).Marshal()
k := new(bn256.G2).ScalarMult(pub,o ).Marshal()
otp := make([]byte,len(sdck))
sha3.ShakeSum256(k,otp)
for i := range otp { sdck[i] ^= otp[i] }
return O,sdck,nil
}
/*
"Encrypts" a shared secret using a public key (pub) and a Time-Ticket (taPub).
*/
func Bob_Encrypt(r io.Reader, pub *bn256.G2, taPub *bn256.G1) (SharedSecret *bn256.GT, B *bn256.G1,E error) {
b,B,e := bn256.RandomG1(r)
if e!=nil { return nil,nil,e }
ptp := bn256.Pair(taPub,pub)
return new(bn256.GT).ScalarMult(ptp,b), B, nil
}
/*
"Decrypts" a shared secret using a signature and the private key.
*/
func Alice_Decrypt(S1 *bn256.G2,S2 []byte,priv *big.Int, B *bn256.G1) (SharedSecret *bn256.GT, E error) {
k := new(bn256.G2).ScalarMult(S1,priv).Marshal()
otp := make([]byte,len(S2))
sdck := make([]byte,len(S2))
sha3.ShakeSum256(k,otp)
for i := range otp { sdck[i] = otp[i]^S2[i] }
TS,ok := new(bn256.G2).Unmarshal(sdck)
if !ok { return nil,errors.New("Invalid public key") }
return bn256.Pair(B,TS),nil
}