| 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 |
// KeySize returns the key size, in bytes, of cipher. |
| 55 |
func (cipher CipherFunction) KeySize() int { |
| 56 |
switch cipher { |
| 57 |
case CAST5: |
| 58 |
return cast5.KeySize |
| 59 |
case AES128: |
| 60 |
return 16 |
| 61 |
case AES192, TripleDES: |
| 62 |
return 24 |
| 63 |
case AES256: |
| 64 |
return 32 |
| 65 |
} |
| 66 |
return 0 |
| 67 |
} |
| 68 |
|
| 69 |
// BlockSize returns the block size, in bytes, of cipher. |
| 70 |
func (cipher CipherFunction) BlockSize() int { |
| 71 |
switch cipher { |
| 72 |
case TripleDES: |
| 73 |
return des.BlockSize |
| 74 |
case CAST5: |
| 75 |
return 8 |
| 76 |
case AES128, AES192, AES256: |
| 77 |
return 16 |
| 78 |
} |
| 79 |
return 0 |
| 80 |
} |
| 81 |
|
| 82 |
// New returns a fresh instance of the given cipher. |
| 83 |
func (cipher CipherFunction) New(key []byte) (block cipher.Block) { |
| 84 |
var err error |
| 85 |
switch cipher { |
| 86 |
case TripleDES: |
| 87 |
block, err = des.NewTripleDESCipher(key) |
| 88 |
case CAST5: |
| 89 |
block, err = cast5.NewCipher(key) |
| 90 |
case AES128, AES192, AES256: |
| 91 |
block, err = aes.NewCipher(key) |
| 92 |
} |
| 93 |
if err != nil { |
| 94 |
panic(err.Error()) |
| 95 |
} |
| 96 |
return |
| 97 |
} |