| 1 | /* |
| 2 | * Copyright (c) 1997-1999 The Stanford SRP Authentication Project |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be |
| 14 | * included in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 18 | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 19 | * |
| 20 | * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL, |
| 21 | * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER |
| 22 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF |
| 23 | * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT |
| 24 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 25 | * |
| 26 | * In addition, the following conditions apply: |
| 27 | * |
| 28 | * 1. Any software that incorporates the SRP authentication technology |
| 29 | * must display the following acknowlegment: |
| 30 | * "This product uses the 'Secure Remote Password' cryptographic |
| 31 | * authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)." |
| 32 | * |
| 33 | * 2. Any software that incorporates all or part of the SRP distribution |
| 34 | * itself must also display the following acknowledgment: |
| 35 | * "This product includes software developed by Tom Wu and Eugene |
| 36 | * Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)." |
| 37 | * |
| 38 | * 3. Redistributions in source or binary form must retain an intact copy |
| 39 | * of this copyright notice and list of conditions. |
| 40 | */ |
| 41 | |
| 42 | #include <stdio.h> |
| 43 | #include <sys/types.h> |
| 44 | |
| 45 | #include "config.h" |
| 46 | |
| 47 | #include "bn.h" |
| 48 | typedef BIGNUM * BigInteger; |
| 49 | #define MATH_PRIV |
| 50 | |
| 51 | #include "t_defines.h" |
| 52 | #include "t_pwd.h" |
| 53 | |
| 54 | /* Math library interface stubs */ |
| 55 | |
| 56 | BigInteger |
| 57 | BigIntegerFromInt(n) |
| 58 | unsigned int n; |
| 59 | { |
| 60 | BIGNUM * a = BN_new(); |
| 61 | BN_set_word(a, n); |
| 62 | return a; |
| 63 | } |
| 64 | |
| 65 | BigInteger |
| 66 | BigIntegerFromBytes(bytes, length) |
| 67 | unsigned char * bytes; |
| 68 | int length; |
| 69 | { |
| 70 | BIGNUM * a = BN_new(); |
| 71 | BN_bin2bn(bytes, length, a); |
| 72 | return a; |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | BigIntegerToBytes(src, dest) |
| 77 | BigInteger src; |
| 78 | unsigned char * dest; |
| 79 | { |
| 80 | return BN_bn2bin(src, dest); |
| 81 | } |
| 82 | |
| 83 | int |
| 84 | BigIntegerCmp(c1, c2) |
| 85 | BigInteger c1, c2; |
| 86 | { |
| 87 | return BN_cmp(c1, c2); |
| 88 | } |
| 89 | |
| 90 | int |
| 91 | BigIntegerCmpInt(c1, c2) |
| 92 | BigInteger c1; |
| 93 | unsigned int c2; |
| 94 | { |
| 95 | BIGNUM * a = BN_new(); |
| 96 | int rv; |
| 97 | BN_set_word(a, c2); |
| 98 | rv = BN_cmp(c1, a); |
| 99 | BN_free(a); |
| 100 | return rv; |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | BigIntegerAdd(result, a1, a2) |
| 105 | BigInteger result, a1, a2; |
| 106 | { |
| 107 | BN_add(result, a1, a2); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | BigIntegerAddInt(result, a1, a2) |
| 112 | BigInteger result, a1; |
| 113 | unsigned int a2; |
| 114 | { |
| 115 | BIGNUM * a = BN_new(); |
| 116 | BN_set_word(a, a2); |
| 117 | BN_add(result, a1, a); |
| 118 | BN_free(a); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | BigIntegerSub(result, s1, s2) |
| 123 | BigInteger result, s1, s2; |
| 124 | { |
| 125 | BN_sub(result, s1, s2); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | BigIntegerMulInt(result, m1, m2) |
| 130 | BigInteger result, m1; |
| 131 | unsigned int m2; |
| 132 | { |
| 133 | BN_CTX * ctx = BN_CTX_new(); |
| 134 | BIGNUM * m = BN_new(); |
| 135 | BN_set_word(m, m2); |
| 136 | BN_mul(result, m1, m, ctx); |
| 137 | BN_CTX_free(ctx); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | BigIntegerModMul(r, m1, m2, modulus) |
| 142 | BigInteger r, m1, m2, modulus; |
| 143 | { |
| 144 | BN_CTX * ctx = BN_CTX_new(); |
| 145 | BN_mod_mul(r, m1, m2, modulus, ctx); |
| 146 | BN_CTX_free(ctx); |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | BigIntegerModExp(r, b, e, m) |
| 151 | BigInteger r, b, e, m; |
| 152 | { |
| 153 | BN_CTX * ctx = BN_CTX_new(); |
| 154 | BN_mod_exp(r, b, e, m, ctx); |
| 155 | BN_CTX_free(ctx); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | BigIntegerModExpInt(r, b, e, m) |
| 160 | BigInteger r, b; |
| 161 | unsigned int e; |
| 162 | BigInteger m; |
| 163 | { |
| 164 | BN_CTX * ctx = BN_CTX_new(); |
| 165 | BIGNUM * p = BN_new(); |
| 166 | BN_set_word(p, e); |
| 167 | BN_mod_exp(r, b, p, m, ctx); |
| 168 | BN_free(p); |
| 169 | BN_CTX_free(ctx); |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | BigIntegerFree(b) |
| 174 | BigInteger b; |
| 175 | { |
| 176 | BN_free(b); |
| 177 | } |
| 178 | |