目录

encryptedUsbManager (Encrypted USB Flash Drive Management)

This module provides the capabilities of managing USB flash drive encryption using the dm-crypt mechanism and supports mounting and unmounting the encrypted partition and swap partition of an encrypted USB flash drive.

These capabilities are available only for MDM apps focused on enterprise security control and granted with the required permissions.

Since: 26.0.1

Modules to Import

import { encryptedUsbManager } from '@kit.EnterpriseDataGuardKit';

CryptType

Enumerates the encryption types.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.PCService.EnterpriseEncryptBlock

Since: 26.0.1

Name Value Description
PLAIN 0 Plain mode.

CipherType

Enumerates encryption algorithms and modes.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.PCService.EnterpriseEncryptBlock

Since: 26.0.1

Name Value Description
SM4_XTS_PLAIN64 0x0 sm4-xts-plain64 algorithm.
SM4_CBC_PLAIN64 0x1 sm4-cbc-plain64 algorithm.
SM4_ECB 0x2 sm4-ecb algorithm.
AES_XTS_PLAIN64 0x10 aes-xts-plain64 algorithm.
AES_ECB 0x20 aes-ecb algorithm.

DiskInfo

Defines the disk device information, which is used to match the target disk for operations.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.PCService.EnterpriseEncryptBlock

Since: 26.0.1

Name Type Read-Only Optional Description
vendorId number No No Vendor ID.
productId number No No Product ID.
busNum number No No Bus number.
devAddress number No No Device address.

PartitionInfo

Defines the block device partition information, corresponding to the binding parameter of the losetup command.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.PCService.EnterpriseEncryptBlock

Since: 26.0.1

Name Type Read-Only Optional Description
offset number No No Offset for binding a loop device, in bytes. This field corresponds to the -o option of the losetup command.
sizeLimit number No No Size limit for binding a loop device, in bytes. This field corresponds to the --sizelimit option of the losetup command.

CryptInfo

Defines the encryption parameters for encrypting a USB flash drive.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.PCService.EnterpriseEncryptBlock

Since: 26.0.1

Name Type Read-Only Optional Description
type CryptType No No Encryption type, for example, plain mode.
cipher CipherType No No Encryption algorithm and mode, for example, sm4-xts-plain64.
keySize number No No Key length, in bits. The value range is (0, 512].
keyFile string No No Path to the dm-crypt key file. The key is stored in the raw binary format.

MountInfo

Defines mount options.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.PCService.EnterpriseEncryptBlock

Since: 26.0.1

Name Type Read-Only Optional Description
readOnly boolean No No Indicates whether to mount in read-only mode. The value true indicates yes, and false indicates no.

encryptedUsbManager.openEncryptedPartition

openEncryptedPartition(diskInfo: DiskInfo, partitionInfo: PartitionInfo, cryptInfo: CryptInfo, mountInfo: MountInfo): Promise<void>

Opens and mounts the encrypted partition of the encrypted USB flash drive. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ENTERPRISE_ENCRYPT_BLOCK_DEVICE

System capability: SystemCapability.PCService.EnterpriseEncryptBlock

Since: 26.0.1

Parameters

Parameter Type Mandatory Description
diskInfo DiskInfo Yes Disk device information, which is used to match the target disk.
partitionInfo PartitionInfo Yes Block device partition information.
cryptInfo CryptInfo Yes Encryption parameters.
mountInfo MountInfo Yes Mount options.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Universal Error Codes and ArkTS API Error Codes.

ID Error Message
201 Permission verification failed. The application does not have the permission required to call the API.
801

Capability not supported. Possible causes:

1. The hardware does not support the capability;

2. The chip does not support the capability;

3. A dependentservice feature is not supported.

1030300001 System service error.
1030300002 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
1030300003 Failed to bind the loop device.
1030300004 Failed to create the dm-crypt volume.
1030300005 Failed to mount the volume.
1030300009 Disk not found.
1030300010 Failed to get the disk list from the disk manager service.
1030300013 The volume is already mounted.

Example:

import { encryptedUsbManager } from '@kit.EnterpriseDataGuardKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { usbManager } from '@kit.BasicServicesKit';

// USB-IF international standard: Mass Storage Class—USB flash drives and external hard drives
const USB_CLASS_MASS_STORAGE = 8;

function getDiskInfo(): encryptedUsbManager.DiskInfo | null {
  const devices: Array<usbManager.USBDevice> = usbManager.getDevices();
  const storageDevice = devices.find(device => {
    // The device class code indicates a USB flash drive or an external hard drive.
    if (device.clazz === USB_CLASS_MASS_STORAGE) {
      return true;
    }
    for (const config of device.configs) {
      for (const iface of config.interfaces) {
        if (iface.clazz === USB_CLASS_MASS_STORAGE) {
          return true;
        }
      }
    }
    return false;
  });

  let diskInfo: encryptedUsbManager.DiskInfo | null = null;
  if (storageDevice) {
    diskInfo = {
      vendorId: storageDevice.vendorId,
      productId: storageDevice.productId,
      busNum: storageDevice.busNum,
      devAddress: storageDevice.devAddress
    };
  }
  return diskInfo;
}

