lpwan Working Group                                          A. Minaburo
Internet-Draft                                                    Acklio
Intended status: Standards Track                              L. Toutain
Expires: June 12, September 6, 2020        Institut MINES TELECOM; IMT Atlantique
                                                            R. Andreasen
                                             Universidad de Buenos Aires
                                                       December 10, 2019
                                                          March 05, 2020

        LPWAN Static Context Header Compression (SCHC) for CoAP


   This draft defines the way SCHC (Static Context Header Compression)
   header compression can be applied to the CoAP protocol.  SCHC is a
   header compression mechanism adapted for constrained devices.  SCHC
   uses a static description of the header to reduce the redundancy and
   the size of the information in the header.  While
   [I-D.ietf-lpwan-ipv6-static-context-hc] describes the SCHC
   compression and fragmentation framework, and its application for
   IPv6/UDP headers, this document applies the use of SCHC for CoAP
   headers.  The CoAP header structure differs from IPv6 and UDP
   protocols since
   CoAP uses a flexible header with a variable number of options,
   themselves of variable length.  The CoAP protocol messages format is
   asymmetric: the request messages have a header format different from
   the one in the response messages.  This document
   explains specification gives guidance
   on how to use the apply SCHC compression mechanism to flexible headers and how to leverage the
   asymmetry for CoAP. more efficient compression Rules.

Status of This Memo

   This Internet-Draft is submitted in full conformance with the
   provisions of BCP 78 and BCP 79.

   Internet-Drafts are working documents of the Internet Engineering
   Task Force (IETF).  Note that other groups may also distribute
   working documents as Internet-Drafts.  The list of current Internet-
   Drafts is at https://datatracker.ietf.org/drafts/current/.

   Internet-Drafts are draft documents valid for a maximum of six months
   and may be updated, replaced, or obsoleted by other documents at any
   time.  It is inappropriate to use Internet-Drafts as reference
   material or to cite them other than as "work in progress."

   This Internet-Draft will expire on June 12, September 6, 2020.

