Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <HadamardUtils.h> |
| 18 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 19 | #include <limits> |
| 20 | |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | |
| 23 | namespace aidl { |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace rebootescrow { |
| 27 | namespace hadamard { |
| 28 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 29 | static inline uint8_t read_bit(const std::vector<uint8_t>& input, size_t bit) { |
| 30 | return (input[bit >> 3] >> (bit & 7)) & 1u; |
| 31 | } |
| 32 | |
Paul Crowley | 53c005f | 2019-12-23 11:35:39 -0800 | [diff] [blame] | 33 | // Use a simple LCG which is easy to run in reverse. |
| 34 | // https://www.johndcook.com/blog/2017/07/05/simple-random-number-generator/ |
| 35 | constexpr uint64_t RNG_MODULUS = 0x7fffffff; |
| 36 | constexpr uint64_t RNG_MUL = 742938285; |
| 37 | constexpr uint64_t RNG_SEED = 20170705; |
| 38 | constexpr uint64_t RNG_INV_MUL = 1413043504; // (mul * inv_mul) % modulus == 1 |
| 39 | constexpr uint64_t RNG_INV_SEED = 1173538311; // (seed * mul**65534) % modulus |
| 40 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 41 | // Apply an error correcting encoding. |
| 42 | // |
| 43 | // The error correcting code used is an augmented Hadamard code with |
| 44 | // k=15, so it takes a 16-bit input and produces a 2^15-bit output. |
| 45 | // We break the 32-byte key into 16 16-bit codewords and encode |
| 46 | // each codeword to a 2^15-bit output. |
| 47 | // |
| 48 | // To better defend against clustered errors, we stripe together the encoded |
| 49 | // codewords. Thus if a single 512-byte DRAM line is lost, instead of losing |
| 50 | // 2^11 bits from the encoding of a single code word, we lose 2^7 bits |
| 51 | // from the encoding of each of the 16 codewords. |
Paul Crowley | 53c005f | 2019-12-23 11:35:39 -0800 | [diff] [blame] | 52 | // In addition we apply a Fisher-Yates shuffle to the bytes of the encoding; |
| 53 | // Hadamard encoding recovers much better from random errors than systematic |
| 54 | // ones, and this ensures that errors will be random. |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 55 | std::vector<uint8_t> EncodeKey(const std::vector<uint8_t>& input) { |
| 56 | CHECK_EQ(input.size(), KEY_SIZE_IN_BYTES); |
| 57 | std::vector<uint8_t> result(OUTPUT_SIZE_BYTES, 0); |
| 58 | static_assert(OUTPUT_SIZE_BYTES == 64 * 1024); |
Paul Crowley | 0080bde | 2019-12-23 12:00:28 -0800 | [diff] [blame] | 59 | // Transpose the key so that each row contains one bit from each codeword |
| 60 | uint16_t wordmatrix[CODEWORD_BITS]; |
| 61 | for (size_t i = 0; i < CODEWORD_BITS; i++) { |
| 62 | uint16_t word = 0; |
| 63 | for (size_t j = 0; j < KEY_CODEWORDS; j++) { |
| 64 | word |= read_bit(input, i + j * CODEWORD_BITS) << j; |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 65 | } |
Paul Crowley | 0080bde | 2019-12-23 12:00:28 -0800 | [diff] [blame] | 66 | wordmatrix[i] = word; |
| 67 | } |
| 68 | // Fill in the encodings in Gray code order for speed. |
| 69 | uint16_t val = wordmatrix[CODEWORD_BITS - 1]; |
| 70 | size_t ix = 0; |
| 71 | for (size_t i = 0; i < ENCODE_LENGTH; i++) { |
| 72 | for (size_t b = 0; b < CODEWORD_BITS; b++) { |
| 73 | if (i & (1 << b)) { |
| 74 | ix ^= (1 << b); |
| 75 | val ^= wordmatrix[b]; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | result[ix * KEY_CODEWORD_BYTES] = val & 0xffu; |
| 80 | result[ix * KEY_CODEWORD_BYTES + 1] = val >> 8u; |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 81 | } |
Paul Crowley | 53c005f | 2019-12-23 11:35:39 -0800 | [diff] [blame] | 82 | // Apply the inverse shuffle here; we apply the forward shuffle in decoding. |
| 83 | uint64_t rng_state = RNG_INV_SEED; |
| 84 | for (size_t i = OUTPUT_SIZE_BYTES - 1; i > 0; i--) { |
| 85 | auto j = rng_state % (i + 1); |
| 86 | auto t = result[i]; |
| 87 | result[i] = result[j]; |
| 88 | result[j] = t; |
| 89 | rng_state *= RNG_INV_MUL; |
| 90 | rng_state %= RNG_MODULUS; |
| 91 | } |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 92 | return result; |
| 93 | } |
| 94 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 95 | // Decode a single codeword. Because of the way codewords are striped together |
| 96 | // this takes the entire input, plus an offset telling it which word to decode. |
| 97 | static uint16_t DecodeWord(size_t word, const std::vector<uint8_t>& encoded) { |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 98 | std::vector<int32_t> scores; |
| 99 | scores.reserve(ENCODE_LENGTH); |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 100 | // Convert x -> -1^x in the encoded bits. e.g [1, 0, 0, 1] -> [-1, 1, 1, -1] |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 101 | for (uint32_t i = 0; i < ENCODE_LENGTH; i++) { |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 102 | scores.push_back(1 - 2 * read_bit(encoded, i * KEY_CODEWORDS + word)); |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Multiply the hadamard matrix by the transformed input. |
| 106 | // |1 1 1 1| |-1| | 0| |
| 107 | // |1 -1 1 -1| * | 1| = | 0| |
| 108 | // |1 1 -1 -1| | 1| | 0| |
| 109 | // |1 -1 -1 1| |-1| |-4| |
| 110 | for (uint32_t i = 0; i < CODE_K; i++) { |
| 111 | uint16_t step = 1u << i; |
| 112 | for (uint32_t j = 0; j < ENCODE_LENGTH; j += 2 * step) { |
| 113 | for (uint32_t k = j; k < j + step; k++) { |
| 114 | auto a0 = scores[k]; |
| 115 | auto a1 = scores[k + step]; |
| 116 | scores[k] = a0 + a1; |
| 117 | scores[k + step] = a0 - a1; |
| 118 | } |
| 119 | } |
| 120 | } |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 121 | auto hiscore = std::numeric_limits<int32_t>::min(); |
| 122 | uint16_t winner; |
| 123 | // TODO(b/146520538): this needs to be constant time |
| 124 | for (size_t i = 0; i < ENCODE_LENGTH; i++) { |
| 125 | if (scores[i] > hiscore) { |
| 126 | winner = i; |
| 127 | hiscore = scores[i]; |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 128 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 129 | } else if (-scores[i] > hiscore) { |
| 130 | winner = i | (1 << CODE_K); |
| 131 | hiscore = -scores[i]; |
| 132 | } |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 133 | } |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 134 | return winner; |
| 135 | } |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 136 | |
Paul Crowley | 53c005f | 2019-12-23 11:35:39 -0800 | [diff] [blame] | 137 | std::vector<uint8_t> DecodeKey(const std::vector<uint8_t>& shuffled) { |
| 138 | CHECK_EQ(OUTPUT_SIZE_BYTES, shuffled.size()); |
| 139 | // Apply the forward Fisher-Yates shuffle. |
| 140 | std::vector<uint8_t> encoded(OUTPUT_SIZE_BYTES, 0); |
| 141 | encoded[0] = shuffled[0]; |
| 142 | uint64_t rng_state = RNG_SEED; |
| 143 | for (size_t i = 1; i < OUTPUT_SIZE_BYTES; i++) { |
| 144 | auto j = rng_state % (i + 1); |
| 145 | encoded[i] = encoded[j]; |
| 146 | encoded[j] = shuffled[i]; |
| 147 | rng_state *= RNG_MUL; |
| 148 | rng_state %= RNG_MODULUS; |
| 149 | } |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 150 | std::vector<uint8_t> result(KEY_SIZE_IN_BYTES, 0); |
| 151 | for (size_t i = 0; i < KEY_CODEWORDS; i++) { |
| 152 | uint16_t val = DecodeWord(i, encoded); |
| 153 | result[i * CODEWORD_BYTES] = val & 0xffu; |
| 154 | result[i * CODEWORD_BYTES + 1] = val >> 8u; |
| 155 | } |
| 156 | return result; |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | } // namespace hadamard |
| 160 | } // namespace rebootescrow |
| 161 | } // namespace hardware |
| 162 | } // namespace android |
| 163 | } // namespace aidl |