Guest User

Untitled

a guest
Jan 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. go get gitlab.com/zfeldt/gencrypt
  2.  
  3. // NOTE: This is a wrapper around portions of the standard libraries crypto
  4. // package.
  5.  
  6. // NOTE: For those deploying on systems not equipped with CPUs supporting
  7. // AES-NI [0], you should be aware of possible bottle-necks when it comes to
  8. // the AES encryption process [1].
  9. // >>"Final caveat, all these recommendations apply only to the amd64
  10. // >> architecture, for which fast, constant time implementations of the crypto
  11. // >> primitives (AES-GCM, ChaCha20-Poly1305, P256) are available. Other
  12. // >> architectures are probably not fit for production use." [1]
  13. // [0] https://en.wikipedia.org/wiki/AES_instruction_set#New_instructions
  14. // [1] https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/
  15.  
  16. // Package gencrypt provides methods for encrypting and decrypting data with
  17. // the AES encryption method. Based on George Tankersley's talk at Gophercon
  18. // 2016.
  19. package gencrypt
  20.  
  21. import (
  22. "crypto/aes"
  23. "crypto/cipher"
  24. "crypto/rand"
  25. )
  26.  
  27. // Galois implements the cipher.AEAD interface type (Authenticated Encryption
  28. // with Associated Data), which allows us to seal and open streams of data,
  29. // check overhead, and check the nonce size.
  30. type Galois struct {
  31. GCM cipher.AEAD
  32. }
  33.  
  34. // NewGCM takes a key and returns a new Galois struct. A 32-byte key is used to
  35. // indicate AES-256. 16 and 24-byte keys are accepted for AES-128 and AES-192
  36. // respectively, but are not recommended.
  37. func NewGCM(key []byte) (*Galois, error) {
  38. g := &Galois{}
  39. // Here we retrieve a new cipher.Block using the key provided. block is a
  40. // 128-bit block cipher (cipher.Block) used for encrypting and decrypting
  41. // data in individual blocks. The mode implementations (e.g. Galois Counter
  42. // Mode) extend that capability to streams of blocks.
  43. block, err := aes.NewCipher(key[:])
  44. if err != nil {
  45. return g, err
  46. }
  47.  
  48. // We pass the cipher.Block to cipher.NewGCM() to retrieve a new GCM (Galois
  49. // Counter Mode).
  50. g.GCM, err = cipher.NewGCM(block)
  51. if err != nil {
  52. return g, err
  53. }
  54.  
  55. // We return the Galois struct containing the GCM so that it can be used for
  56. // encryption and decryption by the client.
  57. return g, nil
  58. }
  59.  
  60. // AESEncrypt is a method of the Galois struct which encrypts data using the
  61. // mode (GCM) and returns an encrypted []byte.
  62. func (g *Galois) AESEncrypt(data []byte) ([]byte, error) {
  63. // We use the gcm.NonceSize() method to create a byte slice with the
  64. // appropriate nonce length, then use the rand.Read() method to write random
  65. // bytes to the slice, thus creating our nonce.
  66. nonce := make([]byte, g.GCM.NonceSize())
  67. _, err := rand.Read(nonce)
  68. if err != nil {
  69. return nil, err
  70. }
  71.  
  72. // gcm.Seal() returns a []byte containing the encrypted data. The nonce is
  73. // used both as the dst []byte, which encrypted data is appended to, and to
  74. // derive the initial GCM counter state (for more details see the
  75. // cipher/gcm.go file in the Go source code).
  76. return g.GCM.Seal(nonce, nonce, data, nil), nil
  77. }
  78.  
  79. // AESDecrypt is a method of the Galois struct which decrypts data using the
  80. // mode (GCM) and returns a decrypted []byte, which can be converted to a type
  81. // (e.g. string) of the original data.
  82. func (g *Galois) AESDecrypt(data []byte) ([]byte, error) {
  83. // We return the decrypted data by passing it through gcm.Open(). Remember:
  84. // the data argument contains the nonce at the beginning of the slice, and
  85. // has the encrypted data appended after it, as seen below. The decrypted
  86. // data is returned as a []byte that can then be converted into its original
  87. // form.
  88. return g.GCM.Open(nil, data[:g.GCM.NonceSize()], data[g.GCM.NonceSize():], nil)
  89. }
  90.  
  91. gcm, _ := gencrypt.NewGCM(key)
  92. encryptedData, _ := gcm.AESEncrypt(data)
  93.  
  94. gcm, _ := gencrypt.NewGCM(key)
  95. encryptedData. _ := gencrypt.AESEncrypt(gcm, data) // this line changed
Add Comment
Please, Sign In to add comment