encrypted_key_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package packet
  5. import (
  6. "bytes"
  7. "crypto/rsa"
  8. "fmt"
  9. "math/big"
  10. "testing"
  11. )
  12. func bigFromBase10(s string) *big.Int {
  13. b, ok := new(big.Int).SetString(s, 10)
  14. if !ok {
  15. panic("bigFromBase10 failed")
  16. }
  17. return b
  18. }
  19. var encryptedKeyPub = rsa.PublicKey{
  20. E: 65537,
  21. N: bigFromBase10("115804063926007623305902631768113868327816898845124614648849934718568541074358183759250136204762053879858102352159854352727097033322663029387610959884180306668628526686121021235757016368038585212410610742029286439607686208110250133174279811431933746643015923132833417396844716207301518956640020862630546868823"),
  22. }
  23. var encryptedKeyRSAPriv = &rsa.PrivateKey{
  24. PublicKey: encryptedKeyPub,
  25. D: bigFromBase10("32355588668219869544751561565313228297765464314098552250409557267371233892496951383426602439009993875125222579159850054973310859166139474359774543943714622292329487391199285040721944491839695981199720170366763547754915493640685849961780092241140181198779299712578774460837139360803883139311171713302987058393"),
  26. }
  27. var encryptedKeyPriv = &PrivateKey{
  28. PublicKey: PublicKey{
  29. PubKeyAlgo: PubKeyAlgoRSA,
  30. },
  31. PrivateKey: encryptedKeyRSAPriv,
  32. }
  33. func TestDecryptingEncryptedKey(t *testing.T) {
  34. const encryptedKeyHex = "c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8"
  35. const expectedKeyHex = "d930363f7e0308c333b9618617ea728963d8df993665ae7be1092d4926fd864b"
  36. p, err := Read(readerFromHex(encryptedKeyHex))
  37. if err != nil {
  38. t.Errorf("error from Read: %s", err)
  39. return
  40. }
  41. ek, ok := p.(*EncryptedKey)
  42. if !ok {
  43. t.Errorf("didn't parse an EncryptedKey, got %#v", p)
  44. return
  45. }
  46. if ek.KeyId != 0x2a67d68660df41c7 || ek.Algo != PubKeyAlgoRSA {
  47. t.Errorf("unexpected EncryptedKey contents: %#v", ek)
  48. return
  49. }
  50. err = ek.Decrypt(encryptedKeyPriv, nil)
  51. if err != nil {
  52. t.Errorf("error from Decrypt: %s", err)
  53. return
  54. }
  55. if ek.CipherFunc != CipherAES256 {
  56. t.Errorf("unexpected EncryptedKey contents: %#v", ek)
  57. return
  58. }
  59. keyHex := fmt.Sprintf("%x", ek.Key)
  60. if keyHex != expectedKeyHex {
  61. t.Errorf("bad key, got %s want %x", keyHex, expectedKeyHex)
  62. }
  63. }
  64. func TestEncryptingEncryptedKey(t *testing.T) {
  65. key := []byte{1, 2, 3, 4}
  66. const expectedKeyHex = "01020304"
  67. const keyId = 42
  68. pub := &PublicKey{
  69. PublicKey: &encryptedKeyPub,
  70. KeyId: keyId,
  71. PubKeyAlgo: PubKeyAlgoRSAEncryptOnly,
  72. }
  73. buf := new(bytes.Buffer)
  74. err := SerializeEncryptedKey(buf, pub, CipherAES128, key, nil)
  75. if err != nil {
  76. t.Errorf("error writing encrypted key packet: %s", err)
  77. }
  78. p, err := Read(buf)
  79. if err != nil {
  80. t.Errorf("error from Read: %s", err)
  81. return
  82. }
  83. ek, ok := p.(*EncryptedKey)
  84. if !ok {
  85. t.Errorf("didn't parse an EncryptedKey, got %#v", p)
  86. return
  87. }
  88. if ek.KeyId != keyId || ek.Algo != PubKeyAlgoRSAEncryptOnly {
  89. t.Errorf("unexpected EncryptedKey contents: %#v", ek)
  90. return
  91. }
  92. err = ek.Decrypt(encryptedKeyPriv, nil)
  93. if err != nil {
  94. t.Errorf("error from Decrypt: %s", err)
  95. return
  96. }
  97. if ek.CipherFunc != CipherAES128 {
  98. t.Errorf("unexpected EncryptedKey contents: %#v", ek)
  99. return
  100. }
  101. keyHex := fmt.Sprintf("%x", ek.Key)
  102. if keyHex != expectedKeyHex {
  103. t.Errorf("bad key, got %s want %x", keyHex, expectedKeyHex)
  104. }
  105. }