예제 #1
0
파일: sha1.go 프로젝트: 9uuso/openssl
func NewSHA1HashWithEngine(e *Engine) (*SHA1Hash, error) {
	hash := &SHA1Hash{engine: e}
	C.EVP_MD_CTX_init(&hash.ctx)
	runtime.SetFinalizer(hash, func(hash *SHA1Hash) { hash.Close() })
	if err := hash.Reset(); err != nil {
		return nil, err
	}
	return hash, nil
}
예제 #2
0
파일: pem.go 프로젝트: 9uuso/openssl
func (key *pKey) VerifyPKCS1v15(method Method, data, sig []byte) error {
	var ctx C.EVP_MD_CTX
	C.EVP_MD_CTX_init(&ctx)
	defer C.EVP_MD_CTX_cleanup(&ctx)

	if 1 != C.EVP_VerifyInit_not_a_macro(&ctx, method) {
		return errors.New("verifypkcs1v15: failed to init verify")
	}
	if len(data) > 0 {
		if 1 != C.EVP_VerifyUpdate_not_a_macro(
			&ctx, unsafe.Pointer(&data[0]), C.uint(len(data))) {
			return errors.New("verifypkcs1v15: failed to update verify")
		}
	}
	if 1 != C.EVP_VerifyFinal(&ctx,
		((*C.uchar)(unsafe.Pointer(&sig[0]))), C.uint(len(sig)), key.key) {
		return errors.New("verifypkcs1v15: failed to finalize verify")
	}
	return nil
}
예제 #3
0
파일: pem.go 프로젝트: 9uuso/openssl
func (key *pKey) SignPKCS1v15(method Method, data []byte) ([]byte, error) {
	var ctx C.EVP_MD_CTX
	C.EVP_MD_CTX_init(&ctx)
	defer C.EVP_MD_CTX_cleanup(&ctx)

	if 1 != C.EVP_SignInit_not_a_macro(&ctx, method) {
		return nil, errors.New("signpkcs1v15: failed to init signature")
	}
	if len(data) > 0 {
		if 1 != C.EVP_SignUpdate_not_a_macro(
			&ctx, unsafe.Pointer(&data[0]), C.uint(len(data))) {
			return nil, errors.New("signpkcs1v15: failed to update signature")
		}
	}
	sig := make([]byte, C.EVP_PKEY_size(key.key))
	var sigblen C.uint
	if 1 != C.EVP_SignFinal(&ctx,
		((*C.uchar)(unsafe.Pointer(&sig[0]))), &sigblen, key.key) {
		return nil, errors.New("signpkcs1v15: failed to finalize signature")
	}
	return sig[:sigblen], nil
}
예제 #4
0
파일: ssl.go 프로젝트: soundcloud/sc-gaws
func (key *pKey) SignPKCS1v15(data []byte) ([]byte, error) {
	//
	// Initialize the context using sha1 as method, as we
	// need to generate RSA-SHA1 signature
	//
	var ctx C.EVP_MD_CTX
	C.EVP_MD_CTX_init(&ctx)
	defer C.EVP_MD_CTX_cleanup(&ctx)

	if 1 != C.EVP_SignInit_not_a_macro(&ctx, C.EVP_sha1()) {
		return nil, errors.New("rsasha1signature: failed to init signature")
	}
	if len(data) > 0 {
		if 1 != C.EVP_SignUpdate_not_a_macro(
			&ctx, unsafe.Pointer(&data[0]), C.uint(len(data))) {
			return nil, errors.New("rsasha1signature: failed to update signature")
		}
	}

	//
	// Sign
	//
	sig := make([]byte, C.EVP_PKEY_size(key.key))
	var sigblen C.uint

	// prevent data race when multiple threads are spawned
	// by the go runtime, mostly because you are performing the signing
	// inside a concurrent http request.
	sslMutex.Lock()
	defer sslMutex.Unlock()

	if 1 != C.EVP_SignFinal(&ctx,
		((*C.uchar)(unsafe.Pointer(&sig[0]))), &sigblen, key.key) {
		return nil, errors.New("rsasha1signature: failed to finalize signature")
	}
	return sig[:sigblen], nil
}