We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
Best PracticesSecurity DesignApplication Data Security

Application Data Security

Overview

The security of an application is the key to its success.

HarmonyOS provides three levels of security capabilities: system security, DevEco Studio tool security, and application ecosystem security.

  • At the system security layer, HarmonyOS uses security protection technologies, such as integrity protection, vulnerability anti-exploitation, and secure and trusted environment, to support secure running of applications and ensure the security and reliability of their services.
  • At the DevEco Studio/tool level, ecosystem applications are created by different developers for a wide range of purposes. In addition to high-quality ecosystem applications, there are also malicious applications for profit-making, black market, fraudulent applications, and malicious marketing advertisement promotion (such as malicious pop-ups). To ensure the security, purity, and controllability of ecosystem applications, HarmonyOS builds an end-to-end secure and controllable ecosystem.
  • At the application security ecosystem level, HarmonyOS uses various software and hardware infrastructures to ensure application security. Specifically, sensitive data storage and user privacy protection are implemented by using the following measures:
    • Sensitive data classification: Data is classified by device and data level to protect user data security and ensure correct data flow between distributed devices.
    • File classification: Application data is saved to the system encrypted directories corresponding to the data security level.
    • Asset data encryption: TEE-based security protection and management APIs are provided so that developers can focus on their tasks without worrying about the underlying implementation details.

The following describes sensitive data storage and user privacy protection at the application security ecosystem level.

Risk Levels

HarmonyOS builds a secure application ecosystem based on hierarchical security, including the security level classification of devices and data.

  • In distributed scenarios, devices are classified into different security levels to ensure secure cross-device data transfer.
  • Flowing data is classified into different levels, for instance, to determine if data can be transferred to common devices based on its sensitivity level, as varying degrees of sensitive data have distinct transfer restrictions. These classifications should be determined at the system level rather than the application level. Otherwise, system data can leak badly if developers do not understand level classifications well.

Device Security Levels

Device security levels are classified into SL1 to SL5 based on devices' security capabilities, for example, whether a TEE or a secure storage chip is available. The intelligent wearable is usually a low-security SL1 device, and the mobile phone and the tablet are usually high-security devices.

From SL1 to SL5, the security capability requirements corresponding to integrity protection, encryption and data protection, permission and access control, trusted execution environment, and vulnerability anti-exploitation are gradually improved.

Data Security Levels

Sensitive data classification helps you identify and classify data based on the sensitivity, value, and potential impact of data leakage. You need to understand different types of data and how they are used, classify the data, and develop appropriate security measures and controls based on the classification to protect the data and ensure compliance with relevant laws and standards. The following table illustrates the personal data classification.

Table 1 Personal data classification
Expand

Personal Data Classification

Data Grading

Example

Sensitive/Personal data

Asset data

Critical

Password, authentication token, and the answer to security question.

Personal racial data

Ethnicity.

Authoritative social ID

High

ID card number, military officer card, social/insurance security number (social number), driver's license, passport number, and visa authorization number.

Negative reputation data

Criminal records (criminal, civil, and litigation records), prison records, and disciplinary records.

Health data

Health data (height, weight, body fat, blood pressure, blood sugar, and heart rate), and medical records.

Home control data

Home control, car reverse control, and reverse control after phone projection.

Age and birth data

Medium

Age and date of birth.

Virtual network ID

HUAWEI ID, broadband account, and other network accounts (phone number and email address).

General social ID

Name.

Personal multimedia data

Photo, video, recording, text, calendar event, and note.

General registration information

Low

Nickname, profile picture, gender, nationality, place of birth, education level, and professional background.

Positive reputation data

Professional achievement.

Non-personal data

Non-personal data

Public

Anonymized personal data.

Non-personal data

Publicly released data in system and device information, such as the version information of the TCB module, integrity measurement value, integrity measurement value of the access control policy data, and policy data.

