-
Notifications
You must be signed in to change notification settings - Fork 37
port shadowsocks 2022 from LostAttractor/next #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
olicesx
wants to merge
5
commits into
daeuniverse:main
Choose a base branch
from
olicesx:feat/ss2022-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb1ed93
port shadowsocks 2022 from LostAttractor/next
AkinoKaede 64452cf
ss2022: complete P0/P1 hardening and add protocol tests
967c12a
perf: optimize memory allocation in shadowsocks protocols
159974f
feat: implement performance optimizations for shadowsocks protocols
2f64a6b
perf(shadowsocks): integrate UDP cipher cache optimization with 5x+ p…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package ciphers | ||
|
|
||
| import ( | ||
| "crypto/aes" | ||
| "crypto/cipher" | ||
| "encoding/base64" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| type CipherConf2022 struct { | ||
| KeyLen int | ||
| SaltLen int | ||
| NonceLen int | ||
| TagLen int | ||
| NewCipher func(key []byte) (cipher.AEAD, error) | ||
| NewBlockCipher func(key []byte) (cipher.Block, error) | ||
| } | ||
|
|
||
| const ( | ||
| // Timestamp tolerance | ||
| TimestampTolerance = 30 * time.Second | ||
|
|
||
| // Salt storage duration | ||
| SaltStorageDuration = 60 * time.Second | ||
| ) | ||
|
|
||
| var ( | ||
| Aead2022CiphersConf = map[string]*CipherConf2022{ | ||
| "2022-blake3-aes-256-gcm": {KeyLen: 32, SaltLen: 32, NonceLen: 12, TagLen: 16, NewCipher: NewGcm, NewBlockCipher: aes.NewCipher}, | ||
| "2022-blake3-aes-128-gcm": {KeyLen: 16, SaltLen: 16, NonceLen: 12, TagLen: 16, NewCipher: NewGcm, NewBlockCipher: aes.NewCipher}, | ||
| } | ||
| ) | ||
|
|
||
| // ValidateBase64PSK validates that the PSK is a valid base64 string with correct length | ||
| func ValidateBase64PSK(pskBase64 string, expectedKeyLen int) ([]byte, error) { | ||
| if pskBase64 == "" { | ||
| return nil, fmt.Errorf("PSK cannot be empty for SIP022 methods") | ||
| } | ||
|
|
||
| psk, err := base64.StdEncoding.DecodeString(pskBase64) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("PSK must be valid base64 for SIP022 methods: %w", err) | ||
| } | ||
|
|
||
| if len(psk) != expectedKeyLen { | ||
| return nil, fmt.Errorf("PSK length must be %d bytes for this method, got %d", expectedKeyLen, len(psk)) | ||
| } | ||
|
|
||
| return psk, nil | ||
| } | ||
|
|
||
| // SlidingWindowFilter implements a sliding window filter for packet ID replay protection | ||
| type SlidingWindowFilter struct { | ||
| window []uint64 | ||
| windowSize int | ||
| latest uint64 | ||
| mutex sync.RWMutex | ||
| } | ||
|
|
||
| // NewSlidingWindowFilter creates a new sliding window filter | ||
| func NewSlidingWindowFilter(windowSize int) *SlidingWindowFilter { | ||
| return &SlidingWindowFilter{ | ||
| window: make([]uint64, windowSize), | ||
| windowSize: windowSize, | ||
| } | ||
| } | ||
|
|
||
| // CheckAndUpdate checks if the packet ID is valid and updates the window | ||
| func (f *SlidingWindowFilter) CheckAndUpdate(packetID uint64) bool { | ||
| f.mutex.Lock() | ||
| defer f.mutex.Unlock() | ||
|
|
||
| // Packet ID too old | ||
| if packetID+uint64(f.windowSize) <= f.latest { | ||
| return false | ||
| } | ||
|
|
||
| // Packet ID in the future, update latest | ||
| if packetID > f.latest { | ||
| // Shift window | ||
| shift := packetID - f.latest | ||
| if shift >= uint64(f.windowSize) { | ||
| // Clear entire window | ||
| for i := range f.window { | ||
| f.window[i] = 0 | ||
| } | ||
| } else { | ||
| // Shift window by 'shift' positions | ||
| for i := 0; i < len(f.window)-int(shift); i++ { | ||
| f.window[i] = f.window[i+int(shift)] | ||
| } | ||
| for i := len(f.window) - int(shift); i < len(f.window); i++ { | ||
| f.window[i] = 0 | ||
| } | ||
| } | ||
| f.latest = packetID | ||
| return true | ||
| } | ||
|
|
||
| // Packet ID in the window | ||
| index := int(f.latest - packetID) | ||
| if index >= f.windowSize { | ||
| return false | ||
| } | ||
|
|
||
| wordIndex := index / 64 | ||
| bitIndex := index % 64 | ||
| mask := uint64(1) << bitIndex | ||
|
|
||
| // Check if already seen | ||
| if f.window[wordIndex]&mask != 0 { | ||
| return false | ||
| } | ||
|
|
||
| // Mark as seen | ||
| f.window[wordIndex] |= mask | ||
| return true | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential bug in sliding window implementation: on line 65, the window is allocated with size 'windowSize', but the implementation appears to treat the window as an array of 64-bit words where each bit represents a packet ID. Line 108 calculates 'wordIndex := index / 64', suggesting the window should have size 'windowSize / 64' (or '(windowSize + 63) / 64' to round up). The current implementation may cause array index out of bounds when index >= 64 * len(f.window).