You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

113 lines
4.0 KiB

  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package conn
  19. import (
  20. "testing"
  21. core "google.golang.org/grpc/credentials/alts/internal"
  22. )
  23. // getGCMCryptoPair outputs a client/server pair on aes128gcmRekey.
  24. func getRekeyCryptoPair(key []byte, counter []byte, t *testing.T) (ALTSRecordCrypto, ALTSRecordCrypto) {
  25. client, err := NewAES128GCMRekey(core.ClientSide, key)
  26. if err != nil {
  27. t.Fatalf("NewAES128GCMRekey(ClientSide, key) = %v", err)
  28. }
  29. server, err := NewAES128GCMRekey(core.ServerSide, key)
  30. if err != nil {
  31. t.Fatalf("NewAES128GCMRekey(ServerSide, key) = %v", err)
  32. }
  33. // set counter if provided.
  34. if counter != nil {
  35. if CounterSide(counter) == core.ClientSide {
  36. client.(*aes128gcmRekey).outCounter = CounterFromValue(counter, overflowLenAES128GCMRekey)
  37. server.(*aes128gcmRekey).inCounter = CounterFromValue(counter, overflowLenAES128GCMRekey)
  38. } else {
  39. server.(*aes128gcmRekey).outCounter = CounterFromValue(counter, overflowLenAES128GCMRekey)
  40. client.(*aes128gcmRekey).inCounter = CounterFromValue(counter, overflowLenAES128GCMRekey)
  41. }
  42. }
  43. return client, server
  44. }
  45. func testRekeyEncryptRoundtrip(client ALTSRecordCrypto, server ALTSRecordCrypto, t *testing.T) {
  46. // Encrypt.
  47. const plaintext = "This is plaintext."
  48. var err error
  49. buf := []byte(plaintext)
  50. buf, err = client.Encrypt(buf[:0], buf)
  51. if err != nil {
  52. t.Fatal("Encrypting with client-side context: unexpected error", err, "\n",
  53. "Plaintext:", []byte(plaintext))
  54. }
  55. // Encrypt a second message.
  56. const plaintext2 = "This is a second plaintext."
  57. buf2 := []byte(plaintext2)
  58. buf2, err = client.Encrypt(buf2[:0], buf2)
  59. if err != nil {
  60. t.Fatal("Encrypting with client-side context: unexpected error", err, "\n",
  61. "Plaintext:", []byte(plaintext2))
  62. }
  63. // Decryption fails: cannot decrypt second message before first.
  64. if got, err := server.Decrypt(nil, buf2); err == nil {
  65. t.Error("Decrypting client-side ciphertext with a client-side context unexpectedly succeeded; want unexpected counter error:\n",
  66. " Original plaintext:", []byte(plaintext2), "\n",
  67. " Ciphertext:", buf2, "\n",
  68. " Decrypted plaintext:", got)
  69. }
  70. // Decryption fails: wrong counter space.
  71. if got, err := client.Decrypt(nil, buf); err == nil {
  72. t.Error("Decrypting client-side ciphertext with a client-side context unexpectedly succeeded; want counter space error:\n",
  73. " Original plaintext:", []byte(plaintext), "\n",
  74. " Ciphertext:", buf, "\n",
  75. " Decrypted plaintext:", got)
  76. }
  77. // Decrypt first message.
  78. ciphertext := append([]byte(nil), buf...)
  79. buf, err = server.Decrypt(buf[:0], buf)
  80. if err != nil || string(buf) != plaintext {
  81. t.Fatal("Decrypting client-side ciphertext with a server-side context did not produce original content:\n",
  82. " Original plaintext:", []byte(plaintext), "\n",
  83. " Ciphertext:", ciphertext, "\n",
  84. " Decryption error:", err, "\n",
  85. " Decrypted plaintext:", buf)
  86. }
  87. // Decryption fails: replay attack.
  88. if got, err := server.Decrypt(nil, buf); err == nil {
  89. t.Error("Decrypting client-side ciphertext with a client-side context unexpectedly succeeded; want unexpected counter error:\n",
  90. " Original plaintext:", []byte(plaintext), "\n",
  91. " Ciphertext:", buf, "\n",
  92. " Decrypted plaintext:", got)
  93. }
  94. }
  95. // Test encrypt and decrypt on roundtrip messages for aes128gcmRekey.
  96. func TestAES128GCMRekeyEncryptRoundtrip(t *testing.T) {
  97. // Test for aes128gcmRekey.
  98. key := make([]byte, 44)
  99. client, server := getRekeyCryptoPair(key, nil, t)
  100. testRekeyEncryptRoundtrip(client, server, t)
  101. }