// Decipher attempts to decipher the encoded mnemonic by first mapping to the
// original chipertext, then applying our deciphering scheme. ErrInvalidPass
// will be returned if the passphrase is incorrect.
483221
func (m *Mnemonic) Decipher(pass []byte) ([DecipheredCipherSeedSize]byte, error) {

	// Before we attempt to map the mnemonic back to the original
	// ciphertext, we'll ensure that all the word are actually a part of
	// the current default word list.
	for _, word := range m {
		if !strings.Contains(englishWordList, word) {
			emptySeed := [DecipheredCipherSeedSize]byte{}
			return emptySeed, ErrUnknownMnenomicWord{word}
		}
	}

	// If the passphrase wasn't provided, then we'll use the string
	// "aezeed" in place.
	passphrase := pass
	if len(passphrase) == 0 {
		passphrase = defaultPassphrase
	}

	// Next, we'll map the mnemonic phrase back into the original cipher
	// text.
	cipherText := mnemonicToCipherText(m)

	// Finally, we'll attempt to decipher the enciphered seed. The result
	// will be the raw seed minus the ciphertext expansion, external
	// version, and salt.
	return decipherCipherSeed(cipherText, passphrase)
}
544497
func (s *GCPCKMSSeal) Decrypt(ctx context.Context, in *physical.EncryptedBlobInfo) (pt []byte, err error) {
	defer func(now time.Time) {
		metrics.MeasureSince([]string{"seal", "decrypt", "time"}, now)
		metrics.MeasureSince([]string{"seal", "gcpckms", "decrypt", "time"}, now)

		if err != nil {
			metrics.IncrCounter([]string{"seal", "decrypt", "error"}, 1)
			metrics.IncrCounter([]string{"seal", "gcpckms", "decrypt", "error"}, 1)
		}
	}(time.Now())

	metrics.IncrCounter([]string{"seal", "decrypt"}, 1)
	metrics.IncrCounter([]string{"seal", "gcpckms", "decrypt"}, 1)

	if in.Ciphertext == nil {
		return nil, fmt.Errorf("given ciphertext for decryption is nil")
	}

	// Default to mechanism used before key info was stored
	if in.KeyInfo == nil {
		in.KeyInfo = &physical.SealKeyInfo{
			Mechanism: GCPKMSEncrypt,
		}
	}

	var plaintext []byte
	switch in.KeyInfo.Mechanism {
	case GCPKMSEncrypt:
		resp, err := s.client.Decrypt(ctx, &kmspb.DecryptRequest{
			Name:       s.parentName,
			Ciphertext: in.Ciphertext,
		})
		if err != nil {
			return nil, errwrap.Wrapf("failed to decrypt data: {{err}}", err)
		}

		plaintext = resp.Plaintext

	case GCPKMSEnvelopeAESGCMEncrypt:
		resp, err := s.client.Decrypt(ctx, &kmspb.DecryptRequest{
			Name:       s.parentName,
			Ciphertext: in.KeyInfo.WrappedKey,
		})
		if err != nil {
			return nil, errwrap.Wrapf("failed to decrypt envelope: {{err}}", err)
		}

		envInfo := &seal.EnvelopeInfo{
			Key:        resp.Plaintext,
			IV:         in.IV,
			Ciphertext: in.Ciphertext,
		}
		plaintext, err = seal.NewEnvelope().Decrypt(envInfo)
		if err != nil {
			return nil, errwrap.Wrapf("error decrypting data with envelope: {{err}}", err)
		}

	default:
		return nil, fmt.Errorf("invalid mechanism: %d", in.KeyInfo.Mechanism)
	}

	return plaintext, nil
}
712595
func (m *Mocker) Decrypt(ctx context.Context, ciphertext []byte) ([]byte, error) {
	return ciphertext, nil
}
264887
func (ea *EncryptedAssertion) Decrypt(cert *tls.Certificate) (*Assertion, error) {
	plaintext, err := ea.DecryptBytes(cert)
	if err != nil {
		return nil, fmt.Errorf("Error decrypting assertion: %v", err)
	}

	assertion := &Assertion{}

	err = xml.Unmarshal(plaintext, assertion)
	if err != nil {
		return nil, fmt.Errorf("Error unmarshaling assertion: %v", err)
	}

	return assertion, nil
}
823558
func passphraseFetcher(name string) ([]byte, error) {
	var passphrase = settings.HelmKeyPassphrase()
	if passphrase != "" {
		return []byte(passphrase), nil
	}

	fmt.Printf("Password for key %q >  ", name)
	pw, err := terminal.ReadPassword(int(syscall.Stdin))
	fmt.Println()
	return pw, err
}
314978
func Decrypt(key []byte, cryptoText string) string {
	ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)

	block, err := aes.NewCipher(key)
	if err != nil {
		return err.Error()
	}

	// The IV needs to be unique, but not secure. Therefore it's common to
	// include it at the beginning of the ciphertext.
	if len(ciphertext) < aes.BlockSize {
		return "ciphertext too short"
	}
	iv := ciphertext[:aes.BlockSize]
	ciphertext = ciphertext[aes.BlockSize:]

	stream := cipher.NewCFBDecrypter(block, iv)

	// XORKeyStream can work in-place if the two arguments are the same.
	stream.XORKeyStream(ciphertext, ciphertext)

	return fmt.Sprintf("%s", ciphertext)
}
163919
func (s *EDStore) Decrypt(dst []byte, cipherText []byte) ([]byte, error) {
	var gcm cipher.AEAD
	if len(cipherText) > 0 {
		switch cipherText[0] {
		case CryptoCodeAES:
			gcm = s.aesgcm
		case CryptoCodeChaCha:
			gcm = s.chachagcm
		default:
			// Anything else, assume no algo or something we don't know how to decrypt.
			return cipherText, nil
		}
	}
	if len(cipherText) <= 1+s.nonceSize {
		return nil, fmt.Errorf("trying to decrypt data that is not (len=%v)", len(cipherText))
	}
	dd, err := gcm.Open(dst, cipherText[1:1+s.nonceSize], cipherText[1+s.nonceSize:], nil)
	if err != nil {
		return nil, err
	}
	return dd, nil
}
22551
func (c *Crypter) Decrypt(dst io.Writer, src io.Reader) (err error) {
	keySize := c.Key.Size()
	headerSize := 1 + keySize + blockSize + c.HashSize

	header := make([]byte, headerSize)
	if _, err = src.Read(header); err != nil {
		return err
	}

	salt, iv, err := c.decHeader(header)
	if err != nil {
		return err
	}
	aesKey, hmacKey := c.Key.Derive(salt)

	mac := hmac.New(c.HashFunc, hmacKey)
	mac.Write(header[:1+keySize+blockSize])

	if !bytes.Equal(header[1+keySize+blockSize:], mac.Sum(nil)) {
		return errors.New("cannot authenticate header")
	}
	mac.Write(header[1+keySize+blockSize:])

	block, err := aes.NewCipher(aesKey)
	if err != nil {
		return err
	}

	stream := cipher.NewCTR(block, iv)
	buf := make([]byte, c.bufSize()+c.HashSize)
	n := 0
	for {
		n, err = src.Read(buf)
		if err != nil {
			if err == io.EOF {
				break
			}
			return err
		}

		stream.XORKeyStream(buf[:n-c.HashSize], buf[:n-c.HashSize])
		mac.Write(buf[:n-c.HashSize])
		if !bytes.Equal(buf[n-c.HashSize:n], mac.Sum(nil)) {
			return errors.New("cannot authenticate packet")
		}
		if _, err = dst.Write(buf[:n-c.HashSize]); err != nil {
			return err
		}
	}
	return nil
}
355590
func (d *Decoder) MustDecode(v interface{}) {
	if d.err != nil {
		panic(d.err)
	}
	d.mustDecode(v)
}
19968
func Decode(r io.Reader, password []byte) (KeyStore, error) {
	ksd := keyStoreDecoder{
		r:  r,
		md: sha1.New(),
	}
	passwordBytes := passwordBytes(password)
	defer zeroing(passwordBytes)
	_, err := ksd.md.Write(passwordBytes)
	if err != nil {
		return nil, err
	}
	_, err = ksd.md.Write(whitenerMessage)
	if err != nil {
		return nil, err
	}

	readMagic, err := ksd.readUint32()
	if err != nil {
		return nil, err
	}
	if readMagic != magic {
		return nil, ErrIncorrectMagic
	}
	version, err := ksd.readUint32()
	if err != nil {
		return nil, err
	}
	count, err := ksd.readUint32()
	if err != nil {
		return nil, err
	}
	keyStore := KeyStore{}
	for entitiesCount := count; entitiesCount > 0; entitiesCount-- {
		alias, entry, err := ksd.readEntry(version, password)
		if err != nil {
			return nil, err
		}
		keyStore[alias] = entry
	}

	computedDigest := ksd.md.Sum(nil)
	actualDigest, err := ksd.readBytes(uint32(ksd.md.Size()))
	for i := 0; i < len(actualDigest); i++ {
		if actualDigest[i] != computedDigest[i] {
			return nil, ErrInvalidDigest
		}
	}

	return keyStore, nil
}
51846
func cipherTextToMnemonic(cipherText [EncipheredCipherSeedSize]byte) (Mnemonic, error) {
	var words [NummnemonicWords]string

	// First, we'll convert the ciphertext itself into a bitstream for easy
	// manipulation.
	cipherBits := bstream.NewBStreamReader(cipherText[:])

	// With our bitstream obtained, we'll read 11 bits at a time, then use
	// that to index into our word list to obtain the next word.
	for i := 0; i < NummnemonicWords; i++ {
		index, err := cipherBits.ReadBits(bitsPerWord)
		if err != nil {
			return words, nil
		}

		words[i] = defaultWordList[index]
	}

	return words, nil
}
696514
func (l *Line) Decode(b []byte) error {
	delimiter := bytes.IndexRune(b, lineDelimiter)
	if delimiter == -1 {
		reason := `delimiter "=" not found`
		err := newDecodeError("line", reason)
		return errors.Wrap(err, "failed to decode")
	}
	if len(b) <= (delimiter + 1) {
		reason := fmt.Sprintf(
			"len(b) %d < (%d + 1), no value found after delimiter",
			len(b), delimiter,
		)
		err := newDecodeError("line", reason)
		return errors.Wrap(err, "failed to decode")
	}
	r, _ := utf8.DecodeRune(b[:delimiter])
	l.Type = Type(r)
	l.Value = append(l.Value, b[delimiter+1:]...)
	return nil
}
966841
func readPasswordLine(reader io.Reader) ([]byte, error) {
	var buf [1]byte
	var ret []byte

	for {
		n, err := reader.Read(buf[:])
		if n > 0 {
			switch buf[0] {
			case '\n':
				return ret, nil
			case '\r':
				// remove \r from passwords on Windows
			default:
				ret = append(ret, buf[0])
			}
			continue
		}
		if err != nil {
			if err == io.EOF && len(ret) > 0 {
				return ret, nil
			}
			return ret, err
		}
	}
}
307157
func (sk *SecretKey) Decrypt(in []byte) ([]byte, error) {
	return sk.Key.Decrypt(in)
}
1183393
func Decrypt(buf []byte, alg jwa.KeyEncryptionAlgorithm, key interface{}) ([]byte, error) {
	msg, err := Parse(buf)
	if err != nil {
		return nil, errors.Wrap(err, "failed to parse buffer for Decrypt")
	}

	return msg.Decrypt(alg, key)
}
1316255
func (c *aesCBC) Decrypt(src io.Reader) io.Reader {
	return &cbcDecryptReader{
		decrypter: c.decrypter,
		src:       src,
		padder:    c.padder,
	}
}
321990
func VerifyEncoded(s string, password []byte) (bool, error) {
	mode, err := getMode(s)

	if err != nil {
		return false, err
	}
	
	cs := C.CString(s)
	defer C.free(unsafe.Pointer(cs))

	result := C.argon2_verify(
		cs,
		unsafe.Pointer(&password[0]),
		C.size_t(len(password)),
		C.argon2_type(mode))

	if result == C.ARGON2_OK {
		return true, nil
	} else if result == C.ARGON2_VERIFY_MISMATCH {
		return false, nil
	}

	// argon2_verify always seems to return an error in this case...
	return false, Error(result)
}