33import org .biscuitsec .biscuit .error .Error ;
44import io .vavr .control .Either ;
55
6- import java .nio . ByteBuffer ;
7- import java .nio . ByteOrder ;
8- import java .security .* ;
6+ import java .security . InvalidKeyException ;
7+ import java .security . NoSuchAlgorithmException ;
8+ import java .security .SignatureException ;
99import java .util .ArrayList ;
1010
1111import static io .vavr .API .Left ;
@@ -17,17 +17,11 @@ class Token {
1717 public final ArrayList <byte []> signatures ;
1818 public final KeyPair next ;
1919
20- public Token (KeyPair rootKeyPair , byte [] message , KeyPair next ) throws NoSuchAlgorithmException , InvalidKeyException , SignatureException {
21- Signature sgr = KeyPair .generateSignature (next .public_key ().algorithm );
22- ByteBuffer algo_buf = ByteBuffer .allocate (4 ).order (ByteOrder .LITTLE_ENDIAN );
23- algo_buf .putInt (Integer .valueOf (next .public_key ().algorithm .getNumber ()));
24- algo_buf .flip ();
25- sgr .initSign (rootKeyPair .private_key ());
26- sgr .update (message );
27- sgr .update (algo_buf );
28- sgr .update (next .public_key ().toBytes ());
20+ public Token (final Signer rootSigner , byte [] message , KeyPair next ) throws NoSuchAlgorithmException , InvalidKeyException , SignatureException {
2921
30- byte [] signature = sgr .sign ();
22+ byte [] payload = BlockSignatureBuffer .getBufferSignature (next .public_key (), message );
23+
24+ byte [] signature = rootSigner .sign (payload );
3125
3226 this .blocks = new ArrayList <>();
3327 this .blocks .add (message );
@@ -47,16 +41,8 @@ public Token(final ArrayList<byte[]> blocks, final ArrayList<PublicKey> keys, fi
4741 }
4842
4943 public Token append (KeyPair keyPair , byte [] message ) throws NoSuchAlgorithmException , SignatureException , InvalidKeyException {
50- Signature sgr = KeyPair .generateSignature (next .public_key ().algorithm );
51- sgr .initSign (this .next .private_key ());
52- ByteBuffer algo_buf = ByteBuffer .allocate (4 ).order (ByteOrder .LITTLE_ENDIAN );
53- algo_buf .putInt (Integer .valueOf (next .public_key ().algorithm .getNumber ()));
54- algo_buf .flip ();
55- sgr .update (message );
56- sgr .update (algo_buf );
57- sgr .update (keyPair .public_key ().toBytes ());
58-
59- byte [] signature = sgr .sign ();
44+ byte [] payload = BlockSignatureBuffer .getBufferSignature (keyPair .public_key (), message );
45+ byte [] signature = this .next .sign (payload );
6046
6147 Token token = new Token (this .blocks , this .keys , this .signatures , keyPair );
6248 token .blocks .add (message );
@@ -74,23 +60,15 @@ public Either<Error, Void> verify(PublicKey root) throws NoSuchAlgorithmExceptio
7460 PublicKey next_key = this .keys .get (i );
7561 byte [] signature = this .signatures .get (i );
7662
77- ByteBuffer algo_buf = ByteBuffer .allocate (4 ).order (ByteOrder .LITTLE_ENDIAN );
78- algo_buf .putInt (Integer .valueOf (next .public_key ().algorithm .getNumber ()));
79- algo_buf .flip ();
80- Signature sgr = KeyPair .generateSignature (next .public_key ().algorithm );
81- sgr .initVerify (current_key .key );
82- sgr .update (block );
83- sgr .update (algo_buf );
84- sgr .update (next_key .toBytes ());
85-
86- if (sgr .verify (signature )) {
63+ byte [] payload = BlockSignatureBuffer .getBufferSignature (next_key , block );
64+ if (KeyPair .verify (current_key , payload , signature )) {
8765 current_key = next_key ;
8866 } else {
8967 return Left (new Error .FormatError .Signature .InvalidSignature ("signature error: Verification equation was not satisfied" ));
9068 }
9169 }
9270
93- if (this .next .publicKey () == current_key . key ) {
71+ if (this .next .public_key (). equals ( current_key ) ) {
9472 return Right (null );
9573 } else {
9674 return Left (new Error .FormatError .Signature .InvalidSignature ("signature error: Verification equation was not satisfied" ));
0 commit comments