2023年3月9日 星期四

A-GPS control plane / user plane

https://en.wikipedia.org/wiki/Assisted_GNSS

 A-GPS protocols are part of Positioning Protocol defined by two different standardization bodies, 3GPP and Open Mobile Alliance (OMA).

Control Plane Protocol
Defined by the 3GPP for various generations of mobile phone systems. These protocols are defined for circuit switched networks. The following positioning protocols have been defined.
  • RRLP – 3GPP defined RRLP (Radio Resource Location Protocol) to support positioning protocol on GSM networks.
  • TIA 801 – CDMA2000 family defined this protocol for CDMA 2000 networks.
  • RRC position protocol – 3GPP defined this protocol as part of the RRC standard for UMTS network.
  • LPP – 3GPP defined LPP or LTE positioning protocol for LTE networks.
User Plane Protocol
Defined by the OMA to support positioning protocols in packet switched networks. Three generations of Secure User Plane Location (SUPL) protocol have been defined, from version 1.0 to 3.0.

SUPL[edit]

The SUPL (Secure User Plane Location) protocol, unlike its control-plane equivalents restricted to mobile networks, runs on the Internet's TCP/IP infrastructure. As a result, its application goes beyond the original intended use of mobile devices and may be used by general-purpose computers.[5] SUPL 3.0 legitimizes such use by adding allowance for WLAN and broadband connections.[6]

Actions defined by SUPL 3.0 include a wide range of services such as geofencing and billing. The A-GNSS functions are defined in the SUPL Positioning Functional Group. It includes:[6]

  • SUPL Assistance Delivery Function (SADF), which provides the basic information sent to the device in both A-GNSS modes.
  • SUPL Reference Retrieval Function (SRRF), which tells the server to prepare the information mentioned above by receiving from the satellites.
  • SUPL Position Calculation Function (SPCF), which lets the client or the server ask for the client's location. The server-generated location may result from MSA or from mobile cell. If a MSB (SET based) mode is used, the client reports its location to the server instead.

The specifics of communication is defined in the ULP (Userplane Location Protocol) substandard of SUPL suite. As of December 2018, GNSS systems supported include GPS, Galileo, GLONASS, and BeiDou.

2023年3月8日 星期三

"IMS/IPME (IMS IP Messaging) ITU recommendation A.76" by ChatGPT

 IMS/IPME (IMS IP Messaging) is a communication service defined by the International Telecommunication Union (ITU) recommendation A.76. It provides an end-to-end messaging service that allows users to exchange text, multimedia, and other content in real-time using IP-based networks.

The IMS/IPME service is designed to work within the context of the IP Multimedia Subsystem (IMS), which is a standardized architecture for multimedia services over IP networks. The IMS provides a framework for real-time multimedia communication between various devices, including smartphones, tablets, and computers.

IMS/IPME messaging service is based on the Session Initiation Protocol (SIP), which is a protocol used to set up and control multimedia communication sessions. SIP enables the establishment of multimedia sessions, including voice, video, and messaging, over IP networks. The IMS also includes a variety of other protocols, including the Diameter protocol for authentication and authorization and the Session Description Protocol (SDP) for describing multimedia sessions.

IMS/IPME messaging supports a range of multimedia content, including text, pictures, videos, and audio. It also supports group messaging, which allows users to communicate with multiple users simultaneously. IMS/IPME messaging service can be used to provide a range of messaging applications, including instant messaging, chat, and multimedia messaging.

IMS/IPME messaging has several advantages over traditional messaging services. It is designed to work over IP networks, which are becoming more widespread and offer faster and more reliable connectivity than traditional cellular networks. IMS/IPME also provides enhanced messaging features, including support for multimedia content, group messaging, and real-time delivery notifications.

In conclusion, IMS/IPME (IMS IP Messaging) is a standardized communication service that provides an end-to-end messaging service using IP-based networks. It is designed to work within the context of the IP Multimedia Subsystem (IMS) and supports a range of multimedia content and messaging applications. IMS/IPME messaging service is based on the Session Initiation Protocol (SIP) and provides enhanced messaging features, including support for multimedia content, group messaging, and real-time delivery notifications.

"RCS" and "ITU recommendation A.67" by ChatGPT

