Source File
symmetrically_encrypted.go
Belonging Package
golang.org/x/crypto/openpgp/packet
package packet
import (
)
type SymmetricallyEncrypted struct {
MDC bool // true iff this is a type 18 packet and thus has an embedded MAC.
contents io.Reader
prefix []byte
}
const symmetricallyEncryptedVersion = 1
func ( *SymmetricallyEncrypted) ( io.Reader) error {
var [1]byte
, := readFull(, [:])
if != nil {
return
}
if [0] != symmetricallyEncryptedVersion {
return errors.UnsupportedError("unknown SymmetricallyEncrypted version")
}
}
.contents =
return nil
}
func ( *SymmetricallyEncrypted) ( CipherFunction, []byte) (io.ReadCloser, error) {
:= .KeySize()
if == 0 {
return nil, errors.UnsupportedError("unknown cipher: " + strconv.Itoa(int()))
}
if len() != {
return nil, errors.InvalidArgumentError("SymmetricallyEncrypted: incorrect key length")
}
if .prefix == nil {
.prefix = make([]byte, .blockSize()+2)
, := readFull(.contents, .prefix)
if != nil {
return nil,
}
} else if len(.prefix) != .blockSize()+2 {
return nil, errors.InvalidArgumentError("can't try ciphers with different block lengths")
}
:= OCFBResync
= OCFBNoResync
}
:= NewOCFBDecrypter(.new(), .prefix, )
if == nil {
return nil, errors.ErrKeyIncorrect
}
:= cipher.StreamReader{S: , R: .contents}
type seMDCReader struct {
in io.Reader
h hash.Hash
trailer [mdcTrailerSize]byte
scratch [mdcTrailerSize]byte
trailerUsed int
error bool
eof bool
}
func ( *seMDCReader) ( []byte) ( int, error) {
if .error {
= io.ErrUnexpectedEOF
return
}
if .eof {
= io.EOF
return
}
for .trailerUsed < mdcTrailerSize {
, = .in.Read(.trailer[.trailerUsed:])
.trailerUsed +=
if == io.EOF {
if .trailerUsed != mdcTrailerSize {
= 0
= io.ErrUnexpectedEOF
.error = true
return
}
.eof = true
= 0
return
}
if != nil {
= 0
return
}
}
if len() <= mdcTrailerSize {
, = readFull(.in, .scratch[:len()])
copy(, .trailer[:])
.h.Write([:])
copy(.trailer[:], .trailer[:])
copy(.trailer[mdcTrailerSize-:], .scratch[:])
if < len() {
.eof = true
= io.EOF
}
return
}
, = .in.Read([mdcTrailerSize:])
copy(, .trailer[:])
.h.Write([:])
copy(.trailer[:], [:])
if == io.EOF {
.eof = true
}
return
}
const mdcPacketTagByte = byte(0x80) | 0x40 | 19
func ( *seMDCReader) () error {
if .error {
return errors.SignatureError("error during reading")
}
var [1024]byte
, := .Read([:])
if == io.EOF {
break
}
if != nil {
return errors.SignatureError("error during reading")
}
}
if .trailer[0] != mdcPacketTagByte || .trailer[1] != sha1.Size {
return errors.SignatureError("MDC packet not found")
}
.h.Write(.trailer[:2])
:= .h.Sum(nil)
if subtle.ConstantTimeCompare(, .trailer[2:]) != 1 {
return errors.SignatureError("hash mismatch")
}
return nil
}
type seMDCWriter struct {
w io.WriteCloser
h hash.Hash
}
func ( *seMDCWriter) ( []byte) ( int, error) {
.h.Write()
return .w.Write()
}
func ( *seMDCWriter) () ( error) {
var [mdcTrailerSize]byte
[0] = mdcPacketTagByte
[1] = sha1.Size
.h.Write([:2])
:= .h.Sum(nil)
copy([2:], )
_, = .w.Write([:])
if != nil {
return
}
return .w.Close()
}
type noOpCloser struct {
w io.Writer
}
func ( noOpCloser) ( []byte) ( int, error) {
return .w.Write()
}
func ( noOpCloser) () error {
return nil
}
func ( io.Writer, CipherFunction, []byte, *Config) ( io.WriteCloser, error) {
if .KeySize() != len() {
return nil, errors.InvalidArgumentError("SymmetricallyEncrypted.Serialize: bad key length")
}
:= noOpCloser{}
, := serializeStreamHeader(, packetTypeSymmetricallyEncryptedMDC)
if != nil {
return
}
_, = .Write([]byte{symmetricallyEncryptedVersion})
if != nil {
return
}
:= .new()
:= .BlockSize()
:= make([]byte, )
_, = .Random().Read()
if != nil {
return
}
, := NewOCFBEncrypter(, , OCFBNoResync)
_, = .Write()
if != nil {
return
}
:= cipher.StreamWriter{S: , W: }
:= sha1.New()
.Write()
.Write([-2:])
= &seMDCWriter{w: , h: }
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |