Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +3 -0
- platform/dbops/binaries/go/go/src/crypto/aes/cipher_generic.go +26 -0
- platform/dbops/binaries/go/go/src/crypto/aes/cipher_s390x.go +96 -0
- platform/dbops/binaries/go/go/src/crypto/aes/const.go +365 -0
- platform/dbops/binaries/go/go/src/crypto/aes/ctr_s390x.go +84 -0
- platform/dbops/binaries/go/go/src/crypto/aes/gcm_amd64.s +1286 -0
- platform/dbops/binaries/go/go/src/crypto/aes/gcm_arm64.s +1021 -0
- platform/dbops/binaries/go/go/src/crypto/aes/gcm_ppc64x.go +243 -0
- platform/dbops/binaries/go/go/src/crypto/aes/gcm_ppc64x.s +1103 -0
- platform/dbops/binaries/go/go/src/crypto/aes/gcm_s390x.go +371 -0
- platform/dbops/binaries/go/go/src/crypto/aes/modes.go +37 -0
- platform/dbops/binaries/go/go/src/crypto/aes/modes_test.go +112 -0
- platform/dbops/binaries/go/go/src/crypto/boring/boring.go +21 -0
- platform/dbops/binaries/go/go/src/crypto/boring/boring_test.go +22 -0
- platform/dbops/binaries/go/go/src/crypto/boring/notboring_test.go +13 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/benchmark_test.go +137 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/cbc.go +189 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/cbc_aes_test.go +104 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/cfb.go +83 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/cfb_test.go +113 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/cipher.go +61 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/cipher_test.go +90 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/common_test.go +28 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/ctr.go +95 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/ctr_aes_test.go +102 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/ctr_test.go +55 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/example_test.go +363 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/export_test.go +9 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/fuzz_test.go +103 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/gcm.go +427 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/gcm_test.go +656 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/io.go +53 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/ofb.go +77 -0
- platform/dbops/binaries/go/go/src/crypto/cipher/ofb_test.go +102 -0
- platform/dbops/binaries/go/go/src/crypto/des/block.go +249 -0
- platform/dbops/binaries/go/go/src/crypto/des/cipher.go +155 -0
- platform/dbops/binaries/go/go/src/crypto/des/const.go +142 -0
- platform/dbops/binaries/go/go/src/crypto/des/des_test.go +1563 -0
- platform/dbops/binaries/go/go/src/crypto/des/example_test.go +25 -0
- platform/dbops/binaries/go/go/src/crypto/des/internal_test.go +29 -0
- platform/dbops/binaries/go/go/src/crypto/dsa/dsa.go +309 -0
- platform/dbops/binaries/go/go/src/crypto/dsa/dsa_test.go +143 -0
- platform/dbops/binaries/go/go/src/crypto/ecdh/ecdh.go +188 -0
- platform/dbops/binaries/go/go/src/crypto/ecdh/ecdh_test.go +525 -0
- platform/dbops/binaries/go/go/src/crypto/ecdh/nist.go +275 -0
- platform/dbops/binaries/go/go/src/crypto/ecdh/x25519.go +136 -0
- platform/dbops/binaries/go/go/src/crypto/ecdsa/boring.go +106 -0
- platform/dbops/binaries/go/go/src/crypto/ecdsa/ecdsa.go +672 -0
- platform/dbops/binaries/go/go/src/crypto/ecdsa/ecdsa_legacy.go +188 -0
- platform/dbops/binaries/go/go/src/crypto/ecdsa/ecdsa_noasm.go +17 -0
.gitattributes
CHANGED
|
@@ -3153,3 +3153,6 @@ platform/dbops/binaries/go/go/pkg/tool/linux_amd64/test2json filter=lfs diff=lfs
|
|
| 3153 |
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/trace filter=lfs diff=lfs merge=lfs -text
|
| 3154 |
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/vet filter=lfs diff=lfs merge=lfs -text
|
| 3155 |
platform/dbops/binaries/go/go/src/cmd/compile/default.pgo filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 3153 |
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/trace filter=lfs diff=lfs merge=lfs -text
|
| 3154 |
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/vet filter=lfs diff=lfs merge=lfs -text
|
| 3155 |
platform/dbops/binaries/go/go/src/cmd/compile/default.pgo filter=lfs diff=lfs merge=lfs -text
|
| 3156 |
+
platform/dbops/binaries/go/go/src/crypto/internal/boring/syso/goboringcrypto_linux_amd64.syso filter=lfs diff=lfs merge=lfs -text
|
| 3157 |
+
platform/dbops/binaries/go/go/src/crypto/internal/boring/syso/goboringcrypto_linux_arm64.syso filter=lfs diff=lfs merge=lfs -text
|
| 3158 |
+
platform/dbops/binaries/go/go/src/debug/pe/testdata/gcc-amd64-mingw-exec filter=lfs diff=lfs merge=lfs -text
|
platform/dbops/binaries/go/go/src/crypto/aes/cipher_generic.go
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2012 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
//go:build !amd64 && !s390x && !ppc64 && !ppc64le && !arm64
|
| 6 |
+
|
| 7 |
+
package aes
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"crypto/cipher"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
// newCipher calls the newCipherGeneric function
|
| 14 |
+
// directly. Platforms with hardware accelerated
|
| 15 |
+
// implementations of AES should implement their
|
| 16 |
+
// own version of newCipher (which may then call
|
| 17 |
+
// newCipherGeneric if needed).
|
| 18 |
+
func newCipher(key []byte) (cipher.Block, error) {
|
| 19 |
+
return newCipherGeneric(key)
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
// expandKey is used by BenchmarkExpand and should
|
| 23 |
+
// call an assembly implementation if one is available.
|
| 24 |
+
func expandKey(key []byte, enc, dec []uint32) {
|
| 25 |
+
expandKeyGo(key, enc, dec)
|
| 26 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/aes/cipher_s390x.go
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2016 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package aes
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/cipher"
|
| 9 |
+
"crypto/internal/alias"
|
| 10 |
+
"internal/cpu"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
type code int
|
| 14 |
+
|
| 15 |
+
// Function codes for the cipher message family of instructions.
|
| 16 |
+
const (
|
| 17 |
+
aes128 code = 18
|
| 18 |
+
aes192 = 19
|
| 19 |
+
aes256 = 20
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
type aesCipherAsm struct {
|
| 23 |
+
function code // code for cipher message instruction
|
| 24 |
+
key []byte // key (128, 192 or 256 bits)
|
| 25 |
+
storage [32]byte // array backing key slice
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// cryptBlocks invokes the cipher message (KM) instruction with
|
| 29 |
+
// the given function code. This is equivalent to AES in ECB
|
| 30 |
+
// mode. The length must be a multiple of BlockSize (16).
|
| 31 |
+
//
|
| 32 |
+
//go:noescape
|
| 33 |
+
func cryptBlocks(c code, key, dst, src *byte, length int)
|
| 34 |
+
|
| 35 |
+
func newCipher(key []byte) (cipher.Block, error) {
|
| 36 |
+
// The aesCipherAsm type implements the cbcEncAble, cbcDecAble,
|
| 37 |
+
// ctrAble and gcmAble interfaces. We therefore need to check
|
| 38 |
+
// for all the features required to implement these modes.
|
| 39 |
+
// Keep in sync with crypto/tls/common.go.
|
| 40 |
+
if !(cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR && (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)) {
|
| 41 |
+
return newCipherGeneric(key)
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
var function code
|
| 45 |
+
switch len(key) {
|
| 46 |
+
case 128 / 8:
|
| 47 |
+
function = aes128
|
| 48 |
+
case 192 / 8:
|
| 49 |
+
function = aes192
|
| 50 |
+
case 256 / 8:
|
| 51 |
+
function = aes256
|
| 52 |
+
default:
|
| 53 |
+
return nil, KeySizeError(len(key))
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
var c aesCipherAsm
|
| 57 |
+
c.function = function
|
| 58 |
+
c.key = c.storage[:len(key)]
|
| 59 |
+
copy(c.key, key)
|
| 60 |
+
return &c, nil
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
func (c *aesCipherAsm) BlockSize() int { return BlockSize }
|
| 64 |
+
|
| 65 |
+
func (c *aesCipherAsm) Encrypt(dst, src []byte) {
|
| 66 |
+
if len(src) < BlockSize {
|
| 67 |
+
panic("crypto/aes: input not full block")
|
| 68 |
+
}
|
| 69 |
+
if len(dst) < BlockSize {
|
| 70 |
+
panic("crypto/aes: output not full block")
|
| 71 |
+
}
|
| 72 |
+
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
|
| 73 |
+
panic("crypto/aes: invalid buffer overlap")
|
| 74 |
+
}
|
| 75 |
+
cryptBlocks(c.function, &c.key[0], &dst[0], &src[0], BlockSize)
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
func (c *aesCipherAsm) Decrypt(dst, src []byte) {
|
| 79 |
+
if len(src) < BlockSize {
|
| 80 |
+
panic("crypto/aes: input not full block")
|
| 81 |
+
}
|
| 82 |
+
if len(dst) < BlockSize {
|
| 83 |
+
panic("crypto/aes: output not full block")
|
| 84 |
+
}
|
| 85 |
+
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
|
| 86 |
+
panic("crypto/aes: invalid buffer overlap")
|
| 87 |
+
}
|
| 88 |
+
// The decrypt function code is equal to the function code + 128.
|
| 89 |
+
cryptBlocks(c.function+128, &c.key[0], &dst[0], &src[0], BlockSize)
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// expandKey is used by BenchmarkExpand. cipher message (KM) does not need key
|
| 93 |
+
// expansion so there is no assembly equivalent.
|
| 94 |
+
func expandKey(key []byte, enc, dec []uint32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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// Package aes implements AES encryption (formerly Rijndael), as defined in
|
| 6 |
+
// U.S. Federal Information Processing Standards Publication 197.
|
| 7 |
+
//
|
| 8 |
+
// The AES operations in this package are not implemented using constant-time algorithms.
|
| 9 |
+
// An exception is when running on systems with enabled hardware support for AES
|
| 10 |
+
// that makes these operations constant-time. Examples include amd64 systems using AES-NI
|
| 11 |
+
// extensions and s390x systems using Message-Security-Assist extensions.
|
| 12 |
+
// On such systems, when the result of NewCipher is passed to cipher.NewGCM,
|
| 13 |
+
// the GHASH operation used by GCM is also constant-time.
|
| 14 |
+
package aes
|
| 15 |
+
|
| 16 |
+
// This file contains AES constants - 8720 bytes of initialized data.
|
| 17 |
+
|
| 18 |
+
// https://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
|
| 19 |
+
|
| 20 |
+
// AES is based on the mathematical behavior of binary polynomials
|
| 21 |
+
// (polynomials over GF(2)) modulo the irreducible polynomial x⁸ + x⁴ + x³ + x + 1.
|
| 22 |
+
// Addition of these binary polynomials corresponds to binary xor.
|
| 23 |
+
// Reducing mod poly corresponds to binary xor with poly every
|
| 24 |
+
// time a 0x100 bit appears.
|
| 25 |
+
const poly = 1<<8 | 1<<4 | 1<<3 | 1<<1 | 1<<0 // x⁸ + x⁴ + x³ + x + 1
|
| 26 |
+
|
| 27 |
+
// Powers of x mod poly in GF(2).
|
| 28 |
+
var powx = [16]byte{
|
| 29 |
+
0x01,
|
| 30 |
+
0x02,
|
| 31 |
+
0x04,
|
| 32 |
+
0x08,
|
| 33 |
+
0x10,
|
| 34 |
+
0x20,
|
| 35 |
+
0x40,
|
| 36 |
+
0x80,
|
| 37 |
+
0x1b,
|
| 38 |
+
0x36,
|
| 39 |
+
0x6c,
|
| 40 |
+
0xd8,
|
| 41 |
+
0xab,
|
| 42 |
+
0x4d,
|
| 43 |
+
0x9a,
|
| 44 |
+
0x2f,
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// FIPS-197 Figure 7. S-box substitution values in hexadecimal format.
|
| 48 |
+
var sbox0 = [256]byte{
|
| 49 |
+
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
| 50 |
+
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
|
| 51 |
+
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
|
| 52 |
+
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
|
| 53 |
+
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
|
| 54 |
+
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
|
| 55 |
+
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
|
| 56 |
+
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
|
| 57 |
+
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
|
| 58 |
+
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
|
| 59 |
+
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
|
| 60 |
+
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
|
| 61 |
+
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
|
| 62 |
+
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
|
| 63 |
+
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
|
| 64 |
+
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// FIPS-197 Figure 14. Inverse S-box substitution values in hexadecimal format.
|
| 68 |
+
var sbox1 = [256]byte{
|
| 69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xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
|
| 80 |
+
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
|
| 81 |
+
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
|
| 82 |
+
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
|
| 83 |
+
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
|
| 84 |
+
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// Lookup tables for encryption.
|
| 88 |
+
// These can be recomputed by adapting the tests in aes_test.go.
|
| 89 |
+
|
| 90 |
+
var te0 = [256]uint32{
|
| 91 |
+
0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554,
|
| 92 |
+
0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a,
|
| 93 |
+
0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b,
|
| 94 |
+
0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea, 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b,
|
| 95 |
+
0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a, 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f,
|
| 96 |
+
0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108, 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f,
|
| 97 |
+
0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e, 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5,
|
| 98 |
+
0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d, 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f,
|
| 99 |
+
0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e, 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb,
|
| 100 |
+
0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce, 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497,
|
| 101 |
+
0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c, 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed,
|
| 102 |
+
0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b, 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a,
|
| 103 |
+
0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16, 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594,
|
| 104 |
+
0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81, 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3,
|
| 105 |
+
0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a, 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504,
|
| 106 |
+
0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163, 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d,
|
| 107 |
+
0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f, 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739,
|
| 108 |
+
0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47, 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395,
|
| 109 |
+
0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f, 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883,
|
| 110 |
+
0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c, 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76,
|
| 111 |
+
0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e, 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4,
|
| 112 |
+
0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6, 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b,
|
| 113 |
+
0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7, 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0,
|
| 114 |
+
0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25, 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818,
|
| 115 |
+
0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72, 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651,
|
| 116 |
+
0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21, 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85,
|
| 117 |
+
0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa, 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12,
|
| 118 |
+
0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0, 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9,
|
| 119 |
+
0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133, 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7,
|
| 120 |
+
0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920, 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a,
|
| 121 |
+
0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17, 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8,
|
| 122 |
+
0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a,
|
| 123 |
+
}
|
| 124 |
+
var te1 = [256]uint32{
|
| 125 |
+
0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5,
|
| 126 |
+
0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676,
|
| 127 |
+
0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d, 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0,
|
| 128 |
+
0xec41adad, 0x67b3d4d4, 0xfd5fa2a2, 0xea45afaf, 0xbf239c9c, 0xf753a4a4, 0x96e47272, 0x5b9bc0c0,
|
| 129 |
+
0xc275b7b7, 0x1ce1fdfd, 0xae3d9393, 0x6a4c2626, 0x5a6c3636, 0x417e3f3f, 0x02f5f7f7, 0x4f83cccc,
|
| 130 |
+
0x5c683434, 0xf451a5a5, 0x34d1e5e5, 0x08f9f1f1, 0x93e27171, 0x73abd8d8, 0x53623131, 0x3f2a1515,
|
| 131 |
+
0x0c080404, 0x5295c7c7, 0x65462323, 0x5e9dc3c3, 0x28301818, 0xa1379696, 0x0f0a0505, 0xb52f9a9a,
|
| 132 |
+
0x090e0707, 0x36241212, 0x9b1b8080, 0x3ddfe2e2, 0x26cdebeb, 0x694e2727, 0xcd7fb2b2, 0x9fea7575,
|
| 133 |
+
0x1b120909, 0x9e1d8383, 0x74582c2c, 0x2e341a1a, 0x2d361b1b, 0xb2dc6e6e, 0xeeb45a5a, 0xfb5ba0a0,
|
| 134 |
+
0xf6a45252, 0x4d763b3b, 0x61b7d6d6, 0xce7db3b3, 0x7b522929, 0x3edde3e3, 0x715e2f2f, 0x97138484,
|
| 135 |
+
0xf5a65353, 0x68b9d1d1, 0x00000000, 0x2cc1eded, 0x60402020, 0x1fe3fcfc, 0xc879b1b1, 0xedb65b5b,
|
| 136 |
+
0xbed46a6a, 0x468dcbcb, 0xd967bebe, 0x4b723939, 0xde944a4a, 0xd4984c4c, 0xe8b05858, 0x4a85cfcf,
|
| 137 |
+
0x6bbbd0d0, 0x2ac5efef, 0xe54faaaa, 0x16edfbfb, 0xc5864343, 0xd79a4d4d, 0x55663333, 0x94118585,
|
| 138 |
+
0xcf8a4545, 0x10e9f9f9, 0x06040202, 0x81fe7f7f, 0xf0a05050, 0x44783c3c, 0xba259f9f, 0xe34ba8a8,
|
| 139 |
+
0xf3a25151, 0xfe5da3a3, 0xc0804040, 0x8a058f8f, 0xad3f9292, 0xbc219d9d, 0x48703838, 0x04f1f5f5,
|
| 140 |
+
0xdf63bcbc, 0xc177b6b6, 0x75afdada, 0x63422121, 0x30201010, 0x1ae5ffff, 0x0efdf3f3, 0x6dbfd2d2,
|
| 141 |
+
0x4c81cdcd, 0x14180c0c, 0x35261313, 0x2fc3ecec, 0xe1be5f5f, 0xa2359797, 0xcc884444, 0x392e1717,
|
| 142 |
+
0x5793c4c4, 0xf255a7a7, 0x82fc7e7e, 0x477a3d3d, 0xacc86464, 0xe7ba5d5d, 0x2b321919, 0x95e67373,
|
| 143 |
+
0xa0c06060, 0x98198181, 0xd19e4f4f, 0x7fa3dcdc, 0x66442222, 0x7e542a2a, 0xab3b9090, 0x830b8888,
|
| 144 |
+
0xca8c4646, 0x29c7eeee, 0xd36bb8b8, 0x3c281414, 0x79a7dede, 0xe2bc5e5e, 0x1d160b0b, 0x76addbdb,
|
| 145 |
+
0x3bdbe0e0, 0x56643232, 0x4e743a3a, 0x1e140a0a, 0xdb924949, 0x0a0c0606, 0x6c482424, 0xe4b85c5c,
|
| 146 |
+
0x5d9fc2c2, 0x6ebdd3d3, 0xef43acac, 0xa6c46262, 0xa8399191, 0xa4319595, 0x37d3e4e4, 0x8bf27979,
|
| 147 |
+
0x32d5e7e7, 0x438bc8c8, 0x596e3737, 0xb7da6d6d, 0x8c018d8d, 0x64b1d5d5, 0xd29c4e4e, 0xe049a9a9,
|
| 148 |
+
0xb4d86c6c, 0xfaac5656, 0x07f3f4f4, 0x25cfeaea, 0xafca6565, 0x8ef47a7a, 0xe947aeae, 0x18100808,
|
| 149 |
+
0xd56fbaba, 0x88f07878, 0x6f4a2525, 0x725c2e2e, 0x24381c1c, 0xf157a6a6, 0xc773b4b4, 0x5197c6c6,
|
| 150 |
+
0x23cbe8e8, 0x7ca1dddd, 0x9ce87474, 0x213e1f1f, 0xdd964b4b, 0xdc61bdbd, 0x860d8b8b, 0x850f8a8a,
|
| 151 |
+
0x90e07070, 0x427c3e3e, 0xc471b5b5, 0xaacc6666, 0xd8904848, 0x05060303, 0x01f7f6f6, 0x121c0e0e,
|
| 152 |
+
0xa3c26161, 0x5f6a3535, 0xf9ae5757, 0xd069b9b9, 0x91178686, 0x5899c1c1, 0x273a1d1d, 0xb9279e9e,
|
| 153 |
+
0x38d9e1e1, 0x13ebf8f8, 0xb32b9898, 0x33221111, 0xbbd26969, 0x70a9d9d9, 0x89078e8e, 0xa7339494,
|
| 154 |
+
0xb62d9b9b, 0x223c1e1e, 0x92158787, 0x20c9e9e9, 0x4987cece, 0xffaa5555, 0x78502828, 0x7aa5dfdf,
|
| 155 |
+
0x8f038c8c, 0xf859a1a1, 0x80098989, 0x171a0d0d, 0xda65bfbf, 0x31d7e6e6, 0xc6844242, 0xb8d06868,
|
| 156 |
+
0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616,
|
| 157 |
+
}
|
| 158 |
+
var te2 = [256]uint32{
|
| 159 |
+
0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5,
|
| 160 |
+
0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76,
|
| 161 |
+
0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d, 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0,
|
| 162 |
+
0xadec41ad, 0xd467b3d4, 0xa2fd5fa2, 0xafea45af, 0x9cbf239c, 0xa4f753a4, 0x7296e472, 0xc05b9bc0,
|
| 163 |
+
0xb7c275b7, 0xfd1ce1fd, 0x93ae3d93, 0x266a4c26, 0x365a6c36, 0x3f417e3f, 0xf702f5f7, 0xcc4f83cc,
|
| 164 |
+
0x345c6834, 0xa5f451a5, 0xe534d1e5, 0xf108f9f1, 0x7193e271, 0xd873abd8, 0x31536231, 0x153f2a15,
|
| 165 |
+
0x040c0804, 0xc75295c7, 0x23654623, 0xc35e9dc3, 0x18283018, 0x96a13796, 0x050f0a05, 0x9ab52f9a,
|
| 166 |
+
0x07090e07, 0x12362412, 0x809b1b80, 0xe23ddfe2, 0xeb26cdeb, 0x27694e27, 0xb2cd7fb2, 0x759fea75,
|
| 167 |
+
0x091b1209, 0x839e1d83, 0x2c74582c, 0x1a2e341a, 0x1b2d361b, 0x6eb2dc6e, 0x5aeeb45a, 0xa0fb5ba0,
|
| 168 |
+
0x52f6a452, 0x3b4d763b, 0xd661b7d6, 0xb3ce7db3, 0x297b5229, 0xe33edde3, 0x2f715e2f, 0x84971384,
|
| 169 |
+
0x53f5a653, 0xd168b9d1, 0x00000000, 0xed2cc1ed, 0x20604020, 0xfc1fe3fc, 0xb1c879b1, 0x5bedb65b,
|
| 170 |
+
0x6abed46a, 0xcb468dcb, 0xbed967be, 0x394b7239, 0x4ade944a, 0x4cd4984c, 0x58e8b058, 0xcf4a85cf,
|
| 171 |
+
0xd06bbbd0, 0xef2ac5ef, 0xaae54faa, 0xfb16edfb, 0x43c58643, 0x4dd79a4d, 0x33556633, 0x85941185,
|
| 172 |
+
0x45cf8a45, 0xf910e9f9, 0x02060402, 0x7f81fe7f, 0x50f0a050, 0x3c44783c, 0x9fba259f, 0xa8e34ba8,
|
| 173 |
+
0x51f3a251, 0xa3fe5da3, 0x40c08040, 0x8f8a058f, 0x92ad3f92, 0x9dbc219d, 0x38487038, 0xf504f1f5,
|
| 174 |
+
0xbcdf63bc, 0xb6c177b6, 0xda75afda, 0x21634221, 0x10302010, 0xff1ae5ff, 0xf30efdf3, 0xd26dbfd2,
|
| 175 |
+
0xcd4c81cd, 0x0c14180c, 0x13352613, 0xec2fc3ec, 0x5fe1be5f, 0x97a23597, 0x44cc8844, 0x17392e17,
|
| 176 |
+
0xc45793c4, 0xa7f255a7, 0x7e82fc7e, 0x3d477a3d, 0x64acc864, 0x5de7ba5d, 0x192b3219, 0x7395e673,
|
| 177 |
+
0x60a0c060, 0x81981981, 0x4fd19e4f, 0xdc7fa3dc, 0x22664422, 0x2a7e542a, 0x90ab3b90, 0x88830b88,
|
| 178 |
+
0x46ca8c46, 0xee29c7ee, 0xb8d36bb8, 0x143c2814, 0xde79a7de, 0x5ee2bc5e, 0x0b1d160b, 0xdb76addb,
|
| 179 |
+
0xe03bdbe0, 0x32566432, 0x3a4e743a, 0x0a1e140a, 0x49db9249, 0x060a0c06, 0x246c4824, 0x5ce4b85c,
|
| 180 |
+
0xc25d9fc2, 0xd36ebdd3, 0xacef43ac, 0x62a6c462, 0x91a83991, 0x95a43195, 0xe437d3e4, 0x798bf279,
|
| 181 |
+
0xe732d5e7, 0xc8438bc8, 0x37596e37, 0x6db7da6d, 0x8d8c018d, 0xd564b1d5, 0x4ed29c4e, 0xa9e049a9,
|
| 182 |
+
0x6cb4d86c, 0x56faac56, 0xf407f3f4, 0xea25cfea, 0x65afca65, 0x7a8ef47a, 0xaee947ae, 0x08181008,
|
| 183 |
+
0xbad56fba, 0x7888f078, 0x256f4a25, 0x2e725c2e, 0x1c24381c, 0xa6f157a6, 0xb4c773b4, 0xc65197c6,
|
| 184 |
+
0xe823cbe8, 0xdd7ca1dd, 0x749ce874, 0x1f213e1f, 0x4bdd964b, 0xbddc61bd, 0x8b860d8b, 0x8a850f8a,
|
| 185 |
+
0x7090e070, 0x3e427c3e, 0xb5c471b5, 0x66aacc66, 0x48d89048, 0x03050603, 0xf601f7f6, 0x0e121c0e,
|
| 186 |
+
0x61a3c261, 0x355f6a35, 0x57f9ae57, 0xb9d069b9, 0x86911786, 0xc15899c1, 0x1d273a1d, 0x9eb9279e,
|
| 187 |
+
0xe138d9e1, 0xf813ebf8, 0x98b32b98, 0x11332211, 0x69bbd269, 0xd970a9d9, 0x8e89078e, 0x94a73394,
|
| 188 |
+
0x9bb62d9b, 0x1e223c1e, 0x87921587, 0xe920c9e9, 0xce4987ce, 0x55ffaa55, 0x28785028, 0xdf7aa5df,
|
| 189 |
+
0x8c8f038c, 0xa1f859a1, 0x89800989, 0x0d171a0d, 0xbfda65bf, 0xe631d7e6, 0x42c68442, 0x68b8d068,
|
| 190 |
+
0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16,
|
| 191 |
+
}
|
| 192 |
+
var te3 = [256]uint32{
|
| 193 |
+
0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491,
|
| 194 |
+
0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec,
|
| 195 |
+
0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa, 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb,
|
| 196 |
+
0xadadec41, 0xd4d467b3, 0xa2a2fd5f, 0xafafea45, 0x9c9cbf23, 0xa4a4f753, 0x727296e4, 0xc0c05b9b,
|
| 197 |
+
0xb7b7c275, 0xfdfd1ce1, 0x9393ae3d, 0x26266a4c, 0x36365a6c, 0x3f3f417e, 0xf7f702f5, 0xcccc4f83,
|
| 198 |
+
0x34345c68, 0xa5a5f451, 0xe5e534d1, 0xf1f108f9, 0x717193e2, 0xd8d873ab, 0x31315362, 0x15153f2a,
|
| 199 |
+
0x04040c08, 0xc7c75295, 0x23236546, 0xc3c35e9d, 0x18182830, 0x9696a137, 0x05050f0a, 0x9a9ab52f,
|
| 200 |
+
0x0707090e, 0x12123624, 0x80809b1b, 0xe2e23ddf, 0xebeb26cd, 0x2727694e, 0xb2b2cd7f, 0x75759fea,
|
| 201 |
+
0x09091b12, 0x83839e1d, 0x2c2c7458, 0x1a1a2e34, 0x1b1b2d36, 0x6e6eb2dc, 0x5a5aeeb4, 0xa0a0fb5b,
|
| 202 |
+
0x5252f6a4, 0x3b3b4d76, 0xd6d661b7, 0xb3b3ce7d, 0x29297b52, 0xe3e33edd, 0x2f2f715e, 0x84849713,
|
| 203 |
+
0x5353f5a6, 0xd1d168b9, 0x00000000, 0xeded2cc1, 0x20206040, 0xfcfc1fe3, 0xb1b1c879, 0x5b5bedb6,
|
| 204 |
+
0x6a6abed4, 0xcbcb468d, 0xbebed967, 0x39394b72, 0x4a4ade94, 0x4c4cd498, 0x5858e8b0, 0xcfcf4a85,
|
| 205 |
+
0xd0d06bbb, 0xefef2ac5, 0xaaaae54f, 0xfbfb16ed, 0x4343c586, 0x4d4dd79a, 0x33335566, 0x85859411,
|
| 206 |
+
0x4545cf8a, 0xf9f910e9, 0x02020604, 0x7f7f81fe, 0x5050f0a0, 0x3c3c4478, 0x9f9fba25, 0xa8a8e34b,
|
| 207 |
+
0x5151f3a2, 0xa3a3fe5d, 0x4040c080, 0x8f8f8a05, 0x9292ad3f, 0x9d9dbc21, 0x38384870, 0xf5f504f1,
|
| 208 |
+
0xbcbcdf63, 0xb6b6c177, 0xdada75af, 0x21216342, 0x10103020, 0xffff1ae5, 0xf3f30efd, 0xd2d26dbf,
|
| 209 |
+
0xcdcd4c81, 0x0c0c1418, 0x13133526, 0xecec2fc3, 0x5f5fe1be, 0x9797a235, 0x4444cc88, 0x1717392e,
|
| 210 |
+
0xc4c45793, 0xa7a7f255, 0x7e7e82fc, 0x3d3d477a, 0x6464acc8, 0x5d5de7ba, 0x19192b32, 0x737395e6,
|
| 211 |
+
0x6060a0c0, 0x81819819, 0x4f4fd19e, 0xdcdc7fa3, 0x22226644, 0x2a2a7e54, 0x9090ab3b, 0x8888830b,
|
| 212 |
+
0x4646ca8c, 0xeeee29c7, 0xb8b8d36b, 0x14143c28, 0xdede79a7, 0x5e5ee2bc, 0x0b0b1d16, 0xdbdb76ad,
|
| 213 |
+
0xe0e03bdb, 0x32325664, 0x3a3a4e74, 0x0a0a1e14, 0x4949db92, 0x06060a0c, 0x24246c48, 0x5c5ce4b8,
|
| 214 |
+
0xc2c25d9f, 0xd3d36ebd, 0xacacef43, 0x6262a6c4, 0x9191a839, 0x9595a431, 0xe4e437d3, 0x79798bf2,
|
| 215 |
+
0xe7e732d5, 0xc8c8438b, 0x3737596e, 0x6d6db7da, 0x8d8d8c01, 0xd5d564b1, 0x4e4ed29c, 0xa9a9e049,
|
| 216 |
+
0x6c6cb4d8, 0x5656faac, 0xf4f407f3, 0xeaea25cf, 0x6565afca, 0x7a7a8ef4, 0xaeaee947, 0x08081810,
|
| 217 |
+
0xbabad56f, 0x787888f0, 0x25256f4a, 0x2e2e725c, 0x1c1c2438, 0xa6a6f157, 0xb4b4c773, 0xc6c65197,
|
| 218 |
+
0xe8e823cb, 0xdddd7ca1, 0x74749ce8, 0x1f1f213e, 0x4b4bdd96, 0xbdbddc61, 0x8b8b860d, 0x8a8a850f,
|
| 219 |
+
0x707090e0, 0x3e3e427c, 0xb5b5c471, 0x6666aacc, 0x4848d890, 0x03030506, 0xf6f601f7, 0x0e0e121c,
|
| 220 |
+
0x6161a3c2, 0x35355f6a, 0x5757f9ae, 0xb9b9d069, 0x86869117, 0xc1c15899, 0x1d1d273a, 0x9e9eb927,
|
| 221 |
+
0xe1e138d9, 0xf8f813eb, 0x9898b32b, 0x11113322, 0x6969bbd2, 0xd9d970a9, 0x8e8e8907, 0x9494a733,
|
| 222 |
+
0x9b9bb62d, 0x1e1e223c, 0x87879215, 0xe9e920c9, 0xcece4987, 0x5555ffaa, 0x28287850, 0xdfdf7aa5,
|
| 223 |
+
0x8c8c8f03, 0xa1a1f859, 0x89898009, 0x0d0d171a, 0xbfbfda65, 0xe6e631d7, 0x4242c684, 0x6868b8d0,
|
| 224 |
+
0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e, 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c,
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
// Lookup tables for decryption.
|
| 228 |
+
// These can be recomputed by adapting the tests in aes_test.go.
|
| 229 |
+
|
| 230 |
+
var td0 = [256]uint32{
|
| 231 |
+
0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393,
|
| 232 |
+
0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f,
|
| 233 |
+
0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6,
|
| 234 |
+
0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da, 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844,
|
| 235 |
+
0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd, 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4,
|
| 236 |
+
0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45, 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94,
|
| 237 |
+
0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7, 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a,
|
| 238 |
+
0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5, 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c,
|
| 239 |
+
0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1, 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a,
|
| 240 |
+
0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75, 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051,
|
| 241 |
+
0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46, 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff,
|
| 242 |
+
0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77, 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb,
|
| 243 |
+
0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000, 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e,
|
| 244 |
+
0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927, 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a,
|
| 245 |
+
0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e, 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16,
|
| 246 |
+
0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d, 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8,
|
| 247 |
+
0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd, 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34,
|
| 248 |
+
0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163, 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120,
|
| 249 |
+
0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d, 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0,
|
| 250 |
+
0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422, 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef,
|
| 251 |
+
0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36, 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4,
|
| 252 |
+
0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662, 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5,
|
| 253 |
+
0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3, 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b,
|
| 254 |
+
0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8, 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6,
|
| 255 |
+
0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6, 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0,
|
| 256 |
+
0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815, 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f,
|
| 257 |
+
0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df, 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f,
|
| 258 |
+
0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e, 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713,
|
| 259 |
+
0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89, 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c,
|
| 260 |
+
0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf, 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86,
|
| 261 |
+
0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541,
|
| 262 |
+
0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742,
|
| 263 |
+
}
|
| 264 |
+
var td1 = [256]uint32{
|
| 265 |
+
0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303,
|
| 266 |
+
0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3,
|
| 267 |
+
0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0, 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9,
|
| 268 |
+
0xe7038f5f, 0x9515929c, 0xebbf6d7a, 0xda955259, 0x2dd4be83, 0xd3587421, 0x2949e069, 0x448ec9c8,
|
| 269 |
+
0x6a75c289, 0x78f48e79, 0x6b99583e, 0xdd27b971, 0xb6bee14f, 0x17f088ad, 0x66c920ac, 0xb47dce3a,
|
| 270 |
+
0x1863df4a, 0x82e51a31, 0x60975133, 0x4562537f, 0xe0b16477, 0x84bb6bae, 0x1cfe81a0, 0x94f9082b,
|
| 271 |
+
0x58704868, 0x198f45fd, 0x8794de6c, 0xb7527bf8, 0x23ab73d3, 0xe2724b02, 0x57e31f8f, 0x2a6655ab,
|
| 272 |
+
0x07b2eb28, 0x032fb5c2, 0x9a86c57b, 0xa5d33708, 0xf2302887, 0xb223bfa5, 0xba02036a, 0x5ced1682,
|
| 273 |
+
0x2b8acf1c, 0x92a779b4, 0xf0f307f2, 0xa14e69e2, 0xcd65daf4, 0xd50605be, 0x1fd13462, 0x8ac4a6fe,
|
| 274 |
+
0x9d342e53, 0xa0a2f355, 0x32058ae1, 0x75a4f6eb, 0x390b83ec, 0xaa4060ef, 0x065e719f, 0x51bd6e10,
|
| 275 |
+
0xf93e218a, 0x3d96dd06, 0xaedd3e05, 0x464de6bd, 0xb591548d, 0x0571c45d, 0x6f0406d4, 0xff605015,
|
| 276 |
+
0x241998fb, 0x97d6bde9, 0xcc894043, 0x7767d99e, 0xbdb0e842, 0x8807898b, 0x38e7195b, 0xdb79c8ee,
|
| 277 |
+
0x47a17c0a, 0xe97c420f, 0xc9f8841e, 0x00000000, 0x83098086, 0x48322bed, 0xac1e1170, 0x4e6c5a72,
|
| 278 |
+
0xfbfd0eff, 0x560f8538, 0x1e3daed5, 0x27362d39, 0x640a0fd9, 0x21685ca6, 0xd19b5b54, 0x3a24362e,
|
| 279 |
+
0xb10c0a67, 0x0f9357e7, 0xd2b4ee96, 0x9e1b9b91, 0x4f80c0c5, 0xa261dc20, 0x695a774b, 0x161c121a,
|
| 280 |
+
0x0ae293ba, 0xe5c0a02a, 0x433c22e0, 0x1d121b17, 0x0b0e090d, 0xadf28bc7, 0xb92db6a8, 0xc8141ea9,
|
| 281 |
+
0x8557f119, 0x4caf7507, 0xbbee99dd, 0xfda37f60, 0x9ff70126, 0xbc5c72f5, 0xc544663b, 0x345bfb7e,
|
| 282 |
+
0x768b4329, 0xdccb23c6, 0x68b6edfc, 0x63b8e4f1, 0xcad731dc, 0x10426385, 0x40139722, 0x2084c611,
|
| 283 |
+
0x7d854a24, 0xf8d2bb3d, 0x11aef932, 0x6dc729a1, 0x4b1d9e2f, 0xf3dcb230, 0xec0d8652, 0xd077c1e3,
|
| 284 |
+
0x6c2bb316, 0x99a970b9, 0xfa119448, 0x2247e964, 0xc4a8fc8c, 0x1aa0f03f, 0xd8567d2c, 0xef223390,
|
| 285 |
+
0xc787494e, 0xc1d938d1, 0xfe8ccaa2, 0x3698d40b, 0xcfa6f581, 0x28a57ade, 0x26dab78e, 0xa43fadbf,
|
| 286 |
+
0xe42c3a9d, 0x0d507892, 0x9b6a5fcc, 0x62547e46, 0xc2f68d13, 0xe890d8b8, 0x5e2e39f7, 0xf582c3af,
|
| 287 |
+
0xbe9f5d80, 0x7c69d093, 0xa96fd52d, 0xb3cf2512, 0x3bc8ac99, 0xa710187d, 0x6ee89c63, 0x7bdb3bbb,
|
| 288 |
+
0x09cd2678, 0xf46e5918, 0x01ec9ab7, 0xa8834f9a, 0x65e6956e, 0x7eaaffe6, 0x0821bccf, 0xe6ef15e8,
|
| 289 |
+
0xd9bae79b, 0xce4a6f36, 0xd4ea9f09, 0xd629b07c, 0xaf31a4b2, 0x312a3f23, 0x30c6a594, 0xc035a266,
|
| 290 |
+
0x37744ebc, 0xa6fc82ca, 0xb0e090d0, 0x1533a7d8, 0x4af10498, 0xf741ecda, 0x0e7fcd50, 0x2f1791f6,
|
| 291 |
+
0x8d764dd6, 0x4d43efb0, 0x54ccaa4d, 0xdfe49604, 0xe39ed1b5, 0x1b4c6a88, 0xb8c12c1f, 0x7f466551,
|
| 292 |
+
0x049d5eea, 0x5d018c35, 0x73fa8774, 0x2efb0b41, 0x5ab3671d, 0x5292dbd2, 0x33e91056, 0x136dd647,
|
| 293 |
+
0x8c9ad761, 0x7a37a10c, 0x8e59f814, 0x89eb133c, 0xeecea927, 0x35b761c9, 0xede11ce5, 0x3c7a47b1,
|
| 294 |
+
0x599cd2df, 0x3f55f273, 0x791814ce, 0xbf73c737, 0xea53f7cd, 0x5b5ffdaa, 0x14df3d6f, 0x867844db,
|
| 295 |
+
0x81caaff3, 0x3eb968c4, 0x2c382434, 0x5fc2a340, 0x72161dc3, 0x0cbce225, 0x8b283c49, 0x41ff0d95,
|
| 296 |
+
0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857,
|
| 297 |
+
}
|
| 298 |
+
var td2 = [256]uint32{
|
| 299 |
+
0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3,
|
| 300 |
+
0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562,
|
| 301 |
+
0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe, 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3,
|
| 302 |
+
0x5fe7038f, 0x9c951592, 0x7aebbf6d, 0x59da9552, 0x832dd4be, 0x21d35874, 0x692949e0, 0xc8448ec9,
|
| 303 |
+
0x896a75c2, 0x7978f48e, 0x3e6b9958, 0x71dd27b9, 0x4fb6bee1, 0xad17f088, 0xac66c920, 0x3ab47dce,
|
| 304 |
+
0x4a1863df, 0x3182e51a, 0x33609751, 0x7f456253, 0x77e0b164, 0xae84bb6b, 0xa01cfe81, 0x2b94f908,
|
| 305 |
+
0x68587048, 0xfd198f45, 0x6c8794de, 0xf8b7527b, 0xd323ab73, 0x02e2724b, 0x8f57e31f, 0xab2a6655,
|
| 306 |
+
0x2807b2eb, 0xc2032fb5, 0x7b9a86c5, 0x08a5d337, 0x87f23028, 0xa5b223bf, 0x6aba0203, 0x825ced16,
|
| 307 |
+
0x1c2b8acf, 0xb492a779, 0xf2f0f307, 0xe2a14e69, 0xf4cd65da, 0xbed50605, 0x621fd134, 0xfe8ac4a6,
|
| 308 |
+
0x539d342e, 0x55a0a2f3, 0xe132058a, 0xeb75a4f6, 0xec390b83, 0xefaa4060, 0x9f065e71, 0x1051bd6e,
|
| 309 |
+
0x8af93e21, 0x063d96dd, 0x05aedd3e, 0xbd464de6, 0x8db59154, 0x5d0571c4, 0xd46f0406, 0x15ff6050,
|
| 310 |
+
0xfb241998, 0xe997d6bd, 0x43cc8940, 0x9e7767d9, 0x42bdb0e8, 0x8b880789, 0x5b38e719, 0xeedb79c8,
|
| 311 |
+
0x0a47a17c, 0x0fe97c42, 0x1ec9f884, 0x00000000, 0x86830980, 0xed48322b, 0x70ac1e11, 0x724e6c5a,
|
| 312 |
+
0xfffbfd0e, 0x38560f85, 0xd51e3dae, 0x3927362d, 0xd9640a0f, 0xa621685c, 0x54d19b5b, 0x2e3a2436,
|
| 313 |
+
0x67b10c0a, 0xe70f9357, 0x96d2b4ee, 0x919e1b9b, 0xc54f80c0, 0x20a261dc, 0x4b695a77, 0x1a161c12,
|
| 314 |
+
0xba0ae293, 0x2ae5c0a0, 0xe0433c22, 0x171d121b, 0x0d0b0e09, 0xc7adf28b, 0xa8b92db6, 0xa9c8141e,
|
| 315 |
+
0x198557f1, 0x074caf75, 0xddbbee99, 0x60fda37f, 0x269ff701, 0xf5bc5c72, 0x3bc54466, 0x7e345bfb,
|
| 316 |
+
0x29768b43, 0xc6dccb23, 0xfc68b6ed, 0xf163b8e4, 0xdccad731, 0x85104263, 0x22401397, 0x112084c6,
|
| 317 |
+
0x247d854a, 0x3df8d2bb, 0x3211aef9, 0xa16dc729, 0x2f4b1d9e, 0x30f3dcb2, 0x52ec0d86, 0xe3d077c1,
|
| 318 |
+
0x166c2bb3, 0xb999a970, 0x48fa1194, 0x642247e9, 0x8cc4a8fc, 0x3f1aa0f0, 0x2cd8567d, 0x90ef2233,
|
| 319 |
+
0x4ec78749, 0xd1c1d938, 0xa2fe8cca, 0x0b3698d4, 0x81cfa6f5, 0xde28a57a, 0x8e26dab7, 0xbfa43fad,
|
| 320 |
+
0x9de42c3a, 0x920d5078, 0xcc9b6a5f, 0x4662547e, 0x13c2f68d, 0xb8e890d8, 0xf75e2e39, 0xaff582c3,
|
| 321 |
+
0x80be9f5d, 0x937c69d0, 0x2da96fd5, 0x12b3cf25, 0x993bc8ac, 0x7da71018, 0x636ee89c, 0xbb7bdb3b,
|
| 322 |
+
0x7809cd26, 0x18f46e59, 0xb701ec9a, 0x9aa8834f, 0x6e65e695, 0xe67eaaff, 0xcf0821bc, 0xe8e6ef15,
|
| 323 |
+
0x9bd9bae7, 0x36ce4a6f, 0x09d4ea9f, 0x7cd629b0, 0xb2af31a4, 0x23312a3f, 0x9430c6a5, 0x66c035a2,
|
| 324 |
+
0xbc37744e, 0xcaa6fc82, 0xd0b0e090, 0xd81533a7, 0x984af104, 0xdaf741ec, 0x500e7fcd, 0xf62f1791,
|
| 325 |
+
0xd68d764d, 0xb04d43ef, 0x4d54ccaa, 0x04dfe496, 0xb5e39ed1, 0x881b4c6a, 0x1fb8c12c, 0x517f4665,
|
| 326 |
+
0xea049d5e, 0x355d018c, 0x7473fa87, 0x412efb0b, 0x1d5ab367, 0xd25292db, 0x5633e910, 0x47136dd6,
|
| 327 |
+
0x618c9ad7, 0x0c7a37a1, 0x148e59f8, 0x3c89eb13, 0x27eecea9, 0xc935b761, 0xe5ede11c, 0xb13c7a47,
|
| 328 |
+
0xdf599cd2, 0x733f55f2, 0xce791814, 0x37bf73c7, 0xcdea53f7, 0xaa5b5ffd, 0x6f14df3d, 0xdb867844,
|
| 329 |
+
0xf381caaf, 0xc43eb968, 0x342c3824, 0x405fc2a3, 0xc372161d, 0x250cbce2, 0x498b283c, 0x9541ff0d,
|
| 330 |
+
0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8,
|
| 331 |
+
}
|
| 332 |
+
var td3 = [256]uint32{
|
| 333 |
+
0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b,
|
| 334 |
+
0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5,
|
| 335 |
+
0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d, 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b,
|
| 336 |
+
0x8f5fe703, 0x929c9515, 0x6d7aebbf, 0x5259da95, 0xbe832dd4, 0x7421d358, 0xe0692949, 0xc9c8448e,
|
| 337 |
+
0xc2896a75, 0x8e7978f4, 0x583e6b99, 0xb971dd27, 0xe14fb6be, 0x88ad17f0, 0x20ac66c9, 0xce3ab47d,
|
| 338 |
+
0xdf4a1863, 0x1a3182e5, 0x51336097, 0x537f4562, 0x6477e0b1, 0x6bae84bb, 0x81a01cfe, 0x082b94f9,
|
| 339 |
+
0x48685870, 0x45fd198f, 0xde6c8794, 0x7bf8b752, 0x73d323ab, 0x4b02e272, 0x1f8f57e3, 0x55ab2a66,
|
| 340 |
+
0xeb2807b2, 0xb5c2032f, 0xc57b9a86, 0x3708a5d3, 0x2887f230, 0xbfa5b223, 0x036aba02, 0x16825ced,
|
| 341 |
+
0xcf1c2b8a, 0x79b492a7, 0x07f2f0f3, 0x69e2a14e, 0xdaf4cd65, 0x05bed506, 0x34621fd1, 0xa6fe8ac4,
|
| 342 |
+
0x2e539d34, 0xf355a0a2, 0x8ae13205, 0xf6eb75a4, 0x83ec390b, 0x60efaa40, 0x719f065e, 0x6e1051bd,
|
| 343 |
+
0x218af93e, 0xdd063d96, 0x3e05aedd, 0xe6bd464d, 0x548db591, 0xc45d0571, 0x06d46f04, 0x5015ff60,
|
| 344 |
+
0x98fb2419, 0xbde997d6, 0x4043cc89, 0xd99e7767, 0xe842bdb0, 0x898b8807, 0x195b38e7, 0xc8eedb79,
|
| 345 |
+
0x7c0a47a1, 0x420fe97c, 0x841ec9f8, 0x00000000, 0x80868309, 0x2bed4832, 0x1170ac1e, 0x5a724e6c,
|
| 346 |
+
0x0efffbfd, 0x8538560f, 0xaed51e3d, 0x2d392736, 0x0fd9640a, 0x5ca62168, 0x5b54d19b, 0x362e3a24,
|
| 347 |
+
0x0a67b10c, 0x57e70f93, 0xee96d2b4, 0x9b919e1b, 0xc0c54f80, 0xdc20a261, 0x774b695a, 0x121a161c,
|
| 348 |
+
0x93ba0ae2, 0xa02ae5c0, 0x22e0433c, 0x1b171d12, 0x090d0b0e, 0x8bc7adf2, 0xb6a8b92d, 0x1ea9c814,
|
| 349 |
+
0xf1198557, 0x75074caf, 0x99ddbbee, 0x7f60fda3, 0x01269ff7, 0x72f5bc5c, 0x663bc544, 0xfb7e345b,
|
| 350 |
+
0x4329768b, 0x23c6dccb, 0xedfc68b6, 0xe4f163b8, 0x31dccad7, 0x63851042, 0x97224013, 0xc6112084,
|
| 351 |
+
0x4a247d85, 0xbb3df8d2, 0xf93211ae, 0x29a16dc7, 0x9e2f4b1d, 0xb230f3dc, 0x8652ec0d, 0xc1e3d077,
|
| 352 |
+
0xb3166c2b, 0x70b999a9, 0x9448fa11, 0xe9642247, 0xfc8cc4a8, 0xf03f1aa0, 0x7d2cd856, 0x3390ef22,
|
| 353 |
+
0x494ec787, 0x38d1c1d9, 0xcaa2fe8c, 0xd40b3698, 0xf581cfa6, 0x7ade28a5, 0xb78e26da, 0xadbfa43f,
|
| 354 |
+
0x3a9de42c, 0x78920d50, 0x5fcc9b6a, 0x7e466254, 0x8d13c2f6, 0xd8b8e890, 0x39f75e2e, 0xc3aff582,
|
| 355 |
+
0x5d80be9f, 0xd0937c69, 0xd52da96f, 0x2512b3cf, 0xac993bc8, 0x187da710, 0x9c636ee8, 0x3bbb7bdb,
|
| 356 |
+
0x267809cd, 0x5918f46e, 0x9ab701ec, 0x4f9aa883, 0x956e65e6, 0xffe67eaa, 0xbccf0821, 0x15e8e6ef,
|
| 357 |
+
0xe79bd9ba, 0x6f36ce4a, 0x9f09d4ea, 0xb07cd629, 0xa4b2af31, 0x3f23312a, 0xa59430c6, 0xa266c035,
|
| 358 |
+
0x4ebc3774, 0x82caa6fc, 0x90d0b0e0, 0xa7d81533, 0x04984af1, 0xecdaf741, 0xcd500e7f, 0x91f62f17,
|
| 359 |
+
0x4dd68d76, 0xefb04d43, 0xaa4d54cc, 0x9604dfe4, 0xd1b5e39e, 0x6a881b4c, 0x2c1fb8c1, 0x65517f46,
|
| 360 |
+
0x5eea049d, 0x8c355d01, 0x877473fa, 0x0b412efb, 0x671d5ab3, 0xdbd25292, 0x105633e9, 0xd647136d,
|
| 361 |
+
0xd7618c9a, 0xa10c7a37, 0xf8148e59, 0x133c89eb, 0xa927eece, 0x61c935b7, 0x1ce5ede1, 0x47b13c7a,
|
| 362 |
+
0xd2df599c, 0xf2733f55, 0x14ce7918, 0xc737bf73, 0xf7cdea53, 0xfdaa5b5f, 0x3d6f14df, 0x44db8678,
|
| 363 |
+
0xaff381ca, 0x68c43eb9, 0x24342c38, 0xa3405fc2, 0x1dc37216, 0xe2250cbc, 0x3c498b28, 0x0d9541ff,
|
| 364 |
+
0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0,
|
| 365 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/aes/ctr_s390x.go
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2016 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package aes
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/cipher"
|
| 9 |
+
"crypto/internal/alias"
|
| 10 |
+
"encoding/binary"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
// Assert that aesCipherAsm implements the ctrAble interface.
|
| 14 |
+
var _ ctrAble = (*aesCipherAsm)(nil)
|
| 15 |
+
|
| 16 |
+
// xorBytes xors the contents of a and b and places the resulting values into
|
| 17 |
+
// dst. If a and b are not the same length then the number of bytes processed
|
| 18 |
+
// will be equal to the length of shorter of the two. Returns the number
|
| 19 |
+
// of bytes processed.
|
| 20 |
+
//
|
| 21 |
+
//go:noescape
|
| 22 |
+
func xorBytes(dst, a, b []byte) int
|
| 23 |
+
|
| 24 |
+
// streamBufferSize is the number of bytes of encrypted counter values to cache.
|
| 25 |
+
const streamBufferSize = 32 * BlockSize
|
| 26 |
+
|
| 27 |
+
type aesctr struct {
|
| 28 |
+
block *aesCipherAsm // block cipher
|
| 29 |
+
ctr [2]uint64 // next value of the counter (big endian)
|
| 30 |
+
buffer []byte // buffer for the encrypted counter values
|
| 31 |
+
storage [streamBufferSize]byte // array backing buffer slice
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
// NewCTR returns a Stream which encrypts/decrypts using the AES block
|
| 35 |
+
// cipher in counter mode. The length of iv must be the same as [BlockSize].
|
| 36 |
+
func (c *aesCipherAsm) NewCTR(iv []byte) cipher.Stream {
|
| 37 |
+
if len(iv) != BlockSize {
|
| 38 |
+
panic("cipher.NewCTR: IV length must equal block size")
|
| 39 |
+
}
|
| 40 |
+
var ac aesctr
|
| 41 |
+
ac.block = c
|
| 42 |
+
ac.ctr[0] = binary.BigEndian.Uint64(iv[0:]) // high bits
|
| 43 |
+
ac.ctr[1] = binary.BigEndian.Uint64(iv[8:]) // low bits
|
| 44 |
+
ac.buffer = ac.storage[:0]
|
| 45 |
+
return &ac
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
func (c *aesctr) refill() {
|
| 49 |
+
// Fill up the buffer with an incrementing count.
|
| 50 |
+
c.buffer = c.storage[:streamBufferSize]
|
| 51 |
+
c0, c1 := c.ctr[0], c.ctr[1]
|
| 52 |
+
for i := 0; i < streamBufferSize; i += 16 {
|
| 53 |
+
binary.BigEndian.PutUint64(c.buffer[i+0:], c0)
|
| 54 |
+
binary.BigEndian.PutUint64(c.buffer[i+8:], c1)
|
| 55 |
+
|
| 56 |
+
// Increment in big endian: c0 is high, c1 is low.
|
| 57 |
+
c1++
|
| 58 |
+
if c1 == 0 {
|
| 59 |
+
// add carry
|
| 60 |
+
c0++
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
c.ctr[0], c.ctr[1] = c0, c1
|
| 64 |
+
// Encrypt the buffer using AES in ECB mode.
|
| 65 |
+
cryptBlocks(c.block.function, &c.block.key[0], &c.buffer[0], &c.buffer[0], streamBufferSize)
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
func (c *aesctr) XORKeyStream(dst, src []byte) {
|
| 69 |
+
if len(dst) < len(src) {
|
| 70 |
+
panic("crypto/cipher: output smaller than input")
|
| 71 |
+
}
|
| 72 |
+
if alias.InexactOverlap(dst[:len(src)], src) {
|
| 73 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 74 |
+
}
|
| 75 |
+
for len(src) > 0 {
|
| 76 |
+
if len(c.buffer) == 0 {
|
| 77 |
+
c.refill()
|
| 78 |
+
}
|
| 79 |
+
n := xorBytes(dst, src, c.buffer)
|
| 80 |
+
c.buffer = c.buffer[n:]
|
| 81 |
+
src = src[n:]
|
| 82 |
+
dst = dst[n:]
|
| 83 |
+
}
|
| 84 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/aes/gcm_amd64.s
ADDED
|
@@ -0,0 +1,1286 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2015 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// This is an optimized implementation of AES-GCM using AES-NI and CLMUL-NI
|
| 6 |
+
// The implementation uses some optimization as described in:
|
| 7 |
+
// [1] Gueron, S., Kounavis, M.E.: Intel® Carry-Less Multiplication
|
| 8 |
+
// Instruction and its Usage for Computing the GCM Mode rev. 2.02
|
| 9 |
+
// [2] Gueron, S., Krasnov, V.: Speeding up Counter Mode in Software and
|
| 10 |
+
// Hardware
|
| 11 |
+
|
| 12 |
+
#include "textflag.h"
|
| 13 |
+
|
| 14 |
+
#define B0 X0
|
| 15 |
+
#define B1 X1
|
| 16 |
+
#define B2 X2
|
| 17 |
+
#define B3 X3
|
| 18 |
+
#define B4 X4
|
| 19 |
+
#define B5 X5
|
| 20 |
+
#define B6 X6
|
| 21 |
+
#define B7 X7
|
| 22 |
+
|
| 23 |
+
#define ACC0 X8
|
| 24 |
+
#define ACC1 X9
|
| 25 |
+
#define ACCM X10
|
| 26 |
+
|
| 27 |
+
#define T0 X11
|
| 28 |
+
#define T1 X12
|
| 29 |
+
#define T2 X13
|
| 30 |
+
#define POLY X14
|
| 31 |
+
#define BSWAP X15
|
| 32 |
+
|
| 33 |
+
DATA bswapMask<>+0x00(SB)/8, $0x08090a0b0c0d0e0f
|
| 34 |
+
DATA bswapMask<>+0x08(SB)/8, $0x0001020304050607
|
| 35 |
+
|
| 36 |
+
DATA gcmPoly<>+0x00(SB)/8, $0x0000000000000001
|
| 37 |
+
DATA gcmPoly<>+0x08(SB)/8, $0xc200000000000000
|
| 38 |
+
|
| 39 |
+
DATA andMask<>+0x00(SB)/8, $0x00000000000000ff
|
| 40 |
+
DATA andMask<>+0x08(SB)/8, $0x0000000000000000
|
| 41 |
+
DATA andMask<>+0x10(SB)/8, $0x000000000000ffff
|
| 42 |
+
DATA andMask<>+0x18(SB)/8, $0x0000000000000000
|
| 43 |
+
DATA andMask<>+0x20(SB)/8, $0x0000000000ffffff
|
| 44 |
+
DATA andMask<>+0x28(SB)/8, $0x0000000000000000
|
| 45 |
+
DATA andMask<>+0x30(SB)/8, $0x00000000ffffffff
|
| 46 |
+
DATA andMask<>+0x38(SB)/8, $0x0000000000000000
|
| 47 |
+
DATA andMask<>+0x40(SB)/8, $0x000000ffffffffff
|
| 48 |
+
DATA andMask<>+0x48(SB)/8, $0x0000000000000000
|
| 49 |
+
DATA andMask<>+0x50(SB)/8, $0x0000ffffffffffff
|
| 50 |
+
DATA andMask<>+0x58(SB)/8, $0x0000000000000000
|
| 51 |
+
DATA andMask<>+0x60(SB)/8, $0x00ffffffffffffff
|
| 52 |
+
DATA andMask<>+0x68(SB)/8, $0x0000000000000000
|
| 53 |
+
DATA andMask<>+0x70(SB)/8, $0xffffffffffffffff
|
| 54 |
+
DATA andMask<>+0x78(SB)/8, $0x0000000000000000
|
| 55 |
+
DATA andMask<>+0x80(SB)/8, $0xffffffffffffffff
|
| 56 |
+
DATA andMask<>+0x88(SB)/8, $0x00000000000000ff
|
| 57 |
+
DATA andMask<>+0x90(SB)/8, $0xffffffffffffffff
|
| 58 |
+
DATA andMask<>+0x98(SB)/8, $0x000000000000ffff
|
| 59 |
+
DATA andMask<>+0xa0(SB)/8, $0xffffffffffffffff
|
| 60 |
+
DATA andMask<>+0xa8(SB)/8, $0x0000000000ffffff
|
| 61 |
+
DATA andMask<>+0xb0(SB)/8, $0xffffffffffffffff
|
| 62 |
+
DATA andMask<>+0xb8(SB)/8, $0x00000000ffffffff
|
| 63 |
+
DATA andMask<>+0xc0(SB)/8, $0xffffffffffffffff
|
| 64 |
+
DATA andMask<>+0xc8(SB)/8, $0x000000ffffffffff
|
| 65 |
+
DATA andMask<>+0xd0(SB)/8, $0xffffffffffffffff
|
| 66 |
+
DATA andMask<>+0xd8(SB)/8, $0x0000ffffffffffff
|
| 67 |
+
DATA andMask<>+0xe0(SB)/8, $0xffffffffffffffff
|
| 68 |
+
DATA andMask<>+0xe8(SB)/8, $0x00ffffffffffffff
|
| 69 |
+
|
| 70 |
+
GLOBL bswapMask<>(SB), (NOPTR+RODATA), $16
|
| 71 |
+
GLOBL gcmPoly<>(SB), (NOPTR+RODATA), $16
|
| 72 |
+
GLOBL andMask<>(SB), (NOPTR+RODATA), $240
|
| 73 |
+
|
| 74 |
+
// func gcmAesFinish(productTable *[256]byte, tagMask, T *[16]byte, pLen, dLen uint64)
|
| 75 |
+
TEXT ·gcmAesFinish(SB),NOSPLIT,$0
|
| 76 |
+
#define pTbl DI
|
| 77 |
+
#define tMsk SI
|
| 78 |
+
#define tPtr DX
|
| 79 |
+
#define plen AX
|
| 80 |
+
#define dlen CX
|
| 81 |
+
|
| 82 |
+
MOVQ productTable+0(FP), pTbl
|
| 83 |
+
MOVQ tagMask+8(FP), tMsk
|
| 84 |
+
MOVQ T+16(FP), tPtr
|
| 85 |
+
MOVQ pLen+24(FP), plen
|
| 86 |
+
MOVQ dLen+32(FP), dlen
|
| 87 |
+
|
| 88 |
+
MOVOU (tPtr), ACC0
|
| 89 |
+
MOVOU (tMsk), T2
|
| 90 |
+
|
| 91 |
+
MOVOU bswapMask<>(SB), BSWAP
|
| 92 |
+
MOVOU gcmPoly<>(SB), POLY
|
| 93 |
+
|
| 94 |
+
SHLQ $3, plen
|
| 95 |
+
SHLQ $3, dlen
|
| 96 |
+
|
| 97 |
+
MOVQ plen, B0
|
| 98 |
+
PINSRQ $1, dlen, B0
|
| 99 |
+
|
| 100 |
+
PXOR ACC0, B0
|
| 101 |
+
|
| 102 |
+
MOVOU (16*14)(pTbl), ACC0
|
| 103 |
+
MOVOU (16*15)(pTbl), ACCM
|
| 104 |
+
MOVOU ACC0, ACC1
|
| 105 |
+
|
| 106 |
+
PCLMULQDQ $0x00, B0, ACC0
|
| 107 |
+
PCLMULQDQ $0x11, B0, ACC1
|
| 108 |
+
PSHUFD $78, B0, T0
|
| 109 |
+
PXOR B0, T0
|
| 110 |
+
PCLMULQDQ $0x00, T0, ACCM
|
| 111 |
+
|
| 112 |
+
PXOR ACC0, ACCM
|
| 113 |
+
PXOR ACC1, ACCM
|
| 114 |
+
MOVOU ACCM, T0
|
| 115 |
+
PSRLDQ $8, ACCM
|
| 116 |
+
PSLLDQ $8, T0
|
| 117 |
+
PXOR ACCM, ACC1
|
| 118 |
+
PXOR T0, ACC0
|
| 119 |
+
|
| 120 |
+
MOVOU POLY, T0
|
| 121 |
+
PCLMULQDQ $0x01, ACC0, T0
|
| 122 |
+
PSHUFD $78, ACC0, ACC0
|
| 123 |
+
PXOR T0, ACC0
|
| 124 |
+
|
| 125 |
+
MOVOU POLY, T0
|
| 126 |
+
PCLMULQDQ $0x01, ACC0, T0
|
| 127 |
+
PSHUFD $78, ACC0, ACC0
|
| 128 |
+
PXOR T0, ACC0
|
| 129 |
+
|
| 130 |
+
PXOR ACC1, ACC0
|
| 131 |
+
|
| 132 |
+
PSHUFB BSWAP, ACC0
|
| 133 |
+
PXOR T2, ACC0
|
| 134 |
+
MOVOU ACC0, (tPtr)
|
| 135 |
+
|
| 136 |
+
RET
|
| 137 |
+
#undef pTbl
|
| 138 |
+
#undef tMsk
|
| 139 |
+
#undef tPtr
|
| 140 |
+
#undef plen
|
| 141 |
+
#undef dlen
|
| 142 |
+
|
| 143 |
+
// func gcmAesInit(productTable *[256]byte, ks []uint32)
|
| 144 |
+
TEXT ·gcmAesInit(SB),NOSPLIT,$0
|
| 145 |
+
#define dst DI
|
| 146 |
+
#define KS SI
|
| 147 |
+
#define NR DX
|
| 148 |
+
|
| 149 |
+
MOVQ productTable+0(FP), dst
|
| 150 |
+
MOVQ ks_base+8(FP), KS
|
| 151 |
+
MOVQ ks_len+16(FP), NR
|
| 152 |
+
|
| 153 |
+
SHRQ $2, NR
|
| 154 |
+
DECQ NR
|
| 155 |
+
|
| 156 |
+
MOVOU bswapMask<>(SB), BSWAP
|
| 157 |
+
MOVOU gcmPoly<>(SB), POLY
|
| 158 |
+
|
| 159 |
+
// Encrypt block 0, with the AES key to generate the hash key H
|
| 160 |
+
MOVOU (16*0)(KS), B0
|
| 161 |
+
MOVOU (16*1)(KS), T0
|
| 162 |
+
AESENC T0, B0
|
| 163 |
+
MOVOU (16*2)(KS), T0
|
| 164 |
+
AESENC T0, B0
|
| 165 |
+
MOVOU (16*3)(KS), T0
|
| 166 |
+
AESENC T0, B0
|
| 167 |
+
MOVOU (16*4)(KS), T0
|
| 168 |
+
AESENC T0, B0
|
| 169 |
+
MOVOU (16*5)(KS), T0
|
| 170 |
+
AESENC T0, B0
|
| 171 |
+
MOVOU (16*6)(KS), T0
|
| 172 |
+
AESENC T0, B0
|
| 173 |
+
MOVOU (16*7)(KS), T0
|
| 174 |
+
AESENC T0, B0
|
| 175 |
+
MOVOU (16*8)(KS), T0
|
| 176 |
+
AESENC T0, B0
|
| 177 |
+
MOVOU (16*9)(KS), T0
|
| 178 |
+
AESENC T0, B0
|
| 179 |
+
MOVOU (16*10)(KS), T0
|
| 180 |
+
CMPQ NR, $12
|
| 181 |
+
JB initEncLast
|
| 182 |
+
AESENC T0, B0
|
| 183 |
+
MOVOU (16*11)(KS), T0
|
| 184 |
+
AESENC T0, B0
|
| 185 |
+
MOVOU (16*12)(KS), T0
|
| 186 |
+
JE initEncLast
|
| 187 |
+
AESENC T0, B0
|
| 188 |
+
MOVOU (16*13)(KS), T0
|
| 189 |
+
AESENC T0, B0
|
| 190 |
+
MOVOU (16*14)(KS), T0
|
| 191 |
+
initEncLast:
|
| 192 |
+
AESENCLAST T0, B0
|
| 193 |
+
|
| 194 |
+
PSHUFB BSWAP, B0
|
| 195 |
+
// H * 2
|
| 196 |
+
PSHUFD $0xff, B0, T0
|
| 197 |
+
MOVOU B0, T1
|
| 198 |
+
PSRAL $31, T0
|
| 199 |
+
PAND POLY, T0
|
| 200 |
+
PSRLL $31, T1
|
| 201 |
+
PSLLDQ $4, T1
|
| 202 |
+
PSLLL $1, B0
|
| 203 |
+
PXOR T0, B0
|
| 204 |
+
PXOR T1, B0
|
| 205 |
+
// Karatsuba pre-computations
|
| 206 |
+
MOVOU B0, (16*14)(dst)
|
| 207 |
+
PSHUFD $78, B0, B1
|
| 208 |
+
PXOR B0, B1
|
| 209 |
+
MOVOU B1, (16*15)(dst)
|
| 210 |
+
|
| 211 |
+
MOVOU B0, B2
|
| 212 |
+
MOVOU B1, B3
|
| 213 |
+
// Now prepare powers of H and pre-computations for them
|
| 214 |
+
MOVQ $7, AX
|
| 215 |
+
|
| 216 |
+
initLoop:
|
| 217 |
+
MOVOU B2, T0
|
| 218 |
+
MOVOU B2, T1
|
| 219 |
+
MOVOU B3, T2
|
| 220 |
+
PCLMULQDQ $0x00, B0, T0
|
| 221 |
+
PCLMULQDQ $0x11, B0, T1
|
| 222 |
+
PCLMULQDQ $0x00, B1, T2
|
| 223 |
+
|
| 224 |
+
PXOR T0, T2
|
| 225 |
+
PXOR T1, T2
|
| 226 |
+
MOVOU T2, B4
|
| 227 |
+
PSLLDQ $8, B4
|
| 228 |
+
PSRLDQ $8, T2
|
| 229 |
+
PXOR B4, T0
|
| 230 |
+
PXOR T2, T1
|
| 231 |
+
|
| 232 |
+
MOVOU POLY, B2
|
| 233 |
+
PCLMULQDQ $0x01, T0, B2
|
| 234 |
+
PSHUFD $78, T0, T0
|
| 235 |
+
PXOR B2, T0
|
| 236 |
+
MOVOU POLY, B2
|
| 237 |
+
PCLMULQDQ $0x01, T0, B2
|
| 238 |
+
PSHUFD $78, T0, T0
|
| 239 |
+
PXOR T0, B2
|
| 240 |
+
PXOR T1, B2
|
| 241 |
+
|
| 242 |
+
MOVOU B2, (16*12)(dst)
|
| 243 |
+
PSHUFD $78, B2, B3
|
| 244 |
+
PXOR B2, B3
|
| 245 |
+
MOVOU B3, (16*13)(dst)
|
| 246 |
+
|
| 247 |
+
DECQ AX
|
| 248 |
+
LEAQ (-16*2)(dst), dst
|
| 249 |
+
JNE initLoop
|
| 250 |
+
|
| 251 |
+
RET
|
| 252 |
+
#undef NR
|
| 253 |
+
#undef KS
|
| 254 |
+
#undef dst
|
| 255 |
+
|
| 256 |
+
// func gcmAesData(productTable *[256]byte, data []byte, T *[16]byte)
|
| 257 |
+
TEXT ·gcmAesData(SB),NOSPLIT,$0
|
| 258 |
+
#define pTbl DI
|
| 259 |
+
#define aut SI
|
| 260 |
+
#define tPtr CX
|
| 261 |
+
#define autLen DX
|
| 262 |
+
|
| 263 |
+
#define reduceRound(a) MOVOU POLY, T0; PCLMULQDQ $0x01, a, T0; PSHUFD $78, a, a; PXOR T0, a
|
| 264 |
+
#define mulRoundAAD(X ,i) \
|
| 265 |
+
MOVOU (16*(i*2))(pTbl), T1;\
|
| 266 |
+
MOVOU T1, T2;\
|
| 267 |
+
PCLMULQDQ $0x00, X, T1;\
|
| 268 |
+
PXOR T1, ACC0;\
|
| 269 |
+
PCLMULQDQ $0x11, X, T2;\
|
| 270 |
+
PXOR T2, ACC1;\
|
| 271 |
+
PSHUFD $78, X, T1;\
|
| 272 |
+
PXOR T1, X;\
|
| 273 |
+
MOVOU (16*(i*2+1))(pTbl), T1;\
|
| 274 |
+
PCLMULQDQ $0x00, X, T1;\
|
| 275 |
+
PXOR T1, ACCM
|
| 276 |
+
|
| 277 |
+
MOVQ productTable+0(FP), pTbl
|
| 278 |
+
MOVQ data_base+8(FP), aut
|
| 279 |
+
MOVQ data_len+16(FP), autLen
|
| 280 |
+
MOVQ T+32(FP), tPtr
|
| 281 |
+
|
| 282 |
+
PXOR ACC0, ACC0
|
| 283 |
+
MOVOU bswapMask<>(SB), BSWAP
|
| 284 |
+
MOVOU gcmPoly<>(SB), POLY
|
| 285 |
+
|
| 286 |
+
TESTQ autLen, autLen
|
| 287 |
+
JEQ dataBail
|
| 288 |
+
|
| 289 |
+
CMPQ autLen, $13 // optimize the TLS case
|
| 290 |
+
JE dataTLS
|
| 291 |
+
CMPQ autLen, $128
|
| 292 |
+
JB startSinglesLoop
|
| 293 |
+
JMP dataOctaLoop
|
| 294 |
+
|
| 295 |
+
dataTLS:
|
| 296 |
+
MOVOU (16*14)(pTbl), T1
|
| 297 |
+
MOVOU (16*15)(pTbl), T2
|
| 298 |
+
PXOR B0, B0
|
| 299 |
+
MOVQ (aut), B0
|
| 300 |
+
PINSRD $2, 8(aut), B0
|
| 301 |
+
PINSRB $12, 12(aut), B0
|
| 302 |
+
XORQ autLen, autLen
|
| 303 |
+
JMP dataMul
|
| 304 |
+
|
| 305 |
+
dataOctaLoop:
|
| 306 |
+
CMPQ autLen, $128
|
| 307 |
+
JB startSinglesLoop
|
| 308 |
+
SUBQ $128, autLen
|
| 309 |
+
|
| 310 |
+
MOVOU (16*0)(aut), X0
|
| 311 |
+
MOVOU (16*1)(aut), X1
|
| 312 |
+
MOVOU (16*2)(aut), X2
|
| 313 |
+
MOVOU (16*3)(aut), X3
|
| 314 |
+
MOVOU (16*4)(aut), X4
|
| 315 |
+
MOVOU (16*5)(aut), X5
|
| 316 |
+
MOVOU (16*6)(aut), X6
|
| 317 |
+
MOVOU (16*7)(aut), X7
|
| 318 |
+
LEAQ (16*8)(aut), aut
|
| 319 |
+
PSHUFB BSWAP, X0
|
| 320 |
+
PSHUFB BSWAP, X1
|
| 321 |
+
PSHUFB BSWAP, X2
|
| 322 |
+
PSHUFB BSWAP, X3
|
| 323 |
+
PSHUFB BSWAP, X4
|
| 324 |
+
PSHUFB BSWAP, X5
|
| 325 |
+
PSHUFB BSWAP, X6
|
| 326 |
+
PSHUFB BSWAP, X7
|
| 327 |
+
PXOR ACC0, X0
|
| 328 |
+
|
| 329 |
+
MOVOU (16*0)(pTbl), ACC0
|
| 330 |
+
MOVOU (16*1)(pTbl), ACCM
|
| 331 |
+
MOVOU ACC0, ACC1
|
| 332 |
+
PSHUFD $78, X0, T1
|
| 333 |
+
PXOR X0, T1
|
| 334 |
+
PCLMULQDQ $0x00, X0, ACC0
|
| 335 |
+
PCLMULQDQ $0x11, X0, ACC1
|
| 336 |
+
PCLMULQDQ $0x00, T1, ACCM
|
| 337 |
+
|
| 338 |
+
mulRoundAAD(X1, 1)
|
| 339 |
+
mulRoundAAD(X2, 2)
|
| 340 |
+
mulRoundAAD(X3, 3)
|
| 341 |
+
mulRoundAAD(X4, 4)
|
| 342 |
+
mulRoundAAD(X5, 5)
|
| 343 |
+
mulRoundAAD(X6, 6)
|
| 344 |
+
mulRoundAAD(X7, 7)
|
| 345 |
+
|
| 346 |
+
PXOR ACC0, ACCM
|
| 347 |
+
PXOR ACC1, ACCM
|
| 348 |
+
MOVOU ACCM, T0
|
| 349 |
+
PSRLDQ $8, ACCM
|
| 350 |
+
PSLLDQ $8, T0
|
| 351 |
+
PXOR ACCM, ACC1
|
| 352 |
+
PXOR T0, ACC0
|
| 353 |
+
reduceRound(ACC0)
|
| 354 |
+
reduceRound(ACC0)
|
| 355 |
+
PXOR ACC1, ACC0
|
| 356 |
+
JMP dataOctaLoop
|
| 357 |
+
|
| 358 |
+
startSinglesLoop:
|
| 359 |
+
MOVOU (16*14)(pTbl), T1
|
| 360 |
+
MOVOU (16*15)(pTbl), T2
|
| 361 |
+
|
| 362 |
+
dataSinglesLoop:
|
| 363 |
+
|
| 364 |
+
CMPQ autLen, $16
|
| 365 |
+
JB dataEnd
|
| 366 |
+
SUBQ $16, autLen
|
| 367 |
+
|
| 368 |
+
MOVOU (aut), B0
|
| 369 |
+
dataMul:
|
| 370 |
+
PSHUFB BSWAP, B0
|
| 371 |
+
PXOR ACC0, B0
|
| 372 |
+
|
| 373 |
+
MOVOU T1, ACC0
|
| 374 |
+
MOVOU T2, ACCM
|
| 375 |
+
MOVOU T1, ACC1
|
| 376 |
+
|
| 377 |
+
PSHUFD $78, B0, T0
|
| 378 |
+
PXOR B0, T0
|
| 379 |
+
PCLMULQDQ $0x00, B0, ACC0
|
| 380 |
+
PCLMULQDQ $0x11, B0, ACC1
|
| 381 |
+
PCLMULQDQ $0x00, T0, ACCM
|
| 382 |
+
|
| 383 |
+
PXOR ACC0, ACCM
|
| 384 |
+
PXOR ACC1, ACCM
|
| 385 |
+
MOVOU ACCM, T0
|
| 386 |
+
PSRLDQ $8, ACCM
|
| 387 |
+
PSLLDQ $8, T0
|
| 388 |
+
PXOR ACCM, ACC1
|
| 389 |
+
PXOR T0, ACC0
|
| 390 |
+
|
| 391 |
+
MOVOU POLY, T0
|
| 392 |
+
PCLMULQDQ $0x01, ACC0, T0
|
| 393 |
+
PSHUFD $78, ACC0, ACC0
|
| 394 |
+
PXOR T0, ACC0
|
| 395 |
+
|
| 396 |
+
MOVOU POLY, T0
|
| 397 |
+
PCLMULQDQ $0x01, ACC0, T0
|
| 398 |
+
PSHUFD $78, ACC0, ACC0
|
| 399 |
+
PXOR T0, ACC0
|
| 400 |
+
PXOR ACC1, ACC0
|
| 401 |
+
|
| 402 |
+
LEAQ 16(aut), aut
|
| 403 |
+
|
| 404 |
+
JMP dataSinglesLoop
|
| 405 |
+
|
| 406 |
+
dataEnd:
|
| 407 |
+
|
| 408 |
+
TESTQ autLen, autLen
|
| 409 |
+
JEQ dataBail
|
| 410 |
+
|
| 411 |
+
PXOR B0, B0
|
| 412 |
+
LEAQ -1(aut)(autLen*1), aut
|
| 413 |
+
|
| 414 |
+
dataLoadLoop:
|
| 415 |
+
|
| 416 |
+
PSLLDQ $1, B0
|
| 417 |
+
PINSRB $0, (aut), B0
|
| 418 |
+
|
| 419 |
+
LEAQ -1(aut), aut
|
| 420 |
+
DECQ autLen
|
| 421 |
+
JNE dataLoadLoop
|
| 422 |
+
|
| 423 |
+
JMP dataMul
|
| 424 |
+
|
| 425 |
+
dataBail:
|
| 426 |
+
MOVOU ACC0, (tPtr)
|
| 427 |
+
RET
|
| 428 |
+
#undef pTbl
|
| 429 |
+
#undef aut
|
| 430 |
+
#undef tPtr
|
| 431 |
+
#undef autLen
|
| 432 |
+
|
| 433 |
+
// func gcmAesEnc(productTable *[256]byte, dst, src []byte, ctr, T *[16]byte, ks []uint32)
|
| 434 |
+
TEXT ·gcmAesEnc(SB),0,$256-96
|
| 435 |
+
#define pTbl DI
|
| 436 |
+
#define ctx DX
|
| 437 |
+
#define ctrPtr CX
|
| 438 |
+
#define ptx SI
|
| 439 |
+
#define ks AX
|
| 440 |
+
#define tPtr R8
|
| 441 |
+
#define ptxLen R9
|
| 442 |
+
#define aluCTR R10
|
| 443 |
+
#define aluTMP R11
|
| 444 |
+
#define aluK R12
|
| 445 |
+
#define NR R13
|
| 446 |
+
|
| 447 |
+
#define increment(i) ADDL $1, aluCTR; MOVL aluCTR, aluTMP; XORL aluK, aluTMP; BSWAPL aluTMP; MOVL aluTMP, (3*4 + 8*16 + i*16)(SP)
|
| 448 |
+
#define aesRnd(k) AESENC k, B0; AESENC k, B1; AESENC k, B2; AESENC k, B3; AESENC k, B4; AESENC k, B5; AESENC k, B6; AESENC k, B7
|
| 449 |
+
#define aesRound(i) MOVOU (16*i)(ks), T0;AESENC T0, B0; AESENC T0, B1; AESENC T0, B2; AESENC T0, B3; AESENC T0, B4; AESENC T0, B5; AESENC T0, B6; AESENC T0, B7
|
| 450 |
+
#define aesRndLast(k) AESENCLAST k, B0; AESENCLAST k, B1; AESENCLAST k, B2; AESENCLAST k, B3; AESENCLAST k, B4; AESENCLAST k, B5; AESENCLAST k, B6; AESENCLAST k, B7
|
| 451 |
+
#define combinedRound(i) \
|
| 452 |
+
MOVOU (16*i)(ks), T0;\
|
| 453 |
+
AESENC T0, B0;\
|
| 454 |
+
AESENC T0, B1;\
|
| 455 |
+
AESENC T0, B2;\
|
| 456 |
+
AESENC T0, B3;\
|
| 457 |
+
MOVOU (16*(i*2))(pTbl), T1;\
|
| 458 |
+
MOVOU T1, T2;\
|
| 459 |
+
AESENC T0, B4;\
|
| 460 |
+
AESENC T0, B5;\
|
| 461 |
+
AESENC T0, B6;\
|
| 462 |
+
AESENC T0, B7;\
|
| 463 |
+
MOVOU (16*i)(SP), T0;\
|
| 464 |
+
PCLMULQDQ $0x00, T0, T1;\
|
| 465 |
+
PXOR T1, ACC0;\
|
| 466 |
+
PSHUFD $78, T0, T1;\
|
| 467 |
+
PCLMULQDQ $0x11, T0, T2;\
|
| 468 |
+
PXOR T1, T0;\
|
| 469 |
+
PXOR T2, ACC1;\
|
| 470 |
+
MOVOU (16*(i*2+1))(pTbl), T2;\
|
| 471 |
+
PCLMULQDQ $0x00, T2, T0;\
|
| 472 |
+
PXOR T0, ACCM
|
| 473 |
+
#define mulRound(i) \
|
| 474 |
+
MOVOU (16*i)(SP), T0;\
|
| 475 |
+
MOVOU (16*(i*2))(pTbl), T1;\
|
| 476 |
+
MOVOU T1, T2;\
|
| 477 |
+
PCLMULQDQ $0x00, T0, T1;\
|
| 478 |
+
PXOR T1, ACC0;\
|
| 479 |
+
PCLMULQDQ $0x11, T0, T2;\
|
| 480 |
+
PXOR T2, ACC1;\
|
| 481 |
+
PSHUFD $78, T0, T1;\
|
| 482 |
+
PXOR T1, T0;\
|
| 483 |
+
MOVOU (16*(i*2+1))(pTbl), T1;\
|
| 484 |
+
PCLMULQDQ $0x00, T0, T1;\
|
| 485 |
+
PXOR T1, ACCM
|
| 486 |
+
|
| 487 |
+
MOVQ productTable+0(FP), pTbl
|
| 488 |
+
MOVQ dst+8(FP), ctx
|
| 489 |
+
MOVQ src_base+32(FP), ptx
|
| 490 |
+
MOVQ src_len+40(FP), ptxLen
|
| 491 |
+
MOVQ ctr+56(FP), ctrPtr
|
| 492 |
+
MOVQ T+64(FP), tPtr
|
| 493 |
+
MOVQ ks_base+72(FP), ks
|
| 494 |
+
MOVQ ks_len+80(FP), NR
|
| 495 |
+
|
| 496 |
+
SHRQ $2, NR
|
| 497 |
+
DECQ NR
|
| 498 |
+
|
| 499 |
+
MOVOU bswapMask<>(SB), BSWAP
|
| 500 |
+
MOVOU gcmPoly<>(SB), POLY
|
| 501 |
+
|
| 502 |
+
MOVOU (tPtr), ACC0
|
| 503 |
+
PXOR ACC1, ACC1
|
| 504 |
+
PXOR ACCM, ACCM
|
| 505 |
+
MOVOU (ctrPtr), B0
|
| 506 |
+
MOVL (3*4)(ctrPtr), aluCTR
|
| 507 |
+
MOVOU (ks), T0
|
| 508 |
+
MOVL (3*4)(ks), aluK
|
| 509 |
+
BSWAPL aluCTR
|
| 510 |
+
BSWAPL aluK
|
| 511 |
+
|
| 512 |
+
PXOR B0, T0
|
| 513 |
+
MOVOU T0, (8*16 + 0*16)(SP)
|
| 514 |
+
increment(0)
|
| 515 |
+
|
| 516 |
+
CMPQ ptxLen, $128
|
| 517 |
+
JB gcmAesEncSingles
|
| 518 |
+
SUBQ $128, ptxLen
|
| 519 |
+
|
| 520 |
+
// We have at least 8 blocks to encrypt, prepare the rest of the counters
|
| 521 |
+
MOVOU T0, (8*16 + 1*16)(SP)
|
| 522 |
+
increment(1)
|
| 523 |
+
MOVOU T0, (8*16 + 2*16)(SP)
|
| 524 |
+
increment(2)
|
| 525 |
+
MOVOU T0, (8*16 + 3*16)(SP)
|
| 526 |
+
increment(3)
|
| 527 |
+
MOVOU T0, (8*16 + 4*16)(SP)
|
| 528 |
+
increment(4)
|
| 529 |
+
MOVOU T0, (8*16 + 5*16)(SP)
|
| 530 |
+
increment(5)
|
| 531 |
+
MOVOU T0, (8*16 + 6*16)(SP)
|
| 532 |
+
increment(6)
|
| 533 |
+
MOVOU T0, (8*16 + 7*16)(SP)
|
| 534 |
+
increment(7)
|
| 535 |
+
|
| 536 |
+
MOVOU (8*16 + 0*16)(SP), B0
|
| 537 |
+
MOVOU (8*16 + 1*16)(SP), B1
|
| 538 |
+
MOVOU (8*16 + 2*16)(SP), B2
|
| 539 |
+
MOVOU (8*16 + 3*16)(SP), B3
|
| 540 |
+
MOVOU (8*16 + 4*16)(SP), B4
|
| 541 |
+
MOVOU (8*16 + 5*16)(SP), B5
|
| 542 |
+
MOVOU (8*16 + 6*16)(SP), B6
|
| 543 |
+
MOVOU (8*16 + 7*16)(SP), B7
|
| 544 |
+
|
| 545 |
+
aesRound(1)
|
| 546 |
+
increment(0)
|
| 547 |
+
aesRound(2)
|
| 548 |
+
increment(1)
|
| 549 |
+
aesRound(3)
|
| 550 |
+
increment(2)
|
| 551 |
+
aesRound(4)
|
| 552 |
+
increment(3)
|
| 553 |
+
aesRound(5)
|
| 554 |
+
increment(4)
|
| 555 |
+
aesRound(6)
|
| 556 |
+
increment(5)
|
| 557 |
+
aesRound(7)
|
| 558 |
+
increment(6)
|
| 559 |
+
aesRound(8)
|
| 560 |
+
increment(7)
|
| 561 |
+
aesRound(9)
|
| 562 |
+
MOVOU (16*10)(ks), T0
|
| 563 |
+
CMPQ NR, $12
|
| 564 |
+
JB encLast1
|
| 565 |
+
aesRnd(T0)
|
| 566 |
+
aesRound(11)
|
| 567 |
+
MOVOU (16*12)(ks), T0
|
| 568 |
+
JE encLast1
|
| 569 |
+
aesRnd(T0)
|
| 570 |
+
aesRound(13)
|
| 571 |
+
MOVOU (16*14)(ks), T0
|
| 572 |
+
encLast1:
|
| 573 |
+
aesRndLast(T0)
|
| 574 |
+
|
| 575 |
+
MOVOU (16*0)(ptx), T0
|
| 576 |
+
PXOR T0, B0
|
| 577 |
+
MOVOU (16*1)(ptx), T0
|
| 578 |
+
PXOR T0, B1
|
| 579 |
+
MOVOU (16*2)(ptx), T0
|
| 580 |
+
PXOR T0, B2
|
| 581 |
+
MOVOU (16*3)(ptx), T0
|
| 582 |
+
PXOR T0, B3
|
| 583 |
+
MOVOU (16*4)(ptx), T0
|
| 584 |
+
PXOR T0, B4
|
| 585 |
+
MOVOU (16*5)(ptx), T0
|
| 586 |
+
PXOR T0, B5
|
| 587 |
+
MOVOU (16*6)(ptx), T0
|
| 588 |
+
PXOR T0, B6
|
| 589 |
+
MOVOU (16*7)(ptx), T0
|
| 590 |
+
PXOR T0, B7
|
| 591 |
+
|
| 592 |
+
MOVOU B0, (16*0)(ctx)
|
| 593 |
+
PSHUFB BSWAP, B0
|
| 594 |
+
PXOR ACC0, B0
|
| 595 |
+
MOVOU B1, (16*1)(ctx)
|
| 596 |
+
PSHUFB BSWAP, B1
|
| 597 |
+
MOVOU B2, (16*2)(ctx)
|
| 598 |
+
PSHUFB BSWAP, B2
|
| 599 |
+
MOVOU B3, (16*3)(ctx)
|
| 600 |
+
PSHUFB BSWAP, B3
|
| 601 |
+
MOVOU B4, (16*4)(ctx)
|
| 602 |
+
PSHUFB BSWAP, B4
|
| 603 |
+
MOVOU B5, (16*5)(ctx)
|
| 604 |
+
PSHUFB BSWAP, B5
|
| 605 |
+
MOVOU B6, (16*6)(ctx)
|
| 606 |
+
PSHUFB BSWAP, B6
|
| 607 |
+
MOVOU B7, (16*7)(ctx)
|
| 608 |
+
PSHUFB BSWAP, B7
|
| 609 |
+
|
| 610 |
+
MOVOU B0, (16*0)(SP)
|
| 611 |
+
MOVOU B1, (16*1)(SP)
|
| 612 |
+
MOVOU B2, (16*2)(SP)
|
| 613 |
+
MOVOU B3, (16*3)(SP)
|
| 614 |
+
MOVOU B4, (16*4)(SP)
|
| 615 |
+
MOVOU B5, (16*5)(SP)
|
| 616 |
+
MOVOU B6, (16*6)(SP)
|
| 617 |
+
MOVOU B7, (16*7)(SP)
|
| 618 |
+
|
| 619 |
+
LEAQ 128(ptx), ptx
|
| 620 |
+
LEAQ 128(ctx), ctx
|
| 621 |
+
|
| 622 |
+
gcmAesEncOctetsLoop:
|
| 623 |
+
|
| 624 |
+
CMPQ ptxLen, $128
|
| 625 |
+
JB gcmAesEncOctetsEnd
|
| 626 |
+
SUBQ $128, ptxLen
|
| 627 |
+
|
| 628 |
+
MOVOU (8*16 + 0*16)(SP), B0
|
| 629 |
+
MOVOU (8*16 + 1*16)(SP), B1
|
| 630 |
+
MOVOU (8*16 + 2*16)(SP), B2
|
| 631 |
+
MOVOU (8*16 + 3*16)(SP), B3
|
| 632 |
+
MOVOU (8*16 + 4*16)(SP), B4
|
| 633 |
+
MOVOU (8*16 + 5*16)(SP), B5
|
| 634 |
+
MOVOU (8*16 + 6*16)(SP), B6
|
| 635 |
+
MOVOU (8*16 + 7*16)(SP), B7
|
| 636 |
+
|
| 637 |
+
MOVOU (16*0)(SP), T0
|
| 638 |
+
PSHUFD $78, T0, T1
|
| 639 |
+
PXOR T0, T1
|
| 640 |
+
|
| 641 |
+
MOVOU (16*0)(pTbl), ACC0
|
| 642 |
+
MOVOU (16*1)(pTbl), ACCM
|
| 643 |
+
MOVOU ACC0, ACC1
|
| 644 |
+
|
| 645 |
+
PCLMULQDQ $0x00, T1, ACCM
|
| 646 |
+
PCLMULQDQ $0x00, T0, ACC0
|
| 647 |
+
PCLMULQDQ $0x11, T0, ACC1
|
| 648 |
+
|
| 649 |
+
combinedRound(1)
|
| 650 |
+
increment(0)
|
| 651 |
+
combinedRound(2)
|
| 652 |
+
increment(1)
|
| 653 |
+
combinedRound(3)
|
| 654 |
+
increment(2)
|
| 655 |
+
combinedRound(4)
|
| 656 |
+
increment(3)
|
| 657 |
+
combinedRound(5)
|
| 658 |
+
increment(4)
|
| 659 |
+
combinedRound(6)
|
| 660 |
+
increment(5)
|
| 661 |
+
combinedRound(7)
|
| 662 |
+
increment(6)
|
| 663 |
+
|
| 664 |
+
aesRound(8)
|
| 665 |
+
increment(7)
|
| 666 |
+
|
| 667 |
+
PXOR ACC0, ACCM
|
| 668 |
+
PXOR ACC1, ACCM
|
| 669 |
+
MOVOU ACCM, T0
|
| 670 |
+
PSRLDQ $8, ACCM
|
| 671 |
+
PSLLDQ $8, T0
|
| 672 |
+
PXOR ACCM, ACC1
|
| 673 |
+
PXOR T0, ACC0
|
| 674 |
+
|
| 675 |
+
reduceRound(ACC0)
|
| 676 |
+
aesRound(9)
|
| 677 |
+
|
| 678 |
+
reduceRound(ACC0)
|
| 679 |
+
PXOR ACC1, ACC0
|
| 680 |
+
|
| 681 |
+
MOVOU (16*10)(ks), T0
|
| 682 |
+
CMPQ NR, $12
|
| 683 |
+
JB encLast2
|
| 684 |
+
aesRnd(T0)
|
| 685 |
+
aesRound(11)
|
| 686 |
+
MOVOU (16*12)(ks), T0
|
| 687 |
+
JE encLast2
|
| 688 |
+
aesRnd(T0)
|
| 689 |
+
aesRound(13)
|
| 690 |
+
MOVOU (16*14)(ks), T0
|
| 691 |
+
encLast2:
|
| 692 |
+
aesRndLast(T0)
|
| 693 |
+
|
| 694 |
+
MOVOU (16*0)(ptx), T0
|
| 695 |
+
PXOR T0, B0
|
| 696 |
+
MOVOU (16*1)(ptx), T0
|
| 697 |
+
PXOR T0, B1
|
| 698 |
+
MOVOU (16*2)(ptx), T0
|
| 699 |
+
PXOR T0, B2
|
| 700 |
+
MOVOU (16*3)(ptx), T0
|
| 701 |
+
PXOR T0, B3
|
| 702 |
+
MOVOU (16*4)(ptx), T0
|
| 703 |
+
PXOR T0, B4
|
| 704 |
+
MOVOU (16*5)(ptx), T0
|
| 705 |
+
PXOR T0, B5
|
| 706 |
+
MOVOU (16*6)(ptx), T0
|
| 707 |
+
PXOR T0, B6
|
| 708 |
+
MOVOU (16*7)(ptx), T0
|
| 709 |
+
PXOR T0, B7
|
| 710 |
+
|
| 711 |
+
MOVOU B0, (16*0)(ctx)
|
| 712 |
+
PSHUFB BSWAP, B0
|
| 713 |
+
PXOR ACC0, B0
|
| 714 |
+
MOVOU B1, (16*1)(ctx)
|
| 715 |
+
PSHUFB BSWAP, B1
|
| 716 |
+
MOVOU B2, (16*2)(ctx)
|
| 717 |
+
PSHUFB BSWAP, B2
|
| 718 |
+
MOVOU B3, (16*3)(ctx)
|
| 719 |
+
PSHUFB BSWAP, B3
|
| 720 |
+
MOVOU B4, (16*4)(ctx)
|
| 721 |
+
PSHUFB BSWAP, B4
|
| 722 |
+
MOVOU B5, (16*5)(ctx)
|
| 723 |
+
PSHUFB BSWAP, B5
|
| 724 |
+
MOVOU B6, (16*6)(ctx)
|
| 725 |
+
PSHUFB BSWAP, B6
|
| 726 |
+
MOVOU B7, (16*7)(ctx)
|
| 727 |
+
PSHUFB BSWAP, B7
|
| 728 |
+
|
| 729 |
+
MOVOU B0, (16*0)(SP)
|
| 730 |
+
MOVOU B1, (16*1)(SP)
|
| 731 |
+
MOVOU B2, (16*2)(SP)
|
| 732 |
+
MOVOU B3, (16*3)(SP)
|
| 733 |
+
MOVOU B4, (16*4)(SP)
|
| 734 |
+
MOVOU B5, (16*5)(SP)
|
| 735 |
+
MOVOU B6, (16*6)(SP)
|
| 736 |
+
MOVOU B7, (16*7)(SP)
|
| 737 |
+
|
| 738 |
+
LEAQ 128(ptx), ptx
|
| 739 |
+
LEAQ 128(ctx), ctx
|
| 740 |
+
|
| 741 |
+
JMP gcmAesEncOctetsLoop
|
| 742 |
+
|
| 743 |
+
gcmAesEncOctetsEnd:
|
| 744 |
+
|
| 745 |
+
MOVOU (16*0)(SP), T0
|
| 746 |
+
MOVOU (16*0)(pTbl), ACC0
|
| 747 |
+
MOVOU (16*1)(pTbl), ACCM
|
| 748 |
+
MOVOU ACC0, ACC1
|
| 749 |
+
PSHUFD $78, T0, T1
|
| 750 |
+
PXOR T0, T1
|
| 751 |
+
PCLMULQDQ $0x00, T0, ACC0
|
| 752 |
+
PCLMULQDQ $0x11, T0, ACC1
|
| 753 |
+
PCLMULQDQ $0x00, T1, ACCM
|
| 754 |
+
|
| 755 |
+
mulRound(1)
|
| 756 |
+
mulRound(2)
|
| 757 |
+
mulRound(3)
|
| 758 |
+
mulRound(4)
|
| 759 |
+
mulRound(5)
|
| 760 |
+
mulRound(6)
|
| 761 |
+
mulRound(7)
|
| 762 |
+
|
| 763 |
+
PXOR ACC0, ACCM
|
| 764 |
+
PXOR ACC1, ACCM
|
| 765 |
+
MOVOU ACCM, T0
|
| 766 |
+
PSRLDQ $8, ACCM
|
| 767 |
+
PSLLDQ $8, T0
|
| 768 |
+
PXOR ACCM, ACC1
|
| 769 |
+
PXOR T0, ACC0
|
| 770 |
+
|
| 771 |
+
reduceRound(ACC0)
|
| 772 |
+
reduceRound(ACC0)
|
| 773 |
+
PXOR ACC1, ACC0
|
| 774 |
+
|
| 775 |
+
TESTQ ptxLen, ptxLen
|
| 776 |
+
JE gcmAesEncDone
|
| 777 |
+
|
| 778 |
+
SUBQ $7, aluCTR
|
| 779 |
+
|
| 780 |
+
gcmAesEncSingles:
|
| 781 |
+
|
| 782 |
+
MOVOU (16*1)(ks), B1
|
| 783 |
+
MOVOU (16*2)(ks), B2
|
| 784 |
+
MOVOU (16*3)(ks), B3
|
| 785 |
+
MOVOU (16*4)(ks), B4
|
| 786 |
+
MOVOU (16*5)(ks), B5
|
| 787 |
+
MOVOU (16*6)(ks), B6
|
| 788 |
+
MOVOU (16*7)(ks), B7
|
| 789 |
+
|
| 790 |
+
MOVOU (16*14)(pTbl), T2
|
| 791 |
+
|
| 792 |
+
gcmAesEncSinglesLoop:
|
| 793 |
+
|
| 794 |
+
CMPQ ptxLen, $16
|
| 795 |
+
JB gcmAesEncTail
|
| 796 |
+
SUBQ $16, ptxLen
|
| 797 |
+
|
| 798 |
+
MOVOU (8*16 + 0*16)(SP), B0
|
| 799 |
+
increment(0)
|
| 800 |
+
|
| 801 |
+
AESENC B1, B0
|
| 802 |
+
AESENC B2, B0
|
| 803 |
+
AESENC B3, B0
|
| 804 |
+
AESENC B4, B0
|
| 805 |
+
AESENC B5, B0
|
| 806 |
+
AESENC B6, B0
|
| 807 |
+
AESENC B7, B0
|
| 808 |
+
MOVOU (16*8)(ks), T0
|
| 809 |
+
AESENC T0, B0
|
| 810 |
+
MOVOU (16*9)(ks), T0
|
| 811 |
+
AESENC T0, B0
|
| 812 |
+
MOVOU (16*10)(ks), T0
|
| 813 |
+
CMPQ NR, $12
|
| 814 |
+
JB encLast3
|
| 815 |
+
AESENC T0, B0
|
| 816 |
+
MOVOU (16*11)(ks), T0
|
| 817 |
+
AESENC T0, B0
|
| 818 |
+
MOVOU (16*12)(ks), T0
|
| 819 |
+
JE encLast3
|
| 820 |
+
AESENC T0, B0
|
| 821 |
+
MOVOU (16*13)(ks), T0
|
| 822 |
+
AESENC T0, B0
|
| 823 |
+
MOVOU (16*14)(ks), T0
|
| 824 |
+
encLast3:
|
| 825 |
+
AESENCLAST T0, B0
|
| 826 |
+
|
| 827 |
+
MOVOU (ptx), T0
|
| 828 |
+
PXOR T0, B0
|
| 829 |
+
MOVOU B0, (ctx)
|
| 830 |
+
|
| 831 |
+
PSHUFB BSWAP, B0
|
| 832 |
+
PXOR ACC0, B0
|
| 833 |
+
|
| 834 |
+
MOVOU T2, ACC0
|
| 835 |
+
MOVOU T2, ACC1
|
| 836 |
+
MOVOU (16*15)(pTbl), ACCM
|
| 837 |
+
|
| 838 |
+
PSHUFD $78, B0, T0
|
| 839 |
+
PXOR B0, T0
|
| 840 |
+
PCLMULQDQ $0x00, B0, ACC0
|
| 841 |
+
PCLMULQDQ $0x11, B0, ACC1
|
| 842 |
+
PCLMULQDQ $0x00, T0, ACCM
|
| 843 |
+
|
| 844 |
+
PXOR ACC0, ACCM
|
| 845 |
+
PXOR ACC1, ACCM
|
| 846 |
+
MOVOU ACCM, T0
|
| 847 |
+
PSRLDQ $8, ACCM
|
| 848 |
+
PSLLDQ $8, T0
|
| 849 |
+
PXOR ACCM, ACC1
|
| 850 |
+
PXOR T0, ACC0
|
| 851 |
+
|
| 852 |
+
reduceRound(ACC0)
|
| 853 |
+
reduceRound(ACC0)
|
| 854 |
+
PXOR ACC1, ACC0
|
| 855 |
+
|
| 856 |
+
LEAQ (16*1)(ptx), ptx
|
| 857 |
+
LEAQ (16*1)(ctx), ctx
|
| 858 |
+
|
| 859 |
+
JMP gcmAesEncSinglesLoop
|
| 860 |
+
|
| 861 |
+
gcmAesEncTail:
|
| 862 |
+
TESTQ ptxLen, ptxLen
|
| 863 |
+
JE gcmAesEncDone
|
| 864 |
+
|
| 865 |
+
MOVOU (8*16 + 0*16)(SP), B0
|
| 866 |
+
AESENC B1, B0
|
| 867 |
+
AESENC B2, B0
|
| 868 |
+
AESENC B3, B0
|
| 869 |
+
AESENC B4, B0
|
| 870 |
+
AESENC B5, B0
|
| 871 |
+
AESENC B6, B0
|
| 872 |
+
AESENC B7, B0
|
| 873 |
+
MOVOU (16*8)(ks), T0
|
| 874 |
+
AESENC T0, B0
|
| 875 |
+
MOVOU (16*9)(ks), T0
|
| 876 |
+
AESENC T0, B0
|
| 877 |
+
MOVOU (16*10)(ks), T0
|
| 878 |
+
CMPQ NR, $12
|
| 879 |
+
JB encLast4
|
| 880 |
+
AESENC T0, B0
|
| 881 |
+
MOVOU (16*11)(ks), T0
|
| 882 |
+
AESENC T0, B0
|
| 883 |
+
MOVOU (16*12)(ks), T0
|
| 884 |
+
JE encLast4
|
| 885 |
+
AESENC T0, B0
|
| 886 |
+
MOVOU (16*13)(ks), T0
|
| 887 |
+
AESENC T0, B0
|
| 888 |
+
MOVOU (16*14)(ks), T0
|
| 889 |
+
encLast4:
|
| 890 |
+
AESENCLAST T0, B0
|
| 891 |
+
MOVOU B0, T0
|
| 892 |
+
|
| 893 |
+
LEAQ -1(ptx)(ptxLen*1), ptx
|
| 894 |
+
|
| 895 |
+
MOVQ ptxLen, aluTMP
|
| 896 |
+
SHLQ $4, aluTMP
|
| 897 |
+
|
| 898 |
+
LEAQ andMask<>(SB), aluCTR
|
| 899 |
+
MOVOU -16(aluCTR)(aluTMP*1), T1
|
| 900 |
+
|
| 901 |
+
PXOR B0, B0
|
| 902 |
+
ptxLoadLoop:
|
| 903 |
+
PSLLDQ $1, B0
|
| 904 |
+
PINSRB $0, (ptx), B0
|
| 905 |
+
LEAQ -1(ptx), ptx
|
| 906 |
+
DECQ ptxLen
|
| 907 |
+
JNE ptxLoadLoop
|
| 908 |
+
|
| 909 |
+
PXOR T0, B0
|
| 910 |
+
PAND T1, B0
|
| 911 |
+
MOVOU B0, (ctx) // I assume there is always space, due to TAG in the end of the CT
|
| 912 |
+
|
| 913 |
+
PSHUFB BSWAP, B0
|
| 914 |
+
PXOR ACC0, B0
|
| 915 |
+
|
| 916 |
+
MOVOU T2, ACC0
|
| 917 |
+
MOVOU T2, ACC1
|
| 918 |
+
MOVOU (16*15)(pTbl), ACCM
|
| 919 |
+
|
| 920 |
+
PSHUFD $78, B0, T0
|
| 921 |
+
PXOR B0, T0
|
| 922 |
+
PCLMULQDQ $0x00, B0, ACC0
|
| 923 |
+
PCLMULQDQ $0x11, B0, ACC1
|
| 924 |
+
PCLMULQDQ $0x00, T0, ACCM
|
| 925 |
+
|
| 926 |
+
PXOR ACC0, ACCM
|
| 927 |
+
PXOR ACC1, ACCM
|
| 928 |
+
MOVOU ACCM, T0
|
| 929 |
+
PSRLDQ $8, ACCM
|
| 930 |
+
PSLLDQ $8, T0
|
| 931 |
+
PXOR ACCM, ACC1
|
| 932 |
+
PXOR T0, ACC0
|
| 933 |
+
|
| 934 |
+
reduceRound(ACC0)
|
| 935 |
+
reduceRound(ACC0)
|
| 936 |
+
PXOR ACC1, ACC0
|
| 937 |
+
|
| 938 |
+
gcmAesEncDone:
|
| 939 |
+
MOVOU ACC0, (tPtr)
|
| 940 |
+
RET
|
| 941 |
+
#undef increment
|
| 942 |
+
|
| 943 |
+
// func gcmAesDec(productTable *[256]byte, dst, src []byte, ctr, T *[16]byte, ks []uint32)
|
| 944 |
+
TEXT ·gcmAesDec(SB),0,$128-96
|
| 945 |
+
#define increment(i) ADDL $1, aluCTR; MOVL aluCTR, aluTMP; XORL aluK, aluTMP; BSWAPL aluTMP; MOVL aluTMP, (3*4 + i*16)(SP)
|
| 946 |
+
#define combinedDecRound(i) \
|
| 947 |
+
MOVOU (16*i)(ks), T0;\
|
| 948 |
+
AESENC T0, B0;\
|
| 949 |
+
AESENC T0, B1;\
|
| 950 |
+
AESENC T0, B2;\
|
| 951 |
+
AESENC T0, B3;\
|
| 952 |
+
MOVOU (16*(i*2))(pTbl), T1;\
|
| 953 |
+
MOVOU T1, T2;\
|
| 954 |
+
AESENC T0, B4;\
|
| 955 |
+
AESENC T0, B5;\
|
| 956 |
+
AESENC T0, B6;\
|
| 957 |
+
AESENC T0, B7;\
|
| 958 |
+
MOVOU (16*i)(ctx), T0;\
|
| 959 |
+
PSHUFB BSWAP, T0;\
|
| 960 |
+
PCLMULQDQ $0x00, T0, T1;\
|
| 961 |
+
PXOR T1, ACC0;\
|
| 962 |
+
PSHUFD $78, T0, T1;\
|
| 963 |
+
PCLMULQDQ $0x11, T0, T2;\
|
| 964 |
+
PXOR T1, T0;\
|
| 965 |
+
PXOR T2, ACC1;\
|
| 966 |
+
MOVOU (16*(i*2+1))(pTbl), T2;\
|
| 967 |
+
PCLMULQDQ $0x00, T2, T0;\
|
| 968 |
+
PXOR T0, ACCM
|
| 969 |
+
|
| 970 |
+
MOVQ productTable+0(FP), pTbl
|
| 971 |
+
MOVQ dst+8(FP), ptx
|
| 972 |
+
MOVQ src_base+32(FP), ctx
|
| 973 |
+
MOVQ src_len+40(FP), ptxLen
|
| 974 |
+
MOVQ ctr+56(FP), ctrPtr
|
| 975 |
+
MOVQ T+64(FP), tPtr
|
| 976 |
+
MOVQ ks_base+72(FP), ks
|
| 977 |
+
MOVQ ks_len+80(FP), NR
|
| 978 |
+
|
| 979 |
+
SHRQ $2, NR
|
| 980 |
+
DECQ NR
|
| 981 |
+
|
| 982 |
+
MOVOU bswapMask<>(SB), BSWAP
|
| 983 |
+
MOVOU gcmPoly<>(SB), POLY
|
| 984 |
+
|
| 985 |
+
MOVOU (tPtr), ACC0
|
| 986 |
+
PXOR ACC1, ACC1
|
| 987 |
+
PXOR ACCM, ACCM
|
| 988 |
+
MOVOU (ctrPtr), B0
|
| 989 |
+
MOVL (3*4)(ctrPtr), aluCTR
|
| 990 |
+
MOVOU (ks), T0
|
| 991 |
+
MOVL (3*4)(ks), aluK
|
| 992 |
+
BSWAPL aluCTR
|
| 993 |
+
BSWAPL aluK
|
| 994 |
+
|
| 995 |
+
PXOR B0, T0
|
| 996 |
+
MOVOU T0, (0*16)(SP)
|
| 997 |
+
increment(0)
|
| 998 |
+
|
| 999 |
+
CMPQ ptxLen, $128
|
| 1000 |
+
JB gcmAesDecSingles
|
| 1001 |
+
|
| 1002 |
+
MOVOU T0, (1*16)(SP)
|
| 1003 |
+
increment(1)
|
| 1004 |
+
MOVOU T0, (2*16)(SP)
|
| 1005 |
+
increment(2)
|
| 1006 |
+
MOVOU T0, (3*16)(SP)
|
| 1007 |
+
increment(3)
|
| 1008 |
+
MOVOU T0, (4*16)(SP)
|
| 1009 |
+
increment(4)
|
| 1010 |
+
MOVOU T0, (5*16)(SP)
|
| 1011 |
+
increment(5)
|
| 1012 |
+
MOVOU T0, (6*16)(SP)
|
| 1013 |
+
increment(6)
|
| 1014 |
+
MOVOU T0, (7*16)(SP)
|
| 1015 |
+
increment(7)
|
| 1016 |
+
|
| 1017 |
+
gcmAesDecOctetsLoop:
|
| 1018 |
+
|
| 1019 |
+
CMPQ ptxLen, $128
|
| 1020 |
+
JB gcmAesDecEndOctets
|
| 1021 |
+
SUBQ $128, ptxLen
|
| 1022 |
+
|
| 1023 |
+
MOVOU (0*16)(SP), B0
|
| 1024 |
+
MOVOU (1*16)(SP), B1
|
| 1025 |
+
MOVOU (2*16)(SP), B2
|
| 1026 |
+
MOVOU (3*16)(SP), B3
|
| 1027 |
+
MOVOU (4*16)(SP), B4
|
| 1028 |
+
MOVOU (5*16)(SP), B5
|
| 1029 |
+
MOVOU (6*16)(SP), B6
|
| 1030 |
+
MOVOU (7*16)(SP), B7
|
| 1031 |
+
|
| 1032 |
+
MOVOU (16*0)(ctx), T0
|
| 1033 |
+
PSHUFB BSWAP, T0
|
| 1034 |
+
PXOR ACC0, T0
|
| 1035 |
+
PSHUFD $78, T0, T1
|
| 1036 |
+
PXOR T0, T1
|
| 1037 |
+
|
| 1038 |
+
MOVOU (16*0)(pTbl), ACC0
|
| 1039 |
+
MOVOU (16*1)(pTbl), ACCM
|
| 1040 |
+
MOVOU ACC0, ACC1
|
| 1041 |
+
|
| 1042 |
+
PCLMULQDQ $0x00, T1, ACCM
|
| 1043 |
+
PCLMULQDQ $0x00, T0, ACC0
|
| 1044 |
+
PCLMULQDQ $0x11, T0, ACC1
|
| 1045 |
+
|
| 1046 |
+
combinedDecRound(1)
|
| 1047 |
+
increment(0)
|
| 1048 |
+
combinedDecRound(2)
|
| 1049 |
+
increment(1)
|
| 1050 |
+
combinedDecRound(3)
|
| 1051 |
+
increment(2)
|
| 1052 |
+
combinedDecRound(4)
|
| 1053 |
+
increment(3)
|
| 1054 |
+
combinedDecRound(5)
|
| 1055 |
+
increment(4)
|
| 1056 |
+
combinedDecRound(6)
|
| 1057 |
+
increment(5)
|
| 1058 |
+
combinedDecRound(7)
|
| 1059 |
+
increment(6)
|
| 1060 |
+
|
| 1061 |
+
aesRound(8)
|
| 1062 |
+
increment(7)
|
| 1063 |
+
|
| 1064 |
+
PXOR ACC0, ACCM
|
| 1065 |
+
PXOR ACC1, ACCM
|
| 1066 |
+
MOVOU ACCM, T0
|
| 1067 |
+
PSRLDQ $8, ACCM
|
| 1068 |
+
PSLLDQ $8, T0
|
| 1069 |
+
PXOR ACCM, ACC1
|
| 1070 |
+
PXOR T0, ACC0
|
| 1071 |
+
|
| 1072 |
+
reduceRound(ACC0)
|
| 1073 |
+
aesRound(9)
|
| 1074 |
+
|
| 1075 |
+
reduceRound(ACC0)
|
| 1076 |
+
PXOR ACC1, ACC0
|
| 1077 |
+
|
| 1078 |
+
MOVOU (16*10)(ks), T0
|
| 1079 |
+
CMPQ NR, $12
|
| 1080 |
+
JB decLast1
|
| 1081 |
+
aesRnd(T0)
|
| 1082 |
+
aesRound(11)
|
| 1083 |
+
MOVOU (16*12)(ks), T0
|
| 1084 |
+
JE decLast1
|
| 1085 |
+
aesRnd(T0)
|
| 1086 |
+
aesRound(13)
|
| 1087 |
+
MOVOU (16*14)(ks), T0
|
| 1088 |
+
decLast1:
|
| 1089 |
+
aesRndLast(T0)
|
| 1090 |
+
|
| 1091 |
+
MOVOU (16*0)(ctx), T0
|
| 1092 |
+
PXOR T0, B0
|
| 1093 |
+
MOVOU (16*1)(ctx), T0
|
| 1094 |
+
PXOR T0, B1
|
| 1095 |
+
MOVOU (16*2)(ctx), T0
|
| 1096 |
+
PXOR T0, B2
|
| 1097 |
+
MOVOU (16*3)(ctx), T0
|
| 1098 |
+
PXOR T0, B3
|
| 1099 |
+
MOVOU (16*4)(ctx), T0
|
| 1100 |
+
PXOR T0, B4
|
| 1101 |
+
MOVOU (16*5)(ctx), T0
|
| 1102 |
+
PXOR T0, B5
|
| 1103 |
+
MOVOU (16*6)(ctx), T0
|
| 1104 |
+
PXOR T0, B6
|
| 1105 |
+
MOVOU (16*7)(ctx), T0
|
| 1106 |
+
PXOR T0, B7
|
| 1107 |
+
|
| 1108 |
+
MOVOU B0, (16*0)(ptx)
|
| 1109 |
+
MOVOU B1, (16*1)(ptx)
|
| 1110 |
+
MOVOU B2, (16*2)(ptx)
|
| 1111 |
+
MOVOU B3, (16*3)(ptx)
|
| 1112 |
+
MOVOU B4, (16*4)(ptx)
|
| 1113 |
+
MOVOU B5, (16*5)(ptx)
|
| 1114 |
+
MOVOU B6, (16*6)(ptx)
|
| 1115 |
+
MOVOU B7, (16*7)(ptx)
|
| 1116 |
+
|
| 1117 |
+
LEAQ 128(ptx), ptx
|
| 1118 |
+
LEAQ 128(ctx), ctx
|
| 1119 |
+
|
| 1120 |
+
JMP gcmAesDecOctetsLoop
|
| 1121 |
+
|
| 1122 |
+
gcmAesDecEndOctets:
|
| 1123 |
+
|
| 1124 |
+
SUBQ $7, aluCTR
|
| 1125 |
+
|
| 1126 |
+
gcmAesDecSingles:
|
| 1127 |
+
|
| 1128 |
+
MOVOU (16*1)(ks), B1
|
| 1129 |
+
MOVOU (16*2)(ks), B2
|
| 1130 |
+
MOVOU (16*3)(ks), B3
|
| 1131 |
+
MOVOU (16*4)(ks), B4
|
| 1132 |
+
MOVOU (16*5)(ks), B5
|
| 1133 |
+
MOVOU (16*6)(ks), B6
|
| 1134 |
+
MOVOU (16*7)(ks), B7
|
| 1135 |
+
|
| 1136 |
+
MOVOU (16*14)(pTbl), T2
|
| 1137 |
+
|
| 1138 |
+
gcmAesDecSinglesLoop:
|
| 1139 |
+
|
| 1140 |
+
CMPQ ptxLen, $16
|
| 1141 |
+
JB gcmAesDecTail
|
| 1142 |
+
SUBQ $16, ptxLen
|
| 1143 |
+
|
| 1144 |
+
MOVOU (ctx), B0
|
| 1145 |
+
MOVOU B0, T1
|
| 1146 |
+
PSHUFB BSWAP, B0
|
| 1147 |
+
PXOR ACC0, B0
|
| 1148 |
+
|
| 1149 |
+
MOVOU T2, ACC0
|
| 1150 |
+
MOVOU T2, ACC1
|
| 1151 |
+
MOVOU (16*15)(pTbl), ACCM
|
| 1152 |
+
|
| 1153 |
+
PCLMULQDQ $0x00, B0, ACC0
|
| 1154 |
+
PCLMULQDQ $0x11, B0, ACC1
|
| 1155 |
+
PSHUFD $78, B0, T0
|
| 1156 |
+
PXOR B0, T0
|
| 1157 |
+
PCLMULQDQ $0x00, T0, ACCM
|
| 1158 |
+
|
| 1159 |
+
PXOR ACC0, ACCM
|
| 1160 |
+
PXOR ACC1, ACCM
|
| 1161 |
+
MOVOU ACCM, T0
|
| 1162 |
+
PSRLDQ $8, ACCM
|
| 1163 |
+
PSLLDQ $8, T0
|
| 1164 |
+
PXOR ACCM, ACC1
|
| 1165 |
+
PXOR T0, ACC0
|
| 1166 |
+
|
| 1167 |
+
reduceRound(ACC0)
|
| 1168 |
+
reduceRound(ACC0)
|
| 1169 |
+
PXOR ACC1, ACC0
|
| 1170 |
+
|
| 1171 |
+
MOVOU (0*16)(SP), B0
|
| 1172 |
+
increment(0)
|
| 1173 |
+
AESENC B1, B0
|
| 1174 |
+
AESENC B2, B0
|
| 1175 |
+
AESENC B3, B0
|
| 1176 |
+
AESENC B4, B0
|
| 1177 |
+
AESENC B5, B0
|
| 1178 |
+
AESENC B6, B0
|
| 1179 |
+
AESENC B7, B0
|
| 1180 |
+
MOVOU (16*8)(ks), T0
|
| 1181 |
+
AESENC T0, B0
|
| 1182 |
+
MOVOU (16*9)(ks), T0
|
| 1183 |
+
AESENC T0, B0
|
| 1184 |
+
MOVOU (16*10)(ks), T0
|
| 1185 |
+
CMPQ NR, $12
|
| 1186 |
+
JB decLast2
|
| 1187 |
+
AESENC T0, B0
|
| 1188 |
+
MOVOU (16*11)(ks), T0
|
| 1189 |
+
AESENC T0, B0
|
| 1190 |
+
MOVOU (16*12)(ks), T0
|
| 1191 |
+
JE decLast2
|
| 1192 |
+
AESENC T0, B0
|
| 1193 |
+
MOVOU (16*13)(ks), T0
|
| 1194 |
+
AESENC T0, B0
|
| 1195 |
+
MOVOU (16*14)(ks), T0
|
| 1196 |
+
decLast2:
|
| 1197 |
+
AESENCLAST T0, B0
|
| 1198 |
+
|
| 1199 |
+
PXOR T1, B0
|
| 1200 |
+
MOVOU B0, (ptx)
|
| 1201 |
+
|
| 1202 |
+
LEAQ (16*1)(ptx), ptx
|
| 1203 |
+
LEAQ (16*1)(ctx), ctx
|
| 1204 |
+
|
| 1205 |
+
JMP gcmAesDecSinglesLoop
|
| 1206 |
+
|
| 1207 |
+
gcmAesDecTail:
|
| 1208 |
+
|
| 1209 |
+
TESTQ ptxLen, ptxLen
|
| 1210 |
+
JE gcmAesDecDone
|
| 1211 |
+
|
| 1212 |
+
MOVQ ptxLen, aluTMP
|
| 1213 |
+
SHLQ $4, aluTMP
|
| 1214 |
+
LEAQ andMask<>(SB), aluCTR
|
| 1215 |
+
MOVOU -16(aluCTR)(aluTMP*1), T1
|
| 1216 |
+
|
| 1217 |
+
MOVOU (ctx), B0 // I assume there is TAG attached to the ctx, and there is no read overflow
|
| 1218 |
+
PAND T1, B0
|
| 1219 |
+
|
| 1220 |
+
MOVOU B0, T1
|
| 1221 |
+
PSHUFB BSWAP, B0
|
| 1222 |
+
PXOR ACC0, B0
|
| 1223 |
+
|
| 1224 |
+
MOVOU (16*14)(pTbl), ACC0
|
| 1225 |
+
MOVOU (16*15)(pTbl), ACCM
|
| 1226 |
+
MOVOU ACC0, ACC1
|
| 1227 |
+
|
| 1228 |
+
PCLMULQDQ $0x00, B0, ACC0
|
| 1229 |
+
PCLMULQDQ $0x11, B0, ACC1
|
| 1230 |
+
PSHUFD $78, B0, T0
|
| 1231 |
+
PXOR B0, T0
|
| 1232 |
+
PCLMULQDQ $0x00, T0, ACCM
|
| 1233 |
+
|
| 1234 |
+
PXOR ACC0, ACCM
|
| 1235 |
+
PXOR ACC1, ACCM
|
| 1236 |
+
MOVOU ACCM, T0
|
| 1237 |
+
PSRLDQ $8, ACCM
|
| 1238 |
+
PSLLDQ $8, T0
|
| 1239 |
+
PXOR ACCM, ACC1
|
| 1240 |
+
PXOR T0, ACC0
|
| 1241 |
+
|
| 1242 |
+
reduceRound(ACC0)
|
| 1243 |
+
reduceRound(ACC0)
|
| 1244 |
+
PXOR ACC1, ACC0
|
| 1245 |
+
|
| 1246 |
+
MOVOU (0*16)(SP), B0
|
| 1247 |
+
increment(0)
|
| 1248 |
+
AESENC B1, B0
|
| 1249 |
+
AESENC B2, B0
|
| 1250 |
+
AESENC B3, B0
|
| 1251 |
+
AESENC B4, B0
|
| 1252 |
+
AESENC B5, B0
|
| 1253 |
+
AESENC B6, B0
|
| 1254 |
+
AESENC B7, B0
|
| 1255 |
+
MOVOU (16*8)(ks), T0
|
| 1256 |
+
AESENC T0, B0
|
| 1257 |
+
MOVOU (16*9)(ks), T0
|
| 1258 |
+
AESENC T0, B0
|
| 1259 |
+
MOVOU (16*10)(ks), T0
|
| 1260 |
+
CMPQ NR, $12
|
| 1261 |
+
JB decLast3
|
| 1262 |
+
AESENC T0, B0
|
| 1263 |
+
MOVOU (16*11)(ks), T0
|
| 1264 |
+
AESENC T0, B0
|
| 1265 |
+
MOVOU (16*12)(ks), T0
|
| 1266 |
+
JE decLast3
|
| 1267 |
+
AESENC T0, B0
|
| 1268 |
+
MOVOU (16*13)(ks), T0
|
| 1269 |
+
AESENC T0, B0
|
| 1270 |
+
MOVOU (16*14)(ks), T0
|
| 1271 |
+
decLast3:
|
| 1272 |
+
AESENCLAST T0, B0
|
| 1273 |
+
PXOR T1, B0
|
| 1274 |
+
|
| 1275 |
+
ptxStoreLoop:
|
| 1276 |
+
PEXTRB $0, B0, (ptx)
|
| 1277 |
+
PSRLDQ $1, B0
|
| 1278 |
+
LEAQ 1(ptx), ptx
|
| 1279 |
+
DECQ ptxLen
|
| 1280 |
+
|
| 1281 |
+
JNE ptxStoreLoop
|
| 1282 |
+
|
| 1283 |
+
gcmAesDecDone:
|
| 1284 |
+
|
| 1285 |
+
MOVOU ACC0, (tPtr)
|
| 1286 |
+
RET
|
platform/dbops/binaries/go/go/src/crypto/aes/gcm_arm64.s
ADDED
|
@@ -0,0 +1,1021 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2018 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
#include "textflag.h"
|
| 6 |
+
|
| 7 |
+
#define B0 V0
|
| 8 |
+
#define B1 V1
|
| 9 |
+
#define B2 V2
|
| 10 |
+
#define B3 V3
|
| 11 |
+
#define B4 V4
|
| 12 |
+
#define B5 V5
|
| 13 |
+
#define B6 V6
|
| 14 |
+
#define B7 V7
|
| 15 |
+
|
| 16 |
+
#define ACC0 V8
|
| 17 |
+
#define ACC1 V9
|
| 18 |
+
#define ACCM V10
|
| 19 |
+
|
| 20 |
+
#define T0 V11
|
| 21 |
+
#define T1 V12
|
| 22 |
+
#define T2 V13
|
| 23 |
+
#define T3 V14
|
| 24 |
+
|
| 25 |
+
#define POLY V15
|
| 26 |
+
#define ZERO V16
|
| 27 |
+
#define INC V17
|
| 28 |
+
#define CTR V18
|
| 29 |
+
|
| 30 |
+
#define K0 V19
|
| 31 |
+
#define K1 V20
|
| 32 |
+
#define K2 V21
|
| 33 |
+
#define K3 V22
|
| 34 |
+
#define K4 V23
|
| 35 |
+
#define K5 V24
|
| 36 |
+
#define K6 V25
|
| 37 |
+
#define K7 V26
|
| 38 |
+
#define K8 V27
|
| 39 |
+
#define K9 V28
|
| 40 |
+
#define K10 V29
|
| 41 |
+
#define K11 V30
|
| 42 |
+
#define KLAST V31
|
| 43 |
+
|
| 44 |
+
#define reduce() \
|
| 45 |
+
VEOR ACC0.B16, ACCM.B16, ACCM.B16 \
|
| 46 |
+
VEOR ACC1.B16, ACCM.B16, ACCM.B16 \
|
| 47 |
+
VEXT $8, ZERO.B16, ACCM.B16, T0.B16 \
|
| 48 |
+
VEXT $8, ACCM.B16, ZERO.B16, ACCM.B16 \
|
| 49 |
+
VEOR ACCM.B16, ACC0.B16, ACC0.B16 \
|
| 50 |
+
VEOR T0.B16, ACC1.B16, ACC1.B16 \
|
| 51 |
+
VPMULL POLY.D1, ACC0.D1, T0.Q1 \
|
| 52 |
+
VEXT $8, ACC0.B16, ACC0.B16, ACC0.B16 \
|
| 53 |
+
VEOR T0.B16, ACC0.B16, ACC0.B16 \
|
| 54 |
+
VPMULL POLY.D1, ACC0.D1, T0.Q1 \
|
| 55 |
+
VEOR T0.B16, ACC1.B16, ACC1.B16 \
|
| 56 |
+
VEXT $8, ACC1.B16, ACC1.B16, ACC1.B16 \
|
| 57 |
+
VEOR ACC1.B16, ACC0.B16, ACC0.B16 \
|
| 58 |
+
|
| 59 |
+
// func gcmAesFinish(productTable *[256]byte, tagMask, T *[16]byte, pLen, dLen uint64)
|
| 60 |
+
TEXT ·gcmAesFinish(SB),NOSPLIT,$0
|
| 61 |
+
#define pTbl R0
|
| 62 |
+
#define tMsk R1
|
| 63 |
+
#define tPtr R2
|
| 64 |
+
#define plen R3
|
| 65 |
+
#define dlen R4
|
| 66 |
+
|
| 67 |
+
MOVD $0xC2, R1
|
| 68 |
+
LSL $56, R1
|
| 69 |
+
MOVD $1, R0
|
| 70 |
+
VMOV R1, POLY.D[0]
|
| 71 |
+
VMOV R0, POLY.D[1]
|
| 72 |
+
VEOR ZERO.B16, ZERO.B16, ZERO.B16
|
| 73 |
+
|
| 74 |
+
MOVD productTable+0(FP), pTbl
|
| 75 |
+
MOVD tagMask+8(FP), tMsk
|
| 76 |
+
MOVD T+16(FP), tPtr
|
| 77 |
+
MOVD pLen+24(FP), plen
|
| 78 |
+
MOVD dLen+32(FP), dlen
|
| 79 |
+
|
| 80 |
+
VLD1 (tPtr), [ACC0.B16]
|
| 81 |
+
VLD1 (tMsk), [B1.B16]
|
| 82 |
+
|
| 83 |
+
LSL $3, plen
|
| 84 |
+
LSL $3, dlen
|
| 85 |
+
|
| 86 |
+
VMOV dlen, B0.D[0]
|
| 87 |
+
VMOV plen, B0.D[1]
|
| 88 |
+
|
| 89 |
+
ADD $14*16, pTbl
|
| 90 |
+
VLD1.P (pTbl), [T1.B16, T2.B16]
|
| 91 |
+
|
| 92 |
+
VEOR ACC0.B16, B0.B16, B0.B16
|
| 93 |
+
|
| 94 |
+
VEXT $8, B0.B16, B0.B16, T0.B16
|
| 95 |
+
VEOR B0.B16, T0.B16, T0.B16
|
| 96 |
+
VPMULL B0.D1, T1.D1, ACC1.Q1
|
| 97 |
+
VPMULL2 B0.D2, T1.D2, ACC0.Q1
|
| 98 |
+
VPMULL T0.D1, T2.D1, ACCM.Q1
|
| 99 |
+
|
| 100 |
+
reduce()
|
| 101 |
+
|
| 102 |
+
VREV64 ACC0.B16, ACC0.B16
|
| 103 |
+
VEOR B1.B16, ACC0.B16, ACC0.B16
|
| 104 |
+
|
| 105 |
+
VST1 [ACC0.B16], (tPtr)
|
| 106 |
+
RET
|
| 107 |
+
#undef pTbl
|
| 108 |
+
#undef tMsk
|
| 109 |
+
#undef tPtr
|
| 110 |
+
#undef plen
|
| 111 |
+
#undef dlen
|
| 112 |
+
|
| 113 |
+
// func gcmAesInit(productTable *[256]byte, ks []uint32)
|
| 114 |
+
TEXT ·gcmAesInit(SB),NOSPLIT,$0
|
| 115 |
+
#define pTbl R0
|
| 116 |
+
#define KS R1
|
| 117 |
+
#define NR R2
|
| 118 |
+
#define I R3
|
| 119 |
+
MOVD productTable+0(FP), pTbl
|
| 120 |
+
MOVD ks_base+8(FP), KS
|
| 121 |
+
MOVD ks_len+16(FP), NR
|
| 122 |
+
|
| 123 |
+
MOVD $0xC2, I
|
| 124 |
+
LSL $56, I
|
| 125 |
+
VMOV I, POLY.D[0]
|
| 126 |
+
MOVD $1, I
|
| 127 |
+
VMOV I, POLY.D[1]
|
| 128 |
+
VEOR ZERO.B16, ZERO.B16, ZERO.B16
|
| 129 |
+
|
| 130 |
+
// Encrypt block 0 with the AES key to generate the hash key H
|
| 131 |
+
VLD1.P 64(KS), [T0.B16, T1.B16, T2.B16, T3.B16]
|
| 132 |
+
VEOR B0.B16, B0.B16, B0.B16
|
| 133 |
+
AESE T0.B16, B0.B16
|
| 134 |
+
AESMC B0.B16, B0.B16
|
| 135 |
+
AESE T1.B16, B0.B16
|
| 136 |
+
AESMC B0.B16, B0.B16
|
| 137 |
+
AESE T2.B16, B0.B16
|
| 138 |
+
AESMC B0.B16, B0.B16
|
| 139 |
+
AESE T3.B16, B0.B16
|
| 140 |
+
AESMC B0.B16, B0.B16
|
| 141 |
+
VLD1.P 64(KS), [T0.B16, T1.B16, T2.B16, T3.B16]
|
| 142 |
+
AESE T0.B16, B0.B16
|
| 143 |
+
AESMC B0.B16, B0.B16
|
| 144 |
+
AESE T1.B16, B0.B16
|
| 145 |
+
AESMC B0.B16, B0.B16
|
| 146 |
+
AESE T2.B16, B0.B16
|
| 147 |
+
AESMC B0.B16, B0.B16
|
| 148 |
+
AESE T3.B16, B0.B16
|
| 149 |
+
AESMC B0.B16, B0.B16
|
| 150 |
+
TBZ $4, NR, initEncFinish
|
| 151 |
+
VLD1.P 32(KS), [T0.B16, T1.B16]
|
| 152 |
+
AESE T0.B16, B0.B16
|
| 153 |
+
AESMC B0.B16, B0.B16
|
| 154 |
+
AESE T1.B16, B0.B16
|
| 155 |
+
AESMC B0.B16, B0.B16
|
| 156 |
+
TBZ $3, NR, initEncFinish
|
| 157 |
+
VLD1.P 32(KS), [T0.B16, T1.B16]
|
| 158 |
+
AESE T0.B16, B0.B16
|
| 159 |
+
AESMC B0.B16, B0.B16
|
| 160 |
+
AESE T1.B16, B0.B16
|
| 161 |
+
AESMC B0.B16, B0.B16
|
| 162 |
+
initEncFinish:
|
| 163 |
+
VLD1 (KS), [T0.B16, T1.B16, T2.B16]
|
| 164 |
+
AESE T0.B16, B0.B16
|
| 165 |
+
AESMC B0.B16, B0.B16
|
| 166 |
+
AESE T1.B16, B0.B16
|
| 167 |
+
VEOR T2.B16, B0.B16, B0.B16
|
| 168 |
+
|
| 169 |
+
VREV64 B0.B16, B0.B16
|
| 170 |
+
|
| 171 |
+
// Multiply by 2 modulo P
|
| 172 |
+
VMOV B0.D[0], I
|
| 173 |
+
ASR $63, I
|
| 174 |
+
VMOV I, T1.D[0]
|
| 175 |
+
VMOV I, T1.D[1]
|
| 176 |
+
VAND POLY.B16, T1.B16, T1.B16
|
| 177 |
+
VUSHR $63, B0.D2, T2.D2
|
| 178 |
+
VEXT $8, ZERO.B16, T2.B16, T2.B16
|
| 179 |
+
VSHL $1, B0.D2, B0.D2
|
| 180 |
+
VEOR T1.B16, B0.B16, B0.B16
|
| 181 |
+
VEOR T2.B16, B0.B16, B0.B16 // Can avoid this when VSLI is available
|
| 182 |
+
|
| 183 |
+
// Karatsuba pre-computation
|
| 184 |
+
VEXT $8, B0.B16, B0.B16, B1.B16
|
| 185 |
+
VEOR B0.B16, B1.B16, B1.B16
|
| 186 |
+
|
| 187 |
+
ADD $14*16, pTbl
|
| 188 |
+
VST1 [B0.B16, B1.B16], (pTbl)
|
| 189 |
+
SUB $2*16, pTbl
|
| 190 |
+
|
| 191 |
+
VMOV B0.B16, B2.B16
|
| 192 |
+
VMOV B1.B16, B3.B16
|
| 193 |
+
|
| 194 |
+
MOVD $7, I
|
| 195 |
+
|
| 196 |
+
initLoop:
|
| 197 |
+
// Compute powers of H
|
| 198 |
+
SUBS $1, I
|
| 199 |
+
|
| 200 |
+
VPMULL B0.D1, B2.D1, T1.Q1
|
| 201 |
+
VPMULL2 B0.D2, B2.D2, T0.Q1
|
| 202 |
+
VPMULL B1.D1, B3.D1, T2.Q1
|
| 203 |
+
VEOR T0.B16, T2.B16, T2.B16
|
| 204 |
+
VEOR T1.B16, T2.B16, T2.B16
|
| 205 |
+
VEXT $8, ZERO.B16, T2.B16, T3.B16
|
| 206 |
+
VEXT $8, T2.B16, ZERO.B16, T2.B16
|
| 207 |
+
VEOR T2.B16, T0.B16, T0.B16
|
| 208 |
+
VEOR T3.B16, T1.B16, T1.B16
|
| 209 |
+
VPMULL POLY.D1, T0.D1, T2.Q1
|
| 210 |
+
VEXT $8, T0.B16, T0.B16, T0.B16
|
| 211 |
+
VEOR T2.B16, T0.B16, T0.B16
|
| 212 |
+
VPMULL POLY.D1, T0.D1, T2.Q1
|
| 213 |
+
VEXT $8, T0.B16, T0.B16, T0.B16
|
| 214 |
+
VEOR T2.B16, T0.B16, T0.B16
|
| 215 |
+
VEOR T1.B16, T0.B16, B2.B16
|
| 216 |
+
VMOV B2.B16, B3.B16
|
| 217 |
+
VEXT $8, B2.B16, B2.B16, B2.B16
|
| 218 |
+
VEOR B2.B16, B3.B16, B3.B16
|
| 219 |
+
|
| 220 |
+
VST1 [B2.B16, B3.B16], (pTbl)
|
| 221 |
+
SUB $2*16, pTbl
|
| 222 |
+
|
| 223 |
+
BNE initLoop
|
| 224 |
+
RET
|
| 225 |
+
#undef I
|
| 226 |
+
#undef NR
|
| 227 |
+
#undef KS
|
| 228 |
+
#undef pTbl
|
| 229 |
+
|
| 230 |
+
// func gcmAesData(productTable *[256]byte, data []byte, T *[16]byte)
|
| 231 |
+
TEXT ·gcmAesData(SB),NOSPLIT,$0
|
| 232 |
+
#define pTbl R0
|
| 233 |
+
#define aut R1
|
| 234 |
+
#define tPtr R2
|
| 235 |
+
#define autLen R3
|
| 236 |
+
#define H0 R4
|
| 237 |
+
#define pTblSave R5
|
| 238 |
+
|
| 239 |
+
#define mulRound(X) \
|
| 240 |
+
VLD1.P 32(pTbl), [T1.B16, T2.B16] \
|
| 241 |
+
VREV64 X.B16, X.B16 \
|
| 242 |
+
VEXT $8, X.B16, X.B16, T0.B16 \
|
| 243 |
+
VEOR X.B16, T0.B16, T0.B16 \
|
| 244 |
+
VPMULL X.D1, T1.D1, T3.Q1 \
|
| 245 |
+
VEOR T3.B16, ACC1.B16, ACC1.B16 \
|
| 246 |
+
VPMULL2 X.D2, T1.D2, T3.Q1 \
|
| 247 |
+
VEOR T3.B16, ACC0.B16, ACC0.B16 \
|
| 248 |
+
VPMULL T0.D1, T2.D1, T3.Q1 \
|
| 249 |
+
VEOR T3.B16, ACCM.B16, ACCM.B16
|
| 250 |
+
|
| 251 |
+
MOVD productTable+0(FP), pTbl
|
| 252 |
+
MOVD data_base+8(FP), aut
|
| 253 |
+
MOVD data_len+16(FP), autLen
|
| 254 |
+
MOVD T+32(FP), tPtr
|
| 255 |
+
|
| 256 |
+
VEOR ACC0.B16, ACC0.B16, ACC0.B16
|
| 257 |
+
CBZ autLen, dataBail
|
| 258 |
+
|
| 259 |
+
MOVD $0xC2, H0
|
| 260 |
+
LSL $56, H0
|
| 261 |
+
VMOV H0, POLY.D[0]
|
| 262 |
+
MOVD $1, H0
|
| 263 |
+
VMOV H0, POLY.D[1]
|
| 264 |
+
VEOR ZERO.B16, ZERO.B16, ZERO.B16
|
| 265 |
+
MOVD pTbl, pTblSave
|
| 266 |
+
|
| 267 |
+
CMP $13, autLen
|
| 268 |
+
BEQ dataTLS
|
| 269 |
+
CMP $128, autLen
|
| 270 |
+
BLT startSinglesLoop
|
| 271 |
+
B octetsLoop
|
| 272 |
+
|
| 273 |
+
dataTLS:
|
| 274 |
+
ADD $14*16, pTbl
|
| 275 |
+
VLD1.P (pTbl), [T1.B16, T2.B16]
|
| 276 |
+
VEOR B0.B16, B0.B16, B0.B16
|
| 277 |
+
|
| 278 |
+
MOVD (aut), H0
|
| 279 |
+
VMOV H0, B0.D[0]
|
| 280 |
+
MOVW 8(aut), H0
|
| 281 |
+
VMOV H0, B0.S[2]
|
| 282 |
+
MOVB 12(aut), H0
|
| 283 |
+
VMOV H0, B0.B[12]
|
| 284 |
+
|
| 285 |
+
MOVD $0, autLen
|
| 286 |
+
B dataMul
|
| 287 |
+
|
| 288 |
+
octetsLoop:
|
| 289 |
+
CMP $128, autLen
|
| 290 |
+
BLT startSinglesLoop
|
| 291 |
+
SUB $128, autLen
|
| 292 |
+
|
| 293 |
+
VLD1.P 32(aut), [B0.B16, B1.B16]
|
| 294 |
+
|
| 295 |
+
VLD1.P 32(pTbl), [T1.B16, T2.B16]
|
| 296 |
+
VREV64 B0.B16, B0.B16
|
| 297 |
+
VEOR ACC0.B16, B0.B16, B0.B16
|
| 298 |
+
VEXT $8, B0.B16, B0.B16, T0.B16
|
| 299 |
+
VEOR B0.B16, T0.B16, T0.B16
|
| 300 |
+
VPMULL B0.D1, T1.D1, ACC1.Q1
|
| 301 |
+
VPMULL2 B0.D2, T1.D2, ACC0.Q1
|
| 302 |
+
VPMULL T0.D1, T2.D1, ACCM.Q1
|
| 303 |
+
|
| 304 |
+
mulRound(B1)
|
| 305 |
+
VLD1.P 32(aut), [B2.B16, B3.B16]
|
| 306 |
+
mulRound(B2)
|
| 307 |
+
mulRound(B3)
|
| 308 |
+
VLD1.P 32(aut), [B4.B16, B5.B16]
|
| 309 |
+
mulRound(B4)
|
| 310 |
+
mulRound(B5)
|
| 311 |
+
VLD1.P 32(aut), [B6.B16, B7.B16]
|
| 312 |
+
mulRound(B6)
|
| 313 |
+
mulRound(B7)
|
| 314 |
+
|
| 315 |
+
MOVD pTblSave, pTbl
|
| 316 |
+
reduce()
|
| 317 |
+
B octetsLoop
|
| 318 |
+
|
| 319 |
+
startSinglesLoop:
|
| 320 |
+
|
| 321 |
+
ADD $14*16, pTbl
|
| 322 |
+
VLD1.P (pTbl), [T1.B16, T2.B16]
|
| 323 |
+
|
| 324 |
+
singlesLoop:
|
| 325 |
+
|
| 326 |
+
CMP $16, autLen
|
| 327 |
+
BLT dataEnd
|
| 328 |
+
SUB $16, autLen
|
| 329 |
+
|
| 330 |
+
VLD1.P 16(aut), [B0.B16]
|
| 331 |
+
dataMul:
|
| 332 |
+
VREV64 B0.B16, B0.B16
|
| 333 |
+
VEOR ACC0.B16, B0.B16, B0.B16
|
| 334 |
+
|
| 335 |
+
VEXT $8, B0.B16, B0.B16, T0.B16
|
| 336 |
+
VEOR B0.B16, T0.B16, T0.B16
|
| 337 |
+
VPMULL B0.D1, T1.D1, ACC1.Q1
|
| 338 |
+
VPMULL2 B0.D2, T1.D2, ACC0.Q1
|
| 339 |
+
VPMULL T0.D1, T2.D1, ACCM.Q1
|
| 340 |
+
|
| 341 |
+
reduce()
|
| 342 |
+
|
| 343 |
+
B singlesLoop
|
| 344 |
+
|
| 345 |
+
dataEnd:
|
| 346 |
+
|
| 347 |
+
CBZ autLen, dataBail
|
| 348 |
+
VEOR B0.B16, B0.B16, B0.B16
|
| 349 |
+
ADD autLen, aut
|
| 350 |
+
|
| 351 |
+
dataLoadLoop:
|
| 352 |
+
MOVB.W -1(aut), H0
|
| 353 |
+
VEXT $15, B0.B16, ZERO.B16, B0.B16
|
| 354 |
+
VMOV H0, B0.B[0]
|
| 355 |
+
SUBS $1, autLen
|
| 356 |
+
BNE dataLoadLoop
|
| 357 |
+
B dataMul
|
| 358 |
+
|
| 359 |
+
dataBail:
|
| 360 |
+
VST1 [ACC0.B16], (tPtr)
|
| 361 |
+
RET
|
| 362 |
+
|
| 363 |
+
#undef pTbl
|
| 364 |
+
#undef aut
|
| 365 |
+
#undef tPtr
|
| 366 |
+
#undef autLen
|
| 367 |
+
#undef H0
|
| 368 |
+
#undef pTblSave
|
| 369 |
+
|
| 370 |
+
// func gcmAesEnc(productTable *[256]byte, dst, src []byte, ctr, T *[16]byte, ks []uint32)
|
| 371 |
+
TEXT ·gcmAesEnc(SB),NOSPLIT,$0
|
| 372 |
+
#define pTbl R0
|
| 373 |
+
#define dstPtr R1
|
| 374 |
+
#define ctrPtr R2
|
| 375 |
+
#define srcPtr R3
|
| 376 |
+
#define ks R4
|
| 377 |
+
#define tPtr R5
|
| 378 |
+
#define srcPtrLen R6
|
| 379 |
+
#define aluCTR R7
|
| 380 |
+
#define aluTMP R8
|
| 381 |
+
#define aluK R9
|
| 382 |
+
#define NR R10
|
| 383 |
+
#define H0 R11
|
| 384 |
+
#define H1 R12
|
| 385 |
+
#define curK R13
|
| 386 |
+
#define pTblSave R14
|
| 387 |
+
|
| 388 |
+
#define aesrndx8(K) \
|
| 389 |
+
AESE K.B16, B0.B16 \
|
| 390 |
+
AESMC B0.B16, B0.B16 \
|
| 391 |
+
AESE K.B16, B1.B16 \
|
| 392 |
+
AESMC B1.B16, B1.B16 \
|
| 393 |
+
AESE K.B16, B2.B16 \
|
| 394 |
+
AESMC B2.B16, B2.B16 \
|
| 395 |
+
AESE K.B16, B3.B16 \
|
| 396 |
+
AESMC B3.B16, B3.B16 \
|
| 397 |
+
AESE K.B16, B4.B16 \
|
| 398 |
+
AESMC B4.B16, B4.B16 \
|
| 399 |
+
AESE K.B16, B5.B16 \
|
| 400 |
+
AESMC B5.B16, B5.B16 \
|
| 401 |
+
AESE K.B16, B6.B16 \
|
| 402 |
+
AESMC B6.B16, B6.B16 \
|
| 403 |
+
AESE K.B16, B7.B16 \
|
| 404 |
+
AESMC B7.B16, B7.B16
|
| 405 |
+
|
| 406 |
+
#define aesrndlastx8(K) \
|
| 407 |
+
AESE K.B16, B0.B16 \
|
| 408 |
+
AESE K.B16, B1.B16 \
|
| 409 |
+
AESE K.B16, B2.B16 \
|
| 410 |
+
AESE K.B16, B3.B16 \
|
| 411 |
+
AESE K.B16, B4.B16 \
|
| 412 |
+
AESE K.B16, B5.B16 \
|
| 413 |
+
AESE K.B16, B6.B16 \
|
| 414 |
+
AESE K.B16, B7.B16
|
| 415 |
+
|
| 416 |
+
MOVD productTable+0(FP), pTbl
|
| 417 |
+
MOVD dst+8(FP), dstPtr
|
| 418 |
+
MOVD src_base+32(FP), srcPtr
|
| 419 |
+
MOVD src_len+40(FP), srcPtrLen
|
| 420 |
+
MOVD ctr+56(FP), ctrPtr
|
| 421 |
+
MOVD T+64(FP), tPtr
|
| 422 |
+
MOVD ks_base+72(FP), ks
|
| 423 |
+
MOVD ks_len+80(FP), NR
|
| 424 |
+
|
| 425 |
+
MOVD $0xC2, H1
|
| 426 |
+
LSL $56, H1
|
| 427 |
+
MOVD $1, H0
|
| 428 |
+
VMOV H1, POLY.D[0]
|
| 429 |
+
VMOV H0, POLY.D[1]
|
| 430 |
+
VEOR ZERO.B16, ZERO.B16, ZERO.B16
|
| 431 |
+
// Compute NR from len(ks)
|
| 432 |
+
MOVD pTbl, pTblSave
|
| 433 |
+
// Current tag, after AAD
|
| 434 |
+
VLD1 (tPtr), [ACC0.B16]
|
| 435 |
+
VEOR ACC1.B16, ACC1.B16, ACC1.B16
|
| 436 |
+
VEOR ACCM.B16, ACCM.B16, ACCM.B16
|
| 437 |
+
// Prepare initial counter, and the increment vector
|
| 438 |
+
VLD1 (ctrPtr), [CTR.B16]
|
| 439 |
+
VEOR INC.B16, INC.B16, INC.B16
|
| 440 |
+
MOVD $1, H0
|
| 441 |
+
VMOV H0, INC.S[3]
|
| 442 |
+
VREV32 CTR.B16, CTR.B16
|
| 443 |
+
VADD CTR.S4, INC.S4, CTR.S4
|
| 444 |
+
// Skip to <8 blocks loop
|
| 445 |
+
CMP $128, srcPtrLen
|
| 446 |
+
|
| 447 |
+
MOVD ks, H0
|
| 448 |
+
// For AES-128 round keys are stored in: K0 .. K10, KLAST
|
| 449 |
+
VLD1.P 64(H0), [K0.B16, K1.B16, K2.B16, K3.B16]
|
| 450 |
+
VLD1.P 64(H0), [K4.B16, K5.B16, K6.B16, K7.B16]
|
| 451 |
+
VLD1.P 48(H0), [K8.B16, K9.B16, K10.B16]
|
| 452 |
+
VMOV K10.B16, KLAST.B16
|
| 453 |
+
|
| 454 |
+
BLT startSingles
|
| 455 |
+
// There are at least 8 blocks to encrypt
|
| 456 |
+
TBZ $4, NR, octetsLoop
|
| 457 |
+
|
| 458 |
+
// For AES-192 round keys occupy: K0 .. K7, K10, K11, K8, K9, KLAST
|
| 459 |
+
VMOV K8.B16, K10.B16
|
| 460 |
+
VMOV K9.B16, K11.B16
|
| 461 |
+
VMOV KLAST.B16, K8.B16
|
| 462 |
+
VLD1.P 16(H0), [K9.B16]
|
| 463 |
+
VLD1.P 16(H0), [KLAST.B16]
|
| 464 |
+
TBZ $3, NR, octetsLoop
|
| 465 |
+
// For AES-256 round keys occupy: K0 .. K7, K10, K11, mem, mem, K8, K9, KLAST
|
| 466 |
+
VMOV KLAST.B16, K8.B16
|
| 467 |
+
VLD1.P 16(H0), [K9.B16]
|
| 468 |
+
VLD1.P 16(H0), [KLAST.B16]
|
| 469 |
+
ADD $10*16, ks, H0
|
| 470 |
+
MOVD H0, curK
|
| 471 |
+
|
| 472 |
+
octetsLoop:
|
| 473 |
+
SUB $128, srcPtrLen
|
| 474 |
+
|
| 475 |
+
VMOV CTR.B16, B0.B16
|
| 476 |
+
VADD B0.S4, INC.S4, B1.S4
|
| 477 |
+
VREV32 B0.B16, B0.B16
|
| 478 |
+
VADD B1.S4, INC.S4, B2.S4
|
| 479 |
+
VREV32 B1.B16, B1.B16
|
| 480 |
+
VADD B2.S4, INC.S4, B3.S4
|
| 481 |
+
VREV32 B2.B16, B2.B16
|
| 482 |
+
VADD B3.S4, INC.S4, B4.S4
|
| 483 |
+
VREV32 B3.B16, B3.B16
|
| 484 |
+
VADD B4.S4, INC.S4, B5.S4
|
| 485 |
+
VREV32 B4.B16, B4.B16
|
| 486 |
+
VADD B5.S4, INC.S4, B6.S4
|
| 487 |
+
VREV32 B5.B16, B5.B16
|
| 488 |
+
VADD B6.S4, INC.S4, B7.S4
|
| 489 |
+
VREV32 B6.B16, B6.B16
|
| 490 |
+
VADD B7.S4, INC.S4, CTR.S4
|
| 491 |
+
VREV32 B7.B16, B7.B16
|
| 492 |
+
|
| 493 |
+
aesrndx8(K0)
|
| 494 |
+
aesrndx8(K1)
|
| 495 |
+
aesrndx8(K2)
|
| 496 |
+
aesrndx8(K3)
|
| 497 |
+
aesrndx8(K4)
|
| 498 |
+
aesrndx8(K5)
|
| 499 |
+
aesrndx8(K6)
|
| 500 |
+
aesrndx8(K7)
|
| 501 |
+
TBZ $4, NR, octetsFinish
|
| 502 |
+
aesrndx8(K10)
|
| 503 |
+
aesrndx8(K11)
|
| 504 |
+
TBZ $3, NR, octetsFinish
|
| 505 |
+
VLD1.P 32(curK), [T1.B16, T2.B16]
|
| 506 |
+
aesrndx8(T1)
|
| 507 |
+
aesrndx8(T2)
|
| 508 |
+
MOVD H0, curK
|
| 509 |
+
octetsFinish:
|
| 510 |
+
aesrndx8(K8)
|
| 511 |
+
aesrndlastx8(K9)
|
| 512 |
+
|
| 513 |
+
VEOR KLAST.B16, B0.B16, B0.B16
|
| 514 |
+
VEOR KLAST.B16, B1.B16, B1.B16
|
| 515 |
+
VEOR KLAST.B16, B2.B16, B2.B16
|
| 516 |
+
VEOR KLAST.B16, B3.B16, B3.B16
|
| 517 |
+
VEOR KLAST.B16, B4.B16, B4.B16
|
| 518 |
+
VEOR KLAST.B16, B5.B16, B5.B16
|
| 519 |
+
VEOR KLAST.B16, B6.B16, B6.B16
|
| 520 |
+
VEOR KLAST.B16, B7.B16, B7.B16
|
| 521 |
+
|
| 522 |
+
VLD1.P 32(srcPtr), [T1.B16, T2.B16]
|
| 523 |
+
VEOR B0.B16, T1.B16, B0.B16
|
| 524 |
+
VEOR B1.B16, T2.B16, B1.B16
|
| 525 |
+
VST1.P [B0.B16, B1.B16], 32(dstPtr)
|
| 526 |
+
VLD1.P 32(srcPtr), [T1.B16, T2.B16]
|
| 527 |
+
VEOR B2.B16, T1.B16, B2.B16
|
| 528 |
+
VEOR B3.B16, T2.B16, B3.B16
|
| 529 |
+
VST1.P [B2.B16, B3.B16], 32(dstPtr)
|
| 530 |
+
VLD1.P 32(srcPtr), [T1.B16, T2.B16]
|
| 531 |
+
VEOR B4.B16, T1.B16, B4.B16
|
| 532 |
+
VEOR B5.B16, T2.B16, B5.B16
|
| 533 |
+
VST1.P [B4.B16, B5.B16], 32(dstPtr)
|
| 534 |
+
VLD1.P 32(srcPtr), [T1.B16, T2.B16]
|
| 535 |
+
VEOR B6.B16, T1.B16, B6.B16
|
| 536 |
+
VEOR B7.B16, T2.B16, B7.B16
|
| 537 |
+
VST1.P [B6.B16, B7.B16], 32(dstPtr)
|
| 538 |
+
|
| 539 |
+
VLD1.P 32(pTbl), [T1.B16, T2.B16]
|
| 540 |
+
VREV64 B0.B16, B0.B16
|
| 541 |
+
VEOR ACC0.B16, B0.B16, B0.B16
|
| 542 |
+
VEXT $8, B0.B16, B0.B16, T0.B16
|
| 543 |
+
VEOR B0.B16, T0.B16, T0.B16
|
| 544 |
+
VPMULL B0.D1, T1.D1, ACC1.Q1
|
| 545 |
+
VPMULL2 B0.D2, T1.D2, ACC0.Q1
|
| 546 |
+
VPMULL T0.D1, T2.D1, ACCM.Q1
|
| 547 |
+
|
| 548 |
+
mulRound(B1)
|
| 549 |
+
mulRound(B2)
|
| 550 |
+
mulRound(B3)
|
| 551 |
+
mulRound(B4)
|
| 552 |
+
mulRound(B5)
|
| 553 |
+
mulRound(B6)
|
| 554 |
+
mulRound(B7)
|
| 555 |
+
MOVD pTblSave, pTbl
|
| 556 |
+
reduce()
|
| 557 |
+
|
| 558 |
+
CMP $128, srcPtrLen
|
| 559 |
+
BGE octetsLoop
|
| 560 |
+
|
| 561 |
+
startSingles:
|
| 562 |
+
CBZ srcPtrLen, done
|
| 563 |
+
ADD $14*16, pTbl
|
| 564 |
+
// Preload H and its Karatsuba precomp
|
| 565 |
+
VLD1.P (pTbl), [T1.B16, T2.B16]
|
| 566 |
+
// Preload AES round keys
|
| 567 |
+
ADD $128, ks
|
| 568 |
+
VLD1.P 48(ks), [K8.B16, K9.B16, K10.B16]
|
| 569 |
+
VMOV K10.B16, KLAST.B16
|
| 570 |
+
TBZ $4, NR, singlesLoop
|
| 571 |
+
VLD1.P 32(ks), [B1.B16, B2.B16]
|
| 572 |
+
VMOV B2.B16, KLAST.B16
|
| 573 |
+
TBZ $3, NR, singlesLoop
|
| 574 |
+
VLD1.P 32(ks), [B3.B16, B4.B16]
|
| 575 |
+
VMOV B4.B16, KLAST.B16
|
| 576 |
+
|
| 577 |
+
singlesLoop:
|
| 578 |
+
CMP $16, srcPtrLen
|
| 579 |
+
BLT tail
|
| 580 |
+
SUB $16, srcPtrLen
|
| 581 |
+
|
| 582 |
+
VLD1.P 16(srcPtr), [T0.B16]
|
| 583 |
+
VEOR KLAST.B16, T0.B16, T0.B16
|
| 584 |
+
|
| 585 |
+
VREV32 CTR.B16, B0.B16
|
| 586 |
+
VADD CTR.S4, INC.S4, CTR.S4
|
| 587 |
+
|
| 588 |
+
AESE K0.B16, B0.B16
|
| 589 |
+
AESMC B0.B16, B0.B16
|
| 590 |
+
AESE K1.B16, B0.B16
|
| 591 |
+
AESMC B0.B16, B0.B16
|
| 592 |
+
AESE K2.B16, B0.B16
|
| 593 |
+
AESMC B0.B16, B0.B16
|
| 594 |
+
AESE K3.B16, B0.B16
|
| 595 |
+
AESMC B0.B16, B0.B16
|
| 596 |
+
AESE K4.B16, B0.B16
|
| 597 |
+
AESMC B0.B16, B0.B16
|
| 598 |
+
AESE K5.B16, B0.B16
|
| 599 |
+
AESMC B0.B16, B0.B16
|
| 600 |
+
AESE K6.B16, B0.B16
|
| 601 |
+
AESMC B0.B16, B0.B16
|
| 602 |
+
AESE K7.B16, B0.B16
|
| 603 |
+
AESMC B0.B16, B0.B16
|
| 604 |
+
AESE K8.B16, B0.B16
|
| 605 |
+
AESMC B0.B16, B0.B16
|
| 606 |
+
AESE K9.B16, B0.B16
|
| 607 |
+
TBZ $4, NR, singlesLast
|
| 608 |
+
AESMC B0.B16, B0.B16
|
| 609 |
+
AESE K10.B16, B0.B16
|
| 610 |
+
AESMC B0.B16, B0.B16
|
| 611 |
+
AESE B1.B16, B0.B16
|
| 612 |
+
TBZ $3, NR, singlesLast
|
| 613 |
+
AESMC B0.B16, B0.B16
|
| 614 |
+
AESE B2.B16, B0.B16
|
| 615 |
+
AESMC B0.B16, B0.B16
|
| 616 |
+
AESE B3.B16, B0.B16
|
| 617 |
+
singlesLast:
|
| 618 |
+
VEOR T0.B16, B0.B16, B0.B16
|
| 619 |
+
encReduce:
|
| 620 |
+
VST1.P [B0.B16], 16(dstPtr)
|
| 621 |
+
|
| 622 |
+
VREV64 B0.B16, B0.B16
|
| 623 |
+
VEOR ACC0.B16, B0.B16, B0.B16
|
| 624 |
+
|
| 625 |
+
VEXT $8, B0.B16, B0.B16, T0.B16
|
| 626 |
+
VEOR B0.B16, T0.B16, T0.B16
|
| 627 |
+
VPMULL B0.D1, T1.D1, ACC1.Q1
|
| 628 |
+
VPMULL2 B0.D2, T1.D2, ACC0.Q1
|
| 629 |
+
VPMULL T0.D1, T2.D1, ACCM.Q1
|
| 630 |
+
|
| 631 |
+
reduce()
|
| 632 |
+
|
| 633 |
+
B singlesLoop
|
| 634 |
+
tail:
|
| 635 |
+
CBZ srcPtrLen, done
|
| 636 |
+
|
| 637 |
+
VEOR T0.B16, T0.B16, T0.B16
|
| 638 |
+
VEOR T3.B16, T3.B16, T3.B16
|
| 639 |
+
MOVD $0, H1
|
| 640 |
+
SUB $1, H1
|
| 641 |
+
ADD srcPtrLen, srcPtr
|
| 642 |
+
|
| 643 |
+
TBZ $3, srcPtrLen, ld4
|
| 644 |
+
MOVD.W -8(srcPtr), H0
|
| 645 |
+
VMOV H0, T0.D[0]
|
| 646 |
+
VMOV H1, T3.D[0]
|
| 647 |
+
ld4:
|
| 648 |
+
TBZ $2, srcPtrLen, ld2
|
| 649 |
+
MOVW.W -4(srcPtr), H0
|
| 650 |
+
VEXT $12, T0.B16, ZERO.B16, T0.B16
|
| 651 |
+
VEXT $12, T3.B16, ZERO.B16, T3.B16
|
| 652 |
+
VMOV H0, T0.S[0]
|
| 653 |
+
VMOV H1, T3.S[0]
|
| 654 |
+
ld2:
|
| 655 |
+
TBZ $1, srcPtrLen, ld1
|
| 656 |
+
MOVH.W -2(srcPtr), H0
|
| 657 |
+
VEXT $14, T0.B16, ZERO.B16, T0.B16
|
| 658 |
+
VEXT $14, T3.B16, ZERO.B16, T3.B16
|
| 659 |
+
VMOV H0, T0.H[0]
|
| 660 |
+
VMOV H1, T3.H[0]
|
| 661 |
+
ld1:
|
| 662 |
+
TBZ $0, srcPtrLen, ld0
|
| 663 |
+
MOVB.W -1(srcPtr), H0
|
| 664 |
+
VEXT $15, T0.B16, ZERO.B16, T0.B16
|
| 665 |
+
VEXT $15, T3.B16, ZERO.B16, T3.B16
|
| 666 |
+
VMOV H0, T0.B[0]
|
| 667 |
+
VMOV H1, T3.B[0]
|
| 668 |
+
ld0:
|
| 669 |
+
|
| 670 |
+
MOVD ZR, srcPtrLen
|
| 671 |
+
VEOR KLAST.B16, T0.B16, T0.B16
|
| 672 |
+
VREV32 CTR.B16, B0.B16
|
| 673 |
+
|
| 674 |
+
AESE K0.B16, B0.B16
|
| 675 |
+
AESMC B0.B16, B0.B16
|
| 676 |
+
AESE K1.B16, B0.B16
|
| 677 |
+
AESMC B0.B16, B0.B16
|
| 678 |
+
AESE K2.B16, B0.B16
|
| 679 |
+
AESMC B0.B16, B0.B16
|
| 680 |
+
AESE K3.B16, B0.B16
|
| 681 |
+
AESMC B0.B16, B0.B16
|
| 682 |
+
AESE K4.B16, B0.B16
|
| 683 |
+
AESMC B0.B16, B0.B16
|
| 684 |
+
AESE K5.B16, B0.B16
|
| 685 |
+
AESMC B0.B16, B0.B16
|
| 686 |
+
AESE K6.B16, B0.B16
|
| 687 |
+
AESMC B0.B16, B0.B16
|
| 688 |
+
AESE K7.B16, B0.B16
|
| 689 |
+
AESMC B0.B16, B0.B16
|
| 690 |
+
AESE K8.B16, B0.B16
|
| 691 |
+
AESMC B0.B16, B0.B16
|
| 692 |
+
AESE K9.B16, B0.B16
|
| 693 |
+
TBZ $4, NR, tailLast
|
| 694 |
+
AESMC B0.B16, B0.B16
|
| 695 |
+
AESE K10.B16, B0.B16
|
| 696 |
+
AESMC B0.B16, B0.B16
|
| 697 |
+
AESE B1.B16, B0.B16
|
| 698 |
+
TBZ $3, NR, tailLast
|
| 699 |
+
AESMC B0.B16, B0.B16
|
| 700 |
+
AESE B2.B16, B0.B16
|
| 701 |
+
AESMC B0.B16, B0.B16
|
| 702 |
+
AESE B3.B16, B0.B16
|
| 703 |
+
|
| 704 |
+
tailLast:
|
| 705 |
+
VEOR T0.B16, B0.B16, B0.B16
|
| 706 |
+
VAND T3.B16, B0.B16, B0.B16
|
| 707 |
+
B encReduce
|
| 708 |
+
|
| 709 |
+
done:
|
| 710 |
+
VST1 [ACC0.B16], (tPtr)
|
| 711 |
+
RET
|
| 712 |
+
|
| 713 |
+
// func gcmAesDec(productTable *[256]byte, dst, src []byte, ctr, T *[16]byte, ks []uint32)
|
| 714 |
+
TEXT ·gcmAesDec(SB),NOSPLIT,$0
|
| 715 |
+
MOVD productTable+0(FP), pTbl
|
| 716 |
+
MOVD dst+8(FP), dstPtr
|
| 717 |
+
MOVD src_base+32(FP), srcPtr
|
| 718 |
+
MOVD src_len+40(FP), srcPtrLen
|
| 719 |
+
MOVD ctr+56(FP), ctrPtr
|
| 720 |
+
MOVD T+64(FP), tPtr
|
| 721 |
+
MOVD ks_base+72(FP), ks
|
| 722 |
+
MOVD ks_len+80(FP), NR
|
| 723 |
+
|
| 724 |
+
MOVD $0xC2, H1
|
| 725 |
+
LSL $56, H1
|
| 726 |
+
MOVD $1, H0
|
| 727 |
+
VMOV H1, POLY.D[0]
|
| 728 |
+
VMOV H0, POLY.D[1]
|
| 729 |
+
VEOR ZERO.B16, ZERO.B16, ZERO.B16
|
| 730 |
+
// Compute NR from len(ks)
|
| 731 |
+
MOVD pTbl, pTblSave
|
| 732 |
+
// Current tag, after AAD
|
| 733 |
+
VLD1 (tPtr), [ACC0.B16]
|
| 734 |
+
VEOR ACC1.B16, ACC1.B16, ACC1.B16
|
| 735 |
+
VEOR ACCM.B16, ACCM.B16, ACCM.B16
|
| 736 |
+
// Prepare initial counter, and the increment vector
|
| 737 |
+
VLD1 (ctrPtr), [CTR.B16]
|
| 738 |
+
VEOR INC.B16, INC.B16, INC.B16
|
| 739 |
+
MOVD $1, H0
|
| 740 |
+
VMOV H0, INC.S[3]
|
| 741 |
+
VREV32 CTR.B16, CTR.B16
|
| 742 |
+
VADD CTR.S4, INC.S4, CTR.S4
|
| 743 |
+
|
| 744 |
+
MOVD ks, H0
|
| 745 |
+
// For AES-128 round keys are stored in: K0 .. K10, KLAST
|
| 746 |
+
VLD1.P 64(H0), [K0.B16, K1.B16, K2.B16, K3.B16]
|
| 747 |
+
VLD1.P 64(H0), [K4.B16, K5.B16, K6.B16, K7.B16]
|
| 748 |
+
VLD1.P 48(H0), [K8.B16, K9.B16, K10.B16]
|
| 749 |
+
VMOV K10.B16, KLAST.B16
|
| 750 |
+
|
| 751 |
+
// Skip to <8 blocks loop
|
| 752 |
+
CMP $128, srcPtrLen
|
| 753 |
+
BLT startSingles
|
| 754 |
+
// There are at least 8 blocks to encrypt
|
| 755 |
+
TBZ $4, NR, octetsLoop
|
| 756 |
+
|
| 757 |
+
// For AES-192 round keys occupy: K0 .. K7, K10, K11, K8, K9, KLAST
|
| 758 |
+
VMOV K8.B16, K10.B16
|
| 759 |
+
VMOV K9.B16, K11.B16
|
| 760 |
+
VMOV KLAST.B16, K8.B16
|
| 761 |
+
VLD1.P 16(H0), [K9.B16]
|
| 762 |
+
VLD1.P 16(H0), [KLAST.B16]
|
| 763 |
+
TBZ $3, NR, octetsLoop
|
| 764 |
+
// For AES-256 round keys occupy: K0 .. K7, K10, K11, mem, mem, K8, K9, KLAST
|
| 765 |
+
VMOV KLAST.B16, K8.B16
|
| 766 |
+
VLD1.P 16(H0), [K9.B16]
|
| 767 |
+
VLD1.P 16(H0), [KLAST.B16]
|
| 768 |
+
ADD $10*16, ks, H0
|
| 769 |
+
MOVD H0, curK
|
| 770 |
+
|
| 771 |
+
octetsLoop:
|
| 772 |
+
SUB $128, srcPtrLen
|
| 773 |
+
|
| 774 |
+
VMOV CTR.B16, B0.B16
|
| 775 |
+
VADD B0.S4, INC.S4, B1.S4
|
| 776 |
+
VREV32 B0.B16, B0.B16
|
| 777 |
+
VADD B1.S4, INC.S4, B2.S4
|
| 778 |
+
VREV32 B1.B16, B1.B16
|
| 779 |
+
VADD B2.S4, INC.S4, B3.S4
|
| 780 |
+
VREV32 B2.B16, B2.B16
|
| 781 |
+
VADD B3.S4, INC.S4, B4.S4
|
| 782 |
+
VREV32 B3.B16, B3.B16
|
| 783 |
+
VADD B4.S4, INC.S4, B5.S4
|
| 784 |
+
VREV32 B4.B16, B4.B16
|
| 785 |
+
VADD B5.S4, INC.S4, B6.S4
|
| 786 |
+
VREV32 B5.B16, B5.B16
|
| 787 |
+
VADD B6.S4, INC.S4, B7.S4
|
| 788 |
+
VREV32 B6.B16, B6.B16
|
| 789 |
+
VADD B7.S4, INC.S4, CTR.S4
|
| 790 |
+
VREV32 B7.B16, B7.B16
|
| 791 |
+
|
| 792 |
+
aesrndx8(K0)
|
| 793 |
+
aesrndx8(K1)
|
| 794 |
+
aesrndx8(K2)
|
| 795 |
+
aesrndx8(K3)
|
| 796 |
+
aesrndx8(K4)
|
| 797 |
+
aesrndx8(K5)
|
| 798 |
+
aesrndx8(K6)
|
| 799 |
+
aesrndx8(K7)
|
| 800 |
+
TBZ $4, NR, octetsFinish
|
| 801 |
+
aesrndx8(K10)
|
| 802 |
+
aesrndx8(K11)
|
| 803 |
+
TBZ $3, NR, octetsFinish
|
| 804 |
+
VLD1.P 32(curK), [T1.B16, T2.B16]
|
| 805 |
+
aesrndx8(T1)
|
| 806 |
+
aesrndx8(T2)
|
| 807 |
+
MOVD H0, curK
|
| 808 |
+
octetsFinish:
|
| 809 |
+
aesrndx8(K8)
|
| 810 |
+
aesrndlastx8(K9)
|
| 811 |
+
|
| 812 |
+
VEOR KLAST.B16, B0.B16, T1.B16
|
| 813 |
+
VEOR KLAST.B16, B1.B16, T2.B16
|
| 814 |
+
VEOR KLAST.B16, B2.B16, B2.B16
|
| 815 |
+
VEOR KLAST.B16, B3.B16, B3.B16
|
| 816 |
+
VEOR KLAST.B16, B4.B16, B4.B16
|
| 817 |
+
VEOR KLAST.B16, B5.B16, B5.B16
|
| 818 |
+
VEOR KLAST.B16, B6.B16, B6.B16
|
| 819 |
+
VEOR KLAST.B16, B7.B16, B7.B16
|
| 820 |
+
|
| 821 |
+
VLD1.P 32(srcPtr), [B0.B16, B1.B16]
|
| 822 |
+
VEOR B0.B16, T1.B16, T1.B16
|
| 823 |
+
VEOR B1.B16, T2.B16, T2.B16
|
| 824 |
+
VST1.P [T1.B16, T2.B16], 32(dstPtr)
|
| 825 |
+
|
| 826 |
+
VLD1.P 32(pTbl), [T1.B16, T2.B16]
|
| 827 |
+
VREV64 B0.B16, B0.B16
|
| 828 |
+
VEOR ACC0.B16, B0.B16, B0.B16
|
| 829 |
+
VEXT $8, B0.B16, B0.B16, T0.B16
|
| 830 |
+
VEOR B0.B16, T0.B16, T0.B16
|
| 831 |
+
VPMULL B0.D1, T1.D1, ACC1.Q1
|
| 832 |
+
VPMULL2 B0.D2, T1.D2, ACC0.Q1
|
| 833 |
+
VPMULL T0.D1, T2.D1, ACCM.Q1
|
| 834 |
+
mulRound(B1)
|
| 835 |
+
|
| 836 |
+
VLD1.P 32(srcPtr), [B0.B16, B1.B16]
|
| 837 |
+
VEOR B2.B16, B0.B16, T1.B16
|
| 838 |
+
VEOR B3.B16, B1.B16, T2.B16
|
| 839 |
+
VST1.P [T1.B16, T2.B16], 32(dstPtr)
|
| 840 |
+
mulRound(B0)
|
| 841 |
+
mulRound(B1)
|
| 842 |
+
|
| 843 |
+
VLD1.P 32(srcPtr), [B0.B16, B1.B16]
|
| 844 |
+
VEOR B4.B16, B0.B16, T1.B16
|
| 845 |
+
VEOR B5.B16, B1.B16, T2.B16
|
| 846 |
+
VST1.P [T1.B16, T2.B16], 32(dstPtr)
|
| 847 |
+
mulRound(B0)
|
| 848 |
+
mulRound(B1)
|
| 849 |
+
|
| 850 |
+
VLD1.P 32(srcPtr), [B0.B16, B1.B16]
|
| 851 |
+
VEOR B6.B16, B0.B16, T1.B16
|
| 852 |
+
VEOR B7.B16, B1.B16, T2.B16
|
| 853 |
+
VST1.P [T1.B16, T2.B16], 32(dstPtr)
|
| 854 |
+
mulRound(B0)
|
| 855 |
+
mulRound(B1)
|
| 856 |
+
|
| 857 |
+
MOVD pTblSave, pTbl
|
| 858 |
+
reduce()
|
| 859 |
+
|
| 860 |
+
CMP $128, srcPtrLen
|
| 861 |
+
BGE octetsLoop
|
| 862 |
+
|
| 863 |
+
startSingles:
|
| 864 |
+
CBZ srcPtrLen, done
|
| 865 |
+
ADD $14*16, pTbl
|
| 866 |
+
// Preload H and its Karatsuba precomp
|
| 867 |
+
VLD1.P (pTbl), [T1.B16, T2.B16]
|
| 868 |
+
// Preload AES round keys
|
| 869 |
+
ADD $128, ks
|
| 870 |
+
VLD1.P 48(ks), [K8.B16, K9.B16, K10.B16]
|
| 871 |
+
VMOV K10.B16, KLAST.B16
|
| 872 |
+
TBZ $4, NR, singlesLoop
|
| 873 |
+
VLD1.P 32(ks), [B1.B16, B2.B16]
|
| 874 |
+
VMOV B2.B16, KLAST.B16
|
| 875 |
+
TBZ $3, NR, singlesLoop
|
| 876 |
+
VLD1.P 32(ks), [B3.B16, B4.B16]
|
| 877 |
+
VMOV B4.B16, KLAST.B16
|
| 878 |
+
|
| 879 |
+
singlesLoop:
|
| 880 |
+
CMP $16, srcPtrLen
|
| 881 |
+
BLT tail
|
| 882 |
+
SUB $16, srcPtrLen
|
| 883 |
+
|
| 884 |
+
VLD1.P 16(srcPtr), [T0.B16]
|
| 885 |
+
VREV64 T0.B16, B5.B16
|
| 886 |
+
VEOR KLAST.B16, T0.B16, T0.B16
|
| 887 |
+
|
| 888 |
+
VREV32 CTR.B16, B0.B16
|
| 889 |
+
VADD CTR.S4, INC.S4, CTR.S4
|
| 890 |
+
|
| 891 |
+
AESE K0.B16, B0.B16
|
| 892 |
+
AESMC B0.B16, B0.B16
|
| 893 |
+
AESE K1.B16, B0.B16
|
| 894 |
+
AESMC B0.B16, B0.B16
|
| 895 |
+
AESE K2.B16, B0.B16
|
| 896 |
+
AESMC B0.B16, B0.B16
|
| 897 |
+
AESE K3.B16, B0.B16
|
| 898 |
+
AESMC B0.B16, B0.B16
|
| 899 |
+
AESE K4.B16, B0.B16
|
| 900 |
+
AESMC B0.B16, B0.B16
|
| 901 |
+
AESE K5.B16, B0.B16
|
| 902 |
+
AESMC B0.B16, B0.B16
|
| 903 |
+
AESE K6.B16, B0.B16
|
| 904 |
+
AESMC B0.B16, B0.B16
|
| 905 |
+
AESE K7.B16, B0.B16
|
| 906 |
+
AESMC B0.B16, B0.B16
|
| 907 |
+
AESE K8.B16, B0.B16
|
| 908 |
+
AESMC B0.B16, B0.B16
|
| 909 |
+
AESE K9.B16, B0.B16
|
| 910 |
+
TBZ $4, NR, singlesLast
|
| 911 |
+
AESMC B0.B16, B0.B16
|
| 912 |
+
AESE K10.B16, B0.B16
|
| 913 |
+
AESMC B0.B16, B0.B16
|
| 914 |
+
AESE B1.B16, B0.B16
|
| 915 |
+
TBZ $3, NR, singlesLast
|
| 916 |
+
AESMC B0.B16, B0.B16
|
| 917 |
+
AESE B2.B16, B0.B16
|
| 918 |
+
AESMC B0.B16, B0.B16
|
| 919 |
+
AESE B3.B16, B0.B16
|
| 920 |
+
singlesLast:
|
| 921 |
+
VEOR T0.B16, B0.B16, B0.B16
|
| 922 |
+
|
| 923 |
+
VST1.P [B0.B16], 16(dstPtr)
|
| 924 |
+
|
| 925 |
+
VEOR ACC0.B16, B5.B16, B5.B16
|
| 926 |
+
VEXT $8, B5.B16, B5.B16, T0.B16
|
| 927 |
+
VEOR B5.B16, T0.B16, T0.B16
|
| 928 |
+
VPMULL B5.D1, T1.D1, ACC1.Q1
|
| 929 |
+
VPMULL2 B5.D2, T1.D2, ACC0.Q1
|
| 930 |
+
VPMULL T0.D1, T2.D1, ACCM.Q1
|
| 931 |
+
reduce()
|
| 932 |
+
|
| 933 |
+
B singlesLoop
|
| 934 |
+
tail:
|
| 935 |
+
CBZ srcPtrLen, done
|
| 936 |
+
|
| 937 |
+
VREV32 CTR.B16, B0.B16
|
| 938 |
+
VADD CTR.S4, INC.S4, CTR.S4
|
| 939 |
+
|
| 940 |
+
AESE K0.B16, B0.B16
|
| 941 |
+
AESMC B0.B16, B0.B16
|
| 942 |
+
AESE K1.B16, B0.B16
|
| 943 |
+
AESMC B0.B16, B0.B16
|
| 944 |
+
AESE K2.B16, B0.B16
|
| 945 |
+
AESMC B0.B16, B0.B16
|
| 946 |
+
AESE K3.B16, B0.B16
|
| 947 |
+
AESMC B0.B16, B0.B16
|
| 948 |
+
AESE K4.B16, B0.B16
|
| 949 |
+
AESMC B0.B16, B0.B16
|
| 950 |
+
AESE K5.B16, B0.B16
|
| 951 |
+
AESMC B0.B16, B0.B16
|
| 952 |
+
AESE K6.B16, B0.B16
|
| 953 |
+
AESMC B0.B16, B0.B16
|
| 954 |
+
AESE K7.B16, B0.B16
|
| 955 |
+
AESMC B0.B16, B0.B16
|
| 956 |
+
AESE K8.B16, B0.B16
|
| 957 |
+
AESMC B0.B16, B0.B16
|
| 958 |
+
AESE K9.B16, B0.B16
|
| 959 |
+
TBZ $4, NR, tailLast
|
| 960 |
+
AESMC B0.B16, B0.B16
|
| 961 |
+
AESE K10.B16, B0.B16
|
| 962 |
+
AESMC B0.B16, B0.B16
|
| 963 |
+
AESE B1.B16, B0.B16
|
| 964 |
+
TBZ $3, NR, tailLast
|
| 965 |
+
AESMC B0.B16, B0.B16
|
| 966 |
+
AESE B2.B16, B0.B16
|
| 967 |
+
AESMC B0.B16, B0.B16
|
| 968 |
+
AESE B3.B16, B0.B16
|
| 969 |
+
tailLast:
|
| 970 |
+
VEOR KLAST.B16, B0.B16, B0.B16
|
| 971 |
+
|
| 972 |
+
// Assuming it is safe to load past dstPtr due to the presence of the tag
|
| 973 |
+
VLD1 (srcPtr), [B5.B16]
|
| 974 |
+
|
| 975 |
+
VEOR B5.B16, B0.B16, B0.B16
|
| 976 |
+
|
| 977 |
+
VEOR T3.B16, T3.B16, T3.B16
|
| 978 |
+
MOVD $0, H1
|
| 979 |
+
SUB $1, H1
|
| 980 |
+
|
| 981 |
+
TBZ $3, srcPtrLen, ld4
|
| 982 |
+
VMOV B0.D[0], H0
|
| 983 |
+
MOVD.P H0, 8(dstPtr)
|
| 984 |
+
VMOV H1, T3.D[0]
|
| 985 |
+
VEXT $8, ZERO.B16, B0.B16, B0.B16
|
| 986 |
+
ld4:
|
| 987 |
+
TBZ $2, srcPtrLen, ld2
|
| 988 |
+
VMOV B0.S[0], H0
|
| 989 |
+
MOVW.P H0, 4(dstPtr)
|
| 990 |
+
VEXT $12, T3.B16, ZERO.B16, T3.B16
|
| 991 |
+
VMOV H1, T3.S[0]
|
| 992 |
+
VEXT $4, ZERO.B16, B0.B16, B0.B16
|
| 993 |
+
ld2:
|
| 994 |
+
TBZ $1, srcPtrLen, ld1
|
| 995 |
+
VMOV B0.H[0], H0
|
| 996 |
+
MOVH.P H0, 2(dstPtr)
|
| 997 |
+
VEXT $14, T3.B16, ZERO.B16, T3.B16
|
| 998 |
+
VMOV H1, T3.H[0]
|
| 999 |
+
VEXT $2, ZERO.B16, B0.B16, B0.B16
|
| 1000 |
+
ld1:
|
| 1001 |
+
TBZ $0, srcPtrLen, ld0
|
| 1002 |
+
VMOV B0.B[0], H0
|
| 1003 |
+
MOVB.P H0, 1(dstPtr)
|
| 1004 |
+
VEXT $15, T3.B16, ZERO.B16, T3.B16
|
| 1005 |
+
VMOV H1, T3.B[0]
|
| 1006 |
+
ld0:
|
| 1007 |
+
|
| 1008 |
+
VAND T3.B16, B5.B16, B5.B16
|
| 1009 |
+
VREV64 B5.B16, B5.B16
|
| 1010 |
+
|
| 1011 |
+
VEOR ACC0.B16, B5.B16, B5.B16
|
| 1012 |
+
VEXT $8, B5.B16, B5.B16, T0.B16
|
| 1013 |
+
VEOR B5.B16, T0.B16, T0.B16
|
| 1014 |
+
VPMULL B5.D1, T1.D1, ACC1.Q1
|
| 1015 |
+
VPMULL2 B5.D2, T1.D2, ACC0.Q1
|
| 1016 |
+
VPMULL T0.D1, T2.D1, ACCM.Q1
|
| 1017 |
+
reduce()
|
| 1018 |
+
done:
|
| 1019 |
+
VST1 [ACC0.B16], (tPtr)
|
| 1020 |
+
|
| 1021 |
+
RET
|
platform/dbops/binaries/go/go/src/crypto/aes/gcm_ppc64x.go
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2019 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
//go:build ppc64le || ppc64
|
| 6 |
+
|
| 7 |
+
package aes
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"crypto/cipher"
|
| 11 |
+
"crypto/subtle"
|
| 12 |
+
"encoding/binary"
|
| 13 |
+
"errors"
|
| 14 |
+
"runtime"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
// This file implements GCM using an optimized GHASH function.
|
| 18 |
+
|
| 19 |
+
//go:noescape
|
| 20 |
+
func gcmInit(productTable *[256]byte, h []byte)
|
| 21 |
+
|
| 22 |
+
//go:noescape
|
| 23 |
+
func gcmHash(output []byte, productTable *[256]byte, inp []byte, len int)
|
| 24 |
+
|
| 25 |
+
//go:noescape
|
| 26 |
+
func gcmMul(output []byte, productTable *[256]byte)
|
| 27 |
+
|
| 28 |
+
const (
|
| 29 |
+
gcmCounterSize = 16
|
| 30 |
+
gcmBlockSize = 16
|
| 31 |
+
gcmTagSize = 16
|
| 32 |
+
gcmStandardNonceSize = 12
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
var errOpen = errors.New("cipher: message authentication failed")
|
| 36 |
+
|
| 37 |
+
// Assert that aesCipherGCM implements the gcmAble interface.
|
| 38 |
+
var _ gcmAble = (*aesCipherAsm)(nil)
|
| 39 |
+
|
| 40 |
+
type gcmAsm struct {
|
| 41 |
+
cipher *aesCipherAsm
|
| 42 |
+
// ks is the key schedule, the length of which depends on the size of
|
| 43 |
+
// the AES key.
|
| 44 |
+
ks []uint32
|
| 45 |
+
// productTable contains pre-computed multiples of the binary-field
|
| 46 |
+
// element used in GHASH.
|
| 47 |
+
productTable [256]byte
|
| 48 |
+
// nonceSize contains the expected size of the nonce, in bytes.
|
| 49 |
+
nonceSize int
|
| 50 |
+
// tagSize contains the size of the tag, in bytes.
|
| 51 |
+
tagSize int
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
func counterCryptASM(nr int, out, in []byte, counter *[gcmBlockSize]byte, key *uint32)
|
| 55 |
+
|
| 56 |
+
// NewGCM returns the AES cipher wrapped in Galois Counter Mode. This is only
|
| 57 |
+
// called by [crypto/cipher.NewGCM] via the gcmAble interface.
|
| 58 |
+
func (c *aesCipherAsm) NewGCM(nonceSize, tagSize int) (cipher.AEAD, error) {
|
| 59 |
+
var h1, h2 uint64
|
| 60 |
+
g := &gcmAsm{cipher: c, ks: c.enc, nonceSize: nonceSize, tagSize: tagSize}
|
| 61 |
+
|
| 62 |
+
hle := make([]byte, gcmBlockSize)
|
| 63 |
+
|
| 64 |
+
c.Encrypt(hle, hle)
|
| 65 |
+
|
| 66 |
+
// Reverse the bytes in each 8 byte chunk
|
| 67 |
+
// Load little endian, store big endian
|
| 68 |
+
if runtime.GOARCH == "ppc64le" {
|
| 69 |
+
h1 = binary.LittleEndian.Uint64(hle[:8])
|
| 70 |
+
h2 = binary.LittleEndian.Uint64(hle[8:])
|
| 71 |
+
} else {
|
| 72 |
+
h1 = binary.BigEndian.Uint64(hle[:8])
|
| 73 |
+
h2 = binary.BigEndian.Uint64(hle[8:])
|
| 74 |
+
}
|
| 75 |
+
binary.BigEndian.PutUint64(hle[:8], h1)
|
| 76 |
+
binary.BigEndian.PutUint64(hle[8:], h2)
|
| 77 |
+
gcmInit(&g.productTable, hle)
|
| 78 |
+
|
| 79 |
+
return g, nil
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
func (g *gcmAsm) NonceSize() int {
|
| 83 |
+
return g.nonceSize
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
func (g *gcmAsm) Overhead() int {
|
| 87 |
+
return g.tagSize
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
func sliceForAppend(in []byte, n int) (head, tail []byte) {
|
| 91 |
+
if total := len(in) + n; cap(in) >= total {
|
| 92 |
+
head = in[:total]
|
| 93 |
+
} else {
|
| 94 |
+
head = make([]byte, total)
|
| 95 |
+
copy(head, in)
|
| 96 |
+
}
|
| 97 |
+
tail = head[len(in):]
|
| 98 |
+
return
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
// deriveCounter computes the initial GCM counter state from the given nonce.
|
| 102 |
+
func (g *gcmAsm) deriveCounter(counter *[gcmBlockSize]byte, nonce []byte) {
|
| 103 |
+
if len(nonce) == gcmStandardNonceSize {
|
| 104 |
+
copy(counter[:], nonce)
|
| 105 |
+
counter[gcmBlockSize-1] = 1
|
| 106 |
+
} else {
|
| 107 |
+
var hash [16]byte
|
| 108 |
+
g.paddedGHASH(&hash, nonce)
|
| 109 |
+
lens := gcmLengths(0, uint64(len(nonce))*8)
|
| 110 |
+
g.paddedGHASH(&hash, lens[:])
|
| 111 |
+
copy(counter[:], hash[:])
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
// counterCrypt encrypts in using AES in counter mode and places the result
|
| 116 |
+
// into out. counter is the initial count value and will be updated with the next
|
| 117 |
+
// count value. The length of out must be greater than or equal to the length
|
| 118 |
+
// of in.
|
| 119 |
+
// counterCryptASM implements counterCrypt which then allows the loop to
|
| 120 |
+
// be unrolled and optimized.
|
| 121 |
+
func (g *gcmAsm) counterCrypt(out, in []byte, counter *[gcmBlockSize]byte) {
|
| 122 |
+
counterCryptASM(len(g.cipher.enc)/4-1, out, in, counter, &g.cipher.enc[0])
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// increments the rightmost 32-bits of the count value by 1.
|
| 126 |
+
func gcmInc32(counterBlock *[16]byte) {
|
| 127 |
+
c := counterBlock[len(counterBlock)-4:]
|
| 128 |
+
x := binary.BigEndian.Uint32(c) + 1
|
| 129 |
+
binary.BigEndian.PutUint32(c, x)
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
// paddedGHASH pads data with zeroes until its length is a multiple of
|
| 133 |
+
// 16-bytes. It then calculates a new value for hash using the ghash
|
| 134 |
+
// algorithm.
|
| 135 |
+
func (g *gcmAsm) paddedGHASH(hash *[16]byte, data []byte) {
|
| 136 |
+
if siz := len(data) - (len(data) % gcmBlockSize); siz > 0 {
|
| 137 |
+
gcmHash(hash[:], &g.productTable, data[:], siz)
|
| 138 |
+
data = data[siz:]
|
| 139 |
+
}
|
| 140 |
+
if len(data) > 0 {
|
| 141 |
+
var s [16]byte
|
| 142 |
+
copy(s[:], data)
|
| 143 |
+
gcmHash(hash[:], &g.productTable, s[:], len(s))
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
// auth calculates GHASH(ciphertext, additionalData), masks the result with
|
| 148 |
+
// tagMask and writes the result to out.
|
| 149 |
+
func (g *gcmAsm) auth(out, ciphertext, aad []byte, tagMask *[gcmTagSize]byte) {
|
| 150 |
+
var hash [16]byte
|
| 151 |
+
g.paddedGHASH(&hash, aad)
|
| 152 |
+
g.paddedGHASH(&hash, ciphertext)
|
| 153 |
+
lens := gcmLengths(uint64(len(aad))*8, uint64(len(ciphertext))*8)
|
| 154 |
+
g.paddedGHASH(&hash, lens[:])
|
| 155 |
+
|
| 156 |
+
copy(out, hash[:])
|
| 157 |
+
for i := range out {
|
| 158 |
+
out[i] ^= tagMask[i]
|
| 159 |
+
}
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
// Seal encrypts and authenticates plaintext. See the [cipher.AEAD] interface for
|
| 163 |
+
// details.
|
| 164 |
+
func (g *gcmAsm) Seal(dst, nonce, plaintext, data []byte) []byte {
|
| 165 |
+
if len(nonce) != g.nonceSize {
|
| 166 |
+
panic("cipher: incorrect nonce length given to GCM")
|
| 167 |
+
}
|
| 168 |
+
if uint64(len(plaintext)) > ((1<<32)-2)*BlockSize {
|
| 169 |
+
panic("cipher: message too large for GCM")
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
ret, out := sliceForAppend(dst, len(plaintext)+g.tagSize)
|
| 173 |
+
|
| 174 |
+
var counter, tagMask [gcmBlockSize]byte
|
| 175 |
+
g.deriveCounter(&counter, nonce)
|
| 176 |
+
|
| 177 |
+
g.cipher.Encrypt(tagMask[:], counter[:])
|
| 178 |
+
gcmInc32(&counter)
|
| 179 |
+
|
| 180 |
+
g.counterCrypt(out, plaintext, &counter)
|
| 181 |
+
g.auth(out[len(plaintext):], out[:len(plaintext)], data, &tagMask)
|
| 182 |
+
|
| 183 |
+
return ret
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
// Open authenticates and decrypts ciphertext. See the [cipher.AEAD] interface
|
| 187 |
+
// for details.
|
| 188 |
+
func (g *gcmAsm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
|
| 189 |
+
if len(nonce) != g.nonceSize {
|
| 190 |
+
panic("cipher: incorrect nonce length given to GCM")
|
| 191 |
+
}
|
| 192 |
+
if len(ciphertext) < g.tagSize {
|
| 193 |
+
return nil, errOpen
|
| 194 |
+
}
|
| 195 |
+
if uint64(len(ciphertext)) > ((1<<32)-2)*uint64(BlockSize)+uint64(g.tagSize) {
|
| 196 |
+
return nil, errOpen
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
tag := ciphertext[len(ciphertext)-g.tagSize:]
|
| 200 |
+
ciphertext = ciphertext[:len(ciphertext)-g.tagSize]
|
| 201 |
+
|
| 202 |
+
var counter, tagMask [gcmBlockSize]byte
|
| 203 |
+
g.deriveCounter(&counter, nonce)
|
| 204 |
+
|
| 205 |
+
g.cipher.Encrypt(tagMask[:], counter[:])
|
| 206 |
+
gcmInc32(&counter)
|
| 207 |
+
|
| 208 |
+
var expectedTag [gcmTagSize]byte
|
| 209 |
+
g.auth(expectedTag[:], ciphertext, data, &tagMask)
|
| 210 |
+
|
| 211 |
+
ret, out := sliceForAppend(dst, len(ciphertext))
|
| 212 |
+
|
| 213 |
+
if subtle.ConstantTimeCompare(expectedTag[:g.tagSize], tag) != 1 {
|
| 214 |
+
for i := range out {
|
| 215 |
+
out[i] = 0
|
| 216 |
+
}
|
| 217 |
+
return nil, errOpen
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
g.counterCrypt(out, ciphertext, &counter)
|
| 221 |
+
return ret, nil
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
func gcmLengths(len0, len1 uint64) [16]byte {
|
| 225 |
+
return [16]byte{
|
| 226 |
+
byte(len0 >> 56),
|
| 227 |
+
byte(len0 >> 48),
|
| 228 |
+
byte(len0 >> 40),
|
| 229 |
+
byte(len0 >> 32),
|
| 230 |
+
byte(len0 >> 24),
|
| 231 |
+
byte(len0 >> 16),
|
| 232 |
+
byte(len0 >> 8),
|
| 233 |
+
byte(len0),
|
| 234 |
+
byte(len1 >> 56),
|
| 235 |
+
byte(len1 >> 48),
|
| 236 |
+
byte(len1 >> 40),
|
| 237 |
+
byte(len1 >> 32),
|
| 238 |
+
byte(len1 >> 24),
|
| 239 |
+
byte(len1 >> 16),
|
| 240 |
+
byte(len1 >> 8),
|
| 241 |
+
byte(len1),
|
| 242 |
+
}
|
| 243 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/aes/gcm_ppc64x.s
ADDED
|
@@ -0,0 +1,1103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2019 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
//go:build ppc64 || ppc64le
|
| 6 |
+
|
| 7 |
+
// Portions based on CRYPTOGAMS code with the following comment:
|
| 8 |
+
// # ====================================================================
|
| 9 |
+
// # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
|
| 10 |
+
// # project. The module is, however, dual licensed under OpenSSL and
|
| 11 |
+
// # CRYPTOGAMS licenses depending on where you obtain it. For further
|
| 12 |
+
// # details see http://www.openssl.org/~appro/cryptogams/.
|
| 13 |
+
// # ====================================================================
|
| 14 |
+
|
| 15 |
+
// The implementations for gcmHash, gcmInit and gcmMul are based on the generated asm
|
| 16 |
+
// from the script https://github.com/dot-asm/cryptogams/blob/master/ppc/ghashp8-ppc.pl
|
| 17 |
+
// from commit d47afb3c.
|
| 18 |
+
|
| 19 |
+
// Changes were made due to differences in the ABI and some register usage.
|
| 20 |
+
// Some arguments were changed due to the way the Go code passes them.
|
| 21 |
+
|
| 22 |
+
// Portions that use the stitched AES-GCM approach in counterCryptASM
|
| 23 |
+
// are based on code found in
|
| 24 |
+
// https://github.com/IBM/ipcri/blob/main/aes/p10_aes_gcm.s
|
| 25 |
+
|
| 26 |
+
#include "textflag.h"
|
| 27 |
+
|
| 28 |
+
#define XIP R3
|
| 29 |
+
#define HTBL R4
|
| 30 |
+
#define INP R5
|
| 31 |
+
#define LEN R6
|
| 32 |
+
|
| 33 |
+
#define XL V0
|
| 34 |
+
#define XM V1
|
| 35 |
+
#define XH V2
|
| 36 |
+
#define IN V3
|
| 37 |
+
#define ZERO V4
|
| 38 |
+
#define T0 V5
|
| 39 |
+
#define T1 V6
|
| 40 |
+
#define T2 V7
|
| 41 |
+
#define XC2 V8
|
| 42 |
+
#define H V9
|
| 43 |
+
#define HH V10
|
| 44 |
+
#define HL V11
|
| 45 |
+
#define LEMASK V12
|
| 46 |
+
#define XL1 V13
|
| 47 |
+
#define XM1 V14
|
| 48 |
+
#define XH1 V15
|
| 49 |
+
#define IN1 V16
|
| 50 |
+
#define H2 V17
|
| 51 |
+
#define H2H V18
|
| 52 |
+
#define H2L V19
|
| 53 |
+
#define XL3 V20
|
| 54 |
+
#define XM2 V21
|
| 55 |
+
#define IN2 V22
|
| 56 |
+
#define H3L V23
|
| 57 |
+
#define H3 V24
|
| 58 |
+
#define H3H V25
|
| 59 |
+
#define XH3 V26
|
| 60 |
+
#define XM3 V27
|
| 61 |
+
#define IN3 V28
|
| 62 |
+
#define H4L V29
|
| 63 |
+
#define H4 V30
|
| 64 |
+
#define H4H V31
|
| 65 |
+
|
| 66 |
+
#define IN0 IN
|
| 67 |
+
#define H21L HL
|
| 68 |
+
#define H21H HH
|
| 69 |
+
#define LOPERM H2L
|
| 70 |
+
#define HIPERM H2H
|
| 71 |
+
|
| 72 |
+
#define VXL VS32
|
| 73 |
+
#define VIN VS35
|
| 74 |
+
#define VXC2 VS40
|
| 75 |
+
#define VH VS41
|
| 76 |
+
#define VHH VS42
|
| 77 |
+
#define VHL VS43
|
| 78 |
+
#define VIN1 VS48
|
| 79 |
+
#define VH2 VS49
|
| 80 |
+
#define VH2H VS50
|
| 81 |
+
#define VH2L VS51
|
| 82 |
+
|
| 83 |
+
#define VIN2 VS54
|
| 84 |
+
#define VH3L VS55
|
| 85 |
+
#define VH3 VS56
|
| 86 |
+
#define VH3H VS57
|
| 87 |
+
#define VIN3 VS60
|
| 88 |
+
#define VH4L VS61
|
| 89 |
+
#define VH4 VS62
|
| 90 |
+
#define VH4H VS63
|
| 91 |
+
|
| 92 |
+
#define VIN0 VIN
|
| 93 |
+
|
| 94 |
+
#define ESPERM V10
|
| 95 |
+
#define TMP2 V11
|
| 96 |
+
|
| 97 |
+
// The following macros provide appropriate
|
| 98 |
+
// implementations for endianness as well as
|
| 99 |
+
// ISA specific for power8 and power9.
|
| 100 |
+
#ifdef GOARCH_ppc64le
|
| 101 |
+
# ifdef GOPPC64_power9
|
| 102 |
+
#define P8_LXVB16X(RA,RB,VT) LXVB16X (RA)(RB), VT
|
| 103 |
+
#define P8_STXVB16X(VS,RA,RB) STXVB16X VS, (RA)(RB)
|
| 104 |
+
# else
|
| 105 |
+
#define NEEDS_ESPERM
|
| 106 |
+
#define P8_LXVB16X(RA,RB,VT) \
|
| 107 |
+
LXVD2X (RA+RB), VT \
|
| 108 |
+
VPERM VT, VT, ESPERM, VT
|
| 109 |
+
|
| 110 |
+
#define P8_STXVB16X(VS,RA,RB) \
|
| 111 |
+
VPERM VS, VS, ESPERM, TMP2; \
|
| 112 |
+
STXVD2X TMP2, (RA+RB)
|
| 113 |
+
|
| 114 |
+
# endif
|
| 115 |
+
#else
|
| 116 |
+
#define P8_LXVB16X(RA,RB,VT) \
|
| 117 |
+
LXVD2X (RA+RB), VT
|
| 118 |
+
|
| 119 |
+
#define P8_STXVB16X(VS,RA,RB) \
|
| 120 |
+
STXVD2X VS, (RA+RB)
|
| 121 |
+
|
| 122 |
+
#endif
|
| 123 |
+
|
| 124 |
+
#define MASK_PTR R8
|
| 125 |
+
|
| 126 |
+
#define MASKV V0
|
| 127 |
+
#define INV V1
|
| 128 |
+
|
| 129 |
+
// The following macros are used for
|
| 130 |
+
// the stitched implementation within
|
| 131 |
+
// counterCryptASM.
|
| 132 |
+
|
| 133 |
+
// Load the initial GCM counter value
|
| 134 |
+
// in V30 and set up the counter increment
|
| 135 |
+
// in V31
|
| 136 |
+
#define SETUP_COUNTER \
|
| 137 |
+
P8_LXVB16X(COUNTER, R0, V30); \
|
| 138 |
+
VSPLTISB $1, V28; \
|
| 139 |
+
VXOR V31, V31, V31; \
|
| 140 |
+
VSLDOI $1, V31, V28, V31
|
| 141 |
+
|
| 142 |
+
// These macros set up the initial value
|
| 143 |
+
// for a single encryption, or 4 or 8
|
| 144 |
+
// stitched encryptions implemented
|
| 145 |
+
// with interleaving vciphers.
|
| 146 |
+
//
|
| 147 |
+
// The input value for each encryption
|
| 148 |
+
// is generated by XORing the counter
|
| 149 |
+
// from V30 with the first key in VS0
|
| 150 |
+
// and incrementing the counter.
|
| 151 |
+
//
|
| 152 |
+
// Single encryption in V15
|
| 153 |
+
#define GEN_VCIPHER_INPUT \
|
| 154 |
+
XXLOR VS0, VS0, V29 \
|
| 155 |
+
VXOR V30, V29, V15; \
|
| 156 |
+
VADDUWM V30, V31, V30
|
| 157 |
+
|
| 158 |
+
// 4 encryptions in V15 - V18
|
| 159 |
+
#define GEN_VCIPHER_4_INPUTS \
|
| 160 |
+
XXLOR VS0, VS0, V29; \
|
| 161 |
+
VXOR V30, V29, V15; \
|
| 162 |
+
VADDUWM V30, V31, V30; \
|
| 163 |
+
VXOR V30, V29, V16; \
|
| 164 |
+
VADDUWM V30, V31, V30; \
|
| 165 |
+
VXOR V30, V29, V17; \
|
| 166 |
+
VADDUWM V30, V31, V30; \
|
| 167 |
+
VXOR V30, V29, V18; \
|
| 168 |
+
VADDUWM V30, V31, V30
|
| 169 |
+
|
| 170 |
+
// 8 encryptions in V15 - V22
|
| 171 |
+
#define GEN_VCIPHER_8_INPUTS \
|
| 172 |
+
XXLOR VS0, VS0, V29; \
|
| 173 |
+
VXOR V30, V29, V15; \
|
| 174 |
+
VADDUWM V30, V31, V30; \
|
| 175 |
+
VXOR V30, V29, V16; \
|
| 176 |
+
VADDUWM V30, V31, V30; \
|
| 177 |
+
VXOR V30, V29, V17; \
|
| 178 |
+
VADDUWM V30, V31, V30; \
|
| 179 |
+
VXOR V30, V29, V18; \
|
| 180 |
+
VADDUWM V30, V31, V30; \
|
| 181 |
+
VXOR V30, V29, V19; \
|
| 182 |
+
VADDUWM V30, V31, V30; \
|
| 183 |
+
VXOR V30, V29, V20; \
|
| 184 |
+
VADDUWM V30, V31, V30; \
|
| 185 |
+
VXOR V30, V29, V21; \
|
| 186 |
+
VADDUWM V30, V31, V30; \
|
| 187 |
+
VXOR V30, V29, V22; \
|
| 188 |
+
VADDUWM V30, V31, V30
|
| 189 |
+
|
| 190 |
+
// Load the keys to be used for
|
| 191 |
+
// encryption based on key_len.
|
| 192 |
+
// Keys are in VS0 - VS14
|
| 193 |
+
// depending on key_len.
|
| 194 |
+
// Valid keys sizes are verified
|
| 195 |
+
// here. CR2 is set and used
|
| 196 |
+
// throughout to check key_len.
|
| 197 |
+
#define LOAD_KEYS(blk_key, key_len) \
|
| 198 |
+
MOVD $16, R16; \
|
| 199 |
+
MOVD $32, R17; \
|
| 200 |
+
MOVD $48, R18; \
|
| 201 |
+
MOVD $64, R19; \
|
| 202 |
+
LXVD2X (blk_key)(R0), VS0; \
|
| 203 |
+
LXVD2X (blk_key)(R16), VS1; \
|
| 204 |
+
LXVD2X (blk_key)(R17), VS2; \
|
| 205 |
+
LXVD2X (blk_key)(R18), VS3; \
|
| 206 |
+
LXVD2X (blk_key)(R19), VS4; \
|
| 207 |
+
ADD $64, R16; \
|
| 208 |
+
ADD $64, R17; \
|
| 209 |
+
ADD $64, R18; \
|
| 210 |
+
ADD $64, R19; \
|
| 211 |
+
LXVD2X (blk_key)(R16), VS5; \
|
| 212 |
+
LXVD2X (blk_key)(R17), VS6; \
|
| 213 |
+
LXVD2X (blk_key)(R18), VS7; \
|
| 214 |
+
LXVD2X (blk_key)(R19), VS8; \
|
| 215 |
+
ADD $64, R16; \
|
| 216 |
+
ADD $64, R17; \
|
| 217 |
+
ADD $64, R18; \
|
| 218 |
+
ADD $64, R19; \
|
| 219 |
+
LXVD2X (blk_key)(R16), VS9; \
|
| 220 |
+
LXVD2X (blk_key)(R17), VS10; \
|
| 221 |
+
CMP key_len, $12, CR2; \
|
| 222 |
+
CMP key_len, $10; \
|
| 223 |
+
BEQ keysLoaded; \
|
| 224 |
+
LXVD2X (blk_key)(R18), VS11; \
|
| 225 |
+
LXVD2X (blk_key)(R19), VS12; \
|
| 226 |
+
BEQ CR2, keysLoaded; \
|
| 227 |
+
ADD $64, R16; \
|
| 228 |
+
ADD $64, R17; \
|
| 229 |
+
LXVD2X (blk_key)(R16), VS13; \
|
| 230 |
+
LXVD2X (blk_key)(R17), VS14; \
|
| 231 |
+
CMP key_len, $14; \
|
| 232 |
+
BEQ keysLoaded; \
|
| 233 |
+
MOVD R0,0(R0); \
|
| 234 |
+
keysLoaded:
|
| 235 |
+
|
| 236 |
+
// Encrypt 1 (vin) with first 9
|
| 237 |
+
// keys from VS1 - VS9.
|
| 238 |
+
#define VCIPHER_1X9_KEYS(vin) \
|
| 239 |
+
XXLOR VS1, VS1, V23; \
|
| 240 |
+
XXLOR VS2, VS2, V24; \
|
| 241 |
+
XXLOR VS3, VS3, V25; \
|
| 242 |
+
XXLOR VS4, VS4, V26; \
|
| 243 |
+
XXLOR VS5, VS5, V27; \
|
| 244 |
+
VCIPHER vin, V23, vin; \
|
| 245 |
+
VCIPHER vin, V24, vin; \
|
| 246 |
+
VCIPHER vin, V25, vin; \
|
| 247 |
+
VCIPHER vin, V26, vin; \
|
| 248 |
+
VCIPHER vin, V27, vin; \
|
| 249 |
+
XXLOR VS6, VS6, V23; \
|
| 250 |
+
XXLOR VS7, VS7, V24; \
|
| 251 |
+
XXLOR VS8, VS8, V25; \
|
| 252 |
+
XXLOR VS9, VS9, V26; \
|
| 253 |
+
VCIPHER vin, V23, vin; \
|
| 254 |
+
VCIPHER vin, V24, vin; \
|
| 255 |
+
VCIPHER vin, V25, vin; \
|
| 256 |
+
VCIPHER vin, V26, vin
|
| 257 |
+
|
| 258 |
+
// Encrypt 1 value (vin) with
|
| 259 |
+
// 2 specified keys
|
| 260 |
+
#define VCIPHER_1X2_KEYS(vin, key1, key2) \
|
| 261 |
+
XXLOR key1, key1, V25; \
|
| 262 |
+
XXLOR key2, key2, V26; \
|
| 263 |
+
VCIPHER vin, V25, vin; \
|
| 264 |
+
VCIPHER vin, V26, vin
|
| 265 |
+
|
| 266 |
+
// Encrypt 4 values in V15 - V18
|
| 267 |
+
// with the specified key from
|
| 268 |
+
// VS1 - VS9.
|
| 269 |
+
#define VCIPHER_4X1_KEY(key) \
|
| 270 |
+
XXLOR key, key, V23; \
|
| 271 |
+
VCIPHER V15, V23, V15; \
|
| 272 |
+
VCIPHER V16, V23, V16; \
|
| 273 |
+
VCIPHER V17, V23, V17; \
|
| 274 |
+
VCIPHER V18, V23, V18
|
| 275 |
+
|
| 276 |
+
// Encrypt 8 values in V15 - V22
|
| 277 |
+
// with the specified key,
|
| 278 |
+
// assuming it is a VSreg
|
| 279 |
+
#define VCIPHER_8X1_KEY(key) \
|
| 280 |
+
XXLOR key, key, V23; \
|
| 281 |
+
VCIPHER V15, V23, V15; \
|
| 282 |
+
VCIPHER V16, V23, V16; \
|
| 283 |
+
VCIPHER V17, V23, V17; \
|
| 284 |
+
VCIPHER V18, V23, V18; \
|
| 285 |
+
VCIPHER V19, V23, V19; \
|
| 286 |
+
VCIPHER V20, V23, V20; \
|
| 287 |
+
VCIPHER V21, V23, V21; \
|
| 288 |
+
VCIPHER V22, V23, V22
|
| 289 |
+
|
| 290 |
+
// Load input block into V1-V4
|
| 291 |
+
// in big endian order and
|
| 292 |
+
// update blk_inp by 64.
|
| 293 |
+
#define LOAD_INPUT_BLOCK64(blk_inp) \
|
| 294 |
+
MOVD $16, R16; \
|
| 295 |
+
MOVD $32, R17; \
|
| 296 |
+
MOVD $48, R18; \
|
| 297 |
+
P8_LXVB16X(blk_inp,R0,V1); \
|
| 298 |
+
P8_LXVB16X(blk_inp,R16,V2); \
|
| 299 |
+
P8_LXVB16X(blk_inp,R17,V3); \
|
| 300 |
+
P8_LXVB16X(blk_inp,R18,V4); \
|
| 301 |
+
ADD $64, blk_inp
|
| 302 |
+
|
| 303 |
+
// Load input block into V1-V8
|
| 304 |
+
// in big endian order and
|
| 305 |
+
// Update blk_inp by 128
|
| 306 |
+
#define LOAD_INPUT_BLOCK128(blk_inp) \
|
| 307 |
+
MOVD $16, R16; \
|
| 308 |
+
MOVD $32, R17; \
|
| 309 |
+
MOVD $48, R18; \
|
| 310 |
+
MOVD $64, R19; \
|
| 311 |
+
MOVD $80, R20; \
|
| 312 |
+
MOVD $96, R21; \
|
| 313 |
+
MOVD $112, R22; \
|
| 314 |
+
P8_LXVB16X(blk_inp,R0,V1); \
|
| 315 |
+
P8_LXVB16X(blk_inp,R16,V2); \
|
| 316 |
+
P8_LXVB16X(blk_inp,R17,V3); \
|
| 317 |
+
P8_LXVB16X(blk_inp,R18,V4); \
|
| 318 |
+
P8_LXVB16X(blk_inp,R19,V5); \
|
| 319 |
+
P8_LXVB16X(blk_inp,R20,V6); \
|
| 320 |
+
P8_LXVB16X(blk_inp,R21,V7); \
|
| 321 |
+
P8_LXVB16X(blk_inp,R22,V8); \
|
| 322 |
+
ADD $128, blk_inp
|
| 323 |
+
|
| 324 |
+
// Finish encryption on 8 streams and
|
| 325 |
+
// XOR with input block
|
| 326 |
+
#define VCIPHERLAST8_XOR_INPUT \
|
| 327 |
+
VCIPHERLAST V15, V23, V15; \
|
| 328 |
+
VCIPHERLAST V16, V23, V16; \
|
| 329 |
+
VCIPHERLAST V17, V23, V17; \
|
| 330 |
+
VCIPHERLAST V18, V23, V18; \
|
| 331 |
+
VCIPHERLAST V19, V23, V19; \
|
| 332 |
+
VCIPHERLAST V20, V23, V20; \
|
| 333 |
+
VCIPHERLAST V21, V23, V21; \
|
| 334 |
+
VCIPHERLAST V22, V23, V22; \
|
| 335 |
+
XXLXOR V1, V15, V1; \
|
| 336 |
+
XXLXOR V2, V16, V2; \
|
| 337 |
+
XXLXOR V3, V17, V3; \
|
| 338 |
+
XXLXOR V4, V18, V4; \
|
| 339 |
+
XXLXOR V5, V19, V5; \
|
| 340 |
+
XXLXOR V6, V20, V6; \
|
| 341 |
+
XXLXOR V7, V21, V7; \
|
| 342 |
+
XXLXOR V8, V22, V8
|
| 343 |
+
|
| 344 |
+
// Finish encryption on 4 streams and
|
| 345 |
+
// XOR with input block
|
| 346 |
+
#define VCIPHERLAST4_XOR_INPUT \
|
| 347 |
+
VCIPHERLAST V15, V23, V15; \
|
| 348 |
+
VCIPHERLAST V16, V23, V16; \
|
| 349 |
+
VCIPHERLAST V17, V23, V17; \
|
| 350 |
+
VCIPHERLAST V18, V23, V18; \
|
| 351 |
+
XXLXOR V1, V15, V1; \
|
| 352 |
+
XXLXOR V2, V16, V2; \
|
| 353 |
+
XXLXOR V3, V17, V3; \
|
| 354 |
+
XXLXOR V4, V18, V4
|
| 355 |
+
|
| 356 |
+
// Store output block from V1-V8
|
| 357 |
+
// in big endian order and
|
| 358 |
+
// Update blk_out by 128
|
| 359 |
+
#define STORE_OUTPUT_BLOCK128(blk_out) \
|
| 360 |
+
P8_STXVB16X(V1,blk_out,R0); \
|
| 361 |
+
P8_STXVB16X(V2,blk_out,R16); \
|
| 362 |
+
P8_STXVB16X(V3,blk_out,R17); \
|
| 363 |
+
P8_STXVB16X(V4,blk_out,R18); \
|
| 364 |
+
P8_STXVB16X(V5,blk_out,R19); \
|
| 365 |
+
P8_STXVB16X(V6,blk_out,R20); \
|
| 366 |
+
P8_STXVB16X(V7,blk_out,R21); \
|
| 367 |
+
P8_STXVB16X(V8,blk_out,R22); \
|
| 368 |
+
ADD $128, blk_out
|
| 369 |
+
|
| 370 |
+
// Store output block from V1-V4
|
| 371 |
+
// in big endian order and
|
| 372 |
+
// Update blk_out by 64
|
| 373 |
+
#define STORE_OUTPUT_BLOCK64(blk_out) \
|
| 374 |
+
P8_STXVB16X(V1,blk_out,R0); \
|
| 375 |
+
P8_STXVB16X(V2,blk_out,R16); \
|
| 376 |
+
P8_STXVB16X(V3,blk_out,R17); \
|
| 377 |
+
P8_STXVB16X(V4,blk_out,R18); \
|
| 378 |
+
ADD $64, blk_out
|
| 379 |
+
|
| 380 |
+
// func gcmInit(productTable *[256]byte, h []byte)
|
| 381 |
+
TEXT ·gcmInit(SB), NOSPLIT, $0-32
|
| 382 |
+
MOVD productTable+0(FP), XIP
|
| 383 |
+
MOVD h+8(FP), HTBL
|
| 384 |
+
|
| 385 |
+
MOVD $0x10, R8
|
| 386 |
+
MOVD $0x20, R9
|
| 387 |
+
MOVD $0x30, R10
|
| 388 |
+
LXVD2X (HTBL)(R0), VH // Load H
|
| 389 |
+
|
| 390 |
+
VSPLTISB $-16, XC2 // 0xf0
|
| 391 |
+
VSPLTISB $1, T0 // one
|
| 392 |
+
VADDUBM XC2, XC2, XC2 // 0xe0
|
| 393 |
+
VXOR ZERO, ZERO, ZERO
|
| 394 |
+
VOR XC2, T0, XC2 // 0xe1
|
| 395 |
+
VSLDOI $15, XC2, ZERO, XC2 // 0xe1...
|
| 396 |
+
VSLDOI $1, ZERO, T0, T1 // ...1
|
| 397 |
+
VADDUBM XC2, XC2, XC2 // 0xc2...
|
| 398 |
+
VSPLTISB $7, T2
|
| 399 |
+
VOR XC2, T1, XC2 // 0xc2....01
|
| 400 |
+
VSPLTB $0, H, T1 // most significant byte
|
| 401 |
+
VSL H, T0, H // H<<=1
|
| 402 |
+
VSRAB T1, T2, T1 // broadcast carry bit
|
| 403 |
+
VAND T1, XC2, T1
|
| 404 |
+
VXOR H, T1, IN // twisted H
|
| 405 |
+
|
| 406 |
+
VSLDOI $8, IN, IN, H // twist even more ...
|
| 407 |
+
VSLDOI $8, ZERO, XC2, XC2 // 0xc2.0
|
| 408 |
+
VSLDOI $8, ZERO, H, HL // ... and split
|
| 409 |
+
VSLDOI $8, H, ZERO, HH
|
| 410 |
+
|
| 411 |
+
STXVD2X VXC2, (XIP+R0) // save pre-computed table
|
| 412 |
+
STXVD2X VHL, (XIP+R8)
|
| 413 |
+
MOVD $0x40, R8
|
| 414 |
+
STXVD2X VH, (XIP+R9)
|
| 415 |
+
MOVD $0x50, R9
|
| 416 |
+
STXVD2X VHH, (XIP+R10)
|
| 417 |
+
MOVD $0x60, R10
|
| 418 |
+
|
| 419 |
+
VPMSUMD IN, HL, XL // H.lo·H.lo
|
| 420 |
+
VPMSUMD IN, H, XM // H.hi·H.lo+H.lo·H.hi
|
| 421 |
+
VPMSUMD IN, HH, XH // H.hi·H.hi
|
| 422 |
+
|
| 423 |
+
VPMSUMD XL, XC2, T2 // 1st reduction phase
|
| 424 |
+
|
| 425 |
+
VSLDOI $8, XM, ZERO, T0
|
| 426 |
+
VSLDOI $8, ZERO, XM, T1
|
| 427 |
+
VXOR XL, T0, XL
|
| 428 |
+
VXOR XH, T1, XH
|
| 429 |
+
|
| 430 |
+
VSLDOI $8, XL, XL, XL
|
| 431 |
+
VXOR XL, T2, XL
|
| 432 |
+
|
| 433 |
+
VSLDOI $8, XL, XL, T1 // 2nd reduction phase
|
| 434 |
+
VPMSUMD XL, XC2, XL
|
| 435 |
+
VXOR T1, XH, T1
|
| 436 |
+
VXOR XL, T1, IN1
|
| 437 |
+
|
| 438 |
+
VSLDOI $8, IN1, IN1, H2
|
| 439 |
+
VSLDOI $8, ZERO, H2, H2L
|
| 440 |
+
VSLDOI $8, H2, ZERO, H2H
|
| 441 |
+
|
| 442 |
+
STXVD2X VH2L, (XIP+R8) // save H^2
|
| 443 |
+
MOVD $0x70, R8
|
| 444 |
+
STXVD2X VH2, (XIP+R9)
|
| 445 |
+
MOVD $0x80, R9
|
| 446 |
+
STXVD2X VH2H, (XIP+R10)
|
| 447 |
+
MOVD $0x90, R10
|
| 448 |
+
|
| 449 |
+
VPMSUMD IN, H2L, XL // H.lo·H^2.lo
|
| 450 |
+
VPMSUMD IN1, H2L, XL1 // H^2.lo·H^2.lo
|
| 451 |
+
VPMSUMD IN, H2, XM // H.hi·H^2.lo+H.lo·H^2.hi
|
| 452 |
+
VPMSUMD IN1, H2, XM1 // H^2.hi·H^2.lo+H^2.lo·H^2.hi
|
| 453 |
+
VPMSUMD IN, H2H, XH // H.hi·H^2.hi
|
| 454 |
+
VPMSUMD IN1, H2H, XH1 // H^2.hi·H^2.hi
|
| 455 |
+
|
| 456 |
+
VPMSUMD XL, XC2, T2 // 1st reduction phase
|
| 457 |
+
VPMSUMD XL1, XC2, HH // 1st reduction phase
|
| 458 |
+
|
| 459 |
+
VSLDOI $8, XM, ZERO, T0
|
| 460 |
+
VSLDOI $8, ZERO, XM, T1
|
| 461 |
+
VSLDOI $8, XM1, ZERO, HL
|
| 462 |
+
VSLDOI $8, ZERO, XM1, H
|
| 463 |
+
VXOR XL, T0, XL
|
| 464 |
+
VXOR XH, T1, XH
|
| 465 |
+
VXOR XL1, HL, XL1
|
| 466 |
+
VXOR XH1, H, XH1
|
| 467 |
+
|
| 468 |
+
VSLDOI $8, XL, XL, XL
|
| 469 |
+
VSLDOI $8, XL1, XL1, XL1
|
| 470 |
+
VXOR XL, T2, XL
|
| 471 |
+
VXOR XL1, HH, XL1
|
| 472 |
+
|
| 473 |
+
VSLDOI $8, XL, XL, T1 // 2nd reduction phase
|
| 474 |
+
VSLDOI $8, XL1, XL1, H // 2nd reduction phase
|
| 475 |
+
VPMSUMD XL, XC2, XL
|
| 476 |
+
VPMSUMD XL1, XC2, XL1
|
| 477 |
+
VXOR T1, XH, T1
|
| 478 |
+
VXOR H, XH1, H
|
| 479 |
+
VXOR XL, T1, XL
|
| 480 |
+
VXOR XL1, H, XL1
|
| 481 |
+
|
| 482 |
+
VSLDOI $8, XL, XL, H
|
| 483 |
+
VSLDOI $8, XL1, XL1, H2
|
| 484 |
+
VSLDOI $8, ZERO, H, HL
|
| 485 |
+
VSLDOI $8, H, ZERO, HH
|
| 486 |
+
VSLDOI $8, ZERO, H2, H2L
|
| 487 |
+
VSLDOI $8, H2, ZERO, H2H
|
| 488 |
+
|
| 489 |
+
STXVD2X VHL, (XIP+R8) // save H^3
|
| 490 |
+
MOVD $0xa0, R8
|
| 491 |
+
STXVD2X VH, (XIP+R9)
|
| 492 |
+
MOVD $0xb0, R9
|
| 493 |
+
STXVD2X VHH, (XIP+R10)
|
| 494 |
+
MOVD $0xc0, R10
|
| 495 |
+
STXVD2X VH2L, (XIP+R8) // save H^4
|
| 496 |
+
STXVD2X VH2, (XIP+R9)
|
| 497 |
+
STXVD2X VH2H, (XIP+R10)
|
| 498 |
+
|
| 499 |
+
RET
|
| 500 |
+
|
| 501 |
+
// func gcmHash(output []byte, productTable *[256]byte, inp []byte, len int)
|
| 502 |
+
TEXT ·gcmHash(SB), NOSPLIT, $0-64
|
| 503 |
+
MOVD output+0(FP), XIP
|
| 504 |
+
MOVD productTable+24(FP), HTBL
|
| 505 |
+
MOVD inp+32(FP), INP
|
| 506 |
+
MOVD len+56(FP), LEN
|
| 507 |
+
|
| 508 |
+
MOVD $0x10, R8
|
| 509 |
+
MOVD $0x20, R9
|
| 510 |
+
MOVD $0x30, R10
|
| 511 |
+
LXVD2X (XIP)(R0), VXL // load Xi
|
| 512 |
+
|
| 513 |
+
LXVD2X (HTBL)(R8), VHL // load pre-computed table
|
| 514 |
+
MOVD $0x40, R8
|
| 515 |
+
LXVD2X (HTBL)(R9), VH
|
| 516 |
+
MOVD $0x50, R9
|
| 517 |
+
LXVD2X (HTBL)(R10), VHH
|
| 518 |
+
MOVD $0x60, R10
|
| 519 |
+
LXVD2X (HTBL)(R0), VXC2
|
| 520 |
+
#ifdef GOARCH_ppc64le
|
| 521 |
+
LVSL (R0)(R0), LEMASK
|
| 522 |
+
VSPLTISB $0x07, T0
|
| 523 |
+
VXOR LEMASK, T0, LEMASK
|
| 524 |
+
VPERM XL, XL, LEMASK, XL
|
| 525 |
+
#endif
|
| 526 |
+
VXOR ZERO, ZERO, ZERO
|
| 527 |
+
|
| 528 |
+
CMPU LEN, $64
|
| 529 |
+
BGE gcm_ghash_p8_4x
|
| 530 |
+
|
| 531 |
+
LXVD2X (INP)(R0), VIN
|
| 532 |
+
ADD $16, INP, INP
|
| 533 |
+
SUBCCC $16, LEN, LEN
|
| 534 |
+
#ifdef GOARCH_ppc64le
|
| 535 |
+
VPERM IN, IN, LEMASK, IN
|
| 536 |
+
#endif
|
| 537 |
+
VXOR IN, XL, IN
|
| 538 |
+
BEQ short
|
| 539 |
+
|
| 540 |
+
LXVD2X (HTBL)(R8), VH2L // load H^2
|
| 541 |
+
MOVD $16, R8
|
| 542 |
+
LXVD2X (HTBL)(R9), VH2
|
| 543 |
+
ADD LEN, INP, R9 // end of input
|
| 544 |
+
LXVD2X (HTBL)(R10), VH2H
|
| 545 |
+
|
| 546 |
+
loop_2x:
|
| 547 |
+
LXVD2X (INP)(R0), VIN1
|
| 548 |
+
#ifdef GOARCH_ppc64le
|
| 549 |
+
VPERM IN1, IN1, LEMASK, IN1
|
| 550 |
+
#endif
|
| 551 |
+
|
| 552 |
+
SUBC $32, LEN, LEN
|
| 553 |
+
VPMSUMD IN, H2L, XL // H^2.lo·Xi.lo
|
| 554 |
+
VPMSUMD IN1, HL, XL1 // H.lo·Xi+1.lo
|
| 555 |
+
SUBE R11, R11, R11 // borrow?-1:0
|
| 556 |
+
VPMSUMD IN, H2, XM // H^2.hi·Xi.lo+H^2.lo·Xi.hi
|
| 557 |
+
VPMSUMD IN1, H, XM1 // H.hi·Xi+1.lo+H.lo·Xi+1.hi
|
| 558 |
+
AND LEN, R11, R11
|
| 559 |
+
VPMSUMD IN, H2H, XH // H^2.hi·Xi.hi
|
| 560 |
+
VPMSUMD IN1, HH, XH1 // H.hi·Xi+1.hi
|
| 561 |
+
ADD R11, INP, INP
|
| 562 |
+
|
| 563 |
+
VXOR XL, XL1, XL
|
| 564 |
+
VXOR XM, XM1, XM
|
| 565 |
+
|
| 566 |
+
VPMSUMD XL, XC2, T2 // 1st reduction phase
|
| 567 |
+
|
| 568 |
+
VSLDOI $8, XM, ZERO, T0
|
| 569 |
+
VSLDOI $8, ZERO, XM, T1
|
| 570 |
+
VXOR XH, XH1, XH
|
| 571 |
+
VXOR XL, T0, XL
|
| 572 |
+
VXOR XH, T1, XH
|
| 573 |
+
|
| 574 |
+
VSLDOI $8, XL, XL, XL
|
| 575 |
+
VXOR XL, T2, XL
|
| 576 |
+
LXVD2X (INP)(R8), VIN
|
| 577 |
+
ADD $32, INP, INP
|
| 578 |
+
|
| 579 |
+
VSLDOI $8, XL, XL, T1 // 2nd reduction phase
|
| 580 |
+
VPMSUMD XL, XC2, XL
|
| 581 |
+
#ifdef GOARCH_ppc64le
|
| 582 |
+
VPERM IN, IN, LEMASK, IN
|
| 583 |
+
#endif
|
| 584 |
+
VXOR T1, XH, T1
|
| 585 |
+
VXOR IN, T1, IN
|
| 586 |
+
VXOR IN, XL, IN
|
| 587 |
+
CMP R9, INP
|
| 588 |
+
BGT loop_2x // done yet?
|
| 589 |
+
|
| 590 |
+
CMPWU LEN, $0
|
| 591 |
+
BNE even
|
| 592 |
+
|
| 593 |
+
short:
|
| 594 |
+
VPMSUMD IN, HL, XL // H.lo·Xi.lo
|
| 595 |
+
VPMSUMD IN, H, XM // H.hi·Xi.lo+H.lo·Xi.hi
|
| 596 |
+
VPMSUMD IN, HH, XH // H.hi·Xi.hi
|
| 597 |
+
|
| 598 |
+
VPMSUMD XL, XC2, T2 // 1st reduction phase
|
| 599 |
+
|
| 600 |
+
VSLDOI $8, XM, ZERO, T0
|
| 601 |
+
VSLDOI $8, ZERO, XM, T1
|
| 602 |
+
VXOR XL, T0, XL
|
| 603 |
+
VXOR XH, T1, XH
|
| 604 |
+
|
| 605 |
+
VSLDOI $8, XL, XL, XL
|
| 606 |
+
VXOR XL, T2, XL
|
| 607 |
+
|
| 608 |
+
VSLDOI $8, XL, XL, T1 // 2nd reduction phase
|
| 609 |
+
VPMSUMD XL, XC2, XL
|
| 610 |
+
VXOR T1, XH, T1
|
| 611 |
+
|
| 612 |
+
even:
|
| 613 |
+
VXOR XL, T1, XL
|
| 614 |
+
#ifdef GOARCH_ppc64le
|
| 615 |
+
VPERM XL, XL, LEMASK, XL
|
| 616 |
+
#endif
|
| 617 |
+
STXVD2X VXL, (XIP+R0)
|
| 618 |
+
|
| 619 |
+
OR R12, R12, R12 // write out Xi
|
| 620 |
+
RET
|
| 621 |
+
|
| 622 |
+
gcm_ghash_p8_4x:
|
| 623 |
+
LVSL (R8)(R0), T0 // 0x0001..0e0f
|
| 624 |
+
MOVD $0x70, R8
|
| 625 |
+
LXVD2X (HTBL)(R9), VH2
|
| 626 |
+
MOVD $0x80, R9
|
| 627 |
+
VSPLTISB $8, T1 // 0x0808..0808
|
| 628 |
+
MOVD $0x90, R10
|
| 629 |
+
LXVD2X (HTBL)(R8), VH3L // load H^3
|
| 630 |
+
MOVD $0xa0, R8
|
| 631 |
+
LXVD2X (HTBL)(R9), VH3
|
| 632 |
+
MOVD $0xb0, R9
|
| 633 |
+
LXVD2X (HTBL)(R10), VH3H
|
| 634 |
+
MOVD $0xc0, R10
|
| 635 |
+
LXVD2X (HTBL)(R8), VH4L // load H^4
|
| 636 |
+
MOVD $0x10, R8
|
| 637 |
+
LXVD2X (HTBL)(R9), VH4
|
| 638 |
+
MOVD $0x20, R9
|
| 639 |
+
LXVD2X (HTBL)(R10), VH4H
|
| 640 |
+
MOVD $0x30, R10
|
| 641 |
+
|
| 642 |
+
VSLDOI $8, ZERO, T1, T2 // 0x0000..0808
|
| 643 |
+
VADDUBM T0, T2, HIPERM // 0x0001..1617
|
| 644 |
+
VADDUBM T1, HIPERM, LOPERM // 0x0809..1e1f
|
| 645 |
+
|
| 646 |
+
SRD $4, LEN, LEN // this allows to use sign bit as carry
|
| 647 |
+
|
| 648 |
+
LXVD2X (INP)(R0), VIN0 // load input
|
| 649 |
+
LXVD2X (INP)(R8), VIN1
|
| 650 |
+
SUBCCC $8, LEN, LEN
|
| 651 |
+
LXVD2X (INP)(R9), VIN2
|
| 652 |
+
LXVD2X (INP)(R10), VIN3
|
| 653 |
+
ADD $0x40, INP, INP
|
| 654 |
+
#ifdef GOARCH_ppc64le
|
| 655 |
+
VPERM IN0, IN0, LEMASK, IN0
|
| 656 |
+
VPERM IN1, IN1, LEMASK, IN1
|
| 657 |
+
VPERM IN2, IN2, LEMASK, IN2
|
| 658 |
+
VPERM IN3, IN3, LEMASK, IN3
|
| 659 |
+
#endif
|
| 660 |
+
|
| 661 |
+
VXOR IN0, XL, XH
|
| 662 |
+
|
| 663 |
+
VPMSUMD IN1, H3L, XL1
|
| 664 |
+
VPMSUMD IN1, H3, XM1
|
| 665 |
+
VPMSUMD IN1, H3H, XH1
|
| 666 |
+
|
| 667 |
+
VPERM H2, H, HIPERM, H21L
|
| 668 |
+
VPERM IN2, IN3, LOPERM, T0
|
| 669 |
+
VPERM H2, H, LOPERM, H21H
|
| 670 |
+
VPERM IN2, IN3, HIPERM, T1
|
| 671 |
+
VPMSUMD IN2, H2, XM2 // H^2.lo·Xi+2.hi+H^2.hi·Xi+2.lo
|
| 672 |
+
VPMSUMD T0, H21L, XL3 // H^2.lo·Xi+2.lo+H.lo·Xi+3.lo
|
| 673 |
+
VPMSUMD IN3, H, XM3 // H.hi·Xi+3.lo +H.lo·Xi+3.hi
|
| 674 |
+
VPMSUMD T1, H21H, XH3 // H^2.hi·Xi+2.hi+H.hi·Xi+3.hi
|
| 675 |
+
|
| 676 |
+
VXOR XM2, XM1, XM2
|
| 677 |
+
VXOR XL3, XL1, XL3
|
| 678 |
+
VXOR XM3, XM2, XM3
|
| 679 |
+
VXOR XH3, XH1, XH3
|
| 680 |
+
|
| 681 |
+
BLT tail_4x
|
| 682 |
+
|
| 683 |
+
loop_4x:
|
| 684 |
+
LXVD2X (INP)(R0), VIN0
|
| 685 |
+
LXVD2X (INP)(R8), VIN1
|
| 686 |
+
SUBCCC $4, LEN, LEN
|
| 687 |
+
LXVD2X (INP)(R9), VIN2
|
| 688 |
+
LXVD2X (INP)(R10), VIN3
|
| 689 |
+
ADD $0x40, INP, INP
|
| 690 |
+
#ifdef GOARCH_ppc64le
|
| 691 |
+
VPERM IN1, IN1, LEMASK, IN1
|
| 692 |
+
VPERM IN2, IN2, LEMASK, IN2
|
| 693 |
+
VPERM IN3, IN3, LEMASK, IN3
|
| 694 |
+
VPERM IN0, IN0, LEMASK, IN0
|
| 695 |
+
#endif
|
| 696 |
+
|
| 697 |
+
VPMSUMD XH, H4L, XL // H^4.lo·Xi.lo
|
| 698 |
+
VPMSUMD XH, H4, XM // H^4.hi·Xi.lo+H^4.lo·Xi.hi
|
| 699 |
+
VPMSUMD XH, H4H, XH // H^4.hi·Xi.hi
|
| 700 |
+
VPMSUMD IN1, H3L, XL1
|
| 701 |
+
VPMSUMD IN1, H3, XM1
|
| 702 |
+
VPMSUMD IN1, H3H, XH1
|
| 703 |
+
|
| 704 |
+
VXOR XL, XL3, XL
|
| 705 |
+
VXOR XM, XM3, XM
|
| 706 |
+
VXOR XH, XH3, XH
|
| 707 |
+
VPERM IN2, IN3, LOPERM, T0
|
| 708 |
+
VPERM IN2, IN3, HIPERM, T1
|
| 709 |
+
|
| 710 |
+
VPMSUMD XL, XC2, T2 // 1st reduction phase
|
| 711 |
+
VPMSUMD T0, H21L, XL3 // H.lo·Xi+3.lo +H^2.lo·Xi+2.lo
|
| 712 |
+
VPMSUMD T1, H21H, XH3 // H.hi·Xi+3.hi +H^2.hi·Xi+2.hi
|
| 713 |
+
|
| 714 |
+
VSLDOI $8, XM, ZERO, T0
|
| 715 |
+
VSLDOI $8, ZERO, XM, T1
|
| 716 |
+
VXOR XL, T0, XL
|
| 717 |
+
VXOR XH, T1, XH
|
| 718 |
+
|
| 719 |
+
VSLDOI $8, XL, XL, XL
|
| 720 |
+
VXOR XL, T2, XL
|
| 721 |
+
|
| 722 |
+
VSLDOI $8, XL, XL, T1 // 2nd reduction phase
|
| 723 |
+
VPMSUMD IN2, H2, XM2 // H^2.hi·Xi+2.lo+H^2.lo·Xi+2.hi
|
| 724 |
+
VPMSUMD IN3, H, XM3 // H.hi·Xi+3.lo +H.lo·Xi+3.hi
|
| 725 |
+
VPMSUMD XL, XC2, XL
|
| 726 |
+
|
| 727 |
+
VXOR XL3, XL1, XL3
|
| 728 |
+
VXOR XH3, XH1, XH3
|
| 729 |
+
VXOR XH, IN0, XH
|
| 730 |
+
VXOR XM2, XM1, XM2
|
| 731 |
+
VXOR XH, T1, XH
|
| 732 |
+
VXOR XM3, XM2, XM3
|
| 733 |
+
VXOR XH, XL, XH
|
| 734 |
+
BGE loop_4x
|
| 735 |
+
|
| 736 |
+
tail_4x:
|
| 737 |
+
VPMSUMD XH, H4L, XL // H^4.lo·Xi.lo
|
| 738 |
+
VPMSUMD XH, H4, XM // H^4.hi·Xi.lo+H^4.lo·Xi.hi
|
| 739 |
+
VPMSUMD XH, H4H, XH // H^4.hi·Xi.hi
|
| 740 |
+
|
| 741 |
+
VXOR XL, XL3, XL
|
| 742 |
+
VXOR XM, XM3, XM
|
| 743 |
+
|
| 744 |
+
VPMSUMD XL, XC2, T2 // 1st reduction phase
|
| 745 |
+
|
| 746 |
+
VSLDOI $8, XM, ZERO, T0
|
| 747 |
+
VSLDOI $8, ZERO, XM, T1
|
| 748 |
+
VXOR XH, XH3, XH
|
| 749 |
+
VXOR XL, T0, XL
|
| 750 |
+
VXOR XH, T1, XH
|
| 751 |
+
|
| 752 |
+
VSLDOI $8, XL, XL, XL
|
| 753 |
+
VXOR XL, T2, XL
|
| 754 |
+
|
| 755 |
+
VSLDOI $8, XL, XL, T1 // 2nd reduction phase
|
| 756 |
+
VPMSUMD XL, XC2, XL
|
| 757 |
+
VXOR T1, XH, T1
|
| 758 |
+
VXOR XL, T1, XL
|
| 759 |
+
|
| 760 |
+
ADDCCC $4, LEN, LEN
|
| 761 |
+
BEQ done_4x
|
| 762 |
+
|
| 763 |
+
LXVD2X (INP)(R0), VIN0
|
| 764 |
+
CMPU LEN, $2
|
| 765 |
+
MOVD $-4, LEN
|
| 766 |
+
BLT one
|
| 767 |
+
LXVD2X (INP)(R8), VIN1
|
| 768 |
+
BEQ two
|
| 769 |
+
|
| 770 |
+
three:
|
| 771 |
+
LXVD2X (INP)(R9), VIN2
|
| 772 |
+
#ifdef GOARCH_ppc64le
|
| 773 |
+
VPERM IN0, IN0, LEMASK, IN0
|
| 774 |
+
VPERM IN1, IN1, LEMASK, IN1
|
| 775 |
+
VPERM IN2, IN2, LEMASK, IN2
|
| 776 |
+
#endif
|
| 777 |
+
|
| 778 |
+
VXOR IN0, XL, XH
|
| 779 |
+
VOR H3L, H3L, H4L
|
| 780 |
+
VOR H3, H3, H4
|
| 781 |
+
VOR H3H, H3H, H4H
|
| 782 |
+
|
| 783 |
+
VPERM IN1, IN2, LOPERM, T0
|
| 784 |
+
VPERM IN1, IN2, HIPERM, T1
|
| 785 |
+
VPMSUMD IN1, H2, XM2 // H^2.lo·Xi+1.hi+H^2.hi·Xi+1.lo
|
| 786 |
+
VPMSUMD IN2, H, XM3 // H.hi·Xi+2.lo +H.lo·Xi+2.hi
|
| 787 |
+
VPMSUMD T0, H21L, XL3 // H^2.lo·Xi+1.lo+H.lo·Xi+2.lo
|
| 788 |
+
VPMSUMD T1, H21H, XH3 // H^2.hi·Xi+1.hi+H.hi·Xi+2.hi
|
| 789 |
+
|
| 790 |
+
VXOR XM3, XM2, XM3
|
| 791 |
+
JMP tail_4x
|
| 792 |
+
|
| 793 |
+
two:
|
| 794 |
+
#ifdef GOARCH_ppc64le
|
| 795 |
+
VPERM IN0, IN0, LEMASK, IN0
|
| 796 |
+
VPERM IN1, IN1, LEMASK, IN1
|
| 797 |
+
#endif
|
| 798 |
+
|
| 799 |
+
VXOR IN, XL, XH
|
| 800 |
+
VPERM ZERO, IN1, LOPERM, T0
|
| 801 |
+
VPERM ZERO, IN1, HIPERM, T1
|
| 802 |
+
|
| 803 |
+
VSLDOI $8, ZERO, H2, H4L
|
| 804 |
+
VOR H2, H2, H4
|
| 805 |
+
VSLDOI $8, H2, ZERO, H4H
|
| 806 |
+
|
| 807 |
+
VPMSUMD T0, H21L, XL3 // H.lo·Xi+1.lo
|
| 808 |
+
VPMSUMD IN1, H, XM3 // H.hi·Xi+1.lo+H.lo·Xi+2.hi
|
| 809 |
+
VPMSUMD T1, H21H, XH3 // H.hi·Xi+1.hi
|
| 810 |
+
|
| 811 |
+
JMP tail_4x
|
| 812 |
+
|
| 813 |
+
one:
|
| 814 |
+
#ifdef GOARCH_ppc64le
|
| 815 |
+
VPERM IN0, IN0, LEMASK, IN0
|
| 816 |
+
#endif
|
| 817 |
+
|
| 818 |
+
VSLDOI $8, ZERO, H, H4L
|
| 819 |
+
VOR H, H, H4
|
| 820 |
+
VSLDOI $8, H, ZERO, H4H
|
| 821 |
+
|
| 822 |
+
VXOR IN0, XL, XH
|
| 823 |
+
VXOR XL3, XL3, XL3
|
| 824 |
+
VXOR XM3, XM3, XM3
|
| 825 |
+
VXOR XH3, XH3, XH3
|
| 826 |
+
|
| 827 |
+
JMP tail_4x
|
| 828 |
+
|
| 829 |
+
done_4x:
|
| 830 |
+
#ifdef GOARCH_ppc64le
|
| 831 |
+
VPERM XL, XL, LEMASK, XL
|
| 832 |
+
#endif
|
| 833 |
+
STXVD2X VXL, (XIP+R0) // write out Xi
|
| 834 |
+
RET
|
| 835 |
+
|
| 836 |
+
// func gcmMul(output []byte, productTable *[256]byte)
|
| 837 |
+
TEXT ·gcmMul(SB), NOSPLIT, $0-32
|
| 838 |
+
MOVD output+0(FP), XIP
|
| 839 |
+
MOVD productTable+24(FP), HTBL
|
| 840 |
+
|
| 841 |
+
MOVD $0x10, R8
|
| 842 |
+
MOVD $0x20, R9
|
| 843 |
+
MOVD $0x30, R10
|
| 844 |
+
LXVD2X (XIP)(R0), VIN // load Xi
|
| 845 |
+
|
| 846 |
+
LXVD2X (HTBL)(R8), VHL // Load pre-computed table
|
| 847 |
+
LXVD2X (HTBL)(R9), VH
|
| 848 |
+
LXVD2X (HTBL)(R10), VHH
|
| 849 |
+
LXVD2X (HTBL)(R0), VXC2
|
| 850 |
+
#ifdef GOARCH_ppc64le
|
| 851 |
+
VSPLTISB $0x07, T0
|
| 852 |
+
VXOR LEMASK, T0, LEMASK
|
| 853 |
+
VPERM IN, IN, LEMASK, IN
|
| 854 |
+
#endif
|
| 855 |
+
VXOR ZERO, ZERO, ZERO
|
| 856 |
+
|
| 857 |
+
VPMSUMD IN, HL, XL // H.lo·Xi.lo
|
| 858 |
+
VPMSUMD IN, H, XM // H.hi·Xi.lo+H.lo·Xi.hi
|
| 859 |
+
VPMSUMD IN, HH, XH // H.hi·Xi.hi
|
| 860 |
+
|
| 861 |
+
VPMSUMD XL, XC2, T2 // 1st reduction phase
|
| 862 |
+
|
| 863 |
+
VSLDOI $8, XM, ZERO, T0
|
| 864 |
+
VSLDOI $8, ZERO, XM, T1
|
| 865 |
+
VXOR XL, T0, XL
|
| 866 |
+
VXOR XH, T1, XH
|
| 867 |
+
|
| 868 |
+
VSLDOI $8, XL, XL, XL
|
| 869 |
+
VXOR XL, T2, XL
|
| 870 |
+
|
| 871 |
+
VSLDOI $8, XL, XL, T1 // 2nd reduction phase
|
| 872 |
+
VPMSUMD XL, XC2, XL
|
| 873 |
+
VXOR T1, XH, T1
|
| 874 |
+
VXOR XL, T1, XL
|
| 875 |
+
|
| 876 |
+
#ifdef GOARCH_ppc64le
|
| 877 |
+
VPERM XL, XL, LEMASK, XL
|
| 878 |
+
#endif
|
| 879 |
+
STXVD2X VXL, (XIP+R0) // write out Xi
|
| 880 |
+
RET
|
| 881 |
+
|
| 882 |
+
#define BLK_INP R3
|
| 883 |
+
#define BLK_OUT R4
|
| 884 |
+
#define BLK_KEY R5
|
| 885 |
+
#define KEY_LEN R6
|
| 886 |
+
#define BLK_IDX R7
|
| 887 |
+
#define IDX R8
|
| 888 |
+
#define IN_LEN R9
|
| 889 |
+
#define COUNTER R10
|
| 890 |
+
#define CONPTR R14
|
| 891 |
+
#define MASK V5
|
| 892 |
+
|
| 893 |
+
// Implementation of the counterCrypt function in assembler.
|
| 894 |
+
// Original loop is unrolled to allow for multiple encryption
|
| 895 |
+
// streams to be done in parallel, which is achieved by interleaving
|
| 896 |
+
// vcipher instructions from each stream. This is also referred to as
|
| 897 |
+
// stitching, and provides significant performance improvements.
|
| 898 |
+
// Some macros are defined which enable execution for big or little
|
| 899 |
+
// endian as well as different ISA targets.
|
| 900 |
+
//func (g *gcmAsm) counterCrypt(out, in []byte, counter *[gcmBlockSize]byte, key[gcmBlockSize]uint32)
|
| 901 |
+
//func counterCryptASM(xr, out, in, counter, key)
|
| 902 |
+
TEXT ·counterCryptASM(SB), NOSPLIT, $16-72
|
| 903 |
+
MOVD xr(FP), KEY_LEN
|
| 904 |
+
MOVD out+8(FP), BLK_OUT
|
| 905 |
+
MOVD out_len+16(FP), R8
|
| 906 |
+
MOVD in+32(FP), BLK_INP
|
| 907 |
+
MOVD in_len+40(FP), IN_LEN
|
| 908 |
+
MOVD counter+56(FP), COUNTER
|
| 909 |
+
MOVD key+64(FP), BLK_KEY
|
| 910 |
+
|
| 911 |
+
// Set up permute string when needed.
|
| 912 |
+
#ifdef NEEDS_ESPERM
|
| 913 |
+
MOVD $·rcon(SB), R14
|
| 914 |
+
LVX (R14), ESPERM // Permute value for P8_ macros.
|
| 915 |
+
#endif
|
| 916 |
+
SETUP_COUNTER // V30 Counter V31 BE {0, 0, 0, 1}
|
| 917 |
+
LOAD_KEYS(BLK_KEY, KEY_LEN) // VS1 - VS10/12/14 based on keysize
|
| 918 |
+
CMP IN_LEN, $128
|
| 919 |
+
BLT block64
|
| 920 |
+
block128_loop:
|
| 921 |
+
// Do 8 encryptions in parallel by setting
|
| 922 |
+
// input values in V15-V22 and executing
|
| 923 |
+
// vcipher on the updated value and the keys.
|
| 924 |
+
GEN_VCIPHER_8_INPUTS
|
| 925 |
+
VCIPHER_8X1_KEY(VS1)
|
| 926 |
+
VCIPHER_8X1_KEY(VS2)
|
| 927 |
+
VCIPHER_8X1_KEY(VS3)
|
| 928 |
+
VCIPHER_8X1_KEY(VS4)
|
| 929 |
+
VCIPHER_8X1_KEY(VS5)
|
| 930 |
+
VCIPHER_8X1_KEY(VS6)
|
| 931 |
+
VCIPHER_8X1_KEY(VS7)
|
| 932 |
+
VCIPHER_8X1_KEY(VS8)
|
| 933 |
+
VCIPHER_8X1_KEY(VS9)
|
| 934 |
+
// Additional encryptions are done based on
|
| 935 |
+
// the key length, with the last key moved
|
| 936 |
+
// to V23 for use with VCIPHERLAST.
|
| 937 |
+
// CR2 = CMP key_len, $12
|
| 938 |
+
XXLOR VS10, VS10, V23
|
| 939 |
+
BLT CR2, block128_last // key_len = 10
|
| 940 |
+
VCIPHER_8X1_KEY(VS10)
|
| 941 |
+
VCIPHER_8X1_KEY(VS11)
|
| 942 |
+
XXLOR VS12,VS12,V23
|
| 943 |
+
BEQ CR2, block128_last // ken_len = 12
|
| 944 |
+
VCIPHER_8X1_KEY(VS12)
|
| 945 |
+
VCIPHER_8X1_KEY(VS13)
|
| 946 |
+
XXLOR VS14,VS14,V23 // key_len = 14
|
| 947 |
+
block128_last:
|
| 948 |
+
// vcipher encryptions are in V15-V22 at this
|
| 949 |
+
// point with vcipherlast remaining to be done.
|
| 950 |
+
// Load input block into V1-V8, setting index offsets
|
| 951 |
+
// in R16-R22 to use with the STORE.
|
| 952 |
+
LOAD_INPUT_BLOCK128(BLK_INP)
|
| 953 |
+
// Do VCIPHERLAST on the last key for each encryption
|
| 954 |
+
// stream and XOR the result with the corresponding
|
| 955 |
+
// value from the input block.
|
| 956 |
+
VCIPHERLAST8_XOR_INPUT
|
| 957 |
+
// Store the results (8*16) and update BLK_OUT by 128.
|
| 958 |
+
STORE_OUTPUT_BLOCK128(BLK_OUT)
|
| 959 |
+
ADD $-128, IN_LEN // input size
|
| 960 |
+
CMP IN_LEN, $128 // check if >= blocksize
|
| 961 |
+
BGE block128_loop // next input block
|
| 962 |
+
CMP IN_LEN, $0
|
| 963 |
+
BEQ done
|
| 964 |
+
block64:
|
| 965 |
+
CMP IN_LEN, $64 // Check if >= 64
|
| 966 |
+
BLT block16_loop
|
| 967 |
+
// Do 4 encryptions in parallel by setting
|
| 968 |
+
// input values in V15-V18 and executing
|
| 969 |
+
// vcipher on the updated value and the keys.
|
| 970 |
+
GEN_VCIPHER_4_INPUTS
|
| 971 |
+
VCIPHER_4X1_KEY(VS1)
|
| 972 |
+
VCIPHER_4X1_KEY(VS2)
|
| 973 |
+
VCIPHER_4X1_KEY(VS3)
|
| 974 |
+
VCIPHER_4X1_KEY(VS4)
|
| 975 |
+
VCIPHER_4X1_KEY(VS5)
|
| 976 |
+
VCIPHER_4X1_KEY(VS6)
|
| 977 |
+
VCIPHER_4X1_KEY(VS7)
|
| 978 |
+
VCIPHER_4X1_KEY(VS8)
|
| 979 |
+
VCIPHER_4X1_KEY(VS9)
|
| 980 |
+
// Check key length based on CR2
|
| 981 |
+
// Move last key to V23 for use with later vcipherlast
|
| 982 |
+
XXLOR VS10, VS10, V23
|
| 983 |
+
BLT CR2, block64_last // size = 10
|
| 984 |
+
VCIPHER_4X1_KEY(VS10) // Encrypt next 2 keys
|
| 985 |
+
VCIPHER_4X1_KEY(VS11)
|
| 986 |
+
XXLOR VS12, VS12, V23
|
| 987 |
+
BEQ CR2, block64_last // size = 12
|
| 988 |
+
VCIPHER_4X1_KEY(VS12) // Encrypt last 2 keys
|
| 989 |
+
VCIPHER_4X1_KEY(VS13)
|
| 990 |
+
XXLOR VS14, VS14, V23 // size = 14
|
| 991 |
+
block64_last:
|
| 992 |
+
LOAD_INPUT_BLOCK64(BLK_INP) // Load 64 bytes of input
|
| 993 |
+
// Do VCIPHERLAST on the last for each encryption
|
| 994 |
+
// stream and XOR the result with the corresponding
|
| 995 |
+
// value from the input block.
|
| 996 |
+
VCIPHERLAST4_XOR_INPUT
|
| 997 |
+
// Store the results (4*16) and update BLK_OUT by 64.
|
| 998 |
+
STORE_OUTPUT_BLOCK64(BLK_OUT)
|
| 999 |
+
ADD $-64, IN_LEN // decrement input block length
|
| 1000 |
+
CMP IN_LEN, $0 // check for remaining length
|
| 1001 |
+
BEQ done
|
| 1002 |
+
block16_loop:
|
| 1003 |
+
CMP IN_LEN, $16 // More input
|
| 1004 |
+
BLT final_block // If not, then handle partial block
|
| 1005 |
+
// Single encryption, no stitching
|
| 1006 |
+
GEN_VCIPHER_INPUT // Generate input value for single encryption
|
| 1007 |
+
VCIPHER_1X9_KEYS(V15) // Encrypt V15 value with 9 keys
|
| 1008 |
+
XXLOR VS10, VS10, V23 // Last key -> V23 for later vcipiherlast
|
| 1009 |
+
// Key length based on CR2. (LT=10, EQ=12, GT=14)
|
| 1010 |
+
BLT CR2, block16_last // Finish for key size 10
|
| 1011 |
+
VCIPHER_1X2_KEYS(V15, VS10, VS11) // Encrypt V15 with 2 more keys
|
| 1012 |
+
XXLOR VS12, VS12, V23 // Last key -> V23 for later vcipherlast
|
| 1013 |
+
BEQ CR2, block16_last // Finish for key size 12
|
| 1014 |
+
VCIPHER_1X2_KEYS(V15, VS12, VS13) // Encrypt V15 with last 2 keys
|
| 1015 |
+
XXLOR VS14, VS14, V23 // Last key -> V23 for vcipherlast with key size 14
|
| 1016 |
+
block16_last:
|
| 1017 |
+
P8_LXVB16X(BLK_INP, R0, V1) // Load input
|
| 1018 |
+
VCIPHERLAST V15, V23, V15 // Encrypt last value in V23
|
| 1019 |
+
XXLXOR V15, V1, V1 // XOR with input
|
| 1020 |
+
P8_STXVB16X(V1,R0,BLK_OUT) // Store final encryption value to output
|
| 1021 |
+
ADD $16, BLK_INP // Increment input pointer
|
| 1022 |
+
ADD $16, BLK_OUT // Increment output pointer
|
| 1023 |
+
ADD $-16, IN_LEN // Decrement input length
|
| 1024 |
+
BR block16_loop // Check for next
|
| 1025 |
+
final_block:
|
| 1026 |
+
CMP IN_LEN, $0
|
| 1027 |
+
BEQ done
|
| 1028 |
+
GEN_VCIPHER_INPUT // Generate input value for partial encryption
|
| 1029 |
+
VCIPHER_1X9_KEYS(V15) // Encrypt V15 with 9 keys
|
| 1030 |
+
XXLOR VS10, VS10, V23 // Save possible last key
|
| 1031 |
+
BLT CR2, final_block_last
|
| 1032 |
+
VCIPHER_1X2_KEYS(V15, VS10, VS11) // Encrypt V15 with next 2 keys
|
| 1033 |
+
XXLOR VS12, VS12, V23 // Save possible last key
|
| 1034 |
+
BEQ CR2, final_block_last
|
| 1035 |
+
VCIPHER_1X2_KEYS(V15, VS12, VS13) // Encrypt V15 with last 2 keys
|
| 1036 |
+
XXLOR VS14, VS14, V23 // Save last key
|
| 1037 |
+
final_block_last:
|
| 1038 |
+
VCIPHERLAST V15, V23, V15 // Finish encryption
|
| 1039 |
+
#ifdef GOPPC64_power10
|
| 1040 |
+
// set up length
|
| 1041 |
+
SLD $56, IN_LEN, R17
|
| 1042 |
+
LXVLL BLK_INP, R17, V25
|
| 1043 |
+
VXOR V25, V15, V25
|
| 1044 |
+
STXVLL V25, BLK_OUT, R17
|
| 1045 |
+
#else
|
| 1046 |
+
ADD $32, R1, MASK_PTR
|
| 1047 |
+
MOVD $0, R16
|
| 1048 |
+
P8_STXVB16X(V15, MASK_PTR, R0)
|
| 1049 |
+
CMP IN_LEN, $8
|
| 1050 |
+
BLT next4
|
| 1051 |
+
MOVD 0(MASK_PTR), R14
|
| 1052 |
+
MOVD 0(BLK_INP), R15
|
| 1053 |
+
XOR R14, R15, R14
|
| 1054 |
+
MOVD R14, 0(BLK_OUT)
|
| 1055 |
+
ADD $8, R16
|
| 1056 |
+
ADD $-8, IN_LEN
|
| 1057 |
+
next4:
|
| 1058 |
+
CMP IN_LEN, $4
|
| 1059 |
+
BLT next2
|
| 1060 |
+
MOVWZ (BLK_INP)(R16), R15
|
| 1061 |
+
MOVWZ (MASK_PTR)(R16), R14
|
| 1062 |
+
XOR R14, R15, R14
|
| 1063 |
+
MOVW R14, (R16)(BLK_OUT)
|
| 1064 |
+
ADD $4, R16
|
| 1065 |
+
ADD $-4, IN_LEN
|
| 1066 |
+
next2:
|
| 1067 |
+
CMP IN_LEN, $2
|
| 1068 |
+
BLT next1
|
| 1069 |
+
MOVHZ (BLK_INP)(R16), R15
|
| 1070 |
+
MOVHZ (MASK_PTR)(R16), R14
|
| 1071 |
+
XOR R14, R15, R14
|
| 1072 |
+
MOVH R14, (R16)(BLK_OUT)
|
| 1073 |
+
ADD $2, R16
|
| 1074 |
+
ADD $-2, IN_LEN
|
| 1075 |
+
next1:
|
| 1076 |
+
CMP IN_LEN, $1
|
| 1077 |
+
BLT done
|
| 1078 |
+
MOVBZ (MASK_PTR)(R16), R14
|
| 1079 |
+
MOVBZ (BLK_INP)(R16), R15
|
| 1080 |
+
XOR R14, R15, R14
|
| 1081 |
+
MOVB R14, (R16)(BLK_OUT)
|
| 1082 |
+
#endif
|
| 1083 |
+
done:
|
| 1084 |
+
// Save the updated counter value
|
| 1085 |
+
P8_STXVB16X(V30, COUNTER, R0)
|
| 1086 |
+
// Clear the keys
|
| 1087 |
+
XXLXOR VS0, VS0, VS0
|
| 1088 |
+
XXLXOR VS1, VS1, VS1
|
| 1089 |
+
XXLXOR VS2, VS2, VS2
|
| 1090 |
+
XXLXOR VS3, VS3, VS3
|
| 1091 |
+
XXLXOR VS4, VS4, VS4
|
| 1092 |
+
XXLXOR VS5, VS5, VS5
|
| 1093 |
+
XXLXOR VS6, VS6, VS6
|
| 1094 |
+
XXLXOR VS7, VS7, VS7
|
| 1095 |
+
XXLXOR VS8, VS8, VS8
|
| 1096 |
+
XXLXOR VS9, VS9, VS9
|
| 1097 |
+
XXLXOR VS10, VS10, VS10
|
| 1098 |
+
XXLXOR VS11, VS11, VS11
|
| 1099 |
+
XXLXOR VS12, VS12, VS12
|
| 1100 |
+
XXLXOR VS13, VS13, VS13
|
| 1101 |
+
XXLXOR VS14, VS14, VS14
|
| 1102 |
+
RET
|
| 1103 |
+
|
platform/dbops/binaries/go/go/src/crypto/aes/gcm_s390x.go
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2016 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package aes
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/cipher"
|
| 9 |
+
"crypto/internal/alias"
|
| 10 |
+
"crypto/subtle"
|
| 11 |
+
"encoding/binary"
|
| 12 |
+
"errors"
|
| 13 |
+
"internal/cpu"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
// This file contains two implementations of AES-GCM. The first implementation
|
| 17 |
+
// (gcmAsm) uses the KMCTR instruction to encrypt using AES in counter mode and
|
| 18 |
+
// the KIMD instruction for GHASH. The second implementation (gcmKMA) uses the
|
| 19 |
+
// newer KMA instruction which performs both operations.
|
| 20 |
+
|
| 21 |
+
// gcmCount represents a 16-byte big-endian count value.
|
| 22 |
+
type gcmCount [16]byte
|
| 23 |
+
|
| 24 |
+
// inc increments the rightmost 32-bits of the count value by 1.
|
| 25 |
+
func (x *gcmCount) inc() {
|
| 26 |
+
binary.BigEndian.PutUint32(x[len(x)-4:], binary.BigEndian.Uint32(x[len(x)-4:])+1)
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
// gcmLengths writes len0 || len1 as big-endian values to a 16-byte array.
|
| 30 |
+
func gcmLengths(len0, len1 uint64) [16]byte {
|
| 31 |
+
v := [16]byte{}
|
| 32 |
+
binary.BigEndian.PutUint64(v[0:], len0)
|
| 33 |
+
binary.BigEndian.PutUint64(v[8:], len1)
|
| 34 |
+
return v
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
// gcmHashKey represents the 16-byte hash key required by the GHASH algorithm.
|
| 38 |
+
type gcmHashKey [16]byte
|
| 39 |
+
|
| 40 |
+
type gcmAsm struct {
|
| 41 |
+
block *aesCipherAsm
|
| 42 |
+
hashKey gcmHashKey
|
| 43 |
+
nonceSize int
|
| 44 |
+
tagSize int
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
const (
|
| 48 |
+
gcmBlockSize = 16
|
| 49 |
+
gcmTagSize = 16
|
| 50 |
+
gcmMinimumTagSize = 12 // NIST SP 800-38D recommends tags with 12 or more bytes.
|
| 51 |
+
gcmStandardNonceSize = 12
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
var errOpen = errors.New("cipher: message authentication failed")
|
| 55 |
+
|
| 56 |
+
// Assert that aesCipherAsm implements the gcmAble interface.
|
| 57 |
+
var _ gcmAble = (*aesCipherAsm)(nil)
|
| 58 |
+
|
| 59 |
+
// NewGCM returns the AES cipher wrapped in Galois Counter Mode. This is only
|
| 60 |
+
// called by [crypto/cipher.NewGCM] via the gcmAble interface.
|
| 61 |
+
func (c *aesCipherAsm) NewGCM(nonceSize, tagSize int) (cipher.AEAD, error) {
|
| 62 |
+
var hk gcmHashKey
|
| 63 |
+
c.Encrypt(hk[:], hk[:])
|
| 64 |
+
g := gcmAsm{
|
| 65 |
+
block: c,
|
| 66 |
+
hashKey: hk,
|
| 67 |
+
nonceSize: nonceSize,
|
| 68 |
+
tagSize: tagSize,
|
| 69 |
+
}
|
| 70 |
+
if cpu.S390X.HasAESGCM {
|
| 71 |
+
g := gcmKMA{g}
|
| 72 |
+
return &g, nil
|
| 73 |
+
}
|
| 74 |
+
return &g, nil
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
func (g *gcmAsm) NonceSize() int {
|
| 78 |
+
return g.nonceSize
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
func (g *gcmAsm) Overhead() int {
|
| 82 |
+
return g.tagSize
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// sliceForAppend takes a slice and a requested number of bytes. It returns a
|
| 86 |
+
// slice with the contents of the given slice followed by that many bytes and a
|
| 87 |
+
// second slice that aliases into it and contains only the extra bytes. If the
|
| 88 |
+
// original slice has sufficient capacity then no allocation is performed.
|
| 89 |
+
func sliceForAppend(in []byte, n int) (head, tail []byte) {
|
| 90 |
+
if total := len(in) + n; cap(in) >= total {
|
| 91 |
+
head = in[:total]
|
| 92 |
+
} else {
|
| 93 |
+
head = make([]byte, total)
|
| 94 |
+
copy(head, in)
|
| 95 |
+
}
|
| 96 |
+
tail = head[len(in):]
|
| 97 |
+
return
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
// ghash uses the GHASH algorithm to hash data with the given key. The initial
|
| 101 |
+
// hash value is given by hash which will be updated with the new hash value.
|
| 102 |
+
// The length of data must be a multiple of 16-bytes.
|
| 103 |
+
//
|
| 104 |
+
//go:noescape
|
| 105 |
+
func ghash(key *gcmHashKey, hash *[16]byte, data []byte)
|
| 106 |
+
|
| 107 |
+
// paddedGHASH pads data with zeroes until its length is a multiple of
|
| 108 |
+
// 16-bytes. It then calculates a new value for hash using the GHASH algorithm.
|
| 109 |
+
func (g *gcmAsm) paddedGHASH(hash *[16]byte, data []byte) {
|
| 110 |
+
siz := len(data) &^ 0xf // align size to 16-bytes
|
| 111 |
+
if siz > 0 {
|
| 112 |
+
ghash(&g.hashKey, hash, data[:siz])
|
| 113 |
+
data = data[siz:]
|
| 114 |
+
}
|
| 115 |
+
if len(data) > 0 {
|
| 116 |
+
var s [16]byte
|
| 117 |
+
copy(s[:], data)
|
| 118 |
+
ghash(&g.hashKey, hash, s[:])
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// cryptBlocksGCM encrypts src using AES in counter mode using the given
|
| 123 |
+
// function code and key. The rightmost 32-bits of the counter are incremented
|
| 124 |
+
// between each block as required by the GCM spec. The initial counter value
|
| 125 |
+
// is given by cnt, which is updated with the value of the next counter value
|
| 126 |
+
// to use.
|
| 127 |
+
//
|
| 128 |
+
// The lengths of both dst and buf must be greater than or equal to the length
|
| 129 |
+
// of src. buf may be partially or completely overwritten during the execution
|
| 130 |
+
// of the function.
|
| 131 |
+
//
|
| 132 |
+
//go:noescape
|
| 133 |
+
func cryptBlocksGCM(fn code, key, dst, src, buf []byte, cnt *gcmCount)
|
| 134 |
+
|
| 135 |
+
// counterCrypt encrypts src using AES in counter mode and places the result
|
| 136 |
+
// into dst. cnt is the initial count value and will be updated with the next
|
| 137 |
+
// count value. The length of dst must be greater than or equal to the length
|
| 138 |
+
// of src.
|
| 139 |
+
func (g *gcmAsm) counterCrypt(dst, src []byte, cnt *gcmCount) {
|
| 140 |
+
// Copying src into a buffer improves performance on some models when
|
| 141 |
+
// src and dst point to the same underlying array. We also need a
|
| 142 |
+
// buffer for counter values.
|
| 143 |
+
var ctrbuf, srcbuf [2048]byte
|
| 144 |
+
for len(src) >= 16 {
|
| 145 |
+
siz := len(src)
|
| 146 |
+
if len(src) > len(ctrbuf) {
|
| 147 |
+
siz = len(ctrbuf)
|
| 148 |
+
}
|
| 149 |
+
siz &^= 0xf // align siz to 16-bytes
|
| 150 |
+
copy(srcbuf[:], src[:siz])
|
| 151 |
+
cryptBlocksGCM(g.block.function, g.block.key, dst[:siz], srcbuf[:siz], ctrbuf[:], cnt)
|
| 152 |
+
src = src[siz:]
|
| 153 |
+
dst = dst[siz:]
|
| 154 |
+
}
|
| 155 |
+
if len(src) > 0 {
|
| 156 |
+
var x [16]byte
|
| 157 |
+
g.block.Encrypt(x[:], cnt[:])
|
| 158 |
+
for i := range src {
|
| 159 |
+
dst[i] = src[i] ^ x[i]
|
| 160 |
+
}
|
| 161 |
+
cnt.inc()
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
// deriveCounter computes the initial GCM counter state from the given nonce.
|
| 166 |
+
// See NIST SP 800-38D, section 7.1.
|
| 167 |
+
func (g *gcmAsm) deriveCounter(nonce []byte) gcmCount {
|
| 168 |
+
// GCM has two modes of operation with respect to the initial counter
|
| 169 |
+
// state: a "fast path" for 96-bit (12-byte) nonces, and a "slow path"
|
| 170 |
+
// for nonces of other lengths. For a 96-bit nonce, the nonce, along
|
| 171 |
+
// with a four-byte big-endian counter starting at one, is used
|
| 172 |
+
// directly as the starting counter. For other nonce sizes, the counter
|
| 173 |
+
// is computed by passing it through the GHASH function.
|
| 174 |
+
var counter gcmCount
|
| 175 |
+
if len(nonce) == gcmStandardNonceSize {
|
| 176 |
+
copy(counter[:], nonce)
|
| 177 |
+
counter[gcmBlockSize-1] = 1
|
| 178 |
+
} else {
|
| 179 |
+
var hash [16]byte
|
| 180 |
+
g.paddedGHASH(&hash, nonce)
|
| 181 |
+
lens := gcmLengths(0, uint64(len(nonce))*8)
|
| 182 |
+
g.paddedGHASH(&hash, lens[:])
|
| 183 |
+
copy(counter[:], hash[:])
|
| 184 |
+
}
|
| 185 |
+
return counter
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
// auth calculates GHASH(ciphertext, additionalData), masks the result with
|
| 189 |
+
// tagMask and writes the result to out.
|
| 190 |
+
func (g *gcmAsm) auth(out, ciphertext, additionalData []byte, tagMask *[gcmTagSize]byte) {
|
| 191 |
+
var hash [16]byte
|
| 192 |
+
g.paddedGHASH(&hash, additionalData)
|
| 193 |
+
g.paddedGHASH(&hash, ciphertext)
|
| 194 |
+
lens := gcmLengths(uint64(len(additionalData))*8, uint64(len(ciphertext))*8)
|
| 195 |
+
g.paddedGHASH(&hash, lens[:])
|
| 196 |
+
|
| 197 |
+
copy(out, hash[:])
|
| 198 |
+
for i := range out {
|
| 199 |
+
out[i] ^= tagMask[i]
|
| 200 |
+
}
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
// Seal encrypts and authenticates plaintext. See the [cipher.AEAD] interface for
|
| 204 |
+
// details.
|
| 205 |
+
func (g *gcmAsm) Seal(dst, nonce, plaintext, data []byte) []byte {
|
| 206 |
+
if len(nonce) != g.nonceSize {
|
| 207 |
+
panic("crypto/cipher: incorrect nonce length given to GCM")
|
| 208 |
+
}
|
| 209 |
+
if uint64(len(plaintext)) > ((1<<32)-2)*BlockSize {
|
| 210 |
+
panic("crypto/cipher: message too large for GCM")
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
ret, out := sliceForAppend(dst, len(plaintext)+g.tagSize)
|
| 214 |
+
if alias.InexactOverlap(out[:len(plaintext)], plaintext) {
|
| 215 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
counter := g.deriveCounter(nonce)
|
| 219 |
+
|
| 220 |
+
var tagMask [gcmBlockSize]byte
|
| 221 |
+
g.block.Encrypt(tagMask[:], counter[:])
|
| 222 |
+
counter.inc()
|
| 223 |
+
|
| 224 |
+
var tagOut [gcmTagSize]byte
|
| 225 |
+
g.counterCrypt(out, plaintext, &counter)
|
| 226 |
+
g.auth(tagOut[:], out[:len(plaintext)], data, &tagMask)
|
| 227 |
+
copy(out[len(plaintext):], tagOut[:])
|
| 228 |
+
|
| 229 |
+
return ret
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
// Open authenticates and decrypts ciphertext. See the [cipher.AEAD] interface
|
| 233 |
+
// for details.
|
| 234 |
+
func (g *gcmAsm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
|
| 235 |
+
if len(nonce) != g.nonceSize {
|
| 236 |
+
panic("crypto/cipher: incorrect nonce length given to GCM")
|
| 237 |
+
}
|
| 238 |
+
// Sanity check to prevent the authentication from always succeeding if an implementation
|
| 239 |
+
// leaves tagSize uninitialized, for example.
|
| 240 |
+
if g.tagSize < gcmMinimumTagSize {
|
| 241 |
+
panic("crypto/cipher: incorrect GCM tag size")
|
| 242 |
+
}
|
| 243 |
+
if len(ciphertext) < g.tagSize {
|
| 244 |
+
return nil, errOpen
|
| 245 |
+
}
|
| 246 |
+
if uint64(len(ciphertext)) > ((1<<32)-2)*uint64(BlockSize)+uint64(g.tagSize) {
|
| 247 |
+
return nil, errOpen
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
tag := ciphertext[len(ciphertext)-g.tagSize:]
|
| 251 |
+
ciphertext = ciphertext[:len(ciphertext)-g.tagSize]
|
| 252 |
+
|
| 253 |
+
counter := g.deriveCounter(nonce)
|
| 254 |
+
|
| 255 |
+
var tagMask [gcmBlockSize]byte
|
| 256 |
+
g.block.Encrypt(tagMask[:], counter[:])
|
| 257 |
+
counter.inc()
|
| 258 |
+
|
| 259 |
+
var expectedTag [gcmTagSize]byte
|
| 260 |
+
g.auth(expectedTag[:], ciphertext, data, &tagMask)
|
| 261 |
+
|
| 262 |
+
ret, out := sliceForAppend(dst, len(ciphertext))
|
| 263 |
+
if alias.InexactOverlap(out, ciphertext) {
|
| 264 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
if subtle.ConstantTimeCompare(expectedTag[:g.tagSize], tag) != 1 {
|
| 268 |
+
// The AESNI code decrypts and authenticates concurrently, and
|
| 269 |
+
// so overwrites dst in the event of a tag mismatch. That
|
| 270 |
+
// behavior is mimicked here in order to be consistent across
|
| 271 |
+
// platforms.
|
| 272 |
+
for i := range out {
|
| 273 |
+
out[i] = 0
|
| 274 |
+
}
|
| 275 |
+
return nil, errOpen
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
g.counterCrypt(out, ciphertext, &counter)
|
| 279 |
+
return ret, nil
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
// gcmKMA implements the cipher.AEAD interface using the KMA instruction. It should
|
| 283 |
+
// only be used if hasKMA is true.
|
| 284 |
+
type gcmKMA struct {
|
| 285 |
+
gcmAsm
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
// flags for the KMA instruction
|
| 289 |
+
const (
|
| 290 |
+
kmaHS = 1 << 10 // hash subkey supplied
|
| 291 |
+
kmaLAAD = 1 << 9 // last series of additional authenticated data
|
| 292 |
+
kmaLPC = 1 << 8 // last series of plaintext or ciphertext blocks
|
| 293 |
+
kmaDecrypt = 1 << 7 // decrypt
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
// kmaGCM executes the encryption or decryption operation given by fn. The tag
|
| 297 |
+
// will be calculated and written to tag. cnt should contain the current
|
| 298 |
+
// counter state and will be overwritten with the updated counter state.
|
| 299 |
+
// TODO(mundaym): could pass in hash subkey
|
| 300 |
+
//
|
| 301 |
+
//go:noescape
|
| 302 |
+
func kmaGCM(fn code, key, dst, src, aad []byte, tag *[16]byte, cnt *gcmCount)
|
| 303 |
+
|
| 304 |
+
// Seal encrypts and authenticates plaintext. See the [cipher.AEAD] interface for
|
| 305 |
+
// details.
|
| 306 |
+
func (g *gcmKMA) Seal(dst, nonce, plaintext, data []byte) []byte {
|
| 307 |
+
if len(nonce) != g.nonceSize {
|
| 308 |
+
panic("crypto/cipher: incorrect nonce length given to GCM")
|
| 309 |
+
}
|
| 310 |
+
if uint64(len(plaintext)) > ((1<<32)-2)*BlockSize {
|
| 311 |
+
panic("crypto/cipher: message too large for GCM")
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
ret, out := sliceForAppend(dst, len(plaintext)+g.tagSize)
|
| 315 |
+
if alias.InexactOverlap(out[:len(plaintext)], plaintext) {
|
| 316 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
counter := g.deriveCounter(nonce)
|
| 320 |
+
fc := g.block.function | kmaLAAD | kmaLPC
|
| 321 |
+
|
| 322 |
+
var tag [gcmTagSize]byte
|
| 323 |
+
kmaGCM(fc, g.block.key, out[:len(plaintext)], plaintext, data, &tag, &counter)
|
| 324 |
+
copy(out[len(plaintext):], tag[:])
|
| 325 |
+
|
| 326 |
+
return ret
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
// Open authenticates and decrypts ciphertext. See the [cipher.AEAD] interface
|
| 330 |
+
// for details.
|
| 331 |
+
func (g *gcmKMA) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
|
| 332 |
+
if len(nonce) != g.nonceSize {
|
| 333 |
+
panic("crypto/cipher: incorrect nonce length given to GCM")
|
| 334 |
+
}
|
| 335 |
+
if len(ciphertext) < g.tagSize {
|
| 336 |
+
return nil, errOpen
|
| 337 |
+
}
|
| 338 |
+
if uint64(len(ciphertext)) > ((1<<32)-2)*uint64(BlockSize)+uint64(g.tagSize) {
|
| 339 |
+
return nil, errOpen
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
tag := ciphertext[len(ciphertext)-g.tagSize:]
|
| 343 |
+
ciphertext = ciphertext[:len(ciphertext)-g.tagSize]
|
| 344 |
+
ret, out := sliceForAppend(dst, len(ciphertext))
|
| 345 |
+
if alias.InexactOverlap(out, ciphertext) {
|
| 346 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
if g.tagSize < gcmMinimumTagSize {
|
| 350 |
+
panic("crypto/cipher: incorrect GCM tag size")
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
counter := g.deriveCounter(nonce)
|
| 354 |
+
fc := g.block.function | kmaLAAD | kmaLPC | kmaDecrypt
|
| 355 |
+
|
| 356 |
+
var expectedTag [gcmTagSize]byte
|
| 357 |
+
kmaGCM(fc, g.block.key, out[:len(ciphertext)], ciphertext, data, &expectedTag, &counter)
|
| 358 |
+
|
| 359 |
+
if subtle.ConstantTimeCompare(expectedTag[:g.tagSize], tag) != 1 {
|
| 360 |
+
// The AESNI code decrypts and authenticates concurrently, and
|
| 361 |
+
// so overwrites dst in the event of a tag mismatch. That
|
| 362 |
+
// behavior is mimicked here in order to be consistent across
|
| 363 |
+
// platforms.
|
| 364 |
+
for i := range out {
|
| 365 |
+
out[i] = 0
|
| 366 |
+
}
|
| 367 |
+
return nil, errOpen
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
return ret, nil
|
| 371 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/aes/modes.go
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2016 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package aes
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/cipher"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
// gcmAble is implemented by cipher.Blocks that can provide an optimized
|
| 12 |
+
// implementation of GCM through the AEAD interface.
|
| 13 |
+
// See crypto/cipher/gcm.go.
|
| 14 |
+
type gcmAble interface {
|
| 15 |
+
NewGCM(nonceSize, tagSize int) (cipher.AEAD, error)
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
// cbcEncAble is implemented by cipher.Blocks that can provide an optimized
|
| 19 |
+
// implementation of CBC encryption through the cipher.BlockMode interface.
|
| 20 |
+
// See crypto/cipher/cbc.go.
|
| 21 |
+
type cbcEncAble interface {
|
| 22 |
+
NewCBCEncrypter(iv []byte) cipher.BlockMode
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
// cbcDecAble is implemented by cipher.Blocks that can provide an optimized
|
| 26 |
+
// implementation of CBC decryption through the cipher.BlockMode interface.
|
| 27 |
+
// See crypto/cipher/cbc.go.
|
| 28 |
+
type cbcDecAble interface {
|
| 29 |
+
NewCBCDecrypter(iv []byte) cipher.BlockMode
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
// ctrAble is implemented by cipher.Blocks that can provide an optimized
|
| 33 |
+
// implementation of CTR through the cipher.Stream interface.
|
| 34 |
+
// See crypto/cipher/ctr.go.
|
| 35 |
+
type ctrAble interface {
|
| 36 |
+
NewCTR(iv []byte) cipher.Stream
|
| 37 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/aes/modes_test.go
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2016 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package aes
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/cipher"
|
| 9 |
+
"testing"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
// Check that the optimized implementations of cipher modes will
|
| 13 |
+
// be picked up correctly.
|
| 14 |
+
|
| 15 |
+
// testInterface can be asserted to check that a type originates
|
| 16 |
+
// from this test group.
|
| 17 |
+
type testInterface interface {
|
| 18 |
+
InAESPackage() bool
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
// testBlock implements the cipher.Block interface and any *Able
|
| 22 |
+
// interfaces that need to be tested.
|
| 23 |
+
type testBlock struct{}
|
| 24 |
+
|
| 25 |
+
func (*testBlock) BlockSize() int { return 0 }
|
| 26 |
+
func (*testBlock) Encrypt(a, b []byte) {}
|
| 27 |
+
func (*testBlock) Decrypt(a, b []byte) {}
|
| 28 |
+
func (*testBlock) NewGCM(int, int) (cipher.AEAD, error) {
|
| 29 |
+
return &testAEAD{}, nil
|
| 30 |
+
}
|
| 31 |
+
func (*testBlock) NewCBCEncrypter([]byte) cipher.BlockMode {
|
| 32 |
+
return &testBlockMode{}
|
| 33 |
+
}
|
| 34 |
+
func (*testBlock) NewCBCDecrypter([]byte) cipher.BlockMode {
|
| 35 |
+
return &testBlockMode{}
|
| 36 |
+
}
|
| 37 |
+
func (*testBlock) NewCTR([]byte) cipher.Stream {
|
| 38 |
+
return &testStream{}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// testAEAD implements the cipher.AEAD interface.
|
| 42 |
+
type testAEAD struct{}
|
| 43 |
+
|
| 44 |
+
func (*testAEAD) NonceSize() int { return 0 }
|
| 45 |
+
func (*testAEAD) Overhead() int { return 0 }
|
| 46 |
+
func (*testAEAD) Seal(a, b, c, d []byte) []byte { return []byte{} }
|
| 47 |
+
func (*testAEAD) Open(a, b, c, d []byte) ([]byte, error) { return []byte{}, nil }
|
| 48 |
+
func (*testAEAD) InAESPackage() bool { return true }
|
| 49 |
+
|
| 50 |
+
// Test the gcmAble interface is detected correctly by the cipher package.
|
| 51 |
+
func TestGCMAble(t *testing.T) {
|
| 52 |
+
b := cipher.Block(&testBlock{})
|
| 53 |
+
if _, ok := b.(gcmAble); !ok {
|
| 54 |
+
t.Fatalf("testBlock does not implement the gcmAble interface")
|
| 55 |
+
}
|
| 56 |
+
aead, err := cipher.NewGCM(b)
|
| 57 |
+
if err != nil {
|
| 58 |
+
t.Fatalf("%v", err)
|
| 59 |
+
}
|
| 60 |
+
if _, ok := aead.(testInterface); !ok {
|
| 61 |
+
t.Fatalf("cipher.NewGCM did not use gcmAble interface")
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
// testBlockMode implements the cipher.BlockMode interface.
|
| 66 |
+
type testBlockMode struct{}
|
| 67 |
+
|
| 68 |
+
func (*testBlockMode) BlockSize() int { return 0 }
|
| 69 |
+
func (*testBlockMode) CryptBlocks(a, b []byte) {}
|
| 70 |
+
func (*testBlockMode) InAESPackage() bool { return true }
|
| 71 |
+
|
| 72 |
+
// Test the cbcEncAble interface is detected correctly by the cipher package.
|
| 73 |
+
func TestCBCEncAble(t *testing.T) {
|
| 74 |
+
b := cipher.Block(&testBlock{})
|
| 75 |
+
if _, ok := b.(cbcEncAble); !ok {
|
| 76 |
+
t.Fatalf("testBlock does not implement the cbcEncAble interface")
|
| 77 |
+
}
|
| 78 |
+
bm := cipher.NewCBCEncrypter(b, []byte{})
|
| 79 |
+
if _, ok := bm.(testInterface); !ok {
|
| 80 |
+
t.Fatalf("cipher.NewCBCEncrypter did not use cbcEncAble interface")
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// Test the cbcDecAble interface is detected correctly by the cipher package.
|
| 85 |
+
func TestCBCDecAble(t *testing.T) {
|
| 86 |
+
b := cipher.Block(&testBlock{})
|
| 87 |
+
if _, ok := b.(cbcDecAble); !ok {
|
| 88 |
+
t.Fatalf("testBlock does not implement the cbcDecAble interface")
|
| 89 |
+
}
|
| 90 |
+
bm := cipher.NewCBCDecrypter(b, []byte{})
|
| 91 |
+
if _, ok := bm.(testInterface); !ok {
|
| 92 |
+
t.Fatalf("cipher.NewCBCDecrypter did not use cbcDecAble interface")
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
// testStream implements the cipher.Stream interface.
|
| 97 |
+
type testStream struct{}
|
| 98 |
+
|
| 99 |
+
func (*testStream) XORKeyStream(a, b []byte) {}
|
| 100 |
+
func (*testStream) InAESPackage() bool { return true }
|
| 101 |
+
|
| 102 |
+
// Test the ctrAble interface is detected correctly by the cipher package.
|
| 103 |
+
func TestCTRAble(t *testing.T) {
|
| 104 |
+
b := cipher.Block(&testBlock{})
|
| 105 |
+
if _, ok := b.(ctrAble); !ok {
|
| 106 |
+
t.Fatalf("testBlock does not implement the ctrAble interface")
|
| 107 |
+
}
|
| 108 |
+
s := cipher.NewCTR(b, []byte{})
|
| 109 |
+
if _, ok := s.(testInterface); !ok {
|
| 110 |
+
t.Fatalf("cipher.NewCTR did not use ctrAble interface")
|
| 111 |
+
}
|
| 112 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/boring/boring.go
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2020 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
//go:build boringcrypto
|
| 6 |
+
|
| 7 |
+
// Package boring exposes functions that are only available when building with
|
| 8 |
+
// Go+BoringCrypto. This package is available on all targets as long as the
|
| 9 |
+
// Go+BoringCrypto toolchain is used. Use the Enabled function to determine
|
| 10 |
+
// whether the BoringCrypto core is actually in use.
|
| 11 |
+
//
|
| 12 |
+
// Any time the Go+BoringCrypto toolchain is used, the "boringcrypto" build tag
|
| 13 |
+
// is satisfied, so that applications can tag files that use this package.
|
| 14 |
+
package boring
|
| 15 |
+
|
| 16 |
+
import "crypto/internal/boring"
|
| 17 |
+
|
| 18 |
+
// Enabled reports whether BoringCrypto handles supported crypto operations.
|
| 19 |
+
func Enabled() bool {
|
| 20 |
+
return boring.Enabled
|
| 21 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/boring/boring_test.go
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2020 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
//go:build boringcrypto
|
| 6 |
+
|
| 7 |
+
package boring_test
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"crypto/boring"
|
| 11 |
+
"runtime"
|
| 12 |
+
"testing"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
func TestEnabled(t *testing.T) {
|
| 16 |
+
supportedPlatform := runtime.GOOS == "linux" && (runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64")
|
| 17 |
+
if supportedPlatform && !boring.Enabled() {
|
| 18 |
+
t.Error("Enabled returned false on a supported platform")
|
| 19 |
+
} else if !supportedPlatform && boring.Enabled() {
|
| 20 |
+
t.Error("Enabled returned true on an unsupported platform")
|
| 21 |
+
}
|
| 22 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/boring/notboring_test.go
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2020 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
//go:build (goexperiment.boringcrypto && !boringcrypto) || (!goexperiment.boringcrypto && boringcrypto)
|
| 6 |
+
|
| 7 |
+
package boring_test
|
| 8 |
+
|
| 9 |
+
import "testing"
|
| 10 |
+
|
| 11 |
+
func TestNotBoring(t *testing.T) {
|
| 12 |
+
t.Error("goexperiment.boringcrypto and boringcrypto should be equivalent build tags")
|
| 13 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/benchmark_test.go
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2013 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package cipher_test
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/aes"
|
| 9 |
+
"crypto/cipher"
|
| 10 |
+
"strconv"
|
| 11 |
+
"testing"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
func benchmarkAESGCMSeal(b *testing.B, buf []byte, keySize int) {
|
| 15 |
+
b.ReportAllocs()
|
| 16 |
+
b.SetBytes(int64(len(buf)))
|
| 17 |
+
|
| 18 |
+
var key = make([]byte, keySize)
|
| 19 |
+
var nonce [12]byte
|
| 20 |
+
var ad [13]byte
|
| 21 |
+
aes, _ := aes.NewCipher(key[:])
|
| 22 |
+
aesgcm, _ := cipher.NewGCM(aes)
|
| 23 |
+
var out []byte
|
| 24 |
+
|
| 25 |
+
b.ResetTimer()
|
| 26 |
+
for i := 0; i < b.N; i++ {
|
| 27 |
+
out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:])
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
func benchmarkAESGCMOpen(b *testing.B, buf []byte, keySize int) {
|
| 32 |
+
b.ReportAllocs()
|
| 33 |
+
b.SetBytes(int64(len(buf)))
|
| 34 |
+
|
| 35 |
+
var key = make([]byte, keySize)
|
| 36 |
+
var nonce [12]byte
|
| 37 |
+
var ad [13]byte
|
| 38 |
+
aes, _ := aes.NewCipher(key[:])
|
| 39 |
+
aesgcm, _ := cipher.NewGCM(aes)
|
| 40 |
+
var out []byte
|
| 41 |
+
|
| 42 |
+
ct := aesgcm.Seal(nil, nonce[:], buf[:], ad[:])
|
| 43 |
+
|
| 44 |
+
b.ResetTimer()
|
| 45 |
+
for i := 0; i < b.N; i++ {
|
| 46 |
+
out, _ = aesgcm.Open(out[:0], nonce[:], ct, ad[:])
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
func BenchmarkAESGCM(b *testing.B) {
|
| 51 |
+
for _, length := range []int{64, 1350, 8 * 1024} {
|
| 52 |
+
b.Run("Open-128-"+strconv.Itoa(length), func(b *testing.B) {
|
| 53 |
+
benchmarkAESGCMOpen(b, make([]byte, length), 128/8)
|
| 54 |
+
})
|
| 55 |
+
b.Run("Seal-128-"+strconv.Itoa(length), func(b *testing.B) {
|
| 56 |
+
benchmarkAESGCMSeal(b, make([]byte, length), 128/8)
|
| 57 |
+
})
|
| 58 |
+
|
| 59 |
+
b.Run("Open-256-"+strconv.Itoa(length), func(b *testing.B) {
|
| 60 |
+
benchmarkAESGCMOpen(b, make([]byte, length), 256/8)
|
| 61 |
+
})
|
| 62 |
+
b.Run("Seal-256-"+strconv.Itoa(length), func(b *testing.B) {
|
| 63 |
+
benchmarkAESGCMSeal(b, make([]byte, length), 256/8)
|
| 64 |
+
})
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
func benchmarkAESStream(b *testing.B, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) {
|
| 69 |
+
b.SetBytes(int64(len(buf)))
|
| 70 |
+
|
| 71 |
+
var key [16]byte
|
| 72 |
+
var iv [16]byte
|
| 73 |
+
aes, _ := aes.NewCipher(key[:])
|
| 74 |
+
stream := mode(aes, iv[:])
|
| 75 |
+
|
| 76 |
+
b.ResetTimer()
|
| 77 |
+
for i := 0; i < b.N; i++ {
|
| 78 |
+
stream.XORKeyStream(buf, buf)
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
// If we test exactly 1K blocks, we would generate exact multiples of
|
| 83 |
+
// the cipher's block size, and the cipher stream fragments would
|
| 84 |
+
// always be wordsize aligned, whereas non-aligned is a more typical
|
| 85 |
+
// use-case.
|
| 86 |
+
const almost1K = 1024 - 5
|
| 87 |
+
const almost8K = 8*1024 - 5
|
| 88 |
+
|
| 89 |
+
func BenchmarkAESCFBEncrypt1K(b *testing.B) {
|
| 90 |
+
benchmarkAESStream(b, cipher.NewCFBEncrypter, make([]byte, almost1K))
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
func BenchmarkAESCFBDecrypt1K(b *testing.B) {
|
| 94 |
+
benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost1K))
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
func BenchmarkAESCFBDecrypt8K(b *testing.B) {
|
| 98 |
+
benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost8K))
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
func BenchmarkAESOFB1K(b *testing.B) {
|
| 102 |
+
benchmarkAESStream(b, cipher.NewOFB, make([]byte, almost1K))
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
func BenchmarkAESCTR1K(b *testing.B) {
|
| 106 |
+
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost1K))
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
func BenchmarkAESCTR8K(b *testing.B) {
|
| 110 |
+
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost8K))
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
func BenchmarkAESCBCEncrypt1K(b *testing.B) {
|
| 114 |
+
buf := make([]byte, 1024)
|
| 115 |
+
b.SetBytes(int64(len(buf)))
|
| 116 |
+
|
| 117 |
+
var key [16]byte
|
| 118 |
+
var iv [16]byte
|
| 119 |
+
aes, _ := aes.NewCipher(key[:])
|
| 120 |
+
cbc := cipher.NewCBCEncrypter(aes, iv[:])
|
| 121 |
+
for i := 0; i < b.N; i++ {
|
| 122 |
+
cbc.CryptBlocks(buf, buf)
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
func BenchmarkAESCBCDecrypt1K(b *testing.B) {
|
| 127 |
+
buf := make([]byte, 1024)
|
| 128 |
+
b.SetBytes(int64(len(buf)))
|
| 129 |
+
|
| 130 |
+
var key [16]byte
|
| 131 |
+
var iv [16]byte
|
| 132 |
+
aes, _ := aes.NewCipher(key[:])
|
| 133 |
+
cbc := cipher.NewCBCDecrypter(aes, iv[:])
|
| 134 |
+
for i := 0; i < b.N; i++ {
|
| 135 |
+
cbc.CryptBlocks(buf, buf)
|
| 136 |
+
}
|
| 137 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/cbc.go
ADDED
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// Cipher block chaining (CBC) mode.
|
| 6 |
+
|
| 7 |
+
// CBC provides confidentiality by xoring (chaining) each plaintext block
|
| 8 |
+
// with the previous ciphertext block before applying the block cipher.
|
| 9 |
+
|
| 10 |
+
// See NIST SP 800-38A, pp 10-11
|
| 11 |
+
|
| 12 |
+
package cipher
|
| 13 |
+
|
| 14 |
+
import (
|
| 15 |
+
"bytes"
|
| 16 |
+
"crypto/internal/alias"
|
| 17 |
+
"crypto/subtle"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
type cbc struct {
|
| 21 |
+
b Block
|
| 22 |
+
blockSize int
|
| 23 |
+
iv []byte
|
| 24 |
+
tmp []byte
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
func newCBC(b Block, iv []byte) *cbc {
|
| 28 |
+
return &cbc{
|
| 29 |
+
b: b,
|
| 30 |
+
blockSize: b.BlockSize(),
|
| 31 |
+
iv: bytes.Clone(iv),
|
| 32 |
+
tmp: make([]byte, b.BlockSize()),
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
type cbcEncrypter cbc
|
| 37 |
+
|
| 38 |
+
// cbcEncAble is an interface implemented by ciphers that have a specific
|
| 39 |
+
// optimized implementation of CBC encryption, like crypto/aes.
|
| 40 |
+
// NewCBCEncrypter will check for this interface and return the specific
|
| 41 |
+
// BlockMode if found.
|
| 42 |
+
type cbcEncAble interface {
|
| 43 |
+
NewCBCEncrypter(iv []byte) BlockMode
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// NewCBCEncrypter returns a BlockMode which encrypts in cipher block chaining
|
| 47 |
+
// mode, using the given Block. The length of iv must be the same as the
|
| 48 |
+
// Block's block size.
|
| 49 |
+
func NewCBCEncrypter(b Block, iv []byte) BlockMode {
|
| 50 |
+
if len(iv) != b.BlockSize() {
|
| 51 |
+
panic("cipher.NewCBCEncrypter: IV length must equal block size")
|
| 52 |
+
}
|
| 53 |
+
if cbc, ok := b.(cbcEncAble); ok {
|
| 54 |
+
return cbc.NewCBCEncrypter(iv)
|
| 55 |
+
}
|
| 56 |
+
return (*cbcEncrypter)(newCBC(b, iv))
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// newCBCGenericEncrypter returns a BlockMode which encrypts in cipher block chaining
|
| 60 |
+
// mode, using the given Block. The length of iv must be the same as the
|
| 61 |
+
// Block's block size. This always returns the generic non-asm encrypter for use
|
| 62 |
+
// in fuzz testing.
|
| 63 |
+
func newCBCGenericEncrypter(b Block, iv []byte) BlockMode {
|
| 64 |
+
if len(iv) != b.BlockSize() {
|
| 65 |
+
panic("cipher.NewCBCEncrypter: IV length must equal block size")
|
| 66 |
+
}
|
| 67 |
+
return (*cbcEncrypter)(newCBC(b, iv))
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
func (x *cbcEncrypter) BlockSize() int { return x.blockSize }
|
| 71 |
+
|
| 72 |
+
func (x *cbcEncrypter) CryptBlocks(dst, src []byte) {
|
| 73 |
+
if len(src)%x.blockSize != 0 {
|
| 74 |
+
panic("crypto/cipher: input not full blocks")
|
| 75 |
+
}
|
| 76 |
+
if len(dst) < len(src) {
|
| 77 |
+
panic("crypto/cipher: output smaller than input")
|
| 78 |
+
}
|
| 79 |
+
if alias.InexactOverlap(dst[:len(src)], src) {
|
| 80 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
iv := x.iv
|
| 84 |
+
|
| 85 |
+
for len(src) > 0 {
|
| 86 |
+
// Write the xor to dst, then encrypt in place.
|
| 87 |
+
subtle.XORBytes(dst[:x.blockSize], src[:x.blockSize], iv)
|
| 88 |
+
x.b.Encrypt(dst[:x.blockSize], dst[:x.blockSize])
|
| 89 |
+
|
| 90 |
+
// Move to the next block with this block as the next iv.
|
| 91 |
+
iv = dst[:x.blockSize]
|
| 92 |
+
src = src[x.blockSize:]
|
| 93 |
+
dst = dst[x.blockSize:]
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
// Save the iv for the next CryptBlocks call.
|
| 97 |
+
copy(x.iv, iv)
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
func (x *cbcEncrypter) SetIV(iv []byte) {
|
| 101 |
+
if len(iv) != len(x.iv) {
|
| 102 |
+
panic("cipher: incorrect length IV")
|
| 103 |
+
}
|
| 104 |
+
copy(x.iv, iv)
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
type cbcDecrypter cbc
|
| 108 |
+
|
| 109 |
+
// cbcDecAble is an interface implemented by ciphers that have a specific
|
| 110 |
+
// optimized implementation of CBC decryption, like crypto/aes.
|
| 111 |
+
// NewCBCDecrypter will check for this interface and return the specific
|
| 112 |
+
// BlockMode if found.
|
| 113 |
+
type cbcDecAble interface {
|
| 114 |
+
NewCBCDecrypter(iv []byte) BlockMode
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// NewCBCDecrypter returns a BlockMode which decrypts in cipher block chaining
|
| 118 |
+
// mode, using the given Block. The length of iv must be the same as the
|
| 119 |
+
// Block's block size and must match the iv used to encrypt the data.
|
| 120 |
+
func NewCBCDecrypter(b Block, iv []byte) BlockMode {
|
| 121 |
+
if len(iv) != b.BlockSize() {
|
| 122 |
+
panic("cipher.NewCBCDecrypter: IV length must equal block size")
|
| 123 |
+
}
|
| 124 |
+
if cbc, ok := b.(cbcDecAble); ok {
|
| 125 |
+
return cbc.NewCBCDecrypter(iv)
|
| 126 |
+
}
|
| 127 |
+
return (*cbcDecrypter)(newCBC(b, iv))
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
// newCBCGenericDecrypter returns a BlockMode which encrypts in cipher block chaining
|
| 131 |
+
// mode, using the given Block. The length of iv must be the same as the
|
| 132 |
+
// Block's block size. This always returns the generic non-asm decrypter for use in
|
| 133 |
+
// fuzz testing.
|
| 134 |
+
func newCBCGenericDecrypter(b Block, iv []byte) BlockMode {
|
| 135 |
+
if len(iv) != b.BlockSize() {
|
| 136 |
+
panic("cipher.NewCBCDecrypter: IV length must equal block size")
|
| 137 |
+
}
|
| 138 |
+
return (*cbcDecrypter)(newCBC(b, iv))
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
func (x *cbcDecrypter) BlockSize() int { return x.blockSize }
|
| 142 |
+
|
| 143 |
+
func (x *cbcDecrypter) CryptBlocks(dst, src []byte) {
|
| 144 |
+
if len(src)%x.blockSize != 0 {
|
| 145 |
+
panic("crypto/cipher: input not full blocks")
|
| 146 |
+
}
|
| 147 |
+
if len(dst) < len(src) {
|
| 148 |
+
panic("crypto/cipher: output smaller than input")
|
| 149 |
+
}
|
| 150 |
+
if alias.InexactOverlap(dst[:len(src)], src) {
|
| 151 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 152 |
+
}
|
| 153 |
+
if len(src) == 0 {
|
| 154 |
+
return
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
// For each block, we need to xor the decrypted data with the previous block's ciphertext (the iv).
|
| 158 |
+
// To avoid making a copy each time, we loop over the blocks BACKWARDS.
|
| 159 |
+
end := len(src)
|
| 160 |
+
start := end - x.blockSize
|
| 161 |
+
prev := start - x.blockSize
|
| 162 |
+
|
| 163 |
+
// Copy the last block of ciphertext in preparation as the new iv.
|
| 164 |
+
copy(x.tmp, src[start:end])
|
| 165 |
+
|
| 166 |
+
// Loop over all but the first block.
|
| 167 |
+
for start > 0 {
|
| 168 |
+
x.b.Decrypt(dst[start:end], src[start:end])
|
| 169 |
+
subtle.XORBytes(dst[start:end], dst[start:end], src[prev:start])
|
| 170 |
+
|
| 171 |
+
end = start
|
| 172 |
+
start = prev
|
| 173 |
+
prev -= x.blockSize
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
// The first block is special because it uses the saved iv.
|
| 177 |
+
x.b.Decrypt(dst[start:end], src[start:end])
|
| 178 |
+
subtle.XORBytes(dst[start:end], dst[start:end], x.iv)
|
| 179 |
+
|
| 180 |
+
// Set the new iv to the first block we copied earlier.
|
| 181 |
+
x.iv, x.tmp = x.tmp, x.iv
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
func (x *cbcDecrypter) SetIV(iv []byte) {
|
| 185 |
+
if len(iv) != len(x.iv) {
|
| 186 |
+
panic("cipher: incorrect length IV")
|
| 187 |
+
}
|
| 188 |
+
copy(x.iv, iv)
|
| 189 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/cbc_aes_test.go
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// CBC AES test vectors.
|
| 6 |
+
|
| 7 |
+
// See U.S. National Institute of Standards and Technology (NIST)
|
| 8 |
+
// Special Publication 800-38A, ``Recommendation for Block Cipher
|
| 9 |
+
// Modes of Operation,'' 2001 Edition, pp. 24-29.
|
| 10 |
+
|
| 11 |
+
package cipher_test
|
| 12 |
+
|
| 13 |
+
import (
|
| 14 |
+
"bytes"
|
| 15 |
+
"crypto/aes"
|
| 16 |
+
"crypto/cipher"
|
| 17 |
+
"testing"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
var cbcAESTests = []struct {
|
| 21 |
+
name string
|
| 22 |
+
key []byte
|
| 23 |
+
iv []byte
|
| 24 |
+
in []byte
|
| 25 |
+
out []byte
|
| 26 |
+
}{
|
| 27 |
+
// NIST SP 800-38A pp 27-29
|
| 28 |
+
{
|
| 29 |
+
"CBC-AES128",
|
| 30 |
+
commonKey128,
|
| 31 |
+
commonIV,
|
| 32 |
+
commonInput,
|
| 33 |
+
[]byte{
|
| 34 |
+
0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
|
| 35 |
+
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
|
| 36 |
+
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
|
| 37 |
+
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7,
|
| 38 |
+
},
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"CBC-AES192",
|
| 42 |
+
commonKey192,
|
| 43 |
+
commonIV,
|
| 44 |
+
commonInput,
|
| 45 |
+
[]byte{
|
| 46 |
+
0x4f, 0x02, 0x1d, 0xb2, 0x43, 0xbc, 0x63, 0x3d, 0x71, 0x78, 0x18, 0x3a, 0x9f, 0xa0, 0x71, 0xe8,
|
| 47 |
+
0xb4, 0xd9, 0xad, 0xa9, 0xad, 0x7d, 0xed, 0xf4, 0xe5, 0xe7, 0x38, 0x76, 0x3f, 0x69, 0x14, 0x5a,
|
| 48 |
+
0x57, 0x1b, 0x24, 0x20, 0x12, 0xfb, 0x7a, 0xe0, 0x7f, 0xa9, 0xba, 0xac, 0x3d, 0xf1, 0x02, 0xe0,
|
| 49 |
+
0x08, 0xb0, 0xe2, 0x79, 0x88, 0x59, 0x88, 0x81, 0xd9, 0x20, 0xa9, 0xe6, 0x4f, 0x56, 0x15, 0xcd,
|
| 50 |
+
},
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"CBC-AES256",
|
| 54 |
+
commonKey256,
|
| 55 |
+
commonIV,
|
| 56 |
+
commonInput,
|
| 57 |
+
[]byte{
|
| 58 |
+
0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
|
| 59 |
+
0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d,
|
| 60 |
+
0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
|
| 61 |
+
0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b,
|
| 62 |
+
},
|
| 63 |
+
},
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
func TestCBCEncrypterAES(t *testing.T) {
|
| 67 |
+
for _, test := range cbcAESTests {
|
| 68 |
+
c, err := aes.NewCipher(test.key)
|
| 69 |
+
if err != nil {
|
| 70 |
+
t.Errorf("%s: NewCipher(%d bytes) = %s", test.name, len(test.key), err)
|
| 71 |
+
continue
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
encrypter := cipher.NewCBCEncrypter(c, test.iv)
|
| 75 |
+
|
| 76 |
+
data := make([]byte, len(test.in))
|
| 77 |
+
copy(data, test.in)
|
| 78 |
+
|
| 79 |
+
encrypter.CryptBlocks(data, data)
|
| 80 |
+
if !bytes.Equal(test.out, data) {
|
| 81 |
+
t.Errorf("%s: CBCEncrypter\nhave %x\nwant %x", test.name, data, test.out)
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
func TestCBCDecrypterAES(t *testing.T) {
|
| 87 |
+
for _, test := range cbcAESTests {
|
| 88 |
+
c, err := aes.NewCipher(test.key)
|
| 89 |
+
if err != nil {
|
| 90 |
+
t.Errorf("%s: NewCipher(%d bytes) = %s", test.name, len(test.key), err)
|
| 91 |
+
continue
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
decrypter := cipher.NewCBCDecrypter(c, test.iv)
|
| 95 |
+
|
| 96 |
+
data := make([]byte, len(test.out))
|
| 97 |
+
copy(data, test.out)
|
| 98 |
+
|
| 99 |
+
decrypter.CryptBlocks(data, data)
|
| 100 |
+
if !bytes.Equal(test.in, data) {
|
| 101 |
+
t.Errorf("%s: CBCDecrypter\nhave %x\nwant %x", test.name, data, test.in)
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/cfb.go
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2010 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// CFB (Cipher Feedback) Mode.
|
| 6 |
+
|
| 7 |
+
package cipher
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"crypto/internal/alias"
|
| 11 |
+
"crypto/subtle"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
type cfb struct {
|
| 15 |
+
b Block
|
| 16 |
+
next []byte
|
| 17 |
+
out []byte
|
| 18 |
+
outUsed int
|
| 19 |
+
|
| 20 |
+
decrypt bool
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
func (x *cfb) XORKeyStream(dst, src []byte) {
|
| 24 |
+
if len(dst) < len(src) {
|
| 25 |
+
panic("crypto/cipher: output smaller than input")
|
| 26 |
+
}
|
| 27 |
+
if alias.InexactOverlap(dst[:len(src)], src) {
|
| 28 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 29 |
+
}
|
| 30 |
+
for len(src) > 0 {
|
| 31 |
+
if x.outUsed == len(x.out) {
|
| 32 |
+
x.b.Encrypt(x.out, x.next)
|
| 33 |
+
x.outUsed = 0
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
if x.decrypt {
|
| 37 |
+
// We can precompute a larger segment of the
|
| 38 |
+
// keystream on decryption. This will allow
|
| 39 |
+
// larger batches for xor, and we should be
|
| 40 |
+
// able to match CTR/OFB performance.
|
| 41 |
+
copy(x.next[x.outUsed:], src)
|
| 42 |
+
}
|
| 43 |
+
n := subtle.XORBytes(dst, src, x.out[x.outUsed:])
|
| 44 |
+
if !x.decrypt {
|
| 45 |
+
copy(x.next[x.outUsed:], dst)
|
| 46 |
+
}
|
| 47 |
+
dst = dst[n:]
|
| 48 |
+
src = src[n:]
|
| 49 |
+
x.outUsed += n
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
// NewCFBEncrypter returns a [Stream] which encrypts with cipher feedback mode,
|
| 54 |
+
// using the given [Block]. The iv must be the same length as the [Block]'s block
|
| 55 |
+
// size.
|
| 56 |
+
func NewCFBEncrypter(block Block, iv []byte) Stream {
|
| 57 |
+
return newCFB(block, iv, false)
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// NewCFBDecrypter returns a [Stream] which decrypts with cipher feedback mode,
|
| 61 |
+
// using the given [Block]. The iv must be the same length as the [Block]'s block
|
| 62 |
+
// size.
|
| 63 |
+
func NewCFBDecrypter(block Block, iv []byte) Stream {
|
| 64 |
+
return newCFB(block, iv, true)
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
func newCFB(block Block, iv []byte, decrypt bool) Stream {
|
| 68 |
+
blockSize := block.BlockSize()
|
| 69 |
+
if len(iv) != blockSize {
|
| 70 |
+
// stack trace will indicate whether it was de or encryption
|
| 71 |
+
panic("cipher.newCFB: IV length must equal block size")
|
| 72 |
+
}
|
| 73 |
+
x := &cfb{
|
| 74 |
+
b: block,
|
| 75 |
+
out: make([]byte, blockSize),
|
| 76 |
+
next: make([]byte, blockSize),
|
| 77 |
+
outUsed: blockSize,
|
| 78 |
+
decrypt: decrypt,
|
| 79 |
+
}
|
| 80 |
+
copy(x.next, iv)
|
| 81 |
+
|
| 82 |
+
return x
|
| 83 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/cfb_test.go
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2010 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package cipher_test
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"crypto/aes"
|
| 10 |
+
"crypto/cipher"
|
| 11 |
+
"crypto/rand"
|
| 12 |
+
"encoding/hex"
|
| 13 |
+
"testing"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
// cfbTests contains the test vectors from
|
| 17 |
+
// https://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, section
|
| 18 |
+
// F.3.13.
|
| 19 |
+
var cfbTests = []struct {
|
| 20 |
+
key, iv, plaintext, ciphertext string
|
| 21 |
+
}{
|
| 22 |
+
{
|
| 23 |
+
"2b7e151628aed2a6abf7158809cf4f3c",
|
| 24 |
+
"000102030405060708090a0b0c0d0e0f",
|
| 25 |
+
"6bc1bee22e409f96e93d7e117393172a",
|
| 26 |
+
"3b3fd92eb72dad20333449f8e83cfb4a",
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"2b7e151628aed2a6abf7158809cf4f3c",
|
| 30 |
+
"3B3FD92EB72DAD20333449F8E83CFB4A",
|
| 31 |
+
"ae2d8a571e03ac9c9eb76fac45af8e51",
|
| 32 |
+
"c8a64537a0b3a93fcde3cdad9f1ce58b",
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"2b7e151628aed2a6abf7158809cf4f3c",
|
| 36 |
+
"C8A64537A0B3A93FCDE3CDAD9F1CE58B",
|
| 37 |
+
"30c81c46a35ce411e5fbc1191a0a52ef",
|
| 38 |
+
"26751f67a3cbb140b1808cf187a4f4df",
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"2b7e151628aed2a6abf7158809cf4f3c",
|
| 42 |
+
"26751F67A3CBB140B1808CF187A4F4DF",
|
| 43 |
+
"f69f2445df4f9b17ad2b417be66c3710",
|
| 44 |
+
"c04b05357c5d1c0eeac4c66f9ff7f2e6",
|
| 45 |
+
},
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
func TestCFBVectors(t *testing.T) {
|
| 49 |
+
for i, test := range cfbTests {
|
| 50 |
+
key, err := hex.DecodeString(test.key)
|
| 51 |
+
if err != nil {
|
| 52 |
+
t.Fatal(err)
|
| 53 |
+
}
|
| 54 |
+
iv, err := hex.DecodeString(test.iv)
|
| 55 |
+
if err != nil {
|
| 56 |
+
t.Fatal(err)
|
| 57 |
+
}
|
| 58 |
+
plaintext, err := hex.DecodeString(test.plaintext)
|
| 59 |
+
if err != nil {
|
| 60 |
+
t.Fatal(err)
|
| 61 |
+
}
|
| 62 |
+
expected, err := hex.DecodeString(test.ciphertext)
|
| 63 |
+
if err != nil {
|
| 64 |
+
t.Fatal(err)
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
block, err := aes.NewCipher(key)
|
| 68 |
+
if err != nil {
|
| 69 |
+
t.Fatal(err)
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
ciphertext := make([]byte, len(plaintext))
|
| 73 |
+
cfb := cipher.NewCFBEncrypter(block, iv)
|
| 74 |
+
cfb.XORKeyStream(ciphertext, plaintext)
|
| 75 |
+
|
| 76 |
+
if !bytes.Equal(ciphertext, expected) {
|
| 77 |
+
t.Errorf("#%d: wrong output: got %x, expected %x", i, ciphertext, expected)
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
cfbdec := cipher.NewCFBDecrypter(block, iv)
|
| 81 |
+
plaintextCopy := make([]byte, len(ciphertext))
|
| 82 |
+
cfbdec.XORKeyStream(plaintextCopy, ciphertext)
|
| 83 |
+
|
| 84 |
+
if !bytes.Equal(plaintextCopy, plaintext) {
|
| 85 |
+
t.Errorf("#%d: wrong plaintext: got %x, expected %x", i, plaintextCopy, plaintext)
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
func TestCFBInverse(t *testing.T) {
|
| 91 |
+
block, err := aes.NewCipher(commonKey128)
|
| 92 |
+
if err != nil {
|
| 93 |
+
t.Error(err)
|
| 94 |
+
return
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
plaintext := []byte("this is the plaintext. this is the plaintext.")
|
| 98 |
+
iv := make([]byte, block.BlockSize())
|
| 99 |
+
rand.Reader.Read(iv)
|
| 100 |
+
cfb := cipher.NewCFBEncrypter(block, iv)
|
| 101 |
+
ciphertext := make([]byte, len(plaintext))
|
| 102 |
+
copy(ciphertext, plaintext)
|
| 103 |
+
cfb.XORKeyStream(ciphertext, ciphertext)
|
| 104 |
+
|
| 105 |
+
cfbdec := cipher.NewCFBDecrypter(block, iv)
|
| 106 |
+
plaintextCopy := make([]byte, len(plaintext))
|
| 107 |
+
copy(plaintextCopy, ciphertext)
|
| 108 |
+
cfbdec.XORKeyStream(plaintextCopy, plaintextCopy)
|
| 109 |
+
|
| 110 |
+
if !bytes.Equal(plaintextCopy, plaintext) {
|
| 111 |
+
t.Errorf("got: %x, want: %x", plaintextCopy, plaintext)
|
| 112 |
+
}
|
| 113 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/cipher.go
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2010 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// Package cipher implements standard block cipher modes that can be wrapped
|
| 6 |
+
// around low-level block cipher implementations.
|
| 7 |
+
// See https://csrc.nist.gov/groups/ST/toolkit/BCM/current_modes.html
|
| 8 |
+
// and NIST Special Publication 800-38A.
|
| 9 |
+
package cipher
|
| 10 |
+
|
| 11 |
+
// A Block represents an implementation of block cipher
|
| 12 |
+
// using a given key. It provides the capability to encrypt
|
| 13 |
+
// or decrypt individual blocks. The mode implementations
|
| 14 |
+
// extend that capability to streams of blocks.
|
| 15 |
+
type Block interface {
|
| 16 |
+
// BlockSize returns the cipher's block size.
|
| 17 |
+
BlockSize() int
|
| 18 |
+
|
| 19 |
+
// Encrypt encrypts the first block in src into dst.
|
| 20 |
+
// Dst and src must overlap entirely or not at all.
|
| 21 |
+
Encrypt(dst, src []byte)
|
| 22 |
+
|
| 23 |
+
// Decrypt decrypts the first block in src into dst.
|
| 24 |
+
// Dst and src must overlap entirely or not at all.
|
| 25 |
+
Decrypt(dst, src []byte)
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// A Stream represents a stream cipher.
|
| 29 |
+
type Stream interface {
|
| 30 |
+
// XORKeyStream XORs each byte in the given slice with a byte from the
|
| 31 |
+
// cipher's key stream. Dst and src must overlap entirely or not at all.
|
| 32 |
+
//
|
| 33 |
+
// If len(dst) < len(src), XORKeyStream should panic. It is acceptable
|
| 34 |
+
// to pass a dst bigger than src, and in that case, XORKeyStream will
|
| 35 |
+
// only update dst[:len(src)] and will not touch the rest of dst.
|
| 36 |
+
//
|
| 37 |
+
// Multiple calls to XORKeyStream behave as if the concatenation of
|
| 38 |
+
// the src buffers was passed in a single run. That is, Stream
|
| 39 |
+
// maintains state and does not reset at each XORKeyStream call.
|
| 40 |
+
XORKeyStream(dst, src []byte)
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// A BlockMode represents a block cipher running in a block-based mode (CBC,
|
| 44 |
+
// ECB etc).
|
| 45 |
+
type BlockMode interface {
|
| 46 |
+
// BlockSize returns the mode's block size.
|
| 47 |
+
BlockSize() int
|
| 48 |
+
|
| 49 |
+
// CryptBlocks encrypts or decrypts a number of blocks. The length of
|
| 50 |
+
// src must be a multiple of the block size. Dst and src must overlap
|
| 51 |
+
// entirely or not at all.
|
| 52 |
+
//
|
| 53 |
+
// If len(dst) < len(src), CryptBlocks should panic. It is acceptable
|
| 54 |
+
// to pass a dst bigger than src, and in that case, CryptBlocks will
|
| 55 |
+
// only update dst[:len(src)] and will not touch the rest of dst.
|
| 56 |
+
//
|
| 57 |
+
// Multiple calls to CryptBlocks behave as if the concatenation of
|
| 58 |
+
// the src buffers was passed in a single run. That is, BlockMode
|
| 59 |
+
// maintains state and does not reset at each CryptBlocks call.
|
| 60 |
+
CryptBlocks(dst, src []byte)
|
| 61 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/cipher_test.go
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2013 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package cipher_test
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"crypto/aes"
|
| 10 |
+
"crypto/cipher"
|
| 11 |
+
"crypto/des"
|
| 12 |
+
"testing"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
func TestCryptBlocks(t *testing.T) {
|
| 16 |
+
buf := make([]byte, 16)
|
| 17 |
+
block, _ := aes.NewCipher(buf)
|
| 18 |
+
|
| 19 |
+
mode := cipher.NewCBCDecrypter(block, buf)
|
| 20 |
+
mustPanic(t, "crypto/cipher: input not full blocks", func() { mode.CryptBlocks(buf, buf[:3]) })
|
| 21 |
+
mustPanic(t, "crypto/cipher: output smaller than input", func() { mode.CryptBlocks(buf[:3], buf) })
|
| 22 |
+
|
| 23 |
+
mode = cipher.NewCBCEncrypter(block, buf)
|
| 24 |
+
mustPanic(t, "crypto/cipher: input not full blocks", func() { mode.CryptBlocks(buf, buf[:3]) })
|
| 25 |
+
mustPanic(t, "crypto/cipher: output smaller than input", func() { mode.CryptBlocks(buf[:3], buf) })
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
func mustPanic(t *testing.T, msg string, f func()) {
|
| 29 |
+
defer func() {
|
| 30 |
+
err := recover()
|
| 31 |
+
if err == nil {
|
| 32 |
+
t.Errorf("function did not panic, wanted %q", msg)
|
| 33 |
+
} else if err != msg {
|
| 34 |
+
t.Errorf("got panic %v, wanted %q", err, msg)
|
| 35 |
+
}
|
| 36 |
+
}()
|
| 37 |
+
f()
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
func TestEmptyPlaintext(t *testing.T) {
|
| 41 |
+
var key [16]byte
|
| 42 |
+
a, err := aes.NewCipher(key[:16])
|
| 43 |
+
if err != nil {
|
| 44 |
+
t.Fatal(err)
|
| 45 |
+
}
|
| 46 |
+
d, err := des.NewCipher(key[:8])
|
| 47 |
+
if err != nil {
|
| 48 |
+
t.Fatal(err)
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
s := 16
|
| 52 |
+
pt := make([]byte, s)
|
| 53 |
+
ct := make([]byte, s)
|
| 54 |
+
for i := 0; i < 16; i++ {
|
| 55 |
+
pt[i], ct[i] = byte(i), byte(i)
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
assertEqual := func(name string, got, want []byte) {
|
| 59 |
+
if !bytes.Equal(got, want) {
|
| 60 |
+
t.Fatalf("%s: got %v, want %v", name, got, want)
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
for _, b := range []cipher.Block{a, d} {
|
| 65 |
+
iv := make([]byte, b.BlockSize())
|
| 66 |
+
cbce := cipher.NewCBCEncrypter(b, iv)
|
| 67 |
+
cbce.CryptBlocks(ct, pt[:0])
|
| 68 |
+
assertEqual("CBC encrypt", ct, pt)
|
| 69 |
+
|
| 70 |
+
cbcd := cipher.NewCBCDecrypter(b, iv)
|
| 71 |
+
cbcd.CryptBlocks(ct, pt[:0])
|
| 72 |
+
assertEqual("CBC decrypt", ct, pt)
|
| 73 |
+
|
| 74 |
+
cfbe := cipher.NewCFBEncrypter(b, iv)
|
| 75 |
+
cfbe.XORKeyStream(ct, pt[:0])
|
| 76 |
+
assertEqual("CFB encrypt", ct, pt)
|
| 77 |
+
|
| 78 |
+
cfbd := cipher.NewCFBDecrypter(b, iv)
|
| 79 |
+
cfbd.XORKeyStream(ct, pt[:0])
|
| 80 |
+
assertEqual("CFB decrypt", ct, pt)
|
| 81 |
+
|
| 82 |
+
ctr := cipher.NewCTR(b, iv)
|
| 83 |
+
ctr.XORKeyStream(ct, pt[:0])
|
| 84 |
+
assertEqual("CTR", ct, pt)
|
| 85 |
+
|
| 86 |
+
ofb := cipher.NewOFB(b, iv)
|
| 87 |
+
ofb.XORKeyStream(ct, pt[:0])
|
| 88 |
+
assertEqual("OFB", ct, pt)
|
| 89 |
+
}
|
| 90 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/common_test.go
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package cipher_test
|
| 6 |
+
|
| 7 |
+
// Common values for tests.
|
| 8 |
+
|
| 9 |
+
var commonInput = []byte{
|
| 10 |
+
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
|
| 11 |
+
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
|
| 12 |
+
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
|
| 13 |
+
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10,
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
var commonKey128 = []byte{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}
|
| 17 |
+
|
| 18 |
+
var commonKey192 = []byte{
|
| 19 |
+
0x8e, 0x73, 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// Counter (CTR) mode.
|
| 6 |
+
|
| 7 |
+
// CTR converts a block cipher into a stream cipher by
|
| 8 |
+
// repeatedly encrypting an incrementing counter and
|
| 9 |
+
// xoring the resulting stream of data with the input.
|
| 10 |
+
|
| 11 |
+
// See NIST SP 800-38A, pp 13-15
|
| 12 |
+
|
| 13 |
+
package cipher
|
| 14 |
+
|
| 15 |
+
import (
|
| 16 |
+
"bytes"
|
| 17 |
+
"crypto/internal/alias"
|
| 18 |
+
"crypto/subtle"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
type ctr struct {
|
| 22 |
+
b Block
|
| 23 |
+
ctr []byte
|
| 24 |
+
out []byte
|
| 25 |
+
outUsed int
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
const streamBufferSize = 512
|
| 29 |
+
|
| 30 |
+
// ctrAble is an interface implemented by ciphers that have a specific optimized
|
| 31 |
+
// implementation of CTR, like crypto/aes. NewCTR will check for this interface
|
| 32 |
+
// and return the specific Stream if found.
|
| 33 |
+
type ctrAble interface {
|
| 34 |
+
NewCTR(iv []byte) Stream
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
// NewCTR returns a [Stream] which encrypts/decrypts using the given [Block] in
|
| 38 |
+
// counter mode. The length of iv must be the same as the [Block]'s block size.
|
| 39 |
+
func NewCTR(block Block, iv []byte) Stream {
|
| 40 |
+
if ctr, ok := block.(ctrAble); ok {
|
| 41 |
+
return ctr.NewCTR(iv)
|
| 42 |
+
}
|
| 43 |
+
if len(iv) != block.BlockSize() {
|
| 44 |
+
panic("cipher.NewCTR: IV length must equal block size")
|
| 45 |
+
}
|
| 46 |
+
bufSize := streamBufferSize
|
| 47 |
+
if bufSize < block.BlockSize() {
|
| 48 |
+
bufSize = block.BlockSize()
|
| 49 |
+
}
|
| 50 |
+
return &ctr{
|
| 51 |
+
b: block,
|
| 52 |
+
ctr: bytes.Clone(iv),
|
| 53 |
+
out: make([]byte, 0, bufSize),
|
| 54 |
+
outUsed: 0,
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
func (x *ctr) refill() {
|
| 59 |
+
remain := len(x.out) - x.outUsed
|
| 60 |
+
copy(x.out, x.out[x.outUsed:])
|
| 61 |
+
x.out = x.out[:cap(x.out)]
|
| 62 |
+
bs := x.b.BlockSize()
|
| 63 |
+
for remain <= len(x.out)-bs {
|
| 64 |
+
x.b.Encrypt(x.out[remain:], x.ctr)
|
| 65 |
+
remain += bs
|
| 66 |
+
|
| 67 |
+
// Increment counter
|
| 68 |
+
for i := len(x.ctr) - 1; i >= 0; i-- {
|
| 69 |
+
x.ctr[i]++
|
| 70 |
+
if x.ctr[i] != 0 {
|
| 71 |
+
break
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
x.out = x.out[:remain]
|
| 76 |
+
x.outUsed = 0
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
func (x *ctr) XORKeyStream(dst, src []byte) {
|
| 80 |
+
if len(dst) < len(src) {
|
| 81 |
+
panic("crypto/cipher: output smaller than input")
|
| 82 |
+
}
|
| 83 |
+
if alias.InexactOverlap(dst[:len(src)], src) {
|
| 84 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 85 |
+
}
|
| 86 |
+
for len(src) > 0 {
|
| 87 |
+
if x.outUsed >= len(x.out)-x.b.BlockSize() {
|
| 88 |
+
x.refill()
|
| 89 |
+
}
|
| 90 |
+
n := subtle.XORBytes(dst, src, x.out[x.outUsed:])
|
| 91 |
+
dst = dst[n:]
|
| 92 |
+
src = src[n:]
|
| 93 |
+
x.outUsed += n
|
| 94 |
+
}
|
| 95 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/ctr_aes_test.go
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// CTR AES test vectors.
|
| 6 |
+
|
| 7 |
+
// See U.S. National Institute of Standards and Technology (NIST)
|
| 8 |
+
// Special Publication 800-38A, ``Recommendation for Block Cipher
|
| 9 |
+
// Modes of Operation,'' 2001 Edition, pp. 55-58.
|
| 10 |
+
|
| 11 |
+
package cipher_test
|
| 12 |
+
|
| 13 |
+
import (
|
| 14 |
+
"bytes"
|
| 15 |
+
"crypto/aes"
|
| 16 |
+
"crypto/cipher"
|
| 17 |
+
"testing"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
var commonCounter = []byte{0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}
|
| 21 |
+
|
| 22 |
+
var ctrAESTests = []struct {
|
| 23 |
+
name string
|
| 24 |
+
key []byte
|
| 25 |
+
iv []byte
|
| 26 |
+
in []byte
|
| 27 |
+
out []byte
|
| 28 |
+
}{
|
| 29 |
+
// NIST SP 800-38A pp 55-58
|
| 30 |
+
{
|
| 31 |
+
"CTR-AES128",
|
| 32 |
+
commonKey128,
|
| 33 |
+
commonCounter,
|
| 34 |
+
commonInput,
|
| 35 |
+
[]byte{
|
| 36 |
+
0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce,
|
| 37 |
+
0x98, 0x06, 0xf6, 0x6b, 0x79, 0x70, 0xfd, 0xff, 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff,
|
| 38 |
+
0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e, 0x5b, 0x4f, 0x09, 0x02, 0x0d, 0xb0, 0x3e, 0xab,
|
| 39 |
+
0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1, 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee,
|
| 40 |
+
},
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"CTR-AES192",
|
| 44 |
+
commonKey192,
|
| 45 |
+
commonCounter,
|
| 46 |
+
commonInput,
|
| 47 |
+
[]byte{
|
| 48 |
+
0x1a, 0xbc, 0x93, 0x24, 0x17, 0x52, 0x1c, 0xa2, 0x4f, 0x2b, 0x04, 0x59, 0xfe, 0x7e, 0x6e, 0x0b,
|
| 49 |
+
0x09, 0x03, 0x39, 0xec, 0x0a, 0xa6, 0xfa, 0xef, 0xd5, 0xcc, 0xc2, 0xc6, 0xf4, 0xce, 0x8e, 0x94,
|
| 50 |
+
0x1e, 0x36, 0xb2, 0x6b, 0xd1, 0xeb, 0xc6, 0x70, 0xd1, 0xbd, 0x1d, 0x66, 0x56, 0x20, 0xab, 0xf7,
|
| 51 |
+
0x4f, 0x78, 0xa7, 0xf6, 0xd2, 0x98, 0x09, 0x58, 0x5a, 0x97, 0xda, 0xec, 0x58, 0xc6, 0xb0, 0x50,
|
| 52 |
+
},
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"CTR-AES256",
|
| 56 |
+
commonKey256,
|
| 57 |
+
commonCounter,
|
| 58 |
+
commonInput,
|
| 59 |
+
[]byte{
|
| 60 |
+
0x60, 0x1e, 0xc3, 0x13, 0x77, 0x57, 0x89, 0xa5, 0xb7, 0xa7, 0xf5, 0x04, 0xbb, 0xf3, 0xd2, 0x28,
|
| 61 |
+
0xf4, 0x43, 0xe3, 0xca, 0x4d, 0x62, 0xb5, 0x9a, 0xca, 0x84, 0xe9, 0x90, 0xca, 0xca, 0xf5, 0xc5,
|
| 62 |
+
0x2b, 0x09, 0x30, 0xda, 0xa2, 0x3d, 0xe9, 0x4c, 0xe8, 0x70, 0x17, 0xba, 0x2d, 0x84, 0x98, 0x8d,
|
| 63 |
+
0xdf, 0xc9, 0xc5, 0x8d, 0xb6, 0x7a, 0xad, 0xa6, 0x13, 0xc2, 0xdd, 0x08, 0x45, 0x79, 0x41, 0xa6,
|
| 64 |
+
},
|
| 65 |
+
},
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
func TestCTR_AES(t *testing.T) {
|
| 69 |
+
for _, tt := range ctrAESTests {
|
| 70 |
+
test := tt.name
|
| 71 |
+
|
| 72 |
+
c, err := aes.NewCipher(tt.key)
|
| 73 |
+
if err != nil {
|
| 74 |
+
t.Errorf("%s: NewCipher(%d bytes) = %s", test, len(tt.key), err)
|
| 75 |
+
continue
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
for j := 0; j <= 5; j += 5 {
|
| 79 |
+
in := tt.in[0 : len(tt.in)-j]
|
| 80 |
+
ctr := cipher.NewCTR(c, tt.iv)
|
| 81 |
+
encrypted := make([]byte, len(in))
|
| 82 |
+
ctr.XORKeyStream(encrypted, in)
|
| 83 |
+
if out := tt.out[0:len(in)]; !bytes.Equal(out, encrypted) {
|
| 84 |
+
t.Errorf("%s/%d: CTR\ninpt %x\nhave %x\nwant %x", test, len(in), in, encrypted, out)
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
for j := 0; j <= 7; j += 7 {
|
| 89 |
+
in := tt.out[0 : len(tt.out)-j]
|
| 90 |
+
ctr := cipher.NewCTR(c, tt.iv)
|
| 91 |
+
plain := make([]byte, len(in))
|
| 92 |
+
ctr.XORKeyStream(plain, in)
|
| 93 |
+
if out := tt.in[0:len(in)]; !bytes.Equal(out, plain) {
|
| 94 |
+
t.Errorf("%s/%d: CTRReader\nhave %x\nwant %x", test, len(out), plain, out)
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if t.Failed() {
|
| 99 |
+
break
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/ctr_test.go
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2015 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package cipher_test
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"crypto/cipher"
|
| 10 |
+
"testing"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
type noopBlock int
|
| 14 |
+
|
| 15 |
+
func (b noopBlock) BlockSize() int { return int(b) }
|
| 16 |
+
func (noopBlock) Encrypt(dst, src []byte) { copy(dst, src) }
|
| 17 |
+
func (noopBlock) Decrypt(dst, src []byte) { copy(dst, src) }
|
| 18 |
+
|
| 19 |
+
func inc(b []byte) {
|
| 20 |
+
for i := len(b) - 1; i >= 0; i++ {
|
| 21 |
+
b[i]++
|
| 22 |
+
if b[i] != 0 {
|
| 23 |
+
break
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
func xor(a, b []byte) {
|
| 29 |
+
for i := range a {
|
| 30 |
+
a[i] ^= b[i]
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
func TestCTR(t *testing.T) {
|
| 35 |
+
for size := 64; size <= 1024; size *= 2 {
|
| 36 |
+
iv := make([]byte, size)
|
| 37 |
+
ctr := cipher.NewCTR(noopBlock(size), iv)
|
| 38 |
+
src := make([]byte, 1024)
|
| 39 |
+
for i := range src {
|
| 40 |
+
src[i] = 0xff
|
| 41 |
+
}
|
| 42 |
+
want := make([]byte, 1024)
|
| 43 |
+
copy(want, src)
|
| 44 |
+
counter := make([]byte, size)
|
| 45 |
+
for i := 1; i < len(want)/size; i++ {
|
| 46 |
+
inc(counter)
|
| 47 |
+
xor(want[i*size:(i+1)*size], counter)
|
| 48 |
+
}
|
| 49 |
+
dst := make([]byte, 1024)
|
| 50 |
+
ctr.XORKeyStream(dst, src)
|
| 51 |
+
if !bytes.Equal(dst, want) {
|
| 52 |
+
t.Errorf("for size %d\nhave %x\nwant %x", size, dst, want)
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/example_test.go
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2012 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package cipher_test
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"crypto/aes"
|
| 10 |
+
"crypto/cipher"
|
| 11 |
+
"crypto/rand"
|
| 12 |
+
"encoding/hex"
|
| 13 |
+
"fmt"
|
| 14 |
+
"io"
|
| 15 |
+
"os"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
func ExampleNewGCM_encrypt() {
|
| 19 |
+
// Load your secret key from a safe place and reuse it across multiple
|
| 20 |
+
// Seal/Open calls. (Obviously don't use this example key for anything
|
| 21 |
+
// real.) If you want to convert a passphrase to a key, use a suitable
|
| 22 |
+
// package like bcrypt or scrypt.
|
| 23 |
+
// When decoded the key should be 16 bytes (AES-128) or 32 (AES-256).
|
| 24 |
+
key, _ := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
|
| 25 |
+
plaintext := []byte("exampleplaintext")
|
| 26 |
+
|
| 27 |
+
block, err := aes.NewCipher(key)
|
| 28 |
+
if err != nil {
|
| 29 |
+
panic(err.Error())
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
// Never use more than 2^32 random nonces with a given key because of the risk of a repeat.
|
| 33 |
+
nonce := make([]byte, 12)
|
| 34 |
+
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
| 35 |
+
panic(err.Error())
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
aesgcm, err := cipher.NewGCM(block)
|
| 39 |
+
if err != nil {
|
| 40 |
+
panic(err.Error())
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)
|
| 44 |
+
fmt.Printf("%x\n", ciphertext)
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
func ExampleNewGCM_decrypt() {
|
| 48 |
+
// Load your secret key from a safe place and reuse it across multiple
|
| 49 |
+
// Seal/Open calls. (Obviously don't use this example key for anything
|
| 50 |
+
// real.) If you want to convert a passphrase to a key, use a suitable
|
| 51 |
+
// package like bcrypt or scrypt.
|
| 52 |
+
// When decoded the key should be 16 bytes (AES-128) or 32 (AES-256).
|
| 53 |
+
key, _ := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
|
| 54 |
+
ciphertext, _ := hex.DecodeString("c3aaa29f002ca75870806e44086700f62ce4d43e902b3888e23ceff797a7a471")
|
| 55 |
+
nonce, _ := hex.DecodeString("64a9433eae7ccceee2fc0eda")
|
| 56 |
+
|
| 57 |
+
block, err := aes.NewCipher(key)
|
| 58 |
+
if err != nil {
|
| 59 |
+
panic(err.Error())
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
aesgcm, err := cipher.NewGCM(block)
|
| 63 |
+
if err != nil {
|
| 64 |
+
panic(err.Error())
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil)
|
| 68 |
+
if err != nil {
|
| 69 |
+
panic(err.Error())
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
fmt.Printf("%s\n", plaintext)
|
| 73 |
+
// Output: exampleplaintext
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
func ExampleNewCBCDecrypter() {
|
| 77 |
+
// Load your secret key from a safe place and reuse it across multiple
|
| 78 |
+
// NewCipher calls. (Obviously don't use this example key for anything
|
| 79 |
+
// real.) If you want to convert a passphrase to a key, use a suitable
|
| 80 |
+
// package like bcrypt or scrypt.
|
| 81 |
+
key, _ := hex.DecodeString("6368616e676520746869732070617373")
|
| 82 |
+
ciphertext, _ := hex.DecodeString("73c86d43a9d700a253a96c85b0f6b03ac9792e0e757f869cca306bd3cba1c62b")
|
| 83 |
+
|
| 84 |
+
block, err := aes.NewCipher(key)
|
| 85 |
+
if err != nil {
|
| 86 |
+
panic(err)
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
// The IV needs to be unique, but not secure. Therefore it's common to
|
| 90 |
+
// include it at the beginning of the ciphertext.
|
| 91 |
+
if len(ciphertext) < aes.BlockSize {
|
| 92 |
+
panic("ciphertext too short")
|
| 93 |
+
}
|
| 94 |
+
iv := ciphertext[:aes.BlockSize]
|
| 95 |
+
ciphertext = ciphertext[aes.BlockSize:]
|
| 96 |
+
|
| 97 |
+
// CBC mode always works in whole blocks.
|
| 98 |
+
if len(ciphertext)%aes.BlockSize != 0 {
|
| 99 |
+
panic("ciphertext is not a multiple of the block size")
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
mode := cipher.NewCBCDecrypter(block, iv)
|
| 103 |
+
|
| 104 |
+
// CryptBlocks can work in-place if the two arguments are the same.
|
| 105 |
+
mode.CryptBlocks(ciphertext, ciphertext)
|
| 106 |
+
|
| 107 |
+
// If the original plaintext lengths are not a multiple of the block
|
| 108 |
+
// size, padding would have to be added when encrypting, which would be
|
| 109 |
+
// removed at this point. For an example, see
|
| 110 |
+
// https://tools.ietf.org/html/rfc5246#section-6.2.3.2. However, it's
|
| 111 |
+
// critical to note that ciphertexts must be authenticated (i.e. by
|
| 112 |
+
// using crypto/hmac) before being decrypted in order to avoid creating
|
| 113 |
+
// a padding oracle.
|
| 114 |
+
|
| 115 |
+
fmt.Printf("%s\n", ciphertext)
|
| 116 |
+
// Output: exampleplaintext
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
func ExampleNewCBCEncrypter() {
|
| 120 |
+
// Load your secret key from a safe place and reuse it across multiple
|
| 121 |
+
// NewCipher calls. (Obviously don't use this example key for anything
|
| 122 |
+
// real.) If you want to convert a passphrase to a key, use a suitable
|
| 123 |
+
// package like bcrypt or scrypt.
|
| 124 |
+
key, _ := hex.DecodeString("6368616e676520746869732070617373")
|
| 125 |
+
plaintext := []byte("exampleplaintext")
|
| 126 |
+
|
| 127 |
+
// CBC mode works on blocks so plaintexts may need to be padded to the
|
| 128 |
+
// next whole block. For an example of such padding, see
|
| 129 |
+
// https://tools.ietf.org/html/rfc5246#section-6.2.3.2. Here we'll
|
| 130 |
+
// assume that the plaintext is already of the correct length.
|
| 131 |
+
if len(plaintext)%aes.BlockSize != 0 {
|
| 132 |
+
panic("plaintext is not a multiple of the block size")
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
block, err := aes.NewCipher(key)
|
| 136 |
+
if err != nil {
|
| 137 |
+
panic(err)
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// The IV needs to be unique, but not secure. Therefore it's common to
|
| 141 |
+
// include it at the beginning of the ciphertext.
|
| 142 |
+
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
| 143 |
+
iv := ciphertext[:aes.BlockSize]
|
| 144 |
+
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
| 145 |
+
panic(err)
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
mode := cipher.NewCBCEncrypter(block, iv)
|
| 149 |
+
mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
|
| 150 |
+
|
| 151 |
+
// It's important to remember that ciphertexts must be authenticated
|
| 152 |
+
// (i.e. by using crypto/hmac) as well as being encrypted in order to
|
| 153 |
+
// be secure.
|
| 154 |
+
|
| 155 |
+
fmt.Printf("%x\n", ciphertext)
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
func ExampleNewCFBDecrypter() {
|
| 159 |
+
// Load your secret key from a safe place and reuse it across multiple
|
| 160 |
+
// NewCipher calls. (Obviously don't use this example key for anything
|
| 161 |
+
// real.) If you want to convert a passphrase to a key, use a suitable
|
| 162 |
+
// package like bcrypt or scrypt.
|
| 163 |
+
key, _ := hex.DecodeString("6368616e676520746869732070617373")
|
| 164 |
+
ciphertext, _ := hex.DecodeString("7dd015f06bec7f1b8f6559dad89f4131da62261786845100056b353194ad")
|
| 165 |
+
|
| 166 |
+
block, err := aes.NewCipher(key)
|
| 167 |
+
if err != nil {
|
| 168 |
+
panic(err)
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
// The IV needs to be unique, but not secure. Therefore it's common to
|
| 172 |
+
// include it at the beginning of the ciphertext.
|
| 173 |
+
if len(ciphertext) < aes.BlockSize {
|
| 174 |
+
panic("ciphertext too short")
|
| 175 |
+
}
|
| 176 |
+
iv := ciphertext[:aes.BlockSize]
|
| 177 |
+
ciphertext = ciphertext[aes.BlockSize:]
|
| 178 |
+
|
| 179 |
+
stream := cipher.NewCFBDecrypter(block, iv)
|
| 180 |
+
|
| 181 |
+
// XORKeyStream can work in-place if the two arguments are the same.
|
| 182 |
+
stream.XORKeyStream(ciphertext, ciphertext)
|
| 183 |
+
fmt.Printf("%s", ciphertext)
|
| 184 |
+
// Output: some plaintext
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
func ExampleNewCFBEncrypter() {
|
| 188 |
+
// Load your secret key from a safe place and reuse it across multiple
|
| 189 |
+
// NewCipher calls. (Obviously don't use this example key for anything
|
| 190 |
+
// real.) If you want to convert a passphrase to a key, use a suitable
|
| 191 |
+
// package like bcrypt or scrypt.
|
| 192 |
+
key, _ := hex.DecodeString("6368616e676520746869732070617373")
|
| 193 |
+
plaintext := []byte("some plaintext")
|
| 194 |
+
|
| 195 |
+
block, err := aes.NewCipher(key)
|
| 196 |
+
if err != nil {
|
| 197 |
+
panic(err)
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
// The IV needs to be unique, but not secure. Therefore it's common to
|
| 201 |
+
// include it at the beginning of the ciphertext.
|
| 202 |
+
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
| 203 |
+
iv := ciphertext[:aes.BlockSize]
|
| 204 |
+
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
| 205 |
+
panic(err)
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
stream := cipher.NewCFBEncrypter(block, iv)
|
| 209 |
+
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
|
| 210 |
+
|
| 211 |
+
// It's important to remember that ciphertexts must be authenticated
|
| 212 |
+
// (i.e. by using crypto/hmac) as well as being encrypted in order to
|
| 213 |
+
// be secure.
|
| 214 |
+
fmt.Printf("%x\n", ciphertext)
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
func ExampleNewCTR() {
|
| 218 |
+
// Load your secret key from a safe place and reuse it across multiple
|
| 219 |
+
// NewCipher calls. (Obviously don't use this example key for anything
|
| 220 |
+
// real.) If you want to convert a passphrase to a key, use a suitable
|
| 221 |
+
// package like bcrypt or scrypt.
|
| 222 |
+
key, _ := hex.DecodeString("6368616e676520746869732070617373")
|
| 223 |
+
plaintext := []byte("some plaintext")
|
| 224 |
+
|
| 225 |
+
block, err := aes.NewCipher(key)
|
| 226 |
+
if err != nil {
|
| 227 |
+
panic(err)
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
// The IV needs to be unique, but not secure. Therefore it's common to
|
| 231 |
+
// include it at the beginning of the ciphertext.
|
| 232 |
+
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
| 233 |
+
iv := ciphertext[:aes.BlockSize]
|
| 234 |
+
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
| 235 |
+
panic(err)
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
stream := cipher.NewCTR(block, iv)
|
| 239 |
+
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
|
| 240 |
+
|
| 241 |
+
// It's important to remember that ciphertexts must be authenticated
|
| 242 |
+
// (i.e. by using crypto/hmac) as well as being encrypted in order to
|
| 243 |
+
// be secure.
|
| 244 |
+
|
| 245 |
+
// CTR mode is the same for both encryption and decryption, so we can
|
| 246 |
+
// also decrypt that ciphertext with NewCTR.
|
| 247 |
+
|
| 248 |
+
plaintext2 := make([]byte, len(plaintext))
|
| 249 |
+
stream = cipher.NewCTR(block, iv)
|
| 250 |
+
stream.XORKeyStream(plaintext2, ciphertext[aes.BlockSize:])
|
| 251 |
+
|
| 252 |
+
fmt.Printf("%s\n", plaintext2)
|
| 253 |
+
// Output: some plaintext
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
func ExampleNewOFB() {
|
| 257 |
+
// Load your secret key from a safe place and reuse it across multiple
|
| 258 |
+
// NewCipher calls. (Obviously don't use this example key for anything
|
| 259 |
+
// real.) If you want to convert a passphrase to a key, use a suitable
|
| 260 |
+
// package like bcrypt or scrypt.
|
| 261 |
+
key, _ := hex.DecodeString("6368616e676520746869732070617373")
|
| 262 |
+
plaintext := []byte("some plaintext")
|
| 263 |
+
|
| 264 |
+
block, err := aes.NewCipher(key)
|
| 265 |
+
if err != nil {
|
| 266 |
+
panic(err)
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
// The IV needs to be unique, but not secure. Therefore it's common to
|
| 270 |
+
// include it at the beginning of the ciphertext.
|
| 271 |
+
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
| 272 |
+
iv := ciphertext[:aes.BlockSize]
|
| 273 |
+
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
| 274 |
+
panic(err)
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
stream := cipher.NewOFB(block, iv)
|
| 278 |
+
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
|
| 279 |
+
|
| 280 |
+
// It's important to remember that ciphertexts must be authenticated
|
| 281 |
+
// (i.e. by using crypto/hmac) as well as being encrypted in order to
|
| 282 |
+
// be secure.
|
| 283 |
+
|
| 284 |
+
// OFB mode is the same for both encryption and decryption, so we can
|
| 285 |
+
// also decrypt that ciphertext with NewOFB.
|
| 286 |
+
|
| 287 |
+
plaintext2 := make([]byte, len(plaintext))
|
| 288 |
+
stream = cipher.NewOFB(block, iv)
|
| 289 |
+
stream.XORKeyStream(plaintext2, ciphertext[aes.BlockSize:])
|
| 290 |
+
|
| 291 |
+
fmt.Printf("%s\n", plaintext2)
|
| 292 |
+
// Output: some plaintext
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
func ExampleStreamReader() {
|
| 296 |
+
// Load your secret key from a safe place and reuse it across multiple
|
| 297 |
+
// NewCipher calls. (Obviously don't use this example key for anything
|
| 298 |
+
// real.) If you want to convert a passphrase to a key, use a suitable
|
| 299 |
+
// package like bcrypt or scrypt.
|
| 300 |
+
key, _ := hex.DecodeString("6368616e676520746869732070617373")
|
| 301 |
+
|
| 302 |
+
encrypted, _ := hex.DecodeString("cf0495cc6f75dafc23948538e79904a9")
|
| 303 |
+
bReader := bytes.NewReader(encrypted)
|
| 304 |
+
|
| 305 |
+
block, err := aes.NewCipher(key)
|
| 306 |
+
if err != nil {
|
| 307 |
+
panic(err)
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
// If the key is unique for each ciphertext, then it's ok to use a zero
|
| 311 |
+
// IV.
|
| 312 |
+
var iv [aes.BlockSize]byte
|
| 313 |
+
stream := cipher.NewOFB(block, iv[:])
|
| 314 |
+
|
| 315 |
+
reader := &cipher.StreamReader{S: stream, R: bReader}
|
| 316 |
+
// Copy the input to the output stream, decrypting as we go.
|
| 317 |
+
if _, err := io.Copy(os.Stdout, reader); err != nil {
|
| 318 |
+
panic(err)
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
// Note that this example is simplistic in that it omits any
|
| 322 |
+
// authentication of the encrypted data. If you were actually to use
|
| 323 |
+
// StreamReader in this manner, an attacker could flip arbitrary bits in
|
| 324 |
+
// the output.
|
| 325 |
+
|
| 326 |
+
// Output: some secret text
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
func ExampleStreamWriter() {
|
| 330 |
+
// Load your secret key from a safe place and reuse it across multiple
|
| 331 |
+
// NewCipher calls. (Obviously don't use this example key for anything
|
| 332 |
+
// real.) If you want to convert a passphrase to a key, use a suitable
|
| 333 |
+
// package like bcrypt or scrypt.
|
| 334 |
+
key, _ := hex.DecodeString("6368616e676520746869732070617373")
|
| 335 |
+
|
| 336 |
+
bReader := bytes.NewReader([]byte("some secret text"))
|
| 337 |
+
|
| 338 |
+
block, err := aes.NewCipher(key)
|
| 339 |
+
if err != nil {
|
| 340 |
+
panic(err)
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
// If the key is unique for each ciphertext, then it's ok to use a zero
|
| 344 |
+
// IV.
|
| 345 |
+
var iv [aes.BlockSize]byte
|
| 346 |
+
stream := cipher.NewOFB(block, iv[:])
|
| 347 |
+
|
| 348 |
+
var out bytes.Buffer
|
| 349 |
+
|
| 350 |
+
writer := &cipher.StreamWriter{S: stream, W: &out}
|
| 351 |
+
// Copy the input to the output buffer, encrypting as we go.
|
| 352 |
+
if _, err := io.Copy(writer, bReader); err != nil {
|
| 353 |
+
panic(err)
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
// Note that this example is simplistic in that it omits any
|
| 357 |
+
// authentication of the encrypted data. If you were actually to use
|
| 358 |
+
// StreamReader in this manner, an attacker could flip arbitrary bits in
|
| 359 |
+
// the decrypted result.
|
| 360 |
+
|
| 361 |
+
fmt.Printf("%x\n", out.Bytes())
|
| 362 |
+
// Output: cf0495cc6f75dafc23948538e79904a9
|
| 363 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/export_test.go
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2018 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package cipher
|
| 6 |
+
|
| 7 |
+
// Export internal functions for testing.
|
| 8 |
+
var NewCBCGenericEncrypter = newCBCGenericEncrypter
|
| 9 |
+
var NewCBCGenericDecrypter = newCBCGenericDecrypter
|
platform/dbops/binaries/go/go/src/crypto/cipher/fuzz_test.go
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2021 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
//go:build ppc64le
|
| 6 |
+
|
| 7 |
+
package cipher_test
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"bytes"
|
| 11 |
+
"crypto/aes"
|
| 12 |
+
"crypto/cipher"
|
| 13 |
+
"crypto/rand"
|
| 14 |
+
"testing"
|
| 15 |
+
"time"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
var cbcAESFuzzTests = []struct {
|
| 19 |
+
name string
|
| 20 |
+
key []byte
|
| 21 |
+
}{
|
| 22 |
+
{
|
| 23 |
+
"CBC-AES128",
|
| 24 |
+
commonKey128,
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"CBC-AES192",
|
| 28 |
+
commonKey192,
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"CBC-AES256",
|
| 32 |
+
commonKey256,
|
| 33 |
+
},
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
var timeout *time.Timer
|
| 37 |
+
|
| 38 |
+
const datalen = 1024
|
| 39 |
+
|
| 40 |
+
func TestFuzz(t *testing.T) {
|
| 41 |
+
|
| 42 |
+
for _, ft := range cbcAESFuzzTests {
|
| 43 |
+
c, _ := aes.NewCipher(ft.key)
|
| 44 |
+
|
| 45 |
+
cbcAsm := cipher.NewCBCEncrypter(c, commonIV)
|
| 46 |
+
cbcGeneric := cipher.NewCBCGenericEncrypter(c, commonIV)
|
| 47 |
+
|
| 48 |
+
if testing.Short() {
|
| 49 |
+
timeout = time.NewTimer(10 * time.Millisecond)
|
| 50 |
+
} else {
|
| 51 |
+
timeout = time.NewTimer(2 * time.Second)
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
indata := make([]byte, datalen)
|
| 55 |
+
outgeneric := make([]byte, datalen)
|
| 56 |
+
outdata := make([]byte, datalen)
|
| 57 |
+
|
| 58 |
+
fuzzencrypt:
|
| 59 |
+
for {
|
| 60 |
+
select {
|
| 61 |
+
case <-timeout.C:
|
| 62 |
+
break fuzzencrypt
|
| 63 |
+
default:
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
rand.Read(indata[:])
|
| 67 |
+
|
| 68 |
+
cbcGeneric.CryptBlocks(indata, outgeneric)
|
| 69 |
+
cbcAsm.CryptBlocks(indata, outdata)
|
| 70 |
+
|
| 71 |
+
if !bytes.Equal(outdata, outgeneric) {
|
| 72 |
+
t.Fatalf("AES-CBC encryption does not match reference result: %x and %x, please report this error to security@golang.org", outdata, outgeneric)
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
cbcAsm = cipher.NewCBCDecrypter(c, commonIV)
|
| 77 |
+
cbcGeneric = cipher.NewCBCGenericDecrypter(c, commonIV)
|
| 78 |
+
|
| 79 |
+
if testing.Short() {
|
| 80 |
+
timeout = time.NewTimer(10 * time.Millisecond)
|
| 81 |
+
} else {
|
| 82 |
+
timeout = time.NewTimer(2 * time.Second)
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
fuzzdecrypt:
|
| 86 |
+
for {
|
| 87 |
+
select {
|
| 88 |
+
case <-timeout.C:
|
| 89 |
+
break fuzzdecrypt
|
| 90 |
+
default:
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
rand.Read(indata[:])
|
| 94 |
+
|
| 95 |
+
cbcGeneric.CryptBlocks(indata, outgeneric)
|
| 96 |
+
cbcAsm.CryptBlocks(indata, outdata)
|
| 97 |
+
|
| 98 |
+
if !bytes.Equal(outdata, outgeneric) {
|
| 99 |
+
t.Fatalf("AES-CBC decryption does not match reference result: %x and %x, please report this error to security@golang.org", outdata, outgeneric)
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/gcm.go
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2013 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package cipher
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/internal/alias"
|
| 9 |
+
"crypto/subtle"
|
| 10 |
+
"encoding/binary"
|
| 11 |
+
"errors"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
// AEAD is a cipher mode providing authenticated encryption with associated
|
| 15 |
+
// data. For a description of the methodology, see
|
| 16 |
+
// https://en.wikipedia.org/wiki/Authenticated_encryption.
|
| 17 |
+
type AEAD interface {
|
| 18 |
+
// NonceSize returns the size of the nonce that must be passed to Seal
|
| 19 |
+
// and Open.
|
| 20 |
+
NonceSize() int
|
| 21 |
+
|
| 22 |
+
// Overhead returns the maximum difference between the lengths of a
|
| 23 |
+
// plaintext and its ciphertext.
|
| 24 |
+
Overhead() int
|
| 25 |
+
|
| 26 |
+
// Seal encrypts and authenticates plaintext, authenticates the
|
| 27 |
+
// additional data and appends the result to dst, returning the updated
|
| 28 |
+
// slice. The nonce must be NonceSize() bytes long and unique for all
|
| 29 |
+
// time, for a given key.
|
| 30 |
+
//
|
| 31 |
+
// To reuse plaintext's storage for the encrypted output, use plaintext[:0]
|
| 32 |
+
// as dst. Otherwise, the remaining capacity of dst must not overlap plaintext.
|
| 33 |
+
Seal(dst, nonce, plaintext, additionalData []byte) []byte
|
| 34 |
+
|
| 35 |
+
// Open decrypts and authenticates ciphertext, authenticates the
|
| 36 |
+
// additional data and, if successful, appends the resulting plaintext
|
| 37 |
+
// to dst, returning the updated slice. The nonce must be NonceSize()
|
| 38 |
+
// bytes long and both it and the additional data must match the
|
| 39 |
+
// value passed to Seal.
|
| 40 |
+
//
|
| 41 |
+
// To reuse ciphertext's storage for the decrypted output, use ciphertext[:0]
|
| 42 |
+
// as dst. Otherwise, the remaining capacity of dst must not overlap plaintext.
|
| 43 |
+
//
|
| 44 |
+
// Even if the function fails, the contents of dst, up to its capacity,
|
| 45 |
+
// may be overwritten.
|
| 46 |
+
Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error)
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// gcmAble is an interface implemented by ciphers that have a specific optimized
|
| 50 |
+
// implementation of GCM, like crypto/aes. NewGCM will check for this interface
|
| 51 |
+
// and return the specific AEAD if found.
|
| 52 |
+
type gcmAble interface {
|
| 53 |
+
NewGCM(nonceSize, tagSize int) (AEAD, error)
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
// gcmFieldElement represents a value in GF(2¹²⁸). In order to reflect the GCM
|
| 57 |
+
// standard and make binary.BigEndian suitable for marshaling these values, the
|
| 58 |
+
// bits are stored in big endian order. For example:
|
| 59 |
+
//
|
| 60 |
+
// the coefficient of x⁰ can be obtained by v.low >> 63.
|
| 61 |
+
// the coefficient of x⁶³ can be obtained by v.low & 1.
|
| 62 |
+
// the coefficient of x⁶⁴ can be obtained by v.high >> 63.
|
| 63 |
+
// the coefficient of x¹²⁷ can be obtained by v.high & 1.
|
| 64 |
+
type gcmFieldElement struct {
|
| 65 |
+
low, high uint64
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// gcm represents a Galois Counter Mode with a specific key. See
|
| 69 |
+
// https://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
|
| 70 |
+
type gcm struct {
|
| 71 |
+
cipher Block
|
| 72 |
+
nonceSize int
|
| 73 |
+
tagSize int
|
| 74 |
+
// productTable contains the first sixteen powers of the key, H.
|
| 75 |
+
// However, they are in bit reversed order. See NewGCMWithNonceSize.
|
| 76 |
+
productTable [16]gcmFieldElement
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
// NewGCM returns the given 128-bit, block cipher wrapped in Galois Counter Mode
|
| 80 |
+
// with the standard nonce length.
|
| 81 |
+
//
|
| 82 |
+
// In general, the GHASH operation performed by this implementation of GCM is not constant-time.
|
| 83 |
+
// An exception is when the underlying [Block] was created by aes.NewCipher
|
| 84 |
+
// on systems with hardware support for AES. See the [crypto/aes] package documentation for details.
|
| 85 |
+
func NewGCM(cipher Block) (AEAD, error) {
|
| 86 |
+
return newGCMWithNonceAndTagSize(cipher, gcmStandardNonceSize, gcmTagSize)
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
// NewGCMWithNonceSize returns the given 128-bit, block cipher wrapped in Galois
|
| 90 |
+
// Counter Mode, which accepts nonces of the given length. The length must not
|
| 91 |
+
// be zero.
|
| 92 |
+
//
|
| 93 |
+
// Only use this function if you require compatibility with an existing
|
| 94 |
+
// cryptosystem that uses non-standard nonce lengths. All other users should use
|
| 95 |
+
// [NewGCM], which is faster and more resistant to misuse.
|
| 96 |
+
func NewGCMWithNonceSize(cipher Block, size int) (AEAD, error) {
|
| 97 |
+
return newGCMWithNonceAndTagSize(cipher, size, gcmTagSize)
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
// NewGCMWithTagSize returns the given 128-bit, block cipher wrapped in Galois
|
| 101 |
+
// Counter Mode, which generates tags with the given length.
|
| 102 |
+
//
|
| 103 |
+
// Tag sizes between 12 and 16 bytes are allowed.
|
| 104 |
+
//
|
| 105 |
+
// Only use this function if you require compatibility with an existing
|
| 106 |
+
// cryptosystem that uses non-standard tag lengths. All other users should use
|
| 107 |
+
// [NewGCM], which is more resistant to misuse.
|
| 108 |
+
func NewGCMWithTagSize(cipher Block, tagSize int) (AEAD, error) {
|
| 109 |
+
return newGCMWithNonceAndTagSize(cipher, gcmStandardNonceSize, tagSize)
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
func newGCMWithNonceAndTagSize(cipher Block, nonceSize, tagSize int) (AEAD, error) {
|
| 113 |
+
if tagSize < gcmMinimumTagSize || tagSize > gcmBlockSize {
|
| 114 |
+
return nil, errors.New("cipher: incorrect tag size given to GCM")
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
if nonceSize <= 0 {
|
| 118 |
+
return nil, errors.New("cipher: the nonce can't have zero length, or the security of the key will be immediately compromised")
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
if cipher, ok := cipher.(gcmAble); ok {
|
| 122 |
+
return cipher.NewGCM(nonceSize, tagSize)
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
if cipher.BlockSize() != gcmBlockSize {
|
| 126 |
+
return nil, errors.New("cipher: NewGCM requires 128-bit block cipher")
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
var key [gcmBlockSize]byte
|
| 130 |
+
cipher.Encrypt(key[:], key[:])
|
| 131 |
+
|
| 132 |
+
g := &gcm{cipher: cipher, nonceSize: nonceSize, tagSize: tagSize}
|
| 133 |
+
|
| 134 |
+
// We precompute 16 multiples of |key|. However, when we do lookups
|
| 135 |
+
// into this table we'll be using bits from a field element and
|
| 136 |
+
// therefore the bits will be in the reverse order. So normally one
|
| 137 |
+
// would expect, say, 4*key to be in index 4 of the table but due to
|
| 138 |
+
// this bit ordering it will actually be in index 0010 (base 2) = 2.
|
| 139 |
+
x := gcmFieldElement{
|
| 140 |
+
binary.BigEndian.Uint64(key[:8]),
|
| 141 |
+
binary.BigEndian.Uint64(key[8:]),
|
| 142 |
+
}
|
| 143 |
+
g.productTable[reverseBits(1)] = x
|
| 144 |
+
|
| 145 |
+
for i := 2; i < 16; i += 2 {
|
| 146 |
+
g.productTable[reverseBits(i)] = gcmDouble(&g.productTable[reverseBits(i/2)])
|
| 147 |
+
g.productTable[reverseBits(i+1)] = gcmAdd(&g.productTable[reverseBits(i)], &x)
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
return g, nil
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
const (
|
| 154 |
+
gcmBlockSize = 16
|
| 155 |
+
gcmTagSize = 16
|
| 156 |
+
gcmMinimumTagSize = 12 // NIST SP 800-38D recommends tags with 12 or more bytes.
|
| 157 |
+
gcmStandardNonceSize = 12
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
func (g *gcm) NonceSize() int {
|
| 161 |
+
return g.nonceSize
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
func (g *gcm) Overhead() int {
|
| 165 |
+
return g.tagSize
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
func (g *gcm) Seal(dst, nonce, plaintext, data []byte) []byte {
|
| 169 |
+
if len(nonce) != g.nonceSize {
|
| 170 |
+
panic("crypto/cipher: incorrect nonce length given to GCM")
|
| 171 |
+
}
|
| 172 |
+
if uint64(len(plaintext)) > ((1<<32)-2)*uint64(g.cipher.BlockSize()) {
|
| 173 |
+
panic("crypto/cipher: message too large for GCM")
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
ret, out := sliceForAppend(dst, len(plaintext)+g.tagSize)
|
| 177 |
+
if alias.InexactOverlap(out, plaintext) {
|
| 178 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
var counter, tagMask [gcmBlockSize]byte
|
| 182 |
+
g.deriveCounter(&counter, nonce)
|
| 183 |
+
|
| 184 |
+
g.cipher.Encrypt(tagMask[:], counter[:])
|
| 185 |
+
gcmInc32(&counter)
|
| 186 |
+
|
| 187 |
+
g.counterCrypt(out, plaintext, &counter)
|
| 188 |
+
|
| 189 |
+
var tag [gcmTagSize]byte
|
| 190 |
+
g.auth(tag[:], out[:len(plaintext)], data, &tagMask)
|
| 191 |
+
copy(out[len(plaintext):], tag[:])
|
| 192 |
+
|
| 193 |
+
return ret
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
var errOpen = errors.New("cipher: message authentication failed")
|
| 197 |
+
|
| 198 |
+
func (g *gcm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
|
| 199 |
+
if len(nonce) != g.nonceSize {
|
| 200 |
+
panic("crypto/cipher: incorrect nonce length given to GCM")
|
| 201 |
+
}
|
| 202 |
+
// Sanity check to prevent the authentication from always succeeding if an implementation
|
| 203 |
+
// leaves tagSize uninitialized, for example.
|
| 204 |
+
if g.tagSize < gcmMinimumTagSize {
|
| 205 |
+
panic("crypto/cipher: incorrect GCM tag size")
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
if len(ciphertext) < g.tagSize {
|
| 209 |
+
return nil, errOpen
|
| 210 |
+
}
|
| 211 |
+
if uint64(len(ciphertext)) > ((1<<32)-2)*uint64(g.cipher.BlockSize())+uint64(g.tagSize) {
|
| 212 |
+
return nil, errOpen
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
tag := ciphertext[len(ciphertext)-g.tagSize:]
|
| 216 |
+
ciphertext = ciphertext[:len(ciphertext)-g.tagSize]
|
| 217 |
+
|
| 218 |
+
var counter, tagMask [gcmBlockSize]byte
|
| 219 |
+
g.deriveCounter(&counter, nonce)
|
| 220 |
+
|
| 221 |
+
g.cipher.Encrypt(tagMask[:], counter[:])
|
| 222 |
+
gcmInc32(&counter)
|
| 223 |
+
|
| 224 |
+
var expectedTag [gcmTagSize]byte
|
| 225 |
+
g.auth(expectedTag[:], ciphertext, data, &tagMask)
|
| 226 |
+
|
| 227 |
+
ret, out := sliceForAppend(dst, len(ciphertext))
|
| 228 |
+
if alias.InexactOverlap(out, ciphertext) {
|
| 229 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
if subtle.ConstantTimeCompare(expectedTag[:g.tagSize], tag) != 1 {
|
| 233 |
+
// The AESNI code decrypts and authenticates concurrently, and
|
| 234 |
+
// so overwrites dst in the event of a tag mismatch. That
|
| 235 |
+
// behavior is mimicked here in order to be consistent across
|
| 236 |
+
// platforms.
|
| 237 |
+
for i := range out {
|
| 238 |
+
out[i] = 0
|
| 239 |
+
}
|
| 240 |
+
return nil, errOpen
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
g.counterCrypt(out, ciphertext, &counter)
|
| 244 |
+
|
| 245 |
+
return ret, nil
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
// reverseBits reverses the order of the bits of 4-bit number in i.
|
| 249 |
+
func reverseBits(i int) int {
|
| 250 |
+
i = ((i << 2) & 0xc) | ((i >> 2) & 0x3)
|
| 251 |
+
i = ((i << 1) & 0xa) | ((i >> 1) & 0x5)
|
| 252 |
+
return i
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
// gcmAdd adds two elements of GF(2¹²⁸) and returns the sum.
|
| 256 |
+
func gcmAdd(x, y *gcmFieldElement) gcmFieldElement {
|
| 257 |
+
// Addition in a characteristic 2 field is just XOR.
|
| 258 |
+
return gcmFieldElement{x.low ^ y.low, x.high ^ y.high}
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
// gcmDouble returns the result of doubling an element of GF(2¹²⁸).
|
| 262 |
+
func gcmDouble(x *gcmFieldElement) (double gcmFieldElement) {
|
| 263 |
+
msbSet := x.high&1 == 1
|
| 264 |
+
|
| 265 |
+
// Because of the bit-ordering, doubling is actually a right shift.
|
| 266 |
+
double.high = x.high >> 1
|
| 267 |
+
double.high |= x.low << 63
|
| 268 |
+
double.low = x.low >> 1
|
| 269 |
+
|
| 270 |
+
// If the most-significant bit was set before shifting then it,
|
| 271 |
+
// conceptually, becomes a term of x^128. This is greater than the
|
| 272 |
+
// irreducible polynomial so the result has to be reduced. The
|
| 273 |
+
// irreducible polynomial is 1+x+x^2+x^7+x^128. We can subtract that to
|
| 274 |
+
// eliminate the term at x^128 which also means subtracting the other
|
| 275 |
+
// four terms. In characteristic 2 fields, subtraction == addition ==
|
| 276 |
+
// XOR.
|
| 277 |
+
if msbSet {
|
| 278 |
+
double.low ^= 0xe100000000000000
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
return
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
var gcmReductionTable = []uint16{
|
| 285 |
+
0x0000, 0x1c20, 0x3840, 0x2460, 0x7080, 0x6ca0, 0x48c0, 0x54e0,
|
| 286 |
+
0xe100, 0xfd20, 0xd940, 0xc560, 0x9180, 0x8da0, 0xa9c0, 0xb5e0,
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
// mul sets y to y*H, where H is the GCM key, fixed during NewGCMWithNonceSize.
|
| 290 |
+
func (g *gcm) mul(y *gcmFieldElement) {
|
| 291 |
+
var z gcmFieldElement
|
| 292 |
+
|
| 293 |
+
for i := 0; i < 2; i++ {
|
| 294 |
+
word := y.high
|
| 295 |
+
if i == 1 {
|
| 296 |
+
word = y.low
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
// Multiplication works by multiplying z by 16 and adding in
|
| 300 |
+
// one of the precomputed multiples of H.
|
| 301 |
+
for j := 0; j < 64; j += 4 {
|
| 302 |
+
msw := z.high & 0xf
|
| 303 |
+
z.high >>= 4
|
| 304 |
+
z.high |= z.low << 60
|
| 305 |
+
z.low >>= 4
|
| 306 |
+
z.low ^= uint64(gcmReductionTable[msw]) << 48
|
| 307 |
+
|
| 308 |
+
// the values in |table| are ordered for
|
| 309 |
+
// little-endian bit positions. See the comment
|
| 310 |
+
// in NewGCMWithNonceSize.
|
| 311 |
+
t := &g.productTable[word&0xf]
|
| 312 |
+
|
| 313 |
+
z.low ^= t.low
|
| 314 |
+
z.high ^= t.high
|
| 315 |
+
word >>= 4
|
| 316 |
+
}
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
*y = z
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
// updateBlocks extends y with more polynomial terms from blocks, based on
|
| 323 |
+
// Horner's rule. There must be a multiple of gcmBlockSize bytes in blocks.
|
| 324 |
+
func (g *gcm) updateBlocks(y *gcmFieldElement, blocks []byte) {
|
| 325 |
+
for len(blocks) > 0 {
|
| 326 |
+
y.low ^= binary.BigEndian.Uint64(blocks)
|
| 327 |
+
y.high ^= binary.BigEndian.Uint64(blocks[8:])
|
| 328 |
+
g.mul(y)
|
| 329 |
+
blocks = blocks[gcmBlockSize:]
|
| 330 |
+
}
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
// update extends y with more polynomial terms from data. If data is not a
|
| 334 |
+
// multiple of gcmBlockSize bytes long then the remainder is zero padded.
|
| 335 |
+
func (g *gcm) update(y *gcmFieldElement, data []byte) {
|
| 336 |
+
fullBlocks := (len(data) >> 4) << 4
|
| 337 |
+
g.updateBlocks(y, data[:fullBlocks])
|
| 338 |
+
|
| 339 |
+
if len(data) != fullBlocks {
|
| 340 |
+
var partialBlock [gcmBlockSize]byte
|
| 341 |
+
copy(partialBlock[:], data[fullBlocks:])
|
| 342 |
+
g.updateBlocks(y, partialBlock[:])
|
| 343 |
+
}
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
// gcmInc32 treats the final four bytes of counterBlock as a big-endian value
|
| 347 |
+
// and increments it.
|
| 348 |
+
func gcmInc32(counterBlock *[16]byte) {
|
| 349 |
+
ctr := counterBlock[len(counterBlock)-4:]
|
| 350 |
+
binary.BigEndian.PutUint32(ctr, binary.BigEndian.Uint32(ctr)+1)
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
// sliceForAppend takes a slice and a requested number of bytes. It returns a
|
| 354 |
+
// slice with the contents of the given slice followed by that many bytes and a
|
| 355 |
+
// second slice that aliases into it and contains only the extra bytes. If the
|
| 356 |
+
// original slice has sufficient capacity then no allocation is performed.
|
| 357 |
+
func sliceForAppend(in []byte, n int) (head, tail []byte) {
|
| 358 |
+
if total := len(in) + n; cap(in) >= total {
|
| 359 |
+
head = in[:total]
|
| 360 |
+
} else {
|
| 361 |
+
head = make([]byte, total)
|
| 362 |
+
copy(head, in)
|
| 363 |
+
}
|
| 364 |
+
tail = head[len(in):]
|
| 365 |
+
return
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
// counterCrypt crypts in to out using g.cipher in counter mode.
|
| 369 |
+
func (g *gcm) counterCrypt(out, in []byte, counter *[gcmBlockSize]byte) {
|
| 370 |
+
var mask [gcmBlockSize]byte
|
| 371 |
+
|
| 372 |
+
for len(in) >= gcmBlockSize {
|
| 373 |
+
g.cipher.Encrypt(mask[:], counter[:])
|
| 374 |
+
gcmInc32(counter)
|
| 375 |
+
|
| 376 |
+
subtle.XORBytes(out, in, mask[:])
|
| 377 |
+
out = out[gcmBlockSize:]
|
| 378 |
+
in = in[gcmBlockSize:]
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
if len(in) > 0 {
|
| 382 |
+
g.cipher.Encrypt(mask[:], counter[:])
|
| 383 |
+
gcmInc32(counter)
|
| 384 |
+
subtle.XORBytes(out, in, mask[:])
|
| 385 |
+
}
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
// deriveCounter computes the initial GCM counter state from the given nonce.
|
| 389 |
+
// See NIST SP 800-38D, section 7.1. This assumes that counter is filled with
|
| 390 |
+
// zeros on entry.
|
| 391 |
+
func (g *gcm) deriveCounter(counter *[gcmBlockSize]byte, nonce []byte) {
|
| 392 |
+
// GCM has two modes of operation with respect to the initial counter
|
| 393 |
+
// state: a "fast path" for 96-bit (12-byte) nonces, and a "slow path"
|
| 394 |
+
// for nonces of other lengths. For a 96-bit nonce, the nonce, along
|
| 395 |
+
// with a four-byte big-endian counter starting at one, is used
|
| 396 |
+
// directly as the starting counter. For other nonce sizes, the counter
|
| 397 |
+
// is computed by passing it through the GHASH function.
|
| 398 |
+
if len(nonce) == gcmStandardNonceSize {
|
| 399 |
+
copy(counter[:], nonce)
|
| 400 |
+
counter[gcmBlockSize-1] = 1
|
| 401 |
+
} else {
|
| 402 |
+
var y gcmFieldElement
|
| 403 |
+
g.update(&y, nonce)
|
| 404 |
+
y.high ^= uint64(len(nonce)) * 8
|
| 405 |
+
g.mul(&y)
|
| 406 |
+
binary.BigEndian.PutUint64(counter[:8], y.low)
|
| 407 |
+
binary.BigEndian.PutUint64(counter[8:], y.high)
|
| 408 |
+
}
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
// auth calculates GHASH(ciphertext, additionalData), masks the result with
|
| 412 |
+
// tagMask and writes the result to out.
|
| 413 |
+
func (g *gcm) auth(out, ciphertext, additionalData []byte, tagMask *[gcmTagSize]byte) {
|
| 414 |
+
var y gcmFieldElement
|
| 415 |
+
g.update(&y, additionalData)
|
| 416 |
+
g.update(&y, ciphertext)
|
| 417 |
+
|
| 418 |
+
y.low ^= uint64(len(additionalData)) * 8
|
| 419 |
+
y.high ^= uint64(len(ciphertext)) * 8
|
| 420 |
+
|
| 421 |
+
g.mul(&y)
|
| 422 |
+
|
| 423 |
+
binary.BigEndian.PutUint64(out, y.low)
|
| 424 |
+
binary.BigEndian.PutUint64(out[8:], y.high)
|
| 425 |
+
|
| 426 |
+
subtle.XORBytes(out, out, tagMask[:])
|
| 427 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/gcm_test.go
ADDED
|
@@ -0,0 +1,656 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2013 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package cipher_test
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"crypto/aes"
|
| 10 |
+
"crypto/cipher"
|
| 11 |
+
"crypto/rand"
|
| 12 |
+
"encoding/hex"
|
| 13 |
+
"errors"
|
| 14 |
+
"io"
|
| 15 |
+
"reflect"
|
| 16 |
+
"testing"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
var aesGCMTests = []struct {
|
| 20 |
+
key, nonce, plaintext, ad, result string
|
| 21 |
+
}{
|
| 22 |
+
{ // key=16, plaintext=null
|
| 23 |
+
"11754cd72aec309bf52f7687212e8957",
|
| 24 |
+
"3c819d9a9bed087615030b65",
|
| 25 |
+
"",
|
| 26 |
+
"",
|
| 27 |
+
"250327c674aaf477aef2675748cf6971",
|
| 28 |
+
},
|
| 29 |
+
{ // key=24, plaintext=null
|
| 30 |
+
"e2e001a36c60d2bf40d69ff5b2b1161ea218db263be16a4e",
|
| 31 |
+
"3c819d9a9bed087615030b65",
|
| 32 |
+
"",
|
| 33 |
+
"",
|
| 34 |
+
"c7b8da1fe2e3dccc4071ba92a0a57ba8",
|
| 35 |
+
},
|
| 36 |
+
{ // key=32, plaintext=null
|
| 37 |
+
"5394e890d37ba55ec9d5f327f15680f6a63ef5279c79331643ad0af6d2623525",
|
| 38 |
+
"3c819d9a9bed087615030b65",
|
| 39 |
+
"",
|
| 40 |
+
"",
|
| 41 |
+
"d9b260d4bc4630733ffb642f5ce45726",
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"ca47248ac0b6f8372a97ac43508308ed",
|
| 45 |
+
"ffd2b598feabc9019262d2be",
|
| 46 |
+
"",
|
| 47 |
+
"",
|
| 48 |
+
"60d20404af527d248d893ae495707d1a",
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"fbe3467cc254f81be8e78d765a2e6333",
|
| 52 |
+
"c6697351ff4aec29cdbaabf2",
|
| 53 |
+
"",
|
| 54 |
+
"67",
|
| 55 |
+
"3659cdc25288bf499ac736c03bfc1159",
|
| 56 |
+
},
|
| 57 |
+
{
|
| 58 |
+
"8a7f9d80d08ad0bd5a20fb689c88f9fc",
|
| 59 |
+
"88b7b27d800937fda4f47301",
|
| 60 |
+
"",
|
| 61 |
+
"50edd0503e0d7b8c91608eb5a1",
|
| 62 |
+
"ed6f65322a4740011f91d2aae22dd44e",
|
| 63 |
+
},
|
| 64 |
+
{
|
| 65 |
+
"051758e95ed4abb2cdc69bb454110e82",
|
| 66 |
+
"c99a66320db73158a35a255d",
|
| 67 |
+
"",
|
| 68 |
+
"67c6697351ff4aec29cdbaabf2fbe3467cc254f81be8e78d765a2e63339f",
|
| 69 |
+
"6ce77f1a5616c505b6aec09420234036",
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"77be63708971c4e240d1cb79e8d77feb",
|
| 73 |
+
"e0e00f19fed7ba0136a797f3",
|
| 74 |
+
"",
|
| 75 |
+
"7a43ec1d9c0a5a78a0b16533a6213cab",
|
| 76 |
+
"209fcc8d3675ed938e9c7166709dd946",
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
"7680c5d3ca6154758e510f4d25b98820",
|
| 80 |
+
"f8f105f9c3df4965780321f8",
|
| 81 |
+
"",
|
| 82 |
+
"c94c410194c765e3dcc7964379758ed3",
|
| 83 |
+
"94dca8edfcf90bb74b153c8d48a17930",
|
| 84 |
+
},
|
| 85 |
+
|
| 86 |
+
{ // key=16, plaintext=16
|
| 87 |
+
"7fddb57453c241d03efbed3ac44e371c",
|
| 88 |
+
"ee283a3fc75575e33efd4887",
|
| 89 |
+
"d5de42b461646c255c87bd2962d3b9a2",
|
| 90 |
+
"",
|
| 91 |
+
"2ccda4a5415cb91e135c2a0f78c9b2fdb36d1df9b9d5e596f83e8b7f52971cb3",
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"ab72c77b97cb5fe9a382d9fe81ffdbed",
|
| 95 |
+
"54cc7dc2c37ec006bcc6d1da",
|
| 96 |
+
"007c5e5b3e59df24a7c355584fc1518d",
|
| 97 |
+
"",
|
| 98 |
+
"0e1bde206a07a9c2c1b65300f8c649972b4401346697138c7a4891ee59867d0c",
|
| 99 |
+
},
|
| 100 |
+
{ // key=24, plaintext=16
|
| 101 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c",
|
| 102 |
+
"54cc7dc2c37ec006bcc6d1da",
|
| 103 |
+
"007c5e5b3e59df24a7c355584fc1518d",
|
| 104 |
+
"",
|
| 105 |
+
"7bd53594c28b6c6596feb240199cad4c9badb907fd65bde541b8df3bd444d3a8",
|
| 106 |
+
},
|
| 107 |
+
{ // key=32, plaintext=16
|
| 108 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
|
| 109 |
+
"54cc7dc2c37ec006bcc6d1da",
|
| 110 |
+
"007c5e5b3e59df24a7c355584fc1518d",
|
| 111 |
+
"",
|
| 112 |
+
"d50b9e252b70945d4240d351677eb10f937cdaef6f2822b6a3191654ba41b197",
|
| 113 |
+
},
|
| 114 |
+
{ // key=16, plaintext=23
|
| 115 |
+
"ab72c77b97cb5fe9a382d9fe81ffdbed",
|
| 116 |
+
"54cc7dc2c37ec006bcc6d1da",
|
| 117 |
+
"007c5e5b3e59df24a7c355584fc1518dabcdefab",
|
| 118 |
+
"",
|
| 119 |
+
"0e1bde206a07a9c2c1b65300f8c64997b73381a6ff6bc24c5146fbd73361f4fe",
|
| 120 |
+
},
|
| 121 |
+
{ // key=24, plaintext=23
|
| 122 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c",
|
| 123 |
+
"54cc7dc2c37ec006bcc6d1da",
|
| 124 |
+
"007c5e5b3e59df24a7c355584fc1518dabcdefab",
|
| 125 |
+
"",
|
| 126 |
+
"7bd53594c28b6c6596feb240199cad4c23b86a96d423cffa929e68541dc16b28",
|
| 127 |
+
},
|
| 128 |
+
{ // key=32, plaintext=23
|
| 129 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
|
| 130 |
+
"54cc7dc2c37ec006bcc6d1da",
|
| 131 |
+
"007c5e5b3e59df24a7c355584fc1518dabcdefab",
|
| 132 |
+
"",
|
| 133 |
+
"d50b9e252b70945d4240d351677eb10f27fd385388ad3b72b96a2d5dea1240ae",
|
| 134 |
+
},
|
| 135 |
+
|
| 136 |
+
{ // key=16, plaintext=51
|
| 137 |
+
"fe47fcce5fc32665d2ae399e4eec72ba",
|
| 138 |
+
"5adb9609dbaeb58cbd6e7275",
|
| 139 |
+
"7c0e88c88899a779228465074797cd4c2e1498d259b54390b85e3eef1c02df60e743f1b840382c4bccaf3bafb4ca8429bea063",
|
| 140 |
+
"88319d6e1d3ffa5f987199166c8a9b56c2aeba5a",
|
| 141 |
+
"98f4826f05a265e6dd2be82db241c0fbbbf9ffb1c173aa83964b7cf5393043736365253ddbc5db8778371495da76d269e5db3e291ef1982e4defedaa2249f898556b47",
|
| 142 |
+
},
|
| 143 |
+
{
|
| 144 |
+
"ec0c2ba17aa95cd6afffe949da9cc3a8",
|
| 145 |
+
"296bce5b50b7d66096d627ef",
|
| 146 |
+
"b85b3753535b825cbe5f632c0b843c741351f18aa484281aebec2f45bb9eea2d79d987b764b9611f6c0f8641843d5d58f3a242",
|
| 147 |
+
"f8d00f05d22bf68599bcdeb131292ad6e2df5d14",
|
| 148 |
+
"a7443d31c26bdf2a1c945e29ee4bd344a99cfaf3aa71f8b3f191f83c2adfc7a07162995506fde6309ffc19e716eddf1a828c5a890147971946b627c40016da1ecf3e77",
|
| 149 |
+
},
|
| 150 |
+
{
|
| 151 |
+
"2c1f21cf0f6fb3661943155c3e3d8492",
|
| 152 |
+
"23cb5ff362e22426984d1907",
|
| 153 |
+
"42f758836986954db44bf37c6ef5e4ac0adaf38f27252a1b82d02ea949c8a1a2dbc0d68b5615ba7c1220ff6510e259f06655d8",
|
| 154 |
+
"5d3624879d35e46849953e45a32a624d6a6c536ed9857c613b572b0333e701557a713e3f010ecdf9a6bd6c9e3e44b065208645aff4aabee611b391528514170084ccf587177f4488f33cfb5e979e42b6e1cfc0a60238982a7aec",
|
| 155 |
+
"81824f0e0d523db30d3da369fdc0d60894c7a0a20646dd015073ad2732bd989b14a222b6ad57af43e1895df9dca2a5344a62cc57a3ee28136e94c74838997ae9823f3a",
|
| 156 |
+
},
|
| 157 |
+
{
|
| 158 |
+
"d9f7d2411091f947b4d6f1e2d1f0fb2e",
|
| 159 |
+
"e1934f5db57cc983e6b180e7",
|
| 160 |
+
"73ed042327f70fe9c572a61545eda8b2a0c6e1d6c291ef19248e973aee6c312012f490c2c6f6166f4a59431e182663fcaea05a",
|
| 161 |
+
"0a8a18a7150e940c3d87b38e73baee9a5c049ee21795663e264b694a949822b639092d0e67015e86363583fcf0ca645af9f43375f05fdb4ce84f411dcbca73c2220dea03a20115d2e51398344b16bee1ed7c499b353d6c597af8",
|
| 162 |
+
"aaadbd5c92e9151ce3db7210b8714126b73e43436d242677afa50384f2149b831f1d573c7891c2a91fbc48db29967ec9542b2321b51ca862cb637cdd03b99a0f93b134",
|
| 163 |
+
},
|
| 164 |
+
{ //key=24 plaintext=51
|
| 165 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c",
|
| 166 |
+
"e1934f5db57cc983e6b180e7",
|
| 167 |
+
"73ed042327f70fe9c572a61545eda8b2a0c6e1d6c291ef19248e973aee6c312012f490c2c6f6166f4a59431e182663fcaea05a",
|
| 168 |
+
"0a8a18a7150e940c3d87b38e73baee9a5c049ee21795663e264b694a949822b639092d0e67015e86363583fcf0ca645af9f43375f05fdb4ce84f411dcbca73c2220dea03a20115d2e51398344b16bee1ed7c499b353d6c597af8",
|
| 169 |
+
"0736378955001d50773305975b3a534a4cd3614dd7300916301ae508cb7b45aa16e79435ca16b5557bcad5991bc52b971806863b15dc0b055748919b8ee91bc8477f68",
|
| 170 |
+
},
|
| 171 |
+
{ //key-32 plaintext=51
|
| 172 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
|
| 173 |
+
"e1934f5db57cc983e6b180e7",
|
| 174 |
+
"73ed042327f70fe9c572a61545eda8b2a0c6e1d6c291ef19248e973aee6c312012f490c2c6f6166f4a59431e182663fcaea05a",
|
| 175 |
+
"0a8a18a7150e940c3d87b38e73baee9a5c049ee21795663e264b694a949822b639092d0e67015e86363583fcf0ca645af9f43375f05fdb4ce84f411dcbca73c2220dea03a20115d2e51398344b16bee1ed7c499b353d6c597af8",
|
| 176 |
+
"fc1ae2b5dcd2c4176c3f538b4c3cc21197f79e608cc3730167936382e4b1e5a7b75ae1678bcebd876705477eb0e0fdbbcda92fb9a0dc58c8d8f84fb590e0422e6077ef",
|
| 177 |
+
},
|
| 178 |
+
{ //key=16 plaintext=138
|
| 179 |
+
"d9f7d2411091f947b4d6f1e2d1f0fb2e",
|
| 180 |
+
"e1934f5db57cc983e6b180e7",
|
| 181 |
+
"67c6697351ff4aec29cdbaabf2fbe3467cc254f81be8e78d765a2e63339fc99a66320db73158a35a255d051758e95ed4abb2cdc69bb454110e827441213ddc8770e93ea141e1fc673e017e97eadc6b968f385c2aecb03bfb32af3c54ec18db5c021afe43fbfaaa3afb29d1e6053c7c9475d8be6189f95cbba8990f95b1ebf1b3aabbccddee",
|
| 182 |
+
"0a8a18a7150e940c3d87b38e73baee9a5c049ee21795663e264b694a949822b639092d0e67015e86363583fcf0ca645af9f43375f05fdb4ce84f411dcbca73c2220dea03a20115d2e51398344b16bee1ed7c499b353d6c597af8",
|
| 183 |
+
"be86d00ce4e150190f646eae0f670ad26b3af66db45d2ee3fd71badd2fe763396bdbca498f3f779c70b80ed2695943e15139b406e5147b3855a1441dfb7bd64954b581e3db0ddf26b1c759e2276a4c18a8e4ad4b890f473e61c78e60074bd0633961e87e66d0a1be77c51ab6b9bb3318ccdd43794ffc18a03a83c1d368eeea590a13407c7ef48efc66e26047f3ab9deed0412ce89e",
|
| 184 |
+
},
|
| 185 |
+
{ //key=24 plaintext=138
|
| 186 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c",
|
| 187 |
+
"e1934f5db57cc983e6b180e7",
|
| 188 |
+
"67c6697351ff4aec29cdbaabf2fbe3467cc254f81be8e78d765a2e63339fc99a66320db73158a35a255d051758e95ed4abb2cdc69bb454110e827441213ddc8770e93ea141e1fc673e017e97eadc6b968f385c2aecb03bfb32af3c54ec18db5c021afe43fbfaaa3afb29d1e6053c7c9475d8be6189f95cbba8990f95b1ebf1b3aabbccddee",
|
| 189 |
+
"0a8a18a7150e940c3d87b38e73baee9a5c049ee21795663e264b694a949822b639092d0e67015e86363583fcf0ca645af9f43375f05fdb4ce84f411dcbca73c2220dea03a20115d2e51398344b16bee1ed7c499b353d6c597af8",
|
| 190 |
+
"131d5ad9230858559b8c1929ec2c18be90d7d4630e49018262ce5c511688bd10622109403db8006014ce93905b0a16bf1d1411acc9e14edf09518bd5967ff4bc202805d4c2810810a093e996a0f56c9a3e3e593c783f68528c1a282ff6f4925902bb2b3d4cdd04b873663bf5fd9dd53b5df462e0424d038f249b10a99c0523200f8c92c3e8a178a25ee8e23b71308c88ec2cfe047e",
|
| 191 |
+
},
|
| 192 |
+
{ //key-32 plaintext=138
|
| 193 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
|
| 194 |
+
"e1934f5db57cc983e6b180e7",
|
| 195 |
+
"67c6697351ff4aec29cdbaabf2fbe3467cc254f81be8e78d765a2e63339fc99a66320db73158a35a255d051758e95ed4abb2cdc69bb454110e827441213ddc8770e93ea141e1fc673e017e97eadc6b968f385c2aecb03bfb32af3c54ec18db5c021afe43fbfaaa3afb29d1e6053c7c9475d8be6189f95cbba8990f95b1ebf1b3aabbccddee",
|
| 196 |
+
"0a8a18a7150e940c3d87b38e73baee9a5c049ee21795663e264b694a949822b639092d0e67015e86363583fcf0ca645af9f43375f05fdb4ce84f411dcbca73c2220dea03a20115d2e51398344b16bee1ed7c499b353d6c597af8",
|
| 197 |
+
"e8318fe5aada811280804f35fb2a89e54bf32b4e55ba7b953547dadb39421d1dc39c7c127c6008b208010177f02fc093c8bbb8b3834d0e060d96dda96ba386c7c01224a4cac1edebffda4f9a64692bfbffb9f7c2999069fab84205224978a10d815d5ab8fa31e4e11630ba01c3b6cb99bef5772357ce86b83b4fb45ea7146402d560b6ad07de635b9366865e788a6bcdb132dcd079",
|
| 198 |
+
},
|
| 199 |
+
{ // key=16, plaintext=13
|
| 200 |
+
"fe9bb47deb3a61e423c2231841cfd1fb",
|
| 201 |
+
"4d328eb776f500a2f7fb47aa",
|
| 202 |
+
"f1cc3818e421876bb6b8bbd6c9",
|
| 203 |
+
"",
|
| 204 |
+
"b88c5c1977b35b517b0aeae96743fd4727fe5cdb4b5b42818dea7ef8c9",
|
| 205 |
+
},
|
| 206 |
+
{ // key=16, plaintext=13
|
| 207 |
+
"6703df3701a7f54911ca72e24dca046a",
|
| 208 |
+
"12823ab601c350ea4bc2488c",
|
| 209 |
+
"793cd125b0b84a043e3ac67717",
|
| 210 |
+
"",
|
| 211 |
+
"b2051c80014f42f08735a7b0cd38e6bcd29962e5f2c13626b85a877101",
|
| 212 |
+
},
|
| 213 |
+
{ // key=24, plaintext=13
|
| 214 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c",
|
| 215 |
+
"12823ab601c350ea4bc2488c",
|
| 216 |
+
"793cd125b0b84a043e3ac67717",
|
| 217 |
+
"",
|
| 218 |
+
"e888c2f438caedd4189d26c59f53439b8a7caec29e98c33ebf7e5712d6",
|
| 219 |
+
},
|
| 220 |
+
{ // key=32, plaintext=13
|
| 221 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
|
| 222 |
+
"12823ab601c350ea4bc2488c",
|
| 223 |
+
"793cd125b0b84a043e3ac67717",
|
| 224 |
+
"",
|
| 225 |
+
"e796c39074c7783a38193e3f8d46b355adacca7198d16d879fbfeac6e3",
|
| 226 |
+
},
|
| 227 |
+
|
| 228 |
+
// These cases test non-standard nonce sizes.
|
| 229 |
+
{ // key=16, plaintext=0
|
| 230 |
+
"1672c3537afa82004c6b8a46f6f0d026",
|
| 231 |
+
"05",
|
| 232 |
+
"",
|
| 233 |
+
"",
|
| 234 |
+
"8e2ad721f9455f74d8b53d3141f27e8e",
|
| 235 |
+
},
|
| 236 |
+
{ //key=16, plaintext=32
|
| 237 |
+
"9a4fea86a621a91ab371e492457796c0",
|
| 238 |
+
"75",
|
| 239 |
+
"ca6131faf0ff210e4e693d6c31c109fc5b6f54224eb120f37de31dc59ec669b6",
|
| 240 |
+
"4f6e2585c161f05a9ae1f2f894e9f0ab52b45d0f",
|
| 241 |
+
"5698c0a384241d30004290aac56bb3ece6fe8eacc5c4be98954deb9c3ff6aebf5d50e1af100509e1fba2a5e8a0af9670",
|
| 242 |
+
},
|
| 243 |
+
{ //key=24, plaintext=32
|
| 244 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c",
|
| 245 |
+
"75",
|
| 246 |
+
"ca6131faf0ff210e4e693d6c31c109fc5b6f54224eb120f37de31dc59ec669b6",
|
| 247 |
+
"4f6e2585c161f05a9ae1f2f894e9f0ab52b45d0f",
|
| 248 |
+
"2709b357ec8334a074dbd5c4c352b216cfd1c8bd66343c5d43bfc6bd3b2b6cd0e3a82315d56ea5e4961c9ef3bc7e4042",
|
| 249 |
+
},
|
| 250 |
+
{ //key=32, plaintext=32
|
| 251 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
|
| 252 |
+
"75",
|
| 253 |
+
"ca6131faf0ff210e4e693d6c31c109fc5b6f54224eb120f37de31dc59ec669b6",
|
| 254 |
+
"4f6e2585c161f05a9ae1f2f894e9f0ab52b45d0f",
|
| 255 |
+
"d73bebe722c5e312fe910ba71d5a6a063a4297203f819103dfa885a8076d095545a999affde3dbac2b5be6be39195ed0",
|
| 256 |
+
},
|
| 257 |
+
{ // key=16, plaintext=0
|
| 258 |
+
"d0f1f4defa1e8c08b4b26d576392027c",
|
| 259 |
+
"42b4f01eb9f5a1ea5b1eb73b0fb0baed54f387ecaa0393c7d7dffc6af50146ecc021abf7eb9038d4303d91f8d741a11743166c0860208bcc02c6258fd9511a2fa626f96d60b72fcff773af4e88e7a923506e4916ecbd814651e9f445adef4ad6a6b6c7290cc13b956130eef5b837c939fcac0cbbcc9656cd75b13823ee5acdac",
|
| 260 |
+
"",
|
| 261 |
+
"",
|
| 262 |
+
"7ab49b57ddf5f62c427950111c5c4f0d",
|
| 263 |
+
},
|
| 264 |
+
{ //key=16, plaintext=13
|
| 265 |
+
"4a0c00a3d284dea9d4bf8b8dde86685e",
|
| 266 |
+
"f8cbe82588e784bcacbe092cd9089b51e01527297f635bf294b3aa787d91057ef23869789698ac960707857f163ecb242135a228ad93964f5dc4a4d7f88fd7b3b07dd0a5b37f9768fb05a523639f108c34c661498a56879e501a2321c8a4a94d7e1b89db255ac1f685e185263368e99735ebe62a7f2931b47282be8eb165e4d7",
|
| 267 |
+
"6d4bf87640a6a48a50d28797b7",
|
| 268 |
+
"8d8c7ffc55086d539b5a8f0d1232654c",
|
| 269 |
+
"0d803ec309482f35b8e6226f2b56303239298e06b281c2d51aaba3c125",
|
| 270 |
+
},
|
| 271 |
+
{ //key=16, plaintext=128
|
| 272 |
+
"0e18a844ac5bf38e4cd72d9b0942e506",
|
| 273 |
+
"0870d4b28a2954489a0abcd5",
|
| 274 |
+
"67c6697351ff4aec29cdbaabf2fbe3467cc254f81be8e78d765a2e63339fc99a66320db73158a35a255d051758e95ed4abb2cdc69bb454110e827441213ddc8770e93ea141e1fc673e017e97eadc6b968f385c2aecb03bfb32af3c54ec18db5c021afe43fbfaaa3afb29d1e6053c7c9475d8be6189f95cbba8990f95b1ebf1b3",
|
| 275 |
+
"05eff700e9a13ae5ca0bcbd0484764bd1f231ea81c7b64c514735ac55e4b79633b706424119e09dcaad4acf21b10af3b33cde3504847155cbb6f2219ba9b7df50be11a1c7f23f829f8a41b13b5ca4ee8983238e0794d3d34bc5f4e77facb6c05ac86212baa1a55a2be70b5733b045cd33694b3afe2f0e49e4f321549fd824ea9",
|
| 276 |
+
"cace28f4976afd72e3c5128167eb788fbf6634dda0a2f53148d00f6fa557f5e9e8f736c12e450894af56cb67f7d99e1027258c8571bd91ee3b7360e0d508aa1f382411a16115f9c05251cc326d4016f62e0eb8151c048465b0c6c8ff12558d43310e18b2cb1889eec91557ce21ba05955cf4c1d4847aadfb1b0a83f3a3b82b7efa62a5f03c5d6eda381a85dd78dbc55c",
|
| 277 |
+
},
|
| 278 |
+
{ //key=24, plaintext=128
|
| 279 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c",
|
| 280 |
+
"0870d4b28a2954489a0abcd5",
|
| 281 |
+
"67c6697351ff4aec29cdbaabf2fbe3467cc254f81be8e78d765a2e63339fc99a66320db73158a35a255d051758e95ed4abb2cdc69bb454110e827441213ddc8770e93ea141e1fc673e017e97eadc6b968f385c2aecb03bfb32af3c54ec18db5c021afe43fbfaaa3afb29d1e6053c7c9475d8be6189f95cbba8990f95b1ebf1b3",
|
| 282 |
+
"05eff700e9a13ae5ca0bcbd0484764bd1f231ea81c7b64c514735ac55e4b79633b706424119e09dcaad4acf21b10af3b33cde3504847155cbb6f2219ba9b7df50be11a1c7f23f829f8a41b13b5ca4ee8983238e0794d3d34bc5f4e77facb6c05ac86212baa1a55a2be70b5733b045cd33694b3afe2f0e49e4f321549fd824ea9",
|
| 283 |
+
"303157d398376a8d51e39eabdd397f45b65f81f09acbe51c726ae85867e1675cad178580bb31c7f37c1af3644bd36ac436e9459139a4903d95944f306e415da709134dccde9d2b2d7d196b6740c196d9d10caa45296cf577a6e15d7ddf3576c20c503617d6a9e6b6d2be09ae28410a1210700a463a5b3b8d391abe9dac217e76a6f78306b5ebe759a5986b7d6682db0b",
|
| 284 |
+
},
|
| 285 |
+
{ //key=32, plaintext=128
|
| 286 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
|
| 287 |
+
"0870d4b28a2954489a0abcd5",
|
| 288 |
+
"67c6697351ff4aec29cdbaabf2fbe3467cc254f81be8e78d765a2e63339fc99a66320db73158a35a255d051758e95ed4abb2cdc69bb454110e827441213ddc8770e93ea141e1fc673e017e97eadc6b968f385c2aecb03bfb32af3c54ec18db5c021afe43fbfaaa3afb29d1e6053c7c9475d8be6189f95cbba8990f95b1ebf1b3",
|
| 289 |
+
"05eff700e9a13ae5ca0bcbd0484764bd1f231ea81c7b64c514735ac55e4b79633b706424119e09dcaad4acf21b10af3b33cde3504847155cbb6f2219ba9b7df50be11a1c7f23f829f8a41b13b5ca4ee8983238e0794d3d34bc5f4e77facb6c05ac86212baa1a55a2be70b5733b045cd33694b3afe2f0e49e4f321549fd824ea9",
|
| 290 |
+
"e4f13934744125b9c35935ed4c5ac7d0c16434d52eadef1da91c6abb62bc757f01e3e42f628f030d750826adceb961f0675b81de48376b181d8781c6a0ccd0f34872ef6901b97ff7c2e152426b3257fb91f6a43f47befaaf7a2136fd0c97de8c48517ce047a5641141092c717b151b44f0794a164b5861f0a77271d1bdbc332e9e43d3b9828ccfdbd4ae338da5baf7a9",
|
| 291 |
+
},
|
| 292 |
+
|
| 293 |
+
{ //key=16, plaintext=512
|
| 294 |
+
"1f6c3a3bc0542aabba4ef8f6c7169e73",
|
| 295 |
+
"f3584606472b260e0dd2ebb2",
|
| 296 |
+
"67c6697351ff4aec29cdbaabf2fbe3467cc254f81be8e78d765a2e63339fc99a66320db73158a35a255d051758e95ed4abb2cdc69bb454110e827441213ddc8770e93ea141e1fc673e017e97eadc6b968f385c2aecb03bfb32af3c54ec18db5c021afe43fbfaaa3afb29d1e6053c7c9475d8be6189f95cbba8990f95b1ebf1b305eff700e9a13ae5ca0bcbd0484764bd1f231ea81c7b64c514735ac55e4b79633b706424119e09dcaad4acf21b10af3b33cde3504847155cbb6f2219ba9b7df50be11a1c7f23f829f8a41b13b5ca4ee8983238e0794d3d34bc5f4e77facb6c05ac86212baa1a55a2be70b5733b045cd33694b3afe2f0e49e4f321549fd824ea90870d4b28a2954489a0abcd50e18a844ac5bf38e4cd72d9b0942e506c433afcda3847f2dadd47647de321cec4ac430f62023856cfbb20704f4ec0bb920ba86c33e05f1ecd96733b79950a3e314d3d934f75ea0f210a8f6059401beb4bc4478fa4969e623d01ada696a7e4c7e5125b34884533a94fb319990325744ee9bbce9e525cf08f5e9e25e5360aad2b2d085fa54d835e8d466826498d9a8877565705a8a3f62802944de7ca5894e5759d351adac869580ec17e485f18c0c66f17cc07cbb22fce466da610b63af62bc83b4692f3affaf271693ac071fb86d11342d8def4f89d4b66335c1c7e4248367d8ed9612ec453902d8e50af89d7709d1a596c1f41f",
|
| 297 |
+
"95aa82ca6c49ae90cd1668baac7aa6f2b4a8ca99b2c2372acb08cf61c9c3805e6e0328da4cd76a19edd2d3994c798b0022569ad418d1fee4d9cd45a391c601ffc92ad91501432fee150287617c13629e69fc7281cd7165a63eab49cf714bce3a75a74f76ea7e64ff81eb61fdfec39b67bf0de98c7e4e32bdf97c8c6ac75ba43c02f4b2ed7216ecf3014df000108b67cf99505b179f8ed4980a6103d1bca70dbe9bbfab0ed59801d6e5f2d6f67d3ec5168e212e2daf02c6b963c98a1f7097de0c56891a2b211b01070dd8fd8b16c2a1a4e3cfd292d2984b3561d555d16c33ddc2bcf7edde13efe520c7e2abdda44d81881c531aeeeb66244c3b791ea8acfb6a68",
|
| 298 |
+
"55864065117e07650ca650a0f0d9ef4b02aee7c58928462fddb49045bf85355b4653fa26158210a7f3ef5b3ca48612e8b7adf5c025c1b821960af770d935df1c9a1dd25077d6b1c7f937b2e20ce981b07980880214698f3fad72fa370b3b7da257ce1d0cf352bc5304fada3e0f8927bd4e5c1abbffa563bdedcb567daa64faaed748cb361732200ba3506836a3c1c82aafa14c76dc07f6c4277ff2c61325f91fdbd6c1883e745fcaadd5a6d692eeaa5ad56eead6a9d74a595d22757ed89532a4b8831e2b9e2315baea70a9b95d228f09d491a5ed5ab7076766703457e3159bbb9b17b329525669863153079448c68cd2f200c0be9d43061a60639cb59d50993d276c05caaa565db8ce633b2673e4012bebbca02b1a64d779d04066f3e949ece173825885ec816468c819a8129007cc05d8785c48077d09eb1abcba14508dde85a6f16a744bc95faef24888d53a8020515ab20307efaecbdf143a26563c67989bceedc2d6d2bb9699bb6c615d93767e4158c1124e3b6c723aaa47796e59a60d3696cd85adfae9a62f2c02c22009f80ed494bdc587f31dd892c253b5c6d6b7db078fa72d23474ee54f8144d6561182d71c862941dbc0b2cb37a4d4b23cbad5637e6be901cc73f16d5aec39c60dddee631511e57b47520b61ae1892d2d1bd2b486e30faec892f171b6de98d96108016fac805604761f8e74742b3bb7dc8a290a46bf697c3e4446e6e65832cbae7cf1aaad1",
|
| 299 |
+
},
|
| 300 |
+
{ //key=24, plaintext=512
|
| 301 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c",
|
| 302 |
+
"f3584606472b260e0dd2ebb2",
|
| 303 |
+
"67c6697351ff4aec29cdbaabf2fbe3467cc254f81be8e78d765a2e63339fc99a66320db73158a35a255d051758e95ed4abb2cdc69bb454110e827441213ddc8770e93ea141e1fc673e017e97eadc6b968f385c2aecb03bfb32af3c54ec18db5c021afe43fbfaaa3afb29d1e6053c7c9475d8be6189f95cbba8990f95b1ebf1b305eff700e9a13ae5ca0bcbd0484764bd1f231ea81c7b64c514735ac55e4b79633b706424119e09dcaad4acf21b10af3b33cde3504847155cbb6f2219ba9b7df50be11a1c7f23f829f8a41b13b5ca4ee8983238e0794d3d34bc5f4e77facb6c05ac86212baa1a55a2be70b5733b045cd33694b3afe2f0e49e4f321549fd824ea90870d4b28a2954489a0abcd50e18a844ac5bf38e4cd72d9b0942e506c433afcda3847f2dadd47647de321cec4ac430f62023856cfbb20704f4ec0bb920ba86c33e05f1ecd96733b79950a3e314d3d934f75ea0f210a8f6059401beb4bc4478fa4969e623d01ada696a7e4c7e5125b34884533a94fb319990325744ee9bbce9e525cf08f5e9e25e5360aad2b2d085fa54d835e8d466826498d9a8877565705a8a3f62802944de7ca5894e5759d351adac869580ec17e485f18c0c66f17cc07cbb22fce466da610b63af62bc83b4692f3affaf271693ac071fb86d11342d8def4f89d4b66335c1c7e4248367d8ed9612ec453902d8e50af89d7709d1a596c1f41f",
|
| 304 |
+
"95aa82ca6c49ae90cd1668baac7aa6f2b4a8ca99b2c2372acb08cf61c9c3805e6e0328da4cd76a19edd2d3994c798b0022569ad418d1fee4d9cd45a391c601ffc92ad91501432fee150287617c13629e69fc7281cd7165a63eab49cf714bce3a75a74f76ea7e64ff81eb61fdfec39b67bf0de98c7e4e32bdf97c8c6ac75ba43c02f4b2ed7216ecf3014df000108b67cf99505b179f8ed4980a6103d1bca70dbe9bbfab0ed59801d6e5f2d6f67d3ec5168e212e2daf02c6b963c98a1f7097de0c56891a2b211b01070dd8fd8b16c2a1a4e3cfd292d2984b3561d555d16c33ddc2bcf7edde13efe520c7e2abdda44d81881c531aeeeb66244c3b791ea8acfb6a68",
|
| 305 |
+
"9daa466c7174dfde72b435fb6041ed7ff8ab8b1b96edb90437c3cc2e7e8a7c2c3629bae3bcaede99ee926ef4c55571e504e1c516975f6c719611c4da74acc23bbc79b3a67491f84d573e0293aa0cf5d775dde93fc466d5babd3e93a6506c0261021ac184f571ab190df83c32b41a67eaaa8dde27c02b08f15cabc75e46d1f9634f32f9233b2cb975386ff3a5e16b6ea2e2e4215cb33beb4de39a861d7f4a02165cd763f8252b2d60ac45d65a70735a8806a8fec3ca9d37c2cdcb21d2bd5c08d350e4bbdfb11dca344b9bee17e71ee0df3449fd9f9581c6b5483843b457534afb4240585f38ac22aa59a68a167fed6f1be0a5b072b2461f16c976b9aa0f5f2f5988818b01faa025ac7788212d92d222f7c14fe6e8f644c8cd117bb8def5a0217dad4f05cbb334ff9ccf819a4a085ed7c19928ddc40edc931b47339f456ccd423b5c0c1cdc96278006b29de945cdceb0737771e14562fff2aba40606f6046da5031647308682060412812317962bb68be3b42876f0905d52da51ec6345677fe86613828f488cc5685a4b973e48babd109a56d1a1effb286133dc2a94b4ada5707d3a7825941fea1a7502693afc7fe5d810bb0050d98aa6b80801e13b563954a35c31f57d5ba1ddb1a2be26426e2fe7bcd13ba183d80ac1c556b7ec2069b01de1450431a1c2e27848e1f5f4af013bce9080aebd2bb0f1de9f7bb460771c266d48ff4cf84a66f82630657db861c032971079",
|
| 306 |
+
},
|
| 307 |
+
{ //key=32, plaintext=512
|
| 308 |
+
"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
|
| 309 |
+
"f3584606472b260e0dd2ebb2",
|
| 310 |
+
"67c6697351ff4aec29cdbaabf2fbe3467cc254f81be8e78d765a2e63339fc99a66320db73158a35a255d051758e95ed4abb2cdc69bb454110e827441213ddc8770e93ea141e1fc673e017e97eadc6b968f385c2aecb03bfb32af3c54ec18db5c021afe43fbfaaa3afb29d1e6053c7c9475d8be6189f95cbba8990f95b1ebf1b305eff700e9a13ae5ca0bcbd0484764bd1f231ea81c7b64c514735ac55e4b79633b706424119e09dcaad4acf21b10af3b33cde3504847155cbb6f2219ba9b7df50be11a1c7f23f829f8a41b13b5ca4ee8983238e0794d3d34bc5f4e77facb6c05ac86212baa1a55a2be70b5733b045cd33694b3afe2f0e49e4f321549fd824ea90870d4b28a2954489a0abcd50e18a844ac5bf38e4cd72d9b0942e506c433afcda3847f2dadd47647de321cec4ac430f62023856cfbb20704f4ec0bb920ba86c33e05f1ecd96733b79950a3e314d3d934f75ea0f210a8f6059401beb4bc4478fa4969e623d01ada696a7e4c7e5125b34884533a94fb319990325744ee9bbce9e525cf08f5e9e25e5360aad2b2d085fa54d835e8d466826498d9a8877565705a8a3f62802944de7ca5894e5759d351adac869580ec17e485f18c0c66f17cc07cbb22fce466da610b63af62bc83b4692f3affaf271693ac071fb86d11342d8def4f89d4b66335c1c7e4248367d8ed9612ec453902d8e50af89d7709d1a596c1f41f",
|
| 311 |
+
"95aa82ca6c49ae90cd1668baac7aa6f2b4a8ca99b2c2372acb08cf61c9c3805e6e0328da4cd76a19edd2d3994c798b0022569ad418d1fee4d9cd45a391c601ffc92ad91501432fee150287617c13629e69fc7281cd7165a63eab49cf714bce3a75a74f76ea7e64ff81eb61fdfec39b67bf0de98c7e4e32bdf97c8c6ac75ba43c02f4b2ed7216ecf3014df000108b67cf99505b179f8ed4980a6103d1bca70dbe9bbfab0ed59801d6e5f2d6f67d3ec5168e212e2daf02c6b963c98a1f7097de0c56891a2b211b01070dd8fd8b16c2a1a4e3cfd292d2984b3561d555d16c33ddc2bcf7edde13efe520c7e2abdda44d81881c531aeeeb66244c3b791ea8acfb6a68",
|
| 312 |
+
"793d34afb982ab70b0e204e1e7243314a19e987d9ab7662f58c3dc6064c9be35667ad53b115c610cfc229f4e5b3e8aae7aac97ce66d1d20b92da3860701b5006dd1385e173e3af7a5a9bb7da85c0434cd55a40fb9c482a0b36f0782846a7f16d05b40a08f0ad9a633f9a1e99e69e6b8039a0f2a91be40f193f4ce3bed1886dab1b0a6112f91503684c1e5afb938b9497166a7147badd1cc19c73e8b9f22e0dcbd18996868d7ad47755e677ee6e6ec87094cab7ee35feb96017c474261ba7391b18a72451e6daa7f38e162358c5d84788c974e614acc362b887c56b756df5aeacdda09b11d35a1f97daaceb5ca1b40a78b6058f7e1d26ad945be6ef74a8e72729f9ab2e3e7dda88d8f803e26e84a34ac07a7cecf5b6be23a4aa1ac6897f23169d894d53369b27673cf2438af9c6b53a2fa412c74dc075c617029e571f4c2951b1cdd63d33765af9d9d20e12430a83784c2bca8603f11521fa97f2e45398b4a385176701c6f416720ca0816bf51a3e0b4c7a28a89f0616a296423760f0f2f471e1def8a2f43956f79790a6b64dfdbb8159236ebd7fe1049e8e005e231e5f1936bfdccbda8cf0cb5116af758dfd6732dfa77ac3e6faf0996c13473292da363f01ddcb6a524dbf1d5d608f57c146173a9b169f979e101fe581f749764fd87119ae301958c8e9a9bfd16249e564ffbb304bc2ca4c34713a20fb858b47c83ce768e04f149884504c0515345631401f829e3259",
|
| 313 |
+
},
|
| 314 |
+
|
| 315 |
+
{ //key=16, plaintext=293
|
| 316 |
+
"0795d80bc7f40f4d41c280271a2e4f7f",
|
| 317 |
+
"ff824c906594aff365d3cb1f",
|
| 318 |
+
"1ad4e74d127f935beee57cff920665babe7ce56227377afe570ba786193ded3412d4812453157f42fafc418c02a746c1232c234a639d49baa8f041c12e2ef540027764568ce49886e0d913e28059a3a485c6eee96337a30b28e4cd5612c2961539fa6bc5de034cbedc5fa15db844013e0bef276e27ca7a4faf47a5c1093bd643354108144454d221b3737e6cb87faac36ed131959babe44af2890cfcc4e23ffa24470e689ce0894f5407bb0c8665cff536008ad2ac6f1c9ef8289abd0bd9b72f21c597bda5210cf928c805af2dd4a464d52e36819d521f967bba5386930ab5b4cf4c71746d7e6e964673457348e9d71d170d9eb560bd4bdb779e610ba816bf776231ebd0af5966f5cdab6815944032ab4dd060ad8dab880549e910f1ffcf6862005432afad",
|
| 319 |
+
"98a47a430d8fd74dc1829a91e3481f8ed024d8ba34c9b903321b04864db333e558ae28653dffb2",
|
| 320 |
+
"3b8f91443480e647473a0a0b03d571c622b7e70e4309a02c9bb7980053010d865e6aec161354dc9f481b2cd5213e09432b57ec4e58fbd0a8549dd15c8c4e74a6529f75fad0ce5a9e20e2beeb2f91eb638bf88999968de438d2f1cedbfb0a1c81f9e8e7362c738e0fddd963692a4f4df9276b7f040979ce874cf6fa3de26da0713784bdb25e4efcb840554ef5b38b5fe8380549a496bd8e423a7456df6f4ae78a07ebe2276a8e22fc2243ec4f78abe0c99c733fd67c8c492699fa5ee2289cdd0a8d469bf883520ee74efb854bfadc7366a49ee65ca4e894e3335e2b672618d362eee12a577dd8dc2ba55c49c1fc3ad68180e9b112d0234d4aa28f5661f1e036450ca6f18be0166676bd80f8a4890c6ddea306fabb7ff3cb2860aa32a827e3a312912a2dfa70f6bc1c07de238448f2d751bd0cf15bf7",
|
| 321 |
+
},
|
| 322 |
+
{ //key=24, plaintext=293
|
| 323 |
+
"e2e001a36c60d2bf40d69ff5b2b1161ea218db263be16a4e",
|
| 324 |
+
"84230643130d05425826641e",
|
| 325 |
+
"adb034f3f4a7ca45e2993812d113a9821d50df151af978bccc6d3bc113e15bc0918fb385377dca1916022ce816d56a332649484043c0fc0f2d37d040182b00a9bbb42ef231f80b48fb3730110d9a4433e38c73264c703579a705b9c031b969ec6d98de9f90e9e78b21179c2eb1e061946cd4bbb844f031ecf6eaac27a4151311adf1b03eda97c9fbae66295f468af4b35faf6ba39f9d8f95873bbc2b51cf3dfec0ed3c9b850696336cc093b24a8765a936d14dd56edc6bf518272169f75e67b74ba452d0aae90416a997c8f31e2e9d54ffea296dc69462debc8347b3e1af6a2d53bdfdfda601134f98db42b609df0a08c9347590c8d86e845bb6373d65a26ab85f67b50569c85401a396b8ad76c2b53ff62bcfbf033e435ef47b9b591d05117c6dc681d68e",
|
| 326 |
+
"d5d7316b8fdee152942148bff007c22e4b2022c6bc7be3c18c5f2e52e004e0b5dc12206bf002bd",
|
| 327 |
+
"f2c39423ee630dfe961da81909159dba018ce09b1073a12a477108316af5b7a31f86be6a0548b572d604bd115ea737dde899e0bd7f7ac9b23e38910dc457551ecc15c814a9f46d8432a1a36097dc1afe2712d1ba0838fa88cb55d9f65a2e9bece0dbf8999562503989041a2c87d7eb80ef649769d2f4978ce5cf9664f2bd0849646aa81cb976e45e1ade2f17a8126219e917aadbb4bae5e2c4b3f57bbc7f13fcc807df7842d9727a1b389e0b749e5191482adacabd812627c6eae2c7a30caf0844ad2a22e08f39edddf0ae10413e47db433dfe3febbb5a5cec9ade21fbba1e548247579395880b747669a8eb7e2ec0c1bff7fed2defdb92b07a14edf07b1bde29c31ab052ff1214e6b5ebbefcb8f21b5d6f8f6e07ee57ad6e14d4e142cb3f51bb465ab3a28a2a12f01b7514ad0463f2bde0d71d221",
|
| 328 |
+
},
|
| 329 |
+
{ //key=32, plaintext=293
|
| 330 |
+
"5394e890d37ba55ec9d5f327f15680f6a63ef5279c79331643ad0af6d2623525",
|
| 331 |
+
"815e840b7aca7af3b324583f",
|
| 332 |
+
"8e63067cd15359f796b43c68f093f55fdf3589fc5f2fdfad5f9d156668a617f7091d73da71cdd207810e6f71a165d0809a597df9885ca6e8f9bb4e616166586b83cc45f49917fc1a256b8bc7d05c476ab5c4633e20092619c4747b26dad3915e9fd65238ee4e5213badeda8a3a22f5efe6582d0762532026c89b4ca26fdd000eb45347a2a199b55b7790e6b1b2dba19833ce9f9522c0bcea5b088ccae68dd99ae0203c81b9f1dd3181c3e2339e83ccd1526b67742b235e872bea5111772aab574ae7d904d9b6355a79178e179b5ae8edc54f61f172bf789ea9c9af21f45b783e4251421b077776808f04972a5e801723cf781442378ce0e0568f014aea7a882dcbcb48d342be53d1c2ebfb206b12443a8a587cc1e55ca23beca385d61d0d03e9d84cbc1b0a",
|
| 333 |
+
"0feccdfae8ed65fa31a0858a1c466f79e8aa658c2f3ba93c3f92158b4e30955e1c62580450beff",
|
| 334 |
+
"b69a7e17bb5af688883274550a4ded0d1aff49a0b18343f4b382f745c163f7f714c9206a32a1ff012427e19431951edd0a755e5f491b0eedfd7df68bbc6085dd2888607a2f998c3e881eb1694109250db28291e71f4ad344a125624fb92e16ea9815047cd1111cabfdc9cb8c3b4b0f40aa91d31774009781231400789ed545404af6c3f76d07ddc984a7bd8f52728159782832e298cc4d529be96d17be898efd83e44dc7b0e2efc645849fd2bba61fef0ae7be0dcab233cc4e2b7ba4e887de9c64b97f2a1818aa54371a8d629dae37975f7784e5e3cc77055ed6e975b1e5f55e6bbacdc9f295ce4ada2c16113cd5b323cf78b7dde39f4a87aa8c141a31174e3584ccbd380cf5ec6d1dba539928b084fa9683e9c0953acf47cc3ac384a2c38914f1da01fb2cfd78905c2b58d36b2574b9df15535d82",
|
| 335 |
+
},
|
| 336 |
+
// These cases test non-standard tag sizes.
|
| 337 |
+
{
|
| 338 |
+
"89c54b0d3bc3c397d5039058c220685f",
|
| 339 |
+
"bc7f45c00868758d62d4bb4d",
|
| 340 |
+
"582670b0baf5540a3775b6615605bd05",
|
| 341 |
+
"48d16cda0337105a50e2ed76fd18e114",
|
| 342 |
+
"fc2d4c4eee2209ddbba6663c02765e6955e783b00156f5da0446e2970b877f",
|
| 343 |
+
},
|
| 344 |
+
{
|
| 345 |
+
"bad6049678bf75c9087b3e3ae7e72c13",
|
| 346 |
+
"a0a017b83a67d8f1b883e561",
|
| 347 |
+
"a1be93012f05a1958440f74a5311f4a1",
|
| 348 |
+
"f7c27b51d5367161dc2ff1e9e3edc6f2",
|
| 349 |
+
"36f032f7e3dc3275ca22aedcdc68436b99a2227f8bb69d45ea5d8842cd08",
|
| 350 |
+
},
|
| 351 |
+
{
|
| 352 |
+
"66a3c722ccf9709525650973ecc100a9",
|
| 353 |
+
"1621d42d3a6d42a2d2bf9494",
|
| 354 |
+
"61fa9dbbed2190fbc2ffabf5d2ea4ff8",
|
| 355 |
+
"d7a9b6523b8827068a6354a6d166c6b9",
|
| 356 |
+
"fef3b20f40e08a49637cc82f4c89b8603fd5c0132acfab97b5fff651c4",
|
| 357 |
+
},
|
| 358 |
+
{
|
| 359 |
+
"562ae8aadb8d23e0f271a99a7d1bd4d1",
|
| 360 |
+
"f7a5e2399413b89b6ad31aff",
|
| 361 |
+
"bbdc3504d803682aa08a773cde5f231a",
|
| 362 |
+
"2b9680b886b3efb7c6354b38c63b5373",
|
| 363 |
+
"e2b7e5ed5ff27fc8664148f5a628a46dcbf2015184fffb82f2651c36",
|
| 364 |
+
},
|
| 365 |
+
{
|
| 366 |
+
"11754cd72aec309bf52f7687212e8957",
|
| 367 |
+
"",
|
| 368 |
+
"",
|
| 369 |
+
"",
|
| 370 |
+
"250327c674aaf477aef2675748cf6971",
|
| 371 |
+
},
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
+
func TestAESGCM(t *testing.T) {
|
| 375 |
+
for i, test := range aesGCMTests {
|
| 376 |
+
key, _ := hex.DecodeString(test.key)
|
| 377 |
+
aes, err := aes.NewCipher(key)
|
| 378 |
+
if err != nil {
|
| 379 |
+
t.Fatal(err)
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
nonce, _ := hex.DecodeString(test.nonce)
|
| 383 |
+
plaintext, _ := hex.DecodeString(test.plaintext)
|
| 384 |
+
ad, _ := hex.DecodeString(test.ad)
|
| 385 |
+
tagSize := (len(test.result) - len(test.plaintext)) / 2
|
| 386 |
+
|
| 387 |
+
var aesgcm cipher.AEAD
|
| 388 |
+
switch {
|
| 389 |
+
// Handle non-standard tag sizes
|
| 390 |
+
case tagSize != 16:
|
| 391 |
+
aesgcm, err = cipher.NewGCMWithTagSize(aes, tagSize)
|
| 392 |
+
if err != nil {
|
| 393 |
+
t.Fatal(err)
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
// Handle 0 nonce size (expect error and continue)
|
| 397 |
+
case len(nonce) == 0:
|
| 398 |
+
aesgcm, err = cipher.NewGCMWithNonceSize(aes, 0)
|
| 399 |
+
if err == nil {
|
| 400 |
+
t.Fatal("expected error for zero nonce size")
|
| 401 |
+
}
|
| 402 |
+
continue
|
| 403 |
+
|
| 404 |
+
// Handle non-standard nonce sizes
|
| 405 |
+
case len(nonce) != 12:
|
| 406 |
+
aesgcm, err = cipher.NewGCMWithNonceSize(aes, len(nonce))
|
| 407 |
+
if err != nil {
|
| 408 |
+
t.Fatal(err)
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
default:
|
| 412 |
+
aesgcm, err = cipher.NewGCM(aes)
|
| 413 |
+
if err != nil {
|
| 414 |
+
t.Fatal(err)
|
| 415 |
+
}
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
ct := aesgcm.Seal(nil, nonce, plaintext, ad)
|
| 419 |
+
if ctHex := hex.EncodeToString(ct); ctHex != test.result {
|
| 420 |
+
t.Errorf("#%d: got %s, want %s", i, ctHex, test.result)
|
| 421 |
+
continue
|
| 422 |
+
}
|
| 423 |
+
|
| 424 |
+
plaintext2, err := aesgcm.Open(nil, nonce, ct, ad)
|
| 425 |
+
if err != nil {
|
| 426 |
+
t.Errorf("#%d: Open failed", i)
|
| 427 |
+
continue
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
if !bytes.Equal(plaintext, plaintext2) {
|
| 431 |
+
t.Errorf("#%d: plaintext's don't match: got %x vs %x", i, plaintext2, plaintext)
|
| 432 |
+
continue
|
| 433 |
+
}
|
| 434 |
+
|
| 435 |
+
if len(ad) > 0 {
|
| 436 |
+
ad[0] ^= 0x80
|
| 437 |
+
if _, err := aesgcm.Open(nil, nonce, ct, ad); err == nil {
|
| 438 |
+
t.Errorf("#%d: Open was successful after altering additional data", i)
|
| 439 |
+
}
|
| 440 |
+
ad[0] ^= 0x80
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
nonce[0] ^= 0x80
|
| 444 |
+
if _, err := aesgcm.Open(nil, nonce, ct, ad); err == nil {
|
| 445 |
+
t.Errorf("#%d: Open was successful after altering nonce", i)
|
| 446 |
+
}
|
| 447 |
+
nonce[0] ^= 0x80
|
| 448 |
+
|
| 449 |
+
ct[0] ^= 0x80
|
| 450 |
+
if _, err := aesgcm.Open(nil, nonce, ct, ad); err == nil {
|
| 451 |
+
t.Errorf("#%d: Open was successful after altering ciphertext", i)
|
| 452 |
+
}
|
| 453 |
+
ct[0] ^= 0x80
|
| 454 |
+
}
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
func TestGCMInvalidTagSize(t *testing.T) {
|
| 458 |
+
key, _ := hex.DecodeString("ab72c77b97cb5fe9a382d9fe81ffdbed")
|
| 459 |
+
|
| 460 |
+
aes, _ := aes.NewCipher(key)
|
| 461 |
+
|
| 462 |
+
for _, tagSize := range []int{0, 1, aes.BlockSize() + 1} {
|
| 463 |
+
aesgcm, err := cipher.NewGCMWithTagSize(aes, tagSize)
|
| 464 |
+
if aesgcm != nil || err == nil {
|
| 465 |
+
t.Fatalf("NewGCMWithNonceAndTagSize was successful with an invalid %d-byte tag size", tagSize)
|
| 466 |
+
}
|
| 467 |
+
}
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
func TestTagFailureOverwrite(t *testing.T) {
|
| 471 |
+
// The AESNI GCM code decrypts and authenticates concurrently and so
|
| 472 |
+
// overwrites the output buffer before checking the authentication tag.
|
| 473 |
+
// In order to be consistent across platforms, all implementations
|
| 474 |
+
// should do this and this test checks that.
|
| 475 |
+
|
| 476 |
+
key, _ := hex.DecodeString("ab72c77b97cb5fe9a382d9fe81ffdbed")
|
| 477 |
+
nonce, _ := hex.DecodeString("54cc7dc2c37ec006bcc6d1db")
|
| 478 |
+
ciphertext, _ := hex.DecodeString("0e1bde206a07a9c2c1b65300f8c649972b4401346697138c7a4891ee59867d0c")
|
| 479 |
+
|
| 480 |
+
aes, _ := aes.NewCipher(key)
|
| 481 |
+
aesgcm, _ := cipher.NewGCM(aes)
|
| 482 |
+
|
| 483 |
+
dst := make([]byte, len(ciphertext)-16)
|
| 484 |
+
for i := range dst {
|
| 485 |
+
dst[i] = 42
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
result, err := aesgcm.Open(dst[:0], nonce, ciphertext, nil)
|
| 489 |
+
if err == nil {
|
| 490 |
+
t.Fatal("Bad Open still resulted in nil error.")
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
if result != nil {
|
| 494 |
+
t.Fatal("Failed Open returned non-nil result.")
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
for i := range dst {
|
| 498 |
+
if dst[i] != 0 {
|
| 499 |
+
t.Fatal("Failed Open didn't zero dst buffer")
|
| 500 |
+
}
|
| 501 |
+
}
|
| 502 |
+
}
|
| 503 |
+
|
| 504 |
+
func TestGCMCounterWrap(t *testing.T) {
|
| 505 |
+
// Test that the last 32-bits of the counter wrap correctly.
|
| 506 |
+
tests := []struct {
|
| 507 |
+
nonce, tag string
|
| 508 |
+
}{
|
| 509 |
+
{"0fa72e25", "37e1948cdfff09fbde0c40ad99fee4a7"}, // counter: 7eb59e4d961dad0dfdd75aaffffffff0
|
| 510 |
+
{"afe05cc1", "438f3aa9fee5e54903b1927bca26bbdf"}, // counter: 75d492a7e6e6bfc979ad3a8ffffffff4
|
| 511 |
+
{"9ffecbef", "7b88ca424df9703e9e8611071ec7e16e"}, // counter: c8bb108b0ecdc71747b9d57ffffffff5
|
| 512 |
+
{"ffc3e5b3", "38d49c86e0abe853ac250e66da54c01a"}, // counter: 706414d2de9b36ab3b900a9ffffffff6
|
| 513 |
+
{"cfdd729d", "e08402eaac36a1a402e09b1bd56500e8"}, // counter: cd0b96fe36b04e750584e56ffffffff7
|
| 514 |
+
{"010ae3d486", "5405bb490b1f95d01e2ba735687154bc"}, // counter: e36c18e69406c49722808104fffffff8
|
| 515 |
+
{"01b1107a9d", "939a585f342e01e17844627492d44dbf"}, // counter: e6d56eaf9127912b6d62c6dcffffffff
|
| 516 |
+
}
|
| 517 |
+
key, err := aes.NewCipher(make([]byte, 16))
|
| 518 |
+
if err != nil {
|
| 519 |
+
t.Fatal(err)
|
| 520 |
+
}
|
| 521 |
+
plaintext := make([]byte, 16*17+1)
|
| 522 |
+
for i, test := range tests {
|
| 523 |
+
nonce, _ := hex.DecodeString(test.nonce)
|
| 524 |
+
want, _ := hex.DecodeString(test.tag)
|
| 525 |
+
aead, err := cipher.NewGCMWithNonceSize(key, len(nonce))
|
| 526 |
+
if err != nil {
|
| 527 |
+
t.Fatal(err)
|
| 528 |
+
}
|
| 529 |
+
got := aead.Seal(nil, nonce, plaintext, nil)
|
| 530 |
+
if !bytes.Equal(got[len(plaintext):], want) {
|
| 531 |
+
t.Errorf("test[%v]: got: %x, want: %x", i, got[len(plaintext):], want)
|
| 532 |
+
}
|
| 533 |
+
_, err = aead.Open(nil, nonce, got, nil)
|
| 534 |
+
if err != nil {
|
| 535 |
+
t.Errorf("test[%v]: authentication failed", i)
|
| 536 |
+
}
|
| 537 |
+
}
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
var _ cipher.Block = (*wrapper)(nil)
|
| 541 |
+
|
| 542 |
+
type wrapper struct {
|
| 543 |
+
block cipher.Block
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
func (w *wrapper) BlockSize() int { return w.block.BlockSize() }
|
| 547 |
+
func (w *wrapper) Encrypt(dst, src []byte) { w.block.Encrypt(dst, src) }
|
| 548 |
+
func (w *wrapper) Decrypt(dst, src []byte) { w.block.Decrypt(dst, src) }
|
| 549 |
+
|
| 550 |
+
// wrap wraps the Block interface so that it does not fulfill
|
| 551 |
+
// any optimizing interfaces such as gcmAble.
|
| 552 |
+
func wrap(b cipher.Block) cipher.Block {
|
| 553 |
+
return &wrapper{b}
|
| 554 |
+
}
|
| 555 |
+
|
| 556 |
+
func TestGCMAsm(t *testing.T) {
|
| 557 |
+
// Create a new pair of AEADs, one using the assembly implementation
|
| 558 |
+
// and one using the generic Go implementation.
|
| 559 |
+
newAESGCM := func(key []byte) (asm, generic cipher.AEAD, err error) {
|
| 560 |
+
block, err := aes.NewCipher(key[:])
|
| 561 |
+
if err != nil {
|
| 562 |
+
return nil, nil, err
|
| 563 |
+
}
|
| 564 |
+
asm, err = cipher.NewGCM(block)
|
| 565 |
+
if err != nil {
|
| 566 |
+
return nil, nil, err
|
| 567 |
+
}
|
| 568 |
+
generic, err = cipher.NewGCM(wrap(block))
|
| 569 |
+
if err != nil {
|
| 570 |
+
return nil, nil, err
|
| 571 |
+
}
|
| 572 |
+
return asm, generic, nil
|
| 573 |
+
}
|
| 574 |
+
|
| 575 |
+
// check for assembly implementation
|
| 576 |
+
var key [16]byte
|
| 577 |
+
asm, generic, err := newAESGCM(key[:])
|
| 578 |
+
if err != nil {
|
| 579 |
+
t.Fatal(err)
|
| 580 |
+
}
|
| 581 |
+
if reflect.TypeOf(asm) == reflect.TypeOf(generic) {
|
| 582 |
+
t.Skipf("no assembly implementation of GCM")
|
| 583 |
+
}
|
| 584 |
+
|
| 585 |
+
// generate permutations
|
| 586 |
+
type pair struct{ align, length int }
|
| 587 |
+
lengths := []int{0, 156, 8192, 8193, 8208}
|
| 588 |
+
keySizes := []int{16, 24, 32}
|
| 589 |
+
alignments := []int{0, 1, 2, 3}
|
| 590 |
+
if testing.Short() {
|
| 591 |
+
keySizes = []int{16}
|
| 592 |
+
alignments = []int{1}
|
| 593 |
+
}
|
| 594 |
+
perms := make([]pair, 0)
|
| 595 |
+
for _, l := range lengths {
|
| 596 |
+
for _, a := range alignments {
|
| 597 |
+
if a != 0 && l == 0 {
|
| 598 |
+
continue
|
| 599 |
+
}
|
| 600 |
+
perms = append(perms, pair{align: a, length: l})
|
| 601 |
+
}
|
| 602 |
+
}
|
| 603 |
+
|
| 604 |
+
// run test for all permutations
|
| 605 |
+
test := func(ks int, pt, ad []byte) error {
|
| 606 |
+
key := make([]byte, ks)
|
| 607 |
+
if _, err := io.ReadFull(rand.Reader, key); err != nil {
|
| 608 |
+
return err
|
| 609 |
+
}
|
| 610 |
+
asm, generic, err := newAESGCM(key)
|
| 611 |
+
if err != nil {
|
| 612 |
+
return err
|
| 613 |
+
}
|
| 614 |
+
if _, err := io.ReadFull(rand.Reader, pt); err != nil {
|
| 615 |
+
return err
|
| 616 |
+
}
|
| 617 |
+
if _, err := io.ReadFull(rand.Reader, ad); err != nil {
|
| 618 |
+
return err
|
| 619 |
+
}
|
| 620 |
+
nonce := make([]byte, 12)
|
| 621 |
+
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
| 622 |
+
return err
|
| 623 |
+
}
|
| 624 |
+
want := generic.Seal(nil, nonce, pt, ad)
|
| 625 |
+
got := asm.Seal(nil, nonce, pt, ad)
|
| 626 |
+
if !bytes.Equal(want, got) {
|
| 627 |
+
return errors.New("incorrect Seal output")
|
| 628 |
+
}
|
| 629 |
+
got, err = asm.Open(nil, nonce, want, ad)
|
| 630 |
+
if err != nil {
|
| 631 |
+
return errors.New("authentication failed")
|
| 632 |
+
}
|
| 633 |
+
if !bytes.Equal(pt, got) {
|
| 634 |
+
return errors.New("incorrect Open output")
|
| 635 |
+
}
|
| 636 |
+
return nil
|
| 637 |
+
}
|
| 638 |
+
for _, a := range perms {
|
| 639 |
+
ad := make([]byte, a.align+a.length)
|
| 640 |
+
ad = ad[a.align:]
|
| 641 |
+
for _, p := range perms {
|
| 642 |
+
pt := make([]byte, p.align+p.length)
|
| 643 |
+
pt = pt[p.align:]
|
| 644 |
+
for _, ks := range keySizes {
|
| 645 |
+
if err := test(ks, pt, ad); err != nil {
|
| 646 |
+
t.Error(err)
|
| 647 |
+
t.Errorf(" key size: %v", ks)
|
| 648 |
+
t.Errorf(" plaintext alignment: %v", p.align)
|
| 649 |
+
t.Errorf(" plaintext length: %v", p.length)
|
| 650 |
+
t.Errorf(" additionalData alignment: %v", a.align)
|
| 651 |
+
t.Fatalf(" additionalData length: %v", a.length)
|
| 652 |
+
}
|
| 653 |
+
}
|
| 654 |
+
}
|
| 655 |
+
}
|
| 656 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/io.go
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2010 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package cipher
|
| 6 |
+
|
| 7 |
+
import "io"
|
| 8 |
+
|
| 9 |
+
// The Stream* objects are so simple that all their members are public. Users
|
| 10 |
+
// can create them themselves.
|
| 11 |
+
|
| 12 |
+
// StreamReader wraps a [Stream] into an [io.Reader]. It calls XORKeyStream
|
| 13 |
+
// to process each slice of data which passes through.
|
| 14 |
+
type StreamReader struct {
|
| 15 |
+
S Stream
|
| 16 |
+
R io.Reader
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
func (r StreamReader) Read(dst []byte) (n int, err error) {
|
| 20 |
+
n, err = r.R.Read(dst)
|
| 21 |
+
r.S.XORKeyStream(dst[:n], dst[:n])
|
| 22 |
+
return
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
// StreamWriter wraps a [Stream] into an io.Writer. It calls XORKeyStream
|
| 26 |
+
// to process each slice of data which passes through. If any [StreamWriter.Write]
|
| 27 |
+
// call returns short then the StreamWriter is out of sync and must be discarded.
|
| 28 |
+
// A StreamWriter has no internal buffering; [StreamWriter.Close] does not need
|
| 29 |
+
// to be called to flush write data.
|
| 30 |
+
type StreamWriter struct {
|
| 31 |
+
S Stream
|
| 32 |
+
W io.Writer
|
| 33 |
+
Err error // unused
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
func (w StreamWriter) Write(src []byte) (n int, err error) {
|
| 37 |
+
c := make([]byte, len(src))
|
| 38 |
+
w.S.XORKeyStream(c, src)
|
| 39 |
+
n, err = w.W.Write(c)
|
| 40 |
+
if n != len(src) && err == nil { // should never happen
|
| 41 |
+
err = io.ErrShortWrite
|
| 42 |
+
}
|
| 43 |
+
return
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// Close closes the underlying Writer and returns its Close return value, if the Writer
|
| 47 |
+
// is also an io.Closer. Otherwise it returns nil.
|
| 48 |
+
func (w StreamWriter) Close() error {
|
| 49 |
+
if c, ok := w.W.(io.Closer); ok {
|
| 50 |
+
return c.Close()
|
| 51 |
+
}
|
| 52 |
+
return nil
|
| 53 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/ofb.go
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2011 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// OFB (Output Feedback) Mode.
|
| 6 |
+
|
| 7 |
+
package cipher
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"crypto/internal/alias"
|
| 11 |
+
"crypto/subtle"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
type ofb struct {
|
| 15 |
+
b Block
|
| 16 |
+
cipher []byte
|
| 17 |
+
out []byte
|
| 18 |
+
outUsed int
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
// NewOFB returns a [Stream] that encrypts or decrypts using the block cipher b
|
| 22 |
+
// in output feedback mode. The initialization vector iv's length must be equal
|
| 23 |
+
// to b's block size.
|
| 24 |
+
func NewOFB(b Block, iv []byte) Stream {
|
| 25 |
+
blockSize := b.BlockSize()
|
| 26 |
+
if len(iv) != blockSize {
|
| 27 |
+
panic("cipher.NewOFB: IV length must equal block size")
|
| 28 |
+
}
|
| 29 |
+
bufSize := streamBufferSize
|
| 30 |
+
if bufSize < blockSize {
|
| 31 |
+
bufSize = blockSize
|
| 32 |
+
}
|
| 33 |
+
x := &ofb{
|
| 34 |
+
b: b,
|
| 35 |
+
cipher: make([]byte, blockSize),
|
| 36 |
+
out: make([]byte, 0, bufSize),
|
| 37 |
+
outUsed: 0,
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
copy(x.cipher, iv)
|
| 41 |
+
return x
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
func (x *ofb) refill() {
|
| 45 |
+
bs := x.b.BlockSize()
|
| 46 |
+
remain := len(x.out) - x.outUsed
|
| 47 |
+
if remain > x.outUsed {
|
| 48 |
+
return
|
| 49 |
+
}
|
| 50 |
+
copy(x.out, x.out[x.outUsed:])
|
| 51 |
+
x.out = x.out[:cap(x.out)]
|
| 52 |
+
for remain < len(x.out)-bs {
|
| 53 |
+
x.b.Encrypt(x.cipher, x.cipher)
|
| 54 |
+
copy(x.out[remain:], x.cipher)
|
| 55 |
+
remain += bs
|
| 56 |
+
}
|
| 57 |
+
x.out = x.out[:remain]
|
| 58 |
+
x.outUsed = 0
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
func (x *ofb) XORKeyStream(dst, src []byte) {
|
| 62 |
+
if len(dst) < len(src) {
|
| 63 |
+
panic("crypto/cipher: output smaller than input")
|
| 64 |
+
}
|
| 65 |
+
if alias.InexactOverlap(dst[:len(src)], src) {
|
| 66 |
+
panic("crypto/cipher: invalid buffer overlap")
|
| 67 |
+
}
|
| 68 |
+
for len(src) > 0 {
|
| 69 |
+
if x.outUsed >= len(x.out)-x.b.BlockSize() {
|
| 70 |
+
x.refill()
|
| 71 |
+
}
|
| 72 |
+
n := subtle.XORBytes(dst, src, x.out[x.outUsed:])
|
| 73 |
+
dst = dst[n:]
|
| 74 |
+
src = src[n:]
|
| 75 |
+
x.outUsed += n
|
| 76 |
+
}
|
| 77 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/cipher/ofb_test.go
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2009 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// OFB AES test vectors.
|
| 6 |
+
|
| 7 |
+
// See U.S. National Institute of Standards and Technology (NIST)
|
| 8 |
+
// Special Publication 800-38A, ``Recommendation for Block Cipher
|
| 9 |
+
// Modes of Operation,'' 2001 Edition, pp. 52-55.
|
| 10 |
+
|
| 11 |
+
package cipher_test
|
| 12 |
+
|
| 13 |
+
import (
|
| 14 |
+
"bytes"
|
| 15 |
+
"crypto/aes"
|
| 16 |
+
"crypto/cipher"
|
| 17 |
+
"testing"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
type ofbTest struct {
|
| 21 |
+
name string
|
| 22 |
+
key []byte
|
| 23 |
+
iv []byte
|
| 24 |
+
in []byte
|
| 25 |
+
out []byte
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
var ofbTests = []ofbTest{
|
| 29 |
+
// NIST SP 800-38A pp 52-55
|
| 30 |
+
{
|
| 31 |
+
"OFB-AES128",
|
| 32 |
+
commonKey128,
|
| 33 |
+
commonIV,
|
| 34 |
+
commonInput,
|
| 35 |
+
[]byte{
|
| 36 |
+
0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a,
|
| 37 |
+
0x77, 0x89, 0x50, 0x8d, 0x16, 0x91, 0x8f, 0x03, 0xf5, 0x3c, 0x52, 0xda, 0xc5, 0x4e, 0xd8, 0x25,
|
| 38 |
+
0x97, 0x40, 0x05, 0x1e, 0x9c, 0x5f, 0xec, 0xf6, 0x43, 0x44, 0xf7, 0xa8, 0x22, 0x60, 0xed, 0xcc,
|
| 39 |
+
0x30, 0x4c, 0x65, 0x28, 0xf6, 0x59, 0xc7, 0x78, 0x66, 0xa5, 0x10, 0xd9, 0xc1, 0xd6, 0xae, 0x5e,
|
| 40 |
+
},
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"OFB-AES192",
|
| 44 |
+
commonKey192,
|
| 45 |
+
commonIV,
|
| 46 |
+
commonInput,
|
| 47 |
+
[]byte{
|
| 48 |
+
0xcd, 0xc8, 0x0d, 0x6f, 0xdd, 0xf1, 0x8c, 0xab, 0x34, 0xc2, 0x59, 0x09, 0xc9, 0x9a, 0x41, 0x74,
|
| 49 |
+
0xfc, 0xc2, 0x8b, 0x8d, 0x4c, 0x63, 0x83, 0x7c, 0x09, 0xe8, 0x17, 0x00, 0xc1, 0x10, 0x04, 0x01,
|
| 50 |
+
0x8d, 0x9a, 0x9a, 0xea, 0xc0, 0xf6, 0x59, 0x6f, 0x55, 0x9c, 0x6d, 0x4d, 0xaf, 0x59, 0xa5, 0xf2,
|
| 51 |
+
0x6d, 0x9f, 0x20, 0x08, 0x57, 0xca, 0x6c, 0x3e, 0x9c, 0xac, 0x52, 0x4b, 0xd9, 0xac, 0xc9, 0x2a,
|
| 52 |
+
},
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"OFB-AES256",
|
| 56 |
+
commonKey256,
|
| 57 |
+
commonIV,
|
| 58 |
+
commonInput,
|
| 59 |
+
[]byte{
|
| 60 |
+
0xdc, 0x7e, 0x84, 0xbf, 0xda, 0x79, 0x16, 0x4b, 0x7e, 0xcd, 0x84, 0x86, 0x98, 0x5d, 0x38, 0x60,
|
| 61 |
+
0x4f, 0xeb, 0xdc, 0x67, 0x40, 0xd2, 0x0b, 0x3a, 0xc8, 0x8f, 0x6a, 0xd8, 0x2a, 0x4f, 0xb0, 0x8d,
|
| 62 |
+
0x71, 0xab, 0x47, 0xa0, 0x86, 0xe8, 0x6e, 0xed, 0xf3, 0x9d, 0x1c, 0x5b, 0xba, 0x97, 0xc4, 0x08,
|
| 63 |
+
0x01, 0x26, 0x14, 0x1d, 0x67, 0xf3, 0x7b, 0xe8, 0x53, 0x8f, 0x5a, 0x8b, 0xe7, 0x40, 0xe4, 0x84,
|
| 64 |
+
},
|
| 65 |
+
},
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
func TestOFB(t *testing.T) {
|
| 69 |
+
for _, tt := range ofbTests {
|
| 70 |
+
test := tt.name
|
| 71 |
+
|
| 72 |
+
c, err := aes.NewCipher(tt.key)
|
| 73 |
+
if err != nil {
|
| 74 |
+
t.Errorf("%s: NewCipher(%d bytes) = %s", test, len(tt.key), err)
|
| 75 |
+
continue
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
for j := 0; j <= 5; j += 5 {
|
| 79 |
+
plaintext := tt.in[0 : len(tt.in)-j]
|
| 80 |
+
ofb := cipher.NewOFB(c, tt.iv)
|
| 81 |
+
ciphertext := make([]byte, len(plaintext))
|
| 82 |
+
ofb.XORKeyStream(ciphertext, plaintext)
|
| 83 |
+
if !bytes.Equal(ciphertext, tt.out[:len(plaintext)]) {
|
| 84 |
+
t.Errorf("%s/%d: encrypting\ninput % x\nhave % x\nwant % x", test, len(plaintext), plaintext, ciphertext, tt.out)
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
for j := 0; j <= 5; j += 5 {
|
| 89 |
+
ciphertext := tt.out[0 : len(tt.in)-j]
|
| 90 |
+
ofb := cipher.NewOFB(c, tt.iv)
|
| 91 |
+
plaintext := make([]byte, len(ciphertext))
|
| 92 |
+
ofb.XORKeyStream(plaintext, ciphertext)
|
| 93 |
+
if !bytes.Equal(plaintext, tt.in[:len(ciphertext)]) {
|
| 94 |
+
t.Errorf("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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2011 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package des
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"encoding/binary"
|
| 9 |
+
"sync"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
func cryptBlock(subkeys []uint64, dst, src []byte, decrypt bool) {
|
| 13 |
+
b := binary.BigEndian.Uint64(src)
|
| 14 |
+
b = permuteInitialBlock(b)
|
| 15 |
+
left, right := uint32(b>>32), uint32(b)
|
| 16 |
+
|
| 17 |
+
left = (left << 1) | (left >> 31)
|
| 18 |
+
right = (right << 1) | (right >> 31)
|
| 19 |
+
|
| 20 |
+
if decrypt {
|
| 21 |
+
for i := 0; i < 8; i++ {
|
| 22 |
+
left, right = feistel(left, right, subkeys[15-2*i], subkeys[15-(2*i+1)])
|
| 23 |
+
}
|
| 24 |
+
} else {
|
| 25 |
+
for i := 0; i < 8; i++ {
|
| 26 |
+
left, right = feistel(left, right, subkeys[2*i], subkeys[2*i+1])
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
left = (left << 31) | (left >> 1)
|
| 31 |
+
right = (right << 31) | (right >> 1)
|
| 32 |
+
|
| 33 |
+
// switch left & right and perform final permutation
|
| 34 |
+
preOutput := (uint64(right) << 32) | uint64(left)
|
| 35 |
+
binary.BigEndian.PutUint64(dst, permuteFinalBlock(preOutput))
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// DES Feistel function. feistelBox must be initialized via
|
| 39 |
+
// feistelBoxOnce.Do(initFeistelBox) first.
|
| 40 |
+
func feistel(l, r uint32, k0, k1 uint64) (lout, rout uint32) {
|
| 41 |
+
var t uint32
|
| 42 |
+
|
| 43 |
+
t = r ^ uint32(k0>>32)
|
| 44 |
+
l ^= feistelBox[7][t&0x3f] ^
|
| 45 |
+
feistelBox[5][(t>>8)&0x3f] ^
|
| 46 |
+
feistelBox[3][(t>>16)&0x3f] ^
|
| 47 |
+
feistelBox[1][(t>>24)&0x3f]
|
| 48 |
+
|
| 49 |
+
t = ((r << 28) | (r >> 4)) ^ uint32(k0)
|
| 50 |
+
l ^= feistelBox[6][(t)&0x3f] ^
|
| 51 |
+
feistelBox[4][(t>>8)&0x3f] ^
|
| 52 |
+
feistelBox[2][(t>>16)&0x3f] ^
|
| 53 |
+
feistelBox[0][(t>>24)&0x3f]
|
| 54 |
+
|
| 55 |
+
t = l ^ uint32(k1>>32)
|
| 56 |
+
r ^= feistelBox[7][t&0x3f] ^
|
| 57 |
+
feistelBox[5][(t>>8)&0x3f] ^
|
| 58 |
+
feistelBox[3][(t>>16)&0x3f] ^
|
| 59 |
+
feistelBox[1][(t>>24)&0x3f]
|
| 60 |
+
|
| 61 |
+
t = ((l << 28) | (l >> 4)) ^ uint32(k1)
|
| 62 |
+
r ^= feistelBox[6][(t)&0x3f] ^
|
| 63 |
+
feistelBox[4][(t>>8)&0x3f] ^
|
| 64 |
+
feistelBox[2][(t>>16)&0x3f] ^
|
| 65 |
+
feistelBox[0][(t>>24)&0x3f]
|
| 66 |
+
|
| 67 |
+
return l, r
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// feistelBox[s][16*i+j] contains the output of permutationFunction
|
| 71 |
+
// for sBoxes[s][i][j] << 4*(7-s)
|
| 72 |
+
var feistelBox [8][64]uint32
|
| 73 |
+
|
| 74 |
+
var feistelBoxOnce sync.Once
|
| 75 |
+
|
| 76 |
+
// general purpose function to perform DES block permutations.
|
| 77 |
+
func permuteBlock(src uint64, permutation []uint8) (block uint64) {
|
| 78 |
+
for position, n := range permutation {
|
| 79 |
+
bit := (src >> n) & 1
|
| 80 |
+
block |= bit << uint((len(permutation)-1)-position)
|
| 81 |
+
}
|
| 82 |
+
return
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
func initFeistelBox() {
|
| 86 |
+
for s := range sBoxes {
|
| 87 |
+
for i := 0; i < 4; i++ {
|
| 88 |
+
for j := 0; j < 16; j++ {
|
| 89 |
+
f := uint64(sBoxes[s][i][j]) << (4 * (7 - uint(s)))
|
| 90 |
+
f = permuteBlock(f, permutationFunction[:])
|
| 91 |
+
|
| 92 |
+
// Row is determined by the 1st and 6th bit.
|
| 93 |
+
// Column is the middle four bits.
|
| 94 |
+
row := uint8(((i & 2) << 4) | i&1)
|
| 95 |
+
col := uint8(j << 1)
|
| 96 |
+
t := row | col
|
| 97 |
+
|
| 98 |
+
// The rotation was performed in the feistel rounds, being factored out and now mixed into the feistelBox.
|
| 99 |
+
f = (f << 1) | (f >> 31)
|
| 100 |
+
|
| 101 |
+
feistelBox[s][t] = uint32(f)
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
// permuteInitialBlock is equivalent to the permutation defined
|
| 108 |
+
// by initialPermutation.
|
| 109 |
+
func permuteInitialBlock(block uint64) uint64 {
|
| 110 |
+
// block = b7 b6 b5 b4 b3 b2 b1 b0 (8 bytes)
|
| 111 |
+
b1 := block >> 48
|
| 112 |
+
b2 := block << 48
|
| 113 |
+
block ^= b1 ^ b2 ^ b1<<48 ^ b2>>48
|
| 114 |
+
|
| 115 |
+
// block = b1 b0 b5 b4 b3 b2 b7 b6
|
| 116 |
+
b1 = block >> 32 & 0xff00ff
|
| 117 |
+
b2 = (block & 0xff00ff00)
|
| 118 |
+
block ^= b1<<32 ^ b2 ^ b1<<8 ^ b2<<24 // exchange b0 b4 with b3 b7
|
| 119 |
+
|
| 120 |
+
// block is now b1 b3 b5 b7 b0 b2 b4 b6, the permutation:
|
| 121 |
+
// ... 8
|
| 122 |
+
// ... 24
|
| 123 |
+
// ... 40
|
| 124 |
+
// ... 56
|
| 125 |
+
// 7 6 5 4 3 2 1 0
|
| 126 |
+
// 23 22 21 20 19 18 17 16
|
| 127 |
+
// ... 32
|
| 128 |
+
// ... 48
|
| 129 |
+
|
| 130 |
+
// exchange 4,5,6,7 with 32,33,34,35 etc.
|
| 131 |
+
b1 = block & 0x0f0f00000f0f0000
|
| 132 |
+
b2 = block & 0x0000f0f00000f0f0
|
| 133 |
+
block ^= b1 ^ b2 ^ b1>>12 ^ b2<<12
|
| 134 |
+
|
| 135 |
+
// block is the permutation:
|
| 136 |
+
//
|
| 137 |
+
// [+8] [+40]
|
| 138 |
+
//
|
| 139 |
+
// 7 6 5 4
|
| 140 |
+
// 23 22 21 20
|
| 141 |
+
// 3 2 1 0
|
| 142 |
+
// 19 18 17 16 [+32]
|
| 143 |
+
|
| 144 |
+
// exchange 0,1,4,5 with 18,19,22,23
|
| 145 |
+
b1 = block & 0x3300330033003300
|
| 146 |
+
b2 = block & 0x00cc00cc00cc00cc
|
| 147 |
+
block ^= b1 ^ b2 ^ b1>>6 ^ b2<<6
|
| 148 |
+
|
| 149 |
+
// block is the permutation:
|
| 150 |
+
// 15 14
|
| 151 |
+
// 13 12
|
| 152 |
+
// 11 10
|
| 153 |
+
// 9 8
|
| 154 |
+
// 7 6
|
| 155 |
+
// 5 4
|
| 156 |
+
// 3 2
|
| 157 |
+
// 1 0 [+16] [+32] [+64]
|
| 158 |
+
|
| 159 |
+
// exchange 0,2,4,6 with 9,11,13,15:
|
| 160 |
+
b1 = block & 0xaaaaaaaa55555555
|
| 161 |
+
block ^= b1 ^ b1>>33 ^ b1<<33
|
| 162 |
+
|
| 163 |
+
// block is the permutation:
|
| 164 |
+
// 6 14 22 30 38 46 54 62
|
| 165 |
+
// 4 12 20 28 36 44 52 60
|
| 166 |
+
// 2 10 18 26 34 42 50 58
|
| 167 |
+
// 0 8 16 24 32 40 48 56
|
| 168 |
+
// 7 15 23 31 39 47 55 63
|
| 169 |
+
// 5 13 21 29 37 45 53 61
|
| 170 |
+
// 3 11 19 27 35 43 51 59
|
| 171 |
+
// 1 9 17 25 33 41 49 57
|
| 172 |
+
return block
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
// permuteFinalBlock is equivalent to the permutation defined
|
| 176 |
+
// by finalPermutation.
|
| 177 |
+
func permuteFinalBlock(block uint64) uint64 {
|
| 178 |
+
// Perform the same bit exchanges as permuteInitialBlock
|
| 179 |
+
// but in reverse order.
|
| 180 |
+
b1 := block & 0xaaaaaaaa55555555
|
| 181 |
+
block ^= b1 ^ b1>>33 ^ b1<<33
|
| 182 |
+
|
| 183 |
+
b1 = block & 0x3300330033003300
|
| 184 |
+
b2 := block & 0x00cc00cc00cc00cc
|
| 185 |
+
block ^= b1 ^ b2 ^ b1>>6 ^ b2<<6
|
| 186 |
+
|
| 187 |
+
b1 = block & 0x0f0f00000f0f0000
|
| 188 |
+
b2 = block & 0x0000f0f00000f0f0
|
| 189 |
+
block ^= b1 ^ b2 ^ b1>>12 ^ b2<<12
|
| 190 |
+
|
| 191 |
+
b1 = block >> 32 & 0xff00ff
|
| 192 |
+
b2 = (block & 0xff00ff00)
|
| 193 |
+
block ^= b1<<32 ^ b2 ^ b1<<8 ^ b2<<24
|
| 194 |
+
|
| 195 |
+
b1 = block >> 48
|
| 196 |
+
b2 = block << 48
|
| 197 |
+
block ^= b1 ^ b2 ^ b1<<48 ^ b2>>48
|
| 198 |
+
return block
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
// creates 16 28-bit blocks rotated according
|
| 202 |
+
// to the rotation schedule.
|
| 203 |
+
func ksRotate(in uint32) (out []uint32) {
|
| 204 |
+
out = make([]uint32, 16)
|
| 205 |
+
last := in
|
| 206 |
+
for i := 0; i < 16; i++ {
|
| 207 |
+
// 28-bit circular left shift
|
| 208 |
+
left := (last << (4 + ksRotations[i])) >> 4
|
| 209 |
+
right := (last << 4) >> (32 - ksRotations[i])
|
| 210 |
+
out[i] = left | right
|
| 211 |
+
last = out[i]
|
| 212 |
+
}
|
| 213 |
+
return
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
// creates 16 56-bit subkeys from the original key.
|
| 217 |
+
func (c *desCipher) generateSubkeys(keyBytes []byte) {
|
| 218 |
+
feistelBoxOnce.Do(initFeistelBox)
|
| 219 |
+
|
| 220 |
+
// apply PC1 permutation to key
|
| 221 |
+
key := binary.BigEndian.Uint64(keyBytes)
|
| 222 |
+
permutedKey := permuteBlock(key, permutedChoice1[:])
|
| 223 |
+
|
| 224 |
+
// rotate halves of permuted key according to the rotation schedule
|
| 225 |
+
leftRotations := ksRotate(uint32(permutedKey >> 28))
|
| 226 |
+
rightRotations := ksRotate(uint32(permutedKey<<4) >> 4)
|
| 227 |
+
|
| 228 |
+
// generate subkeys
|
| 229 |
+
for i := 0; i < 16; i++ {
|
| 230 |
+
// combine halves to form 56-bit input to PC2
|
| 231 |
+
pc2Input := uint64(leftRotations[i])<<28 | uint64(rightRotations[i])
|
| 232 |
+
// apply PC2 permutation to 7 byte input
|
| 233 |
+
c.subkeys[i] = unpack(permuteBlock(pc2Input, permutedChoice2[:]))
|
| 234 |
+
}
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
// Expand 48-bit input to 64-bit, with each 6-bit block padded by extra two bits at the top.
|
| 238 |
+
// By doing so, we can have the input blocks (four bits each), and the key blocks (six bits each) well-aligned without
|
| 239 |
+
// extra shifts/rotations for alignments.
|
| 240 |
+
func unpack(x uint64) uint64 {
|
| 241 |
+
return ((x>>(6*1))&0xff)<<(8*0) |
|
| 242 |
+
((x>>(6*3))&0xff)<<(8*1) |
|
| 243 |
+
((x>>(6*5))&0xff)<<(8*2) |
|
| 244 |
+
((x>>(6*7))&0xff)<<(8*3) |
|
| 245 |
+
((x>>(6*0))&0xff)<<(8*4) |
|
| 246 |
+
((x>>(6*2))&0xff)<<(8*5) |
|
| 247 |
+
((x>>(6*4))&0xff)<<(8*6) |
|
| 248 |
+
((x>>(6*6))&0xff)<<(8*7)
|
| 249 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/des/cipher.go
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2011 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package des
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/cipher"
|
| 9 |
+
"crypto/internal/alias"
|
| 10 |
+
"encoding/binary"
|
| 11 |
+
"strconv"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
// The DES block size in bytes.
|
| 15 |
+
const BlockSize = 8
|
| 16 |
+
|
| 17 |
+
type KeySizeError int
|
| 18 |
+
|
| 19 |
+
func (k KeySizeError) Error() string {
|
| 20 |
+
return "crypto/des: invalid key size " + strconv.Itoa(int(k))
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// desCipher is an instance of DES encryption.
|
| 24 |
+
type desCipher struct {
|
| 25 |
+
subkeys [16]uint64
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// NewCipher creates and returns a new [cipher.Block].
|
| 29 |
+
func NewCipher(key []byte) (cipher.Block, error) {
|
| 30 |
+
if len(key) != 8 {
|
| 31 |
+
return nil, KeySizeError(len(key))
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
c := new(desCipher)
|
| 35 |
+
c.generateSubkeys(key)
|
| 36 |
+
return c, nil
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
func (c *desCipher) BlockSize() int { return BlockSize }
|
| 40 |
+
|
| 41 |
+
func (c *desCipher) Encrypt(dst, src []byte) {
|
| 42 |
+
if len(src) < BlockSize {
|
| 43 |
+
panic("crypto/des: input not full block")
|
| 44 |
+
}
|
| 45 |
+
if len(dst) < BlockSize {
|
| 46 |
+
panic("crypto/des: output not full block")
|
| 47 |
+
}
|
| 48 |
+
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
|
| 49 |
+
panic("crypto/des: invalid buffer overlap")
|
| 50 |
+
}
|
| 51 |
+
cryptBlock(c.subkeys[:], dst, src, false)
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
func (c *desCipher) Decrypt(dst, src []byte) {
|
| 55 |
+
if len(src) < BlockSize {
|
| 56 |
+
panic("crypto/des: input not full block")
|
| 57 |
+
}
|
| 58 |
+
if len(dst) < BlockSize {
|
| 59 |
+
panic("crypto/des: output not full block")
|
| 60 |
+
}
|
| 61 |
+
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
|
| 62 |
+
panic("crypto/des: invalid buffer overlap")
|
| 63 |
+
}
|
| 64 |
+
cryptBlock(c.subkeys[:], dst, src, true)
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// A tripleDESCipher is an instance of TripleDES encryption.
|
| 68 |
+
type tripleDESCipher struct {
|
| 69 |
+
cipher1, cipher2, cipher3 desCipher
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
// NewTripleDESCipher creates and returns a new [cipher.Block].
|
| 73 |
+
func NewTripleDESCipher(key []byte) (cipher.Block, error) {
|
| 74 |
+
if len(key) != 24 {
|
| 75 |
+
return nil, KeySizeError(len(key))
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
c := new(tripleDESCipher)
|
| 79 |
+
c.cipher1.generateSubkeys(key[:8])
|
| 80 |
+
c.cipher2.generateSubkeys(key[8:16])
|
| 81 |
+
c.cipher3.generateSubkeys(key[16:])
|
| 82 |
+
return c, nil
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
func (c *tripleDESCipher) BlockSize() int { return BlockSize }
|
| 86 |
+
|
| 87 |
+
func (c *tripleDESCipher) Encrypt(dst, src []byte) {
|
| 88 |
+
if len(src) < BlockSize {
|
| 89 |
+
panic("crypto/des: input not full block")
|
| 90 |
+
}
|
| 91 |
+
if len(dst) < BlockSize {
|
| 92 |
+
panic("crypto/des: output not full block")
|
| 93 |
+
}
|
| 94 |
+
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
|
| 95 |
+
panic("crypto/des: invalid buffer overlap")
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
b := binary.BigEndian.Uint64(src)
|
| 99 |
+
b = permuteInitialBlock(b)
|
| 100 |
+
left, right := uint32(b>>32), uint32(b)
|
| 101 |
+
|
| 102 |
+
left = (left << 1) | (left >> 31)
|
| 103 |
+
right = (right << 1) | (right >> 31)
|
| 104 |
+
|
| 105 |
+
for i := 0; i < 8; i++ {
|
| 106 |
+
left, right = feistel(left, right, c.cipher1.subkeys[2*i], c.cipher1.subkeys[2*i+1])
|
| 107 |
+
}
|
| 108 |
+
for i := 0; i < 8; i++ {
|
| 109 |
+
right, left = feistel(right, left, c.cipher2.subkeys[15-2*i], c.cipher2.subkeys[15-(2*i+1)])
|
| 110 |
+
}
|
| 111 |
+
for i := 0; i < 8; i++ {
|
| 112 |
+
left, right = feistel(left, right, c.cipher3.subkeys[2*i], c.cipher3.subkeys[2*i+1])
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
left = (left << 31) | (left >> 1)
|
| 116 |
+
right = (right << 31) | (right >> 1)
|
| 117 |
+
|
| 118 |
+
preOutput := (uint64(right) << 32) | uint64(left)
|
| 119 |
+
binary.BigEndian.PutUint64(dst, permuteFinalBlock(preOutput))
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
func (c *tripleDESCipher) Decrypt(dst, src []byte) {
|
| 123 |
+
if len(src) < BlockSize {
|
| 124 |
+
panic("crypto/des: input not full block")
|
| 125 |
+
}
|
| 126 |
+
if len(dst) < BlockSize {
|
| 127 |
+
panic("crypto/des: output not full block")
|
| 128 |
+
}
|
| 129 |
+
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
|
| 130 |
+
panic("crypto/des: invalid buffer overlap")
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
b := binary.BigEndian.Uint64(src)
|
| 134 |
+
b = permuteInitialBlock(b)
|
| 135 |
+
left, right := uint32(b>>32), uint32(b)
|
| 136 |
+
|
| 137 |
+
left = (left << 1) | (left >> 31)
|
| 138 |
+
right = (right << 1) | (right >> 31)
|
| 139 |
+
|
| 140 |
+
for i := 0; i < 8; i++ {
|
| 141 |
+
left, right = feistel(left, right, c.cipher3.subkeys[15-2*i], c.cipher3.subkeys[15-(2*i+1)])
|
| 142 |
+
}
|
| 143 |
+
for i := 0; i < 8; i++ {
|
| 144 |
+
right, left = feistel(right, left, c.cipher2.subkeys[2*i], c.cipher2.subkeys[2*i+1])
|
| 145 |
+
}
|
| 146 |
+
for i := 0; i < 8; i++ {
|
| 147 |
+
left, right = feistel(left, right, c.cipher1.subkeys[15-2*i], c.cipher1.subkeys[15-(2*i+1)])
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
left = (left << 31) | (left >> 1)
|
| 151 |
+
right = (right << 31) | (right >> 1)
|
| 152 |
+
|
| 153 |
+
preOutput := (uint64(right) << 32) | uint64(left)
|
| 154 |
+
binary.BigEndian.PutUint64(dst, permuteFinalBlock(preOutput))
|
| 155 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/des/const.go
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2010 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// Package des implements the Data Encryption Standard (DES) and the
|
| 6 |
+
// Triple Data Encryption Algorithm (TDEA) as defined
|
| 7 |
+
// in U.S. Federal Information Processing Standards Publication 46-3.
|
| 8 |
+
//
|
| 9 |
+
// DES is cryptographically broken and should not be used for secure
|
| 10 |
+
// applications.
|
| 11 |
+
package des
|
| 12 |
+
|
| 13 |
+
// Used to perform an initial permutation of a 64-bit input block.
|
| 14 |
+
var initialPermutation = [64]byte{
|
| 15 |
+
6, 14, 22, 30, 38, 46, 54, 62,
|
| 16 |
+
4, 12, 20, 28, 36, 44, 52, 60,
|
| 17 |
+
2, 10, 18, 26, 34, 42, 50, 58,
|
| 18 |
+
0, 8, 16, 24, 32, 40, 48, 56,
|
| 19 |
+
7, 15, 23, 31, 39, 47, 55, 63,
|
| 20 |
+
5, 13, 21, 29, 37, 45, 53, 61,
|
| 21 |
+
3, 11, 19, 27, 35, 43, 51, 59,
|
| 22 |
+
1, 9, 17, 25, 33, 41, 49, 57,
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
// Used to perform a final permutation of a 4-bit preoutput block. This is the
|
| 26 |
+
// inverse of initialPermutation
|
| 27 |
+
var finalPermutation = [64]byte{
|
| 28 |
+
24, 56, 16, 48, 8, 40, 0, 32,
|
| 29 |
+
25, 57, 17, 49, 9, 41, 1, 33,
|
| 30 |
+
26, 58, 18, 50, 10, 42, 2, 34,
|
| 31 |
+
27, 59, 19, 51, 11, 43, 3, 35,
|
| 32 |
+
28, 60, 20, 52, 12, 44, 4, 36,
|
| 33 |
+
29, 61, 21, 53, 13, 45, 5, 37,
|
| 34 |
+
30, 62, 22, 54, 14, 46, 6, 38,
|
| 35 |
+
31, 63, 23, 55, 15, 47, 7, 39,
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// Used to expand an input block of 32 bits, producing an output block of 48
|
| 39 |
+
// bits.
|
| 40 |
+
var expansionFunction = [48]byte{
|
| 41 |
+
0, 31, 30, 29, 28, 27, 28, 27,
|
| 42 |
+
26, 25, 24, 23, 24, 23, 22, 21,
|
| 43 |
+
20, 19, 20, 19, 18, 17, 16, 15,
|
| 44 |
+
16, 15, 14, 13, 12, 11, 12, 11,
|
| 45 |
+
10, 9, 8, 7, 8, 7, 6, 5,
|
| 46 |
+
4, 3, 4, 3, 2, 1, 0, 31,
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// Yields a 32-bit output from a 32-bit input
|
| 50 |
+
var permutationFunction = [32]byte{
|
| 51 |
+
16, 25, 12, 11, 3, 20, 4, 15,
|
| 52 |
+
31, 17, 9, 6, 27, 14, 1, 22,
|
| 53 |
+
30, 24, 8, 18, 0, 5, 29, 23,
|
| 54 |
+
13, 19, 2, 26, 10, 21, 28, 7,
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// Used in the key schedule to select 56 bits
|
| 58 |
+
// from a 64-bit input.
|
| 59 |
+
var permutedChoice1 = [56]byte{
|
| 60 |
+
7, 15, 23, 31, 39, 47, 55, 63,
|
| 61 |
+
6, 14, 22, 30, 38, 46, 54, 62,
|
| 62 |
+
5, 13, 21, 29, 37, 45, 53, 61,
|
| 63 |
+
4, 12, 20, 28, 1, 9, 17, 25,
|
| 64 |
+
33, 41, 49, 57, 2, 10, 18, 26,
|
| 65 |
+
34, 42, 50, 58, 3, 11, 19, 27,
|
| 66 |
+
35, 43, 51, 59, 36, 44, 52, 60,
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
// Used in the key schedule to produce each subkey by selecting 48 bits from
|
| 70 |
+
// the 56-bit input
|
| 71 |
+
var permutedChoice2 = [48]byte{
|
| 72 |
+
42, 39, 45, 32, 55, 51, 53, 28,
|
| 73 |
+
41, 50, 35, 46, 33, 37, 44, 52,
|
| 74 |
+
30, 48, 40, 49, 29, 36, 43, 54,
|
| 75 |
+
15, 4, 25, 19, 9, 1, 26, 16,
|
| 76 |
+
5, 11, 23, 8, 12, 7, 17, 0,
|
| 77 |
+
22, 3, 10, 14, 6, 20, 27, 24,
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
// 8 S-boxes composed of 4 rows and 16 columns
|
| 81 |
+
// Used in the DES cipher function
|
| 82 |
+
var sBoxes = [8][4][16]uint8{
|
| 83 |
+
// S-box 1
|
| 84 |
+
{
|
| 85 |
+
{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
|
| 86 |
+
{0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
|
| 87 |
+
{4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
|
| 88 |
+
{15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13},
|
| 89 |
+
},
|
| 90 |
+
// S-box 2
|
| 91 |
+
{
|
| 92 |
+
{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
|
| 93 |
+
{3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
|
| 94 |
+
{0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
|
| 95 |
+
{13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9},
|
| 96 |
+
},
|
| 97 |
+
// S-box 3
|
| 98 |
+
{
|
| 99 |
+
{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
|
| 100 |
+
{13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
|
| 101 |
+
{13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
|
| 102 |
+
{1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12},
|
| 103 |
+
},
|
| 104 |
+
// S-box 4
|
| 105 |
+
{
|
| 106 |
+
{7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
|
| 107 |
+
{13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
|
| 108 |
+
{10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
|
| 109 |
+
{3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14},
|
| 110 |
+
},
|
| 111 |
+
// S-box 5
|
| 112 |
+
{
|
| 113 |
+
{2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
|
| 114 |
+
{14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
|
| 115 |
+
{4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
|
| 116 |
+
{11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3},
|
| 117 |
+
},
|
| 118 |
+
// S-box 6
|
| 119 |
+
{
|
| 120 |
+
{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
|
| 121 |
+
{10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
|
| 122 |
+
{9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
|
| 123 |
+
{4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13},
|
| 124 |
+
},
|
| 125 |
+
// S-box 7
|
| 126 |
+
{
|
| 127 |
+
{4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
|
| 128 |
+
{13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
|
| 129 |
+
{1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
|
| 130 |
+
{6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12},
|
| 131 |
+
},
|
| 132 |
+
// S-box 8
|
| 133 |
+
{
|
| 134 |
+
{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
|
| 135 |
+
{1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
|
| 136 |
+
{7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
|
| 137 |
+
{2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11},
|
| 138 |
+
},
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
// Size of left rotation per round in each half of the key schedule
|
| 142 |
+
var ksRotations = [16]uint8{1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1}
|
platform/dbops/binaries/go/go/src/crypto/des/des_test.go
ADDED
|
@@ -0,0 +1,1563 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2011 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package des_test
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"crypto/cipher"
|
| 10 |
+
"crypto/des"
|
| 11 |
+
"testing"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
type CryptTest struct {
|
| 15 |
+
key []byte
|
| 16 |
+
in []byte
|
| 17 |
+
out []byte
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
// some custom tests for DES
|
| 21 |
+
var encryptDESTests = []CryptTest{
|
| 22 |
+
{
|
| 23 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 24 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 25 |
+
[]byte{0x8c, 0xa6, 0x4d, 0xe9, 0xc1, 0xb1, 0x23, 0xa7}},
|
| 26 |
+
{
|
| 27 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 28 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 29 |
+
[]byte{0x35, 0x55, 0x50, 0xb2, 0x15, 0x0e, 0x24, 0x51}},
|
| 30 |
+
{
|
| 31 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 32 |
+
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
|
| 33 |
+
[]byte{0x61, 0x7b, 0x3a, 0x0c, 0xe8, 0xf0, 0x71, 0x00}},
|
| 34 |
+
{
|
| 35 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 36 |
+
[]byte{0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
|
| 37 |
+
[]byte{0x92, 0x31, 0xf2, 0x36, 0xff, 0x9a, 0xa9, 0x5c}},
|
| 38 |
+
{
|
| 39 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 40 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 41 |
+
[]byte{0xca, 0xaa, 0xaf, 0x4d, 0xea, 0xf1, 0xdb, 0xae}},
|
| 42 |
+
{
|
| 43 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 44 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 45 |
+
[]byte{0x73, 0x59, 0xb2, 0x16, 0x3e, 0x4e, 0xdc, 0x58}},
|
| 46 |
+
{
|
| 47 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 48 |
+
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
|
| 49 |
+
[]byte{0x6d, 0xce, 0x0d, 0xc9, 0x00, 0x65, 0x56, 0xa3}},
|
| 50 |
+
{
|
| 51 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 52 |
+
[]byte{0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
|
| 53 |
+
[]byte{0x9e, 0x84, 0xc5, 0xf3, 0x17, 0x0f, 0x8e, 0xff}},
|
| 54 |
+
{
|
| 55 |
+
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
|
| 56 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 57 |
+
[]byte{0xd5, 0xd4, 0x4f, 0xf7, 0x20, 0x68, 0x3d, 0x0d}},
|
| 58 |
+
{
|
| 59 |
+
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
|
| 60 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 61 |
+
[]byte{0x59, 0x73, 0x23, 0x56, 0xf3, 0x6f, 0xde, 0x06}},
|
| 62 |
+
{
|
| 63 |
+
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
|
| 64 |
+
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
|
| 65 |
+
[]byte{0x56, 0xcc, 0x09, 0xe7, 0xcf, 0xdc, 0x4c, 0xef}},
|
| 66 |
+
{
|
| 67 |
+
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
|
| 68 |
+
[]byte{0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
|
| 69 |
+
[]byte{0x12, 0xc6, 0x26, 0xaf, 0x05, 0x8b, 0x43, 0x3b}},
|
| 70 |
+
{
|
| 71 |
+
[]byte{0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
|
| 72 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 73 |
+
[]byte{0xa6, 0x8c, 0xdc, 0xa9, 0x0c, 0x90, 0x21, 0xf9}},
|
| 74 |
+
{
|
| 75 |
+
[]byte{0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
|
| 76 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 77 |
+
[]byte{0x2a, 0x2b, 0xb0, 0x08, 0xdf, 0x97, 0xc2, 0xf2}},
|
| 78 |
+
{
|
| 79 |
+
[]byte{0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
|
| 80 |
+
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
|
| 81 |
+
[]byte{0xed, 0x39, 0xd9, 0x50, 0xfa, 0x74, 0xbc, 0xc4}},
|
| 82 |
+
{
|
| 83 |
+
[]byte{0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
|
| 84 |
+
[]byte{0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
|
| 85 |
+
[]byte{0xa9, 0x33, 0xf6, 0x18, 0x30, 0x23, 0xb3, 0x10}},
|
| 86 |
+
{
|
| 87 |
+
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
|
| 88 |
+
[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
|
| 89 |
+
[]byte{0x17, 0x66, 0x8d, 0xfc, 0x72, 0x92, 0x53, 0x2d}},
|
| 90 |
+
{
|
| 91 |
+
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
|
| 92 |
+
[]byte{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 93 |
+
[]byte{0xb4, 0xfd, 0x23, 0x16, 0x47, 0xa5, 0xbe, 0xc0}},
|
| 94 |
+
{
|
| 95 |
+
[]byte{0x0e, 0x32, 0x92, 0x32, 0xea, 0x6d, 0x0d, 0x73},
|
| 96 |
+
[]byte{0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87},
|
| 97 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
|
| 98 |
+
{
|
| 99 |
+
[]byte{0x73, 0x65, 0x63, 0x52, 0x33, 0x74, 0x24, 0x3b}, // "secR3t$;"
|
| 100 |
+
[]byte{0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x31, 0x32}, // "a test12"
|
| 101 |
+
[]byte{0x37, 0x0d, 0xee, 0x2c, 0x1f, 0xb4, 0xf7, 0xa5}},
|
| 102 |
+
{
|
| 103 |
+
[]byte{0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68}, // "abcdefgh"
|
| 104 |
+
[]byte{0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68}, // "abcdefgh"
|
| 105 |
+
[]byte{0x2a, 0x8d, 0x69, 0xde, 0x9d, 0x5f, 0xdf, 0xf9}},
|
| 106 |
+
{
|
| 107 |
+
[]byte{0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68}, // "abcdefgh"
|
| 108 |
+
[]byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38}, // "12345678"
|
| 109 |
+
[]byte{0x21, 0xc6, 0x0d, 0xa5, 0x34, 0x24, 0x8b, 0xce}},
|
| 110 |
+
{
|
| 111 |
+
[]byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38}, // "12345678"
|
| 112 |
+
[]byte{0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68}, // "abcdefgh"
|
| 113 |
+
[]byte{0x94, 0xd4, 0x43, 0x6b, 0xc3, 0xb5, 0xb6, 0x93}},
|
| 114 |
+
{
|
| 115 |
+
[]byte{0x1f, 0x79, 0x90, 0x5f, 0x88, 0x01, 0xc8, 0x88}, // random
|
| 116 |
+
[]byte{0xc7, 0x46, 0x18, 0x73, 0xaf, 0x48, 0x5f, 0xb3}, // random
|
| 117 |
+
[]byte{0xb0, 0x93, 0x50, 0x88, 0xf9, 0x92, 0x44, 0x6a}},
|
| 118 |
+
{
|
| 119 |
+
[]byte{0xe6, 0xf4, 0xf2, 0xdb, 0x31, 0x42, 0x53, 0x01}, // random
|
| 120 |
+
[]byte{0xff, 0x3d, 0x25, 0x50, 0x12, 0xe3, 0x4a, 0xc5}, // random
|
| 121 |
+
[]byte{0x86, 0x08, 0xd3, 0xd1, 0x6c, 0x2f, 0xd2, 0x55}},
|
| 122 |
+
{
|
| 123 |
+
[]byte{0x69, 0xc1, 0x9d, 0xc1, 0x15, 0xc5, 0xfb, 0x2b}, // random
|
| 124 |
+
[]byte{0x1a, 0x22, 0x5c, 0xaf, 0x1f, 0x1d, 0xa3, 0xf9}, // random
|
| 125 |
+
[]byte{0x64, 0xba, 0x31, 0x67, 0x56, 0x91, 0x1e, 0xa7}},
|
| 126 |
+
{
|
| 127 |
+
[]byte{0x6e, 0x5e, 0xe2, 0x47, 0xc4, 0xbf, 0xf6, 0x51}, // random
|
| 128 |
+
[]byte{0x11, 0xc9, 0x57, 0xff, 0x66, 0x89, 0x0e, 0xf0}, // random
|
| 129 |
+
[]byte{0x94, 0xc5, 0x35, 0xb2, 0xc5, 0x8b, 0x39, 0x72}},
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
var weakKeyTests = []CryptTest{
|
| 133 |
+
{
|
| 134 |
+
[]byte{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 135 |
+
[]byte{0x55, 0x74, 0xc0, 0xbd, 0x7c, 0xdf, 0xf7, 0x39}, // random
|
| 136 |
+
nil},
|
| 137 |
+
{
|
| 138 |
+
[]byte{0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe},
|
| 139 |
+
[]byte{0xe8, 0xe1, 0xa7, 0xc1, 0xde, 0x11, 0x89, 0xaa}, // random
|
| 140 |
+
nil},
|
| 141 |
+
{
|
| 142 |
+
[]byte{0xe0, 0xe0, 0xe0, 0xe0, 0xf1, 0xf1, 0xf1, 0xf1},
|
| 143 |
+
[]byte{0x50, 0x6a, 0x4b, 0x94, 0x3b, 0xed, 0x7d, 0xdc}, // random
|
| 144 |
+
nil},
|
| 145 |
+
{
|
| 146 |
+
[]byte{0x1f, 0x1f, 0x1f, 0x1f, 0x0e, 0x0e, 0x0e, 0x0e},
|
| 147 |
+
[]byte{0x88, 0x81, 0x56, 0x38, 0xec, 0x3b, 0x1c, 0x97}, // random
|
| 148 |
+
nil},
|
| 149 |
+
{
|
| 150 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 151 |
+
[]byte{0x17, 0xa0, 0x83, 0x62, 0x32, 0xfe, 0x9a, 0x0b}, // random
|
| 152 |
+
nil},
|
| 153 |
+
{
|
| 154 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 155 |
+
[]byte{0xca, 0x8f, 0xca, 0x1f, 0x50, 0xc5, 0x7b, 0x49}, // random
|
| 156 |
+
nil},
|
| 157 |
+
{
|
| 158 |
+
[]byte{0xe1, 0xe1, 0xe1, 0xe1, 0xf0, 0xf0, 0xf0, 0xf0},
|
| 159 |
+
[]byte{0xb1, 0xea, 0xad, 0x7d, 0xe7, 0xc3, 0x7a, 0x43}, // random
|
| 160 |
+
nil},
|
| 161 |
+
{
|
| 162 |
+
[]byte{0x1e, 0x1e, 0x1e, 0x1e, 0x0f, 0x0f, 0x0f, 0x0f},
|
| 163 |
+
[]byte{0xae, 0x74, 0x7d, 0x6f, 0xef, 0x16, 0xbb, 0x81}, // random
|
| 164 |
+
nil},
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
var semiWeakKeyTests = []CryptTest{
|
| 168 |
+
// key and out contain the semi-weak key pair
|
| 169 |
+
{
|
| 170 |
+
[]byte{0x01, 0x1f, 0x01, 0x1f, 0x01, 0x0e, 0x01, 0x0e},
|
| 171 |
+
[]byte{0x12, 0xfa, 0x31, 0x16, 0xf9, 0xc5, 0x0a, 0xe4}, // random
|
| 172 |
+
[]byte{0x1f, 0x01, 0x1f, 0x01, 0x0e, 0x01, 0x0e, 0x01}},
|
| 173 |
+
{
|
| 174 |
+
[]byte{0x01, 0xe0, 0x01, 0xe0, 0x01, 0xf1, 0x01, 0xf1},
|
| 175 |
+
[]byte{0xb0, 0x4c, 0x7a, 0xee, 0xd2, 0xe5, 0x4d, 0xb7}, // random
|
| 176 |
+
[]byte{0xe0, 0x01, 0xe0, 0x01, 0xf1, 0x01, 0xf1, 0x01}},
|
| 177 |
+
{
|
| 178 |
+
[]byte{0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe},
|
| 179 |
+
[]byte{0xa4, 0x81, 0xcd, 0xb1, 0x64, 0x6f, 0xd3, 0xbc}, // random
|
| 180 |
+
[]byte{0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01}},
|
| 181 |
+
{
|
| 182 |
+
[]byte{0x1f, 0xe0, 0x1f, 0xe0, 0x0e, 0xf1, 0x0e, 0xf1},
|
| 183 |
+
[]byte{0xee, 0x27, 0xdd, 0x88, 0x4c, 0x22, 0xcd, 0xce}, // random
|
| 184 |
+
[]byte{0xe0, 0x1f, 0xe0, 0x1f, 0xf1, 0x0e, 0xf1, 0x0e}},
|
| 185 |
+
{
|
| 186 |
+
[]byte{0x1f, 0xfe, 0x1f, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe},
|
| 187 |
+
[]byte{0x19, 0x3d, 0xcf, 0x97, 0x70, 0xfb, 0xab, 0xe1}, // random
|
| 188 |
+
[]byte{0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x0e, 0xfe, 0x0e}},
|
| 189 |
+
{
|
| 190 |
+
[]byte{0xe0, 0xfe, 0xe0, 0xfe, 0xf1, 0xfe, 0xf1, 0xfe},
|
| 191 |
+
[]byte{0x7c, 0x82, 0x69, 0xe4, 0x1e, 0x86, 0x99, 0xd7}, // random
|
| 192 |
+
[]byte{0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf1, 0xfe, 0xf1}},
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
// some custom tests for TripleDES
|
| 196 |
+
var encryptTripleDESTests = []CryptTest{
|
| 197 |
+
{
|
| 198 |
+
[]byte{
|
| 199 |
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
| 200 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 201 |
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 202 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 203 |
+
[]byte{0x92, 0x95, 0xb5, 0x9b, 0xb3, 0x84, 0x73, 0x6e}},
|
| 204 |
+
{
|
| 205 |
+
[]byte{
|
| 206 |
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
| 207 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 208 |
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 209 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 210 |
+
[]byte{0xc1, 0x97, 0xf5, 0x58, 0x74, 0x8a, 0x20, 0xe7}},
|
| 211 |
+
{
|
| 212 |
+
[]byte{
|
| 213 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 214 |
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
| 215 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 216 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 217 |
+
[]byte{0x3e, 0x68, 0x0a, 0xa7, 0x8b, 0x75, 0xdf, 0x18}},
|
| 218 |
+
{
|
| 219 |
+
[]byte{
|
| 220 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 221 |
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
| 222 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 223 |
+
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
| 224 |
+
[]byte{0x6d, 0x6a, 0x4a, 0x64, 0x4c, 0x7b, 0x8c, 0x91}},
|
| 225 |
+
{
|
| 226 |
+
[]byte{ // "abcdefgh12345678ABCDEFGH"
|
| 227 |
+
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
|
| 228 |
+
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
|
| 229 |
+
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48},
|
| 230 |
+
[]byte{0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30}, // "00000000"
|
| 231 |
+
[]byte{0xe4, 0x61, 0xb7, 0x59, 0x68, 0x8b, 0xff, 0x66}},
|
| 232 |
+
{
|
| 233 |
+
[]byte{ // "abcdefgh12345678ABCDEFGH"
|
| 234 |
+
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
|
| 235 |
+
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
|
| 236 |
+
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48},
|
| 237 |
+
[]byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38}, // "12345678"
|
| 238 |
+
[]byte{0xdb, 0xd0, 0x92, 0xde, 0xf8, 0x34, 0xff, 0x58}},
|
| 239 |
+
{
|
| 240 |
+
[]byte{ // "abcdefgh12345678ABCDEFGH"
|
| 241 |
+
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
|
| 242 |
+
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
|
| 243 |
+
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48},
|
| 244 |
+
[]byte{0xf0, 0xc5, 0x82, 0x22, 0xd3, 0xe6, 0x12, 0xd2}, // random
|
| 245 |
+
[]byte{0xba, 0xe4, 0x41, 0xb1, 0x3c, 0x37, 0x4d, 0xf4}},
|
| 246 |
+
{
|
| 247 |
+
[]byte{ // random
|
| 248 |
+
0xd3, 0x7d, 0x45, 0xee, 0x22, 0xe9, 0xcf, 0x52,
|
| 249 |
+
0xf4, 0x65, 0xa2, 0x4f, 0x70, 0xd1, 0x81, 0x8a,
|
| 250 |
+
0x3d, 0xbe, 0x2f, 0x39, 0xc7, 0x71, 0xd2, 0xe9},
|
| 251 |
+
[]byte{0x49, 0x53, 0xc3, 0xe9, 0x78, 0xdf, 0x9f, 0xaf}, // random
|
| 252 |
+
[]byte{0x53, 0x40, 0x51, 0x24, 0xd8, 0x3c, 0xf9, 0x88}},
|
| 253 |
+
{
|
| 254 |
+
[]byte{ // random
|
| 255 |
+
0xcb, 0x10, 0x7d, 0xda, 0x7e, 0x96, 0x57, 0x0a,
|
| 256 |
+
0xe8, 0xeb, 0xe8, 0x07, 0x8e, 0x87, 0xd3, 0x57,
|
| 257 |
+
0xb2, 0x61, 0x12, 0xb8, 0x2a, 0x90, 0xb7, 0x2f},
|
| 258 |
+
[]byte{0xa3, 0xc2, 0x60, 0xb1, 0x0b, 0xb7, 0x28, 0x6e}, // random
|
| 259 |
+
[]byte{0x56, 0x73, 0x7d, 0xfb, 0xb5, 0xa1, 0xc3, 0xde}},
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
// NIST Special Publication 800-20, Appendix A
|
| 263 |
+
// Key for use with Table A.1 tests
|
| 264 |
+
var tableA1Key = []byte{
|
| 265 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 266 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 267 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
// Table A.1 Resulting Ciphertext from the Variable Plaintext Known Answer Test
|
| 271 |
+
var tableA1Tests = []CryptTest{
|
| 272 |
+
{nil, // 0
|
| 273 |
+
[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 274 |
+
[]byte{0x95, 0xf8, 0xa5, 0xe5, 0xdd, 0x31, 0xd9, 0x00}},
|
| 275 |
+
{nil, // 1
|
| 276 |
+
[]byte{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 277 |
+
[]byte{0xdd, 0x7f, 0x12, 0x1c, 0xa5, 0x01, 0x56, 0x19}},
|
| 278 |
+
{nil, // 2
|
| 279 |
+
[]byte{0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 280 |
+
[]byte{0x2e, 0x86, 0x53, 0x10, 0x4f, 0x38, 0x34, 0xea}},
|
| 281 |
+
{nil, // 3
|
| 282 |
+
[]byte{0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 283 |
+
[]byte{0x4b, 0xd3, 0x88, 0xff, 0x6c, 0xd8, 0x1d, 0x4f}},
|
| 284 |
+
{nil, // 4
|
| 285 |
+
[]byte{0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 286 |
+
[]byte{0x20, 0xb9, 0xe7, 0x67, 0xb2, 0xfb, 0x14, 0x56}},
|
| 287 |
+
{nil, // 5
|
| 288 |
+
[]byte{0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 289 |
+
[]byte{0x55, 0x57, 0x93, 0x80, 0xd7, 0x71, 0x38, 0xef}},
|
| 290 |
+
{nil, // 6
|
| 291 |
+
[]byte{0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 292 |
+
[]byte{0x6c, 0xc5, 0xde, 0xfa, 0xaf, 0x04, 0x51, 0x2f}},
|
| 293 |
+
{nil, // 7
|
| 294 |
+
[]byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 295 |
+
[]byte{0x0d, 0x9f, 0x27, 0x9b, 0xa5, 0xd8, 0x72, 0x60}},
|
| 296 |
+
{nil, // 8
|
| 297 |
+
[]byte{0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 298 |
+
[]byte{0xd9, 0x03, 0x1b, 0x02, 0x71, 0xbd, 0x5a, 0x0a}},
|
| 299 |
+
{nil, // 9
|
| 300 |
+
[]byte{0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 301 |
+
[]byte{0x42, 0x42, 0x50, 0xb3, 0x7c, 0x3d, 0xd9, 0x51}},
|
| 302 |
+
{nil, // 10
|
| 303 |
+
[]byte{0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 304 |
+
[]byte{0xb8, 0x06, 0x1b, 0x7e, 0xcd, 0x9a, 0x21, 0xe5}},
|
| 305 |
+
{nil, // 11
|
| 306 |
+
[]byte{0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 307 |
+
[]byte{0xf1, 0x5d, 0x0f, 0x28, 0x6b, 0x65, 0xbd, 0x28}},
|
| 308 |
+
{nil, // 12
|
| 309 |
+
[]byte{0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 310 |
+
[]byte{0xad, 0xd0, 0xcc, 0x8d, 0x6e, 0x5d, 0xeb, 0xa1}},
|
| 311 |
+
{nil, // 13
|
| 312 |
+
[]byte{0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 313 |
+
[]byte{0xe6, 0xd5, 0xf8, 0x27, 0x52, 0xad, 0x63, 0xd1}},
|
| 314 |
+
{nil, // 14
|
| 315 |
+
[]byte{0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 316 |
+
[]byte{0xec, 0xbf, 0xe3, 0xbd, 0x3f, 0x59, 0x1a, 0x5e}},
|
| 317 |
+
{nil, // 15
|
| 318 |
+
[]byte{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 319 |
+
[]byte{0xf3, 0x56, 0x83, 0x43, 0x79, 0xd1, 0x65, 0xcd}},
|
| 320 |
+
{nil, // 16
|
| 321 |
+
[]byte{0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 322 |
+
[]byte{0x2b, 0x9f, 0x98, 0x2f, 0x20, 0x03, 0x7f, 0xa9}},
|
| 323 |
+
{nil, // 17
|
| 324 |
+
[]byte{0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 325 |
+
[]byte{0x88, 0x9d, 0xe0, 0x68, 0xa1, 0x6f, 0x0b, 0xe6}},
|
| 326 |
+
{nil, // 18
|
| 327 |
+
[]byte{0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 328 |
+
[]byte{0xe1, 0x9e, 0x27, 0x5d, 0x84, 0x6a, 0x12, 0x98}},
|
| 329 |
+
{nil, // 19
|
| 330 |
+
[]byte{0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 331 |
+
[]byte{0x32, 0x9a, 0x8e, 0xd5, 0x23, 0xd7, 0x1a, 0xec}},
|
| 332 |
+
{nil, // 20
|
| 333 |
+
[]byte{0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 334 |
+
[]byte{0xe7, 0xfc, 0xe2, 0x25, 0x57, 0xd2, 0x3c, 0x97}},
|
| 335 |
+
{nil, // 21
|
| 336 |
+
[]byte{0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 337 |
+
[]byte{0x12, 0xa9, 0xf5, 0x81, 0x7f, 0xf2, 0xd6, 0x5d}},
|
| 338 |
+
{nil, // 22
|
| 339 |
+
[]byte{0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 340 |
+
[]byte{0xa4, 0x84, 0xc3, 0xad, 0x38, 0xdc, 0x9c, 0x19}},
|
| 341 |
+
{nil, // 23
|
| 342 |
+
[]byte{0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
|
| 343 |
+
[]byte{0xfb, 0xe0, 0x0a, 0x8a, 0x1e, 0xf8, 0xad, 0x72}},
|
| 344 |
+
{nil, // 24
|
| 345 |
+
[]byte{0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00},
|
| 346 |
+
[]byte{0x75, 0x0d, 0x07, 0x94, 0x07, 0x52, 0x13, 0x63}},
|
| 347 |
+
{nil, // 25
|
| 348 |
+
[]byte{0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00},
|
| 349 |
+
[]byte{0x64, 0xfe, 0xed, 0x9c, 0x72, 0x4c, 0x2f, 0xaf}},
|
| 350 |
+
{nil, // 26
|
| 351 |
+
[]byte{0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00},
|
| 352 |
+
[]byte{0xf0, 0x2b, 0x26, 0x3b, 0x32, 0x8e, 0x2b, 0x60}},
|
| 353 |
+
{nil, // 27
|
| 354 |
+
[]byte{0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00},
|
| 355 |
+
[]byte{0x9d, 0x64, 0x55, 0x5a, 0x9a, 0x10, 0xb8, 0x52}},
|
| 356 |
+
{nil, // 28
|
| 357 |
+
[]byte{0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00},
|
| 358 |
+
[]byte{0xd1, 0x06, 0xff, 0x0b, 0xed, 0x52, 0x55, 0xd7}},
|
| 359 |
+
{nil, // 29
|
| 360 |
+
[]byte{0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00},
|
| 361 |
+
[]byte{0xe1, 0x65, 0x2c, 0x6b, 0x13, 0x8c, 0x64, 0xa5}},
|
| 362 |
+
{nil, // 30
|
| 363 |
+
[]byte{0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00},
|
| 364 |
+
[]byte{0xe4, 0x28, 0x58, 0x11, 0x86, 0xec, 0x8f, 0x46}},
|
| 365 |
+
{nil, // 31
|
| 366 |
+
[]byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00},
|
| 367 |
+
[]byte{0xae, 0xb5, 0xf5, 0xed, 0xe2, 0x2d, 0x1a, 0x36}},
|
| 368 |
+
{nil, // 32
|
| 369 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00},
|
| 370 |
+
[]byte{0xe9, 0x43, 0xd7, 0x56, 0x8a, 0xec, 0x0c, 0x5c}},
|
| 371 |
+
{nil, // 33
|
| 372 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00},
|
| 373 |
+
[]byte{0xdf, 0x98, 0xc8, 0x27, 0x6f, 0x54, 0xb0, 0x4b}},
|
| 374 |
+
{nil, // 34
|
| 375 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00},
|
| 376 |
+
[]byte{0xb1, 0x60, 0xe4, 0x68, 0x0f, 0x6c, 0x69, 0x6f}},
|
| 377 |
+
{nil, // 35
|
| 378 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00},
|
| 379 |
+
[]byte{0xfa, 0x07, 0x52, 0xb0, 0x7d, 0x9c, 0x4a, 0xb8}},
|
| 380 |
+
{nil, // 36
|
| 381 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00},
|
| 382 |
+
[]byte{0xca, 0x3a, 0x2b, 0x03, 0x6d, 0xbc, 0x85, 0x02}},
|
| 383 |
+
{nil, // 37
|
| 384 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00},
|
| 385 |
+
[]byte{0x5e, 0x09, 0x05, 0x51, 0x7b, 0xb5, 0x9b, 0xcf}},
|
| 386 |
+
{nil, // 38
|
| 387 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00},
|
| 388 |
+
[]byte{0x81, 0x4e, 0xeb, 0x3b, 0x91, 0xd9, 0x07, 0x26}},
|
| 389 |
+
{nil, // 39
|
| 390 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
|
| 391 |
+
[]byte{0x4d, 0x49, 0xdb, 0x15, 0x32, 0x91, 0x9c, 0x9f}},
|
| 392 |
+
{nil, // 40
|
| 393 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00},
|
| 394 |
+
[]byte{0x25, 0xeb, 0x5f, 0xc3, 0xf8, 0xcf, 0x06, 0x21}},
|
| 395 |
+
{nil, // 41
|
| 396 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00},
|
| 397 |
+
[]byte{0xab, 0x6a, 0x20, 0xc0, 0x62, 0x0d, 0x1c, 0x6f}},
|
| 398 |
+
{nil, // 42
|
| 399 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00},
|
| 400 |
+
[]byte{0x79, 0xe9, 0x0d, 0xbc, 0x98, 0xf9, 0x2c, 0xca}},
|
| 401 |
+
{nil, // 43
|
| 402 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00},
|
| 403 |
+
[]byte{0x86, 0x6e, 0xce, 0xdd, 0x80, 0x72, 0xbb, 0x0e}},
|
| 404 |
+
{nil, // 44
|
| 405 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00},
|
| 406 |
+
[]byte{0x8b, 0x54, 0x53, 0x6f, 0x2f, 0x3e, 0x64, 0xa8}},
|
| 407 |
+
{nil, // 45
|
| 408 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00},
|
| 409 |
+
[]byte{0xea, 0x51, 0xd3, 0x97, 0x55, 0x95, 0xb8, 0x6b}},
|
| 410 |
+
{nil, // 46
|
| 411 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00},
|
| 412 |
+
[]byte{0xca, 0xff, 0xc6, 0xac, 0x45, 0x42, 0xde, 0x31}},
|
| 413 |
+
{nil, // 47
|
| 414 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00},
|
| 415 |
+
[]byte{0x8d, 0xd4, 0x5a, 0x2d, 0xdf, 0x90, 0x79, 0x6c}},
|
| 416 |
+
{nil, // 48
|
| 417 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00},
|
| 418 |
+
[]byte{0x10, 0x29, 0xd5, 0x5e, 0x88, 0x0e, 0xc2, 0xd0}},
|
| 419 |
+
{nil, // 49
|
| 420 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00},
|
| 421 |
+
[]byte{0x5d, 0x86, 0xcb, 0x23, 0x63, 0x9d, 0xbe, 0xa9}},
|
| 422 |
+
{nil, // 50
|
| 423 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00},
|
| 424 |
+
[]byte{0x1d, 0x1c, 0xa8, 0x53, 0xae, 0x7c, 0x0c, 0x5f}},
|
| 425 |
+
{nil, // 51
|
| 426 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00},
|
| 427 |
+
[]byte{0xce, 0x33, 0x23, 0x29, 0x24, 0x8f, 0x32, 0x28}},
|
| 428 |
+
{nil, // 52
|
| 429 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00},
|
| 430 |
+
[]byte{0x84, 0x05, 0xd1, 0xab, 0xe2, 0x4f, 0xb9, 0x42}},
|
| 431 |
+
{nil, // 53
|
| 432 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00},
|
| 433 |
+
[]byte{0xe6, 0x43, 0xd7, 0x80, 0x90, 0xca, 0x42, 0x07}},
|
| 434 |
+
{nil, // 54
|
| 435 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00},
|
| 436 |
+
[]byte{0x48, 0x22, 0x1b, 0x99, 0x37, 0x74, 0x8a, 0x23}},
|
| 437 |
+
{nil, // 55
|
| 438 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00},
|
| 439 |
+
[]byte{0xdd, 0x7c, 0x0b, 0xbd, 0x61, 0xfa, 0xfd, 0x54}},
|
| 440 |
+
{nil, // 56
|
| 441 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80},
|
| 442 |
+
[]byte{0x2f, 0xbc, 0x29, 0x1a, 0x57, 0x0d, 0xb5, 0xc4}},
|
| 443 |
+
{nil, // 57
|
| 444 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40},
|
| 445 |
+
[]byte{0xe0, 0x7c, 0x30, 0xd7, 0xe4, 0xe2, 0x6e, 0x12}},
|
| 446 |
+
{nil, // 58
|
| 447 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20},
|
| 448 |
+
[]byte{0x09, 0x53, 0xe2, 0x25, 0x8e, 0x8e, 0x90, 0xa1}},
|
| 449 |
+
{nil, // 59
|
| 450 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
|
| 451 |
+
[]byte{0x5b, 0x71, 0x1b, 0xc4, 0xce, 0xeb, 0xf2, 0xee}},
|
| 452 |
+
{nil, // 60
|
| 453 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08},
|
| 454 |
+
[]byte{0xcc, 0x08, 0x3f, 0x1e, 0x6d, 0x9e, 0x85, 0xf6}},
|
| 455 |
+
{nil, // 61
|
| 456 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04},
|
| 457 |
+
[]byte{0xd2, 0xfd, 0x88, 0x67, 0xd5, 0x0d, 0x2d, 0xfe}},
|
| 458 |
+
{nil, // 62
|
| 459 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
|
| 460 |
+
[]byte{0x06, 0xe7, 0xea, 0x22, 0xce, 0x92, 0x70, 0x8f}},
|
| 461 |
+
{nil, // 63
|
| 462 |
+
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
|
| 463 |
+
[]byte{0x16, 0x6b, 0x40, 0xb4, 0x4a, 0xba, 0x4b, 0xd6}},
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
// Plaintext for use with Table A.2 tests
|
| 467 |
+
var tableA2Plaintext = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
| 468 |
+
|
| 469 |
+
// Table A.2 Resulting Ciphertext from the Variable Key Known Answer Test
|
| 470 |
+
var tableA2Tests = []CryptTest{
|
| 471 |
+
{ // 0
|
| 472 |
+
[]byte{
|
| 473 |
+
0x80, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 474 |
+
0x80, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 475 |
+
0x80, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 476 |
+
nil,
|
| 477 |
+
[]byte{0x95, 0xa8, 0xd7, 0x28, 0x13, 0xda, 0xa9, 0x4d}},
|
| 478 |
+
{ // 1
|
| 479 |
+
[]byte{
|
| 480 |
+
0x40, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 481 |
+
0x40, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 482 |
+
0x40, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 483 |
+
nil,
|
| 484 |
+
[]byte{0x0e, 0xec, 0x14, 0x87, 0xdd, 0x8c, 0x26, 0xd5}},
|
| 485 |
+
{ // 2
|
| 486 |
+
[]byte{
|
| 487 |
+
0x20, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 488 |
+
0x20, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 489 |
+
0x20, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 490 |
+
nil,
|
| 491 |
+
[]byte{0x7a, 0xd1, 0x6f, 0xfb, 0x79, 0xc4, 0x59, 0x26}},
|
| 492 |
+
{ // 3
|
| 493 |
+
[]byte{
|
| 494 |
+
0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 495 |
+
0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 496 |
+
0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 497 |
+
nil,
|
| 498 |
+
[]byte{0xd3, 0x74, 0x62, 0x94, 0xca, 0x6a, 0x6c, 0xf3}},
|
| 499 |
+
{ // 4
|
| 500 |
+
[]byte{
|
| 501 |
+
0x08, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 502 |
+
0x08, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 503 |
+
0x08, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 504 |
+
nil,
|
| 505 |
+
[]byte{0x80, 0x9f, 0x5f, 0x87, 0x3c, 0x1f, 0xd7, 0x61}},
|
| 506 |
+
{ // 5
|
| 507 |
+
[]byte{
|
| 508 |
+
0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 509 |
+
0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 510 |
+
0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 511 |
+
nil,
|
| 512 |
+
[]byte{0xc0, 0x2f, 0xaf, 0xfe, 0xc9, 0x89, 0xd1, 0xfc}},
|
| 513 |
+
{ // 6
|
| 514 |
+
[]byte{
|
| 515 |
+
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 516 |
+
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 517 |
+
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 518 |
+
nil,
|
| 519 |
+
[]byte{0x46, 0x15, 0xaa, 0x1d, 0x33, 0xe7, 0x2f, 0x10}},
|
| 520 |
+
{ // 7
|
| 521 |
+
[]byte{
|
| 522 |
+
0x01, 0x80, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 523 |
+
0x01, 0x80, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 524 |
+
0x01, 0x80, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 525 |
+
nil,
|
| 526 |
+
[]byte{0x20, 0x55, 0x12, 0x33, 0x50, 0xc0, 0x08, 0x58}},
|
| 527 |
+
{ // 8
|
| 528 |
+
[]byte{
|
| 529 |
+
0x01, 0x40, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 530 |
+
0x01, 0x40, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 531 |
+
0x01, 0x40, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 532 |
+
nil,
|
| 533 |
+
[]byte{0xdf, 0x3b, 0x99, 0xd6, 0x57, 0x73, 0x97, 0xc8}},
|
| 534 |
+
{ // 9
|
| 535 |
+
[]byte{
|
| 536 |
+
0x01, 0x20, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 537 |
+
0x01, 0x20, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 538 |
+
0x01, 0x20, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 539 |
+
nil,
|
| 540 |
+
[]byte{0x31, 0xfe, 0x17, 0x36, 0x9b, 0x52, 0x88, 0xc9}},
|
| 541 |
+
{ // 10
|
| 542 |
+
[]byte{
|
| 543 |
+
0x01, 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 544 |
+
0x01, 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 545 |
+
0x01, 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 546 |
+
nil,
|
| 547 |
+
[]byte{0xdf, 0xdd, 0x3c, 0xc6, 0x4d, 0xae, 0x16, 0x42}},
|
| 548 |
+
{ // 11
|
| 549 |
+
[]byte{
|
| 550 |
+
0x01, 0x08, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 551 |
+
0x01, 0x08, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 552 |
+
0x01, 0x08, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 553 |
+
nil,
|
| 554 |
+
[]byte{0x17, 0x8c, 0x83, 0xce, 0x2b, 0x39, 0x9d, 0x94}},
|
| 555 |
+
{ // 12
|
| 556 |
+
[]byte{
|
| 557 |
+
0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 558 |
+
0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 559 |
+
0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 560 |
+
nil,
|
| 561 |
+
[]byte{0x50, 0xf6, 0x36, 0x32, 0x4a, 0x9b, 0x7f, 0x80}},
|
| 562 |
+
{ // 13
|
| 563 |
+
[]byte{
|
| 564 |
+
0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 565 |
+
0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 566 |
+
0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 567 |
+
nil,
|
| 568 |
+
[]byte{0xa8, 0x46, 0x8e, 0xe3, 0xbc, 0x18, 0xf0, 0x6d}},
|
| 569 |
+
{ // 14
|
| 570 |
+
[]byte{
|
| 571 |
+
0x01, 0x01, 0x80, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 572 |
+
0x01, 0x01, 0x80, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 573 |
+
0x01, 0x01, 0x80, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 574 |
+
nil,
|
| 575 |
+
[]byte{0xa2, 0xdc, 0x9e, 0x92, 0xfd, 0x3c, 0xde, 0x92}},
|
| 576 |
+
{ // 15
|
| 577 |
+
[]byte{
|
| 578 |
+
0x01, 0x01, 0x40, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 579 |
+
0x01, 0x01, 0x40, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 580 |
+
0x01, 0x01, 0x40, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 581 |
+
nil,
|
| 582 |
+
[]byte{0xca, 0xc0, 0x9f, 0x79, 0x7d, 0x03, 0x12, 0x87}},
|
| 583 |
+
{ // 16
|
| 584 |
+
[]byte{
|
| 585 |
+
0x01, 0x01, 0x20, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 586 |
+
0x01, 0x01, 0x20, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 587 |
+
0x01, 0x01, 0x20, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 588 |
+
nil,
|
| 589 |
+
[]byte{0x90, 0xba, 0x68, 0x0b, 0x22, 0xae, 0xb5, 0x25}},
|
| 590 |
+
{ // 17
|
| 591 |
+
[]byte{
|
| 592 |
+
0x01, 0x01, 0x10, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 593 |
+
0x01, 0x01, 0x10, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 594 |
+
0x01, 0x01, 0x10, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 595 |
+
nil,
|
| 596 |
+
[]byte{0xce, 0x7a, 0x24, 0xf3, 0x50, 0xe2, 0x80, 0xb6}},
|
| 597 |
+
{ // 18
|
| 598 |
+
[]byte{
|
| 599 |
+
0x01, 0x01, 0x08, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 600 |
+
0x01, 0x01, 0x08, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 601 |
+
0x01, 0x01, 0x08, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 602 |
+
nil,
|
| 603 |
+
[]byte{0x88, 0x2b, 0xff, 0x0a, 0xa0, 0x1a, 0x0b, 0x87}},
|
| 604 |
+
{ // 19
|
| 605 |
+
[]byte{
|
| 606 |
+
0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 607 |
+
0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 608 |
+
0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 609 |
+
nil,
|
| 610 |
+
[]byte{0x25, 0x61, 0x02, 0x88, 0x92, 0x45, 0x11, 0xc2}},
|
| 611 |
+
{ // 20
|
| 612 |
+
[]byte{
|
| 613 |
+
0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 614 |
+
0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
|
| 615 |
+
0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01},
|
| 616 |
+
nil,
|
| 617 |
+
[]byte{0xc7, 0x15, 0x16, 0xc2, 0x9c, 0x75, 0xd1, 0x70}},
|
| 618 |
+
{ // 21
|
| 619 |
+
[]byte{
|
| 620 |
+
0x01, 0x01, 0x01, 0x80, 0x01, 0x01, 0x01, 0x01,
|
| 621 |
+
0x01, 0x01, 0x01, 0x80, 0x01, 0x01, 0x01, 0x01,
|
| 622 |
+
0x01, 0x01, 0x01, 0x80, 0x01, 0x01, 0x01, 0x01},
|
| 623 |
+
nil,
|
| 624 |
+
[]byte{0x51, 0x99, 0xc2, 0x9a, 0x52, 0xc9, 0xf0, 0x59}},
|
| 625 |
+
{ // 22
|
| 626 |
+
[]byte{
|
| 627 |
+
0x01, 0x01, 0x01, 0x40, 0x01, 0x01, 0x01, 0x01,
|
| 628 |
+
0x01, 0x01, 0x01, 0x40, 0x01, 0x01, 0x01, 0x01,
|
| 629 |
+
0x01, 0x01, 0x01, 0x40, 0x01, 0x01, 0x01, 0x01},
|
| 630 |
+
nil,
|
| 631 |
+
[]byte{0xc2, 0x2f, 0x0a, 0x29, 0x4a, 0x71, 0xf2, 0x9f}},
|
| 632 |
+
{ // 23
|
| 633 |
+
[]byte{
|
| 634 |
+
0x01, 0x01, 0x01, 0x20, 0x01, 0x01, 0x01, 0x01,
|
| 635 |
+
0x01, 0x01, 0x01, 0x20, 0x01, 0x01, 0x01, 0x01,
|
| 636 |
+
0x01, 0x01, 0x01, 0x20, 0x01, 0x01, 0x01, 0x01},
|
| 637 |
+
nil,
|
| 638 |
+
[]byte{0xee, 0x37, 0x14, 0x83, 0x71, 0x4c, 0x02, 0xea}},
|
| 639 |
+
{ // 24
|
| 640 |
+
[]byte{
|
| 641 |
+
0x01, 0x01, 0x01, 0x10, 0x01, 0x01, 0x01, 0x01,
|
| 642 |
+
0x01, 0x01, 0x01, 0x10, 0x01, 0x01, 0x01, 0x01,
|
| 643 |
+
0x01, 0x01, 0x01, 0x10, 0x01, 0x01, 0x01, 0x01},
|
| 644 |
+
nil,
|
| 645 |
+
[]byte{0xa8, 0x1f, 0xbd, 0x44, 0x8f, 0x9e, 0x52, 0x2f}},
|
| 646 |
+
{ // 25
|
| 647 |
+
[]byte{
|
| 648 |
+
0x01, 0x01, 0x01, 0x08, 0x01, 0x01, 0x01, 0x01,
|
| 649 |
+
0x01, 0x01, 0x01, 0x08, 0x01, 0x01, 0x01, 0x01,
|
| 650 |
+
0x01, 0x01, 0x01, 0x08, 0x01, 0x01, 0x01, 0x01},
|
| 651 |
+
nil,
|
| 652 |
+
[]byte{0x4f, 0x64, 0x4c, 0x92, 0xe1, 0x92, 0xdf, 0xed}},
|
| 653 |
+
{ // 26
|
| 654 |
+
[]byte{
|
| 655 |
+
0x01, 0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x01,
|
| 656 |
+
0x01, 0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x01,
|
| 657 |
+
0x01, 0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x01},
|
| 658 |
+
nil,
|
| 659 |
+
[]byte{0x1a, 0xfa, 0x9a, 0x66, 0xa6, 0xdf, 0x92, 0xae}},
|
| 660 |
+
{ // 27
|
| 661 |
+
[]byte{
|
| 662 |
+
0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01,
|
| 663 |
+
0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01,
|
| 664 |
+
0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01},
|
| 665 |
+
nil,
|
| 666 |
+
[]byte{0xb3, 0xc1, 0xcc, 0x71, 0x5c, 0xb8, 0x79, 0xd8}},
|
| 667 |
+
{ // 28
|
| 668 |
+
[]byte{
|
| 669 |
+
0x01, 0x01, 0x01, 0x01, 0x80, 0x01, 0x01, 0x01,
|
| 670 |
+
0x01, 0x01, 0x01, 0x01, 0x80, 0x01, 0x01, 0x01,
|
| 671 |
+
0x01, 0x01, 0x01, 0x01, 0x80, 0x01, 0x01, 0x01},
|
| 672 |
+
nil,
|
| 673 |
+
[]byte{0x19, 0xd0, 0x32, 0xe6, 0x4a, 0xb0, 0xbd, 0x8b}},
|
| 674 |
+
{ // 29
|
| 675 |
+
[]byte{
|
| 676 |
+
0x01, 0x01, 0x01, 0x01, 0x40, 0x01, 0x01, 0x01,
|
| 677 |
+
0x01, 0x01, 0x01, 0x01, 0x40, 0x01, 0x01, 0x01,
|
| 678 |
+
0x01, 0x01, 0x01, 0x01, 0x40, 0x01, 0x01, 0x01},
|
| 679 |
+
nil,
|
| 680 |
+
[]byte{0x3c, 0xfa, 0xa7, 0xa7, 0xdc, 0x87, 0x20, 0xdc}},
|
| 681 |
+
{ // 30
|
| 682 |
+
[]byte{
|
| 683 |
+
0x01, 0x01, 0x01, 0x01, 0x20, 0x01, 0x01, 0x01,
|
| 684 |
+
0x01, 0x01, 0x01, 0x01, 0x20, 0x01, 0x01, 0x01,
|
| 685 |
+
0x01, 0x01, 0x01, 0x01, 0x20, 0x01, 0x01, 0x01},
|
| 686 |
+
nil,
|
| 687 |
+
[]byte{0xb7, 0x26, 0x5f, 0x7f, 0x44, 0x7a, 0xc6, 0xf3}},
|
| 688 |
+
{ // 31
|
| 689 |
+
[]byte{
|
| 690 |
+
0x01, 0x01, 0x01, 0x01, 0x10, 0x01, 0x01, 0x01,
|
| 691 |
+
0x01, 0x01, 0x01, 0x01, 0x10, 0x01, 0x01, 0x01,
|
| 692 |
+
0x01, 0x01, 0x01, 0x01, 0x10, 0x01, 0x01, 0x01},
|
| 693 |
+
nil,
|
| 694 |
+
[]byte{0x9d, 0xb7, 0x3b, 0x3c, 0x0d, 0x16, 0x3f, 0x54}},
|
| 695 |
+
{ // 32
|
| 696 |
+
[]byte{
|
| 697 |
+
0x01, 0x01, 0x01, 0x01, 0x08, 0x01, 0x01, 0x01,
|
| 698 |
+
0x01, 0x01, 0x01, 0x01, 0x08, 0x01, 0x01, 0x01,
|
| 699 |
+
0x01, 0x01, 0x01, 0x01, 0x08, 0x01, 0x01, 0x01},
|
| 700 |
+
nil,
|
| 701 |
+
[]byte{0x81, 0x81, 0xb6, 0x5b, 0xab, 0xf4, 0xa9, 0x75}},
|
| 702 |
+
{ // 33
|
| 703 |
+
[]byte{
|
| 704 |
+
0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x01, 0x01,
|
| 705 |
+
0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x01, 0x01,
|
| 706 |
+
0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x01, 0x01},
|
| 707 |
+
nil,
|
| 708 |
+
[]byte{0x93, 0xc9, 0xb6, 0x40, 0x42, 0xea, 0xa2, 0x40}},
|
| 709 |
+
{ // 34
|
| 710 |
+
[]byte{
|
| 711 |
+
0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01,
|
| 712 |
+
0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01,
|
| 713 |
+
0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01},
|
| 714 |
+
nil,
|
| 715 |
+
[]byte{0x55, 0x70, 0x53, 0x08, 0x29, 0x70, 0x55, 0x92}},
|
| 716 |
+
{ // 35
|
| 717 |
+
[]byte{
|
| 718 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x80, 0x01, 0x01,
|
| 719 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x80, 0x01, 0x01,
|
| 720 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x80, 0x01, 0x01},
|
| 721 |
+
nil,
|
| 722 |
+
[]byte{0x86, 0x38, 0x80, 0x9e, 0x87, 0x87, 0x87, 0xa0}},
|
| 723 |
+
{ // 36
|
| 724 |
+
[]byte{
|
| 725 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x40, 0x01, 0x01,
|
| 726 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x40, 0x01, 0x01,
|
| 727 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x40, 0x01, 0x01},
|
| 728 |
+
nil,
|
| 729 |
+
[]byte{0x41, 0xb9, 0xa7, 0x9a, 0xf7, 0x9a, 0xc2, 0x08}},
|
| 730 |
+
{ // 37
|
| 731 |
+
[]byte{
|
| 732 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x20, 0x01, 0x01,
|
| 733 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x20, 0x01, 0x01,
|
| 734 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x20, 0x01, 0x01},
|
| 735 |
+
nil,
|
| 736 |
+
[]byte{0x7a, 0x9b, 0xe4, 0x2f, 0x20, 0x09, 0xa8, 0x92}},
|
| 737 |
+
{ // 38
|
| 738 |
+
[]byte{
|
| 739 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x10, 0x01, 0x01,
|
| 740 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x10, 0x01, 0x01,
|
| 741 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x10, 0x01, 0x01},
|
| 742 |
+
nil,
|
| 743 |
+
[]byte{0x29, 0x03, 0x8d, 0x56, 0xba, 0x6d, 0x27, 0x45}},
|
| 744 |
+
{ // 39
|
| 745 |
+
[]byte{
|
| 746 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x01, 0x01,
|
| 747 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x01, 0x01,
|
| 748 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x01, 0x01},
|
| 749 |
+
nil,
|
| 750 |
+
[]byte{0x54, 0x95, 0xc6, 0xab, 0xf1, 0xe5, 0xdf, 0x51}},
|
| 751 |
+
{ // 40
|
| 752 |
+
[]byte{
|
| 753 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x01,
|
| 754 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x01,
|
| 755 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x01},
|
| 756 |
+
nil,
|
| 757 |
+
[]byte{0xae, 0x13, 0xdb, 0xd5, 0x61, 0x48, 0x89, 0x33}},
|
| 758 |
+
{ // 41
|
| 759 |
+
[]byte{
|
| 760 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01,
|
| 761 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01,
|
| 762 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01},
|
| 763 |
+
nil,
|
| 764 |
+
[]byte{0x02, 0x4d, 0x1f, 0xfa, 0x89, 0x04, 0xe3, 0x89}},
|
| 765 |
+
{ // 42
|
| 766 |
+
[]byte{
|
| 767 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x80, 0x01,
|
| 768 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x80, 0x01,
|
| 769 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x80, 0x01},
|
| 770 |
+
nil,
|
| 771 |
+
[]byte{0xd1, 0x39, 0x97, 0x12, 0xf9, 0x9b, 0xf0, 0x2e}},
|
| 772 |
+
{ // 43
|
| 773 |
+
[]byte{
|
| 774 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x40, 0x01,
|
| 775 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x40, 0x01,
|
| 776 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x40, 0x01},
|
| 777 |
+
nil,
|
| 778 |
+
[]byte{0x14, 0xc1, 0xd7, 0xc1, 0xcf, 0xfe, 0xc7, 0x9e}},
|
| 779 |
+
{ // 44
|
| 780 |
+
[]byte{
|
| 781 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x20, 0x01,
|
| 782 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x20, 0x01,
|
| 783 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x20, 0x01},
|
| 784 |
+
nil,
|
| 785 |
+
[]byte{0x1d, 0xe5, 0x27, 0x9d, 0xae, 0x3b, 0xed, 0x6f}},
|
| 786 |
+
{ // 45
|
| 787 |
+
[]byte{
|
| 788 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x10, 0x01,
|
| 789 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x10, 0x01,
|
| 790 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x10, 0x01},
|
| 791 |
+
nil,
|
| 792 |
+
[]byte{0xe9, 0x41, 0xa3, 0x3f, 0x85, 0x50, 0x13, 0x03}},
|
| 793 |
+
{ // 46
|
| 794 |
+
[]byte{
|
| 795 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x01,
|
| 796 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x01,
|
| 797 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x01},
|
| 798 |
+
nil,
|
| 799 |
+
[]byte{0xda, 0x99, 0xdb, 0xbc, 0x9a, 0x03, 0xf3, 0x79}},
|
| 800 |
+
{ // 47
|
| 801 |
+
[]byte{
|
| 802 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01,
|
| 803 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01,
|
| 804 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01},
|
| 805 |
+
nil,
|
| 806 |
+
[]byte{0xb7, 0xfc, 0x92, 0xf9, 0x1d, 0x8e, 0x92, 0xe9}},
|
| 807 |
+
{ // 48
|
| 808 |
+
[]byte{
|
| 809 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01,
|
| 810 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01,
|
| 811 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01},
|
| 812 |
+
nil,
|
| 813 |
+
[]byte{0xae, 0x8e, 0x5c, 0xaa, 0x3c, 0xa0, 0x4e, 0x85}},
|
| 814 |
+
{ // 49
|
| 815 |
+
[]byte{
|
| 816 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x80,
|
| 817 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x80,
|
| 818 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x80},
|
| 819 |
+
nil,
|
| 820 |
+
[]byte{0x9c, 0xc6, 0x2d, 0xf4, 0x3b, 0x6e, 0xed, 0x74}},
|
| 821 |
+
{ // 50
|
| 822 |
+
[]byte{
|
| 823 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x40,
|
| 824 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x40,
|
| 825 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x40},
|
| 826 |
+
nil,
|
| 827 |
+
[]byte{0xd8, 0x63, 0xdb, 0xb5, 0xc5, 0x9a, 0x91, 0xa0}},
|
| 828 |
+
{ // 50
|
| 829 |
+
[]byte{
|
| 830 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x20,
|
| 831 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x20,
|
| 832 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x20},
|
| 833 |
+
nil,
|
| 834 |
+
[]byte{0xa1, 0xab, 0x21, 0x90, 0x54, 0x5b, 0x91, 0xd7}},
|
| 835 |
+
{ // 52
|
| 836 |
+
[]byte{
|
| 837 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x10,
|
| 838 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x10,
|
| 839 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x10},
|
| 840 |
+
nil,
|
| 841 |
+
[]byte{0x08, 0x75, 0x04, 0x1e, 0x64, 0xc5, 0x70, 0xf7}},
|
| 842 |
+
{ // 53
|
| 843 |
+
[]byte{
|
| 844 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08,
|
| 845 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08,
|
| 846 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08},
|
| 847 |
+
nil,
|
| 848 |
+
[]byte{0x5a, 0x59, 0x45, 0x28, 0xbe, 0xbe, 0xf1, 0xcc}},
|
| 849 |
+
{ // 54
|
| 850 |
+
[]byte{
|
| 851 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04,
|
| 852 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04,
|
| 853 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04},
|
| 854 |
+
nil,
|
| 855 |
+
[]byte{0xfc, 0xdb, 0x32, 0x91, 0xde, 0x21, 0xf0, 0xc0}},
|
| 856 |
+
{ // 55
|
| 857 |
+
[]byte{
|
| 858 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02,
|
| 859 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02,
|
| 860 |
+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02},
|
| 861 |
+
nil,
|
| 862 |
+
[]byte{0x86, 0x9e, 0xfd, 0x7f, 0x9f, 0x26, 0x5a, 0x09}},
|
| 863 |
+
}
|
| 864 |
+
|
| 865 |
+
// Plaintext for use with Table A.3 tests
|
| 866 |
+
var tableA3Plaintext = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
| 867 |
+
|
| 868 |
+
// Table A.3 Values To Be Used for the Permutation Operation Known Answer Test
|
| 869 |
+
var tableA3Tests = []CryptTest{
|
| 870 |
+
{ // 0
|
| 871 |
+
[]byte{
|
| 872 |
+
0x10, 0x46, 0x91, 0x34, 0x89, 0x98, 0x01, 0x31,
|
| 873 |
+
0x10, 0x46, 0x91, 0x34, 0x89, 0x98, 0x01, 0x31,
|
| 874 |
+
0x10, 0x46, 0x91, 0x34, 0x89, 0x98, 0x01, 0x31,
|
| 875 |
+
},
|
| 876 |
+
nil,
|
| 877 |
+
[]byte{0x88, 0xd5, 0x5e, 0x54, 0xf5, 0x4c, 0x97, 0xb4}},
|
| 878 |
+
{ // 1
|
| 879 |
+
[]byte{
|
| 880 |
+
0x10, 0x07, 0x10, 0x34, 0x89, 0x98, 0x80, 0x20,
|
| 881 |
+
0x10, 0x07, 0x10, 0x34, 0x89, 0x98, 0x80, 0x20,
|
| 882 |
+
0x10, 0x07, 0x10, 0x34, 0x89, 0x98, 0x80, 0x20,
|
| 883 |
+
},
|
| 884 |
+
nil,
|
| 885 |
+
[]byte{0x0c, 0x0c, 0xc0, 0x0c, 0x83, 0xea, 0x48, 0xfd}},
|
| 886 |
+
{ // 2
|
| 887 |
+
[]byte{
|
| 888 |
+
0x10, 0x07, 0x10, 0x34, 0xc8, 0x98, 0x01, 0x20,
|
| 889 |
+
0x10, 0x07, 0x10, 0x34, 0xc8, 0x98, 0x01, 0x20,
|
| 890 |
+
0x10, 0x07, 0x10, 0x34, 0xc8, 0x98, 0x01, 0x20,
|
| 891 |
+
},
|
| 892 |
+
nil,
|
| 893 |
+
[]byte{0x83, 0xbc, 0x8e, 0xf3, 0xa6, 0x57, 0x01, 0x83}},
|
| 894 |
+
{ // 3
|
| 895 |
+
[]byte{
|
| 896 |
+
0x10, 0x46, 0x10, 0x34, 0x89, 0x98, 0x80, 0x20,
|
| 897 |
+
0x10, 0x46, 0x10, 0x34, 0x89, 0x98, 0x80, 0x20,
|
| 898 |
+
0x10, 0x46, 0x10, 0x34, 0x89, 0x98, 0x80, 0x20,
|
| 899 |
+
},
|
| 900 |
+
nil,
|
| 901 |
+
[]byte{0xdf, 0x72, 0x5d, 0xca, 0xd9, 0x4e, 0xa2, 0xe9}},
|
| 902 |
+
{ // 4
|
| 903 |
+
[]byte{
|
| 904 |
+
0x10, 0x86, 0x91, 0x15, 0x19, 0x19, 0x01, 0x01,
|
| 905 |
+
0x10, 0x86, 0x91, 0x15, 0x19, 0x19, 0x01, 0x01,
|
| 906 |
+
0x10, 0x86, 0x91, 0x15, 0x19, 0x19, 0x01, 0x01,
|
| 907 |
+
},
|
| 908 |
+
nil,
|
| 909 |
+
[]byte{0xe6, 0x52, 0xb5, 0x3b, 0x55, 0x0b, 0xe8, 0xb0}},
|
| 910 |
+
{ // 5
|
| 911 |
+
[]byte{
|
| 912 |
+
0x10, 0x86, 0x91, 0x15, 0x19, 0x58, 0x01, 0x01,
|
| 913 |
+
0x10, 0x86, 0x91, 0x15, 0x19, 0x58, 0x01, 0x01,
|
| 914 |
+
0x10, 0x86, 0x91, 0x15, 0x19, 0x58, 0x01, 0x01,
|
| 915 |
+
},
|
| 916 |
+
nil,
|
| 917 |
+
[]byte{0xaf, 0x52, 0x71, 0x20, 0xc4, 0x85, 0xcb, 0xb0}},
|
| 918 |
+
{ // 6
|
| 919 |
+
[]byte{
|
| 920 |
+
0x51, 0x07, 0xb0, 0x15, 0x19, 0x58, 0x01, 0x01,
|
| 921 |
+
0x51, 0x07, 0xb0, 0x15, 0x19, 0x58, 0x01, 0x01,
|
| 922 |
+
0x51, 0x07, 0xb0, 0x15, 0x19, 0x58, 0x01, 0x01,
|
| 923 |
+
},
|
| 924 |
+
nil,
|
| 925 |
+
[]byte{0x0f, 0x04, 0xce, 0x39, 0x3d, 0xb9, 0x26, 0xd5}},
|
| 926 |
+
{ // 7
|
| 927 |
+
[]byte{
|
| 928 |
+
0x10, 0x07, 0xb0, 0x15, 0x19, 0x19, 0x01, 0x01,
|
| 929 |
+
0x10, 0x07, 0xb0, 0x15, 0x19, 0x19, 0x01, 0x01,
|
| 930 |
+
0x10, 0x07, 0xb0, 0x15, 0x19, 0x19, 0x01, 0x01,
|
| 931 |
+
},
|
| 932 |
+
nil,
|
| 933 |
+
[]byte{0xc9, 0xf0, 0x0f, 0xfc, 0x74, 0x07, 0x90, 0x67}},
|
| 934 |
+
{ // 8
|
| 935 |
+
[]byte{
|
| 936 |
+
0x31, 0x07, 0x91, 0x54, 0x98, 0x08, 0x01, 0x01,
|
| 937 |
+
0x31, 0x07, 0x91, 0x54, 0x98, 0x08, 0x01, 0x01,
|
| 938 |
+
0x31, 0x07, 0x91, 0x54, 0x98, 0x08, 0x01, 0x01,
|
| 939 |
+
},
|
| 940 |
+
nil,
|
| 941 |
+
[]byte{0x7c, 0xfd, 0x82, 0xa5, 0x93, 0x25, 0x2b, 0x4e}},
|
| 942 |
+
{ // 9
|
| 943 |
+
[]byte{
|
| 944 |
+
0x31, 0x07, 0x91, 0x94, 0x98, 0x08, 0x01, 0x01,
|
| 945 |
+
0x31, 0x07, 0x91, 0x94, 0x98, 0x08, 0x01, 0x01,
|
| 946 |
+
0x31, 0x07, 0x91, 0x94, 0x98, 0x08, 0x01, 0x01,
|
| 947 |
+
},
|
| 948 |
+
nil,
|
| 949 |
+
[]byte{0xcb, 0x49, 0xa2, 0xf9, 0xe9, 0x13, 0x63, 0xe3}},
|
| 950 |
+
{ // 10
|
| 951 |
+
[]byte{
|
| 952 |
+
0x10, 0x07, 0x91, 0x15, 0xb9, 0x08, 0x01, 0x40,
|
| 953 |
+
0x10, 0x07, 0x91, 0x15, 0xb9, 0x08, 0x01, 0x40,
|
| 954 |
+
0x10, 0x07, 0x91, 0x15, 0xb9, 0x08, 0x01, 0x40,
|
| 955 |
+
},
|
| 956 |
+
nil,
|
| 957 |
+
[]byte{0x00, 0xb5, 0x88, 0xbe, 0x70, 0xd2, 0x3f, 0x56}},
|
| 958 |
+
{ // 11
|
| 959 |
+
[]byte{
|
| 960 |
+
0x31, 0x07, 0x91, 0x15, 0x98, 0x08, 0x01, 0x40,
|
| 961 |
+
0x31, 0x07, 0x91, 0x15, 0x98, 0x08, 0x01, 0x40,
|
| 962 |
+
0x31, 0x07, 0x91, 0x15, 0x98, 0x08, 0x01, 0x40,
|
| 963 |
+
},
|
| 964 |
+
nil,
|
| 965 |
+
[]byte{0x40, 0x6a, 0x9a, 0x6a, 0xb4, 0x33, 0x99, 0xae}},
|
| 966 |
+
{ // 12
|
| 967 |
+
[]byte{
|
| 968 |
+
0x10, 0x07, 0xd0, 0x15, 0x89, 0x98, 0x01, 0x01,
|
| 969 |
+
0x10, 0x07, 0xd0, 0x15, 0x89, 0x98, 0x01, 0x01,
|
| 970 |
+
0x10, 0x07, 0xd0, 0x15, 0x89, 0x98, 0x01, 0x01,
|
| 971 |
+
},
|
| 972 |
+
nil,
|
| 973 |
+
[]byte{0x6c, 0xb7, 0x73, 0x61, 0x1d, 0xca, 0x9a, 0xda}},
|
| 974 |
+
{ // 13
|
| 975 |
+
[]byte{
|
| 976 |
+
0x91, 0x07, 0x91, 0x15, 0x89, 0x98, 0x01, 0x01,
|
| 977 |
+
0x91, 0x07, 0x91, 0x15, 0x89, 0x98, 0x01, 0x01,
|
| 978 |
+
0x91, 0x07, 0x91, 0x15, 0x89, 0x98, 0x01, 0x01,
|
| 979 |
+
},
|
| 980 |
+
nil,
|
| 981 |
+
[]byte{0x67, 0xfd, 0x21, 0xc1, 0x7d, 0xbb, 0x5d, 0x70}},
|
| 982 |
+
{ // 14
|
| 983 |
+
[]byte{
|
| 984 |
+
0x91, 0x07, 0xd0, 0x15, 0x89, 0x19, 0x01, 0x01,
|
| 985 |
+
0x91, 0x07, 0xd0, 0x15, 0x89, 0x19, 0x01, 0x01,
|
| 986 |
+
0x91, 0x07, 0xd0, 0x15, 0x89, 0x19, 0x01, 0x01,
|
| 987 |
+
},
|
| 988 |
+
nil,
|
| 989 |
+
[]byte{0x95, 0x92, 0xcb, 0x41, 0x10, 0x43, 0x07, 0x87}},
|
| 990 |
+
{ // 15
|
| 991 |
+
[]byte{
|
| 992 |
+
0x10, 0x07, 0xd0, 0x15, 0x98, 0x98, 0x01, 0x20,
|
| 993 |
+
0x10, 0x07, 0xd0, 0x15, 0x98, 0x98, 0x01, 0x20,
|
| 994 |
+
0x10, 0x07, 0xd0, 0x15, 0x98, 0x98, 0x01, 0x20,
|
| 995 |
+
},
|
| 996 |
+
nil,
|
| 997 |
+
[]byte{0xa6, 0xb7, 0xff, 0x68, 0xa3, 0x18, 0xdd, 0xd3}},
|
| 998 |
+
{ // 16
|
| 999 |
+
[]byte{
|
| 1000 |
+
0x10, 0x07, 0x94, 0x04, 0x98, 0x19, 0x01, 0x01,
|
| 1001 |
+
0x10, 0x07, 0x94, 0x04, 0x98, 0x19, 0x01, 0x01,
|
| 1002 |
+
0x10, 0x07, 0x94, 0x04, 0x98, 0x19, 0x01, 0x01,
|
| 1003 |
+
},
|
| 1004 |
+
nil,
|
| 1005 |
+
[]byte{0x4d, 0x10, 0x21, 0x96, 0xc9, 0x14, 0xca, 0x16}},
|
| 1006 |
+
{ // 17
|
| 1007 |
+
[]byte{
|
| 1008 |
+
0x01, 0x07, 0x91, 0x04, 0x91, 0x19, 0x04, 0x01,
|
| 1009 |
+
0x01, 0x07, 0x91, 0x04, 0x91, 0x19, 0x04, 0x01,
|
| 1010 |
+
0x01, 0x07, 0x91, 0x04, 0x91, 0x19, 0x04, 0x01,
|
| 1011 |
+
},
|
| 1012 |
+
nil,
|
| 1013 |
+
[]byte{0x2d, 0xfa, 0x9f, 0x45, 0x73, 0x59, 0x49, 0x65}},
|
| 1014 |
+
{ // 18
|
| 1015 |
+
[]byte{
|
| 1016 |
+
0x01, 0x07, 0x91, 0x04, 0x91, 0x19, 0x01, 0x01,
|
| 1017 |
+
0x01, 0x07, 0x91, 0x04, 0x91, 0x19, 0x01, 0x01,
|
| 1018 |
+
0x01, 0x07, 0x91, 0x04, 0x91, 0x19, 0x01, 0x01,
|
| 1019 |
+
},
|
| 1020 |
+
nil,
|
| 1021 |
+
[]byte{0xb4, 0x66, 0x04, 0x81, 0x6c, 0x0e, 0x07, 0x74}},
|
| 1022 |
+
{ // 19
|
| 1023 |
+
[]byte{
|
| 1024 |
+
0x01, 0x07, 0x94, 0x04, 0x91, 0x19, 0x04, 0x01,
|
| 1025 |
+
0x01, 0x07, 0x94, 0x04, 0x91, 0x19, 0x04, 0x01,
|
| 1026 |
+
0x01, 0x07, 0x94, 0x04, 0x91, 0x19, 0x04, 0x01,
|
| 1027 |
+
},
|
| 1028 |
+
nil,
|
| 1029 |
+
[]byte{0x6e, 0x7e, 0x62, 0x21, 0xa4, 0xf3, 0x4e, 0x87}},
|
| 1030 |
+
{ // 20
|
| 1031 |
+
[]byte{
|
| 1032 |
+
0x19, 0x07, 0x92, 0x10, 0x98, 0x1a, 0x01, 0x01,
|
| 1033 |
+
0x19, 0x07, 0x92, 0x10, 0x98, 0x1a, 0x01, 0x01,
|
| 1034 |
+
0x19, 0x07, 0x92, 0x10, 0x98, 0x1a, 0x01, 0x01,
|
| 1035 |
+
},
|
| 1036 |
+
nil,
|
| 1037 |
+
[]byte{0xaa, 0x85, 0xe7, 0x46, 0x43, 0x23, 0x31, 0x99}},
|
| 1038 |
+
{ // 21
|
| 1039 |
+
[]byte{
|
| 1040 |
+
0x10, 0x07, 0x91, 0x19, 0x98, 0x19, 0x08, 0x01,
|
| 1041 |
+
0x10, 0x07, 0x91, 0x19, 0x98, 0x19, 0x08, 0x01,
|
| 1042 |
+
0x10, 0x07, 0x91, 0x19, 0x98, 0x19, 0x08, 0x01,
|
| 1043 |
+
},
|
| 1044 |
+
nil,
|
| 1045 |
+
[]byte{0x2e, 0x5a, 0x19, 0xdb, 0x4d, 0x19, 0x62, 0xd6}},
|
| 1046 |
+
{ // 22
|
| 1047 |
+
[]byte{
|
| 1048 |
+
0x10, 0x07, 0x91, 0x19, 0x98, 0x1a, 0x08, 0x01,
|
| 1049 |
+
0x10, 0x07, 0x91, 0x19, 0x98, 0x1a, 0x08, 0x01,
|
| 1050 |
+
0x10, 0x07, 0x91, 0x19, 0x98, 0x1a, 0x08, 0x01,
|
| 1051 |
+
},
|
| 1052 |
+
nil,
|
| 1053 |
+
[]byte{0x23, 0xa8, 0x66, 0xa8, 0x09, 0xd3, 0x08, 0x94}},
|
| 1054 |
+
{ // 23
|
| 1055 |
+
[]byte{
|
| 1056 |
+
0x10, 0x07, 0x92, 0x10, 0x98, 0x19, 0x01, 0x01,
|
| 1057 |
+
0x10, 0x07, 0x92, 0x10, 0x98, 0x19, 0x01, 0x01,
|
| 1058 |
+
0x10, 0x07, 0x92, 0x10, 0x98, 0x19, 0x01, 0x01,
|
| 1059 |
+
},
|
| 1060 |
+
nil,
|
| 1061 |
+
[]byte{0xd8, 0x12, 0xd9, 0x61, 0xf0, 0x17, 0xd3, 0x20}},
|
| 1062 |
+
{ // 24
|
| 1063 |
+
[]byte{
|
| 1064 |
+
0x10, 0x07, 0x91, 0x15, 0x98, 0x19, 0x01, 0x0b,
|
| 1065 |
+
0x10, 0x07, 0x91, 0x15, 0x98, 0x19, 0x01, 0x0b,
|
| 1066 |
+
0x10, 0x07, 0x91, 0x15, 0x98, 0x19, 0x01, 0x0b,
|
| 1067 |
+
},
|
| 1068 |
+
nil,
|
| 1069 |
+
[]byte{0x05, 0x56, 0x05, 0x81, 0x6e, 0x58, 0x60, 0x8f}},
|
| 1070 |
+
{ // 25
|
| 1071 |
+
[]byte{
|
| 1072 |
+
0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x01,
|
| 1073 |
+
0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x01,
|
| 1074 |
+
0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x01,
|
| 1075 |
+
},
|
| 1076 |
+
nil,
|
| 1077 |
+
[]byte{0xab, 0xd8, 0x8e, 0x8b, 0x1b, 0x77, 0x16, 0xf1}},
|
| 1078 |
+
{ // 26
|
| 1079 |
+
[]byte{
|
| 1080 |
+
0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x02,
|
| 1081 |
+
0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x02,
|
| 1082 |
+
0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x02,
|
| 1083 |
+
},
|
| 1084 |
+
nil,
|
| 1085 |
+
[]byte{0x53, 0x7a, 0xc9, 0x5b, 0xe6, 0x9d, 0xa1, 0xe1}},
|
| 1086 |
+
{ // 27
|
| 1087 |
+
[]byte{
|
| 1088 |
+
0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x08,
|
| 1089 |
+
0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x08,
|
| 1090 |
+
0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x08,
|
| 1091 |
+
},
|
| 1092 |
+
nil,
|
| 1093 |
+
[]byte{0xae, 0xd0, 0xf6, 0xae, 0x3c, 0x25, 0xcd, 0xd8}},
|
| 1094 |
+
{ // 28
|
| 1095 |
+
[]byte{
|
| 1096 |
+
0x10, 0x02, 0x91, 0x15, 0x98, 0x10, 0x01, 0x04,
|
| 1097 |
+
0x10, 0x02, 0x91, 0x15, 0x98, 0x10, 0x01, 0x04,
|
| 1098 |
+
0x10, 0x02, 0x91, 0x15, 0x98, 0x10, 0x01, 0x04,
|
| 1099 |
+
},
|
| 1100 |
+
nil,
|
| 1101 |
+
[]byte{0xb3, 0xe3, 0x5a, 0x5e, 0xe5, 0x3e, 0x7b, 0x8d}},
|
| 1102 |
+
{ // 29
|
| 1103 |
+
[]byte{
|
| 1104 |
+
0x10, 0x02, 0x91, 0x15, 0x98, 0x19, 0x01, 0x04,
|
| 1105 |
+
0x10, 0x02, 0x91, 0x15, 0x98, 0x19, 0x01, 0x04,
|
| 1106 |
+
0x10, 0x02, 0x91, 0x15, 0x98, 0x19, 0x01, 0x04,
|
| 1107 |
+
},
|
| 1108 |
+
nil,
|
| 1109 |
+
[]byte{0x61, 0xc7, 0x9c, 0x71, 0x92, 0x1a, 0x2e, 0xf8}},
|
| 1110 |
+
{ // 30
|
| 1111 |
+
[]byte{
|
| 1112 |
+
0x10, 0x02, 0x91, 0x15, 0x98, 0x10, 0x02, 0x01,
|
| 1113 |
+
0x10, 0x02, 0x91, 0x15, 0x98, 0x10, 0x02, 0x01,
|
| 1114 |
+
0x10, 0x02, 0x91, 0x15, 0x98, 0x10, 0x02, 0x01,
|
| 1115 |
+
},
|
| 1116 |
+
nil,
|
| 1117 |
+
[]byte{0xe2, 0xf5, 0x72, 0x8f, 0x09, 0x95, 0x01, 0x3c}},
|
| 1118 |
+
{ // 31
|
| 1119 |
+
[]byte{
|
| 1120 |
+
0x10, 0x02, 0x91, 0x16, 0x98, 0x10, 0x01, 0x01,
|
| 1121 |
+
0x10, 0x02, 0x91, 0x16, 0x98, 0x10, 0x01, 0x01,
|
| 1122 |
+
0x10, 0x02, 0x91, 0x16, 0x98, 0x10, 0x01, 0x01,
|
| 1123 |
+
},
|
| 1124 |
+
nil,
|
| 1125 |
+
[]byte{0x1a, 0xea, 0xc3, 0x9a, 0x61, 0xf0, 0xa4, 0x64}},
|
| 1126 |
+
}
|
| 1127 |
+
|
| 1128 |
+
// Table A.4 Values To Be Used for the Substitution Table Known Answer Test
|
| 1129 |
+
var tableA4Tests = []CryptTest{
|
| 1130 |
+
{ // 0
|
| 1131 |
+
[]byte{
|
| 1132 |
+
0x7c, 0xa1, 0x10, 0x45, 0x4a, 0x1a, 0x6e, 0x57,
|
| 1133 |
+
0x7c, 0xa1, 0x10, 0x45, 0x4a, 0x1a, 0x6e, 0x57,
|
| 1134 |
+
0x7c, 0xa1, 0x10, 0x45, 0x4a, 0x1a, 0x6e, 0x57},
|
| 1135 |
+
[]byte{0x01, 0xa1, 0xd6, 0xd0, 0x39, 0x77, 0x67, 0x42},
|
| 1136 |
+
[]byte{0x69, 0x0f, 0x5b, 0x0d, 0x9a, 0x26, 0x93, 0x9b}},
|
| 1137 |
+
{ // 1
|
| 1138 |
+
[]byte{
|
| 1139 |
+
0x01, 0x31, 0xd9, 0x61, 0x9d, 0xc1, 0x37, 0x6e,
|
| 1140 |
+
0x01, 0x31, 0xd9, 0x61, 0x9d, 0xc1, 0x37, 0x6e,
|
| 1141 |
+
0x01, 0x31, 0xd9, 0x61, 0x9d, 0xc1, 0x37, 0x6e},
|
| 1142 |
+
[]byte{0x5c, 0xd5, 0x4c, 0xa8, 0x3d, 0xef, 0x57, 0xda},
|
| 1143 |
+
[]byte{0x7a, 0x38, 0x9d, 0x10, 0x35, 0x4b, 0xd2, 0x71}},
|
| 1144 |
+
{ // 2
|
| 1145 |
+
[]byte{
|
| 1146 |
+
0x07, 0xa1, 0x13, 0x3e, 0x4a, 0x0b, 0x26, 0x86,
|
| 1147 |
+
0x07, 0xa1, 0x13, 0x3e, 0x4a, 0x0b, 0x26, 0x86,
|
| 1148 |
+
0x07, 0xa1, 0x13, 0x3e, 0x4a, 0x0b, 0x26, 0x86},
|
| 1149 |
+
[]byte{0x02, 0x48, 0xd4, 0x38, 0x06, 0xf6, 0x71, 0x72},
|
| 1150 |
+
[]byte{0x86, 0x8e, 0xbb, 0x51, 0xca, 0xb4, 0x59, 0x9a}},
|
| 1151 |
+
{ // 3
|
| 1152 |
+
[]byte{
|
| 1153 |
+
0x38, 0x49, 0x67, 0x4c, 0x26, 0x02, 0x31, 0x9e,
|
| 1154 |
+
0x38, 0x49, 0x67, 0x4c, 0x26, 0x02, 0x31, 0x9e,
|
| 1155 |
+
0x38, 0x49, 0x67, 0x4c, 0x26, 0x02, 0x31, 0x9e},
|
| 1156 |
+
[]byte{0x51, 0x45, 0x4b, 0x58, 0x2d, 0xdf, 0x44, 0x0a},
|
| 1157 |
+
[]byte{0x71, 0x78, 0x87, 0x6e, 0x01, 0xf1, 0x9b, 0x2a}},
|
| 1158 |
+
{ // 4
|
| 1159 |
+
[]byte{
|
| 1160 |
+
0x04, 0xb9, 0x15, 0xba, 0x43, 0xfe, 0xb5, 0xb6,
|
| 1161 |
+
0x04, 0xb9, 0x15, 0xba, 0x43, 0xfe, 0xb5, 0xb6,
|
| 1162 |
+
0x04, 0xb9, 0x15, 0xba, 0x43, 0xfe, 0xb5, 0xb6},
|
| 1163 |
+
[]byte{0x42, 0xfd, 0x44, 0x30, 0x59, 0x57, 0x7f, 0xa2},
|
| 1164 |
+
[]byte{0xaf, 0x37, 0xfb, 0x42, 0x1f, 0x8c, 0x40, 0x95}},
|
| 1165 |
+
{ // 5
|
| 1166 |
+
[]byte{
|
| 1167 |
+
0x01, 0x13, 0xb9, 0x70, 0xfd, 0x34, 0xf2, 0xce,
|
| 1168 |
+
0x01, 0x13, 0xb9, 0x70, 0xfd, 0x34, 0xf2, 0xce,
|
| 1169 |
+
0x01, 0x13, 0xb9, 0x70, 0xfd, 0x34, 0xf2, 0xce},
|
| 1170 |
+
[]byte{0x05, 0x9b, 0x5e, 0x08, 0x51, 0xcf, 0x14, 0x3a},
|
| 1171 |
+
[]byte{0x86, 0xa5, 0x60, 0xf1, 0x0e, 0xc6, 0xd8, 0x5b}},
|
| 1172 |
+
{ // 6
|
| 1173 |
+
[]byte{
|
| 1174 |
+
0x01, 0x70, 0xf1, 0x75, 0x46, 0x8f, 0xb5, 0xe6,
|
| 1175 |
+
0x01, 0x70, 0xf1, 0x75, 0x46, 0x8f, 0xb5, 0xe6,
|
| 1176 |
+
0x01, 0x70, 0xf1, 0x75, 0x46, 0x8f, 0xb5, 0xe6},
|
| 1177 |
+
[]byte{0x07, 0x56, 0xd8, 0xe0, 0x77, 0x47, 0x61, 0xd2},
|
| 1178 |
+
[]byte{0x0c, 0xd3, 0xda, 0x02, 0x00, 0x21, 0xdc, 0x09}},
|
| 1179 |
+
{ // 7
|
| 1180 |
+
[]byte{
|
| 1181 |
+
0x43, 0x29, 0x7f, 0xad, 0x38, 0xe3, 0x73, 0xfe,
|
| 1182 |
+
0x43, 0x29, 0x7f, 0xad, 0x38, 0xe3, 0x73, 0xfe,
|
| 1183 |
+
0x43, 0x29, 0x7f, 0xad, 0x38, 0xe3, 0x73, 0xfe},
|
| 1184 |
+
[]byte{0x76, 0x25, 0x14, 0xb8, 0x29, 0xbf, 0x48, 0x6a},
|
| 1185 |
+
[]byte{0xea, 0x67, 0x6b, 0x2c, 0xb7, 0xdb, 0x2b, 0x7a}},
|
| 1186 |
+
{ // 8
|
| 1187 |
+
[]byte{
|
| 1188 |
+
0x07, 0xa7, 0x13, 0x70, 0x45, 0xda, 0x2a, 0x16,
|
| 1189 |
+
0x07, 0xa7, 0x13, 0x70, 0x45, 0xda, 0x2a, 0x16,
|
| 1190 |
+
0x07, 0xa7, 0x13, 0x70, 0x45, 0xda, 0x2a, 0x16},
|
| 1191 |
+
[]byte{0x3b, 0xdd, 0x11, 0x90, 0x49, 0x37, 0x28, 0x02},
|
| 1192 |
+
[]byte{0xdf, 0xd6, 0x4a, 0x81, 0x5c, 0xaf, 0x1a, 0x0f}},
|
| 1193 |
+
{ // 9
|
| 1194 |
+
[]byte{
|
| 1195 |
+
0x04, 0x68, 0x91, 0x04, 0xc2, 0xfd, 0x3b, 0x2f,
|
| 1196 |
+
0x04, 0x68, 0x91, 0x04, 0xc2, 0xfd, 0x3b, 0x2f,
|
| 1197 |
+
0x04, 0x68, 0x91, 0x04, 0xc2, 0xfd, 0x3b, 0x2f},
|
| 1198 |
+
[]byte{0x26, 0x95, 0x5f, 0x68, 0x35, 0xaf, 0x60, 0x9a},
|
| 1199 |
+
[]byte{0x5c, 0x51, 0x3c, 0x9c, 0x48, 0x86, 0xc0, 0x88}},
|
| 1200 |
+
{ // 10
|
| 1201 |
+
[]byte{
|
| 1202 |
+
0x37, 0xd0, 0x6b, 0xb5, 0x16, 0xcb, 0x75, 0x46,
|
| 1203 |
+
0x37, 0xd0, 0x6b, 0xb5, 0x16, 0xcb, 0x75, 0x46,
|
| 1204 |
+
0x37, 0xd0, 0x6b, 0xb5, 0x16, 0xcb, 0x75, 0x46},
|
| 1205 |
+
[]byte{0x16, 0x4d, 0x5e, 0x40, 0x4f, 0x27, 0x52, 0x32},
|
| 1206 |
+
[]byte{0x0a, 0x2a, 0xee, 0xae, 0x3f, 0xf4, 0xab, 0x77}},
|
| 1207 |
+
{ // 11
|
| 1208 |
+
[]byte{
|
| 1209 |
+
0x1f, 0x08, 0x26, 0x0d, 0x1a, 0xc2, 0x46, 0x5e,
|
| 1210 |
+
0x1f, 0x08, 0x26, 0x0d, 0x1a, 0xc2, 0x46, 0x5e,
|
| 1211 |
+
0x1f, 0x08, 0x26, 0x0d, 0x1a, 0xc2, 0x46, 0x5e},
|
| 1212 |
+
[]byte{0x6b, 0x05, 0x6e, 0x18, 0x75, 0x9f, 0x5c, 0xca},
|
| 1213 |
+
[]byte{0xef, 0x1b, 0xf0, 0x3e, 0x5d, 0xfa, 0x57, 0x5a}},
|
| 1214 |
+
{ // 12
|
| 1215 |
+
[]byte{
|
| 1216 |
+
0x58, 0x40, 0x23, 0x64, 0x1a, 0xba, 0x61, 0x76,
|
| 1217 |
+
0x58, 0x40, 0x23, 0x64, 0x1a, 0xba, 0x61, 0x76,
|
| 1218 |
+
0x58, 0x40, 0x23, 0x64, 0x1a, 0xba, 0x61, 0x76},
|
| 1219 |
+
[]byte{0x00, 0x4b, 0xd6, 0xef, 0x09, 0x17, 0x60, 0x62},
|
| 1220 |
+
[]byte{0x88, 0xbf, 0x0d, 0xb6, 0xd7, 0x0d, 0xee, 0x56}},
|
| 1221 |
+
{ // 13
|
| 1222 |
+
[]byte{
|
| 1223 |
+
0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xb0, 0x07,
|
| 1224 |
+
0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xb0, 0x07,
|
| 1225 |
+
0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xb0, 0x07},
|
| 1226 |
+
[]byte{0x48, 0x0d, 0x39, 0x00, 0x6e, 0xe7, 0x62, 0xf2},
|
| 1227 |
+
[]byte{0xa1, 0xf9, 0x91, 0x55, 0x41, 0x02, 0x0b, 0x56}},
|
| 1228 |
+
{ // 14
|
| 1229 |
+
[]byte{
|
| 1230 |
+
0x49, 0x79, 0x3e, 0xbc, 0x79, 0xb3, 0x25, 0x8f,
|
| 1231 |
+
0x49, 0x79, 0x3e, 0xbc, 0x79, 0xb3, 0x25, 0x8f,
|
| 1232 |
+
0x49, 0x79, 0x3e, 0xbc, 0x79, 0xb3, 0x25, 0x8f},
|
| 1233 |
+
[]byte{0x43, 0x75, 0x40, 0xc8, 0x69, 0x8f, 0x3c, 0xfa},
|
| 1234 |
+
[]byte{0x6f, 0xbf, 0x1c, 0xaf, 0xcf, 0xfd, 0x05, 0x56}},
|
| 1235 |
+
{ // 15
|
| 1236 |
+
[]byte{
|
| 1237 |
+
0x4f, 0xb0, 0x5e, 0x15, 0x15, 0xab, 0x73, 0xa7,
|
| 1238 |
+
0x4f, 0xb0, 0x5e, 0x15, 0x15, 0xab, 0x73, 0xa7,
|
| 1239 |
+
0x4f, 0xb0, 0x5e, 0x15, 0x15, 0xab, 0x73, 0xa7},
|
| 1240 |
+
[]byte{0x07, 0x2d, 0x43, 0xa0, 0x77, 0x07, 0x52, 0x92},
|
| 1241 |
+
[]byte{0x2f, 0x22, 0xe4, 0x9b, 0xab, 0x7c, 0xa1, 0xac}},
|
| 1242 |
+
{ // 16
|
| 1243 |
+
[]byte{
|
| 1244 |
+
0x49, 0xe9, 0x5d, 0x6d, 0x4c, 0xa2, 0x29, 0xbf,
|
| 1245 |
+
0x49, 0xe9, 0x5d, 0x6d, 0x4c, 0xa2, 0x29, 0xbf,
|
| 1246 |
+
0x49, 0xe9, 0x5d, 0x6d, 0x4c, 0xa2, 0x29, 0xbf},
|
| 1247 |
+
[]byte{0x02, 0xfe, 0x55, 0x77, 0x81, 0x17, 0xf1, 0x2a},
|
| 1248 |
+
[]byte{0x5a, 0x6b, 0x61, 0x2c, 0xc2, 0x6c, 0xce, 0x4a}},
|
| 1249 |
+
{ // 17
|
| 1250 |
+
[]byte{
|
| 1251 |
+
0x01, 0x83, 0x10, 0xdc, 0x40, 0x9b, 0x26, 0xd6,
|
| 1252 |
+
0x01, 0x83, 0x10, 0xdc, 0x40, 0x9b, 0x26, 0xd6,
|
| 1253 |
+
0x01, 0x83, 0x10, 0xdc, 0x40, 0x9b, 0x26, 0xd6},
|
| 1254 |
+
[]byte{0x1d, 0x9d, 0x5c, 0x50, 0x18, 0xf7, 0x28, 0xc2},
|
| 1255 |
+
[]byte{0x5f, 0x4c, 0x03, 0x8e, 0xd1, 0x2b, 0x2e, 0x41}},
|
| 1256 |
+
{ // 18
|
| 1257 |
+
[]byte{
|
| 1258 |
+
0x1c, 0x58, 0x7f, 0x1c, 0x13, 0x92, 0x4f, 0xef,
|
| 1259 |
+
0x1c, 0x58, 0x7f, 0x1c, 0x13, 0x92, 0x4f, 0xef,
|
| 1260 |
+
0x1c, 0x58, 0x7f, 0x1c, 0x13, 0x92, 0x4f, 0xef},
|
| 1261 |
+
[]byte{0x30, 0x55, 0x32, 0x28, 0x6d, 0x6f, 0x29, 0x5a},
|
| 1262 |
+
[]byte{0x63, 0xfa, 0xc0, 0xd0, 0x34, 0xd9, 0xf7, 0x93}},
|
| 1263 |
+
}
|
| 1264 |
+
|
| 1265 |
+
func newCipher(key []byte) cipher.Block {
|
| 1266 |
+
c, err := des.NewCipher(key)
|
| 1267 |
+
if err != nil {
|
| 1268 |
+
panic("NewCipher failed: " + err.Error())
|
| 1269 |
+
}
|
| 1270 |
+
return c
|
| 1271 |
+
}
|
| 1272 |
+
|
| 1273 |
+
// Use the known weak keys to test DES implementation
|
| 1274 |
+
func TestWeakKeys(t *testing.T) {
|
| 1275 |
+
for i, tt := range weakKeyTests {
|
| 1276 |
+
var encrypt = func(in []byte) (out []byte) {
|
| 1277 |
+
c := newCipher(tt.key)
|
| 1278 |
+
out = make([]byte, len(in))
|
| 1279 |
+
c.Encrypt(out, in)
|
| 1280 |
+
return
|
| 1281 |
+
}
|
| 1282 |
+
|
| 1283 |
+
// Encrypting twice with a DES weak
|
| 1284 |
+
// key should reproduce the original input
|
| 1285 |
+
result := encrypt(tt.in)
|
| 1286 |
+
result = encrypt(result)
|
| 1287 |
+
|
| 1288 |
+
if !bytes.Equal(result, tt.in) {
|
| 1289 |
+
t.Errorf("#%d: result: %x want: %x", i, result, tt.in)
|
| 1290 |
+
}
|
| 1291 |
+
}
|
| 1292 |
+
}
|
| 1293 |
+
|
| 1294 |
+
// Use the known semi-weak key pairs to test DES implementation
|
| 1295 |
+
func TestSemiWeakKeyPairs(t *testing.T) {
|
| 1296 |
+
for i, tt := range semiWeakKeyTests {
|
| 1297 |
+
var encrypt = func(key, in []byte) (out []byte) {
|
| 1298 |
+
c := newCipher(key)
|
| 1299 |
+
out = make([]byte, len(in))
|
| 1300 |
+
c.Encrypt(out, in)
|
| 1301 |
+
return
|
| 1302 |
+
}
|
| 1303 |
+
|
| 1304 |
+
// Encrypting with one member of the semi-weak pair
|
| 1305 |
+
// and then encrypting the result with the other member
|
| 1306 |
+
// should reproduce the original input.
|
| 1307 |
+
result := encrypt(tt.key, tt.in)
|
| 1308 |
+
result = encrypt(tt.out, result)
|
| 1309 |
+
|
| 1310 |
+
if !bytes.Equal(result, tt.in) {
|
| 1311 |
+
t.Errorf("#%d: result: %x want: %x", i, result, tt.in)
|
| 1312 |
+
}
|
| 1313 |
+
}
|
| 1314 |
+
}
|
| 1315 |
+
|
| 1316 |
+
func TestDESEncryptBlock(t *testing.T) {
|
| 1317 |
+
for i, tt := range encryptDESTests {
|
| 1318 |
+
c := newCipher(tt.key)
|
| 1319 |
+
out := make([]byte, len(tt.in))
|
| 1320 |
+
c.Encrypt(out, tt.in)
|
| 1321 |
+
|
| 1322 |
+
if !bytes.Equal(out, tt.out) {
|
| 1323 |
+
t.Errorf("#%d: result: %x want: %x", i, out, tt.out)
|
| 1324 |
+
}
|
| 1325 |
+
}
|
| 1326 |
+
}
|
| 1327 |
+
|
| 1328 |
+
func TestDESDecryptBlock(t *testing.T) {
|
| 1329 |
+
for i, tt := range encryptDESTests {
|
| 1330 |
+
c := newCipher(tt.key)
|
| 1331 |
+
plain := make([]byte, len(tt.in))
|
| 1332 |
+
c.Decrypt(plain, tt.out)
|
| 1333 |
+
|
| 1334 |
+
if !bytes.Equal(plain, tt.in) {
|
| 1335 |
+
t.Errorf("#%d: result: %x want: %x", i, plain, tt.in)
|
| 1336 |
+
}
|
| 1337 |
+
}
|
| 1338 |
+
}
|
| 1339 |
+
|
| 1340 |
+
func TestEncryptTripleDES(t *testing.T) {
|
| 1341 |
+
for i, tt := range encryptTripleDESTests {
|
| 1342 |
+
c, _ := des.NewTripleDESCipher(tt.key)
|
| 1343 |
+
out := make([]byte, len(tt.in))
|
| 1344 |
+
c.Encrypt(out, tt.in)
|
| 1345 |
+
|
| 1346 |
+
if !bytes.Equal(out, tt.out) {
|
| 1347 |
+
t.Errorf("#%d: result: %x want: %x", i, out, tt.out)
|
| 1348 |
+
}
|
| 1349 |
+
}
|
| 1350 |
+
}
|
| 1351 |
+
|
| 1352 |
+
func TestDecryptTripleDES(t *testing.T) {
|
| 1353 |
+
for i, tt := range encryptTripleDESTests {
|
| 1354 |
+
c, _ := des.NewTripleDESCipher(tt.key)
|
| 1355 |
+
|
| 1356 |
+
plain := make([]byte, len(tt.in))
|
| 1357 |
+
c.Decrypt(plain, tt.out)
|
| 1358 |
+
|
| 1359 |
+
if !bytes.Equal(plain, tt.in) {
|
| 1360 |
+
t.Errorf("#%d: result: %x want: %x", i, plain, tt.in)
|
| 1361 |
+
}
|
| 1362 |
+
}
|
| 1363 |
+
}
|
| 1364 |
+
|
| 1365 |
+
// Defined in Pub 800-20
|
| 1366 |
+
func TestVariablePlaintextKnownAnswer(t *testing.T) {
|
| 1367 |
+
for i, tt := range tableA1Tests {
|
| 1368 |
+
c, _ := des.NewTripleDESCipher(tableA1Key)
|
| 1369 |
+
|
| 1370 |
+
out := make([]byte, len(tt.in))
|
| 1371 |
+
c.Encrypt(out, tt.in)
|
| 1372 |
+
|
| 1373 |
+
if !bytes.Equal(out, tt.out) {
|
| 1374 |
+
t.Errorf("#%d: result: %x want: %x", i, out, tt.out)
|
| 1375 |
+
}
|
| 1376 |
+
}
|
| 1377 |
+
}
|
| 1378 |
+
|
| 1379 |
+
// Defined in Pub 800-20
|
| 1380 |
+
func TestVariableCiphertextKnownAnswer(t *testing.T) {
|
| 1381 |
+
for i, tt := range tableA1Tests {
|
| 1382 |
+
c, _ := des.NewTripleDESCipher(tableA1Key)
|
| 1383 |
+
|
| 1384 |
+
plain := make([]byte, len(tt.out))
|
| 1385 |
+
c.Decrypt(plain, tt.out)
|
| 1386 |
+
|
| 1387 |
+
if !bytes.Equal(plain, tt.in) {
|
| 1388 |
+
t.Errorf("#%d: result: %x want: %x", i, plain, tt.in)
|
| 1389 |
+
}
|
| 1390 |
+
}
|
| 1391 |
+
}
|
| 1392 |
+
|
| 1393 |
+
// Defined in Pub 800-20
|
| 1394 |
+
// Encrypting the Table A.1 ciphertext with the
|
| 1395 |
+
// 0x01... key produces the original plaintext
|
| 1396 |
+
func TestInversePermutationKnownAnswer(t *testing.T) {
|
| 1397 |
+
for i, tt := range tableA1Tests {
|
| 1398 |
+
c, _ := des.NewTripleDESCipher(tableA1Key)
|
| 1399 |
+
|
| 1400 |
+
plain := make([]byte, len(tt.in))
|
| 1401 |
+
c.Encrypt(plain, tt.out)
|
| 1402 |
+
|
| 1403 |
+
if !bytes.Equal(plain, tt.in) {
|
| 1404 |
+
t.Errorf("#%d: result: %x want: %x", i, plain, tt.in)
|
| 1405 |
+
}
|
| 1406 |
+
}
|
| 1407 |
+
}
|
| 1408 |
+
|
| 1409 |
+
// Defined in Pub 800-20
|
| 1410 |
+
// Decrypting the Table A.1 plaintext with the
|
| 1411 |
+
// 0x01... key produces the corresponding ciphertext
|
| 1412 |
+
func TestInitialPermutationKnownAnswer(t *testing.T) {
|
| 1413 |
+
for i, tt := range tableA1Tests {
|
| 1414 |
+
c, _ := des.NewTripleDESCipher(tableA1Key)
|
| 1415 |
+
|
| 1416 |
+
out := make([]byte, len(tt.in))
|
| 1417 |
+
c.Decrypt(out, tt.in)
|
| 1418 |
+
|
| 1419 |
+
if !bytes.Equal(out, tt.out) {
|
| 1420 |
+
t.Errorf("#%d: result: %x want: %x", i, out, tt.out)
|
| 1421 |
+
}
|
| 1422 |
+
}
|
| 1423 |
+
}
|
| 1424 |
+
|
| 1425 |
+
// Defined in Pub 800-20
|
| 1426 |
+
func TestVariableKeyKnownAnswerEncrypt(t *testing.T) {
|
| 1427 |
+
for i, tt := range tableA2Tests {
|
| 1428 |
+
c, _ := des.NewTripleDESCipher(tt.key)
|
| 1429 |
+
|
| 1430 |
+
out := make([]byte, len(tableA2Plaintext))
|
| 1431 |
+
c.Encrypt(out, tableA2Plaintext)
|
| 1432 |
+
|
| 1433 |
+
if !bytes.Equal(out, tt.out) {
|
| 1434 |
+
t.Errorf("#%d: result: %x want: %x", i, out, tt.out)
|
| 1435 |
+
}
|
| 1436 |
+
}
|
| 1437 |
+
}
|
| 1438 |
+
|
| 1439 |
+
// Defined in Pub 800-20
|
| 1440 |
+
func TestVariableKeyKnownAnswerDecrypt(t *testing.T) {
|
| 1441 |
+
for i, tt := range tableA2Tests {
|
| 1442 |
+
c, _ := des.NewTripleDESCipher(tt.key)
|
| 1443 |
+
|
| 1444 |
+
out := make([]byte, len(tt.out))
|
| 1445 |
+
c.Decrypt(out, tt.out)
|
| 1446 |
+
|
| 1447 |
+
if !bytes.Equal(out, tableA2Plaintext) {
|
| 1448 |
+
t.Errorf("#%d: result: %x want: %x", i, out, tableA2Plaintext)
|
| 1449 |
+
}
|
| 1450 |
+
}
|
| 1451 |
+
}
|
| 1452 |
+
|
| 1453 |
+
// Defined in Pub 800-20
|
| 1454 |
+
func TestPermutationOperationKnownAnswerEncrypt(t *testing.T) {
|
| 1455 |
+
for i, tt := range tableA3Tests {
|
| 1456 |
+
c, _ := des.NewTripleDESCipher(tt.key)
|
| 1457 |
+
|
| 1458 |
+
out := make([]byte, len(tableA3Plaintext))
|
| 1459 |
+
c.Encrypt(out, tableA3Plaintext)
|
| 1460 |
+
|
| 1461 |
+
if !bytes.Equal(out, tt.out) {
|
| 1462 |
+
t.Errorf("#%d: result: %x want: %x", i, out, tt.out)
|
| 1463 |
+
}
|
| 1464 |
+
}
|
| 1465 |
+
}
|
| 1466 |
+
|
| 1467 |
+
// Defined in Pub 800-20
|
| 1468 |
+
func TestPermutationOperationKnownAnswerDecrypt(t *testing.T) {
|
| 1469 |
+
for i, tt := range tableA3Tests {
|
| 1470 |
+
c, _ := des.NewTripleDESCipher(tt.key)
|
| 1471 |
+
|
| 1472 |
+
out := make([]byte, len(tt.out))
|
| 1473 |
+
c.Decrypt(out, tt.out)
|
| 1474 |
+
|
| 1475 |
+
if !bytes.Equal(out, tableA3Plaintext) {
|
| 1476 |
+
t.Errorf("#%d: result: %x want: %x", i, out, tableA3Plaintext)
|
| 1477 |
+
}
|
| 1478 |
+
}
|
| 1479 |
+
}
|
| 1480 |
+
|
| 1481 |
+
// Defined in Pub 800-20
|
| 1482 |
+
func TestSubstitutionTableKnownAnswerEncrypt(t *testing.T) {
|
| 1483 |
+
for i, tt := range tableA4Tests {
|
| 1484 |
+
c, _ := des.NewTripleDESCipher(tt.key)
|
| 1485 |
+
|
| 1486 |
+
out := make([]byte, len(tt.in))
|
| 1487 |
+
c.Encrypt(out, tt.in)
|
| 1488 |
+
|
| 1489 |
+
if !bytes.Equal(out, tt.out) {
|
| 1490 |
+
t.Errorf("#%d: result: %x want: %x", i, out, tt.out)
|
| 1491 |
+
}
|
| 1492 |
+
}
|
| 1493 |
+
}
|
| 1494 |
+
|
| 1495 |
+
// Defined in Pub 800-20
|
| 1496 |
+
func TestSubstitutionTableKnownAnswerDecrypt(t *testing.T) {
|
| 1497 |
+
for i, tt := range tableA4Tests {
|
| 1498 |
+
c, _ := des.NewTripleDESCipher(tt.key)
|
| 1499 |
+
|
| 1500 |
+
out := make([]byte, len(tt.out))
|
| 1501 |
+
c.Decrypt(out, tt.out)
|
| 1502 |
+
|
| 1503 |
+
if !bytes.Equal(out, tt.in) {
|
| 1504 |
+
t.Errorf("#%d: result: %x want: %x", i, out, tt.in)
|
| 1505 |
+
}
|
| 1506 |
+
}
|
| 1507 |
+
}
|
| 1508 |
+
|
| 1509 |
+
func BenchmarkEncrypt(b *testing.B) {
|
| 1510 |
+
tt := encryptDESTests[0]
|
| 1511 |
+
c, err := des.NewCipher(tt.key)
|
| 1512 |
+
if err != nil {
|
| 1513 |
+
b.Fatal("NewCipher:", err)
|
| 1514 |
+
}
|
| 1515 |
+
out := make([]byte, len(tt.in))
|
| 1516 |
+
b.SetBytes(int64(len(out)))
|
| 1517 |
+
b.ResetTimer()
|
| 1518 |
+
for i := 0; i < b.N; i++ {
|
| 1519 |
+
c.Encrypt(out, tt.in)
|
| 1520 |
+
}
|
| 1521 |
+
}
|
| 1522 |
+
|
| 1523 |
+
func BenchmarkDecrypt(b *testing.B) {
|
| 1524 |
+
tt := encryptDESTests[0]
|
| 1525 |
+
c, err := des.NewCipher(tt.key)
|
| 1526 |
+
if err != nil {
|
| 1527 |
+
b.Fatal("NewCipher:", err)
|
| 1528 |
+
}
|
| 1529 |
+
out := make([]byte, len(tt.out))
|
| 1530 |
+
b.SetBytes(int64(len(out)))
|
| 1531 |
+
b.ResetTimer()
|
| 1532 |
+
for i := 0; i < b.N; i++ {
|
| 1533 |
+
c.Decrypt(out, tt.out)
|
| 1534 |
+
}
|
| 1535 |
+
}
|
| 1536 |
+
|
| 1537 |
+
func BenchmarkTDESEncrypt(b *testing.B) {
|
| 1538 |
+
tt := encryptTripleDESTests[0]
|
| 1539 |
+
c, err := des.NewTripleDESCipher(tt.key)
|
| 1540 |
+
if err != nil {
|
| 1541 |
+
b.Fatal("NewCipher:", err)
|
| 1542 |
+
}
|
| 1543 |
+
out := make([]byte, len(tt.in))
|
| 1544 |
+
b.SetBytes(int64(len(out)))
|
| 1545 |
+
b.ResetTimer()
|
| 1546 |
+
for i := 0; i < b.N; i++ {
|
| 1547 |
+
c.Encrypt(out, tt.in)
|
| 1548 |
+
}
|
| 1549 |
+
}
|
| 1550 |
+
|
| 1551 |
+
func BenchmarkTDESDecrypt(b *testing.B) {
|
| 1552 |
+
tt := encryptTripleDESTests[0]
|
| 1553 |
+
c, err := des.NewTripleDESCipher(tt.key)
|
| 1554 |
+
if err != nil {
|
| 1555 |
+
b.Fatal("NewCipher:", err)
|
| 1556 |
+
}
|
| 1557 |
+
out := make([]byte, len(tt.out))
|
| 1558 |
+
b.SetBytes(int64(len(out)))
|
| 1559 |
+
b.ResetTimer()
|
| 1560 |
+
for i := 0; i < b.N; i++ {
|
| 1561 |
+
c.Decrypt(out, tt.out)
|
| 1562 |
+
}
|
| 1563 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/des/example_test.go
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2013 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package des_test
|
| 6 |
+
|
| 7 |
+
import "crypto/des"
|
| 8 |
+
|
| 9 |
+
func ExampleNewTripleDESCipher() {
|
| 10 |
+
// NewTripleDESCipher can also be used when EDE2 is required by
|
| 11 |
+
// duplicating the first 8 bytes of the 16-byte key.
|
| 12 |
+
ede2Key := []byte("example key 1234")
|
| 13 |
+
|
| 14 |
+
var tripleDESKey []byte
|
| 15 |
+
tripleDESKey = append(tripleDESKey, ede2Key[:16]...)
|
| 16 |
+
tripleDESKey = append(tripleDESKey, ede2Key[:8]...)
|
| 17 |
+
|
| 18 |
+
_, err := des.NewTripleDESCipher(tripleDESKey)
|
| 19 |
+
if err != nil {
|
| 20 |
+
panic(err)
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// See crypto/cipher for how to use a cipher.Block for encryption and
|
| 24 |
+
// decryption.
|
| 25 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/des/internal_test.go
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2023 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package des
|
| 6 |
+
|
| 7 |
+
import "testing"
|
| 8 |
+
|
| 9 |
+
func TestInitialPermute(t *testing.T) {
|
| 10 |
+
for i := uint(0); i < 64; i++ {
|
| 11 |
+
bit := uint64(1) << i
|
| 12 |
+
got := permuteInitialBlock(bit)
|
| 13 |
+
want := uint64(1) << finalPermutation[63-i]
|
| 14 |
+
if got != want {
|
| 15 |
+
t.Errorf("permute(%x) = %x, want %x", bit, got, want)
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
func TestFinalPermute(t *testing.T) {
|
| 21 |
+
for i := uint(0); i < 64; i++ {
|
| 22 |
+
bit := uint64(1) << i
|
| 23 |
+
got := permuteFinalBlock(bit)
|
| 24 |
+
want := uint64(1) << initialPermutation[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| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// Package dsa implements the Digital Signature Algorithm, as defined in FIPS 186-3.
|
| 6 |
+
//
|
| 7 |
+
// The DSA operations in this package are not implemented using constant-time algorithms.
|
| 8 |
+
//
|
| 9 |
+
// Deprecated: DSA is a legacy algorithm, and modern alternatives such as
|
| 10 |
+
// Ed25519 (implemented by package crypto/ed25519) should be used instead. Keys
|
| 11 |
+
// with 1024-bit moduli (L1024N160 parameters) are cryptographically weak, while
|
| 12 |
+
// bigger keys are not widely supported. Note that FIPS 186-5 no longer approves
|
| 13 |
+
// DSA for signature generation.
|
| 14 |
+
package dsa
|
| 15 |
+
|
| 16 |
+
import (
|
| 17 |
+
"errors"
|
| 18 |
+
"io"
|
| 19 |
+
"math/big"
|
| 20 |
+
|
| 21 |
+
"crypto/internal/randutil"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
// Parameters represents the domain parameters for a key. These parameters can
|
| 25 |
+
// be shared across many keys. The bit length of Q must be a multiple of 8.
|
| 26 |
+
type Parameters struct {
|
| 27 |
+
P, Q, G *big.Int
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// PublicKey represents a DSA public key.
|
| 31 |
+
type PublicKey struct {
|
| 32 |
+
Parameters
|
| 33 |
+
Y *big.Int
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// PrivateKey represents a DSA private key.
|
| 37 |
+
type PrivateKey struct {
|
| 38 |
+
PublicKey
|
| 39 |
+
X *big.Int
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
// ErrInvalidPublicKey results when a public key is not usable by this code.
|
| 43 |
+
// FIPS is quite strict about the format of DSA keys, but other code may be
|
| 44 |
+
// less so. Thus, when using keys which may have been generated by other code,
|
| 45 |
+
// this error must be handled.
|
| 46 |
+
var ErrInvalidPublicKey = errors.New("crypto/dsa: invalid public key")
|
| 47 |
+
|
| 48 |
+
// ParameterSizes is an enumeration of the acceptable bit lengths of the primes
|
| 49 |
+
// in a set of DSA parameters. See FIPS 186-3, section 4.2.
|
| 50 |
+
type ParameterSizes int
|
| 51 |
+
|
| 52 |
+
const (
|
| 53 |
+
L1024N160 ParameterSizes = iota
|
| 54 |
+
L2048N224
|
| 55 |
+
L2048N256
|
| 56 |
+
L3072N256
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
// numMRTests is the number of Miller-Rabin primality tests that we perform. We
|
| 60 |
+
// pick the largest recommended number from table C.1 of FIPS 186-3.
|
| 61 |
+
const numMRTests = 64
|
| 62 |
+
|
| 63 |
+
// GenerateParameters puts a random, valid set of DSA parameters into params.
|
| 64 |
+
// This function can take many seconds, even on fast machines.
|
| 65 |
+
func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes) error {
|
| 66 |
+
// This function doesn't follow FIPS 186-3 exactly in that it doesn't
|
| 67 |
+
// use a verification seed to generate the primes. The verification
|
| 68 |
+
// seed doesn't appear to be exported or used by other code and
|
| 69 |
+
// omitting it makes the code cleaner.
|
| 70 |
+
|
| 71 |
+
var L, N int
|
| 72 |
+
switch sizes {
|
| 73 |
+
case L1024N160:
|
| 74 |
+
L = 1024
|
| 75 |
+
N = 160
|
| 76 |
+
case L2048N224:
|
| 77 |
+
L = 2048
|
| 78 |
+
N = 224
|
| 79 |
+
case L2048N256:
|
| 80 |
+
L = 2048
|
| 81 |
+
N = 256
|
| 82 |
+
case L3072N256:
|
| 83 |
+
L = 3072
|
| 84 |
+
N = 256
|
| 85 |
+
default:
|
| 86 |
+
return errors.New("crypto/dsa: invalid ParameterSizes")
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
qBytes := make([]byte, N/8)
|
| 90 |
+
pBytes := make([]byte, L/8)
|
| 91 |
+
|
| 92 |
+
q := new(big.Int)
|
| 93 |
+
p := new(big.Int)
|
| 94 |
+
rem := new(big.Int)
|
| 95 |
+
one := new(big.Int)
|
| 96 |
+
one.SetInt64(1)
|
| 97 |
+
|
| 98 |
+
GeneratePrimes:
|
| 99 |
+
for {
|
| 100 |
+
if _, err := io.ReadFull(rand, qBytes); err != nil {
|
| 101 |
+
return err
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
qBytes[len(qBytes)-1] |= 1
|
| 105 |
+
qBytes[0] |= 0x80
|
| 106 |
+
q.SetBytes(qBytes)
|
| 107 |
+
|
| 108 |
+
if !q.ProbablyPrime(numMRTests) {
|
| 109 |
+
continue
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
for i := 0; i < 4*L; i++ {
|
| 113 |
+
if _, err := io.ReadFull(rand, pBytes); err != nil {
|
| 114 |
+
return err
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
pBytes[len(pBytes)-1] |= 1
|
| 118 |
+
pBytes[0] |= 0x80
|
| 119 |
+
|
| 120 |
+
p.SetBytes(pBytes)
|
| 121 |
+
rem.Mod(p, q)
|
| 122 |
+
rem.Sub(rem, one)
|
| 123 |
+
p.Sub(p, rem)
|
| 124 |
+
if p.BitLen() < L {
|
| 125 |
+
continue
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
if !p.ProbablyPrime(numMRTests) {
|
| 129 |
+
continue
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
params.P = p
|
| 133 |
+
params.Q = q
|
| 134 |
+
break GeneratePrimes
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
h := new(big.Int)
|
| 139 |
+
h.SetInt64(2)
|
| 140 |
+
g := new(big.Int)
|
| 141 |
+
|
| 142 |
+
pm1 := new(big.Int).Sub(p, one)
|
| 143 |
+
e := new(big.Int).Div(pm1, q)
|
| 144 |
+
|
| 145 |
+
for {
|
| 146 |
+
g.Exp(h, e, p)
|
| 147 |
+
if g.Cmp(one) == 0 {
|
| 148 |
+
h.Add(h, one)
|
| 149 |
+
continue
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
params.G = g
|
| 153 |
+
return nil
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
// GenerateKey generates a public&private key pair. The Parameters of the
|
| 158 |
+
// [PrivateKey] must already be valid (see [GenerateParameters]).
|
| 159 |
+
func GenerateKey(priv *PrivateKey, rand io.Reader) error {
|
| 160 |
+
if priv.P == nil || priv.Q == nil || priv.G == nil {
|
| 161 |
+
return errors.New("crypto/dsa: parameters not set up before generating key")
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
x := new(big.Int)
|
| 165 |
+
xBytes := make([]byte, priv.Q.BitLen()/8)
|
| 166 |
+
|
| 167 |
+
for {
|
| 168 |
+
_, err := io.ReadFull(rand, xBytes)
|
| 169 |
+
if err != nil {
|
| 170 |
+
return err
|
| 171 |
+
}
|
| 172 |
+
x.SetBytes(xBytes)
|
| 173 |
+
if x.Sign() != 0 && x.Cmp(priv.Q) < 0 {
|
| 174 |
+
break
|
| 175 |
+
}
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
priv.X = x
|
| 179 |
+
priv.Y = new(big.Int)
|
| 180 |
+
priv.Y.Exp(priv.G, x, priv.P)
|
| 181 |
+
return nil
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
// fermatInverse calculates the inverse of k in GF(P) using Fermat's method.
|
| 185 |
+
// This has better constant-time properties than Euclid's method (implemented
|
| 186 |
+
// in math/big.Int.ModInverse) although math/big itself isn't strictly
|
| 187 |
+
// constant-time so it's not perfect.
|
| 188 |
+
func fermatInverse(k, P *big.Int) *big.Int {
|
| 189 |
+
two := big.NewInt(2)
|
| 190 |
+
pMinus2 := new(big.Int).Sub(P, two)
|
| 191 |
+
return new(big.Int).Exp(k, pMinus2, P)
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
// Sign signs an arbitrary length hash (which should be the result of hashing a
|
| 195 |
+
// larger message) using the private key, priv. It returns the signature as a
|
| 196 |
+
// pair of integers. The security of the private key depends on the entropy of
|
| 197 |
+
// rand.
|
| 198 |
+
//
|
| 199 |
+
// Note that FIPS 186-3 section 4.6 specifies that the hash should be truncated
|
| 200 |
+
// to the byte-length of the subgroup. This function does not perform that
|
| 201 |
+
// truncation itself.
|
| 202 |
+
//
|
| 203 |
+
// Be aware that calling Sign with an attacker-controlled [PrivateKey] may
|
| 204 |
+
// require an arbitrary amount of CPU.
|
| 205 |
+
func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) {
|
| 206 |
+
randutil.MaybeReadByte(rand)
|
| 207 |
+
|
| 208 |
+
// FIPS 186-3, section 4.6
|
| 209 |
+
|
| 210 |
+
n := priv.Q.BitLen()
|
| 211 |
+
if priv.Q.Sign() <= 0 || priv.P.Sign() <= 0 || priv.G.Sign() <= 0 || priv.X.Sign() <= 0 || n%8 != 0 {
|
| 212 |
+
err = ErrInvalidPublicKey
|
| 213 |
+
return
|
| 214 |
+
}
|
| 215 |
+
n >>= 3
|
| 216 |
+
|
| 217 |
+
var attempts int
|
| 218 |
+
for attempts = 10; attempts > 0; attempts-- {
|
| 219 |
+
k := new(big.Int)
|
| 220 |
+
buf := make([]byte, n)
|
| 221 |
+
for {
|
| 222 |
+
_, err = io.ReadFull(rand, buf)
|
| 223 |
+
if err != nil {
|
| 224 |
+
return
|
| 225 |
+
}
|
| 226 |
+
k.SetBytes(buf)
|
| 227 |
+
// priv.Q must be >= 128 because the test above
|
| 228 |
+
// requires it to be > 0 and that
|
| 229 |
+
// ceil(log_2(Q)) mod 8 = 0
|
| 230 |
+
// Thus this loop will quickly terminate.
|
| 231 |
+
if k.Sign() > 0 && k.Cmp(priv.Q) < 0 {
|
| 232 |
+
break
|
| 233 |
+
}
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
kInv := fermatInverse(k, priv.Q)
|
| 237 |
+
|
| 238 |
+
r = new(big.Int).Exp(priv.G, k, priv.P)
|
| 239 |
+
r.Mod(r, priv.Q)
|
| 240 |
+
|
| 241 |
+
if r.Sign() == 0 {
|
| 242 |
+
continue
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
z := k.SetBytes(hash)
|
| 246 |
+
|
| 247 |
+
s = new(big.Int).Mul(priv.X, r)
|
| 248 |
+
s.Add(s, z)
|
| 249 |
+
s.Mod(s, priv.Q)
|
| 250 |
+
s.Mul(s, kInv)
|
| 251 |
+
s.Mod(s, priv.Q)
|
| 252 |
+
|
| 253 |
+
if s.Sign() != 0 {
|
| 254 |
+
break
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
// Only degenerate private keys will require more than a handful of
|
| 259 |
+
// attempts.
|
| 260 |
+
if attempts == 0 {
|
| 261 |
+
return nil, nil, ErrInvalidPublicKey
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
return
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
// Verify verifies the signature in r, s of hash using the public key, pub. It
|
| 268 |
+
// reports whether the signature is valid.
|
| 269 |
+
//
|
| 270 |
+
// Note that FIPS 186-3 section 4.6 specifies that the hash should be truncated
|
| 271 |
+
// to the byte-length of the subgroup. This function does not perform that
|
| 272 |
+
// truncation itself.
|
| 273 |
+
func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
|
| 274 |
+
// FIPS 186-3, section 4.7
|
| 275 |
+
|
| 276 |
+
if pub.P.Sign() == 0 {
|
| 277 |
+
return false
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
if r.Sign() < 1 || r.Cmp(pub.Q) >= 0 {
|
| 281 |
+
return false
|
| 282 |
+
}
|
| 283 |
+
if s.Sign() < 1 || s.Cmp(pub.Q) >= 0 {
|
| 284 |
+
return false
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
w := new(big.Int).ModInverse(s, pub.Q)
|
| 288 |
+
if w == nil {
|
| 289 |
+
return false
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
n := pub.Q.BitLen()
|
| 293 |
+
if n%8 != 0 {
|
| 294 |
+
return false
|
| 295 |
+
}
|
| 296 |
+
z := new(big.Int).SetBytes(hash)
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2011 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package dsa
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/rand"
|
| 9 |
+
"math/big"
|
| 10 |
+
"testing"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
func testSignAndVerify(t *testing.T, i int, priv *PrivateKey) {
|
| 14 |
+
hashed := []byte("testing")
|
| 15 |
+
r, s, err := Sign(rand.Reader, priv, hashed)
|
| 16 |
+
if err != nil {
|
| 17 |
+
t.Errorf("%d: error signing: %s", i, err)
|
| 18 |
+
return
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
if !Verify(&priv.PublicKey, hashed, r, s) {
|
| 22 |
+
t.Errorf("%d: Verify failed", i)
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
func testParameterGeneration(t *testing.T, sizes ParameterSizes, L, N int) {
|
| 27 |
+
var priv PrivateKey
|
| 28 |
+
params := &priv.Parameters
|
| 29 |
+
|
| 30 |
+
err := GenerateParameters(params, rand.Reader, sizes)
|
| 31 |
+
if err != nil {
|
| 32 |
+
t.Errorf("%d: %s", int(sizes), err)
|
| 33 |
+
return
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
if params.P.BitLen() != L {
|
| 37 |
+
t.Errorf("%d: params.BitLen got:%d want:%d", int(sizes), params.P.BitLen(), L)
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
if params.Q.BitLen() != N {
|
| 41 |
+
t.Errorf("%d: q.BitLen got:%d want:%d", int(sizes), params.Q.BitLen(), L)
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
one := new(big.Int)
|
| 45 |
+
one.SetInt64(1)
|
| 46 |
+
pm1 := new(big.Int).Sub(params.P, one)
|
| 47 |
+
quo, rem := new(big.Int).DivMod(pm1, params.Q, new(big.Int))
|
| 48 |
+
if rem.Sign() != 0 {
|
| 49 |
+
t.Errorf("%d: p-1 mod q != 0", int(sizes))
|
| 50 |
+
}
|
| 51 |
+
x := new(big.Int).Exp(params.G, quo, params.P)
|
| 52 |
+
if x.Cmp(one) == 0 {
|
| 53 |
+
t.Errorf("%d: invalid generator", int(sizes))
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
err = GenerateKey(&priv, rand.Reader)
|
| 57 |
+
if err != nil {
|
| 58 |
+
t.Errorf("error generating key: %s", err)
|
| 59 |
+
return
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
testSignAndVerify(t, int(sizes), &priv)
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
func TestParameterGeneration(t *testing.T) {
|
| 66 |
+
if testing.Short() {
|
| 67 |
+
t.Skip("skipping parameter generation test in short mode")
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
testParameterGeneration(t, L1024N160, 1024, 160)
|
| 71 |
+
testParameterGeneration(t, L2048N224, 2048, 224)
|
| 72 |
+
testParameterGeneration(t, L2048N256, 2048, 256)
|
| 73 |
+
testParameterGeneration(t, L3072N256, 3072, 256)
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
func fromHex(s string) *big.Int {
|
| 77 |
+
result, ok := new(big.Int).SetString(s, 16)
|
| 78 |
+
if !ok {
|
| 79 |
+
panic(s)
|
| 80 |
+
}
|
| 81 |
+
return result
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
func TestSignAndVerify(t *testing.T) {
|
| 85 |
+
priv := PrivateKey{
|
| 86 |
+
PublicKey: PublicKey{
|
| 87 |
+
Parameters: Parameters{
|
| 88 |
+
P: fromHex("A9B5B793FB4785793D246BAE77E8FF63CA52F442DA763C440259919FE1BC1D6065A9350637A04F75A2F039401D49F08E066C4D275A5A65DA5684BC563C14289D7AB8A67163BFBF79D85972619AD2CFF55AB0EE77A9002B0EF96293BDD0F42685EBB2C66C327079F6C98000FBCB79AACDE1BC6F9D5C7B1A97E3D9D54ED7951FEF"),
|
| 89 |
+
Q: fromHex("E1D3391245933D68A0714ED34BBCB7A1F422B9C1"),
|
| 90 |
+
G: fromHex("634364FC25248933D01D1993ECABD0657CC0CB2CEED7ED2E3E8AECDFCDC4A25C3B15E9E3B163ACA2984B5539181F3EFF1A5E8903D71D5B95DA4F27202B77D2C44B430BB53741A8D59A8F86887525C9F2A6A5980A195EAA7F2FF910064301DEF89D3AA213E1FAC7768D89365318E370AF54A112EFBA9246D9158386BA1B4EEFDA"),
|
| 91 |
+
},
|
| 92 |
+
Y: fromHex("32969E5780CFE1C849A1C276D7AEB4F38A23B591739AA2FE197349AEEBD31366AEE5EB7E6C6DDB7C57D02432B30DB5AA66D9884299FAA72568944E4EEDC92EA3FBC6F39F53412FBCC563208F7C15B737AC8910DBC2D9C9B8C001E72FDC40EB694AB1F06A5A2DBD18D9E36C66F31F566742F11EC0A52E9F7B89355C02FB5D32D2"),
|
| 93 |
+
},
|
| 94 |
+
X: fromHex("5078D4D29795CBE76D3AACFE48C9AF0BCDBEE91A"),
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
testSignAndVerify(t, 0, &priv)
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
func TestSignAndVerifyWithBadPublicKey(t *testing.T) {
|
| 101 |
+
pub := PublicKey{
|
| 102 |
+
Parameters: Parameters{
|
| 103 |
+
P: fromHex("A9B5B793FB4785793D246BAE77E8FF63CA52F442DA763C440259919FE1BC1D6065A9350637A04F75A2F039401D49F08E066C4D275A5A65DA5684BC563C14289D7AB8A67163BFBF79D85972619AD2CFF55AB0EE77A9002B0EF96293BDD0F42685EBB2C66C327079F6C98000FBCB79AACDE1BC6F9D5C7B1A97E3D9D54ED7951FEF"),
|
| 104 |
+
Q: fromHex("FA"),
|
| 105 |
+
G: fromHex("634364FC25248933D01D1993ECABD0657CC0CB2CEED7ED2E3E8AECDFCDC4A25C3B15E9E3B163ACA2984B5539181F3EFF1A5E8903D71D5B95DA4F27202B77D2C44B430BB53741A8D59A8F86887525C9F2A6A5980A195EAA7F2FF910064301DEF89D3AA213E1FAC7768D89365318E370AF54A112EFBA9246D9158386BA1B4EEFDA"),
|
| 106 |
+
},
|
| 107 |
+
Y: fromHex("32969E5780CFE1C849A1C276D7AEB4F38A23B591739AA2FE197349AEEBD31366AEE5EB7E6C6DDB7C57D02432B30DB5AA66D9884299FAA72568944E4EEDC92EA3FBC6F39F53412FBCC563208F7C15B737AC8910DBC2D9C9B8C001E72FDC40EB694AB1F06A5A2DBD18D9E36C66F31F566742F11EC0A52E9F7B89355C02FB5D32D2"),
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
if Verify(&pub, []byte("testing"), fromHex("2"), fromHex("4")) {
|
| 111 |
+
t.Errorf("Verify unexpected success with non-existent mod inverse of Q")
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
func TestSigningWithDegenerateKeys(t *testing.T) {
|
| 116 |
+
// Signing with degenerate private keys should not cause an infinite
|
| 117 |
+
// loop.
|
| 118 |
+
badKeys := []struct {
|
| 119 |
+
p, q, g, y, x string
|
| 120 |
+
}{
|
| 121 |
+
{"00", "01", "00", "00", "00"},
|
| 122 |
+
{"01", "ff", "00", "00", "00"},
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
for i, test := range badKeys {
|
| 126 |
+
priv := PrivateKey{
|
| 127 |
+
PublicKey: PublicKey{
|
| 128 |
+
Parameters: Parameters{
|
| 129 |
+
P: fromHex(test.p),
|
| 130 |
+
Q: fromHex(test.q),
|
| 131 |
+
G: fromHex(test.g),
|
| 132 |
+
},
|
| 133 |
+
Y: fromHex(test.y),
|
| 134 |
+
},
|
| 135 |
+
X: fromHex(test.x),
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
hashed := []byte("testing")
|
| 139 |
+
if _, _, err := Sign(rand.Reader, &priv, hashed); err == nil {
|
| 140 |
+
t.Errorf("#%d: unexpected success", i)
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/ecdh/ecdh.go
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2022 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// Package ecdh implements Elliptic Curve Diffie-Hellman over
|
| 6 |
+
// NIST curves and Curve25519.
|
| 7 |
+
package ecdh
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"crypto"
|
| 11 |
+
"crypto/internal/boring"
|
| 12 |
+
"crypto/subtle"
|
| 13 |
+
"errors"
|
| 14 |
+
"io"
|
| 15 |
+
"sync"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
type Curve interface {
|
| 19 |
+
// GenerateKey generates a random PrivateKey.
|
| 20 |
+
//
|
| 21 |
+
// Most applications should use [crypto/rand.Reader] as rand. Note that the
|
| 22 |
+
// returned key does not depend deterministically on the bytes read from rand,
|
| 23 |
+
// and may change between calls and/or between versions.
|
| 24 |
+
GenerateKey(rand io.Reader) (*PrivateKey, error)
|
| 25 |
+
|
| 26 |
+
// NewPrivateKey checks that key is valid and returns a PrivateKey.
|
| 27 |
+
//
|
| 28 |
+
// For NIST curves, this follows SEC 1, Version 2.0, Section 2.3.6, which
|
| 29 |
+
// amounts to decoding the bytes as a fixed length big endian integer and
|
| 30 |
+
// checking that the result is lower than the order of the curve. The zero
|
| 31 |
+
// private key is also rejected, as the encoding of the corresponding public
|
| 32 |
+
// key would be irregular.
|
| 33 |
+
//
|
| 34 |
+
// For X25519, this only checks the scalar length.
|
| 35 |
+
NewPrivateKey(key []byte) (*PrivateKey, error)
|
| 36 |
+
|
| 37 |
+
// NewPublicKey checks that key is valid and returns a PublicKey.
|
| 38 |
+
//
|
| 39 |
+
// For NIST curves, this decodes an uncompressed point according to SEC 1,
|
| 40 |
+
// Version 2.0, Section 2.3.4. Compressed encodings and the point at
|
| 41 |
+
// infinity are rejected.
|
| 42 |
+
//
|
| 43 |
+
// For X25519, this only checks the u-coordinate length. Adversarially
|
| 44 |
+
// selected public keys can cause ECDH to return an error.
|
| 45 |
+
NewPublicKey(key []byte) (*PublicKey, error)
|
| 46 |
+
|
| 47 |
+
// ecdh performs an ECDH exchange and returns the shared secret. It's exposed
|
| 48 |
+
// as the PrivateKey.ECDH method.
|
| 49 |
+
//
|
| 50 |
+
// The private method also allow us to expand the ECDH interface with more
|
| 51 |
+
// methods in the future without breaking backwards compatibility.
|
| 52 |
+
ecdh(local *PrivateKey, remote *PublicKey) ([]byte, error)
|
| 53 |
+
|
| 54 |
+
// privateKeyToPublicKey converts a PrivateKey to a PublicKey. It's exposed
|
| 55 |
+
// as the PrivateKey.PublicKey method.
|
| 56 |
+
//
|
| 57 |
+
// This method always succeeds: for X25519, the zero key can't be
|
| 58 |
+
// constructed due to clamping; for NIST curves, it is rejected by
|
| 59 |
+
// NewPrivateKey.
|
| 60 |
+
privateKeyToPublicKey(*PrivateKey) *PublicKey
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// PublicKey is an ECDH public key, usually a peer's ECDH share sent over the wire.
|
| 64 |
+
//
|
| 65 |
+
// These keys can be parsed with [crypto/x509.ParsePKIXPublicKey] and encoded
|
| 66 |
+
// with [crypto/x509.MarshalPKIXPublicKey]. For NIST curves, they then need to
|
| 67 |
+
// be converted with [crypto/ecdsa.PublicKey.ECDH] after parsing.
|
| 68 |
+
type PublicKey struct {
|
| 69 |
+
curve Curve
|
| 70 |
+
publicKey []byte
|
| 71 |
+
boring *boring.PublicKeyECDH
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
// Bytes returns a copy of the encoding of the public key.
|
| 75 |
+
func (k *PublicKey) Bytes() []byte {
|
| 76 |
+
// Copy the public key to a fixed size buffer that can get allocated on the
|
| 77 |
+
// caller's stack after inlining.
|
| 78 |
+
var buf [133]byte
|
| 79 |
+
return append(buf[:0], k.publicKey...)
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
// Equal returns whether x represents the same public key as k.
|
| 83 |
+
//
|
| 84 |
+
// Note that there can be equivalent public keys with different encodings which
|
| 85 |
+
// would return false from this check but behave the same way as inputs to ECDH.
|
| 86 |
+
//
|
| 87 |
+
// This check is performed in constant time as long as the key types and their
|
| 88 |
+
// curve match.
|
| 89 |
+
func (k *PublicKey) Equal(x crypto.PublicKey) bool {
|
| 90 |
+
xx, ok := x.(*PublicKey)
|
| 91 |
+
if !ok {
|
| 92 |
+
return false
|
| 93 |
+
}
|
| 94 |
+
return k.curve == xx.curve &&
|
| 95 |
+
subtle.ConstantTimeCompare(k.publicKey, xx.publicKey) == 1
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
func (k *PublicKey) Curve() Curve {
|
| 99 |
+
return k.curve
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// PrivateKey is an ECDH private key, usually kept secret.
|
| 103 |
+
//
|
| 104 |
+
// These keys can be parsed with [crypto/x509.ParsePKCS8PrivateKey] and encoded
|
| 105 |
+
// with [crypto/x509.MarshalPKCS8PrivateKey]. For NIST curves, they then need to
|
| 106 |
+
// be converted with [crypto/ecdsa.PrivateKey.ECDH] after parsing.
|
| 107 |
+
type PrivateKey struct {
|
| 108 |
+
curve Curve
|
| 109 |
+
privateKey []byte
|
| 110 |
+
boring *boring.PrivateKeyECDH
|
| 111 |
+
// publicKey is set under publicKeyOnce, to allow loading private keys with
|
| 112 |
+
// NewPrivateKey without having to perform a scalar multiplication.
|
| 113 |
+
publicKey *PublicKey
|
| 114 |
+
publicKeyOnce sync.Once
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// ECDH performs an ECDH exchange and returns the shared secret. The [PrivateKey]
|
| 118 |
+
// and [PublicKey] must use the same curve.
|
| 119 |
+
//
|
| 120 |
+
// For NIST curves, this performs ECDH as specified in SEC 1, Version 2.0,
|
| 121 |
+
// Section 3.3.1, and returns the x-coordinate encoded according to SEC 1,
|
| 122 |
+
// Version 2.0, Section 2.3.5. The result is never the point at infinity.
|
| 123 |
+
//
|
| 124 |
+
// For [X25519], this performs ECDH as specified in RFC 7748, Section 6.1. If
|
| 125 |
+
// the result is the all-zero value, ECDH returns an error.
|
| 126 |
+
func (k *PrivateKey) ECDH(remote *PublicKey) ([]byte, error) {
|
| 127 |
+
if k.curve != remote.curve {
|
| 128 |
+
return nil, errors.New("crypto/ecdh: private key and public key curves do not match")
|
| 129 |
+
}
|
| 130 |
+
return k.curve.ecdh(k, remote)
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
// Bytes returns a copy of the encoding of the private key.
|
| 134 |
+
func (k *PrivateKey) Bytes() []byte {
|
| 135 |
+
// Copy the private key to a fixed size buffer that can get allocated on the
|
| 136 |
+
// caller's stack after inlining.
|
| 137 |
+
var buf [66]byte
|
| 138 |
+
return append(buf[:0], k.privateKey...)
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
// Equal returns whether x represents the same private key as k.
|
| 142 |
+
//
|
| 143 |
+
// Note that there can be equivalent private keys with different encodings which
|
| 144 |
+
// would return false from this check but behave the same way as inputs to [ECDH].
|
| 145 |
+
//
|
| 146 |
+
// This check is performed in constant time as long as the key types and their
|
| 147 |
+
// curve match.
|
| 148 |
+
func (k *PrivateKey) Equal(x crypto.PrivateKey) bool {
|
| 149 |
+
xx, ok := x.(*PrivateKey)
|
| 150 |
+
if !ok {
|
| 151 |
+
return false
|
| 152 |
+
}
|
| 153 |
+
return k.curve == xx.curve &&
|
| 154 |
+
subtle.ConstantTimeCompare(k.privateKey, xx.privateKey) == 1
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
func (k *PrivateKey) Curve() Curve {
|
| 158 |
+
return k.curve
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
func (k *PrivateKey) PublicKey() *PublicKey {
|
| 162 |
+
k.publicKeyOnce.Do(func() {
|
| 163 |
+
if k.boring != nil {
|
| 164 |
+
// Because we already checked in NewPrivateKey that the key is valid,
|
| 165 |
+
// there should not be any possible errors from BoringCrypto,
|
| 166 |
+
// so we turn the error into a panic.
|
| 167 |
+
// (We can't return it anyhow.)
|
| 168 |
+
kpub, err := k.boring.PublicKey()
|
| 169 |
+
if err != nil {
|
| 170 |
+
panic("boringcrypto: " + err.Error())
|
| 171 |
+
}
|
| 172 |
+
k.publicKey = &PublicKey{
|
| 173 |
+
curve: k.curve,
|
| 174 |
+
publicKey: kpub.Bytes(),
|
| 175 |
+
boring: kpub,
|
| 176 |
+
}
|
| 177 |
+
} else {
|
| 178 |
+
k.publicKey = k.curve.privateKeyToPublicKey(k)
|
| 179 |
+
}
|
| 180 |
+
})
|
| 181 |
+
return k.publicKey
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
// Public implements the implicit interface of all standard library private
|
| 185 |
+
// keys. See the docs of [crypto.PrivateKey].
|
| 186 |
+
func (k *PrivateKey) Public() crypto.PublicKey {
|
| 187 |
+
return k.PublicKey()
|
| 188 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/ecdh/ecdh_test.go
ADDED
|
@@ -0,0 +1,525 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package ecdh_test
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"crypto"
|
| 10 |
+
"crypto/cipher"
|
| 11 |
+
"crypto/ecdh"
|
| 12 |
+
"crypto/rand"
|
| 13 |
+
"crypto/sha256"
|
| 14 |
+
"encoding/hex"
|
| 15 |
+
"fmt"
|
| 16 |
+
"internal/testenv"
|
| 17 |
+
"io"
|
| 18 |
+
"os"
|
| 19 |
+
"os/exec"
|
| 20 |
+
"path/filepath"
|
| 21 |
+
"regexp"
|
| 22 |
+
"strings"
|
| 23 |
+
"testing"
|
| 24 |
+
|
| 25 |
+
"golang.org/x/crypto/chacha20"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
// Check that PublicKey and PrivateKey implement the interfaces documented in
|
| 29 |
+
// crypto.PublicKey and crypto.PrivateKey.
|
| 30 |
+
var _ interface {
|
| 31 |
+
Equal(x crypto.PublicKey) bool
|
| 32 |
+
} = &ecdh.PublicKey{}
|
| 33 |
+
var _ interface {
|
| 34 |
+
Public() crypto.PublicKey
|
| 35 |
+
Equal(x crypto.PrivateKey) bool
|
| 36 |
+
} = &ecdh.PrivateKey{}
|
| 37 |
+
|
| 38 |
+
func TestECDH(t *testing.T) {
|
| 39 |
+
testAllCurves(t, func(t *testing.T, curve ecdh.Curve) {
|
| 40 |
+
aliceKey, err := curve.GenerateKey(rand.Reader)
|
| 41 |
+
if err != nil {
|
| 42 |
+
t.Fatal(err)
|
| 43 |
+
}
|
| 44 |
+
bobKey, err := curve.GenerateKey(rand.Reader)
|
| 45 |
+
if err != nil {
|
| 46 |
+
t.Fatal(err)
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
alicePubKey, err := curve.NewPublicKey(aliceKey.PublicKey().Bytes())
|
| 50 |
+
if err != nil {
|
| 51 |
+
t.Error(err)
|
| 52 |
+
}
|
| 53 |
+
if !bytes.Equal(aliceKey.PublicKey().Bytes(), alicePubKey.Bytes()) {
|
| 54 |
+
t.Error("encoded and decoded public keys are different")
|
| 55 |
+
}
|
| 56 |
+
if !aliceKey.PublicKey().Equal(alicePubKey) {
|
| 57 |
+
t.Error("encoded and decoded public keys are different")
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
alicePrivKey, err := curve.NewPrivateKey(aliceKey.Bytes())
|
| 61 |
+
if err != nil {
|
| 62 |
+
t.Error(err)
|
| 63 |
+
}
|
| 64 |
+
if !bytes.Equal(aliceKey.Bytes(), alicePrivKey.Bytes()) {
|
| 65 |
+
t.Error("encoded and decoded private keys are different")
|
| 66 |
+
}
|
| 67 |
+
if !aliceKey.Equal(alicePrivKey) {
|
| 68 |
+
t.Error("encoded and decoded private keys are different")
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
bobSecret, err := bobKey.ECDH(aliceKey.PublicKey())
|
| 72 |
+
if err != nil {
|
| 73 |
+
t.Fatal(err)
|
| 74 |
+
}
|
| 75 |
+
aliceSecret, err := aliceKey.ECDH(bobKey.PublicKey())
|
| 76 |
+
if err != nil {
|
| 77 |
+
t.Fatal(err)
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
if !bytes.Equal(bobSecret, aliceSecret) {
|
| 81 |
+
t.Error("two ECDH computations came out different")
|
| 82 |
+
}
|
| 83 |
+
})
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
type countingReader struct {
|
| 87 |
+
r io.Reader
|
| 88 |
+
n int
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
func (r *countingReader) Read(p []byte) (int, error) {
|
| 92 |
+
n, err := r.r.Read(p)
|
| 93 |
+
r.n += n
|
| 94 |
+
return n, err
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
func TestGenerateKey(t *testing.T) {
|
| 98 |
+
testAllCurves(t, func(t *testing.T, curve ecdh.Curve) {
|
| 99 |
+
r := &countingReader{r: rand.Reader}
|
| 100 |
+
k, err := curve.GenerateKey(r)
|
| 101 |
+
if err != nil {
|
| 102 |
+
t.Fatal(err)
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
// GenerateKey does rejection sampling. If the masking works correctly,
|
| 106 |
+
// the probability of a rejection is 1-ord(G)/2^ceil(log2(ord(G))),
|
| 107 |
+
// which for all curves is small enough (at most 2^-32, for P-256) that
|
| 108 |
+
// a bit flip is more likely to make this test fail than bad luck.
|
| 109 |
+
// Account for the extra MaybeReadByte byte, too.
|
| 110 |
+
if got, expected := r.n, len(k.Bytes())+1; got > expected {
|
| 111 |
+
t.Errorf("expected GenerateKey to consume at most %v bytes, got %v", expected, got)
|
| 112 |
+
}
|
| 113 |
+
})
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
var vectors = map[ecdh.Curve]struct {
|
| 117 |
+
PrivateKey, PublicKey string
|
| 118 |
+
PeerPublicKey string
|
| 119 |
+
SharedSecret string
|
| 120 |
+
}{
|
| 121 |
+
// NIST vectors from CAVS 14.1, ECC CDH Primitive (SP800-56A).
|
| 122 |
+
ecdh.P256(): {
|
| 123 |
+
PrivateKey: "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534",
|
| 124 |
+
PublicKey: "04ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230" +
|
| 125 |
+
"28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141",
|
| 126 |
+
PeerPublicKey: "04700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287" +
|
| 127 |
+
"db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac",
|
| 128 |
+
SharedSecret: "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b",
|
| 129 |
+
},
|
| 130 |
+
ecdh.P384(): {
|
| 131 |
+
PrivateKey: "3cc3122a68f0d95027ad38c067916ba0eb8c38894d22e1b15618b6818a661774ad463b205da88cf699ab4d43c9cf98a1",
|
| 132 |
+
PublicKey: "049803807f2f6d2fd966cdd0290bd410c0190352fbec7ff6247de1302df86f25d34fe4a97bef60cff548355c015dbb3e5f" +
|
| 133 |
+
"ba26ca69ec2f5b5d9dad20cc9da711383a9dbe34ea3fa5a2af75b46502629ad54dd8b7d73a8abb06a3a3be47d650cc99",
|
| 134 |
+
PeerPublicKey: "04a7c76b970c3b5fe8b05d2838ae04ab47697b9eaf52e764592efda27fe7513272734466b400091adbf2d68c58e0c50066" +
|
| 135 |
+
"ac68f19f2e1cb879aed43a9969b91a0839c4c38a49749b661efedf243451915ed0905a32b060992b468c64766fc8437a",
|
| 136 |
+
SharedSecret: "5f9d29dc5e31a163060356213669c8ce132e22f57c9a04f40ba7fcead493b457e5621e766c40a2e3d4d6a04b25e533f1",
|
| 137 |
+
},
|
| 138 |
+
// For some reason all field elements in the test vector (both scalars and
|
| 139 |
+
// base field elements), but not the shared secret output, have two extra
|
| 140 |
+
// leading zero bytes (which in big-endian are irrelevant). Removed here.
|
| 141 |
+
ecdh.P521(): {
|
| 142 |
+
PrivateKey: "017eecc07ab4b329068fba65e56a1f8890aa935e57134ae0ffcce802735151f4eac6564f6ee9974c5e6887a1fefee5743ae2241bfeb95d5ce31ddcb6f9edb4d6fc47",
|
| 143 |
+
PublicKey: "0400602f9d0cf9e526b29e22381c203c48a886c2b0673033366314f1ffbcba240ba42f4ef38a76174635f91e6b4ed34275eb01c8467d05ca80315bf1a7bbd945f550a5" +
|
| 144 |
+
"01b7c85f26f5d4b2d7355cf6b02117659943762b6d1db5ab4f1dbc44ce7b2946eb6c7de342962893fd387d1b73d7a8672d1f236961170b7eb3579953ee5cdc88cd2d",
|
| 145 |
+
PeerPublicKey: "0400685a48e86c79f0f0875f7bc18d25eb5fc8c0b07e5da4f4370f3a9490340854334b1e1b87fa395464c60626124a4e70d0f785601d37c09870ebf176666877a2046d" +
|
| 146 |
+
"01ba52c56fc8776d9e8f5db4f0cc27636d0b741bbe05400697942e80b739884a83bde99e0f6716939e632bc8986fa18dccd443a348b6c3e522497955a4f3c302f676",
|
| 147 |
+
SharedSecret: "005fc70477c3e63bc3954bd0df3ea0d1f41ee21746ed95fc5e1fdf90930d5e136672d72cc770742d1711c3c3a4c334a0ad9759436a4d3c5bf6e74b9578fac148c831",
|
| 148 |
+
},
|
| 149 |
+
// X25519 test vector from RFC 7748, Section 6.1.
|
| 150 |
+
ecdh.X25519(): {
|
| 151 |
+
PrivateKey: "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a",
|
| 152 |
+
PublicKey: "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a",
|
| 153 |
+
PeerPublicKey: "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f",
|
| 154 |
+
SharedSecret: "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742",
|
| 155 |
+
},
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
func TestVectors(t *testing.T) {
|
| 159 |
+
testAllCurves(t, func(t *testing.T, curve ecdh.Curve) {
|
| 160 |
+
v := vectors[curve]
|
| 161 |
+
key, err := curve.NewPrivateKey(hexDecode(t, v.PrivateKey))
|
| 162 |
+
if err != nil {
|
| 163 |
+
t.Fatal(err)
|
| 164 |
+
}
|
| 165 |
+
if !bytes.Equal(key.PublicKey().Bytes(), hexDecode(t, v.PublicKey)) {
|
| 166 |
+
t.Error("public key derived from the private key does not match")
|
| 167 |
+
}
|
| 168 |
+
peer, err := curve.NewPublicKey(hexDecode(t, v.PeerPublicKey))
|
| 169 |
+
if err != nil {
|
| 170 |
+
t.Fatal(err)
|
| 171 |
+
}
|
| 172 |
+
secret, err := key.ECDH(peer)
|
| 173 |
+
if err != nil {
|
| 174 |
+
t.Fatal(err)
|
| 175 |
+
}
|
| 176 |
+
if !bytes.Equal(secret, hexDecode(t, v.SharedSecret)) {
|
| 177 |
+
t.Errorf("shared secret does not match: %x %x %s %x", secret, sha256.Sum256(secret), v.SharedSecret,
|
| 178 |
+
sha256.Sum256(hexDecode(t, v.SharedSecret)))
|
| 179 |
+
}
|
| 180 |
+
})
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
func hexDecode(t *testing.T, s string) []byte {
|
| 184 |
+
b, err := hex.DecodeString(s)
|
| 185 |
+
if err != nil {
|
| 186 |
+
t.Fatal("invalid hex string:", s)
|
| 187 |
+
}
|
| 188 |
+
return b
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
func TestString(t *testing.T) {
|
| 192 |
+
testAllCurves(t, func(t *testing.T, curve ecdh.Curve) {
|
| 193 |
+
s := fmt.Sprintf("%s", curve)
|
| 194 |
+
if s[:1] != "P" && s[:1] != "X" {
|
| 195 |
+
t.Errorf("unexpected Curve string encoding: %q", s)
|
| 196 |
+
}
|
| 197 |
+
})
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
func TestX25519Failure(t *testing.T) {
|
| 201 |
+
identity := hexDecode(t, "0000000000000000000000000000000000000000000000000000000000000000")
|
| 202 |
+
lowOrderPoint := hexDecode(t, "e0eb7a7c3b41b8ae1656e3faf19fc46ada098deb9c32b1fd866205165f49b800")
|
| 203 |
+
randomScalar := make([]byte, 32)
|
| 204 |
+
rand.Read(randomScalar)
|
| 205 |
+
|
| 206 |
+
t.Run("identity point", func(t *testing.T) { testX25519Failure(t, randomScalar, identity) })
|
| 207 |
+
t.Run("low order point", func(t *testing.T) { testX25519Failure(t, randomScalar, lowOrderPoint) })
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
func testX25519Failure(t *testing.T, private, public []byte) {
|
| 211 |
+
priv, err := ecdh.X25519().NewPrivateKey(private)
|
| 212 |
+
if err != nil {
|
| 213 |
+
t.Fatal(err)
|
| 214 |
+
}
|
| 215 |
+
pub, err := ecdh.X25519().NewPublicKey(public)
|
| 216 |
+
if err != nil {
|
| 217 |
+
t.Fatal(err)
|
| 218 |
+
}
|
| 219 |
+
secret, err := priv.ECDH(pub)
|
| 220 |
+
if err == nil {
|
| 221 |
+
t.Error("expected ECDH error")
|
| 222 |
+
}
|
| 223 |
+
if secret != nil {
|
| 224 |
+
t.Errorf("unexpected ECDH output: %x", secret)
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
var invalidPrivateKeys = map[ecdh.Curve][]string{
|
| 229 |
+
ecdh.P256(): {
|
| 230 |
+
// Bad lengths.
|
| 231 |
+
"",
|
| 232 |
+
"01",
|
| 233 |
+
"01010101010101010101010101010101010101010101010101010101010101",
|
| 234 |
+
"000101010101010101010101010101010101010101010101010101010101010101",
|
| 235 |
+
strings.Repeat("01", 200),
|
| 236 |
+
// Zero.
|
| 237 |
+
"0000000000000000000000000000000000000000000000000000000000000000",
|
| 238 |
+
// Order of the curve and above.
|
| 239 |
+
"ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
|
| 240 |
+
"ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632552",
|
| 241 |
+
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
| 242 |
+
},
|
| 243 |
+
ecdh.P384(): {
|
| 244 |
+
// Bad lengths.
|
| 245 |
+
"",
|
| 246 |
+
"01",
|
| 247 |
+
"0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
|
| 248 |
+
"00010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
|
| 249 |
+
strings.Repeat("01", 200),
|
| 250 |
+
// Zero.
|
| 251 |
+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
| 252 |
+
// Order of the curve and above.
|
| 253 |
+
"ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973",
|
| 254 |
+
"ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52974",
|
| 255 |
+
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
| 256 |
+
},
|
| 257 |
+
ecdh.P521(): {
|
| 258 |
+
// Bad lengths.
|
| 259 |
+
"",
|
| 260 |
+
"01",
|
| 261 |
+
"0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
|
| 262 |
+
"00010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
|
| 263 |
+
strings.Repeat("01", 200),
|
| 264 |
+
// Zero.
|
| 265 |
+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
| 266 |
+
// Order of the curve and above.
|
| 267 |
+
"01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
|
| 268 |
+
"01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e9138640a",
|
| 269 |
+
"11fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
|
| 270 |
+
"03fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4a30d0f077e5f2cd6ff980291ee134ba0776b937113388f5d76df6e3d2270c812",
|
| 271 |
+
},
|
| 272 |
+
ecdh.X25519(): {
|
| 273 |
+
// X25519 only rejects bad lengths.
|
| 274 |
+
"",
|
| 275 |
+
"01",
|
| 276 |
+
"01010101010101010101010101010101010101010101010101010101010101",
|
| 277 |
+
"000101010101010101010101010101010101010101010101010101010101010101",
|
| 278 |
+
strings.Repeat("01", 200),
|
| 279 |
+
},
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
func TestNewPrivateKey(t *testing.T) {
|
| 283 |
+
testAllCurves(t, func(t *testing.T, curve ecdh.Curve) {
|
| 284 |
+
for _, input := range invalidPrivateKeys[curve] {
|
| 285 |
+
k, err := curve.NewPrivateKey(hexDecode(t, input))
|
| 286 |
+
if err == nil {
|
| 287 |
+
t.Errorf("unexpectedly accepted %q", input)
|
| 288 |
+
} else if k != nil {
|
| 289 |
+
t.Error("PrivateKey was not nil on error")
|
| 290 |
+
} else if strings.Contains(err.Error(), "boringcrypto") {
|
| 291 |
+
t.Errorf("boringcrypto error leaked out: %v", err)
|
| 292 |
+
}
|
| 293 |
+
}
|
| 294 |
+
})
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
var invalidPublicKeys = map[ecdh.Curve][]string{
|
| 298 |
+
ecdh.P256(): {
|
| 299 |
+
// Bad lengths.
|
| 300 |
+
"",
|
| 301 |
+
"04",
|
| 302 |
+
strings.Repeat("04", 200),
|
| 303 |
+
// Infinity.
|
| 304 |
+
"00",
|
| 305 |
+
// Compressed encodings.
|
| 306 |
+
"036b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
|
| 307 |
+
"02e2534a3532d08fbba02dde659ee62bd0031fe2db785596ef509302446b030852",
|
| 308 |
+
// Points not on the curve.
|
| 309 |
+
"046b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c2964fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f6",
|
| 310 |
+
"0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
| 311 |
+
},
|
| 312 |
+
ecdh.P384(): {
|
| 313 |
+
// Bad lengths.
|
| 314 |
+
"",
|
| 315 |
+
"04",
|
| 316 |
+
strings.Repeat("04", 200),
|
| 317 |
+
// Infinity.
|
| 318 |
+
"00",
|
| 319 |
+
// Compressed encodings.
|
| 320 |
+
"03aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7",
|
| 321 |
+
"0208d999057ba3d2d969260045c55b97f089025959a6f434d651d207d19fb96e9e4fe0e86ebe0e64f85b96a9c75295df61",
|
| 322 |
+
// Points not on the curve.
|
| 323 |
+
"04aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab73617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e60",
|
| 324 |
+
"04000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
| 325 |
+
},
|
| 326 |
+
ecdh.P521(): {
|
| 327 |
+
// Bad lengths.
|
| 328 |
+
"",
|
| 329 |
+
"04",
|
| 330 |
+
strings.Repeat("04", 200),
|
| 331 |
+
// Infinity.
|
| 332 |
+
"00",
|
| 333 |
+
// Compressed encodings.
|
| 334 |
+
"030035b5df64ae2ac204c354b483487c9070cdc61c891c5ff39afc06c5d55541d3ceac8659e24afe3d0750e8b88e9f078af066a1d5025b08e5a5e2fbc87412871902f3",
|
| 335 |
+
"0200c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
|
| 336 |
+
// Points not on the curve.
|
| 337 |
+
"0400c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16651",
|
| 338 |
+
"04000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
| 339 |
+
},
|
| 340 |
+
ecdh.X25519(): {},
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
func TestNewPublicKey(t *testing.T) {
|
| 344 |
+
testAllCurves(t, func(t *testing.T, curve ecdh.Curve) {
|
| 345 |
+
for _, input := range invalidPublicKeys[curve] {
|
| 346 |
+
k, err := curve.NewPublicKey(hexDecode(t, input))
|
| 347 |
+
if err == nil {
|
| 348 |
+
t.Errorf("unexpectedly accepted %q", input)
|
| 349 |
+
} else if k != nil {
|
| 350 |
+
t.Error("PublicKey was not nil on error")
|
| 351 |
+
} else if strings.Contains(err.Error(), "boringcrypto") {
|
| 352 |
+
t.Errorf("boringcrypto error leaked out: %v", err)
|
| 353 |
+
}
|
| 354 |
+
}
|
| 355 |
+
})
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
func testAllCurves(t *testing.T, f func(t *testing.T, curve ecdh.Curve)) {
|
| 359 |
+
t.Run("P256", func(t *testing.T) { f(t, ecdh.P256()) })
|
| 360 |
+
t.Run("P384", func(t *testing.T) { f(t, ecdh.P384()) })
|
| 361 |
+
t.Run("P521", func(t *testing.T) { f(t, ecdh.P521()) })
|
| 362 |
+
t.Run("X25519", func(t *testing.T) { f(t, ecdh.X25519()) })
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
func BenchmarkECDH(b *testing.B) {
|
| 366 |
+
benchmarkAllCurves(b, func(b *testing.B, curve ecdh.Curve) {
|
| 367 |
+
c, err := chacha20.NewUnauthenticatedCipher(make([]byte, 32), make([]byte, 12))
|
| 368 |
+
if err != nil {
|
| 369 |
+
b.Fatal(err)
|
| 370 |
+
}
|
| 371 |
+
rand := cipher.StreamReader{
|
| 372 |
+
S: c, R: zeroReader,
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
peerKey, err := curve.GenerateKey(rand)
|
| 376 |
+
if err != nil {
|
| 377 |
+
b.Fatal(err)
|
| 378 |
+
}
|
| 379 |
+
peerShare := peerKey.PublicKey().Bytes()
|
| 380 |
+
b.ResetTimer()
|
| 381 |
+
b.ReportAllocs()
|
| 382 |
+
|
| 383 |
+
var allocationsSink byte
|
| 384 |
+
|
| 385 |
+
for i := 0; i < b.N; i++ {
|
| 386 |
+
key, err := curve.GenerateKey(rand)
|
| 387 |
+
if err != nil {
|
| 388 |
+
b.Fatal(err)
|
| 389 |
+
}
|
| 390 |
+
share := key.PublicKey().Bytes()
|
| 391 |
+
peerPubKey, err := curve.NewPublicKey(peerShare)
|
| 392 |
+
if err != nil {
|
| 393 |
+
b.Fatal(err)
|
| 394 |
+
}
|
| 395 |
+
secret, err := key.ECDH(peerPubKey)
|
| 396 |
+
if err != nil {
|
| 397 |
+
b.Fatal(err)
|
| 398 |
+
}
|
| 399 |
+
allocationsSink ^= secret[0] ^ share[0]
|
| 400 |
+
}
|
| 401 |
+
})
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
func benchmarkAllCurves(b *testing.B, f func(b *testing.B, curve ecdh.Curve)) {
|
| 405 |
+
b.Run("P256", func(b *testing.B) { f(b, ecdh.P256()) })
|
| 406 |
+
b.Run("P384", func(b *testing.B) { f(b, ecdh.P384()) })
|
| 407 |
+
b.Run("P521", func(b *testing.B) { f(b, ecdh.P521()) })
|
| 408 |
+
b.Run("X25519", func(b *testing.B) { f(b, ecdh.X25519()) })
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
type zr struct{}
|
| 412 |
+
|
| 413 |
+
// Read replaces the contents of dst with zeros. It is safe for concurrent use.
|
| 414 |
+
func (zr) Read(dst []byte) (n int, err error) {
|
| 415 |
+
for i := range dst {
|
| 416 |
+
dst[i] = 0
|
| 417 |
+
}
|
| 418 |
+
return len(dst), nil
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
var zeroReader = zr{}
|
| 422 |
+
|
| 423 |
+
const linkerTestProgram = `
|
| 424 |
+
package main
|
| 425 |
+
import "crypto/ecdh"
|
| 426 |
+
import "crypto/rand"
|
| 427 |
+
func main() {
|
| 428 |
+
curve := ecdh.P384()
|
| 429 |
+
key, err := curve.GenerateKey(rand.Reader)
|
| 430 |
+
if err != nil { panic(err) }
|
| 431 |
+
_, err = curve.NewPublicKey(key.PublicKey().Bytes())
|
| 432 |
+
if err != nil { panic(err) }
|
| 433 |
+
_, err = curve.NewPrivateKey(key.Bytes())
|
| 434 |
+
if err != nil { panic(err) }
|
| 435 |
+
_, err = key.ECDH(key.PublicKey())
|
| 436 |
+
if err != nil { panic(err) }
|
| 437 |
+
println("OK")
|
| 438 |
+
}
|
| 439 |
+
`
|
| 440 |
+
|
| 441 |
+
// TestLinker ensures that using one curve does not bring all other
|
| 442 |
+
// implementations into the binary. This also guarantees that govulncheck can
|
| 443 |
+
// avoid warning about a curve-specific vulnerability if that curve is not used.
|
| 444 |
+
func TestLinker(t *testing.T) {
|
| 445 |
+
if testing.Short() {
|
| 446 |
+
t.Skip("test requires running 'go build'")
|
| 447 |
+
}
|
| 448 |
+
testenv.MustHaveGoBuild(t)
|
| 449 |
+
|
| 450 |
+
dir := t.TempDir()
|
| 451 |
+
hello := filepath.Join(dir, "hello.go")
|
| 452 |
+
err := os.WriteFile(hello, []byte(linkerTestProgram), 0664)
|
| 453 |
+
if err != nil {
|
| 454 |
+
t.Fatal(err)
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
run := func(args ...string) string {
|
| 458 |
+
cmd := exec.Command(args[0], args[1:]...)
|
| 459 |
+
cmd.Dir = dir
|
| 460 |
+
out, err := cmd.CombinedOutput()
|
| 461 |
+
if err != nil {
|
| 462 |
+
t.Fatalf("%v: %v\n%s", args, err, string(out))
|
| 463 |
+
}
|
| 464 |
+
return string(out)
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
goBin := testenv.GoToolPath(t)
|
| 468 |
+
run(goBin, "build", "-o", "hello.exe", "hello.go")
|
| 469 |
+
if out := run("./hello.exe"); out != "OK\n" {
|
| 470 |
+
t.Error("unexpected output:", out)
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
// List all text symbols under crypto/... and make sure there are some for
|
| 474 |
+
// P384, but none for the other curves.
|
| 475 |
+
var consistent bool
|
| 476 |
+
nm := run(goBin, "tool", "nm", "hello.exe")
|
| 477 |
+
for _, match := range regexp.MustCompile(`(?m)T (crypto/.*)$`).FindAllStringSubmatch(nm, -1) {
|
| 478 |
+
symbol := strings.ToLower(match[1])
|
| 479 |
+
if strings.Contains(symbol, "p384") {
|
| 480 |
+
consistent = true
|
| 481 |
+
}
|
| 482 |
+
if strings.Contains(symbol, "p224") || strings.Contains(symbol, "p256") || strings.Contains(symbol, "p521") {
|
| 483 |
+
t.Errorf("unexpected symbol in program using only ecdh.P384: %s", match[1])
|
| 484 |
+
}
|
| 485 |
+
}
|
| 486 |
+
if !consistent {
|
| 487 |
+
t.Error("no P384 symbols found in program using ecdh.P384, test is broken")
|
| 488 |
+
}
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
func TestMismatchedCurves(t *testing.T) {
|
| 492 |
+
curves := []struct {
|
| 493 |
+
name string
|
| 494 |
+
curve ecdh.Curve
|
| 495 |
+
}{
|
| 496 |
+
{"P256", ecdh.P256()},
|
| 497 |
+
{"P384", ecdh.P384()},
|
| 498 |
+
{"P521", ecdh.P521()},
|
| 499 |
+
{"X25519", ecdh.X25519()},
|
| 500 |
+
}
|
| 501 |
+
|
| 502 |
+
for _, privCurve := range curves {
|
| 503 |
+
priv, err := privCurve.curve.GenerateKey(rand.Reader)
|
| 504 |
+
if err != nil {
|
| 505 |
+
t.Fatalf("failed to generate test key: %s", err)
|
| 506 |
+
}
|
| 507 |
+
|
| 508 |
+
for _, pubCurve := range curves {
|
| 509 |
+
if privCurve == pubCurve {
|
| 510 |
+
continue
|
| 511 |
+
}
|
| 512 |
+
t.Run(fmt.Sprintf("%s/%s", privCurve.name, pubCurve.name), func(t *testing.T) {
|
| 513 |
+
pub, err := pubCurve.curve.GenerateKey(rand.Reader)
|
| 514 |
+
if err != nil {
|
| 515 |
+
t.Fatalf("failed to generate test key: %s", err)
|
| 516 |
+
}
|
| 517 |
+
expected := "crypto/ecdh: private key and public key curves do not match"
|
| 518 |
+
_, err = priv.ECDH(pub.PublicKey())
|
| 519 |
+
if err.Error() != expected {
|
| 520 |
+
t.Fatalf("unexpected error: want %q, got %q", expected, err)
|
| 521 |
+
}
|
| 522 |
+
})
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2022 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package ecdh
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/internal/boring"
|
| 9 |
+
"crypto/internal/nistec"
|
| 10 |
+
"crypto/internal/randutil"
|
| 11 |
+
"encoding/binary"
|
| 12 |
+
"errors"
|
| 13 |
+
"io"
|
| 14 |
+
"math/bits"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
type nistCurve[Point nistPoint[Point]] struct {
|
| 18 |
+
name string
|
| 19 |
+
newPoint func() Point
|
| 20 |
+
scalarOrder []byte
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// nistPoint is a generic constraint for the nistec Point types.
|
| 24 |
+
type nistPoint[T any] interface {
|
| 25 |
+
Bytes() []byte
|
| 26 |
+
BytesX() ([]byte, error)
|
| 27 |
+
SetBytes([]byte) (T, error)
|
| 28 |
+
ScalarMult(T, []byte) (T, error)
|
| 29 |
+
ScalarBaseMult([]byte) (T, error)
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
func (c *nistCurve[Point]) String() string {
|
| 33 |
+
return c.name
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
var errInvalidPrivateKey = errors.New("crypto/ecdh: invalid private key")
|
| 37 |
+
|
| 38 |
+
func (c *nistCurve[Point]) GenerateKey(rand io.Reader) (*PrivateKey, error) {
|
| 39 |
+
if boring.Enabled && rand == boring.RandReader {
|
| 40 |
+
key, bytes, err := boring.GenerateKeyECDH(c.name)
|
| 41 |
+
if err != nil {
|
| 42 |
+
return nil, err
|
| 43 |
+
}
|
| 44 |
+
return newBoringPrivateKey(c, key, bytes)
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
key := make([]byte, len(c.scalarOrder))
|
| 48 |
+
randutil.MaybeReadByte(rand)
|
| 49 |
+
for {
|
| 50 |
+
if _, err := io.ReadFull(rand, key); err != nil {
|
| 51 |
+
return nil, err
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// Mask off any excess bits if the size of the underlying field is not a
|
| 55 |
+
// whole number of bytes, which is only the case for P-521. We use a
|
| 56 |
+
// pointer to the scalarOrder field because comparing generic and
|
| 57 |
+
// instantiated types is not supported.
|
| 58 |
+
if &c.scalarOrder[0] == &p521Order[0] {
|
| 59 |
+
key[0] &= 0b0000_0001
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
// In tests, rand will return all zeros and NewPrivateKey will reject
|
| 63 |
+
// the zero key as it generates the identity as a public key. This also
|
| 64 |
+
// makes this function consistent with crypto/elliptic.GenerateKey.
|
| 65 |
+
key[1] ^= 0x42
|
| 66 |
+
|
| 67 |
+
k, err := c.NewPrivateKey(key)
|
| 68 |
+
if err == errInvalidPrivateKey {
|
| 69 |
+
continue
|
| 70 |
+
}
|
| 71 |
+
return k, err
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
func (c *nistCurve[Point]) NewPrivateKey(key []byte) (*PrivateKey, error) {
|
| 76 |
+
if len(key) != len(c.scalarOrder) {
|
| 77 |
+
return nil, errors.New("crypto/ecdh: invalid private key size")
|
| 78 |
+
}
|
| 79 |
+
if isZero(key) || !isLess(key, c.scalarOrder) {
|
| 80 |
+
return nil, errInvalidPrivateKey
|
| 81 |
+
}
|
| 82 |
+
if boring.Enabled {
|
| 83 |
+
bk, err := boring.NewPrivateKeyECDH(c.name, key)
|
| 84 |
+
if err != nil {
|
| 85 |
+
return nil, err
|
| 86 |
+
}
|
| 87 |
+
return newBoringPrivateKey(c, bk, key)
|
| 88 |
+
}
|
| 89 |
+
k := &PrivateKey{
|
| 90 |
+
curve: c,
|
| 91 |
+
privateKey: append([]byte{}, key...),
|
| 92 |
+
}
|
| 93 |
+
return k, nil
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
func newBoringPrivateKey(c Curve, bk *boring.PrivateKeyECDH, privateKey []byte) (*PrivateKey, error) {
|
| 97 |
+
k := &PrivateKey{
|
| 98 |
+
curve: c,
|
| 99 |
+
boring: bk,
|
| 100 |
+
privateKey: append([]byte(nil), privateKey...),
|
| 101 |
+
}
|
| 102 |
+
return k, nil
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
func (c *nistCurve[Point]) privateKeyToPublicKey(key *PrivateKey) *PublicKey {
|
| 106 |
+
boring.Unreachable()
|
| 107 |
+
if key.curve != c {
|
| 108 |
+
panic("crypto/ecdh: internal error: converting the wrong key type")
|
| 109 |
+
}
|
| 110 |
+
p, err := c.newPoint().ScalarBaseMult(key.privateKey)
|
| 111 |
+
if err != nil {
|
| 112 |
+
// This is unreachable because the only error condition of
|
| 113 |
+
// ScalarBaseMult is if the input is not the right size.
|
| 114 |
+
panic("crypto/ecdh: internal error: nistec ScalarBaseMult failed for a fixed-size input")
|
| 115 |
+
}
|
| 116 |
+
publicKey := p.Bytes()
|
| 117 |
+
if len(publicKey) == 1 {
|
| 118 |
+
// The encoding of the identity is a single 0x00 byte. This is
|
| 119 |
+
// unreachable because the only scalar that generates the identity is
|
| 120 |
+
// zero, which is rejected by NewPrivateKey.
|
| 121 |
+
panic("crypto/ecdh: internal error: nistec ScalarBaseMult returned the identity")
|
| 122 |
+
}
|
| 123 |
+
return &PublicKey{
|
| 124 |
+
curve: key.curve,
|
| 125 |
+
publicKey: publicKey,
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// isZero returns whether a is all zeroes in constant time.
|
| 130 |
+
func isZero(a []byte) bool {
|
| 131 |
+
var acc byte
|
| 132 |
+
for _, b := range a {
|
| 133 |
+
acc |= b
|
| 134 |
+
}
|
| 135 |
+
return acc == 0
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
// isLess returns whether a < b, where a and b are big-endian buffers of the
|
| 139 |
+
// same length and shorter than 72 bytes.
|
| 140 |
+
func isLess(a, b []byte) bool {
|
| 141 |
+
if len(a) != len(b) {
|
| 142 |
+
panic("crypto/ecdh: internal error: mismatched isLess inputs")
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
// Copy the values into a fixed-size preallocated little-endian buffer.
|
| 146 |
+
// 72 bytes is enough for every scalar in this package, and having a fixed
|
| 147 |
+
// size lets us avoid heap allocations.
|
| 148 |
+
if len(a) > 72 {
|
| 149 |
+
panic("crypto/ecdh: internal error: isLess input too large")
|
| 150 |
+
}
|
| 151 |
+
bufA, bufB := make([]byte, 72), make([]byte, 72)
|
| 152 |
+
for i := range a {
|
| 153 |
+
bufA[i], bufB[i] = a[len(a)-i-1], b[len(b)-i-1]
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
// Perform a subtraction with borrow.
|
| 157 |
+
var borrow uint64
|
| 158 |
+
for i := 0; i < len(bufA); i += 8 {
|
| 159 |
+
limbA, limbB := binary.LittleEndian.Uint64(bufA[i:]), binary.LittleEndian.Uint64(bufB[i:])
|
| 160 |
+
_, borrow = bits.Sub64(limbA, limbB, borrow)
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
// If there is a borrow at the end of the operation, then a < b.
|
| 164 |
+
return borrow == 1
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
func (c *nistCurve[Point]) NewPublicKey(key []byte) (*PublicKey, error) {
|
| 168 |
+
// Reject the point at infinity and compressed encodings.
|
| 169 |
+
if len(key) == 0 || key[0] != 4 {
|
| 170 |
+
return nil, errors.New("crypto/ecdh: invalid public key")
|
| 171 |
+
}
|
| 172 |
+
k := &PublicKey{
|
| 173 |
+
curve: c,
|
| 174 |
+
publicKey: append([]byte{}, key...),
|
| 175 |
+
}
|
| 176 |
+
if boring.Enabled {
|
| 177 |
+
bk, err := boring.NewPublicKeyECDH(c.name, k.publicKey)
|
| 178 |
+
if err != nil {
|
| 179 |
+
return nil, err
|
| 180 |
+
}
|
| 181 |
+
k.boring = bk
|
| 182 |
+
} else {
|
| 183 |
+
// SetBytes also checks that the point is on the curve.
|
| 184 |
+
if _, err := c.newPoint().SetBytes(key); err != nil {
|
| 185 |
+
return nil, err
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
return k, nil
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
func (c *nistCurve[Point]) ecdh(local *PrivateKey, remote *PublicKey) ([]byte, error) {
|
| 192 |
+
// Note that this function can't return an error, as NewPublicKey rejects
|
| 193 |
+
// invalid points and the point at infinity, and NewPrivateKey rejects
|
| 194 |
+
// invalid scalars and the zero value. BytesX returns an error for the point
|
| 195 |
+
// at infinity, but in a prime order group such as the NIST curves that can
|
| 196 |
+
// only be the result of a scalar multiplication if one of the inputs is the
|
| 197 |
+
// zero scalar or the point at infinity.
|
| 198 |
+
|
| 199 |
+
if boring.Enabled {
|
| 200 |
+
return boring.ECDH(local.boring, remote.boring)
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
boring.Unreachable()
|
| 204 |
+
p, err := c.newPoint().SetBytes(remote.publicKey)
|
| 205 |
+
if err != nil {
|
| 206 |
+
return nil, err
|
| 207 |
+
}
|
| 208 |
+
if _, err := p.ScalarMult(p, local.privateKey); err != nil {
|
| 209 |
+
return nil, err
|
| 210 |
+
}
|
| 211 |
+
return p.BytesX()
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
// P256 returns a [Curve] which implements NIST P-256 (FIPS 186-3, section D.2.3),
|
| 215 |
+
// also known as secp256r1 or prime256v1.
|
| 216 |
+
//
|
| 217 |
+
// Multiple invocations of this function will return the same value, which can
|
| 218 |
+
// be used for equality checks and switch statements.
|
| 219 |
+
func P256() Curve { return p256 }
|
| 220 |
+
|
| 221 |
+
var p256 = &nistCurve[*nistec.P256Point]{
|
| 222 |
+
name: "P-256",
|
| 223 |
+
newPoint: nistec.NewP256Point,
|
| 224 |
+
scalarOrder: p256Order,
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
var p256Order = []byte{
|
| 228 |
+
0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
|
| 229 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 230 |
+
0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84,
|
| 231 |
+
0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51}
|
| 232 |
+
|
| 233 |
+
// P384 returns a [Curve] which implements NIST P-384 (FIPS 186-3, section D.2.4),
|
| 234 |
+
// also known as secp384r1.
|
| 235 |
+
//
|
| 236 |
+
// Multiple invocations of this function will return the same value, which can
|
| 237 |
+
// be used for equality checks and switch statements.
|
| 238 |
+
func P384() Curve { return p384 }
|
| 239 |
+
|
| 240 |
+
var p384 = &nistCurve[*nistec.P384Point]{
|
| 241 |
+
name: "P-384",
|
| 242 |
+
newPoint: nistec.NewP384Point,
|
| 243 |
+
scalarOrder: p384Order,
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
var p384Order = []byte{
|
| 247 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 248 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 249 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 250 |
+
0xc7, 0x63, 0x4d, 0x81, 0xf4, 0x37, 0x2d, 0xdf,
|
| 251 |
+
0x58, 0x1a, 0x0d, 0xb2, 0x48, 0xb0, 0xa7, 0x7a,
|
| 252 |
+
0xec, 0xec, 0x19, 0x6a, 0xcc, 0xc5, 0x29, 0x73}
|
| 253 |
+
|
| 254 |
+
// P521 returns a [Curve] which implements NIST P-521 (FIPS 186-3, section D.2.5),
|
| 255 |
+
// also known as secp521r1.
|
| 256 |
+
//
|
| 257 |
+
// Multiple invocations of this function will return the same value, which can
|
| 258 |
+
// be used for equality checks and switch statements.
|
| 259 |
+
func P521() Curve { return p521 }
|
| 260 |
+
|
| 261 |
+
var p521 = &nistCurve[*nistec.P521Point]{
|
| 262 |
+
name: "P-521",
|
| 263 |
+
newPoint: nistec.NewP521Point,
|
| 264 |
+
scalarOrder: p521Order,
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
var p521Order = []byte{0x01, 0xff,
|
| 268 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 269 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 270 |
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
| 271 |
+
0xff, 0xff, 0xff, 0xff, 0xff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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2022 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package ecdh
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/internal/edwards25519/field"
|
| 9 |
+
"crypto/internal/randutil"
|
| 10 |
+
"errors"
|
| 11 |
+
"io"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
var (
|
| 15 |
+
x25519PublicKeySize = 32
|
| 16 |
+
x25519PrivateKeySize = 32
|
| 17 |
+
x25519SharedSecretSize = 32
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
// X25519 returns a [Curve] which implements the X25519 function over Curve25519
|
| 21 |
+
// (RFC 7748, Section 5).
|
| 22 |
+
//
|
| 23 |
+
// Multiple invocations of this function will return the same value, so it can
|
| 24 |
+
// be used for equality checks and switch statements.
|
| 25 |
+
func X25519() Curve { return x25519 }
|
| 26 |
+
|
| 27 |
+
var x25519 = &x25519Curve{}
|
| 28 |
+
|
| 29 |
+
type x25519Curve struct{}
|
| 30 |
+
|
| 31 |
+
func (c *x25519Curve) String() string {
|
| 32 |
+
return "X25519"
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
func (c *x25519Curve) GenerateKey(rand io.Reader) (*PrivateKey, error) {
|
| 36 |
+
key := make([]byte, x25519PrivateKeySize)
|
| 37 |
+
randutil.MaybeReadByte(rand)
|
| 38 |
+
if _, err := io.ReadFull(rand, key); err != nil {
|
| 39 |
+
return nil, err
|
| 40 |
+
}
|
| 41 |
+
return c.NewPrivateKey(key)
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
func (c *x25519Curve) NewPrivateKey(key []byte) (*PrivateKey, error) {
|
| 45 |
+
if len(key) != x25519PrivateKeySize {
|
| 46 |
+
return nil, errors.New("crypto/ecdh: invalid private key size")
|
| 47 |
+
}
|
| 48 |
+
return &PrivateKey{
|
| 49 |
+
curve: c,
|
| 50 |
+
privateKey: append([]byte{}, key...),
|
| 51 |
+
}, nil
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
func (c *x25519Curve) privateKeyToPublicKey(key *PrivateKey) *PublicKey {
|
| 55 |
+
if key.curve != c {
|
| 56 |
+
panic("crypto/ecdh: internal error: converting the wrong key type")
|
| 57 |
+
}
|
| 58 |
+
k := &PublicKey{
|
| 59 |
+
curve: key.curve,
|
| 60 |
+
publicKey: make([]byte, x25519PublicKeySize),
|
| 61 |
+
}
|
| 62 |
+
x25519Basepoint := [32]byte{9}
|
| 63 |
+
x25519ScalarMult(k.publicKey, key.privateKey, x25519Basepoint[:])
|
| 64 |
+
return k
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
func (c *x25519Curve) NewPublicKey(key []byte) (*PublicKey, error) {
|
| 68 |
+
if len(key) != x25519PublicKeySize {
|
| 69 |
+
return nil, errors.New("crypto/ecdh: invalid public key")
|
| 70 |
+
}
|
| 71 |
+
return &PublicKey{
|
| 72 |
+
curve: c,
|
| 73 |
+
publicKey: append([]byte{}, key...),
|
| 74 |
+
}, nil
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
func (c *x25519Curve) ecdh(local *PrivateKey, remote *PublicKey) ([]byte, error) {
|
| 78 |
+
out := make([]byte, x25519SharedSecretSize)
|
| 79 |
+
x25519ScalarMult(out, local.privateKey, remote.publicKey)
|
| 80 |
+
if isZero(out) {
|
| 81 |
+
return nil, errors.New("crypto/ecdh: bad X25519 remote ECDH input: low order point")
|
| 82 |
+
}
|
| 83 |
+
return out, nil
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
func x25519ScalarMult(dst, scalar, point []byte) {
|
| 87 |
+
var e [32]byte
|
| 88 |
+
|
| 89 |
+
copy(e[:], scalar[:])
|
| 90 |
+
e[0] &= 248
|
| 91 |
+
e[31] &= 127
|
| 92 |
+
e[31] |= 64
|
| 93 |
+
|
| 94 |
+
var x1, x2, z2, x3, z3, tmp0, tmp1 field.Element
|
| 95 |
+
x1.SetBytes(point[:])
|
| 96 |
+
x2.One()
|
| 97 |
+
x3.Set(&x1)
|
| 98 |
+
z3.One()
|
| 99 |
+
|
| 100 |
+
swap := 0
|
| 101 |
+
for pos := 254; pos >= 0; pos-- {
|
| 102 |
+
b := e[pos/8] >> uint(pos&7)
|
| 103 |
+
b &= 1
|
| 104 |
+
swap ^= int(b)
|
| 105 |
+
x2.Swap(&x3, swap)
|
| 106 |
+
z2.Swap(&z3, swap)
|
| 107 |
+
swap = int(b)
|
| 108 |
+
|
| 109 |
+
tmp0.Subtract(&x3, &z3)
|
| 110 |
+
tmp1.Subtract(&x2, &z2)
|
| 111 |
+
x2.Add(&x2, &z2)
|
| 112 |
+
z2.Add(&x3, &z3)
|
| 113 |
+
z3.Multiply(&tmp0, &x2)
|
| 114 |
+
z2.Multiply(&z2, &tmp1)
|
| 115 |
+
tmp0.Square(&tmp1)
|
| 116 |
+
tmp1.Square(&x2)
|
| 117 |
+
x3.Add(&z3, &z2)
|
| 118 |
+
z2.Subtract(&z3, &z2)
|
| 119 |
+
x2.Multiply(&tmp1, &tmp0)
|
| 120 |
+
tmp1.Subtract(&tmp1, &tmp0)
|
| 121 |
+
z2.Square(&z2)
|
| 122 |
+
|
| 123 |
+
z3.Mult32(&tmp1, 121666)
|
| 124 |
+
x3.Square(&x3)
|
| 125 |
+
tmp0.Add(&tmp0, &z3)
|
| 126 |
+
z3.Multiply(&x1, &z2)
|
| 127 |
+
z2.Multiply(&tmp1, &tmp0)
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
x2.Swap(&x3, swap)
|
| 131 |
+
z2.Swap(&z3, swap)
|
| 132 |
+
|
| 133 |
+
z2.Invert(&z2)
|
| 134 |
+
x2.Multiply(&x2, &z2)
|
| 135 |
+
copy(dst[:], x2.Bytes())
|
| 136 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/ecdsa/boring.go
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
//go:build boringcrypto
|
| 6 |
+
|
| 7 |
+
package ecdsa
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"crypto/internal/boring"
|
| 11 |
+
"crypto/internal/boring/bbig"
|
| 12 |
+
"crypto/internal/boring/bcache"
|
| 13 |
+
"math/big"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
// Cached conversions from Go PublicKey/PrivateKey to BoringCrypto.
|
| 17 |
+
//
|
| 18 |
+
// The first operation on a PublicKey or PrivateKey makes a parallel
|
| 19 |
+
// BoringCrypto key and saves it in pubCache or privCache.
|
| 20 |
+
//
|
| 21 |
+
// We could just assume that once used in a Sign or Verify operation,
|
| 22 |
+
// a particular key is never again modified, but that has not been a
|
| 23 |
+
// stated assumption before. Just in case there is any existing code that
|
| 24 |
+
// does modify the key between operations, we save the original values
|
| 25 |
+
// alongside the cached BoringCrypto key and check that the real key
|
| 26 |
+
// still matches before using the cached key. The theory is that the real
|
| 27 |
+
// operations are significantly more expensive than the comparison.
|
| 28 |
+
|
| 29 |
+
var pubCache bcache.Cache[PublicKey, boringPub]
|
| 30 |
+
var privCache bcache.Cache[PrivateKey, boringPriv]
|
| 31 |
+
|
| 32 |
+
func init() {
|
| 33 |
+
pubCache.Register()
|
| 34 |
+
privCache.Register()
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
type boringPub struct {
|
| 38 |
+
key *boring.PublicKeyECDSA
|
| 39 |
+
orig PublicKey
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
func boringPublicKey(pub *PublicKey) (*boring.PublicKeyECDSA, error) {
|
| 43 |
+
b := pubCache.Get(pub)
|
| 44 |
+
if b != nil && publicKeyEqual(&b.orig, pub) {
|
| 45 |
+
return b.key, nil
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
b = new(boringPub)
|
| 49 |
+
b.orig = copyPublicKey(pub)
|
| 50 |
+
key, err := boring.NewPublicKeyECDSA(b.orig.Curve.Params().Name, bbig.Enc(b.orig.X), bbig.Enc(b.orig.Y))
|
| 51 |
+
if err != nil {
|
| 52 |
+
return nil, err
|
| 53 |
+
}
|
| 54 |
+
b.key = key
|
| 55 |
+
pubCache.Put(pub, b)
|
| 56 |
+
return key, nil
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
type boringPriv struct {
|
| 60 |
+
key *boring.PrivateKeyECDSA
|
| 61 |
+
orig PrivateKey
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
func boringPrivateKey(priv *PrivateKey) (*boring.PrivateKeyECDSA, error) {
|
| 65 |
+
b := privCache.Get(priv)
|
| 66 |
+
if b != nil && privateKeyEqual(&b.orig, priv) {
|
| 67 |
+
return b.key, nil
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
b = new(boringPriv)
|
| 71 |
+
b.orig = copyPrivateKey(priv)
|
| 72 |
+
key, err := boring.NewPrivateKeyECDSA(b.orig.Curve.Params().Name, bbig.Enc(b.orig.X), bbig.Enc(b.orig.Y), bbig.Enc(b.orig.D))
|
| 73 |
+
if err != nil {
|
| 74 |
+
return nil, err
|
| 75 |
+
}
|
| 76 |
+
b.key = key
|
| 77 |
+
privCache.Put(priv, b)
|
| 78 |
+
return key, nil
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
func publicKeyEqual(k1, k2 *PublicKey) bool {
|
| 82 |
+
return k1.X != nil &&
|
| 83 |
+
k1.Curve.Params() == k2.Curve.Params() &&
|
| 84 |
+
k1.X.Cmp(k2.X) == 0 &&
|
| 85 |
+
k1.Y.Cmp(k2.Y) == 0
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
func privateKeyEqual(k1, k2 *PrivateKey) bool {
|
| 89 |
+
return publicKeyEqual(&k1.PublicKey, &k2.PublicKey) &&
|
| 90 |
+
k1.D.Cmp(k2.D) == 0
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
func copyPublicKey(k *PublicKey) PublicKey {
|
| 94 |
+
return PublicKey{
|
| 95 |
+
Curve: k.Curve,
|
| 96 |
+
X: new(big.Int).Set(k.X),
|
| 97 |
+
Y: new(big.Int).Set(k.Y),
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
func copyPrivateKey(k *PrivateKey) PrivateKey {
|
| 102 |
+
return PrivateKey{
|
| 103 |
+
PublicKey: copyPublicKey(&k.PublicKey),
|
| 104 |
+
D: new(big.Int).Set(k.D),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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2011 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// Package ecdsa implements the Elliptic Curve Digital Signature Algorithm, as
|
| 6 |
+
// defined in FIPS 186-4 and SEC 1, Version 2.0.
|
| 7 |
+
//
|
| 8 |
+
// Signatures generated by this package are not deterministic, but entropy is
|
| 9 |
+
// mixed with the private key and the message, achieving the same level of
|
| 10 |
+
// security in case of randomness source failure.
|
| 11 |
+
package ecdsa
|
| 12 |
+
|
| 13 |
+
// [FIPS 186-4] references ANSI X9.62-2005 for the bulk of the ECDSA algorithm.
|
| 14 |
+
// That standard is not freely available, which is a problem in an open source
|
| 15 |
+
// implementation, because not only the implementer, but also any maintainer,
|
| 16 |
+
// contributor, reviewer, auditor, and learner needs access to it. Instead, this
|
| 17 |
+
// package references and follows the equivalent [SEC 1, Version 2.0].
|
| 18 |
+
//
|
| 19 |
+
// [FIPS 186-4]: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
|
| 20 |
+
// [SEC 1, Version 2.0]: https://www.secg.org/sec1-v2.pdf
|
| 21 |
+
|
| 22 |
+
import (
|
| 23 |
+
"bytes"
|
| 24 |
+
"crypto"
|
| 25 |
+
"crypto/aes"
|
| 26 |
+
"crypto/cipher"
|
| 27 |
+
"crypto/ecdh"
|
| 28 |
+
"crypto/elliptic"
|
| 29 |
+
"crypto/internal/bigmod"
|
| 30 |
+
"crypto/internal/boring"
|
| 31 |
+
"crypto/internal/boring/bbig"
|
| 32 |
+
"crypto/internal/nistec"
|
| 33 |
+
"crypto/internal/randutil"
|
| 34 |
+
"crypto/sha512"
|
| 35 |
+
"crypto/subtle"
|
| 36 |
+
"errors"
|
| 37 |
+
"io"
|
| 38 |
+
"math/big"
|
| 39 |
+
"sync"
|
| 40 |
+
|
| 41 |
+
"golang.org/x/crypto/cryptobyte"
|
| 42 |
+
"golang.org/x/crypto/cryptobyte/asn1"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
// PublicKey represents an ECDSA public key.
|
| 46 |
+
type PublicKey struct {
|
| 47 |
+
elliptic.Curve
|
| 48 |
+
X, Y *big.Int
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// Any methods implemented on PublicKey might need to also be implemented on
|
| 52 |
+
// PrivateKey, as the latter embeds the former and will expose its methods.
|
| 53 |
+
|
| 54 |
+
// ECDH returns k as a [ecdh.PublicKey]. It returns an error if the key is
|
| 55 |
+
// invalid according to the definition of [ecdh.Curve.NewPublicKey], or if the
|
| 56 |
+
// Curve is not supported by crypto/ecdh.
|
| 57 |
+
func (k *PublicKey) ECDH() (*ecdh.PublicKey, error) {
|
| 58 |
+
c := curveToECDH(k.Curve)
|
| 59 |
+
if c == nil {
|
| 60 |
+
return nil, errors.New("ecdsa: unsupported curve by crypto/ecdh")
|
| 61 |
+
}
|
| 62 |
+
if !k.Curve.IsOnCurve(k.X, k.Y) {
|
| 63 |
+
return nil, errors.New("ecdsa: invalid public key")
|
| 64 |
+
}
|
| 65 |
+
return c.NewPublicKey(elliptic.Marshal(k.Curve, k.X, k.Y))
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// Equal reports whether pub and x have the same value.
|
| 69 |
+
//
|
| 70 |
+
// Two keys are only considered to have the same value if they have the same Curve value.
|
| 71 |
+
// Note that for example [elliptic.P256] and elliptic.P256().Params() are different
|
| 72 |
+
// values, as the latter is a generic not constant time implementation.
|
| 73 |
+
func (pub *PublicKey) Equal(x crypto.PublicKey) bool {
|
| 74 |
+
xx, ok := x.(*PublicKey)
|
| 75 |
+
if !ok {
|
| 76 |
+
return false
|
| 77 |
+
}
|
| 78 |
+
return bigIntEqual(pub.X, xx.X) && bigIntEqual(pub.Y, xx.Y) &&
|
| 79 |
+
// Standard library Curve implementations are singletons, so this check
|
| 80 |
+
// will work for those. Other Curves might be equivalent even if not
|
| 81 |
+
// singletons, but there is no definitive way to check for that, and
|
| 82 |
+
// better to err on the side of safety.
|
| 83 |
+
pub.Curve == xx.Curve
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// PrivateKey represents an ECDSA private key.
|
| 87 |
+
type PrivateKey struct {
|
| 88 |
+
PublicKey
|
| 89 |
+
D *big.Int
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// ECDH returns k as a [ecdh.PrivateKey]. It returns an error if the key is
|
| 93 |
+
// invalid according to the definition of [ecdh.Curve.NewPrivateKey], or if the
|
| 94 |
+
// Curve is not supported by [crypto/ecdh].
|
| 95 |
+
func (k *PrivateKey) ECDH() (*ecdh.PrivateKey, error) {
|
| 96 |
+
c := curveToECDH(k.Curve)
|
| 97 |
+
if c == nil {
|
| 98 |
+
return nil, errors.New("ecdsa: unsupported curve by crypto/ecdh")
|
| 99 |
+
}
|
| 100 |
+
size := (k.Curve.Params().N.BitLen() + 7) / 8
|
| 101 |
+
if k.D.BitLen() > size*8 {
|
| 102 |
+
return nil, errors.New("ecdsa: invalid private key")
|
| 103 |
+
}
|
| 104 |
+
return c.NewPrivateKey(k.D.FillBytes(make([]byte, size)))
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
func curveToECDH(c elliptic.Curve) ecdh.Curve {
|
| 108 |
+
switch c {
|
| 109 |
+
case elliptic.P256():
|
| 110 |
+
return ecdh.P256()
|
| 111 |
+
case elliptic.P384():
|
| 112 |
+
return ecdh.P384()
|
| 113 |
+
case elliptic.P521():
|
| 114 |
+
return ecdh.P521()
|
| 115 |
+
default:
|
| 116 |
+
return nil
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
// Public returns the public key corresponding to priv.
|
| 121 |
+
func (priv *PrivateKey) Public() crypto.PublicKey {
|
| 122 |
+
return &priv.PublicKey
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// Equal reports whether priv and x have the same value.
|
| 126 |
+
//
|
| 127 |
+
// See [PublicKey.Equal] for details on how Curve is compared.
|
| 128 |
+
func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool {
|
| 129 |
+
xx, ok := x.(*PrivateKey)
|
| 130 |
+
if !ok {
|
| 131 |
+
return false
|
| 132 |
+
}
|
| 133 |
+
return priv.PublicKey.Equal(&xx.PublicKey) && bigIntEqual(priv.D, xx.D)
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
// bigIntEqual reports whether a and b are equal leaking only their bit length
|
| 137 |
+
// through timing side-channels.
|
| 138 |
+
func bigIntEqual(a, b *big.Int) bool {
|
| 139 |
+
return subtle.ConstantTimeCompare(a.Bytes(), b.Bytes()) == 1
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// Sign signs digest with priv, reading randomness from rand. The opts argument
|
| 143 |
+
// is not currently used but, in keeping with the crypto.Signer interface,
|
| 144 |
+
// should be the hash function used to digest the message.
|
| 145 |
+
//
|
| 146 |
+
// This method implements crypto.Signer, which is an interface to support keys
|
| 147 |
+
// where the private part is kept in, for example, a hardware module. Common
|
| 148 |
+
// uses can use the [SignASN1] function in this package directly.
|
| 149 |
+
func (priv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
|
| 150 |
+
return SignASN1(rand, priv, digest)
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
// GenerateKey generates a new ECDSA private key for the specified curve.
|
| 154 |
+
//
|
| 155 |
+
// Most applications should use [crypto/rand.Reader] as rand. Note that the
|
| 156 |
+
// returned key does not depend deterministically on the bytes read from rand,
|
| 157 |
+
// and may change between calls and/or between versions.
|
| 158 |
+
func GenerateKey(c elliptic.Curve, rand io.Reader) (*PrivateKey, error) {
|
| 159 |
+
randutil.MaybeReadByte(rand)
|
| 160 |
+
|
| 161 |
+
if boring.Enabled && rand == boring.RandReader {
|
| 162 |
+
x, y, d, err := boring.GenerateKeyECDSA(c.Params().Name)
|
| 163 |
+
if err != nil {
|
| 164 |
+
return nil, err
|
| 165 |
+
}
|
| 166 |
+
return &PrivateKey{PublicKey: PublicKey{Curve: c, X: bbig.Dec(x), Y: bbig.Dec(y)}, D: bbig.Dec(d)}, nil
|
| 167 |
+
}
|
| 168 |
+
boring.UnreachableExceptTests()
|
| 169 |
+
|
| 170 |
+
switch c.Params() {
|
| 171 |
+
case elliptic.P224().Params():
|
| 172 |
+
return generateNISTEC(p224(), rand)
|
| 173 |
+
case elliptic.P256().Params():
|
| 174 |
+
return generateNISTEC(p256(), rand)
|
| 175 |
+
case elliptic.P384().Params():
|
| 176 |
+
return generateNISTEC(p384(), rand)
|
| 177 |
+
case elliptic.P521().Params():
|
| 178 |
+
return generateNISTEC(p521(), rand)
|
| 179 |
+
default:
|
| 180 |
+
return generateLegacy(c, rand)
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
func generateNISTEC[Point nistPoint[Point]](c *nistCurve[Point], rand io.Reader) (*PrivateKey, error) {
|
| 185 |
+
k, Q, err := randomPoint(c, rand)
|
| 186 |
+
if err != nil {
|
| 187 |
+
return nil, err
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
priv := new(PrivateKey)
|
| 191 |
+
priv.PublicKey.Curve = c.curve
|
| 192 |
+
priv.D = new(big.Int).SetBytes(k.Bytes(c.N))
|
| 193 |
+
priv.PublicKey.X, priv.PublicKey.Y, err = c.pointToAffine(Q)
|
| 194 |
+
if err != nil {
|
| 195 |
+
return nil, err
|
| 196 |
+
}
|
| 197 |
+
return priv, nil
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
// randomPoint returns a random scalar and the corresponding point using the
|
| 201 |
+
// procedure given in FIPS 186-4, Appendix B.5.2 (rejection sampling).
|
| 202 |
+
func randomPoint[Point nistPoint[Point]](c *nistCurve[Point], rand io.Reader) (k *bigmod.Nat, p Point, err error) {
|
| 203 |
+
k = bigmod.NewNat()
|
| 204 |
+
for {
|
| 205 |
+
b := make([]byte, c.N.Size())
|
| 206 |
+
if _, err = io.ReadFull(rand, b); err != nil {
|
| 207 |
+
return
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
// Mask off any excess bits to increase the chance of hitting a value in
|
| 211 |
+
// (0, N). These are the most dangerous lines in the package and maybe in
|
| 212 |
+
// the library: a single bit of bias in the selection of nonces would likely
|
| 213 |
+
// lead to key recovery, but no tests would fail. Look but DO NOT TOUCH.
|
| 214 |
+
if excess := len(b)*8 - c.N.BitLen(); excess > 0 {
|
| 215 |
+
// Just to be safe, assert that this only happens for the one curve that
|
| 216 |
+
// doesn't have a round number of bits.
|
| 217 |
+
if excess != 0 && c.curve.Params().Name != "P-521" {
|
| 218 |
+
panic("ecdsa: internal error: unexpectedly masking off bits")
|
| 219 |
+
}
|
| 220 |
+
b[0] >>= excess
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
// FIPS 186-4 makes us check k <= N - 2 and then add one.
|
| 224 |
+
// Checking 0 < k <= N - 1 is strictly equivalent.
|
| 225 |
+
// None of this matters anyway because the chance of selecting
|
| 226 |
+
// zero is cryptographically negligible.
|
| 227 |
+
if _, err = k.SetBytes(b, c.N); err == nil && k.IsZero() == 0 {
|
| 228 |
+
break
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
if testingOnlyRejectionSamplingLooped != nil {
|
| 232 |
+
testingOnlyRejectionSamplingLooped()
|
| 233 |
+
}
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
p, err = c.newPoint().ScalarBaseMult(k.Bytes(c.N))
|
| 237 |
+
return
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
// testingOnlyRejectionSamplingLooped is called when rejection sampling in
|
| 241 |
+
// randomPoint rejects a candidate for being higher than the modulus.
|
| 242 |
+
var testingOnlyRejectionSamplingLooped func()
|
| 243 |
+
|
| 244 |
+
// errNoAsm is returned by signAsm and verifyAsm when the assembly
|
| 245 |
+
// implementation is not available.
|
| 246 |
+
var errNoAsm = errors.New("no assembly implementation available")
|
| 247 |
+
|
| 248 |
+
// SignASN1 signs a hash (which should be the result of hashing a larger message)
|
| 249 |
+
// using the private key, priv. If the hash is longer than the bit-length of the
|
| 250 |
+
// private key's curve order, the hash will be truncated to that length. It
|
| 251 |
+
// returns the ASN.1 encoded signature.
|
| 252 |
+
//
|
| 253 |
+
// The signature is randomized. Most applications should use [crypto/rand.Reader]
|
| 254 |
+
// as rand. Note that the returned signature does not depend deterministically on
|
| 255 |
+
// the bytes read from rand, and may change between calls and/or between versions.
|
| 256 |
+
func SignASN1(rand io.Reader, priv *PrivateKey, hash []byte) ([]byte, error) {
|
| 257 |
+
randutil.MaybeReadByte(rand)
|
| 258 |
+
|
| 259 |
+
if boring.Enabled && rand == boring.RandReader {
|
| 260 |
+
b, err := boringPrivateKey(priv)
|
| 261 |
+
if err != nil {
|
| 262 |
+
return nil, err
|
| 263 |
+
}
|
| 264 |
+
return boring.SignMarshalECDSA(b, hash)
|
| 265 |
+
}
|
| 266 |
+
boring.UnreachableExceptTests()
|
| 267 |
+
|
| 268 |
+
csprng, err := mixedCSPRNG(rand, priv, hash)
|
| 269 |
+
if err != nil {
|
| 270 |
+
return nil, err
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
if sig, err := signAsm(priv, csprng, hash); err != errNoAsm {
|
| 274 |
+
return sig, err
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
switch priv.Curve.Params() {
|
| 278 |
+
case elliptic.P224().Params():
|
| 279 |
+
return signNISTEC(p224(), priv, csprng, hash)
|
| 280 |
+
case elliptic.P256().Params():
|
| 281 |
+
return signNISTEC(p256(), priv, csprng, hash)
|
| 282 |
+
case elliptic.P384().Params():
|
| 283 |
+
return signNISTEC(p384(), priv, csprng, hash)
|
| 284 |
+
case elliptic.P521().Params():
|
| 285 |
+
return signNISTEC(p521(), priv, csprng, hash)
|
| 286 |
+
default:
|
| 287 |
+
return signLegacy(priv, csprng, hash)
|
| 288 |
+
}
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
func signNISTEC[Point nistPoint[Point]](c *nistCurve[Point], priv *PrivateKey, csprng io.Reader, hash []byte) (sig []byte, err error) {
|
| 292 |
+
// SEC 1, Version 2.0, Section 4.1.3
|
| 293 |
+
|
| 294 |
+
k, R, err := randomPoint(c, csprng)
|
| 295 |
+
if err != nil {
|
| 296 |
+
return nil, err
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
// kInv = k⁻¹
|
| 300 |
+
kInv := bigmod.NewNat()
|
| 301 |
+
inverse(c, kInv, k)
|
| 302 |
+
|
| 303 |
+
Rx, err := R.BytesX()
|
| 304 |
+
if err != nil {
|
| 305 |
+
return nil, err
|
| 306 |
+
}
|
| 307 |
+
r, err := bigmod.NewNat().SetOverflowingBytes(Rx, c.N)
|
| 308 |
+
if err != nil {
|
| 309 |
+
return nil, err
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
// The spec wants us to retry here, but the chance of hitting this condition
|
| 313 |
+
// on a large prime-order group like the NIST curves we support is
|
| 314 |
+
// cryptographically negligible. If we hit it, something is awfully wrong.
|
| 315 |
+
if r.IsZero() == 1 {
|
| 316 |
+
return nil, errors.New("ecdsa: internal error: r is zero")
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
e := bigmod.NewNat()
|
| 320 |
+
hashToNat(c, e, hash)
|
| 321 |
+
|
| 322 |
+
s, err := bigmod.NewNat().SetBytes(priv.D.Bytes(), c.N)
|
| 323 |
+
if err != nil {
|
| 324 |
+
return nil, err
|
| 325 |
+
}
|
| 326 |
+
s.Mul(r, c.N)
|
| 327 |
+
s.Add(e, c.N)
|
| 328 |
+
s.Mul(kInv, c.N)
|
| 329 |
+
|
| 330 |
+
// Again, the chance of this happening is cryptographically negligible.
|
| 331 |
+
if s.IsZero() == 1 {
|
| 332 |
+
return nil, errors.New("ecdsa: internal error: s is zero")
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
return encodeSignature(r.Bytes(c.N), s.Bytes(c.N))
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
func encodeSignature(r, s []byte) ([]byte, error) {
|
| 339 |
+
var b cryptobyte.Builder
|
| 340 |
+
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
|
| 341 |
+
addASN1IntBytes(b, r)
|
| 342 |
+
addASN1IntBytes(b, s)
|
| 343 |
+
})
|
| 344 |
+
return b.Bytes()
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
// addASN1IntBytes encodes in ASN.1 a positive integer represented as
|
| 348 |
+
// a big-endian byte slice with zero or more leading zeroes.
|
| 349 |
+
func addASN1IntBytes(b *cryptobyte.Builder, bytes []byte) {
|
| 350 |
+
for len(bytes) > 0 && bytes[0] == 0 {
|
| 351 |
+
bytes = bytes[1:]
|
| 352 |
+
}
|
| 353 |
+
if len(bytes) == 0 {
|
| 354 |
+
b.SetError(errors.New("invalid integer"))
|
| 355 |
+
return
|
| 356 |
+
}
|
| 357 |
+
b.AddASN1(asn1.INTEGER, func(c *cryptobyte.Builder) {
|
| 358 |
+
if bytes[0]&0x80 != 0 {
|
| 359 |
+
c.AddUint8(0)
|
| 360 |
+
}
|
| 361 |
+
c.AddBytes(bytes)
|
| 362 |
+
})
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
// inverse sets kInv to the inverse of k modulo the order of the curve.
|
| 366 |
+
func inverse[Point nistPoint[Point]](c *nistCurve[Point], kInv, k *bigmod.Nat) {
|
| 367 |
+
if c.curve.Params().Name == "P-256" {
|
| 368 |
+
kBytes, err := nistec.P256OrdInverse(k.Bytes(c.N))
|
| 369 |
+
// Some platforms don't implement P256OrdInverse, and always return an error.
|
| 370 |
+
if err == nil {
|
| 371 |
+
_, err := kInv.SetBytes(kBytes, c.N)
|
| 372 |
+
if err != nil {
|
| 373 |
+
panic("ecdsa: internal error: P256OrdInverse produced an invalid value")
|
| 374 |
+
}
|
| 375 |
+
return
|
| 376 |
+
}
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
// Calculate the inverse of s in GF(N) using Fermat's method
|
| 380 |
+
// (exponentiation modulo P - 2, per Euler's theorem)
|
| 381 |
+
kInv.Exp(k, c.nMinus2, c.N)
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
// hashToNat sets e to the left-most bits of hash, according to
|
| 385 |
+
// SEC 1, Section 4.1.3, point 5 and Section 4.1.4, point 3.
|
| 386 |
+
func hashToNat[Point nistPoint[Point]](c *nistCurve[Point], e *bigmod.Nat, hash []byte) {
|
| 387 |
+
// ECDSA asks us to take the left-most log2(N) bits of hash, and use them as
|
| 388 |
+
// an integer modulo N. This is the absolute worst of all worlds: we still
|
| 389 |
+
// have to reduce, because the result might still overflow N, but to take
|
| 390 |
+
// the left-most bits for P-521 we have to do a right shift.
|
| 391 |
+
if size := c.N.Size(); len(hash) >= size {
|
| 392 |
+
hash = hash[:size]
|
| 393 |
+
if excess := len(hash)*8 - c.N.BitLen(); excess > 0 {
|
| 394 |
+
hash = bytes.Clone(hash)
|
| 395 |
+
for i := len(hash) - 1; i >= 0; i-- {
|
| 396 |
+
hash[i] >>= excess
|
| 397 |
+
if i > 0 {
|
| 398 |
+
hash[i] |= hash[i-1] << (8 - excess)
|
| 399 |
+
}
|
| 400 |
+
}
|
| 401 |
+
}
|
| 402 |
+
}
|
| 403 |
+
_, err := e.SetOverflowingBytes(hash, c.N)
|
| 404 |
+
if err != nil {
|
| 405 |
+
panic("ecdsa: internal error: truncated hash is too long")
|
| 406 |
+
}
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
// mixedCSPRNG returns a CSPRNG that mixes entropy from rand with the message
|
| 410 |
+
// and the private key, to protect the key in case rand fails. This is
|
| 411 |
+
// equivalent in security to RFC 6979 deterministic nonce generation, but still
|
| 412 |
+
// produces randomized signatures.
|
| 413 |
+
func mixedCSPRNG(rand io.Reader, priv *PrivateKey, hash []byte) (io.Reader, error) {
|
| 414 |
+
// This implementation derives the nonce from an AES-CTR CSPRNG keyed by:
|
| 415 |
+
//
|
| 416 |
+
// SHA2-512(priv.D || entropy || hash)[:32]
|
| 417 |
+
//
|
| 418 |
+
// The CSPRNG key is indifferentiable from a random oracle as shown in
|
| 419 |
+
// [Coron], the AES-CTR stream is indifferentiable from a random oracle
|
| 420 |
+
// under standard cryptographic assumptions (see [Larsson] for examples).
|
| 421 |
+
//
|
| 422 |
+
// [Coron]: https://cs.nyu.edu/~dodis/ps/merkle.pdf
|
| 423 |
+
// [Larsson]: https://web.archive.org/web/20040719170906/https://www.nada.kth.se/kurser/kth/2D1441/semteo03/lecturenotes/assump.pdf
|
| 424 |
+
|
| 425 |
+
// Get 256 bits of entropy from rand.
|
| 426 |
+
entropy := make([]byte, 32)
|
| 427 |
+
if _, err := io.ReadFull(rand, entropy); err != nil {
|
| 428 |
+
return nil, err
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
// Initialize an SHA-512 hash context; digest...
|
| 432 |
+
md := sha512.New()
|
| 433 |
+
md.Write(priv.D.Bytes()) // the private key,
|
| 434 |
+
md.Write(entropy) // the entropy,
|
| 435 |
+
md.Write(hash) // and the input hash;
|
| 436 |
+
key := md.Sum(nil)[:32] // and compute ChopMD-256(SHA-512),
|
| 437 |
+
// which is an indifferentiable MAC.
|
| 438 |
+
|
| 439 |
+
// Create an AES-CTR instance to use as a CSPRNG.
|
| 440 |
+
block, err := aes.NewCipher(key)
|
| 441 |
+
if err != nil {
|
| 442 |
+
return nil, err
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
// Create a CSPRNG that xors a stream of zeros with
|
| 446 |
+
// the output of the AES-CTR instance.
|
| 447 |
+
const aesIV = "IV for ECDSA CTR"
|
| 448 |
+
return &cipher.StreamReader{
|
| 449 |
+
R: zeroReader,
|
| 450 |
+
S: cipher.NewCTR(block, []byte(aesIV)),
|
| 451 |
+
}, nil
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
type zr struct{}
|
| 455 |
+
|
| 456 |
+
var zeroReader = zr{}
|
| 457 |
+
|
| 458 |
+
// Read replaces the contents of dst with zeros. It is safe for concurrent use.
|
| 459 |
+
func (zr) Read(dst []byte) (n int, err error) {
|
| 460 |
+
for i := range dst {
|
| 461 |
+
dst[i] = 0
|
| 462 |
+
}
|
| 463 |
+
return len(dst), nil
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
// VerifyASN1 verifies the ASN.1 encoded signature, sig, of hash using the
|
| 467 |
+
// public key, pub. Its return value records whether the signature is valid.
|
| 468 |
+
func VerifyASN1(pub *PublicKey, hash, sig []byte) bool {
|
| 469 |
+
if boring.Enabled {
|
| 470 |
+
key, err := boringPublicKey(pub)
|
| 471 |
+
if err != nil {
|
| 472 |
+
return false
|
| 473 |
+
}
|
| 474 |
+
return boring.VerifyECDSA(key, hash, sig)
|
| 475 |
+
}
|
| 476 |
+
boring.UnreachableExceptTests()
|
| 477 |
+
|
| 478 |
+
if err := verifyAsm(pub, hash, sig); err != errNoAsm {
|
| 479 |
+
return err == nil
|
| 480 |
+
}
|
| 481 |
+
|
| 482 |
+
switch pub.Curve.Params() {
|
| 483 |
+
case elliptic.P224().Params():
|
| 484 |
+
return verifyNISTEC(p224(), pub, hash, sig)
|
| 485 |
+
case elliptic.P256().Params():
|
| 486 |
+
return verifyNISTEC(p256(), pub, hash, sig)
|
| 487 |
+
case elliptic.P384().Params():
|
| 488 |
+
return verifyNISTEC(p384(), pub, hash, sig)
|
| 489 |
+
case elliptic.P521().Params():
|
| 490 |
+
return verifyNISTEC(p521(), pub, hash, sig)
|
| 491 |
+
default:
|
| 492 |
+
return verifyLegacy(pub, hash, sig)
|
| 493 |
+
}
|
| 494 |
+
}
|
| 495 |
+
|
| 496 |
+
func verifyNISTEC[Point nistPoint[Point]](c *nistCurve[Point], pub *PublicKey, hash, sig []byte) bool {
|
| 497 |
+
rBytes, sBytes, err := parseSignature(sig)
|
| 498 |
+
if err != nil {
|
| 499 |
+
return false
|
| 500 |
+
}
|
| 501 |
+
|
| 502 |
+
Q, err := c.pointFromAffine(pub.X, pub.Y)
|
| 503 |
+
if err != nil {
|
| 504 |
+
return false
|
| 505 |
+
}
|
| 506 |
+
|
| 507 |
+
// SEC 1, Version 2.0, Section 4.1.4
|
| 508 |
+
|
| 509 |
+
r, err := bigmod.NewNat().SetBytes(rBytes, c.N)
|
| 510 |
+
if err != nil || r.IsZero() == 1 {
|
| 511 |
+
return false
|
| 512 |
+
}
|
| 513 |
+
s, err := bigmod.NewNat().SetBytes(sBytes, c.N)
|
| 514 |
+
if err != nil || s.IsZero() == 1 {
|
| 515 |
+
return false
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
e := bigmod.NewNat()
|
| 519 |
+
hashToNat(c, e, hash)
|
| 520 |
+
|
| 521 |
+
// w = s⁻¹
|
| 522 |
+
w := bigmod.NewNat()
|
| 523 |
+
inverse(c, w, s)
|
| 524 |
+
|
| 525 |
+
// p₁ = [e * s⁻¹]G
|
| 526 |
+
p1, err := c.newPoint().ScalarBaseMult(e.Mul(w, c.N).Bytes(c.N))
|
| 527 |
+
if err != nil {
|
| 528 |
+
return false
|
| 529 |
+
}
|
| 530 |
+
// p₂ = [r * s⁻¹]Q
|
| 531 |
+
p2, err := Q.ScalarMult(Q, w.Mul(r, c.N).Bytes(c.N))
|
| 532 |
+
if err != nil {
|
| 533 |
+
return false
|
| 534 |
+
}
|
| 535 |
+
// BytesX returns an error for the point at infinity.
|
| 536 |
+
Rx, err := p1.Add(p1, p2).BytesX()
|
| 537 |
+
if err != nil {
|
| 538 |
+
return false
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
v, err := bigmod.NewNat().SetOverflowingBytes(Rx, c.N)
|
| 542 |
+
if err != nil {
|
| 543 |
+
return false
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
return v.Equal(r) == 1
|
| 547 |
+
}
|
| 548 |
+
|
| 549 |
+
func parseSignature(sig []byte) (r, s []byte, err error) {
|
| 550 |
+
var inner cryptobyte.String
|
| 551 |
+
input := cryptobyte.String(sig)
|
| 552 |
+
if !input.ReadASN1(&inner, asn1.SEQUENCE) ||
|
| 553 |
+
!input.Empty() ||
|
| 554 |
+
!inner.ReadASN1Integer(&r) ||
|
| 555 |
+
!inner.ReadASN1Integer(&s) ||
|
| 556 |
+
!inner.Empty() {
|
| 557 |
+
return nil, nil, errors.New("invalid ASN.1")
|
| 558 |
+
}
|
| 559 |
+
return r, s, nil
|
| 560 |
+
}
|
| 561 |
+
|
| 562 |
+
type nistCurve[Point nistPoint[Point]] struct {
|
| 563 |
+
newPoint func() Point
|
| 564 |
+
curve elliptic.Curve
|
| 565 |
+
N *bigmod.Modulus
|
| 566 |
+
nMinus2 []byte
|
| 567 |
+
}
|
| 568 |
+
|
| 569 |
+
// nistPoint is a generic constraint for the nistec Point types.
|
| 570 |
+
type nistPoint[T any] interface {
|
| 571 |
+
Bytes() []byte
|
| 572 |
+
BytesX() ([]byte, error)
|
| 573 |
+
SetBytes([]byte) (T, error)
|
| 574 |
+
Add(T, T) T
|
| 575 |
+
ScalarMult(T, []byte) (T, error)
|
| 576 |
+
ScalarBaseMult([]byte) (T, error)
|
| 577 |
+
}
|
| 578 |
+
|
| 579 |
+
// pointFromAffine is used to convert the PublicKey to a nistec Point.
|
| 580 |
+
func (curve *nistCurve[Point]) pointFromAffine(x, y *big.Int) (p Point, err error) {
|
| 581 |
+
bitSize := curve.curve.Params().BitSize
|
| 582 |
+
// Reject values that would not get correctly encoded.
|
| 583 |
+
if x.Sign() < 0 || y.Sign() < 0 {
|
| 584 |
+
return p, errors.New("negative coordinate")
|
| 585 |
+
}
|
| 586 |
+
if x.BitLen() > bitSize || y.BitLen() > bitSize {
|
| 587 |
+
return p, errors.New("overflowing coordinate")
|
| 588 |
+
}
|
| 589 |
+
// Encode the coordinates and let SetBytes reject invalid points.
|
| 590 |
+
byteLen := (bitSize + 7) / 8
|
| 591 |
+
buf := make([]byte, 1+2*byteLen)
|
| 592 |
+
buf[0] = 4 // uncompressed point
|
| 593 |
+
x.FillBytes(buf[1 : 1+byteLen])
|
| 594 |
+
y.FillBytes(buf[1+byteLen : 1+2*byteLen])
|
| 595 |
+
return curve.newPoint().SetBytes(buf)
|
| 596 |
+
}
|
| 597 |
+
|
| 598 |
+
// pointToAffine is used to convert a nistec Point to a PublicKey.
|
| 599 |
+
func (curve *nistCurve[Point]) pointToAffine(p Point) (x, y *big.Int, err error) {
|
| 600 |
+
out := p.Bytes()
|
| 601 |
+
if len(out) == 1 && out[0] == 0 {
|
| 602 |
+
// This is the encoding of the point at infinity.
|
| 603 |
+
return nil, nil, errors.New("ecdsa: public key point is the infinity")
|
| 604 |
+
}
|
| 605 |
+
byteLen := (curve.curve.Params().BitSize + 7) / 8
|
| 606 |
+
x = new(big.Int).SetBytes(out[1 : 1+byteLen])
|
| 607 |
+
y = new(big.Int).SetBytes(out[1+byteLen:])
|
| 608 |
+
return x, y, nil
|
| 609 |
+
}
|
| 610 |
+
|
| 611 |
+
var p224Once sync.Once
|
| 612 |
+
var _p224 *nistCurve[*nistec.P224Point]
|
| 613 |
+
|
| 614 |
+
func p224() *nistCurve[*nistec.P224Point] {
|
| 615 |
+
p224Once.Do(func() {
|
| 616 |
+
_p224 = &nistCurve[*nistec.P224Point]{
|
| 617 |
+
newPoint: func() *nistec.P224Point { return nistec.NewP224Point() },
|
| 618 |
+
}
|
| 619 |
+
precomputeParams(_p224, elliptic.P224())
|
| 620 |
+
})
|
| 621 |
+
return _p224
|
| 622 |
+
}
|
| 623 |
+
|
| 624 |
+
var p256Once sync.Once
|
| 625 |
+
var _p256 *nistCurve[*nistec.P256Point]
|
| 626 |
+
|
| 627 |
+
func p256() *nistCurve[*nistec.P256Point] {
|
| 628 |
+
p256Once.Do(func() {
|
| 629 |
+
_p256 = &nistCurve[*nistec.P256Point]{
|
| 630 |
+
newPoint: func() *nistec.P256Point { return nistec.NewP256Point() },
|
| 631 |
+
}
|
| 632 |
+
precomputeParams(_p256, elliptic.P256())
|
| 633 |
+
})
|
| 634 |
+
return _p256
|
| 635 |
+
}
|
| 636 |
+
|
| 637 |
+
var p384Once sync.Once
|
| 638 |
+
var _p384 *nistCurve[*nistec.P384Point]
|
| 639 |
+
|
| 640 |
+
func p384() *nistCurve[*nistec.P384Point] {
|
| 641 |
+
p384Once.Do(func() {
|
| 642 |
+
_p384 = &nistCurve[*nistec.P384Point]{
|
| 643 |
+
newPoint: func() *nistec.P384Point { return nistec.NewP384Point() },
|
| 644 |
+
}
|
| 645 |
+
precomputeParams(_p384, elliptic.P384())
|
| 646 |
+
})
|
| 647 |
+
return _p384
|
| 648 |
+
}
|
| 649 |
+
|
| 650 |
+
var p521Once sync.Once
|
| 651 |
+
var _p521 *nistCurve[*nistec.P521Point]
|
| 652 |
+
|
| 653 |
+
func p521() *nistCurve[*nistec.P521Point] {
|
| 654 |
+
p521Once.Do(func() {
|
| 655 |
+
_p521 = &nistCurve[*nistec.P521Point]{
|
| 656 |
+
newPoint: func() *nistec.P521Point { return nistec.NewP521Point() },
|
| 657 |
+
}
|
| 658 |
+
precomputeParams(_p521, elliptic.P521())
|
| 659 |
+
})
|
| 660 |
+
return _p521
|
| 661 |
+
}
|
| 662 |
+
|
| 663 |
+
func precomputeParams[Point nistPoint[Point]](c *nistCurve[Point], curve elliptic.Curve) {
|
| 664 |
+
params := curve.Params()
|
| 665 |
+
c.curve = curve
|
| 666 |
+
var err error
|
| 667 |
+
c.N, err = bigmod.NewModulusFromBig(params.N)
|
| 668 |
+
if err != nil {
|
| 669 |
+
panic(err)
|
| 670 |
+
}
|
| 671 |
+
c.nMinus2 = new(big.Int).Sub(params.N, big.NewInt(2)).Bytes()
|
| 672 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/ecdsa/ecdsa_legacy.go
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
package ecdsa
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/elliptic"
|
| 9 |
+
"errors"
|
| 10 |
+
"io"
|
| 11 |
+
"math/big"
|
| 12 |
+
|
| 13 |
+
"golang.org/x/crypto/cryptobyte"
|
| 14 |
+
"golang.org/x/crypto/cryptobyte/asn1"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
// This file contains a math/big implementation of ECDSA that is only used for
|
| 18 |
+
// deprecated custom curves.
|
| 19 |
+
|
| 20 |
+
func generateLegacy(c elliptic.Curve, rand io.Reader) (*PrivateKey, error) {
|
| 21 |
+
k, err := randFieldElement(c, rand)
|
| 22 |
+
if err != nil {
|
| 23 |
+
return nil, err
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
priv := new(PrivateKey)
|
| 27 |
+
priv.PublicKey.Curve = c
|
| 28 |
+
priv.D = k
|
| 29 |
+
priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes())
|
| 30 |
+
return priv, nil
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
// hashToInt converts a hash value to an integer. Per FIPS 186-4, Section 6.4,
|
| 34 |
+
// we use the left-most bits of the hash to match the bit-length of the order of
|
| 35 |
+
// the curve. This also performs Step 5 of SEC 1, Version 2.0, Section 4.1.3.
|
| 36 |
+
func hashToInt(hash []byte, c elliptic.Curve) *big.Int {
|
| 37 |
+
orderBits := c.Params().N.BitLen()
|
| 38 |
+
orderBytes := (orderBits + 7) / 8
|
| 39 |
+
if len(hash) > orderBytes {
|
| 40 |
+
hash = hash[:orderBytes]
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
ret := new(big.Int).SetBytes(hash)
|
| 44 |
+
excess := len(hash)*8 - orderBits
|
| 45 |
+
if excess > 0 {
|
| 46 |
+
ret.Rsh(ret, uint(excess))
|
| 47 |
+
}
|
| 48 |
+
return ret
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
var errZeroParam = errors.New("zero parameter")
|
| 52 |
+
|
| 53 |
+
// Sign signs a hash (which should be the result of hashing a larger message)
|
| 54 |
+
// using the private key, priv. If the hash is longer than the bit-length of the
|
| 55 |
+
// private key's curve order, the hash will be truncated to that length. It
|
| 56 |
+
// returns the signature as a pair of integers. Most applications should use
|
| 57 |
+
// [SignASN1] instead of dealing directly with r, s.
|
| 58 |
+
func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) {
|
| 59 |
+
sig, err := SignASN1(rand, priv, hash)
|
| 60 |
+
if err != nil {
|
| 61 |
+
return nil, nil, err
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
r, s = new(big.Int), new(big.Int)
|
| 65 |
+
var inner cryptobyte.String
|
| 66 |
+
input := cryptobyte.String(sig)
|
| 67 |
+
if !input.ReadASN1(&inner, asn1.SEQUENCE) ||
|
| 68 |
+
!input.Empty() ||
|
| 69 |
+
!inner.ReadASN1Integer(r) ||
|
| 70 |
+
!inner.ReadASN1Integer(s) ||
|
| 71 |
+
!inner.Empty() {
|
| 72 |
+
return nil, nil, errors.New("invalid ASN.1 from SignASN1")
|
| 73 |
+
}
|
| 74 |
+
return r, s, nil
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
func signLegacy(priv *PrivateKey, csprng io.Reader, hash []byte) (sig []byte, err error) {
|
| 78 |
+
c := priv.Curve
|
| 79 |
+
|
| 80 |
+
// SEC 1, Version 2.0, Section 4.1.3
|
| 81 |
+
N := c.Params().N
|
| 82 |
+
if N.Sign() == 0 {
|
| 83 |
+
return nil, errZeroParam
|
| 84 |
+
}
|
| 85 |
+
var k, kInv, r, s *big.Int
|
| 86 |
+
for {
|
| 87 |
+
for {
|
| 88 |
+
k, err = randFieldElement(c, csprng)
|
| 89 |
+
if err != nil {
|
| 90 |
+
return nil, err
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
kInv = new(big.Int).ModInverse(k, N)
|
| 94 |
+
|
| 95 |
+
r, _ = c.ScalarBaseMult(k.Bytes())
|
| 96 |
+
r.Mod(r, N)
|
| 97 |
+
if r.Sign() != 0 {
|
| 98 |
+
break
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
e := hashToInt(hash, c)
|
| 103 |
+
s = new(big.Int).Mul(priv.D, r)
|
| 104 |
+
s.Add(s, e)
|
| 105 |
+
s.Mul(s, kInv)
|
| 106 |
+
s.Mod(s, N) // N != 0
|
| 107 |
+
if s.Sign() != 0 {
|
| 108 |
+
break
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
return encodeSignature(r.Bytes(), s.Bytes())
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
// Verify verifies the signature in r, s of hash using the public key, pub. Its
|
| 116 |
+
// return value records whether the signature is valid. Most applications should
|
| 117 |
+
// use VerifyASN1 instead of dealing directly with r, s.
|
| 118 |
+
func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
|
| 119 |
+
if r.Sign() <= 0 || s.Sign() <= 0 {
|
| 120 |
+
return false
|
| 121 |
+
}
|
| 122 |
+
sig, err := encodeSignature(r.Bytes(), s.Bytes())
|
| 123 |
+
if err != nil {
|
| 124 |
+
return false
|
| 125 |
+
}
|
| 126 |
+
return VerifyASN1(pub, hash, sig)
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
func verifyLegacy(pub *PublicKey, hash []byte, sig []byte) bool {
|
| 130 |
+
rBytes, sBytes, err := parseSignature(sig)
|
| 131 |
+
if err != nil {
|
| 132 |
+
return false
|
| 133 |
+
}
|
| 134 |
+
r, s := new(big.Int).SetBytes(rBytes), new(big.Int).SetBytes(sBytes)
|
| 135 |
+
|
| 136 |
+
c := pub.Curve
|
| 137 |
+
N := c.Params().N
|
| 138 |
+
|
| 139 |
+
if r.Sign() <= 0 || s.Sign() <= 0 {
|
| 140 |
+
return false
|
| 141 |
+
}
|
| 142 |
+
if r.Cmp(N) >= 0 || s.Cmp(N) >= 0 {
|
| 143 |
+
return false
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
// SEC 1, Version 2.0, Section 4.1.4
|
| 147 |
+
e := hashToInt(hash, c)
|
| 148 |
+
w := new(big.Int).ModInverse(s, N)
|
| 149 |
+
|
| 150 |
+
u1 := e.Mul(e, w)
|
| 151 |
+
u1.Mod(u1, N)
|
| 152 |
+
u2 := w.Mul(r, w)
|
| 153 |
+
u2.Mod(u2, N)
|
| 154 |
+
|
| 155 |
+
x1, y1 := c.ScalarBaseMult(u1.Bytes())
|
| 156 |
+
x2, y2 := c.ScalarMult(pub.X, pub.Y, u2.Bytes())
|
| 157 |
+
x, y := c.Add(x1, y1, x2, y2)
|
| 158 |
+
|
| 159 |
+
if x.Sign() == 0 && y.Sign() == 0 {
|
| 160 |
+
return false
|
| 161 |
+
}
|
| 162 |
+
x.Mod(x, N)
|
| 163 |
+
return x.Cmp(r) == 0
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
var one = new(big.Int).SetInt64(1)
|
| 167 |
+
|
| 168 |
+
// randFieldElement returns a random element of the order of the given
|
| 169 |
+
// curve using the procedure given in FIPS 186-4, Appendix B.5.2.
|
| 170 |
+
func randFieldElement(c elliptic.Curve, rand io.Reader) (k *big.Int, err error) {
|
| 171 |
+
// See randomPoint for notes on the algorithm. This has to match, or s390x
|
| 172 |
+
// signatures will come out different from other architectures, which will
|
| 173 |
+
// break TLS recorded tests.
|
| 174 |
+
for {
|
| 175 |
+
N := c.Params().N
|
| 176 |
+
b := make([]byte, (N.BitLen()+7)/8)
|
| 177 |
+
if _, err = io.ReadFull(rand, b); err != nil {
|
| 178 |
+
return
|
| 179 |
+
}
|
| 180 |
+
if excess := len(b)*8 - N.BitLen(); excess > 0 {
|
| 181 |
+
b[0] >>= excess
|
| 182 |
+
}
|
| 183 |
+
k = new(big.Int).SetBytes(b)
|
| 184 |
+
if k.Sign() != 0 && k.Cmp(N) < 0 {
|
| 185 |
+
return
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
}
|
platform/dbops/binaries/go/go/src/crypto/ecdsa/ecdsa_noasm.go
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2020 The Go Authors. All rights reserved.
|
| 2 |
+
// Use of this source code is governed by a BSD-style
|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
//go:build !s390x
|
| 6 |
+
|
| 7 |
+
package ecdsa
|
| 8 |
+
|
| 9 |
+
import "io"
|
| 10 |
+
|
| 11 |
+
func verifyAsm(pub *PublicKey, hash []byte, sig []byte) error {
|
| 12 |
+
return errNoAsm
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
func signAsm(priv *PrivateKey, csprng io.Reader, hash []byte) (sig []byte, err error) {
|
| 16 |
+
return nil, errNoAsm
|
| 17 |
+
}
|