Esempio n. 1
0
func NewIdentityKey(keytype int) (*IdentityKey, error) {
	var err error
	i := new(IdentityKey)

	switch keytype {
	case KEYRSA:
		i.keyType = keytype
		i.rsa, err = GenKeysRSA(rand.Reader)
	case KEYECDSA:
		i.keyType = keytype
		i.ecdsa, err = GenKeysECDSA(rand.Reader)
		//fmt.Printf("ECDSAAAAA: %v / %v\n", i.ecdsa, err)
		jsonProut, err := json.Marshal(i.ecdsa.Public())
		jsonTa, err := json.Marshal(i.ecdsa)
		fmt.Printf("ERROR: %s\n", err)
		b64comp, err := acutl.CompressData(jsonProut)
		b64pub := acutl.B64EncodeData(b64comp)
		fmt.Printf("JSON PublicKey: %s\n", jsonProut)
		fmt.Printf("JSON PublicKey: ac-ecdsa %s\n", b64pub)
		fmt.Printf("JSON AllKey: %s\n", jsonTa)
	case KEYEC25519:
		i.keyType = keytype
		i.ec25519, err = GenKeysED25519(rand.Reader)
	default:
		err = errors.New("invalid type")
		return nil, err
	}
	fmt.Printf("C'EST BON ON A FINI\n")
	return i, nil
}
Esempio n. 2
0
// A very pragmatic approach to protobuf encoding it's roughly true for most cases.
// XXX TODO: need to fix/clean that!
func PredictLenNACL(input []byte) (outlen int) {
	zipped, err := acutl.CompressData(input)
	if err != nil {
		return 0
	}
	sboxLen := len(zipped)        // zipped data
	sboxLen += secretbox.Overhead // NACL hash appended
	sboxLen += 3                  // 1 byte pb header value type + 2 bytes size  for the bytes part in PB message
	sboxLen += 6                  // 1 byte pb header + 1 byte size + 4 bytes data for AC header in PB message
	sboxLen += 7                  // 1 byte pb header value type + 2 byte size + 4 bytes nonce
	sboxLen += 2                  // 1 byte pb header value type + 1 byte size
	outlen = base64.StdEncoding.EncodedLen(sboxLen)
	//outlen += 14
	acutl.DebugLog.Printf("PredictLenNACL(%d): %d\n", len(input), outlen)
	return outlen
}
Esempio n. 3
0
func CreateACMessageNACL(context *ackp.SecKey, rnd, msg, myNick []byte) (out []byte, err error) {
	//var noncebyte [24]byte

	/* lets build our header */
	myHdr, intHdr, err := BuildHeader([]byte(msgHdrAC))
	if err != nil {
		return nil, &acutl.AcError{Value: -1, Msg: "CreateACMessageNACL().BuildHeader(): ", Err: err}
	}

	// first let's compress
	myBody, err := acutl.CompressData(msg)
	if err != nil {
		return nil, &acutl.AcError{Value: -2, Msg: "CreateACMessageNACL().CompressData(): ", Err: err}
	}

	//BuildNonceAC(inonce uint32, bob, mynick, myhdr []byte) (nonce []byte, noncebyte *[24]byte, err error)
	_, noncebyte, err := BuildNonceAC(context.GetNonce(), context.GetBob(), myNick, myHdr)

	// OPEN the key
	// XXX new to check rnd and context.key are the same size
	/*
		for j := 0; j < len(rnd); j++ {
			context.key[j] = context.key[j] ^ rnd[j]
		}
	*/
	context.RndKey(rnd)

	// encrypt
	myCipher := secretbox.Seal(nil, myBody, noncebyte, context.GetSealKey())

	// close the key
	context.RndKey(rnd)
	/*
		for j := 0; j < len(rnd); j++ {
			context.key[j] = context.key[j] ^ rnd[j]
		}
	*/

	// XXX error checking
	out, err = packMessageAC(&intHdr, context.GetNonce(), &myCipher)

	//fmt.Fprintf(os.Stderr, "NACL PB == AC MSG OUT[%d]: %s\n", len(out), out)
	//context.nonce++
	context.IncNonce(0)

	return out, nil
}
Esempio n. 4
0
func CreateKXMessageNACL(context *ackp.SecKey, rnd []byte, peerPubkey, myPrivkey *[32]byte, channel, myNick, peerNick []byte) (out []byte, err error) {

	/* lets build our header */
	myHdr, intHdr, err := BuildHeader([]byte(msgHdrKX))
	if err != nil {
		return nil, &acutl.AcError{Value: -1, Msg: "CreateKXMessageNACL().BuildHeader(): ", Err: err}
	}

	// Open the key
	context.RndKey(rnd)

	//fmt.Fprintf(os.Stderr, "CREATE KX KEY: %s\n", hex.EncodeToString(context.key[:]))
	// first let's compress
	myBody, err := acutl.CompressData(context.GetKey())
	if err != nil {
		return nil, &acutl.AcError{Value: -2, Msg: "CreateKXMessageNACL().CompressData(): ", Err: err}
	}

	// Close the key
	context.RndKey(rnd)

	//fmt.Fprintf(os.Stderr, "channel: %s context.bob: %s\n", channel, context.bob)

	kx_channel := IsChannelOrPriv(channel, myNick, peerNick)
	// XXX i can probably use context.bob instead of a specific channel specification...
	//BuildNonceAC(inonce uint32, bob, mynick, myhdr []byte) (nonce []byte, noncebyte *[24]byte, err error)
	_, noncebyte, err := BuildNonceKX(context.GetNonce(), kx_channel, myNick, peerNick, myHdr)

	//fmt.Fprintf(os.Stderr, "peerpk : %p myprivkey: %p\n", peerPubkey, myPrivkey)
	// XXX TODO: need serious cleanup and error checking!!
	//fmt.Fprintf(os.Stderr, "body.Bytes(): %p, noncebyte: %p, peerPub: %p myPriv: %p\n", myBody, &noncebyte, peerPubkey, myPrivkey)
	cipherKex := box.Seal(nil, myBody, noncebyte, peerPubkey, myPrivkey)

	//func packMessageKX(hdr, nonce *uint32, dst, blob *[]byte) (out []byte, err error) {
	out, err = packMessageKX(&intHdr, context.GetNonce(), peerNick, cipherKex)
	if err != nil {
		return nil, &acutl.AcError{Value: -3, Msg: "CreateKXMessageNACL().packMessageKX(): ", Err: err}
	}

	//context.nonce++
	context.IncNonce(0)
	return
}
Esempio n. 5
0
func CreatePKMessageNACL(pubkey []byte) (out []byte, err error) {
	/* lets build our header */
	_, intHdr, err := BuildHeader([]byte(msgHdrPK))
	if err != nil {
		return nil, &acutl.AcError{Value: -1, Msg: "CreatePKMessageNACL().BuildHeader(): ", Err: err}
	}

	// first let's compress
	myBody, err := acutl.CompressData(pubkey)
	if err != nil {
		return nil, &acutl.AcError{Value: -2, Msg: "CreatePKMessageNACL().CompressData(): ", Err: err}
	}

	out, err = packMessagePK(intHdr, myBody)
	if err != nil {
		return nil, &acutl.AcError{Value: -3, Msg: "CreatePKMessageNACL().PackMsg(): ", Err: err}
	}

	//fmt.Printf("Pubkey2Irc: %s\n", out)
	return out, nil
}