kex_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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
  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, fail := skB.UAKEResponderShared(rand.Reader, stateA.Message)
  44. require.Equal(0, fail, "UAKEResponderShared(): fail")
  45. require.Len(msgB, p.UAKEResponderMessageSize(), "UAKEResponderShared(): msgB Length")
  46. require.Len(ssB, SymSize, "UAKEResponderShared(): ssB Length")
  47. // Create the initiator shared secret.
  48. ssA, fail := stateA.Shared(msgB)
  49. require.Equal(0, fail, "stateA.Shared(): fail")
  50. require.Equal(ssA, ssB, "Shared secret mismatch")
  51. }
  52. }
  53. func doTestAKE(t *testing.T, p *ParameterSet) {
  54. require := require.New(t)
  55. t.Logf("AKEInitiatorMessageSize(): %v", p.AKEInitiatorMessageSize())
  56. t.Logf("AKEResponderMessageSize(): %v", p.AKEResponderMessageSize())
  57. for i := 0; i < nTests; i++ {
  58. // Generate the initiator and responder key pairs.
  59. pkB, skB, err := p.GenerateKeyPair(rand.Reader)
  60. require.NoError(err, "GenerateKeyPair(): Responder")
  61. pkA, skA, err := p.GenerateKeyPair(rand.Reader)
  62. require.NoError(err, "GenerateKeyPair(): Initiator")
  63. // Create the initiator state.
  64. stateA, err := pkB.NewAKEInitiatorState(rand.Reader)
  65. require.NoError(err, "NewAKEInitiatorState()")
  66. require.Len(stateA.Message, p.AKEInitiatorMessageSize(), "stateA.Message: Length")
  67. // Create the responder message and shared secret.
  68. msgB, ssB, fail := skB.AKEResponderShared(rand.Reader, stateA.Message, pkA)
  69. require.Equal(0, fail, "AKEResponderShared(): fail")
  70. require.Len(msgB, p.AKEResponderMessageSize(), "AKEResponderShared(): msgB Length")
  71. require.Len(ssB, SymSize, "AKEResponderShared(): ssB Length")
  72. // Create the initiator shared secret.
  73. ssA, fail := stateA.Shared(msgB, skA)
  74. require.Equal(0, fail, "stateA.Shared(): fail")
  75. require.Equal(ssA, ssB, "Shared secret mismatch")
  76. }
  77. }