mirror of
https://github.com/libp2p/go-openssl.git
synced 2026-08-18 14:43:28 +08:00
change error checking style to match existing code.
This commit is contained in:
12
dh.go
12
dh.go
@@ -34,21 +34,18 @@ func DeriveSharedSecret(private PrivateKey, public PublicKey) ([]byte, error) {
|
|||||||
defer C.EVP_PKEY_CTX_free(dhCtx)
|
defer C.EVP_PKEY_CTX_free(dhCtx)
|
||||||
|
|
||||||
// Initialize the context
|
// Initialize the context
|
||||||
rc := C.EVP_PKEY_derive_init(dhCtx)
|
if int(C.EVP_PKEY_derive_init(dhCtx)) != 1 {
|
||||||
if rc != 1 {
|
|
||||||
return nil, errors.New("failed initializing shared secret derivation context")
|
return nil, errors.New("failed initializing shared secret derivation context")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provide the peer's public key
|
// Provide the peer's public key
|
||||||
rc = C.EVP_PKEY_derive_set_peer(dhCtx, public.evpPKey())
|
if int(C.EVP_PKEY_derive_set_peer(dhCtx, public.evpPKey())) != 1 {
|
||||||
if rc != 1 {
|
|
||||||
return nil, errors.New("failed adding peer public key to context")
|
return nil, errors.New("failed adding peer public key to context")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine how large of a buffer we need for the shared secret
|
// Determine how large of a buffer we need for the shared secret
|
||||||
var buffLen C.size_t
|
var buffLen C.size_t
|
||||||
rc = C.EVP_PKEY_derive(dhCtx, nil, &buffLen)
|
if int(C.EVP_PKEY_derive(dhCtx, nil, &buffLen)) != 1 {
|
||||||
if rc != 1 {
|
|
||||||
return nil, errors.New("failed determining shared secret length")
|
return nil, errors.New("failed determining shared secret length")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,8 +57,7 @@ func DeriveSharedSecret(private PrivateKey, public PublicKey) ([]byte, error) {
|
|||||||
defer C.X_OPENSSL_free(buffer)
|
defer C.X_OPENSSL_free(buffer)
|
||||||
|
|
||||||
// Derive the shared secret
|
// Derive the shared secret
|
||||||
rc = C.EVP_PKEY_derive(dhCtx, (*C.uchar)(buffer), &buffLen)
|
if int(C.EVP_PKEY_derive(dhCtx, (*C.uchar)(buffer), &buffLen)) != 1 {
|
||||||
if rc != 1 {
|
|
||||||
return nil, errors.New("failed deriving the shared secret")
|
return nil, errors.New("failed deriving the shared secret")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
key.go
19
key.go
@@ -193,8 +193,7 @@ func (key *pKey) MarshalPKIXPublicKeyPEM() (pem_block []byte,
|
|||||||
}
|
}
|
||||||
defer C.BIO_free(bio)
|
defer C.BIO_free(bio)
|
||||||
|
|
||||||
rc := C.PEM_write_bio_PUBKEY(bio, key.key)
|
if int(C.PEM_write_bio_PUBKEY(bio, key.key)) != 1 {
|
||||||
if rc != 1 {
|
|
||||||
return nil, errors.New("failed dumping public key pem")
|
return nil, errors.New("failed dumping public key pem")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,7 +373,6 @@ func GenerateRSAKeyWithExponent(bits int, exponent int) (PrivateKey, error) {
|
|||||||
// GenerateECKey generates a new elliptic curve private key on the speicified
|
// GenerateECKey generates a new elliptic curve private key on the speicified
|
||||||
// curve.
|
// curve.
|
||||||
func GenerateECKey(curve EllipticCurve) (PrivateKey, error) {
|
func GenerateECKey(curve EllipticCurve) (PrivateKey, error) {
|
||||||
var rc C.int
|
|
||||||
|
|
||||||
// Create context for parameter generation
|
// Create context for parameter generation
|
||||||
paramCtx := C.EVP_PKEY_CTX_new_id(C.EVP_PKEY_EC, nil)
|
paramCtx := C.EVP_PKEY_CTX_new_id(C.EVP_PKEY_EC, nil)
|
||||||
@@ -384,21 +382,18 @@ func GenerateECKey(curve EllipticCurve) (PrivateKey, error) {
|
|||||||
defer C.EVP_PKEY_CTX_free(paramCtx)
|
defer C.EVP_PKEY_CTX_free(paramCtx)
|
||||||
|
|
||||||
// Intialize the parameter generation
|
// Intialize the parameter generation
|
||||||
rc = C.EVP_PKEY_paramgen_init(paramCtx)
|
if int(C.EVP_PKEY_paramgen_init(paramCtx)) != 1 {
|
||||||
if rc != 1 {
|
|
||||||
return nil, errors.New("failed initializing EC parameter generation context")
|
return nil, errors.New("failed initializing EC parameter generation context")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set curve in EC parameter generation context
|
// Set curve in EC parameter generation context
|
||||||
rc = C.X_EVP_PKEY_CTX_set_ec_paramgen_curve_nid(paramCtx, C.int(curve))
|
if int(C.X_EVP_PKEY_CTX_set_ec_paramgen_curve_nid(paramCtx, C.int(curve))) != 1 {
|
||||||
if rc != 1 {
|
|
||||||
return nil, errors.New("failed setting curve in EC parameter generation context")
|
return nil, errors.New("failed setting curve in EC parameter generation context")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create parameter object
|
// Create parameter object
|
||||||
var params *C.EVP_PKEY
|
var params *C.EVP_PKEY
|
||||||
rc = C.EVP_PKEY_paramgen(paramCtx, ¶ms)
|
if int(C.EVP_PKEY_paramgen(paramCtx, ¶ms)) != 1 {
|
||||||
if rc != 1 {
|
|
||||||
return nil, errors.New("failed creating EC key generation parameters")
|
return nil, errors.New("failed creating EC key generation parameters")
|
||||||
}
|
}
|
||||||
defer C.EVP_PKEY_free(params)
|
defer C.EVP_PKEY_free(params)
|
||||||
@@ -412,12 +407,10 @@ func GenerateECKey(curve EllipticCurve) (PrivateKey, error) {
|
|||||||
|
|
||||||
// Generate the key
|
// Generate the key
|
||||||
var privKey *C.EVP_PKEY
|
var privKey *C.EVP_PKEY
|
||||||
rc = C.EVP_PKEY_keygen_init(keyCtx)
|
if int(C.EVP_PKEY_keygen_init(keyCtx)) != 1 {
|
||||||
if rc != 1 {
|
|
||||||
return nil, errors.New("failed initializing EC key generation context")
|
return nil, errors.New("failed initializing EC key generation context")
|
||||||
}
|
}
|
||||||
rc = C.EVP_PKEY_keygen(keyCtx, &privKey)
|
if int(C.EVP_PKEY_keygen(keyCtx, &privKey)) != 1 {
|
||||||
if rc != 1 {
|
|
||||||
return nil, errors.New("failed generating EC private key")
|
return nil, errors.New("failed generating EC private key")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user