Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
46f365dd
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
46f365dd
编写于
3月 23, 2017
作者:
I
igerasim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8171252: Improve exception checking
8158517: Minor optimizations to ISO10126PADDING Reviewed-by: ascarpino, mschoene
上级
088a356d
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
60 addition
and
592 deletion
+60
-592
src/share/classes/com/sun/crypto/provider/AESCipher.java
src/share/classes/com/sun/crypto/provider/AESCipher.java
+5
-5
src/share/classes/com/sun/crypto/provider/AESWrapCipher.java
src/share/classes/com/sun/crypto/provider/AESWrapCipher.java
+4
-4
src/share/classes/com/sun/crypto/provider/ARCFOURCipher.java
src/share/classes/com/sun/crypto/provider/ARCFOURCipher.java
+2
-2
src/share/classes/com/sun/crypto/provider/BlowfishCipher.java
...share/classes/com/sun/crypto/provider/BlowfishCipher.java
+2
-2
src/share/classes/com/sun/crypto/provider/CipherCore.java
src/share/classes/com/sun/crypto/provider/CipherCore.java
+22
-20
src/share/classes/com/sun/crypto/provider/DESedeWrapCipher.java
...are/classes/com/sun/crypto/provider/DESedeWrapCipher.java
+4
-4
src/share/classes/com/sun/crypto/provider/ISO10126Padding.java
...hare/classes/com/sun/crypto/provider/ISO10126Padding.java
+9
-7
src/share/classes/com/sun/crypto/provider/PBECipherCore.java
src/share/classes/com/sun/crypto/provider/PBECipherCore.java
+0
-535
src/share/classes/com/sun/crypto/provider/PBES1Core.java
src/share/classes/com/sun/crypto/provider/PBES1Core.java
+2
-2
src/share/classes/com/sun/crypto/provider/PKCS5Padding.java
src/share/classes/com/sun/crypto/provider/PKCS5Padding.java
+10
-11
未找到文件。
src/share/classes/com/sun/crypto/provider/AESCipher.java
浏览文件 @
46f365dd
/*
/*
* Copyright (c) 2002, 201
6
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 201
7
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -156,7 +156,7 @@ abstract class AESCipher extends CipherSpi {
...
@@ -156,7 +156,7 @@ abstract class AESCipher extends CipherSpi {
throw
new
InvalidKeyException
(
"Key encoding must not be null"
);
throw
new
InvalidKeyException
(
"Key encoding must not be null"
);
}
else
if
(
value
.
length
!=
fixedKeySize
)
{
}
else
if
(
value
.
length
!=
fixedKeySize
)
{
throw
new
InvalidKeyException
(
"The key must be "
+
throw
new
InvalidKeyException
(
"The key must be "
+
fixedKeySize
*
8
+
" bit
s"
);
fixedKeySize
+
" byte
s"
);
}
}
}
}
}
}
...
@@ -509,7 +509,7 @@ abstract class AESCipher extends CipherSpi {
...
@@ -509,7 +509,7 @@ abstract class AESCipher extends CipherSpi {
throw
new
InvalidKeyException
(
"Invalid AES key length: "
+
throw
new
InvalidKeyException
(
"Invalid AES key length: "
+
encoded
.
length
+
" bytes"
);
encoded
.
length
+
" bytes"
);
}
}
return
encoded
.
length
*
8
;
return
Math
.
multiplyExact
(
encoded
.
length
,
8
)
;
}
}
/**
/**
...
@@ -628,9 +628,9 @@ abstract class AESCipher extends CipherSpi {
...
@@ -628,9 +628,9 @@ abstract class AESCipher extends CipherSpi {
}
}
if
(
src
!=
null
)
{
if
(
src
!=
null
)
{
int
aadLen
=
src
.
limit
()
-
src
.
position
();
int
aadLen
=
src
.
limit
()
-
src
.
position
();
if
(
aadLen
!=
0
)
{
if
(
aadLen
>
0
)
{
if
(
src
.
hasArray
())
{
if
(
src
.
hasArray
())
{
int
aadOfs
=
src
.
arrayOffset
()
+
src
.
position
(
);
int
aadOfs
=
Math
.
addExact
(
src
.
arrayOffset
(),
src
.
position
()
);
core
.
updateAAD
(
src
.
array
(),
aadOfs
,
aadLen
);
core
.
updateAAD
(
src
.
array
(),
aadOfs
,
aadLen
);
src
.
position
(
src
.
limit
());
src
.
position
(
src
.
limit
());
}
else
{
}
else
{
...
...
src/share/classes/com/sun/crypto/provider/AESWrapCipher.java
浏览文件 @
46f365dd
/*
/*
* Copyright (c) 2004, 201
2
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 201
7
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -156,7 +156,7 @@ abstract class AESWrapCipher extends CipherSpi {
...
@@ -156,7 +156,7 @@ abstract class AESWrapCipher extends CipherSpi {
if
(
decrypting
)
{
if
(
decrypting
)
{
result
=
inputLen
-
8
;
result
=
inputLen
-
8
;
}
else
{
}
else
{
result
=
inputLen
+
8
;
result
=
Math
.
addExact
(
inputLen
,
8
)
;
}
}
return
(
result
<
0
?
0
:
result
);
return
(
result
<
0
?
0
:
result
);
}
}
...
@@ -378,7 +378,7 @@ abstract class AESWrapCipher extends CipherSpi {
...
@@ -378,7 +378,7 @@ abstract class AESWrapCipher extends CipherSpi {
throw
new
InvalidKeyException
(
"Invalid key length: "
+
throw
new
InvalidKeyException
(
"Invalid key length: "
+
encoded
.
length
+
" bytes"
);
encoded
.
length
+
" bytes"
);
}
}
return
encoded
.
length
*
8
;
return
Math
.
multiplyExact
(
encoded
.
length
,
8
)
;
}
}
/**
/**
...
@@ -404,7 +404,7 @@ abstract class AESWrapCipher extends CipherSpi {
...
@@ -404,7 +404,7 @@ abstract class AESWrapCipher extends CipherSpi {
throw
new
InvalidKeyException
(
"Cannot get an encoding of "
+
throw
new
InvalidKeyException
(
"Cannot get an encoding of "
+
"the key to be wrapped"
);
"the key to be wrapped"
);
}
}
byte
[]
out
=
new
byte
[
keyVal
.
length
+
8
];
byte
[]
out
=
new
byte
[
Math
.
addExact
(
keyVal
.
length
,
8
)
];
if
(
keyVal
.
length
==
8
)
{
if
(
keyVal
.
length
==
8
)
{
System
.
arraycopy
(
IV
,
0
,
out
,
0
,
IV
.
length
);
System
.
arraycopy
(
IV
,
0
,
out
,
0
,
IV
.
length
);
...
...
src/share/classes/com/sun/crypto/provider/ARCFOURCipher.java
浏览文件 @
46f365dd
/*
/*
* Copyright (c) 2003, 201
0
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 201
7
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -257,7 +257,7 @@ public final class ARCFOURCipher extends CipherSpi {
...
@@ -257,7 +257,7 @@ public final class ARCFOURCipher extends CipherSpi {
// see JCE spec
// see JCE spec
protected
int
engineGetKeySize
(
Key
key
)
throws
InvalidKeyException
{
protected
int
engineGetKeySize
(
Key
key
)
throws
InvalidKeyException
{
byte
[]
encodedKey
=
getEncodedKey
(
key
);
byte
[]
encodedKey
=
getEncodedKey
(
key
);
return
encodedKey
.
length
<<
3
;
return
Math
.
multiplyExact
(
encodedKey
.
length
,
8
)
;
}
}
}
}
src/share/classes/com/sun/crypto/provider/BlowfishCipher.java
浏览文件 @
46f365dd
/*
/*
* Copyright (c) 1998, 20
09
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 20
17
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -373,7 +373,7 @@ public final class BlowfishCipher extends CipherSpi {
...
@@ -373,7 +373,7 @@ public final class BlowfishCipher extends CipherSpi {
* @exception InvalidKeyException if <code>key</code> is invalid.
* @exception InvalidKeyException if <code>key</code> is invalid.
*/
*/
protected
int
engineGetKeySize
(
Key
key
)
throws
InvalidKeyException
{
protected
int
engineGetKeySize
(
Key
key
)
throws
InvalidKeyException
{
return
(
key
.
getEncoded
().
length
*
8
);
return
Math
.
multiplyExact
(
key
.
getEncoded
().
length
,
8
);
}
}
/**
/**
...
...
src/share/classes/com/sun/crypto/provider/CipherCore.java
浏览文件 @
46f365dd
/*
/*
* Copyright (c) 2002, 201
6
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 201
7
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -324,13 +324,14 @@ final class CipherCore {
...
@@ -324,13 +324,14 @@ final class CipherCore {
}
}
private
int
getOutputSizeByOperation
(
int
inputLen
,
boolean
isDoFinal
)
{
private
int
getOutputSizeByOperation
(
int
inputLen
,
boolean
isDoFinal
)
{
int
totalLen
=
buffered
+
inputLen
+
cipher
.
getBufferedLength
();
int
totalLen
=
Math
.
addExact
(
buffered
,
cipher
.
getBufferedLength
());
totalLen
=
Math
.
addExact
(
totalLen
,
inputLen
);
switch
(
cipherMode
)
{
switch
(
cipherMode
)
{
case
GCM_MODE:
case
GCM_MODE:
if
(
isDoFinal
)
{
if
(
isDoFinal
)
{
int
tagLen
=
((
GaloisCounterMode
)
cipher
).
getTagLen
();
int
tagLen
=
((
GaloisCounterMode
)
cipher
).
getTagLen
();
if
(!
decrypting
)
{
if
(!
decrypting
)
{
totalLen
+=
tagLen
;
totalLen
=
Math
.
addExact
(
totalLen
,
tagLen
)
;
}
else
{
}
else
{
totalLen
-=
tagLen
;
totalLen
-=
tagLen
;
}
}
...
@@ -346,10 +347,10 @@ final class CipherCore {
...
@@ -346,10 +347,10 @@ final class CipherCore {
totalLen
=
diffBlocksize
;
totalLen
=
diffBlocksize
;
}
else
{
}
else
{
int
residue
=
(
totalLen
-
diffBlocksize
)
%
blockSize
;
int
residue
=
(
totalLen
-
diffBlocksize
)
%
blockSize
;
totalLen
+=
(
blockSize
-
residue
);
totalLen
=
Math
.
addExact
(
totalLen
,
(
blockSize
-
residue
)
);
}
}
}
else
{
}
else
{
totalLen
+=
padding
.
padLength
(
totalLen
);
totalLen
=
Math
.
addExact
(
totalLen
,
padding
.
padLength
(
totalLen
)
);
}
}
}
}
break
;
break
;
...
@@ -711,7 +712,8 @@ final class CipherCore {
...
@@ -711,7 +712,8 @@ final class CipherCore {
}
}
// figure out how much can be sent to crypto function
// figure out how much can be sent to crypto function
int
len
=
buffered
+
inputLen
-
minBytes
;
int
len
=
Math
.
addExact
(
buffered
,
inputLen
);
len
-=
minBytes
;
if
(
padding
!=
null
&&
decrypting
)
{
if
(
padding
!=
null
&&
decrypting
)
{
// do not include the padding bytes when decrypting
// do not include the padding bytes when decrypting
len
-=
blockSize
;
len
-=
blockSize
;
...
@@ -730,12 +732,12 @@ final class CipherCore {
...
@@ -730,12 +732,12 @@ final class CipherCore {
int
outLen
=
0
;
int
outLen
=
0
;
if
(
len
!=
0
)
{
// there is some work to do
if
(
len
!=
0
)
{
// there is some work to do
if
((
input
==
output
)
if
((
input
==
output
)
&&
(
outputOffset
<
(
inputOffset
+
inputLen
)
)
&&
(
outputOffset
-
inputOffset
<
inputLen
)
&&
(
inputOffset
<
(
outputOffset
+
buffer
.
length
)
))
{
&&
(
inputOffset
-
outputOffset
<
buffer
.
length
))
{
// copy 'input' out to avoid its content being
// copy 'input' out to avoid its content being
// overwritten prematurely.
// overwritten prematurely.
input
=
Arrays
.
copyOfRange
(
input
,
inputOffset
,
input
=
Arrays
.
copyOfRange
(
input
,
inputOffset
,
inputOffset
+
inputLen
);
Math
.
addExact
(
inputOffset
,
inputLen
)
);
inputOffset
=
0
;
inputOffset
=
0
;
}
}
if
(
len
<=
buffered
)
{
if
(
len
<=
buffered
)
{
...
@@ -757,13 +759,13 @@ final class CipherCore {
...
@@ -757,13 +759,13 @@ final class CipherCore {
if
(
bufferCapacity
!=
0
)
{
if
(
bufferCapacity
!=
0
)
{
temp
=
Math
.
min
(
bufferCapacity
,
inputConsumed
);
temp
=
Math
.
min
(
bufferCapacity
,
inputConsumed
);
if
(
unitBytes
!=
blockSize
)
{
if
(
unitBytes
!=
blockSize
)
{
temp
-=
(
(
buffered
+
temp
)
%
unitBytes
);
temp
-=
(
Math
.
addExact
(
buffered
,
temp
)
%
unitBytes
);
}
}
System
.
arraycopy
(
input
,
inputOffset
,
buffer
,
buffered
,
temp
);
System
.
arraycopy
(
input
,
inputOffset
,
buffer
,
buffered
,
temp
);
inputOffset
+=
temp
;
inputOffset
=
Math
.
addExact
(
inputOffset
,
temp
)
;
inputConsumed
-=
temp
;
inputConsumed
-=
temp
;
inputLen
-=
temp
;
inputLen
-=
temp
;
buffered
+=
temp
;
buffered
=
Math
.
addExact
(
buffered
,
temp
)
;
}
}
// process 'buffer'
// process 'buffer'
if
(
decrypting
)
{
if
(
decrypting
)
{
...
@@ -771,7 +773,7 @@ final class CipherCore {
...
@@ -771,7 +773,7 @@ final class CipherCore {
}
else
{
}
else
{
outLen
=
cipher
.
encrypt
(
buffer
,
0
,
buffered
,
output
,
outputOffset
);
outLen
=
cipher
.
encrypt
(
buffer
,
0
,
buffered
,
output
,
outputOffset
);
}
}
outputOffset
+=
outLen
;
outputOffset
=
Math
.
addExact
(
outputOffset
,
outLen
)
;
buffered
=
0
;
buffered
=
0
;
}
}
if
(
inputConsumed
>
0
)
{
// still has input to process
if
(
inputConsumed
>
0
)
{
// still has input to process
...
@@ -802,7 +804,7 @@ final class CipherCore {
...
@@ -802,7 +804,7 @@ final class CipherCore {
if
(
inputLen
>
0
)
{
if
(
inputLen
>
0
)
{
System
.
arraycopy
(
input
,
inputOffset
,
buffer
,
buffered
,
System
.
arraycopy
(
input
,
inputOffset
,
buffer
,
buffered
,
inputLen
);
inputLen
);
buffered
+=
inputLen
;
buffered
=
Math
.
addExact
(
buffered
,
inputLen
)
;
}
}
return
outLen
;
return
outLen
;
}
}
...
@@ -912,10 +914,10 @@ final class CipherCore {
...
@@ -912,10 +914,10 @@ final class CipherCore {
}
}
// calculate total input length
// calculate total input length
int
len
=
buffered
+
inputLen
;
int
len
=
Math
.
addExact
(
buffered
,
inputLen
)
;
// calculate padding length
// calculate padding length
int
totalLen
=
len
+
cipher
.
getBufferedLength
(
);
int
totalLen
=
Math
.
addExact
(
len
,
cipher
.
getBufferedLength
()
);
int
paddingLen
=
0
;
int
paddingLen
=
0
;
// will the total input length be a multiple of blockSize?
// will the total input length be a multiple of blockSize?
if
(
unitBytes
!=
blockSize
)
{
if
(
unitBytes
!=
blockSize
)
{
...
@@ -948,12 +950,12 @@ final class CipherCore {
...
@@ -948,12 +950,12 @@ final class CipherCore {
int
finalBufLen
=
inputLen
;
int
finalBufLen
=
inputLen
;
if
((
buffered
!=
0
)
||
(!
decrypting
&&
padding
!=
null
)
||
if
((
buffered
!=
0
)
||
(!
decrypting
&&
padding
!=
null
)
||
((
input
==
output
)
((
input
==
output
)
&&
(
outputOffset
<
(
inputOffset
+
inputLen
)
)
&&
(
outputOffset
-
inputOffset
<
inputLen
)
&&
(
inputOffset
<
(
outputOffset
+
buffer
.
length
)
)))
{
&&
(
inputOffset
-
outputOffset
<
buffer
.
length
)))
{
if
(
decrypting
||
padding
==
null
)
{
if
(
decrypting
||
padding
==
null
)
{
paddingLen
=
0
;
paddingLen
=
0
;
}
}
finalBuf
=
new
byte
[
len
+
paddingLen
];
finalBuf
=
new
byte
[
Math
.
addExact
(
len
,
paddingLen
)
];
finalOffset
=
0
;
finalOffset
=
0
;
if
(
buffered
!=
0
)
{
if
(
buffered
!=
0
)
{
System
.
arraycopy
(
buffer
,
0
,
finalBuf
,
0
,
buffered
);
System
.
arraycopy
(
buffer
,
0
,
finalBuf
,
0
,
buffered
);
...
@@ -963,7 +965,7 @@ final class CipherCore {
...
@@ -963,7 +965,7 @@ final class CipherCore {
buffered
,
inputLen
);
buffered
,
inputLen
);
}
}
if
(
paddingLen
!=
0
)
{
if
(
paddingLen
!=
0
)
{
padding
.
padWithLen
(
finalBuf
,
(
buffered
+
inputLen
),
paddingLen
);
padding
.
padWithLen
(
finalBuf
,
Math
.
addExact
(
buffered
,
inputLen
),
paddingLen
);
}
}
finalBufLen
=
finalBuf
.
length
;
finalBufLen
=
finalBuf
.
length
;
}
}
...
...
src/share/classes/com/sun/crypto/provider/DESedeWrapCipher.java
浏览文件 @
46f365dd
/*
/*
* Copyright (c) 2004, 201
3
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 201
7
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -140,7 +140,7 @@ public final class DESedeWrapCipher extends CipherSpi {
...
@@ -140,7 +140,7 @@ public final class DESedeWrapCipher extends CipherSpi {
if
(
decrypting
)
{
if
(
decrypting
)
{
result
=
inputLen
-
16
;
// CHECKSUM_LEN + IV_LEN;
result
=
inputLen
-
16
;
// CHECKSUM_LEN + IV_LEN;
}
else
{
}
else
{
result
=
inputLen
+
16
;
result
=
Math
.
addExact
(
inputLen
,
16
)
;
}
}
return
(
result
<
0
?
0
:
result
);
return
(
result
<
0
?
0
:
result
);
}
}
...
@@ -452,11 +452,11 @@ public final class DESedeWrapCipher extends CipherSpi {
...
@@ -452,11 +452,11 @@ public final class DESedeWrapCipher extends CipherSpi {
}
}
byte
[]
cks
=
getChecksum
(
keyVal
);
byte
[]
cks
=
getChecksum
(
keyVal
);
byte
[]
in
=
new
byte
[
keyVal
.
length
+
CHECKSUM_LEN
];
byte
[]
in
=
new
byte
[
Math
.
addExact
(
keyVal
.
length
,
CHECKSUM_LEN
)
];
System
.
arraycopy
(
keyVal
,
0
,
in
,
0
,
keyVal
.
length
);
System
.
arraycopy
(
keyVal
,
0
,
in
,
0
,
keyVal
.
length
);
System
.
arraycopy
(
cks
,
0
,
in
,
keyVal
.
length
,
CHECKSUM_LEN
);
System
.
arraycopy
(
cks
,
0
,
in
,
keyVal
.
length
,
CHECKSUM_LEN
);
byte
[]
out
=
new
byte
[
iv
.
length
+
in
.
length
];
byte
[]
out
=
new
byte
[
Math
.
addExact
(
iv
.
length
,
in
.
length
)
];
System
.
arraycopy
(
iv
,
0
,
out
,
0
,
iv
.
length
);
System
.
arraycopy
(
iv
,
0
,
out
,
0
,
iv
.
length
);
cipher
.
encrypt
(
in
,
0
,
in
.
length
,
out
,
iv
.
length
);
cipher
.
encrypt
(
in
,
0
,
in
.
length
,
out
,
iv
.
length
);
...
...
src/share/classes/com/sun/crypto/provider/ISO10126Padding.java
浏览文件 @
46f365dd
/*
/*
* Copyright (c) 2003, 201
3
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 201
7
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -63,15 +63,16 @@ final class ISO10126Padding implements Padding {
...
@@ -63,15 +63,16 @@ final class ISO10126Padding implements Padding {
if
(
in
==
null
)
if
(
in
==
null
)
return
;
return
;
if
((
off
+
len
)
>
in
.
length
)
{
int
idx
=
Math
.
addExact
(
off
,
len
);
if
(
idx
>
in
.
length
)
{
throw
new
ShortBufferException
(
"Buffer too small to hold padding"
);
throw
new
ShortBufferException
(
"Buffer too small to hold padding"
);
}
}
byte
paddingOctet
=
(
byte
)
(
len
&
0xff
);
byte
paddingOctet
=
(
byte
)
(
len
&
0xff
);
byte
[]
padding
=
new
byte
[
len
];
byte
[]
padding
=
new
byte
[
len
-
1
];
SunJCE
.
getRandom
().
nextBytes
(
padding
);
SunJCE
.
getRandom
().
nextBytes
(
padding
);
padding
[
len
-
1
]
=
paddingOctet
;
System
.
arraycopy
(
padding
,
0
,
in
,
off
,
len
-
1
)
;
System
.
arraycopy
(
padding
,
0
,
in
,
off
,
len
)
;
in
[
idx
-
1
]
=
paddingOctet
;
return
;
return
;
}
}
...
@@ -94,14 +95,15 @@ final class ISO10126Padding implements Padding {
...
@@ -94,14 +95,15 @@ final class ISO10126Padding implements Padding {
return
0
;
return
0
;
}
}
byte
lastByte
=
in
[
off
+
len
-
1
];
int
idx
=
Math
.
addExact
(
off
,
len
);
byte
lastByte
=
in
[
idx
-
1
];
int
padValue
=
(
int
)
lastByte
&
0x0ff
;
int
padValue
=
(
int
)
lastByte
&
0x0ff
;
if
((
padValue
<
0x01
)
if
((
padValue
<
0x01
)
||
(
padValue
>
blockSize
))
{
||
(
padValue
>
blockSize
))
{
return
-
1
;
return
-
1
;
}
}
int
start
=
off
+
len
-
((
int
)
lastByte
&
0x0ff
)
;
int
start
=
idx
-
padValue
;
if
(
start
<
off
)
{
if
(
start
<
off
)
{
return
-
1
;
return
-
1
;
}
}
...
...
src/share/classes/com/sun/crypto/provider/PBECipherCore.java
已删除
100644 → 0
浏览文件 @
088a356d
/*
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package
com.sun.crypto.provider
;
import
java.security.*
;
import
java.security.spec.*
;
import
javax.crypto.*
;
import
javax.crypto.spec.*
;
/**
* This class represents password-based encryption as defined by the PKCS #5
* standard.
*
* @author Jan Luehe
*
*
* @see javax.crypto.Cipher
*/
final
class
PBECipherCore
{
// the encapsulated DES cipher
private
CipherCore
cipher
;
private
MessageDigest
md
;
private
int
blkSize
;
private
String
algo
=
null
;
private
byte
[]
salt
=
null
;
private
int
iCount
=
10
;
/**
* Creates an instance of PBE Cipher using the specified CipherSpi
* instance.
*
*/
PBECipherCore
(
String
cipherAlg
)
throws
NoSuchAlgorithmException
,
NoSuchPaddingException
{
algo
=
cipherAlg
;
if
(
algo
.
equals
(
"DES"
))
{
cipher
=
new
CipherCore
(
new
DESCrypt
(),
DESConstants
.
DES_BLOCK_SIZE
);
}
else
if
(
algo
.
equals
(
"DESede"
))
{
cipher
=
new
CipherCore
(
new
DESedeCrypt
(),
DESConstants
.
DES_BLOCK_SIZE
);
}
else
{
throw
new
NoSuchAlgorithmException
(
"No Cipher implementation "
+
"for PBEWithMD5And"
+
algo
);
}
cipher
.
setMode
(
"CBC"
);
cipher
.
setPadding
(
"PKCS5Padding"
);
// get instance of MD5
md
=
MessageDigest
.
getInstance
(
"MD5"
);
}
/**
* Sets the mode of this cipher. This algorithm can only be run in CBC
* mode.
*
* @param mode the cipher mode
*
* @exception NoSuchAlgorithmException if the requested cipher mode is
* invalid
*/
void
setMode
(
String
mode
)
throws
NoSuchAlgorithmException
{
cipher
.
setMode
(
mode
);
}
/**
* Sets the padding mechanism of this cipher. This algorithm only uses
* PKCS #5 padding.
*
* @param padding the padding mechanism
*
* @exception NoSuchPaddingException if the requested padding mechanism
* is invalid
*/
void
setPadding
(
String
paddingScheme
)
throws
NoSuchPaddingException
{
cipher
.
setPadding
(
paddingScheme
);
}
/**
* Returns the block size (in bytes).
*
* @return the block size (in bytes)
*/
int
getBlockSize
()
{
return
DESConstants
.
DES_BLOCK_SIZE
;
}
/**
* Returns the length in bytes that an output buffer would need to be in
* order to hold the result of the next <code>update</code> or
* <code>doFinal</code> operation, given the input length
* <code>inputLen</code> (in bytes).
*
* <p>This call takes into account any unprocessed (buffered) data from a
* previous <code>update</code> call, and padding.
*
* <p>The actual output length of the next <code>update</code> or
* <code>doFinal</code> call may be smaller than the length returned by
* this method.
*
* @param inputLen the input length (in bytes)
*
* @return the required output buffer size (in bytes)
*
*/
int
getOutputSize
(
int
inputLen
)
{
return
cipher
.
getOutputSize
(
inputLen
);
}
/**
* Returns the initialization vector (IV) in a new buffer.
*
* <p> This is useful in the case where a random IV has been created
* (see <a href = "#init">init</a>),
* or in the context of password-based encryption or
* decryption, where the IV is derived from a user-supplied password.
*
* @return the initialization vector in a new buffer, or null if the
* underlying algorithm does not use an IV, or if the IV has not yet
* been set.
*/
byte
[]
getIV
()
{
return
cipher
.
getIV
();
}
/**
* Returns the parameters used with this cipher.
*
* <p>The returned parameters may be the same that were used to initialize
* this cipher, or may contain the default set of parameters or a set of
* randomly generated parameters used by the underlying cipher
* implementation (provided that the underlying cipher implementation
* uses a default set of parameters or creates new parameters if it needs
* parameters but was not initialized with any).
*
* @return the parameters used with this cipher, or null if this cipher
* does not use any parameters.
*/
AlgorithmParameters
getParameters
()
{
AlgorithmParameters
params
=
null
;
if
(
salt
==
null
)
{
salt
=
new
byte
[
8
];
SunJCE
.
getRandom
().
nextBytes
(
salt
);
}
PBEParameterSpec
pbeSpec
=
new
PBEParameterSpec
(
salt
,
iCount
);
try
{
params
=
AlgorithmParameters
.
getInstance
(
"PBEWithMD5And"
+
(
algo
.
equalsIgnoreCase
(
"DES"
)?
"DES"
:
"TripleDES"
),
SunJCE
.
getInstance
());
params
.
init
(
pbeSpec
);
}
catch
(
NoSuchAlgorithmException
nsae
)
{
// should never happen
throw
new
RuntimeException
(
"SunJCE called, but not configured"
);
}
catch
(
InvalidParameterSpecException
ipse
)
{
// should never happen
throw
new
RuntimeException
(
"PBEParameterSpec not supported"
);
}
return
params
;
}
/**
* Initializes this cipher with a key, a set of
* algorithm parameters, and a source of randomness.
* The cipher is initialized for one of the following four operations:
* encryption, decryption, key wrapping or key unwrapping, depending on
* the value of <code>opmode</code>.
*
* <p>If this cipher (including its underlying feedback or padding scheme)
* requires any random bytes, it will get them from <code>random</code>.
*
* @param opmode the operation mode of this cipher (this is one of
* the following:
* <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>),
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
* @param key the encryption key
* @param params the algorithm parameters
* @param random the source of randomness
*
* @exception InvalidKeyException if the given key is inappropriate for
* initializing this cipher
* @exception InvalidAlgorithmParameterException if the given algorithm
* parameters are inappropriate for this cipher
*/
void
init
(
int
opmode
,
Key
key
,
AlgorithmParameterSpec
params
,
SecureRandom
random
)
throws
InvalidKeyException
,
InvalidAlgorithmParameterException
{
if
(((
opmode
==
Cipher
.
DECRYPT_MODE
)
||
(
opmode
==
Cipher
.
UNWRAP_MODE
))
&&
(
params
==
null
))
{
throw
new
InvalidAlgorithmParameterException
(
"Parameters "
+
"missing"
);
}
if
((
key
==
null
)
||
(
key
.
getEncoded
()
==
null
)
||
!(
key
.
getAlgorithm
().
regionMatches
(
true
,
0
,
"PBE"
,
0
,
3
)))
{
throw
new
InvalidKeyException
(
"Missing password"
);
}
if
(
params
==
null
)
{
// create random salt and use default iteration count
salt
=
new
byte
[
8
];
random
.
nextBytes
(
salt
);
}
else
{
if
(!(
params
instanceof
PBEParameterSpec
))
{
throw
new
InvalidAlgorithmParameterException
(
"Wrong parameter type: PBE expected"
);
}
salt
=
((
PBEParameterSpec
)
params
).
getSalt
();
// salt must be 8 bytes long (by definition)
if
(
salt
.
length
!=
8
)
{
throw
new
InvalidAlgorithmParameterException
(
"Salt must be 8 bytes long"
);
}
iCount
=
((
PBEParameterSpec
)
params
).
getIterationCount
();
if
(
iCount
<=
0
)
{
throw
new
InvalidAlgorithmParameterException
(
"IterationCount must be a positive number"
);
}
}
byte
[]
derivedKey
=
deriveCipherKey
(
key
);
// use all but the last 8 bytes as the key value
SecretKeySpec
cipherKey
=
new
SecretKeySpec
(
derivedKey
,
0
,
derivedKey
.
length
-
8
,
algo
);
// use the last 8 bytes as the IV
IvParameterSpec
ivSpec
=
new
IvParameterSpec
(
derivedKey
,
derivedKey
.
length
-
8
,
8
);
// initialize the underlying cipher
cipher
.
init
(
opmode
,
cipherKey
,
ivSpec
,
random
);
}
private
byte
[]
deriveCipherKey
(
Key
key
)
{
byte
[]
result
=
null
;
byte
[]
passwdBytes
=
key
.
getEncoded
();
if
(
algo
.
equals
(
"DES"
))
{
// P || S (password concatenated with salt)
byte
[]
concat
=
new
byte
[
passwdBytes
.
length
+
salt
.
length
];
System
.
arraycopy
(
passwdBytes
,
0
,
concat
,
0
,
passwdBytes
.
length
);
java
.
util
.
Arrays
.
fill
(
passwdBytes
,
(
byte
)
0x00
);
System
.
arraycopy
(
salt
,
0
,
concat
,
passwdBytes
.
length
,
salt
.
length
);
// digest P || S with c iterations
byte
[]
toBeHashed
=
concat
;
for
(
int
i
=
0
;
i
<
iCount
;
i
++)
{
md
.
update
(
toBeHashed
);
toBeHashed
=
md
.
digest
();
// this resets the digest
}
java
.
util
.
Arrays
.
fill
(
concat
,
(
byte
)
0x00
);
result
=
toBeHashed
;
}
else
if
(
algo
.
equals
(
"DESede"
))
{
// if the 2 salt halves are the same, invert one of them
int
i
;
for
(
i
=
0
;
i
<
4
;
i
++)
{
if
(
salt
[
i
]
!=
salt
[
i
+
4
])
break
;
}
if
(
i
==
4
)
{
// same, invert 1st half
for
(
i
=
0
;
i
<
2
;
i
++)
{
byte
tmp
=
salt
[
i
];
salt
[
i
]
=
salt
[
3
-
i
];
salt
[
3
-
1
]
=
tmp
;
}
}
// Now digest each half (concatenated with password). For each
// half, go through the loop as many times as specified by the
// iteration count parameter (inner for loop).
// Concatenate the output from each digest round with the
// password, and use the result as the input to the next digest
// operation.
byte
[]
kBytes
=
null
;
IvParameterSpec
iv
=
null
;
byte
[]
toBeHashed
=
null
;
result
=
new
byte
[
DESedeKeySpec
.
DES_EDE_KEY_LEN
+
DESConstants
.
DES_BLOCK_SIZE
];
for
(
i
=
0
;
i
<
2
;
i
++)
{
toBeHashed
=
new
byte
[
salt
.
length
/
2
];
System
.
arraycopy
(
salt
,
i
*(
salt
.
length
/
2
),
toBeHashed
,
0
,
toBeHashed
.
length
);
for
(
int
j
=
0
;
j
<
iCount
;
j
++)
{
md
.
update
(
toBeHashed
);
md
.
update
(
passwdBytes
);
toBeHashed
=
md
.
digest
();
// this resets the digest
}
System
.
arraycopy
(
toBeHashed
,
0
,
result
,
i
*
16
,
toBeHashed
.
length
);
}
}
return
result
;
}
void
init
(
int
opmode
,
Key
key
,
AlgorithmParameters
params
,
SecureRandom
random
)
throws
InvalidKeyException
,
InvalidAlgorithmParameterException
{
PBEParameterSpec
pbeSpec
=
null
;
if
(
params
!=
null
)
{
try
{
pbeSpec
=
params
.
getParameterSpec
(
PBEParameterSpec
.
class
);
}
catch
(
InvalidParameterSpecException
ipse
)
{
throw
new
InvalidAlgorithmParameterException
(
"Wrong parameter "
+
"type: PBE "
+
"expected"
);
}
}
init
(
opmode
,
key
,
pbeSpec
,
random
);
}
/**
* Continues a multiple-part encryption or decryption operation
* (depending on how this cipher was initialized), processing another data
* part.
*
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
* buffer, starting at <code>inputOffset</code>, are processed, and the
* result is stored in a new buffer.
*
* @param input the input buffer
* @param inputOffset the offset in <code>input</code> where the input
* starts
* @param inputLen the input length
*
* @return the new buffer with the result
*
*/
byte
[]
update
(
byte
[]
input
,
int
inputOffset
,
int
inputLen
)
{
return
cipher
.
update
(
input
,
inputOffset
,
inputLen
);
}
/**
* Continues a multiple-part encryption or decryption operation
* (depending on how this cipher was initialized), processing another data
* part.
*
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
* buffer, starting at <code>inputOffset</code>, are processed, and the
* result is stored in the <code>output</code> buffer, starting at
* <code>outputOffset</code>.
*
* @param input the input buffer
* @param inputOffset the offset in <code>input</code> where the input
* starts
* @param inputLen the input length
* @param output the buffer for the result
* @param outputOffset the offset in <code>output</code> where the result
* is stored
*
* @return the number of bytes stored in <code>output</code>
*
* @exception ShortBufferException if the given output buffer is too small
* to hold the result
*/
int
update
(
byte
[]
input
,
int
inputOffset
,
int
inputLen
,
byte
[]
output
,
int
outputOffset
)
throws
ShortBufferException
{
return
cipher
.
update
(
input
,
inputOffset
,
inputLen
,
output
,
outputOffset
);
}
/**
* Encrypts or decrypts data in a single-part operation,
* or finishes a multiple-part operation.
* The data is encrypted or decrypted, depending on how this cipher was
* initialized.
*
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
* buffer, starting at <code>inputOffset</code>, and any input bytes that
* may have been buffered during a previous <code>update</code> operation,
* are processed, with padding (if requested) being applied.
* The result is stored in a new buffer.
*
* <p>The cipher is reset to its initial state (uninitialized) after this
* call.
*
* @param input the input buffer
* @param inputOffset the offset in <code>input</code> where the input
* starts
* @param inputLen the input length
*
* @return the new buffer with the result
*
* @exception IllegalBlockSizeException if this cipher is a block cipher,
* no padding has been requested (only in encryption mode), and the total
* input length of the data processed by this cipher is not a multiple of
* block size
* @exception BadPaddingException if decrypting and padding is chosen,
* but the last input data does not have proper padding bytes.
*/
byte
[]
doFinal
(
byte
[]
input
,
int
inputOffset
,
int
inputLen
)
throws
IllegalBlockSizeException
,
BadPaddingException
{
return
cipher
.
doFinal
(
input
,
inputOffset
,
inputLen
);
}
/**
* Encrypts or decrypts data in a single-part operation,
* or finishes a multiple-part operation.
* The data is encrypted or decrypted, depending on how this cipher was
* initialized.
*
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
* buffer, starting at <code>inputOffset</code>, and any input bytes that
* may have been buffered during a previous <code>update</code> operation,
* are processed, with padding (if requested) being applied.
* The result is stored in the <code>output</code> buffer, starting at
* <code>outputOffset</code>.
*
* <p>The cipher is reset to its initial state (uninitialized) after this
* call.
*
* @param input the input buffer
* @param inputOffset the offset in <code>input</code> where the input
* starts
* @param inputLen the input length
* @param output the buffer for the result
* @param outputOffset the offset in <code>output</code> where the result
* is stored
*
* @return the number of bytes stored in <code>output</code>
*
* @exception IllegalBlockSizeException if this cipher is a block cipher,
* no padding has been requested (only in encryption mode), and the total
* input length of the data processed by this cipher is not a multiple of
* block size
* @exception ShortBufferException if the given output buffer is too small
* to hold the result
* @exception BadPaddingException if decrypting and padding is chosen,
* but the last input data does not have proper padding bytes.
*/
int
doFinal
(
byte
[]
input
,
int
inputOffset
,
int
inputLen
,
byte
[]
output
,
int
outputOffset
)
throws
ShortBufferException
,
IllegalBlockSizeException
,
BadPaddingException
{
return
cipher
.
doFinal
(
input
,
inputOffset
,
inputLen
,
output
,
outputOffset
);
}
/**
* Wrap a key.
*
* @param key the key to be wrapped.
*
* @return the wrapped key.
*
* @exception IllegalBlockSizeException if this cipher is a block
* cipher, no padding has been requested, and the length of the
* encoding of the key to be wrapped is not a
* multiple of the block size.
*
* @exception InvalidKeyException if it is impossible or unsafe to
* wrap the key with this cipher (e.g., a hardware protected key is
* being passed to a software only cipher).
*/
byte
[]
wrap
(
Key
key
)
throws
IllegalBlockSizeException
,
InvalidKeyException
{
byte
[]
result
=
null
;
try
{
byte
[]
encodedKey
=
key
.
getEncoded
();
if
((
encodedKey
==
null
)
||
(
encodedKey
.
length
==
0
))
{
throw
new
InvalidKeyException
(
"Cannot get an encoding of "
+
"the key to be wrapped"
);
}
result
=
doFinal
(
encodedKey
,
0
,
encodedKey
.
length
);
}
catch
(
BadPaddingException
e
)
{
// Should never happen
}
return
result
;
}
/**
* Unwrap a previously wrapped key.
*
* @param wrappedKey the key to be unwrapped.
*
* @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
*
* @param wrappedKeyType the type of the wrapped key.
* This is one of <code>Cipher.SECRET_KEY</code>,
* <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
*
* @return the unwrapped key.
*
* @exception NoSuchAlgorithmException if no installed providers
* can create keys of type <code>wrappedKeyType</code> for the
* <code>wrappedKeyAlgorithm</code>.
*
* @exception InvalidKeyException if <code>wrappedKey</code> does not
* represent a wrapped key of type <code>wrappedKeyType</code> for
* the <code>wrappedKeyAlgorithm</code>.
*/
Key
unwrap
(
byte
[]
wrappedKey
,
String
wrappedKeyAlgorithm
,
int
wrappedKeyType
)
throws
InvalidKeyException
,
NoSuchAlgorithmException
{
byte
[]
encodedKey
;
try
{
encodedKey
=
doFinal
(
wrappedKey
,
0
,
wrappedKey
.
length
);
}
catch
(
BadPaddingException
ePadding
)
{
throw
new
InvalidKeyException
(
"The wrapped key is not padded "
+
"correctly"
);
}
catch
(
IllegalBlockSizeException
eBlockSize
)
{
throw
new
InvalidKeyException
(
"The wrapped key does not have "
+
"the correct length"
);
}
return
ConstructKeys
.
constructKey
(
encodedKey
,
wrappedKeyAlgorithm
,
wrappedKeyType
);
}
}
src/share/classes/com/sun/crypto/provider/PBES1Core.java
浏览文件 @
46f365dd
/*
/*
* Copyright (c) 2002, 201
3
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 201
7
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -260,7 +260,7 @@ final class PBES1Core {
...
@@ -260,7 +260,7 @@ final class PBES1Core {
if
(
algo
.
equals
(
"DES"
))
{
if
(
algo
.
equals
(
"DES"
))
{
// P || S (password concatenated with salt)
// P || S (password concatenated with salt)
byte
[]
concat
=
new
byte
[
passwdBytes
.
length
+
salt
.
length
];
byte
[]
concat
=
new
byte
[
Math
.
addExact
(
passwdBytes
.
length
,
salt
.
length
)
];
System
.
arraycopy
(
passwdBytes
,
0
,
concat
,
0
,
passwdBytes
.
length
);
System
.
arraycopy
(
passwdBytes
,
0
,
concat
,
0
,
passwdBytes
.
length
);
java
.
util
.
Arrays
.
fill
(
passwdBytes
,
(
byte
)
0x00
);
java
.
util
.
Arrays
.
fill
(
passwdBytes
,
(
byte
)
0x00
);
System
.
arraycopy
(
salt
,
0
,
concat
,
passwdBytes
.
length
,
salt
.
length
);
System
.
arraycopy
(
salt
,
0
,
concat
,
passwdBytes
.
length
,
salt
.
length
);
...
...
src/share/classes/com/sun/crypto/provider/PKCS5Padding.java
浏览文件 @
46f365dd
/*
/*
* Copyright (c) 1997, 20
0
7, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 20
1
7, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -26,6 +26,7 @@
...
@@ -26,6 +26,7 @@
package
com.sun.crypto.provider
;
package
com.sun.crypto.provider
;
import
javax.crypto.ShortBufferException
;
import
javax.crypto.ShortBufferException
;
import
java.util.Arrays
;
/**
/**
* This class implements padding as specified in the PKCS#5 standard.
* This class implements padding as specified in the PKCS#5 standard.
...
@@ -63,14 +64,13 @@ final class PKCS5Padding implements Padding {
...
@@ -63,14 +64,13 @@ final class PKCS5Padding implements Padding {
if
(
in
==
null
)
if
(
in
==
null
)
return
;
return
;
if
((
off
+
len
)
>
in
.
length
)
{
int
idx
=
Math
.
addExact
(
off
,
len
);
if
(
idx
>
in
.
length
)
{
throw
new
ShortBufferException
(
"Buffer too small to hold padding"
);
throw
new
ShortBufferException
(
"Buffer too small to hold padding"
);
}
}
byte
paddingOctet
=
(
byte
)
(
len
&
0xff
);
byte
paddingOctet
=
(
byte
)
(
len
&
0xff
);
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
Arrays
.
fill
(
in
,
off
,
idx
,
paddingOctet
);
in
[
i
+
off
]
=
paddingOctet
;
}
return
;
return
;
}
}
...
@@ -92,25 +92,24 @@ final class PKCS5Padding implements Padding {
...
@@ -92,25 +92,24 @@ final class PKCS5Padding implements Padding {
(
len
==
0
))
{
// this can happen if input is really a padded buffer
(
len
==
0
))
{
// this can happen if input is really a padded buffer
return
0
;
return
0
;
}
}
int
idx
=
Math
.
addExact
(
off
,
len
);
byte
lastByte
=
in
[
off
+
len
-
1
];
byte
lastByte
=
in
[
idx
-
1
];
int
padValue
=
(
int
)
lastByte
&
0x0ff
;
int
padValue
=
(
int
)
lastByte
&
0x0ff
;
if
((
padValue
<
0x01
)
if
((
padValue
<
0x01
)
||
(
padValue
>
blockSize
))
{
||
(
padValue
>
blockSize
))
{
return
-
1
;
return
-
1
;
}
}
int
start
=
off
+
len
-
((
int
)
lastByte
&
0x0ff
)
;
int
start
=
idx
-
padValue
;
if
(
start
<
off
)
{
if
(
start
<
off
)
{
return
-
1
;
return
-
1
;
}
}
for
(
int
i
=
0
;
i
<
((
int
)
lastByte
&
0x0ff
)
;
i
++)
{
for
(
int
i
=
start
;
i
<
idx
;
i
++)
{
if
(
in
[
start
+
i
]
!=
lastByte
)
{
if
(
in
[
i
]
!=
lastByte
)
{
return
-
1
;
return
-
1
;
}
}
}
}
return
start
;
return
start
;
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录