RCS stands for "Rich Communication Services," which is a communication protocol that enables enhanced messaging and communication services on mobile devices beyond traditional SMS and MMS. RCS allows for richer media types, such as high-resolution images, videos, and audio files, to be sent over cellular networks, along with features such as read receipts, typing indicators, and group messaging.

RCS also allows for the integration of additional services, such as payment and location-sharing, within the messaging application, making it a more versatile communication tool.

ITU recommendation A.67, on the other hand, is a technical specification document issued by the International Telecommunication Union (ITU) that outlines the technical requirements for implementing RCS on mobile networks. The document defines the necessary interfaces, protocols, and parameters that must be implemented in the mobile network to enable RCS services.

ITU recommendation A.67 also provides guidelines for implementing various RCS features, such as messaging, file transfer, and presence, and specifies how these features should be integrated with the existing network infrastructure. The document helps ensure that RCS services can be deployed effectively and efficiently across different mobile networks, making it easier for users to communicate and access additional services through their messaging app. 

"IPv6 - A66" by ChatGPT

 IPv6 (Internet Protocol version 6) is the latest version of the Internet Protocol, which provides the addressing and routing of data packets across the internet. It was developed as a successor to IPv4 (Internet Protocol version 4), which has been the dominant protocol used for over 30 years.

The primary motivation for the development of IPv6 was the exhaustion of IPv4 addresses, which were based on 32-bit numbers and were limited to approximately 4.3 billion unique addresses. IPv6, on the other hand, uses 128-bit addresses, which allow for an almost infinite number of unique addresses. This will enable the continued growth and expansion of the internet, as well as the deployment of new services and devices that require unique addresses.

One notable feature of IPv6 is its simplified header structure compared to IPv4. IPv6 headers are fixed in size and do not require checksums, which simplifies processing and reduces the overhead on network devices. IPv6 also supports features such as multicast addressing, anycast addressing, and flow labeling, which can improve the efficiency and flexibility of network communication.

The "A66" in your question is unclear, as it does not appear to be related to IPv6. However, it is possible that it may refer to a specific implementation or application of IPv6 in a particular context. Without more information, it is difficult to provide a more detailed explanation.


ITU reference A.66

ITU-T (International Telecommunication Union - Telecommunication Standardization Sector) is the United Nations specialized agency responsible for developing and coordinating international telecommunications standards. ITU-T Recommendation A.66 is a recommendation that provides guidance on the allocation and use of the international numbering resources.

In particular, ITU-T Recommendation A.66 provides guidance on the allocation of international numbering resources to ensure that they are used efficiently and effectively. It also provides guidance on the administration of international numbering resources, including the responsibilities of national and international organizations in managing these resources.

The recommendation covers a range of topics related to international numbering resources, including the principles of numbering, the structure of international numbering plans, the allocation of numbers to different services, and the management of numbering resources in the event of mergers or acquisitions.

ITU-T Recommendation A.66 is an important reference for telecommunication regulatory authorities, network operators, and other stakeholders involved in the management and allocation of international numbering resources. It is regularly updated to reflect changes in the telecommunications industry and evolving best practices for the management of international numbering resources.

"UCE (User Capabilities Exchange) - A63" by ChatGPT

 The User Capabilities Exchange (UCE) protocol is a standard proposed by the International Telecommunication Union (ITU) under the reference A.63. It is used to facilitate the exchange of information between two communication endpoints to establish a communication session.

In simple terms, UCE enables two communicating endpoints to exchange information about their capabilities before establishing a communication session. This information includes various parameters such as audio and video codecs, frame rate, resolution, and other relevant settings required to establish a successful session.

The UCE protocol allows endpoints to negotiate various session parameters based on their capabilities. For instance, if one endpoint has a low bandwidth connection, it may not support high-definition video streams. The protocol enables the endpoint to inform the other endpoint about its capabilities so that they can negotiate and agree on appropriate parameters that both can support.

The UCE protocol typically runs in the signaling layer of a communication network and is responsible for exchanging capabilities between endpoints during the signaling phase of a session. It uses Session Description Protocol (SDP) to describe and exchange the capabilities of the endpoints.

In summary, the User Capabilities Exchange protocol (UCE) provides a standard mechanism for communication endpoints to exchange information about their capabilities, enabling them to negotiate and establish successful communication sessions.

