Skip to content

Commit 2ccf3e5

Browse files
committed
Fix fomratting
Signed-off-by: Dusan Borovcanin <[email protected]>
1 parent 79bb158 commit 2ccf3e5

File tree

4 files changed

+12
-25
lines changed

4 files changed

+12
-25
lines changed

crypto/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- `aes_encrypt(string, string, string, string)` - return AES encrypted hex-encoded ciphertext
77
- `aes_decrypt(string, string, string, string)` - return AES decrypted hex-encoded plain text
88

9-
AES support 3 modes: GCM, CBC, and CTR - first parameter is mode, second is key, third is initialization vector or nonce - depending on the mode, and forth is plain text or ciphertext.
9+
AES support 3 modes: GCM, CBC, and CTR - first parameter is mode, second is hex-encoded key, third is hex-encoded initialization vector or nonce - depending on the mode, and forth is hex-encoded plain text or ciphertext.
1010

1111
## Examples
1212

crypto/aes.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ func decryptAES(m mode, key, init, ciphertext []byte) ([]byte, error) {
142142
mode := cipher.NewCBCDecrypter(block, init)
143143
plaintext := make([]byte, len(ciphertext))
144144
mode.CryptBlocks(plaintext, ciphertext)
145-
return unpad(plaintext, aes.BlockSize)
145+
// Padding reversal is intentionally delegated to the application layer.
146+
// On constrained devices with fixed-length payloads, padding is sometimes omitted
147+
// to avoid unnecessary processing load and data overhead.
148+
return plaintext, nil
146149
case CTR:
147150
stream := cipher.NewCTR(block, init)
148151
plaintext := make([]byte, len(ciphertext))
@@ -158,19 +161,3 @@ func pad(data []byte, blockSize int) []byte {
158161
padding := bytes.Repeat([]byte{byte(padLen)}, padLen)
159162
return append(data, padding...)
160163
}
161-
162-
func unpad(data []byte, blockSize int) ([]byte, error) {
163-
if len(data) == 0 || len(data)%blockSize != 0 {
164-
return nil, fmt.Errorf("invalid padding size")
165-
}
166-
padLen := int(data[len(data)-1])
167-
if padLen == 0 || padLen > blockSize {
168-
return nil, fmt.Errorf("invalid padding")
169-
}
170-
for i := 0; i < padLen; i++ {
171-
if data[len(data)-1-i] != byte(padLen) {
172-
return nil, fmt.Errorf("invalid padding byte")
173-
}
174-
}
175-
return data[:len(data)-padLen], nil
176-
}

crypto/loader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import lua "github.com/yuin/gopher-lua"
55
// Preload adds crypto to the given Lua state's package.preload table. After it
66
// has been preloaded, it can be loaded using require:
77
//
8-
// local crypto = require("crypto")
8+
// local crypto = require("crypto")
99
func Preload(L *lua.LState) {
1010
L.PreloadModule("crypto", Loader)
1111
}

crypto/test/test_api.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ function TestAESEncrypt(t)
8282
err = "failed to encrypt: incorrect GCM nonce size: 14, expected: 12",
8383
},
8484
{
85-
data = "48656c6c6f20776f726c64", -- "Hello world" in hex
86-
mode = "cbc",
87-
key = "86e15cbc1cbf510d8f2e51d4b63a2144",
88-
init = "068bb92e032884ba8b260fa7d3a80005",
85+
data = "48656c6c6f20776f726c64", -- "Hello world" in hex
86+
mode = "cbc",
87+
key = "86e15cbc1cbf510d8f2e51d4b63a2144",
88+
init = "068bb92e032884ba8b260fa7d3a80005",
8989
expected = "dfba6f71cce4d4b76be301b577d9f095",
90-
err = nil,
90+
err = nil,
9191
},
9292
{
9393
data = "48656c6c6f20776f726c64", -- "Hello world" in hex
@@ -162,7 +162,7 @@ function TestAESDecrypt(t)
162162
mode = "cbc",
163163
key = "86e15cbc1cbf510d8f2e51d4b63a2144",
164164
init = "068bb92e032884ba8b260fa7d3a80005",
165-
expected = "48656c6c6f20776f726c64", -- "Hello world" in hex
165+
expected = "48656c6c6f20776f726c640505050505", -- "Hello world" + padding in hex
166166
err = nil,
167167
},
168168
{

0 commit comments

Comments
 (0)