mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-05 14:13:30 -04:00
add base files
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
*~
|
||||||
|
\#*
|
||||||
|
.\#*
|
11
lib/bootstrap/bootstrap.go
Normal file
11
lib/bootstrap/bootstrap.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package bootstrap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bounce-chat/go-i2p/lib/stdi2p"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Reseed interface {
|
||||||
|
// do reseed, return nil on success otherwise error
|
||||||
|
// sends down all retrieved SU3 files down chan
|
||||||
|
Reseed(chnl chan *stdi2p.SU3) error
|
||||||
|
}
|
12
lib/bootstrap/reseed.go
Normal file
12
lib/bootstrap/reseed.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package bootstrap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bounce-chat/go-i2p/lib/stdi2p"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type HTTPSReseed string
|
||||||
|
|
||||||
|
func (r HTTPSReseed) Reseed(chnl chan *stdi2p.SU3) (err error) {
|
||||||
|
return
|
||||||
|
}
|
11
lib/common/base32/base32.go
Normal file
11
lib/common/base32/base32.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
//
|
||||||
|
// base32 encoding using i2p's alphabet
|
||||||
|
//
|
||||||
|
package base32
|
||||||
|
|
||||||
|
import (
|
||||||
|
b32 "encoding/base32"
|
||||||
|
)
|
||||||
|
|
||||||
|
// i2p base32 encoding
|
||||||
|
var I2PEncoding *b32.Encoding = b32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567")
|
11
lib/common/base64/base64.go
Normal file
11
lib/common/base64/base64.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
//
|
||||||
|
// base64 encoding with i2p's alphabet
|
||||||
|
//
|
||||||
|
package base64
|
||||||
|
|
||||||
|
import (
|
||||||
|
b64 "encoding/base64"
|
||||||
|
)
|
||||||
|
|
||||||
|
// i2p base64 encoding
|
||||||
|
var I2PEncoding *b64.Encoding = b64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~")
|
5
lib/common/data.go
Normal file
5
lib/common/data.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
|
||||||
|
// the sha256 of some datastructure
|
||||||
|
type IdentHash [32]byte
|
14
lib/common/utils.go
Normal file
14
lib/common/utils.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// check if a file is there and writeable
|
||||||
|
func FileExists(fname string) (exists bool) {
|
||||||
|
_, err := os.Stat(fname)
|
||||||
|
if err == nil {
|
||||||
|
exists = true
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
6
lib/config/bootstrap.go
Normal file
6
lib/config/bootstrap.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
|
||||||
|
type BootstrapConfig struct {
|
||||||
|
LowPeerThreshold int
|
||||||
|
}
|
13
lib/config/router.go
Normal file
13
lib/config/router.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
// router.config options
|
||||||
|
type RouterConfig struct {
|
||||||
|
NetDbDir string
|
||||||
|
|
||||||
|
Bootstrap BootstrapConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// defaults for router
|
||||||
|
var Router = &RouterConfig{
|
||||||
|
NetDbDir: "./netDb",
|
||||||
|
}
|
28
lib/crypto/aes.go
Normal file
28
lib/crypto/aes.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
type AesCBC struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type AesECB struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type TunnelKey [32]byte
|
||||||
|
type TunnelIV [16]byte
|
||||||
|
|
||||||
|
//
|
||||||
|
// tunnel aes base
|
||||||
|
//
|
||||||
|
type TunnelAes struct {
|
||||||
|
layerKey TunnelKey
|
||||||
|
ivKey TunnelKey
|
||||||
|
iv TunnelIV
|
||||||
|
}
|
||||||
|
|
||||||
|
type TunnelEncryption struct {
|
||||||
|
TunnelAes
|
||||||
|
}
|
||||||
|
|
||||||
|
type TunnelDecryption struct {
|
||||||
|
TunnelAes
|
||||||
|
}
|
71
lib/crypto/dsa.go
Normal file
71
lib/crypto/dsa.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/dsa"
|
||||||
|
"io"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
var dsap = new(big.Int).SetBytes([]byte{
|
||||||
|
0x9c, 0x05, 0xb2, 0xaa, 0x96, 0x0d, 0x9b, 0x97, 0xb8, 0x93, 0x19, 0x63, 0xc9, 0xcc, 0x9e, 0x8c,
|
||||||
|
0x30, 0x26, 0xe9, 0xb8, 0xed, 0x92, 0xfa, 0xd0, 0xa6, 0x9c, 0xc8, 0x86, 0xd5, 0xbf, 0x80, 0x15,
|
||||||
|
0xfc, 0xad, 0xae, 0x31, 0xa0, 0xad, 0x18, 0xfa, 0xb3, 0xf0, 0x1b, 0x00, 0xa3, 0x58, 0xde, 0x23,
|
||||||
|
0x76, 0x55, 0xc4, 0x96, 0x4a, 0xfa, 0xa2, 0xb3, 0x37, 0xe9, 0x6a, 0xd3, 0x16, 0xb9, 0xfb, 0x1c,
|
||||||
|
0xc5, 0x64, 0xb5, 0xae, 0xc5, 0xb6, 0x9a, 0x9f, 0xf6, 0xc3, 0xe4, 0x54, 0x87, 0x07, 0xfe, 0xf8,
|
||||||
|
0x50, 0x3d, 0x91, 0xdd, 0x86, 0x02, 0xe8, 0x67, 0xe6, 0xd3, 0x5d, 0x22, 0x35, 0xc1, 0x86, 0x9c,
|
||||||
|
0xe2, 0x47, 0x9c, 0x3b, 0x9d, 0x54, 0x01, 0xde, 0x04, 0xe0, 0x72, 0x7f, 0xb3, 0x3d, 0x65, 0x11,
|
||||||
|
0x28, 0x5d, 0x4c, 0xf2, 0x95, 0x38, 0xd9, 0xe3, 0xb6, 0x05, 0x1f, 0x5b, 0x22, 0xcc, 0x1c, 0x93,
|
||||||
|
})
|
||||||
|
|
||||||
|
var dsaq = new(big.Int).SetBytes([]byte{
|
||||||
|
0xa5, 0xdf, 0xc2, 0x8f, 0xef, 0x4c, 0xa1, 0xe2, 0x86, 0x74, 0x4c, 0xd8, 0xee, 0xd9, 0xd2, 0x9d,
|
||||||
|
0x68, 0x40, 0x46, 0xb7,
|
||||||
|
});
|
||||||
|
|
||||||
|
var dsag = new(big.Int).SetBytes([]byte{
|
||||||
|
0x0c, 0x1f, 0x4d, 0x27, 0xd4, 0x00, 0x93, 0xb4, 0x29, 0xe9, 0x62, 0xd7, 0x22, 0x38, 0x24, 0xe0,
|
||||||
|
0xbb, 0xc4, 0x7e, 0x7c, 0x83, 0x2a, 0x39, 0x23, 0x6f, 0xc6, 0x83, 0xaf, 0x84, 0x88, 0x95, 0x81,
|
||||||
|
0x07, 0x5f, 0xf9, 0x08, 0x2e, 0xd3, 0x23, 0x53, 0xd4, 0x37, 0x4d, 0x73, 0x01, 0xcd, 0xa1, 0xd2,
|
||||||
|
0x3c, 0x43, 0x1f, 0x46, 0x98, 0x59, 0x9d, 0xda, 0x02, 0x45, 0x18, 0x24, 0xff, 0x36, 0x97, 0x52,
|
||||||
|
0x59, 0x36, 0x47, 0xcc, 0x3d, 0xdc, 0x19, 0x7d, 0xe9, 0x85, 0xe4, 0x3d, 0x13, 0x6c, 0xdc, 0xfc,
|
||||||
|
0x6b, 0xd5, 0x40, 0x9c, 0xd2, 0xf4, 0x50, 0x82, 0x11, 0x42, 0xa5, 0xe6, 0xf8, 0xeb, 0x1c, 0x3a,
|
||||||
|
0xb5, 0xd0, 0x48, 0x4b, 0x81, 0x29, 0xfc, 0xf1, 0x7b, 0xce, 0x4f, 0x7f, 0x33, 0x32, 0x1c, 0x3c,
|
||||||
|
0xb3, 0xdb, 0xb1, 0x4a, 0x90, 0x5e, 0x7b, 0x2b, 0x3e, 0x93, 0xbe, 0x47, 0x08, 0xcb, 0xcc, 0x82,
|
||||||
|
});
|
||||||
|
|
||||||
|
var param = dsa.Parameters{
|
||||||
|
P: dsap,
|
||||||
|
Q: dsaq,
|
||||||
|
G: dsag,
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate a dsa keypair
|
||||||
|
func DSAGenerate(priv *dsa.PrivateKey, rand io.Reader) error {
|
||||||
|
// put our paramters in
|
||||||
|
priv.P = param.P
|
||||||
|
priv.Q = param.Q
|
||||||
|
priv.G = param.G
|
||||||
|
// generate the keypair
|
||||||
|
return dsa.GenerateKey(priv, rand)
|
||||||
|
}
|
||||||
|
|
||||||
|
// create i2p dsa public key given its public component
|
||||||
|
func DSAPublicKey(Y *big.Int) *dsa.PublicKey {
|
||||||
|
return &dsa.PublicKey{
|
||||||
|
Parameters: param,
|
||||||
|
Y: Y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// createa i2p dsa private key given its public component
|
||||||
|
func DSAPrivkey(X *big.Int) *dsa.PrivateKey {
|
||||||
|
Y := new(big.Int)
|
||||||
|
Y.Exp(dsag, X, dsap)
|
||||||
|
return &dsa.PrivateKey{
|
||||||
|
PublicKey: dsa.PublicKey{
|
||||||
|
Parameters: param,
|
||||||
|
Y: Y,
|
||||||
|
},
|
||||||
|
X: X,
|
||||||
|
}
|
||||||
|
}
|
28
lib/crypto/dsa_test.go
Normal file
28
lib/crypto/dsa_test.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/dsa"
|
||||||
|
"crypto/rand"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func TestDSA(t *testing.T) {
|
||||||
|
rng := rand.Reader
|
||||||
|
kp := new(dsa.PrivateKey)
|
||||||
|
err := DSAGenerate(kp, rng)
|
||||||
|
if err == nil {
|
||||||
|
t.Logf("DSA Key Pair generated")
|
||||||
|
} else {
|
||||||
|
t.Logf("error while generating key: %s", err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
h := make([]byte, 20)
|
||||||
|
_, _, err = dsa.Sign(rng, kp, h)
|
||||||
|
if err == nil {
|
||||||
|
t.Log("signed")
|
||||||
|
} else {
|
||||||
|
t.Logf("error signing: %s", err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
172
lib/crypto/elg.go
Normal file
172
lib/crypto/elg.go
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/subtle"
|
||||||
|
"errors"
|
||||||
|
"golang.org/x/crypto/openpgp/elgamal"
|
||||||
|
"io"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
var elgp = new(big.Int).SetBytes([]byte{
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34,
|
||||||
|
0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74,
|
||||||
|
0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
|
||||||
|
0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37,
|
||||||
|
0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45, 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6,
|
||||||
|
0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
|
||||||
|
0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6,
|
||||||
|
0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B, 0x3D, 0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF, 0x05,
|
||||||
|
0x98, 0xDA, 0x48, 0x36, 0x1C, 0x55, 0xD3, 0x9A, 0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF, 0x5F,
|
||||||
|
0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD, 0x96, 0x1C, 0x62, 0xF3, 0x56, 0x20, 0x85, 0x52, 0xBB,
|
||||||
|
0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D, 0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04,
|
||||||
|
0xF1, 0x74, 0x6C, 0x08, 0xCA, 0x18, 0x21, 0x7C, 0x32, 0x90, 0x5E, 0x46, 0x2E, 0x36, 0xCE, 0x3B,
|
||||||
|
0xE3, 0x9E, 0x77, 0x2C, 0x18, 0x0E, 0x86, 0x03, 0x9B, 0x27, 0x83, 0xA2, 0xEC, 0x07, 0xA2, 0x8F,
|
||||||
|
0xB5, 0xC5, 0x5D, 0xF0, 0x6F, 0x4C, 0x52, 0xC9, 0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17, 0x18,
|
||||||
|
0x39, 0x95, 0x49, 0x7C, 0xEA, 0x95, 0x6A, 0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10,
|
||||||
|
0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAC, 0xAA, 0x68, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
})
|
||||||
|
|
||||||
|
var one = big.NewInt(1)
|
||||||
|
var elgg = big.NewInt(2)
|
||||||
|
|
||||||
|
var ElgDecryptFail = errors.New("failed to decrypt elgamal encrypted data")
|
||||||
|
var ElgEncryptTooBig = errors.New("failed to encrypt data, too big for elgamal")
|
||||||
|
|
||||||
|
// generate an elgamal key pair
|
||||||
|
func ElgamalGenerate(priv *elgamal.PrivateKey, rand io.Reader) (err error) {
|
||||||
|
priv.P = elgp
|
||||||
|
priv.G = elgg
|
||||||
|
xBytes := make([]byte, priv.P.BitLen()/8)
|
||||||
|
_, err = io.ReadFull(rand, xBytes)
|
||||||
|
if err == nil {
|
||||||
|
// set private key
|
||||||
|
priv.X = new(big.Int).SetBytes(xBytes)
|
||||||
|
// compute public key
|
||||||
|
priv.Y = new(big.Int).Exp(priv.G, priv.X, priv.P)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// decrypt an elgamal encrypted message, i2p style
|
||||||
|
func ElgamelDecrypt(priv *elgamal.PrivateKey, data []byte, zeroPadding bool) (decrypted []byte, err error) {
|
||||||
|
a := new(big.Int)
|
||||||
|
b := new(big.Int)
|
||||||
|
idx := 0
|
||||||
|
if zeroPadding {
|
||||||
|
idx ++
|
||||||
|
}
|
||||||
|
a.SetBytes(data[idx : idx + 256])
|
||||||
|
if zeroPadding {
|
||||||
|
idx ++
|
||||||
|
}
|
||||||
|
b.SetBytes(data[idx + 256:])
|
||||||
|
|
||||||
|
// decrypt
|
||||||
|
m := new(big.Int).Mod(new(big.Int).Mul(b, new(big.Int).Exp(a,new(big.Int).Sub(new(big.Int).Sub(priv.P, priv.X), one), priv.P)), priv.P).Bytes()
|
||||||
|
|
||||||
|
// check digest
|
||||||
|
d := sha256.Sum256(m[33:255])
|
||||||
|
good := 0
|
||||||
|
if subtle.ConstantTimeCompare(d[:], m[1:33]) == 1 {
|
||||||
|
// decryption successful
|
||||||
|
good = 1
|
||||||
|
} else {
|
||||||
|
// decrypt failed
|
||||||
|
err = ElgDecryptFail
|
||||||
|
}
|
||||||
|
// copy result
|
||||||
|
decrypted = make([]byte, 222)
|
||||||
|
subtle.ConstantTimeCopy(good, decrypted, m[33:255])
|
||||||
|
|
||||||
|
if good == 0 {
|
||||||
|
// if decrypt failed nil out decrypted slice
|
||||||
|
decrypted = nil
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type ElgamalEncryption struct {
|
||||||
|
p, a, b1 *big.Int
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (elg *ElgamalEncryption) Encrypt(data []byte, zeroPadding bool) (encrypted []byte, err error) {
|
||||||
|
if len(data) > 222 {
|
||||||
|
err = ElgEncryptTooBig
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mbytes := make([]byte, 255)
|
||||||
|
mbytes[0] = 0xFF
|
||||||
|
copy(mbytes[33:], data)
|
||||||
|
// do sha256 of payload
|
||||||
|
d := sha256.Sum256(mbytes[33:len(data)+33])
|
||||||
|
copy(mbytes[1:], d[:])
|
||||||
|
m := new(big.Int).SetBytes(mbytes)
|
||||||
|
// do encryption
|
||||||
|
b := new(big.Int).Mod(new(big.Int).Mul(elg.b1, m), elg.p).Bytes()
|
||||||
|
|
||||||
|
if zeroPadding {
|
||||||
|
encrypted = make([]byte, 514)
|
||||||
|
copy(encrypted[1:], elg.a.Bytes())
|
||||||
|
copy(encrypted[258:], b)
|
||||||
|
} else {
|
||||||
|
encrypted = make([]byte, 512)
|
||||||
|
copy(encrypted, elg.a.Bytes())
|
||||||
|
copy(encrypted[256:], b)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create an elgamal public key from byte slice
|
||||||
|
func ElgamalPublicKey(data []byte) (k *elgamal.PublicKey) {
|
||||||
|
if len(data) == 256 {
|
||||||
|
k = &elgamal.PublicKey{
|
||||||
|
G: elgg,
|
||||||
|
P: elgp,
|
||||||
|
Y: new(big.Int).SetBytes(data),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create an elgamal private key from byte slice
|
||||||
|
func ElgamalPrivateKey(data []byte) (k *elgamal.PrivateKey) {
|
||||||
|
if len(data) == 256 {
|
||||||
|
x := new(big.Int).SetBytes(data)
|
||||||
|
y := new(big.Int).Exp(elgg, x, elgp)
|
||||||
|
k = &elgamal.PrivateKey{
|
||||||
|
PublicKey: elgamal.PublicKey{
|
||||||
|
Y: y,
|
||||||
|
G: elgg,
|
||||||
|
P: elgp,
|
||||||
|
},
|
||||||
|
X: x,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a new elgamal encryption session
|
||||||
|
func NewElgamalEncryption(pub *elgamal.PublicKey, rand io.Reader) (enc *ElgamalEncryption, err error) {
|
||||||
|
kbytes := make([]byte, 256)
|
||||||
|
k := new(big.Int)
|
||||||
|
for err == nil {
|
||||||
|
_, err = io.ReadFull(rand, kbytes)
|
||||||
|
k = new(big.Int).SetBytes(kbytes)
|
||||||
|
k = k.Mod(k, pub.P)
|
||||||
|
if k.Sign() != 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
enc = &ElgamalEncryption{
|
||||||
|
p: pub.P,
|
||||||
|
a: new(big.Int).Exp(pub.G, k, pub.P),
|
||||||
|
b1: new(big.Int).Exp(pub.Y, k, pub.P),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
50
lib/crypto/elg_test.go
Normal file
50
lib/crypto/elg_test.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/rand"
|
||||||
|
"golang.org/x/crypto/openpgp/elgamal"
|
||||||
|
"io"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func TestElg(t *testing.T) {
|
||||||
|
k := new(elgamal.PrivateKey)
|
||||||
|
err := ElgamalGenerate(k, rand.Reader)
|
||||||
|
if err == nil {
|
||||||
|
msg := make([]byte, 222)
|
||||||
|
_, err := io.ReadFull(rand.Reader, msg)
|
||||||
|
if err == nil {
|
||||||
|
pub := ElgamalPublicKey(k.Y.Bytes())
|
||||||
|
enc, err := NewElgamalEncryption(pub, rand.Reader)
|
||||||
|
if err == nil {
|
||||||
|
emsg, err := enc.Encrypt(msg, true)
|
||||||
|
if err == nil {
|
||||||
|
dec, err := ElgamelDecrypt(k, emsg, true)
|
||||||
|
if err == nil {
|
||||||
|
if ! bytes.Equal(dec, msg) {
|
||||||
|
t.Logf("%q != %q", dec, msg)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Logf("decrypt failed: %s", err.Error())
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Logf("failed to encrypt message: %s", err.Error())
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Logf("failed to create encryption: %s", err.Error())
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Logf("failed to generate random message: %s", err.Error())
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Logf("error while generating key: %s", err.Error())
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
43
lib/crypto/hmac.go
Normal file
43
lib/crypto/hmac.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
)
|
||||||
|
|
||||||
|
const IPAD = byte(0x36)
|
||||||
|
const OPAD = byte(0x5C)
|
||||||
|
|
||||||
|
type HMACKey [32]byte
|
||||||
|
type HMACDigest [16]byte
|
||||||
|
|
||||||
|
func (hk HMACKey) xor(p byte) (i []byte) {
|
||||||
|
i = make([]byte, 64)
|
||||||
|
for idx, b := range hk {
|
||||||
|
i[idx] = b ^ p
|
||||||
|
}
|
||||||
|
c := 32
|
||||||
|
for c > 0 {
|
||||||
|
c--
|
||||||
|
i[c+32] = p
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// do i2p hmac
|
||||||
|
//
|
||||||
|
func I2PHMAC(data []byte, k HMACKey) (d HMACDigest) {
|
||||||
|
|
||||||
|
buff := make([]byte, 64+len(data))
|
||||||
|
ip := k.xor(IPAD)
|
||||||
|
copy(buff, ip)
|
||||||
|
copy(buff[64:], data)
|
||||||
|
h := md5.Sum(buff)
|
||||||
|
|
||||||
|
buff = make([]byte, 96)
|
||||||
|
copy(buff, k.xor(OPAD))
|
||||||
|
copy(buff[64:], h[:])
|
||||||
|
// go zeros slices so we do not have to zero
|
||||||
|
d = md5.Sum(buff)
|
||||||
|
return
|
||||||
|
}
|
28
lib/crypto/hmac_test.go
Normal file
28
lib/crypto/hmac_test.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
// XXX: IMPLEMENT THIS
|
||||||
|
func Test_I2PHMAC(t *testing.T) {
|
||||||
|
data := make([]byte, 64)
|
||||||
|
for idx, _ := range data {
|
||||||
|
data[idx] = 1
|
||||||
|
}
|
||||||
|
var k HMACKey
|
||||||
|
for idx, _ := range k[:] {
|
||||||
|
k[idx] = 1
|
||||||
|
}
|
||||||
|
d := I2PHMAC(data, k)
|
||||||
|
expected_str := "WypV9tIaH1Kn9i7/9OqP6Q=="
|
||||||
|
expected, _ := base64.StdEncoding.DecodeString(expected_str)
|
||||||
|
if ! bytes.Equal(d[:], expected) {
|
||||||
|
t.Logf("%d vs %d", len(d), len(expected))
|
||||||
|
t.Logf("%q != %q", d, expected)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
5
lib/netdb/doc.go
Normal file
5
lib/netdb/doc.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
package netdb
|
1
lib/netdb/entry.go
Normal file
1
lib/netdb/entry.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package netdb
|
36
lib/netdb/std.go
Normal file
36
lib/netdb/std.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package netdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"github.com/majestrate/goi2p/lib/common"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// standard network database implementation
|
||||||
|
type StdNetDB string
|
||||||
|
|
||||||
|
|
||||||
|
// get netdb path
|
||||||
|
func (db StdNetDB) Path() string {
|
||||||
|
return string(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// return how many routers we know about in our network database
|
||||||
|
//
|
||||||
|
func (db StdNetDB) KnownPeerCount() (routers int) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// return true if the network db directory exists and is writable
|
||||||
|
func (db StdNetDB) Exists() bool {
|
||||||
|
return common.FileExists(db.Path())
|
||||||
|
}
|
||||||
|
|
||||||
|
// create base network database directory
|
||||||
|
func (db StdNetDB) Create() (err error) {
|
||||||
|
glog.Infof("Create network database in %s", db.Path())
|
||||||
|
err = os.Mkdir(db.Path(), 0600)
|
||||||
|
return
|
||||||
|
}
|
27
lib/router/router.go
Normal file
27
lib/router/router.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bounce-chat/go-i2p/lib/config"
|
||||||
|
"github.com/bounce-chat/go-i2p/lib/netdb"
|
||||||
|
)
|
||||||
|
|
||||||
|
// i2p router type
|
||||||
|
type Router struct {
|
||||||
|
cfg *config.RouterConfig
|
||||||
|
ndb netdb.StdNetDB
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func CreateRouter() (r *Router, err error) {
|
||||||
|
cfg := config.Router
|
||||||
|
r = &Router{
|
||||||
|
cfg: cfg,
|
||||||
|
ndb: netdb.StdNetDB(cfg.NetDbDir),
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// run i2p router mainloop
|
||||||
|
func (r *Router) Run() {
|
||||||
|
r.ndb.Run()
|
||||||
|
}
|
7
lib/ssu/doc.go
Normal file
7
lib/ssu/doc.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
i2p ssu transport implementation
|
||||||
|
|
||||||
|
*/
|
||||||
|
package ssu
|
||||||
|
|
1
lib/ssu/packet.go
Normal file
1
lib/ssu/packet.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package ssu
|
3
lib/ssu/session.go
Normal file
3
lib/ssu/session.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package ssu
|
||||||
|
|
||||||
|
|
7
lib/stdi2p/RouterIdentity.go
Normal file
7
lib/stdi2p/RouterIdentity.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package stdi2p
|
||||||
|
|
||||||
|
|
||||||
|
// Router Identity
|
||||||
|
type RouterIdentity struct {
|
||||||
|
|
||||||
|
}
|
6
lib/stdi2p/RouterInfo.go
Normal file
6
lib/stdi2p/RouterInfo.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package stdi2p
|
||||||
|
|
||||||
|
|
||||||
|
type RouterInfo struct {
|
||||||
|
|
||||||
|
}
|
2
lib/stdi2p/i2np.go
Normal file
2
lib/stdi2p/i2np.go
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
package stdi2p
|
||||||
|
|
7
lib/stdi2p/su3.go
Normal file
7
lib/stdi2p/su3.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package stdi2p
|
||||||
|
|
||||||
|
|
||||||
|
// an su3 archive
|
||||||
|
type SU3 struct {
|
||||||
|
|
||||||
|
}
|
26
main.go
Normal file
26
main.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
|
||||||
|
"github.com/bounce-chat/go-i2p/lib/router"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
glog.Info("parsing i2p router configuration")
|
||||||
|
|
||||||
|
glog.Info("starting up i2p router")
|
||||||
|
r, err := router.CreateRouter()
|
||||||
|
if err == nil {
|
||||||
|
r.Run()
|
||||||
|
} else {
|
||||||
|
glog.Errorf("failed to create i2p router: %s", err)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user