"LTE Positioning Protocol (LPP)" elaborated by ChatGPT

 LTE (Long-Term Evolution) Positioning Protocol is a protocol used to determine the location of a mobile device within a cellular network. The protocol provides location information by using various measurements taken by the mobile device and the network.

Here are some of the location information that LTE Positioning Protocol carries:

  1. Time Difference of Arrival (TDOA): This measurement is used to calculate the distance between the mobile device and multiple base stations. The time difference between the arrival of the signal at different base stations is used to calculate the location of the mobile device.

  2. Observed Time Difference of Arrival (OTDOA): This measurement is similar to TDOA but uses the time difference between the mobile device and the base station instead of between multiple base stations.

  3. Cell Global Identity (CGI): This is a unique identifier for each cell in a cellular network. The CGI can be used to identify the cell where the mobile device is located.

  4. Reference Signal Received Power (RSRP): This measurement is the received signal power from the base station. The strength of the signal can be used to estimate the distance between the mobile device and the base station.

  5. Reference Signal Received Quality (RSRQ): This measurement is the quality of the received signal from the base station. The quality of the signal can be used to estimate the distance between the mobile device and the base station.

  6. Serving Cell Identifier (SCID): This is a unique identifier for the cell where the mobile device is currently connected.

  7. Cell ID: This is a unique identifier for the cell where the mobile device is currently located.

  8. Global Navigation Satellite System (GNSS) Information: This information includes the latitude, longitude, and altitude of the mobile device, which is obtained from GPS or other satellite-based navigation systems.

Overall, LTE Positioning Protocol combines various measurements to provide accurate location information of the mobile device within the cellular network.

2022年10月6日 星期四

TrustZone

 Source https://www.twblogs.net/a/5bddf4012b717720b51b1916

瞭解TrustZone,讀了這篇就夠了

這篇文章源於老闆想了解TrustZone,要求我寫一篇文章簡單介紹TrustZone的原理。既然是給領導看的,只介紹原理哪裏夠,因此也添加了公司自己現有TEE環境的設計、實現和發展,也順帶加入了一些題外話。也是因爲要給領導看,所以文章也不能涉及太多技術細節,包括TrustZone模塊的詳細設計以及示例代碼等,所以只從總體上講解了什麼是TrustZone,TrustZone是如何實現安全隔離的、TrustZone相關的一些資源等。

如果你之前對TrustZone亦無所知,好吧,本文或許值得你一看;如果你已經瞭解了TrustZone,想知道更多的實現細節,抱歉,本文並不適合你,或許閱讀ARM官方網站和文檔以及各開源項目源碼是更好的選擇。

本文先交代TrustZone的安全背景,然後從較高層次展開介紹TrustZone的工作機制和原理(包括AXI總線架構、CPU、內存和中斷模型,以及安全隔離機制),列舉了幾個常見ARM平臺上的實現以及當前博通ARM平臺上的狀況,最後附帶一些TrustZone相關的開源項目以及其他資源鏈接,全文約7500字。(由於涉及安全的原因,本文已經刪掉介紹公司自己平臺相關的部分)。

本文內容主要來源於網絡,綜合了網上的多篇文章,也加入了一些自己的理解,重新組織了文章結構使其便於理解。

主要參考的文章包括:

本文還參考了貼吧、知乎等部分文章,由於涉及較多,無法一一列舉,再次對原作者的付出一併表示感謝!

除上面列舉的資源外,本文主要資料參考了ARM官方對TrustZone的介紹,主要有:

事實上,前面多篇文章的細節也來源於官方文檔。 本人不保留本文的所有權,歡迎轉載本文,讓更多的人來了解TrustZone。由於不想再次以類似 《TrustZone原理介紹》 一類作爲標題,但又不知道以什麼作爲標題貼切,所以隨手用了現在標題黨的套路,抱歉。

1. TrustZone介紹

1.1 安全背景

在介紹TrustZone前有必要簡單回顧下目前的一些安全手段。

CPU通過內存映射手段給每個進程營造一個單獨的地址空間來隔離多個進程的代碼和數據,通過內核空間和用戶空間不同的特權級來隔離操作系統和用戶進程的代碼和數據。但由於內存中的代碼和數據都是明文,容易被同處於內存中的其它應用偷窺,因此出現了擴展的安全模塊,應用將加密數據送往安全模塊,由安全模塊處理完後再返回結果給相應的應用。

