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 void or_bit(std::vector<uint8_t>* input, size_t bit, uint8_t val) { |
| 30 | (*input)[bit >> 3] |= (val & 1u) << (bit & 7); |
| 31 | } |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 32 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 33 | static inline uint8_t read_bit(const std::vector<uint8_t>& input, size_t bit) { |
| 34 | return (input[bit >> 3] >> (bit & 7)) & 1u; |
| 35 | } |
| 36 | |
Paul Crowley | 53c005f | 2019-12-23 11:35:39 -0800 | [diff] [blame^] | 37 | // Use a simple LCG which is easy to run in reverse. |
| 38 | // https://www.johndcook.com/blog/2017/07/05/simple-random-number-generator/ |
| 39 | constexpr uint64_t RNG_MODULUS = 0x7fffffff; |
| 40 | constexpr uint64_t RNG_MUL = 742938285; |
| 41 | constexpr uint64_t RNG_SEED = 20170705; |
| 42 | constexpr uint64_t RNG_INV_MUL = 1413043504; // (mul * inv_mul) % modulus == 1 |
| 43 | constexpr uint64_t RNG_INV_SEED = 1173538311; // (seed * mul**65534) % modulus |
| 44 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 45 | // Apply an error correcting encoding. |
| 46 | // |
| 47 | // The error correcting code used is an augmented Hadamard code with |
| 48 | // k=15, so it takes a 16-bit input and produces a 2^15-bit output. |
| 49 | // We break the 32-byte key into 16 16-bit codewords and encode |
| 50 | // each codeword to a 2^15-bit output. |
| 51 | // |
| 52 | // To better defend against clustered errors, we stripe together the encoded |
| 53 | // codewords. Thus if a single 512-byte DRAM line is lost, instead of losing |
| 54 | // 2^11 bits from the encoding of a single code word, we lose 2^7 bits |
| 55 | // from the encoding of each of the 16 codewords. |
Paul Crowley | 53c005f | 2019-12-23 11:35:39 -0800 | [diff] [blame^] | 56 | // In addition we apply a Fisher-Yates shuffle to the bytes of the encoding; |
| 57 | // Hadamard encoding recovers much better from random errors than systematic |
| 58 | // ones, and this ensures that errors will be random. |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 59 | std::vector<uint8_t> EncodeKey(const std::vector<uint8_t>& input) { |
| 60 | CHECK_EQ(input.size(), KEY_SIZE_IN_BYTES); |
| 61 | std::vector<uint8_t> result(OUTPUT_SIZE_BYTES, 0); |
| 62 | static_assert(OUTPUT_SIZE_BYTES == 64 * 1024); |
| 63 | for (size_t i = 0; i < KEY_CODEWORDS; i++) { |
| 64 | uint16_t word = input[i * 2 + 1] << 8 | input[i * 2]; |
| 65 | for (size_t j = 0; j < ENCODE_LENGTH; j++) { |
| 66 | uint16_t wi = word & (j + ENCODE_LENGTH); |
| 67 | // Sum all the bits in the word and check its parity. |
| 68 | wi ^= wi >> 8u; |
| 69 | wi ^= wi >> 4u; |
| 70 | wi ^= wi >> 2u; |
| 71 | wi ^= wi >> 1u; |
| 72 | or_bit(&result, (j * KEY_CODEWORDS) + i, wi & 1); |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 73 | } |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 74 | } |
Paul Crowley | 53c005f | 2019-12-23 11:35:39 -0800 | [diff] [blame^] | 75 | // Apply the inverse shuffle here; we apply the forward shuffle in decoding. |
| 76 | uint64_t rng_state = RNG_INV_SEED; |
| 77 | for (size_t i = OUTPUT_SIZE_BYTES - 1; i > 0; i--) { |
| 78 | auto j = rng_state % (i + 1); |
| 79 | auto t = result[i]; |
| 80 | result[i] = result[j]; |
| 81 | result[j] = t; |
| 82 | rng_state *= RNG_INV_MUL; |
| 83 | rng_state %= RNG_MODULUS; |
| 84 | } |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 85 | return result; |
| 86 | } |
| 87 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 88 | // Decode a single codeword. Because of the way codewords are striped together |
| 89 | // this takes the entire input, plus an offset telling it which word to decode. |
| 90 | 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] | 91 | std::vector<int32_t> scores; |
| 92 | scores.reserve(ENCODE_LENGTH); |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 93 | // 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] | 94 | for (uint32_t i = 0; i < ENCODE_LENGTH; i++) { |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 95 | scores.push_back(1 - 2 * read_bit(encoded, i * KEY_CODEWORDS + word)); |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Multiply the hadamard matrix by the transformed input. |
| 99 | // |1 1 1 1| |-1| | 0| |
| 100 | // |1 -1 1 -1| * | 1| = | 0| |
| 101 | // |1 1 -1 -1| | 1| | 0| |
| 102 | // |1 -1 -1 1| |-1| |-4| |
| 103 | for (uint32_t i = 0; i < CODE_K; i++) { |
| 104 | uint16_t step = 1u << i; |
| 105 | for (uint32_t j = 0; j < ENCODE_LENGTH; j += 2 * step) { |
| 106 | for (uint32_t k = j; k < j + step; k++) { |
| 107 | auto a0 = scores[k]; |
| 108 | auto a1 = scores[k + step]; |
| 109 | scores[k] = a0 + a1; |
| 110 | scores[k + step] = a0 - a1; |
| 111 | } |
| 112 | } |
| 113 | } |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 114 | auto hiscore = std::numeric_limits<int32_t>::min(); |
| 115 | uint16_t winner; |
| 116 | // TODO(b/146520538): this needs to be constant time |
| 117 | for (size_t i = 0; i < ENCODE_LENGTH; i++) { |
| 118 | if (scores[i] > hiscore) { |
| 119 | winner = i; |
| 120 | hiscore = scores[i]; |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 121 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 122 | } else if (-scores[i] > hiscore) { |
| 123 | winner = i | (1 << CODE_K); |
| 124 | hiscore = -scores[i]; |
| 125 | } |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 126 | } |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 127 | return winner; |
| 128 | } |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 129 | |
Paul Crowley | 53c005f | 2019-12-23 11:35:39 -0800 | [diff] [blame^] | 130 | std::vector<uint8_t> DecodeKey(const std::vector<uint8_t>& shuffled) { |
| 131 | CHECK_EQ(OUTPUT_SIZE_BYTES, shuffled.size()); |
| 132 | // Apply the forward Fisher-Yates shuffle. |
| 133 | std::vector<uint8_t> encoded(OUTPUT_SIZE_BYTES, 0); |
| 134 | encoded[0] = shuffled[0]; |
| 135 | uint64_t rng_state = RNG_SEED; |
| 136 | for (size_t i = 1; i < OUTPUT_SIZE_BYTES; i++) { |
| 137 | auto j = rng_state % (i + 1); |
| 138 | encoded[i] = encoded[j]; |
| 139 | encoded[j] = shuffled[i]; |
| 140 | rng_state *= RNG_MUL; |
| 141 | rng_state %= RNG_MODULUS; |
| 142 | } |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 143 | std::vector<uint8_t> result(KEY_SIZE_IN_BYTES, 0); |
| 144 | for (size_t i = 0; i < KEY_CODEWORDS; i++) { |
| 145 | uint16_t val = DecodeWord(i, encoded); |
| 146 | result[i * CODEWORD_BYTES] = val & 0xffu; |
| 147 | result[i * CODEWORD_BYTES + 1] = val >> 8u; |
| 148 | } |
| 149 | return result; |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | } // namespace hadamard |
| 153 | } // namespace rebootescrow |
| 154 | } // namespace hardware |
| 155 | } // namespace android |
| 156 | } // namespace aidl |