Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Openssl
提交
2c8d0dcc
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,发现更多精彩内容 >>
提交
2c8d0dcc
编写于
5月 05, 2002
作者:
B
Bodo Möller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
improve wNAF generation
上级
a4f576a3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
57 addition
and
36 deletion
+57
-36
crypto/ec/ec_mult.c
crypto/ec/ec_mult.c
+57
-36
未找到文件。
crypto/ec/ec_mult.c
浏览文件 @
2c8d0dcc
/* crypto/ec/ec_mult.c */
/* crypto/ec/ec_mult.c */
/* ====================================================================
/* ====================================================================
* Copyright (c) 1998-200
1
The OpenSSL Project. All rights reserved.
* Copyright (c) 1998-200
2
The OpenSSL Project. All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* modification, are permitted provided that the following conditions
...
@@ -68,25 +68,23 @@
...
@@ -68,25 +68,23 @@
*/
*/
/* Determine the width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
/* Determine the
modified
width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
* This is an array r[] of values that are either zero or odd with an
* This is an array r[] of values that are either zero or odd with an
* absolute value less than 2^w satisfying
* absolute value less than 2^w satisfying
* scalar = \sum_j r[j]*2^j
* scalar = \sum_j r[j]*2^j
* where at most one of any w+1 consecutive digits is non-zero.
* where at most one of any w+1 consecutive digits is non-zero
* with the exception that the most significant digit may be only
* w-1 zeros away from that next non-zero digit.
*/
*/
static
signed
char
*
compute_wNAF
(
const
BIGNUM
*
scalar
,
int
w
,
size_t
*
ret_len
,
BN_CTX
*
ctx
)
static
signed
char
*
compute_wNAF
(
const
BIGNUM
*
scalar
,
int
w
,
size_t
*
ret_len
)
{
{
BIGNUM
*
c
;
int
window_val
;
int
ok
=
0
;
int
ok
=
0
;
signed
char
*
r
=
NULL
;
signed
char
*
r
=
NULL
;
int
sign
=
1
;
int
sign
=
1
;
int
bit
,
next_bit
,
mask
;
int
bit
,
next_bit
,
mask
;
size_t
len
=
0
,
j
;
size_t
len
=
0
,
j
;
BN_CTX_start
(
ctx
);
c
=
BN_CTX_get
(
ctx
);
if
(
c
==
NULL
)
goto
err
;
if
(
w
<=
0
||
w
>
7
)
/* 'signed char' can represent integers with absolute values less than 2^7 */
if
(
w
<=
0
||
w
>
7
)
/* 'signed char' can represent integers with absolute values less than 2^7 */
{
{
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
...
@@ -96,60 +94,84 @@ static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len, B
...
@@ -96,60 +94,84 @@ static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len, B
next_bit
=
bit
<<
1
;
/* at most 256 */
next_bit
=
bit
<<
1
;
/* at most 256 */
mask
=
next_bit
-
1
;
/* at most 255 */
mask
=
next_bit
-
1
;
/* at most 255 */
if
(
!
BN_copy
(
c
,
scalar
))
goto
err
;
if
(
scalar
->
neg
)
if
(
c
->
neg
)
{
{
sign
=
-
1
;
sign
=
-
1
;
c
->
neg
=
0
;
}
}
len
=
BN_num_bits
(
c
)
+
1
;
/* wNAF may be one digit longer than binary representation */
len
=
BN_num_bits
(
scalar
);
r
=
OPENSSL_malloc
(
len
);
r
=
OPENSSL_malloc
(
len
+
1
);
/* modified wNAF may be one digit longer than binary representation */
if
(
r
==
NULL
)
goto
err
;
if
(
r
==
NULL
)
goto
err
;
if
(
scalar
->
d
==
NULL
||
scalar
->
top
==
0
)
{
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
goto
err
;
}
window_val
=
scalar
->
d
[
0
]
&
mask
;
j
=
0
;
j
=
0
;
while
(
!
BN_is_zero
(
c
))
while
(
(
window_val
!=
0
)
||
(
j
+
w
+
1
<
len
))
/* if j+w+1 >= len, window_val will not increase */
{
{
int
u
=
0
;
int
digit
=
0
;
if
(
BN_is_odd
(
c
))
/* 0 <= window_val <= 2^(w+1) */
if
(
window_val
&
1
)
{
{
if
(
c
->
d
==
NULL
||
c
->
top
==
0
)
/* 0 < window_val < 2^(w+1) */
if
(
window_val
&
bit
)
{
{
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
digit
=
window_val
-
next_bit
;
/* -2^w < digit < 0 */
goto
err
;
#if 1
/* modified wNAF */
if
(
j
+
w
+
1
>=
len
)
{
/* special case for generating modified wNAFs:
* no new bits will be added into window_val,
* so using a positive digit here will decrease
* the total length of the representation */
digit
=
window_val
&
(
mask
>>
1
);
/* 0 < digit < 2^w */
}
#endif
}
}
u
=
c
->
d
[
0
]
&
mask
;
else
if
(
u
&
bit
)
{
{
u
-=
next_bit
;
digit
=
window_val
;
/* 0 < digit < 2^w */
/* u < 0 */
if
(
!
BN_add_word
(
c
,
-
u
))
goto
err
;
}
}
else
if
(
digit
<=
-
bit
||
digit
>=
bit
||
!
(
digit
&
1
))
{
{
/* u > 0 */
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
if
(
!
BN_sub_word
(
c
,
u
))
goto
err
;
goto
err
;
}
}
if
(
u
<=
-
bit
||
u
>=
bit
||
!
(
u
&
1
)
||
c
->
neg
)
window_val
-=
digit
;
/* now window_val is 0 or 2^(w+1) in standard wNAF generation;
* for modified window NAFs, it may also be 2^w
*/
if
(
window_val
!=
0
&&
window_val
!=
next_bit
&&
window_val
!=
bit
)
{
{
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
goto
err
;
goto
err
;
}
}
}
}
r
[
j
++
]
=
sign
*
u
;
r
[
j
++
]
=
sign
*
digit
;
if
(
BN_is_odd
(
c
))
window_val
>>=
1
;
window_val
+=
bit
*
BN_is_bit_set
(
scalar
,
j
+
w
);
if
(
window_val
>
next_bit
)
{
{
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
goto
err
;
goto
err
;
}
}
if
(
!
BN_rshift1
(
c
,
c
))
goto
err
;
}
}
if
(
j
>
len
)
if
(
j
>
len
+
1
)
{
{
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
ECerr
(
EC_F_COMPUTE_WNAF
,
ERR_R_INTERNAL_ERROR
);
goto
err
;
goto
err
;
...
@@ -158,7 +180,6 @@ static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len, B
...
@@ -158,7 +180,6 @@ static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len, B
ok
=
1
;
ok
=
1
;
err:
err:
BN_CTX_end
(
ctx
);
if
(
!
ok
)
if
(
!
ok
)
{
{
OPENSSL_free
(
r
);
OPENSSL_free
(
r
);
...
@@ -314,7 +335,7 @@ int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
...
@@ -314,7 +335,7 @@ int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
}
}
wNAF
[
i
+
1
]
=
NULL
;
/* make sure we always have a pivot */
wNAF
[
i
+
1
]
=
NULL
;
/* make sure we always have a pivot */
wNAF
[
i
]
=
compute_wNAF
((
i
<
num
?
scalars
[
i
]
:
scalar
),
wsize
[
i
],
&
wNAF_len
[
i
]
,
ctx
);
wNAF
[
i
]
=
compute_wNAF
((
i
<
num
?
scalars
[
i
]
:
scalar
),
wsize
[
i
],
&
wNAF_len
[
i
]);
if
(
wNAF
[
i
]
==
NULL
)
goto
err
;
if
(
wNAF
[
i
]
==
NULL
)
goto
err
;
if
(
wNAF_len
[
i
]
>
max_len
)
if
(
wNAF_len
[
i
]
>
max_len
)
max_len
=
wNAF_len
[
i
];
max_len
=
wNAF_len
[
i
];
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录