很多消費電子設備都使用擴展的安全模塊來確保數據安全,目前常見的方式有:

  1. 外部掛接硬件安全模塊

    數據的處理交由外部的安全模塊實現,這些模塊能夠保護自己的資源和密鑰等數據的安全,如SIM卡、各種智能卡或連接到外部的硬件加解密模塊等,但其同主芯片的通信線路暴露在外部,容易被監聽破解。另外,通信的速率比較低。

  2. 內部集成硬件安全模塊

    將外部安全模塊的功能集成到芯片內,因此一個芯片上至少有兩個核:一個普通核和一個安全核。優點是核與核之間的通信在芯片內部實現,不再暴露在外面。缺點是核之間的通信速度仍然較低,而且單獨的安全核性能有限,還會會佔用SoC面積,成本較高。

1.2 TrustZone是個什麼鬼?

TrustZone是ARM針對消費電子設備設計的一種硬件架構,其目的是爲消費電子產品構建一個安全框架來抵禦各種可能的攻擊。

TrustZone在概念上將SoC的硬件和軟件資源劃分爲安全(Secure World)和非安全(Normal World)兩個世界,所有需要保密的操作在安全世界執行(如指紋識別、密碼處理、數據加解密、安全認證等),其餘操作在非安全世界執行(如用戶操作系統、各種應用程序等),安全世界和非安全世界通過一個名爲Monitor Mode的模式進行轉換,如圖1:

ARM的安全世界和非安全世界

圖1. ARM的安全世界和非安全世界

處理器架構上,TrustZone將每個物理核虛擬爲兩個核,一個非安全核(Non-secure Core, NS Core),運行非安全世界的代碼;和另一個安全核(Secure Core),運行安全世界的代碼。

兩個虛擬的核以基於時間片的方式運行,根據需要實時佔用物理核,並通過Monitor Mode在安全世界和非安全世界之間切換,類似同一CPU下的多應用程序環境,不同的是多應用程序環境下操作系統實現的是進程間切換,而Trustzone下的Monitor Mode實現了同一CPU上兩個操作系統間的切換。

AMBA3 AXI(AMBA3 Advanced eXtensble Interface)系統總線作爲TrustZone的基礎架構設施,提供了安全世界和非安全世界的隔離機制,確保非安全核只能訪問非安全世界的系統資源,而安全核能訪問所有資源,因此安全世界的資源不會被非安全世界(或普通世界)所訪問。

設計上,TrustZone並不是採用一刀切的方式讓每個芯片廠家都使用同樣的實現。總體上以AMBA3 AXI總線爲基礎,針對不同的應用場景設計了各種安全組件,芯片廠商根據具體的安全需求,選擇不同的安全組件來構建他們的TrustZone實現。

其中主要的組件有:

  • 必選組件

    • AMBA3 AXI總線,安全機制的基礎設施
    • 虛擬化的ARM Core,虛擬安全和非安全核
    • TZPC (TrustZone Protection Controller),根據需要控制外設的安全特性
    • TZASC (TrustZone Address Space Controller),對內存進行安全和非安全區域劃分和保護
  • 可選組件

    • TZMA (TrustZone Memory Adapter),片上ROM或RAM安全區域和非安全區域的劃分和保護
    • AXI-to-APB bridge,橋接APB總線,配合TZPC使APB總線外設支持TrustZone安全特性

除了以上列出的組件外,還有諸如 Level 2 Cache Controller, DMA Controller, Generic Interrupt Controller等。

邏輯上,安全世界中,安全系統的OS提供統一的服務,針對不同的安全需求加載不同的安全應用TA(Trusted Application)。 例如:針對某具體DRM的TA,針對DTCP-IP的TA,針對HDCP 2.0驗證的TA等。

圖2是一個ARM官網對TrustZone介紹的應用示意圖:

基於TrustZone的應用示意圖

圖2. 基於TrustZone的應用示意圖

圖中左邊藍色部分Rich OS Application Environment(REE)表示用戶操作環境,可以運行各種應用,例如電視或手機的用戶操作系統,圖中右邊綠色部分Trusted Execution Envrionment(TEE)表示系統的安全環境,運行Trusted OS,在此基礎上執行可信任應用,包括身份驗證、授權管理、DRM認證等,這部分隱藏在用戶界面背後,獨立於用戶操作環境,爲用戶操作環境提供安全服務。

