A monotonic counter, counting method, electronic device and storage medium
By employing a method of alternating counting of two storage blocks in the response protection monotonic counter, combined with storage block switching and erasure under abnormal conditions, the problems of large storage block area and complex counting logic in the prior art are solved, achieving area reduction and logic optimization.
Patent Information
- Application Number
- CN202111671836.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-31
- Publication Date
- 2026-02-06
- Estimated Expiration
- 2041-12-31
AI Technical Summary
The existing response protection monotonic counter occupies a large area due to the additional storage block and has complex counting logic, so it is necessary to optimize memory usage and counting process.
Two storage blocks are used to count in turn. In case of abnormality, the current storage block to be operated on is determined by judging the starting address of the data bits and the data content of the storage block. The abnormal storage block is then switched and erased, thus optimizing the counting process.
It reduces the footprint of storage blocks, simplifies counting logic, reduces power consumption, and ensures the accuracy and efficiency of counting.
Smart Images

Figure CN114300026B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application belongs to the technical field of monotonic counter, and particularly relates to a monotonic counter, a counting method, an electronic device and a storage medium. BACKGROUND
[0002] The replay protection monotonic counter (RPMC) refers to a counter with a monotonic counting function. After data is counted in the replay protection monotonic counter, the replay protection monotonic counter only monotonically increases with the change of the counting value.
[0003] The replay protection monotonic counter in the prior art adopts two storage blocks to alternately store and count data, and an additional storage block for storing an additional identification bit to determine the currently operated storage block.
[0004] When the current storage block is not full of counting data, only data reading or counting needs to be performed in the current storage block; however, when a storage block is full of counting data, the current total counting value is written into another storage block, and the counting base bit, the identification bit and the check bit of the other storage block are written at the same time, and then the storage block full of data is erased. When an abnormal situation occurs and it is difficult to determine the currently operated storage block, the currently operated storage block is determined according to the additional identification bit, and accurate data reading or counting is realized. However, the additional storage block occupies a certain area, and the counting logic is relatively complex.
[0005] Therefore, the prior art needs to be improved and developed. SUMMARY
[0006] The application aims to provide a monotonic counter, a counting method, an electronic device and a storage medium, which can reduce the area occupied by the memory and optimize the counting process.
[0007] In a first aspect, the application provides a counting method of a monotonic counter, which is used for counting the monotonic counter, the monotonic counter comprises a storage block, the storage block comprises a first storage block and a second storage block, the storage block comprises a counting base bit, an identification bit, a check bit and a data bit, the counting method comprises a normal counting method and an abnormal situation processing method, the normal counting method comprises the following steps:
[0008] Alternately counting the first storage block and the second storage block;
[0009] When the monotonic counter has an abnormal situation, the processing steps of the abnormal situation comprise:
[0010] Repowering and determining the currently operated storage block according to an external address;
[0011] reading all byte data of data bits of the current operating memory block;
[0012] determining whether the byte data is all data 0xFF, if not, counting the count value and counting according to the normal counting method, if all data 0xFF, switching to another memory block and erasing, and then switching to the current operating memory block to count according to the normal counting method.
[0013] The monotonic counter only needs to count data in turn in two memory blocks, and the count value of the memory block can be obtained in abnormal conditions, thereby optimizing the counting process.
[0014] Optionally, the step of determining the current operating memory block comprises determining whether the starting address of the data bits of the memory block is consistent with the external address.
[0015] The current operating memory block is determined by determining whether the starting address of the data bits of the memory block is consistent with the external address, so as to count the count value.
[0016] Optionally, the step of counting in turn in the first memory block and the second memory block comprises:
[0017] continuously counting the first memory block until the data bits of the first memory block are full;
[0018] editing the count base bit, the identification bit and the check bit of the second memory block, switching to the first memory block and erasing;
[0019] continuously counting the second memory block until the data bits of the second memory block are full, and switching back to the first memory block after being full;
[0020] editing the count base bit, the identification bit and the check bit of the first memory block, switching to the second memory block and erasing, and repeating all the above steps.
[0021] Optionally, the step of determining whether the data exists data 0xFF comprises:
[0022] traversing all byte addresses of the data bits of the current operating memory block, and comparing the byte data of all addresses with data 0xFF one by one.
[0023] The count value is counted through the step.
[0024] Optionally, the step of editing the count base bit, the identification bit and the check bit of the memory block comprises writing the total count value of the memory block before switching into the count base bit, the identification bit and the check bit of the memory block after switching.
[0025] Optionally, the count base bit, the identification bit and the check bit of the storage block are the first eight bytes of the storage block.
[0026] Optionally, the abnormal situation includes any one of power failure, reset, and voltage instability.
[0027] The counting method of the monotonic counter according to the embodiment of the application, without the abnormal situation, only rotates between the two storage blocks, and does not need to perform additional storage blocks to back up the storage block counting value in the abnormal situation, thereby optimizing the operation flow of counting; in the abnormal situation, firstly, it is judged whether the external address is the start address of the data bit of the storage block, and then it is judged whether the storage block has the abnormal situation, for the storage block without the abnormal situation, the counting value is obtained by counting the data bit, thereby ensuring accurate counting, and for the storage block with the abnormal situation during counting, it is switched to another storage block for erasing and then switched back to the current storage block for counting. Compared with the counting method of the monotonic counter provided with the additional storage block, the counting method optimizes the counting and abnormal situation processing steps under the premise of realizing the same function, and reduces the logic complexity.
[0028] In the second aspect, the application provides a monotonic counter including a storage block, the storage block including a first storage block and a second storage block, characterized in that the counting method is executed, and further including:
[0029] a normal counting module, configured to rotate the first storage block and the second storage block for counting;
[0030] an abnormality processing module, including a determination module, a reading module and a comparison module;
[0031] the determination module, configured to re-power on and determine the current operation storage block according to the external address;
[0032] the reading module, configured to read all byte data of the data bit of the current operation storage block;
[0033] the comparison module, configured to compare all byte data of the data bit of the first storage block or the second storage block with data 0xFF, and judge whether the byte data is all data 0xFF, if not, count the counting value and count according to the counting method; if all data 0xFF, switch to another storage block and erase, and then switch to the current operation storage block to count according to the counting method.
[0034] The monotonic counter of the embodiment of the application judges whether the storage block has an abnormal condition through a comparison module, counts the storage block without the abnormal condition according to a counting method, and switches to another to erase and count again for the storage block with the abnormal condition. The hardware resource used by the comparison module reuses the existing hardware resource of the monotonic counter, only two storage blocks are used, and no additional storage block is needed, thereby reducing the occupied area of the storage block and reducing power consumption.
[0035] In a third aspect, the application provides an electronic device including a processor and a memory, the memory storing computer readable instructions that, when executed by the processor, perform the steps of any of the counting methods described above.
[0036] In a fourth aspect, the application provides a storage medium having stored thereon a computer program that, when executed by a processor, performs the steps of any of the counting methods described above.
[0037] Other features and advantages of the present application will be further described in the following description, and in part will become apparent to those skilled in the art from the following description, or will be learned by practice of the present application. The objects and other advantages of the present application will be realized and attained by the structure particularly pointed out in the written description and claims hereof as well as the appended drawings. BRIEF DESCRIPTION OF DRAWINGS
[0038] Figure 1 The step flow chart of the counting method of the embodiment of the application.
[0039] Figure 2 The structural schematic diagram of the electronic device of the embodiment of the application.
[0040] Label explanation: 1, electronic device; 2, processor; 3, memory; 4, communication bus. DETAILED DESCRIPTION
[0041] The technical solutions in the embodiments of the application will be described clearly and completely below with reference to the accompanying drawings of the embodiments of the application. Obviously, the described embodiments are only part of the embodiments of the application, rather than all the embodiments of the application. The components of the embodiments of the application described and shown in the accompanying drawings can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the application provided in the accompanying drawings is not intended to limit the scope of the claimed application, but only represents selected embodiments of the application. Based on the embodiments of the application, all other embodiments obtained by those skilled in the art without creative work fall within the scope of the application.
[0042] It should be noted that like reference numerals and characters refer to like elements throughout the following description and the claims attached hereto. Note that, in describing the present application, the terms "first", "second", and the like, merely identify features being described as distinguished from other features, rather than implying these relative importance.
[0043] The response protection monotonic counter includes two storage blocks, a first storage block and a second storage block, which can be set in the same memory and controlled by the same peripheral circuit to provide control selection signals and address signals, or can be set in different memories. The first storage block and the second storage block each have a count base bit, an identification bit, a check bit, and a data bit.
[0044] The count base bit is used to record the total count value at the time of switching the full storage block. When switching the storage block for counting, the data of the storage block before switching is saved to the storage block after switching. The identification bit is used to indicate whether the corresponding storage block needs to be erased and programmed.
[0045] The check bit is used to ensure the validity of the data in the special bit of the storage block. Specifically, the count base bit generates a value through a certain check operation (such as addition operation, parity operation, or XOR operation), and the value is compared with the value in the check bit. If the two values are equal, the data in the storage block corresponding to the check bit is valid and no exception has occurred; if the two values are not equal, the data in the storage block corresponding to the check bit is invalid and an exception has occurred.
[0046] The data bit is used to store the count data, and the count data of the monotonic counter is stored in a storage array composed of multiple storage units. Each storage block includes multiple data bits, and each time the monotonic counter reads and writes data from the corresponding flash memory, one data bit of the storage block in the monotonic counter is written into the data.
[0047] The count value is the sum of all data bits of the current storage block, and the total count value is the sum of the count value of the data bits of the storage block and the total count value of the storage block before switching. Generally, the monotonic counter is used together with the flash memory, and is used to ensure the confidentiality and integrity of the read and write data during data storage. For example, after the flash memory performs a read and write operation of data, a count value in the monotonic counter is increased, thereby providing a record information for the number of read and write times experienced by the flash memory. The flash memory performs a read and write operation of data every time, and the un-written data bit in the current storage block of the monotonic counter is written with data. Each byte of the data bit represents a count value, and each 8 bits form a byte. In the embodiment, the data bit stores 8 0s to indicate that the data bit is written, that is, the byte data is 0x00, and the count value is increased by 1 for each byte data 0x00; and 8 1s indicate that the data bit is not written or has been erased, that is, the byte data is 0xFF. The byte data above is represented in hexadecimal.
[0048] It is assumed that the storage block is a storage array composed of 2048 bit lines (BL) and 16 word lines (WL) connected storage cells. Each word line connects eight storage cells arranged in sequence to form a byte. The storage block can store 4096 bytes, of which the first 8 bytes are used to store the count base bits, identification bits and check bits of the total count value. Therefore, the remaining 4088 bytes are data bits, which can store a total of 4088 times of data, that is, a single storage block can count 4088 times.
[0049] Figure 1 The step flow chart of the counting method of the embodiment of the application. The monotonic counter of the application only uses two storage blocks, and does not need to additionally increase an additional storage block. The specific counting method steps are as follows:
[0050] The counting method includes a normal counting method S1 and an abnormal situation processing method S2. The normal counting method S1 includes the following steps:
[0051] The first storage block and the second storage block are counted in turn, and this step includes:
[0052] S11, continuously counting the first storage block until the data bits of the first storage block are full;
[0053] Before the monotonic counter starts counting, it is assumed that both the first memory block and the second memory block are erased, and the current count value is 0. When the monotonic counter starts counting, it starts from the start address of the data bit of the first memory block, i.e. from the 9th byte address of the first memory block, and every time the flash performs a read-write operation on the data, i.e. sequentially writes byte data 0x00 into the data bit which has not been written, until the 4096th byte address is written. At the same time when the data bit is written with byte data, the count value is also constantly incremented by 1, and the count value is written into the first 8 bytes of the first memory. After the data bit of the first memory block is written full, all the data information of the data bit is 0x00, and the total count value of the first memory block is 4088.
[0054] S12, edit the count base, identification bit and check bit of the second memory block, switch to the first memory block and erase;
[0055] In this step, the peripheral circuit receives an address instruction according to a control signal, and selects the second memory block by calling an address decoder. Then the counting is performed in the second memory block.
[0056] The step of editing the count base, identification bit and check bit of the memory block includes writing the total count value of the memory block before switching into the count base, identification bit and check bit of the memory block after switching. The count value and the check bit need to be recalculated, and the identification bit does not need to be calculated, i.e. after the first memory is written full, the total count value of the first memory is written into the first 8 bytes of the second memory, so as to inherit the total count value of the first memory, and the count value of the second memory is accumulated.
[0057] S13, continuously count the second memory block until the data bit of the second memory block is written full;
[0058] This step is similar to the step S11, both of which start counting from the start address of the data bit of the second memory block, and count until the data bit of the second memory block is written full. Before switching back to the first memory block, the total count value counted by the count base, identification bit and check bit of the second memory block is 8176.
[0059] S14, edit the count base, identification bit and check bit of the first memory block, switch to the second memory block and erase, and repeat all the steps of S11-S14.
[0060] In S11-S12 and S13-S14, the process of switching the monotonic counter between the two memory blocks is involved. If abnormal conditions such as power failure, reset, unstable voltage, etc. occur during the switching process, it is not possible to determine which memory block was being counted and the count value before the abnormal condition occurred. Therefore, after the monotonic counter is powered on again, it first needs to determine whether the currently operated memory block has an abnormality.
[0061] The specific abnormal situation processing method S2 includes the following steps:
[0062] S21, re-powering, determining the current operating storage block according to the external address;
[0063] S22, reading all data of the data bits of the current operating storage block;
[0064] S23, judging whether the data is all data 0xFF, if not all data 0xFF, counting the count value and counting according to the counting method; if all data 0xFF, switching to another storage block and erasing, and then switching to the current operating storage block and counting according to the counting method.
[0065] Suppose that the monotonic counter experiences abnormal situations such as power failure, unstable voltage and the like during the counting process, then the storage block has the following three situations:
[0066] Case 1: the first storage block is erased, at this time the byte data of the data bits of the first storage block is all 0xFF, the second storage block is counting, the byte data of the first N data bits of the second storage block is all 0x00, and the byte data of the last N data bits is all 0xFF (N is a positive integer and greater than 9);
[0067] Case 2: the second storage block is erased, at this time the byte data of the data bits of the second storage block is all 0xFF, the first storage block is counting, the byte data of the first N data bits of the first storage block is all 0x00, and the byte data of the last N data bits is all 0xFF;
[0068] Case 3: one of the storage blocks is abnormal, and the other storage block is erased, the byte data of the data bits of the first storage block and the second storage block is all 0xFF.
[0069] In actual application, the first storage block and the second storage block are completely consistent and can be replaced with each other, and for case 1 and case 2, they can be combined into the same kind of situation. In this case, first, it is judged whether the starting address of the data bits of the first storage block is consistent with the external address, for example, in case 1, the byte data of the data bits of the first storage block is all 0xFF, the starting address of the data bits of the first storage block is the 9th byte address, and the external address of the erased storage is consistent with the starting address, then the current operating storage block is the first storage block. On the contrary, as long as the first storage block has byte data 0x00, the starting address is inconsistent with the external address, indicating that the current operating storage block is the second storage block.
[0070] When the current operating storage block is the first storage block, in order to obtain the count value of the second storage block, switching to the second storage block, when the current operating storage block is the second storage block, directly proceeding to the next step. By traversing all byte addresses of the data bits of the second storage block, comparing the byte data of each data bit with the data 0xFF one by one, if the byte data is not 0xFF, the count value is incremented by one, until the byte data is 0xFF, then the loop is exited and the byte address and the count value before the exception are recorded. If the last N byte data of the second storage block are all 0xFF, then the count value is N-8, and the byte address and the count value before the exception can be restored through the count value. Then the counting mode according to steps S11-S14 can be performed.
[0071] For case 3, by traversing all byte addresses of the data bits of one of the storage blocks, the byte data is compared with the data 0xFF one by one. The byte data of the first storage block or the second storage block is 0xFF, so the count value is always 0. Therefore, it can be determined that the current operating storage block has an abnormal condition. Then switch to the other storage block for erasing operation. The other storage block should be erased because its data is unreliable. The current operating storage block has verified the reliability of the data during the traversal of all data, so it can not be erased. After the second storage block is erased, switch to the current operating storage block again and perform the counting mode according to steps S11-S14.
[0072] The counting method of the monotonic counter of the embodiment of the application, without abnormal conditions, only rotates between two storage blocks for counting, without the need to perform additional storage blocks to backup the count value of the storage block in abnormal conditions, optimizing the operation process of counting; in the case of abnormal conditions, first determine whether the external address is the starting address of the data bit of the storage block, and then determine whether the storage block has an abnormal condition. For the storage block without abnormal conditions, count the data bits to obtain the count value, thereby ensuring accurate counting. During counting, the storage block with an abnormal condition is switched to another storage block for erasing and then switched back to the current storage block for counting. Compared with the counting method of the monotonic counter with additional storage blocks, the counting method optimizes the counting and abnormal condition processing steps under the premise of achieving the same function, and reduces the logic complexity.
[0073] The monotonic counter of the embodiment of the application includes a storage block, the storage block includes a first storage block and a second storage block, the storage block performs the above counting method, and further includes:
[0074] The normal counting module is configured to rotate counting for the first storage block and the second storage block;
[0075] The abnormality processing module includes a determination module, a reading module and a comparison module.
[0076] determining module, configured to determine the current operation storage block according to the external address after the re-powering;
[0077] reading module, configured to read all byte data of the data bits of the current operation storage block;
[0078] comparing module, configured to compare the byte data of all data bits of the first storage block or the second storage block with data 0xFF, and determine whether the byte data is all data 0xFF, if not, count the count value and count according to the counting method; if all data 0xFF, switch to another storage block and erase, and then switch to the current operation storage block to count according to the counting method.
[0079] The monotonic counter of the embodiment of the application determines whether the storage block has an abnormal condition through the comparing module, counts the count value and counts according to the counting method for the storage block without the abnormal condition, and switches to another storage block to erase and then count for the storage block with the abnormal condition. The hardware resources used by the comparing module reuse the existing hardware resources of the monotonic counter, only two storage blocks are used, and no additional storage block needs to be set, thereby reducing the occupied area of the storage block and reducing the power consumption.
[0080] Figure 2 A structure schematic diagram of an electronic device is shown. The application provides an electronic device 1, comprising a processor 2 and a memory 3, the processor 2 and the memory 3 are interconnected and communicate with each other through a communication bus 4 and / or other forms of connection mechanism (not marked), the memory 3 stores a computer program executable by the processor 2, when the computing device runs, the processor 2 executes the computer program to execute the method in any optional implementation manner of the above-mentioned embodiments.
[0081] The embodiment of the present application further provides a storage medium, which stores a computer program, and the computer program is executed by a processor to execute the method in any optional implementation manner of the above embodiment. The storage medium can be realized by any type of volatile or nonvolatile storage devices or a combination thereof, such as a static random access memory (SRAM), an electrically erasable programmable read-only memory (EEPROM), an erasable programmable read-only memory (EPROM), a programmable read-only memory (PROM), a read-only memory (ROM), a magnetic storage, a flash memory.
[0082] In the embodiments of the present application, it should be understood that the disclosed device and method can be implemented in other ways. The device embodiments described above are merely schematic, for example, the division of the units is only a logical function division, and another division mode can be used in actual implementation, for example, a plurality of units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between the units or components shown or discussed can be indirect coupling or communication connection through some communication interfaces, devices or units, and can be electrical, mechanical or other forms.
[0083] In addition, the units described as separate components can or can not be physically separate, and the components shown as units can or can not be physical units, that is, they can be located in one place, or can be distributed on a plurality of network units. According to actual needs, some or all of the units can be selected to achieve the purpose of the embodiment.
[0084] Furthermore, the functional modules in each of the embodiments of the present application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0085] In this article, the relationship terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between the entities or operations.
[0086] The above merely illustrates the embodiments of the present application but should not be taken as limitations to the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A counting method of a monotonic counter, for counting a monotonic counter, the monotonic counter comprising a memory block, The storage block includes a first storage block and a second storage block, and the first storage block and the second storage block each include a count radix bit, an identification bit, a check bit and a data bit, characterized in that, The counting method comprises a normal counting method and an abnormal condition processing method, the normal counting method comprising the following steps: counting the first memory block and the second memory block alternately; when an abnormal condition occurs in the monotonic counter, the abnormal condition processing method comprising the following steps: re-powering, and determining a currently operated memory block according to an external address; reading all byte data of data bits of the currently operated memory block; determining whether all the byte data are data 0xFF, if not, counting a count value and counting according to the normal counting method; if all the byte data are data 0xFF, switching to another memory block and erasing, and then switching to the currently operated memory block and counting according to the normal counting method.
2. The counting method of a monotonic counter according to claim 1, wherein, The step of counting the first memory block and the second memory block alternately comprises: continuously counting the first memory block until the data bits of the first memory block are full; editing count radix bits, identification bits and check bits of the second memory block, switching to the first memory block and erasing; continuously counting the second memory block until the data bits of the second memory block are full, and switching back to the first memory block after the data bits of the second memory block are full; editing count radix bits, identification bits and check bits of the first memory block, switching to the second memory block and erasing, and repeating all the above steps.
3. The method of claim 1, wherein, The step of determining the currently operated memory block comprises determining whether a start address of data bits of the memory block is consistent with the external address.
4. The method of claim 1, wherein, The step of determining whether the data contains data 0xFF comprises: traversing all byte addresses of the data bits of the currently operated memory block, and comparing byte data of all the addresses with data 0xFF one by one.
5. The method of claim 2, wherein, The step of editing count radix bits, identification bits and check bits of the second memory block comprises writing a total count value of the memory block before switching into count radix bits, identification bits and check bits of the memory block after switching.
6. The method of claim 1, wherein, The count radix bits, the identification bits and the check bits of the memory block are the first eight bytes of the memory block.
7. The method of claim 1, wherein, The abnormal condition comprises any one of power failure, reset and unstable voltage.
8. A monotonic counter comprising a storage block, the storage block comprising a first storage block and a second storage block, characterized in that, Further comprising: a normal counting module, for counting the first memory block and the second memory block alternately; an abnormal condition processing module, the abnormal condition processing module comprising a determining module, a reading module and a comparing module; the determining module, for re-powering and determining a currently operated memory block according to an external address; the reading module, for reading all byte data of data bits of the currently operated memory block; the comparing module, for comparing byte data of all data bits of the first memory block or the second memory block with data 0xFF, the comparing module determining whether all the byte data are data 0xFF, if not, counting a count value and counting according to the normal counting method; if all the byte data are data 0xFF, switching to another memory block and erasing, and then switching to the currently operated memory block and counting according to the normal counting method.
9. An electronic device, comprising: A computer program product comprising a processor and a memory storing computer readable instructions which, when executed by the processor, perform the steps of the counting method of any of claims 1-7.
10. A storage medium having stored thereon a computer program, characterized in that The computer program, when executed by a processor, performs the steps of the counting method of any of claims 1-7.
Citation Information
Patent Citations
Monotonic counter and operating method thereof
CN112582008A
Monotonic counter and counting method thereof
CN112582009A