@@ -131,7 +131,6 @@ def verify_kid(self, verify_kid: bool):
131131 # key (Optional[COSEKeyInterface]): A content encryption key as COSEKey.
132132 # protected (Optional[dict]): Parameters that are to be cryptographically protected.
133133 # unprotected (Optional[dict]): Parameters that are not cryptographically protected.
134- # nonce (bytes): A nonce for encryption.
135134 # recipients (Optional[List[RecipientInterface]]): A list of recipient
136135 # information structures.
137136 # signers (List[Signer]): A list of signer information objects for
@@ -175,7 +174,6 @@ def encode_and_encrypt(
175174 key : Optional [COSEKeyInterface ] = None ,
176175 protected : Optional [dict ] = None ,
177176 unprotected : Optional [dict ] = None ,
178- nonce : bytes = b"" ,
179177 recipients : Optional [List [RecipientInterface ]] = None ,
180178 external_aad : bytes = b"" ,
181179 out : str = "" ,
@@ -188,7 +186,6 @@ def encode_and_encrypt(
188186 key (Optional[COSEKeyInterface]): A content encryption key as COSEKey.
189187 protected (Optional[dict]): Parameters that are to be cryptographically protected.
190188 unprotected (Optional[dict]): Parameters that are not cryptographically protected.
191- nonce (bytes): A nonce for encryption.
192189 recipients (Optional[List[RecipientInterface]]): A list of recipient
193190 information structures.
194191 external_aad(bytes): External additional authenticated data supplied
@@ -206,7 +203,7 @@ def encode_and_encrypt(
206203 EncodeError: Failed to encode data.
207204 """
208205 p , u = self ._build_headers (key , protected , unprotected )
209- return self ._encode_and_encrypt (payload , key , p , u , nonce , recipients , external_aad , out )
206+ return self ._encode_and_encrypt (payload , key , p , u , recipients , external_aad , out )
210207
211208 def encode_and_mac (
212209 self ,
@@ -493,13 +490,27 @@ def decode(
493490 err = e
494491 raise err
495492
493+ def _build_headers (
494+ self ,
495+ key : Optional [COSEKeyInterface ],
496+ protected : Optional [dict ],
497+ unprotected : Optional [dict ],
498+ ) -> Tuple [Dict [int , Any ], Dict [int , Any ]]:
499+ p = to_cose_header (protected )
500+ u = to_cose_header (unprotected )
501+ if key is not None :
502+ if self ._alg_auto_inclusion :
503+ p [1 ] = key .alg
504+ if self ._kid_auto_inclusion and key .kid :
505+ u [4 ] = key .kid
506+ return p , u
507+
496508 def _encode_and_encrypt (
497509 self ,
498510 payload : bytes ,
499511 key : Optional [COSEKeyInterface ],
500512 p : Dict [int , Any ],
501513 u : Dict [int , Any ],
502- nonce : bytes ,
503514 recipients : Optional [List [RecipientInterface ]],
504515 external_aad : bytes ,
505516 out : str ,
@@ -519,13 +530,12 @@ def _encode_and_encrypt(
519530 return res if out == "cbor2/CBORTag" else self ._dumps (res )
520531 if key is None :
521532 raise ValueError ("key should be set." )
522- if not nonce :
533+ if 5 not in u : # nonce
523534 try :
524- nonce = key .generate_nonce ()
535+ u [ 5 ] = key .generate_nonce ()
525536 except NotImplementedError :
526537 raise ValueError ("Nonce generation is not supported for the key. Set a nonce explicitly." )
527- u [5 ] = nonce
528- ciphertext = key .encrypt (payload , nonce , aad )
538+ ciphertext = key .encrypt (payload , u [5 ], aad )
529539 res = CBORTag (16 , [b_protected , u , ciphertext ])
530540 return res if out == "cbor2/CBORTag" else self ._dumps (res )
531541
@@ -544,35 +554,19 @@ def _encode_and_encrypt(
544554
545555 if cek is None :
546556 raise ValueError ("key should be set." )
547- if not nonce :
557+ if 5 not in u : # nonce
548558 try :
549- nonce = cek .generate_nonce ()
559+ u [ 5 ] = cek .generate_nonce ()
550560 except NotImplementedError :
551561 raise ValueError ("Nonce generation is not supported for the key. Set a nonce explicitly." )
552- u [5 ] = nonce
553562 enc_structure = ["Encrypt" , b_protected , external_aad ]
554563 aad = self ._dumps (enc_structure )
555- ciphertext = cek .encrypt (payload , nonce , aad )
564+ ciphertext = cek .encrypt (payload , u [ 5 ] , aad )
556565 cose_enc : List [Any ] = [b_protected , u , ciphertext ]
557566 cose_enc .append (recs )
558567 res = CBORTag (96 , cose_enc )
559568 return res if out == "cbor2/CBORTag" else self ._dumps (res )
560569
561- def _build_headers (
562- self ,
563- key : Optional [COSEKeyInterface ],
564- protected : Optional [dict ],
565- unprotected : Optional [dict ],
566- ) -> Tuple [Dict [int , Any ], Dict [int , Any ]]:
567- p = to_cose_header (protected )
568- u = to_cose_header (unprotected )
569- if key is not None :
570- if self ._alg_auto_inclusion :
571- p [1 ] = key .alg
572- if self ._kid_auto_inclusion and key .kid :
573- u [4 ] = key .kid
574- return p , u
575-
576570 def _encode_and_mac (
577571 self ,
578572 payload : bytes ,
0 commit comments