blob: c578152ba12b3797e73cc74166a4d658e91f5c11 [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 void or_bit(std::vector<uint8_t>* input, size_t bit, uint8_t val) {
30 (*input)[bit >> 3] |= (val & 1u) << (bit & 7);
31}
Tianjie Xua0a12cf2019-12-05 21:50:22 -080032
Paul Crowleyc675b182019-12-18 16:09:24 -080033static 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 Crowley53c005f2019-12-23 11:35:39 -080037// Use a simple LCG which is easy to run in reverse.
38// https://www.johndcook.com/blog/2017/07/05/simple-random-number-generator/
39constexpr uint64_t RNG_MODULUS = 0x7fffffff;
40constexpr uint64_t RNG_MUL = 742938285;
41constexpr uint64_t RNG_SEED = 20170705;
42constexpr uint64_t RNG_INV_MUL = 1413043504; // (mul * inv_mul) % modulus == 1
43constexpr uint64_t RNG_INV_SEED = 1173538311; // (seed * mul**65534) % modulus
44
Paul Crowleyc675b182019-12-18 16:09:24 -080045// 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 Crowley53c005f2019-12-23 11:35:39 -080056// 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 Crowleyc675b182019-12-18 16:09:24 -080059std::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 Xua0a12cf2019-12-05 21:50:22 -080073 }
Tianjie Xua0a12cf2019-12-05 21:50:22 -080074 }
Paul Crowley53c005f2019-12-23 11:35:39 -080075 // 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 Xua0a12cf2019-12-05 21:50:22 -080085 return result;
86}
87
Paul Crowleyc675b182019-12-18 16:09:24 -080088// 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.
90static uint16_t DecodeWord(size_t word, const std::vector<uint8_t>& encoded) {
Tianjie Xua0a12cf2019-12-05 21:50:22 -080091 std::vector<int32_t> scores;
92 scores.reserve(ENCODE_LENGTH);
Paul Crowleyc675b182019-12-18 16:09:24 -080093 // 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 -080094 for (uint32_t i = 0; i < ENCODE_LENGTH; i++) {
Paul Crowleyc675b182019-12-18 16:09:24 -080095 scores.push_back(1 - 2 * read_bit(encoded, i * KEY_CODEWORDS + word));
Tianjie Xua0a12cf2019-12-05 21:50:22 -080096 }
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 Crowleyc675b182019-12-18 16:09:24 -0800114 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 Xua0a12cf2019-12-05 21:50:22 -0800121
Paul Crowleyc675b182019-12-18 16:09:24 -0800122 } else if (-scores[i] > hiscore) {
123 winner = i | (1 << CODE_K);
124 hiscore = -scores[i];
125 }
Tianjie Xua0a12cf2019-12-05 21:50:22 -0800126 }
Paul Crowleyc675b182019-12-18 16:09:24 -0800127 return winner;
128}
Tianjie Xua0a12cf2019-12-05 21:50:22 -0800129
Paul Crowley53c005f2019-12-23 11:35:39 -0800130std::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 Crowleyc675b182019-12-18 16:09:24 -0800143 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 Xua0a12cf2019-12-05 21:50:22 -0800150}
151
152} // namespace hadamard
153} // namespace rebootescrow
154} // namespace hardware
155} // namespace android
156} // namespace aidl