Skip to content

Commit

Permalink
hmac
Browse files Browse the repository at this point in the history
  • Loading branch information
ms committed Mar 22, 2024
1 parent 136ff53 commit 3733d61
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"errors"
"io"
)

// AES encryption
// AES encryption with HMAC
func EncryptAES(key, message []byte) ([]byte, error) {
// Initialize block cipher
block, err := aes.NewCipher(key)
Expand All @@ -18,9 +21,9 @@ func EncryptAES(key, message []byte) ([]byte, error) {
// Create the byte slice that will hold encrypted message
cipherText := make([]byte, aes.BlockSize+len(message))

// Generate the Initialization Vector (# IV) nonce which is
// stored at the beginning of the byte slice. The IV is the same
// length as the AES blocksize
// Generate the Initialization Vector nonce which is
// stored at the beginning of the byte slice.
// The IV is the same length as the AES blocksize
iv := cipherText[:aes.BlockSize]
_, err = io.ReadFull(rand.Reader, iv)
if err != nil {
Expand All @@ -29,21 +32,47 @@ func EncryptAES(key, message []byte) ([]byte, error) {

// Create the AES cipher stream
cfb := cipher.NewCFBEncrypter(block, iv)
// Generate the encrypted message and store it in the remaining
// bytes after the IV nonce

// Generate the encrypted message and store it in
// the remaining bytes after the IV nonce
cfb.XORKeyStream(cipherText[aes.BlockSize:], message)

// Create a new HMAC
h := hmac.New(sha256.New, key)

// Write the ciphertext to the HMAC
h.Write(cipherText)

// Append the HMAC to the ciphertext
cipherText = h.Sum(cipherText)

return cipherText, nil
}

// AES decryption
// AES decryption with HMAC
func DecryptAES(key, cipherText []byte) ([]byte, error) {
// Initialize block cipher
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}

// Separate the HMAC from the ciphertext
hmacSize := sha256.Size
if len(cipherText) < hmacSize {
return nil, errors.New("ciphertext too short")
}
msgHMAC := cipherText[len(cipherText)-hmacSize:]
cipherText = cipherText[:len(cipherText)-hmacSize]

// Verify the HMAC
h := hmac.New(sha256.New, key)
h.Write(cipherText)
expectedHMAC := h.Sum(nil)
if !hmac.Equal(msgHMAC, expectedHMAC) {
return nil, errors.New("HMAC verification failed")
}

// Separate the IV nonce from the encrypted message bytes
iv := cipherText[:aes.BlockSize]
cipherText = cipherText[aes.BlockSize:]
Expand Down

0 comments on commit 3733d61

Please sign in to comment.