| 1 |
/* |
| 2 |
* recovery_test.go - tests for recovery codes in the crypto package |
| 3 |
* tests key wrapping/unwrapping and key generation |
| 4 |
* |
| 5 |
* Copyright 2017 Google Inc. |
| 6 |
* Author: Joe Richey ([email protected]) |
| 7 |
* |
| 8 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 9 |
* use this file except in compliance with the License. You may obtain a copy of |
| 10 |
* the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, software |
| 15 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 |
* License for the specific language governing permissions and limitations under |
| 18 |
* the License. |
| 19 |
*/ |
| 20 |
|
| 21 |
package crypto |
| 22 |
|
| 23 |
import ( |
| 24 |
"bytes" |
| 25 |
"fmt" |
| 26 |
"testing" |
| 27 |
|
| 28 |
"github.com/google/fscrypt/metadata" |
| 29 |
) |
| 30 |
|
| 31 |
const fakeSecretRecoveryCode = "EYTCMJRG-EYTCMJRG-EYTCMJRG-EYTCMJRG-EYTCMJRG-EYTCMJRG-EYTCMJRG-EYTCMJRG-EYTCMJRG-EYTCMJRG-EYTCMJRG-EYTCMJRG-EYTCMJQ=" |
| 32 |
|
| 33 |
var fakeSecretKey, _ = makeKey(38, metadata.PolicyKeyLen) |
| 34 |
|
| 35 |
// Note that this function is INSECURE. FOR TESTING ONLY |
| 36 |
func getRecoveryCodeFromKey(key *Key) ([]byte, error) { |
| 37 |
var buf bytes.Buffer |
| 38 |
if err := WriteRecoveryCode(key, &buf); err != nil { |
| 39 |
return nil, err |
| 40 |
} |
| 41 |
return buf.Bytes(), nil |
| 42 |
} |
| 43 |
|
| 44 |
func getRandomRecoveryCodeBuffer() ([]byte, error) { |
| 45 |
key, err := NewRandomKey(metadata.PolicyKeyLen) |
| 46 |
if err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
defer key.Wipe() |
| 50 |
return getRecoveryCodeFromKey(key) |
| 51 |
} |
| 52 |
|
| 53 |
func getKeyFromRecoveryCode(buf []byte) (*Key, error) { |
| 54 |
return ReadRecoveryCode(bytes.NewReader(buf)) |
| 55 |
} |
| 56 |
|
| 57 |
// Given a key, make a recovery code from that key, use that code to rederive |
| 58 |
// another key and check if they are the same. |
| 59 |
func testKeyEncodeDecode(key *Key) error { |
| 60 |
buf, err := getRecoveryCodeFromKey(key) |
| 61 |
if err != nil { |
| 62 |
return err |
| 63 |
} |
| 64 |
|
| 65 |
key2, err := getKeyFromRecoveryCode(buf) |
| 66 |
if err != nil { |
| 67 |
return err |
| 68 |
} |
| 69 |
defer key2.Wipe() |
| 70 |
|
| 71 |
if !bytes.Equal(key.data, key2.data) { |
| 72 |
return fmt.Errorf("encoding then decoding %x didn't yield the same key", key.data) |
| 73 |
} |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
// Given a recovery code, make a key from that recovery code, use that key to |
| 78 |
// rederive another recovery code and check if they are the same. |
| 79 |
func testRecoveryDecodeEncode(buf []byte) error { |
| 80 |
key, err := getKeyFromRecoveryCode(buf) |
| 81 |
if err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
defer key.Wipe() |
| 85 |
|
| 86 |
buf2, err := getRecoveryCodeFromKey(key) |
| 87 |
if err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
|
| 91 |
if !bytes.Equal(buf, buf2) { |
| 92 |
return fmt.Errorf("decoding then encoding %x didn't yield the same key", buf) |
| 93 |
} |
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
func TestGetRandomRecoveryString(t *testing.T) { |
| 98 |
b, err := getRandomRecoveryCodeBuffer() |
| 99 |
if err != nil { |
| 100 |
t.Fatal(err) |
| 101 |
} |
| 102 |
|
| 103 |
t.Log(string(b)) |
| 104 |
// t.Fail() // Uncomment to see an example random recovery code |
| 105 |
} |
| 106 |
|
| 107 |
func TestFakeSecretKey(t *testing.T) { |
| 108 |
buf, err := getRecoveryCodeFromKey(fakeSecretKey) |
| 109 |
if err != nil { |
| 110 |
t.Fatal(err) |
| 111 |
} |
| 112 |
|
| 113 |
recoveryCode := string(buf) |
| 114 |
if recoveryCode != fakeSecretRecoveryCode { |
| 115 |
t.Errorf("got '%s' instead of '%s'", recoveryCode, fakeSecretRecoveryCode) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func TestEncodeDecode(t *testing.T) { |
| 120 |
key, err := NewRandomKey(metadata.PolicyKeyLen) |
| 121 |
if err != nil { |
| 122 |
t.Fatal(err) |
| 123 |
} |
| 124 |
defer key.Wipe() |
| 125 |
|
| 126 |
if err = testKeyEncodeDecode(key); err != nil { |
| 127 |
t.Error(err) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
func TestDecodeEncode(t *testing.T) { |
| 132 |
buf, err := getRandomRecoveryCodeBuffer() |
| 133 |
if err != nil { |
| 134 |
t.Fatal(err) |
| 135 |
} |
| 136 |
|
| 137 |
if err = testRecoveryDecodeEncode(buf); err != nil { |
| 138 |
t.Error(err) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestWrongLengthError(t *testing.T) { |
| 143 |
key, err := NewRandomKey(metadata.PolicyKeyLen - 1) |
| 144 |
if err != nil { |
| 145 |
t.Fatal(err) |
| 146 |
} |
| 147 |
defer key.Wipe() |
| 148 |
|
| 149 |
if _, err = getRecoveryCodeFromKey(key); err == nil { |
| 150 |
t.Error("key with wrong length should have failed to encode") |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
func TestBadCharacterError(t *testing.T) { |
| 155 |
buf, err := getRandomRecoveryCodeBuffer() |
| 156 |
if err != nil { |
| 157 |
t.Fatal(err) |
| 158 |
} |
| 159 |
// Lowercase letters not allowed |
| 160 |
buf[3] = 'k' |
| 161 |
if key, err := getKeyFromRecoveryCode(buf); err == nil { |
| 162 |
key.Wipe() |
| 163 |
t.Error("lowercase letters should make decoding fail") |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func TestBadEndCharacterError(t *testing.T) { |
| 168 |
buf, err := getRandomRecoveryCodeBuffer() |
| 169 |
if err != nil { |
| 170 |
t.Fatal(err) |
| 171 |
} |
| 172 |
// Separator must be '-' |
| 173 |
buf[blockSize] = '_' |
| 174 |
if key, err := getKeyFromRecoveryCode(buf); err == nil { |
| 175 |
key.Wipe() |
| 176 |
t.Error("any separator that isn't '-' should make decoding fail") |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
func BenchmarkEncode(b *testing.B) { |
| 181 |
b.StopTimer() |
| 182 |
|
| 183 |
key, err := NewRandomKey(metadata.PolicyKeyLen) |
| 184 |
if err != nil { |
| 185 |
b.Fatal(err) |
| 186 |
} |
| 187 |
defer key.Wipe() |
| 188 |
|
| 189 |
b.StartTimer() |
| 190 |
for n := 0; n < b.N; n++ { |
| 191 |
if _, err = getRecoveryCodeFromKey(key); err != nil { |
| 192 |
b.Fatal(err) |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func BenchmarkDecode(b *testing.B) { |
| 198 |
b.StopTimer() |
| 199 |
|
| 200 |
buf, err := getRandomRecoveryCodeBuffer() |
| 201 |
if err != nil { |
| 202 |
b.Fatal(err) |
| 203 |
} |
| 204 |
|
| 205 |
b.StartTimer() |
| 206 |
for n := 0; n < b.N; n++ { |
| 207 |
key, err := getKeyFromRecoveryCode(buf) |
| 208 |
if err != nil { |
| 209 |
b.Fatal(err) |
| 210 |
} |
| 211 |
key.Wipe() |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func BenchmarkEncodeDecode(b *testing.B) { |
| 216 |
b.StopTimer() |
| 217 |
|
| 218 |
key, err := NewRandomKey(metadata.PolicyKeyLen) |
| 219 |
if err != nil { |
| 220 |
b.Fatal(err) |
| 221 |
} |
| 222 |
defer key.Wipe() |
| 223 |
|
| 224 |
b.StartTimer() |
| 225 |
for n := 0; n < b.N; n++ { |
| 226 |
if err = testKeyEncodeDecode(key); err != nil { |
| 227 |
b.Fatal(err) |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
func BenchmarkDecodeEncode(b *testing.B) { |
| 233 |
b.StopTimer() |
| 234 |
|
| 235 |
buf, err := getRandomRecoveryCodeBuffer() |
| 236 |
if err != nil { |
| 237 |
b.Fatal(err) |
| 238 |
} |
| 239 |
|
| 240 |
b.StartTimer() |
| 241 |
for n := 0; n < b.N; n++ { |
| 242 |
if err = testRecoveryDecodeEncode(buf); err != nil { |
| 243 |
b.Fatal(err) |
| 244 |
} |
| 245 |
} |
| 246 |
} |