In addition to content-based data classification, you must comply with laws and regulations, such as the General Data Protection Regulation (GDPR) and Personal Information Protection Law (PIPL), to protect user privacy and data security. Figure 1 shows the data classification.

The data can be rated into four security levels: S1, S2, S3, and S4.

Table 2 Data security level
Expand

Risk Level

Risk Criteria

Definition

Example

Critical

S4

Special data types defined by industry laws and regulations, involving the most private individual information or data that may cause severe adverse impact on an individual or group once disclosed, tampered with, corrupted, or destroyed.

Political opinion, religious and philosophical belief, trade union membership, genetic data, biological information, health and sexual life status, sexual orientation, device authentication, and personal credit card information.

High

S3

Data that may cause severe adverse impact on an individual or group once disclosed, tampered with, corrupted, or destroyed.

Personal real-time precise positioning information and workout routes.

Medium

S2

Data that may cause severe adverse impact on an individual or group once disclosed, tampered with, corrupted, or destroyed.

Detailed personal address, name, and nickname.

Low

S1

Data that may cause minor adverse impact on an individual or group once disclosed, tampered with, corrupted, or destroyed.

Gender, nationality, and user application record.

A higher data security label and level indicate stricter encryption and access control measures and higher data security.

In cross-device data sync, data access is controlled based on the device security level and data security labels. In principle, data can be synced only to the devices whose data security labels are not higher than the device's security level. The access control matrix is as follows:

Table 3 Access control matrix
Expand

Device Security Level

Data Security Labels of the Synchronizable Device

SL1

S1

SL2

S1 to S2

SL3

S1 to S3

SL4

S1 to S4

SL5

S1 to S4

The watch is an SL1 device with a low security level. The database with data security label S1 can be synced with devices, but the databases with database labels S2 to S4 cannot.

Hierarchical Data Encryption

Hierarchical Data Protection

Different file paths have different attributes and features. The application sandbox protects hierarchical data to prevent malicious path traversal access. In the application file directory, different directories are distinguished based on file encryption types.

  • el1: Directory for the data that can be accessed once the device starts. This directory contains device-focused files.
  • el2: Directory for the data that can be accessed only after at least one successful unlock operation (by PIN, fingerprint, or facial authentication, or password-free sign-in) upon the start of the device. This directory contains user-focused files.

    Unless otherwise required, application data is placed in the el2 directory for security purposes. The data that needs to be accessed before the screen is unlocked (such as the clock, alarm, and wallpaper data) is placed in the el1 directory. For details, see Obtaining and Modifying Encryption Levels.

For details about the application file directory, see Application File Directory and Application File Path.

The hierarchical data file path is the common file path of the application. The code for obtaining the path is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. getEl2Path(): void {
  2. let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  3. context.area = contextConstant.AreaMode.EL2;
  4. let filePath = context.filesDir + '/health_data.txt';
  5. this.message = filePath;
  6. }

To obtain the el1 path, modify AreaMode. The sample code is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. getEl1Path(): void {
  2. let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  3. context.area = contextConstant.AreaMode.EL1;
  4. let filePath = context.filesDir + '/health_data.txt';
  5. this.message = filePath;
  6. }

Unless otherwise required, application data is placed in the el2 directory for security purposes.

The system provides four file-level encryption types to protect files. The application can save files to the corresponding data directories as required. The following table lists the hierarchical encryption policies.

Table 4 Hierarchical encryption policy
Expand

Encryption Level

Policy

el4

10 seconds after the user locks the device, the decrypted data protection key is discarded from the memory. All such data cannot be accessed unless the user enters the password again or uses the fingerprint or face to unlock the device.

el3

After a user locks a device, if a file has been opened, the file can still be accessed. Once the file is closed (lock screen), the file cannot be accessed again unless the user enters the password again or uses the fingerprint or face to unlock the device.

el2

After the user unlocks the device for the first time after power-on, the user can access the file. This is the default data protection class for all third-party application data that is not allocated to the data protection class.

el1

Files can be accessed in direct startup mode or after the device is unlocked.

