mirror of
https://github.com/emmansun/gmsm.git
synced 2025-10-14 07:10:45 +08:00
cipher: update comments
This commit is contained in:
parent
00a09d0208
commit
5b12daceea
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2025 Sun Yimin. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package cipher
|
package cipher
|
||||||
|
|
||||||
import "github.com/emmansun/gmsm/internal/byteorder"
|
import "github.com/emmansun/gmsm/internal/byteorder"
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2025 Sun Yimin. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package cipher
|
package cipher
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -19,12 +23,16 @@ type gxm struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewGXM creates a new GXM instance using the provided cipher stream and hash key.
|
// NewGXM creates a new GXM instance using the provided cipher stream and hash key.
|
||||||
// It uses the default tag size of 16 bytes.
|
// It uses the default tag size of 16 bytes.
|
||||||
|
//
|
||||||
|
// Due to the nature of GXM, the same stream cipher instance should not be reused.
|
||||||
func NewGXM(stream cipher.Stream, hkey []byte) (*gxm, error) {
|
func NewGXM(stream cipher.Stream, hkey []byte) (*gxm, error) {
|
||||||
return NewGXMWithTagSize(stream, hkey, 16)
|
return NewGXMWithTagSize(stream, hkey, 16)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGXMWithTagSize creates a new instance of GXM (Galois XOR Mode) with a specified tag size.
|
// NewGXMWithTagSize creates a new instance of GXM (Galois XOR Mode) with a specified tag size.
|
||||||
|
//
|
||||||
|
// Due to the nature of GXM, the same stream cipher instance should not be reused.
|
||||||
func NewGXMWithTagSize(stream cipher.Stream, hkey []byte, tagSize int) (*gxm, error) {
|
func NewGXMWithTagSize(stream cipher.Stream, hkey []byte, tagSize int) (*gxm, error) {
|
||||||
if len(hkey) != ghashBlockSize {
|
if len(hkey) != ghashBlockSize {
|
||||||
return nil, errors.New("cipher: invalid hash key length")
|
return nil, errors.New("cipher: invalid hash key length")
|
||||||
@ -65,8 +73,7 @@ func (g *gxm) Overhead() int {
|
|||||||
|
|
||||||
// Seal encrypts and authenticates plaintext, authenticates the
|
// Seal encrypts and authenticates plaintext, authenticates the
|
||||||
// additional data and appends the result to dst, returning the updated
|
// additional data and appends the result to dst, returning the updated
|
||||||
// slice. The nonce must be NonceSize() bytes long and unique for all
|
// slice.
|
||||||
// time, for a given key.
|
|
||||||
//
|
//
|
||||||
// To reuse plaintext's storage for the encrypted output, use plaintext[:0]
|
// To reuse plaintext's storage for the encrypted output, use plaintext[:0]
|
||||||
// as dst. Otherwise, the remaining capacity of dst must not overlap plaintext.
|
// as dst. Otherwise, the remaining capacity of dst must not overlap plaintext.
|
||||||
@ -87,8 +94,7 @@ func (g *gxm) Seal(dst, plaintext, additionalData []byte) []byte {
|
|||||||
|
|
||||||
// Open decrypts and authenticates ciphertext, authenticates the
|
// Open decrypts and authenticates ciphertext, authenticates the
|
||||||
// additional data and, if successful, appends the resulting plaintext
|
// additional data and, if successful, appends the resulting plaintext
|
||||||
// to dst, returning the updated slice. The nonce must be NonceSize()
|
// to dst, returning the updated slice. The additional data must match the
|
||||||
// bytes long and both it and the additional data must match the
|
|
||||||
// value passed to Seal.
|
// value passed to Seal.
|
||||||
//
|
//
|
||||||
// To reuse ciphertext's storage for the decrypted output, use ciphertext[:0]
|
// To reuse ciphertext's storage for the decrypted output, use ciphertext[:0]
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2024 Sun Yimin. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package cipher
|
package cipher
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2025 Sun Yimin. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package cipher
|
package cipher
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -70,13 +74,12 @@ func (g *mur) Overhead() int {
|
|||||||
|
|
||||||
// Seal encrypts and authenticates plaintext, authenticates the
|
// Seal encrypts and authenticates plaintext, authenticates the
|
||||||
// additional data and appends the result to dst, returning the updated
|
// additional data and appends the result to dst, returning the updated
|
||||||
// slice. The nonce must be NonceSize() bytes long and unique for all
|
// slice.
|
||||||
// time, for a given key.
|
|
||||||
//
|
//
|
||||||
// To reuse plaintext's storage for the encrypted output, use plaintext[:0]
|
// To reuse plaintext's storage for the encrypted output, use plaintext[:0]
|
||||||
// as dst. Otherwise, the remaining capacity of dst must not overlap plaintext.
|
// as dst. Otherwise, the remaining capacity of dst must not overlap plaintext.
|
||||||
// dst and additionalData may not overlap.
|
// dst and additionalData may not overlap.
|
||||||
func (g *mur) Seal(iv, key1, key2, dst, plaintext, additionalData []byte) ([]byte, error) {
|
func (g *mur) Seal(iv, dataKey, tagKey, dst, plaintext, additionalData []byte) ([]byte, error) {
|
||||||
ret, out := alias.SliceForAppend(dst, len(plaintext)+g.tagSize)
|
ret, out := alias.SliceForAppend(dst, len(plaintext)+g.tagSize)
|
||||||
if alias.InexactOverlap(out, plaintext) {
|
if alias.InexactOverlap(out, plaintext) {
|
||||||
panic("cipher: invalid buffer overlap")
|
panic("cipher: invalid buffer overlap")
|
||||||
@ -95,7 +98,7 @@ func (g *mur) Seal(iv, key1, key2, dst, plaintext, additionalData []byte) ([]byt
|
|||||||
copy(tmpIV[:], iv)
|
copy(tmpIV[:], iv)
|
||||||
g.murAuth(tmpIV[:], plaintext, additionalData)
|
g.murAuth(tmpIV[:], plaintext, additionalData)
|
||||||
subtle.XORBytes(tmpIV[:], tmpIV[:], iv)
|
subtle.XORBytes(tmpIV[:], tmpIV[:], iv)
|
||||||
tagStream, err := g.streamCipherCreator(key2, tmpIV[:ivLen])
|
tagStream, err := g.streamCipherCreator(tagKey, tmpIV[:ivLen])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -103,7 +106,7 @@ func (g *mur) Seal(iv, key1, key2, dst, plaintext, additionalData []byte) ([]byt
|
|||||||
|
|
||||||
clear(tmpIV[:])
|
clear(tmpIV[:])
|
||||||
subtle.XORBytes(tmpIV[:], iv, tag[:])
|
subtle.XORBytes(tmpIV[:], iv, tag[:])
|
||||||
dataStream, err := g.streamCipherCreator(key1, tmpIV[:ivLen])
|
dataStream, err := g.streamCipherCreator(dataKey, tmpIV[:ivLen])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -114,9 +117,8 @@ func (g *mur) Seal(iv, key1, key2, dst, plaintext, additionalData []byte) ([]byt
|
|||||||
|
|
||||||
// Open decrypts and authenticates ciphertext, authenticates the
|
// Open decrypts and authenticates ciphertext, authenticates the
|
||||||
// additional data and, if successful, appends the resulting plaintext
|
// additional data and, if successful, appends the resulting plaintext
|
||||||
// to dst, returning the updated slice. The nonce must be NonceSize()
|
// to dst, returning the updated slice. The iv, dataKey, tagKey
|
||||||
// bytes long and both it and the additional data must match the
|
// and the additional data must match the value passed to Seal.
|
||||||
// value passed to Seal.
|
|
||||||
//
|
//
|
||||||
// To reuse ciphertext's storage for the decrypted output, use ciphertext[:0]
|
// To reuse ciphertext's storage for the decrypted output, use ciphertext[:0]
|
||||||
// as dst. Otherwise, the remaining capacity of dst must not overlap ciphertext.
|
// as dst. Otherwise, the remaining capacity of dst must not overlap ciphertext.
|
||||||
@ -124,7 +126,7 @@ func (g *mur) Seal(iv, key1, key2, dst, plaintext, additionalData []byte) ([]byt
|
|||||||
//
|
//
|
||||||
// Even if the function fails, the contents of dst, up to its capacity,
|
// Even if the function fails, the contents of dst, up to its capacity,
|
||||||
// may be overwritten.
|
// may be overwritten.
|
||||||
func (g *mur) Open(iv, key1, key2, dst, ciphertext, additionalData []byte) ([]byte, error) {
|
func (g *mur) Open(iv, dataKey, tagKey, dst, ciphertext, additionalData []byte) ([]byte, error) {
|
||||||
if len(ciphertext) < g.tagSize {
|
if len(ciphertext) < g.tagSize {
|
||||||
return nil, errOpen
|
return nil, errOpen
|
||||||
}
|
}
|
||||||
@ -148,7 +150,7 @@ func (g *mur) Open(iv, key1, key2, dst, ciphertext, additionalData []byte) ([]by
|
|||||||
}
|
}
|
||||||
copy(tmpIV[:], tag)
|
copy(tmpIV[:], tag)
|
||||||
subtle.XORBytes(tmpIV[:], iv, tmpIV[:])
|
subtle.XORBytes(tmpIV[:], iv, tmpIV[:])
|
||||||
dataStream, err := g.streamCipherCreator(key1, tmpIV[:ivLen])
|
dataStream, err := g.streamCipherCreator(dataKey, tmpIV[:ivLen])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -157,7 +159,7 @@ func (g *mur) Open(iv, key1, key2, dst, ciphertext, additionalData []byte) ([]by
|
|||||||
clear(tmpIV[:])
|
clear(tmpIV[:])
|
||||||
g.murAuth(tmpIV[:], out, additionalData)
|
g.murAuth(tmpIV[:], out, additionalData)
|
||||||
subtle.XORBytes(tmpIV[:], tmpIV[:], iv)
|
subtle.XORBytes(tmpIV[:], tmpIV[:], iv)
|
||||||
tagStream, err := g.streamCipherCreator(key2, tmpIV[:ivLen])
|
tagStream, err := g.streamCipherCreator(tagKey, tmpIV[:ivLen])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2025 Sun Yimin. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package cipher_test
|
package cipher_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2025 Sun Yimin. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package cipher_test
|
package cipher_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user