1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Buffers . Binary ;
5
+ using System . Diagnostics ;
6
+ using System . Formats . Cbor ;
7
+ using System . Runtime . Versioning ;
4
8
using System . Text ;
5
9
6
10
namespace System . Security . Cryptography . Cose
@@ -33,6 +37,47 @@ internal static int GetIntegerEncodedSize(long value)
33
37
}
34
38
}
35
39
40
+ internal static void WriteByteStringLength ( IncrementalHash hasher , ulong value )
41
+ {
42
+ const CborMajorType MajorType = CborMajorType . ByteString ;
43
+ CborInitialByte initialByte ;
44
+
45
+ if ( value < ( byte ) CborAdditionalInfo . Additional8BitData )
46
+ {
47
+ initialByte = new CborInitialByte ( MajorType , ( CborAdditionalInfo ) value ) ;
48
+ hasher . AppendData ( stackalloc byte [ ] { initialByte . InitialByte } ) ;
49
+ }
50
+ else if ( value <= byte . MaxValue )
51
+ {
52
+ initialByte = new CborInitialByte ( MajorType , CborAdditionalInfo . Additional8BitData ) ;
53
+ hasher . AppendData ( stackalloc byte [ ] { initialByte . InitialByte , ( byte ) value } ) ;
54
+ }
55
+ else if ( value <= ushort . MaxValue )
56
+ {
57
+ initialByte = new CborInitialByte ( MajorType , CborAdditionalInfo . Additional16BitData ) ;
58
+ Span < byte > buffer = stackalloc byte [ 1 + sizeof ( ushort ) ] ;
59
+ buffer [ 0 ] = initialByte . InitialByte ;
60
+ BinaryPrimitives . WriteUInt16BigEndian ( buffer . Slice ( 1 ) , ( ushort ) value ) ;
61
+ hasher . AppendData ( buffer ) ;
62
+ }
63
+ else if ( value <= uint . MaxValue )
64
+ {
65
+ initialByte = new CborInitialByte ( MajorType , CborAdditionalInfo . Additional32BitData ) ;
66
+ Span < byte > buffer = stackalloc byte [ 1 + sizeof ( uint ) ] ;
67
+ buffer [ 0 ] = initialByte . InitialByte ;
68
+ BinaryPrimitives . WriteUInt32BigEndian ( buffer . Slice ( 1 ) , ( uint ) value ) ;
69
+ hasher . AppendData ( buffer ) ;
70
+ }
71
+ else
72
+ {
73
+ initialByte = new CborInitialByte ( MajorType , CborAdditionalInfo . Additional64BitData ) ;
74
+ Span < byte > buffer = stackalloc byte [ 1 + sizeof ( ulong ) ] ;
75
+ buffer [ 0 ] = initialByte . InitialByte ;
76
+ BinaryPrimitives . WriteUInt64BigEndian ( buffer . Slice ( 1 ) , value ) ;
77
+ hasher . AppendData ( buffer ) ;
78
+ }
79
+ }
80
+
36
81
internal static int GetIntegerEncodedSize ( ulong value )
37
82
{
38
83
if ( value < 24 )
@@ -56,5 +101,56 @@ internal static int GetIntegerEncodedSize(ulong value)
56
101
return 1 + sizeof ( ulong ) ;
57
102
}
58
103
}
104
+
105
+ [ UnsupportedOSPlatform ( "browser" ) ]
106
+ internal static int SignHashWithECDsa ( ECDsa key , IncrementalHash hasher , Span < byte > destination )
107
+ {
108
+ #if NETSTANDARD2_0 || NETFRAMEWORK
109
+ byte [ ] signature = key . SignHash ( hasher . GetHashAndReset ( ) ) ;
110
+ signature . CopyTo ( destination ) ;
111
+ return signature . Length ;
112
+ #else
113
+ Debug . Assert ( hasher . HashLengthInBytes <= 512 / 8 ) ; // largest hash we can get (SHA512).
114
+ Span < byte > hash = stackalloc byte [ hasher . HashLengthInBytes ] ;
115
+ hasher . GetHashAndReset ( hash ) ;
116
+
117
+ if ( ! key . TrySignHash ( hash , destination , out int bytesWritten ) )
118
+ {
119
+ Debug . Fail ( "TrySignData failed with a pre-calculated destination" ) ;
120
+ throw new CryptographicException ( ) ;
121
+ }
122
+
123
+ return bytesWritten ;
124
+ #endif
125
+ }
126
+
127
+ [ UnsupportedOSPlatform ( "browser" ) ]
128
+ internal static int SignHashWithRSA ( RSA key , IncrementalHash hasher , HashAlgorithmName hashAlgorithm , Span < byte > destination )
129
+ {
130
+ #if NETSTANDARD2_0 || NETFRAMEWORK
131
+ byte [ ] signature = key . SignHash ( hasher . GetHashAndReset ( ) , hashAlgorithm , RSASignaturePadding . Pss ) ;
132
+ signature . CopyTo ( destination ) ;
133
+ return signature . Length ;
134
+ #else
135
+ Debug . Assert ( hasher . HashLengthInBytes <= 512 / 8 ) ; // largest hash we can get (SHA512).
136
+ Span < byte > hash = stackalloc byte [ hasher . HashLengthInBytes ] ;
137
+ hasher . GetHashAndReset ( hash ) ;
138
+
139
+ if ( ! key . TrySignHash ( hash , destination , hashAlgorithm , RSASignaturePadding . Pss , out int bytesWritten ) )
140
+ {
141
+ Debug . Fail ( "TrySignData failed with a pre-calculated destination" ) ;
142
+ throw new CryptographicException ( ) ;
143
+ }
144
+
145
+ return bytesWritten ;
146
+ #endif
147
+ }
148
+
149
+ #if NETSTANDARD2_0 || NETFRAMEWORK
150
+ internal static void AppendData ( this IncrementalHash hasher , ReadOnlySpan < byte > data )
151
+ {
152
+ hasher . AppendData ( data . ToArray ( ) ) ;
153
+ }
154
+ #endif
59
155
}
60
156
}
0 commit comments