可信執行環境(TEE, Trusted Execution Environment)是Global Platform(GP)提出的概念。對應於TEE還有一個REE(Rich Execution Environment)概念,分別對應於安全世界(Secure World)和非安全世界(Non-secure World, Normal World)。

GlobalPlatform(GP)是跨行業的國際標準組織,致力於開發、制定併發布安全芯片的技術標準,以促進多應用產業環境的管理 及其安全、可互操作的業務部署。目標是創建一個標準化的基礎架構, 加快安全應用程序及其關聯資源的部署,如數據和密鑰,同時保護安全應用程序及其關聯資源免受軟件方面的攻擊。

2. TrustZone原理和設計

以下主要從TrustZone的總線設計,CPU設計(包括處理器模型、內存模型和中斷模型)和安全隔離機制來介紹TrustZone的設計和工作原理。

2.1 總線設計

  • 總線

設計上,TrustZone 在系統總線上針對每一個信道的讀寫增加了一個額外的控制信號位,這個控制位叫做Non-Secure或者NS位,是AMBA3 AXI總線針對TrustZone作出的最重要、最核心的擴展設計。

這個控制信號針對讀和寫分別叫做ARPORT[1]和AWPORT[1]:

  • ARPROT[1]: 用於讀操作(Read transaction), 低表示Secure, 高表示Non-Secure
  • AWPROT[1]: 用於寫操作(Write transaction), 低表示Secure,高表示Non-Secure

總線上的所有主設備(master)在發起新的操作(transaction)時會設置這些信號,總線或從設備(slave)上解析模塊會對主設備發起的信號進行辨識,來確保主設備發起的操作在安全上沒有違規。

例如:硬件設計上,所有非安全世界的主設備(Non-Secure masters)在操作時必須將信號的NS位置高,而NS位置高又使得其無法訪問總線上安全世界的從設備(Secure Slaves),簡單來說就是對非安全世界主設備發出的地址信號進行解碼時在安全世界中找不到對應的從設備,從而導致操作失敗。

NS控制信號在AMBA3 AXI總線規範中定義。可以將其看作爲原有地址的擴展位,如果原有32爲尋址,增加NS可以看成是33位尋址,其中一半的32位物理尋址位於安全世界,另一半32位物理尋址位於非安全世界。

當然,非安全世界的主設備嘗試訪問安全世界的從設備會引發訪問錯誤,可能是SLVERR(slave error)或者DECERR(decode error),具體的錯誤依賴於其訪問外設的設計或系統總線的配置。

  • 外設

在TrustZone出現前,ARM的外設基於AMBA2 APB (Advanced Peripheral Bus)總線協議,但是APB總線上不存在類似AXI總線上的NS控制位。爲了兼容已經存在的APB總線設計,AMBA3規範中包含了AXI-to-APB bridge組件,這樣就確保基於AMBA2 APB的外設同AMBA3 AXI的系統兼容。AXI-to-APB bridge負責管理APB總線設備的安全事宜,其會拒絕不合理的安全請求,保證這些請求不會被轉發到相應的外設。

例如:新一代的芯片可以通過增加AXI-to-APB bridge組件來沿用上一代芯片的設計來使其外圍設備可以支持TrustZone。

2.2 處理器設計

2.2.1 處理器模型

TrustZone中,每個物理處理器核被虛擬爲一個安全核(Secure)和一個非安全核(Non-Secure),安全核運行安全世界的代碼,非安全核運行除安全世界外的其它代碼。由於安全世界和非安全世界的代碼採用時間片機制輪流運行在同一個物理核上,相應的節省了一個物理處理器核。

多核處理器上,也有建議說讓將某一個或幾個核指定爲安全專用核,只運行安全系統代碼來構建安全世界,其餘核運行非安全代碼,暫不清楚目前有哪些平臺採用這個實現。

圖3中,系統有4個物理核,每個又分爲兩個虛擬核(安全核和非安全核)的情況:

多核處理器上的安全核和非安全核

圖3. 多核處理器上的安全核和非安全核

2.2.2 L1內存模型

  • MMU