try {
  let diskInfo: encryptedUsbManager.DiskInfo | null = getDiskInfo();
  if (diskInfo) {
    let partitionInfo: encryptedUsbManager.PartitionInfo = { offset: 210763776, sizeLimit: 2147483648 };
    let cryptInfo: encryptedUsbManager.CryptInfo = {
      type: encryptedUsbManager.CryptType.PLAIN,
      cipher: encryptedUsbManager.CipherType.SM4_XTS_PLAIN64,
      keySize: 256,
      keyFile: '/data/test/sm4_keyfile.bin'
    };
    let mountInfo: encryptedUsbManager.MountInfo = { readOnly: false };

    encryptedUsbManager.openEncryptedPartition(diskInfo, partitionInfo, cryptInfo, mountInfo).then(() => {
      console.info('Succeeded in opening encrypted partition.');
    }).catch((err: BusinessError) => {
      console.error(`Failed to open encrypted partition. Code: ${err.code}, message: ${err.message}.`);
    });
  } else {
    console.error('Failed to get disk info.');
  }
} catch (e) {
  console.error(`Failed to execute the openEncryptedPartition method. Code: ${e.code}, message: ${e.message}.`);
}

encryptedUsbManager.closeEncryptedPartition

closeEncryptedPartition(diskInfo: DiskInfo): Promise<void>

Unmounts and closes the encrypted partition of the encrypted USB flash drive. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ENTERPRISE_ENCRYPT_BLOCK_DEVICE

System capability: SystemCapability.PCService.EnterpriseEncryptBlock

Since: 26.0.1

Parameters

Parameter Type Mandatory Description
diskInfo DiskInfo Yes Disk device information, which is used to match the target disk.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Universal Error Codes and ArkTS API Error Codes.

ID Error Message
201 Permission verification failed. The application does not have the permission required to call the API.
801

Capability not supported. Possible causes:

1. The hardware does not support the capability;

2. The chip does not support the capability;

3. A dependentservice feature is not supported.

1030300001 System service error.
1030300002 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
1030300006 Failed to unmount the volume.
1030300007 Failed to destroy the dm-crypt volume.
1030300008 Failed to unbind the loop device.
1030300009 Disk not found.
1030300010 Failed to get the disk list from the disk manager service.
1030300011 Failed to get the volume list from the disk manager service.
1030300012 Volume not found.

Example:

import { encryptedUsbManager } from '@kit.EnterpriseDataGuardKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { usbManager } from '@kit.BasicServicesKit';

// USB-IF international standard: Mass Storage Class—USB flash drives and external hard drives
const USB_CLASS_MASS_STORAGE = 8;

function getDiskInfo(): encryptedUsbManager.DiskInfo | null {
  const devices: Array<usbManager.USBDevice> = usbManager.getDevices();
  const storageDevice = devices.find(device => {
    // The device class code indicates a USB flash drive or an external hard drive.
    if (device.clazz === USB_CLASS_MASS_STORAGE) {
      return true;
    }
    for (const config of device.configs) {
      for (const iface of config.interfaces) {
        if (iface.clazz === USB_CLASS_MASS_STORAGE) {
          return true;
        }
      }
    }
    return false;
  });

  let diskInfo: encryptedUsbManager.DiskInfo | null = null;
  if (storageDevice) {
    diskInfo = {
      vendorId: storageDevice.vendorId,
      productId: storageDevice.productId,
      busNum: storageDevice.busNum,
      devAddress: storageDevice.devAddress
    };
  }
  return diskInfo;
}

try {
  let diskInfo: encryptedUsbManager.DiskInfo | null = getDiskInfo();
  if (diskInfo) {
    encryptedUsbManager.closeEncryptedPartition(diskInfo).then(() => {
      console.info('Succeeded in closing encrypted partition.');
    }).catch((err: BusinessError) => {
      console.error(`Failed to close encrypted partition. Code: ${err.code}, message: ${err.message}.`);
    });
  } else {
    console.error('Failed to get disk info.');
  }
} catch (e) {
  console.error(`Failed to execute the closeEncryptedPartition method. Code: ${e.code}, message: ${e.message}.`);
}

encryptedUsbManager.openSwapPartition

openSwapPartition(diskInfo: DiskInfo, partitionInfo: PartitionInfo, mountInfo: MountInfo): Promise<void>

Mounts the swap partition of the encrypted USB flash drive. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ENTERPRISE_ENCRYPT_BLOCK_DEVICE

System capability: SystemCapability.PCService.EnterpriseEncryptBlock

Since: 26.0.1

Parameters

Parameter Type Mandatory Description
diskInfo DiskInfo Yes Disk device information, which is used to match the target disk.
partitionInfo PartitionInfo Yes Block device partition information, corresponding to the binding parameter of the losetup command.
mountInfo MountInfo Yes Mount options.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Universal Error Codes and ArkTS API Error Codes.

ID Error Message
201 Permission verification failed. The application does not have the permission required to call the API.
801

Capability not supported. Possible causes:

1. The hardware does not support the capability;

2. The chip does not support the capability;

