Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Openssl
提交
67a315b6
T
Third Party Openssl
项目概览
OpenHarmony
/
Third Party Openssl
大约 1 年 前同步成功
通知
9
Star
18
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party Openssl
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
67a315b6
编写于
4月 10, 2010
作者:
A
Andy Polyakov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cts128.c: add support for NIST "Ciphertext Stealing" proposal.
上级
6c83629b
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
227 addition
and
9 deletion
+227
-9
crypto/modes/cts128.c
crypto/modes/cts128.c
+214
-9
crypto/modes/modes.h
crypto/modes/modes.h
+13
-0
未找到文件。
crypto/modes/cts128.c
浏览文件 @
67a315b6
...
...
@@ -23,8 +23,9 @@
* deviates from mentioned RFCs. Most notably it allows input to be
* of block length and it doesn't flip the order of the last two
* blocks. CTS is being discussed even in ECB context, but it's not
* adopted for any known application. This implementation complies
* with mentioned RFCs and [as such] extends CBC mode.
* adopted for any known application. This implementation provides
* two interfaces: one compliant with above mentioned RFCs and one
* compliant with the NIST proposal, both extending CBC mode.
*/
size_t
CRYPTO_cts128_encrypt_block
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
...
...
@@ -54,6 +55,34 @@ size_t CRYPTO_cts128_encrypt_block(const unsigned char *in, unsigned char *out,
return
len
+
residue
;
}
size_t
CRYPTO_nistcts128_encrypt_block
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
block128_f
block
)
{
size_t
residue
,
n
;
assert
(
in
&&
out
&&
key
&&
ivec
);
if
(
len
<
16
)
return
0
;
residue
=
len
%
16
;
len
-=
residue
;
CRYPTO_cbc128_encrypt
(
in
,
out
,
len
,
key
,
ivec
,
block
);
if
(
residue
==
0
)
return
len
;
in
+=
len
;
out
+=
len
;
for
(
n
=
0
;
n
<
residue
;
++
n
)
ivec
[
n
]
^=
in
[
n
];
(
*
block
)(
ivec
,
ivec
,
key
);
memcpy
(
out
-
16
+
residue
,
ivec
,
16
);
return
len
+
residue
;
}
size_t
CRYPTO_cts128_encrypt
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
cbc128_f
cbc
)
...
...
@@ -90,6 +119,41 @@ size_t CRYPTO_cts128_encrypt(const unsigned char *in, unsigned char *out,
return
len
+
residue
;
}
size_t
CRYPTO_nistcts128_encrypt
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
cbc128_f
cbc
)
{
size_t
residue
;
union
{
size_t
align
;
unsigned
char
c
[
16
];
}
tmp
;
assert
(
in
&&
out
&&
key
&&
ivec
);
if
(
len
<
16
)
return
0
;
residue
=
len
%
16
;
len
-=
residue
;
(
*
cbc
)(
in
,
out
,
len
,
key
,
ivec
,
1
);
if
(
residue
==
0
)
return
len
;
in
+=
len
;
out
+=
len
;
#if defined(CBC_HANDLES_TRUNCATED_IO)
(
*
cbc
)(
in
,
out
-
16
+
residue
,
residue
,
key
,
ivec
,
1
);
#else
{
size_t
n
;
for
(
n
=
0
;
n
<
16
;
n
+=
sizeof
(
size_t
))
*
(
size_t
*
)(
tmp
.
c
+
n
)
=
0
;
memcpy
(
tmp
.
c
,
in
,
residue
);
}
(
*
cbc
)(
tmp
.
c
,
out
-
16
+
residue
,
16
,
key
,
ivec
,
1
);
#endif
return
len
+
residue
;
}
size_t
CRYPTO_cts128_decrypt_block
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
block128_f
block
)
...
...
@@ -125,7 +189,51 @@ size_t CRYPTO_cts128_decrypt_block(const unsigned char *in, unsigned char *out,
for
(
residue
+=
16
;
n
<
residue
;
++
n
)
out
[
n
]
=
tmp
.
c
[
n
]
^
in
[
n
];
return
len
+
residue
-
16
;
return
16
+
len
+
residue
;
}
size_t
CRYPTO_nistcts128_decrypt_block
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
block128_f
block
)
{
size_t
residue
,
n
;
union
{
size_t
align
;
unsigned
char
c
[
32
];
}
tmp
;
assert
(
in
&&
out
&&
key
&&
ivec
);
if
(
len
<
16
)
return
0
;
residue
=
len
%
16
;
if
(
residue
==
0
)
{
CRYPTO_cbc128_decrypt
(
in
,
out
,
len
,
key
,
ivec
,
block
);
return
len
;
}
len
-=
16
+
residue
;
if
(
len
)
{
CRYPTO_cbc128_decrypt
(
in
,
out
,
len
,
key
,
ivec
,
block
);
in
+=
len
;
out
+=
len
;
}
(
*
block
)(
in
+
residue
,
tmp
.
c
+
16
,
key
);
for
(
n
=
0
;
n
<
16
;
n
+=
sizeof
(
size_t
))
*
(
size_t
*
)(
tmp
.
c
+
n
)
=
*
(
size_t
*
)(
tmp
.
c
+
16
+
n
);
memcpy
(
tmp
.
c
,
in
,
residue
);
(
*
block
)(
tmp
.
c
,
tmp
.
c
,
key
);
for
(
n
=
0
;
n
<
16
;
++
n
)
{
unsigned
char
c
=
in
[
n
];
out
[
n
]
=
tmp
.
c
[
n
]
^
ivec
[
n
];
ivec
[
n
]
=
in
[
n
+
residue
];
tmp
.
c
[
n
]
=
c
;
}
for
(
residue
+=
16
;
n
<
residue
;
++
n
)
out
[
n
]
=
tmp
.
c
[
n
]
^
tmp
.
c
[
n
-
16
];
return
16
+
len
+
residue
;
}
size_t
CRYPTO_cts128_decrypt
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
...
...
@@ -160,7 +268,47 @@ size_t CRYPTO_cts128_decrypt(const unsigned char *in, unsigned char *out,
(
*
cbc
)(
tmp
.
c
,
tmp
.
c
,
32
,
key
,
ivec
,
0
);
memcpy
(
out
,
tmp
.
c
,
16
+
residue
);
#endif
return
len
+
residue
;
return
16
+
len
+
residue
;
}
size_t
CRYPTO_nistcts128_decrypt
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
cbc128_f
cbc
)
{
size_t
residue
,
n
;
union
{
size_t
align
;
unsigned
char
c
[
32
];
}
tmp
;
assert
(
in
&&
out
&&
key
&&
ivec
);
if
(
len
<
16
)
return
0
;
residue
=
len
%
16
;
if
(
residue
==
0
)
{
(
*
cbc
)(
in
,
out
,
len
,
key
,
ivec
,
0
);
return
len
;
}
len
-=
16
+
residue
;
if
(
len
)
{
(
*
cbc
)(
in
,
out
,
len
,
key
,
ivec
,
0
);
in
+=
len
;
out
+=
len
;
}
for
(
n
=
16
;
n
<
32
;
n
+=
sizeof
(
size_t
))
*
(
size_t
*
)(
tmp
.
c
+
n
)
=
0
;
/* this places in[16] at &tmp.c[16] and decrypted block at &tmp.c[0] */
(
*
cbc
)(
in
+
residue
,
tmp
.
c
,
16
,
key
,
tmp
.
c
+
16
,
0
);
memcpy
(
tmp
.
c
,
in
,
residue
);
#if defined(CBC_HANDLES_TRUNCATED_IO)
(
*
cbc
)(
tmp
.
c
,
out
,
16
+
residue
,
key
,
ivec
,
0
);
#else
(
*
cbc
)(
tmp
.
c
,
tmp
.
c
,
32
,
key
,
ivec
,
0
);
memcpy
(
out
,
tmp
.
c
,
16
+
residue
);
#endif
return
16
+
len
+
residue
;
}
#if defined(SELFTEST)
...
...
@@ -200,9 +348,8 @@ static const unsigned char vector_64[64] =
static
AES_KEY
encks
,
decks
;
void
test_vector
(
const
unsigned
char
*
vector
,
size_t
len
)
{
unsigned
char
cleartext
[
64
];
unsigned
char
iv
[
sizeof
(
test_iv
)];
unsigned
char
ciphertext
[
64
];
{
unsigned
char
iv
[
sizeof
(
test_iv
)];
unsigned
char
cleartext
[
64
],
ciphertext
[
64
];
size_t
tail
;
printf
(
"vector_%d
\n
"
,
len
);
fflush
(
stdout
);
...
...
@@ -243,7 +390,57 @@ void test_vector(const unsigned char *vector,size_t len)
fprintf
(
stderr
,
"iv_%d mismatch
\n
"
,
len
),
exit
(
4
);
}
main
()
void
test_nistvector
(
const
unsigned
char
*
vector
,
size_t
len
)
{
unsigned
char
iv
[
sizeof
(
test_iv
)];
unsigned
char
cleartext
[
64
],
ciphertext
[
64
],
nistvector
[
64
];
size_t
tail
;
printf
(
"nistvector_%d
\n
"
,
len
);
fflush
(
stdout
);
if
((
tail
=
len
%
16
)
==
0
)
tail
=
16
;
len
-=
16
+
tail
;
memcpy
(
nistvector
,
vector
,
len
);
/* flip two last blocks */
memcpy
(
nistvector
+
len
,
vector
+
len
+
16
,
tail
);
memcpy
(
nistvector
+
len
+
tail
,
vector
+
len
,
16
);
len
+=
16
+
tail
;
tail
=
16
;
/* test block-based encryption */
memcpy
(
iv
,
test_iv
,
sizeof
(
test_iv
));
CRYPTO_nistcts128_encrypt_block
(
test_input
,
ciphertext
,
len
,
&
encks
,
iv
,(
block128_f
)
AES_encrypt
);
if
(
memcmp
(
ciphertext
,
nistvector
,
len
))
fprintf
(
stderr
,
"output_%d mismatch
\n
"
,
len
),
exit
(
1
);
if
(
memcmp
(
iv
,
nistvector
+
len
-
tail
,
sizeof
(
iv
)))
fprintf
(
stderr
,
"iv_%d mismatch
\n
"
,
len
),
exit
(
1
);
/* test block-based decryption */
memcpy
(
iv
,
test_iv
,
sizeof
(
test_iv
));
CRYPTO_nistcts128_decrypt_block
(
ciphertext
,
cleartext
,
len
,
&
decks
,
iv
,(
block128_f
)
AES_decrypt
);
if
(
memcmp
(
cleartext
,
test_input
,
len
))
fprintf
(
stderr
,
"input_%d mismatch
\n
"
,
len
),
exit
(
2
);
if
(
memcmp
(
iv
,
nistvector
+
len
-
tail
,
sizeof
(
iv
)))
fprintf
(
stderr
,
"iv_%d mismatch
\n
"
,
len
),
exit
(
2
);
/* test streamed encryption */
memcpy
(
iv
,
test_iv
,
sizeof
(
test_iv
));
CRYPTO_nistcts128_encrypt
(
test_input
,
ciphertext
,
len
,
&
encks
,
iv
,(
cbc128_f
)
AES_cbc_encrypt
);
if
(
memcmp
(
ciphertext
,
nistvector
,
len
))
fprintf
(
stderr
,
"output_%d mismatch
\n
"
,
len
),
exit
(
3
);
if
(
memcmp
(
iv
,
nistvector
+
len
-
tail
,
sizeof
(
iv
)))
fprintf
(
stderr
,
"iv_%d mismatch
\n
"
,
len
),
exit
(
3
);
/* test streamed decryption */
memcpy
(
iv
,
test_iv
,
sizeof
(
test_iv
));
CRYPTO_nistcts128_decrypt
(
ciphertext
,
cleartext
,
len
,
&
decks
,
iv
,(
cbc128_f
)
AES_cbc_encrypt
);
if
(
memcmp
(
cleartext
,
test_input
,
len
))
fprintf
(
stderr
,
"input_%d mismatch
\n
"
,
len
),
exit
(
4
);
if
(
memcmp
(
iv
,
nistvector
+
len
-
tail
,
sizeof
(
iv
)))
fprintf
(
stderr
,
"iv_%d mismatch
\n
"
,
len
),
exit
(
4
);
}
int
main
()
{
AES_set_encrypt_key
(
test_key
,
128
,
&
encks
);
AES_set_decrypt_key
(
test_key
,
128
,
&
decks
);
...
...
@@ -254,6 +451,14 @@ main()
test_vector
(
vector_47
,
sizeof
(
vector_47
));
test_vector
(
vector_48
,
sizeof
(
vector_48
));
test_vector
(
vector_64
,
sizeof
(
vector_64
));
exit
(
0
);
test_nistvector
(
vector_17
,
sizeof
(
vector_17
));
test_nistvector
(
vector_31
,
sizeof
(
vector_31
));
test_nistvector
(
vector_32
,
sizeof
(
vector_32
));
test_nistvector
(
vector_47
,
sizeof
(
vector_47
));
test_nistvector
(
vector_48
,
sizeof
(
vector_48
));
test_nistvector
(
vector_64
,
sizeof
(
vector_64
));
return
0
;
}
#endif
crypto/modes/modes.h
浏览文件 @
67a315b6
...
...
@@ -57,3 +57,16 @@ size_t CRYPTO_cts128_decrypt_block(const unsigned char *in, unsigned char *out,
size_t
CRYPTO_cts128_decrypt
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
cbc128_f
cbc
);
size_t
CRYPTO_nistcts128_encrypt_block
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
block128_f
block
);
size_t
CRYPTO_nistcts128_encrypt
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
cbc128_f
cbc
);
size_t
CRYPTO_nistcts128_decrypt_block
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
block128_f
block
);
size_t
CRYPTO_nistcts128_decrypt
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
cbc128_f
cbc
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录