| 1 | package internalscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ |
| 2 | |
| 3 | //go:generate go run chacha_core_gen.go |
| 4 | |
| 5 | import ( |
| 6 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | wordSizescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/wordSize. = 4 // the size of ChaCha20's words |
| 11 | stateSizescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/stateSize. = 16 // the size of ChaCha20's state, in words |
| 12 | blockSizescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/blockSize. = stateSizescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/stateSize. * wordSizescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/wordSize. // the size of ChaCha20's block, in bytes |
| 13 | ) |
| 14 | |
| 15 | type ChaCha20Streamscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream# struct { |
| 16 | statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state. [stateSizescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/stateSize.]uint32 // the state as an array of 16 32-bit words |
| 17 | blockscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#block. [blockSizescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/blockSize.]byte // the keystream as an array of 64 bytes |
| 18 | offsetscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#offset. int // the offset of used bytes in block |
| 19 | roundsscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#rounds. int |
| 20 | } |
| 21 | |
| 22 | func NewChaCha20Streamscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/NewChaCha20Stream().(key []byte, nonce []byte, rounds int) *ChaCha20Streamscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream# { |
| 23 | s := new(ChaCha20Streamscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#) |
| 24 | // the magic constants for 256-bit keys |
| 25 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[0] = 0x61707865 |
| 26 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[1] = 0x3320646e |
| 27 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[2] = 0x79622d32 |
| 28 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[3] = 0x6b206574 |
| 29 | |
| 30 | for i := 0; i < 8; i++ { |
| 31 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[i+4] = binaryscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/.LittleEndianscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/LittleEndian..Uint32scip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/littleEndian#Uint32().(key[i*4 : i*4+4]) |
| 32 | } |
| 33 | |
| 34 | switch len(nonce) { |
| 35 | case 8: |
| 36 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[14] = binaryscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/.LittleEndianscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/LittleEndian..Uint32scip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/littleEndian#Uint32().(nonce[0:]) |
| 37 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[15] = binaryscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/.LittleEndianscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/LittleEndian..Uint32scip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/littleEndian#Uint32().(nonce[4:]) |
| 38 | case 12: |
| 39 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[13] = binaryscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/.LittleEndianscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/LittleEndian..Uint32scip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/littleEndian#Uint32().(nonce[0:4]) |
| 40 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[14] = binaryscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/.LittleEndianscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/LittleEndian..Uint32scip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/littleEndian#Uint32().(nonce[4:8]) |
| 41 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[15] = binaryscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/.LittleEndianscip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/LittleEndian..Uint32scip-go gomod github.com/golang/go/src go1.15 `encoding/binary`/littleEndian#Uint32().(nonce[8:12]) |
| 42 | default: |
| 43 | panic("bad nonce length") |
| 44 | } |
| 45 | |
| 46 | s.roundsscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#rounds. = rounds |
| 47 | ChaCha20Blockscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Block().(&s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state., s.blockscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#block.[:], s.roundsscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#rounds.) |
| 48 | return s |
| 49 | } |
| 50 | |
| 51 | func (s *ChaCha20Streamscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#) XORKeyStreamscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#XORKeyStream().(dst, src []byte) { |
| 52 | // Stride over the input in 64-byte blocks, minus the amount of keystream |
| 53 | // previously used. This will produce best results when processing blocks |
| 54 | // of a size evenly divisible by 64. |
| 55 | i := 0 |
| 56 | max := len(src) |
| 57 | for i < max { |
| 58 | gap := blockSizescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/blockSize. - s.offsetscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#offset. |
| 59 | |
| 60 | limit := i + gap |
| 61 | if limit > max { |
| 62 | limit = max |
| 63 | } |
| 64 | |
| 65 | o := s.offsetscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#offset. |
| 66 | for j := i; j < limit; j++ { |
| 67 | dst[j] = src[j] ^ s.blockscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#block.[o] |
| 68 | o++ |
| 69 | } |
| 70 | |
| 71 | i += gap |
| 72 | s.offsetscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#offset. = o |
| 73 | |
| 74 | if o == blockSizescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/blockSize. { |
| 75 | s.offsetscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#offset. = 0 |
| 76 | s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state.[12]++ |
| 77 | ChaCha20Blockscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Block().(&s.statescip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#state., s.blockscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#block.[:], s.roundsscip-go gomod v2ray.com/core . `v2ray.com/core/common/crypto/internal`/ChaCha20Stream#rounds.) |
| 78 | } |
| 79 | } |
| 80 | } |