Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
matrix-org
Olm
Commits
8df4d9e9
Commit
8df4d9e9
authored
Feb 25, 2015
by
Mark Haines
Browse files
Tweak AES cbc to add pcks7 padding bytes
parent
38332e0a
Changes
4
Hide whitespace changes
Inline
Side-by-side
include/axolotl/crypto.hh
View file @
8df4d9e9
...
...
@@ -42,17 +42,11 @@ struct Aes256Iv {
};
std
::
size_t
aes_
pkcs_7_padded
_length
(
std
::
size_t
aes_
encrypt_cbc
_length
(
std
::
size_t
input_length
);
void
aes_pkcs_7_padding
(
std
::
uint8_t
const
*
input
,
std
::
size_t
input_length
,
std
::
uint8_t
*
output
);
void
aes_encrypt_cbc
(
Aes256Key
const
&
key
,
Aes256Iv
const
&
iv
,
...
...
@@ -61,7 +55,7 @@ void aes_encrypt_cbc(
);
void
aes_decrypt_cbc
(
std
::
size_t
aes_decrypt_cbc
(
Aes256Key
const
&
key
,
Aes256Iv
const
&
iv
,
std
::
uint8_t
const
*
input
,
std
::
size_t
input_length
,
...
...
include/axolotl/list.hh
View file @
8df4d9e9
...
...
@@ -15,6 +15,11 @@ public:
T
const
*
begin
()
const
{
return
_data
;
}
T
const
*
end
()
const
{
return
_end
;
}
/**
* Is the list empty?
*/
bool
empty
()
{
return
_end
==
_data
;
}
/**
* The number of items in the list.
*/
...
...
src/crypto.cpp
View file @
8df4d9e9
...
...
@@ -107,26 +107,13 @@ void axolotl::curve25519_shared_secret(
}
std
::
size_t
axolotl
::
aes_
pkcs_7_padded
_length
(
std
::
size_t
axolotl
::
aes_
encrypt_cbc
_length
(
std
::
size_t
input_length
)
{
return
input_length
+
AES_BLOCK_LENGTH
-
input_length
%
AES_BLOCK_LENGTH
;
}
void
axolotl
::
aes_pkcs_7_padding
(
std
::
uint8_t
const
*
input
,
std
::
size_t
input_length
,
std
::
uint8_t
*
output
)
{
std
::
memcpy
(
output
,
input
,
input_length
);
std
::
size_t
padded_length
=
axolotl
::
aes_pkcs_7_padded_length
(
input_length
);
std
::
uint8_t
padding
=
padded_length
-
input_length
;
for
(
std
::
size_t
i
=
input_length
;
i
<
padded_length
;
++
i
)
{
output
[
i
]
=
padding
;
}
}
void
axolotl
::
aes_encrypt_cbc
(
axolotl
::
Aes256Key
const
&
key
,
axolotl
::
Aes256Iv
const
&
iv
,
...
...
@@ -137,17 +124,28 @@ void axolotl::aes_encrypt_cbc(
::
aes_key_setup
(
key
.
key
,
key_schedule
,
256
);
std
::
uint8_t
input_block
[
AES_BLOCK_LENGTH
];
std
::
memcpy
(
input_block
,
iv
.
iv
,
AES_BLOCK_LENGTH
);
for
(
std
::
size_t
i
=
0
;
i
<
input_length
;
i
+=
AES_BLOCK_LENGTH
)
{
xor_block
<
AES_BLOCK_LENGTH
>
(
input_block
,
&
input
[
i
]);
::
aes_encrypt
(
input_block
,
&
output
[
i
],
key_schedule
,
256
);
std
::
memcpy
(
input_block
,
&
output
[
i
],
AES_BLOCK_LENGTH
);
while
(
input_length
>=
AES_BLOCK_LENGTH
)
{
xor_block
<
AES_BLOCK_LENGTH
>
(
input_block
,
input
);
::
aes_encrypt
(
input_block
,
output
,
key_schedule
,
256
);
std
::
memcpy
(
input_block
,
output
,
AES_BLOCK_LENGTH
);
input
+=
AES_BLOCK_LENGTH
;
output
+=
AES_BLOCK_LENGTH
;
input_length
-=
AES_BLOCK_LENGTH
;
}
std
::
size_t
i
=
0
;
for
(;
i
<
input_length
;
++
i
)
{
input_block
[
i
]
^=
input
[
i
];
}
for
(;
i
<
AES_BLOCK_LENGTH
;
++
i
)
{
input_block
[
i
]
^=
AES_BLOCK_LENGTH
-
input_length
;
}
::
aes_encrypt
(
input_block
,
output
,
key_schedule
,
256
);
std
::
memset
(
key_schedule
,
0
,
sizeof
(
key_schedule
));
std
::
memset
(
input_block
,
0
,
sizeof
(
AES_BLOCK_LENGTH
));
}
void
axolotl
::
aes_decrypt_cbc
(
std
::
size_t
axolotl
::
aes_decrypt_cbc
(
axolotl
::
Aes256Key
const
&
key
,
axolotl
::
Aes256Iv
const
&
iv
,
std
::
uint8_t
const
*
input
,
std
::
size_t
input_length
,
...
...
@@ -164,6 +162,8 @@ void axolotl::aes_decrypt_cbc(
}
}
std
::
memset
(
key_schedule
,
0
,
sizeof
(
key_schedule
));
std
::
size_t
padding
=
output
[
input_length
-
1
];
return
(
padding
>
input_length
)
?
std
::
size_t
(
-
1
)
:
(
input_length
-
padding
);
}
...
...
tests/test_crypto.cpp
View file @
8df4d9e9
...
...
@@ -73,24 +73,27 @@ TestCase test_case("AES Test Case 1");
axolotl
::
Aes256Key
key
=
{};
axolotl
::
Aes256Iv
iv
=
{};
std
::
uint8_t
input
[
32
]
=
{};
std
::
uint8_t
input
[
16
]
=
{};
std
::
uint8_t
expected
[
32
]
=
{
0xDC
,
0x95
,
0xC0
,
0x78
,
0xA2
,
0x40
,
0x89
,
0x89
,
0xAD
,
0x48
,
0xA2
,
0x14
,
0x92
,
0x84
,
0x20
,
0x87
,
0x
08
,
0xC
3
,
0x
74
,
0x
84
,
0x
8
C
,
0x
22
,
0x8
2
,
0x
33
,
0x
C2
,
0x
B3
,
0x
4
F
,
0x
33
,
0x
2B
,
0x
D
2
,
0x
E9
,
0x
D
3
0x
F3
,
0xC
0
,
0x
03
,
0x
DD
,
0xC
4
,
0x
A7
,
0x
B
8
,
0x
A9
,
0x
4B
,
0x
AE
,
0x
D
F
,
0x
FC
,
0x
3D
,
0x2
1
,
0x
4C
,
0x3
8
};
std
::
size_t
length
=
axolotl
::
aes_encrypt_cbc_length
(
sizeof
(
input
));
assert_equals
(
std
::
size_t
(
32
),
length
);
std
::
uint8_t
actual
[
32
]
=
{};
axolotl
::
aes_encrypt_cbc
(
key
,
iv
,
input
,
sizeof
(
input
),
actual
);
assert_equals
(
expected
,
actual
,
32
);
axolotl
::
aes_decrypt_cbc
(
key
,
iv
,
expected
,
sizeof
(
expected
),
actual
);
assert_equals
(
input
,
actual
,
32
);
length
=
axolotl
::
aes_decrypt_cbc
(
key
,
iv
,
expected
,
sizeof
(
expected
),
actual
);
assert_equals
(
std
::
size_t
(
16
),
length
);
assert_equals
(
input
,
actual
,
length
);
}
/* AES Test Case 1 */
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment