Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Openssl
提交
7e5b4d67
T
Third Party Openssl
项目概览
OpenHarmony
/
Third Party Openssl
1 年多 前同步成功
通知
10
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看板
提交
7e5b4d67
编写于
4月 16, 2011
作者:
A
Andy Polyakov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ccm128.c: minor optimization and bugfix in CRYPTO_ccm128_[en|de]crypt.
上级
75707a32
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
63 addition
and
51 deletion
+63
-51
crypto/modes/ccm128.c
crypto/modes/ccm128.c
+63
-51
未找到文件。
crypto/modes/ccm128.c
浏览文件 @
7e5b4d67
...
...
@@ -59,8 +59,7 @@
#include <assert.h>
typedef
struct
{
union
{
u8
c
[
16
];
size_t
s
[
16
/
sizeof
(
size_t
)];
}
nonce
,
cmac
,
scratch
,
inp
;
union
{
u64
u
[
2
];
u8
c
[
16
];
}
nonce
,
cmac
;
u64
blocks
;
block128_f
block
;
void
*
key
;
...
...
@@ -94,7 +93,7 @@ int CRYPTO_ccm128_setiv(CCM128_CONTEXT *ctx,
ctx
->
nonce
.
c
[
11
]
=
(
u8
)(
mlen
>>
(
32
%
(
sizeof
(
mlen
)
*
8
)));
}
else
*
(
(
size_t
*
)
&
ctx
->
nonce
.
s
[
8
])
=
0
;
*
(
u32
*
)(
&
ctx
->
nonce
.
c
[
8
])
=
0
;
ctx
->
nonce
.
c
[
12
]
=
(
u8
)(
mlen
>>
24
);
ctx
->
nonce
.
c
[
13
]
=
(
u8
)(
mlen
>>
16
);
...
...
@@ -111,11 +110,12 @@ int CRYPTO_ccm128_setiv(CCM128_CONTEXT *ctx,
void
CRYPTO_ccm128_aad
(
CCM128_CONTEXT
*
ctx
,
const
unsigned
char
*
aad
,
size_t
alen
)
{
unsigned
int
i
;
block128_f
block
=
ctx
->
block
;
if
(
alen
==
0
)
return
;
ctx
->
nonce
.
c
[
0
]
|=
0x40
;
/* set Adata flag */
(
*
ctx
->
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
),
(
*
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
),
ctx
->
blocks
++
;
if
(
alen
<
(
0x10000
-
0x100
))
{
...
...
@@ -149,7 +149,7 @@ void CRYPTO_ccm128_aad(CCM128_CONTEXT *ctx,
do
{
for
(;
i
<
16
&&
alen
;
++
i
,
++
aad
,
--
alen
)
ctx
->
cmac
.
c
[
i
]
^=
*
aad
;
(
*
ctx
->
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
),
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
),
ctx
->
blocks
++
;
i
=
0
;
}
while
(
alen
);
...
...
@@ -175,16 +175,19 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
size_t
len
)
{
size_t
n
;
unsigned
int
i
;
unsigned
char
flags
=
ctx
->
nonce
.
c
[
0
];
unsigned
int
i
,
L
;
unsigned
char
flags0
=
ctx
->
nonce
.
c
[
0
];
block128_f
block
=
ctx
->
block
;
union
{
u64
u
[
2
];
u8
c
[
16
];
}
scratch
;
if
(
!
(
flags
&
0x40
))
(
*
ctx
->
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
),
if
(
!
(
flags
0
&
0x40
))
(
*
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
),
ctx
->
blocks
++
;
flags
&=
7
;
/* extract the L parameter */
for
(
n
=
0
,
i
=
15
-
flags
;
i
<
15
;
++
i
)
{
n
|=
ctx
->
nonce
.
c
[
i
];
ctx
->
nonce
.
c
[
i
]
=
0
;
ctx
->
nonce
.
c
[
0
]
=
L
=
flags0
&
7
;
for
(
n
=
0
,
i
=
15
-
L
;
i
<
15
;
++
i
)
{
n
|=
ctx
->
nonce
.
c
[
i
];
ctx
->
nonce
.
c
[
i
]
=
0
;
n
<<=
8
;
}
n
|=
ctx
->
nonce
.
c
[
15
];
/* reconstructed length */
...
...
@@ -197,23 +200,25 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
while
(
len
>=
16
)
{
#if defined(STRICT_ALIGNMENT)
memcpy
(
ctx
->
inp
.
c
,
inp
,
16
);
for
(
i
=
0
;
i
<
16
/
sizeof
(
size_t
);
++
i
)
ctx
->
cmac
.
s
[
i
]
^=
ctx
->
inp
.
s
[
i
];
union
{
u64
u
[
2
];
u8
c
[
16
];
}
temp
;
memcpy
(
temp
.
c
,
inp
,
16
);
ctx
->
cmac
.
u
[
0
]
^=
temp
.
u
[
0
];
ctx
->
cmac
.
u
[
1
]
^=
temp
.
u
[
1
];
#else
for
(
i
=
0
;
i
<
16
/
sizeof
(
size_t
);
++
i
)
ctx
->
cmac
.
s
[
i
]
^=
((
size_t
*
)
inp
)[
i
];
ctx
->
cmac
.
u
[
0
]
^=
((
u64
*
)
inp
)[
0
];
ctx
->
cmac
.
u
[
1
]
^=
((
u64
*
)
inp
)[
1
];
#endif
(
*
ctx
->
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
(
*
ctx
->
block
)(
ctx
->
nonce
.
c
,
ctx
->
scratch
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
ctr128_inc
(
ctx
->
nonce
.
c
);
#if defined(STRICT_ALIGNMENT)
for
(
i
=
0
;
i
<
16
/
sizeof
(
size_t
);
++
i
)
ctx
->
inp
.
s
[
i
]
^=
ctx
->
scratch
.
s
[
i
];
memcpy
(
out
,
ctx
->
in
p
.
c
,
16
);
temp
.
u
[
0
]
^=
scratch
.
u
[
0
];
temp
.
u
[
1
]
^=
scratch
.
u
[
1
];
memcpy
(
out
,
tem
p
.
c
,
16
);
#else
for
(
i
=
0
;
i
<
16
/
sizeof
(
size_t
);
++
i
)
((
size_t
*
)
out
)[
i
]
=
ctx
->
scratch
.
s
[
i
]
^
((
size_t
*
)
inp
)[
i
];
((
u64
*
)
out
)[
0
]
=
scratch
.
u
[
0
]
^
((
u64
*
)
inp
)[
0
];
((
u64
*
)
out
)[
1
]
=
scratch
.
u
[
1
]
^
((
u64
*
)
inp
)[
1
];
#endif
inp
+=
16
;
out
+=
16
;
...
...
@@ -222,17 +227,19 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
if
(
len
)
{
for
(
i
=
0
;
i
<
len
;
++
i
)
ctx
->
cmac
.
c
[
i
]
^=
inp
[
i
];
(
*
ctx
->
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
(
*
ctx
->
block
)(
ctx
->
nonce
.
c
,
ctx
->
scratch
.
c
,
ctx
->
key
);
for
(
i
=
0
;
i
<
len
;
++
i
)
out
[
i
]
=
ctx
->
scratch
.
c
[
i
]
^
inp
[
i
];
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
for
(
i
=
0
;
i
<
len
;
++
i
)
out
[
i
]
=
scratch
.
c
[
i
]
^
inp
[
i
];
}
for
(
i
=
15
-
flags
;
i
<
16
;
++
i
)
for
(
i
=
15
-
L
;
i
<
16
;
++
i
)
ctx
->
nonce
.
c
[
i
]
=
0
;
(
*
ctx
->
block
)(
ctx
->
nonce
.
c
,
ctx
->
scratch
.
c
,
ctx
->
key
);
for
(
i
=
0
;
i
<
16
/
sizeof
(
size_t
);
++
i
)
ctx
->
cmac
.
s
[
i
]
^=
ctx
->
scratch
.
s
[
i
];
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
ctx
->
cmac
.
u
[
0
]
^=
scratch
.
u
[
0
];
ctx
->
cmac
.
u
[
1
]
^=
scratch
.
u
[
1
];
ctx
->
nonce
.
c
[
0
]
=
flags0
;
return
0
;
}
...
...
@@ -242,15 +249,18 @@ int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx,
size_t
len
)
{
size_t
n
;
unsigned
int
i
;
unsigned
char
flags
=
ctx
->
nonce
.
c
[
0
];
unsigned
int
i
,
L
;
unsigned
char
flags0
=
ctx
->
nonce
.
c
[
0
];
block128_f
block
;
union
{
u64
u
[
2
];
u8
c
[
16
];
}
scratch
;
if
(
!
(
flags
&
0x40
))
(
*
ctx
->
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
if
(
!
(
flags
0
&
0x40
))
(
*
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
flags
&=
7
;
/* extract the L parameter */
for
(
n
=
0
,
i
=
15
-
flags
;
i
<
15
;
++
i
)
{
n
|=
ctx
->
nonce
.
c
[
i
];
ctx
->
nonce
.
c
[
i
]
=
0
;
ctx
->
nonce
.
c
[
0
]
=
L
=
flags0
&
7
;
for
(
n
=
0
,
i
=
15
-
L
;
i
<
15
;
++
i
)
{
n
|=
ctx
->
nonce
.
c
[
i
];
ctx
->
nonce
.
c
[
i
]
=
0
;
n
<<=
8
;
}
n
|=
ctx
->
nonce
.
c
[
15
];
/* reconstructed length */
...
...
@@ -259,18 +269,18 @@ int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx,
if
(
n
!=
len
)
return
-
1
;
while
(
len
>=
16
)
{
(
*
ctx
->
block
)(
ctx
->
nonce
.
c
,
ctx
->
scratch
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
ctr128_inc
(
ctx
->
nonce
.
c
);
#if defined(STRICT_ALIGNMENT)
memcpy
(
ctx
->
inp
.
c
,
inp
,
16
);
for
(
i
=
0
;
i
<
16
/
sizeof
(
size_t
);
++
i
)
ctx
->
cmac
.
s
[
i
]
^=
(
ctx
->
scratch
.
s
[
i
]
^=
ctx
->
inp
.
s
[
i
]);
memcpy
(
out
,
ctx
->
scratch
,
16
);
ctx
->
cmac
.
s
[
i
]
^=
(
scratch
.
s
[
i
]
^=
ctx
->
inp
.
s
[
i
]);
memcpy
(
out
,
scratch
,
16
);
#else
for
(
i
=
0
;
i
<
16
/
sizeof
(
size_t
);
++
i
)
ctx
->
cmac
.
s
[
i
]
^=
((
size_t
*
)
out
)[
i
]
=
ctx
->
scratch
.
s
[
i
]
^
((
size_t
*
)
inp
)[
i
]
;
ctx
->
cmac
.
u
[
0
]
^=
(((
u64
*
)
out
)[
0
]
=
scratch
.
u
[
0
]
^
((
u64
*
)
inp
)[
0
]);
ctx
->
cmac
.
u
[
1
]
^=
(((
u64
*
)
out
)[
1
]
=
scratch
.
u
[
1
]
^
((
u64
*
)
inp
)[
1
])
;
#endif
(
*
ctx
->
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
inp
+=
16
;
out
+=
16
;
...
...
@@ -278,18 +288,20 @@ int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx,
}
if
(
len
)
{
(
*
ctx
->
block
)(
ctx
->
nonce
.
c
,
ctx
->
scratch
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
for
(
i
=
0
;
i
<
len
;
++
len
)
ctx
->
cmac
.
c
[
i
]
^=
(
out
[
i
]
=
ctx
->
scratch
.
c
[
i
]
^
inp
[
i
]);
(
*
ctx
->
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
ctx
->
cmac
.
c
[
i
]
^=
(
out
[
i
]
=
scratch
.
c
[
i
]
^
inp
[
i
]);
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
}
for
(
i
=
15
-
flags
;
i
<
16
;
++
i
)
for
(
i
=
15
-
L
;
i
<
16
;
++
i
)
ctx
->
nonce
.
c
[
i
]
=
0
;
(
*
ctx
->
block
)(
ctx
->
nonce
.
c
,
ctx
->
scratch
.
c
,
ctx
->
key
);
for
(
i
=
0
;
i
<
16
/
sizeof
(
size_t
);
++
i
)
ctx
->
cmac
.
s
[
i
]
^=
ctx
->
scratch
.
s
[
i
];
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
ctx
->
cmac
.
u
[
0
]
^=
scratch
.
u
[
0
];
ctx
->
cmac
.
u
[
1
]
^=
scratch
.
u
[
1
];
ctx
->
nonce
.
c
[
0
]
=
flags0
;
return
0
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录