MMU是一種硬件電路,它包含兩類部件,一類是分段部件,一類是分頁部件,對應於內存管理的分段機制和分頁機制。分段機制把一個邏輯地址轉換爲線性地址;接着,分頁機制把一個線性地址轉換爲物理地址。

當CPU訪問一個虛擬地址時,這個虛地址被送到MMU翻譯,硬件首先把它和TLB中的所有條目同時(並行地)進行比較,如果它的虛頁號在TLB中,並且訪問沒有違反保護位,它的頁面會直接從TLB中取出而不去訪問頁表,從而提高地址轉換的效率。

安全世界和非安全世界都有自己的虛擬MMU,各自管理物理地址的映射。實際上只是兩個世界都有一份TTBR0、TTBR1、TTBCR寄存器,因此就會對應兩個MMU表。

儘管MMU有兩套,但TBL緩存硬件上只有一套,因此TBL對於兩個世界來說是共享的,其通過NS位來標誌其每一項具體屬於哪一個世界。這樣在兩個世界間進行切換時不再需要重新刷新TLB,提高執行效率。

對於TLB共享並不是硬性規定的,部分芯片在兩個世界間切換時可能通過硬件部分或全部刷新TLB。

  • Cache

同TLB類似,硬件上兩個世界共享一套Cache,具體的Cache數據屬於哪一個世界也由其NS位指定,在世界間切換也不需要刷新Cache。

2.2.3 中斷模型

基於TrustZone的處理器有三套異常向量表:

  • 一套用於非安全世界,
  • 一套用於安全世界,
  • 還有一套用於Monitor模式。

與之前非TrustZone的處理器不同的是,這三套中斷向量表的基地址在運行時可以通過CP15的寄存器VBAR(Vector Base Address Register)進行修改。

復位時,安全世界的中斷向量表由處理器的輸入信號VINITHI決定,沒有設置時爲0x00000000,有設置時爲0xFFFF0000;非安全世界和Monitor模式的中斷向量表默認沒有設置,需要通過軟件設置後才能使用。

默認情況下,IRQ和FIQ異常發生後系統直接進入Monitor模式,由於IRQ是絕大多數環境下最常見的中斷源,因此ARM建議配置IRQ作爲非安全世界的中斷源,FIQ作爲安全世界的中斷源。這樣配置有兩個優點:

  • 當處理器運行在非安全世界時,IRQ直接進入非安全世界的處理函數;如果處理器運行在安全世界,當IRQ發生時,會先進入到Monitor模式,然後跳到非安全世界的IRQ處理函數執行
  • 僅將FIQ配置爲安全世界的中斷源,而IRQ保持不變,現有代碼僅需做少量修改就可以滿足

將IRQ設置爲非安全世界的中斷源時系統IRQ的切換見圖4:

IRQ作爲非安全世界的中斷源

圖4. IRQ作爲非安全世界的中斷源

2.2.4 系統模式切換

基於TrustZone的系統有三種狀態,安全世界、非安全世界和用於二者切換的Monitor Mode。

協處理器CP15的寄存器SCR(Secure Configuration Register)有一個NS位用於指示當前處理器位於哪一個世界,該寄存器在非安全世界是不能訪問的。當CPU處於Monitor Mode時,無論NS位是0還是1,處理器都是在安全世界運行代碼。因此Monitor Mode下總是安全世界,但如果此時NS爲1,訪問CP15的其它寄存器獲取到的是其在非安全世界的值。

非安全世界到Monitor模式的切換

處理器從非安全世界進入Monitor Mode的操作由系統嚴格控制,而且所有這些操作在Monitor Mode看來都屬於異常。 從非安全世界到Monitor Mode的操作可通過以下方式觸發:

  • 軟件執行SMC (Secure Monitor Call)指令
  • 硬件異常機制的一個子集(換而言之,並非所有硬件異常都可以觸發進入Monitor Mode),包括:
    • IRQ
    • FIQ
    • external Data Abort
    • external Prefetch Abort

Monitor Mode

Monitor Mode內執行的代碼依賴於具體的實現,其功能類似於進程切換,不同的是這裏是不同模式間CPU狀態切換。

軟件在Monitor Mode下先保存當前世界的狀態,然後恢復下一個世界的狀態。操作完成後以從異常返回的方式開始運行下一個世界的代碼。

