@@ -154,15 +154,56 @@ def to_list(self, payload: bytes = b"", external_aad: bytes = b"", aad_context:
154154 res .append (children )
155155 return res
156156
157+ def encode (
158+ self ,
159+ plaintext : bytes ,
160+ recipient_key : Optional [COSEKeyInterface ] = None ,
161+ salt : Optional [bytes ] = None ,
162+ context : Optional [Union [List [Any ], Dict [str , Any ]]] = None ,
163+ external_aad : bytes = b"" ,
164+ aad_context : str = "Enc_Recipient" ,
165+ ) -> Optional [COSEKeyInterface ]:
166+ """
167+ Encodes a specified plaintext to a ciphertext with the recipient-specific
168+ method (e.g., key wrapping, key agreement, or the combination of them)
169+ and sets up the related information (context information or ciphertext)
170+ in the recipient structure.
171+ Therefore, it will be used by the sender of the recipient information
172+ before calling COSE.encode_* functions with the Recipient object. The
173+ key generated through this function will be set to ``key`` parameter
174+ of COSE.encode_* functions.
175+
176+ Args:
177+ plaintext (bytes): A plaing text to be encrypted. In most of the cases,
178+ the plaintext is a byte string of a content encryption key.
179+ recipient_key (Optional[COSEKeyInterface]): The external public
180+ key provided by the recipient used for ECDH key agreement, HPKE, etc.
181+ salt (Optional[bytes]): A salt used for deriving a key.
182+ context (Optional[Union[List[Any], Dict[str, Any]]]): Context
183+ information structure.
184+ external_aad (bytes): External additional authenticated data for AEAD.
185+ aad_context (bytes): An additional authenticated data context to build
186+ an Enc_structure internally.
187+ Returns:
188+ Optional[COSEKeyInterface]: A generated key or passed-through key
189+ which is used as ``key`` parameter of COSE.encode_* functions.
190+ Raises:
191+ ValueError: Invalid arguments.
192+ EncodeError: Failed to encode(e.g., wrap, derive) the key.
193+ """
194+ raise NotImplementedError
195+
157196 def apply (
158197 self ,
159198 key : Optional [COSEKeyInterface ] = None ,
160199 recipient_key : Optional [COSEKeyInterface ] = None ,
161200 salt : Optional [bytes ] = None ,
162201 context : Optional [Union [List [Any ], Dict [str , Any ]]] = None ,
202+ external_aad : bytes = b"" ,
203+ aad_context : str = "Enc_Recipient" ,
163204 ) -> COSEKeyInterface :
164205 """
165- Applies a COSEKey as a material to prepare a MAC/encryption key with
206+ [DEPRECATED] Applies a COSEKey as a material to prepare a MAC/encryption key with
166207 the recipient-specific method (e.g., key wrapping, key agreement,
167208 or the combination of them) and sets up the related information
168209 (context information or ciphertext) in the recipient structure.
@@ -179,6 +220,9 @@ def apply(
179220 salt (Optional[bytes]): A salt used for deriving a key.
180221 context (Optional[Union[List[Any], Dict[str, Any]]]): Context
181222 information structure.
223+ external_aad (bytes): External additional authenticated data for AEAD.
224+ aad_context (bytes): An additional authenticated data context to build
225+ an Enc_structure internally.
182226 Returns:
183227 COSEKeyInterface: A generated key or passed-throug key which is used
184228 as ``key`` parameter of COSE.encode_* functions.
@@ -233,7 +277,9 @@ def decrypt(
233277 key (COSEKeyInterface): The external key to be used for extracting the key.
234278 alg (Optional[int]): The algorithm of the key extracted.
235279 context (Optional[Union[List[Any], Dict[str, Any]]]): Context information structure.
236- external_aad (bytes): External additional authenticated data.
280+ external_aad (bytes): External additional authenticated data for AEAD.
281+ aad_context (bytes): An additional authenticated data context to build
282+ an Enc_structure internally.
237283 Returns:
238284 bytes: The decrypted plain text.
239285 Raises:
0 commit comments