mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vlib: run vfmt over vlib files, so that v doc -m vlib/ can run without warnings
This commit is contained in:
@@ -1,14 +1,12 @@
|
||||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module cipher
|
||||
|
||||
// NOTE: Implement other versions (joe-c)
|
||||
|
||||
// xor_bytes xors the bytes in a and b. The destination should have enough
|
||||
// space, otherwise xor_bytes will panic. Returns the number of bytes xor'd.
|
||||
pub fn xor_bytes(mut dst []byte, a, b []byte) int {
|
||||
pub fn xor_bytes(mut dst []byte, a []byte, b []byte) int {
|
||||
mut n := a.len
|
||||
if b.len < n {
|
||||
n = b.len
|
||||
@@ -16,21 +14,19 @@ pub fn xor_bytes(mut dst []byte, a, b []byte) int {
|
||||
if n == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
safe_xor_bytes(mut dst, a, b, n)
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
// n needs to be smaller or equal than the length of a and b.
|
||||
pub fn safe_xor_bytes(mut dst []byte, a, b []byte, n int) {
|
||||
for i in 0..n {
|
||||
pub fn safe_xor_bytes(mut dst []byte, a []byte, b []byte, n int) {
|
||||
for i in 0 .. n {
|
||||
dst[i] = a[i] ^ b[i]
|
||||
}
|
||||
}
|
||||
|
||||
// fast_xor_words XORs multiples of 4 or 8 bytes (depending on architecture.)
|
||||
// The slice arguments a and b are assumed to be of equal length.
|
||||
pub fn xor_words(mut dst []byte, a, b []byte) {
|
||||
pub fn xor_words(mut dst []byte, a []byte, b []byte) {
|
||||
safe_xor_bytes(mut dst, a, b, b.len)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ const (
|
||||
)
|
||||
|
||||
// Returns an HMAC byte array, depending on the hash algorithm used
|
||||
pub fn new(key, data []byte, hash_func fn (bytes []byte) []byte, blocksize int) []byte {
|
||||
pub fn new(key []byte, data []byte, hash_func fn (bytes []byte) []byte, blocksize int) []byte {
|
||||
mut b_key := []byte{}
|
||||
if key.len <= blocksize {
|
||||
b_key = key.clone() // TODO: remove .clone() once https://github.com/vlang/v/issues/6604 gets fixed
|
||||
@@ -39,6 +39,6 @@ pub fn new(key, data []byte, hash_func fn (bytes []byte) []byte, blocksize int)
|
||||
// equal compares 2 MACs for equality, without leaking timing info
|
||||
// NB: if the lengths of the 2 MACs are different, probably a completely different
|
||||
// hash function was used to generate them => no useful timing information.
|
||||
pub fn equal(mac1, mac2 []byte) bool {
|
||||
pub fn equal(mac1 []byte, mac2 []byte) bool {
|
||||
return subtle.constant_time_compare(mac1, mac2) == 1
|
||||
}
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Package subtle implements functions that are often useful in cryptographic
|
||||
// code but require careful thought to use correctly.
|
||||
|
||||
module subtle
|
||||
|
||||
// NOTE: require unsafe in future
|
||||
|
||||
// any_overlap reports whether x and y share memory at any (not necessarily
|
||||
// corresponding) index. The memory beyond the slice length is ignored.
|
||||
pub fn any_overlap(x, y []byte) bool {
|
||||
pub fn any_overlap(x []byte, y []byte) bool {
|
||||
// NOTE: Remember to come back to this (joe-c)
|
||||
return x.len > 0 && y.len > 0 &&
|
||||
// &x.data[0] <= &y.data[y.len-1] &&
|
||||
// &y.data[0] <= &x.data[x.len-1]
|
||||
unsafe { &x[0] <= &y[y.len-1] &&
|
||||
&y[0] <= &x[x.len-1] }
|
||||
return x.len > 0 && y.len > 0 && // &x.data[0] <= &y.data[y.len-1] &&
|
||||
// &y.data[0] <= &x.data[x.len-1]
|
||||
unsafe {&x[0] <= &y[y.len - 1] && &y[0] <= &x[x.len - 1]}
|
||||
}
|
||||
|
||||
// inexact_overlap reports whether x and y share memory at any non-corresponding
|
||||
@@ -26,8 +21,8 @@ pub fn any_overlap(x, y []byte) bool {
|
||||
//
|
||||
// inexact_overlap can be used to implement the requirements of the crypto/cipher
|
||||
// AEAD, Block, BlockMode and Stream interfaces.
|
||||
pub fn inexact_overlap(x, y []byte) bool {
|
||||
if x.len == 0 || y.len == 0 || unsafe { &x[0] == &y[0] } {
|
||||
pub fn inexact_overlap(x []byte, y []byte) bool {
|
||||
if x.len == 0 || y.len == 0 || unsafe {&x[0] == &y[0]} {
|
||||
return false
|
||||
}
|
||||
return any_overlap(x, y)
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
module subtle
|
||||
|
||||
// constant_time_byte_eq returns 1 when x == y.
|
||||
pub fn constant_time_byte_eq(x, y byte) int {
|
||||
pub fn constant_time_byte_eq(x byte, y byte) int {
|
||||
return int((u32(x ^ y) - 1) >> 31)
|
||||
}
|
||||
|
||||
// constant_time_eq returns 1 when x == y.
|
||||
pub fn constant_time_eq(x, y int) int {
|
||||
pub fn constant_time_eq(x int, y int) int {
|
||||
return int((u64(u32(x ^ y)) - 1) >> 63)
|
||||
}
|
||||
|
||||
// constant_time_select returns x when v == 1, and y when v == 0.
|
||||
// it is undefined when v is any other value
|
||||
pub fn constant_time_select(v, x, y int) int {
|
||||
pub fn constant_time_select(v int, x int, y int) int {
|
||||
return (~(v - 1) & x) | ((v - 1) & y)
|
||||
}
|
||||
|
||||
// constant_time_compare returns 1 when x and y have equal contents.
|
||||
// The runtime of this function is proportional of the length of x and y.
|
||||
// It is *NOT* dependent on their content.
|
||||
pub fn constant_time_compare(x, y []byte) int {
|
||||
pub fn constant_time_compare(x []byte, y []byte) int {
|
||||
if x.len != y.len {
|
||||
return 0
|
||||
}
|
||||
@@ -46,7 +46,7 @@ pub fn constant_time_copy(v int, mut x []byte, y []byte) {
|
||||
|
||||
// constant_time_less_or_eq returns 1 if x <= y, and 0 otherwise.
|
||||
// it is undefined when x or y are negative, or > (2^32 - 1)
|
||||
pub fn constant_time_less_or_eq(x, y int) int {
|
||||
pub fn constant_time_less_or_eq(x int, y int) int {
|
||||
x32 := int(x)
|
||||
y32 := int(y)
|
||||
return int(((x32 - y32 - 1) >> 31) & 1)
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Package rc4 implements RC4 encryption, as defined in Bruce Schneier's
|
||||
// Applied Cryptography.
|
||||
//
|
||||
// RC4 is cryptographically broken and should not be used for secure
|
||||
// applications.
|
||||
|
||||
// Based off: https://github.com/golang/go/blob/master/src/crypto/rc4
|
||||
// Last commit: https://github.com/golang/go/commit/b35dacaac57b039205d9b07ea24098e2c3fcb12e
|
||||
|
||||
module rc4
|
||||
|
||||
import crypto.internal.subtle
|
||||
@@ -30,14 +27,14 @@ pub fn new_cipher(key []byte) ?Cipher {
|
||||
return error('crypto.rc4: invalid key size ' + key.len.str())
|
||||
}
|
||||
mut c := Cipher{
|
||||
s: []u32{len:(256)}
|
||||
s: []u32{len: (256)}
|
||||
}
|
||||
for i in 0..256 {
|
||||
for i in 0 .. 256 {
|
||||
c.s[i] = u32(i)
|
||||
}
|
||||
mut j := byte(0)
|
||||
for i in 0..256 {
|
||||
j += byte(c.s[i]) + key[i%key.len]
|
||||
for i in 0 .. 256 {
|
||||
j += byte(c.s[i]) + key[i % key.len]
|
||||
tmp := c.s[i]
|
||||
c.s[i] = c.s[j]
|
||||
c.s[j] = tmp
|
||||
@@ -59,7 +56,7 @@ pub fn (mut c Cipher) reset() {
|
||||
|
||||
// xor_key_stream sets dst to the result of XORing src with the key stream.
|
||||
// Dst and src must overlap entirely or not at all.
|
||||
pub fn (mut c Cipher) xor_key_stream(mut dst, src []byte) {
|
||||
pub fn (mut c Cipher) xor_key_stream(mut dst []byte, mut src []byte) {
|
||||
if src.len == 0 {
|
||||
return
|
||||
}
|
||||
@@ -75,7 +72,7 @@ pub fn (mut c Cipher) xor_key_stream(mut dst, src []byte) {
|
||||
y := c.s[j]
|
||||
c.s[i] = y
|
||||
c.s[j] = x
|
||||
dst[k] = v ^ byte(c.s[byte(x+y)])
|
||||
dst[k] = v ^ byte(c.s[byte(x + y)])
|
||||
}
|
||||
c.i = i
|
||||
c.j = j
|
||||
|
||||
Reference in New Issue
Block a user