爲什麼安全模式和非安全模式不能直接切換?

非安全世界無權訪問CP15的SCR寄存器,所以無法通過設置NS來直接切換到安全世界,只能先轉換到Monitor Mode,再到安全世界。

如果軟件運行在安全世界(非Monitor Mode)下,通過將CP15的NS位置1,安全世界可以直接跳轉到非安全世界,由於此時CPU的流水線和寄存器還遺留了安全世界的數據和設置,非安全模式下的應用可以獲取到這些數據,會有極大的安全風險。因此,只建議在Monitor Mode下通過設置NS位來切換到非安全模式。

綜上,安全世界和非安全世界不存在直接的切換,所有切換操作都通過Monitor Mode來執行。

圖5展現了安全世界和非安全世界之間的切換方式:

安全世界和非安全世界之間的切換

圖5. 安全世界和非安全世界之間的切換

2.3 隔離機制

除了CPU執行時實行安全世界和非安全世界的隔離外,AMBA3 AXI總線提供了外設隔離的基礎。

2.3.1 內存隔離機制

這裏的內存指外部的DDR和片上的ROM以及SRAM,其隔離和保護通過總線組件TZASC和TZMA的設置來實現。

  • TZASC (TrustZone Address Space Controller)
    • TZASC可以把外部DDR分成多個區域,每個區域可以單獨配置爲安全或非安全區域,非安全世界的代碼和應用只能訪問非安全區域。TZASC只能用於內存設備,不適合用於配置塊設備,如Nand Flash。
  • TZMA (TrustZone Memory Adapter)
    • TZMA可以把片上ROM和SRAM隔離出安全和非安全區域。TZMA最大可以將片上存儲的低2MB配置爲安全區域,其餘部分配置爲非安全區域。大小劃分上,片上安全區域可以在芯片出廠前設置爲固定大小,或運行時通過TZPC動態配置。TZMA使用上有些限制,其不適用於外部內存劃分,而且也只能配置一個安全區域。

2.3.2 外設隔離機制

外設上,基於APB總線的設備不支持AXI總線的NS控制信號,所以AXI到APB總線需要AXI-to-APB bridge設備連接,除此之外,還需要TZPC (TrustZone Protection Controller) 來向APB總線上的設備提供類似AXI上的NS控制信號。

由於TZPC可以在運行時動態設置,這就決定了外設的安全特性是動態變化的,例如鍵盤平時可以作爲非安全的輸入設備,在輸入密碼時可以配置爲安全設備,只允許安全世界訪問。

2.3.3 隔離機制示意圖

整個系統內存和外設隔離機制示意圖見圖6.

系統內存和外設隔離機制示意圖

圖6. 系統內存和外設隔離機制示意圖

此圖來源於網上,實際上TZPC還連接到片內的ROM/RAM設備上,用於配置片上存儲的安全區域。

2.4 安全啓動

AMBA3 AXI總線機制隔離出安全世界和非安全世界,但這是系統啓動之後的事情。如何確保系統本身是安全的呢?這就涉及到系統啓動的過程。

系統上電覆位後,先從安全世界開始執行。安全世界會對非安全世界的bootloader進行驗證,確保非安全世界執行的代碼經過授權而沒有被篡改過。然後非安全世界的bootloader會加載非安全世界的OS,完成整個系統的啓動。

在非安全系統的bootloader加載OS時,仍然需要安全世界對OS的代碼進行驗證,確保沒有被篡改。

圖7是典型的TrustZone芯片的啓動流程:

典型的TruestZone芯片啓動流程

圖7. 典型的TruestZone芯片啓動流程

整個啓動流程跟目前博通平臺的安全啓動原理基本一致,上電後安全芯片先啓動,然後校驗主芯片的bootloader,接下來bootloader提交系統的OS和文件系統給BSP進行校驗,通過後加載主系統,確保主系統是安全的。

從上電覆位開始的整個啓動過程中,下一級的安全基於上一級的驗證,最終依賴於芯片內置的OTP和安全硬件,逐級的驗證構成了整個系統的信任鏈。信任鏈中的某一個環節被破壞,都會導致整個系統不安全。

3. 各家TrustZone實現

基於安全考慮,各家TrustZone都實行閉源,關於其實現細節的介紹都較少。

