github.com/jasoet/gopki@v1.15.0/encryption/asymmetric/ed25519.go (about) 1 package asymmetric 2 3 import ( 4 "crypto/ecdh" 5 "crypto/rand" 6 "fmt" 7 "time" 8 9 "github.com/jasoet/gopki/encryption" 10 "github.com/jasoet/gopki/keypair/algo" 11 ) 12 13 // EncryptWithEd25519 encrypts data using X25519 key agreement + AES-GCM. 14 // 15 // Note: Ed25519 keys cannot be directly used for ECDH. This function uses a workaround 16 // by deriving an X25519 key from the Ed25519 private key seed for key agreement. 17 // 18 // This provides a bridge between Ed25519 signing keys and X25519 encryption keys, 19 // allowing the same key material to be used for both signing and encryption operations 20 // (though this is generally not recommended for security reasons). 21 // 22 // Process: 23 // 1. Extract seed from Ed25519 private key 24 // 2. Derive X25519 private key from the seed 25 // 3. Generate ephemeral X25519 key pair for forward secrecy 26 // 4. Perform X25519 key agreement to get shared secret 27 // 5. Derive AES-256 key from shared secret 28 // 6. Encrypt data with AES-GCM using derived key 29 // 30 // Parameters: 31 // - data: The plaintext data to encrypt (no size limitations) 32 // - keyPair: The Ed25519 key pair (converted to X25519 for encryption) 33 // - opts: Encryption options (algorithm will be set to AlgorithmX25519) 34 // 35 // Returns: 36 // - *encryption.EncryptedData: Encrypted data with X25519 algorithm metadata 37 // - error: Any error that occurred during key derivation, agreement, or encryption 38 // 39 // Security considerations: 40 // - Using Ed25519 keys for encryption is not standard practice 41 // - Perfect forward secrecy through ephemeral X25519 keys 42 // - Authenticated encryption via AES-GCM 43 // 44 // Example: 45 // 46 // ed25519Keys, _ := algo.GenerateEd25519KeyPair() 47 // data := []byte("data to encrypt") 48 // 49 // encrypted, err := asymmetric.EncryptWithEd25519(data, ed25519Keys, opts) 50 // if err != nil { 51 // log.Fatal("Ed25519 encryption failed:", err) 52 // } 53 func EncryptWithEd25519(data []byte, keyPair *algo.Ed25519KeyPair, opts encryption.EncryptOptions) (*encryption.EncryptedData, error) { 54 if err := encryption.ValidateEncryptOptions(opts); err != nil { 55 return nil, err 56 } 57 58 // Ed25519 keys can't be directly converted to X25519 for ECDH 59 // We need to use the private key seed to derive an X25519 key 60 curve := ecdh.X25519() 61 62 // Use the Ed25519 private key seed to derive X25519 private key 63 seed := keyPair.PrivateKey.Seed() 64 x25519PrivateKey, err := curve.NewPrivateKey(seed) 65 if err != nil { 66 return nil, fmt.Errorf("failed to create X25519 private key from Ed25519 seed: %w", err) 67 } 68 69 // Get the corresponding X25519 public key 70 x25519PublicKey := x25519PrivateKey.PublicKey() 71 72 // Generate ephemeral key pair for forward secrecy 73 ephemeralPrivateKey, err := curve.GenerateKey(rand.Reader) 74 if err != nil { 75 return nil, fmt.Errorf("failed to generate ephemeral X25519 key: %w", err) 76 } 77 78 // Perform X25519 key agreement between ephemeral key and derived X25519 public key 79 sharedSecret, err := ephemeralPrivateKey.ECDH(x25519PublicKey) 80 if err != nil { 81 return nil, fmt.Errorf("X25519 key agreement failed: %w", err) 82 } 83 84 // Derive AES key from shared secret 85 aesKey := deriveAESKey(sharedSecret) 86 87 // Encrypt data using AES-GCM 88 encryptedData, iv, tag, err := encryptAESGCM(data, aesKey) 89 if err != nil { 90 return nil, fmt.Errorf("AES-GCM encryption failed: %w", err) 91 } 92 93 return &encryption.EncryptedData{ 94 Algorithm: encryption.AlgorithmX25519, 95 Format: opts.Format, 96 Data: encryptedData, 97 EncryptedKey: ephemeralPrivateKey.PublicKey().Bytes(), // Store ephemeral public key 98 IV: iv, 99 Tag: tag, 100 Timestamp: time.Now(), 101 Metadata: opts.Metadata, 102 }, nil 103 } 104 105 // DecryptWithEd25519 decrypts X25519 + AES-GCM encrypted data. 106 // 107 // This function reverses the Ed25519 encryption process by: 108 // 1. Extracting seed from Ed25519 private key 109 // 2. Deriving the same X25519 private key used for encryption 110 // 3. Extracting sender's ephemeral X25519 public key from encrypted data 111 // 4. Performing X25519 key agreement to recover shared secret 112 // 5. Deriving the same AES key used for encryption 113 // 6. Decrypting data using AES-GCM 114 // 115 // Parameters: 116 // - encrypted: The encrypted data to decrypt (must contain ephemeral key) 117 // - keyPair: The Ed25519 key pair (converted to X25519 for decryption) 118 // - opts: Decryption options 119 // 120 // Returns: 121 // - []byte: The decrypted plaintext data 122 // - error: Any error that occurred during key derivation, agreement, or decryption 123 // 124 // Example: 125 // 126 // plaintext, err := asymmetric.DecryptWithEd25519(encrypted, ed25519Keys, opts) 127 func DecryptWithEd25519(encrypted *encryption.EncryptedData, keyPair *algo.Ed25519KeyPair, opts encryption.DecryptOptions) ([]byte, error) { 128 if err := encryption.ValidateDecryptOptions(opts); err != nil { 129 return nil, err 130 } 131 132 if encrypted.Algorithm != encryption.AlgorithmX25519 { 133 return nil, fmt.Errorf("expected X25519 algorithm, got %s", encrypted.Algorithm) 134 } 135 136 // Derive X25519 private key from Ed25519 private key seed 137 // This must match the derivation used during encryption 138 curve := ecdh.X25519() 139 seed := keyPair.PrivateKey.Seed() 140 x25519PrivateKey, err := curve.NewPrivateKey(seed) 141 if err != nil { 142 return nil, fmt.Errorf("failed to create X25519 private key from Ed25519 seed: %w", err) 143 } 144 145 // Parse ephemeral public key from encrypted data 146 ephemeralPublicKey, err := curve.NewPublicKey(encrypted.EncryptedKey) 147 if err != nil { 148 return nil, fmt.Errorf("failed to parse ephemeral public key: %w", err) 149 } 150 151 // Perform X25519 key agreement between our derived private key and ephemeral public key 152 sharedSecret, err := x25519PrivateKey.ECDH(ephemeralPublicKey) 153 if err != nil { 154 return nil, fmt.Errorf("X25519 key agreement failed: %w", err) 155 } 156 157 // Derive AES key from shared secret (same derivation as encryption) 158 aesKey := deriveAESKey(sharedSecret) 159 160 // Decrypt data using AES-GCM 161 decrypted, err := decryptAESGCM(encrypted.Data, aesKey, encrypted.IV, encrypted.Tag) 162 if err != nil { 163 return nil, fmt.Errorf("AES-GCM decryption failed: %w", err) 164 } 165 166 return decrypted, nil 167 }