| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 /* C port of DumpPublicKey.java from the Android Open source project with | 6 /* C port of DumpPublicKey.java from the Android Open source project with |
| 7 * support for additional RSA key sizes. (platform/system/core,git/libmincrypt | 7 * support for additional RSA key sizes. (platform/system/core,git/libmincrypt |
| 8 * /tools/DumpPublicKey.java). Uses the OpenSSL X509 and BIGNUM library. | 8 * /tools/DumpPublicKey.java). Uses the OpenSSL X509 and BIGNUM library. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 BIGNUM *Big1, *Big2, *Big32, *BigMinus1; | 46 BIGNUM *Big1, *Big2, *Big32, *BigMinus1; |
| 47 BIGNUM *B; | 47 BIGNUM *B; |
| 48 BIGNUM *N0inv, *R, *RR, *RRTemp, *NnumBits; | 48 BIGNUM *N0inv, *R, *RR, *RRTemp, *NnumBits; |
| 49 BIGNUM *n, *rr; | 49 BIGNUM *n, *rr; |
| 50 BN_CTX *bn_ctx = BN_CTX_new(); | 50 BN_CTX *bn_ctx = BN_CTX_new(); |
| 51 uint32_t n0invout; | 51 uint32_t n0invout; |
| 52 | 52 |
| 53 N = key->n; | 53 N = key->n; |
| 54 /* Output size of RSA key in 32-bit words */ | 54 /* Output size of RSA key in 32-bit words */ |
| 55 nwords = BN_num_bits(N) / 32; | 55 nwords = BN_num_bits(N) / 32; |
| 56 write(1, &nwords, sizeof(nwords)); | 56 if (-1 == write(1, &nwords, sizeof(nwords))) |
| 57 goto failure; |
| 58 |
| 57 | 59 |
| 58 /* Initialize BIGNUMs */ | 60 /* Initialize BIGNUMs */ |
| 59 Big1 = BN_new(); | 61 Big1 = BN_new(); |
| 60 Big2 = BN_new(); | 62 Big2 = BN_new(); |
| 61 Big32 = BN_new(); | 63 Big32 = BN_new(); |
| 62 BigMinus1 = BN_new(); | 64 BigMinus1 = BN_new(); |
| 63 N0inv= BN_new(); | 65 N0inv= BN_new(); |
| 64 R = BN_new(); | 66 R = BN_new(); |
| 65 RR = BN_new(); | 67 RR = BN_new(); |
| 66 RRTemp = BN_new(); | 68 RRTemp = BN_new(); |
| 67 NnumBits = BN_new(); | 69 NnumBits = BN_new(); |
| 68 n = BN_new(); | 70 n = BN_new(); |
| 69 rr = BN_new(); | 71 rr = BN_new(); |
| 70 | 72 |
| 71 | 73 |
| 72 BN_set_word(Big1, 1L); | 74 BN_set_word(Big1, 1L); |
| 73 BN_set_word(Big2, 2L); | 75 BN_set_word(Big2, 2L); |
| 74 BN_set_word(Big32, 32L); | 76 BN_set_word(Big32, 32L); |
| 75 BN_sub(BigMinus1, Big1, Big2); | 77 BN_sub(BigMinus1, Big1, Big2); |
| 76 | 78 |
| 77 B = BN_new(); | 79 B = BN_new(); |
| 78 BN_exp(B, Big2, Big32, bn_ctx); /* B = 2^32 */ | 80 BN_exp(B, Big2, Big32, bn_ctx); /* B = 2^32 */ |
| 79 | 81 |
| 80 /* Calculate and output N0inv = -1 / N[0] mod 2^32 */ | 82 /* Calculate and output N0inv = -1 / N[0] mod 2^32 */ |
| 81 BN_mod_inverse(N0inv, N, B, bn_ctx); | 83 BN_mod_inverse(N0inv, N, B, bn_ctx); |
| 82 BN_sub(N0inv, B, N0inv); | 84 BN_sub(N0inv, B, N0inv); |
| 83 n0invout = BN_get_word(N0inv); | 85 n0invout = BN_get_word(N0inv); |
| 84 write(1, &n0invout, sizeof(n0invout)); | 86 if (-1 == write(1, &n0invout, sizeof(n0invout))) |
| 87 goto failure; |
| 85 | 88 |
| 86 /* Calculate R = 2^(# of key bits) */ | 89 /* Calculate R = 2^(# of key bits) */ |
| 87 BN_set_word(NnumBits, BN_num_bits(N)); | 90 BN_set_word(NnumBits, BN_num_bits(N)); |
| 88 BN_exp(R, Big2, NnumBits, bn_ctx); | 91 BN_exp(R, Big2, NnumBits, bn_ctx); |
| 89 | 92 |
| 90 /* Calculate RR = R^2 mod N */ | 93 /* Calculate RR = R^2 mod N */ |
| 91 BN_copy(RR, R); | 94 BN_copy(RR, R); |
| 92 BN_mul(RRTemp, RR, R, bn_ctx); | 95 BN_mul(RRTemp, RR, R, bn_ctx); |
| 93 BN_mod(RR, RRTemp, N, bn_ctx); | 96 BN_mod(RR, RRTemp, N, bn_ctx); |
| 94 | 97 |
| 95 | 98 |
| 96 /* Write out modulus as little endian array of integers. */ | 99 /* Write out modulus as little endian array of integers. */ |
| 97 for (i = 0; i < nwords; ++i) { | 100 for (i = 0; i < nwords; ++i) { |
| 98 uint32_t nout; | 101 uint32_t nout; |
| 99 | 102 |
| 100 BN_mod(n, N, B, bn_ctx); /* n = N mod B */ | 103 BN_mod(n, N, B, bn_ctx); /* n = N mod B */ |
| 101 nout = BN_get_word(n); | 104 nout = BN_get_word(n); |
| 102 write(1, &nout, sizeof(nout)); | 105 if (-1 == write(1, &nout, sizeof(nout))) |
| 106 goto failure; |
| 103 | 107 |
| 104 BN_rshift(N, N, 32); /* N = N/B */ | 108 BN_rshift(N, N, 32); /* N = N/B */ |
| 105 } | 109 } |
| 106 | 110 |
| 107 /* Write R^2 as little endian array of integers. */ | 111 /* Write R^2 as little endian array of integers. */ |
| 108 for (i = 0; i < nwords; ++i) { | 112 for (i = 0; i < nwords; ++i) { |
| 109 uint32_t rrout; | 113 uint32_t rrout; |
| 110 | 114 |
| 111 BN_mod(rr, RR, B, bn_ctx); /* rr = RR mod B */ | 115 BN_mod(rr, RR, B, bn_ctx); /* rr = RR mod B */ |
| 112 rrout = BN_get_word(rr); | 116 rrout = BN_get_word(rr); |
| 113 write(1, &rrout, sizeof(rrout)); | 117 if (-1 == write(1, &rrout, sizeof(rrout))) |
| 118 goto failure; |
| 114 | 119 |
| 115 BN_rshift(RR, RR, 32); /* RR = RR/B */ | 120 BN_rshift(RR, RR, 32); /* RR = RR/B */ |
| 116 } | 121 } |
| 117 | 122 |
| 123 failure: |
| 118 /* Free BIGNUMs. */ | 124 /* Free BIGNUMs. */ |
| 119 BN_free(Big1); | 125 BN_free(Big1); |
| 120 BN_free(Big2); | 126 BN_free(Big2); |
| 121 BN_free(Big32); | 127 BN_free(Big32); |
| 122 BN_free(BigMinus1); | 128 BN_free(BigMinus1); |
| 123 BN_free(N0inv); | 129 BN_free(N0inv); |
| 124 BN_free(R); | 130 BN_free(R); |
| 125 BN_free(RRTemp); | 131 BN_free(RRTemp); |
| 126 BN_free(NnumBits); | 132 BN_free(NnumBits); |
| 127 BN_free(n); | 133 BN_free(n); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 output (pubkey); | 172 output (pubkey); |
| 167 } | 173 } |
| 168 | 174 |
| 169 fail: | 175 fail: |
| 170 X509_free(cert); | 176 X509_free(cert); |
| 171 RSA_free(pubkey); | 177 RSA_free(pubkey); |
| 172 fclose(fp); | 178 fclose(fp); |
| 173 | 179 |
| 174 return 0; | 180 return 0; |
| 175 } | 181 } |
| OLD | NEW |