math

package
v0.0.0-...-c0af43b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2019 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Copyright (C) 2018 NuCypher

This file is part of goUmbral.

goUmbral is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

goUmbral is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with goUmbral. If not, see <https://www.gnu.org/licenses/>.

This file is part of goUmbral.

goUmbral is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

goUmbral is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with goUmbral. If not, see <https://www.gnu.org/licenses/>.

This file is part of goUmbral.

goUmbral is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

goUmbral is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with goUmbral. If not, see <https://www.gnu.org/licenses/>.

Index

Constants

View Source
const (
	SALT_SIZE           = 32
	DEFAULT_SCRYPT_COST = 20
	SYMMETRIC_KEY_SIZE  = 128
	NONCE_SIZE          = 64
)

Variables

This section is empty.

Functions

func DefaultCurve

func DefaultCurve() *openssl.Curve

func DeriveKeyFromPassword

func DeriveKeyFromPassword(password []byte, salt []byte) []byte

* Derives a symmetric encryption key from a pair of password and salt. It uses Scrypt by default.

func ExpectedBytesLength

func ExpectedBytesLength(curve *openssl.Curve)

func NewDerivedKey

func NewDerivedKey(password []byte, salt []byte) ([]byte, error)

* Derives a symmetric encryption key from a pair of password and salt. WARNING: RFC7914 recommends that you use a 2^20 cost value for sensitive files. It is NOT recommended to change the `_scrypt_cost` value unless you know what you are doing.

func PointLength

func PointLength(curve *openssl.Curve, isCompressed bool) uint

Returns the size (in bytes) of a compressed Point given a curve. If no curve is provided, it returns 0.

func SetDefaultCurve

func SetDefaultCurve(curve *openssl.Curve) error

func UnwrapKey

func UnwrapKey(wrappedKey []byte, wrappingKey []byte, password []byte, nonce *[24]byte) []byte

* Unwraps a key using a provided wrapping key. Alternatively, it can derive the wrapping key from a password.

TODO can nonce be null? if so, delete that from params

func WrapKey

func WrapKey(keyToWrap []byte, wrappingKey []byte, password []byte, nonce *[24]byte) []byte

* Wraps a key using a provided wrapping key. Alternatively, it can derive the wrapping key from a password.

TODO look at pyUmbral keys function and nacl.secretbox python implementation, can nonce be null? if so, delete that from params

Types

type Config

type Config struct {
	Curve  *openssl.Curve
	Params *UmbralParameters
}

TODO pointer params so they can be nil

func NewConfig

func NewConfig(curve openssl.Curve, params UmbralParameters) Config

type ModBigNum

type ModBigNum struct {
	Bignum openssl.BigNum
	Curve  *openssl.Curve
}

func BytesToModBN

func BytesToModBN(data []byte, curve *openssl.Curve) (*ModBigNum, error)

Returns the ModBigNum associated with the bytes-converted bignum provided by the data argument.

func GenRandModBN

func GenRandModBN(curve *openssl.Curve) (*ModBigNum, error)

Returns a ModBigNum with a cryptographically secure OpenSSL BIGNUM based on the given curve.

func HashToModBN

func HashToModBN(bytes []byte, params *UmbralParameters) (*ModBigNum, error)

Returns a ModBigNum based on provided data hashed by blake2b.

func IntToModBN

func IntToModBN(num int, curve *openssl.Curve) (*ModBigNum, error)

func NewModBigNum

func NewModBigNum(cNum openssl.BigNum, curve *openssl.Curve) (*ModBigNum, error)

func (*ModBigNum) Add

func (z *ModBigNum) Add(x, y *ModBigNum) error

ModBigNum.Add() will perform (x + y) modulo the order of the curve of x and y. It will then set z to the result of that operation.

x, y, and z must use the same curve and must be initialized.

Add will return the error if one occurred, and nil otherwise.

func (*ModBigNum) Bytes

func (m *ModBigNum) Bytes() ([]byte, error)

func (ModBigNum) Compare

func (m ModBigNum) Compare(other *ModBigNum) int

func (*ModBigNum) Copy

func (m *ModBigNum) Copy() (*ModBigNum, error)

func (*ModBigNum) Div

func (z *ModBigNum) Div(x, y *ModBigNum) error

ModBigNum.Div() will perform (x / y) modulo the order of the curve of x and y. It will then set z to the result of that operation.

x, y, and z must use the same curve and must be initialized.

Div will return the error if one occurred, and nil otherwise.

func (*ModBigNum) Equals

func (m *ModBigNum) Equals(other *ModBigNum) bool

func (*ModBigNum) Free

func (m *ModBigNum) Free()

