Fixes https://github.com/heimdal/heimdal/issues/294
[heimdal.git] / lib / hcrypto / rsa.c
blobc99b2b6cbe95da6d908e010a892cf8686929fad2
1 /*
2 * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include <config.h>
35 #include <roken.h>
36 #include <krb5-types.h>
37 #include <rfc2459_asn1.h>
39 #include <der.h>
41 #include <rsa.h>
43 #include "common.h"
45 /**
46 * @page page_rsa RSA - public-key cryptography
48 * RSA is named by its inventors (Ron Rivest, Adi Shamir, and Leonard
49 * Adleman) (published in 1977), patented expired in 21 September 2000.
52 * Speed for RSA in seconds
53 * no key blinding
54 * 1000 iteration,
55 * same rsa keys (1024 and 2048)
56 * operation performed each eteration sign, verify, encrypt, decrypt on a random bit pattern
58 * name 1024 2048 4098
59 * =================================
60 * gmp: 0.73 6.60 44.80
61 * tfm: 2.45 -- --
62 * ltm: 3.79 20.74 105.41 (default in hcrypto)
63 * openssl: 4.04 11.90 82.59
64 * cdsa: 15.89 102.89 721.40
65 * imath: 40.62 -- --
67 * See the library functions here: @ref hcrypto_rsa
70 /**
71 * Same as RSA_new_method() using NULL as engine.
73 * @return a newly allocated RSA object. Free with RSA_free().
75 * @ingroup hcrypto_rsa
78 RSA *
79 RSA_new(void)
81 return RSA_new_method(NULL);
84 /**
85 * Allocate a new RSA object using the engine, if NULL is specified as
86 * the engine, use the default RSA engine as returned by
87 * ENGINE_get_default_RSA().
89 * @param engine Specific what ENGINE RSA provider should be used.
91 * @return a newly allocated RSA object. Free with RSA_free().
93 * @ingroup hcrypto_rsa
96 RSA *
97 RSA_new_method(ENGINE *engine)
99 RSA *rsa;
101 rsa = calloc(1, sizeof(*rsa));
102 if (rsa == NULL)
103 return NULL;
105 rsa->references = 1;
107 if (engine) {
108 ENGINE_up_ref(engine);
109 rsa->engine = engine;
110 } else {
111 rsa->engine = ENGINE_get_default_RSA();
114 if (rsa->engine) {
115 rsa->meth = ENGINE_get_RSA(rsa->engine);
116 if (rsa->meth == NULL) {
117 ENGINE_finish(engine);
118 free(rsa);
119 return 0;
123 if (rsa->meth == NULL)
124 rsa->meth = rk_UNCONST(RSA_get_default_method());
126 (*rsa->meth->init)(rsa);
128 return rsa;
132 * Free an allocation RSA object.
134 * @param rsa the RSA object to free.
135 * @ingroup hcrypto_rsa
138 void
139 RSA_free(RSA *rsa)
141 if (rsa->references <= 0)
142 abort();
144 if (--rsa->references > 0)
145 return;
147 (*rsa->meth->finish)(rsa);
149 if (rsa->engine)
150 ENGINE_finish(rsa->engine);
152 #define free_if(f) if (f) { BN_free(f); }
153 free_if(rsa->n);
154 free_if(rsa->e);
155 free_if(rsa->d);
156 free_if(rsa->p);
157 free_if(rsa->q);
158 free_if(rsa->dmp1);
159 free_if(rsa->dmq1);
160 free_if(rsa->iqmp);
161 #undef free_if
163 memset(rsa, 0, sizeof(*rsa));
164 free(rsa);
168 * Add an extra reference to the RSA object. The object should be free
169 * with RSA_free() to drop the reference.
171 * @param rsa the object to add reference counting too.
173 * @return the current reference count, can't safely be used except
174 * for debug printing.
176 * @ingroup hcrypto_rsa
180 RSA_up_ref(RSA *rsa)
182 return ++rsa->references;
186 * Return the RSA_METHOD used for this RSA object.
188 * @param rsa the object to get the method from.
190 * @return the method used for this RSA object.
192 * @ingroup hcrypto_rsa
195 const RSA_METHOD *
196 RSA_get_method(const RSA *rsa)
198 return rsa->meth;
202 * Set a new method for the RSA keypair.
204 * @param rsa rsa parameter.
205 * @param method the new method for the RSA parameter.
207 * @return 1 on success.
209 * @ingroup hcrypto_rsa
213 RSA_set_method(RSA *rsa, const RSA_METHOD *method)
215 (*rsa->meth->finish)(rsa);
217 if (rsa->engine) {
218 ENGINE_finish(rsa->engine);
219 rsa->engine = NULL;
222 rsa->meth = method;
223 (*rsa->meth->init)(rsa);
224 return 1;
228 * Set the application data for the RSA object.
230 * @param rsa the rsa object to set the parameter for
231 * @param arg the data object to store
233 * @return 1 on success.
235 * @ingroup hcrypto_rsa
239 RSA_set_app_data(RSA *rsa, void *arg)
241 rsa->ex_data.sk = arg;
242 return 1;
246 * Get the application data for the RSA object.
248 * @param rsa the rsa object to get the parameter for
250 * @return the data object
252 * @ingroup hcrypto_rsa
255 void *
256 RSA_get_app_data(const RSA *rsa)
258 return rsa->ex_data.sk;
262 RSA_check_key(const RSA *key)
264 static const unsigned char inbuf[] = "hello, world!";
265 RSA *rsa = rk_UNCONST(key);
266 void *buffer;
267 int ret;
270 * XXX I have no clue how to implement this w/o a bignum library.
271 * Well, when we have a RSA key pair, we can try to encrypt/sign
272 * and then decrypt/verify.
275 if ((rsa->d == NULL || rsa->n == NULL) &&
276 (rsa->p == NULL || rsa->q || rsa->dmp1 == NULL || rsa->dmq1 == NULL || rsa->iqmp == NULL))
277 return 0;
279 buffer = malloc(RSA_size(rsa));
280 if (buffer == NULL)
281 return 0;
283 ret = RSA_private_encrypt(sizeof(inbuf), inbuf, buffer,
284 rsa, RSA_PKCS1_PADDING);
285 if (ret == -1) {
286 free(buffer);
287 return 0;
290 ret = RSA_public_decrypt(ret, buffer, buffer,
291 rsa, RSA_PKCS1_PADDING);
292 if (ret == -1) {
293 free(buffer);
294 return 0;
297 if (ret == sizeof(inbuf) && ct_memcmp(buffer, inbuf, sizeof(inbuf)) == 0) {
298 free(buffer);
299 return 1;
301 free(buffer);
302 return 0;
306 RSA_size(const RSA *rsa)
308 return BN_num_bytes(rsa->n);
311 #define RSAFUNC(name, body) \
312 int \
313 name(int flen,const unsigned char* f, unsigned char* t, RSA* r, int p){\
314 return body; \
317 RSAFUNC(RSA_public_encrypt, (r)->meth->rsa_pub_enc(flen, f, t, r, p))
318 RSAFUNC(RSA_public_decrypt, (r)->meth->rsa_pub_dec(flen, f, t, r, p))
319 RSAFUNC(RSA_private_encrypt, (r)->meth->rsa_priv_enc(flen, f, t, r, p))
320 RSAFUNC(RSA_private_decrypt, (r)->meth->rsa_priv_dec(flen, f, t, r, p))
322 static const heim_octet_string null_entry_oid = { 2, rk_UNCONST("\x05\x00") };
324 static const unsigned sha1_oid_tree[] = { 1, 3, 14, 3, 2, 26 };
325 static const AlgorithmIdentifier _signature_sha1_data = {
326 { 6, rk_UNCONST(sha1_oid_tree) }, rk_UNCONST(&null_entry_oid)
328 static const unsigned sha256_oid_tree[] = { 2, 16, 840, 1, 101, 3, 4, 2, 1 };
329 static const AlgorithmIdentifier _signature_sha256_data = {
330 { 9, rk_UNCONST(sha256_oid_tree) }, rk_UNCONST(&null_entry_oid)
332 static const unsigned md5_oid_tree[] = { 1, 2, 840, 113549, 2, 5 };
333 static const AlgorithmIdentifier _signature_md5_data = {
334 { 6, rk_UNCONST(md5_oid_tree) }, rk_UNCONST(&null_entry_oid)
339 RSA_sign(int type, const unsigned char *from, unsigned int flen,
340 unsigned char *to, unsigned int *tlen, RSA *rsa)
342 if (rsa->meth->rsa_sign)
343 return rsa->meth->rsa_sign(type, from, flen, to, tlen, rsa);
345 if (rsa->meth->rsa_priv_enc) {
346 heim_octet_string indata;
347 DigestInfo di;
348 size_t size;
349 int ret;
351 memset(&di, 0, sizeof(di));
353 if (type == NID_sha1) {
354 di.digestAlgorithm = _signature_sha1_data;
355 } else if (type == NID_md5) {
356 di.digestAlgorithm = _signature_md5_data;
357 } else if (type == NID_sha256) {
358 di.digestAlgorithm = _signature_sha256_data;
359 } else
360 return -1;
362 di.digest.data = rk_UNCONST(from);
363 di.digest.length = flen;
365 ASN1_MALLOC_ENCODE(DigestInfo,
366 indata.data,
367 indata.length,
368 &di,
369 &size,
370 ret);
371 if (ret)
372 return ret;
373 if (indata.length != size)
374 abort();
376 ret = rsa->meth->rsa_priv_enc(indata.length, indata.data, to,
377 rsa, RSA_PKCS1_PADDING);
378 free(indata.data);
379 if (ret > 0) {
380 *tlen = ret;
381 ret = 1;
382 } else
383 ret = 0;
385 return ret;
388 return 0;
392 RSA_verify(int type, const unsigned char *from, unsigned int flen,
393 unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
395 if (rsa->meth->rsa_verify)
396 return rsa->meth->rsa_verify(type, from, flen, sigbuf, siglen, rsa);
398 if (rsa->meth->rsa_pub_dec) {
399 const AlgorithmIdentifier *digest_alg;
400 void *data;
401 DigestInfo di;
402 size_t size;
403 int ret, ret2;
405 data = malloc(RSA_size(rsa));
406 if (data == NULL)
407 return -1;
409 memset(&di, 0, sizeof(di));
411 ret = rsa->meth->rsa_pub_dec(siglen, sigbuf, data, rsa, RSA_PKCS1_PADDING);
412 if (ret <= 0) {
413 free(data);
414 return -2;
417 ret2 = decode_DigestInfo(data, ret, &di, &size);
418 free(data);
419 if (ret2 != 0)
420 return -3;
421 if (ret != size) {
422 free_DigestInfo(&di);
423 return -4;
426 if (flen != di.digest.length || memcmp(di.digest.data, from, flen) != 0) {
427 free_DigestInfo(&di);
428 return -5;
431 if (type == NID_sha1) {
432 digest_alg = &_signature_sha1_data;
433 } else if (type == NID_md5) {
434 digest_alg = &_signature_md5_data;
435 } else if (type == NID_sha256) {
436 digest_alg = &_signature_sha256_data;
437 } else {
438 free_DigestInfo(&di);
439 return -1;
442 ret = der_heim_oid_cmp(&digest_alg->algorithm,
443 &di.digestAlgorithm.algorithm);
444 free_DigestInfo(&di);
446 if (ret != 0)
447 return 0;
448 return 1;
451 return 0;
455 * A NULL RSA_METHOD that returns failure for all operations. This is
456 * used as the default RSA method if we don't have any native
457 * support.
460 static RSAFUNC(null_rsa_public_encrypt, -1)
461 static RSAFUNC(null_rsa_public_decrypt, -1)
462 static RSAFUNC(null_rsa_private_encrypt, -1)
463 static RSAFUNC(null_rsa_private_decrypt, -1)
470 RSA_generate_key_ex(RSA *r, int bits, BIGNUM *e, BN_GENCB *cb)
472 if (r->meth->rsa_keygen)
473 return (*r->meth->rsa_keygen)(r, bits, e, cb);
474 return 0;
482 static int
483 null_rsa_init(RSA *rsa)
485 return 1;
488 static int
489 null_rsa_finish(RSA *rsa)
491 return 1;
494 static const RSA_METHOD rsa_null_method = {
495 "hcrypto null RSA",
496 null_rsa_public_encrypt,
497 null_rsa_public_decrypt,
498 null_rsa_private_encrypt,
499 null_rsa_private_decrypt,
500 NULL,
501 NULL,
502 null_rsa_init,
503 null_rsa_finish,
505 NULL,
506 NULL,
507 NULL,
508 NULL
511 const RSA_METHOD *
512 RSA_null_method(void)
514 return &rsa_null_method;
517 extern const RSA_METHOD hc_rsa_gmp_method;
518 extern const RSA_METHOD hc_rsa_tfm_method;
519 extern const RSA_METHOD hc_rsa_ltm_method;
520 static const RSA_METHOD *default_rsa_method = &hc_rsa_ltm_method;
523 const RSA_METHOD *
524 RSA_get_default_method(void)
526 return default_rsa_method;
529 void
530 RSA_set_default_method(const RSA_METHOD *meth)
532 default_rsa_method = meth;
539 RSA *
540 d2i_RSAPrivateKey(RSA *rsa, const unsigned char **pp, size_t len)
542 RSAPrivateKey data;
543 RSA *k = rsa;
544 size_t size;
545 int ret;
547 ret = decode_RSAPrivateKey(*pp, len, &data, &size);
548 if (ret)
549 return NULL;
551 *pp += size;
553 if (k == NULL) {
554 k = RSA_new();
555 if (k == NULL) {
556 free_RSAPrivateKey(&data);
557 return NULL;
561 k->n = _hc_integer_to_BN(&data.modulus, NULL);
562 k->e = _hc_integer_to_BN(&data.publicExponent, NULL);
563 k->d = _hc_integer_to_BN(&data.privateExponent, NULL);
564 k->p = _hc_integer_to_BN(&data.prime1, NULL);
565 k->q = _hc_integer_to_BN(&data.prime2, NULL);
566 k->dmp1 = _hc_integer_to_BN(&data.exponent1, NULL);
567 k->dmq1 = _hc_integer_to_BN(&data.exponent2, NULL);
568 k->iqmp = _hc_integer_to_BN(&data.coefficient, NULL);
569 free_RSAPrivateKey(&data);
571 if (k->n == NULL || k->e == NULL || k->d == NULL || k->p == NULL ||
572 k->q == NULL || k->dmp1 == NULL || k->dmq1 == NULL || k->iqmp == NULL)
574 RSA_free(k);
575 return NULL;
578 return k;
582 i2d_RSAPrivateKey(RSA *rsa, unsigned char **pp)
584 RSAPrivateKey data;
585 size_t size;
586 int ret;
588 if (rsa->n == NULL || rsa->e == NULL || rsa->d == NULL || rsa->p == NULL ||
589 rsa->q == NULL || rsa->dmp1 == NULL || rsa->dmq1 == NULL ||
590 rsa->iqmp == NULL)
591 return -1;
593 memset(&data, 0, sizeof(data));
595 ret = _hc_BN_to_integer(rsa->n, &data.modulus);
596 ret |= _hc_BN_to_integer(rsa->e, &data.publicExponent);
597 ret |= _hc_BN_to_integer(rsa->d, &data.privateExponent);
598 ret |= _hc_BN_to_integer(rsa->p, &data.prime1);
599 ret |= _hc_BN_to_integer(rsa->q, &data.prime2);
600 ret |= _hc_BN_to_integer(rsa->dmp1, &data.exponent1);
601 ret |= _hc_BN_to_integer(rsa->dmq1, &data.exponent2);
602 ret |= _hc_BN_to_integer(rsa->iqmp, &data.coefficient);
603 if (ret) {
604 free_RSAPrivateKey(&data);
605 return -1;
608 if (pp == NULL) {
609 size = length_RSAPrivateKey(&data);
610 free_RSAPrivateKey(&data);
611 } else {
612 void *p;
613 size_t len;
615 ASN1_MALLOC_ENCODE(RSAPrivateKey, p, len, &data, &size, ret);
616 free_RSAPrivateKey(&data);
617 if (ret)
618 return -1;
619 if (len != size)
620 abort();
622 memcpy(*pp, p, size);
623 free(p);
625 *pp += size;
628 return size;
632 i2d_RSAPublicKey(RSA *rsa, unsigned char **pp)
634 RSAPublicKey data;
635 size_t size;
636 int ret;
638 memset(&data, 0, sizeof(data));
640 if (_hc_BN_to_integer(rsa->n, &data.modulus) ||
641 _hc_BN_to_integer(rsa->e, &data.publicExponent))
643 free_RSAPublicKey(&data);
644 return -1;
647 if (pp == NULL) {
648 size = length_RSAPublicKey(&data);
649 free_RSAPublicKey(&data);
650 } else {
651 void *p;
652 size_t len;
654 ASN1_MALLOC_ENCODE(RSAPublicKey, p, len, &data, &size, ret);
655 free_RSAPublicKey(&data);
656 if (ret)
657 return -1;
658 if (len != size)
659 abort();
661 memcpy(*pp, p, size);
662 free(p);
664 *pp += size;
667 return size;
670 RSA *
671 d2i_RSAPublicKey(RSA *rsa, const unsigned char **pp, size_t len)
673 RSAPublicKey data;
674 RSA *k = rsa;
675 size_t size;
676 int ret;
678 ret = decode_RSAPublicKey(*pp, len, &data, &size);
679 if (ret)
680 return NULL;
682 *pp += size;
684 if (k == NULL) {
685 k = RSA_new();
686 if (k == NULL) {
687 free_RSAPublicKey(&data);
688 return NULL;
692 k->n = _hc_integer_to_BN(&data.modulus, NULL);
693 k->e = _hc_integer_to_BN(&data.publicExponent, NULL);
695 free_RSAPublicKey(&data);
697 if (k->n == NULL || k->e == NULL) {
698 RSA_free(k);
699 return NULL;
702 return k;