Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Openssl
提交
f855b9d7
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,发现更多精彩内容 >>
提交
f855b9d7
编写于
4月 24, 2011
作者:
A
Andy Polyakov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ccm128.c: add CRYPTO_ccm128_[en|de]crypt_ccm64 and minor optimization.
上级
ce676476
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
162 addition
and
23 deletion
+162
-23
crypto/modes/ccm128.c
crypto/modes/ccm128.c
+150
-21
crypto/modes/modes.h
crypto/modes/modes.h
+12
-2
未找到文件。
crypto/modes/ccm128.c
浏览文件 @
f855b9d7
...
...
@@ -58,7 +58,8 @@
#endif
#include <assert.h>
/* First you setup M and L parameters and pass the key schedule */
/* First you setup M and L parameters and pass the key schedule.
* This is called once per session setup... */
void
CRYPTO_ccm128_init
(
CCM128_CONTEXT
*
ctx
,
unsigned
int
M
,
unsigned
int
L
,
void
*
key
,
block128_f
block
)
{
...
...
@@ -150,10 +151,13 @@ void CRYPTO_ccm128_aad(CCM128_CONTEXT *ctx,
/* Finally you encrypt or decrypt the message */
static
void
ctr128_inc
(
unsigned
char
*
counter
)
{
unsigned
int
n
=
16
;
/* counter part of nonce may not be larger than L*8 bits,
* L is not larger than 8, therefore 64-bit counter... */
static
void
ctr64_inc
(
unsigned
char
*
counter
)
{
unsigned
int
n
=
8
;
u8
c
;
counter
+=
8
;
do
{
--
n
;
c
=
counter
[
n
];
...
...
@@ -169,12 +173,13 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
{
size_t
n
;
unsigned
int
i
,
L
;
unsigned
char
flags0
=
ctx
->
nonce
.
c
[
0
];
block128_f
block
=
ctx
->
block
;
unsigned
char
flags0
=
ctx
->
nonce
.
c
[
0
];
block128_f
block
=
ctx
->
block
;
void
*
key
=
ctx
->
key
;
union
{
u64
u
[
2
];
u8
c
[
16
];
}
scratch
;
if
(
!
(
flags0
&
0x40
))
(
*
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
),
(
*
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
key
),
ctx
->
blocks
++
;
ctx
->
nonce
.
c
[
0
]
=
L
=
flags0
&
7
;
...
...
@@ -202,9 +207,9 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
ctx
->
cmac
.
u
[
0
]
^=
((
u64
*
)
inp
)[
0
];
ctx
->
cmac
.
u
[
1
]
^=
((
u64
*
)
inp
)[
1
];
#endif
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
ctr
128
_inc
(
ctx
->
nonce
.
c
);
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
key
);
ctr
64
_inc
(
ctx
->
nonce
.
c
);
#if defined(STRICT_ALIGNMENT)
temp
.
u
[
0
]
^=
scratch
.
u
[
0
];
temp
.
u
[
1
]
^=
scratch
.
u
[
1
];
...
...
@@ -220,15 +225,15 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
if
(
len
)
{
for
(
i
=
0
;
i
<
len
;
++
i
)
ctx
->
cmac
.
c
[
i
]
^=
inp
[
i
];
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
key
);
for
(
i
=
0
;
i
<
len
;
++
i
)
out
[
i
]
=
scratch
.
c
[
i
]
^
inp
[
i
];
}
for
(
i
=
15
-
L
;
i
<
16
;
++
i
)
ctx
->
nonce
.
c
[
i
]
=
0
;
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
key
);
ctx
->
cmac
.
u
[
0
]
^=
scratch
.
u
[
0
];
ctx
->
cmac
.
u
[
1
]
^=
scratch
.
u
[
1
];
...
...
@@ -243,12 +248,13 @@ int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx,
{
size_t
n
;
unsigned
int
i
,
L
;
unsigned
char
flags0
=
ctx
->
nonce
.
c
[
0
];
block128_f
block
=
ctx
->
block
;
unsigned
char
flags0
=
ctx
->
nonce
.
c
[
0
];
block128_f
block
=
ctx
->
block
;
void
*
key
=
ctx
->
key
;
union
{
u64
u
[
2
];
u8
c
[
16
];
}
scratch
;
if
(
!
(
flags0
&
0x40
))
(
*
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
key
);
ctx
->
nonce
.
c
[
0
]
=
L
=
flags0
&
7
;
for
(
n
=
0
,
i
=
15
-
L
;
i
<
15
;
++
i
)
{
...
...
@@ -265,8 +271,8 @@ int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx,
#if defined(STRICT_ALIGNMENT)
union
{
u64
u
[
2
];
u8
c
[
16
];
}
temp
;
#endif
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
ctr
128
_inc
(
ctx
->
nonce
.
c
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
key
);
ctr
64
_inc
(
ctx
->
nonce
.
c
);
#if defined(STRICT_ALIGNMENT)
memcpy
(
temp
.
c
,
inp
,
16
);
ctx
->
cmac
.
u
[
0
]
^=
(
scratch
.
u
[
0
]
^=
temp
.
u
[
0
]);
...
...
@@ -276,7 +282,7 @@ int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx,
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
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
key
);
inp
+=
16
;
out
+=
16
;
...
...
@@ -284,16 +290,139 @@ int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx,
}
if
(
len
)
{
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
key
);
for
(
i
=
0
;
i
<
len
;
++
i
)
ctx
->
cmac
.
c
[
i
]
^=
(
out
[
i
]
=
scratch
.
c
[
i
]
^
inp
[
i
]);
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
key
);
}
for
(
i
=
15
-
L
;
i
<
16
;
++
i
)
ctx
->
nonce
.
c
[
i
]
=
0
;
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
ctx
->
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
key
);
ctx
->
cmac
.
u
[
0
]
^=
scratch
.
u
[
0
];
ctx
->
cmac
.
u
[
1
]
^=
scratch
.
u
[
1
];
ctx
->
nonce
.
c
[
0
]
=
flags0
;
return
0
;
}
static
void
ctr64_add
(
unsigned
char
*
counter
,
size_t
inc
)
{
size_t
n
=
8
,
val
=
0
;
counter
+=
8
;
do
{
--
n
;
val
+=
counter
[
n
]
+
(
inc
&
0xff
);
counter
[
n
]
=
(
unsigned
char
)
val
;
val
>>=
8
;
/* carry bit */
inc
>>=
8
;
}
while
(
n
&&
(
inc
||
val
));
}
int
CRYPTO_ccm128_encrypt_ccm64
(
CCM128_CONTEXT
*
ctx
,
const
unsigned
char
*
inp
,
unsigned
char
*
out
,
size_t
len
,
ccm128_f
stream
)
{
size_t
n
;
unsigned
int
i
,
L
;
unsigned
char
flags0
=
ctx
->
nonce
.
c
[
0
];
block128_f
block
=
ctx
->
block
;
void
*
key
=
ctx
->
key
;
union
{
u64
u
[
2
];
u8
c
[
16
];
}
scratch
;
if
(
!
(
flags0
&
0x40
))
(
*
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
key
),
ctx
->
blocks
++
;
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 */
ctx
->
nonce
.
c
[
15
]
=
1
;
if
(
n
!=
len
)
return
-
1
;
/* length mismatch */
ctx
->
blocks
+=
((
len
+
15
)
>>
3
)
|
1
;
if
(
ctx
->
blocks
>
(
U64
(
1
)
<<
61
))
return
-
2
;
/* too much data */
if
((
n
=
len
/
16
))
{
(
*
stream
)(
inp
,
out
,
n
,
key
,
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
);
n
*=
16
;
inp
+=
n
;
out
+=
n
;
len
-=
n
;
}
if
(
len
)
{
if
(
n
)
ctr64_add
(
ctx
->
nonce
.
c
,
n
/
16
);
for
(
i
=
0
;
i
<
len
;
++
i
)
ctx
->
cmac
.
c
[
i
]
^=
inp
[
i
];
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
key
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
key
);
for
(
i
=
0
;
i
<
len
;
++
i
)
out
[
i
]
=
scratch
.
c
[
i
]
^
inp
[
i
];
}
for
(
i
=
15
-
L
;
i
<
16
;
++
i
)
ctx
->
nonce
.
c
[
i
]
=
0
;
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
key
);
ctx
->
cmac
.
u
[
0
]
^=
scratch
.
u
[
0
];
ctx
->
cmac
.
u
[
1
]
^=
scratch
.
u
[
1
];
ctx
->
nonce
.
c
[
0
]
=
flags0
;
return
0
;
}
int
CRYPTO_ccm128_decrypt_ccm64
(
CCM128_CONTEXT
*
ctx
,
const
unsigned
char
*
inp
,
unsigned
char
*
out
,
size_t
len
,
ccm128_f
stream
)
{
size_t
n
;
unsigned
int
i
,
L
;
unsigned
char
flags0
=
ctx
->
nonce
.
c
[
0
];
block128_f
block
=
ctx
->
block
;
void
*
key
=
ctx
->
key
;
union
{
u64
u
[
2
];
u8
c
[
16
];
}
scratch
;
if
(
!
(
flags0
&
0x40
))
(
*
block
)(
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
,
key
);
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 */
ctx
->
nonce
.
c
[
15
]
=
1
;
if
(
n
!=
len
)
return
-
1
;
if
((
n
=
len
/
16
))
{
(
*
stream
)(
inp
,
out
,
n
,
key
,
ctx
->
nonce
.
c
,
ctx
->
cmac
.
c
);
n
*=
16
;
inp
+=
n
;
out
+=
n
;
len
-=
n
;
}
if
(
len
)
{
if
(
n
)
ctr64_add
(
ctx
->
nonce
.
c
,
n
/
16
);
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
key
);
for
(
i
=
0
;
i
<
len
;
++
i
)
ctx
->
cmac
.
c
[
i
]
^=
(
out
[
i
]
=
scratch
.
c
[
i
]
^
inp
[
i
]);
(
*
block
)(
ctx
->
cmac
.
c
,
ctx
->
cmac
.
c
,
key
);
}
for
(
i
=
15
-
L
;
i
<
16
;
++
i
)
ctx
->
nonce
.
c
[
i
]
=
0
;
(
*
block
)(
ctx
->
nonce
.
c
,
scratch
.
c
,
key
);
ctx
->
cmac
.
u
[
0
]
^=
scratch
.
u
[
0
];
ctx
->
cmac
.
u
[
1
]
^=
scratch
.
u
[
1
];
...
...
crypto/modes/modes.h
浏览文件 @
f855b9d7
...
...
@@ -19,6 +19,10 @@ typedef void (*ctr128_f)(const unsigned char *in, unsigned char *out,
size_t
blocks
,
const
void
*
key
,
const
unsigned
char
ivec
[
16
]);
typedef
void
(
*
ccm128_f
)(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
blocks
,
const
void
*
key
,
const
unsigned
char
ivec
[
16
],
unsigned
char
cmac
[
16
]);
void
CRYPTO_cbc128_encrypt
(
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
const
void
*
key
,
unsigned
char
ivec
[
16
],
block128_f
block
);
...
...
@@ -100,9 +104,9 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
int
CRYPTO_gcm128_decrypt_ctr32
(
GCM128_CONTEXT
*
ctx
,
const
unsigned
char
*
in
,
unsigned
char
*
out
,
size_t
len
,
ctr128_f
stream
);
int
CRYPTO_gcm128_finish
(
GCM128_CONTEXT
*
ctx
,
const
unsigned
char
*
tag
,
int
CRYPTO_gcm128_finish
(
GCM128_CONTEXT
*
ctx
,
const
unsigned
char
*
tag
,
size_t
len
);
void
CRYPTO_gcm128_tag
(
GCM128_CONTEXT
*
ctx
,
unsigned
char
*
tag
,
size_t
len
);
void
CRYPTO_gcm128_tag
(
GCM128_CONTEXT
*
ctx
,
unsigned
char
*
tag
,
size_t
len
);
void
CRYPTO_gcm128_release
(
GCM128_CONTEXT
*
ctx
);
typedef
struct
ccm128_context
CCM128_CONTEXT
;
...
...
@@ -117,6 +121,12 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
const
unsigned
char
*
inp
,
unsigned
char
*
out
,
size_t
len
);
int
CRYPTO_ccm128_decrypt
(
CCM128_CONTEXT
*
ctx
,
const
unsigned
char
*
inp
,
unsigned
char
*
out
,
size_t
len
);
int
CRYPTO_ccm128_encrypt_ccm64
(
CCM128_CONTEXT
*
ctx
,
const
unsigned
char
*
inp
,
unsigned
char
*
out
,
size_t
len
,
ccm128_f
stream
);
int
CRYPTO_ccm128_decrypt_ccm64
(
CCM128_CONTEXT
*
ctx
,
const
unsigned
char
*
inp
,
unsigned
char
*
out
,
size_t
len
,
ccm128_f
stream
);
size_t
CRYPTO_ccm128_tag
(
CCM128_CONTEXT
*
ctx
,
unsigned
char
*
tag
,
size_t
len
);
typedef
struct
xts128_context
XTS128_CONTEXT
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录