Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional Padding in the decrypted string when using the golang utilities for signing and verification #58

Open
Avijeet-Blocsol opened this issue Jan 15, 2024 · 3 comments

Comments

@Avijeet-Blocsol
Copy link

During the onboarding process, I used the golang utilities for encrypting and decrypting which can be found on this URL: https://github.com/ONDC-Official/reference-implementations/blob/main/utilities/signing_and_verification/golang/crypto.go#L44

There is a bug when using the decrypt function on the incoming challenge in the on_subscribe call from the gateway. The decryption process does not result in any errors but there is some extra padding at the end of the decrypted string returned from the function. If you return this string to the gateway in the response body under "answer" key, the gateway throws "Encryption verification is failed" error.

To fix the problem, I propose the following changes to the aesDecrypt and the aesEncrypt functions in the utility file:

func aesDecrypt(cipherText []byte, key []byte) ([]byte, error) {
cipher, err := aes.NewCipher(key)
if err != nil {
fmt.Println("Error creating AES cipher", err)
return nil, err
}

blockSize := cipher.BlockSize()
decrypted := make([]byte, len(cipherText))
for i := 0; i < len(cipherText); i += blockSize {
	cipher.Decrypt(decrypted[i:i+blockSize], cipherText[i:i+blockSize])
}
padding := decrypted[len(decrypted)-1]
return decrypted[:len(decrypted)-int(padding)], nil

}

func aesEncrypt(payload []byte, key []byte) ([]byte, error) {
cipher, err := aes.NewCipher(key)
if err != nil {
fmt.Println("Error creating AES cipher", err)
return nil, err
}

blockSize := cipher.BlockSize()
padding := blockSize - len(payload)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
payload = append(payload, padText...)

encrypted := make([]byte, len(payload))
for i := 0; i < len(payload); i += blockSize {
	cipher.Encrypt(encrypted[i:i+blockSize], payload[i:i+blockSize])
}

return encrypted, nil

}

This results in the encryption function properly using PKCS#7 scheme to properly add the padding and the decrypt function to use the same to remove the additional padding. This resolves the beforementioned bug.

You can also use a public package to do the same:

import "github.com/zenazn/pkcs7pad"

func aesEncrypt(payload []byte, key []byte) ([]byte, error) {
cipher, err := aes.NewCipher(key)
if err != nil {
fmt.Println("Error creating AES cipher", err)
return nil, err
}

// Pad the payload using PKCS#7 padding
paddedData := pkcs7.Pad(payload, cipher.BlockSize())

// Encrypt the padded data
encrypted := make([]byte, len(paddedData))
cipher.NewEncrypter(key).CryptBlocks(encrypted, paddedData)

return encrypted, nil

}

func aesDecrypt(cipherText []byte, key []byte) ([]byte, error) {
cipher, err := aes.NewCipher(key)
if err != nil {
fmt.Println("Error creating AES cipher", err)
return nil, err
}

// Decrypt the data
decrypted := make([]byte, len(cipherText))
cipher.NewDecrypter(key).CryptBlocks(decrypted, cipherText)

// Unpad the decrypted data using PKCS#7
unpaddedData, err := pkcs7.Unpad(decrypted)
if err != nil {
	fmt.Println("Error removing PKCS#7 padding:", err)
	return nil, err
}

return unpaddedData, nil

}

@bmarwaha-godaddy
Copy link

HI

I am facing same problem.. I tried ur code but it throws
2024/06/02 10:59:34 http: panic serving 10.0.8.139:36126: runtime error: slice bounds out of range [:-164]

padding := decrypted[len(decrypted)-1]
return decrypted[:len(decrypted)-int(padding)], nil

@Avijeet-Blocsol
Copy link
Author

Avijeet-Blocsol commented Jun 3, 2024 via email

@bmarwaha-godaddy
Copy link

where is the link to join meets?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants