@@ -53,6 +53,7 @@ func (s shortKEM) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) {
5353 bitmask = 0x01
5454 }
5555
56+ Nsk := s .PrivateKeySize ()
5657 dkpPrk := s .labeledExtract ([]byte ("" ), []byte ("dkp_prk" ), seed )
5758 var bytes []byte
5859 ctr := 0
@@ -64,14 +65,12 @@ func (s shortKEM) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) {
6465 dkpPrk ,
6566 []byte ("candidate" ),
6667 []byte {byte (ctr )},
67- uint16 (s . byteSize () ),
68+ uint16 (Nsk ),
6869 )
6970 bytes [0 ] &= bitmask
7071 skBig .SetBytes (bytes )
7172 }
72- l := s .PrivateKeySize ()
73- sk := & shortKEMPrivKey {s , make ([]byte , l ), nil }
74- copy (sk .priv [l - len (bytes ):], bytes )
73+ sk := & shortKEMPrivKey {s , bytes , nil }
7574 return sk .Public (), sk
7675}
7776
@@ -83,11 +82,11 @@ func (s shortKEM) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) {
8382
8483func (s shortKEM ) UnmarshalBinaryPrivateKey (data []byte ) (kem.PrivateKey , error ) {
8584 l := s .PrivateKeySize ()
86- if len (data ) < l {
87- return nil , ErrInvalidKEMPrivateKey
85+ if len (data ) != l {
86+ return nil , kem . ErrPrivKeySize
8887 }
8988 sk := & shortKEMPrivKey {s , make ([]byte , l ), nil }
90- copy (sk .priv [ l - len ( data ): l ] , data [:l ])
89+ copy (sk .priv , data [:l ])
9190 if ! sk .validate () {
9291 return nil , ErrInvalidKEMPrivateKey
9392 }
@@ -96,7 +95,11 @@ func (s shortKEM) UnmarshalBinaryPrivateKey(data []byte) (kem.PrivateKey, error)
9695}
9796
9897func (s shortKEM ) UnmarshalBinaryPublicKey (data []byte ) (kem.PublicKey , error ) {
99- x , y := elliptic .Unmarshal (s , data )
98+ l := s .PublicKeySize ()
99+ if len (data ) != l {
100+ return nil , kem .ErrPubKeySize
101+ }
102+ x , y := elliptic .Unmarshal (s , data [:l ])
100103 if x == nil {
101104 return nil , ErrInvalidKEMPublicKey
102105 }
0 commit comments