zzd.go 2.8 KB

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