// Code generated by cmd/cgo; DO NOT EDIT.

//line /builddir/build/BUILD/go-go-1.12.8-2-openssl-fips/src/crypto/internal/boring/boring.go:1:1
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build linux
// +build !android
// +build !no_openssl
// +build !cmd_go_bootstrap
// +build !msan

package boring

// #include "goboringcrypto.h"
// #cgo LDFLAGS: -ldl
import _ "unsafe"
import (
	"crypto/internal/boring/fipstls"
	"crypto/internal/boring/sig"
	"math/big"
	"os"
	"runtime"
)

const (
	fipsOn  =  /*line :25:12*/_Ctype_int /*line :25:17*/(1)
	fipsOff =  /*line :26:12*/_Ctype_int /*line :26:17*/(0)
)

// Enabled controls whether FIPS crypto is enabled.
var enabled = false

func init() {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	// Check if we can `dlopen` OpenSSL
	if ( /*line :37:5*/_Cfunc__goboringcrypto_DLOPEN_OPENSSL /*line :37:36*/)() == ( /*line :37:43*/_Cmacro_NULL() /*line :37:48*/) {
		return
	}

	// Initialize the OpenSSL library.
	( /*line :42:2*/_Cfunc__goboringcrypto_OPENSSL_setup /*line :42:32*/)()

	// Check to see if the system is running in FIPS mode, if so
	// enable "boring" mode to call into OpenSSL for FIPS compliance.
	if fipsModeEnabled() {
		enableBoringFIPSMode()
	}
	sig.BoringCrypto()
}

func enableBoringFIPSMode() {
	enabled = true

	if ( /*line :55:5*/_Cfunc__goboringcrypto_OPENSSL_thread_setup /*line :55:42*/)() != 1 {
		panic("boringcrypto: OpenSSL thread setup failed")
	}
	fipstls.Force()
}

func fipsModeEnabled() bool {
	return os.Getenv("GOLANG_FIPS") == "1" ||
		( /*line :63:3*/_Cfunc__goboringcrypto_FIPS_mode /*line :63:29*/)() == fipsOn
}

var randstub bool

func RandStubbed() bool {
	return randstub
}

func StubOpenSSLRand() {
	if !randstub {
		randstub = true
		( /*line :75:3*/_Cfunc__goboringcrypto_stub_openssl_rand /*line :75:37*/)()
	}
}

func RestoreOpenSSLRand() {
	if randstub {
		randstub = false
		( /*line :82:3*/_Cfunc__goboringcrypto_restore_openssl_rand /*line :82:40*/)()
	}
}

// Unreachable marks code that should be unreachable
// when BoringCrypto is in use. It panics only when
// the system is in FIPS mode.
func Unreachable() {
	if Enabled() {
		panic("boringcrypto: invalid code execution")
	}
}

// provided by runtime to avoid os import
func runtime_arg0() string

func hasSuffix(s, t string) bool {
	return len(s) > len(t) && s[len(s)-len(t):] == t
}

// UnreachableExceptTests marks code that should be unreachable
// when BoringCrypto is in use. It panics.
func UnreachableExceptTests() {
	name := runtime_arg0()
	// If BoringCrypto ran on Windows we'd need to allow _test.exe and .test.exe as well.
	if Enabled() && !hasSuffix(name, "_test") && !hasSuffix(name, ".test") {
		println("boringcrypto: unexpected code execution in", name)
		panic("boringcrypto: invalid code execution")
	}
}

type fail string

func (e fail) Error() string { return "boringcrypto: " + string(e) + " failed" }

func bigToBN(x *big.Int) * /*line :117:27*/_Ctype_struct_bignum_st /*line :117:38*/ {
	raw := x.Bytes()
	return ( /*line :119:9*/_Cfunc__goboringcrypto_BN_bin2bn /*line :119:35*/)(base(raw),  /*line :119:48*/_Ctype_size_t /*line :119:56*/(len(raw)), nil)
}

func bnToBig(bn * /*line :122:18*/_Ctype_struct_bignum_st /*line :122:29*/) *big.Int {
	raw := make([]byte, func() _Ctype_uint{ _cgo0 := /*line :123:53*/bn; _cgoCheckPointer(_cgo0); return _Cfunc__goboringcrypto_BN_num_bytes(_cgo0); }())
	n := func() _Ctype_size_t{ _cgo0 := /*line :124:35*/bn; var _cgo1 *_Ctype_uint8_t = /*line :124:39*/base(raw); _cgoCheckPointer(_cgo0); return _Cfunc__goboringcrypto_BN_bn2bin(_cgo0, _cgo1); }()
	return new(big.Int).SetBytes(raw[:n])
}

func bigToBn(bnp ** /*line :128:20*/_Ctype_struct_bignum_st /*line :128:31*/, b *big.Int) bool {
	if *bnp != nil {
		func() { _cgo0 := /*line :130:29*/*bnp; _cgoCheckPointer(_cgo0); _Cfunc__goboringcrypto_BN_free(_cgo0); }()
		*bnp = nil
	}
	if b == nil {
		return true
	}
	raw := b.Bytes()
	bn := ( /*line :137:8*/_Cfunc__goboringcrypto_BN_bin2bn /*line :137:34*/)(base(raw),  /*line :137:47*/_Ctype_size_t /*line :137:55*/(len(raw)), nil)
	if bn == nil {
		return false
	}
	*bnp = bn
	return true
}
