|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strconv" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "math/big" |
| 9 | + "github.com/cloudflare/circl/ecc/p384" |
| 10 | +) |
| 11 | + |
| 12 | +import "C" |
| 13 | + |
| 14 | +type ByteSlice []byte |
| 15 | +type Type uint64 |
| 16 | + |
| 17 | +type SliceOpt struct { |
| 18 | + slice ByteSlice |
| 19 | + opt byte |
| 20 | +} |
| 21 | + |
| 22 | +func (t *Type) UnmarshalJSON(in []byte) error { |
| 23 | + res, err := strconv.ParseUint(string(in[1:len(in)-1]), 10, 64) |
| 24 | + *t = Type(res) |
| 25 | + return err |
| 26 | +} |
| 27 | + |
| 28 | +func (b *ByteSlice) MarshalJSON() ([]byte, error) { |
| 29 | + var buffer bytes.Buffer |
| 30 | + buffer.WriteString("\"") |
| 31 | + buffer.WriteString(hex.EncodeToString(*b)) |
| 32 | + buffer.WriteString("\"") |
| 33 | + return buffer.Bytes(), nil |
| 34 | +} |
| 35 | + |
| 36 | +func (b *ByteSlice) UnmarshalJSON(in []byte) error { |
| 37 | + res, err := hex.DecodeString(string(in[1:len(in)-1])) |
| 38 | + *b = res |
| 39 | + return err |
| 40 | +} |
| 41 | + |
| 42 | +func decodeBignum(s string) *big.Int { |
| 43 | + if s == "" { |
| 44 | + s = "0" |
| 45 | + } |
| 46 | + |
| 47 | + bn, ok := new(big.Int).SetString(s, 10) |
| 48 | + if ok == false { |
| 49 | + panic("Cannot decode bignum") |
| 50 | + } |
| 51 | + return bn |
| 52 | +} |
| 53 | + |
| 54 | +type OpECC_Point_Add struct { |
| 55 | + Modifier ByteSlice |
| 56 | + CurveType Type |
| 57 | + A_x string |
| 58 | + A_y string |
| 59 | + B_x string |
| 60 | + B_y string |
| 61 | +} |
| 62 | + |
| 63 | +type OpECC_Point_Mul struct { |
| 64 | + Modifier ByteSlice |
| 65 | + CurveType Type |
| 66 | + A_x string |
| 67 | + A_y string |
| 68 | + B string |
| 69 | +} |
| 70 | + |
| 71 | +type OpECC_Point_Dbl struct { |
| 72 | + Modifier ByteSlice |
| 73 | + CurveType Type |
| 74 | + A_x string |
| 75 | + A_y string |
| 76 | +} |
| 77 | + |
| 78 | +var result []byte |
| 79 | + |
| 80 | +func resetResult() { |
| 81 | + result = []byte{} |
| 82 | +} |
| 83 | + |
| 84 | +func setResult(r ByteSlice) { |
| 85 | + r2, err := json.Marshal(&r) |
| 86 | + if err != nil { |
| 87 | + panic("Cannot marshal to JSON") |
| 88 | + } |
| 89 | + result = r2 |
| 90 | +} |
| 91 | + |
| 92 | +func unmarshal(in []byte, op interface{}) { |
| 93 | + err := json.Unmarshal(in, &op) |
| 94 | + if err != nil { |
| 95 | + panic("Cannot unmarshal JSON, which is expected to be well-formed") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +//export circl_Cryptofuzz_GetResult |
| 100 | +func circl_Cryptofuzz_GetResult() *C.char { |
| 101 | + return C.CString(string(result)) |
| 102 | +} |
| 103 | + |
| 104 | +//export circl_Cryptofuzz_OpECC_Point_Add |
| 105 | +func circl_Cryptofuzz_OpECC_Point_Add(in []byte) { |
| 106 | + resetResult() |
| 107 | + |
| 108 | + var op OpECC_Point_Add |
| 109 | + unmarshal(in, &op) |
| 110 | + |
| 111 | + if !issecp384r1(op.CurveType) { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + curve := p384.P384() |
| 116 | + |
| 117 | + a_x := decodeBignum(op.A_x) |
| 118 | + a_y := decodeBignum(op.A_y) |
| 119 | + |
| 120 | + b_x := decodeBignum(op.B_x) |
| 121 | + b_y := decodeBignum(op.B_y) |
| 122 | + |
| 123 | + res_x, res_y := curve.Add(a_x, a_y, b_x, b_y) |
| 124 | + |
| 125 | + if curve.IsOnCurve(a_x, a_y) == false { |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + if curve.IsOnCurve(b_x, b_y) == false { |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + res := make([]string, 2) |
| 134 | + res[0], res[1] = res_x.String(), res_y.String() |
| 135 | + |
| 136 | + r2, err := json.Marshal(&res) |
| 137 | + if err != nil { |
| 138 | + panic("Cannot marshal to JSON") |
| 139 | + } |
| 140 | + |
| 141 | + result = r2 |
| 142 | +} |
| 143 | + |
| 144 | +//export circl_Cryptofuzz_OpECC_Point_Mul |
| 145 | +func circl_Cryptofuzz_OpECC_Point_Mul(in []byte) { |
| 146 | + resetResult() |
| 147 | + |
| 148 | + var op OpECC_Point_Mul |
| 149 | + unmarshal(in, &op) |
| 150 | + |
| 151 | + if !issecp384r1(op.CurveType) { |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + curve := p384.P384() |
| 156 | + |
| 157 | + a_x := decodeBignum(op.A_x) |
| 158 | + a_y := decodeBignum(op.A_y) |
| 159 | + |
| 160 | + b := decodeBignum(op.B) |
| 161 | + /* https://github.com/cloudflare/circl/issues/312 */ |
| 162 | + order := decodeBignum("39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643") |
| 163 | + if ( b.Cmp(order) >= 0 ) { |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + res_x, res_y := curve.ScalarMult(a_x, a_y, b.Bytes()) |
| 168 | + |
| 169 | + if curve.IsOnCurve(a_x, a_y) == false { |
| 170 | + return |
| 171 | + } |
| 172 | + |
| 173 | + res := make([]string, 2) |
| 174 | + res[0], res[1] = res_x.String(), res_y.String() |
| 175 | + |
| 176 | + r2, err := json.Marshal(&res) |
| 177 | + if err != nil { |
| 178 | + panic("Cannot marshal to JSON") |
| 179 | + } |
| 180 | + |
| 181 | + result = r2 |
| 182 | +} |
| 183 | + |
| 184 | +//export circl_Cryptofuzz_OpECC_Point_Dbl |
| 185 | +func circl_Cryptofuzz_OpECC_Point_Dbl(in []byte) { |
| 186 | + resetResult() |
| 187 | + |
| 188 | + var op OpECC_Point_Dbl |
| 189 | + unmarshal(in, &op) |
| 190 | + |
| 191 | + if !issecp384r1(op.CurveType) { |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + curve := p384.P384() |
| 196 | + |
| 197 | + a_x := decodeBignum(op.A_x) |
| 198 | + a_y := decodeBignum(op.A_y) |
| 199 | + |
| 200 | + res_x, res_y := curve.Double(a_x, a_y) |
| 201 | + |
| 202 | + if curve.IsOnCurve(a_x, a_y) == false { |
| 203 | + return |
| 204 | + } |
| 205 | + |
| 206 | + res := make([]string, 2) |
| 207 | + res[0], res[1] = res_x.String(), res_y.String() |
| 208 | + |
| 209 | + r2, err := json.Marshal(&res) |
| 210 | + if err != nil { |
| 211 | + panic("Cannot marshal to JSON") |
| 212 | + } |
| 213 | + |
| 214 | + result = r2 |
| 215 | +} |
| 216 | + |
| 217 | +func main() { } |
0 commit comments