1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package packet 6 7 import ( 8 "crypto" 9 "crypto/rand" 10 "io" 11 "time" 12 ) 13 14 // Config collects a number of parameters along with sensible defaults. 15 // A nil *Config is valid and results in all default values. 16 type Config struct { 17 // Rand provides the source of entropy. 18 // If nil, the crypto/rand Reader is used. 19 Rand io.Reader 20 // DefaultHash is the default hash function to be used. 21 // If zero, SHA-256 is used. 22 DefaultHash crypto.Hash 23 // DefaultCipher is the cipher to be used. 24 // If zero, AES-128 is used. 25 DefaultCipher CipherFunction 26 // Time returns the current time as the number of seconds since the 27 // epoch. If Time is nil, time.Now is used. 28 Time func() time.Time 29 // DefaultCompressionAlgo is the compression algorithm to be 30 // applied to the plaintext before encryption. If zero, no 31 // compression is done. 32 DefaultCompressionAlgo CompressionAlgo 33 // CompressionConfig configures the compression settings. 34 CompressionConfig *CompressionConfig 35 // S2KCount is only used for symmetric encryption. It 36 // determines the strength of the passphrase stretching when 37 // the said passphrase is hashed to produce a key. S2KCount 38 // should be between 1024 and 65011712, inclusive. If Config 39 // is nil or S2KCount is 0, the value 65536 used. Not all 40 // values in the above range can be represented. S2KCount will 41 // be rounded up to the next representable value if it cannot 42 // be encoded exactly. When set, it is strongly encrouraged to 43 // use a value that is at least 65536. See RFC 4880 Section 44 // 3.7.1.3. 45 S2KCount int 46 // RSABits is the number of bits in new RSA keys made with NewEntity. 47 // If zero, then 2048 bit keys are created. 48 RSABits int 49 } 50 51 func (c *Config) Random() io.Reader { 52 if c == nil || c.Rand == nil { 53 return rand.Reader 54 } 55 return c.Rand 56 } 57 58 func (c *Config) Hash() crypto.Hash { 59 if c == nil || uint(c.DefaultHash) == 0 { 60 return crypto.SHA256 61 } 62 return c.DefaultHash 63 } 64 65 func (c *Config) Cipher() CipherFunction { 66 if c == nil || uint8(c.DefaultCipher) == 0 { 67 return CipherAES128 68 } 69 return c.DefaultCipher 70 } 71 72 func (c *Config) Now() time.Time { 73 if c == nil || c.Time == nil { 74 return time.Now() 75 } 76 return c.Time() 77 } 78 79 func (c *Config) Compression() CompressionAlgo { 80 if c == nil { 81 return CompressionNone 82 } 83 return c.DefaultCompressionAlgo 84 } 85 86 func (c *Config) PasswordHashIterations() int { 87 if c == nil || c.S2KCount == 0 { 88 return 0 89 } 90 return c.S2KCount 91 } 92