...

Source file src/github.com/tjfoc/gmsm/pkcs12/crypto.go

Documentation: github.com/tjfoc/gmsm/pkcs12

     1  // Copyright 2015 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 pkcs12
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/cipher"
    10  	"crypto/des"
    11  	"crypto/x509/pkix"
    12  	"encoding/asn1"
    13  	"errors"
    14  )
    15  
    16  var (
    17  	oidPBEWithSHAAnd3KeyTripleDESCBC = asn1.ObjectIdentifier([]int{1, 2, 840, 113549, 1, 12, 1, 3})
    18  	oidPBEWithSHAAnd40BitRC2CBC      = asn1.ObjectIdentifier([]int{1, 2, 840, 113549, 1, 12, 1, 6})
    19  )
    20  
    21  // pbeCipher is an abstraction of a PKCS#12 cipher.
    22  type pbeCipher interface {
    23  	// create returns a cipher.Block given a key.
    24  	create(key []byte) (cipher.Block, error)
    25  	// deriveKey returns a key derived from the given password and salt.
    26  	deriveKey(salt, password []byte, iterations int) []byte
    27  	// deriveKey returns an IV derived from the given password and salt.
    28  	deriveIV(salt, password []byte, iterations int) []byte
    29  }
    30  
    31  type shaWithTripleDESCBC struct{}
    32  
    33  func (shaWithTripleDESCBC) create(key []byte) (cipher.Block, error) {
    34  	return des.NewTripleDESCipher(key)
    35  }
    36  
    37  func (shaWithTripleDESCBC) deriveKey(salt, password []byte, iterations int) []byte {
    38  	return pbkdf(sha1Sum, 20, 64, salt, password, iterations, 1, 24)
    39  }
    40  
    41  func (shaWithTripleDESCBC) deriveIV(salt, password []byte, iterations int) []byte {
    42  	return pbkdf(sha1Sum, 20, 64, salt, password, iterations, 2, 8)
    43  }
    44  
    45  type shaWith40BitRC2CBC struct{}
    46  
    47  func (shaWith40BitRC2CBC) create(key []byte) (cipher.Block, error) {
    48  	return New(key, len(key)*8)
    49  }
    50  
    51  func (shaWith40BitRC2CBC) deriveKey(salt, password []byte, iterations int) []byte {
    52  	return pbkdf(sha1Sum, 20, 64, salt, password, iterations, 1, 5)
    53  }
    54  
    55  func (shaWith40BitRC2CBC) deriveIV(salt, password []byte, iterations int) []byte {
    56  	return pbkdf(sha1Sum, 20, 64, salt, password, iterations, 2, 8)
    57  }
    58  
    59  type pbeParams struct {
    60  	Salt       []byte
    61  	Iterations int
    62  }
    63  
    64  func pbeCipherFor(algorithm pkix.AlgorithmIdentifier, password []byte) (cipher.Block, []byte, error) {
    65  	var cipherType pbeCipher
    66  
    67  	switch {
    68  	case algorithm.Algorithm.Equal(oidPBEWithSHAAnd3KeyTripleDESCBC):
    69  		cipherType = shaWithTripleDESCBC{}
    70  	case algorithm.Algorithm.Equal(oidPBEWithSHAAnd40BitRC2CBC):
    71  		cipherType = shaWith40BitRC2CBC{}
    72  	default:
    73  		return nil, nil, NotImplementedError("algorithm " + algorithm.Algorithm.String() + " is not supported")
    74  	}
    75  
    76  	var params pbeParams
    77  	if err := unmarshal(algorithm.Parameters.FullBytes, &params); err != nil {
    78  		return nil, nil, err
    79  	}
    80  
    81  	key := cipherType.deriveKey(params.Salt, password, params.Iterations)
    82  	iv := cipherType.deriveIV(params.Salt, password, params.Iterations)
    83  
    84  	block, err := cipherType.create(key)
    85  	if err != nil {
    86  		return nil, nil, err
    87  	}
    88  
    89  	return block, iv, nil
    90  }
    91  
    92  func pbDecrypterFor(algorithm pkix.AlgorithmIdentifier, password []byte) (cipher.BlockMode, int, error) {
    93  	block, iv, err := pbeCipherFor(algorithm, password)
    94  	if err != nil {
    95  		return nil, 0, err
    96  	}
    97  
    98  	return cipher.NewCBCDecrypter(block, iv), block.BlockSize(), nil
    99  }
   100  
   101  func pbDecrypt(info decryptable, password []byte) (decrypted []byte, err error) {
   102  	cbc, blockSize, err := pbDecrypterFor(info.Algorithm(), password)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	encrypted := info.Data()
   108  	if len(encrypted) == 0 {
   109  		return nil, errors.New("go-pkcs12: empty encrypted data")
   110  	}
   111  	if len(encrypted)%blockSize != 0 {
   112  		return nil, errors.New("go-pkcs12: input is not a multiple of the block size")
   113  	}
   114  	decrypted = make([]byte, len(encrypted))
   115  	cbc.CryptBlocks(decrypted, encrypted)
   116  
   117  	psLen := int(decrypted[len(decrypted)-1])
   118  	if psLen == 0 || psLen > blockSize {
   119  		return nil, ErrDecryption
   120  	}
   121  
   122  	if len(decrypted) < psLen {
   123  		return nil, ErrDecryption
   124  	}
   125  	ps := decrypted[len(decrypted)-psLen:]
   126  	decrypted = decrypted[:len(decrypted)-psLen]
   127  	if bytes.Compare(ps, bytes.Repeat([]byte{byte(psLen)}, psLen)) != 0 {
   128  		return nil, ErrDecryption
   129  	}
   130  
   131  	return
   132  }
   133  
   134  // decryptable abstracts a object that contains ciphertext.
   135  type decryptable interface {
   136  	Algorithm() pkix.AlgorithmIdentifier
   137  	Data() []byte
   138  }
   139  
   140  func pbEncrypterFor(algorithm pkix.AlgorithmIdentifier, password []byte) (cipher.BlockMode, int, error) {
   141  	block, iv, err := pbeCipherFor(algorithm, password)
   142  	if err != nil {
   143  		return nil, 0, err
   144  	}
   145  
   146  	return cipher.NewCBCEncrypter(block, iv), block.BlockSize(), nil
   147  }
   148  
   149  func pbEncrypt(info encryptable, decrypted []byte, password []byte) error {
   150  	cbc, blockSize, err := pbEncrypterFor(info.Algorithm(), password)
   151  	if err != nil {
   152  		return err
   153  	}
   154  
   155  	psLen := blockSize - len(decrypted)%blockSize
   156  	encrypted := make([]byte, len(decrypted)+psLen)
   157  	copy(encrypted[:len(decrypted)], decrypted)
   158  	copy(encrypted[len(decrypted):], bytes.Repeat([]byte{byte(psLen)}, psLen))
   159  	cbc.CryptBlocks(encrypted, encrypted)
   160  
   161  	info.SetData(encrypted)
   162  
   163  	return nil
   164  }
   165  
   166  // encryptable abstracts a object that contains ciphertext.
   167  type encryptable interface {
   168  	Algorithm() pkix.AlgorithmIdentifier
   169  	SetData([]byte)
   170  }
   171  

View as plain text