3. A dependentservice feature is not supported.

1030300001 System service error.
1030300002 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
1030300003 Failed to bind the loop device.
1030300005 Failed to mount the volume.
1030300009 Disk not found.
1030300010 Failed to get the disk list from the disk manager service.
1030300013 The volume is already mounted.

Example:

import { encryptedUsbManager } from '@kit.EnterpriseDataGuardKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { usbManager } from '@kit.BasicServicesKit';

// USB-IF international standard: Mass Storage Class—USB flash drives and external hard drives
const USB_CLASS_MASS_STORAGE = 8;

function getDiskInfo(): encryptedUsbManager.DiskInfo | null {
  const devices: Array<usbManager.USBDevice> = usbManager.getDevices();
  const storageDevice = devices.find(device => {
    // The device class code indicates a USB flash drive or an external hard drive.
    if (device.clazz === USB_CLASS_MASS_STORAGE) {
      return true;
    }
    for (const config of device.configs) {
      for (const iface of config.interfaces) {
        if (iface.clazz === USB_CLASS_MASS_STORAGE) {
          return true;
        }
      }
    }
    return false;
  });

  let diskInfo: encryptedUsbManager.DiskInfo | null = null;
  if (storageDevice) {
    diskInfo = {
      vendorId: storageDevice.vendorId,
      productId: storageDevice.productId,
      busNum: storageDevice.busNum,
      devAddress: storageDevice.devAddress
    };
  }
  return diskInfo;
}

try {
  let diskInfo: encryptedUsbManager.DiskInfo | null = getDiskInfo();
  if (diskInfo) {
    let partitionInfo: encryptedUsbManager.PartitionInfo = { offset: 210763776, sizeLimit: 2147483648 };
    let mountInfo: encryptedUsbManager.MountInfo = { readOnly: false };

    encryptedUsbManager.openSwapPartition(diskInfo, partitionInfo, mountInfo).then(() => {
      console.info('Succeeded in opening swap partition.');
    }).catch((err: BusinessError) => {
      console.error(`Failed to open swap partition. Code: ${err.code}, message: ${err.message}.`);
    });
  } else {
    console.error('Failed to get disk info.');
  }
} catch (e) {
  console.error(`Failed to execute the openSwapPartition method. Code: ${e.code}, message: ${e.message}.`);
}

encryptedUsbManager.closeSwapPartition

closeSwapPartition(diskInfo: DiskInfo): Promise<void>

Unmounts the swap partition of the encrypted USB flash drive. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ENTERPRISE_ENCRYPT_BLOCK_DEVICE

System capability: SystemCapability.PCService.EnterpriseEncryptBlock

Since: 26.0.1

Parameters

Parameter Type Mandatory Description
diskInfo DiskInfo Yes Disk device information, which is used to match the target disk.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Universal Error Codes and ArkTS API Error Codes.

ID Error Message
201 Permission verification failed. The application does not have the permission required to call the API.
801

Capability not supported. Possible causes:

1. The hardware does not support the capability;

2. The chip does not support the capability;

3. A dependentservice feature is not supported.

1030300001 System service error.
1030300002 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
1030300006 Failed to unmount the volume.
1030300008 Failed to unbind the loop device.
1030300009 Disk not found.
1030300010 Failed to get the disk list from the disk manager service.
1030300011 Failed to get the volume list from the disk manager service.
1030300012 Volume not found.

Example:

import { encryptedUsbManager } from '@kit.EnterpriseDataGuardKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { usbManager } from '@kit.BasicServicesKit';

// USB-IF international standard: Mass Storage Class—USB flash drives and external hard drives
const USB_CLASS_MASS_STORAGE = 8;

function getDiskInfo(): encryptedUsbManager.DiskInfo | null {
  const devices: Array<usbManager.USBDevice> = usbManager.getDevices();
  const storageDevice = devices.find(device => {
    // The device class code indicates a USB flash drive or an external hard drive.
    if (device.clazz === USB_CLASS_MASS_STORAGE) {
      return true;
    }
    for (const config of device.configs) {
      for (const iface of config.interfaces) {
        if (iface.clazz === USB_CLASS_MASS_STORAGE) {
          return true;
        }
      }
    }
    return false;
  });

  let diskInfo: encryptedUsbManager.DiskInfo | null = null;
  if (storageDevice) {
    diskInfo = {
      vendorId: storageDevice.vendorId,
      productId: storageDevice.productId,
      busNum: storageDevice.busNum,
      devAddress: storageDevice.devAddress
    };
  }
  return diskInfo;
}

try {
  let diskInfo: encryptedUsbManager.DiskInfo | null = getDiskInfo();
  if (diskInfo) {
    encryptedUsbManager.closeSwapPartition(diskInfo).then(() => {
      console.info('Succeeded in closing swap partition.');
    }).catch((err: BusinessError) => {
      console.error(`Failed to close swap partition. Code: ${err.code}, message: ${err.message}.`);
    });
  } else {
    console.error('Failed to get disk info.');
  }
} catch (e) {
  console.error(`Failed to execute the closeSwapPartition method. Code: ${e.code}, message: ${e.message}.`);
}