Copyright Notice

   Copyright (c) 2019 2020 IETF Trust and the persons identified as the
   document authors.  All rights reserved.

   This document is subject to BCP 78 and the IETF Trust's Legal
   Provisions Relating to IETF Documents
   (https://trustee.ietf.org/license-info) in effect on the date of
   publication of this document.  Please review these documents
   carefully, as they describe your rights and restrictions with respect
   to this document.  Code Components extracted from this document must
   include Simplified BSD License text as described in Section 4.e of
   the Trust Legal Provisions and are provided without warranty as
   described in the Simplified BSD License.

Table of Contents

   1.  Introduction  . . . . . . . . . . . . . . . . . . . . . . . .   2   3
     1.1.  Terminology . . . . . . . . . . . . . . . . . . . . . . .   3   4
   2.  Applying SCHC Compression Process to CoAP . . . . . . . . . . . . . . . . . .   3 . .   4
   3.  CoAP Compression with SCHC  . . . . . . . . . . . . . . . . .   4   5
     3.1.  Differences between CoAP and UDP/IP . . . . . . . . . . .   5
   4.  Compression of CoAP header fields . . . . . . . . . . . . . .   6
     4.1.  CoAP version field  . . . . . . . . . . . . . . . . . . .   6   7
     4.2.  CoAP type field . . . . . . . . . . . . . . . . . . . . .   6   7
     4.3.  CoAP code field . . . . . . . . . . . . . . . . . . . . .   6   7
     4.4.  CoAP Message ID field . . . . . . . . . . . . . . . . . .   7
     4.5.  CoAP Token fields . . . . . . . . . . . . . . . . . . . .   7
   5.  CoAP options  . . . . . . . . . . . . . . . . . . . . . . . .   7   8
     5.1.  CoAP Content and Accept options.  . . . . . . . . . . . .   7   8
     5.2.  CoAP option Max-Age, Uri-Host and Uri-Port fields . . . .   8
     5.3.  CoAP option Uri-Path and Uri-Query fields . . . . . . . .   8
       5.3.1.  Variable length Uri-Path and Uri-Query  . . . . . . .   9
       5.3.2.  Variable number of path or query elements . . . . . .   9  10
     5.4.  CoAP option Size1, Size2, Proxy-URI and Proxy-Scheme
           fields  . . . . . . . . . . . . . . . . . . . . . . . . .   9  10
     5.5.  CoAP option ETag, If-Match, If-None-Match, Location-Path
           and Location-Query fields . . . . . . . . . . . . . . . .  10
   6.  Other  SCHC compression of CoAP extension RFCs . . . . . . . . . . . . . . . . . . . . . . . . .  10
     6.1.  Block . . . . . . . . . . . . . . . . . . . . . . . . . .  10
     6.2.  Observe . . . . . . . . . . . . . . . . . . . . . . . . .  10  11
     6.3.  No-Response . . . . . . . . . . . . . . . . . . . . . . .  10  11
     6.4.  OSCORE  . . . . . . . . . . . . . . . . . . . . . . . . .  11
   7.  Examples of CoAP header compression . . . . . . . . . . . . .  12
     7.1.  Mandatory header with CON message . . . . . . . . . . . .  12
     7.2.  OSCORE Compression  . . . . . . . . . . . . . . . . . . .  13
     7.3.  Example OSCORE Compression  . . . . . . . . . . . . . . .  16  17
   8.  IANA Considerations . . . . . . . . . . . . . . . . . . . . .  26  27
   9.  Security considerations . . . . . . . . . . . . . . . . . . .  26  27
   10. Acknowledgements  . . . . . . . . . . . . . . . . . . . . . .  26  28
   11. Normative References  . . . . . . . . . . . . . . . . . . . .  26  28
   Authors' Addresses  . . . . . . . . . . . . . . . . . . . . . . .  27  29

1.  Introduction

   CoAP [rfc7252] is an implementation a transfer protocol that implements a subset of the REST architecture
   HTTP (Hypertext Transfer Protocol) and is optimized for
   constrained devices. REST-based
   (Representational state transfer) services.  Although CoAP was
   designed for constrained devices, the size of a CoAP header still is
   too large for the constraints of Low LPWAN (Low Power Wide Area Networks (LPWAN) Networks)
   and some compression is needed to reduce the header size.

   The [I-D.ietf-lpwan-ipv6-static-context-hc] defines SCHC, a header
   compression mechanism for LPWAN network based on a static context.
   The context
   is said static since the field description composing section 5 of the Rules are
   not learned during [I-D.ietf-lpwan-ipv6-static-context-hc] explains
   the packet exchanges but architecture where compression and decompression are previously defined. done.  The context(s) is(are)
   context is known by both ends before transmission.

   A  The way the
   context is composed configured or exchanged is out of a set the scope for this

   SCHC compresses and decompresses headers based on shared contexts
   between devices.  Each context consists of rules that are referenced by Rule
   IDs (identifiers).  A multiple Rules.  Each rule contains an ordered list
   can match header fields and specific values or ranges of values.  If
   a rule matches, the matched header fields
   descriptions containing a field are substituted by the rule
   ID (FID), its length (FL) and its
   position (FP), a direction indicator (DI) (upstream, optionally some residual bits.  Thus, different Rules may
   correspond to different types of packets that a device expects to
   send or receive.

   A Rule describes the complete header of the packet with an ordered
   list of fields descriptions, see section 7 of the
   [I-D.ietf-lpwan-ipv6-static-context-hc], thereby each description
   contains the field ID (FID), its length (FL) and its position (FP), a
   direction indicator (DI) (upstream, downstream and bidirectional) and
   some associated Target Values (TV).  Target Value
   indicates the value that can be expected.  TV can also be a list of

   A Matching Operator (MO) is associated to each header field
   description.  The rule is selected if all the MOs fit the TVs for all
   fields of the incoming packet.
   In that case, a Compression/
   Decompression Compression/Decompression Action (CDA) associated to
   each field defines how the compressed and the decompressed values are
   computed out of each other, for each of the header fields.
   Compression mainly results in one of 4 actions: * send the field
   value, * send nothing, * send some least significant bits of the
   field or * send an index.  After applying the compression there may
   be some bits to be sent, these values are called Compression Residues and are transmitted after the Rule ID in
   the compressed messages.

   The compression rules define

   SCHC is a generic way general concept mechanism that can be applied to compress and decompress
   the fields.  If different
   protocols, the device is modified, for example, exact Rules to introduce new
   functionalities or new CoAP options, the rules must be updated to
   reflect used depend on the protocol and the evolution.
   application, and CoAP differs from UDP and IPv6, see Section 3.

1.1.  Terminology

   The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
   "OPTIONAL" in this document are to be interpreted as described in BCP
   14 [rfc2119][rfc8174] when, and only when, they appear in all
   capitals, as shown here.

2.  Applying SCHC Compression Process to CoAP

   The SCHC Compression rules can be applied to CoAP flows.  SCHC
   Compression of the CoAP header MAY be done in conjunction with the
   lower layers (IPv6/UDP) or independently.  The SCHC adaptation layers
   as described in section 5 of [I-D.ietf-lpwan-ipv6-static-context-hc]
   may be used as shown in Figure 1.

    ^   +------------+    ^  +------------+        ^  +------------+
    |   |    CoAP    |    |  |    CoAP    |  inner |  |    CoAP    |
    |   +------------+    v  +------------+        x  |    OSCORE  |
    |   |    UDP     |       |    DTLS    |  outer |  +------------+
    |   +------------+       +------------+        |  |    UDP     |
    |   |    IPv6    |       |    UDP     |        |  +------------+
    v   +------------+       +------------+        |  |    IPv6    |
                             |    IPv6    |        v  +------------+

                       Figure 1: rule scope for CoAP

   Figure 1 shows some examples for CoAP architecture and the SCHC
   rule's scope.

   In the first example, a rule compresses the complete header stack
   from IPv6 to CoAP.  In this case, SCHC C/D (Static Context Header
   Compression Compressor/Decompressor) is performed at the device Sender and
   at the LPWAN boundary. Receiver.

   In the second example, an end-to-end encryption mechanisms is used
   between the device Sender and the application. Receiver.  The SCHC compression is applied
   in the CoAP layer compressing the CoAP header independently of the
   other layers.  The rule ID and the compression residue are encrypted
   using a mechanism such as DTLS.  Only the other end can decipher the
   information.  Layers below may also be compressed using other SCHC
   rules (this is out of the scope of this document) as defined in the
   SCHC [I-D.ietf-lpwan-ipv6-static-context-hc] document.

   In the third example, OSCORE [rfc8613] is used.  In this case, two
   rulesets are used to compress the CoAP message.  A first ruleset
   focused on the inner header and is applied end to end by both ends.
   A second ruleset compresses the outer header and the layers below and
   is done between the device Sender and the LPWAN boundary. Receiver.

3.  CoAP Compression with SCHC

   SCHC with CoAP will be used exactly the same way as it is applied in
   any protocol as IP or UDP with the difference that the fields
   description needs to be defined based on both headers and target
   values of the request and the responses.  SCHC Rules description use
   the direction information to optmize the compression by reducing the
   number of Rules needed to compress traffic.  CoAP compression follows
   the [I-D.ietf-lpwan-ipv6-static-context-hc] scheme and as for other
   protocols, if no valid Rule was found, then the packet MUST be sent
   uncompressed using the RuleID dedicated to this purpose and the
   Compression Residue is the complete header of the packet.  See
   section 6 of [I-D.ietf-lpwan-ipv6-static-context-hc].

3.1.  Differences between CoAP and UDP/IP

   CoAP differs from IPv6 and UDP protocols on the following aspects:

   o  IPv6 and UDP are symmetrical protocols.  The same fields are found
      in the not request and in response protocols as CoAP, and
      so the response, same header fields are used in all packets for all
      directions, with the value of some fields being swapped on the
      return path (e.g. source and destination addresses fields).  A  The
      CoAP request is intrinsically headers instead are asymmetric, the headers are different from for
      a request or a response.  For example, the URI-path option is
      mandatory in the request and is not found in the response, a
      request may contain an Accept option and the response may contain
      a Content option.

      The [I-D.ietf-lpwan-ipv6-static-context-hc] defines the use of a
      message direction
      Direction Indicator (DI) in the Field Description, which allows a
      single Rule to process message headers differently depending of on
      the direction.

   o  Even when a field is "symmetric" (i.e. found in both directions)
      the values carried in each direction are different.  Combined with  To performs
      the compression a matching list in the TV, TV might be use because
      this allows reducing the range of expected values in a particular
      direction and therefore reduce reduces the size of the
      compression residue.  For instance, if a client sends only CON request,
      requests, the type can be elided by compression and the answer may
      use one single bit to carry either the ACK or RST type.
      The  In CoAP
      some fields have the same behavior can be applied to behavior, for example the CoAP Code field Code can
      have 0.0X code
      Format is found format value in the request and Y.ZZ code format in
      the answer.
      The response.  Through the direction allows splitting indicator, a field
      description in two parts the Rules splits the possible values
      for each direction field value in the same Rule. two
      parts.  Resulting in a smaller compression residue.

   o  In IPv6 and UDP, header fields have a fixed size and it size, defined in the
      Rule, which is not sent.  In CoAP, some fields in the header have
      a varying size, variable length, for example the Token size may vary from 0 to 8
      bytes, the length is given by a field in the header.  More systematically, the  The CoAP
      options are described using the Type-Length-Value. Type-Length-Value encoding format.

      Section 7.5.2 from [I-D.ietf-lpwan-ipv6-static-context-hc] offers
      the possibility to define a function for the Field Length in the
      Field Description. Description to have knwoledge of the length before
      compression.  When doing SCHC compression of a variable length
      field two cases may be raised after applying the CDA: * The result
      of the compression is of fixed length and the compressed value is
      sent in the residue.  * Or the result of the compression is of
      variable-length and in this case, the size is sent with the
      compressed value in the residue.

   o  In CoAP headers, a field can appear several times.  This is
      typical for elements of a URI (path or queries).  The SCHC
      specification [I-D.ietf-lpwan-ipv6-static-context-hc] allows a
      Field ID to appears several times in the rule, and uses the Field
      Position (FP) to identify the correct instance, and thereby
      removing the ambiguity of the matching operation.

   o  Field sizes defined in the CoAP protocol can be too large
      regarding LPWAN traffic constraints.  This is particularly true
      for the Message ID field and the Token field.  The MSB  SCHC uses different
      Matching operators (MO) to performs the compression, see section
      7.4 of [I-D.ietf-lpwan-ipv6-static-context-hc].  In this case the
      Most Significant Bits (MSB) MO can be applied to reduce the
      information carried on LPWANs.

   o LP

4.  Compression of CoAP also obeys the client/server paradigm and header fields

   This section discusses the compression
      ratio can be of the different if CoAP header
   fields.  The CoAP compression with SCHC follows the request is issued from an LPWAN
      device or from a non LPWAN device.  For instance, a Device (Dev)
      aware Section 7.1 of LPWAN constraints can generate a 1-byte token, but a

4.1.  CoAP client will certainly send a larger token to the Dev.
      The SCHC compression-decompression process never modifies the
      Values it only reduces their sizes.  Nevertheless, a proxy placed
      before the compressor may change some field values to allow SCHC
      achieving a better compression ratio, while maintaining the
      necessary context for interoperability with existing CoAP

   o  If no valid Rule was found, then the packet MUST be sent
      uncompressed using the RuleID dedicated to this purpose and the
      Compression Residue is the complete header of the packet.  See
      section 6 of [I-D.ietf-lpwan-ipv6-static-context-hc].

4.  Compression of CoAP header fields

   This section discusses the compression of the different CoAP header

4.1.  CoAP version field

   CoAP version is bidirectional and MUST be elided during version field

   CoAP version is bidirectional and MUST be elided during the SCHC
   compression, since it always contains the same value.  In the future,
   if new versions of CoAP are defined, new rules will be needed to
   avoid ambiguities between versions.

4.2.  CoAP type field

   The CoAP Protocol [rfc7252] defines 4 types has four type of messages: CON, NON, ACK
   and RST.  ACK and RST are a two request
   (CON, NON); one response to the CON (ACK) and NON.  If the
   device plays a specific client or server role, a rule can take
   advantage of these properties with the mapping list: [CON, NON] for one direction and [ACK, RST] for the other direction and so, the
   compression residue is reduced to 1 bit. empty message (RST).

   The field SHOULD be elided if for instance a client is sending only
   NON or only CON messages.

   In any case,  For the RST message a rule MUST dedicated Rule may
   be defined to carry RST to needed.  For other usages a client. mapping list can be used.

4.3.  CoAP code field

   The code field indicates the Request Method used in CoAP, a registry
   is given in section 12.1 of [rfc7252].  The compression of the CoAP
   code field follows the same principle as that of the CoAP type field.
   If the device plays a specific role, the set of code values can be
   split in two parts, the request codes with the 0 class and the
   response values.

   If the device only implements a CoAP client, the request code can be
   reduced to the set of requests the client is able to process.

   All the response codes MUST

   A mapping list can be compressed with a SCHC rule.

4.4.  CoAP Message ID field

   The Message ID field is bidirectional and is used to manage
   acknowledgments.  The server memorizes the value for an
   EXCHANGE_LIFETIME period (by default 247 seconds) for CON messages
   and a NON_LIFETIME period (by default 145 seconds) known values, for NON messages.
   During that period, a server receiving the same Message ID value will
   process other values the message as a retransmission.  After this period, it will
   field cannot be processed as a new message.

   In case where the Device is a client, compressed an the size of value needs to be sent in the

4.4.  CoAP Message ID field may be too large regarding the number of messages sent.

   client SHOULD use only small Message ID values, for instance 4 bit
   long.  Therefore, an MSB field can be used to limit the size of the
   compression residue.

   In case where the Device is a server, the client may be located
   outside of compressed with the LPWAN area MSB(x) MO and it views the Device as a regular device
   connected to the Internet.  The client will generate Message ID using
   the 16 bits space offered by this field.  A CoAP proxy can be set
   before the SCHC C/D to reduce the value
   Least Significant Bits (LSB) CDA, see section 7.4 of the Message ID, to allow
   its compression with the MSB matching operator and LSB CDA.

4.5.  CoAP Token fields

   Token is defined through two CoAP fields, Token Length in the
   mandatory header and Token Value directly following the mandatory
   CoAP header.

   Token Length is processed as any protocol field.  If the value
   remains the same during all the transaction, does
   not change, the size can be stored in the context TV and elided during the
   transmission.  Otherwise, it will have to be sent as a in the compression

   Token Value size cannot MUST not be defined directly in sent as a variable length residue to avoid
   ambiguity with Token Length.  Therefore, Token Length value MUST be
   used to define the rule in size of the Field
   Length (FL).  Instead, a residue.  A specific function
   designated as "TKL" MUST be used and length does not have to be sent with in the residue. Rule.  During the
   decompression, this function returns the value contained in the Token
   Length field.

5.  CoAP options

5.1.  CoAP Content and Accept options.

   These fields are both unidirectional and MUST NOT be set to
   bidirectional in a rule entry.

   If a single value is expected by the client, it can be stored in the
   TV and elided during the transmission.  Otherwise, if several
   possible values are expected by the client, a matching-list SHOULD be
   used to limit the size of the residue.  Otherwise, the value has to
   be sent as a residue (fixed or variable length).

5.2.  CoAP option Max-Age, Uri-Host and Uri-Port fields

   These fields are unidirectional and MUST NOT be set to bidirectional
   in a rule DI entry. see section 7.1 of
   [I-D.ietf-lpwan-ipv6-static-context-hc].  They are used only by the
   server to inform of the caching duration and is never found in client

   If the duration is known by both ends, the value can be elided on the

   A matching list can be used if some well-known values are defined.

   Otherwise these options SHOULD can be sent as a residue (fixed or variable

5.3.  CoAP option Uri-Path and Uri-Query fields

   These fields are unidirectional and MUST NOT be set to bidirectional
   in a rule entry.  They are used only by the client to access a
   specific resource and are never found in server responses.

   Uri-Path and Uri-Query elements are a repeatable options, the Field
   Position (FP) gives the position in the path.

   A Mapping list can be used to reduce the size of variable Paths or
   Queries.  In that case, to optimize the compression, several elements
   can be regrouped into a single entry.  Numbering of elements do not
   change, MO comparison is set with the first element of the matching.

      | Field       |FL|FP|DI| Target | Match   |     CDA     |
      |             |  |  |  | Value  | Opera.  |             |
      |URI-Path     |  | 1|up|["/a/b",|equal    |not-sent     |
      |             |  |  |  |"/c/d"] |         |             |
      |URI-Path     |  | 3|up|        |ignore   |value-sent   |

                      Figure 2: complex path example

   In Figure 2 a single bit residue can be used to code one of the 2
   paths.  If regrouping were not allowed, a 2 bits residue would be

5.3.1.  Variable length Uri-Path and Uri-Query

   When the length is not known at the rule creation, the Field Length
   MUST be set to variable, and the unit is set to bytes.

   The MSB MO can be applied to a Uri-Path or Uri-Query element.  Since
   MSB value is given in bit, the size MUST always be a multiple of 8

   The length sent at the beginning of a variable length residue
   indicates the size of the LSB in bytes.

   For instance for a CORECONF path /c/X6?k="eth0" the rule can be set

      | Field       |FL |FP|DI| Target | Match   |     CDA     |
      |             |   |  |  | Value  | Opera.  |             |
      |URI-Path     |  8| 1|up|"c"     |equal    |not-sent     |
      |URI-Path     |var| 2|up|        |ignore   |value-sent   |
      |URI-Query    |var| 1|up|"k="    |MSB(16)  |LSB          |

                    Figure 3: CORECONF URI compression

   Figure 3 shows the parsing and the compression of the URI, where c is
   not sent.  The second element is sent with the length (i.e. 0x2 X 6)
   followed by the query option (i.e. 0x05 "eth0").

5.3.2.  Variable number of path or query elements

   The number of Uri-path or Uri-Query elements in a rule is fixed at
   the rule creation time.  If the number varies, several rules SHOULD
   be created to cover all the possibilities.  Another possibility is to
   define the length of Uri-Path to variable and send a compression
   residue with a length of 0 to indicate that this Uri-Path is empty.
   This adds the 4 bits to of the compression residue. variable residue size.  See section 7.5.2

5.4.  CoAP option Size1, Size2, Proxy-URI and Proxy-Scheme fields

   These fields are unidirectional and MUST NOT be set to bidirectional
   in a rule entry. DI entry, see section 7.1 of the
   [I-D.ietf-lpwan-ipv6-static-context-hc].  They are used only by the
   client to access a specific resource and are never found in server

   If the field value has to be sent, TV is not set, MO is set to
   "ignore" and CDA is set to "value-sent".  A mapping MAY also be used.

   Otherwise, the TV is set to the value, MO is set to "equal" and CDA
   is set to "not-sent".

5.5.  CoAP option ETag, If-Match, If-None-Match, Location-Path and
      Location-Query fields

   These fields are unidirectional.

   These fields values cannot be stored in a rule entry.  They MUST
   always be sent with the compression residues.

6.  Other  SCHC compression of CoAP extension RFCs

6.1.  Block

   Block [rfc7959] allows a fragmentation at the CoAP level.  SCHC also
   includes a fragmentation protocol.  They are compatible.  If a block
   option is used, its content MUST be sent as a compression residue.

6.2.  Observe

   The [rfc7641] defines the Observe option.  The TV is not set, MO is
   set to "ignore" and the CDA is set to "value-sent".  SCHC does not
   limit the maximum size for this option (3 bytes).  To reduce the
   transmission size, either the device implementation MAY limit the
   delta between two consecutive values, or a proxy can modify the

   Since an RST message may be sent to inform a server that the client
   does not require Observe response, a rule MUST allow the transmission
   of this message.

6.3.  No-Response

   The [rfc7967] defines a No-Response option limiting the responses
   made by a server to a request.  If the value is known by both ends,
   then TV is set to this value, MO is set to "equal" and CDA is set to

   Otherwise, if the value is changing over time, TV is not set, MO is
   set to "ignore" and CDA to "value-sent".  A matching list can also be
   used to reduce the size.

6.4.  OSCORE

   OSCORE [rfc8613] defines end-to-end protection for CoAP messages.
   This section describes how SCHC rules can be applied to compress
   OSCORE-protected messages.

         0 1 2 3 4 5 6 7 <--------- n bytes ------------->
        |0 0 0|h|k|  n  |      Partial IV (if any) ...
        |               |                                |
        |<--  CoAP   -->|<------ CoAP OSCORE_piv ------> |

         <- 1 byte -> <------ s bytes ----->
        | s (if any) | kid context (if any) | kid (if any)      ... |
        |                                   |                       |
        | <------ CoAP OSCORE_kidctxt ----->|<-- CoAP OSCORE_kid -->|

                          Figure 4: OSCORE Option

   The encoding of the OSCORE Option Value defined in Section 6.1 of
   [rfc8613] is repeated in Figure 4.

   The first byte is used for flags that specify the contents of the
   OSCORE option.  The 3 most significant bits of this byte are reserved
   and always set to 0.  Bit h, when set, indicates the presence of the
   kid context field in the option.  Bit k, when set, indicates the
   presence of a kid field.  The 3 least significant bits n indicate the
   length of the piv (Partial Initialization Vector) field in bytes.
   When n = 0, no piv is present.

   The flag byte is followed by the piv field, kid context field and kid
   field in this order and if present; the length of the kid context
   field is encoded in the first byte denoting by s the length of the
   kid context in bytes.

   This draft specification recommends to implement a parser that is able to identify the OSCORE Option and the
   fields it contains.

   Conceptually, it discerns up to 4 distinct pieces of information
   within the OSCORE option: the flag bits, the piv, the kid context,
   and the kid.  It is thus recommended that the parser split the OSCORE
   option into the 4 subsequent fields:

   o  CoAP OSCORE_flags,

   o  CoAP OSCORE_piv,

   o  CoAP OSCORE_kidctxt,

   o  CoAP OSCORE_kid.

   These fields are shown superimposed on the OSCORE Option format in
   Figure 4, the CoAP OSCORE_kidctxt field including the size bits s.
   Their size SHOULD be reduced using SCHC compression.

7.  Examples of CoAP header compression

7.1.  Mandatory header with CON message

   In this first scenario, the LPWAN compressor at the Network Gateway
   side receives from an Internet client a POST message, which is
   immediately acknowledged by the Device.  For this simple scenario,
   the rules are described Figure 5.

    Rule ID 1
   | Field       |FL|FP|DI|Target| Match   |     CDA     ||    Sent    |
   |             |  |  |  |Value | Opera.  |             ||   [bits]   |
   |CoAP version |  |  |bi|  01  |equal    |not-sent     ||            |
   |CoAP Type    |  |  |dw| CON  |equal    |not-sent     ||            |
   |CoAP Type    |  |  |up|[ACK, |         |             ||            |
   |             |  |  |  | RST] |match-map|matching-sent|| T          |
   |CoAP TKL     |  |  |bi| 0    |equal    |not-sent     ||            |
   |CoAP Code    |  |  |bi|[0.00,|         |             ||            |
   |             |  |  |  | ...  |         |             ||            |
   |             |  |  |  | 5.05]|match-map|matching-sent||  CC CCC    |
   |CoAP MID     |  |  |bi| 0000 |MSB(7 )  |LSB          ||        M-ID|
   |CoAP Uri-Path|  |  |dw| path |equal 1  |not-sent     ||            |

          Figure 5: CoAP Context to compress header without token

   The version and Token Length fields are elided.  The 26 method and
   response codes defined in [rfc7252] has been shrunk to 5 bits using a
   matching list.  Uri-Path contains a single element indicated in the
   matching operator.

   SCHC Compression reduces the header sending only the Type, a mapped
   code and the least significant bits of Message ID (9 bits in the
   example above).

   Note that a request sent by a client located in an Application Server
   to a server located in the device, may not be compressed through this
   rule since the MID will not start with 7 bits equal to 0.  A CoAP
   proxy, before the core SCHC C/D can rewrite the message ID to a value
   matched by the rule.

7.2.  OSCORE Compression

   OSCORE aims to solve the problem of end-to-end encryption for CoAP
   messages.  The goal, therefore, is to hide as much of the message as
   possible while still enabling proxy operation.

   Conceptually this is achieved by splitting the CoAP message into an
   Inner Plaintext and Outer OSCORE Message.  The Inner Plaintext
   contains sensitive information which is not necessary for proxy
   operation.  This, in turn, is the part of the message which can be
   encrypted until it reaches its end destination.  The Outer Message
   acts as a shell matching the format of a regular CoAP message, and
   includes all Options and information needed for proxy operation and
   caching.  This decomposition is illustrated in Figure 6.

   CoAP options are sorted into one of 3 classes, each granted a
   specific type of protection by the protocol:

   o  Class E: Encrypted options moved to the Inner Plaintext,

   o  Class I: Integrity-protected options included in the AAD for the
      encryption of the Plaintext but otherwise left untouched in the
      Outer Message,

   o  Class U: Unprotected options left untouched in the Outer Message.

   Additionally, the OSCORE Option is added as an Outer option,
   signalling that the message is OSCORE protected.  This option carries
   the information necessary to retrieve the Security Context with which
   the message was encrypted so that it may be correctly decrypted at
   the other end-point.

                         Original CoAP Message
                      |v|t|tkl| code  |  Msg Id.      |
                      | Token                              |
                      | Options (IEU)            |
                      .                          .
                      .                          .
                      | 0xFF |
                      |                               |
                      |     Payload                   |
                      |                               |
                             /                \
                            /                  \
                           /                    \
                          /                      \
        Outer Header     v                        v  Plaintext
     +-+-+---+--------+---------------+          +-------+
     |v|t|tkl|new code|  Msg Id.      |          | code  |
     +-+-+---+--------+---------------+....+     +-------+-----......+
     | Token                               |     | Options (E)       |
     +--------------------------------.....+     +-------+------.....+
     | Options (IU)             |                | OxFF  |
     .                          .                +-------+-----------+
     . OSCORE Option            .                |                   |
     +------+-------------------+                | Payload           |
     | 0xFF |                                    |                   |
     +------+                                    +-------------------+

   Figure 6: A CoAP message is split into an OSCORE outer and plaintext

   Figure 6 shows the message format for the OSCORE Message and

   In the Outer Header, the original message code is hidden and replaced
   by a default dummy value.  As seen in sections and 4.2 of the
   [rfc8613], the message code is replaced by POST for requests and
   Changed for responses when Observe is not used.  If Observe is used,
   the message code is replaced by FETCH for requests and Content for

   The original message code is put into the first byte of the
   Plaintext.  Following the message code, the class E options comes and
   if present the original message Payload is preceded by its payload

   The Plaintext is now encrypted by an AEAD algorithm which integrity
   protects Security Context parameters and eventually any class I
   options from the Outer Header.  Currently no CoAP options are marked
   class I.  The resulting Ciphertext becomes the new Payload of the
   OSCORE message, as illustrated in Figure 7.

   This Ciphertext is, as defined in RFC 5116, the concatenation of the
   encrypted Plaintext and its authentication tag.  Note that Inner
   Compression only affects the Plaintext before encryption, thus we can
   only aim to reduce this first, variable length component of the
   Ciphertext.  The authentication tag is fixed in length and considered
   part of the cost of protection.

        Outer Header
     |v|t|tkl|new code|  Msg Id.      |
     | Token                               |
     | Options (IU)             |
     .                          .
     . OSCORE Option            .
     | 0xFF |
     |                                  |
     | Ciphertext: Encrypted Inner      |
     |             Header and Payload   |
     |             + Authentication Tag |
     |                                  |

                         Figure 7: OSCORE message

   The SCHC Compression scheme consists of compressing both the
   Plaintext before encryption and the resulting OSCORE message after
   encryption, see Figure 8.

   This translates into a segmented process where SCHC compression is
   applied independently in 2 stages, each with its corresponding set of
   rules, with the Inner SCHC Rules and the Outer SCHC Rules.  This way
   compression is applied to all fields of the original CoAP message.

   Note that since the Inner part of the message can only be decrypted
   by the corresponding end-point, this end-point will also have to
   implement Inner SCHC Compression/Decompression.

        Outer Message                             OSCORE Plaintext
     +-+-+---+--------+---------------+          +-------+
     |v|t|tkl|new code|  Msg Id.      |          | code  |
     +-+-+---+--------+---------------+....+     +-------+-----......+
     | Token                               |     | Options (E)       |
     +--------------------------------.....+     +-------+------.....+
     | Options (IU)             |                | OxFF  |
     .                          .                +-------+-----------+
     . OSCORE Option            .                |                   |
     +------+-------------------+                | Payload           |
     | 0xFF |                                    |                   |
     +------+------------+                       +-------------------+
     |  Ciphertext       |<---------\                      |
     |                   |          |                      v
     +-------------------+          |             +-----------------+
             |                      |             |   Inner SCHC    |
             v                      |             |   Compression   |
       +-----------------+          |             +-----------------+
       |   Outer SCHC    |          |                      |
       |   Compression   |          |                      v
       +-----------------+          |              +-------+
             |                      |              |Rule ID|
             v                      |              +-------+--+
         +--------+           +------------+       | Residue  |
         |Rule ID'|           | Encryption | <---  +----------+--------+
         +--------+--+        +------------+       |                   |
         | Residue'  |                             | Payload           |
         +-----------+-------+                     |                   |
         |  Ciphertext       |                     +-------------------+
         |                   |

                   Figure 8: OSCORE Compression Diagram

7.3.  Example OSCORE Compression

   An example is given with a GET Request and its consequent CONTENT
   Response from a device-based CoAP client to a cloud-based CoAP
   server.  A possible set of rules for the Inner and Outer SCHC
   Compression is shown.  A dump of the results and a contrast between
   SCHC + OSCORE performance with SCHC + COAP performance is also
   listed.  This gives an approximation to the cost of security with

   Our first example CoAP message is the GET Request in Figure 9

   Original message:

   01   Ver
     00   CON
       0001   tkl
           00000001   Request Code 1 "GET"

   0x0001 = mid
   0x82 = token

   Option 11: URI_PATH
   Value = temperature

   Original msg length:   17 bytes.

                        Figure 9: CoAP GET Request

   Its corresponding response is the CONTENT Response in Figure 10.

   Original message:

   01   Ver
     10   ACK
       0001   tkl
           01000101   Successful Response Code 69 "2.05 Content"

   0x0001 = mid
   0x82 = token

   0xFF  Payload marker

   Original msg length:   10

                     Figure 10: CoAP CONTENT Response

   The SCHC

   TheSCHC Rules for the Inner Compression include all fields that are
   already present
   alreadypresent in a regular CoAP message, what is important is their
   order and the definition of only those CoAP fields are into
   Plaintext, message.  The methods described in
   section Section 4 applies to these fields.  As an example, see
   Figure 11.

    Rule ID 0
   | Field         |FP|DI|  Target   |    MO     |     CDA   || Sent |
   |               |  |  |  Value    |           |           ||[bits]|
   |CoAP Code      |  |up|   1       |  equal    |not-sent   ||      |
   |CoAP Code      |  |dw|[69,132]   | match-map |match-sent || c    |
   |CoAP Uri-Path  |  |up|temperature|  equal    |not-sent   ||      |
   |COAP Option-End|  |dw| 0xFF      |  equal    |not-sent   ||      |

                        Figure 11: Inner SCHC Rules

   Figure 12 shows the Plaintext obtained for our example GET Request
   and follows the process of Inner Compression and Encryption until we
   end up with the Payload to be added in the outer OSCORE Message.

   In this case the original message has no payload and its resulting
   Plaintext can be compressed up to only 1 byte (size of the Rule ID).
   The AEAD algorithm preserves this length in its first output, but
   also yields a fixed-size tag which cannot be compressed and has to be
   included in the OSCORE message.  This translates into an overhead in
   total message length, which limits the amount of compression that can
   be achieved and plays into the cost of adding security to the

     |                                                        |
     | OSCORE Plaintext                                       |
     |                                                        |
     | 0x01bb74656d7065726174757265  (13 bytes)               |
     |                                                        |
     | 0x01 Request Code GET                                  |
     |                                                        |
     |      bb74656d7065726174757265 Option 11: URI_PATH      |
     |                               Value = temperature      |

                                 | Inner SCHC Compression
                  |                                 |
                  | Compressed Plaintext            |
                  |                                 |
                  | 0x00                            |
                  |                                 |
                  | Rule ID = 0x00 (1 byte)         |
                  | (No residue)                    |

                                 | AEAD Encryption
                                 |  (piv = 0x04)
           |                                                 |
           |  encrypted_plaintext = 0xa2 (1 byte)            |
           |  tag = 0xc54fe1b434297b62 (8 bytes)             |
           |                                                 |
           |  ciphertext = 0xa2c54fe1b434297b62 (9 bytes)    |

      Figure 12: Plaintext compression and encryption for GET Request

   In Figure 13 we repeat the process is repeated for the example CONTENT
   In this case the misalignment produced by the compression  The residue (1
   bit) makes it so is 1 bit long.  Note that 7 bits of since SCHC adds
   padding have to be applied after the payload, resulting in a compressed Plaintext that is the same size as
   before compression.  This this misalignment also causes the hexcode hexadecimal
   code from the payload to differ from the original, even though it has
   not been compressed.

   On top of this, the overhead from the tag bytes is incurred as

     |                                                        |
     | OSCORE Plaintext                                       |
     |                                                        |
     | 0x45ff32332043  (6 bytes)                              |
     |                                                        |
     | 0x45 Successful Response Code 69 "2.05 Content"        |
     |                                                        |
     |     ff Payload marker                                  |
     |                                                        |
     |       32332043 Payload                                 |

                                 | Inner SCHC Compression
           |                                          |
           | Compressed Plaintext                     |
           |                                          |
           | 0x001919902180 (6 bytes)                 |
           |                                          |
           |   00 Rule ID                             |
           |                                          |
           |    0b0 (1 bit match-map residue)         |
           |       0x32332043 >> 1 (shifted payload)  |
           |                        0b0000000 Padding |

                                 | AEAD Encryption
                                 |  (piv = 0x04)
       |                                                         |
       |  encrypted_plaintext = 0x10c6d7c26cc1 (6 bytes)         |
       |  tag = 0xe9aef3f2461e0c29 (8 bytes)                     |
       |                                                         |
       |  ciphertext = 0x10c6d7c26cc1e9aef3f2461e0c29 (14 bytes) |

   Figure 13: Plaintext compression and encryption for CONTENT Response
   The Outer SCHC Rules (Figure 16) MUST process the OSCORE Options
   fields.  In Figure 14 and Figure 15 we show a dump of the OSCORE
   Messages generated from our example messages once they have been
   provided with the Inner Compressed Ciphertext in the payload.  These
   are the messages that have to be compressed by the Outer SCHC

   Protected message:
   (25 bytes)

   01   Ver
     00   CON
       0001   tkl
           00000010   Request Code 2 "POST"

   0x0001 = mid
   0x82 = token

   0xd8080904636c69656e74 (10 bytes)
   Value = 0x0904636c69656e74
             09 = 000 0 1 001 Flag byte
                      h k  n
               04 piv
                 636c69656e74 kid

   0xFF  Payload marker
   0xa2c54fe1b434297b62 (9 bytes)

        Figure 14: Protected and Inner SCHC Compressed GET Request

   Protected message:
   (22 bytes)

   01   Ver
     10   ACK
       0001   tkl
           01000100   Successful Response Code 68 "2.04 Changed"

   0x0001 = mid
   0x82 = token

   0xd008 (2 bytes)
   Value = b''

   0xFF  Payload marker
   0x10c6d7c26cc1e9aef3f2461e0c29 (14 bytes)

      Figure 15: Protected and Inner SCHC Compressed CONTENT Response

   For the flag bits, a number of compression methods has been shown to
   be useful depending on the application.  The simplest alternative is
   to provide a fixed value for the flags, combining MO equal and CDA
   not- sent.  This saves most bits but could prevent flexibility.
   Otherwise, match-mapping could be used to choose from an interested
   number of configurations to the exchange.  Otherwise, MSB could be
   used to mask off the 3 hard-coded most significant bits.

   Note that fixing a flag bit will limit the choice of CoAP Options
   that can be used in the exchange, since their values are dependent on
   certain options.

   The piv field lends itself to having a number of bits masked off with
   MO MSB and CDA LSB.  This could be useful in applications where the
   message frequency is low such as that found in LPWAN technologies.
   Note that compressing the sequence numbers effectively reduces the
   maximum amount of sequence numbers that can be used in an exchange.
   Once this amount is exceeded, the SCHC Context would OSCORE keys need to be re-

   The size s included in the kid context field MAY be masked off with
   CDA MSB.  The rest of the field could have additional bits masked
   off, or have the whole field be fixed with MO equal and CDA not-sent.
   The same holds for the kid field.

   Figure 16 shows a possible set of Outer Rules to compress the Outer

   Rule ID 0
   | Field             |FP|DI|    Target    |   MO   |   CDA   || Sent |
   |                   |  |  |    Value     |        |         ||[bits]|
   |CoAP version       |  |bi|      01      |equal   |not-sent ||      |
   |CoAP Type          |  |up|      0       |equal   |not-sent ||      |
   |CoAP Type          |  |dw|      2       |equal   |not-sent ||      |
   |CoAP TKL           |  |bi|      1       |equal   |not-sent ||      |
   |CoAP Code          |  |up|      2       |equal   |not-sent ||      |
   |CoAP Code          |  |dw|      68      |equal   |not-sent ||      |
   |CoAP MID           |  |bi|     0000     |MSB(12) |LSB      ||MMMM  |
   |CoAP Token         |  |bi|     0x80     |MSB(5)  |LSB      ||TTT   |
   |CoAP OSCORE_flags  |  |up|     0x09     |equal   |not-sent ||      |
   |CoAP OSCORE_piv    |  |up|     0x00     |MSB(4)  |LSB      ||PPPP  |
   |COAP OSCORE_kid    |  |up|0x636c69656e70|MSB(52) |LSB      ||KKKK  |
   |COAP OSCORE_kidctxt|  |bi|     b''      |equal   |not-sent ||      |
   |CoAP OSCORE_flags  |  |dw|     b''      |equal   |not-sent ||      |
   |CoAP OSCORE_piv    |  |dw|     b''      |equal   |not-sent ||      |
   |CoAP OSCORE_kid    |  |dw|     b''      |equal   |not-sent ||      |
   |COAP Option-End    |  |dw|     0xFF     |equal   |not-sent ||      |

                        Figure 16: Outer SCHC Rules

   These Outer Rules are applied to the example GET Request and CONTENT
   Response.  The resulting messages are shown in Figure 17 and
   Figure 18.

   Compressed message:
   0x001489458a9fc3686852f6c4 (12 bytes)
   0x00 Rule ID
       1489 Compression Residue
           458a9fc3686852f6c4 Padded payload

   Compression residue:
   0b 0001 010 0100 0100 (15 bits -> 2 bytes with padding)
       mid tkn piv  kid

   0xa2c54fe1b434297b62 (9 bytes)

   Compressed message length: 12 bytes

               Figure 17: SCHC-OSCORE Compressed GET Request

   Compressed message:
   0x0014218daf84d983d35de7e48c3c1852 (16 bytes)
   0x00 Rule ID
       14 Compression residue
         218daf84d983d35de7e48c3c1852 Padded payload
   Compression residue:
   0b0001 010 (7 bits -> 1 byte with padding)
     mid  tkn

   0x10c6d7c26cc1e9aef3f2461e0c29 (14 bytes)

   Compressed msg length: 16 bytes

            Figure 18: SCHC-OSCORE Compressed CONTENT Response

   For contrast, we compare these results with what would be obtained by
   SCHC compressing the original CoAP messages without protecting them
   with OSCORE.  To do this, we compress the CoAP messages according to
   the SCHC rules in Figure 19.

   Rule ID 1
   | Field         |FP|DI|  Target   |   MO    |     CDA   ||  Sent  |
   |               |  |  |  Value    |         |           || [bits] |
   |CoAP version   |  |bi|    01     |equal    |not-sent   ||        |
   |CoAP Type      |  |up|    0      |equal    |not-sent   ||        |
   |CoAP Type      |  |dw|    2      |equal    |not-sent   ||        |
   |CoAP TKL       |  |bi|    1      |equal    |not-sent   ||        |
   |CoAP Code      |  |up|    2      |equal    |not-sent   ||        |
   |CoAP Code      |  |dw| [69,132]  |match-map|map-sent   ||C       |
   |CoAP MID       |  |bi|   0000    |MSB(12)  |LSB        ||MMMM    |
   |CoAP Token     |  |bi|    0x80   |MSB(5)   |LSB        ||TTT     |
   |CoAP Uri-Path  |  |up|temperature|equal    |not-sent   ||        |
   |COAP Option-End|  |dw|   0xFF    |equal    |not-sent   ||        |

                  Figure 19: SCHC-CoAP Rules (No OSCORE)

   This yields the results in Figure 20 for the Request, and Figure 21
   for the Response.

   Compressed message:
   0x01 = Rule ID

   Compression residue:
   0b00010100 (1 byte)

   Compressed msg length: 2

               Figure 20: CoAP GET Compressed without OSCORE

   Compressed message:
   0x01 = Rule ID

   Compression residue:
   0b00001010 (1 byte)


   Compressed msg length: 6

             Figure 21: CoAP CONTENT Compressed without OSCORE

   As can be seen, the difference between applying SCHC + OSCORE as
   compared to regular SCHC + COAP is about 10 bytes of cost.

8.  IANA Considerations

   This document has no request to IANA.

9.  Security considerations

   This document does not have any more Security consideration than the
   ones already raised on [I-D.ietf-lpwan-ipv6-static-context-hc] [I-D.ietf-lpwan-ipv6-static-context-hc].
   Variable length residues may be used to compress URI elements.  They
   cannot produce a packet expansion either on the LPWAN network or in
   the Internet network after decompression.  The length send is not
   used to indicate the information that should be reconstructed at the
   other end, but on the contrary the information sent as a Residue.
   Therefore, if a length is set to a high value, but the number of bits
   on the SCHC packet is smaller, the packet must be dropped by the

   OSCORE compression is also based on the same compression method
   described in [I-D.ietf-lpwan-ipv6-static-context-hc].  The size of
   the Initialisation Vector residue size must be considered carefully.
   A too large value has a impact on the compression efficiency and a
   too small value will force the device to renew its key more often.
   This operation may be long and energy consuming.

10.  Acknowledgements

   The authors would like to thank Dominique Barthel, Carsten Bormann,
   Thomas Fossati, Klaus Hartke, Francesca Palombini, Alexander Pelov,
   Goran Selander.

11.  Normative References

              Minaburo, A., Toutain, L., Gomez, C., Barthel, D., and J.
              Zuniga, "Static Context Header Compression (SCHC) and
              fragmentation for LPWAN, application to UDP/IPv6", draft-
              ietf-lpwan-ipv6-static-context-hc-24 (work in progress),
              December 2019.

   [rfc2119]  Bradner, S., "Key words for use in RFCs to Indicate
              Requirement Levels", BCP 14, RFC 2119,
              DOI 10.17487/RFC2119, March 1997,

   [rfc7252]  Shelby, Z., Hartke, K., and C. Bormann, "The Constrained
              Application Protocol (CoAP)", RFC 7252,
              DOI 10.17487/RFC7252, June 2014,

   [rfc7641]  Hartke, K., "Observing Resources in the Constrained
              Application Protocol (CoAP)", RFC 7641,
              DOI 10.17487/RFC7641, September 2015,

   [rfc7959]  Bormann, C. and Z. Shelby, Ed., "Block-Wise Transfers in
              the Constrained Application Protocol (CoAP)", RFC 7959,
              DOI 10.17487/RFC7959, August 2016,

   [rfc7967]  Bhattacharyya, A., Bandyopadhyay, S., Pal, A., and T.
              Bose, "Constrained Application Protocol (CoAP) Option for
              No Server Response", RFC 7967, DOI 10.17487/RFC7967,
              August 2016, <https://www.rfc-editor.org/info/rfc7967>.

   [rfc8174]  Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC
              2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174,
              May 2017, <https://www.rfc-editor.org/info/rfc8174>.

   [rfc8613]  Selander, G., Mattsson, J., Palombini, F., and L. Seitz,
              "Object Security for Constrained RESTful Environments
              (OSCORE)", RFC 8613, DOI 10.17487/RFC8613, July 2019,

Authors' Addresses

   Ana Minaburo
   1137A avenue des Champs Blancs
   35510 Cesson-Sevigne Cedex

   Email: ana@ackl.io

   Laurent Toutain
   Institut MINES TELECOM; IMT Atlantique
   2 rue de la Chataigneraie
   CS 17607
   35576 Cesson-Sevigne Cedex

   Email: Laurent.Toutain@imt-atlantique.fr

   Ricardo Andreasen
   Universidad de Buenos Aires
   Av. Paseo Colon 850
   C1063ACV Ciudad Autonoma de Buenos Aires

   Email: randreasen@fi.uba.ar