3 * The RSA publickey algorithm, RSAREF compatible interface.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
30 #include "rsa-compat.h"
36 R_SignInit(R_SIGNATURE_CTX
*ctx
,
39 if (digestAlgorithm
!= DA_MD5
)
40 return RE_DIGEST_ALGORITHM
;
48 R_SignUpdate(R_SIGNATURE_CTX
*ctx
,
50 /* Length is an unsigned char according to rsaref.txt,
51 * but that must be a typo. */
54 md5_update(&ctx
->hash
, length
, data
);
60 R_SignFinal(R_SIGNATURE_CTX
*ctx
,
63 R_RSA_PRIVATE_KEY
*key
)
65 struct rsa_private_key k
;
68 nettle_mpz_init_set_str_256_u(k
.p
,
69 MAX_RSA_MODULUS_LEN
, key
->prime
[0]);
70 nettle_mpz_init_set_str_256_u(k
.q
,
71 MAX_RSA_MODULUS_LEN
, key
->prime
[1]);
72 nettle_mpz_init_set_str_256_u(k
.a
,
73 MAX_RSA_MODULUS_LEN
, key
->primeExponent
[0]);
74 nettle_mpz_init_set_str_256_u(k
.b
,
75 MAX_RSA_MODULUS_LEN
, key
->primeExponent
[1]);
76 nettle_mpz_init_set_str_256_u(k
.c
,
77 MAX_RSA_MODULUS_LEN
, key
->coefficient
);
79 if (rsa_private_key_prepare(&k
) && (k
.size
<= MAX_RSA_MODULUS_LEN
))
84 if (rsa_md5_sign(&k
, &ctx
->hash
, s
))
86 nettle_mpz_get_str_256(k
.size
, signature
, s
);
109 R_VerifyInit(R_SIGNATURE_CTX
*ctx
,
112 return R_SignInit(ctx
, digestAlgorithm
);
116 R_VerifyUpdate(R_SIGNATURE_CTX
*ctx
,
118 /* Length is an unsigned char according to rsaref.txt,
119 * but that must be a typo. */
122 return R_SignUpdate(ctx
, data
, length
);
126 R_VerifyFinal(R_SIGNATURE_CTX
*ctx
,
129 R_RSA_PUBLIC_KEY
*key
)
131 struct rsa_public_key k
;
134 nettle_mpz_init_set_str_256_u(k
.n
,
135 MAX_RSA_MODULUS_LEN
, key
->modulus
);
136 nettle_mpz_init_set_str_256_u(k
.e
,
137 MAX_RSA_MODULUS_LEN
, key
->exponent
);
139 if (rsa_public_key_prepare(&k
) && (k
.size
== length
))
143 nettle_mpz_init_set_str_256_u(s
,
145 res
= rsa_md5_verify(&k
, &ctx
->hash
, s
)
146 ? RE_SUCCESS
: RE_SIGNATURE
;