Data Encryption Algorithm

OpenHarmony Universal KeyStore (HUKS) provides platform-level key management for services, including key generation, import, destruction, attestation, agreement, derivation, encryption/decryption, signature/signature verification, and access control. HUKS is the basic security capability of the system. Services can securely and conveniently use the key lifecycle management capability provided by HUKS without implementing complex cryptographic algorithms and key access control models, ensuring the security of sensitive service data.

For details, see Introduction to Universal Keystore Kit.

Data Security Cases

Health data is common sensitive data in daily life and is widely used in HarmonyOS devices, such as mobile phones and watches. The following describes how to store and protect health check reports.

Scenario Design

Health check data is classified as high-risk data based on data classification rules. Therefore, in a program design process, in addition to a protection measure of hierarchical data encryption, secondary encryption needs to be performed to enhance data security. Specifically, the data may be first encrypted once, and then the encrypted data is processed by using a hierarchical data encryption method. This is to improve data confidentiality and integrity and effectively prevent potential data leakage and unauthorized access. With such multi-level encryption measures, health check data can be stored and transmitted more securely and reliably.

The program scenarios consist of the health checklist page, data entry page, and data details page, as shown in the following figure.

Figure 1 Scenario design diagram

Scenario Development

In actual development, data encryption and decryption often coexist. This scenario consists of two sub-scenarios: data entry and data details. The following describes specific implementations of the two sub-scenarios respectively.

  • Implementation of data recording
  1. Tap the entry button on the list page to enter the data entry page.
  2. Enter information on the data entry page and use the tag prompt character as the key to assemble the data into a JSON object.
  3. Encrypt the entered information. Serialize the assembled JSON object into a string, convert the string into a byte array, and use HUKS APIs to encrypt the byte array to obtain the encrypted byte array.
  4. Encrypt the data in hierarchical mode, and then use the fileio API to write the encrypted byte array to the hierarchically encrypted file.
  • Implementation of data details
  1. Tap a data item on the list page to go to the data details page. Use the params parameter of the pushUrl API to pass the name of the hierarchically encrypted file.
  2. Decrypt the encrypted data. Call the fileio API to read the encrypted data based on the name of the hierarchically encrypted file transferred by params, and then call the HUKS API to decrypt the read byte array to obtain the decrypted byte array.
  3. Display data details. Convert the decrypted byte array into a string, then into a JSON object, and display the JSON object on the page.

Code Implementation

  • Data read and write with hierarchical encryption

After obtaining the data path by referring to the code in Hierarchical Data Protection, read and write files as normal. The code is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. function writeFile(filePath: string, data: string): void {
  2. try {
  3. let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
  4. let writeLen = fileIo.writeSync(file.fd, data);
  5. hilog.info(0x0000, 'AppDataSecurity', 'The length of str is: ' + writeLen);
  6. fileIo.closeSync(file);
  7. } catch (error) {
  8. hilog.error(0x0000, 'AppDataSecurity', `writeFile error ${JSON.stringify(error)}`);
  9. }
  10. }
  11. function readFile(filePath: string): string {
  12. try {
  13. let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
  14. let arrayBuffer = new ArrayBuffer(1024);
  15. class Option {
  16. public offset: number = 0;
  17. public length: number = 0;
  18. }
  19. let option = new Option();
  20. option.length = arrayBuffer.byteLength;
  21. let readLen = fileIo.readSync(file.fd, arrayBuffer, option);
  22. let buf = buffer.from(arrayBuffer, 0, readLen);
  23. hilog.info(0x0000, 'AppDataSecurity', `The length of of file: ${readLen}`);
  24. fileIo.closeSync(file);
  25. return buf.toString();
  26. } catch (error) {
  27. hilog.error(0x0000, 'AppDataSecurity', `readFile error ${JSON.stringify(error)}`);
  28. }
  29. return '';
  30. }
  • Data encryption/decryption

