gitee.com/curryzheng/dm@v0.0.1/security/zze.go (about)

     1  /*
     2   * Copyright (c) 2000-2018, 达梦数据库有限公司.
     3   * All rights reserved.
     4   */
     5  
     6  package security
     7  
     8  import (
     9  	"math/big"
    10  )
    11  
    12  const (
    13  	DH_KEY_LENGTH int = 64
    14  	/* 低7位用于保存分组加密算法中的工作模式 */
    15  	WORK_MODE_MASK int = 0x007f
    16  	ECB_MODE       int = 0x1
    17  	CBC_MODE       int = 0x2
    18  	CFB_MODE       int = 0x4
    19  	OFB_MODE       int = 0x8
    20  	/* 高位保存加密算法 */
    21  	ALGO_MASK int = 0xff80
    22  	DES       int = 0x0080
    23  	DES3      int = 0x0100
    24  	AES128    int = 0x0200
    25  	AES192    int = 0x0400
    26  	AES256    int = 0x0800
    27  	RC4       int = 0x1000
    28  	MD5       int = 0x1100
    29  
    30  	// 用户名密码加密算法
    31  	DES_CFB int = 132
    32  	// 消息加密摘要长度
    33  	MD5_DIGEST_SIZE int = 16
    34  
    35  	MIN_EXTERNAL_CIPHER_ID int = 5000
    36  )
    37  
    38  var dhParaP = "C009D877BAF5FAF416B7F778E6115DCB90D65217DCC2F08A9DFCB5A192C593EBAB02929266B8DBFC2021039FDBD4B7FDE2B996E00008F57AE6EFB4ED3F17B6D3"
    39  var dhParaG = "5"
    40  var defaultIV = []byte{0x20, 0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
    41  	0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a,
    42  	0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x20}
    43  var p *big.Int
    44  var g *big.Int
    45  
    46  func NewClientKeyPair() (key *DhKey, err error) {
    47  	p, _ = new(big.Int).SetString(dhParaP, 16)
    48  	g, _ = new(big.Int).SetString(dhParaG, 16)
    49  	dhGroup := newDhGroup(p, g)
    50  	key, err = dhGroup.GeneratePrivateKey(nil)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return key, nil
    55  }
    56  
    57  func ComputeSessionKey(clientPrivKey *DhKey, serverPubKey []byte) []byte {
    58  	serverKeyX := bytes2Bn(serverPubKey)
    59  	clientPrivKeyX := clientPrivKey.GetX()
    60  	sessionKeyBN := serverKeyX.Exp(serverKeyX, clientPrivKeyX, p)
    61  	return Bn2Bytes(sessionKeyBN, 0)
    62  }
    63  
    64  func bytes2Bn(bnBytesSrc []byte) *big.Int {
    65  	if bnBytesSrc == nil {
    66  		return nil
    67  	}
    68  	if bnBytesSrc[0] == 0 {
    69  		return new(big.Int).SetBytes(bnBytesSrc)
    70  	}
    71  	validBytesCount := len(bnBytesSrc) + 1
    72  	bnBytesTo := make([]byte, validBytesCount)
    73  	bnBytesTo[0] = 0
    74  	copy(bnBytesTo[1:validBytesCount], bnBytesSrc)
    75  	return new(big.Int).SetBytes(bnBytesTo)
    76  }
    77  
    78  func Bn2Bytes(bn *big.Int, bnLen int) []byte {
    79  	var bnBytesSrc, bnBytesTemp, bnBytesTo []byte
    80  	var leading_zero_count int
    81  	validBytesCount := 0
    82  	if bn == nil {
    83  		return nil
    84  	}
    85  	bnBytesSrc = bn.Bytes()
    86  
    87  	// 去除首位0
    88  	if bnBytesSrc[0] != 0 {
    89  		bnBytesTemp = bnBytesSrc
    90  		validBytesCount = len(bnBytesTemp)
    91  	} else {
    92  		validBytesCount = len(bnBytesSrc) - 1
    93  		bnBytesTemp = make([]byte, validBytesCount)
    94  		copy(bnBytesTemp, bnBytesSrc[1:validBytesCount+1])
    95  	}
    96  
    97  	if bnLen == 0 {
    98  		leading_zero_count = 0
    99  	} else {
   100  		leading_zero_count = bnLen - validBytesCount
   101  	}
   102  	// 如果位数不足DH_KEY_LENGTH则在前面补0
   103  	if leading_zero_count > 0 {
   104  		bnBytesTo = make([]byte, DH_KEY_LENGTH)
   105  		i := 0
   106  		for i = 0; i < leading_zero_count; i++ {
   107  			bnBytesTo[i] = 0
   108  		}
   109  		copy(bnBytesTo[i:i+validBytesCount], bnBytesTemp)
   110  	} else {
   111  		bnBytesTo = bnBytesTemp
   112  	}
   113  	return bnBytesTo
   114  }