-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90d2bce
commit 34a1fd3
Showing
5 changed files
with
139 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# OpenSSL GCM | ||
AES GCM Encryption/Decryption I/O streams | ||
|
||
## See | ||
* https://github.com/minio/sio/ | ||
|
||
## Credits | ||
* https://github.com/aws/aws-sdk-go/blob/master/service/s3/s3crypto/aes_gcm.go | ||
* https://github.com/catalyzeio/gcm/blob/master/gcm/gcm.go | ||
* https://github.com/spacemonkeygo/openssl/blob/master/ciphers_test.go | ||
* https://github.com/marcopaganini/iocrypt/blob/master/iocrypt.go | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
aesgcm "github.com/hbakhtiyor/openssl_gcm" | ||
) | ||
|
||
// DecryptFile decrypts the file at the specified path using GCM. | ||
func DecryptFile(inFilePath, outFilePath string, key, iv, aad []byte) error { | ||
stat, err := os.Stat(inFilePath) | ||
if os.IsNotExist(err) { | ||
return fmt.Errorf("A file does not exist at %s", inFilePath) | ||
} else if err != nil { | ||
return err | ||
} | ||
|
||
inFile, err := os.Open(inFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer inFile.Close() | ||
|
||
outFile, err := os.Create(outFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer outFile.Close() | ||
|
||
r, err := aesgcm.NewGcmDecryptReader(inFile, key, iv, aad, stat.Size()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = io.Copy(outFile, r) | ||
return err | ||
} | ||
|
||
func main() { | ||
inFilePath := "testdata.enc" | ||
outFilePath := "testdata" | ||
key, _ := hex.DecodeString("81be5e09c111576103a8507658d47891") | ||
iv, _ := hex.DecodeString("81be5e09c111576103a85076") | ||
|
||
if err := DecryptFile(inFilePath, outFilePath, key, iv, nil); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package openssl_gcm | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/spacemonkeygo/openssl" | ||
) | ||
|
||
type gcmEncryptReader struct { | ||
src io.Reader | ||
ctx openssl.AuthenticatedEncryptionCipherCtx | ||
eof bool | ||
buf *bytes.Buffer // for finalize small bytes | ||
} | ||
|
||
func NewGcmEncryptReader(r io.Reader, key, iv, aad []byte) (*gcmEncryptReader, error) { | ||
ctx, err := openssl.NewGCMEncryptionCipherCtx(len(key)*8, nil, key, iv) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed making GCM encryption ctx: %v", err) | ||
} | ||
|
||
if len(aad) > 0 { | ||
err = ctx.ExtraData(aad) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to add authenticated data: %v", err) | ||
} | ||
} | ||
|
||
return &gcmEncryptReader{ | ||
src: r, | ||
ctx: ctx, | ||
buf: &bytes.Buffer{}, | ||
}, nil | ||
} | ||
|
||
func (r *gcmEncryptReader) Read(p []byte) (int, error) { | ||
if r.eof { | ||
return r.buf.Read(p) | ||
} | ||
|
||
n, err := r.src.Read(p) | ||
|
||
if err == io.EOF { | ||
data, err := r.ctx.EncryptFinal() | ||
if err != nil { | ||
return len(data), fmt.Errorf("Failed to finalize encryption: %v", err) | ||
} | ||
r.buf.Write(data) | ||
|
||
tag, err := r.ctx.GetTag() | ||
if err != nil { | ||
return len(data) + len(tag), fmt.Errorf("Failed to get GCM tag: %v", err) | ||
} | ||
r.buf.Write(tag) | ||
|
||
r.eof = true | ||
|
||
return r.buf.Read(p) | ||
} else if err != nil { | ||
return n, err | ||
} | ||
|
||
data, err := r.ctx.EncryptUpdate(p[:n]) | ||
if err != nil { | ||
return len(data), fmt.Errorf("Failed to perform an encryption: %v", err) | ||
} | ||
copy(p, data) | ||
|
||
return n, nil | ||
} |