Before using HUKS, configure the specifications of the encryption algorithm. In this case, the AES algorithm with the key length of 128 is selected. Before using the crypto algorithms, generate the algorithm key. The following shows the code for algorithm configuration and algorithm key generation.

The algorithm type HUKS_ALG_AES, key size, and key usage must be specified in the configuration function for generating the algorithm key.

Collapse
Word wrap
Dark theme
Copy code
  1. function GetAesGenerateProperties(): Array<huks.HuksParam> {
  2. let properties: Array<huks.HuksParam> = [{
  3. tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
  4. value: huks.HuksKeyAlg.HUKS_ALG_AES
  5. }, {
  6. tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
  7. value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128
  8. }, {
  9. tag: huks.HuksTag.HUKS_TAG_PURPOSE,
  10. value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT |
  11. huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
  12. }];
  13. return properties;
  14. }

The algorithm specifications configured for the encryption function must be the same as those for generating the algorithm key.

HUKS_TAG_PADDING has three padding modes:

  • NoPadding: No padding.
  • PKCS5: pads a block cipher with a block size of 8 bytes.
  • PKCS7: pads any block size from 1 to 255 bytes. The PKCS7 padding scheme is the same as that of PKCS5.

HUKS_TAG_BLOCK_MODE has seven block modes. For CBC, CTR, OFB, and CFB modes, only HUKS_TAG_IV is used. For GCM and CCM modes, set HUKS_TAG_NONCE, HUKS_TAG_ASSOCIATED_DATA, and HUKS_TAG_IV.

Collapse
Word wrap
Dark theme
Copy code
  1. function GetAesEncryptProperties(): Array<huks.HuksParam> {
  2. let properties: Array<huks.HuksParam> = [{
  3. tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
  4. value: huks.HuksKeyAlg.HUKS_ALG_AES
  5. }, {
  6. tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
  7. value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128
  8. }, {
  9. tag: huks.HuksTag.HUKS_TAG_PURPOSE,
  10. value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT
  11. }, {
  12. tag: huks.HuksTag.HUKS_TAG_PADDING,
  13. value: huks.HuksKeyPadding.HUKS_PADDING_PKCS7
  14. }, {
  15. tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
  16. value: huks.HuksCipherMode.HUKS_MODE_CBC
  17. }, {
  18. tag: huks.HuksTag.HUKS_TAG_IV,
  19. value: StringToUint8Array(IV)
  20. }];
  21. return properties;
  22. }

The algorithm specifications of the decryption function must be the same as those of the encryption algorithm, but the key usage must be set to decryption.

Collapse
Word wrap
Dark theme
Copy code
  1. function GetAesDecryptProperties(): Array<huks.HuksParam> {
  2. let properties: Array<huks.HuksParam> = [{
  3. tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
  4. value: huks.HuksKeyAlg.HUKS_ALG_AES
  5. }, {
  6. tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
  7. value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128
  8. }, {
  9. tag: huks.HuksTag.HUKS_TAG_PURPOSE,
  10. value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
  11. }, {
  12. tag: huks.HuksTag.HUKS_TAG_PADDING,
  13. value: huks.HuksKeyPadding.HUKS_PADDING_PKCS7
  14. }, {
  15. tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
  16. value: huks.HuksCipherMode.HUKS_MODE_CBC
  17. }, {
  18. tag: huks.HuksTag.HUKS_TAG_IV,
  19. value: StringToUint8Array(IV)
  20. }];
  21. return properties;
  22. }

After the preceding configuration is complete, call the function for generating the algorithm key.

Collapse
Word wrap
Dark theme
Copy code
  1. async function GenerateAesKey(): Promise<void> {
  2. let genProperties = GetAesGenerateProperties();
  3. let options: huks.HuksOptions = {
  4. properties: genProperties
  5. };
  6. await huks.generateKeyItem(aesKeyAlias, options)
  7. .then((data) => {
  8. hilog.info(0x0000, 'AppDataSecurity', `promise: generate AES Key success, data = ${JSON.stringify(data)}`);
  9. }).catch((error: Error) => {
  10. hilog.error(0x0000, 'AppDataSecurity', `promise: generate AES Key failed, ${JSON.stringify(error)}`);
  11. })
  12. }

