| 1 |
|
| 2 |
|
| 3 |
package mlkemtest |
| 4 |
|
| 5 |
import ( |
| 6 |
"crypto/mlkem" |
| 7 |
"errors" |
| 8 |
"unsafe" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
func Encapsulate768(ek *mlkem.EncapsulationKey768, rand []byte) (sharedKey, ciphertext []byte, err error) { |
| 15 |
if len(rand) != 32 { |
| 16 |
return nil, nil, errors.New("invalid ML-KEM-768 randomness size") |
| 17 |
} |
| 18 |
key := (*mlkem768EncapsulationKey)(unsafe.Pointer(ek)) |
| 19 |
sharedKey, ciphertext = mlkem768EncapsulateInternal(key.key, (*[32]byte)(rand)) |
| 20 |
return sharedKey, ciphertext, nil |
| 21 |
} |
| 22 |
|
| 23 |
type mlkem768EncapsulationKey struct { |
| 24 |
key unsafe.Pointer |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
func mlkem768EncapsulateInternal(ek unsafe.Pointer, m *[32]byte) (sharedKey, ciphertext []byte) |
| 29 |
|
| 30 |
func Encapsulate1024(ek *mlkem.EncapsulationKey1024, rand []byte) (sharedKey, ciphertext []byte, err error) { |
| 31 |
if len(rand) != 32 { |
| 32 |
return nil, nil, errors.New("invalid ML-KEM-1024 randomness size") |
| 33 |
} |
| 34 |
key := (*mlkem1024EncapsulationKey)(unsafe.Pointer(ek)) |
| 35 |
sharedKey, ciphertext = mlkem1024EncapsulateInternal(key.key, (*[32]byte)(rand)) |
| 36 |
return sharedKey, ciphertext, nil |
| 37 |
} |
| 38 |
|
| 39 |
type mlkem1024EncapsulationKey struct { |
| 40 |
key unsafe.Pointer |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func mlkem1024EncapsulateInternal(ek unsafe.Pointer, m *[32]byte) (sharedKey, ciphertext []byte) |