func (*ModBigNum) Invert

func (z *ModBigNum) Invert(x *ModBigNum) error

ModBigNum.Invert() computes (x*z)%m==1 where m is the order of the curve of x and z. It will then set z to the result of that operation.

x and z must use the same curve and must be initialized.

Invert will return the error if one occurred, and nil otherwise.

func (*ModBigNum) Mod

func (z *ModBigNum) Mod(x, y *ModBigNum) error

ModBigNum.Mod() will perform (x % y). It will then set z to the result of that operation.

x, y, and z must use the same curve and must be initialized.

Mod will return the error, and nil otherwise.

func (*ModBigNum) Mul

func (z *ModBigNum) Mul(x, y *ModBigNum) error

ModBigNum.Mul() will perform (x * y) modulo the order of the curve of x and y. It will then set z to the result of that operation.

x, y, and z must use the same curve and must be initialized.

Mul will return the error if one occurred, and nil otherwise.

func (*ModBigNum) Neg

func (z *ModBigNum) Neg(x *ModBigNum) error

ModBigNum.Neg() computes the modular opposite (i. e., additive inverse) of x. It will then set z to the result of that operation.

x and z must use the same curve and must be initialized.

Neg will return the error if one occurred, and nil otherwise.

func (*ModBigNum) Pow

func (z *ModBigNum) Pow(x, y *ModBigNum) error

ModBigNum.Pow() will perform (x^y) modulo the order of the curve of x and y. It will then set z to the result of that operation.

x, y, and z must use the same curve and must be initialized.

Pow will return the error if one occurred, and nil otherwise.

func (*ModBigNum) Sub

func (z *ModBigNum) Sub(x, y *ModBigNum) error

ModBigNum.Sub() will perform (x - y) modulo the order of the curve of x and y. It will then set z to the result of that operation.

x, y, and z must use the same curve and must be initialized.

Sub will return the error if one occurred, and nil otherwise.

type Point

type Point struct {
	ECPoint openssl.ECPoint
	Curve   *openssl.Curve
}

func AffineToPoint

func AffineToPoint(affineX, affineY *big.Int, curve *openssl.Curve) (*Point, error)

Returns a Point object from the given affine coordinates.

func BytesToPoint

func BytesToPoint(data []byte, curve *openssl.Curve) (*Point, error)

func GenRandPoint

func GenRandPoint(curve *openssl.Curve) (*Point, error)

Returns a Point struct with a cryptographically secure EC_POINT based on the provided curve.

This operation isn't safe unless you know the discrete log of the generated Point.

func GetGeneratorFromCurve

func GetGeneratorFromCurve(curve *openssl.Curve) *Point

func NewPoint

func NewPoint(point openssl.ECPoint, curve *openssl.Curve) (*Point, error)

Generate a new Point struct based on the arguments provided.

If point is nil then GetNewPoint will generate a new opensslraphically secure ECPoint and check for errors before returning the new Point.

if point is nil AND the curve group is also nil then GetNewPoint will fail and return the error.

func UnsafeHashToPoint

func UnsafeHashToPoint(data []byte, params *UmbralParameters, label []byte) (*Point, error)

WARNING: Do not use when the input data is secret, as this implementation is not in constant time, and hence, it is not safe with respect to timing attacks. TODO: Check how to uniformly generate ycoords. Currently, it only outputs points where ycoord is even (i.e., starting with 0x02 in compressed notation)

func (*Point) Add

func (z *Point) Add(x, y *Point) error

Point.Add() will perform (x + y). It will then set z to the result of that operation.

x, y, and z must use the same curve and must be initialized.

Add will return the error if one occurred, and nil otherwise.

func (*Point) Copy

func (m *Point) Copy() (*Point, error)

func (*Point) Equals

func (m *Point) Equals(other *Point) (bool, error)

func (*Point) Free

func (m *Point) Free()

func (*Point) Invert

func (z *Point) Invert(x *Point) error

Point.Invert() will find the inverse of x. It will then set z to the result of that operation.

x must be initialized.

Invert will return the error if one occurred, and nil otherwise.

func (*Point) Mul

func (z *Point) Mul(x *Point, y *ModBigNum) error

Point.Mul() will perform (x * y). It will then set z to the result of that operation.

x, y, and z must use the same curve and must be initialized.

Mul will return the error if one occurred, and nil otherwise.

func (*Point) Sub

func (z *Point) Sub(x, y *Point) error

Point.Sub() will perform (x - y). It will then set z to the result of that operation.

x, y, and z must use the same curve and must be initialized.

Sub will return the error if one occurred, and nil otherwise.

func (Point) ToAffine