After the algorithm key is generated, encryption and decryption can be performed. During encryption and decryption, obtain the corresponding configuration and then call initSession and finishSession to complete the operation.

Collapse
Word wrap
Dark theme
Copy code
  1. async function EncryptData(): Promise<void> {
  2. let encryptProperties = GetAesEncryptProperties();
  3. let options: huks.HuksOptions = {
  4. properties: encryptProperties,
  5. inData: StringToUint8Array(plainText)
  6. };
  7. await huks.initSession(aesKeyAlias, options)
  8. .then((data) => {
  9. handle = data.handle;
  10. }).catch((error: Error) => {
  11. hilog.error(0x0000, 'AppDataSecurity', `promise: init EncryptData failed, ${JSON.stringify(error)}`);
  12. })
  13. await huks.finishSession(handle, options)
  14. .then((data) => {
  15. hilog.info(0x0000, 'AppDataSecurity',
  16. `promise: encrypt data success, data is ` + Uint8ArrayToString(data.outData as Uint8Array));
  17. cipherData = data.outData as Uint8Array;
  18. }).catch((error: Error) => {
  19. hilog.error(0x0000, 'AppDataSecurity', `promise: encrypt data failed, ${JSON.stringify(error)}`);
  20. })
  21. }
  22. async function DecryptData(): Promise<void> {
  23. let decryptOptions = GetAesDecryptProperties()
  24. let options: huks.HuksOptions = {
  25. properties: decryptOptions,
  26. inData: cipherData
  27. };
  28. await huks.initSession(aesKeyAlias, options)
  29. .then((data) => {
  30. handle = data.handle;
  31. }).catch((error: Error) => {
  32. hilog.error(0x0000, 'AppDataSecurity', `promise: init DecryptData failed, ${JSON.stringify(error)}`);
  33. })
  34. await huks.finishSession(handle, options)
  35. .then((data) => {
  36. hilog.info(0x0000, 'AppDataSecurity',
  37. `promise: decrypt data success, data is ` + Uint8ArrayToString(data.outData as Uint8Array));
  38. }).catch((error: Error) => {
  39. hilog.error(0x0000, 'AppDataSecurity', `promise: decrypt data failed, ${JSON.stringify(error)}`);
  40. })
  41. }
NOTE

Sensitive data exists in the health check report. Therefore, secondary encryption and hierarchical encryption are required. The decryption process is the reverse of the encryption process. The data encrypted in hierarchical mode is read first, and then decrypted. When sensitive data of different security levels is involved, the data can be encapsulated and serialized by security level to prevent high-security data from being leaked to low-security devices during data flow.

To facilitate log printing and output, Base64 transcoding is performed on the encrypted data. This step can be omitted during application development.

Summary

Data security is critical to application development and must be considered in analysis, evaluation, design, and development phases.

  • Perform comprehensive risk assessment and classification analysis on various types of data involved in applications, and determine corresponding security protection policies based on the sensitivity and importance of the data.
  • Use hierarchical data protection and proper encryption algorithm specifications to prevent sensitive data leakage.

In this document, security levels of data in the health check report are classified and corresponding encryption is performed.

  • Data security classification is a way to protect data importance and sensitivity. You can classify data, take security measures, and select appropriate hierarchical data protection policies based on service scenarios to simplify data management and improve data security. For example, you can use strict access control policies to prevent unauthorized access and leakage of sensitive personal privacy data.
  • Data encryption technology protects data confidentiality. You need to select proper encryption algorithms and specifications to balance security and computing complexity and ensure normal use of the data. You also need to consider data transmission encryption and storage encryption.

In actual application development, you need to pay attention to data security and continuously improve data security protection measures to ensure that user data security and privacy are effectively protected.

Search in Best Practices
Enter a keyword.