blob: d2422b9cd5f608d00d8bbce1207ec38834114d4d [file] [log] [blame]
Tianjie Xua0a12cf2019-12-05 21:50:22 -08001/*
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 Crowleyc675b182019-12-18 16:09:24 -080019#include <limits>
20
Tianjie Xua0a12cf2019-12-05 21:50:22 -080021#include <android-base/logging.h>
22
23namespace aidl {
24namespace android {
25namespace hardware {
26namespace rebootescrow {
27namespace hadamard {
28
Paul Crowleyc675b182019-12-18 16:09:24 -080029static 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 Crowley53c005f2019-12-23 11:35:39 -080033// Use a simple LCG which is easy to run in reverse.
34// https://www.johndcook.com/blog/2017/07/05/simple-random-number-generator/
35constexpr uint64_t RNG_MODULUS = 0x7fffffff;
36constexpr uint64_t RNG_MUL = 742938285;
37constexpr uint64_t RNG_SEED = 20170705;
38constexpr uint64_t RNG_INV_MUL = 1413043504; // (mul * inv_mul) % modulus == 1
39constexpr uint64_t RNG_INV_SEED = 1173538311; // (seed * mul**65534) % modulus
40
Paul Crowleyc675b182019-12-18 16:09:24 -080041// 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 Crowley53c005f2019-12-23 11:35:39 -080052// 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 Crowleyc675b182019-12-18 16:09:24 -080055std::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 Crowley0080bde2019-12-23 12:00:28 -080059 // 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 Xua0a12cf2019-12-05 21:50:22 -080065 }
Paul Crowley0080bde2019-12-23 12:00:28 -080066 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 Xua0a12cf2019-12-05 21:50:22 -080081 }
Paul Crowley53c005f2019-12-23 11:35:39 -080082 // 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 Xua0a12cf2019-12-05 21:50:22 -080092 return result;
93}
94
Paul Crowleyc675b182019-12-18 16:09:24 -080095// 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.
97static uint16_t DecodeWord(size_t word, const std::vector<uint8_t>& encoded) {
Tianjie Xua0a12cf2019-12-05 21:50:22 -080098 std::vector<int32_t> scores;
99 scores.reserve(ENCODE_LENGTH);
Paul Crowleyc675b182019-12-18 16:09:24 -0800100 // Convert x -> -1^x in the encoded bits. e.g [1, 0, 0, 1] -> [-1, 1, 1, -1]
Tianjie Xua0a12cf2019-12-05 21:50:22 -0800101 for (uint32_t i = 0; i < ENCODE_LENGTH; i++) {
Paul Crowleyc675b182019-12-18 16:09:24 -0800102 scores.push_back(1 - 2 * read_bit(encoded, i * KEY_CODEWORDS + word));
Tianjie Xua0a12cf2019-12-05 21:50:22 -0800103 }
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 Crowleyc675b182019-12-18 16:09:24 -0800121 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 Xua0a12cf2019-12-05 21:50:22 -0800128
Paul Crowleyc675b182019-12-18 16:09:24 -0800129 } else if (-scores[i] > hiscore) {
130 winner = i | (1 << CODE_K);
131 hiscore = -scores[i];
132 }
Tianjie Xua0a12cf2019-12-05 21:50:22 -0800133 }
Paul Crowleyc675b182019-12-18 16:09:24 -0800134 return winner;
135}
Tianjie Xua0a12cf2019-12-05 21:50:22 -0800136
Paul Crowley53c005f2019-12-23 11:35:39 -0800137std::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 Crowleyc675b182019-12-18 16:09:24 -0800150 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 Xua0a12cf2019-12-05 21:50:22 -0800157}
158
159} // namespace hadamard
160} // namespace rebootescrow
161} // namespace hardware
162} // namespace android
163} // namespace aidl