func (m Point) ToAffine() (*big.Int, *big.Int, error)

Returns an x and y coordinate of the Point as a Go big.Int.

func (Point) ToBytes

func (m Point) ToBytes(isCompressed bool) ([]byte, error)

Returns the Point serialized as bytes. It will return a compressed form if isCompressed is set to True.

type UmbralKeyingMaterial

type UmbralKeyingMaterial struct {
	KeyingMaterial []byte
}

* This type with its methods handles keying material for Umbral, by allowing deterministic derivation of UmbralPrivateKeys based on labels. Don't use this key material directly as a key.

func KeyingMaterialFromBytes

func KeyingMaterialFromBytes(keyBytes []byte, wrappingKey []byte, password []byte, nonce *[24]byte) UmbralKeyingMaterial

func NewUmbralKeyingMaterial

func NewUmbralKeyingMaterial(keyingMaterial *[]byte) UmbralKeyingMaterial

func (UmbralKeyingMaterial) DerivePrivKeyByLabel

func (uKeyMat UmbralKeyingMaterial) DerivePrivKeyByLabel(label []byte, salt []byte, params *UmbralParameters) UmbralPrivateKey

func (UmbralKeyingMaterial) ToBytes

func (uKeyMat UmbralKeyingMaterial) ToBytes(wrappingKey []byte, password []byte, nonce *[24]byte) []byte

type UmbralParameters

type UmbralParameters struct {
	Curve *openssl.Curve
	Size  uint
	G     *Point
	U     *Point
}

func DefaultParams

func DefaultParams() *UmbralParameters

func NewUmbralParameters

func NewUmbralParameters(curve *openssl.Curve) (*UmbralParameters, error)

func (*UmbralParameters) Equals

func (m *UmbralParameters) Equals(other *UmbralParameters) bool

type UmbralPrivateKey

type UmbralPrivateKey struct {
	Params    UmbralParameters
	BnKey     ModBigNum
	PublicKey UmbralPublicKey
}

func GenerateUmbralPrivateKey

func GenerateUmbralPrivateKey(params *UmbralParameters) UmbralPrivateKey

Generates a private key and returns it.

func PrivateKeyFromBytes

func PrivateKeyFromBytes(keyBytes []byte, wrappingKey *[]byte, password *[]byte, params *UmbralParameters, nonce *[24]byte) UmbralPrivateKey

Loads an Umbral private key from bytes. Optionally, uses a wrapping key to unwrap an encrypted Umbral private key. Alternatively, if a password is provided it will derive the wrapping key from it.

func (UmbralPrivateKey) GetPublicKey

func (uPrivKey UmbralPrivateKey) GetPublicKey() UmbralPublicKey

func (UmbralPrivateKey) ToBytes

func (uPrivKey UmbralPrivateKey) ToBytes(wrappingKey *[]byte, password *[]byte, nonce *[24]byte) []byte

Returns an UmbralPrivateKey as bytes with optional symmetric encryption via nacl's Salsa20-Poly1305. If a password is provided instead of a wrapping key, it will use Scrypt for key derivation.

func (UmbralPrivateKey) ToCryptographyPrivKey

func (uPrivKey UmbralPrivateKey) ToCryptographyPrivKey() ecdsa.PrivateKey

type UmbralPublicKey

type UmbralPublicKey struct {
	PointKey Point
	Params   UmbralParameters
}

func FromHex

func FromHex(hexUmbralPubKey string, isCompressed bool) UmbralPublicKey

func PublicKeyFromBytes

func PublicKeyFromBytes(keyBytes []byte, params *UmbralParameters) UmbralPublicKey

* Loads an Umbral public key from bytes.

func (UmbralPublicKey) ByteString

func (uPubKey UmbralPublicKey) ByteString() ([]byte, error)

func (UmbralPublicKey) Equals

func (uPubKey UmbralPublicKey) Equals(umbralPubKey UmbralPublicKey) bool

func (UmbralPublicKey) ExpectedBytesLength

func (uPubKey UmbralPublicKey) ExpectedBytesLength(curve *openssl.Curve, isCompressed bool) uint

* Returns the size (in bytes) of an UmbralPublicKey given a curve. If no curve is provided, it uses the default curve. By default, it assumes compressed representation (is_compressed = True).

func (UmbralPublicKey) Hash

func (uPubKey UmbralPublicKey) Hash() int

func (UmbralPublicKey) ToBytes

func (uPubKey UmbralPublicKey) ToBytes(isCompressed bool) []byte

* Returns an Umbral public key as bytes.

func (UmbralPublicKey) ToHex

func (uPubKey UmbralPublicKey) ToHex(isCompressed bool) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL