Skip to content

Commit

Permalink
add Salsa20 and XSalsa20
Browse files Browse the repository at this point in the history
  • Loading branch information
bstnbuck committed Apr 20, 2024
1 parent 78ed01d commit ea31dbb
Show file tree
Hide file tree
Showing 8 changed files with 660 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
cd v
make
- name: test algorithms
run: ./v/v test md4/ tea/ xtea/ _cipher/
run: ./v/v test md4/ tea/ xtea/ _cipher/ salsa20/
2 changes: 1 addition & 1 deletion .github/workflows/win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
cd v
.\make.bat -tcc
- name: test algorithms
run: ./v/v test md4/ tea/ xtea/ _cipher/
run: ./v/v test md4/ tea/ xtea/ _cipher/ salsa20/
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> **Attention!**<br>
**V-crypto** has no connection to the official V community and is not maintained by it.<br>
**&rarr; It is not recommended to use the algorithms implemented here productively.** As a non-cryptographer, I cannot fully validate the security.
**&rarr; It is not recommended to use the algorithms implemented here productively until the status is *implemented*.** As a non-cryptographer, I cannot fully validate the security.

>**Contributions welcome!**
Expand Down Expand Up @@ -80,13 +80,13 @@ The V wrapper libsodium [[Git](https://github.com/vlang/libsodium)] has some of
| Kyber(512,1024) | key encapsulation mechanism, post-quanten crypto | low | :x: |
| MD4 | legacy hash-algorithm | low | experimental :yellow_circle: [[Git](https://github.com/bstnbuck/V-crypto/tree/main/md4)] |
| **RIPEMD160** | legacy hash-algorithm | moderate | :x: |
| **Salsa20** | symmetric stream cipher | high | :x: |
| **(X)Salsa20** | symmetric stream cipher | high | experimental :yellow_circle: [[Git](https://github.com/bstnbuck/V-crypto/tree/main/salsa20)] |
| **scrypt** | hash-algorithm / key derivation function | high | :x: |
| TEA, XTEA | legacy block cipher | low | experimental :yellow_circle: [[Git](https://github.com/bstnbuck/V-crypto/tree/main/tea)] [[Git](https://github.com/bstnbuck/V-crypto/tree/main/xtea)]|
| **Twofisch** | symmetric block cipher | moderate | :x: |
| **yescrypt** | hash-algorithm / key derivation function | high | :x: |

> Last Update: 18-04-2024
> Last Update: 20-04-2024
---
## v_crypto
### Installation
Expand All @@ -95,6 +95,8 @@ v install https://github.com/bstnbuck/V-crypto
```

### Usage
In general, the functionality is easy to understand based on the tests of the respective algorithm. For larger algorithms, a README file with the most important functions follows.

```v
import v_crypto.md4
Expand All @@ -111,3 +113,5 @@ fn main(){
_, _ := d.checksum('Hi from V_crypto. This is an example of a long long line.'.bytes())
}
```

> Please report security related issues to: bstnbuck (at) proton (dot) me
54 changes: 54 additions & 0 deletions salsa20/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## Salsa20/XSalsa20 -- stream cipher encryption

Salsa20 is a stream cipher based on a symmetric **256-Bit key**, a **64-bit** (or **192-bit for XSalsa20**) **nonce** and a **64-bit counter**. the plaintext can have a variable size.
The parameters key, nonce and counter can be adjusted at runtime (using **rekey()** and/or **set_counter()**).

### Examples -- Salsa20
```v
import v_crypto.salsa20
import encoding.hex
fn main(){
key := hex.decode('0053A6F94C9FF24598EB3E91E4378ADD3083D6297CCF2275C81B6EC11467BA0D')!
nonce := hex.decode('0D74DB42A91077DE')!
plain := hex.decode('766572792073686f7274206d7367')! // very short msg
// short way to encrypt the given string `plain` using key and nonce
println("`very short msg` encrypted with Salsa20 is: " + salsa20.encrypt(key, nonce, plain)!) // >>> 839fa746598ab737b6da80bd9efd <<<
ciphertext := 'some-ciphertext'.bytes()
// decrypt a string `ciphertext` with a given key, nonce, a different counter and number of Salsa20 rounds
mut dec := []u8{len: plain4.len}
mut c := new_cipher(key, nonce)!
// if only the counter should be different
// c.set_counter(45)
c.rekey(key, nonce, 45, 8)! // key, nonce, counter, rounds
c.xor_key_stream(mut dec, ciphertext)
println("The decrypted ciphertext with initial counter 48 and 8 rounds is:" +dec.hex())
}
```

### Examples -- XSalsa20
```v
import v_crypto.salsa20
import encoding.hex
fn main(){
key := hex.decode('0053A6F94C9FF24598EB3E91E4378ADD3083D6297CCF2275C81B6EC11467BA0D')!
nonce := hex.decode('404142434445464748494a4b4c4d4e4f5051525354555658')! // XSalsa20 has a larger Nonce
plain := hex.decode('766572792073686f7274206d7367')! // very short msg
// short way to encrypt the given string `plain` using key and nonce
println("`very short msg` encrypted with XSalsa20 is: " + salsa20.encrypt(key, nonce, plain)!) // >>> d0df8be036e7d95728040244ef2f <<<
ciphertext := 'some-ciphertext'.bytes()
// decrypt a string `ciphertext` with a given key, nonce, a different counter and number of Salsa20 rounds
mut dec := []u8{len: plain4.len}
mut c := new_cipher(key, nonce)!
c.rekey(key, nonce, 45, 8)! // key, nonce, counter, rounds
c.xor_key_stream(mut dec, ciphertext)
println("The decrypted ciphertext with initial counter 48 and 8 rounds is:" +dec.hex())
}
```
Loading

0 comments on commit ea31dbb

Please sign in to comment.