@@ -83,15 +83,50 @@ RSA exponentiation step when only public key is available.
83
83
Uses [repeated squares](https://en.wikipedia.org/wiki/Exponentiation_by_squaring)
84
84
and other fast modulo exponentiation tricks in its GMP implementation (Base.GMP.MPZ.powm).
85
85
"""
86
- function RSADP (:: pkcs1_v1_5_t , msg:: BigInt , key:: RSAPublicKey )
86
+ function RSADP (v:: pkcs1_v1_5_t , msg:: BigInt , key:: RSAPublicKey )
87
+ return RSAStep (v, msg, key)
88
+ end
89
+
90
+ """
91
+ RSASP1(::pkcs1_v1_5_t, msg::BigInt, key::RSAPrivateKey)
92
+
93
+ """
94
+ function RSASP1 (v:: pkcs1_v1_5_t , msg:: BigInt , key:: RSAPrivateKey )
95
+ return RSAStep (v, msg, key)
96
+ end
97
+
98
+ """
99
+ RSAVP1(::pkcs1_v1_5_t, msg::BigInt, key::RSAPublicKey)
100
+
101
+ """
102
+ function RSAVP1 (v:: pkcs1_v1_5_t , msg:: BigInt , key:: RSAPublicKey )
103
+ return RSAStep (v, msg, key)
104
+ end
105
+
106
+ function RSAStep (:: pkcs1_v1_5_t , msg:: BigInt , key:: RSAPrivateKey )
87
107
if ! (0 <= msg < key. modulus)
88
108
error (" msg has to be 0 <= msg < n, got: msg = $msg , n = $key .modulus" )
89
109
end
90
- return Base. GMP. MPZ. powm (msg, key. exponent, key. modulus)
110
+ ret = power_crt (
111
+ msg,
112
+ key. primes[1 ],
113
+ key. primes[2 ],
114
+ key. crt_exponents[1 ],
115
+ key. crt_exponents[2 ],
116
+ key. crt_coefficients[1 ],
117
+ )
118
+ ret < 0 && (ret += key. modulus)
119
+ return ret
91
120
end
92
121
93
- RSAStep (a:: pkcs1_v1_5_t , msg:: BigInt , key:: RSAPrivateKey ) = RSAEP (a, msg, key)
94
- RSAStep (a:: pkcs1_v1_5_t , msg:: BigInt , key:: RSAPublicKey ) = RSADP (a, msg, key)
122
+ function RSAStep (:: pkcs1_v1_5_t , msg:: BigInt , key:: RSAPublicKey )
123
+ if ! (0 <= msg < key. modulus)
124
+ error (" msg has to be 0 <= msg < n, got: msg = $msg , n = $key .modulus" )
125
+ end
126
+ ret = Base. GMP. MPZ. powm (msg, key. exponent, key. modulus)
127
+ ret < 0 && (ret += key. modulus)
128
+ return ret
129
+ end
95
130
96
131
"""
97
132
RSAStep(::pkcs1_v1_5_t, msg::AbstractVector{T}, key::RSAKey) where {T<:Base.BitInteger}
@@ -130,6 +165,38 @@ function RSAStep(::pkcs1_v1_5_t, msg::String, key::RSAKey)
130
165
return transformed_msg
131
166
end
132
167
168
+ function rsaes_oaep_encrypt (M:: String , key:: RSAPublicKey ; label= " " , hash= SHA. sha1, MGF= MGF1)
169
+ EM = pad (pkcs1_v2_2, M, key, label= label, hash= hash, MGF= MGF)
170
+ m = OS2IP (EM)
171
+ c = RSAEP (pkcs1_v1_5, m, key)
172
+ C = I2OSP (c, k)
173
+ return C
174
+ end
175
+
176
+ function rsaes_oaep_decrypt (C:: String , key:: RSAPublicKey ; label= " " , hash= SHA. sha1, MGF= MGF1)
177
+ c = OS2IP (C)
178
+ m = RSADP (pkcs1_v1_5, c, key)
179
+ EM = I2OSP (m, k)
180
+ M = unpad (pkcs1_v2_2, EM, key, label= label, hash= hash, MGF= MGF)
181
+ return M
182
+ end
183
+
184
+ function rsaes_pkvs1_v1_5_encrypt (M:: String , key:: RSAPublicKey )
185
+ EM = pad (pkcs1_v1_5, M)
186
+ m = OS2IP (EM)
187
+ c = RSAEP (pkcs1_v1_5, m, key)
188
+ C = I2OSP (c)
189
+ return C
190
+ end
191
+
192
+ function rsaes_pkvs1_v1_5_decrypt (C:: String , key:: RSAPrivateKey )
193
+ c = OS2IP (C)
194
+ m = RSADP (pkcs1_v1_5, c, key)
195
+ EM = I2OSP (m)
196
+ m = unpad (pkcs1_v1_5, EM)
197
+ return m
198
+ end
199
+
133
200
"""
134
201
encrypt(::pkcs1_v1_5_t,
135
202
msg::Union{AbstractString,AbstractVector},
207
274
208
275
Sign string with RSA key.
209
276
"""
210
- function sign (:: pkcs1_v1_5_t , msg:: String , key:: RSAPrivateKey ; pad_length= 32 )
211
- digest = SHA. sha256 (msg)
277
+ function sign (:: pkcs1_v1_5_t , msg:: String , key:: RSAPrivateKey ; pad_length= 32 , hash= SHA. sha1)
278
+ error (" needs reimplementing to be pkcs1_v1_5_t compliant" ) |> throw
279
+ digest = hash (msg)
212
280
msg_padded = ToyPublicKeys. pad (pkcs1_v1_5, digest, pad_length)
213
281
return String (RSAStep (pkcs1_v1_5, msg_padded, key))
214
282
end
218
286
219
287
Sign AbstractVector (arbitrary buffer using [SHA256](https://en.wikipedia.org/wiki/SHA-2)) with RSA key.
220
288
"""
221
- function sign (:: pkcs1_v1_5_t , msg:: AbstractVector , key:: RSAPrivateKey ; pad_length= 32 )
222
- digest = SHA. sha256 (String (msg))
289
+ function sign (:: pkcs1_v1_5_t , msg:: AbstractVector , key:: RSAPrivateKey ; pad_length= 32 , hash= SHA. sha1)
290
+ error (" needs reimplementing to be pkcs1_v1_5_t compliant" ) |> throw
291
+ digest = hash (String (msg))
223
292
msg_padded = ToyPublicKeys. pad (pkcs1_v1_5, digest, pad_length)
224
293
return RSAStep (pkcs1_v1_5, msg_padded, key)
225
294
end
@@ -229,10 +298,11 @@ end
229
298
230
299
Verify the signature.
231
300
"""
232
- function verify_signature (:: pkcs1_v1_5_t , msg:: String , signature:: String , key:: RSAPublicKey )
301
+ function verify_signature (:: pkcs1_v1_5_t , msg:: String , signature:: String , key:: RSAPublicKey , hash= SHA. sha1)
302
+ error (" needs reimplementing to be pkcs1_v1_5_t compliant" ) |> throw
233
303
signature_ = codeunits (signature)
234
304
signature_decr = ToyPublicKeys. RSAStep (pkcs1_v1_5, signature_, key)
235
305
unpaded_hash = ToyPublicKeys. unpad (pkcs1_v1_5, vcat (typeof (signature_decr)([0 ]), signature_decr)) # todo: leading zero is ignored, gotta deal with this
236
- digest = SHA . sha256 (msg)
306
+ digest = hash (msg)
237
307
return unpaded_hash == digest
238
308
end
0 commit comments