change error checking style to match existing code.

This commit is contained in:
Christopher Dudley
2017-12-15 15:33:53 -05:00
committed by Jeff
parent 7689615645
commit c26b4563dc
2 changed files with 10 additions and 21 deletions

12
dh.go
View File

@@ -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
View File

@@ -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, &params) if int(C.EVP_PKEY_paramgen(paramCtx, &params)) != 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")
} }