網上能找到少許關於高通方案上TrustZone的介紹:

  • 安全世界 QSEE (Qualcomm Secure Execution Environment)
  • 非安全世界 HLOS (High Level OS)

整個系統的架構如圖8:

image

圖8. 高通QSEE系統架構圖

4. 其它

  • ARMv8-A架構定義了四個異常等級,分別爲EL0到EL3,其中數字越大代表特權(privilege)越大:

    • EL0: 無特權模式(unprivileged)
    • EL1: 操作系統內核模式(OS kernel mode)
    • EL2: 虛擬機監視器模式(Hypervisor mode)
    • EL3: TrustZone monitor mode ARMv8-A Security Level
  • TrustZone設計的相關方

    • ARM公司,定義TrustZone並實現硬件設計,TEE,TZAPI等
    • 芯片廠家,在具體芯片上實現TrustZone設計,包括三星、高通、MTK、TI、ST、華爲等
    • 應用提供方,如DRM廠家和安全應用開發商,實現DRM、Playready、DTCP-IP和一些其它安全應用開發和認證
  • Trust OS

    TEE環境下也要有一個操作系統,各家都有自己的Trustzone的操作系統,如Trustonic、高通的QSEE、國內的豆莢,還有開源的OPTEE等。在操作系統之上自然要有應用程序,在Trustzone裏面我們一般叫TrustApp,當然TEE裏面每個TrustApp都在一個沙盒裏,互相之間是隔離的。比如說支付,就可以做成一個App(需要注意的是,和Normal World裏面的App是兩個概念),這個App簡單來說就負責用私鑰把網上發來的Challenge籤個名,而這個簽名的動作是需要在Secure World裏面做的,避免惡意程序竊取到私鑰來僞造簽名。

    例如支付寶,其實支付寶也是隻支持幾個Trust OS的。同時,支付寶還定義了一系列標準,用來完成他的行爲。

    現在的Trust OS大都會遵循GlobalPlatform的規範,這個組織致力於制定統一的Trust OS的API的接口規範,這樣一個TrustApp只要用GP API,就可以方便移植到各個不同的TEE操作系統上了。

  • Intel 平臺的 SGX

    針對可信計算,類似ARM的TrustZone,Intel也針對x86平臺提出了自己的安全架構SGX:

    Intel® Software Guard Extensions (Intel® SGX)

    https://software.intel.com/zh-cn/sgx-sdk

    SGX全稱Intel Software Guard Extensions,顧名思義,其是對因特爾體系(IA)的一個擴展,用於增強軟件的安全性。這種方式並不是識別和隔離平臺上的所有惡意軟件,而是將合法軟件的安全操作封裝在一個enclave中,保護其不受惡意軟件的攻擊,特權或者非特權的軟件都無法訪問enclave,也就是說,一旦軟件和數據位於enclave中,即便操作系統或者和VMM(Hypervisor)也無法影響enclave裏面的代碼和數據。Enclave的安全邊界只包含CPU和它自身。SGX創建的enclave也可以理解爲一個可信執行環境TEE(Trusted Execution Environment)。不過其與ARM TrustZone(TZ)還是有一點小區別的,TZ中通過CPU劃分爲兩個隔離環境(安全世界和正常世界),兩者之間通過SMC指令通信;而SGX中一個CPU可以運行多個安全enclaves,併發執行亦可。

    簡單來講, Intel SGX最關鍵的優勢在於將程序以外的software stack如OS和BIOS都排除在了TCB(Trusted Computing Base)以外。換句話說,就是在容器enclave裏的code只信任自己和intel的CPU。

    網上有人是這樣對比TrustZone和SGX的:

    Trustzone默認相信SecureOS,安全世界。SGX僅相信CPU core,通過SGX指令構建enclave容器。簡單比喻,TEE是個公用大保險櫃,什麼東西都裝進去,有漏洞的app可能也進去了,而且保險櫃鑰匙在管理員手上,必須相信管理員。SGX每個app有自己的保險櫃,鑰匙在自己手上

    SGX要進入工業界應用尚需時間,一個重要的問題是現在在intel發行的服務器芯片上還沒有SGX,而SGX的重要應用就是在數據中心和雲端的應用。

5. TrustZone開源項目

除了各傢俬有實現外,ARM也有不少開源項目,知名度較高的有:

6. 參考