1
1
using System ;
2
- using System . Diagnostics ;
3
2
4
3
using Org . BouncyCastle . Crypto ;
5
4
using Org . BouncyCastle . Crypto . Digests ;
6
5
using Org . BouncyCastle . Pqc . Crypto . Ntru . Owcpa ;
7
- using Org . BouncyCastle . Pqc . Crypto . Ntru . ParameterSets ;
6
+ using Org . BouncyCastle . Utilities ;
8
7
9
8
namespace Org . BouncyCastle . Pqc . Crypto . Ntru
10
9
{
11
10
/// <summary>
12
11
/// NTRU secret encapsulation extractor.
13
12
/// </summary>
14
- public class NtruKemExtractor : IEncapsulatedSecretExtractor
13
+ public class NtruKemExtractor
14
+ : IEncapsulatedSecretExtractor
15
15
{
16
- private readonly NtruParameters _parameters ;
17
- private readonly NtruPrivateKeyParameters _ntruPrivateKey ;
16
+ private readonly NtruPrivateKeyParameters m_privateKey ;
18
17
19
18
public NtruKemExtractor ( NtruPrivateKeyParameters ntruPrivateKey )
20
19
{
21
- _parameters = ntruPrivateKey . Parameters ;
22
- _ntruPrivateKey = ntruPrivateKey ;
20
+ m_privateKey = ntruPrivateKey ?? throw new ArgumentNullException ( nameof ( ntruPrivateKey ) ) ;
23
21
}
24
22
25
-
26
23
public byte [ ] ExtractSecret ( byte [ ] encapsulation )
27
24
{
28
- Debug . Assert ( _ntruPrivateKey != null ) ;
25
+ var parameterSet = m_privateKey . Parameters . ParameterSet ;
29
26
30
- NtruParameterSet parameterSet = _parameters . ParameterSet ;
27
+ if ( encapsulation == null )
28
+ throw new ArgumentNullException ( nameof ( encapsulation ) ) ;
29
+ if ( encapsulation . Length != parameterSet . NtruCiphertextBytes ( ) )
30
+ throw new ArgumentException ( nameof ( encapsulation ) ) ;
31
31
32
- byte [ ] sk = _ntruPrivateKey . PrivateKey ;
33
- int i , fail ;
34
- byte [ ] rm ;
35
- byte [ ] buf = new byte [ parameterSet . PrfKeyBytes + parameterSet . NtruCiphertextBytes ( ) ] ;
32
+ // TODO[pqc] Avoid copy?
33
+ byte [ ] sk = m_privateKey . GetEncoded ( ) ;
36
34
37
35
NtruOwcpa owcpa = new NtruOwcpa ( parameterSet ) ;
38
- OwcpaDecryptResult owcpaResult = owcpa . Decrypt ( encapsulation , _ntruPrivateKey . PrivateKey ) ;
39
- rm = owcpaResult . Rm ;
40
- fail = owcpaResult . Fail ;
36
+ OwcpaDecryptResult owcpaResult = owcpa . Decrypt ( encapsulation , sk ) ;
37
+ byte [ ] rm = owcpaResult . Rm ;
38
+ int fail = owcpaResult . Fail ;
41
39
42
40
Sha3Digest sha3256 = new Sha3Digest ( 256 ) ;
43
-
44
41
byte [ ] k = new byte [ sha3256 . GetDigestSize ( ) ] ;
45
42
46
43
sha3256 . BlockUpdate ( rm , 0 , rm . Length ) ;
47
44
sha3256 . DoFinal ( k , 0 ) ;
48
45
49
46
/* shake(secret PRF key || input ciphertext) */
50
- for ( i = 0 ; i < parameterSet . PrfKeyBytes ; i ++ )
51
- {
52
- buf [ i ] = sk [ i + parameterSet . OwcpaSecretKeyBytes ( ) ] ;
53
- }
54
-
55
- for ( i = 0 ; i < parameterSet . NtruCiphertextBytes ( ) ; i ++ )
56
- {
57
- buf [ parameterSet . PrfKeyBytes + i ] = encapsulation [ i ] ;
58
- }
59
-
60
- sha3256 . Reset ( ) ;
61
- sha3256 . BlockUpdate ( buf , 0 , buf . Length ) ;
47
+ sha3256 . BlockUpdate ( sk , parameterSet . OwcpaSecretKeyBytes ( ) , parameterSet . PrfKeyBytes ) ;
48
+ sha3256 . BlockUpdate ( encapsulation , 0 , encapsulation . Length ) ;
62
49
sha3256 . DoFinal ( rm , 0 ) ;
63
50
64
51
Cmov ( k , rm , ( byte ) fail ) ;
65
52
66
- byte [ ] sharedKey = new byte [ parameterSet . SharedKeyBytes ] ;
67
- Array . Copy ( k , 0 , sharedKey , 0 , parameterSet . SharedKeyBytes ) ;
68
-
53
+ var sharedKey = Arrays . CopyOfRange ( k , 0 , parameterSet . SharedKeyBytes ) ;
69
54
Array . Clear ( k , 0 , k . Length ) ;
70
55
71
56
return sharedKey ;
@@ -80,6 +65,6 @@ private static void Cmov(byte[] r, byte[] x, byte b)
80
65
}
81
66
}
82
67
83
- public int EncapsulationLength => _parameters . ParameterSet . NtruCiphertextBytes ( ) ;
68
+ public int EncapsulationLength => m_privateKey . Parameters . ParameterSet . NtruCiphertextBytes ( ) ;
84
69
}
85
70
}
0 commit comments