From f57e6a4a1d09e0e30617ec7c07f73a9553d4d6b4 Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Mon, 6 Mar 2023 17:18:55 +0800 Subject: [PATCH] pkcs8: rename internal struct --- pkcs8/cipher.go | 21 +++++++++++---------- pkcs8/cipher_aes.go | 12 ++++++------ pkcs8/cipher_des.go | 4 ++-- pkcs8/cipher_sm4.go | 4 ++-- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/pkcs8/cipher.go b/pkcs8/cipher.go index 135f0f4..aa41487 100644 --- a/pkcs8/cipher.go +++ b/pkcs8/cipher.go @@ -16,22 +16,22 @@ func genRandom(len int) ([]byte, error) { return value, err } -type cipherWithBlock struct { +type cbcBlockCipher struct { oid asn1.ObjectIdentifier ivSize int keySize int newBlock func(key []byte) (cipher.Block, error) } -func (c cipherWithBlock) KeySize() int { +func (c cbcBlockCipher) KeySize() int { return c.keySize } -func (c cipherWithBlock) OID() asn1.ObjectIdentifier { +func (c cbcBlockCipher) OID() asn1.ObjectIdentifier { return c.oid } -func (c cipherWithBlock) Encrypt(key, plaintext []byte) (*pkix.AlgorithmIdentifier, []byte, error) { +func (c cbcBlockCipher) Encrypt(key, plaintext []byte) (*pkix.AlgorithmIdentifier, []byte, error) { block, err := c.newBlock(key) if err != nil { return nil, nil, err @@ -44,6 +44,7 @@ func (c cipherWithBlock) Encrypt(key, plaintext []byte) (*pkix.AlgorithmIdentifi if err != nil { return nil, nil, err } + marshalledIV, err := asn1.Marshal(iv) if err != nil { return nil, nil, err @@ -57,7 +58,7 @@ func (c cipherWithBlock) Encrypt(key, plaintext []byte) (*pkix.AlgorithmIdentifi return &encryptionScheme, ciphertext, nil } -func (c cipherWithBlock) Decrypt(key []byte, parameters *asn1.RawValue, encryptedKey []byte) ([]byte, error) { +func (c cbcBlockCipher) Decrypt(key []byte, parameters *asn1.RawValue, encryptedKey []byte) ([]byte, error) { block, err := c.newBlock(key) if err != nil { return nil, err @@ -88,7 +89,7 @@ func cbcDecrypt(block cipher.Block, key, iv, ciphertext []byte) ([]byte, error) return pkcs7.Unpad(plaintext) } -type cipherWithGCM struct { +type gcmBlockCipher struct { oid asn1.ObjectIdentifier nonceSize int keySize int @@ -101,15 +102,15 @@ type gcmParameters struct { ICVLen int } -func (c cipherWithGCM) KeySize() int { +func (c gcmBlockCipher) KeySize() int { return c.keySize } -func (c cipherWithGCM) OID() asn1.ObjectIdentifier { +func (c gcmBlockCipher) OID() asn1.ObjectIdentifier { return c.oid } -func (c cipherWithGCM) Encrypt(key, plaintext []byte) (*pkix.AlgorithmIdentifier, []byte, error) { +func (c gcmBlockCipher) Encrypt(key, plaintext []byte) (*pkix.AlgorithmIdentifier, []byte, error) { block, err := c.newBlock(key) if err != nil { return nil, nil, err @@ -141,7 +142,7 @@ func (c cipherWithGCM) Encrypt(key, plaintext []byte) (*pkix.AlgorithmIdentifier return &encryptionAlgorithm, ciphertext, nil } -func (c cipherWithGCM) Decrypt(key []byte, parameters *asn1.RawValue, encryptedKey []byte) ([]byte, error) { +func (c gcmBlockCipher) Decrypt(key []byte, parameters *asn1.RawValue, encryptedKey []byte) ([]byte, error) { block, err := c.newBlock(key) if err != nil { return nil, err diff --git a/pkcs8/cipher_aes.go b/pkcs8/cipher_aes.go index 00c6da2..549bd64 100644 --- a/pkcs8/cipher_aes.go +++ b/pkcs8/cipher_aes.go @@ -36,7 +36,7 @@ func init() { } // AES128CBC is the 128-bit key AES cipher in CBC mode. -var AES128CBC = cipherWithBlock{ +var AES128CBC = cbcBlockCipher{ ivSize: aes.BlockSize, keySize: 16, newBlock: aes.NewCipher, @@ -44,7 +44,7 @@ var AES128CBC = cipherWithBlock{ } // AES128GCM is the 128-bit key AES cipher in GCM mode. -var AES128GCM = cipherWithGCM{ +var AES128GCM = gcmBlockCipher{ nonceSize: 12, keySize: 16, newBlock: aes.NewCipher, @@ -52,7 +52,7 @@ var AES128GCM = cipherWithGCM{ } // AES192CBC is the 192-bit key AES cipher in CBC mode. -var AES192CBC = cipherWithBlock{ +var AES192CBC = cbcBlockCipher{ ivSize: aes.BlockSize, keySize: 24, newBlock: aes.NewCipher, @@ -60,7 +60,7 @@ var AES192CBC = cipherWithBlock{ } // AES192GCM is the 912-bit key AES cipher in GCM mode. -var AES192GCM = cipherWithGCM{ +var AES192GCM = gcmBlockCipher{ nonceSize: 12, keySize: 24, newBlock: aes.NewCipher, @@ -68,7 +68,7 @@ var AES192GCM = cipherWithGCM{ } // AES256CBC is the 256-bit key AES cipher in CBC mode. -var AES256CBC = cipherWithBlock{ +var AES256CBC = cbcBlockCipher{ ivSize: aes.BlockSize, keySize: 32, newBlock: aes.NewCipher, @@ -76,7 +76,7 @@ var AES256CBC = cipherWithBlock{ } // AES256GCM is the 256-bit key AES cipher in GCM mode. -var AES256GCM = cipherWithGCM{ +var AES256GCM = gcmBlockCipher{ nonceSize: 12, keySize: 32, newBlock: aes.NewCipher, diff --git a/pkcs8/cipher_des.go b/pkcs8/cipher_des.go index e229e2f..ab110b2 100644 --- a/pkcs8/cipher_des.go +++ b/pkcs8/cipher_des.go @@ -20,7 +20,7 @@ func init() { }) } -var DESCBC = cipherWithBlock{ +var DESCBC = cbcBlockCipher{ ivSize: des.BlockSize, keySize: 8, newBlock: des.NewCipher, @@ -28,7 +28,7 @@ var DESCBC = cipherWithBlock{ } // TripleDESCBC is the 168-bit key 3DES cipher in CBC mode. -var TripleDESCBC = cipherWithBlock{ +var TripleDESCBC = cbcBlockCipher{ ivSize: des.BlockSize, keySize: 24, newBlock: des.NewTripleDESCipher, diff --git a/pkcs8/cipher_sm4.go b/pkcs8/cipher_sm4.go index 90ec44a..cfd4c97 100644 --- a/pkcs8/cipher_sm4.go +++ b/pkcs8/cipher_sm4.go @@ -21,7 +21,7 @@ func init() { } // SM4CBC is the 128-bit key SM4 cipher in CBC mode. -var SM4CBC = cipherWithBlock{ +var SM4CBC = cbcBlockCipher{ ivSize: sm4.BlockSize, keySize: 16, newBlock: sm4.NewCipher, @@ -29,7 +29,7 @@ var SM4CBC = cipherWithBlock{ } // SM4GCM is the 128-bit key SM4 cipher in GCM mode. -var SM4GCM = cipherWithGCM{ +var SM4GCM = gcmBlockCipher{ nonceSize: 12, keySize: 16, newBlock: sm4.NewCipher,