// Reads in encoded ciphertext from disk and breaks into component parts.
341393
func readCiphertext(filename string) ([]byte, int, *secret.SealedBytes, error) {
	plaintextBytes, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, 0, nil, err
	}

	var ec EncodedCiphertext
	err = json.Unmarshal(plaintextBytes, &ec)
	if err != nil {
		return nil, 0, nil, err
	}

	sealedBytes := &secret.SealedBytes{
		Ciphertext: ec.Ciphertext,
		Nonce:      ec.CiphertextNonce,
	}

	return ec.KeySalt, ec.KeyIter, sealedBytes, nil
}
41842
func (d *Diskv) ReadString(key string) string {
	value, _ := d.Read(key)
	return string(value)
}
295519
func (s *AESEncryptedStorage) readEncryptedStorage() (map[string]storedCredential, error) {
	creds := make(map[string]storedCredential)
	raw, err := ioutil.ReadFile(s.filename)

	if err != nil {
		if os.IsNotExist(err) {
			// Doesn't exist yet
			return creds, nil
		}
		log.Warn("Failed to read encrypted storage", "err", err, "file", s.filename)
	}
	if err = json.Unmarshal(raw, &creds); err != nil {
		log.Warn("Failed to unmarshal encrypted storage", "err", err, "file", s.filename)
		return nil, err
	}
	return creds, nil
}
498912
func getEncryptedSinglePartOffsetLength(offset, length int64, objInfo ObjectInfo) (seqNumber uint32, encOffset int64, encLength int64) {
	onePkgSize := int64(SSEDAREPackageBlockSize + SSEDAREPackageMetaSize)

	seqNumber = uint32(offset / SSEDAREPackageBlockSize)
	encOffset = int64(seqNumber) * onePkgSize
	// The math to compute the encrypted length is always
	// originalLength i.e (offset+length-1) to be divided under
	// 64KiB blocks which is the payload size for each encrypted
	// block. This is then multiplied by final package size which
	// is basically 64KiB + 32. Finally negate the encrypted offset
	// to get the final encrypted length on disk.
	encLength = ((offset+length)/SSEDAREPackageBlockSize)*onePkgSize - encOffset

	// Check for the remainder, to figure if we need one extract package to read from.
	if (offset+length)%SSEDAREPackageBlockSize > 0 {
		encLength += onePkgSize
	}

	if encLength+encOffset > objInfo.EncryptedSize() {
		encLength = objInfo.EncryptedSize() - encOffset
	}
	return seqNumber, encOffset, encLength
}
1007230
func readBytes(r io.Reader) ([]byte, error) {
	length, err := readInt32(r)
	if err != nil {
		return nil, err
	}
	buf := make([]byte, length)
	_, err = io.ReadFull(r, buf)
	if err != nil {
		return nil, err
	}
	return buf, nil
}
117742
func decryptPayloadFromReader(payload io.Reader,
	keyRing keychain.KeyRing) ([]byte, error) {

	// First, we'll re-generate the encryption key that we use for all the
	// SCBs.
	encryptionKey, err := genEncryptionKey(keyRing)
	if err != nil {
		return nil, err
	}

	// Next, we'll read out the entire blob as we need to isolate the nonce
	// from the rest of the ciphertext.
	packedBackup, err := ioutil.ReadAll(payload)
	if err != nil {
		return nil, err
	}
	if len(packedBackup) < chacha20poly1305.NonceSizeX {
		return nil, fmt.Errorf("payload size too small, must be at "+
			"least %v bytes", chacha20poly1305.NonceSizeX)
	}

	nonce := packedBackup[:chacha20poly1305.NonceSizeX]
	ciphertext := packedBackup[chacha20poly1305.NonceSizeX:]

	// Now that we have the cipher text and the nonce separated, we can go
	// ahead and decrypt the final blob so we can properly serialized the
	// SCB.
	cipher, err := chacha20poly1305.NewX(encryptionKey)
	if err != nil {
		return nil, err
	}
	plaintext, err := cipher.Open(nil, nonce, ciphertext, nonce)
	if err != nil {
		return nil, err
	}

	return plaintext, nil
}
863146
func (c *Cipher) Decrypt(dst, src []byte) {

    uintLen := c.stateSize / 64

    // This saves a make because tmpin and tmpout are of variable length
    tmpin, tmpout := c.getTempData()

    for i := 0; i < uintLen; i++ {
        tmpin[i] = binary.LittleEndian.Uint64(src[i*8 : i*8+8])
    }
    c.decrypt(tmpin, tmpout)

    for i := 0; i < uintLen; i++ {
        binary.LittleEndian.PutUint64(dst[i*8:i*8+8], tmpout[i])
    }
}
770973
func (fs *FSObjects) decodePartFile(name string) (partNumber int, etag string, actualSize int64, err error) {
	result := strings.Split(name, ".")
	if len(result) != 3 {
		return 0, "", 0, errUnexpected
	}
	partNumber, err = strconv.Atoi(result[0])
	if err != nil {
		return 0, "", 0, errUnexpected
	}
	actualSize, err = strconv.ParseInt(result[2], 10, 64)
	if err != nil {
		return 0, "", 0, errUnexpected
	}
	return partNumber, result[1], actualSize, nil
}
575009
func (c *Cipher) Decrypt64(dst, src []uint64) {
    c.decrypt(src, dst)
}
975893
func (s *EncryptedStorageProvider) Reader(path ...string) (decryptReader io.ReadCloser, err error) {
	var unEncryptedReader io.ReadCloser
	stream := s.getStream()

	if unEncryptedReader, err = s.wrappedStorageProvider.Reader(path...); err == nil {

		decryptReader = &StreamReadCloser{
			StreamReader: cipher.StreamReader{S: stream, R: unEncryptedReader},
			Closer:       unEncryptedReader,
		}
	}
	return
}
845525
func (c *Cipher) Decrypt(dst, src []byte) {
	l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
	r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
	l, r = decryptBlock(l, r, c)
	dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
	dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r)
}
119155
func (p *PrivateKey) Decrypt(passphrase string) error {
	if !p.Encrypted {
		return nil
	}
	secretKey := p.deriveKey(passphrase)

	decrypted, ok := secretbox.Open(nil, p.EncryptedData, &p.Nonce, &secretKey)
	if !ok {
		return fmt.Errorf("decryption error")
	}
	copy(p.privateKey[:], decrypted)

	p.Encrypted = false
	return nil
}
401341
func Aes128Decrypt(key, cipherString, encodedSrc string, decode textToBinary) (string, error) {
	encrypted, err := decode(encodedSrc)
	if err != nil {
		log.Printf("Fail decode: err[%s]", err.Error())
		return "", err
	}

	block, _ := aes.NewCipher([]byte(key))
	ciphertext := []byte(cipherString)

	iv := ciphertext[:aes.BlockSize]
	decrypter := cipher.NewCFBDecrypter(block, iv)

	decrypted := make([]byte, len(encrypted))
	decrypter.XORKeyStream(decrypted, encrypted)

	return string(decrypted[:]), nil
}
1076143
func (bd *BinaryDecoder) ReadString() (string, error) {
	if err := checkEOF(bd.buf, bd.pos, 1); err != nil {
		return "", err
	}
	length, err := bd.ReadLong()
	if err != nil || length < 0 {
		return "", InvalidStringLength
	}
	if err := checkEOF(bd.buf, bd.pos, int(length)); err != nil {
		return "", err
	}
	value := string(bd.buf[bd.pos : bd.pos+length])
	bd.pos += length
	return value, nil
}
329687
func Aes128CFBEncrypt(key [16]byte, plainReader io.Reader) (cipherReader io.Reader, err error) {
	return aesCFBEnc(key[:], plainReader)
}
978131
func Aes128OFBDecrypt(key [16]byte, cipherReader io.Reader) (plainReader io.Reader, err error) {
	return aesOFBDec(key[:], cipherReader)
}
766652
func (o OpenSSL) EncryptBytes(passphrase string, plainData []byte, kdf DigestFunc) ([]byte, error) {
	salt, err := o.GenerateSalt()
	if err != nil {
		return nil, err
	}

	return o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, plainData, kdf)
}