mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-12-29 18:11:05 +00:00
Add the implementation of a robust non-volatile incrementing counter using 2 pages from the underlying flash. It is used to implement the U2F functionality. The main goal of the counter is providing a strictly incrementing value whatever adverse events (malicious or not) happen as it is used to prevent rollback attacks in the U2F protocol. Given the limitation of the flash process: ie wear-out endurance and 2kB-page erase granularity only and possible isolated bit-flips (accentuated by power losses), the counting is done by pulling down several bits at a time from their erased state (1) to 0. The counting is implemented this way with 2 pages called LOW and HIGH: The LOW page is implemented in a strike style, with each "strike" zero-ing out 4 bits at a time, meaning each word can be struck a total of 8 times. Once the LOW page is completely struck, the HIGH page is incremented by 2. The even increment is for the value, the odd increment is a guard signal that the LOW page must be erased. So as an example: If HIGH is 2, the LOW page would increment to 3, erase itself, and then increment to 4. If this process is interrupted for some reason (power loss or user intervention) and the HIGH left at 3, on next resume, the HI page will recognize something was left pending and erase again. For a platform with 2-kB flash pages, it can count up to 8388608, then it is stuck at 0xFFFFFFF indefinitely. Mostly copied over from Marius code in cr52 code-base. Signed-off-by: Marius Schilder <mschilder@google.com> Signed-off-by: Vincent Palatin <vpalatin@chromium.org> BRANCH=cr50 BUG=b:35545754 TEST=with follow-up CLs, run U2FTest on Eve Change-Id: Idd0756078e3641c4a24f9c4ccf6611909bd5f00f Reviewed-on: https://chromium-review.googlesource.com/518135 Commit-Ready: Vincent Palatin <vpalatin@chromium.org> Tested-by: Vincent Palatin <vpalatin@chromium.org> Reviewed-by: Marius Schilder <mschilder@chromium.org> Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
21 lines
635 B
C
21 lines
635 B
C
/* Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
#ifndef __EC_INCLUDE_NVCOUNTER_H
|
|
#define __EC_INCLUDE_NVCOUNTER_H
|
|
|
|
/*
|
|
* CONFIG_FLASH_NVCOUNTER provides a robust, non-volatile incrementing counter.
|
|
*
|
|
* It is currently uses 2 physical pages of flash for its underlying storage
|
|
* which are configured by CONFIG_FLASH_NVCTR_BASE_A and
|
|
* CONFIG_FLASH_NVCTR_BASE_B in board.h
|
|
*/
|
|
|
|
/* return the value of the counter after incrementing it */
|
|
uint32_t nvcounter_incr(void);
|
|
|
|
#endif /* __EC_INCLUDE_NVCOUNTER_H */
|