@@ -146,9 +146,9 @@ static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = {
146146 FALSE,
147147 NULL ,
148148 crypt_sodium_init ,
149- crypt_sodium_encode , crypt_sodium_decode ,
149+ NULL , NULL ,
150150 crypt_sodium_buffer_encode , crypt_sodium_buffer_decode ,
151- crypt_sodium_encode , crypt_sodium_decode ,
151+ NULL , NULL ,
152152 },
153153
154154 // NOTE: when adding a new method, use some random bytes for the magic key,
@@ -250,6 +250,26 @@ crypt_get_header_len(int method_nr)
250250 + cryptmethods [method_nr ].seed_len ;
251251}
252252
253+
254+ /*
255+ * Get maximum crypt method specific length of the file header in bytes.
256+ */
257+ int
258+ crypt_get_max_header_len ()
259+ {
260+ int i ;
261+ int max = 0 ;
262+ int temp = 0 ;
263+
264+ for (i = 0 ; i < CRYPT_M_COUNT ; ++ i )
265+ {
266+ temp = crypt_get_header_len (i );
267+ if (temp > max )
268+ max = temp ;
269+ }
270+ return max ;
271+ }
272+
253273/*
254274 * Set the crypt method for buffer "buf" to "method_nr" using the int value as
255275 * returned by crypt_method_nr_from_name().
@@ -403,8 +423,10 @@ crypt_create_for_writing(
403423#ifdef FEAT_SODIUM
404424 if (sodium_init () >= 0 )
405425 {
406- randombytes_buf (salt , salt_len );
407- randombytes_buf (seed , seed_len );
426+ if (salt_len > 0 )
427+ randombytes_buf (salt , salt_len );
428+ if (seed_len > 0 )
429+ randombytes_buf (seed , seed_len );
408430 }
409431 else
410432#endif
@@ -581,6 +603,13 @@ crypt_check_method(int method)
581603 msg_scroll = TRUE;
582604 msg (_ ("Warning: Using a weak encryption method; see :help 'cm'" ));
583605 }
606+ }
607+
608+ #ifdef FEAT_SODIUM
609+ static void
610+ crypt_check_swapfile_curbuf (void )
611+ {
612+ int method = crypt_get_method_nr (curbuf );
584613 if (method == CRYPT_M_SOD )
585614 {
586615 // encryption uses padding and MAC, that does not work very well with
@@ -590,11 +619,11 @@ crypt_check_method(int method)
590619#ifdef FEAT_PERSISTENT_UNDO
591620 set_option_value ((char_u * )"udf" , 0 , NULL , OPT_LOCAL );
592621#endif
593-
594622 msg_scroll = TRUE;
595623 msg (_ ("Note: Encryption of swapfile not supported, disabling swap- and undofile" ));
596624 }
597625}
626+ #endif
598627
599628 void
600629crypt_check_current_method (void )
@@ -647,17 +676,23 @@ crypt_get_key(
647676 set_option_value ((char_u * )"key" , 0L , p1 , OPT_LOCAL );
648677 crypt_free_key (p1 );
649678 p1 = curbuf -> b_p_key ;
679+ #ifdef FEAT_SODIUM
680+ crypt_check_swapfile_curbuf ();
681+ #endif
650682 }
651683 break ;
652684 }
653685 p2 = p1 ;
654686 }
655687
656688 // since the user typed this, no need to wait for return
657- if (msg_didout )
658- msg_putchar ('\n' );
659- need_wait_return = FALSE;
660- msg_didout = FALSE;
689+ if (crypt_get_method_nr (curbuf ) != CRYPT_M_SOD )
690+ {
691+ if (msg_didout )
692+ msg_putchar ('\n' );
693+ need_wait_return = FALSE;
694+ msg_didout = FALSE;
695+ }
661696
662697 crypt_free_key (p2 );
663698 return p1 ;
@@ -726,6 +761,7 @@ crypt_sodium_init(
726761 * "from" and "to" can be equal to encrypt in place.
727762 * Call needs to ensure that there is enough space in to (for the header)
728763 */
764+ #if 0 // Currently unused
729765 void
730766crypt_sodium_encode (
731767 cryptstate_T * state UNUSED ,
@@ -764,11 +800,13 @@ crypt_sodium_encode(
764800 sod_st -> count ++ ;
765801# endif
766802}
803+ #endif
767804
768- /* TODO: Unused
805+ /*
769806 * Decrypt "from[len]" into "to[len]".
770807 * "from" and "to" can be equal to encrypt in place.
771808 */
809+ #if 0 // Currently unused
772810 void
773811crypt_sodium_decode (
774812 cryptstate_T * state UNUSED ,
@@ -841,6 +879,7 @@ crypt_sodium_decode(
841879 vim_free (buf_out );
842880# endif
843881}
882+ #endif
844883
845884/*
846885 * Encrypt "from[len]" into "to[len]".
@@ -864,7 +903,7 @@ crypt_sodium_buffer_encode(
864903 sodium_state_T * sod_st = state -> method_state ;
865904 int first = (sod_st -> count == 0 );
866905
867- length = len + crypto_secretstream_xchacha20poly1305_ABYTES
906+ length = ( int ) len + crypto_secretstream_xchacha20poly1305_ABYTES
868907 + (first ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0 );
869908 * buf_out = alloc_clear (length );
870909 if (* buf_out == NULL )
0 commit comments