...

Source file src/github.com/ProtonMail/go-crypto/openpgp/internal/algorithm/cipher.go

Documentation: github.com/ProtonMail/go-crypto/openpgp/internal/algorithm

     1  // Copyright 2017 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 algorithm
     6  
     7  import (
     8  	"crypto/aes"
     9  	"crypto/cipher"
    10  	"crypto/des"
    11  
    12  	"golang.org/x/crypto/cast5"
    13  )
    14  
    15  // Cipher is an official symmetric key cipher algorithm. See RFC 4880,
    16  // section 9.2.
    17  type Cipher interface {
    18  	// Id returns the algorithm ID, as a byte, of the cipher.
    19  	Id() uint8
    20  	// KeySize returns the key size, in bytes, of the cipher.
    21  	KeySize() int
    22  	// BlockSize returns the block size, in bytes, of the cipher.
    23  	BlockSize() int
    24  	// New returns a fresh instance of the given cipher.
    25  	New(key []byte) cipher.Block
    26  }
    27  
    28  // The following constants mirror the OpenPGP standard (RFC 4880).
    29  const (
    30  	TripleDES = CipherFunction(2)
    31  	CAST5     = CipherFunction(3)
    32  	AES128    = CipherFunction(7)
    33  	AES192    = CipherFunction(8)
    34  	AES256    = CipherFunction(9)
    35  )
    36  
    37  // CipherById represents the different block ciphers specified for OpenPGP. See
    38  // http://www.iana.org/assignments/pgp-parameters/pgp-parameters.xhtml#pgp-parameters-13
    39  var CipherById = map[uint8]Cipher{
    40  	TripleDES.Id(): TripleDES,
    41  	CAST5.Id():     CAST5,
    42  	AES128.Id():    AES128,
    43  	AES192.Id():    AES192,
    44  	AES256.Id():    AES256,
    45  }
    46  
    47  type CipherFunction uint8
    48  
    49  // ID returns the algorithm Id, as a byte, of cipher.
    50  func (sk CipherFunction) Id() uint8 {
    51  	return uint8(sk)
    52  }
    53  
    54  var keySizeByID = map[uint8]int{
    55  	TripleDES.Id(): 24,
    56  	CAST5.Id():     cast5.KeySize,
    57  	AES128.Id():    16,
    58  	AES192.Id():    24,
    59  	AES256.Id():    32,
    60  }
    61  
    62  // KeySize returns the key size, in bytes, of cipher.
    63  func (cipher CipherFunction) KeySize() int {
    64  	switch cipher {
    65  	case TripleDES:
    66  		return 24
    67  	case CAST5:
    68  		return cast5.KeySize
    69  	case AES128:
    70  		return 16
    71  	case AES192:
    72  		return 24
    73  	case AES256:
    74  		return 32
    75  	}
    76  	return 0
    77  }
    78  
    79  // BlockSize returns the block size, in bytes, of cipher.
    80  func (cipher CipherFunction) BlockSize() int {
    81  	switch cipher {
    82  	case TripleDES:
    83  		return des.BlockSize
    84  	case CAST5:
    85  		return 8
    86  	case AES128, AES192, AES256:
    87  		return 16
    88  	}
    89  	return 0
    90  }
    91  
    92  // New returns a fresh instance of the given cipher.
    93  func (cipher CipherFunction) New(key []byte) (block cipher.Block) {
    94  	var err error
    95  	switch cipher {
    96  	case TripleDES:
    97  		block, err = des.NewTripleDESCipher(key)
    98  	case CAST5:
    99  		block, err = cast5.NewCipher(key)
   100  	case AES128, AES192, AES256:
   101  		block, err = aes.NewCipher(key)
   102  	}
   103  	if err != nil {
   104  		panic(err.Error())
   105  	}
   106  	return
   107  }
   108  

View as plain text