qtls.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package qtls
  2. import (
  3. "crypto"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "encoding/binary"
  7. "io"
  8. "golang.org/x/crypto/hkdf"
  9. )
  10. const (
  11. VersionDraft29 = 0xff00001d
  12. Version1 = 0x1
  13. Version2 = 0x709a50c4
  14. )
  15. var (
  16. SaltOld = []byte{0xaf, 0xbf, 0xec, 0x28, 0x99, 0x93, 0xd2, 0x4c, 0x9e, 0x97, 0x86, 0xf1, 0x9c, 0x61, 0x11, 0xe0, 0x43, 0x90, 0xa8, 0x99}
  17. SaltV1 = []byte{0x38, 0x76, 0x2c, 0xf7, 0xf5, 0x59, 0x34, 0xb3, 0x4d, 0x17, 0x9a, 0xe6, 0xa4, 0xc8, 0x0c, 0xad, 0xcc, 0xbb, 0x7f, 0x0a}
  18. SaltV2 = []byte{0xa7, 0x07, 0xc2, 0x03, 0xa5, 0x9b, 0x47, 0x18, 0x4a, 0x1d, 0x62, 0xca, 0x57, 0x04, 0x06, 0xea, 0x7a, 0xe3, 0xe5, 0xd3}
  19. )
  20. const (
  21. HKDFLabelKeyV1 = "quic key"
  22. HKDFLabelKeyV2 = "quicv2 key"
  23. HKDFLabelIVV1 = "quic iv"
  24. HKDFLabelIVV2 = "quicv2 iv"
  25. HKDFLabelHeaderProtectionV1 = "quic hp"
  26. HKDFLabelHeaderProtectionV2 = "quicv2 hp"
  27. )
  28. func AEADAESGCMTLS13(key, nonceMask []byte) cipher.AEAD {
  29. if len(nonceMask) != 12 {
  30. panic("tls: internal error: wrong nonce length")
  31. }
  32. aes, err := aes.NewCipher(key)
  33. if err != nil {
  34. panic(err)
  35. }
  36. aead, err := cipher.NewGCM(aes)
  37. if err != nil {
  38. panic(err)
  39. }
  40. ret := &xorNonceAEAD{aead: aead}
  41. copy(ret.nonceMask[:], nonceMask)
  42. return ret
  43. }
  44. type xorNonceAEAD struct {
  45. nonceMask [12]byte
  46. aead cipher.AEAD
  47. }
  48. func (f *xorNonceAEAD) NonceSize() int { return 8 } // 64-bit sequence number
  49. func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
  50. func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
  51. func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  52. for i, b := range nonce {
  53. f.nonceMask[4+i] ^= b
  54. }
  55. result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
  56. for i, b := range nonce {
  57. f.nonceMask[4+i] ^= b
  58. }
  59. return result
  60. }
  61. func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  62. for i, b := range nonce {
  63. f.nonceMask[4+i] ^= b
  64. }
  65. result, err := f.aead.Open(out, f.nonceMask[:], ciphertext, additionalData)
  66. for i, b := range nonce {
  67. f.nonceMask[4+i] ^= b
  68. }
  69. return result, err
  70. }
  71. func HKDFExpandLabel(hash crypto.Hash, secret, context []byte, label string, length int) []byte {
  72. b := make([]byte, 3, 3+6+len(label)+1+len(context))
  73. binary.BigEndian.PutUint16(b, uint16(length))
  74. b[2] = uint8(6 + len(label))
  75. b = append(b, []byte("tls13 ")...)
  76. b = append(b, []byte(label)...)
  77. b = b[:3+6+len(label)+1]
  78. b[3+6+len(label)] = uint8(len(context))
  79. b = append(b, context...)
  80. out := make([]byte, length)
  81. n, err := hkdf.Expand(hash.New, secret, b).Read(out)
  82. if err != nil || n != length {
  83. panic("quic: HKDF-Expand-Label invocation failed unexpectedly")
  84. }
  85. return out
  86. }
  87. func ReadUvarint(r io.ByteReader) (uint64, error) {
  88. firstByte, err := r.ReadByte()
  89. if err != nil {
  90. return 0, err
  91. }
  92. // the first two bits of the first byte encode the length
  93. len := 1 << ((firstByte & 0xc0) >> 6)
  94. b1 := firstByte & (0xff - 0xc0)
  95. if len == 1 {
  96. return uint64(b1), nil
  97. }
  98. b2, err := r.ReadByte()
  99. if err != nil {
  100. return 0, err
  101. }
  102. if len == 2 {
  103. return uint64(b2) + uint64(b1)<<8, nil
  104. }
  105. b3, err := r.ReadByte()
  106. if err != nil {
  107. return 0, err
  108. }
  109. b4, err := r.ReadByte()
  110. if err != nil {
  111. return 0, err
  112. }
  113. if len == 4 {
  114. return uint64(b4) + uint64(b3)<<8 + uint64(b2)<<16 + uint64(b1)<<24, nil
  115. }
  116. b5, err := r.ReadByte()
  117. if err != nil {
  118. return 0, err
  119. }
  120. b6, err := r.ReadByte()
  121. if err != nil {
  122. return 0, err
  123. }
  124. b7, err := r.ReadByte()
  125. if err != nil {
  126. return 0, err
  127. }
  128. b8, err := r.ReadByte()
  129. if err != nil {
  130. return 0, err
  131. }
  132. return uint64(b8) + uint64(b7)<<8 + uint64(b6)<<16 + uint64(b5)<<24 + uint64(b4)<<32 + uint64(b3)<<40 + uint64(b2)<<48 + uint64(b1)<<56, nil
  133. }