kex_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // kex_test.go - Kyber key exchange tests.
  2. //
  3. // To the extent possible under law, Yawning Angel has waived all copyright
  4. // and related or neighboring rights to the software, using the Creative
  5. // Commons "CC0" public domain dedication. See LICENSE or
  6. // <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
  7. package kyber
  8. import (
  9. "crypto/rand"
  10. "testing"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestAKE(t *testing.T) {
  14. forceDisableHardwareAcceleration()
  15. doTestKEX(t)
  16. if !canAccelerate {
  17. t.Log("Hardware acceleration not supported on this host.")
  18. return
  19. }
  20. mustInitHardwareAcceleration()
  21. doTestKEX(t)
  22. }
  23. func doTestKEX(t *testing.T) {
  24. impl := "_" + hardwareAccelImpl.name
  25. for _, p := range allParams {
  26. t.Run(p.Name()+"_UAKE"+impl, func(t *testing.T) { doTestUAKE(t, p) })
  27. t.Run(p.Name()+"_AKE"+impl, func(t *testing.T) { doTestAKE(t, p) })
  28. }
  29. }
  30. func doTestUAKE(t *testing.T, p *ParameterSet) {
  31. require := require.New(t)
  32. t.Logf("UAKEInitiatorMessageSize(): %v", p.UAKEInitiatorMessageSize())
  33. t.Logf("UAKEResponderMessageSize(): %v", p.UAKEResponderMessageSize())
  34. for i := 0; i < nTests; i++ {
  35. // Generate the responder key pair.
  36. pkB, skB, err := p.GenerateKeyPair(rand.Reader)
  37. require.NoError(err, "GenerateKeyPair()")
  38. // Create the initiator state.
  39. stateA, err := pkB.NewUAKEInitiatorState(rand.Reader)
  40. require.NoError(err, "NewUAKEInitiatorState()")
  41. require.Len(stateA.Message, p.UAKEInitiatorMessageSize(), "stateA.Message: Length")
  42. // Create the responder message and shared secret.
  43. msgB, ssB := skB.UAKEResponderShared(rand.Reader, stateA.Message)
  44. require.Len(msgB, p.UAKEResponderMessageSize(), "UAKEResponderShared(): msgB Length")
  45. require.Len(ssB, SymSize, "UAKEResponderShared(): ssB Length")
  46. // Create the initiator shared secret.
  47. ssA := stateA.Shared(msgB)
  48. require.Equal(ssA, ssB, "Shared secret mismatch")
  49. }
  50. }
  51. func doTestAKE(t *testing.T, p *ParameterSet) {
  52. require := require.New(t)
  53. t.Logf("AKEInitiatorMessageSize(): %v", p.AKEInitiatorMessageSize())
  54. t.Logf("AKEResponderMessageSize(): %v", p.AKEResponderMessageSize())
  55. for i := 0; i < nTests; i++ {
  56. // Generate the initiator and responder key pairs.
  57. pkB, skB, err := p.GenerateKeyPair(rand.Reader)
  58. require.NoError(err, "GenerateKeyPair(): Responder")
  59. pkA, skA, err := p.GenerateKeyPair(rand.Reader)
  60. require.NoError(err, "GenerateKeyPair(): Initiator")
  61. // Create the initiator state.
  62. stateA, err := pkB.NewAKEInitiatorState(rand.Reader)
  63. require.NoError(err, "NewAKEInitiatorState()")
  64. require.Len(stateA.Message, p.AKEInitiatorMessageSize(), "stateA.Message: Length")
  65. // Create the responder message and shared secret.
  66. msgB, ssB := skB.AKEResponderShared(rand.Reader, stateA.Message, pkA)
  67. require.Len(msgB, p.AKEResponderMessageSize(), "AKEResponderShared(): msgB Length")
  68. require.Len(ssB, SymSize, "AKEResponderShared(): ssB Length")
  69. // Create the initiator shared secret.
  70. ssA := stateA.Shared(msgB, skA)
  71. require.Equal(ssA, ssB, "Shared secret mismatch")
  72. }
  73. }