Source File
chacha_generic.go
Belonging Package
golang.org/x/crypto/chacha20
package chacha20
import (
)
KeySize = 32
NonceSize = 12
NonceSizeX = 24
)
, _ = HChaCha20(, [0:16])
:= make([]byte, NonceSize)
copy([4:12], [16:24])
=
} else if len() != NonceSize {
return nil, errors.New("chacha20: wrong nonce size")
}
, = [:KeySize], [:NonceSize] // bounds check elimination hint
.key = [8]uint32{
binary.LittleEndian.Uint32([0:4]),
binary.LittleEndian.Uint32([4:8]),
binary.LittleEndian.Uint32([8:12]),
binary.LittleEndian.Uint32([12:16]),
binary.LittleEndian.Uint32([16:20]),
binary.LittleEndian.Uint32([20:24]),
binary.LittleEndian.Uint32([24:28]),
binary.LittleEndian.Uint32([28:32]),
}
.nonce = [3]uint32{
binary.LittleEndian.Uint32([0:4]),
binary.LittleEndian.Uint32([4:8]),
binary.LittleEndian.Uint32([8:12]),
}
return , nil
}
func (, , , uint32) (uint32, uint32, uint32, uint32) {
+=
^=
= bits.RotateLeft32(, 16)
+=
^=
= bits.RotateLeft32(, 12)
+=
^=
= bits.RotateLeft32(, 8)
+=
^=
= bits.RotateLeft32(, 7)
return , , ,
}
:= len() - len()%bufSize
if > 0 {
.xorKeyStreamBlocks([:], [:])
}
, = [:], [:]
if !.precompDone {
.p1, .p5, .p9, .p13 = quarterRound(, , , )
.p2, .p6, .p10, .p14 = quarterRound(, , , )
.p3, .p7, .p11, .p15 = quarterRound(, , , )
.precompDone = true
}
, , , := quarterRound(, , , .counter)
, , , := quarterRound(, .p5, .p10, .p15)
, , , := quarterRound(.p1, .p6, .p11, )
, , , := quarterRound(.p2, .p7, , .p13)
, , , := quarterRound(.p3, , .p9, .p14)
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
}
addXor([0:4], [0:4], , )
addXor([4:8], [4:8], , )
addXor([8:12], [8:12], , )
addXor([12:16], [12:16], , )
addXor([16:20], [16:20], , )
addXor([20:24], [20:24], , )
addXor([24:28], [24:28], , )
addXor([28:32], [28:32], , )
addXor([32:36], [32:36], , )
addXor([36:40], [36:40], , )
addXor([40:44], [40:44], , )
addXor([44:48], [44:48], , )
addXor([48:52], [48:52], , .counter)
addXor([52:56], [52:56], , )
addXor([56:60], [56:60], , )
addXor([60:64], [60:64], , )
.counter += 1
, = [blockSize:], [blockSize:]
}
}
:= make([]byte, 32)
return hChaCha20(, , )
}
func (, , []byte) ([]byte, error) {
if len() != KeySize {
return nil, errors.New("chacha20: wrong HChaCha20 key size")
}
if len() != 16 {
return nil, errors.New("chacha20: wrong HChaCha20 nonce size")
}
, , , := j0, j1, j2, j3
:= binary.LittleEndian.Uint32([0:4])
:= binary.LittleEndian.Uint32([4:8])
:= binary.LittleEndian.Uint32([8:12])
:= binary.LittleEndian.Uint32([12:16])
:= binary.LittleEndian.Uint32([16:20])
:= binary.LittleEndian.Uint32([20:24])
:= binary.LittleEndian.Uint32([24:28])
:= binary.LittleEndian.Uint32([28:32])
:= binary.LittleEndian.Uint32([0:4])
:= binary.LittleEndian.Uint32([4:8])
:= binary.LittleEndian.Uint32([8:12])
:= binary.LittleEndian.Uint32([12:16])
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
, , , = quarterRound(, , , )
}
_ = [31] // bounds check elimination hint
binary.LittleEndian.PutUint32([0:4], )
binary.LittleEndian.PutUint32([4:8], )
binary.LittleEndian.PutUint32([8:12], )
binary.LittleEndian.PutUint32([12:16], )
binary.LittleEndian.PutUint32([16:20], )
binary.LittleEndian.PutUint32([20:24], )
binary.LittleEndian.PutUint32([24:28], )
binary.LittleEndian.PutUint32([28:32], )
return , nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |