mirror of
https://github.com/fatedier/frp.git
synced 2026-03-24 09:08:13 +08:00
vendor: add kcp-go package
This commit is contained in:
66
vendor/golang.org/x/crypto/xtea/block.go
generated
vendored
Normal file
66
vendor/golang.org/x/crypto/xtea/block.go
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
/*
|
||||
Implementation adapted from Needham and Wheeler's paper:
|
||||
http://www.cix.co.uk/~klockstone/xtea.pdf
|
||||
|
||||
A precalculated look up table is used during encryption/decryption for values that are based purely on the key.
|
||||
*/
|
||||
|
||||
package xtea
|
||||
|
||||
// XTEA is based on 64 rounds.
|
||||
const numRounds = 64
|
||||
|
||||
// blockToUint32 reads an 8 byte slice into two uint32s.
|
||||
// The block is treated as big endian.
|
||||
func blockToUint32(src []byte) (uint32, uint32) {
|
||||
r0 := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
|
||||
r1 := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// uint32ToBlock writes two uint32s into an 8 byte data block.
|
||||
// Values are written as big endian.
|
||||
func uint32ToBlock(v0, v1 uint32, dst []byte) {
|
||||
dst[0] = byte(v0 >> 24)
|
||||
dst[1] = byte(v0 >> 16)
|
||||
dst[2] = byte(v0 >> 8)
|
||||
dst[3] = byte(v0)
|
||||
dst[4] = byte(v1 >> 24)
|
||||
dst[5] = byte(v1 >> 16)
|
||||
dst[6] = byte(v1 >> 8)
|
||||
dst[7] = byte(v1 >> 0)
|
||||
}
|
||||
|
||||
// encryptBlock encrypts a single 8 byte block using XTEA.
|
||||
func encryptBlock(c *Cipher, dst, src []byte) {
|
||||
v0, v1 := blockToUint32(src)
|
||||
|
||||
// Two rounds of XTEA applied per loop
|
||||
for i := 0; i < numRounds; {
|
||||
v0 += ((v1<<4 ^ v1>>5) + v1) ^ c.table[i]
|
||||
i++
|
||||
v1 += ((v0<<4 ^ v0>>5) + v0) ^ c.table[i]
|
||||
i++
|
||||
}
|
||||
|
||||
uint32ToBlock(v0, v1, dst)
|
||||
}
|
||||
|
||||
// decryptBlock decrypt a single 8 byte block using XTEA.
|
||||
func decryptBlock(c *Cipher, dst, src []byte) {
|
||||
v0, v1 := blockToUint32(src)
|
||||
|
||||
// Two rounds of XTEA applied per loop
|
||||
for i := numRounds; i > 0; {
|
||||
i--
|
||||
v1 -= ((v0<<4 ^ v0>>5) + v0) ^ c.table[i]
|
||||
i--
|
||||
v0 -= ((v1<<4 ^ v1>>5) + v1) ^ c.table[i]
|
||||
}
|
||||
|
||||
uint32ToBlock(v0, v1, dst)
|
||||
}
|
||||
82
vendor/golang.org/x/crypto/xtea/cipher.go
generated
vendored
Normal file
82
vendor/golang.org/x/crypto/xtea/cipher.go
generated
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package xtea implements XTEA encryption, as defined in Needham and Wheeler's
|
||||
// 1997 technical report, "Tea extensions."
|
||||
package xtea
|
||||
|
||||
// For details, see http://www.cix.co.uk/~klockstone/xtea.pdf
|
||||
|
||||
import "strconv"
|
||||
|
||||
// The XTEA block size in bytes.
|
||||
const BlockSize = 8
|
||||
|
||||
// A Cipher is an instance of an XTEA cipher using a particular key.
|
||||
// table contains a series of precalculated values that are used each round.
|
||||
type Cipher struct {
|
||||
table [64]uint32
|
||||
}
|
||||
|
||||
type KeySizeError int
|
||||
|
||||
func (k KeySizeError) Error() string {
|
||||
return "crypto/xtea: invalid key size " + strconv.Itoa(int(k))
|
||||
}
|
||||
|
||||
// NewCipher creates and returns a new Cipher.
|
||||
// The key argument should be the XTEA key.
|
||||
// XTEA only supports 128 bit (16 byte) keys.
|
||||
func NewCipher(key []byte) (*Cipher, error) {
|
||||
k := len(key)
|
||||
switch k {
|
||||
default:
|
||||
return nil, KeySizeError(k)
|
||||
case 16:
|
||||
break
|
||||
}
|
||||
|
||||
c := new(Cipher)
|
||||
initCipher(c, key)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// BlockSize returns the XTEA block size, 8 bytes.
|
||||
// It is necessary to satisfy the Block interface in the
|
||||
// package "crypto/cipher".
|
||||
func (c *Cipher) BlockSize() int { return BlockSize }
|
||||
|
||||
// Encrypt encrypts the 8 byte buffer src using the key and stores the result in dst.
|
||||
// Note that for amounts of data larger than a block,
|
||||
// it is not safe to just call Encrypt on successive blocks;
|
||||
// instead, use an encryption mode like CBC (see crypto/cipher/cbc.go).
|
||||
func (c *Cipher) Encrypt(dst, src []byte) { encryptBlock(c, dst, src) }
|
||||
|
||||
// Decrypt decrypts the 8 byte buffer src using the key k and stores the result in dst.
|
||||
func (c *Cipher) Decrypt(dst, src []byte) { decryptBlock(c, dst, src) }
|
||||
|
||||
// initCipher initializes the cipher context by creating a look up table
|
||||
// of precalculated values that are based on the key.
|
||||
func initCipher(c *Cipher, key []byte) {
|
||||
// Load the key into four uint32s
|
||||
var k [4]uint32
|
||||
for i := 0; i < len(k); i++ {
|
||||
j := i << 2 // Multiply by 4
|
||||
k[i] = uint32(key[j+0])<<24 | uint32(key[j+1])<<16 | uint32(key[j+2])<<8 | uint32(key[j+3])
|
||||
}
|
||||
|
||||
// Precalculate the table
|
||||
const delta = 0x9E3779B9
|
||||
var sum uint32 = 0
|
||||
|
||||
// Two rounds of XTEA applied per loop
|
||||
for i := 0; i < numRounds; {
|
||||
c.table[i] = sum + k[sum&3]
|
||||
i++
|
||||
sum += delta
|
||||
c.table[i] = sum + k[(sum>>11)&3]
|
||||
i++
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user