分类: Android平台
2013-06-10 16:29:26
转自:http://blog.csdn.net/magod/article/details/7928090
Android加密,挺实用一功能,e文看着费劲,磕磕巴巴翻译一下。
-----------------------------------------------------------------------------
原文地址:
2013/11/19 18:54 增加一篇文章:http://blog.csdn.net/godisagirl2/article/details/8989216
If you want to enable encryption on your device based on Android3.0 aka Honeycomb, there are only a few requirements:
如果你想在3.0 Honeycomb设备上打开加密功能,那么需要:
The /data filesystem must be on a device that presents a blockdevice interface. eMMC is used in the first devices. This isbecause the encryption is done by the dm-crypt layer in the kernel,which works at the block device layer.
/data文件系统必须基于块设备,eMMC是首选。这是因为磁盘加密功能工作于kernel的dm-crypt模块,工作在块设备层。
The function get_fs_size() in system/vold/cryptfs.c assumes thefilesystem used for /data is ext4. It's just error checking code tomake sure the filesystem doesn't extend into the last 16 Kbytes ofthe partition where the crypto footer is kept. It was useful fordevelopment when sizes were changing, but should not be requiredfor release. If you are not using ext4, you can either delete itand the call to it, or fix it to understand the filesystem you areusing.
system/vold/cryptfs.c中的get_fs_size()函数会假定/data的文件系统是ext4,异常检查确定文件系统没有使用分区上最后16Kbytes,以确保cryptofooter的存储空间。对开发人员而言是有用的,因为它的大小会变化,但是无法释放它。如果你用的不是ext4,要么删除这个函数及其调用,要么改造它以适用你的系统。
Most of the code to handle the setup and teardown of the temporaryframework is in files that are not usually required to be changedon a per device basis. However, the init..rc file will require somechanges. All services must be put in one of three classes: core,main or late_state. Services in the core class are not shutdown andrestarted when the temporary framework gets the disk password.Services in the main class are restarted when the framework isrestarted. Services in late_start are not started until after thetemporary framework is restarted. Put services here that are notrequired to be running while the temporary framework gets the diskpassword.
Also any directories that need to be created on /data that aredevice specific need to be in the Action for post-fs-data, and thatAction must end with the command "setprop vold.post_fs_data_done1". If your init..rc file does not have a post-fs-data Action, thenthe post-fs-data Action in the main init.rc file must end with thecommand "setprop vold.post_fs_data_done 1".
大部分控制临时framework的设置和代码是在那些通常和设备无关的代码文件中。但是init.
需要在/data目录下创建的所有目录都是需要写在post-fs-data的action,并且必须用命令"setpropvold.post_fs_data_done 1"来结束。如果你的init.
PS:vold会在停止main class服务后将/datamount为tmpfs,然后触发post-fs-data action的操作并等待"setpropvold.post_fs_data_done1",然后继续启动临时framework并开始加密过程。如果init.rc中不执行"setpropvold.post_fs_data_done 1",那么vold会认为异常从而重启整个系统。
Disk encryption on Android is based on dm-crypt, which is a kernelfeature that works at the block device layer. Therefore, it is notusable with YAFFS, which talks directly to a raw nand flash chip,but does work with emmc and similar flash devices which presentthemselves to the kernel as a block device. The current preferredfilesystem to use on these devices is ext4, though that isindependent of whether encryption is used or not.
Android的磁盘加密基于dm-crypt,它是一个kernel的一个功能动作于块设备上。因此不能基于YAFFS文件系统,它会直接访问NANDFlash芯片,但是可以工作在eMMC及其相似的Flash设备上,只要对于kernel来说是块设备。目前设备上首选使用的文件系统是ext4,但它并不关心加密与否。
While the actual encryption work is a standard linux kernelfeature, enabling it on an Android device proved somewhat tricky.The Android system tries to avoid incorporating GPL components, sousing the cryptsetup command or libdevmapper were not availableoptions. So making the appropriate ioctl(2) calls into the kernelwas the best choice. The Android volume daemon (vold) already didthis to support moving apps to the SD card, so I chose to leveragethat work for whole disk encryption. The actual encryption used forthe filesystem for first release is 128 AES with CBC andESSIV:SHA256. The master key is encrypted with 128 bit AES viacalls to the openssl library.
实际上加密功能是linuxkernel的标准功能,但使它能够工作与Android有些棘手。Android尽量避免包含GPL成分,所以不能使用cryptsetup命令或者libdevmapper。所以使用ioctl(2)通知内核是最好的选择。Android volumedaemon(vold)已经支持了apps to SD功能,所以利用它进行整个磁盘的加密。实际用于文件系统加密的首先是128 AESCBC和ESSIV:SHA256。主键通过调用openssl库使用128bit AES加密。
Once it was decided to put the smarts in vold, it became obviousthat invoking the encryption features would be done like invokingother vold commands, by adding a new module to vold (calledcryptfs) and teaching it various commands. The commands arecheckpw, restart, enablecrypto, changepw and cryptocomplete. Theywill be described in more detail below.
这个功能添加到vold,很明显,调用加密功能将像调用其他vold命令,新的加密模块在vold(称作cryptfs)包含各种命令。命令有checkpw,restart, enablecrypto, changepw 和cryptocomplete.下面会详细说明。
The other big issue was how to get the password from the user onboot. The initial plan was to implement a minimal UI that could beinvoked from init in the initial ramdisk, and then init woulddecrypt and mount /data. However, the UI engineer said that was alot of work, and suggested instead that init communicate uponstartup to tell the framework to pop up the password entry screen,get the password, and then shutdown and have the real frameworkstarted. It was decided to go this route, and this then led to ahost of other decisions described below. In particular, init set aproperty to tell the framework to go into the special passwordentry mode, and that set the stage for much communication betweenvold, init and the framework using properties. The details aredescribed below.
另外的一个重要问题是如何在boot阶段得到密码,最初设计在ramdisk实现一个能够被init调用的轻量UI,并且初始化解密功能和mount/data,无论如何,这UI对工程师是大量的工作,取而代之的是init启动临时framework弹出password输入对话框来获得密码,然后停止临时framework并重新启动真的framework,这个方案的确定引出下面的设计。详细点说,init通过设置property告诉framework进入密码输入模式,然后作为和vold沟通的平台,init和framework之间使用properties。详细的之后描述。
Finally, there were problems around killing and restarting variousservices so that /data could be unmounted and remounted. Bringingup the temporary framework to get the user password requires that atmpfs /data filesystem be mounted, otherwise the framework will notrun. But to unmount the tmpfs /data filesystem so the realdecrypted /data filesystem could be mounted meant that everyprocess that had open files on the tmpfs /data filesystem had to bekilled and restarted on the real /data filesystem. This magic wasaccomplished by requiring all services to be in 1 of 3 groups:core, main and late_start. Core services are never shut down afterstarting. main services are shutdown and then restarted after thedisk password is entered. late_start services are not started untilafter /data has been decrypted and mounted. The magic to triggerthese actions is by setting the property vold.decrypt to variousmagic strings, which is described below. Also, a new init command"class_reset" was invented to stop a service, but allow it to berestarted with a "class_start" command. If the command "class_stop"was used instead of the new command "class_reset" the flagSVC_DISABLED was added to the state of any service stopped, whichmeans it would not be started when the command class_start was usedon its class.
最后,会涉及的问题有杀掉及重启不同的服务,/data会卸载再重新加载。启动一个临时framework来获得密码,需要mount/data为tmpfs,否则framework无法启动。但是当umount tmpfs/data然后mount实际的/data加密文件系统时,所有在tmpfs上打开文件的进程会被kill并重启到实际的/data文件系统上。这个逻辑需要所有服务属于1到3组:core,main, late_start.core服务在启动后不会停止,main服务会在输入完磁盘密码后停止并重启,late_start服务直到/data挂载并解密后才会启动。这个逻辑去触发动作是通过设置propertyvold.decrypt实现,描述在后边。同时,出现一个新的init命令"class_reset"负责停止一个服务,但是允许它通过"class_start"命令重启。如果用"class_stop"替代"class_reset",会在服务停止状态设置SVC_DISABLED标志,这意味着当服务所属class使用class_start时,它不会被启动。
When init fails to mount /data, it assumes the filesystem isencrypted, and sets several properties: ro.crypto.state ="encrypted" vold.decrypt = 1 It then mounts a /data on a tmpfsramdisk, using parameters it picks up from ro.crypto.tmpfs_options,which is set in init.rc.
If init was able to mount /data, it sets ro.crypto.state to"unencrypted".
In either case, init then sets 5 properties to save the initialmount options given for /data in these properties:ro.crypto.fs_type ro.crypto.fs_real_blkdev ro.crypto.fs_mnt_pointro.crypto.fs_options ro.crypto.fs_flags (saved as an ascii 8 digithex number preceded by 0x)
当init mount/data失败,它假定文件系统已经加密,并设置几个properties:ro.crypto.state = "encrypted"vold.decrypt = 1 表明/data被mount为tmpfsramdisk,使用init.rc设置的参数ro.crypto.tmpfs_options。
如果init能够mount/data,会设置ro.crypto.state ="unencrypted".
无论如何,对于/data,init会设置5个properties来保存初始mount选项:ro.crypto.fs_typero.crypto.fs_real_blkdev ro.crypto.fs_mnt_pointro.crypto.fs_options ro.crypto.fs_flags(0x开头的8个ascii字符的16进制数字)
The framework starts up, and sees that vold.decrypt is set to "1".This tells the framework that it is booting on a tmpfs /data disk,and it needs to get the user password. First, however, it needs tomake sure that the disk was properly encrypted. It sends thecommand "cryptfs cryptocomplete" to vold, and vold returns 0 ifencryption was completed successfully, or -1 on internal error, or-2 if encryption was not completed successfully. Vold determinesthis by looking in the crypto footer for theCRYPTO_ENCRYPTION_IN_PROGRESS flag. If it's set, the encryptionprocess was interrupted, and there is no usable data on the device.If vold returns an error, the UI should pop up a message saying theuser needs to reboot and factory reset the device, and give theuser a button to press to do so.
framework启动时会判断 vold.decrypt,如果设置成 "1",那么framework 知道 /data 被mount为tmpfs,必须询问用户密码。 首先,需要先要确定disk是否处于加密状态,通过给vold 发送命令 "cryptfs cryptocomplete",vold 返回0 表示加密状态,返回 -1 表示内部错误,-2 表示加密没有完成。Vold 通过cryptofooter的CRYPTO_ENCRYPTION_IN_PROGRESSflag来确定。如果它被设置了,那么表示加密过程被中断了,没有可用的存储设备。当vold 返回错误时,UI应该弹出 message 通知用户必须reboot 并 factoryreset 设备,没有别的选择。
Assuming the "cryptfs cryptocomplete" command returned success, theframework should pop up a UI asking for the disk password. The UIthen sends the command "cryptfs checkpw " to vold. If thepassword is correct (which is determined by successfully mountingthe decrypted at a temporary location, then unmounting it), voldsaves the name of the decrypted block device in the propertyro.crypto.fs_crypto_blkdev, and returns status 0 to the UI. If thepassword is incorrect, it returns -1 to the UI.
假设"cryptfs cryptocomplete" 命令返回成果,那么framework应该弹出UI询问用户磁盘密码,这个UI会发送命令"cryptfs checkpw " 给vold,如果密码正确(够成功mount磁盘到临时路径,然后再unmount),vold 保存加密块设备的名字到ro.crypto.fs_crypto_blkdev然后返回0 给 UI,如果密码错误返回 -1 给UI。
The UI puts up a crypto boot graphic, and then calls vold with thecommand "cryptfs restart". vold sets the property vold.decrypt to"trigger_reset_main", which causes init.rc to do "class_resetmain". This stops all services in the main class, which allows thetmpfs /data to be unmounted. vold then mounts the decrypted real/data partition, and then preps the new partition (which may neverhave been prepped if it was encrypted with the wipe option, whichis not supported on first release). It sets the propertyvold.post_fs_data_done to "0", and then sets vold.decrypt to"trigger_post_fs_dat". This causes init.rc to run the post-fs-datacommands in init.rc and init..rc. They will create any necessarydirectories, links, et al, and then set vold.post_fs_data_done to"1". Vold waits until it sees the "1" in that property. Finally,vold sets the property vold.decrypt to "trigger_restart_framework"which causes init.rc to start services in class main again, andalso start services in class late_start for the first time sinceboot.
Now the framework boots all its services using the decrypted /datafilesystem, and the system is ready for use.
UI显示一个加密启动画面,然后调用"cryptfsrestart",vold设置property vold.decrypt ="trigger_reset_main",关联 init.rc 的"class_reset main",会导致停止所有属于mainclass的 services,允许unmount tmpfs的/data,vold会mount被加密的真实/data分区,并做相应准备工作(如果通过wipe选项加密,data会被清除,需要重新加载数据。不支持首次启动)。然后设置vold.post_fs_data_done ="0"和 vold.decrypt ="trigger_post_fs_dat", 它导致init.rc和init.
现在,framework已经使用加密的/data文件系统启动完成并准备好。
For first release, we only support encrypt in place, which requiresthe framework to be shutdown, /data unmounted, and then everysector of the device encrypted, after which the device reboots togo through the process described above. Here are the details:
首次发布时,我们只支持inplace加密,要求frameworkshutdown,/data unmount,然后加密每个区块,以上操作需要reboot设备之后开始。详细如下:
From the UI, the user selects to encrypt the device. The UI ensuresthat there is a full charge on the battery, and the AC adapter isplugged in. It does this to make sure there is enough power tofinish the encryption process, because if the device runs out ofpower and shuts down before it has finished encrypting, file datais left in a partially encrypted state, and the device must befactory reset (and all data lost).
Once the user presses the final button to encrypt the device, theUI calls vold with the command "cryptfs enablecryptoinplace "where passwd is the user's lock screen password.
用户从UI选择加密设备,UI确认电池电量充足并且插入电源, 以确保有足够电量完成加密过程,因为如果设备在加密过程完成前因为没电而关机,数据会停留在一个不完整的加密状态,设备必须进行factoryreset(所有数据将丢失)。vold does some error checking, and returns -1 if it can't encrypt,and prints a reason in the log. If it thinks it can, it sets theproperty vold.decrypt to "trigger_shutdown_framework". This causesinit.rc to stop services in the classes late_start and main. voldthen unmounts /mnt/sdcard and then /data.
vold会进行一些错误检查,返回 -1代表不能加密并输出包含原因的log,如果确定可以加密,它会设置vold.decrypt="trigger_shutdown_framework",将触发init.rc停止属于late_start和main的服务进程。vold会unmount/mnt/sdcard 和 /dataIf doing an inplace encryption, vold then mounts a tmpfs /data(using the tmpfs options from ro.crypto.tmpfs_options) and sets theproperty vold.encrypt_progress to "0". It then preps the tmpfs/data filesystem as mentioned in step 3 for booting an encryptedsystem, and then sets the property vold.decrypt to"trigger_restart_min_framework". This causes init.rc to start themain class of services. When the framework sees thatvold.encrypt_progress is set to "0", it will bring up the progressbar UI, which queries that property every 5 seconds and updates aprogress bar.
如果进行一个inplace加密,vold会mount/data成为tmpfs(使用tmpfs选项从 ro.crypto.tmpfs_options )并且设置vold.encrypt_progress="0",为了启动一个能够进行加密系统,它会准备tmpfs的/data在这步,并且设置 vold.decrypt= "trigger_restart_min_framework",这将触发init.rc启动mainclass的服务进程,当framework看到 vold.encrypt_progress设置为 "0",会启动显示进度的UI,每5s刷新一次进度。vold then sets up the crypto mapping, which creates a virtualcrypto block device that maps onto the real block device, butencrypts each sector as it is written, and decrypts each sector asit is read. vold then creates and writes out the crypto footer.
The crypto footer contains details on the type of encryption, andan encrypted copy of the master key to decrypt the filesystem. Themaster key is a 128 bit number created by reading from/dev/urandom. It is encrypted with a hash of the user passwordcreated with the PBKDF2 function from the SSL library. The footeralso contains a random salt (also read from /dev/urandom) used toadd entropy to the hash from PBKDF2, and prevent rainbow tableattacks on the password. Also, the flagCRYPT_ENCRYPTION_IN_PROGRESS is set in the crypto footer to detectfailure to complete the encryption process. See the file cryptfs.hfor details on the crypto footer layout. The crypto footer is keptin the last 16 Kbytes of the partition, and the /data filesystemcannot extend into that part of the partition.
之后vold设置加密mapping,它创建了一个虚拟的加密块设备映射到实际的块设备,但加密是一块块写入,解密是一块块读取,vold会创建并写入cryptofooterIf told was to enable encryption with wipe, vold invokes thecommand "make_ext4fs" on the crypto block device, taking care tonot include the last 16 Kbytes of the partition in thefilesystem.
If the command was to enable inplace, vold starts a loop to readeach sector of the real block device, and then write it to thecrypto block device. This takes about an hour on a 30 Gbytepartition on the Motorola Xoom. This will vary on other hardware.The loop updates the property vold.encrypt_progress every time itencrypts another 1 percent of the partition. The UI checks thisproperty every 5 seconds and updates the progress bar when itchanges.
如果是wipe模式的加密,vold会调用命令"make_ext4fs" 在这个加密块设备,分区上不会包含那最后的16Kbytes。When either encryption method has finished successfully, voldclears the flag ENCRYPTION_IN_PROGRESS in the footer, and rebootsthe system. If the reboot fails for some reason, vold sets theproperty vold.encrypt_progress to "error_reboot_failed" and the UIshould display a message asking the user to press a button toreboot. This is not expected to ever occur.
当各种加密全部成功时,vold会清除 footer 上的 ENCRYPTION_IN_PROGRESS标志,然后reboot系统,如果因为某些原因reboot失败,vold会设置 vold.encrypt_progress="error_reboot_failed" ,UI告知用户强行reboot,这不是预期会发生的情况。If vold detects an error during the encryption process, and if nodata has been destroyed yet and the framework is up, vold sets theproperty vold.encrypt_progress to "error_not_encrypted" and the UIshould give the user the option to reboot, telling them that theencryption process never started. If the error occurs after theframework has been torn down, but before the progress bar UI is up,vold will just reboot the system. If the reboot fails, it setsvold.encrypt_progress to "error_shutting_down" and returns -1, butthere will not be anyone to catch the error. This is not expectedto happen.
If vold detects an error during the encryption process, it setsvold.encrypt_progress to "error_partially_encrypted" and returns-1. The UI should then display a message saying the encryptionfailed, and provide a button for the user to factory reset thedevice.
如果vold 在加密过程中遇到错误,没有数据遭到破坏并且framework在运行,vold会设置vold.encrypt_progress ="error_not_encrypted" ,UI提示用户reboot并告知加密没有进行。如果错误发生在framework停止之后,但是在UI进度条运行之前,vold会reboot系统,如果reboot失败,会设置 vold.encrypt_progress= "error_shutting_down" 并返回 -1,但不会有谁捕捉这个错误,这不是预期发生的。To change the password for the disk encryption, the UI sends thecommand "cryptfs changepw " to vold, and voldre-encrypts the disk master key with the new password.
改变磁盘加密的密码时,UI发送命令"cryptfs changepw" 给vold,vold用disk master key重新加密。
Here is a table summarizing the various properties, their possiblevalues, and what they mean:
以下表格是关联properties、键值及描述:
vold.decrypt 1 Set by init to tell the UI to ask
for the disk pw
vold.decrypt trigger_reset_main Set by vold to shutdown the UI
asking for the disk password
vold.decrypt trigger_post_fs_data Set by vold to prep /data with
necessary dirs, et al.
vold.decrypt trigger_restart_framework Set by vold to start the real
framework and all services
vold.decrypt trigger_shutdown_framework Set by vold to shutdown the full
framework to start encryption
vold.decrypt trigger_restart_min_framework Set by vold to start the progress
bar UI for encryption.
vold.enrypt_progress When the framework starts up, if
this property is set, enter the
progress bar UI mode.
vold.encrypt_progress 0 to100 The progress bar UI should display
the percentage value set.
vold.encrypt_progress error_partially_encrypted The progress bar UIshould
display a message that the
encryption failed, and give
the user an option to factory
reset the device.
vold.encrypt_progress error_reboot_failed The progress bar UI should display
a message saying encryption
completed, and give the user a
button to reboot the device.
This error is not expected to
happen.
vold.encrypt_progress error_not_encrypted The progress bar UI should display
a message saying an error occured,
and no data was encrypted or lost,
and give the user a button to
reboot the system.
vold.encrypt_progress error_shutting_down The progress bar UI is not
running, so it's unclear who
will respond to this error,
and it should never happen
anyway.
vold.post_fs_data_done 0 Set by vold just before setting
vold.decrypt to
trigger_post_fs_data.
vold.post_fs_data_done 1 Set by init.rc orinit.
just after finishing the task
post-fs-data.
ro.crypto.fs_crypto_blkdev Set by the vold command checkpw
for later use by the vold command
restart.
ro.crypto.stateunencrypted Set by init to say this system is
running with an unencrypted /data
ro.crypto.stateencrypted Set by init to say this system is
running with an encrypted /data
ro.crypto.fs_type These 5 properties are set by init
ro.crypto.fs_real_blkdev when it tries to mount /data with
ro.crypto.fs_mnt_point parameters passed in from init.rc.
ro.crypto.fs_options vold uses these to setup the
ro.crypto.fs_flags crypto mapping.
ro.crypto.tmpfs_options Set by init.rc with the options
init should use when mounting
the tmpfs /data filesystem
A list of the new Actions that are added to init.rc and/orinit..rc:
增加到init.rcinit.
on post-fs-data
on nonencrypted
on property:vold.decrypt=trigger_reset_main
on property:vold.decrypt=trigger_post_fs_data
on property:vold.decrypt=trigger_restart_min_framework
on property:vold.decrypt=trigger_restart_framework
onproperty:vold.decrypt=trigger_shutdown_framework