]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec.c
Move ec.h to ec2.h because it is not compatible with what we will use.
[thirdparty/openssl.git] / crypto / ec / ec.c
CommitLineData
6cc5e19d
BM
1/*
2 *
3 * ec.c
4 *
5 * Elliptic Curve Arithmetic Functions
6 *
7 * Copyright (C) Lenka Fibikova 2000
8 *
9 *
10 */
11
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <assert.h>
16
65e81670 17#include "ec2.h"
6cc5e19d
BM
18
19
20
21EC *EC_new()
22{
23 EC *ret;
24
25 ret=(EC *)malloc(sizeof(EC));
26 if (ret == NULL) return NULL;
27 ret->A = BN_new();
28 ret->B = BN_new();
29 ret->p = BN_new();
6cc5e19d
BM
30 ret->is_in_mont = 0;
31
786e0c24 32 if (ret->A == NULL || ret->B == NULL || ret->p == NULL)
6cc5e19d
BM
33 {
34 if (ret->A != NULL) BN_free(ret->A);
35 if (ret->B != NULL) BN_free(ret->B);
36 if (ret->p != NULL) BN_free(ret->p);
6cc5e19d
BM
37 free(ret);
38 return(NULL);
39 }
40 return(ret);
41}
42
43
44void EC_clear_free(EC *E)
45{
46 if (E == NULL) return;
47
48 if (E->A != NULL) BN_clear_free(E->A);
49 if (E->B != NULL) BN_clear_free(E->B);
50 if (E->p != NULL) BN_clear_free(E->p);
6cc5e19d
BM
51 E->is_in_mont = 0;
52 free(E);
53}
54
55
56#ifdef MONTGOMERY
57int EC_to_montgomery(EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
58{
59 assert(E != NULL);
786e0c24 60 assert(E->A != NULL && E->B != NULL && E->p != NULL);
6cc5e19d
BM
61
62 assert(mont != NULL);
63 assert(mont->p != NULL);
64
65 assert(ctx != NULL);
66
67 if (E->is_in_mont) return 1;
68
69 if (!BN_lshift(E->A, E->A, mont->R_num_bits)) return 0;
70 if (!BN_mod(E->A, E->A, mont->p, ctx)) return 0;
71
72 if (!BN_lshift(E->B, E->B, mont->R_num_bits)) return 0;
73 if (!BN_mod(E->B, E->B, mont->p, ctx)) return 0;
74
6cc5e19d
BM
75 E->is_in_mont = 1;
76 return 1;
77
78}
79
80
81int EC_from_montgomery(EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
82{
83 assert(E != NULL);
786e0c24 84 assert(E->A != NULL && E->B != NULL && E->p != NULL);
6cc5e19d
BM
85
86 assert(mont != NULL);
87 assert(mont->p != NULL);
88
89 assert(ctx != NULL);
90
91 if (!E->is_in_mont) return 1;
92
db5bda67
BM
93 if (!BN_mont_red(E->A, mont)) return 0;
94 if (!BN_mont_red(E->B, mont)) return 0;
6cc5e19d
BM
95
96 E->is_in_mont = 0;
97 return 1;
98}
99#endif /* MONTGOMERY */