1
2
3
4
5
6 package mlkemtest
7
8 import (
9 fips140mlkem "crypto/internal/fips140/mlkem"
10 "crypto/internal/fips140only"
11 "crypto/mlkem"
12 "errors"
13 )
14
15
16
17
18
19
20 func Encapsulate768(ek *mlkem.EncapsulationKey768, random []byte) (sharedKey, ciphertext []byte, err error) {
21 if len(random) != 32 {
22 return nil, nil, errors.New("mlkemtest: Encapsulate768: random must be 32 bytes")
23 }
24 if fips140only.Enforced() {
25 return nil, nil, errors.New("crypto/mlkem/mlkemtest: use of derandomized encapsulation is not allowed in FIPS 140-only mode")
26 }
27 k, err := fips140mlkem.NewEncapsulationKey768(ek.Bytes())
28 if err != nil {
29 return nil, nil, errors.New("mlkemtest: Encapsulate768: failed to reconstruct key: " + err.Error())
30 }
31 sharedKey, ciphertext = k.EncapsulateInternal((*[32]byte)(random))
32 return sharedKey, ciphertext, nil
33 }
34
35
36
37
38
39
40 func Encapsulate1024(ek *mlkem.EncapsulationKey1024, random []byte) (sharedKey, ciphertext []byte, err error) {
41 if len(random) != 32 {
42 return nil, nil, errors.New("mlkemtest: Encapsulate1024: random must be 32 bytes")
43 }
44 if fips140only.Enforced() {
45 return nil, nil, errors.New("crypto/mlkem/mlkemtest: use of derandomized encapsulation is not allowed in FIPS 140-only mode")
46 }
47 k, err := fips140mlkem.NewEncapsulationKey1024(ek.Bytes())
48 if err != nil {
49 return nil, nil, errors.New("mlkemtest: Encapsulate1024: failed to reconstruct key: " + err.Error())
50 }
51 sharedKey, ciphertext = k.EncapsulateInternal((*[32]byte)(random))
52 return sharedKey, ciphertext, nil
53 }
54
View as plain text