aboutsummaryrefslogtreecommitdiff
path: root/internal/cryptocore/cryptocore.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2016-05-04 19:51:58 +0200
committerJakob Unterwurzacher2016-05-04 19:56:07 +0200
commit1bb907b38e1fefdfb4ad66f1d423a607477deb3c (patch)
tree2656ae4d20b881e943726fd4fc841fe82255a0ec /internal/cryptocore/cryptocore.go
parentbb16f2d565674153bd7c3781e23048586c73e0a8 (diff)
cryptocore: add API tests
Diffstat (limited to 'internal/cryptocore/cryptocore.go')
-rw-r--r--internal/cryptocore/cryptocore.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/internal/cryptocore/cryptocore.go b/internal/cryptocore/cryptocore.go
new file mode 100644
index 0000000..f286896
--- /dev/null
+++ b/internal/cryptocore/cryptocore.go
@@ -0,0 +1,57 @@
+package cryptocore
+
+import (
+ "crypto/aes"
+ "crypto/cipher"
+ "fmt"
+)
+
+const (
+ KeyLen = 32 // AES-256
+ AuthTagLen = 16
+)
+
+type CryptoCore struct {
+ BlockCipher cipher.Block
+ Gcm cipher.AEAD
+ GcmIVGen *nonceGenerator
+ IVLen int
+}
+
+// "New" returns a new CryptoCore object or panics.
+func New(key []byte, useOpenssl bool, GCMIV128 bool) *CryptoCore {
+
+ if len(key) != KeyLen {
+ panic(fmt.Sprintf("Unsupported key length %d", len(key)))
+ }
+
+ // We want the IV size in bytes
+ IVLen := 96 / 8
+ if GCMIV128 {
+ IVLen = 128 / 8
+ }
+
+ // We always use built-in Go crypto for blockCipher because it is not
+ // performance-critical.
+ blockCipher, err := aes.NewCipher(key)
+ if err != nil {
+ panic(err)
+ }
+
+ var gcm cipher.AEAD
+ if useOpenssl {
+ gcm = opensslGCM{key}
+ } else {
+ gcm, err = goGCMWrapper(blockCipher, IVLen)
+ if err != nil {
+ panic(err)
+ }
+ }
+
+ return &CryptoCore{
+ BlockCipher: blockCipher,
+ Gcm: gcm,
+ GcmIVGen: &nonceGenerator{nonceLen: IVLen},
+ IVLen: IVLen,
+ }
+}