-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Also adding some key/offset generation functions
- Loading branch information
Doug Saylor
committed
Jul 20, 2023
1 parent
2e6dccd
commit 64839d8
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,7 @@ | ||
Copyright 2023 Joseph D. Saylor | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains 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,16 @@ | ||
# Go Cryptx | ||
|
||
This repo has a few helpful crypto utilities that I've used or plan to use. | ||
|
||
--- | ||
## Note | ||
Use at your own risk! | ||
While I've done my best to ensure that these functions are working as intended, I don't recommend using any of this for anything that could risk life, limb, property, or any serious material harm without extensive security review. | ||
|
||
If you suspect that a security issue exists, please notify me by creating an issue here on GitHub, and I will address it as soon as possible. | ||
If you are a security professional, I can always use another set of eyes to verify the security and integrity of these utilities. | ||
|
||
When the time comes where I am no longer maintaining this repository, either by responding to or resolving issues, then I will mark it as archived to indicate that it should no longer be used in its current state. | ||
If this is the case - and even before then - feel free to fork this repository and enhance as you see fit. | ||
|
||
--- |
This file contains 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,30 @@ | ||
/* | ||
Package xor provides some light-weight screening of lower sensitivity data. | ||
Note that this is NOT encryption, since it is easily reversible. | ||
This falls squarely under the obfuscation category. | ||
As such, it is NOT recommended for security critical use. | ||
That being said, it's useful for preventing passive observation of plain text information since it generally requires knowledge of the original key to correctly reverse the process. | ||
# How it works: | ||
An XOR key (with optional offset) is provided to the functions in this package, which will be used to apply a bitwise XOR to every byte that passes through Reader or Writer. | ||
Once a key byte is used, the screen will progress to the next byte in the key. | ||
When the last byte is used, the first will be used again, operating like a ring buffer. | ||
Providing an offset will make the screen start at the given offset instead of the first byte. | ||
This is useful for adding a little randomness to the process in case it's likely that the same key can be used more than once. | ||
# Important note: | ||
The same key and offset parameters must be provided to accurately reverse the process. | ||
Failing to do so will likely result in garbled or partly de-obfuscated data. | ||
# General guidelines: | ||
- Longer keys are better, but have limited usefulness with a short payload. | ||
- Key length should ideally be a function of payload length. | ||
- For shorter payloads, using a shorter key with random offset is sufficient, but will still yield a predictable pattern. | ||
- Using securely generated keys with the OS entropy pool (like with GenKey or GenKeyAndOffset) are better. | ||
- Using a random offset is recommended, but not required. | ||
*/ | ||
package xor |
This file contains 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,34 @@ | ||
package xor | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/binary" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// GenKey will generate an XOR key with the given length. | ||
func GenKey(length int) ([]byte, error) { | ||
if length == 0 { | ||
return nil, errors.New("asked to generate a 0-length key") | ||
} | ||
buf := make([]byte, length) | ||
n, err := rand.Read(buf) | ||
if n < length { | ||
return nil, fmt.Errorf("failed to read requested bytes: %v", err) | ||
} | ||
return buf, nil | ||
} | ||
|
||
func GenKeyAndOffset(length int) ([]byte, int, error) { | ||
key, err := GenKey(length) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
buf := make([]byte, 4) | ||
_, err = rand.Read(buf) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
return key, int(binary.BigEndian.Uint32(buf)) % length, nil | ||
} |
This file contains 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,29 @@ | ||
package xor | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestGenKeyAndOffset(t *testing.T) { | ||
key, offset, err := GenKeyAndOffset(32) | ||
assert.NoError(t, err) | ||
assert.Len(t, key, 32) | ||
assert.Less(t, offset, 32) | ||
} | ||
|
||
func TestGenKeyAndOffset_Neg(t *testing.T) { | ||
_, _, err := GenKeyAndOffset(0) | ||
assert.Error(t, err) | ||
|
||
orig := rand.Reader | ||
defer func() { | ||
rand.Reader = orig | ||
}() | ||
rand.Reader = bytes.NewBuffer(nil) | ||
|
||
_, _, err = GenKeyAndOffset(10) | ||
assert.Error(t, err) | ||
} |