Source file src/crypto/mlkem/mlkem.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package mlkem implements the quantum-resistant key encapsulation method
     6  // ML-KEM (formerly known as Kyber), as specified in [NIST FIPS 203].
     7  //
     8  // Most applications should use the ML-KEM-768 parameter set, as implemented by
     9  // [DecapsulationKey768] and [EncapsulationKey768].
    10  //
    11  // [NIST FIPS 203]: https://doi.org/10.6028/NIST.FIPS.203
    12  package mlkem
    13  
    14  import (
    15  	"crypto"
    16  	"crypto/internal/fips140/mlkem"
    17  	"crypto/subtle"
    18  )
    19  
    20  const (
    21  	// SharedKeySize is the size of a shared key produced by ML-KEM.
    22  	SharedKeySize = 32
    23  
    24  	// SeedSize is the size of a seed used to generate a decapsulation key.
    25  	SeedSize = 64
    26  
    27  	// CiphertextSize768 is the size of a ciphertext produced by ML-KEM-768.
    28  	CiphertextSize768 = 1088
    29  
    30  	// EncapsulationKeySize768 is the size of an ML-KEM-768 encapsulation key.
    31  	EncapsulationKeySize768 = 1184
    32  
    33  	// CiphertextSize1024 is the size of a ciphertext produced by ML-KEM-1024.
    34  	CiphertextSize1024 = 1568
    35  
    36  	// EncapsulationKeySize1024 is the size of an ML-KEM-1024 encapsulation key.
    37  	EncapsulationKeySize1024 = 1568
    38  )
    39  
    40  // DecapsulationKey768 is the secret key used to decapsulate a shared key
    41  // from a ciphertext. It includes various precomputed values.
    42  type DecapsulationKey768 struct {
    43  	key *mlkem.DecapsulationKey768
    44  }
    45  
    46  // GenerateKey768 generates a new decapsulation key, drawing random bytes from
    47  // a secure source. The decapsulation key must be kept secret.
    48  func GenerateKey768() (*DecapsulationKey768, error) {
    49  	var k *mlkem.DecapsulationKey768
    50  	var err error
    51  	subtle.WithDataIndependentTiming(func() {
    52  		k, err = mlkem.GenerateKey768()
    53  	})
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	return &DecapsulationKey768{k}, nil
    58  }
    59  
    60  // NewDecapsulationKey768 expands a decapsulation key from a 64-byte seed in the
    61  // "d || z" form. The seed must be uniformly random.
    62  func NewDecapsulationKey768(seed []byte) (*DecapsulationKey768, error) {
    63  	var k *mlkem.DecapsulationKey768
    64  	var err error
    65  	subtle.WithDataIndependentTiming(func() {
    66  		k, err = mlkem.NewDecapsulationKey768(seed)
    67  	})
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return &DecapsulationKey768{k}, nil
    72  }
    73  
    74  // Bytes returns the decapsulation key as a 64-byte seed in the "d || z" form.
    75  //
    76  // The decapsulation key must be kept secret.
    77  func (dk *DecapsulationKey768) Bytes() []byte {
    78  	return dk.key.Bytes()
    79  }
    80  
    81  // Decapsulate generates a shared key from a ciphertext and a decapsulation
    82  // key. If the ciphertext is not the correct length, Decapsulate returns an
    83  // error. A ciphertext that is the correct length but otherwise invalid will
    84  // not return an error; instead, it will produce a shared key that does not
    85  // match the sender's, according to FIPS 203.
    86  //
    87  // The shared key must be kept secret.
    88  func (dk *DecapsulationKey768) Decapsulate(ciphertext []byte) (sharedKey []byte, err error) {
    89  	subtle.WithDataIndependentTiming(func() { sharedKey, err = dk.key.Decapsulate(ciphertext) })
    90  	return
    91  }
    92  
    93  // EncapsulationKey returns the public encapsulation key necessary to produce
    94  // ciphertexts.
    95  func (dk *DecapsulationKey768) EncapsulationKey() *EncapsulationKey768 {
    96  	return &EncapsulationKey768{dk.key.EncapsulationKey()}
    97  }
    98  
    99  // Encapsulator returns the encapsulation key, like
   100  // [DecapsulationKey768.EncapsulationKey].
   101  //
   102  // It implements [crypto.Decapsulator].
   103  func (dk *DecapsulationKey768) Encapsulator() crypto.Encapsulator {
   104  	return dk.EncapsulationKey()
   105  }
   106  
   107  var _ crypto.Decapsulator = (*DecapsulationKey768)(nil)
   108  
   109  // An EncapsulationKey768 is the public key used to produce ciphertexts to be
   110  // decapsulated by the corresponding DecapsulationKey768.
   111  type EncapsulationKey768 struct {
   112  	key *mlkem.EncapsulationKey768
   113  }
   114  
   115  // NewEncapsulationKey768 parses an encapsulation key from its encoded form. If
   116  // the encapsulation key is not valid, NewEncapsulationKey768 returns an error.
   117  func NewEncapsulationKey768(encapsulationKey []byte) (*EncapsulationKey768, error) {
   118  	key, err := mlkem.NewEncapsulationKey768(encapsulationKey)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	return &EncapsulationKey768{key}, nil
   124  }
   125  
   126  // Bytes returns the encapsulation key as a byte slice.
   127  func (ek *EncapsulationKey768) Bytes() []byte {
   128  	return ek.key.Bytes()
   129  }
   130  
   131  // Encapsulate generates a shared key and an associated ciphertext from an
   132  // encapsulation key, drawing random bytes from a secure source.
   133  //
   134  // The shared key must be kept secret.
   135  //
   136  // For testing, derandomized encapsulation is provided by the
   137  // [crypto/mlkem/mlkemtest] package.
   138  func (ek *EncapsulationKey768) Encapsulate() (sharedKey, ciphertext []byte) {
   139  	subtle.WithDataIndependentTiming(func() { sharedKey, ciphertext = ek.key.Encapsulate() })
   140  	return
   141  }
   142  
   143  // DecapsulationKey1024 is the secret key used to decapsulate a shared key
   144  // from a ciphertext. It includes various precomputed values.
   145  type DecapsulationKey1024 struct {
   146  	key *mlkem.DecapsulationKey1024
   147  }
   148  
   149  // GenerateKey1024 generates a new decapsulation key, drawing random bytes from
   150  // a secure source. The decapsulation key must be kept secret.
   151  func GenerateKey1024() (*DecapsulationKey1024, error) {
   152  	var key *mlkem.DecapsulationKey1024
   153  	var err error
   154  	subtle.WithDataIndependentTiming(func() { key, err = mlkem.GenerateKey1024() })
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  
   159  	return &DecapsulationKey1024{key}, nil
   160  }
   161  
   162  // NewDecapsulationKey1024 expands a decapsulation key from a 64-byte seed in the
   163  // "d || z" form. The seed must be uniformly random.
   164  func NewDecapsulationKey1024(seed []byte) (*DecapsulationKey1024, error) {
   165  	var key *mlkem.DecapsulationKey1024
   166  	var err error
   167  	subtle.WithDataIndependentTiming(func() { key, err = mlkem.NewDecapsulationKey1024(seed) })
   168  	if err != nil {
   169  		return nil, err
   170  	}
   171  
   172  	return &DecapsulationKey1024{key}, nil
   173  }
   174  
   175  // Bytes returns the decapsulation key as a 64-byte seed in the "d || z" form.
   176  //
   177  // The decapsulation key must be kept secret.
   178  func (dk *DecapsulationKey1024) Bytes() []byte {
   179  	return dk.key.Bytes()
   180  }
   181  
   182  // Decapsulate generates a shared key from a ciphertext and a decapsulation
   183  // key. If the ciphertext is not the correct length, Decapsulate returns an
   184  // error. A ciphertext that is the correct length but otherwise invalid will
   185  // not return an error; instead, it will produce a shared key that does not
   186  // match the sender's, according to FIPS 203.
   187  //
   188  // The shared key must be kept secret.
   189  func (dk *DecapsulationKey1024) Decapsulate(ciphertext []byte) (sharedKey []byte, err error) {
   190  	subtle.WithDataIndependentTiming(func() { sharedKey, err = dk.key.Decapsulate(ciphertext) })
   191  	return
   192  }
   193  
   194  // EncapsulationKey returns the public encapsulation key necessary to produce
   195  // ciphertexts.
   196  func (dk *DecapsulationKey1024) EncapsulationKey() *EncapsulationKey1024 {
   197  	return &EncapsulationKey1024{dk.key.EncapsulationKey()}
   198  }
   199  
   200  // Encapsulator returns the encapsulation key, like
   201  // [DecapsulationKey1024.EncapsulationKey].
   202  //
   203  // It implements [crypto.Decapsulator].
   204  func (dk *DecapsulationKey1024) Encapsulator() crypto.Encapsulator {
   205  	return dk.EncapsulationKey()
   206  }
   207  
   208  var _ crypto.Decapsulator = (*DecapsulationKey1024)(nil)
   209  
   210  // An EncapsulationKey1024 is the public key used to produce ciphertexts to be
   211  // decapsulated by the corresponding DecapsulationKey1024.
   212  type EncapsulationKey1024 struct {
   213  	key *mlkem.EncapsulationKey1024
   214  }
   215  
   216  // NewEncapsulationKey1024 parses an encapsulation key from its encoded form. If
   217  // the encapsulation key is not valid, NewEncapsulationKey1024 returns an error.
   218  func NewEncapsulationKey1024(encapsulationKey []byte) (*EncapsulationKey1024, error) {
   219  	key, err := mlkem.NewEncapsulationKey1024(encapsulationKey)
   220  	if err != nil {
   221  		return nil, err
   222  	}
   223  
   224  	return &EncapsulationKey1024{key}, nil
   225  }
   226  
   227  // Bytes returns the encapsulation key as a byte slice.
   228  func (ek *EncapsulationKey1024) Bytes() []byte {
   229  	return ek.key.Bytes()
   230  }
   231  
   232  // Encapsulate generates a shared key and an associated ciphertext from an
   233  // encapsulation key, drawing random bytes from a secure source.
   234  //
   235  // The shared key must be kept secret.
   236  //
   237  // For testing, derandomized encapsulation is provided by the
   238  // [crypto/mlkem/mlkemtest] package.
   239  func (ek *EncapsulationKey1024) Encapsulate() (sharedKey, ciphertext []byte) {
   240  	subtle.WithDataIndependentTiming(func() { sharedKey, ciphertext = ek.key.Encapsulate() })
   241  	return
   242  }
   243  

View as plain text