跳到主要内容

6. MAC 对象

  1. MAC 对象

COSE 支持两种不同的 MAC 结构. 当不需要 recipient 结构, 因为要使用的 key 是隐式已知时, 使用 COSE_Mac0. 其他所有情况使用 COSE_Mac. 这些情况包括需要多个 recipient, key 未知, 或使用 direct 以外的 recipient algorithm.

本节描述在 COSE 中执行 MAC authentication 时使用的结构和方法. 本文档允许使用与 encryption 相同的所有 recipient algorithm 类别.

MAC 操作可以在两种模式中使用. 第一种只是检查内容自 MAC 计算以来未被更改. 任意类别的 recipient algorithm 都可用于此目的. 第二种模式既检查内容自 MAC 计算以来未被更改, 又使用 recipient algorithm 验证发送者. 支持此模式的 recipient algorithm 类别是使用 preshared secret 的算法, 或执行 Static-Static (SS) key agreement 的算法(不含 key wrap 步骤). 在这两种情况下, 可以验证创建并发送消息 MAC 的实体. (这种 sender 知识假定只涉及两方, 且你没有把消息发给自己.) 两种 MAC message structure 都可以获得 origination 属性.

6.1. 带 Recipients 的 MACed Message

多 recipient MACed message 使用两个结构: 本节定义的 COSE_Mac 结构用于承载 body, COSE_recipient 结构(Section 5.1)用于保存 MAC 计算所用的 key. MACed messages 示例见 Appendix C.5.

MAC 结构可根据使用上下文编码为 tagged 或 untagged. tagged COSE_Mac 结构由 CBOR tag 97 标识. 表示它的 CDDL 片段为:

COSE_Mac_Tagged = #6.97(COSE_Mac)

COSE_Mac 结构是一个 CBOR array. array 的字段按顺序为:

protected: 如 Section 3 所述.

unprotected: 如 Section 3 所述.

payload: 此字段包含待 MACed 的序列化内容. 如果消息中不存在 payload, 应用程序需要单独提供 payload. payload 封装在 bstr 中, 以确保传输时不会发生变化. 如果 payload 单独传输(即 detached content), 则在此位置放置一个 nil CBOR value, 并由应用程序负责确保它在传输时不会发生变化.

tag: 此字段包含 MAC 值.

recipients: 如 Section 5.1 所述.

表示上述 COSE_Mac 文本的 CDDL 片段如下.

COSE_Mac = [ Headers, payload : bstr / nil, tag : bstr, recipients : [+COSE_recipient] ]

6.2. 使用隐式 Key 的 MACed Messages

本节描述在 recipient 隐式已知的情况下执行 MAC authentication 时使用的结构和方法.

MACed message 使用本节定义的 COSE_Mac0 结构承载 body. 使用隐式 key 的 MACed messages 示例见 Appendix C.6.

MAC 结构可根据使用上下文编码为 tagged 或 untagged. tagged COSE_Mac0 结构由 CBOR tag 17 标识. 表示它的 CDDL 片段为:

COSE_Mac0_Tagged = #6.17(COSE_Mac0)

COSE_Mac0 结构是一个 CBOR array. array 的字段按顺序为:

protected: 如 Section 3 所述.

unprotected: 如 Section 3 所述.

payload: 如 Section 6.1 所述.

tag: 此字段包含 MAC 值.

对应上述文本的 CDDL 片段为:

COSE_Mac0 = [ Headers, payload : bstr / nil, tag : bstr, ]

6.3. 如何计算和验证 MAC

为了得到待认证数据的一致编码, 使用 MAC_structure 创建 canonical form. MAC_structure 是一个 CBOR array. MAC_structure 的字段按顺序为:

  1. 一个 context text string, 用于标识正在编码的结构. 对 COSE_Mac 结构, 此 context text string 为 "MAC". 对 COSE_Mac0 结构, 此 context text string 为 "MAC0".

  2. 来自 body structure 的 protected attributes. 如果没有 protected attributes, 则使用零长度 bstr.

  3. 来自应用程序的 externally supplied data, 编码为 bstr 类型. 如果未提供此字段, 它默认为零长度 byte string. (关于构造此字段的应用指导, 见 Section 4.3.)

  4. 待 MACed 的 payload, 编码为 bstr 类型. 此处使用完整 payload, 与它如何传输无关.

对应上述文本的 CDDL 片段为:

MAC_structure = [ context : "MAC" / "MAC0", protected : empty_or_serialized_map, external_aad : bstr, payload : bstr ]

MAC 计算步骤:

  1. 创建 MAC_structure 并填充适当字段.

  2. 按 Section 9 所述编码方式, 将 MAC_structure 编码为 byte string, 以创建 ToBeMaced 值.

  3. 调用 MAC creation algorithm, 传入 K (要使用的 key), alg (用于 MAC 的 algorithm), 以及 ToBeMaced (用于计算 MAC 的值).

  4. 将生成的 MAC 放入 COSE_Mac 或 COSE_Mac0 结构的 "tag" 字段.

  5. 对于 COSE_Mac 结构, 为消息的每个 recipient 加密并编码 MAC key.

MAC 验证步骤:

  1. 创建 MAC_structure 并填充适当字段.

  2. 按 Section 9 所述编码方式, 将 MAC_structure 编码为 byte string, 以创建 ToBeMaced 值.

  3. 对于 COSE_Mac 结构, 通过解码并解密其中一个 recipient 结构来获得 cryptographic key.

  4. 调用 MAC creation algorithm, 传入 K (要使用的 key), alg (用于 MAC 的 algorithm), 以及 ToBeMaced (用于计算 MAC 的值).

  5. 将 MAC 值与 COSE_Mac 或 COSE_Mac0 结构的 "tag" 字段进行比较.