github.com/jasoet/gopki@v1.15.0/encryption/asymmetric/rsa.go (about) 1 package asymmetric 2 3 import ( 4 "crypto/rand" 5 "crypto/rsa" 6 "crypto/sha256" 7 "fmt" 8 "time" 9 10 "github.com/jasoet/gopki/encryption" 11 "github.com/jasoet/gopki/keypair/algo" 12 ) 13 14 // EncryptWithRSA encrypts data using RSA-OAEP (Optimal Asymmetric Encryption Padding). 15 // 16 // This function provides direct RSA encryption using OAEP padding with SHA-256 hash 17 // function. RSA-OAEP is semantically secure and prevents various padding oracle 18 // attacks that affect older RSA padding schemes. 19 // 20 // Data size limitations: 21 // 22 // For a k-bit RSA key, the maximum plaintext size is approximately: 23 // - 2048-bit key: ~190 bytes 24 // - 3072-bit key: ~318 bytes 25 // - 4096-bit key: ~446 bytes 26 // 27 // Parameters: 28 // - data: The plaintext data to encrypt (must fit within RSA size limits) 29 // - keyPair: The RSA key pair (public key will be used for encryption) 30 // - opts: Encryption options (algorithm will be set to AlgorithmRSAOAEP) 31 // 32 // Returns: 33 // - *encryption.EncryptedData: Encrypted data with RSA-OAEP algorithm metadata 34 // - error: encryption.ErrDataTooLarge if data exceeds RSA capacity, or other encryption errors 35 // 36 // Security properties: 37 // - Semantic security: identical plaintexts produce different ciphertexts 38 // - Chosen ciphertext security under standard assumptions 39 // - Protection against padding oracle attacks 40 // 41 // Example: 42 // 43 // rsaKeys, _ := algo.GenerateRSAKeyPair(2048) 44 // data := []byte("small secret message") // Must be ≤190 bytes for 2048-bit key 45 // 46 // encrypted, err := asymmetric.EncryptWithRSA(data, rsaKeys, opts) 47 // if err != nil { 48 // log.Fatal("RSA encryption failed:", err) 49 // } 50 func EncryptWithRSA(data []byte, keyPair *algo.RSAKeyPair, opts encryption.EncryptOptions) (*encryption.EncryptedData, error) { 51 if err := encryption.ValidateEncryptOptions(opts); err != nil { 52 return nil, err 53 } 54 55 // RSA-OAEP has data size limitations 56 maxDataSize := keyPair.PublicKey.Size() - 2*sha256.Size - 2 57 if len(data) > maxDataSize { 58 return nil, fmt.Errorf("%w: data size %d exceeds maximum %d for RSA key size %d", 59 encryption.ErrDataTooLarge, len(data), maxDataSize, keyPair.PublicKey.Size()*8) 60 } 61 62 // Encrypt using RSA-OAEP with SHA-256 63 encrypted, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, keyPair.PublicKey, data, nil) 64 if err != nil { 65 return nil, fmt.Errorf("RSA-OAEP encryption failed: %w", err) 66 } 67 68 return &encryption.EncryptedData{ 69 Algorithm: encryption.AlgorithmRSAOAEP, 70 Format: opts.Format, 71 Data: encrypted, 72 Timestamp: time.Now(), 73 Metadata: opts.Metadata, 74 }, nil 75 } 76 77 // DecryptWithRSA decrypts RSA-OAEP encrypted data. 78 // 79 // Parameters: 80 // - encrypted: The encrypted data to decrypt 81 // - keyPair: The RSA key pair (private key will be used for decryption) 82 // - opts: Decryption options 83 // 84 // Returns: 85 // - []byte: The decrypted plaintext data 86 // - error: Any error that occurred during decryption 87 // 88 // Example: 89 // 90 // plaintext, err := asymmetric.DecryptWithRSA(encrypted, rsaKeys, opts) 91 func DecryptWithRSA(encrypted *encryption.EncryptedData, keyPair *algo.RSAKeyPair, opts encryption.DecryptOptions) ([]byte, error) { 92 if err := encryption.ValidateDecryptOptions(opts); err != nil { 93 return nil, err 94 } 95 96 if encrypted.Algorithm != encryption.AlgorithmRSAOAEP { 97 return nil, fmt.Errorf("expected RSA-OAEP algorithm, got %s", encrypted.Algorithm) 98 } 99 100 // Decrypt using RSA-OAEP with SHA-256 101 decrypted, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, keyPair.PrivateKey, encrypted.Data, nil) 102 if err != nil { 103 return nil, fmt.Errorf("RSA-OAEP decryption failed: %w", err) 104 } 105 106 return decrypted, nil 107 }