本文整理匯總了Golang中crypto/elliptic.Unmarshal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Unmarshal函數的具體用法?Golang Unmarshal怎麽用?Golang Unmarshal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Unmarshal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: UnmarshalMark
func UnmarshalMark(c elliptic.Curve, bytes []byte) *Mark {
bytelen := (c.Params().BitSize + 7) >> 3
pointlen := 1 + 2*bytelen
if len(bytes) != 2*pointlen {
return nil
}
ret := new(Mark)
ret.ax, ret.ay = elliptic.Unmarshal(c, bytes[:pointlen])
ret.bx, ret.by = elliptic.Unmarshal(c, bytes[pointlen:2*pointlen])
return ret
}
示例2: readECPoint
// Decode Q point from CKA_EC_POINT attribute
func readECPoint(curve elliptic.Curve, ecpoint []byte) (*big.Int, *big.Int) {
x, y := elliptic.Unmarshal(curve, ecpoint)
if x == nil {
// http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/os/pkcs11-curr-v2.40-os.html#_ftn1
// PKCS#11 v2.20 specified that the CKA_EC_POINT was to be store in a DER-encoded
// OCTET STRING.
var point asn1.RawValue
asn1.Unmarshal(ecpoint, &point)
if len(point.Bytes) > 0 {
x, y = elliptic.Unmarshal(curve, point.Bytes)
}
}
return x, y
}
示例3: loadPublicKey
func loadPublicKey(csr *CertificateSignatureRequest, req *certificateRequest) bool {
var pkInfo = req.Info.PKInfo
var algo = pkInfo.Algorithm.Algorithm
switch {
case algo.Equal(asn1RSAEncryption):
csr.Algo = RSA
var pub rsa.PublicKey
_, err := asn1.Unmarshal(pkInfo.Public.Bytes, &pub)
if err != nil {
return false
}
csr.Public = pub
return true
case algo.Equal(asn1ECCEncryption):
csr.Algo = ECDSA
var pub ecdsa.PublicKey
curveOID := decodeOID(req.Info.PKInfo.Algorithm.Parameters.FullBytes)
if curveOID == nil {
return false
}
pub.Curve = oidToCurve(curveOID)
if pub.Curve == nil {
return false
}
pub.X, pub.Y = elliptic.Unmarshal(pub.Curve, req.Info.PKInfo.Public.Bytes)
if pub.X == nil {
return false
}
csr.Public = pub
return true
default:
return false
}
}
示例4: UnmarshalPublic
// Decode a DER-encoded public key.
func UnmarshalPublic(in []byte) (pub *PublicKey, err error) {
var subj asnSubjectPublicKeyInfo
if _, err = asn1.Unmarshal(in, &subj); err != nil {
return
}
if !subj.Algorithm.Equal(idEcPublicKeySupplemented) {
err = ErrInvalidPublicKey
return
}
pub = new(PublicKey)
pub.Curve = namedCurveFromOID(subj.Supplements.ECDomain)
x, y := elliptic.Unmarshal(pub.Curve, subj.PublicKey.Bytes)
if x == nil {
err = ErrInvalidPublicKey
return
}
pub.X = x
pub.Y = y
pub.Params = new(ECIESParams)
asnECIEStoParams(subj.Supplements.ECCAlgorithms.ECIES, pub.Params)
asnECDHtoParams(subj.Supplements.ECCAlgorithms.ECDH, pub.Params)
if pub.Params == nil {
if pub.Params = ParamsFromCurve(pub.Curve); pub.Params == nil {
err = ErrInvalidPublicKey
}
}
return
}
示例5: CreateExchangedCipher
func CreateExchangedCipher(peerPub, priv []byte) (Cipher, Cipher, error) {
x, y := elliptic.Unmarshal(curve, peerPub)
sx, _ := curve.ScalarMult(x, y, priv)
secret := cryptohash(sx.Bytes())
aesKey1 := secret[0:aes.BlockSize]
aesKey2 := secret[aes.BlockSize : 2*aes.BlockSize]
vector1 := secret[2*aes.BlockSize : 3*aes.BlockSize]
vector2 := secret[3*aes.BlockSize : 4*aes.BlockSize]
block1, err := aes.NewCipher(aesKey1)
if err != nil {
return nil, nil, err
}
block2, err := aes.NewCipher(aesKey2)
if err != nil {
return nil, nil, err
}
stream1 := cipher.NewOFB(block1, vector1)
stream2 := cipher.NewOFB(block2, vector2)
return stream1, stream2, nil
}
示例6: VerifyHash
// verify a signature given the hash
func (v *ECDSAVerifier) VerifyHash(h, sig []byte) (err error) {
r, s := elliptic.Unmarshal(v.c, sig)
if r == nil || s == nil || !ecdsa.Verify(v.k, h, r, s) {
err = ErrInvalidSignature
}
return
}
示例7: ToECDSAPub
func ToECDSAPub(pub []byte) *ecdsa.PublicKey {
if len(pub) == 0 {
return nil
}
x, y := elliptic.Unmarshal(secp256k1.S256(), pub)
return &ecdsa.PublicKey{Curve: secp256k1.S256(), X: x, Y: y}
}
示例8: UnmarshalSignerProto
// UnmarshalSignerProto decodes a signing key from a CryptoKey protobuf
// message.
func UnmarshalSignerProto(ck *CryptoKey) (*Signer, error) {
if *ck.Version != CryptoVersion_CRYPTO_VERSION_1 {
return nil, newError("bad version")
}
if *ck.Purpose != CryptoKey_SIGNING {
return nil, newError("bad purpose")
}
if *ck.Algorithm != CryptoKey_ECDSA_SHA {
return nil, newError("bad algorithm")
}
var k ECDSA_SHA_SigningKeyV1
defer ZeroBytes(k.EcPrivate)
if err := proto.Unmarshal(ck.Key, &k); err != nil {
return nil, err
}
if *k.Curve != NamedEllipticCurve_PRIME256_V1 {
return nil, newError("bad Curve")
}
x, y := elliptic.Unmarshal(elliptic.P256(), k.EcPublic)
pk := &ecdsa.PrivateKey{
D: new(big.Int).SetBytes(k.EcPrivate),
PublicKey: ecdsa.PublicKey{
Curve: elliptic.P256(),
X: x,
Y: y,
},
}
return &Signer{pk}, nil
}
示例9: parseECDSA
// parseECDSA parses an ECDSA key according to RFC 5656, section 3.1.
func parseECDSA(in []byte) (out PublicKey, rest []byte, err error) {
var identifier []byte
var ok bool
if identifier, in, ok = parseString(in); !ok {
return nil, nil, errShortRead
}
key := new(ecdsa.PublicKey)
switch string(identifier) {
case "nistp256":
key.Curve = elliptic.P256()
case "nistp384":
key.Curve = elliptic.P384()
case "nistp521":
key.Curve = elliptic.P521()
default:
return nil, nil, errors.New("ssh: unsupported curve")
}
var keyBytes []byte
if keyBytes, in, ok = parseString(in); !ok {
return nil, nil, errShortRead
}
key.X, key.Y = elliptic.Unmarshal(key.Curve, keyBytes)
if key.X == nil || key.Y == nil {
return nil, nil, errors.New("ssh: invalid curve point")
}
return (*ecdsaPublicKey)(key), in, nil
}
示例10: UnmarshalBinary
func (p *curvePoint) UnmarshalBinary(buf []byte) error {
p.x, p.y = elliptic.Unmarshal(p.c, buf)
if p.x == nil || !p.Valid() {
return errors.New("invalid elliptic curve point")
}
return nil
}
示例11: UnmarshalSignerProto
// UnmarshalSignerProto decodes a signing key from a CryptoKey protobuf
// message.
func UnmarshalSignerProto(ck *CryptoKey) (*Signer, error) {
if *ck.Version != CryptoVersion_CRYPTO_VERSION_1 {
return nil, newError("bad version")
}
if *ck.Purpose != CryptoKey_SIGNING {
return nil, newError("bad purpose")
}
if *ck.Algorithm != CryptoKey_ECDSA_SHA {
return nil, newError("bad algorithm")
}
k := new(ECDSA_SHA_SigningKeyV1)
defer ZeroBytes(k.EcPrivate)
if err := proto.Unmarshal(ck.Key, k); err != nil {
return nil, err
}
if *k.Curve != NamedEllipticCurve_PRIME256_V1 {
return nil, newError("bad Curve")
}
s := new(Signer)
s.ec = new(ecdsa.PrivateKey)
s.ec.D = new(big.Int).SetBytes(k.EcPrivate)
s.ec.Curve = elliptic.P256()
s.ec.X, s.ec.Y = elliptic.Unmarshal(elliptic.P256(), k.EcPublic)
if s.ec.X == nil || s.ec.Y == nil {
return nil, fmt.Errorf("failed to unmarshal EC point: X=%v, Y=%v", s.ec.X, s.ec.Y)
}
return s, nil
}
示例12: Decrypt
// Decrypt authentications and recovers the original message from
// its input using the private key and the ephemeral key included in
// the message.
func Decrypt(priv *ecdsa.PrivateKey, in []byte) (out []byte, err error) {
ephLen := int(in[0])
ephPub := in[1 : 1+ephLen]
ct := in[1+ephLen:]
if len(ct) < (sha1.Size + aes.BlockSize) {
return nil, errors.New("Invalid ciphertext")
}
x, y := elliptic.Unmarshal(Curve(), ephPub)
if x == nil {
return nil, errors.New("Invalid public key")
}
x, _ = priv.Curve.ScalarMult(x, y, priv.D.Bytes())
if x == nil {
return nil, errors.New("Failed to generate encryption key")
}
shared := sha256.Sum256(x.Bytes())
tagStart := len(ct) - sha1.Size
h := hmac.New(sha1.New, shared[16:])
h.Write(ct[:tagStart])
mac := h.Sum(nil)
if !hmac.Equal(mac, ct[tagStart:]) {
return nil, errors.New("Invalid MAC")
}
paddedOut, err := symcrypt.DecryptCBC(ct[aes.BlockSize:tagStart], ct[:aes.BlockSize], shared[:16])
if err != nil {
return
}
out, err = padding.RemovePadding(paddedOut)
return
}
示例13: parseECDSA
// parseECDSA parses an ECDSA key according to RFC 5656, section 3.1.
func parseECDSA(in []byte) (out *ecdsa.PublicKey, rest []byte, ok bool) {
var identifier []byte
if identifier, in, ok = parseString(in); !ok {
return
}
key := new(ecdsa.PublicKey)
switch string(identifier) {
case "nistp256":
key.Curve = elliptic.P256()
case "nistp384":
key.Curve = elliptic.P384()
case "nistp521":
key.Curve = elliptic.P521()
default:
ok = false
return
}
var keyBytes []byte
if keyBytes, in, ok = parseString(in); !ok {
return
}
key.X, key.Y = elliptic.Unmarshal(key.Curve, keyBytes)
if key.X == nil || key.Y == nil {
ok = false
return
}
return key, in, ok
}
示例14: ToECDSAPub
func ToECDSAPub(pub []byte) *ecdsa.PublicKey {
if len(pub) == 0 {
return nil
}
x, y := elliptic.Unmarshal(S256(), pub)
return &ecdsa.PublicKey{S256(), x, y}
}
示例15: processServerKeyExchange
func (ka *ecdheKeyAgreement) processServerKeyExchange(config *Config, clientHello *clientHelloMsg, serverHello *serverHelloMsg, cert *x509.Certificate, skx *serverKeyExchangeMsg) error {
if len(skx.key) < 4 {
return errServerKeyExchange
}
if skx.key[0] != 3 { // named curve
return errors.New("tls: server selected unsupported curve")
}
curveid := CurveID(skx.key[1])<<8 | CurveID(skx.key[2])
var ok bool
if ka.curve, ok = curveForCurveID(curveid); !ok {
return errors.New("tls: server selected unsupported curve")
}
publicLen := int(skx.key[3])
if publicLen+4 > len(skx.key) {
return errServerKeyExchange
}
ka.x, ka.y = elliptic.Unmarshal(ka.curve, skx.key[4:4+publicLen])
if ka.x == nil {
return errServerKeyExchange
}
serverECDHParams := skx.key[:4+publicLen]
sig := skx.key[4+publicLen:]
return ka.auth.verifyParameters(config, clientHello, serverHello, cert, serverECDHParams, sig)
}