/usr/share/gocode/src/github.com/jacobsa/crypto/siv/decrypt_test.go is in golang-github-jacobsa-crypto-dev 0.0~git20161111.0.293ce0c-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | // Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package siv_test
import (
"crypto/rand"
"testing"
"github.com/jacobsa/crypto/siv"
aes_testing "github.com/jacobsa/crypto/testing"
. "github.com/jacobsa/oglematchers"
. "github.com/jacobsa/ogletest"
)
func TestDecrypt(t *testing.T) { RunTests(t) }
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
func dup(d []byte) []byte {
result := make([]byte, len(d))
copy(result, d)
return result
}
type DecryptTest struct{}
func init() { RegisterTestSuite(&DecryptTest{}) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *DecryptTest) NilKey() {
key := []byte(nil)
ciphertext := make([]byte, 16)
_, err := siv.Decrypt(key, ciphertext, nil)
ExpectThat(err, Error(HasSubstr("-byte")))
}
func (t *DecryptTest) ShortKey() {
key := make([]byte, 31)
ciphertext := make([]byte, 16)
_, err := siv.Decrypt(key, ciphertext, nil)
ExpectThat(err, Error(HasSubstr("-byte")))
}
func (t *DecryptTest) LongKey() {
key := make([]byte, 65)
ciphertext := make([]byte, 16)
_, err := siv.Decrypt(key, ciphertext, nil)
ExpectThat(err, Error(HasSubstr("-byte")))
}
func (t *DecryptTest) NilCiphertext() {
key := make([]byte, 64)
ciphertext := []byte(nil)
_, err := siv.Decrypt(key, ciphertext, nil)
ExpectThat(err, Error(HasSubstr("Invalid")))
ExpectThat(err, Error(HasSubstr("ciphertext")))
ExpectThat(err, Error(HasSubstr("length")))
}
func (t *DecryptTest) ShortCiphertext() {
key := make([]byte, 64)
ciphertext := make([]byte, 15)
_, err := siv.Decrypt(key, ciphertext, nil)
ExpectThat(err, Error(HasSubstr("Invalid")))
ExpectThat(err, Error(HasSubstr("ciphertext")))
ExpectThat(err, Error(HasSubstr("length")))
}
func (t *DecryptTest) TooMuchAssociatedData() {
key := make([]byte, 64)
ciphertext := make([]byte, 16)
associated := make([][]byte, 127)
_, err := siv.Decrypt(key, ciphertext, associated)
ExpectThat(err, Error(HasSubstr("associated")))
ExpectThat(err, Error(HasSubstr("126")))
}
func (t *DecryptTest) DoesntClobberAssociatedSlice() {
// Grab a test case with some associated data.
cases := aes_testing.EncryptCases()
AssertGt(len(cases), 1)
c := cases[1]
AssertEq(len(c.Associated), 1)
// Make a copy of the associated data.
associated0 := dup(c.Associated[0])
// Create a longer slice with some other data too.
associated1 := aes_testing.FromRfcHex("deadbeef")
longSlice := [][]byte{
associated0,
associated1,
}
// Call with a slice missing the last element, equivalent to the original
// associated data. The last element shouldn't be clobbered.
_, err := siv.Decrypt(c.Key, c.Output, longSlice[:1])
AssertEq(nil, err)
ExpectThat(
longSlice,
ElementsAre(
DeepEquals(associated0),
DeepEquals(associated1),
))
}
func (t *DecryptTest) WrongKey() {
// Grab a test case.
cases := aes_testing.EncryptCases()
AssertGt(len(cases), 1)
c := cases[1]
// Corrupt its key and call.
AssertGt(len(c.Key), 13)
c.Key[13]++
_, err := siv.Decrypt(c.Key, c.Output, c.Associated)
ExpectThat(err, Error(HasSubstr("authentic")))
_, ok := err.(*siv.NotAuthenticError)
ExpectTrue(ok, "Not an instance of *NotAuthenticError.")
}
func (t *DecryptTest) CorruptedSiv() {
// Grab a test case.
cases := aes_testing.EncryptCases()
AssertGt(len(cases), 1)
c := cases[1]
// Corrupt its SIV and call.
AssertGt(len(c.Output), 13)
c.Output[13]++
_, err := siv.Decrypt(c.Key, c.Output, c.Associated)
ExpectThat(err, Error(HasSubstr("authentic")))
_, ok := err.(*siv.NotAuthenticError)
ExpectTrue(ok, "Not an instance of *NotAuthenticError.")
}
func (t *DecryptTest) CorruptedCiphertext() {
// Grab a test case.
cases := aes_testing.EncryptCases()
AssertGt(len(cases), 10)
c := cases[10]
// Corrupt its ciphertext and call.
AssertGt(len(c.Output), 19)
c.Output[19]++
_, err := siv.Decrypt(c.Key, c.Output, c.Associated)
ExpectThat(err, Error(HasSubstr("authentic")))
_, ok := err.(*siv.NotAuthenticError)
ExpectTrue(ok, "Not an instance of *NotAuthenticError.")
}
func (t *DecryptTest) CorruptedAssociatedData() {
// Grab a test case.
cases := aes_testing.EncryptCases()
AssertGt(len(cases), 10)
c := cases[10]
// Corrupt its associated data and call.
AssertGt(len(c.Associated), 2)
AssertGt(len(c.Associated[2]), 3)
c.Associated[2][3]++
_, err := siv.Decrypt(c.Key, c.Output, c.Associated)
ExpectThat(err, Error(HasSubstr("authentic")))
_, ok := err.(*siv.NotAuthenticError)
ExpectTrue(ok, "Not an instance of *NotAuthenticError.")
}
func (t *DecryptTest) Rfc5297TestCaseA1() {
key := aes_testing.FromRfcHex(
"fffefdfc fbfaf9f8 f7f6f5f4 f3f2f1f0" +
"f0f1f2f3 f4f5f6f7 f8f9fafb fcfdfeff")
ciphertext := aes_testing.FromRfcHex(
"85632d07 c6e8f37f 950acd32 0a2ecc93" +
"40c02b96 90c4dc04 daef7f6a fe5c")
associated := [][]byte{
aes_testing.FromRfcHex(
"10111213 14151617 18191a1b 1c1d1e1f" +
"20212223 24252627"),
}
expected := aes_testing.FromRfcHex(
"11223344 55667788 99aabbcc ddee")
output, err := siv.Decrypt(key, ciphertext, associated)
AssertEq(nil, err)
ExpectThat(output, DeepEquals(expected))
}
func (t *DecryptTest) Rfc5297TestCaseA2() {
key := aes_testing.FromRfcHex(
"7f7e7d7c 7b7a7978 77767574 73727170" +
"40414243 44454647 48494a4b 4c4d4e4f")
ciphertext := aes_testing.FromRfcHex(
"7bdb6e3b 432667eb 06f4d14b ff2fbd0f" +
"cb900f2f ddbe4043 26601965 c889bf17" +
"dba77ceb 094fa663 b7a3f748 ba8af829" +
"ea64ad54 4a272e9c 485b62a3 fd5c0d")
associated := [][]byte{
aes_testing.FromRfcHex(
"00112233 44556677 8899aabb ccddeeff" +
"deaddada deaddada ffeeddcc bbaa9988" +
"77665544 33221100"),
aes_testing.FromRfcHex(
"10203040 50607080 90a0"),
aes_testing.FromRfcHex(
"09f91102 9d74e35b d84156c5 635688c0"),
}
expected := aes_testing.FromRfcHex(
"74686973 20697320 736f6d65 20706c61" +
"696e7465 78742074 6f20656e 63727970" +
"74207573 696e6720 5349562d 414553")
output, err := siv.Decrypt(key, ciphertext, associated)
AssertEq(nil, err)
ExpectThat(output, DeepEquals(expected))
}
func (t *DecryptTest) GeneratedTestCases() {
cases := aes_testing.EncryptCases()
AssertGe(len(cases), 100)
for i, c := range cases {
plaintext, err := siv.Decrypt(c.Key, c.Output, c.Associated)
AssertEq(nil, err, "Case %d: %v", i, c)
ExpectThat(plaintext, DeepEquals(c.Plaintext), "Case %d: %v", i, c)
}
}
////////////////////////////////////////////////////////////////////////
// Benchmarks
////////////////////////////////////////////////////////////////////////
func benchmarkDecrypt(
b *testing.B,
size int) {
var err error
// Generate the appropriate amount of random data.
plaintext := make([]byte, size)
_, err = rand.Read(plaintext)
if err != nil {
b.Fatalf("rand.Read: %v", err)
}
// Create a random key.
const keyLen = 32
key := make([]byte, keyLen)
_, err = rand.Read(key)
if err != nil {
b.Fatalf("rand.Read: %v", err)
}
// Encrypt it.
ciphertext, err := siv.Encrypt(nil, key, plaintext, nil)
if err != nil {
b.Fatalf("Encrypt: %v", err)
}
// Repeatedly decrypt it.
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = siv.Decrypt(key, ciphertext, nil)
if err != nil {
b.Fatalf("Decrypt: %v", err)
}
}
b.SetBytes(int64(size))
}
func BenchmarkDecrypt1KiB(b *testing.B) {
const size = 1 << 10
benchmarkDecrypt(b, size)
}
func BenchmarkDecrypt1MiB(b *testing.B) {
const size = 1 << 20
benchmarkDecrypt(b, size)
}
func BenchmarkDecrypt16MiB(b *testing.B) {
const size = 1 << 24
benchmarkDecrypt(b, size)
}
|